fimc-is2: fix sizeof pointer use
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / media / platform / exynos / fimc-is2 / sensor / module_framework / cis / fimc-is-cis-2x5sp.c
1 /*
2 * Samsung Exynos5 SoC series Sensor driver
3 *
4 *
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/version.h>
18 #include <linux/gpio.h>
19 #include <linux/clk.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/videodev2.h>
22 #include <linux/videodev2_exynos_camera.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/platform_device.h>
26 #include <linux/of_gpio.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-subdev.h>
30
31 #include <exynos-fimc-is-sensor.h>
32 #include "fimc-is-hw.h"
33 #include "fimc-is-core.h"
34 #include "fimc-is-param.h"
35 #include "fimc-is-device-sensor.h"
36 #include "fimc-is-device-sensor-peri.h"
37 #include "fimc-is-resourcemgr.h"
38 #include "fimc-is-dt.h"
39 #include "fimc-is-cis-2x5sp.h"
40 #include "fimc-is-cis-2x5sp-setA.h"
41 #include "fimc-is-cis-2x5sp-setB.h"
42
43 #include "fimc-is-helper-i2c.h"
44
45 #define SENSOR_NAME "S5K2X5SP"
46 /* #define DEBUG_2X5SP_PLL */
47
48 static const struct v4l2_subdev_ops subdev_ops;
49
50 static const u32 *sensor_2x5sp_global;
51 static u32 sensor_2x5sp_global_size;
52 static const u32 **sensor_2x5sp_setfiles;
53 static const u32 *sensor_2x5sp_setfile_sizes;
54 static const u32 *sensor_2x5sp_otp_initial;
55 static u32 sensor_2x5sp_otp_initial_size;
56 static const struct sensor_pll_info_compact **sensor_2x5sp_pllinfos;
57 static u32 sensor_2x5sp_max_setfile_num;
58
59 static const u32 *sensor_2x5sp_setfile_throttling;
60 static u32 sensor_2x5sp_setfile_throttling_size;
61 static const struct sensor_pll_info_compact *sensor_2x5sp_pllinfo_throttling;
62
63 static void sensor_2x5sp_cis_data_calculation(const struct sensor_pll_info_compact *pll_info_compact, cis_shared_data *cis_data)
64 {
65 u32 vt_pix_clk_hz = 0;
66 u32 frame_rate = 0, max_fps = 0, frame_valid_us = 0;
67
68 FIMC_BUG_VOID(!pll_info_compact);
69
70 /* 1. get pclk value from pll info */
71 vt_pix_clk_hz = pll_info_compact->pclk;
72
73 dbg_sensor(1, "ext_clock(%d), mipi_datarate(%d), pclk(%d)\n",
74 pll_info_compact->ext_clk, pll_info_compact->mipi_datarate, pll_info_compact->pclk);
75
76 /* 2. the time of processing one frame calculation (us) */
77 cis_data->min_frame_us_time = (pll_info_compact->frame_length_lines * pll_info_compact->line_length_pck
78 / (vt_pix_clk_hz / (1000 * 1000)));
79 cis_data->cur_frame_us_time = cis_data->min_frame_us_time;
80
81 /* 3. FPS calculation */
82 frame_rate = vt_pix_clk_hz / (pll_info_compact->frame_length_lines * pll_info_compact->line_length_pck);
83 dbg_sensor(1, "frame_rate (%d) = vt_pix_clk_hz(%d) / "
84 KERN_CONT "(pll_info_compact->frame_length_lines(%d) * pll_info_compact->line_length_pck(%d))\n",
85 frame_rate, vt_pix_clk_hz, pll_info_compact->frame_length_lines, pll_info_compact->line_length_pck);
86
87 /* calculate max fps */
88 max_fps = (vt_pix_clk_hz * 10) / (pll_info_compact->frame_length_lines * pll_info_compact->line_length_pck);
89 max_fps = (max_fps % 10 >= 5 ? frame_rate + 1 : frame_rate);
90
91 cis_data->pclk = vt_pix_clk_hz;
92 cis_data->max_fps = max_fps;
93 cis_data->frame_length_lines = pll_info_compact->frame_length_lines;
94 cis_data->line_length_pck = pll_info_compact->line_length_pck;
95 cis_data->line_readOut_time = sensor_cis_do_div64((u64)cis_data->line_length_pck * (u64)(1000 * 1000 * 1000), cis_data->pclk);
96 cis_data->rolling_shutter_skew = (cis_data->cur_height - 1) * cis_data->line_readOut_time;
97 cis_data->stream_on = false;
98
99 /* Frame valid time calcuration */
100 frame_valid_us = sensor_cis_do_div64((u64)cis_data->cur_height * (u64)cis_data->line_length_pck * (u64)(1000 * 1000), cis_data->pclk);
101 cis_data->frame_valid_us_time = (int)frame_valid_us;
102
103 dbg_sensor(1, "%s\n", __func__);
104 dbg_sensor(1, "Sensor size(%d x %d) setting: SUCCESS!\n",
105 cis_data->cur_width, cis_data->cur_height);
106 dbg_sensor(1, "Frame Valid(us): %d\n", frame_valid_us);
107 dbg_sensor(1, "rolling_shutter_skew: %lld\n", cis_data->rolling_shutter_skew);
108
109 dbg_sensor(1, "Fps: %d, max fps(%d)\n", frame_rate, cis_data->max_fps);
110 dbg_sensor(1, "min_frame_time(%d us)\n", cis_data->min_frame_us_time);
111 dbg_sensor(1, "Pixel rate(Mbps): %d\n", cis_data->pclk / 1000000);
112
113 /* Frame period calculation */
114 cis_data->frame_time = (cis_data->line_readOut_time * cis_data->cur_height / 1000);
115 cis_data->rolling_shutter_skew = (cis_data->cur_height - 1) * cis_data->line_readOut_time;
116
117 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__,
118 cis_data->frame_time, cis_data->rolling_shutter_skew);
119
120 /* Constant values */
121 cis_data->min_fine_integration_time = SENSOR_2X5SP_FINE_INTEGRATION_TIME_MIN;
122 cis_data->max_fine_integration_time = cis_data->cur_width;
123 cis_data->min_coarse_integration_time = SENSOR_2X5SP_COARSE_INTEGRATION_TIME_MIN;
124 cis_data->max_margin_coarse_integration_time = SENSOR_2X5SP_COARSE_INTEGRATION_TIME_MAX_MARGIN;
125 }
126
127 int sensor_2x5sp_cis_check_rev(struct v4l2_subdev *subdev)
128 {
129 int ret = 0;
130 u8 rev = 0;
131 struct i2c_client *client;
132 struct fimc_is_cis *cis = NULL;
133
134 WARN_ON(!subdev);
135
136 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
137 WARN_ON(!cis);
138 WARN_ON(!cis->cis_data);
139
140 client = cis->client;
141 if (unlikely(!client)) {
142 err("client is NULL");
143 return -EINVAL;
144 }
145
146 memset(cis->cis_data, 0, sizeof(cis_shared_data));
147 cis->rev_flag = false;
148
149 I2C_MUTEX_LOCK(cis->i2c_lock);
150
151 ret = fimc_is_sensor_read8(client, 0x0002, &rev);
152 if (ret < 0) {
153 cis->rev_flag = true;
154 ret = -EAGAIN;
155 } else {
156 cis->cis_data->cis_rev = rev;
157 pr_info("%s : Rev. 0x%X\n", __func__, rev);
158 }
159
160 I2C_MUTEX_UNLOCK(cis->i2c_lock);
161
162 return ret;
163 }
164
165 int sensor_2x5sp_cis_otp_check_awb_ratio(char *unit, char *golden, char *limit)
166 {
167 int ret = 0;
168
169 float r_g_min = (float)(limit[0]) / 1000;
170 float r_g_max = (float)(limit[1]) / 1000;
171 float b_g_min = (float)(limit[2]) / 1000;
172 float b_g_max = (float)(limit[3]) / 1000;
173
174 float rg = (float) ((unit[1]) | (unit[0] << 8)) / 16384;
175 float bg = (float) ((unit[3]) | (unit[2] << 8)) / 16384;
176
177 float golden_rg = (float) ((golden[1]) | (golden[0] << 8)) / 16384;
178 float golden_bg = (float) ((golden[3]) | (golden[2] << 8)) / 16384;
179
180 if (rg < (golden_rg - r_g_min) || rg > (golden_rg + r_g_max)) {
181 err("%s(): Final RG calibration factors out of range! rg=0x%x golden_rg=0x%x",
182 __func__, (unit[1] | unit[0] << 8), (golden[1] | golden[0] << 8));
183 ret = 1;
184 }
185
186 if (bg < (golden_bg - b_g_min) || bg > (golden_bg + b_g_max)) {
187 err("%s(): Final BG calibration factors out of range! bg=0x%x, golden_bg=0x%x",
188 __func__, (unit[3] | unit[2] << 8), (golden[3] | golden[2] << 8));
189 ret = 1;
190 }
191
192 return ret;
193 }
194
195 int sensor_2x5sp_cis_otp_check_crc(struct v4l2_subdev *subdev,
196 struct fimc_is_device_sensor *device, int group)
197 {
198 int ret = 0;
199 u16 crc_value = 0;
200 u16 crc16 = 0;
201 char *check_buf = (char *)&device->otp_cal_buf[0][0];
202
203 switch (group) {
204 case OTP_GROUP_ONE:
205 /* OTP Group1 CRC check */
206 crc_value = ((device->otp_cal_buf[254][60] << 8) | (device->otp_cal_buf[254][61]));
207 crc16 = sensor_cis_otp_get_crc16(&check_buf[OTP_GRP1_AWB_CRC_START], OTP_GRP1_AWB_CRC_SIZE);
208 if (crc_value != crc16) {
209 sensor_cis_otp_data_set(&check_buf[OTP_GRP1_AWB_CRC_START], "awb",
210 OTP_GRP1_AWB_CRC_SIZE, 0xff);
211 err("GR1: Error to AWB CRC16 : 0x%x, cal buffer CRC: 0x%x", crc16, crc_value);
212 ret = -EINVAL;
213 } else
214 info("GR1: AWB CRC16 : 0x%x, cal buffer CRC: 0x%x\n", crc16, crc_value);
215
216 crc_value = ((device->otp_cal_buf[254][62] << 8) | (device->otp_cal_buf[254][63]));
217 crc16 = sensor_cis_otp_get_crc16(&check_buf[OTP_GRP1_LSC_XTC_CRC_START], OTP_GRP1_LSC_XTC_CRC_SIZE);
218 if (crc_value != crc16) {
219 sensor_cis_otp_data_set(&check_buf[OTP_GRP1_LSC_XTC_CRC_START], "lsc_xtc",
220 OTP_GRP1_LSC_XTC_CRC_SIZE, 0xff);
221 err("GR1: Error to LSC & XTC CRC16 : 0x%x, cal buffer CRC: 0x%x", crc16, crc_value);
222 ret = -EINVAL;
223 } else
224 info("GR1: LSC & XTC CRC16 : 0x%x, cal buffer CRC: 0x%x\n", crc16, crc_value);
225 break;
226 case OTP_GROUP_TWO:
227 /* OTP Group2 CRC check */
228 crc_value = ((device->otp_cal_buf[255][60] << 8) | (device->otp_cal_buf[255][61]));
229 crc16 = sensor_cis_otp_get_crc16(&check_buf[OTP_GRP2_AWB_CRC_START], OTP_GRP2_AWB_CRC_SIZE);
230 if (crc_value != crc16) {
231 sensor_cis_otp_data_set(&check_buf[OTP_GRP2_AWB_CRC_START], "awb",
232 OTP_GRP2_AWB_CRC_SIZE, 0xff);
233 err("GR2: Error to AWB CRC16 : 0x%x, cal buffer CRC: 0x%x", crc16, crc_value);
234 ret = -EINVAL;
235 } else
236 info("GR2: AWB CRC16 : 0x%x, cal buffer CRC: 0x%x\n", crc16, crc_value);
237
238 crc_value = ((device->otp_cal_buf[255][62] << 8) | (device->otp_cal_buf[255][63]));
239 crc16 = sensor_cis_otp_get_crc16(&check_buf[OTP_GRP2_LSC_XTC_CRC_START], OTP_GRP2_LSC_XTC_CRC_SIZE);
240 if (crc_value != crc16) {
241 sensor_cis_otp_data_set(&check_buf[OTP_GRP2_LSC_XTC_CRC_START], "lsc_xtc",
242 OTP_GRP2_LSC_XTC_CRC_SIZE, 0xff);
243 err("GR2: Error to LSC & XTC CRC16 : 0x%x, cal buffer CRC: 0x%x", crc16, crc_value);
244 ret = -EINVAL;
245 } else
246 info("GR2: LSC & XTC CRC16 : 0x%x, cal buffer CRC: 0x%x\n", crc16, crc_value);
247 break;
248 default:
249 err("invalid OTP group when crc check(%d), check map data", group);
250 break;
251 }
252
253 return ret;
254 }
255
256 static int sensor_2x5sp_cis_otp_check(struct fimc_is_device_sensor *device, int group)
257 {
258 int ret = 0;
259 u16 group_flag = 0;
260
261 switch (group) {
262 case OTP_GROUP_ONE:
263 /* Group1 valid check */
264 group_flag = device->otp_cal_buf[254][0];
265 if (group_flag != OTP_DATA_VALID) {
266 err("error Group1 OTP data invalid(0x%x)", group_flag);
267 ret = -EINVAL;
268 } else {
269 ret = OTP_GROUP_ONE;
270 }
271
272 break;
273 case OTP_GROUP_TWO:
274 /* First check Group2 data valid */
275 group_flag = device->otp_cal_buf[255][0];
276 if (group_flag != OTP_DATA_VALID) {
277 err("error Group2 OTP data invalid(0x%x)", group_flag);
278 ret = -EINVAL;
279
280 } else {
281 ret = OTP_GROUP_TWO;
282 }
283
284 break;
285 default:
286 err("invalid OTP group when invalid check(%d), check map data", group);
287 break;
288 }
289
290 return ret;
291 }
292
293 int sensor_2x5sp_cis_otp_read(struct v4l2_subdev *subdev, struct fimc_is_device_sensor *device)
294 {
295 int ret = 0;
296 struct fimc_is_cis *cis;
297 struct i2c_client *client;
298 u16 val = 1, page;
299 int i;
300 int retry;
301
302 FIMC_BUG(!subdev);
303
304 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
305 if (!cis) {
306 err("cis is NULL");
307 return -EINVAL;
308 }
309
310 client = cis->client;
311 if (unlikely(!client)) {
312 err("client is NULL");
313 return -ENODEV;
314 }
315
316 info("OTP read start\n");
317 dbg_sensor(1, "%s, 1. sensor initial setting", __func__);
318
319 I2C_MUTEX_LOCK(cis->i2c_lock);
320
321 /* Basic settings for OTP R/W */
322 fimc_is_sensor_write16(client, 0x6028, 0x4000);
323 fimc_is_sensor_write16(client, 0x6018, 0x0001);
324 fimc_is_sensor_write16(client, 0x7002, 0x020C);
325 fimc_is_sensor_write16(client, 0x6014, 0x0001);
326
327 usleep_range(3000, 3001);
328
329 fimc_is_sensor_write16(client, 0x0136, 0x1A00);
330 fimc_is_sensor_write16(client, 0x0300, 0x0003);
331 fimc_is_sensor_write16(client, 0x0302, 0x0001);
332 fimc_is_sensor_write16(client, 0x0304, 0x0004);
333 fimc_is_sensor_write16(client, 0x0306, 0x00DD);
334 fimc_is_sensor_write16(client, 0x030C, 0x0001);
335 fimc_is_sensor_write16(client, 0x0308, 0x0008);
336 fimc_is_sensor_write16(client, 0x030A, 0x0001);
337 fimc_is_sensor_write16(client, 0x030E, 0x0004);
338 fimc_is_sensor_write16(client, 0x0310, 0x014A);
339 fimc_is_sensor_write16(client, 0x0312, 0x0000);
340
341 dbg_sensor(1, "%s, 2. sensor stream on", __func__);
342 fimc_is_sensor_write16(client, 0x6028, 0x4000);
343 fimc_is_sensor_write8(client, 0x0100, 0x01);
344
345 /* wait streamon */
346 CALL_CISOPS(cis, cis_wait_streamon, subdev);
347
348 dbg_sensor(1, "%s, 3. page select & read cal", __func__);
349 for (page = OTP_PAGE_START; page <= OTP_PAGE_END; page++) {
350 /* page select & read start */
351 fimc_is_sensor_write16(client, OTP_PAGE_SELECT, page);
352 fimc_is_sensor_write16(client, OTP_PAGE_CTRL, 0x0100);
353
354 /* wait 0x0A00 == 0 [0]: read completed with no errors */
355 retry = 500;
356 while (retry > 0 && val) {
357 fimc_is_sensor_read16(client, OTP_PAGE_CTRL, &val);
358 if (val == 0)
359 break;
360
361 usleep_range(100, 100);
362 retry--;
363 }
364
365 if (!retry)
366 err("%s: OTP page[%d] read fail with err(%d)\n",
367 __func__, page, val);
368
369 for (i = 0; i < OTP_PAGE_SIZE; i++) {
370 fimc_is_sensor_read8(client, OTP_PAGE_BASE + i, &device->otp_cal_buf[page][i]);
371 dbg_sensor(2, "cal: [%d][0x%x]: %x\n", page, OTP_PAGE_BASE + i, device->otp_cal_buf[page][i]);
372 }
373
374 /* make initial state */
375 fimc_is_sensor_write16(client, OTP_PAGE_CTRL, 0x0000);
376 }
377
378 fimc_is_sensor_write8(client, 0x0100, 0x00);
379 msleep(20);
380 info("OTP read end\n");
381
382 I2C_MUTEX_UNLOCK(cis->i2c_lock);
383
384 return ret;
385 }
386
387 int sensor_2x5sp_cis_otp(struct v4l2_subdev *subdev, struct fimc_is_device_sensor *device)
388 {
389 int ret = 0;
390 int i;
391 int otp_group = 0x0;
392 char *otp_buf = (char *)&device->otp_cal_buf[0][0];
393
394 ret = sensor_cis_otp_read_file(OTP_DATA_PATH, (void *)device->otp_cal_buf, OTP_PAGE_SIZE * 256);
395 if (ret) {
396 /* OTP data read */
397 ret = sensor_2x5sp_cis_otp_read(subdev, device);
398 if (ret < 0) {
399 sensor_cis_otp_data_set(&otp_buf[0], "all", OTP_PAGE_SIZE * 256, 0xff);
400 err("Don't read to 2x5 OTP data");
401 goto p_err;
402 }
403
404 /* Write to OTP data at file */
405 ret = sensor_cis_otp_write_file(OTP_DATA_PATH, (void *)device->otp_cal_buf, OTP_PAGE_SIZE * 256);
406 if (ret < 0) {
407 sensor_cis_otp_data_set(&otp_buf[0], "all", OTP_PAGE_SIZE * 256, 0xff);
408 err("2x5 OTP data don't file write");
409 goto p_err;
410 }
411 }
412
413 /* Need to first check GROUP2 */
414 for (i = OTP_GROUP_MAX - 1; i >= OTP_GROUP_ONE; i--) {
415 /* OTP valid check */
416 otp_group = sensor_2x5sp_cis_otp_check(device, i);
417 if (otp_group < 0) {
418 if (i == OTP_GROUP_ONE) {
419 sensor_cis_otp_data_set(&otp_buf[0], "all", OTP_PAGE_SIZE * 256, 0xff);
420 err("All OTP data are invalid, check module");
421 goto p_err;
422 } else {
423 continue;
424 }
425 } else {
426 break;
427 }
428 }
429
430 /* OTP CRC check */
431 ret = sensor_2x5sp_cis_otp_check_crc(subdev, device, otp_group);
432 if (ret < 0) {
433 err("All OTP data CRC check fail, check module");
434
435 device->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_ERROR;
436 goto p_err;
437 } else {
438 err("%s: OTP group%d data availble\n", __func__, otp_group);
439
440 device->cal_status[CAMERA_CRC_INDEX_AWB] = CRC_NO_ERROR;
441
442 ret = sensor_2x5sp_cis_otp_check_awb_ratio(&otp_buf[(253 + otp_group) * OTP_PAGE_SIZE + OTP_AWB_UNIT_OFFSET],
443 &otp_buf[(253 + otp_group) * OTP_PAGE_SIZE + OTP_AWB_GOLDEN_OFFSET],
444 &otp_buf[(253 + otp_group) * OTP_PAGE_SIZE + OTP_AWB_LIMIT_OFFSET]);
445
446 if (ret) {
447 err("%s(): 2X5 OTP AWB Group%d ratio out of limit(%d)", __func__, otp_group, ret);
448 device->cal_status[CAMERA_CRC_INDEX_AWB] = LIMIT_FAILURE;
449 ret = -1;
450 }
451 }
452
453 p_err:
454 return ret;
455 }
456
457 /* CIS OPS */
458 int sensor_2x5sp_cis_init(struct v4l2_subdev *subdev)
459 {
460 int ret = 0;
461 struct fimc_is_cis *cis;
462 struct fimc_is_module_enum *module;
463 struct fimc_is_device_sensor *device = NULL;
464 u32 setfile_index = 0;
465 cis_setting_info setinfo;
466
467 setinfo.return_value = 0;
468
469 setinfo.param = NULL;
470
471 FIMC_BUG(!subdev);
472
473 module = (struct fimc_is_module_enum *)v4l2_get_subdev_hostdata(subdev);
474
475 device = (struct fimc_is_device_sensor *)v4l2_get_subdev_hostdata(module->subdev);
476 if (!device) {
477 err("device sensor is NULL");
478 ret = -ENODEV;
479 goto p_err;
480 }
481
482 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
483 if (!cis) {
484 err("cis is NULL");
485 ret = -EINVAL;
486 goto p_err;
487 }
488
489 FIMC_BUG(!cis->cis_data);
490 memset(cis->cis_data, 0, sizeof(cis_shared_data));
491
492 cis->cis_data->cur_width = SENSOR_2X5SP_MAX_WIDTH;
493 cis->cis_data->cur_height = SENSOR_2X5SP_MAX_HEIGHT;
494 cis->cis_data->low_expo_start = 33000;
495 cis->need_mode_change = false;
496
497 sensor_2x5sp_cis_data_calculation(sensor_2x5sp_pllinfos[setfile_index], cis->cis_data);
498
499 setinfo.return_value = 0;
500 CALL_CISOPS(cis, cis_get_min_exposure_time, subdev, &setinfo.return_value);
501 dbg_sensor(1, "[%s] min exposure time : %d\n", __func__, setinfo.return_value);
502 setinfo.return_value = 0;
503 CALL_CISOPS(cis, cis_get_max_exposure_time, subdev, &setinfo.return_value);
504 dbg_sensor(1, "[%s] max exposure time : %d\n", __func__, setinfo.return_value);
505 setinfo.return_value = 0;
506 CALL_CISOPS(cis, cis_get_min_analog_gain, subdev, &setinfo.return_value);
507 dbg_sensor(1, "[%s] min again : %d\n", __func__, setinfo.return_value);
508 setinfo.return_value = 0;
509 CALL_CISOPS(cis, cis_get_max_analog_gain, subdev, &setinfo.return_value);
510 dbg_sensor(1, "[%s] max again : %d\n", __func__, setinfo.return_value);
511 setinfo.return_value = 0;
512 CALL_CISOPS(cis, cis_get_min_digital_gain, subdev, &setinfo.return_value);
513 dbg_sensor(1, "[%s] min dgain : %d\n", __func__, setinfo.return_value);
514 setinfo.return_value = 0;
515 CALL_CISOPS(cis, cis_get_max_digital_gain, subdev, &setinfo.return_value);
516 dbg_sensor(1, "[%s] max dgain : %d\n", __func__, setinfo.return_value);
517
518 #ifdef DEBUG_SENSOR_TIME
519 do_gettimeofday(&end);
520 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
521 #endif
522
523 if (device->use_otp_cal) {
524 ret = sensor_2x5sp_cis_otp(subdev, device);
525 if (ret < 0) {
526 err("2x5sp OTP data have probelm, check module");
527 ret = 0;
528 }
529 }
530
531 p_err:
532 return ret;
533 }
534
535 int sensor_2x5sp_cis_log_status(struct v4l2_subdev *subdev)
536 {
537 int ret = 0;
538 struct fimc_is_cis *cis;
539 struct i2c_client *client = NULL;
540 u8 data8 = 0;
541 u16 data16 = 0;
542
543 FIMC_BUG(!subdev);
544
545 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
546 if (!cis) {
547 err("cis is NULL");
548 return -ENODEV;
549 }
550
551 client = cis->client;
552 if (unlikely(!client)) {
553 err("client is NULL");
554 return -ENODEV;
555 }
556
557 I2C_MUTEX_LOCK(cis->i2c_lock);
558
559 pr_err("[SEN:DUMP] *******************************\n");
560 fimc_is_sensor_read16(client, 0x0000, &data16);
561 pr_err("[SEN:DUMP] model_id(%x)\n", data16);
562 fimc_is_sensor_read8(client, 0x0002, &data8);
563 pr_err("[SEN:DUMP] revision_number(%x)\n", data8);
564 fimc_is_sensor_read8(client, 0x0005, &data8);
565 pr_err("[SEN:DUMP] frame_count(%x)\n", data8);
566 fimc_is_sensor_read8(client, 0x0100, &data8);
567 pr_err("[SEN:DUMP] mode_select(%x)\n", data8);
568
569 sensor_cis_dump_registers(subdev, sensor_2x5sp_setfiles[0], sensor_2x5sp_setfile_sizes[0]);
570
571 I2C_MUTEX_UNLOCK(cis->i2c_lock);
572
573 pr_err("[SEN:DUMP] *******************************\n");
574
575 return ret;
576 }
577
578 #if USE_GROUP_PARAM_HOLD
579 static int sensor_2x5sp_cis_group_param_hold_func(struct v4l2_subdev *subdev, unsigned int hold)
580 {
581 int ret = 0;
582 struct fimc_is_cis *cis = NULL;
583 struct i2c_client *client = NULL;
584
585 FIMC_BUG(!subdev);
586
587 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
588
589 FIMC_BUG(!cis);
590 FIMC_BUG(!cis->cis_data);
591
592 client = cis->client;
593 if (unlikely(!client)) {
594 err("client is NULL");
595 ret = -EINVAL;
596 goto p_err;
597 }
598
599 if (hold == cis->cis_data->group_param_hold) {
600 pr_debug("already group_param_hold (%d)\n", cis->cis_data->group_param_hold);
601 goto p_err;
602 }
603
604 ret = fimc_is_sensor_write8(client, 0x0104, hold);
605 if (ret < 0)
606 goto p_err;
607
608 cis->cis_data->group_param_hold = hold;
609 ret = 1;
610 p_err:
611 return ret;
612 }
613 #else
614 static inline int sensor_2x5sp_cis_group_param_hold_func(struct v4l2_subdev *subdev, unsigned int hold)
615 { return 0; }
616 #endif
617
618 /* Input
619 * hold : true - hold, flase - no hold
620 * Output
621 * return: 0 - no effect(already hold or no hold)
622 * positive - setted by request
623 * negative - ERROR value
624 */
625 int sensor_2x5sp_cis_group_param_hold(struct v4l2_subdev *subdev, bool hold)
626 {
627 int ret = 0;
628 struct fimc_is_cis *cis = NULL;
629
630 FIMC_BUG(!subdev);
631
632 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
633
634 FIMC_BUG(!cis);
635 FIMC_BUG(!cis->cis_data);
636
637 I2C_MUTEX_LOCK(cis->i2c_lock);
638
639 ret = sensor_2x5sp_cis_group_param_hold_func(subdev, hold);
640 if (ret < 0)
641 goto p_err;
642
643 p_err:
644 I2C_MUTEX_UNLOCK(cis->i2c_lock);
645
646 return ret;
647 }
648
649 int sensor_2x5sp_cis_set_global_setting(struct v4l2_subdev *subdev)
650 {
651 int ret = 0;
652 struct fimc_is_cis *cis = NULL;
653
654 FIMC_BUG(!subdev);
655
656 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
657 FIMC_BUG(!cis);
658
659 I2C_MUTEX_LOCK(cis->i2c_lock);
660
661 ret = sensor_cis_set_registers(subdev, sensor_2x5sp_global, sensor_2x5sp_global_size);
662
663 if (ret < 0) {
664 err("sensor_3p8sp_set_registers fail!!");
665 goto p_err;
666 }
667
668 dbg_sensor(1, "[%s] global setting done\n", __func__);
669
670 p_err:
671 I2C_MUTEX_UNLOCK(cis->i2c_lock);
672
673 return ret;
674 }
675
676 int sensor_2x5sp_cis_mode_change(struct v4l2_subdev *subdev, u32 mode)
677 {
678 int ret = 0;
679 struct fimc_is_cis *cis = NULL;
680
681 FIMC_BUG(!subdev);
682
683 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
684 FIMC_BUG(!cis);
685 FIMC_BUG(!cis->cis_data);
686
687 if (mode > sensor_2x5sp_max_setfile_num) {
688 err("invalid mode(%d)!!", mode);
689 return -EINVAL;
690 }
691
692 sensor_2x5sp_cis_data_calculation(sensor_2x5sp_pllinfos[mode], cis->cis_data);
693
694 I2C_MUTEX_LOCK(cis->i2c_lock);
695
696 ret = sensor_cis_set_registers(subdev, sensor_2x5sp_setfiles[mode], sensor_2x5sp_setfile_sizes[mode]);
697 if (ret < 0) {
698 err("sensor_2x5sp_set_registers fail!!");
699 goto p_err;
700 }
701
702 cis->cis_data->frame_time = (cis->cis_data->line_readOut_time * cis->cis_data->cur_height / 1000);
703 cis->cis_data->rolling_shutter_skew = (cis->cis_data->cur_height - 1) * cis->cis_data->line_readOut_time;
704 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__,
705 cis->cis_data->frame_time, cis->cis_data->rolling_shutter_skew);
706
707 dbg_sensor(1, "[%s] mode changed(%d)\n", __func__, mode);
708
709 p_err:
710 I2C_MUTEX_UNLOCK(cis->i2c_lock);
711
712 return ret;
713 }
714
715 int sensor_2x5sp_cis_mode_change_throttling(struct v4l2_subdev *subdev)
716 {
717 int ret = 0;
718 struct fimc_is_cis *cis = NULL;
719
720 FIMC_BUG(!subdev);
721
722 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
723 FIMC_BUG(!cis);
724 FIMC_BUG(!cis->cis_data);
725
726 sensor_2x5sp_cis_data_calculation(sensor_2x5sp_pllinfo_throttling, cis->cis_data);
727
728 I2C_MUTEX_LOCK(cis->i2c_lock);
729
730 ret = sensor_cis_set_registers(subdev, sensor_2x5sp_setfile_throttling,
731 sensor_2x5sp_setfile_throttling_size);
732 if (ret < 0) {
733 err("sensor_gm1sp_set_registers fail!!");
734 goto p_err;
735 }
736
737 cis->cis_data->frame_time = (cis->cis_data->line_readOut_time * cis->cis_data->cur_height / 1000);
738 cis->cis_data->rolling_shutter_skew = (cis->cis_data->cur_height - 1) * cis->cis_data->line_readOut_time;
739 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__,
740 cis->cis_data->frame_time, cis->cis_data->rolling_shutter_skew);
741
742 dbg_sensor(1, "[%s] throttling mode changed\n", __func__);
743
744 p_err:
745 I2C_MUTEX_UNLOCK(cis->i2c_lock);
746
747 return ret;
748 }
749
750 /* Deprecated */
751 int sensor_2x5sp_cis_set_size(struct v4l2_subdev *subdev, cis_shared_data *cis_data)
752 {
753 return 0;
754 }
755
756 int sensor_2x5sp_cis_stream_on(struct v4l2_subdev *subdev)
757 {
758 int ret = 0;
759 struct fimc_is_cis *cis;
760 struct i2c_client *client;
761 cis_shared_data *cis_data;
762
763 #ifdef DEBUG_SENSOR_TIME
764 struct timeval st, end;
765
766 do_gettimeofday(&st);
767 #endif
768
769 FIMC_BUG(!subdev);
770
771 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
772
773 FIMC_BUG(!cis);
774 FIMC_BUG(!cis->cis_data);
775
776 client = cis->client;
777 if (unlikely(!client)) {
778 err("client is NULL");
779 return -EINVAL;
780 }
781
782 cis_data = cis->cis_data;
783
784 dbg_sensor(1, "[MOD:D:%d] %s\n", cis->id, __func__);
785
786 I2C_MUTEX_LOCK(cis->i2c_lock);
787
788 ret = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
789 if (ret < 0)
790 err("group_param_hold_func failed at stream on");
791
792 #ifdef DEBUG_2X5SP_PLL
793 {
794 u16 pll;
795
796 fimc_is_sensor_read16(client, 0x0300, &pll);
797 dbg_sensor(1, "______ vt_pix_clk_div(%x)\n", pll);
798 fimc_is_sensor_read16(client, 0x0302, &pll);
799 dbg_sensor(1, "______ vt_sys_clk_div(%x)\n", pll);
800 fimc_is_sensor_read16(client, 0x0304, &pll);
801 dbg_sensor(1, "______ pre_pll_clk_div(%x)\n", pll);
802 fimc_is_sensor_read16(client, 0x0306, &pll);
803 dbg_sensor(1, "______ pll_multiplier(%x)\n", pll);
804 fimc_is_sensor_read16(client, 0x0308, &pll);
805 dbg_sensor(1, "______ op_pix_clk_div(%x)\n", pll);
806 fimc_is_sensor_read16(client, 0x030a, &pll);
807 dbg_sensor(1, "______ op_sys_clk_div(%x)\n", pll);
808
809 fimc_is_sensor_read16(client, 0x030c, &pll);
810 dbg_sensor(1, "______ secnd_pre_pll_clk_div(%x)\n", pll);
811 fimc_is_sensor_read16(client, 0x030e, &pll);
812 dbg_sensor(1, "______ secnd_pll_multiplier(%x)\n", pll);
813 fimc_is_sensor_read16(client, 0x0340, &pll);
814 dbg_sensor(1, "______ frame_length_lines(%x)\n", pll);
815 fimc_is_sensor_read16(client, 0x0342, &pll);
816 dbg_sensor(1, "______ line_length_pck(%x)\n", pll);
817 }
818 #endif
819
820 /* Sensor stream on */
821 fimc_is_sensor_write16(client, 0x6028, 0x4000);
822 fimc_is_sensor_write8(client, 0x0100, 0x01);
823
824 cis_data->stream_on = true;
825
826 #ifdef DEBUG_SENSOR_TIME
827 do_gettimeofday(&end);
828 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
829 #endif
830
831 I2C_MUTEX_UNLOCK(cis->i2c_lock);
832
833 return ret;
834 }
835
836 int sensor_2x5sp_cis_stream_off(struct v4l2_subdev *subdev)
837 {
838 int ret = 0;
839 struct fimc_is_cis *cis;
840 struct i2c_client *client;
841 cis_shared_data *cis_data;
842
843 #ifdef DEBUG_SENSOR_TIME
844 struct timeval st, end;
845
846 do_gettimeofday(&st);
847 #endif
848
849 FIMC_BUG(!subdev);
850
851 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
852
853 FIMC_BUG(!cis);
854 FIMC_BUG(!cis->cis_data);
855
856 client = cis->client;
857 if (unlikely(!client)) {
858 err("client is NULL");
859 return -EINVAL;
860 }
861
862 cis_data = cis->cis_data;
863
864 dbg_sensor(1, "[MOD:D:%d] %s\n", cis->id, __func__);
865
866 I2C_MUTEX_LOCK(cis->i2c_lock);
867
868 ret = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
869 if (ret < 0)
870 err("group_param_hold_func failed at stream off");
871
872 fimc_is_sensor_write16(client, 0x6028, 0x4000);
873
874 /* during LEC mode, clear 0x0BCC before stream off for next frame
875 * After cancelled, restore value
876 */
877 if (cis->long_term_mode.sen_strm_off_on_enable)
878 fimc_is_sensor_write8(client, 0x0BCC, 0);
879 else
880 fimc_is_sensor_write8(client, 0x0BCC, 0x1);
881
882 /* Sensor stream off */
883 fimc_is_sensor_write8(client, 0x0100, 0x00);
884
885 cis_data->stream_on = false;
886
887 #ifdef DEBUG_SENSOR_TIME
888 do_gettimeofday(&end);
889 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
890 #endif
891
892 I2C_MUTEX_UNLOCK(cis->i2c_lock);
893
894 return ret;
895 }
896
897 int sensor_2x5sp_cis_set_exposure_time(struct v4l2_subdev *subdev, struct ae_param *target_exposure)
898 {
899 int ret = 0;
900 int hold = 0;
901 struct fimc_is_cis *cis;
902 struct i2c_client *client;
903 cis_shared_data *cis_data;
904
905 u32 vt_pic_clk_freq_mhz = 0;
906 u16 long_coarse_int = 0;
907 u16 short_coarse_int = 0;
908 u16 middle_coarse_int = 0;
909 u32 line_length_pck = 0;
910 u32 min_fine_int = 0;
911 u64 numerator;
912 u8 lte_shifter;
913 u32 multiple_ratio = 1;
914 #ifdef DEBUG_SENSOR_TIME
915 struct timeval st, end;
916
917 do_gettimeofday(&st);
918 #endif
919
920 FIMC_BUG(!subdev);
921 FIMC_BUG(!target_exposure);
922
923 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
924
925 FIMC_BUG(!cis);
926 FIMC_BUG(!cis->cis_data);
927
928 client = cis->client;
929 if (unlikely(!client)) {
930 err("client is NULL");
931 return -EINVAL;
932 }
933
934 if ((target_exposure->long_val <= 0) || (target_exposure->short_val <= 0)) {
935 err("[%s] invalid target exposure(%d, %d)\n", __func__,
936 target_exposure->long_val, target_exposure->short_val);
937 return -EINVAL;
938 }
939
940 cis_data = cis->cis_data;
941
942 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), target long(%d), short(%d), middle(%d)\n", cis->id, __func__,
943 cis_data->sen_vsync_count,
944 target_exposure->long_val, target_exposure->short_val, target_exposure->middle_val);
945
946 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
947 line_length_pck = cis_data->line_length_pck;
948 min_fine_int = cis_data->min_fine_integration_time;
949
950 lte_shifter = cis->long_term_mode.sen_strm_off_on_enable ?
951 GET_2X5SP_LTE_SHIFT_CNT(target_exposure->long_val) : 0;
952
953 /* In 24M remosaic mode, set 4 times of exposure val */
954 if (cis_data->sens_config_index_cur == SENSOR_2X5SP_5760X4320_24FPS) {
955 multiple_ratio = 4;
956 dbg_sensor(1, "[mod:d:%d] %s, Set 4 times of coarse_int for 24M mode\n", cis->id, __func__);
957 }
958
959 numerator = (u64)cis_data->pclk * target_exposure->long_val * multiple_ratio;
960 long_coarse_int = (numerator - min_fine_int)
961 /(1000 * 1000) / line_length_pck / (1 << lte_shifter);
962 numerator = (u64)cis_data->pclk * target_exposure->short_val * multiple_ratio;
963 short_coarse_int = (numerator - min_fine_int)
964 /(1000 * 1000) / line_length_pck / (1 << lte_shifter);
965 numerator = (u64)cis_data->pclk * target_exposure->middle_val * multiple_ratio;
966 middle_coarse_int = (numerator - min_fine_int)
967 /(1000 * 1000) / line_length_pck / (1 << lte_shifter);
968
969 /* 4FDSUM mode should be set half of coarse integration */
970 if (cis_data->sens_config_index_cur == SENSOR_2X5SP_2880X2160_30FPS
971 || cis_data->sens_config_index_cur == SENSOR_2X5SP_1920X1080_120FPS
972 || cis_data->sens_config_index_cur == SENSOR_2X5SP_1280X720_240FPS) {
973 long_coarse_int /= 2;
974 short_coarse_int /= 2;
975 middle_coarse_int /= 2;
976 }
977
978 if (long_coarse_int > cis_data->max_coarse_integration_time) {
979 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) max(%d)\n", cis->id, __func__,
980 cis_data->sen_vsync_count, long_coarse_int, cis_data->max_coarse_integration_time);
981 long_coarse_int = cis_data->max_coarse_integration_time;
982 }
983
984 if (short_coarse_int > cis_data->max_coarse_integration_time) {
985 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) max(%d)\n", cis->id, __func__,
986 cis_data->sen_vsync_count, short_coarse_int, cis_data->max_coarse_integration_time);
987 short_coarse_int = cis_data->max_coarse_integration_time;
988 }
989
990 if (middle_coarse_int > cis_data->max_coarse_integration_time) {
991 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), middle coarse(%d) max(%d)\n", cis->id, __func__,
992 cis_data->sen_vsync_count, middle_coarse_int, cis_data->max_coarse_integration_time);
993 middle_coarse_int = cis_data->max_coarse_integration_time;
994 }
995
996 if (long_coarse_int < cis_data->min_coarse_integration_time) {
997 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) min(%d)\n", cis->id, __func__,
998 cis_data->sen_vsync_count, long_coarse_int, cis_data->min_coarse_integration_time);
999 long_coarse_int = cis_data->min_coarse_integration_time;
1000 }
1001
1002 if (short_coarse_int < cis_data->min_coarse_integration_time) {
1003 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) min(%d)\n", cis->id, __func__,
1004 cis_data->sen_vsync_count, short_coarse_int, cis_data->min_coarse_integration_time);
1005 short_coarse_int = cis_data->min_coarse_integration_time;
1006 }
1007
1008 if (middle_coarse_int < cis_data->min_coarse_integration_time) {
1009 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), middle coarse(%d) min(%d)\n", cis->id, __func__,
1010 cis_data->sen_vsync_count, middle_coarse_int, cis_data->min_coarse_integration_time);
1011 middle_coarse_int = cis_data->min_coarse_integration_time;
1012 }
1013
1014 I2C_MUTEX_LOCK(cis->i2c_lock);
1015
1016 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1017 if (hold < 0) {
1018 ret = hold;
1019 goto p_err;
1020 }
1021
1022 /* Short exposure */
1023 ret = fimc_is_sensor_write16(client, 0x0202, short_coarse_int);
1024 if (ret < 0)
1025 goto p_err;
1026
1027 /* Long exposure */
1028 if (cis_data->is_data.wdr_mode != CAMERA_WDR_OFF) {
1029 ret = fimc_is_sensor_write16(client, 0x0226, long_coarse_int);
1030 if (ret < 0)
1031 goto p_err;
1032
1033 ret = fimc_is_sensor_write16(client, 0x022C, middle_coarse_int);
1034 if (ret < 0)
1035 goto p_err;
1036 }
1037
1038 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), vt_pic_clk_freq_mhz (%d), line_length_pck(%d), min_fine_int (%d)\n",
1039 cis->id, __func__, cis_data->sen_vsync_count, vt_pic_clk_freq_mhz, line_length_pck, min_fine_int);
1040 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), frame_length_lines(%#x), coarse_int (L:%#x, S:%#x, M:%#x)\n",
1041 cis->id, __func__, cis_data->sen_vsync_count, cis_data->frame_length_lines,
1042 long_coarse_int, short_coarse_int, middle_coarse_int);
1043
1044 #ifdef DEBUG_SENSOR_TIME
1045 do_gettimeofday(&end);
1046 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1047 #endif
1048
1049 p_err:
1050 if (hold > 0) {
1051 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1052 if (hold < 0)
1053 ret = hold;
1054 }
1055
1056 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1057
1058 return ret;
1059 }
1060
1061 int sensor_2x5sp_cis_get_min_exposure_time(struct v4l2_subdev *subdev, u32 *min_expo)
1062 {
1063 int ret = 0;
1064 struct fimc_is_cis *cis = NULL;
1065 cis_shared_data *cis_data = NULL;
1066 u32 min_integration_time = 0;
1067 u32 min_coarse = 0;
1068 u32 min_fine = 0;
1069 u32 vt_pic_clk_freq_mhz = 0;
1070 u32 line_length_pck = 0;
1071
1072 #ifdef DEBUG_SENSOR_TIME
1073 struct timeval st, end;
1074
1075 do_gettimeofday(&st);
1076 #endif
1077
1078 FIMC_BUG(!subdev);
1079 FIMC_BUG(!min_expo);
1080
1081 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1082
1083 FIMC_BUG(!cis);
1084 FIMC_BUG(!cis->cis_data);
1085
1086 cis_data = cis->cis_data;
1087
1088 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1089 if (vt_pic_clk_freq_mhz == 0) {
1090 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis->id, __func__, vt_pic_clk_freq_mhz);
1091 goto p_err;
1092 }
1093 line_length_pck = cis_data->line_length_pck;
1094 min_coarse = cis_data->min_coarse_integration_time;
1095 min_fine = cis_data->min_fine_integration_time;
1096
1097 min_integration_time = ((line_length_pck * min_coarse) + min_fine) / vt_pic_clk_freq_mhz;
1098 *min_expo = min_integration_time;
1099
1100 dbg_sensor(1, "[%s] min integration time %d\n", __func__, min_integration_time);
1101
1102 #ifdef DEBUG_SENSOR_TIME
1103 do_gettimeofday(&end);
1104 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1105 #endif
1106
1107 p_err:
1108 return ret;
1109 }
1110
1111 int sensor_2x5sp_cis_get_max_exposure_time(struct v4l2_subdev *subdev, u32 *max_expo)
1112 {
1113 int ret = 0;
1114 struct fimc_is_cis *cis;
1115 cis_shared_data *cis_data;
1116 u32 max_integration_time = 0;
1117 u32 max_coarse_margin = 0;
1118 u32 max_fine_margin = 0;
1119 u32 max_coarse = 0;
1120 u32 max_fine = 0;
1121 u32 vt_pic_clk_freq_mhz = 0;
1122 u32 line_length_pck = 0;
1123 u32 frame_length_lines = 0;
1124 u32 multiple_ratio = 1;
1125
1126 #ifdef DEBUG_SENSOR_TIME
1127 struct timeval st, end;
1128
1129 do_gettimeofday(&st);
1130 #endif
1131
1132 FIMC_BUG(!subdev);
1133 FIMC_BUG(!max_expo);
1134
1135 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1136
1137 FIMC_BUG(!cis);
1138 FIMC_BUG(!cis->cis_data);
1139
1140 cis_data = cis->cis_data;
1141
1142 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1143 if (vt_pic_clk_freq_mhz == 0) {
1144 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis->id, __func__, vt_pic_clk_freq_mhz);
1145 goto p_err;
1146 }
1147 line_length_pck = cis_data->line_length_pck;
1148 frame_length_lines = cis_data->frame_length_lines;
1149
1150 max_coarse_margin = cis_data->max_margin_coarse_integration_time;
1151 max_fine_margin = line_length_pck - cis_data->min_fine_integration_time;
1152 max_coarse = frame_length_lines - max_coarse_margin;
1153 max_fine = cis_data->max_fine_integration_time;
1154
1155 max_integration_time = ((line_length_pck * max_coarse) + max_fine) / vt_pic_clk_freq_mhz;
1156
1157 *max_expo = max_integration_time;
1158
1159 /* In 24M remosaic mode, set 4 times of max_coarse_integration_time */
1160 if (cis_data->sens_config_index_cur == SENSOR_2X5SP_5760X4320_24FPS) {
1161 multiple_ratio = 4;
1162 dbg_sensor(1, "[mod:d:%d] %s, Set 4 times of coarse_int for 24M mode\n", cis->id, __func__);
1163 }
1164
1165 /* TODO: Is this values update here? */
1166 cis_data->max_margin_fine_integration_time = max_fine_margin;
1167 cis_data->max_coarse_integration_time = max_coarse * multiple_ratio;
1168
1169 dbg_sensor(1, "[%s] max integration time %d, max margin fine integration %d, max coarse integration %d\n",
1170 __func__, max_integration_time, cis_data->max_margin_fine_integration_time,
1171 cis_data->max_coarse_integration_time);
1172
1173 #ifdef DEBUG_SENSOR_TIME
1174 do_gettimeofday(&end);
1175 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1176 #endif
1177
1178 p_err:
1179 return ret;
1180 }
1181
1182 int sensor_2x5sp_cis_adjust_frame_duration(struct v4l2_subdev *subdev,
1183 u32 input_exposure_time,
1184 u32 *target_duration)
1185 {
1186 int ret = 0;
1187 struct fimc_is_cis *cis;
1188 cis_shared_data *cis_data;
1189
1190 u32 vt_pic_clk_freq_mhz = 0;
1191 u32 line_length_pck = 0;
1192 u32 frame_length_lines = 0;
1193 u32 frame_duration = 0;
1194 u64 numerator;
1195 u8 lte_shifter;
1196 u32 multiple_ratio = 1;
1197
1198 #ifdef DEBUG_SENSOR_TIME
1199 struct timeval st, end;
1200
1201 do_gettimeofday(&st);
1202 #endif
1203
1204 FIMC_BUG(!subdev);
1205 FIMC_BUG(!target_duration);
1206
1207 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1208
1209 FIMC_BUG(!cis);
1210 FIMC_BUG(!cis->cis_data);
1211
1212 cis_data = cis->cis_data;
1213
1214 lte_shifter = cis->long_term_mode.sen_strm_off_on_enable ?
1215 GET_2X5SP_LTE_SHIFT_CNT(input_exposure_time) : 0;
1216
1217 /* In 24M remosaic mode, set 4 times of frame_length_lines */
1218 if (cis_data->sens_config_index_cur == SENSOR_2X5SP_5760X4320_24FPS) {
1219 multiple_ratio = 4;
1220 dbg_sensor(1, "[mod:d:%d] %s, Set 4 times of coarse_int for 24M mode\n", cis->id, __func__);
1221 }
1222
1223 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1224 line_length_pck = cis_data->line_length_pck;
1225 numerator = (u64)cis_data->pclk * input_exposure_time * multiple_ratio;
1226 frame_length_lines = (u16)((numerator / (1000 * 1000))/ line_length_pck / (1 << lte_shifter));
1227 frame_length_lines += cis_data->max_margin_coarse_integration_time;
1228
1229 numerator = (u64)frame_length_lines * line_length_pck;
1230 frame_duration = (numerator << lte_shifter) / vt_pic_clk_freq_mhz;
1231
1232 dbg_sensor(1, "[%s](vsync cnt = %d) input exp(%d), adj duration, frame duraion(%d), min_frame_us(%d)\n",
1233 __func__, cis_data->sen_vsync_count, input_exposure_time,
1234 frame_duration, cis_data->min_frame_us_time);
1235 dbg_sensor(1, "[%s](vsync cnt = %d) adj duration, frame duraion(%d), min_frame_us(%d)\n",
1236 __func__, cis_data->sen_vsync_count, frame_duration, cis_data->min_frame_us_time);
1237
1238 *target_duration = MAX(frame_duration, cis_data->min_frame_us_time);
1239
1240 #ifdef DEBUG_SENSOR_TIME
1241 do_gettimeofday(&end);
1242 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1243 #endif
1244
1245 return ret;
1246 }
1247
1248 int sensor_2x5sp_cis_set_frame_duration(struct v4l2_subdev *subdev, u32 frame_duration)
1249 {
1250 int ret = 0;
1251 int hold = 0;
1252 struct fimc_is_cis *cis;
1253 struct i2c_client *client;
1254 cis_shared_data *cis_data;
1255
1256 u32 line_length_pck = 0;
1257 u16 frame_length_lines = 0;
1258 u64 numerator;
1259 u32 max_coarse_integration_time = 0;
1260 u8 lte_shifter;
1261
1262 #ifdef DEBUG_SENSOR_TIME
1263 struct timeval st, end;
1264
1265 do_gettimeofday(&st);
1266 #endif
1267
1268 FIMC_BUG(!subdev);
1269
1270 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1271
1272 FIMC_BUG(!cis);
1273 FIMC_BUG(!cis->cis_data);
1274
1275 client = cis->client;
1276 if (unlikely(!client)) {
1277 err("client is NULL");
1278 return -EINVAL;
1279 }
1280
1281 cis_data = cis->cis_data;
1282
1283 if (frame_duration < cis_data->min_frame_us_time) {
1284 dbg_sensor(1, "frame duration is less than min(%d)\n", frame_duration);
1285 frame_duration = cis_data->min_frame_us_time;
1286 }
1287
1288 lte_shifter = cis->long_term_mode.sen_strm_off_on_enable ?
1289 GET_2X5SP_LTE_SHIFT_CNT(frame_duration) : 0;
1290
1291 line_length_pck = cis_data->line_length_pck;
1292 numerator = (u64)cis_data->pclk * frame_duration;
1293 frame_length_lines = (u16)((numerator / line_length_pck) / (1000 * 1000) / (1 << lte_shifter));
1294
1295 dbg_sensor(1, "[MOD:D:%d] %s, vt_pic_clk(%#x) frame_duration = %d us,"
1296 KERN_CONT "(line_length_pck%#x), frame_length_lines(%#x)\n",
1297 cis->id, __func__, cis_data->pclk, frame_duration, line_length_pck, frame_length_lines);
1298
1299 I2C_MUTEX_LOCK(cis->i2c_lock);
1300
1301 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1302 if (hold < 0) {
1303 ret = hold;
1304 goto p_err;
1305 }
1306
1307 ret = fimc_is_sensor_write16(client, 0x0340, frame_length_lines);
1308 if (ret < 0)
1309 goto p_err;
1310
1311 cis_data->cur_frame_us_time = frame_duration;
1312 cis_data->frame_length_lines = frame_length_lines;
1313
1314 max_coarse_integration_time = cis_data->frame_length_lines - cis_data->max_margin_coarse_integration_time;
1315 cis_data->max_coarse_integration_time = max_coarse_integration_time;
1316
1317 #ifdef DEBUG_SENSOR_TIME
1318 do_gettimeofday(&end);
1319 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1320 #endif
1321
1322 p_err:
1323 if (hold > 0) {
1324 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1325 if (hold < 0)
1326 ret = hold;
1327 }
1328
1329 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1330
1331 return ret;
1332 }
1333
1334 int sensor_2x5sp_cis_set_frame_rate(struct v4l2_subdev *subdev, u32 min_fps)
1335 {
1336 int ret = 0;
1337 struct fimc_is_cis *cis;
1338 cis_shared_data *cis_data;
1339
1340 u32 frame_duration = 0;
1341
1342 #ifdef DEBUG_SENSOR_TIME
1343 struct timeval st, end;
1344
1345 do_gettimeofday(&st);
1346 #endif
1347
1348 FIMC_BUG(!subdev);
1349
1350 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1351
1352 FIMC_BUG(!cis);
1353 FIMC_BUG(!cis->cis_data);
1354
1355 cis_data = cis->cis_data;
1356
1357 if (min_fps > cis_data->max_fps) {
1358 err("[MOD:D:%d] %s, request FPS is too high(%d), set to max(%d)\n",
1359 cis->id, __func__, min_fps, cis_data->max_fps);
1360 min_fps = cis_data->max_fps;
1361 }
1362
1363 if (min_fps == 0) {
1364 err("[MOD:D:%d] %s, request FPS is 0, set to min FPS(1)\n",
1365 cis->id, __func__);
1366 min_fps = 1;
1367 }
1368
1369 frame_duration = (1 * 1000 * 1000) / min_fps;
1370
1371 dbg_sensor(1, "[MOD:D:%d] %s, set FPS(%d), frame duration(%d)\n",
1372 cis->id, __func__, min_fps, frame_duration);
1373
1374 ret = sensor_2x5sp_cis_set_frame_duration(subdev, frame_duration);
1375 if (ret < 0) {
1376 err("[MOD:D:%d] %s, set frame duration is fail(%d)\n",
1377 cis->id, __func__, ret);
1378 goto p_err;
1379 }
1380
1381 cis_data->min_frame_us_time = frame_duration;
1382
1383 #ifdef DEBUG_SENSOR_TIME
1384 do_gettimeofday(&end);
1385 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1386 #endif
1387
1388 p_err:
1389
1390 return ret;
1391 }
1392
1393 int sensor_2x5sp_cis_adjust_analog_gain(struct v4l2_subdev *subdev, u32 input_again, u32 *target_permile)
1394 {
1395 int ret = 0;
1396 struct fimc_is_cis *cis;
1397 cis_shared_data *cis_data;
1398
1399 u32 again_code = 0;
1400 u32 again_permile = 0;
1401
1402 #ifdef DEBUG_SENSOR_TIME
1403 struct timeval st, end;
1404
1405 do_gettimeofday(&st);
1406 #endif
1407
1408 FIMC_BUG(!subdev);
1409 FIMC_BUG(!target_permile);
1410
1411 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1412
1413 FIMC_BUG(!cis);
1414 FIMC_BUG(!cis->cis_data);
1415
1416 cis_data = cis->cis_data;
1417
1418 again_code = sensor_cis_calc_again_code(input_again);
1419
1420 if (again_code > cis_data->max_analog_gain[0])
1421 again_code = cis_data->max_analog_gain[0];
1422 else if (again_code < cis_data->min_analog_gain[0])
1423 again_code = cis_data->min_analog_gain[0];
1424
1425 again_permile = sensor_cis_calc_again_permile(again_code);
1426
1427 dbg_sensor(1, "[%s] min again(%d), max(%d), input_again(%d), code(%d), permile(%d)\n", __func__,
1428 cis_data->max_analog_gain[0],
1429 cis_data->min_analog_gain[0],
1430 input_again,
1431 again_code,
1432 again_permile);
1433
1434 *target_permile = again_permile;
1435
1436 return ret;
1437 }
1438
1439 int sensor_2x5sp_cis_set_analog_gain(struct v4l2_subdev *subdev, struct ae_param *again)
1440 {
1441 int ret = 0;
1442 int hold = 0;
1443 struct fimc_is_cis *cis;
1444 struct i2c_client *client;
1445
1446 u16 analog_gain = 0;
1447
1448 #ifdef DEBUG_SENSOR_TIME
1449 struct timeval st, end;
1450
1451 do_gettimeofday(&st);
1452 #endif
1453
1454 FIMC_BUG(!subdev);
1455 FIMC_BUG(!again);
1456
1457 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1458
1459 FIMC_BUG(!cis);
1460
1461 client = cis->client;
1462 if (unlikely(!client)) {
1463 err("client is NULL");
1464 return -EINVAL;
1465 }
1466
1467 analog_gain = (u16)sensor_cis_calc_again_code(again->val);
1468
1469 if (analog_gain < cis->cis_data->min_analog_gain[0])
1470 analog_gain = cis->cis_data->min_analog_gain[0];
1471
1472 if (analog_gain > cis->cis_data->max_analog_gain[0])
1473 analog_gain = cis->cis_data->max_analog_gain[0];
1474
1475 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_again = %d us, analog_gain(%#x)\n",
1476 cis->id, __func__, cis->cis_data->sen_vsync_count, again->long_val, analog_gain);
1477
1478 I2C_MUTEX_LOCK(cis->i2c_lock);
1479
1480 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1481 if (hold < 0) {
1482 ret = hold;
1483 goto p_err;
1484 }
1485
1486 ret = fimc_is_sensor_write16(client, 0x0204, analog_gain);
1487 if (ret < 0)
1488 goto p_err;
1489
1490 #ifdef DEBUG_SENSOR_TIME
1491 do_gettimeofday(&end);
1492 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1493 #endif
1494
1495 p_err:
1496 if (hold > 0) {
1497 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1498 if (hold < 0)
1499 ret = hold;
1500 }
1501
1502 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1503
1504 return ret;
1505 }
1506
1507 int sensor_2x5sp_cis_get_analog_gain(struct v4l2_subdev *subdev, u32 *again)
1508 {
1509 int ret = 0;
1510 int hold = 0;
1511 struct fimc_is_cis *cis;
1512 struct i2c_client *client;
1513
1514 u16 analog_gain = 0;
1515
1516 #ifdef DEBUG_SENSOR_TIME
1517 struct timeval st, end;
1518
1519 do_gettimeofday(&st);
1520 #endif
1521
1522 FIMC_BUG(!subdev);
1523 FIMC_BUG(!again);
1524
1525 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1526
1527 FIMC_BUG(!cis);
1528
1529 client = cis->client;
1530 if (unlikely(!client)) {
1531 err("client is NULL");
1532 return -EINVAL;
1533 }
1534
1535 I2C_MUTEX_LOCK(cis->i2c_lock);
1536
1537 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1538 if (hold < 0) {
1539 ret = hold;
1540 goto p_err;
1541 }
1542
1543 ret = fimc_is_sensor_read16(client, 0x0204, &analog_gain);
1544 if (ret < 0)
1545 goto p_err;
1546
1547 *again = sensor_cis_calc_again_permile(analog_gain);
1548
1549 dbg_sensor(1, "[MOD:D:%d] %s, cur_again = %d us, analog_gain(%#x)\n",
1550 cis->id, __func__, *again, analog_gain);
1551
1552 #ifdef DEBUG_SENSOR_TIME
1553 do_gettimeofday(&end);
1554 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1555 #endif
1556
1557 p_err:
1558 if (hold > 0) {
1559 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1560 if (hold < 0)
1561 ret = hold;
1562 }
1563
1564 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1565
1566 return ret;
1567 }
1568
1569 int sensor_2x5sp_cis_get_min_analog_gain(struct v4l2_subdev *subdev, u32 *min_again)
1570 {
1571 int ret = 0;
1572 struct fimc_is_cis *cis;
1573 struct i2c_client *client;
1574 cis_shared_data *cis_data;
1575
1576 #ifdef DEBUG_SENSOR_TIME
1577 struct timeval st, end;
1578
1579 do_gettimeofday(&st);
1580 #endif
1581
1582 FIMC_BUG(!subdev);
1583 FIMC_BUG(!min_again);
1584
1585 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1586
1587 FIMC_BUG(!cis);
1588 FIMC_BUG(!cis->cis_data);
1589
1590 client = cis->client;
1591 if (unlikely(!client)) {
1592 err("client is NULL");
1593 return -EINVAL;
1594 }
1595
1596 cis_data = cis->cis_data;
1597 cis_data->min_analog_gain[0] = 0x20;
1598 cis_data->min_analog_gain[1] = sensor_cis_calc_again_permile(cis_data->min_analog_gain[0]);
1599
1600 *min_again = cis_data->min_analog_gain[1];
1601
1602 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->min_analog_gain[0], cis_data->min_analog_gain[1]);
1603
1604 #ifdef DEBUG_SENSOR_TIME
1605 do_gettimeofday(&end);
1606 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1607 #endif
1608
1609 return ret;
1610 }
1611
1612 int sensor_2x5sp_cis_get_max_analog_gain(struct v4l2_subdev *subdev, u32 *max_again)
1613 {
1614 int ret = 0;
1615 struct fimc_is_cis *cis;
1616 struct i2c_client *client;
1617 cis_shared_data *cis_data;
1618
1619 #ifdef DEBUG_SENSOR_TIME
1620 struct timeval st, end;
1621
1622 do_gettimeofday(&st);
1623 #endif
1624
1625 FIMC_BUG(!subdev);
1626 FIMC_BUG(!max_again);
1627
1628 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1629
1630 FIMC_BUG(!cis);
1631 FIMC_BUG(!cis->cis_data);
1632
1633 client = cis->client;
1634 if (unlikely(!client)) {
1635 err("client is NULL");
1636 return -EINVAL;
1637 }
1638
1639 cis_data = cis->cis_data;
1640 cis_data->max_analog_gain[0] = 0x200;
1641 cis_data->max_analog_gain[1] = sensor_cis_calc_again_permile(cis_data->max_analog_gain[0]);
1642
1643 *max_again = cis_data->max_analog_gain[1];
1644
1645 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->max_analog_gain[0], cis_data->max_analog_gain[1]);
1646
1647 #ifdef DEBUG_SENSOR_TIME
1648 do_gettimeofday(&end);
1649 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1650 #endif
1651
1652 return ret;
1653 }
1654
1655 int sensor_2x5sp_cis_set_digital_gain(struct v4l2_subdev *subdev, struct ae_param *dgain)
1656 {
1657 int ret = 0;
1658 int hold = 0;
1659 struct fimc_is_cis *cis;
1660 struct i2c_client *client;
1661 cis_shared_data *cis_data;
1662
1663 u16 long_gain = 0;
1664 u16 short_gain = 0;
1665 u16 middle_gain = 0;
1666 u16 dgains[4] = {0};
1667
1668 #ifdef DEBUG_SENSOR_TIME
1669 struct timeval st, end;
1670
1671 do_gettimeofday(&st);
1672 #endif
1673
1674 FIMC_BUG(!subdev);
1675 FIMC_BUG(!dgain);
1676
1677 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1678
1679 FIMC_BUG(!cis);
1680 FIMC_BUG(!cis->cis_data);
1681
1682 client = cis->client;
1683 if (unlikely(!client)) {
1684 err("client is NULL");
1685 return -EINVAL;
1686 }
1687
1688 cis_data = cis->cis_data;
1689
1690 long_gain = (u16)sensor_cis_calc_dgain_code(dgain->long_val);
1691 short_gain = (u16)sensor_cis_calc_dgain_code(dgain->short_val);
1692 middle_gain = (u16)sensor_cis_calc_dgain_code(dgain->middle_val);
1693
1694 if (long_gain < cis->cis_data->min_digital_gain[0])
1695 long_gain = cis->cis_data->min_digital_gain[0];
1696
1697 if (long_gain > cis->cis_data->max_digital_gain[0])
1698 long_gain = cis->cis_data->max_digital_gain[0];
1699
1700 if (short_gain < cis->cis_data->min_digital_gain[0])
1701 short_gain = cis->cis_data->min_digital_gain[0];
1702
1703 if (short_gain > cis->cis_data->max_digital_gain[0])
1704 short_gain = cis->cis_data->max_digital_gain[0];
1705
1706 if (middle_gain < cis->cis_data->min_digital_gain[0])
1707 middle_gain = cis->cis_data->min_digital_gain[0];
1708
1709 if (middle_gain > cis->cis_data->max_digital_gain[0])
1710 middle_gain = cis->cis_data->max_digital_gain[0];
1711
1712 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_dgain = %d/%d/%d us, gain(L:%#x, S:%#x, M:%#x)\n",
1713 cis->id, __func__, cis->cis_data->sen_vsync_count, dgain->long_val,
1714 dgain->short_val, dgain->middle_val, long_gain, short_gain, middle_gain);
1715
1716 I2C_MUTEX_LOCK(cis->i2c_lock);
1717
1718 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1719 if (hold < 0) {
1720 ret = hold;
1721 goto p_err;
1722 }
1723
1724 dgains[0] = dgains[1] = dgains[2] = dgains[3] = short_gain;
1725 /* Short digital gain */
1726 ret = fimc_is_sensor_write16_array(client, 0x020E, dgains, 4);
1727 if (ret < 0)
1728 goto p_err;
1729
1730 /* Long & medium digital gain */
1731 if (cis_data->is_data.wdr_mode != CAMERA_WDR_OFF) {
1732 dgains[0] = dgains[1] = dgains[2] = dgains[3] = long_gain;
1733 /* long digital gain */
1734 ret = fimc_is_sensor_write16_array(client, 0x0230, dgains, 4);
1735 if (ret < 0)
1736 goto p_err;
1737 dgains[0] = dgains[1] = dgains[2] = dgains[3] = middle_gain;
1738 /* middle digital gain */
1739 ret = fimc_is_sensor_write16_array(client, 0x0238, dgains, 4);
1740 if (ret < 0)
1741 goto p_err;
1742 }
1743
1744 #ifdef DEBUG_SENSOR_TIME
1745 do_gettimeofday(&end);
1746 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1747 #endif
1748
1749 p_err:
1750 if (hold > 0) {
1751 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1752 if (hold < 0)
1753 ret = hold;
1754 }
1755
1756 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1757
1758 return ret;
1759 }
1760
1761 int sensor_2x5sp_cis_get_digital_gain(struct v4l2_subdev *subdev, u32 *dgain)
1762 {
1763 int ret = 0;
1764 int hold = 0;
1765 struct fimc_is_cis *cis;
1766 struct i2c_client *client;
1767
1768 u16 digital_gain = 0;
1769
1770 #ifdef DEBUG_SENSOR_TIME
1771 struct timeval st, end;
1772
1773 do_gettimeofday(&st);
1774 #endif
1775
1776 FIMC_BUG(!subdev);
1777 FIMC_BUG(!dgain);
1778
1779 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1780
1781 FIMC_BUG(!cis);
1782
1783 client = cis->client;
1784 if (unlikely(!client)) {
1785 err("client is NULL");
1786 return -EINVAL;
1787 }
1788
1789 I2C_MUTEX_LOCK(cis->i2c_lock);
1790
1791 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1792 if (hold < 0) {
1793 ret = hold;
1794 goto p_err;
1795 }
1796
1797 ret = fimc_is_sensor_read16(client, 0x020E, &digital_gain);
1798 if (ret < 0)
1799 goto p_err;
1800
1801 *dgain = sensor_cis_calc_dgain_permile(digital_gain);
1802
1803 dbg_sensor(1, "[MOD:D:%d] %s, cur_dgain = %d us, digital_gain(%#x)\n",
1804 cis->id, __func__, *dgain, digital_gain);
1805
1806 #ifdef DEBUG_SENSOR_TIME
1807 do_gettimeofday(&end);
1808 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1809 #endif
1810
1811 p_err:
1812 if (hold > 0) {
1813 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1814 if (hold < 0)
1815 ret = hold;
1816 }
1817
1818 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1819
1820 return ret;
1821 }
1822
1823 int sensor_2x5sp_cis_get_min_digital_gain(struct v4l2_subdev *subdev, u32 *min_dgain)
1824 {
1825 int ret = 0;
1826 struct fimc_is_cis *cis;
1827 struct i2c_client *client;
1828 cis_shared_data *cis_data;
1829
1830 #ifdef DEBUG_SENSOR_TIME
1831 struct timeval st, end;
1832
1833 do_gettimeofday(&st);
1834 #endif
1835
1836 FIMC_BUG(!subdev);
1837 FIMC_BUG(!min_dgain);
1838
1839 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1840
1841 FIMC_BUG(!cis);
1842 FIMC_BUG(!cis->cis_data);
1843
1844 client = cis->client;
1845 if (unlikely(!client)) {
1846 err("client is NULL");
1847 return -EINVAL;
1848 }
1849
1850 cis_data = cis->cis_data;
1851 cis_data->min_digital_gain[0] = 0x100;
1852 cis_data->min_digital_gain[1] = sensor_cis_calc_dgain_permile(cis_data->min_digital_gain[0]);
1853
1854 *min_dgain = cis_data->min_digital_gain[1];
1855
1856 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__,
1857 cis_data->min_digital_gain[0], cis_data->min_digital_gain[1]);
1858
1859 #ifdef DEBUG_SENSOR_TIME
1860 do_gettimeofday(&end);
1861 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1862 #endif
1863
1864 return ret;
1865 }
1866
1867 int sensor_2x5sp_cis_get_max_digital_gain(struct v4l2_subdev *subdev, u32 *max_dgain)
1868 {
1869 int ret = 0;
1870 struct fimc_is_cis *cis;
1871 struct i2c_client *client;
1872 cis_shared_data *cis_data;
1873
1874 #ifdef DEBUG_SENSOR_TIME
1875 struct timeval st, end;
1876
1877 do_gettimeofday(&st);
1878 #endif
1879
1880 FIMC_BUG(!subdev);
1881 FIMC_BUG(!max_dgain);
1882
1883 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1884
1885 FIMC_BUG(!cis);
1886 FIMC_BUG(!cis->cis_data);
1887
1888 client = cis->client;
1889 if (unlikely(!client)) {
1890 err("client is NULL");
1891 return -EINVAL;
1892 }
1893
1894 cis_data = cis->cis_data;
1895 cis_data->max_digital_gain[0] = 0x8000;
1896 cis_data->max_digital_gain[1] = sensor_cis_calc_dgain_permile(cis_data->max_digital_gain[0]);
1897
1898 *max_dgain = cis_data->max_digital_gain[1];
1899
1900 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__,
1901 cis_data->max_digital_gain[0], cis_data->max_digital_gain[1]);
1902
1903 #ifdef DEBUG_SENSOR_TIME
1904 do_gettimeofday(&end);
1905 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
1906 #endif
1907
1908 return ret;
1909 }
1910
1911 int sensor_2x5sp_cis_set_wb_gain(struct v4l2_subdev *subdev, struct wb_gains wb_gains)
1912 {
1913 int ret = 0;
1914 int hold = 0;
1915 struct fimc_is_cis *cis;
1916 struct i2c_client *client;
1917 u16 abs_gains[4] = {0, }; /* [0]=gr, [1]=r, [2]=b, [3]=gb */
1918
1919 #ifdef DEBUG_SENSOR_TIME
1920 struct timeval st, end;
1921
1922 do_gettimeofday(&st);
1923 #endif
1924
1925 BUG_ON(!subdev);
1926
1927 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1928
1929 BUG_ON(!cis);
1930 BUG_ON(!cis->cis_data);
1931
1932 if (!cis->use_wb_gain)
1933 return ret;
1934
1935 client = cis->client;
1936 if (unlikely(!client)) {
1937 err("client is NULL");
1938 return -EINVAL;
1939 }
1940
1941 dbg_sensor(1, "[SEN:%d]%s:DDK vlaue: wb_gain_gr(%d), wb_gain_r(%d), wb_gain_b(%d)\n",
1942 cis->id, __func__, wb_gains.gr, wb_gains.r, wb_gains.b, wb_gains.gb);
1943
1944 if (wb_gains.gr == 0 || wb_gains.r == 0 || wb_gains.b == 0 || wb_gains.gb == 0)
1945 return ret;
1946
1947 abs_gains[0] = (u16)((wb_gains.r / 4) & 0xFFFF);
1948 abs_gains[1] = (u16)((wb_gains.gr / 4) & 0xFFFF);
1949 abs_gains[2] = (u16)((wb_gains.b / 4) & 0xFFFF);
1950
1951 dbg_sensor(1, "[SEN:%d]%s, abs_gain_r(0x%4X), abs_gain_gr(0x%4X), abs_gain_b(0x%4X)\n",
1952 cis->id, __func__, abs_gains[0], abs_gains[1], abs_gains[2]);
1953
1954 I2C_MUTEX_LOCK(cis->i2c_lock);
1955
1956 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
1957 if (hold < 0) {
1958 ret = hold;
1959 goto p_err;
1960 }
1961
1962 /* 0x40000D12 ~ 0x400000D16: api_rw_color_temperature_absolute_gain_red/green/blue */
1963 ret = fimc_is_sensor_write16(client, 0x6028, 0x4000);
1964 ret |= fimc_is_sensor_write16_array(client, 0x0D12, abs_gains, 3);
1965 if (ret < 0)
1966 goto p_err;
1967
1968 #ifdef DEBUG_SENSOR_TIME
1969 do_gettimeofday(&end);
1970 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1971 #endif
1972
1973 p_err:
1974 if (hold > 0) {
1975 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
1976 if (hold < 0)
1977 ret = hold;
1978 }
1979
1980 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1981
1982 return ret;
1983 }
1984
1985 int sensor_2x5sp_cis_set_3hdr_roi(struct v4l2_subdev *subdev, struct roi_setting_t roi_control)
1986 {
1987 int ret = 0;
1988 int hold = 0;
1989 struct fimc_is_cis *cis;
1990 struct i2c_client *client;
1991 u16 roi_val[4];
1992 #ifdef DEBUG_SENSOR_TIME
1993 struct timeval st, end;
1994
1995 do_gettimeofday(&st);
1996 #endif
1997
1998 BUG_ON(!subdev);
1999
2000 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
2001
2002 BUG_ON(!cis);
2003 BUG_ON(!cis->cis_data);
2004
2005 if (!cis->use_3hdr)
2006 return ret;
2007
2008 client = cis->client;
2009 if (unlikely(!client)) {
2010 err("client is NULL");
2011 return -EINVAL;
2012 }
2013
2014 dbg_sensor(1, "%s: [MOD:%d] roi_control (start_x:%d, start_y:%d, end_x:%d, end_y:%d)\n",
2015 __func__, cis->id,
2016 roi_control.roi_start_x, roi_control.roi_start_y,
2017 roi_control.roi_end_x, roi_control.roi_end_y);
2018
2019 I2C_MUTEX_LOCK(cis->i2c_lock);
2020
2021 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
2022 if (hold < 0) {
2023 ret = hold;
2024 goto p_err;
2025 }
2026
2027 /* 0x2000_XXXX */
2028 ret = fimc_is_sensor_write16(client, 0x6028, 0x2000);
2029 if (ret < 0)
2030 goto p_err;
2031
2032 /* t_isp_rgby_hist_mem_cfg_active_window_percent_14bit */
2033 roi_val[0] = roi_control.roi_start_x; /* 0x20004B26: top_left_x */
2034 roi_val[1] = roi_control.roi_start_y; /* 0x20004B28: top_left_y */
2035 roi_val[2] = roi_control.roi_end_x; /* 0x20004B2A: bot_right_x */
2036 roi_val[3] = roi_control.roi_end_y; /* 0x20004B2C: bot_right_y */
2037
2038 ret = fimc_is_sensor_write16_array(client, 0x4B26, roi_val, 4);
2039 if (ret < 0)
2040 goto p_err;
2041
2042 /* t_isp_rgby_hist_grid_grid & thstat_grid_area */
2043 roi_val[0] = roi_control.roi_end_x - roi_control.roi_start_x; /* 0x20004BEE & 0x20004E44: width */
2044 roi_val[1] = roi_control.roi_end_y - roi_control.roi_start_y; /* 0x20004BF0 & 0x20004E46: height */
2045 roi_val[2] = (roi_control.roi_start_x + roi_control.roi_end_x) / 2; /* center_x */
2046 roi_val[3] = (roi_control.roi_start_y + roi_control.roi_end_y) / 2; /* center_y */
2047
2048 ret = fimc_is_sensor_write16_array(client, 0x4BEE, roi_val, 4);
2049 if (ret < 0)
2050 goto p_err;
2051
2052 ret = fimc_is_sensor_write16_array(client, 0x4E44, roi_val, 2);
2053 if (ret < 0)
2054 goto p_err;
2055
2056 /* restore 0x4000_XXXX */
2057 ret = fimc_is_sensor_write16(client, 0x6028, 0x4000);
2058 if (ret < 0)
2059 goto p_err;
2060
2061 #ifdef DEBUG_SENSOR_TIME
2062 do_gettimeofday(&end);
2063 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
2064 #endif
2065
2066 p_err:
2067 if (hold > 0) {
2068 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
2069 if (hold < 0)
2070 ret = hold;
2071 }
2072
2073 I2C_MUTEX_UNLOCK(cis->i2c_lock);
2074
2075 return ret;
2076 }
2077
2078 int sensor_2x5sp_cis_set_3hdr_stat(struct v4l2_subdev *subdev, bool streaming, void *data)
2079 {
2080 int ret = 0;
2081 int hold = 0;
2082 struct fimc_is_cis *cis;
2083 struct i2c_client *client;
2084 u16 weight[3];
2085 u16 low_gate_thr, high_gate_thr;
2086 struct roi_setting_t y_sum_roi;
2087 struct sensor_lsi_3hdr_stat_control_mode_change mode_change_stat;
2088 struct sensor_lsi_3hdr_stat_control_per_frame per_frame_stat;
2089 #ifdef DEBUG_SENSOR_TIME
2090 struct timeval st, end;
2091
2092 do_gettimeofday(&st);
2093 #endif
2094
2095 FIMC_BUG(!subdev);
2096 FIMC_BUG(!data);
2097
2098 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
2099
2100 BUG_ON(!cis);
2101 BUG_ON(!cis->cis_data);
2102
2103 if (!cis->use_3hdr)
2104 return ret;
2105
2106 client = cis->client;
2107 if (unlikely(!client)) {
2108 err("client is NULL");
2109 return -EINVAL;
2110 }
2111
2112 I2C_MUTEX_LOCK(cis->i2c_lock);
2113
2114 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x01);
2115 if (hold < 0) {
2116 ret = hold;
2117 goto p_err;
2118 }
2119
2120 if (streaming) {
2121 per_frame_stat = *(struct sensor_lsi_3hdr_stat_control_per_frame *)data;
2122
2123 weight[0] = per_frame_stat.r_weight;
2124 weight[1] = per_frame_stat.g_weight;
2125 weight[2] = per_frame_stat.b_weight;
2126 } else {
2127 mode_change_stat = *(struct sensor_lsi_3hdr_stat_control_mode_change *)data;
2128
2129 weight[0] = mode_change_stat.r_weight;
2130 weight[1] = mode_change_stat.g_weight;
2131 weight[2] = mode_change_stat.b_weight;
2132
2133 low_gate_thr = mode_change_stat.low_gate_thr;
2134 high_gate_thr = mode_change_stat.high_gate_thr;
2135
2136 y_sum_roi = mode_change_stat.y_sum_roi;
2137 }
2138
2139 ret = fimc_is_sensor_write16(client, 0x6028, 0x2000);
2140 if (ret < 0)
2141 goto p_err;
2142
2143 /* t_isp_rgby_hist_short_exp_weight */
2144 ret = fimc_is_sensor_write16_array(client, 0x4BBC, weight, 3);
2145 if (ret < 0)
2146 goto p_err;
2147
2148 /* t_isp_rgby_hist_long_exp_weight */
2149 ret = fimc_is_sensor_write16_array(client, 0x4BCA, weight, 3);
2150 if (ret < 0)
2151 goto p_err;
2152
2153 /* t_isp_rgby_hist_medium_exp_weight */
2154 ret = fimc_is_sensor_write16_array(client, 0x4BD8, weight, 3);
2155 if (ret < 0)
2156 goto p_err;
2157
2158 /* t_isp_rgby_hist_mixed_exp_weight */
2159 ret = fimc_is_sensor_write16_array(client, 0x4BE6, weight, 3);
2160 if (ret < 0)
2161 goto p_err;
2162
2163 /* t_isp_drc_thstat_rgb_weights */
2164 ret = fimc_is_sensor_write16_array(client, 0x4E2C, weight, 3);
2165 if (ret < 0)
2166 goto p_err;
2167
2168 if (!streaming) {
2169 /* t_isp_drc_thstat_u_low_tresh_red */
2170 ret = fimc_is_sensor_write16(client, 0x4E1A, low_gate_thr);
2171 if (ret < 0)
2172 goto p_err;
2173 /* t_isp_drc_thstat_u_high_tresh_red */
2174 ret = fimc_is_sensor_write16(client, 0x4E1C, high_gate_thr);
2175 if (ret < 0)
2176 goto p_err;
2177
2178 /* t_isp_drc_thstat_u_low_tresh_green */
2179 ret = fimc_is_sensor_write16(client, 0x4E1E, low_gate_thr);
2180 if (ret < 0)
2181 goto p_err;
2182
2183 /* t_isp_drc_thstat_u_high_tresh_green */
2184 ret = fimc_is_sensor_write16(client, 0x4E20, high_gate_thr);
2185 if (ret < 0)
2186 goto p_err;
2187
2188 /* t_isp_drc_thstat_u_low_tresh_blue */
2189 ret = fimc_is_sensor_write16(client, 0x4E22, low_gate_thr);
2190 if (ret < 0)
2191 goto p_err;
2192
2193 /* t_isp_drc_thstat_u_high_tresh_blue */
2194 ret = fimc_is_sensor_write16(client, 0x4E24, high_gate_thr);
2195 if (ret < 0)
2196 goto p_err;
2197
2198 /* t_isp_y_sum_top_left_x */
2199 ret = fimc_is_sensor_write16(client, 0x4E04, y_sum_roi.roi_start_x);
2200 if (ret < 0)
2201 goto p_err;
2202 ret = fimc_is_sensor_write16(client, 0x4E06, y_sum_roi.roi_start_y);
2203 if (ret < 0)
2204 goto p_err;
2205 } else {
2206 /* update 3hdr motion stat */
2207 ret = fimc_is_sensor_write16(client, 0x6028, 0x2001);
2208 if (ret < 0)
2209 goto p_err;
2210
2211 ret |= fimc_is_sensor_write16(client, 0x602A, 0x29D8);
2212 ret |= fimc_is_sensor_write16(client, 0x6F12,
2213 (u16)per_frame_stat.motion_indication);
2214
2215 ret |= fimc_is_sensor_write16(client, 0x602A, 0x2A52);
2216 ret |= fimc_is_sensor_write16(client, 0x6F12,
2217 (u16)per_frame_stat.motion_high_end_ty2ty1);
2218 ret |= fimc_is_sensor_write16(client, 0x6F12,
2219 (u16)per_frame_stat.motion_high_end_ty3ty2);
2220 ret |= fimc_is_sensor_write16(client, 0x6F12,
2221 (u16)per_frame_stat.motion_high_start_ty2ty1);
2222 ret |= fimc_is_sensor_write16(client, 0x6F12,
2223 (u16)per_frame_stat.motion_high_start_ty3ty2);
2224 ret |= fimc_is_sensor_write16(client, 0x6F12,
2225 (u16)per_frame_stat.motion_low_end_ty2ty1);
2226 ret |= fimc_is_sensor_write16(client, 0x6F12,
2227 (u16)per_frame_stat.motion_low_end_ty3ty2);
2228 ret |= fimc_is_sensor_write16(client, 0x6F12,
2229 (u16)per_frame_stat.motion_low_start_ty2ty1);
2230 ret |= fimc_is_sensor_write16(client, 0x6F12,
2231 (u16)per_frame_stat.motion_low_start_ty3ty2);
2232
2233 dbg_sensor(2, "[%s] motion idc(%d) high21(e:%d, s:%d), low21(e:%d, s:%d)\n",
2234 __func__,
2235 (u16)per_frame_stat.motion_indication,
2236 (u16)per_frame_stat.motion_high_end_ty2ty1,
2237 (u16)per_frame_stat.motion_high_start_ty2ty1,
2238 (u16)per_frame_stat.motion_low_end_ty2ty1,
2239 (u16)per_frame_stat.motion_low_start_ty2ty1);
2240 dbg_sensor(2, "[%s] motion high32(e:%d, s:%d), low32(e:%d, s:%d)\n",
2241 __func__,
2242 (u16)per_frame_stat.motion_high_end_ty3ty2,
2243 (u16)per_frame_stat.motion_high_start_ty3ty2,
2244 (u16)per_frame_stat.motion_low_end_ty3ty2,
2245 (u16)per_frame_stat.motion_low_start_ty3ty2);
2246
2247 ret |= fimc_is_sensor_write16(client, 0x602A, 0x2A68);
2248 ret |= fimc_is_sensor_write16(client, 0x6F12,
2249 (u16)per_frame_stat.decision_thresh_override);
2250 ret |= fimc_is_sensor_write16(client, 0x6F12,
2251 (u16)per_frame_stat.motion_abs_high_ty3ty2);
2252 ret |= fimc_is_sensor_write16(client, 0x6F12,
2253 (u16)per_frame_stat.motion_abs_low_ty3ty2);
2254 ret |= fimc_is_sensor_write16(client, 0x6F12,
2255 (u16)per_frame_stat.motion_abs_high_ty2ty1);
2256 ret |= fimc_is_sensor_write16(client, 0x6F12,
2257 (u16)per_frame_stat.motion_abs_low_ty2ty1);
2258
2259 dbg_sensor(2, "[%s] motion DTO(%d), abs(h32:%d, l32:%d), abs(h21:%d, l21:%d)\n",
2260 __func__,
2261 (u16)per_frame_stat.decision_thresh_override,
2262 (u16)per_frame_stat.motion_abs_high_ty3ty2,
2263 (u16)per_frame_stat.motion_abs_low_ty3ty2,
2264 (u16)per_frame_stat.motion_abs_high_ty2ty1,
2265 (u16)per_frame_stat.motion_abs_low_ty2ty1);
2266 }
2267
2268 /* restore 0x4000_XXXX */
2269 ret = fimc_is_sensor_write16(client, 0x6028, 0x4000);
2270 if (ret < 0)
2271 goto p_err;
2272
2273 #ifdef DEBUG_SENSOR_TIME
2274 do_gettimeofday(&end);
2275 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
2276 #endif
2277
2278 p_err:
2279 if (hold > 0) {
2280 hold = sensor_2x5sp_cis_group_param_hold_func(subdev, 0x00);
2281 if (hold < 0)
2282 ret = hold;
2283 }
2284
2285 I2C_MUTEX_UNLOCK(cis->i2c_lock);
2286
2287 return ret;
2288 }
2289
2290 void sensor_2x5sp_cis_check_wdr_mode(struct v4l2_subdev *subdev, u32 mode_idx)
2291 {
2292 struct fimc_is_cis *cis;
2293
2294 FIMC_BUG_VOID(!subdev);
2295
2296 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
2297
2298 FIMC_BUG_VOID(!cis);
2299 FIMC_BUG_VOID(!cis->cis_data);
2300
2301 /* check wdr mode */
2302 if (mode_idx == SENSOR_2X5SP_2880X2160_30FPS_3DHDR)
2303 cis->cis_data->is_data.wdr_enable = true;
2304 else
2305 cis->cis_data->is_data.wdr_enable = false;
2306
2307 dbg_sensor(1, "[%s] wdr_enable: %d\n", __func__,
2308 cis->cis_data->is_data.wdr_enable);
2309 }
2310
2311 int sensor_2x5sp_cis_long_term_exposure(struct v4l2_subdev *subdev)
2312 {
2313 int ret = 0;
2314 struct fimc_is_cis *cis;
2315 struct fimc_is_long_term_expo_mode *lte_mode;
2316 u8 shift_count;
2317
2318 WARN_ON(!subdev);
2319
2320 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
2321 lte_mode = &cis->long_term_mode;
2322
2323 I2C_MUTEX_LOCK(cis->i2c_lock);
2324 /* LTE mode or normal mode set */
2325 if (lte_mode->sen_strm_off_on_enable) {
2326 shift_count = GET_2X5SP_LTE_SHIFT_CNT(lte_mode->expo[0]);
2327 ret |= fimc_is_sensor_write16(cis->client, 0xFCFC, 0x4000);
2328 ret |= fimc_is_sensor_write8(cis->client, 0x0702, shift_count);
2329 ret |= fimc_is_sensor_write8(cis->client, 0x0704, shift_count);
2330 } else {
2331 ret |= fimc_is_sensor_write16(cis->client, 0xFCFC, 0x4000);
2332 ret |= fimc_is_sensor_write8(cis->client, 0x0702, 0);
2333 ret |= fimc_is_sensor_write8(cis->client, 0x0704, 0);
2334 }
2335
2336 I2C_MUTEX_UNLOCK(cis->i2c_lock);
2337
2338 info("%s enable(%d)", __func__, lte_mode->sen_strm_off_on_enable);
2339
2340 if (ret < 0) {
2341 pr_err("ERR[%s]: LTE register setting fail\n", __func__);
2342 return ret;
2343 }
2344
2345 return ret;
2346 }
2347
2348 static struct fimc_is_cis_ops cis_ops = {
2349 .cis_init = sensor_2x5sp_cis_init,
2350 .cis_log_status = sensor_2x5sp_cis_log_status,
2351 .cis_group_param_hold = sensor_2x5sp_cis_group_param_hold,
2352 .cis_set_global_setting = sensor_2x5sp_cis_set_global_setting,
2353 .cis_mode_change = sensor_2x5sp_cis_mode_change,
2354 .cis_set_size = sensor_2x5sp_cis_set_size,
2355 .cis_stream_on = sensor_2x5sp_cis_stream_on,
2356 .cis_stream_off = sensor_2x5sp_cis_stream_off,
2357 .cis_set_exposure_time = sensor_2x5sp_cis_set_exposure_time,
2358 .cis_get_min_exposure_time = sensor_2x5sp_cis_get_min_exposure_time,
2359 .cis_get_max_exposure_time = sensor_2x5sp_cis_get_max_exposure_time,
2360 .cis_adjust_frame_duration = sensor_2x5sp_cis_adjust_frame_duration,
2361 .cis_set_frame_duration = sensor_2x5sp_cis_set_frame_duration,
2362 .cis_set_frame_rate = sensor_2x5sp_cis_set_frame_rate,
2363 .cis_adjust_analog_gain = sensor_2x5sp_cis_adjust_analog_gain,
2364 .cis_set_analog_gain = sensor_2x5sp_cis_set_analog_gain,
2365 .cis_get_analog_gain = sensor_2x5sp_cis_get_analog_gain,
2366 .cis_get_min_analog_gain = sensor_2x5sp_cis_get_min_analog_gain,
2367 .cis_get_max_analog_gain = sensor_2x5sp_cis_get_max_analog_gain,
2368 .cis_set_digital_gain = sensor_2x5sp_cis_set_digital_gain,
2369 .cis_get_digital_gain = sensor_2x5sp_cis_get_digital_gain,
2370 .cis_get_min_digital_gain = sensor_2x5sp_cis_get_min_digital_gain,
2371 .cis_get_max_digital_gain = sensor_2x5sp_cis_get_max_digital_gain,
2372 .cis_compensate_gain_for_extremely_br = sensor_cis_compensate_gain_for_extremely_br,
2373 .cis_wait_streamoff = sensor_cis_wait_streamoff,
2374 .cis_wait_streamon = sensor_cis_wait_streamon,
2375 .cis_set_initial_exposure = sensor_cis_set_initial_exposure,
2376 .cis_check_rev = sensor_2x5sp_cis_check_rev,
2377 .cis_factory_test = sensor_cis_factory_test,
2378 .cis_set_wb_gains = sensor_2x5sp_cis_set_wb_gain,
2379 .cis_set_roi_stat = sensor_2x5sp_cis_set_3hdr_roi,
2380 .cis_set_3hdr_stat = sensor_2x5sp_cis_set_3hdr_stat,
2381 .cis_check_wdr_mode = sensor_2x5sp_cis_check_wdr_mode,
2382 .cis_set_long_term_exposure = sensor_2x5sp_cis_long_term_exposure,
2383 .cis_mode_change_throttling = sensor_2x5sp_cis_mode_change_throttling,
2384 };
2385
2386 static int cis_2x5sp_probe(struct i2c_client *client,
2387 const struct i2c_device_id *id)
2388 {
2389 int ret = 0, i;
2390 struct fimc_is_core *core = NULL;
2391 struct v4l2_subdev *subdev_cis = NULL;
2392 struct fimc_is_cis *cis = NULL;
2393 struct fimc_is_device_sensor *device = NULL;
2394 struct fimc_is_device_sensor_peri *sensor_peri = NULL;
2395 u32 sensor_id = 0;
2396 char const *setfile;
2397 struct device *dev;
2398 struct device_node *dnode;
2399
2400 FIMC_BUG(!client);
2401 FIMC_BUG(!fimc_is_dev);
2402
2403 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
2404 if (!core) {
2405 probe_info("core device is not yet probed");
2406 return -EPROBE_DEFER;
2407 }
2408
2409 dev = &client->dev;
2410 dnode = dev->of_node;
2411
2412 ret = of_property_read_u32(dnode, "id", &sensor_id);
2413 if (ret) {
2414 err("sensor id read is fail(%d)", ret);
2415 goto p_err;
2416 }
2417
2418 probe_info("%s sensor id %d\n", __func__, sensor_id);
2419
2420 device = &core->sensor[sensor_id];
2421
2422 sensor_peri = find_peri_by_cis_id(device, SENSOR_NAME_S5K2X5SP);
2423 if (!sensor_peri) {
2424 probe_info("sensor peri is net yet probed");
2425 return -EPROBE_DEFER;
2426 }
2427
2428 cis = &sensor_peri->cis;
2429 if (!cis) {
2430 err("cis is NULL");
2431 ret = -ENOMEM;
2432 goto p_err;
2433 }
2434
2435 subdev_cis = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
2436 if (!subdev_cis) {
2437 probe_err("subdev_cis NULL");
2438 ret = -ENOMEM;
2439 goto p_err;
2440 }
2441 sensor_peri->subdev_cis = subdev_cis;
2442
2443 cis->id = SENSOR_NAME_S5K2X5SP;
2444 cis->subdev = subdev_cis;
2445 cis->device = 0;
2446 cis->client = client;
2447 sensor_peri->module->client = cis->client;
2448 cis->ctrl_delay = N_PLUS_TWO_FRAME;
2449
2450 cis->cis_data = kzalloc(sizeof(cis_shared_data), GFP_KERNEL);
2451 if (!cis->cis_data) {
2452 err("cis_data is NULL");
2453 ret = -ENOMEM;
2454 goto p_err;
2455 }
2456 cis->cis_ops = &cis_ops;
2457
2458 /* belows are depend on sensor cis. MUST check sensor spec */
2459 cis->bayer_order = OTF_INPUT_ORDER_BAYER_GR_BG;
2460
2461 if (of_property_read_bool(dnode, "sensor_f_number")) {
2462 ret = of_property_read_u32(dnode, "sensor_f_number", &cis->aperture_num);
2463 if (ret)
2464 warn("f-number read is fail(%d)", ret);
2465 } else {
2466 cis->aperture_num = F2_2;
2467 }
2468
2469 probe_info("%s f-number %d\n", __func__, cis->aperture_num);
2470
2471 cis->use_dgain = true;
2472 cis->hdr_ctrl_by_again = false;
2473 cis->use_wb_gain = true;
2474 cis->use_3hdr = true;
2475
2476 ret = of_property_read_string(dnode, "setfile", &setfile);
2477 if (ret) {
2478 err("setfile index read fail(%d), take default setfile!!", ret);
2479 setfile = "default";
2480 }
2481
2482 if (strcmp(setfile, "default") == 0 ||
2483 strcmp(setfile, "setA") == 0) {
2484 probe_info("%s setfile_A\n", __func__);
2485 sensor_2x5sp_global = sensor_2x5sp_setfile_A_Global;
2486 sensor_2x5sp_global_size = ARRAY_SIZE(sensor_2x5sp_setfile_A_Global);
2487 sensor_2x5sp_setfiles = sensor_2x5sp_setfiles_A;
2488 sensor_2x5sp_setfile_sizes = sensor_2x5sp_setfile_A_sizes;
2489 sensor_2x5sp_otp_initial = sensor_2x5sp_setfiles_A_otp_initial;
2490 sensor_2x5sp_otp_initial_size = ARRAY_SIZE(sensor_2x5sp_setfiles_A_otp_initial);
2491 sensor_2x5sp_pllinfos = sensor_2x5sp_pllinfos_A;
2492 sensor_2x5sp_max_setfile_num = ARRAY_SIZE(sensor_2x5sp_setfiles_A);
2493 } else if (strcmp(setfile, "setB") == 0) {
2494 probe_info("%s setfile_B\n", __func__);
2495 sensor_2x5sp_global = sensor_2x5sp_setfile_B_Global;
2496 sensor_2x5sp_global_size = ARRAY_SIZE(sensor_2x5sp_setfile_B_Global);
2497 sensor_2x5sp_setfiles = sensor_2x5sp_setfiles_B;
2498 sensor_2x5sp_setfile_sizes = sensor_2x5sp_setfile_B_sizes;
2499 sensor_2x5sp_otp_initial = sensor_2x5sp_setfiles_A_otp_initial;
2500 sensor_2x5sp_otp_initial_size = ARRAY_SIZE(sensor_2x5sp_setfiles_A_otp_initial);
2501 sensor_2x5sp_pllinfos = sensor_2x5sp_pllinfos_B;
2502 sensor_2x5sp_max_setfile_num = ARRAY_SIZE(sensor_2x5sp_setfiles_B);
2503
2504 /* throttling setting */
2505 sensor_2x5sp_setfile_throttling = sensor_2x5sp_setfile_B_2880x2160_15fps;
2506 sensor_2x5sp_setfile_throttling_size = ARRAY_SIZE(sensor_2x5sp_setfile_B_2880x2160_15fps);
2507 sensor_2x5sp_pllinfo_throttling = &sensor_2x5sp_pllinfo_B_2880x2160_15fps;
2508 } else {
2509 err("%s setfile index out of bound, take default (setfile_A)", __func__);
2510 sensor_2x5sp_global = sensor_2x5sp_setfile_A_Global;
2511 sensor_2x5sp_global_size = ARRAY_SIZE(sensor_2x5sp_setfile_A_Global);
2512 sensor_2x5sp_setfiles = sensor_2x5sp_setfiles_A;
2513 sensor_2x5sp_setfile_sizes = sensor_2x5sp_setfile_A_sizes;
2514 sensor_2x5sp_pllinfos = sensor_2x5sp_pllinfos_A;
2515 sensor_2x5sp_max_setfile_num = ARRAY_SIZE(sensor_2x5sp_setfiles_A);
2516 }
2517
2518 cis->use_initial_ae = of_property_read_bool(dnode, "use_initial_ae");
2519 probe_info("%s use initial_ae(%d)\n", __func__, cis->use_initial_ae);
2520
2521 device->use_otp_cal = of_property_read_bool(dnode, "use_otp_cal");
2522 probe_info("%s use otp_cal(%d)\n", __func__, device->use_otp_cal);
2523
2524 for (i = 0; i < CAMERA_CRC_INDEX_MAX; i++)
2525 device->cal_status[i] = CRC_NO_ERROR;
2526
2527 v4l2_i2c_subdev_init(subdev_cis, client, &subdev_ops);
2528 v4l2_set_subdevdata(subdev_cis, cis);
2529 v4l2_set_subdev_hostdata(subdev_cis, device);
2530 snprintf(subdev_cis->name, V4L2_SUBDEV_NAME_SIZE, "cis-subdev.%d", cis->id);
2531
2532 probe_info("%s done\n", __func__);
2533
2534 p_err:
2535 return ret;
2536 }
2537
2538 static const struct of_device_id sensor_cis_2x5sp_match[] = {
2539 {
2540 .compatible = "samsung,exynos5-fimc-is-cis-2x5sp",
2541 },
2542 {},
2543 };
2544 MODULE_DEVICE_TABLE(of, sensor_cis_2x5sp_match);
2545
2546 static const struct i2c_device_id sensor_cis_2x5sp_idt[] = {
2547 { SENSOR_NAME, 0 },
2548 {},
2549 };
2550
2551 static struct i2c_driver sensor_cis_2x5sp_driver = {
2552 .probe = cis_2x5sp_probe,
2553 .driver = {
2554 .name = SENSOR_NAME,
2555 .owner = THIS_MODULE,
2556 .of_match_table = sensor_cis_2x5sp_match,
2557 .suppress_bind_attrs = true,
2558 },
2559 .id_table = sensor_cis_2x5sp_idt
2560 };
2561
2562 static int __init sensor_cis_2x5sp_init(void)
2563 {
2564 int ret;
2565
2566 ret = i2c_add_driver(&sensor_cis_2x5sp_driver);
2567 if (ret)
2568 err("failed to add %s driver: %d\n",
2569 sensor_cis_2x5sp_driver.driver.name, ret);
2570
2571 return ret;
2572 }
2573 late_initcall_sync(sensor_cis_2x5sp_init);