[9610] fimc-is2: Buffer modification in case of otp data error at 2x5 module
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / drivers / media / platform / exynos / fimc-is2 / sensor / module_framework / cis / fimc-is-cis-imx333.c
1 /*
2 * Samsung Exynos5 SoC series Sensor driver
3 *
4 *
5 * Copyright (c) 2016 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-imx333.h"
40 #include "fimc-is-cis-imx333-setA.h"
41 #include "fimc-is-cis-imx333-setB.h"
42
43 #include "fimc-is-helper-i2c.h"
44
45 #define SENSOR_NAME "IMX333"
46 /* #define DEBUG_IMX333_PLL */
47
48 static const u32 *sensor_imx333_global;
49 static u32 sensor_imx333_global_size;
50 static const u32 **sensor_imx333_setfiles;
51 static const u32 *sensor_imx333_setfile_sizes;
52 static const struct sensor_pll_info_compact **sensor_imx333_pllinfos;
53 static u32 sensor_imx333_max_setfile_num;
54
55 static void sensor_imx333_set_integration_max_margin(u32 mode, cis_shared_data *cis_data)
56 {
57 FIMC_BUG(!cis_data);
58
59 switch (mode) {
60 case SENSOR_IMX333_8064X3024_30FPS:
61 case SENSOR_IMX333_8064X2268_30FPS:
62 case SENSOR_IMX333_6048X3024_30FPS:
63 case SENSOR_IMX333_4032X2268_60FPS:
64 case SENSOR_IMX333_8064X1960_30FPS:
65 cis_data->max_margin_coarse_integration_time = 0x14;
66 dbg_sensor(1, "max_margin_coarse_integration_time(%d)\n",
67 cis_data->max_margin_coarse_integration_time);
68 break;
69 case SENSOR_IMX333_2016X1134_120FPS:
70 case SENSOR_IMX333_2016X1134_30FPS:
71 cis_data->max_margin_coarse_integration_time = 0x0E;
72 break;
73 case SENSOR_IMX333_2016X1134_240FPS:
74 case SENSOR_IMX333_1008X756_120FPS:
75 case SENSOR_IMX333_2016X1512_30FPS:
76 case SENSOR_IMX333_1504X1504_30FPS:
77 cis_data->max_margin_coarse_integration_time = 0x0A;
78 dbg_sensor(1, "max_margin_coarse_integration_time(%d)\n",
79 cis_data->max_margin_coarse_integration_time);
80 break;
81 default:
82 err("[%s] Unsupport imx333 sensor mode\n", __func__);
83 cis_data->max_margin_coarse_integration_time = SENSOR_IMX333_COARSE_INTEGRATION_TIME_MAX_MARGIN;
84 dbg_sensor(1, "max_margin_coarse_integration_time(%d)\n",
85 cis_data->max_margin_coarse_integration_time);
86 break;
87 }
88 }
89
90 static void sensor_imx333_set_integration_min(u32 mode, cis_shared_data *cis_data)
91 {
92 FIMC_BUG(!cis_data);
93
94 switch (mode) {
95 case SENSOR_IMX333_8064X3024_30FPS:
96 case SENSOR_IMX333_8064X2268_30FPS:
97 case SENSOR_IMX333_6048X3024_30FPS:
98 case SENSOR_IMX333_8064X1960_30FPS:
99 cis_data->min_coarse_integration_time = 2;
100 dbg_sensor(1, "min_coarse_integration_time(%d)\n",
101 cis_data->min_coarse_integration_time);
102 break;
103 case SENSOR_IMX333_4032X2268_60FPS:
104 cis_data->min_coarse_integration_time = 4;
105 break;
106 case SENSOR_IMX333_2016X1134_120FPS:
107 case SENSOR_IMX333_2016X1134_30FPS:
108 cis_data->min_coarse_integration_time = 2;
109 break;
110 case SENSOR_IMX333_2016X1134_240FPS:
111 case SENSOR_IMX333_1008X756_120FPS:
112 case SENSOR_IMX333_2016X1512_30FPS:
113 case SENSOR_IMX333_1504X1504_30FPS:
114 cis_data->min_coarse_integration_time = 4;
115 dbg_sensor(1, "min_coarse_integration_time(%d)\n",
116 cis_data->min_coarse_integration_time);
117 break;
118 default:
119 err("[%s] Unsupport imx333 sensor mode\n", __func__);
120 cis_data->min_coarse_integration_time = 2;
121 dbg_sensor(1, "min_coarse_integration_time(%d)\n",
122 cis_data->min_coarse_integration_time);
123 break;
124 }
125 }
126
127 static void sensor_imx333_set_integration_time_step(u32 mode, cis_shared_data *cis_data)
128 {
129 FIMC_BUG(!cis_data);
130
131 switch (mode) {
132 case SENSOR_IMX333_8064X3024_30FPS:
133 case SENSOR_IMX333_8064X2268_30FPS:
134 case SENSOR_IMX333_6048X3024_30FPS:
135 case SENSOR_IMX333_8064X1960_30FPS:
136 cis_data->cur_coarse_integration_time_step = SENSOR_IMX333_COARSE_STEP_2N;
137 dbg_sensor(1, "cur_coarse_integration_time_step(%d)\n",
138 cis_data->cur_coarse_integration_time_step);
139 break;
140 case SENSOR_IMX333_4032X2268_60FPS:
141 cis_data->cur_coarse_integration_time_step = SENSOR_IMX333_COARSE_STEP_4N;
142 break;
143 case SENSOR_IMX333_2016X1134_120FPS:
144 case SENSOR_IMX333_2016X1134_30FPS:
145 cis_data->cur_coarse_integration_time_step = SENSOR_IMX333_COARSE_STEP_2N;
146 dbg_sensor(1, "cur_coarse_integration_time_step(%d)\n",
147 cis_data->cur_coarse_integration_time_step);
148 break;
149 case SENSOR_IMX333_2016X1134_240FPS:
150 case SENSOR_IMX333_1008X756_120FPS:
151 case SENSOR_IMX333_2016X1512_30FPS:
152 case SENSOR_IMX333_1504X1504_30FPS:
153 cis_data->cur_coarse_integration_time_step = SENSOR_IMX333_COARSE_STEP_4N;
154 dbg_sensor(1, "cur_coarse_integration_time_step(%d)\n",
155 cis_data->cur_coarse_integration_time_step);
156 break;
157 default:
158 err("[%s] Unsupport imx333 sensor mode\n", __func__);
159 cis_data->cur_coarse_integration_time_step = SENSOR_IMX333_COARSE_STEP_2N;
160 dbg_sensor(1, "cur_coarse_integration_time_step(%d)\n",
161 cis_data->cur_coarse_integration_time_step);
162 break;
163 }
164 }
165
166 static void sensor_imx333_cis_data_calculation(const struct sensor_pll_info_compact *pll_info, cis_shared_data *cis_data)
167 {
168 u32 vt_pix_clk_hz = 0;
169 u32 frame_rate = 0, max_fps = 0, frame_valid_us = 0;
170
171 FIMC_BUG(!pll_info);
172
173 /* 1. get pclk value from pll info */
174 vt_pix_clk_hz = pll_info->pclk;
175
176 /* 2. the time of processing one frame calculation (us) */
177 cis_data->min_frame_us_time = (pll_info->frame_length_lines * pll_info->line_length_pck
178 / (vt_pix_clk_hz / (1000 * 1000)));
179 cis_data->cur_frame_us_time = cis_data->min_frame_us_time;
180
181 /* 3. FPS calculation */
182 frame_rate = vt_pix_clk_hz / (pll_info->frame_length_lines * pll_info->line_length_pck);
183 dbg_sensor(1, "frame_rate (%d) = vt_pix_clk_hz(%d) / "
184 KERN_CONT "(pll_info->frame_length_lines(%d) * pll_info->line_length_pck(%d))\n",
185 frame_rate, vt_pix_clk_hz, pll_info->frame_length_lines, pll_info->line_length_pck);
186
187 /* calculate max fps */
188 max_fps = (vt_pix_clk_hz * 10) / (pll_info->frame_length_lines * pll_info->line_length_pck);
189 max_fps = (max_fps % 10 >= 5 ? frame_rate + 1 : frame_rate);
190
191 cis_data->pclk = vt_pix_clk_hz;
192 cis_data->max_fps = max_fps;
193 cis_data->frame_length_lines = pll_info->frame_length_lines;
194 cis_data->line_length_pck = pll_info->line_length_pck;
195 cis_data->line_readOut_time = sensor_cis_do_div64((u64)cis_data->line_length_pck * (u64)(1000 * 1000 * 1000), cis_data->pclk);
196 cis_data->rolling_shutter_skew = (cis_data->cur_height - 1) * cis_data->line_readOut_time;
197 cis_data->stream_on = false;
198
199 /* Frame valid time calcuration */
200 frame_valid_us = sensor_cis_do_div64((u64)cis_data->cur_height * (u64)cis_data->line_length_pck * (u64)(1000 * 1000), cis_data->pclk);
201 cis_data->frame_valid_us_time = (int)frame_valid_us;
202
203 dbg_sensor(1, "%s\n", __func__);
204 dbg_sensor(1, "Sensor size(%d x %d) setting: SUCCESS!\n",
205 cis_data->cur_width, cis_data->cur_height);
206 dbg_sensor(1, "Frame Valid(us): %d\n", frame_valid_us);
207 dbg_sensor(1, "rolling_shutter_skew: %lld\n", cis_data->rolling_shutter_skew);
208
209 dbg_sensor(1, "Fps: %d, max fps(%d)\n", frame_rate, cis_data->max_fps);
210 dbg_sensor(1, "min_frame_time(%d us)\n", cis_data->min_frame_us_time);
211 dbg_sensor(1, "Pixel rate(Mbps): %d\n", cis_data->pclk / 1000000);
212
213 /* Frame period calculation */
214 cis_data->frame_time = (cis_data->line_readOut_time * cis_data->cur_height / 1000);
215 cis_data->rolling_shutter_skew = (cis_data->cur_height - 1) * cis_data->line_readOut_time;
216
217 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__,
218 cis_data->frame_time, cis_data->rolling_shutter_skew);
219
220 /* Constant values */
221 cis_data->min_fine_integration_time = SENSOR_IMX333_FINE_INTEGRATION_TIME_MIN;
222 cis_data->max_fine_integration_time = SENSOR_IMX333_FINE_INTEGRATION_TIME_MAX;
223 info("%s: done", __func__);
224 }
225
226 void sensor_imx333_cis_data_calc(struct v4l2_subdev *subdev, u32 mode)
227 {
228 int ret = 0;
229 struct fimc_is_cis *cis = NULL;
230
231 FIMC_BUG(!subdev);
232
233 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
234 FIMC_BUG(!cis);
235 FIMC_BUG(!cis->cis_data);
236
237 if (mode > sensor_imx333_max_setfile_num) {
238 err("invalid mode(%d)!!", mode);
239 return;
240 }
241
242 /* If check_rev fail when cis_init, one more check_rev in mode_change */
243 if (cis->rev_flag == true) {
244 cis->rev_flag = false;
245 ret = sensor_cis_check_rev(cis);
246 if (ret < 0) {
247 err("sensor_imx333_check_rev is fail: ret(%d)", ret);
248 return;
249 }
250 }
251
252 sensor_imx333_cis_data_calculation(sensor_imx333_pllinfos[mode], cis->cis_data);
253 }
254
255 static int sensor_imx333_wait_stream_off_status(cis_shared_data *cis_data)
256 {
257 int ret = 0;
258 u32 timeout = 0;
259
260 FIMC_BUG(!cis_data);
261
262 #define STREAM_OFF_WAIT_TIME 250
263 while (timeout < STREAM_OFF_WAIT_TIME) {
264 if (cis_data->is_active_area == false &&
265 cis_data->stream_on == false) {
266 pr_debug("actual stream off\n");
267 break;
268 }
269 timeout++;
270 }
271
272 if (timeout == STREAM_OFF_WAIT_TIME) {
273 pr_err("actual stream off wait timeout\n");
274 ret = -1;
275 }
276
277 return ret;
278 }
279
280 int sensor_imx333_cis_check_rev(struct fimc_is_cis *cis)
281 {
282 int ret = 0;
283 u8 rev = 0, status=0;
284 struct i2c_client *client;
285
286 FIMC_BUG(!cis);
287 FIMC_BUG(!cis->cis_data);
288
289 client = cis->client;
290 if (unlikely(!client)) {
291 err("client is NULL");
292 ret = -EINVAL;
293 }
294
295 I2C_MUTEX_LOCK(cis->i2c_lock);
296 /* Specify OPT Page Address for READ */
297 fimc_is_sensor_write8(client, 0x0A02, 0x7F);
298
299 /* Turn ON OTP Read MODE */
300 fimc_is_sensor_write8(client, 0x0A00, 0x01);
301
302 fimc_is_sensor_read8(client, 0x0A01, &status);
303 if ((status & 0x1) == false)
304 err("status fail, (%d)", status);
305
306 ret = fimc_is_sensor_read8(client, 0x0A21, &rev);
307 if (ret < 0) {
308 err("fimc_is_sensor_read8 fail (ret %d)", ret);
309 I2C_MUTEX_UNLOCK(cis->i2c_lock);
310 return ret;
311 }
312 I2C_MUTEX_UNLOCK(cis->i2c_lock);
313
314 cis->cis_data->cis_rev = rev;
315 probe_info("imx333 rev:%x", rev);
316
317 return 0;
318 }
319
320 /* CIS OPS */
321 int sensor_imx333_cis_init(struct v4l2_subdev *subdev)
322 {
323 int ret = 0;
324 struct fimc_is_cis *cis;
325 u32 setfile_index = 0;
326 cis_setting_info setinfo;
327 setinfo.param = NULL;
328 setinfo.return_value = 0;
329
330 FIMC_BUG(!subdev);
331
332 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
333 if (!cis) {
334 err("cis is NULL");
335 ret = -EINVAL;
336 goto p_err;
337 }
338
339 FIMC_BUG(!cis->cis_data);
340 memset(cis->cis_data, 0, sizeof(cis_shared_data));
341 cis->rev_flag = false;
342
343 ret = sensor_imx333_cis_check_rev(cis);
344 if (ret < 0) {
345 warn("sensor_imx333_check_rev is fail when cis init, ret(%d)", ret);
346 cis->rev_flag = true;
347 goto p_err;
348 }
349
350 if (cis->cis_data->cis_rev < 0x12) {
351 probe_info("%s setfile_A\n", __func__);
352 sensor_imx333_global = sensor_imx333_setfile_A_Global;
353 sensor_imx333_global_size = sizeof(sensor_imx333_setfile_A_Global) / sizeof(sensor_imx333_setfile_A_Global[0]);
354 sensor_imx333_setfiles = sensor_imx333_setfiles_A;
355 sensor_imx333_setfile_sizes = sensor_imx333_setfile_A_sizes;
356 sensor_imx333_pllinfos = sensor_imx333_pllinfos_A;
357 sensor_imx333_max_setfile_num = sizeof(sensor_imx333_setfiles_A) / sizeof(sensor_imx333_setfiles_A[0]);
358 } else {
359 probe_info("%s setfile_B (retention)\n", __func__);
360 sensor_imx333_global = sensor_imx333_setfile_B_Global;
361 sensor_imx333_global_size = sizeof(sensor_imx333_setfile_B_Global) / sizeof(sensor_imx333_setfile_B_Global[0]);
362 sensor_imx333_setfiles = sensor_imx333_setfiles_B;
363 sensor_imx333_setfile_sizes = sensor_imx333_setfile_B_sizes;
364 sensor_imx333_pllinfos = sensor_imx333_pllinfos_B;
365 sensor_imx333_max_setfile_num = sizeof(sensor_imx333_setfiles_B) / sizeof(sensor_imx333_setfiles_B[0]);
366 }
367
368 cis->cis_data->product_name = cis->id;
369 cis->cis_data->cur_width = SENSOR_IMX333_MAX_WIDTH;
370 cis->cis_data->cur_height = SENSOR_IMX333_MAX_HEIGHT;
371 cis->cis_data->low_expo_start = 33000;
372 cis->need_mode_change = false;
373 cis->long_term_mode.sen_strm_off_on_step = 0;
374 cis->long_term_mode.sen_strm_off_on_enable = false;
375
376 sensor_imx333_cis_data_calculation(sensor_imx333_pllinfos[setfile_index], cis->cis_data);
377 sensor_imx333_set_integration_max_margin(setfile_index, cis->cis_data);
378 sensor_imx333_set_integration_min(setfile_index, cis->cis_data);
379 sensor_imx333_set_integration_time_step(setfile_index, cis->cis_data);
380
381 setinfo.return_value = 0;
382 CALL_CISOPS(cis, cis_get_min_exposure_time, subdev, &setinfo.return_value);
383 dbg_sensor(1, "[%s] min exposure time : %d\n", __func__, setinfo.return_value);
384 setinfo.return_value = 0;
385 CALL_CISOPS(cis, cis_get_max_exposure_time, subdev, &setinfo.return_value);
386 dbg_sensor(1, "[%s] max exposure time : %d\n", __func__, setinfo.return_value);
387 setinfo.return_value = 0;
388 CALL_CISOPS(cis, cis_get_min_analog_gain, subdev, &setinfo.return_value);
389 dbg_sensor(1, "[%s] min again : %d\n", __func__, setinfo.return_value);
390 setinfo.return_value = 0;
391 CALL_CISOPS(cis, cis_get_max_analog_gain, subdev, &setinfo.return_value);
392 dbg_sensor(1, "[%s] max again : %d\n", __func__, setinfo.return_value);
393 setinfo.return_value = 0;
394 CALL_CISOPS(cis, cis_get_min_digital_gain, subdev, &setinfo.return_value);
395 dbg_sensor(1, "[%s] min dgain : %d\n", __func__, setinfo.return_value);
396 setinfo.return_value = 0;
397 CALL_CISOPS(cis, cis_get_max_digital_gain, subdev, &setinfo.return_value);
398 dbg_sensor(1, "[%s] max dgain : %d\n", __func__, setinfo.return_value);
399
400 #ifdef DEBUG_SENSOR_TIME
401 do_gettimeofday(&end);
402 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
403 #endif
404
405 p_err:
406 return ret;
407 }
408
409 int sensor_imx333_cis_log_status(struct v4l2_subdev *subdev)
410 {
411 int ret = 0;
412 struct fimc_is_cis *cis;
413 struct i2c_client *client = NULL;
414 u8 data8 = 0;
415 u16 data16 = 0;
416
417 FIMC_BUG(!subdev);
418
419 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
420 if (!cis) {
421 err("cis is NULL");
422 ret = -ENODEV;
423 goto p_err;
424 }
425
426 client = cis->client;
427 if (unlikely(!client)) {
428 err("client is NULL");
429 ret = -ENODEV;
430 goto p_err;
431 }
432
433 I2C_MUTEX_LOCK(cis->i2c_lock);
434 pr_err("[SEN:DUMP] *******************************\n");
435 fimc_is_sensor_read16(client, 0x0000, &data16);
436 pr_err("[SEN:DUMP] model_id(%x)\n", data16);
437 fimc_is_sensor_read8(client, 0x0002, &data8);
438 pr_err("[SEN:DUMP] revision_number(%x)\n", data8);
439 fimc_is_sensor_read8(client, 0x0005, &data8);
440 pr_err("[SEN:DUMP] frame_count(%x)\n", data8);
441 fimc_is_sensor_read8(client, 0x0100, &data8);
442 pr_err("[SEN:DUMP] mode_select(%x)\n", data8);
443 I2C_MUTEX_UNLOCK(cis->i2c_lock);
444
445 pr_err("[SEN:DUMP] *******************************\n");
446
447 p_err:
448 return ret;
449 }
450
451 #if USE_GROUP_PARAM_HOLD
452 static int sensor_imx333_cis_group_param_hold_func(struct v4l2_subdev *subdev, unsigned int hold)
453 {
454 int ret = 0;
455 struct fimc_is_cis *cis = NULL;
456 struct i2c_client *client = NULL;
457
458 FIMC_BUG(!subdev);
459
460 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
461
462 FIMC_BUG(!cis);
463 FIMC_BUG(!cis->cis_data);
464
465 client = cis->client;
466 if (unlikely(!client)) {
467 err("client is NULL");
468 ret = -EINVAL;
469 goto p_err;
470 }
471
472 if (hold == cis->cis_data->group_param_hold) {
473 pr_debug("already group_param_hold (%d)\n", cis->cis_data->group_param_hold);
474 goto p_err;
475 }
476
477 ret = fimc_is_sensor_write8(client, 0x0104, hold);
478 if (ret < 0)
479 goto p_err;
480
481 cis->cis_data->group_param_hold = hold;
482 ret = 1;
483 p_err:
484 return ret;
485 }
486 #else
487 static inline int sensor_imx333_cis_group_param_hold_func(struct v4l2_subdev *subdev, unsigned int hold)
488 { return 0; }
489 #endif
490
491 /* Input
492 * hold : true - hold, flase - no hold
493 * Output
494 * return: 0 - no effect(already hold or no hold)
495 * positive - setted by request
496 * negative - ERROR value
497 */
498 int sensor_imx333_cis_group_param_hold(struct v4l2_subdev *subdev, bool hold)
499 {
500 int ret = 0;
501 struct fimc_is_cis *cis = NULL;
502
503 FIMC_BUG(!subdev);
504
505 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
506
507 FIMC_BUG(!cis);
508 FIMC_BUG(!cis->cis_data);
509
510 I2C_MUTEX_LOCK(cis->i2c_lock);
511 ret = sensor_imx333_cis_group_param_hold_func(subdev, hold);
512 if (ret < 0)
513 goto p_err;
514
515 p_err:
516 I2C_MUTEX_UNLOCK(cis->i2c_lock);
517 return ret;
518 }
519
520 int sensor_imx333_cis_set_global_setting(struct v4l2_subdev *subdev)
521 {
522 int ret = 0;
523 struct fimc_is_cis *cis = NULL;
524 struct fimc_is_module_enum *module;
525 struct fimc_is_device_sensor_peri *sensor_peri = NULL;
526 struct sensor_open_extended *ext_info;
527
528 FIMC_BUG(!subdev);
529
530 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
531 FIMC_BUG(!cis);
532
533 sensor_peri = container_of(cis, struct fimc_is_device_sensor_peri, cis);
534 module = sensor_peri->module;
535 ext_info = &module->ext;
536
537 I2C_MUTEX_LOCK(cis->i2c_lock);
538 /* setfile global setting is at camera entrance */
539 ret = sensor_cis_set_registers(subdev, sensor_imx333_global, sensor_imx333_global_size);
540 if (ret < 0) {
541 err("sensor_imx333_set_registers fail!!");
542 goto p_err;
543 }
544
545 dbg_sensor(1, "[%s] global setting done\n", __func__);
546
547 p_err:
548 I2C_MUTEX_UNLOCK(cis->i2c_lock);
549 return ret;
550 }
551
552 int sensor_imx333_cis_mode_change(struct v4l2_subdev *subdev, u32 mode)
553 {
554 int ret = 0;
555 struct fimc_is_cis *cis = NULL;
556 struct fimc_is_device_sensor_peri *sensor_peri = NULL;
557 struct fimc_is_module_enum *module;
558 struct sensor_open_extended *ext_info;
559
560 FIMC_BUG(!subdev);
561
562 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
563 FIMC_BUG(!cis);
564 FIMC_BUG(!cis->cis_data);
565
566 if (mode > sensor_imx333_max_setfile_num) {
567 err("invalid mode(%d)!!", mode);
568 ret = -EINVAL;
569 goto p_err;
570 }
571
572 /* If check_rev fail when cis_init, one more check_rev in mode_change */
573 if (cis->rev_flag == true) {
574 cis->rev_flag = false;
575 ret = sensor_cis_check_rev(cis);
576 if (ret < 0) {
577 err("sensor_imx333_check_rev is fail");
578 goto p_err;
579 }
580 }
581
582 #if 0 /* cis_data_calculation is called in module_s_format */
583 sensor_imx333_cis_data_calculation(sensor_imx333_pllinfos[mode], cis->cis_data);
584 #endif
585 sensor_imx333_set_integration_max_margin(mode, cis->cis_data);
586
587 sensor_peri = container_of(cis, struct fimc_is_device_sensor_peri, cis);
588 module = sensor_peri->module;
589 ext_info = &module->ext;
590
591 I2C_MUTEX_LOCK(cis->i2c_lock);
592
593 ret = sensor_cis_set_registers(subdev, sensor_imx333_setfiles[mode], sensor_imx333_setfile_sizes[mode]);
594 if (ret < 0) {
595 err("sensor_imx333_set_registers fail!!");
596 goto p_err;
597 }
598
599 dbg_sensor(1, "[%s] mode changed(%d)\n", __func__, mode);
600
601 p_err:
602 I2C_MUTEX_UNLOCK(cis->i2c_lock);
603 return ret;
604 }
605
606 /* TODO: Sensor set size sequence(sensor done, sensor stop, 3AA done in FW case */
607 int sensor_imx333_cis_set_size(struct v4l2_subdev *subdev, cis_shared_data *cis_data)
608 {
609 int ret = 0;
610 bool binning = false;
611 u32 ratio_w = 0, ratio_h = 0, start_x = 0, start_y = 0, end_x = 0, end_y = 0;
612 u32 even_x= 0, odd_x = 0, even_y = 0, odd_y = 0;
613 struct i2c_client *client = NULL;
614 struct fimc_is_cis *cis = NULL;
615 #ifdef DEBUG_SENSOR_TIME
616 struct timeval st, end;
617 do_gettimeofday(&st);
618 #endif
619 FIMC_BUG(!subdev);
620
621 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
622 FIMC_BUG(!cis);
623
624 dbg_sensor(1, "[MOD:D:%d] %s\n", cis->id, __func__);
625
626 if (unlikely(!cis_data)) {
627 err("cis data is NULL");
628 if (unlikely(!cis->cis_data)) {
629 ret = -EINVAL;
630 goto p_err;
631 } else {
632 cis_data = cis->cis_data;
633 }
634 }
635
636 client = cis->client;
637 if (unlikely(!client)) {
638 err("client is NULL");
639 ret = -EINVAL;
640 goto p_err;
641 }
642
643 /* Wait actual stream off */
644 ret = sensor_imx333_wait_stream_off_status(cis_data);
645 if (ret) {
646 err("Must stream off\n");
647 ret = -EINVAL;
648 goto p_err;
649 }
650
651 binning = cis_data->binning;
652 if (binning) {
653 ratio_w = (SENSOR_IMX333_MAX_WIDTH / cis_data->cur_width);
654 ratio_h = (SENSOR_IMX333_MAX_HEIGHT / cis_data->cur_height);
655 } else {
656 ratio_w = 1;
657 ratio_h = 1;
658 }
659
660 if (((cis_data->cur_width * ratio_w) > SENSOR_IMX333_MAX_WIDTH) ||
661 ((cis_data->cur_height * ratio_h) > SENSOR_IMX333_MAX_HEIGHT)) {
662 err("Config max sensor size over~!!\n");
663 ret = -EINVAL;
664 goto p_err;
665 }
666
667 I2C_MUTEX_LOCK(cis->i2c_lock);
668 /* 1. page_select */
669 ret = fimc_is_sensor_write16(client, 0x6028, 0x2000);
670 if (ret < 0)
671 goto p_err;
672
673 /* 2. pixel address region setting */
674 start_x = ((SENSOR_IMX333_MAX_WIDTH - cis_data->cur_width * ratio_w) / 2) & (~0x1);
675 start_y = ((SENSOR_IMX333_MAX_HEIGHT - cis_data->cur_height * ratio_h) / 2) & (~0x1);
676 end_x = start_x + (cis_data->cur_width * ratio_w - 1);
677 end_y = start_y + (cis_data->cur_height * ratio_h - 1);
678
679 if (!(end_x & (0x1)) || !(end_y & (0x1))) {
680 err("Sensor pixel end address must odd\n");
681 ret = -EINVAL;
682 goto p_err;
683 }
684
685 ret = fimc_is_sensor_write16(client, 0x0344, start_x);
686 if (ret < 0)
687 goto p_err;
688 ret = fimc_is_sensor_write16(client, 0x0346, start_y);
689 if (ret < 0)
690 goto p_err;
691 ret = fimc_is_sensor_write16(client, 0x0348, end_x);
692 if (ret < 0)
693 goto p_err;
694 ret = fimc_is_sensor_write16(client, 0x034A, end_y);
695 if (ret < 0)
696 goto p_err;
697
698 /* 3. output address setting */
699 ret = fimc_is_sensor_write16(client, 0x034C, cis_data->cur_width);
700 if (ret < 0)
701 goto p_err;
702 ret = fimc_is_sensor_write16(client, 0x034E, cis_data->cur_height);
703 if (ret < 0)
704 goto p_err;
705
706 /* If not use to binning, sensor image should set only crop */
707 if (!binning) {
708 dbg_sensor(1, "Sensor size set is not binning\n");
709 goto p_err;
710 }
711
712 /* 4. sub sampling setting */
713 even_x = 1; /* 1: not use to even sampling */
714 even_y = 1;
715 odd_x = (ratio_w * 2) - even_x;
716 odd_y = (ratio_h * 2) - even_y;
717
718 ret = fimc_is_sensor_write16(client, 0x0380, even_x);
719 if (ret < 0)
720 goto p_err;
721 ret = fimc_is_sensor_write16(client, 0x0382, odd_x);
722 if (ret < 0)
723 goto p_err;
724 ret = fimc_is_sensor_write16(client, 0x0384, even_y);
725 if (ret < 0)
726 goto p_err;
727 ret = fimc_is_sensor_write16(client, 0x0386, odd_y);
728 if (ret < 0)
729 goto p_err;
730
731 /* 5. binnig setting */
732 ret = fimc_is_sensor_write8(client, 0x0900, binning); /* 1: binning enable, 0: disable */
733 if (ret < 0)
734 goto p_err;
735 ret = fimc_is_sensor_write8(client, 0x0901, (ratio_w << 4) | ratio_h);
736 if (ret < 0)
737 goto p_err;
738
739 /* 6. scaling setting: but not use */
740 /* scaling_mode (0: No scaling, 1: Horizontal, 2: Full) */
741 ret = fimc_is_sensor_write16(client, 0x0400, 0x0000);
742 if (ret < 0)
743 goto p_err;
744 /* down_scale_m: 1 to 16 upwards (scale_n: 16(fixed))
745 down scale factor = down_scale_m / down_scale_n */
746 ret = fimc_is_sensor_write16(client, 0x0404, 0x0010);
747 if (ret < 0)
748 goto p_err;
749
750 cis_data->frame_time = (cis_data->line_readOut_time * cis_data->cur_height / 1000);
751 cis->cis_data->rolling_shutter_skew = (cis->cis_data->cur_height - 1) * cis->cis_data->line_readOut_time;
752 dbg_sensor(1, "[%s] frame_time(%d), rolling_shutter_skew(%lld)\n", __func__, cis->cis_data->frame_time,
753 cis->cis_data->rolling_shutter_skew);
754
755 #ifdef DEBUG_SENSOR_TIME
756 do_gettimeofday(&end);
757 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec) * 1000000 + (end.tv_usec - st.tv_usec));
758 #endif
759
760 p_err:
761 I2C_MUTEX_UNLOCK(cis->i2c_lock);
762 return ret;
763 }
764
765 int sensor_imx333_cis_stream_on(struct v4l2_subdev *subdev)
766 {
767 int ret = 0;
768 struct fimc_is_cis *cis;
769 struct i2c_client *client;
770 cis_shared_data *cis_data;
771 struct fimc_is_device_sensor_peri *sensor_peri = NULL;
772
773 #ifdef DEBUG_SENSOR_TIME
774 struct timeval st, end;
775 do_gettimeofday(&st);
776 #endif
777
778 FIMC_BUG(!subdev);
779
780 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
781
782 FIMC_BUG(!cis);
783 FIMC_BUG(!cis->cis_data);
784
785 sensor_peri = container_of(cis, struct fimc_is_device_sensor_peri, cis);
786 FIMC_BUG(!sensor_peri);
787
788 client = cis->client;
789 if (unlikely(!client)) {
790 err("client is NULL");
791 ret = -EINVAL;
792 goto p_err;
793 }
794
795 cis_data = cis->cis_data;
796
797 dbg_sensor(1, "[MOD:D:%d] %s\n", cis->id, __func__);
798
799 I2C_MUTEX_LOCK(cis->i2c_lock);
800 sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
801
802 #ifdef DEBUG_IMX333_PLL
803 {
804 u16 pll;
805 fimc_is_sensor_read16(client, 0x0300, &pll);
806 dbg_sensor(1, "______ vt_pix_clk_div(%x)\n", pll);
807 fimc_is_sensor_read16(client, 0x0302, &pll);
808 dbg_sensor(1, "______ vt_sys_clk_div(%x)\n", pll);
809 fimc_is_sensor_read16(client, 0x0304, &pll);
810 dbg_sensor(1, "______ pre_pll_clk_div(%x)\n", pll);
811 fimc_is_sensor_read16(client, 0x0306, &pll);
812 dbg_sensor(1, "______ pll_multiplier(%x)\n", pll);
813 fimc_is_sensor_read16(client, 0x0308, &pll);
814 dbg_sensor(1, "______ op_pix_clk_div(%x)\n", pll);
815 fimc_is_sensor_read16(client, 0x030a, &pll);
816 dbg_sensor(1, "______ op_sys_clk_div(%x)\n", pll);
817
818 fimc_is_sensor_read16(client, 0x030c, &pll);
819 dbg_sensor(1, "______ secnd_pre_pll_clk_div(%x)\n", pll);
820 fimc_is_sensor_read16(client, 0x030e, &pll);
821 dbg_sensor(1, "______ secnd_pll_multiplier(%x)\n", pll);
822 fimc_is_sensor_read16(client, 0x0340, &pll);
823 dbg_sensor(1, "______ frame_length_lines(%x)\n", pll);
824 fimc_is_sensor_read16(client, 0x0342, &pll);
825 dbg_sensor(1, "______ line_length_pck(%x)\n", pll);
826 }
827 #endif
828
829 /*
830 * If a companion is used,
831 * then 8 ms waiting is needed before the StreamOn of a sensor (IMX333).
832 */
833 if (test_bit(FIMC_IS_SENSOR_PREPROCESSOR_AVAILABLE, &sensor_peri->peri_state)) {
834 mdelay(8);
835 }
836
837 /* Sensor stream on */
838 fimc_is_sensor_write8(client, 0x0100, 0x01);
839
840 sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
841 I2C_MUTEX_UNLOCK(cis->i2c_lock);
842
843 cis_data->stream_on = true;
844
845 #ifdef DEBUG_SENSOR_TIME
846 do_gettimeofday(&end);
847 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
848 #endif
849
850 p_err:
851 return ret;
852 }
853
854 int sensor_imx333_cis_stream_off(struct v4l2_subdev *subdev)
855 {
856 int ret = 0;
857 struct fimc_is_cis *cis;
858 struct i2c_client *client;
859 cis_shared_data *cis_data;
860
861 #ifdef DEBUG_SENSOR_TIME
862 struct timeval st, end;
863 do_gettimeofday(&st);
864 #endif
865
866 FIMC_BUG(!subdev);
867
868 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
869
870 FIMC_BUG(!cis);
871 FIMC_BUG(!cis->cis_data);
872
873 client = cis->client;
874 if (unlikely(!client)) {
875 err("client is NULL");
876 ret = -EINVAL;
877 goto p_err;
878 }
879
880 cis_data = cis->cis_data;
881
882 dbg_sensor(1, "[MOD:D:%d] %s\n", cis->id, __func__);
883
884 I2C_MUTEX_LOCK(cis->i2c_lock);
885 ret = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
886 if (ret < 0)
887 err("group_param_hold_func failed at stream off");
888
889 fimc_is_sensor_write8(client, 0x0100, 0x00);
890 I2C_MUTEX_UNLOCK(cis->i2c_lock);
891
892 cis_data->stream_on = false;
893
894 #ifdef DEBUG_SENSOR_TIME
895 do_gettimeofday(&end);
896 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
897 #endif
898
899 p_err:
900 return ret;
901 }
902
903 int sensor_imx333_cis_compensate_gain_under_ext_br(struct v4l2_subdev *subdev, u32 expo, u32 *again, u32 *dgain)
904 {
905 int ret = 0;
906 struct fimc_is_cis *cis;
907 cis_shared_data *cis_data;
908
909 u32 ratio;
910 u32 vt_pic_clk_freq_mhz = 0;
911 u32 inflated_coarse_int = 0;
912 u32 coarse_int = 0;
913 u32 line_length_pck = 0;
914 u32 min_fine_int = 0;
915 u32 expected_again = 0;
916
917 FIMC_BUG(!subdev);
918 FIMC_BUG(!again);
919 FIMC_BUG(!dgain);
920
921 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
922 if (!cis) {
923 err("cis is NULL");
924 ret = -EINVAL;
925 goto p_err;
926 }
927 cis_data = cis->cis_data;
928
929 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
930 line_length_pck = cis_data->line_length_pck;
931 min_fine_int = cis_data->min_fine_integration_time;
932
933 if (line_length_pck <= 0) {
934 err("[%s] invalid line_length_pck(%d)\n", __func__, line_length_pck);
935 goto p_err;
936 }
937
938 inflated_coarse_int = (u32)((u64)(expo * vt_pic_clk_freq_mhz - min_fine_int)
939 * SENSOR_IMX333_DUMMY_MULTIPLIER / line_length_pck);
940 coarse_int = (u32)(inflated_coarse_int / SENSOR_IMX333_DUMMY_MULTIPLIER);
941
942 if (coarse_int > cis_data->max_coarse_integration_time) {
943 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long_coarse(%d) max(%d)\n", cis->id, __func__,
944 cis_data->sen_vsync_count, coarse_int, cis_data->max_coarse_integration_time);
945 inflated_coarse_int = cis_data->max_coarse_integration_time * SENSOR_IMX333_DUMMY_MULTIPLIER;
946 }
947
948 if (coarse_int < cis_data->min_coarse_integration_time) {
949 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long_coarse(%d) min(%d)\n", cis->id, __func__,
950 cis_data->sen_vsync_count, coarse_int, cis_data->min_coarse_integration_time);
951 inflated_coarse_int = cis_data->min_coarse_integration_time * SENSOR_IMX333_DUMMY_MULTIPLIER;
952 }
953
954 ratio = inflated_coarse_int / coarse_int;
955
956 if(coarse_int <= SENSOR_IMX333_BRIGHT_ILLUMINATION_COMPENSATE_THR)
957 {
958 if(cis_data->cur_coarse_integration_time_step == SENSOR_IMX333_COARSE_STEP_2N)
959 {
960 if(coarse_int % SENSOR_IMX333_COARSE_STEP_2N == 1)
961 {
962 if((coarse_int % SENSOR_IMX333_COARSE_STEP_2N) != 0)
963 {
964 ratio = inflated_coarse_int / (coarse_int - 1);
965 }
966 }
967 }
968 else if(cis_data->cur_coarse_integration_time_step == SENSOR_IMX333_COARSE_STEP_4N)
969 {
970 if((coarse_int % SENSOR_IMX333_COARSE_STEP_4N) != 0)
971 {
972 if((coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 1)
973 {
974 ratio = inflated_coarse_int / (coarse_int - 1);
975 }
976 else if((coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 2)
977 {
978 ratio = inflated_coarse_int / (coarse_int - 2);
979 }
980 else if((coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 3)
981 {
982 ratio = inflated_coarse_int / (coarse_int - 3);
983 }
984 }
985 }
986 else
987 {
988 err("[%s] Unsupported Coarse Integration Time Step Value:%d\n", __func__, cis_data->cur_coarse_integration_time_step);
989 }
990
991 expected_again = (u32)((*again) * ratio) / SENSOR_IMX333_DUMMY_MULTIPLIER;
992 if(expected_again < cis_data->min_analog_gain[1])
993 {
994 *again = cis_data->min_analog_gain[1];
995 }
996 else if(expected_again > cis_data->max_analog_gain[1])
997 {
998 *dgain = (u32)((*dgain) * ratio) / SENSOR_IMX333_DUMMY_MULTIPLIER;
999 }
1000 else
1001 {
1002 *again = expected_again;
1003 }
1004 }
1005 p_err:
1006 return ret;
1007 }
1008
1009 static void sensor_imx333_cis_calc_integration_time_by_step(u16 *coarse_int, u32 int_time_step)
1010 {
1011 if(int_time_step == SENSOR_IMX333_COARSE_STEP_2N)
1012 {
1013 if((*coarse_int % SENSOR_IMX333_COARSE_STEP_2N) != 0)
1014 {
1015 *coarse_int -= 1;
1016 }
1017
1018 if(*coarse_int <= SENSOR_IMX333_COARSE_STEP_2N)
1019 {
1020 *coarse_int = SENSOR_IMX333_COARSE_STEP_2N;
1021 }
1022 }
1023 else if(int_time_step == SENSOR_IMX333_COARSE_STEP_4N)
1024 {
1025 if((*coarse_int % SENSOR_IMX333_COARSE_STEP_4N) != 0)
1026 {
1027 if((*coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 1)
1028 {
1029 *coarse_int -= 1;
1030 }
1031 else if((*coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 2)
1032 {
1033 *coarse_int -= 2;
1034 }
1035 else if((*coarse_int % SENSOR_IMX333_COARSE_STEP_4N) == 3)
1036 {
1037 *coarse_int -= 3;
1038 }
1039 }
1040
1041 if(*coarse_int <= SENSOR_IMX333_COARSE_STEP_4N)
1042 {
1043 *coarse_int = SENSOR_IMX333_COARSE_STEP_4N;
1044 }
1045 }
1046 else
1047 {
1048 info("[%s] Unsupported Coarse Integration Time Step: %d\n", __func__, int_time_step);
1049 }
1050
1051 if(*coarse_int <= SENSOR_IMX333_COARSE_STEP_2N)
1052 {
1053 *coarse_int = SENSOR_IMX333_COARSE_STEP_2N;
1054 }
1055 }
1056
1057 int sensor_imx333_cis_set_exposure_time(struct v4l2_subdev *subdev, struct ae_param *target_exposure)
1058 {
1059 int ret = 0;
1060 int hold = 0;
1061 struct fimc_is_cis *cis;
1062 struct i2c_client *client;
1063 cis_shared_data *cis_data;
1064
1065 u32 vt_pic_clk_freq_mhz = 0;
1066 u16 long_coarse_int = 0;
1067 u16 short_coarse_int = 0;
1068 u32 line_length_pck = 0;
1069 u32 min_fine_int = 0;
1070 u8 arrayBuf[4];
1071
1072 #ifdef DEBUG_SENSOR_TIME
1073 struct timeval st, end;
1074 do_gettimeofday(&st);
1075 #endif
1076
1077 FIMC_BUG(!subdev);
1078 FIMC_BUG(!target_exposure);
1079
1080 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1081
1082 FIMC_BUG(!cis);
1083 FIMC_BUG(!cis->cis_data);
1084
1085 client = cis->client;
1086 if (unlikely(!client)) {
1087 err("client is NULL");
1088 ret = -EINVAL;
1089 goto p_err;
1090 }
1091
1092 if ((target_exposure->long_val <= 0) || (target_exposure->short_val <= 0)) {
1093 err("[%s] invalid target exposure(%d, %d)\n", __func__,
1094 target_exposure->long_val, target_exposure->short_val);
1095 ret = -EINVAL;
1096 goto p_err;
1097 }
1098
1099 cis_data = cis->cis_data;
1100
1101 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), target long(%d), short(%d)\n", cis->id, __func__,
1102 cis_data->sen_vsync_count, target_exposure->long_val, target_exposure->short_val);
1103
1104 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1105 line_length_pck = cis_data->line_length_pck;
1106 min_fine_int = cis_data->min_fine_integration_time;
1107
1108 long_coarse_int = ((target_exposure->long_val * vt_pic_clk_freq_mhz) - min_fine_int) / line_length_pck;
1109 short_coarse_int = ((target_exposure->short_val * vt_pic_clk_freq_mhz) - min_fine_int) / line_length_pck;
1110
1111 if (long_coarse_int > cis_data->max_coarse_integration_time) {
1112 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) max(%d)\n", cis->id, __func__,
1113 cis_data->sen_vsync_count, long_coarse_int, cis_data->max_coarse_integration_time);
1114 long_coarse_int = cis_data->max_coarse_integration_time;
1115 }
1116
1117 if (short_coarse_int > cis_data->max_coarse_integration_time) {
1118 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) max(%d)\n", cis->id, __func__,
1119 cis_data->sen_vsync_count, short_coarse_int, cis_data->max_coarse_integration_time);
1120 short_coarse_int = cis_data->max_coarse_integration_time;
1121 }
1122
1123 if (long_coarse_int < cis_data->min_coarse_integration_time) {
1124 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), long coarse(%d) min(%d)\n", cis->id, __func__,
1125 cis_data->sen_vsync_count, long_coarse_int, cis_data->min_coarse_integration_time);
1126 long_coarse_int = cis_data->min_coarse_integration_time;
1127 }
1128
1129 if (short_coarse_int < cis_data->min_coarse_integration_time) {
1130 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), short coarse(%d) min(%d)\n", cis->id, __func__,
1131 cis_data->sen_vsync_count, short_coarse_int, cis_data->min_coarse_integration_time);
1132 short_coarse_int = cis_data->min_coarse_integration_time;
1133 }
1134
1135 sensor_imx333_cis_calc_integration_time_by_step(&long_coarse_int, cis_data->cur_coarse_integration_time_step);
1136 sensor_imx333_cis_calc_integration_time_by_step(&short_coarse_int, cis_data->cur_coarse_integration_time_step);
1137
1138 cis_data->cur_long_exposure_coarse = long_coarse_int;
1139 cis_data->cur_short_exposure_coarse = short_coarse_int;
1140
1141 I2C_MUTEX_LOCK(cis->i2c_lock);
1142 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1143 if (hold < 0) {
1144 ret = hold;
1145 goto p_err;
1146 }
1147
1148 /* WDR mode off */
1149 if (fimc_is_vender_wdr_mode_on(cis_data)) {
1150 fimc_is_sensor_write8(cis->client, 0x0220, 0x61);
1151 } else {
1152 fimc_is_sensor_write8(cis->client, 0x0220, 0x00);
1153 }
1154
1155 /* Long exposure */
1156 arrayBuf[0] = (cis_data->cur_long_exposure_coarse & 0xFF00) >> 8;
1157 arrayBuf[1] = cis_data->cur_long_exposure_coarse & 0xFF;
1158 ret = fimc_is_sensor_write8_array(client, 0x0202, arrayBuf, 2);
1159 if (ret < 0)
1160 goto p_err;
1161
1162 /* Short exposure */
1163 /* if (fimc_is_vender_wdr_mode_on(cis_data)) */
1164 {
1165 arrayBuf[0] = (cis_data->cur_short_exposure_coarse & 0xFF00) >> 8;
1166 arrayBuf[1] = cis_data->cur_short_exposure_coarse & 0xFF;
1167 ret = fimc_is_sensor_write8_array(client, 0x0224, arrayBuf, 2);
1168 if (ret < 0)
1169 goto p_err;
1170 }
1171
1172 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), vt_pic_clk_freq_mhz (%d),"
1173 KERN_CONT "line_length_pck(%d), min_fine_int (%d)\n", cis->id, __func__,
1174 cis_data->sen_vsync_count, vt_pic_clk_freq_mhz, line_length_pck, min_fine_int);
1175 dbg_sensor(1, "[MOD:D:%d] %s, vsync_cnt(%d), frame_length_lines(%#x),"
1176 KERN_CONT "long_coarse_int %#x, short_coarse_int %#x\n", cis->id, __func__,
1177 cis_data->sen_vsync_count, cis_data->frame_length_lines, long_coarse_int, short_coarse_int);
1178
1179 #ifdef DEBUG_SENSOR_TIME
1180 do_gettimeofday(&end);
1181 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1182 #endif
1183
1184 p_err:
1185 if (hold > 0) {
1186 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1187 if (hold < 0)
1188 ret = hold;
1189 }
1190 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1191
1192 return ret;
1193 }
1194
1195 int sensor_imx333_cis_get_min_exposure_time(struct v4l2_subdev *subdev, u32 *min_expo)
1196 {
1197 int ret = 0;
1198 struct fimc_is_cis *cis = NULL;
1199 cis_shared_data *cis_data = NULL;
1200 u32 min_integration_time = 0;
1201 u32 min_coarse = 0;
1202 u32 min_fine = 0;
1203 u32 vt_pic_clk_freq_mhz = 0;
1204 u32 line_length_pck = 0;
1205
1206 #ifdef DEBUG_SENSOR_TIME
1207 struct timeval st, end;
1208 do_gettimeofday(&st);
1209 #endif
1210
1211 FIMC_BUG(!subdev);
1212 FIMC_BUG(!min_expo);
1213
1214 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1215
1216 FIMC_BUG(!cis);
1217 FIMC_BUG(!cis->cis_data);
1218
1219 cis_data = cis->cis_data;
1220
1221 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1222 if (vt_pic_clk_freq_mhz == 0) {
1223 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis->id, __func__, vt_pic_clk_freq_mhz);
1224 goto p_err;
1225 }
1226 line_length_pck = cis_data->line_length_pck;
1227 min_coarse = cis_data->min_coarse_integration_time;
1228 min_fine = cis_data->min_fine_integration_time;
1229
1230 min_integration_time = ((line_length_pck * min_coarse) + min_fine) / vt_pic_clk_freq_mhz;
1231 *min_expo = min_integration_time;
1232
1233 dbg_sensor(1, "[%s] min integration time %d\n", __func__, min_integration_time);
1234
1235 #ifdef DEBUG_SENSOR_TIME
1236 do_gettimeofday(&end);
1237 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1238 #endif
1239
1240 p_err:
1241 return ret;
1242 }
1243
1244 int sensor_imx333_cis_get_max_exposure_time(struct v4l2_subdev *subdev, u32 *max_expo)
1245 {
1246 int ret = 0;
1247 struct fimc_is_cis *cis;
1248 cis_shared_data *cis_data;
1249 u32 max_integration_time = 0;
1250 u32 max_coarse_margin = 0;
1251 u32 max_fine_margin = 0;
1252 u32 max_coarse = 0;
1253 u32 max_fine = 0;
1254 u32 vt_pic_clk_freq_mhz = 0;
1255 u32 line_length_pck = 0;
1256 u32 frame_length_lines = 0;
1257
1258 #ifdef DEBUG_SENSOR_TIME
1259 struct timeval st, end;
1260 do_gettimeofday(&st);
1261 #endif
1262
1263 FIMC_BUG(!subdev);
1264 FIMC_BUG(!max_expo);
1265
1266 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1267
1268 FIMC_BUG(!cis);
1269 FIMC_BUG(!cis->cis_data);
1270
1271 cis_data = cis->cis_data;
1272
1273 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1274 if (vt_pic_clk_freq_mhz == 0) {
1275 pr_err("[MOD:D:%d] %s, Invalid vt_pic_clk_freq_mhz(%d)\n", cis->id, __func__, vt_pic_clk_freq_mhz);
1276 goto p_err;
1277 }
1278 line_length_pck = cis_data->line_length_pck;
1279 frame_length_lines = cis_data->frame_length_lines;
1280
1281 max_coarse_margin = cis_data->max_margin_coarse_integration_time;
1282 max_fine_margin = line_length_pck - cis_data->min_fine_integration_time;
1283 max_coarse = frame_length_lines - max_coarse_margin;
1284 max_fine = cis_data->max_fine_integration_time;
1285
1286 max_integration_time = ((line_length_pck * max_coarse) + max_fine) / vt_pic_clk_freq_mhz;
1287
1288 *max_expo = max_integration_time;
1289
1290 /* TODO: Is this values update hear? */
1291 cis_data->max_margin_fine_integration_time = max_fine_margin;
1292 cis_data->max_coarse_integration_time = max_coarse;
1293
1294 dbg_sensor(1, "[%s] max integration time %d, max margin fine integration %d, max coarse integration %d\n",
1295 __func__, max_integration_time, cis_data->max_margin_fine_integration_time, cis_data->max_coarse_integration_time);
1296
1297 #ifdef DEBUG_SENSOR_TIME
1298 do_gettimeofday(&end);
1299 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1300 #endif
1301
1302 p_err:
1303 return ret;
1304 }
1305
1306 int sensor_imx333_cis_adjust_frame_duration(struct v4l2_subdev *subdev,
1307 u32 input_exposure_time,
1308 u32 *target_duration)
1309 {
1310 int ret = 0;
1311 struct fimc_is_cis *cis;
1312 cis_shared_data *cis_data;
1313
1314 u32 vt_pic_clk_freq_mhz = 0;
1315 u32 line_length_pck = 0;
1316 u32 frame_length_lines = 0;
1317 u32 frame_duration = 0;
1318
1319 #ifdef DEBUG_SENSOR_TIME
1320 struct timeval st, end;
1321 do_gettimeofday(&st);
1322 #endif
1323
1324 FIMC_BUG(!subdev);
1325 FIMC_BUG(!target_duration);
1326
1327 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1328
1329 FIMC_BUG(!cis);
1330 FIMC_BUG(!cis->cis_data);
1331
1332 cis_data = cis->cis_data;
1333
1334 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1335 line_length_pck = cis_data->line_length_pck;
1336 frame_length_lines = ((vt_pic_clk_freq_mhz * input_exposure_time) / line_length_pck);
1337 frame_length_lines += cis_data->max_margin_coarse_integration_time;
1338
1339 frame_duration = (frame_length_lines * line_length_pck) / vt_pic_clk_freq_mhz;
1340
1341 dbg_sensor(1, "[%s](vsync cnt = %d) input exp(%d), adj duration, frame duraion(%d), min_frame_us(%d)\n",
1342 __func__, cis_data->sen_vsync_count, input_exposure_time, frame_duration, cis_data->min_frame_us_time);
1343 dbg_sensor(1, "[%s](vsync cnt = %d) adj duration, frame duraion(%d), min_frame_us(%d)\n",
1344 __func__, cis_data->sen_vsync_count, frame_duration, cis_data->min_frame_us_time);
1345
1346 *target_duration = MAX(frame_duration, cis_data->min_frame_us_time);
1347
1348 #ifdef DEBUG_SENSOR_TIME
1349 do_gettimeofday(&end);
1350 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1351 #endif
1352
1353 return ret;
1354 }
1355
1356 int sensor_imx333_cis_set_frame_duration(struct v4l2_subdev *subdev, u32 frame_duration)
1357 {
1358 int ret = 0;
1359 int hold = 0;
1360 struct fimc_is_cis *cis;
1361 struct i2c_client *client;
1362 cis_shared_data *cis_data;
1363
1364 u32 vt_pic_clk_freq_mhz = 0;
1365 u32 line_length_pck = 0;
1366 u16 frame_length_lines = 0;
1367
1368 #ifdef DEBUG_SENSOR_TIME
1369 struct timeval st, end;
1370 do_gettimeofday(&st);
1371 #endif
1372
1373 FIMC_BUG(!subdev);
1374
1375 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1376
1377 FIMC_BUG(!cis);
1378 FIMC_BUG(!cis->cis_data);
1379
1380 client = cis->client;
1381 if (unlikely(!client)) {
1382 err("client is NULL");
1383 ret = -EINVAL;
1384 goto p_err;
1385 }
1386
1387 cis_data = cis->cis_data;
1388
1389 if (frame_duration < cis_data->min_frame_us_time) {
1390 dbg_sensor(1, "frame duration is less than min(%d)\n", frame_duration);
1391 frame_duration = cis_data->min_frame_us_time;
1392 }
1393
1394 vt_pic_clk_freq_mhz = cis_data->pclk / (1000 * 1000);
1395 line_length_pck = cis_data->line_length_pck;
1396
1397 frame_length_lines = (u16)((vt_pic_clk_freq_mhz * frame_duration) / line_length_pck);
1398
1399 dbg_sensor(1, "[MOD:D:%d] %s, vt_pic_clk_freq_mhz(%#x) frame_duration = %d us,"
1400 KERN_CONT "(line_length_pck%#x), frame_length_lines(%#x)\n",
1401 cis->id, __func__, vt_pic_clk_freq_mhz, frame_duration,
1402 line_length_pck, frame_length_lines);
1403
1404 I2C_MUTEX_LOCK(cis->i2c_lock);
1405 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1406 if (hold < 0) {
1407 ret = hold;
1408 goto p_err;
1409 }
1410
1411 ret = fimc_is_sensor_write16(client, 0x0340, frame_length_lines);
1412 if (ret < 0)
1413 goto p_err;
1414
1415 cis_data->cur_frame_us_time = frame_duration;
1416 cis_data->frame_length_lines = frame_length_lines;
1417 cis_data->max_coarse_integration_time = cis_data->frame_length_lines - cis_data->max_margin_coarse_integration_time;
1418
1419 #ifdef DEBUG_SENSOR_TIME
1420 do_gettimeofday(&end);
1421 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1422 #endif
1423
1424 p_err:
1425 if (hold > 0) {
1426 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1427 if (hold < 0)
1428 ret = hold;
1429 }
1430 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1431
1432 return ret;
1433 }
1434
1435 int sensor_imx333_cis_set_frame_rate(struct v4l2_subdev *subdev, u32 min_fps)
1436 {
1437 int ret = 0;
1438 struct fimc_is_cis *cis;
1439 cis_shared_data *cis_data;
1440
1441 u32 frame_duration = 0;
1442
1443 #ifdef DEBUG_SENSOR_TIME
1444 struct timeval st, end;
1445 do_gettimeofday(&st);
1446 #endif
1447
1448 FIMC_BUG(!subdev);
1449
1450 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1451
1452 FIMC_BUG(!cis);
1453 FIMC_BUG(!cis->cis_data);
1454
1455 cis_data = cis->cis_data;
1456
1457 if (min_fps > cis_data->max_fps) {
1458 err("[MOD:D:%d] %s, request FPS is too high(%d), set to max(%d)\n",
1459 cis->id, __func__, min_fps, cis_data->max_fps);
1460 min_fps = cis_data->max_fps;
1461 }
1462
1463 if (min_fps == 0) {
1464 err("[MOD:D:%d] %s, request FPS is 0, set to min FPS(1)\n",
1465 cis->id, __func__);
1466 min_fps = 1;
1467 }
1468
1469 frame_duration = (1 * 1000 * 1000) / min_fps;
1470
1471 dbg_sensor(1, "[MOD:D:%d] %s, set FPS(%d), frame duration(%d)\n",
1472 cis->id, __func__, min_fps, frame_duration);
1473
1474 ret = sensor_imx333_cis_set_frame_duration(subdev, frame_duration);
1475 if (ret < 0) {
1476 err("[MOD:D:%d] %s, set frame duration is fail(%d)\n",
1477 cis->id, __func__, ret);
1478 goto p_err;
1479 }
1480
1481 cis_data->min_frame_us_time = frame_duration;
1482
1483 #ifdef DEBUG_SENSOR_TIME
1484 do_gettimeofday(&end);
1485 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1486 #endif
1487
1488 p_err:
1489
1490 return ret;
1491 }
1492
1493 u32 sensor_imx333_cis_calc_again_code(u32 permille)
1494 {
1495 return 512 - (512000 / permille);
1496 }
1497
1498 u32 sensor_imx333_cis_calc_again_permile(u32 code)
1499 {
1500 return 512000 / (512 - code);
1501 }
1502
1503 int sensor_imx333_cis_adjust_analog_gain(struct v4l2_subdev *subdev, u32 input_again, u32 *target_permile)
1504 {
1505 int ret = 0;
1506 struct fimc_is_cis *cis;
1507 cis_shared_data *cis_data;
1508
1509 u32 again_code = 0;
1510 u32 again_permile = 0;
1511
1512 #ifdef DEBUG_SENSOR_TIME
1513 struct timeval st, end;
1514 do_gettimeofday(&st);
1515 #endif
1516
1517 FIMC_BUG(!subdev);
1518 FIMC_BUG(!target_permile);
1519
1520 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1521
1522 FIMC_BUG(!cis);
1523 FIMC_BUG(!cis->cis_data);
1524
1525 cis_data = cis->cis_data;
1526
1527 again_code = sensor_cis_calc_again_code(input_again);
1528
1529 if (again_code > cis_data->max_analog_gain[0]) {
1530 again_code = cis_data->max_analog_gain[0];
1531 } else if (again_code < cis_data->min_analog_gain[0]) {
1532 again_code = cis_data->min_analog_gain[0];
1533 }
1534
1535 again_permile = sensor_imx333_cis_calc_again_permile(again_code);
1536
1537 dbg_sensor(1, "[%s] min again(%d), max(%d), input_again(%d), code(%d), permile(%d)\n", __func__,
1538 cis_data->max_analog_gain[0],
1539 cis_data->min_analog_gain[0],
1540 input_again,
1541 again_code,
1542 again_permile);
1543
1544 *target_permile = again_permile;
1545
1546 return ret;
1547 }
1548
1549 int sensor_imx333_cis_set_analog_gain(struct v4l2_subdev *subdev, struct ae_param *again)
1550 {
1551 int ret = 0;
1552 int hold = 0;
1553 struct fimc_is_cis *cis;
1554 struct i2c_client *client;
1555
1556 u16 analog_gain = 0;
1557 u8 arrayBuf[2];
1558
1559 #ifdef DEBUG_SENSOR_TIME
1560 struct timeval st, end;
1561 do_gettimeofday(&st);
1562 #endif
1563
1564 FIMC_BUG(!subdev);
1565 FIMC_BUG(!again);
1566
1567 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1568
1569 FIMC_BUG(!cis);
1570
1571 client = cis->client;
1572 if (unlikely(!client)) {
1573 err("client is NULL");
1574 ret = -EINVAL;
1575 goto p_err;
1576 }
1577
1578 analog_gain = (u16)sensor_imx333_cis_calc_again_code(again->val);
1579
1580 if (analog_gain < cis->cis_data->min_analog_gain[0]) {
1581 analog_gain = cis->cis_data->min_analog_gain[0];
1582 }
1583
1584 if (analog_gain > cis->cis_data->max_analog_gain[0]) {
1585 analog_gain = cis->cis_data->max_analog_gain[0];
1586 }
1587
1588 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_again = %d us, analog_gain(%#x)\n",
1589 cis->id, __func__, cis->cis_data->sen_vsync_count, again->val, analog_gain);
1590
1591 I2C_MUTEX_LOCK(cis->i2c_lock);
1592 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1593 if (hold < 0) {
1594 ret = hold;
1595 goto p_err;
1596 }
1597
1598 /* Analog gain */
1599 arrayBuf[0] = (analog_gain & 0xFF00) >> 8;
1600 arrayBuf[1] = analog_gain & 0xFF;
1601 ret = fimc_is_sensor_write8_array(client, 0x0204, arrayBuf, 2);
1602 if (ret < 0)
1603 goto p_err;
1604
1605 #ifdef DEBUG_SENSOR_TIME
1606 do_gettimeofday(&end);
1607 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1608 #endif
1609
1610 p_err:
1611 if (hold > 0) {
1612 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1613 if (hold < 0)
1614 ret = hold;
1615 }
1616 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1617
1618 return ret;
1619 }
1620
1621 int sensor_imx333_cis_get_analog_gain(struct v4l2_subdev *subdev, u32 *again)
1622 {
1623 int ret = 0;
1624 int hold = 0;
1625 struct fimc_is_cis *cis;
1626 struct i2c_client *client;
1627
1628 u16 analog_gain = 0;
1629
1630 #ifdef DEBUG_SENSOR_TIME
1631 struct timeval st, end;
1632 do_gettimeofday(&st);
1633 #endif
1634
1635 FIMC_BUG(!subdev);
1636 FIMC_BUG(!again);
1637
1638 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1639
1640 FIMC_BUG(!cis);
1641
1642 client = cis->client;
1643 if (unlikely(!client)) {
1644 err("client is NULL");
1645 ret = -EINVAL;
1646 goto p_err;
1647 }
1648
1649 I2C_MUTEX_LOCK(cis->i2c_lock);
1650 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1651 if (hold < 0) {
1652 ret = hold;
1653 goto p_err;
1654 }
1655
1656 ret = fimc_is_sensor_read16(client, 0x0204, &analog_gain);
1657 if (ret < 0)
1658 goto p_err;
1659
1660 *again = sensor_imx333_cis_calc_again_permile(analog_gain);
1661
1662 dbg_sensor(1, "[MOD:D:%d] %s, cur_again = %d us, analog_gain(%#x)\n",
1663 cis->id, __func__, *again, analog_gain);
1664
1665 #ifdef DEBUG_SENSOR_TIME
1666 do_gettimeofday(&end);
1667 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1668 #endif
1669
1670 p_err:
1671 if (hold > 0) {
1672 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1673 if (hold < 0)
1674 ret = hold;
1675 }
1676 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1677
1678 return ret;
1679 }
1680
1681 int sensor_imx333_cis_get_min_analog_gain(struct v4l2_subdev *subdev, u32 *min_again)
1682 {
1683 int ret = 0;
1684 struct fimc_is_cis *cis;
1685 struct i2c_client *client;
1686 cis_shared_data *cis_data;
1687
1688 #ifdef DEBUG_SENSOR_TIME
1689 struct timeval st, end;
1690 do_gettimeofday(&st);
1691 #endif
1692
1693 FIMC_BUG(!subdev);
1694 FIMC_BUG(!min_again);
1695
1696 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1697
1698 FIMC_BUG(!cis);
1699 FIMC_BUG(!cis->cis_data);
1700
1701 client = cis->client;
1702 if (unlikely(!client)) {
1703 err("client is NULL");
1704 ret = -EINVAL;
1705 goto p_err;
1706 }
1707
1708 cis_data = cis->cis_data;
1709
1710 cis_data->min_analog_gain[0] = 0x0;
1711 cis_data->min_analog_gain[1] = sensor_imx333_cis_calc_again_permile(cis_data->min_analog_gain[0]);
1712 *min_again = cis_data->min_analog_gain[1];
1713
1714 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->min_analog_gain[0],
1715 cis_data->min_analog_gain[1]);
1716
1717 #ifdef DEBUG_SENSOR_TIME
1718 do_gettimeofday(&end);
1719 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1720 #endif
1721
1722 p_err:
1723 return ret;
1724 }
1725
1726 int sensor_imx333_cis_get_max_analog_gain(struct v4l2_subdev *subdev, u32 *max_again)
1727 {
1728 int ret = 0;
1729 struct fimc_is_cis *cis;
1730 struct i2c_client *client;
1731 cis_shared_data *cis_data;
1732
1733 #ifdef DEBUG_SENSOR_TIME
1734 struct timeval st, end;
1735 do_gettimeofday(&st);
1736 #endif
1737
1738 FIMC_BUG(!subdev);
1739 FIMC_BUG(!max_again);
1740
1741 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1742
1743 FIMC_BUG(!cis);
1744 FIMC_BUG(!cis->cis_data);
1745
1746 client = cis->client;
1747 if (unlikely(!client)) {
1748 err("client is NULL");
1749 ret = -EINVAL;
1750 goto p_err;
1751 }
1752
1753 cis_data = cis->cis_data;
1754 cis_data->max_analog_gain[0] = 0x1C0;
1755 cis_data->max_analog_gain[1] = sensor_imx333_cis_calc_again_permile(cis_data->max_analog_gain[0]);
1756 *max_again = cis_data->max_analog_gain[1];
1757
1758 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->max_analog_gain[0],
1759 cis_data->max_analog_gain[1]);
1760
1761 #ifdef DEBUG_SENSOR_TIME
1762 do_gettimeofday(&end);
1763 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1764 #endif
1765
1766 p_err:
1767 return ret;
1768 }
1769
1770 int sensor_imx333_cis_set_digital_gain(struct v4l2_subdev *subdev, struct ae_param *dgain)
1771 {
1772 int ret = 0;
1773 int hold = 0;
1774 struct fimc_is_cis *cis;
1775 struct i2c_client *client;
1776 cis_shared_data *cis_data;
1777
1778 u16 long_gain = 0;
1779 u16 short_gain = 0;
1780 u8 dgains[2] = {0};
1781
1782 #ifdef DEBUG_SENSOR_TIME
1783 struct timeval st, end;
1784 do_gettimeofday(&st);
1785 #endif
1786
1787 FIMC_BUG(!subdev);
1788 FIMC_BUG(!dgain);
1789
1790 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1791
1792 FIMC_BUG(!cis);
1793 FIMC_BUG(!cis->cis_data);
1794
1795 client = cis->client;
1796 if (unlikely(!client)) {
1797 err("client is NULL");
1798 ret = -EINVAL;
1799 goto p_err;
1800 }
1801
1802 cis_data = cis->cis_data;
1803
1804 long_gain = (u16)sensor_cis_calc_dgain_code(dgain->long_val);
1805 short_gain = (u16)sensor_cis_calc_dgain_code(dgain->short_val);
1806
1807 if (long_gain < cis->cis_data->min_digital_gain[0]) {
1808 long_gain = cis->cis_data->min_digital_gain[0];
1809 }
1810 if (long_gain > cis->cis_data->max_digital_gain[0]) {
1811 long_gain = cis->cis_data->max_digital_gain[0];
1812 }
1813
1814 if (short_gain < cis->cis_data->min_digital_gain[0]) {
1815 short_gain = cis->cis_data->min_digital_gain[0];
1816 }
1817 if (short_gain > cis->cis_data->max_digital_gain[0]) {
1818 short_gain = cis->cis_data->max_digital_gain[0];
1819 }
1820
1821 dbg_sensor(1, "[MOD:D:%d] %s(vsync cnt = %d), input_dgain = %d/%d us,"
1822 KERN_CONT "long_gain(%#x), short_gain(%#x)\n",
1823 cis->id, __func__, cis->cis_data->sen_vsync_count,
1824 dgain->long_val, dgain->short_val, long_gain, short_gain);
1825
1826 I2C_MUTEX_LOCK(cis->i2c_lock);
1827 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1828 if (hold < 0) {
1829 ret = hold;
1830 goto p_err;
1831 }
1832
1833 /* Set current short & long digitial gain */
1834 /* 0x0226 ~ 0x0227 : ST_DIG_GAIN_GLOBAL */
1835 /* 0x020E ~ 0x020F : DIG_GAIN_GLOBAL */
1836 /* if (fimc_is_vender_wdr_mode_on(cis_data)) */
1837 {
1838 dgains[0] = (short_gain & 0xFF00) >> 8;
1839 dgains[1] = short_gain & 0xFF;
1840 ret = fimc_is_sensor_write8_array(client, 0x0226, dgains, 2);
1841 if (ret < 0)
1842 goto p_err;
1843 }
1844 dgains[0] = (long_gain & 0xFF00) >> 8;
1845 dgains[1] = long_gain & 0xFF;
1846 ret = fimc_is_sensor_write8_array(client, 0x020E, dgains, 2);
1847 if (ret < 0)
1848 goto p_err;
1849
1850 #ifdef DEBUG_SENSOR_TIME
1851 do_gettimeofday(&end);
1852 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1853 #endif
1854
1855 p_err:
1856 if (hold > 0) {
1857 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1858 if (hold < 0)
1859 ret = hold;
1860 }
1861 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1862
1863 return ret;
1864 }
1865
1866 int sensor_imx333_cis_get_digital_gain(struct v4l2_subdev *subdev, u32 *dgain)
1867 {
1868 int ret = 0;
1869 int hold = 0;
1870 struct fimc_is_cis *cis;
1871 struct i2c_client *client;
1872
1873 u16 digital_gain = 0;
1874
1875 #ifdef DEBUG_SENSOR_TIME
1876 struct timeval st, end;
1877 do_gettimeofday(&st);
1878 #endif
1879
1880 FIMC_BUG(!subdev);
1881 FIMC_BUG(!dgain);
1882
1883 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1884
1885 FIMC_BUG(!cis);
1886
1887 client = cis->client;
1888 if (unlikely(!client)) {
1889 err("client is NULL");
1890 ret = -EINVAL;
1891 goto p_err;
1892 }
1893
1894 I2C_MUTEX_LOCK(cis->i2c_lock);
1895 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x01);
1896 if (hold < 0) {
1897 ret = hold;
1898 goto p_err;
1899 }
1900
1901 ret = fimc_is_sensor_read16(client, 0x020E, &digital_gain);
1902 if (ret < 0)
1903 goto p_err;
1904
1905 *dgain = sensor_cis_calc_dgain_permile(digital_gain);
1906
1907 dbg_sensor(1, "[MOD:D:%d] %s, cur_dgain = %d us, digital_gain(%#x)\n",
1908 cis->id, __func__, *dgain, digital_gain);
1909
1910 #ifdef DEBUG_SENSOR_TIME
1911 do_gettimeofday(&end);
1912 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1913 #endif
1914
1915 p_err:
1916 if (hold > 0) {
1917 hold = sensor_imx333_cis_group_param_hold_func(subdev, 0x00);
1918 if (hold < 0)
1919 ret = hold;
1920 }
1921 I2C_MUTEX_UNLOCK(cis->i2c_lock);
1922
1923 return ret;
1924 }
1925
1926 int sensor_imx333_cis_get_min_digital_gain(struct v4l2_subdev *subdev, u32 *min_dgain)
1927 {
1928 int ret = 0;
1929 struct fimc_is_cis *cis;
1930 struct i2c_client *client;
1931 cis_shared_data *cis_data;
1932
1933 #ifdef DEBUG_SENSOR_TIME
1934 struct timeval st, end;
1935 do_gettimeofday(&st);
1936 #endif
1937
1938 FIMC_BUG(!subdev);
1939 FIMC_BUG(!min_dgain);
1940
1941 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1942
1943 FIMC_BUG(!cis);
1944 FIMC_BUG(!cis->cis_data);
1945
1946 client = cis->client;
1947 if (unlikely(!client)) {
1948 err("client is NULL");
1949 ret = -EINVAL;
1950 goto p_err;
1951 }
1952
1953 cis_data = cis->cis_data;
1954 cis_data->min_digital_gain[0] = 0x100;
1955 cis_data->min_digital_gain[1] = sensor_cis_calc_dgain_permile(cis_data->min_digital_gain[0]);
1956
1957 *min_dgain = cis_data->min_digital_gain[1];
1958
1959 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->min_digital_gain[0],
1960 cis_data->min_digital_gain[1]);
1961
1962 #ifdef DEBUG_SENSOR_TIME
1963 do_gettimeofday(&end);
1964 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
1965 #endif
1966
1967 p_err:
1968 return ret;
1969 }
1970
1971 int sensor_imx333_cis_get_max_digital_gain(struct v4l2_subdev *subdev, u32 *max_dgain)
1972 {
1973 int ret = 0;
1974 struct fimc_is_cis *cis;
1975 struct i2c_client *client;
1976 cis_shared_data *cis_data;
1977
1978 #ifdef DEBUG_SENSOR_TIME
1979 struct timeval st, end;
1980 do_gettimeofday(&st);
1981 #endif
1982
1983 FIMC_BUG(!subdev);
1984 FIMC_BUG(!max_dgain);
1985
1986 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
1987
1988 FIMC_BUG(!cis);
1989 FIMC_BUG(!cis->cis_data);
1990
1991 client = cis->client;
1992 if (unlikely(!client)) {
1993 err("client is NULL");
1994 ret = -EINVAL;
1995 goto p_err;
1996 }
1997
1998 cis_data = cis->cis_data;
1999 cis_data->max_digital_gain[0] = 0xfff;
2000 cis_data->max_digital_gain[1] = sensor_cis_calc_dgain_permile(cis_data->max_digital_gain[0]);
2001
2002 *max_dgain = cis_data->max_digital_gain[1];
2003
2004 dbg_sensor(1, "[%s] code %d, permile %d\n", __func__, cis_data->max_digital_gain[0],
2005 cis_data->max_digital_gain[1]);
2006
2007 #ifdef DEBUG_SENSOR_TIME
2008 do_gettimeofday(&end);
2009 dbg_sensor(1, "[%s] time %lu us\n", __func__, (end.tv_sec - st.tv_sec)*1000000 + (end.tv_usec - st.tv_usec));
2010 #endif
2011
2012 p_err:
2013 return ret;
2014 }
2015
2016 int sensor_imx333_cis_long_term_exposure(struct v4l2_subdev *subdev)
2017 {
2018 int ret = 0;
2019 struct fimc_is_cis *cis;
2020 struct fimc_is_long_term_expo_mode *lte_mode;
2021 unsigned char cit_lshift_val = 0;
2022
2023 FIMC_BUG(!subdev);
2024
2025 cis = (struct fimc_is_cis *)v4l2_get_subdevdata(subdev);
2026 lte_mode = &cis->long_term_mode;
2027
2028 /* LTE mode or normal mode set */
2029 if (lte_mode->sen_strm_off_on_enable) {
2030 if (lte_mode->expo[0] > 500000) {
2031 cit_lshift_val = (unsigned char)(lte_mode->expo[0] / 500000);
2032 cit_lshift_val = cit_lshift_val / 2;
2033 lte_mode->expo[0] = 500000;
2034 ret |= fimc_is_sensor_write8(cis->client, 0x3008, cit_lshift_val);
2035 }
2036 } else {
2037 cit_lshift_val = 0;
2038 ret |= fimc_is_sensor_write8(cis->client, 0x3008, cit_lshift_val);
2039 }
2040
2041 info("%s enable(%d) %d %d", __func__, lte_mode->sen_strm_off_on_enable, cit_lshift_val, lte_mode->expo[0]);
2042
2043 if (ret < 0) {
2044 pr_err("ERR[%s]: LTE register setting fail\n", __func__);
2045 return ret;
2046 }
2047
2048 return ret;
2049 }
2050 static struct fimc_is_cis_ops cis_ops_imx333 = {
2051 .cis_init = sensor_imx333_cis_init,
2052 .cis_log_status = sensor_imx333_cis_log_status,
2053 .cis_group_param_hold = sensor_imx333_cis_group_param_hold,
2054 .cis_set_global_setting = sensor_imx333_cis_set_global_setting,
2055 .cis_mode_change = sensor_imx333_cis_mode_change,
2056 .cis_set_size = sensor_imx333_cis_set_size,
2057 .cis_stream_on = sensor_imx333_cis_stream_on,
2058 .cis_stream_off = sensor_imx333_cis_stream_off,
2059 .cis_set_exposure_time = sensor_imx333_cis_set_exposure_time,
2060 .cis_get_min_exposure_time = sensor_imx333_cis_get_min_exposure_time,
2061 .cis_get_max_exposure_time = sensor_imx333_cis_get_max_exposure_time,
2062 .cis_adjust_frame_duration = sensor_imx333_cis_adjust_frame_duration,
2063 .cis_set_frame_duration = sensor_imx333_cis_set_frame_duration,
2064 .cis_set_frame_rate = sensor_imx333_cis_set_frame_rate,
2065 .cis_adjust_analog_gain = sensor_imx333_cis_adjust_analog_gain,
2066 .cis_set_analog_gain = sensor_imx333_cis_set_analog_gain,
2067 .cis_get_analog_gain = sensor_imx333_cis_get_analog_gain,
2068 .cis_get_min_analog_gain = sensor_imx333_cis_get_min_analog_gain,
2069 .cis_get_max_analog_gain = sensor_imx333_cis_get_max_analog_gain,
2070 .cis_set_digital_gain = sensor_imx333_cis_set_digital_gain,
2071 .cis_get_digital_gain = sensor_imx333_cis_get_digital_gain,
2072 .cis_get_min_digital_gain = sensor_imx333_cis_get_min_digital_gain,
2073 .cis_get_max_digital_gain = sensor_imx333_cis_get_max_digital_gain,
2074 #if 0
2075 .cis_compensate_gain_for_extremely_br = sensor_cis_compensate_gain_for_extremely_br,
2076 #else
2077 .cis_compensate_gain_for_extremely_br = sensor_imx333_cis_compensate_gain_under_ext_br,
2078 #endif
2079 .cis_wait_streamoff = sensor_cis_wait_streamoff,
2080 .cis_data_calculation = sensor_imx333_cis_data_calc,
2081 .cis_set_long_term_exposure = sensor_imx333_cis_long_term_exposure,
2082 };
2083
2084 static int cis_imx333_probe(struct i2c_client *client,
2085 const struct i2c_device_id *id)
2086 {
2087 int ret = 0;
2088 struct fimc_is_core *core = NULL;
2089 struct v4l2_subdev *subdev_cis = NULL;
2090 struct fimc_is_cis *cis = NULL;
2091 struct fimc_is_device_sensor *device = NULL;
2092 struct fimc_is_device_sensor_peri *sensor_peri = NULL;
2093 u32 sensor_id[FIMC_IS_SENSOR_COUNT] = {0, };
2094 u32 sensor_id_len;
2095 const u32 *sensor_id_spec;
2096 char const *setfile;
2097 struct device *dev;
2098 struct device_node *dnode;
2099 int i;
2100
2101 FIMC_BUG(!client);
2102 FIMC_BUG(!fimc_is_dev);
2103
2104 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
2105 if (!core) {
2106 probe_info("core device is not yet probed");
2107 return -EPROBE_DEFER;
2108 }
2109
2110 dev = &client->dev;
2111 dnode = dev->of_node;
2112
2113 sensor_id_spec = of_get_property(dnode, "id", &sensor_id_len);
2114 if (!sensor_id_spec) {
2115 err("sensor_id num read is fail(%d)", ret);
2116 goto p_err;
2117 }
2118
2119 sensor_id_len /= sizeof(*sensor_id_spec);
2120
2121 probe_info("%s sensor_id_spec %d, sensor_id_len %d\n", __func__,
2122 *sensor_id_spec, sensor_id_len);
2123
2124 ret = of_property_read_u32_array(dnode, "id", sensor_id, sensor_id_len);
2125 if (ret) {
2126 err("sensor_id read is fail(%d)", ret);
2127 goto p_err;
2128 }
2129
2130 for (i = 0; i < sensor_id_len; i++) {
2131 probe_info("%s sensor_id %d\n", __func__, sensor_id[i]);
2132 device = &core->sensor[sensor_id[i]];
2133
2134 sensor_peri = find_peri_by_cis_id(device, SENSOR_NAME_IMX333);
2135 if (!sensor_peri) {
2136 probe_info("sensor peri is net yet probed");
2137 return -EPROBE_DEFER;
2138 }
2139 }
2140
2141 for (i = 0; i < sensor_id_len; i++) {
2142 device = &core->sensor[sensor_id[i]];
2143 sensor_peri = find_peri_by_cis_id(device, SENSOR_NAME_IMX333);
2144
2145 cis = &sensor_peri->cis;
2146 subdev_cis = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
2147 if (!subdev_cis) {
2148 probe_err("subdev_cis is NULL");
2149 ret = -ENOMEM;
2150 goto p_err;
2151 }
2152
2153 sensor_peri->subdev_cis = subdev_cis;
2154
2155 cis->id = SENSOR_NAME_IMX333;
2156 cis->subdev = subdev_cis;
2157 cis->device = sensor_id[i];
2158 cis->client = client;
2159 sensor_peri->module->client = cis->client;
2160 cis->i2c_lock = NULL;
2161 cis->ctrl_delay = N_PLUS_TWO_FRAME;
2162
2163 cis->cis_data = kzalloc(sizeof(cis_shared_data), GFP_KERNEL);
2164 if (!cis->cis_data) {
2165 err("cis_data is NULL");
2166 ret = -ENOMEM;
2167 goto p_err;
2168 }
2169
2170 cis->cis_ops = &cis_ops_imx333;
2171
2172 /* belows are depend on sensor cis. MUST check sensor spec */
2173 cis->bayer_order = OTF_INPUT_ORDER_BAYER_GR_BG;
2174
2175 if (of_property_read_bool(dnode, "sensor_f_number")) {
2176 ret = of_property_read_u32(dnode, "sensor_f_number", &cis->aperture_num);
2177 if (ret) {
2178 warn("f-number read is fail(%d)",ret);
2179 }
2180 } else {
2181 cis->aperture_num = F2_2;
2182 }
2183
2184 probe_info("%s f-number %d\n", __func__, cis->aperture_num);
2185
2186 cis->use_dgain = true;
2187 cis->hdr_ctrl_by_again = false;
2188
2189 v4l2_set_subdevdata(subdev_cis, cis);
2190 v4l2_set_subdev_hostdata(subdev_cis, device);
2191 snprintf(subdev_cis->name, V4L2_SUBDEV_NAME_SIZE, "cis-subdev.%d", cis->id);
2192 }
2193
2194 ret = of_property_read_string(dnode, "setfile", &setfile);
2195 if (ret) {
2196 err("setfile index read fail(%d), take default setfile!!", ret);
2197 setfile = "default";
2198 }
2199
2200 probe_info("%s done\n", __func__);
2201
2202 p_err:
2203 return ret;
2204 }
2205
2206 static const struct of_device_id sensor_cis_imx333_match[] = {
2207 {
2208 .compatible = "samsung,exynos5-fimc-is-cis-imx333",
2209 },
2210 {},
2211 };
2212 MODULE_DEVICE_TABLE(of, sensor_cis_imx333_match);
2213
2214 static const struct i2c_device_id sensor_cis_imx333_idt[] = {
2215 { SENSOR_NAME, 0 },
2216 {},
2217 };
2218
2219 static struct i2c_driver sensor_cis_imx333_driver = {
2220 .probe = cis_imx333_probe,
2221 .driver = {
2222 .name = SENSOR_NAME,
2223 .owner = THIS_MODULE,
2224 .of_match_table = sensor_cis_imx333_match,
2225 .suppress_bind_attrs = true,
2226 },
2227 .id_table = sensor_cis_imx333_idt
2228 };
2229
2230 static int __init sensor_cis_imx333_init(void)
2231 {
2232 int ret;
2233
2234 ret = i2c_add_driver(&sensor_cis_imx333_driver);
2235 if (ret)
2236 err("failed to add %s driver: %d\n",
2237 sensor_cis_imx333_driver.driver.name, ret);
2238
2239 return ret;
2240 }
2241 late_initcall_sync(sensor_cis_imx333_init);