2 * Samsung Exynos5 SoC series Sensor driver
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd
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.
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>
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-12a10.h"
40 #include "fimc-is-cis-12a10-setA.h"
41 #include "fimc-is-helper-i2c.h"
43 #define SENSOR_NAME "OV12A10"
44 /* #define DEBUG_12A10_PLL */
46 static const struct v4l2_subdev_ops subdev_ops
;
48 static const u32
*sensor_12a10_global
;
49 static u32 sensor_12a10_global_size
;
50 static const u32
**sensor_12a10_setfiles
;
51 static const u32
*sensor_12a10_setfile_sizes
;
52 static const struct sensor_pll_info_compact
**sensor_12a10_pllinfos
;
53 static const struct sensor_crop_info
**sensor_12a10_crop_infos
;
54 static u32 sensor_12a10_max_setfile_num
;
56 static void sensor_12a10_cis_data_calculation(const struct sensor_pll_info_compact
*pll_info_compact
, cis_shared_data
*cis_data
)
58 u32 vt_pix_clk_hz
= 0;
59 u32 frame_rate
= 0, frame_valid_us
= 0;
62 FIMC_BUG_VOID(!pll_info_compact
);
64 /* 1. get pclk value from pll info */
65 vt_pix_clk_hz
= pll_info_compact
->pclk
;
67 dbg_sensor(1, "ext_clock(%d), mipi_datarate(%d), pclk(%d)\n",
68 pll_info_compact
->ext_clk
, pll_info_compact
->mipi_datarate
, pll_info_compact
->pclk
);
70 /* 2. the time of processing one frame calculation (us) */
71 cis_data
->min_frame_us_time
= (pll_info_compact
->frame_length_lines
* pll_info_compact
->line_length_pck
72 / (vt_pix_clk_hz
/ (1000 * 1000)));
73 cis_data
->cur_frame_us_time
= cis_data
->min_frame_us_time
;
75 /* 3. FPS calculation */
76 frame_rate
= vt_pix_clk_hz
/ (pll_info_compact
->frame_length_lines
* pll_info_compact
->line_length_pck
);
77 dbg_sensor(1, "frame_rate (%d) = vt_pix_clk_hz(%d) / "
78 KERN_CONT
"(pll_info_compact->frame_length_lines(%d) * pll_info_compact->line_length_pck(%d))\n",
79 frame_rate
, vt_pix_clk_hz
, pll_info_compact
->frame_length_lines
, pll_info_compact
->line_length_pck
);
81 /* calculate max fps */
82 max_fps
= ((u64
)vt_pix_clk_hz
* 10) / (pll_info_compact
->frame_length_lines
* pll_info_compact
->line_length_pck
);
83 max_fps
= (max_fps
% 10 >= 5 ? frame_rate
+ 1 : frame_rate
);
85 cis_data
->pclk
= vt_pix_clk_hz
;
86 cis_data
->max_fps
= (u32
)max_fps
;
87 cis_data
->frame_length_lines
= pll_info_compact
->frame_length_lines
;
88 cis_data
->line_length_pck
= pll_info_compact
->line_length_pck
;
89 cis_data
->line_readOut_time
= sensor_cis_do_div64((u64
)cis_data
->line_length_pck
* (u64
)(1000 * 1000 * 1000), cis_data
->pclk
);
90 cis_data
->rolling_shutter_skew
= (cis_data
->cur_height
- 1) * cis_data
->line_readOut_time
;
91 cis_data
->stream_on
= false;
93 /* Frame valid time calcuration */
94 frame_valid_us
= sensor_cis_do_div64((u64
)cis_data
->cur_height
* (u64
)cis_data
->line_length_pck
* (u64
)(1000 * 1000), cis_data
->pclk
);
95 cis_data
->frame_valid_us_time
= (int)frame_valid_us
;
97 dbg_sensor(1, "%s\n", __func__
);
98 dbg_sensor(1, "Sensor size(%d x %d) setting: SUCCESS!\n",
99 cis_data
->cur_width
, cis_data
->cur_height
);
100 dbg_sensor(1, "Frame Valid(us): %d\n", frame_valid_us
);
101 dbg_sensor(1, "rolling_shutter_skew: %lld\n", cis_data
->rolling_shutter_skew
);
103 dbg_sensor(1, "Fps: %d, max fps(%d)\n", frame_rate
, cis_data
->max_fps
);
104 dbg_sensor(1, "min_frame_time(%d us)\n", cis_data
->min_frame_us_time
);
105 dbg_sensor(1, "Pixel rate(Mbps): %d\n", cis_data
->pclk
/ 1000000);
107 /* Frame period calculation */
108 cis_data
->frame_time
= (cis_data
->line_readOut_time
* cis_data
->cur_height
/ 1000);
109 cis_data
->rolling_shutter_skew
= (cis_data
->cur_height
- 1) * cis_data
->line_readOut_time
;
111 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__
,
112 cis_data
->frame_time
, cis_data
->rolling_shutter_skew
);
114 /* Constant values */
115 cis_data
->min_fine_integration_time
= SENSOR_12A10_FINE_INTEGRATION_TIME_MIN
;
116 cis_data
->max_fine_integration_time
= cis_data
->cur_width
;
117 cis_data
->min_coarse_integration_time
= SENSOR_12A10_COARSE_INTEGRATION_TIME_MIN
;
118 cis_data
->max_margin_coarse_integration_time
= SENSOR_12A10_COARSE_INTEGRATION_TIME_MAX_MARGIN
;
121 int sensor_12a10_cis_check_rev(struct v4l2_subdev
*subdev
)
125 struct i2c_client
*client
;
126 struct fimc_is_cis
*cis
= NULL
;
130 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
132 WARN_ON(!cis
->cis_data
);
134 client
= cis
->client
;
135 if (unlikely(!client
)) {
136 err("client is NULL");
140 memset(cis
->cis_data
, 0, sizeof(cis_shared_data
));
141 cis
->rev_flag
= false;
143 I2C_MUTEX_LOCK(cis
->i2c_lock
);
145 ret
= fimc_is_sensor_read8(client
, 0x300B, &rev
);
147 cis
->rev_flag
= true;
150 cis
->cis_data
->cis_rev
= rev
;
151 pr_info("%s : Rev. 0x%X\n", __func__
, rev
);
154 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
159 int sensor_12a10_cis_init(struct v4l2_subdev
*subdev
)
162 struct fimc_is_cis
*cis
;
163 u32 setfile_index
= 0;
164 cis_setting_info setinfo
;
166 setinfo
.return_value
= 0;
168 setinfo
.param
= NULL
;
172 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
179 FIMC_BUG(!cis
->cis_data
);
180 memset(cis
->cis_data
, 0, sizeof(cis_shared_data
));
182 cis
->cis_data
->cur_width
= SENSOR_12A10_MAX_WIDTH
;
183 cis
->cis_data
->cur_height
= SENSOR_12A10_MAX_HEIGHT
;
184 cis
->cis_data
->low_expo_start
= 33000;
185 cis
->need_mode_change
= false;
187 sensor_12a10_cis_data_calculation(sensor_12a10_pllinfos
[setfile_index
], cis
->cis_data
);
189 setinfo
.return_value
= 0;
190 CALL_CISOPS(cis
, cis_get_min_exposure_time
, subdev
, &setinfo
.return_value
);
191 dbg_sensor(1, "[%s] min exposure time : %d\n", __func__
, setinfo
.return_value
);
192 setinfo
.return_value
= 0;
193 CALL_CISOPS(cis
, cis_get_max_exposure_time
, subdev
, &setinfo
.return_value
);
194 dbg_sensor(1, "[%s] max exposure time : %d\n", __func__
, setinfo
.return_value
);
195 setinfo
.return_value
= 0;
196 CALL_CISOPS(cis
, cis_get_min_analog_gain
, subdev
, &setinfo
.return_value
);
197 dbg_sensor(1, "[%s] min again : %d\n", __func__
, setinfo
.return_value
);
198 setinfo
.return_value
= 0;
199 CALL_CISOPS(cis
, cis_get_max_analog_gain
, subdev
, &setinfo
.return_value
);
200 dbg_sensor(1, "[%s] max again : %d\n", __func__
, setinfo
.return_value
);
201 setinfo
.return_value
= 0;
202 CALL_CISOPS(cis
, cis_get_min_digital_gain
, subdev
, &setinfo
.return_value
);
203 dbg_sensor(1, "[%s] min dgain : %d\n", __func__
, setinfo
.return_value
);
204 setinfo
.return_value
= 0;
205 CALL_CISOPS(cis
, cis_get_max_digital_gain
, subdev
, &setinfo
.return_value
);
206 dbg_sensor(1, "[%s] max dgain : %d\n", __func__
, setinfo
.return_value
);
208 #ifdef DEBUG_SENSOR_TIME
209 do_gettimeofday(&end
);
210 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
)*1000000 + (end
.tv_usec
- st
.tv_usec
));
217 int sensor_12a10_cis_log_status(struct v4l2_subdev
*subdev
)
220 struct fimc_is_cis
*cis
;
221 struct i2c_client
*client
= NULL
;
225 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
231 client
= cis
->client
;
232 if (unlikely(!client
)) {
233 err("client is NULL");
237 I2C_MUTEX_LOCK(cis
->i2c_lock
);
239 pr_err("[SEN:DUMP] *******************************\n");
240 sensor_cis_dump_registers(subdev
, sensor_12a10_setfiles
[0], sensor_12a10_setfile_sizes
[0]);
242 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
244 pr_err("[SEN:DUMP] *******************************\n");
249 #if USE_GROUP_PARAM_HOLD
250 static int sensor_12a10_cis_group_param_hold_func(struct v4l2_subdev
*subdev
, unsigned int hold
)
253 struct fimc_is_cis
*cis
= NULL
;
254 struct i2c_client
*client
= NULL
;
258 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
261 FIMC_BUG(!cis
->cis_data
);
263 client
= cis
->client
;
264 if (unlikely(!client
)) {
265 err("client is NULL");
270 if (hold
== cis
->cis_data
->group_param_hold
) {
271 pr_debug("already group_param_hold (%d)\n", cis
->cis_data
->group_param_hold
);
275 ret
= fimc_is_sensor_write8(client
, 0x3208, 0x00);
277 ret
= fimc_is_sensor_write8(client
, 0x3208, 0x10);
278 ret
= fimc_is_sensor_write8(client
, 0x320b, 0x00);
279 ret
= fimc_is_sensor_write8(client
, 0x3208, 0xa0);
284 cis
->cis_data
->group_param_hold
= hold
;
290 static inline int sensor_12a10_cis_group_param_hold_func(struct v4l2_subdev
*subdev
, unsigned int hold
)
295 * hold : true - hold, flase - no hold
297 * return: 0 - no effect(already hold or no hold)
298 * positive - setted by request
299 * negative - ERROR value
301 int sensor_12a10_cis_group_param_hold(struct v4l2_subdev
*subdev
, bool hold
)
304 struct fimc_is_cis
*cis
= NULL
;
308 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
311 FIMC_BUG(!cis
->cis_data
);
313 I2C_MUTEX_LOCK(cis
->i2c_lock
);
315 ret
= sensor_12a10_cis_group_param_hold_func(subdev
, hold
);
320 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
325 int sensor_12a10_cis_set_global_setting(struct v4l2_subdev
*subdev
)
328 struct fimc_is_cis
*cis
= NULL
;
332 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
335 I2C_MUTEX_LOCK(cis
->i2c_lock
);
337 ret
= sensor_cis_set_registers(subdev
, sensor_12a10_global
, sensor_12a10_global_size
);
340 err("sensor_12a10_set_registers fail!!");
344 dbg_sensor(1, "[%s] global setting done\n", __func__
);
347 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
352 int sensor_12a10_cis_mode_change(struct v4l2_subdev
*subdev
, u32 mode
)
355 struct fimc_is_cis
*cis
= NULL
;
359 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
361 FIMC_BUG(!cis
->cis_data
);
363 if (mode
> sensor_12a10_max_setfile_num
) {
364 err("invalid mode(%d)!!", mode
);
368 sensor_12a10_cis_data_calculation(sensor_12a10_pllinfos
[mode
], cis
->cis_data
);
370 if (cis
->cis_data
->is_data
.paf_mode
) {
371 cis
->cis_data
->cur_pos_x
= 0;
372 cis
->cis_data
->cur_pos_y
= 0;
373 info("get cur_pos_x/y value for MS paf mode x: %d, y: %d\n",
374 cis
->cis_data
->cur_pos_x
, cis
->cis_data
->cur_pos_y
);
377 I2C_MUTEX_LOCK(cis
->i2c_lock
);
379 ret
= sensor_cis_set_registers(subdev
, sensor_12a10_setfiles
[mode
], sensor_12a10_setfile_sizes
[mode
]);
381 err("sensor_12a10_set_registers fail!!");
385 cis
->cis_data
->frame_time
= (cis
->cis_data
->line_readOut_time
* cis
->cis_data
->cur_height
/ 1000);
386 cis
->cis_data
->rolling_shutter_skew
= (cis
->cis_data
->cur_height
- 1) * cis
->cis_data
->line_readOut_time
;
387 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__
,
388 cis
->cis_data
->frame_time
, cis
->cis_data
->rolling_shutter_skew
);
390 dbg_sensor(1, "[%s] mode changed(%d)\n", __func__
, mode
);
393 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
399 int sensor_12a10_cis_set_size(struct v4l2_subdev
*subdev
, cis_shared_data
*cis_data
)
404 int sensor_12a10_cis_stream_on(struct v4l2_subdev
*subdev
)
407 struct fimc_is_cis
*cis
;
408 struct i2c_client
*client
;
409 cis_shared_data
*cis_data
;
411 #ifdef DEBUG_SENSOR_TIME
412 struct timeval st
, end
;
414 do_gettimeofday(&st
);
419 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
422 FIMC_BUG(!cis
->cis_data
);
424 client
= cis
->client
;
425 if (unlikely(!client
)) {
426 err("client is NULL");
430 cis_data
= cis
->cis_data
;
432 dbg_sensor(1, "[MOD:D:%d] %s\n", cis
->id
, __func__
);
434 I2C_MUTEX_LOCK(cis
->i2c_lock
);
436 ret
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
438 err("group_param_hold_func failed at stream on");
440 /* Sensor stream on */
441 fimc_is_sensor_write8(client
, 0x0100, 0x01);
442 cis_data
->stream_on
= true;
444 #ifdef DEBUG_SENSOR_TIME
445 do_gettimeofday(&end
);
446 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
449 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
454 int sensor_12a10_cis_stream_off(struct v4l2_subdev
*subdev
)
457 struct fimc_is_cis
*cis
;
458 struct i2c_client
*client
;
459 cis_shared_data
*cis_data
;
461 #ifdef DEBUG_SENSOR_TIME
462 struct timeval st
, end
;
464 do_gettimeofday(&st
);
469 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
472 FIMC_BUG(!cis
->cis_data
);
474 client
= cis
->client
;
475 if (unlikely(!client
)) {
476 err("client is NULL");
480 cis_data
= cis
->cis_data
;
482 dbg_sensor(1, "[MOD:D:%d] %s\n", cis
->id
, __func__
);
484 I2C_MUTEX_LOCK(cis
->i2c_lock
);
486 ret
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
488 err("group_param_hold_func failed at stream off");
490 /* Sensor stream off */
491 fimc_is_sensor_write8(client
, 0x0100, 0x00);
493 cis_data
->stream_on
= false;
495 #ifdef DEBUG_SENSOR_TIME
496 do_gettimeofday(&end
);
497 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
500 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
505 int sensor_12a10_cis_set_exposure_time(struct v4l2_subdev
*subdev
, struct ae_param
*target_exposure
)
509 struct fimc_is_cis
*cis
;
510 struct i2c_client
*client
;
511 cis_shared_data
*cis_data
;
513 u32 vt_pic_clk_freq_mhz
= 0;
514 u32 long_coarse_int
= 0;
515 u32 short_coarse_int
= 0;
516 u32 line_length_pck
= 0;
517 u32 min_fine_int
= 0;
518 u8 short_coarse_val
[3] = {0};
520 #ifdef DEBUG_SENSOR_TIME
521 struct timeval st
, end
;
523 do_gettimeofday(&st
);
527 FIMC_BUG(!target_exposure
);
529 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
532 FIMC_BUG(!cis
->cis_data
);
534 client
= cis
->client
;
535 if (unlikely(!client
)) {
536 err("client is NULL");
540 if ((target_exposure
->long_val
<= 0) || (target_exposure
->short_val
<= 0)) {
541 err("[%s] invalid target exposure(%d, %d)\n", __func__
,
542 target_exposure
->long_val
, target_exposure
->short_val
);
546 cis_data
= cis
->cis_data
;
548 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), target long(%d), short(%d)\n", cis
->id
, __func__
,
549 cis_data
->sen_vsync_count
, target_exposure
->long_val
, target_exposure
->short_val
);
551 vt_pic_clk_freq_mhz
= cis_data
->pclk
/ (1000 * 1000);
552 line_length_pck
= cis_data
->line_length_pck
;
553 min_fine_int
= cis_data
->min_fine_integration_time
;
555 long_coarse_int
= ((target_exposure
->long_val
* vt_pic_clk_freq_mhz
) - min_fine_int
) / line_length_pck
;
556 short_coarse_int
= ((target_exposure
->short_val
* vt_pic_clk_freq_mhz
) - min_fine_int
) / line_length_pck
;
558 if (cis
->long_term_mode
.sen_strm_off_on_enable
) {
562 /* Long Exposure Time register caculated by below formula
563 * value = ((Long exp coarse int - normal max coarse int (VTS-8)) / 256) * line_length_pck(HTS)
565 reg_long_exp
= ((long_coarse_int
- cis_data
->max_coarse_integration_time
) / 256) * line_length_pck
;
567 temp
= reg_long_exp
& 0xff;
568 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c95, temp
);
569 temp
= (reg_long_exp
>> 8) & 0xff;
570 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c94, temp
);
571 temp
= (reg_long_exp
>> 16) & 0xff;
572 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c93, temp
);
573 temp
= (reg_long_exp
>> 24) & 0xff;
574 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c92, temp
);
576 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), reg_val for LTE(%d)\n", cis
->id
, __func__
,
577 cis_data
->sen_vsync_count
, reg_long_exp
);
579 /* set normal exposure value to normal max when LTE mode */
580 long_coarse_int
= cis_data
->max_coarse_integration_time
;
581 short_coarse_int
= cis_data
->max_coarse_integration_time
;
584 if (long_coarse_int
> cis_data
->max_coarse_integration_time
) {
585 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) max(%d)\n", cis
->id
, __func__
,
586 cis_data
->sen_vsync_count
, long_coarse_int
, cis_data
->max_coarse_integration_time
);
587 long_coarse_int
= cis_data
->max_coarse_integration_time
;
590 if (short_coarse_int
> cis_data
->max_coarse_integration_time
) {
591 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) max(%d)\n", cis
->id
, __func__
,
592 cis_data
->sen_vsync_count
, short_coarse_int
, cis_data
->max_coarse_integration_time
);
593 short_coarse_int
= cis_data
->max_coarse_integration_time
;
596 if (long_coarse_int
< cis_data
->min_coarse_integration_time
) {
597 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) min(%d)\n", cis
->id
, __func__
,
598 cis_data
->sen_vsync_count
, long_coarse_int
, cis_data
->min_coarse_integration_time
);
599 long_coarse_int
= cis_data
->min_coarse_integration_time
;
602 if (short_coarse_int
< cis_data
->min_coarse_integration_time
) {
603 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) min(%d)\n", cis
->id
, __func__
,
604 cis_data
->sen_vsync_count
, short_coarse_int
, cis_data
->min_coarse_integration_time
);
605 short_coarse_int
= cis_data
->min_coarse_integration_time
;
608 I2C_MUTEX_LOCK(cis
->i2c_lock
);
610 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x01);
617 short_coarse_val
[0] = (u8
)((short_coarse_int
& 0xF000) >> 12);
618 short_coarse_val
[1] = (u8
)((short_coarse_int
& 0x0FF0) >> 4);
619 short_coarse_val
[2] = (u8
)((short_coarse_int
& 0x000F) << 4);
620 ret
= fimc_is_sensor_write8_array(client
, 0x3500, short_coarse_val
, 3);
625 if (fimc_is_vender_wdr_mode_on(cis_data
)) {
626 ret
= fimc_is_sensor_write16(client
, 0x3c92, long_coarse_int
);
631 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",
632 cis
->id
, __func__
, cis_data
->sen_vsync_count
, vt_pic_clk_freq_mhz
, line_length_pck
, min_fine_int
);
633 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), frame_length_lines(%#x), long_coarse_int %#x, short_coarse_int %#x\n",
634 cis
->id
, __func__
, cis_data
->sen_vsync_count
, cis_data
->frame_length_lines
,
635 long_coarse_int
, short_coarse_int
);
637 #ifdef DEBUG_SENSOR_TIME
638 do_gettimeofday(&end
);
639 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
644 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
649 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
654 int sensor_12a10_cis_get_min_exposure_time(struct v4l2_subdev
*subdev
, u32
*min_expo
)
657 struct fimc_is_cis
*cis
= NULL
;
658 cis_shared_data
*cis_data
= NULL
;
659 u32 min_integration_time
= 0;
662 u32 vt_pic_clk_freq_mhz
= 0;
663 u32 line_length_pck
= 0;
665 #ifdef DEBUG_SENSOR_TIME
666 struct timeval st
, end
;
668 do_gettimeofday(&st
);
674 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
677 FIMC_BUG(!cis
->cis_data
);
679 cis_data
= cis
->cis_data
;
681 vt_pic_clk_freq_mhz
= cis_data
->pclk
/ (1000 * 1000);
682 if (vt_pic_clk_freq_mhz
== 0) {
683 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis
->id
, __func__
, vt_pic_clk_freq_mhz
);
686 line_length_pck
= cis_data
->line_length_pck
;
687 min_coarse
= cis_data
->min_coarse_integration_time
;
688 min_fine
= cis_data
->min_fine_integration_time
;
690 min_integration_time
= ((line_length_pck
* min_coarse
) + min_fine
) / vt_pic_clk_freq_mhz
;
691 *min_expo
= min_integration_time
;
693 dbg_sensor(1, "[%s] min integration time %d\n", __func__
, min_integration_time
);
695 #ifdef DEBUG_SENSOR_TIME
696 do_gettimeofday(&end
);
697 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
704 int sensor_12a10_cis_get_max_exposure_time(struct v4l2_subdev
*subdev
, u32
*max_expo
)
707 struct fimc_is_cis
*cis
;
708 cis_shared_data
*cis_data
;
709 u32 max_integration_time
= 0;
710 u32 max_coarse_margin
= 0;
711 u32 max_fine_margin
= 0;
714 u32 vt_pic_clk_freq_mhz
= 0;
715 u32 line_length_pck
= 0;
716 u32 frame_length_lines
= 0;
718 #ifdef DEBUG_SENSOR_TIME
719 struct timeval st
, end
;
721 do_gettimeofday(&st
);
727 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
730 FIMC_BUG(!cis
->cis_data
);
732 cis_data
= cis
->cis_data
;
734 vt_pic_clk_freq_mhz
= cis_data
->pclk
/ (1000 * 1000);
735 if (vt_pic_clk_freq_mhz
== 0) {
736 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis
->id
, __func__
, vt_pic_clk_freq_mhz
);
739 line_length_pck
= cis_data
->line_length_pck
;
740 frame_length_lines
= cis_data
->frame_length_lines
;
742 max_coarse_margin
= cis_data
->max_margin_coarse_integration_time
;
743 max_fine_margin
= line_length_pck
- cis_data
->min_fine_integration_time
;
744 max_coarse
= frame_length_lines
- max_coarse_margin
;
745 max_fine
= cis_data
->max_fine_integration_time
;
747 max_integration_time
= ((line_length_pck
* max_coarse
) + max_fine
) / vt_pic_clk_freq_mhz
;
749 *max_expo
= max_integration_time
;
751 /* TODO: Is this values update here? */
752 cis_data
->max_margin_fine_integration_time
= max_fine_margin
;
753 cis_data
->max_coarse_integration_time
= max_coarse
;
755 dbg_sensor(1, "[%s] max integration time %d, max margin fine integration %d, max coarse integration %d\n",
756 __func__
, max_integration_time
, cis_data
->max_margin_fine_integration_time
,
757 cis_data
->max_coarse_integration_time
);
759 #ifdef DEBUG_SENSOR_TIME
760 do_gettimeofday(&end
);
761 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
768 int sensor_12a10_cis_adjust_frame_duration(struct v4l2_subdev
*subdev
,
769 u32 input_exposure_time
,
770 u32
*target_duration
)
773 struct fimc_is_cis
*cis
;
774 cis_shared_data
*cis_data
;
776 u32 vt_pic_clk_freq_mhz
= 0;
777 u32 line_length_pck
= 0;
778 u32 frame_length_lines
= 0;
779 u32 frame_duration
= 0;
781 #ifdef DEBUG_SENSOR_TIME
782 struct timeval st
, end
;
784 do_gettimeofday(&st
);
788 FIMC_BUG(!target_duration
);
790 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
793 FIMC_BUG(!cis
->cis_data
);
795 cis_data
= cis
->cis_data
;
797 if (cis
->long_term_mode
.sen_strm_off_on_enable
) {
798 dbg_sensor(1, "[%s] LTE mode, use exp to input(%d) -> based(%d)\n",
799 __func__
, input_exposure_time
, cis
->long_term_mode
.expo
[1]);
800 input_exposure_time
= cis
->long_term_mode
.expo
[1];
803 vt_pic_clk_freq_mhz
= cis_data
->pclk
/ (1000 * 1000);
804 line_length_pck
= cis_data
->line_length_pck
;
805 frame_length_lines
= ((vt_pic_clk_freq_mhz
* input_exposure_time
) / line_length_pck
);
806 frame_length_lines
+= cis_data
->max_margin_coarse_integration_time
;
807 frame_duration
= (frame_length_lines
* line_length_pck
) / vt_pic_clk_freq_mhz
;
809 dbg_sensor(1, "[%s](vsync cnt = %d) input exp(%d), adj duration, frame duraion(%d), min_frame_us(%d)\n",
810 __func__
, cis_data
->sen_vsync_count
, input_exposure_time
,
811 frame_duration
, cis_data
->min_frame_us_time
);
812 dbg_sensor(1, "[%s](vsync cnt = %d) adj duration, frame duraion(%d), min_frame_us(%d)\n",
813 __func__
, cis_data
->sen_vsync_count
, frame_duration
, cis_data
->min_frame_us_time
);
815 *target_duration
= MAX(frame_duration
, cis_data
->min_frame_us_time
);
817 #ifdef DEBUG_SENSOR_TIME
818 do_gettimeofday(&end
);
819 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
825 int sensor_12a10_cis_set_frame_duration(struct v4l2_subdev
*subdev
, u32 frame_duration
)
829 struct fimc_is_cis
*cis
;
830 struct i2c_client
*client
;
831 cis_shared_data
*cis_data
;
833 u32 line_length_pck
= 0;
834 u16 frame_length_lines
= 0;
836 u32 max_coarse_integration_time
= 0;
838 #ifdef DEBUG_SENSOR_TIME
839 struct timeval st
, end
;
841 do_gettimeofday(&st
);
846 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
849 FIMC_BUG(!cis
->cis_data
);
851 client
= cis
->client
;
852 if (unlikely(!client
)) {
853 err("client is NULL");
857 cis_data
= cis
->cis_data
;
859 if (frame_duration
< cis_data
->min_frame_us_time
) {
860 dbg_sensor(1, "frame duration is less than min(%d)\n", frame_duration
);
861 frame_duration
= cis_data
->min_frame_us_time
;
864 line_length_pck
= cis_data
->line_length_pck
;
865 numerator
= (u64
)cis_data
->pclk
* frame_duration
;
866 frame_length_lines
= (u16
)((numerator
/ line_length_pck
) / (1000 * 1000));
868 dbg_sensor(1, "[MOD:D:%d] %s, vt_pic_clk(%#x) frame_duration = %d us,"
869 KERN_CONT
"(line_length_pck%#x), frame_length_lines(%#x)\n",
870 cis
->id
, __func__
, cis_data
->pclk
, frame_duration
, line_length_pck
, frame_length_lines
);
872 I2C_MUTEX_LOCK(cis
->i2c_lock
);
874 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x01);
880 ret
= fimc_is_sensor_write16(client
, 0x380e, frame_length_lines
);
884 cis_data
->cur_frame_us_time
= frame_duration
;
885 cis_data
->frame_length_lines
= frame_length_lines
;
887 max_coarse_integration_time
= cis_data
->frame_length_lines
- cis_data
->max_margin_coarse_integration_time
;
888 cis_data
->max_coarse_integration_time
= max_coarse_integration_time
;
890 #ifdef DEBUG_SENSOR_TIME
891 do_gettimeofday(&end
);
892 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
897 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
902 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
907 int sensor_12a10_cis_set_frame_rate(struct v4l2_subdev
*subdev
, u32 min_fps
)
910 struct fimc_is_cis
*cis
;
911 cis_shared_data
*cis_data
;
913 u32 frame_duration
= 0;
915 #ifdef DEBUG_SENSOR_TIME
916 struct timeval st
, end
;
918 do_gettimeofday(&st
);
923 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
926 FIMC_BUG(!cis
->cis_data
);
928 cis_data
= cis
->cis_data
;
930 if (min_fps
> cis_data
->max_fps
) {
931 err("[MOD:D:%d] %s, request FPS is too high(%d), set to max(%d)\n",
932 cis
->id
, __func__
, min_fps
, cis_data
->max_fps
);
933 min_fps
= cis_data
->max_fps
;
937 err("[MOD:D:%d] %s, request FPS is 0, set to min FPS(1)\n",
942 frame_duration
= (1 * 1000 * 1000) / min_fps
;
944 dbg_sensor(1, "[MOD:D:%d] %s, set FPS(%d), frame duration(%d)\n",
945 cis
->id
, __func__
, min_fps
, frame_duration
);
947 ret
= sensor_12a10_cis_set_frame_duration(subdev
, frame_duration
);
949 err("[MOD:D:%d] %s, set frame duration is fail(%d)\n",
950 cis
->id
, __func__
, ret
);
954 cis_data
->min_frame_us_time
= frame_duration
;
956 #ifdef DEBUG_SENSOR_TIME
957 do_gettimeofday(&end
);
958 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
966 #define REG_1X_BASE_VALUE 1000
967 #define REG_1X_BASE 0x0080
968 #define REG_2X_BASE_VALUE 2000
969 #define REG_2X_BASE 0x0100
970 #define REG_4X_BASE_VALUE 4000
971 #define REG_4X_BASE 0x0200
972 #define REG_8X_BASE_VALUE 8000
973 #define REG_8X_BASE 0x0400
974 #define REG_UPPER_BOUND 15500
976 #define REG_1X_MIN_VALUE 0x0080
977 #define REG_1X_MAX_VALUE 0x00F8
978 #define REG_2X_MAX_VALUE 0x01F0
979 #define REG_4X_MAX_VALUE 0x03E0
980 #define REG_8X_MAX_VALUE 0x07F0
982 static u32
sensor_12a10_again_to_reg_value(u32 again
)
987 /* 1x ~ 1.9375x: 0x0080 ~ 0x00F8, step = 0.0625x, 0x08 */
988 /* 2x ~ 3.875 x: 0x0100 ~ 0x01F0, step = 0.125x, 0x10 */
989 /* 4x ~ 7.75 x: 0x0200 ~ 0x03E0, step = 0.25x, 0x20 */
990 /* 8x ~ 15.5 x: 0x0400 ~ 0x07F0, step = 0.5x, 0x40 */
991 if (again
< REG_2X_BASE_VALUE
) {
992 level
= ((again
- REG_1X_BASE_VALUE
) << 4) / 1000; /* step_num */
993 reg_value
= REG_1X_BASE
+ level
* 8; /* reg_val = reg_base + step_num * step */
994 } else if (again
< REG_4X_BASE_VALUE
) {
995 level
= ((again
- REG_2X_BASE_VALUE
) << 3) / 1000;
996 reg_value
= REG_2X_BASE
+ level
* 16;
997 } else if (again
< REG_8X_BASE_VALUE
) {
998 level
= ((again
- REG_4X_BASE_VALUE
) << 2) / 1000;
999 reg_value
= REG_4X_BASE
+ level
* 32;
1000 } else if (again
< REG_UPPER_BOUND
) {
1001 level
= ((again
- REG_8X_BASE_VALUE
) << 1) / 1000;
1002 reg_value
= REG_8X_BASE
+ level
* 64;
1004 reg_value
= REG_8X_MAX_VALUE
;
1010 static u32
sensor_12a10_reg_value_to_again(u32 reg_value
)
1014 if (reg_value
<= REG_1X_MAX_VALUE
) {
1015 again
= ((((reg_value
- REG_1X_BASE
) >> 3) * 1000) >> 4) + REG_1X_BASE_VALUE
;
1016 } else if (reg_value
<= REG_2X_MAX_VALUE
) {
1017 again
= ((((reg_value
- REG_2X_BASE
) >> 2) * 1000) >> 4) + REG_2X_BASE_VALUE
;
1018 } else if (reg_value
<= REG_4X_MAX_VALUE
) {
1019 again
= ((((reg_value
- REG_4X_BASE
) >> 1) * 1000) >> 4) + REG_4X_BASE_VALUE
;
1020 } else if (reg_value
<= REG_8X_MAX_VALUE
) {
1021 again
= ((((reg_value
- REG_8X_BASE
) >> 0) * 1000) >> 4) + REG_8X_BASE_VALUE
;
1023 again
= REG_1X_BASE_VALUE
;
1029 int sensor_12a10_cis_adjust_analog_gain(struct v4l2_subdev
*subdev
, u32 input_again
, u32
*target_permile
)
1032 struct fimc_is_cis
*cis
;
1033 cis_shared_data
*cis_data
;
1036 u32 again_permile
= 0;
1038 #ifdef DEBUG_SENSOR_TIME
1039 struct timeval st
, end
;
1041 do_gettimeofday(&st
);
1045 FIMC_BUG(!target_permile
);
1047 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1050 FIMC_BUG(!cis
->cis_data
);
1052 cis_data
= cis
->cis_data
;
1054 again_code
= sensor_12a10_again_to_reg_value(input_again
);
1056 if (again_code
> cis_data
->max_analog_gain
[0])
1057 again_code
= cis_data
->max_analog_gain
[0];
1058 else if (again_code
< cis_data
->min_analog_gain
[0])
1059 again_code
= cis_data
->min_analog_gain
[0];
1061 again_permile
= sensor_12a10_reg_value_to_again(again_code
);
1063 dbg_sensor(1, "[%s] min again(%d), max(%d), input_again(%d), code(%d), permile(%d)\n", __func__
,
1064 cis_data
->max_analog_gain
[0],
1065 cis_data
->min_analog_gain
[0],
1070 *target_permile
= again_permile
;
1075 int sensor_12a10_cis_set_analog_gain(struct v4l2_subdev
*subdev
, struct ae_param
*again
)
1079 struct fimc_is_cis
*cis
;
1080 struct i2c_client
*client
;
1082 u16 analog_gain
= 0;
1083 u8 analog_val
[2] = {0};
1085 #ifdef DEBUG_SENSOR_TIME
1086 struct timeval st
, end
;
1088 do_gettimeofday(&st
);
1094 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1098 client
= cis
->client
;
1099 if (unlikely(!client
)) {
1100 err("client is NULL");
1104 analog_gain
= (u16
)sensor_12a10_again_to_reg_value(again
->val
);
1106 if (analog_gain
< cis
->cis_data
->min_analog_gain
[0])
1107 analog_gain
= cis
->cis_data
->min_analog_gain
[0];
1109 if (analog_gain
> cis
->cis_data
->max_analog_gain
[0])
1110 analog_gain
= cis
->cis_data
->max_analog_gain
[0];
1112 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_again = %d us, analog_gain(%#x)\n",
1113 cis
->id
, __func__
, cis
->cis_data
->sen_vsync_count
, again
->val
, analog_gain
);
1115 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1117 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x01);
1123 analog_val
[0] = (analog_gain
& 0xFF00) >> 8;
1124 analog_val
[1] = (analog_gain
& 0x00FF);
1125 ret
= fimc_is_sensor_write8_array(client
, 0x3508, analog_val
, 2);
1126 ret
= fimc_is_sensor_write8_array(client
, 0x350c, analog_val
, 2);
1130 #ifdef DEBUG_SENSOR_TIME
1131 do_gettimeofday(&end
);
1132 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1137 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
1142 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1147 int sensor_12a10_cis_get_analog_gain(struct v4l2_subdev
*subdev
, u32
*again
)
1151 struct fimc_is_cis
*cis
;
1152 struct i2c_client
*client
;
1154 u16 analog_gain
= 0;
1156 #ifdef DEBUG_SENSOR_TIME
1157 struct timeval st
, end
;
1159 do_gettimeofday(&st
);
1165 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1169 client
= cis
->client
;
1170 if (unlikely(!client
)) {
1171 err("client is NULL");
1175 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1177 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x01);
1183 ret
= fimc_is_sensor_read16(client
, 0x3508, &analog_gain
);
1187 *again
= sensor_12a10_reg_value_to_again(analog_gain
);
1189 dbg_sensor(1, "[MOD:D:%d] %s, cur_again = %d us, analog_gain(%#x)\n",
1190 cis
->id
, __func__
, *again
, analog_gain
);
1192 #ifdef DEBUG_SENSOR_TIME
1193 do_gettimeofday(&end
);
1194 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1199 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
1204 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1209 int sensor_12a10_cis_get_min_analog_gain(struct v4l2_subdev
*subdev
, u32
*min_again
)
1212 struct fimc_is_cis
*cis
;
1213 struct i2c_client
*client
;
1214 cis_shared_data
*cis_data
;
1216 #ifdef DEBUG_SENSOR_TIME
1217 struct timeval st
, end
;
1219 do_gettimeofday(&st
);
1223 FIMC_BUG(!min_again
);
1225 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1228 FIMC_BUG(!cis
->cis_data
);
1230 client
= cis
->client
;
1231 if (unlikely(!client
)) {
1232 err("client is NULL");
1236 cis_data
= cis
->cis_data
;
1237 cis_data
->min_analog_gain
[0] = REG_1X_MIN_VALUE
;
1238 cis_data
->min_analog_gain
[1] = 1000;
1239 *min_again
= cis_data
->min_analog_gain
[1];
1241 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__
, cis_data
->min_analog_gain
[0], cis_data
->min_analog_gain
[1]);
1243 #ifdef DEBUG_SENSOR_TIME
1244 do_gettimeofday(&end
);
1245 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1251 int sensor_12a10_cis_get_max_analog_gain(struct v4l2_subdev
*subdev
, u32
*max_again
)
1254 struct fimc_is_cis
*cis
;
1255 struct i2c_client
*client
;
1256 cis_shared_data
*cis_data
;
1258 #ifdef DEBUG_SENSOR_TIME
1259 struct timeval st
, end
;
1261 do_gettimeofday(&st
);
1265 FIMC_BUG(!max_again
);
1267 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1270 FIMC_BUG(!cis
->cis_data
);
1272 client
= cis
->client
;
1273 if (unlikely(!client
)) {
1274 err("client is NULL");
1278 cis_data
= cis
->cis_data
;
1279 cis_data
->max_analog_gain
[0] = REG_8X_MAX_VALUE
;
1280 cis_data
->max_analog_gain
[1] = 15500;
1281 *max_again
= cis_data
->max_analog_gain
[1];
1283 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__
, cis_data
->max_analog_gain
[0], cis_data
->max_analog_gain
[1]);
1285 #ifdef DEBUG_SENSOR_TIME
1286 do_gettimeofday(&end
);
1287 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1293 #define REG_1X_BASE_DGAIN_VALUE 1000
1294 #define REG_1X_BASE_DGAIN 0x0400
1296 u32
sensor_12a10_dgain_to_reg_value(u32 permile
)
1299 reg_dgain
= permile
* REG_1X_BASE_DGAIN
/ REG_1X_BASE_DGAIN_VALUE
;
1304 int sensor_12a10_cis_set_digital_gain(struct v4l2_subdev
*subdev
, struct ae_param
*dgain
)
1307 struct fimc_is_cis
*cis
;
1308 struct i2c_client
*client
;
1309 cis_shared_data
*cis_data
;
1313 u8 reg_short_gain
[2] = {0};
1315 #ifdef DEBUG_SENSOR_TIME
1316 struct timeval st
, end
;
1318 do_gettimeofday(&st
);
1324 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1327 FIMC_BUG(!cis
->cis_data
);
1329 client
= cis
->client
;
1330 if (unlikely(!client
)) {
1331 err("client is NULL");
1335 cis_data
= cis
->cis_data
;
1337 long_gain
= (u16
)sensor_12a10_dgain_to_reg_value(dgain
->long_val
);
1338 short_gain
= (u16
)sensor_12a10_dgain_to_reg_value(dgain
->short_val
);
1340 if (long_gain
< cis
->cis_data
->min_digital_gain
[0])
1341 long_gain
= cis
->cis_data
->min_digital_gain
[0];
1343 if (long_gain
> cis
->cis_data
->max_digital_gain
[0])
1344 long_gain
= cis
->cis_data
->max_digital_gain
[0];
1346 if (short_gain
< cis
->cis_data
->min_digital_gain
[0])
1347 short_gain
= cis
->cis_data
->min_digital_gain
[0];
1349 if (short_gain
> cis
->cis_data
->max_digital_gain
[0])
1350 short_gain
= cis
->cis_data
->max_digital_gain
[0];
1352 reg_short_gain
[0] = (short_gain
& 0x3FFF) >> 8;
1353 reg_short_gain
[1] = (short_gain
& 0xFF);
1354 fimc_is_sensor_write8(client
, 0x350A, reg_short_gain
[0]);
1355 fimc_is_sensor_write8(client
, 0x350B, reg_short_gain
[1]);
1357 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_dgain = %d/%d us, long_gain(%#x), short_gain(%#x)\n",
1358 cis
->id
, __func__
, cis
->cis_data
->sen_vsync_count
, dgain
->long_val
,
1359 dgain
->short_val
, long_gain
, short_gain
);
1361 #ifdef DEBUG_SENSOR_TIME
1362 do_gettimeofday(&end
);
1363 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1369 int sensor_12a10_cis_get_digital_gain(struct v4l2_subdev
*subdev
, u32
*dgain
)
1373 struct fimc_is_cis
*cis
;
1374 struct i2c_client
*client
;
1376 u16 digital_gain
= 0;
1378 #ifdef DEBUG_SENSOR_TIME
1379 struct timeval st
, end
;
1381 do_gettimeofday(&st
);
1387 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1391 client
= cis
->client
;
1392 if (unlikely(!client
)) {
1393 err("client is NULL");
1397 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1399 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x01);
1405 ret
= fimc_is_sensor_read16(client
, 0x350a, &digital_gain
);
1409 *dgain
= sensor_cis_calc_dgain_permile(digital_gain
);
1411 dbg_sensor(1, "[MOD:D:%d] %s, cur_dgain = %d us, digital_gain(%#x)\n",
1412 cis
->id
, __func__
, *dgain
, digital_gain
);
1414 #ifdef DEBUG_SENSOR_TIME
1415 do_gettimeofday(&end
);
1416 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1421 hold
= sensor_12a10_cis_group_param_hold_func(subdev
, 0x00);
1426 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1431 int sensor_12a10_cis_get_min_digital_gain(struct v4l2_subdev
*subdev
, u32
*min_dgain
)
1434 struct fimc_is_cis
*cis
;
1435 struct i2c_client
*client
;
1436 cis_shared_data
*cis_data
;
1438 #ifdef DEBUG_SENSOR_TIME
1439 struct timeval st
, end
;
1441 do_gettimeofday(&st
);
1445 FIMC_BUG(!min_dgain
);
1447 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1450 FIMC_BUG(!cis
->cis_data
);
1452 client
= cis
->client
;
1453 if (unlikely(!client
)) {
1454 err("client is NULL");
1458 cis_data
= cis
->cis_data
;
1459 cis_data
->min_digital_gain
[0] = REG_1X_BASE_DGAIN
;
1460 cis_data
->min_digital_gain
[1] = REG_1X_BASE_DGAIN_VALUE
;
1461 *min_dgain
= cis_data
->min_digital_gain
[1];
1463 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__
,
1464 cis_data
->min_digital_gain
[0], cis_data
->min_digital_gain
[1]);
1466 #ifdef DEBUG_SENSOR_TIME
1467 do_gettimeofday(&end
);
1468 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1474 int sensor_12a10_cis_get_max_digital_gain(struct v4l2_subdev
*subdev
, u32
*max_dgain
)
1477 struct fimc_is_cis
*cis
;
1478 struct i2c_client
*client
;
1479 cis_shared_data
*cis_data
;
1481 #ifdef DEBUG_SENSOR_TIME
1482 struct timeval st
, end
;
1484 do_gettimeofday(&st
);
1488 FIMC_BUG(!max_dgain
);
1490 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1493 FIMC_BUG(!cis
->cis_data
);
1495 client
= cis
->client
;
1496 if (unlikely(!client
)) {
1497 err("client is NULL");
1501 cis_data
= cis
->cis_data
;
1502 cis_data
->max_digital_gain
[0] = 0x3FFF;
1503 cis_data
->max_digital_gain
[1] = 16000;
1504 *max_dgain
= cis_data
->max_digital_gain
[1];
1506 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__
,
1507 cis_data
->max_digital_gain
[0], cis_data
->max_digital_gain
[1]);
1509 #ifdef DEBUG_SENSOR_TIME
1510 do_gettimeofday(&end
);
1511 dbg_sensor(1, "[%s] time %lu us\n", __func__
, (end
.tv_sec
- st
.tv_sec
) * 1000000 + (end
.tv_usec
- st
.tv_usec
));
1517 int sensor_12a10_cis_wait_streamon(struct v4l2_subdev
*subdev
)
1520 struct fimc_is_cis
*cis
;
1521 struct i2c_client
*client
;
1522 cis_shared_data
*cis_data
;
1523 u32 wait_cnt
= 0, time_out_cnt
= 2500;
1524 u8 sensor_fcount
= 0;
1528 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1529 if (unlikely(!cis
)) {
1535 cis_data
= cis
->cis_data
;
1536 if (unlikely(!cis_data
)) {
1537 err("cis_data is NULL");
1542 client
= cis
->client
;
1543 if (unlikely(!client
)) {
1544 err("client is NULL");
1549 ret
= fimc_is_sensor_read8(client
, 0x483F, &sensor_fcount
);
1551 err("i2c transfer fail addr(%x), val(%x), ret = %d\n", 0x483F, sensor_fcount
, ret
);
1554 * Read sensor frame counter (sensor_fcount address = 0x483F)
1555 * stream on (0x00 ~ 0xFE), stream off (0xFF)
1557 while (sensor_fcount
== 0x00) {
1558 usleep_range(CIS_STREAM_ON_WAIT_TIME
, CIS_STREAM_ON_WAIT_TIME
);
1561 ret
= fimc_is_sensor_read8(client
, 0x483F, &sensor_fcount
);
1563 err("i2c transfer fail addr(%x), val(%x), ret = %d\n", 0x483F, sensor_fcount
, ret
);
1565 if (wait_cnt
>= time_out_cnt
) {
1566 err("[MOD:D:%d] %s, Don't sensor stream on and time out, wait_limit(%d) > time_out(%d), sensor_fcount(%d)",
1567 cis
->id
, __func__
, wait_cnt
, time_out_cnt
, sensor_fcount
);
1572 dbg_sensor(1, "[MOD:D:%d] %s, sensor_fcount(%d), (wait_limit(%d) < time_out(%d))\n",
1573 cis
->id
, __func__
, sensor_fcount
, wait_cnt
, time_out_cnt
);
1576 #ifdef CONFIG_SENSOR_RETENTION_USE
1577 /* retention mode CRC wait calculation */
1578 usleep_range(1000, 1000);
1585 int sensor_12a10_cis_wait_streamoff(struct v4l2_subdev
*subdev
)
1588 struct fimc_is_cis
*cis
;
1589 struct i2c_client
*client
;
1590 cis_shared_data
*cis_data
;
1591 u32 wait_cnt
= 0, time_out_cnt
= 250;
1592 u8 sensor_fcount
= 0;
1596 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1597 cis_data
= cis
->cis_data
;
1598 client
= cis
->client
;
1600 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1601 ret
= fimc_is_sensor_read8(client
, 0x483F, &sensor_fcount
);
1602 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1605 err("i2c transfer fail addr(%x), val(%x), ret = %d\n", 0x483F, sensor_fcount
, ret
);
1607 while (sensor_fcount
!= 0x00) {
1608 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1609 ret
= fimc_is_sensor_read8(client
, 0x483F, &sensor_fcount
);
1610 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1612 err("i2c transfer fail addr(%x), val(%x), ret = %d\n", 0x483F, sensor_fcount
, ret
);
1614 usleep_range(CIS_STREAM_OFF_WAIT_TIME
, CIS_STREAM_OFF_WAIT_TIME
);
1617 if (wait_cnt
>= time_out_cnt
) {
1618 err("[MOD:D:%d] %s, time out, wait_limit(%d) > time_out(%d), sensor_fcount(%d)",
1619 cis
->id
, __func__
, wait_cnt
, time_out_cnt
, sensor_fcount
);
1624 dbg_sensor(1, "[MOD:D:%d] %s, sensor_fcount(%d), (wait_limit(%d) < time_out(%d))\n",
1625 cis
->id
, __func__
, sensor_fcount
, wait_cnt
, time_out_cnt
);
1632 static int sensor_12a10_cis_set_dual_slave_setting(struct fimc_is_cis
*cis
)
1635 struct i2c_client
*client
;
1637 cis_shared_data
*cis_data
;
1640 client
= cis
->client
;
1641 if (unlikely(!client
)) {
1642 err("client is NULL");
1647 dbg_sensor(1, "[MOD:D:%d] %s\n", cis
->id
, __func__
);
1649 cis_data
= cis
->cis_data
;
1651 /* Vsync Input Source Select */
1652 fimc_is_sensor_write8(client
, 0x3002, 0x21);
1653 fimc_is_sensor_write8(client
, 0x3643, 0x22);
1654 fimc_is_sensor_write8(client
, 0x3822, 0xa1);
1655 fimc_is_sensor_write8(client
, 0x3823, 0x78);
1656 fimc_is_sensor_write8(client
, 0x3824, 0x00);// frame start sync
1657 fimc_is_sensor_write8(client
, 0x3825, 0x20);
1658 fimc_is_sensor_write8(client
, 0x3826, 0x00);
1659 fimc_is_sensor_write8(client
, 0x3827, 0x08);
1660 fimc_is_sensor_write8(client
, 0x3c80, 0x08);
1666 int sensor_12a10_cis_set_dual_setting(struct v4l2_subdev
*subdev
)
1669 struct fimc_is_cis
*cis
;
1670 struct i2c_client
*client
;
1674 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1678 client
= cis
->client
;
1679 if (unlikely(!client
)) {
1680 err("client is NULL");
1685 switch (cis
->dual_sync_mode
) {
1686 case DUAL_SYNC_MASTER
:
1688 case DUAL_SYNC_SLAVE
:
1689 ret
= sensor_12a10_cis_set_dual_slave_setting(cis
);
1691 err("12a10 dual slave setting fail");
1694 err("invalid cis->dual_sync_mode(%d)\n", cis
->dual_sync_mode
);
1702 int sensor_12a10_cis_long_term_exposure(struct v4l2_subdev
*subdev
)
1705 struct fimc_is_cis
*cis
;
1706 struct fimc_is_long_term_expo_mode
*lte_mode
;
1710 cis
= (struct fimc_is_cis
*)v4l2_get_subdevdata(subdev
);
1711 lte_mode
= &cis
->long_term_mode
;
1713 I2C_MUTEX_LOCK(cis
->i2c_lock
);
1714 /* LTE mode or normal mode set */
1715 if (lte_mode
->sen_strm_off_on_enable
) {
1716 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c80, 0x0c);
1717 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c90, 0x01);
1718 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3826, 0X0d);
1719 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3827, 0x04);
1720 ret
|= fimc_is_sensor_write8(cis
->client
, 0x401a, 0x40);
1721 ret
|= fimc_is_sensor_write8(cis
->client
, 0x4902, 0x01);
1722 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3640, 0x14);
1723 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3714, 0x20);
1724 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5203, 0x0c);
1725 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5204, 0x11);
1726 ret
|= fimc_is_sensor_write8(cis
->client
, 0x520d, 0xf5);
1727 ret
|= fimc_is_sensor_write8(cis
->client
, 0x520e, 0xf5);
1728 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5208, 0xff);
1729 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5201, 0xa8);
1730 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5200, 0x01);
1732 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c80, 0x00);
1733 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3c90, 0x00);
1734 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3827, 0x00);
1735 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3826, 0x00);
1736 ret
|= fimc_is_sensor_write8(cis
->client
, 0x401a, 0x58);
1737 ret
|= fimc_is_sensor_write8(cis
->client
, 0x4902, 0x00);
1738 ret
|= fimc_is_sensor_write8(cis
->client
, 0x3714, 0x24);
1739 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5203, 0x24);
1740 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5204, 0x12);
1741 ret
|= fimc_is_sensor_write8(cis
->client
, 0x520d, 0x0f);
1742 ret
|= fimc_is_sensor_write8(cis
->client
, 0x520e, 0xfd);
1743 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5208, 0x40);
1744 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5201, 0x94);
1745 ret
|= fimc_is_sensor_write8(cis
->client
, 0x5200, 0x03);
1748 I2C_MUTEX_UNLOCK(cis
->i2c_lock
);
1750 info("%s enable(%d)", __func__
, lte_mode
->sen_strm_off_on_enable
);
1753 pr_err("ERR[%s]: LTE register setting fail\n", __func__
);
1760 static struct fimc_is_cis_ops cis_ops
= {
1761 .cis_init
= sensor_12a10_cis_init
,
1762 .cis_log_status
= sensor_12a10_cis_log_status
,
1763 .cis_group_param_hold
= sensor_12a10_cis_group_param_hold
,
1764 .cis_set_global_setting
= sensor_12a10_cis_set_global_setting
,
1765 .cis_mode_change
= sensor_12a10_cis_mode_change
,
1766 .cis_set_size
= sensor_12a10_cis_set_size
,
1767 .cis_stream_on
= sensor_12a10_cis_stream_on
,
1768 .cis_stream_off
= sensor_12a10_cis_stream_off
,
1769 .cis_set_exposure_time
= sensor_12a10_cis_set_exposure_time
,
1770 .cis_get_min_exposure_time
= sensor_12a10_cis_get_min_exposure_time
,
1771 .cis_get_max_exposure_time
= sensor_12a10_cis_get_max_exposure_time
,
1772 .cis_adjust_frame_duration
= sensor_12a10_cis_adjust_frame_duration
,
1773 .cis_set_frame_duration
= sensor_12a10_cis_set_frame_duration
,
1774 .cis_set_frame_rate
= sensor_12a10_cis_set_frame_rate
,
1775 .cis_adjust_analog_gain
= sensor_12a10_cis_adjust_analog_gain
,
1776 .cis_set_analog_gain
= sensor_12a10_cis_set_analog_gain
,
1777 .cis_get_analog_gain
= sensor_12a10_cis_get_analog_gain
,
1778 .cis_get_min_analog_gain
= sensor_12a10_cis_get_min_analog_gain
,
1779 .cis_get_max_analog_gain
= sensor_12a10_cis_get_max_analog_gain
,
1780 .cis_set_digital_gain
= sensor_12a10_cis_set_digital_gain
,
1781 .cis_get_digital_gain
= sensor_12a10_cis_get_digital_gain
,
1782 .cis_get_min_digital_gain
= sensor_12a10_cis_get_min_digital_gain
,
1783 .cis_get_max_digital_gain
= sensor_12a10_cis_get_max_digital_gain
,
1784 .cis_compensate_gain_for_extremely_br
= sensor_cis_compensate_gain_for_extremely_br
,
1785 .cis_set_initial_exposure
= sensor_cis_set_initial_exposure
,
1786 .cis_check_rev
= sensor_12a10_cis_check_rev
,
1787 .cis_factory_test
= sensor_cis_factory_test
,
1788 .cis_wait_streamoff
= sensor_12a10_cis_wait_streamoff
,
1789 .cis_wait_streamon
= sensor_12a10_cis_wait_streamon
,
1790 .cis_set_dual_setting
= sensor_12a10_cis_set_dual_setting
,
1791 .cis_set_long_term_exposure
= sensor_12a10_cis_long_term_exposure
,
1794 static int cis_12a10_probe(struct i2c_client
*client
,
1795 const struct i2c_device_id
*id
)
1798 struct fimc_is_core
*core
= NULL
;
1799 struct v4l2_subdev
*subdev_cis
= NULL
;
1800 struct fimc_is_cis
*cis
= NULL
;
1801 struct fimc_is_device_sensor
*device
= NULL
;
1802 struct fimc_is_device_sensor_peri
*sensor_peri
= NULL
;
1804 char const *setfile
;
1806 struct device_node
*dnode
;
1809 FIMC_BUG(!fimc_is_dev
);
1811 core
= (struct fimc_is_core
*)dev_get_drvdata(fimc_is_dev
);
1813 probe_info("core device is not yet probed");
1814 return -EPROBE_DEFER
;
1818 dnode
= dev
->of_node
;
1820 ret
= of_property_read_u32(dnode
, "id", &sensor_id
);
1822 err("sensor id read is fail(%d)", ret
);
1826 probe_info("%s sensor id %d\n", __func__
, sensor_id
);
1828 device
= &core
->sensor
[sensor_id
];
1830 sensor_peri
= find_peri_by_cis_id(device
, SENSOR_NAME_OV12A10
);
1832 probe_info("sensor peri is net yet probed");
1833 return -EPROBE_DEFER
;
1836 cis
= &sensor_peri
->cis
;
1843 subdev_cis
= kzalloc(sizeof(struct v4l2_subdev
), GFP_KERNEL
);
1845 probe_err("subdev_cis NULL");
1849 sensor_peri
->subdev_cis
= subdev_cis
;
1851 cis
->id
= SENSOR_NAME_OV12A10
;
1852 cis
->subdev
= subdev_cis
;
1854 cis
->client
= client
;
1855 sensor_peri
->module
->client
= cis
->client
;
1856 cis
->ctrl_delay
= N_PLUS_TWO_FRAME
;
1858 cis
->cis_data
= kzalloc(sizeof(cis_shared_data
), GFP_KERNEL
);
1859 if (!cis
->cis_data
) {
1860 err("cis_data is NULL");
1864 cis
->cis_ops
= &cis_ops
;
1866 /* belows are depend on sensor cis. MUST check sensor spec */
1867 cis
->bayer_order
= OTF_INPUT_ORDER_BAYER_BG_GR
;
1869 if (of_property_read_bool(dnode
, "sensor_f_number")) {
1870 ret
= of_property_read_u32(dnode
, "sensor_f_number", &cis
->aperture_num
);
1872 warn("f-number read is fail(%d)", ret
);
1874 cis
->aperture_num
= F2_2
;
1877 probe_info("%s f-number %d\n", __func__
, cis
->aperture_num
);
1879 if (of_property_read_bool(dnode
, "dual_sync_mode")) {
1880 ret
= of_property_read_u32(dnode
, "dual_sync_mode", &cis
->dual_sync_mode
);
1882 warn("dual_sync_mode read is fail(%d)", ret
);
1884 cis
->dual_sync_mode
= DUAL_SYNC_NONE
;
1887 probe_info("%s dual_sync_mode %d\n", __func__
, cis
->dual_sync_mode
);
1889 cis
->use_dgain
= true;
1890 cis
->hdr_ctrl_by_again
= false;
1892 ret
= of_property_read_string(dnode
, "setfile", &setfile
);
1894 err("setfile index read fail(%d), take default setfile!!", ret
);
1895 setfile
= "default";
1898 if (strcmp(setfile
, "default") == 0 ||
1899 strcmp(setfile
, "setA") == 0) {
1900 probe_info("%s setfile_A\n", __func__
);
1901 sensor_12a10_global
= sensor_12a10_setfile_A_Global
;
1902 sensor_12a10_global_size
= ARRAY_SIZE(sensor_12a10_setfile_A_Global
);
1903 sensor_12a10_setfiles
= sensor_12a10_setfiles_A
;
1904 sensor_12a10_setfile_sizes
= sensor_12a10_setfile_A_sizes
;
1905 sensor_12a10_pllinfos
= sensor_12a10_pllinfos_A
;
1906 sensor_12a10_max_setfile_num
= ARRAY_SIZE(sensor_12a10_setfiles_A
);
1907 sensor_12a10_crop_infos
= sensor_12a10_crop_infos_A
;
1910 cis
->use_initial_ae
= of_property_read_bool(dnode
, "use_initial_ae");
1911 probe_info("%s use initial_ae(%d)\n", __func__
, cis
->use_initial_ae
);
1913 v4l2_i2c_subdev_init(subdev_cis
, client
, &subdev_ops
);
1914 v4l2_set_subdevdata(subdev_cis
, cis
);
1915 v4l2_set_subdev_hostdata(subdev_cis
, device
);
1916 snprintf(subdev_cis
->name
, V4L2_SUBDEV_NAME_SIZE
, "cis-subdev.%d", cis
->id
);
1918 probe_info("%s done\n", __func__
);
1924 static const struct of_device_id sensor_cis_12a10_match
[] = {
1926 .compatible
= "samsung,exynos5-fimc-is-cis-12a10",
1930 MODULE_DEVICE_TABLE(of
, sensor_cis_12a10_match
);
1932 static const struct i2c_device_id sensor_cis_12a10_idt
[] = {
1937 static struct i2c_driver sensor_cis_12a10_driver
= {
1938 .probe
= cis_12a10_probe
,
1940 .name
= SENSOR_NAME
,
1941 .owner
= THIS_MODULE
,
1942 .of_match_table
= sensor_cis_12a10_match
,
1943 .suppress_bind_attrs
= true,
1945 .id_table
= sensor_cis_12a10_idt
1948 static int __init
sensor_cis_12a10_init(void)
1952 ret
= i2c_add_driver(&sensor_cis_12a10_driver
);
1954 err("failed to add %s driver: %d\n",
1955 sensor_cis_12a10_driver
.driver
.name
, ret
);
1959 late_initcall_sync(sensor_cis_12a10_init
);