1 /*****************************************************************************
13 * Source code of Sensor driver
16 *------------------------------------------------------------------------------
17 * Upper this line, this part is controlled by CC/CQ. DO NOT MODIFY!!
18 *============================================================================
19 ****************************************************************************/
21 #include <linux/videodev2.h>
22 #include <linux/i2c.h>
23 #include <linux/platform_device.h>
24 #include <linux/delay.h>
25 #include <linux/cdev.h>
26 #include <linux/uaccess.h>
28 #include <asm/atomic.h>
29 #include <asm/system.h>
30 #include <linux/xlog.h>
32 #include "kd_camera_hw.h"
33 #include "kd_imgsensor.h"
34 #include "kd_imgsensor_define.h"
35 #include "kd_imgsensor_errcode.h"
37 #include "ov5670mipi_Sensor.h"
39 #define PFX "ov5670_camera_sensor"
40 //#define LOG_WRN(format, args...) xlog_printk(ANDROID_LOG_WARN ,PFX, "[%S] " format, __FUNCTION__, ##args)
41 //#defineLOG_INF(format, args...) xlog_printk(ANDROID_LOG_INFO ,PFX, "[%s] " format, __FUNCTION__, ##args)
42 //#define LOG_DBG(format, args...) xlog_printk(ANDROID_LOG_DEBUG ,PFX, "[%S] " format, __FUNCTION__, ##args)
43 #define LOG_INF(format, args...) xlog_printk(ANDROID_LOG_INFO , PFX, "[%s] " format, __FUNCTION__, ##args)
45 static DEFINE_SPINLOCK(imgsensor_drv_lock
);
48 static imgsensor_info_struct imgsensor_info
= {
49 .sensor_id
= OV5670_SENSOR_ID
, //record sensor id defined in Kd_imgsensor.h
51 .checksum_value
= 0x523c51f6, //checksum value for Camera Auto Test
54 .pclk
= 102850000, //record different mode's pclk
55 .linelength
= 1676, //record different mode's linelength
56 .framelength
= 2045, //record different mode's framelength
57 .startx
= 1, //record different mode's startx of grabwindow
58 .starty
= 0, //record different mode's starty of grabwindow
59 .grabwindow_width
= 1280, //record different mode's width of grabwindow
60 .grabwindow_height
= 960, //record different mode's height of grabwindow
61 /* following for MIPIDataLowPwr2HighSpeedSettleDelayCount by different scenario */
62 .mipi_data_lp2hs_settle_dc
= 14,//unit , ns
63 /* following for GetDefaultFramerateByScenario() */
72 .grabwindow_width
= 2560,
73 .grabwindow_height
= 1920,
74 .mipi_data_lp2hs_settle_dc
= 14,//unit , ns
83 .grabwindow_width
= 1280,
84 .grabwindow_height
= 960,
85 .mipi_data_lp2hs_settle_dc
= 14,//unit , ns
88 .margin
= 4, //sensor framelength & shutter margin
89 .min_shutter
= 4, //min shutter
90 .max_frame_length
= 0x7fff,//max framelength by sensor register's limitation
91 .ae_shut_delay_frame
= 0, //shutter delay frame for AE cycle, 2 frame with ispGain_delay-shut_delay=2-0=2
92 .ae_sensor_gain_delay_frame
= 0,//sensor gain delay frame for AE cycle,2 frame with ispGain_delay-sensor_gain_delay=2-0=2
93 .ae_ispGain_delay_frame
= 2,//isp gain delay frame for AE cycle
94 .sensor_mode_num
= 5, //support sensor mode num
96 .cap_delay_frame
= 3, //enter capture delay frame num
97 .pre_delay_frame
= 3, //enter preview delay frame num
98 .video_delay_frame
= 3, //enter video delay frame num
99 .isp_driving_current
= ISP_DRIVING_8MA
, //mclk driving current
100 .sensor_interface_type
= SENSOR_INTERFACE_TYPE_MIPI
,//sensor_interface_type
101 //.mipi_sensor_type = MIPI_OPHY_NCSI2; //0,MIPI_OPHY_NCSI2; 1,MIPI_OPHY_CSI2
102 //.mipi_settle_delay_mode = MIPI_SETTLEDELAY_AUTO; //0,MIPI_SETTLEDELAY_AUTO; 1,MIPI_SETTLEDELAY_MANNUAL
103 .sensor_output_dataformat
= SENSOR_OUTPUT_FORMAT_RAW_Gb
,//sensor output first pixel color
104 .mclk
= 24,//mclk value, suggest 24 or 26 for 24Mhz or 26Mhz
105 .mipi_lane_num
= SENSOR_MIPI_2_LANE
,//mipi lane num
106 .i2c_addr_table
= {0x6c, 0x20, 0xff},//record sensor support all write id addr, only supprt 4must end with 0xff
110 static imgsensor_struct imgsensor
= {
111 .mirror
= IMAGE_NORMAL
, //mirrorflip information
112 .sensor_mode
= IMGSENSOR_MODE_INIT
, //IMGSENSOR_MODE enum value,record current sensor mode,such as: INIT, Preview, Capture, Video,High Speed Video, Slim Video
113 .shutter
= 0x4C00, //current shutter
114 .gain
= 0x0200, //current gain
115 .dummy_pixel
= 0, //current dummypixel
116 .dummy_line
= 0, //current dummyline
117 .current_fps
= 0, //full size current fps : 24fps for PIP, 30fps for Normal or ZSD
118 .autoflicker_en
= KAL_FALSE
, //auto flicker enable: KAL_FALSE for disable auto flicker, KAL_TRUE for enable auto flicker
119 .test_pattern
= KAL_FALSE
, //test pattern mode or not. KAL_FALSE for in test pattern mode, KAL_TRUE for normal output
120 .current_scenario_id
= MSDK_SCENARIO_ID_CAMERA_PREVIEW
,//current scenario id
121 .i2c_write_id
= 0x6c,//record current sensor's i2c write id
125 /* Sensor output window information */
127 static kal_uint16
read_cmos_sensor(kal_uint32 addr
)
129 kal_uint16 get_byte
=0;
131 char pu_send_cmd
[2] = {(char)(addr
>> 8), (char)(addr
& 0xFF) };
132 iReadRegI2C(pu_send_cmd
, 2, (u8
*)&get_byte
, 1, imgsensor
.i2c_write_id
);
137 static void write_cmos_sensor(kal_uint32 addr
, kal_uint32 para
)
139 char pu_send_cmd
[3] = {(char)(addr
>> 8), (char)(addr
& 0xFF), (char)(para
& 0xFF)};
140 iWriteRegI2C(pu_send_cmd
, 3, imgsensor
.i2c_write_id
);
143 static void set_dummy()
145 LOG_INF("dummyline = %d, dummypixels = %d \n", imgsensor
.dummy_line
, imgsensor
.dummy_pixel
);
146 /* you can set dummy by imgsensor.dummy_line and imgsensor.dummy_pixel, or you can set dummy by imgsensor.frame_length and imgsensor.line_length */
147 write_cmos_sensor(0x380e, (imgsensor
.frame_length
>> 8) & 0xFF);
148 write_cmos_sensor(0x380f, imgsensor
.frame_length
& 0xFF);
149 write_cmos_sensor(0x380c, (imgsensor
.line_length
>> 8) & 0xFF);
150 write_cmos_sensor(0x380d, imgsensor
.line_length
& 0xFF);
155 static void set_max_framerate(UINT16 framerate
,kal_bool min_framelength_en
)
157 kal_int16 dummy_line
;
158 kal_uint32 frame_length
= imgsensor
.frame_length
;
159 //unsigned long flags;
161 LOG_INF("framerate = %d, min framelength should enable? \n", framerate
,min_framelength_en
);
163 frame_length
= imgsensor
.pclk
/ framerate
* 10 / imgsensor
.line_length
;
164 spin_lock(&imgsensor_drv_lock
);
165 imgsensor
.frame_length
= (frame_length
> imgsensor
.min_frame_length
) ? frame_length
: imgsensor
.min_frame_length
;
166 imgsensor
.dummy_line
= imgsensor
.frame_length
- imgsensor
.min_frame_length
;
167 //dummy_line = frame_length - imgsensor.min_frame_length;
168 //if (dummy_line < 0)
169 //imgsensor.dummy_line = 0;
171 //imgsensor.dummy_line = dummy_line;
172 //imgsensor.frame_length = frame_length + imgsensor.dummy_line;
173 if (imgsensor
.frame_length
> imgsensor_info
.max_frame_length
)
175 imgsensor
.frame_length
= imgsensor_info
.max_frame_length
;
176 imgsensor
.dummy_line
= imgsensor
.frame_length
- imgsensor
.min_frame_length
;
178 if (min_framelength_en
)
179 imgsensor
.min_frame_length
= imgsensor
.frame_length
;
180 spin_unlock(&imgsensor_drv_lock
);
182 } /* set_max_framerate */
185 static void write_shutter(kal_uint16 shutter
)
188 kal_uint16 realtime_fps
= 0;
189 kal_uint32 frame_length
= 0;
190 spin_lock_irqsave(&imgsensor_drv_lock
, flags
);
191 imgsensor
.shutter
= shutter
;
192 spin_unlock_irqrestore(&imgsensor_drv_lock
, flags
);
194 /* 0x3500, 0x3501, 0x3502 will increase VBLANK to get exposure larger than frame exposure */
195 /* AE doesn't update sensor gain at capture mode, thus extra exposure lines must be updated here. */
197 // OV Recommend Solution
198 // if shutter bigger than frame_length, should extend frame length first
199 spin_lock(&imgsensor_drv_lock
);
200 if (shutter
> imgsensor
.min_frame_length
- imgsensor_info
.margin
)
201 imgsensor
.frame_length
= shutter
+ imgsensor_info
.margin
;
203 imgsensor
.frame_length
= imgsensor
.min_frame_length
;
204 if (imgsensor
.frame_length
> imgsensor_info
.max_frame_length
)
205 imgsensor
.frame_length
= imgsensor_info
.max_frame_length
;
206 imgsensor
.frame_length
= (imgsensor
.frame_length
>>1)<<1;
207 spin_unlock(&imgsensor_drv_lock
);
208 shutter
= (shutter
< imgsensor_info
.min_shutter
) ? imgsensor_info
.min_shutter
: shutter
;
209 shutter
= (shutter
> (imgsensor_info
.max_frame_length
- imgsensor_info
.margin
)) ? (imgsensor_info
.max_frame_length
- imgsensor_info
.margin
) : shutter
;
210 shutter
= (shutter
>> 1) << 1;
212 if (imgsensor
.autoflicker_en
) {
213 realtime_fps
= imgsensor
.pclk
/ imgsensor
.line_length
* 10 / imgsensor
.frame_length
;
214 if(realtime_fps
>= 297 && realtime_fps
<= 305)
215 set_max_framerate(296,0);
216 else if(realtime_fps
>= 147 && realtime_fps
<= 150)
217 set_max_framerate(146,0);
219 // Extend frame length
220 write_cmos_sensor(0x380e, imgsensor
.frame_length
>> 8);
221 write_cmos_sensor(0x380f, imgsensor
.frame_length
& 0xFF);
224 // Extend frame length
225 write_cmos_sensor(0x380e, (imgsensor
.frame_length
>> 8)& 0xFF);
226 write_cmos_sensor(0x380f, imgsensor
.frame_length
& 0xFF);
230 write_cmos_sensor(0x3502, (shutter
<< 4) & 0xF0);
231 write_cmos_sensor(0x3501, (shutter
>> 4) & 0xFF);
232 write_cmos_sensor(0x3500, (shutter
>> 12) & 0x0F);
233 LOG_INF("Exit! shutter =%d, framelength =%d\n", shutter
,imgsensor
.frame_length
);
235 //LOG_INF("frame_length = %d ", frame_length);
237 } /* write_shutter */
241 /*************************************************************************
246 * This function set e-shutter of sensor to change exposure time.
249 * iShutter : exposured lines
256 *************************************************************************/
257 static void set_shutter(kal_uint16 shutter
)
260 spin_lock_irqsave(&imgsensor_drv_lock
, flags
);
261 imgsensor
.shutter
= shutter
;
262 spin_unlock_irqrestore(&imgsensor_drv_lock
, flags
);
264 write_shutter(shutter
);
269 static kal_uint16
gain2reg(const kal_uint16 gain
)
271 kal_uint16 reg_gain
= 0x0000;
273 reg_gain
= ((gain
* 128)/BASEGAIN
);
274 //reg_gain = ((gain /BASEGAIN)<<7)+((gain % BASEGAIN)<<7/BASEGAIN);
275 reg_gain
= reg_gain
& 0xFFFF;
276 return (kal_uint16
)reg_gain
;
279 /*************************************************************************
284 * This function is to set global gain to sensor.
287 * iGain : sensor global gain(base: 0x40)
290 * the actually gain set to sensor.
294 *************************************************************************/
295 static kal_uint16
set_gain(kal_uint16 gain
)
299 kal_uint8 ChangeFlag
=0x07;
302 /* 0x350A[0:1], 0x350B[0:7] AGC real gain */
303 /* [0:3] = N meams N /16 X */
304 /* [4:9] = M meams M X */
305 /* Total gain = M + N /16 X */
309 if (gain
< BASEGAIN
|| gain
> 8 * BASEGAIN
) {
310 LOG_INF("Error gain setting");
314 else if (gain
> 8 * BASEGAIN
)
318 iGain
= gain
/BASEGAIN
;
333 reg_gain
= gain2reg(gain
);
334 spin_lock(&imgsensor_drv_lock
);
335 imgsensor
.gain
= reg_gain
;
336 spin_unlock(&imgsensor_drv_lock
);
337 LOG_INF("gain = %d ,reg[0x366a]= %d, reg_gain = 0x%x\n ", gain
, ChangeFlag
, reg_gain
);
339 write_cmos_sensor(0x301d, 0xf0);
340 write_cmos_sensor(0x3209, 0x00);
341 write_cmos_sensor(0x320a, 0x01);
344 //group 0:delay 0x366a for one frame
345 write_cmos_sensor(0x3208, 0x00);
346 write_cmos_sensor(0x366a, ChangeFlag
);
347 write_cmos_sensor(0x3208, 0x10);
349 //group 1:all other registers( gain)
350 write_cmos_sensor(0x3208, 0x01);
351 write_cmos_sensor(0x3508, reg_gain
>> 8);
352 write_cmos_sensor(0x3509, reg_gain
& 0xFF);
354 write_cmos_sensor(0x3208, 0x11);
357 write_cmos_sensor(0x320B, 0x15);
358 write_cmos_sensor(0x3208, 0xA1);
367 static void set_mirror_flip(kal_uint8 image_mirror
)
369 LOG_INF("image_mirror = %d\n", image_mirror
);
371 /********************************************************
373 * 0x3820[2] ISP Vertical flip
374 * 0x3820[1] Sensor Vertical flip
376 * 0x3821[2] ISP Horizontal mirror
377 * 0x3821[1] Sensor Horizontal mirror
379 * ISP and Sensor flip or mirror register bit should be the same!!
381 ********************************************************/
383 switch (image_mirror
) {
385 write_cmos_sensor(0x3820,((read_cmos_sensor(0x3820) & 0xF9) | 0x00));
386 write_cmos_sensor(0x3821,((read_cmos_sensor(0x3821) & 0xF9) | 0x06));
389 write_cmos_sensor(0x3820,((read_cmos_sensor(0x3820) & 0xF9) | 0x00));
390 write_cmos_sensor(0x3821,((read_cmos_sensor(0x3821) & 0xF9) | 0x00));
393 write_cmos_sensor(0x3820,((read_cmos_sensor(0x3820) & 0xF9) | 0x06));
394 write_cmos_sensor(0x3821,((read_cmos_sensor(0x3821) & 0xF9) | 0x06));
396 case IMAGE_HV_MIRROR
:
397 write_cmos_sensor(0x3820,((read_cmos_sensor(0x3820) & 0xF9) | 0x06));
398 write_cmos_sensor(0x3821,((read_cmos_sensor(0x3821) & 0xF9) | 0x00));
401 LOG_INF("Error image_mirror setting\n");
406 /*************************************************************************
411 * This function night mode of sensor.
414 * bEnable: KAL_TRUE -> enable night mode, otherwise, disable night mode
421 *************************************************************************/
422 static void night_mode(kal_bool enable
)
424 /*No Need to implement this function*/
427 static void sensor_init(void)
429 LOG_INF("OV5670_Sensor_Init_2lane E\n");
431 write_cmos_sensor(0x0103,0x01);// ; software reset
433 write_cmos_sensor(0x0100, 0x00);// ; software standby
434 write_cmos_sensor(0x0300, 0x04);
435 write_cmos_sensor(0x0301, 0x00);
436 write_cmos_sensor(0x0302, 0x69); //;78 MIPI datarate 960 -> 840
437 write_cmos_sensor(0x0303, 0x00);
438 write_cmos_sensor(0x0304, 0x03);
439 write_cmos_sensor(0x0305, 0x01);
440 write_cmos_sensor(0x0306, 0x01);
441 write_cmos_sensor(0x030a, 0x00);
442 write_cmos_sensor(0x030b, 0x00);
443 write_cmos_sensor(0x030c, 0x00);
444 write_cmos_sensor(0x030d, 0x1e);
445 write_cmos_sensor(0x030e, 0x00);
446 write_cmos_sensor(0x030f, 0x06);
447 write_cmos_sensor(0x0312, 0x01);
448 write_cmos_sensor(0x3000, 0x00);
449 write_cmos_sensor(0x3002, 0x61);
450 write_cmos_sensor(0x3005, 0xf0);
451 write_cmos_sensor(0x3007, 0x00);
452 write_cmos_sensor(0x300d, 0x00);
453 write_cmos_sensor(0x3010, 0x40);
454 write_cmos_sensor(0x3015, 0x0f);
455 write_cmos_sensor(0x3018, 0x32);
456 write_cmos_sensor(0x301a, 0xf0);
457 write_cmos_sensor(0x301b, 0xf0);
458 write_cmos_sensor(0x301c, 0xf0);
459 write_cmos_sensor(0x301d, 0xf0);
460 write_cmos_sensor(0x301e, 0xf0);
461 write_cmos_sensor(0x3030, 0x00);
462 write_cmos_sensor(0x3031, 0x0a);
463 write_cmos_sensor(0x303c, 0xff);
464 write_cmos_sensor(0x303e, 0xff);
465 write_cmos_sensor(0x3040, 0xf0);
466 write_cmos_sensor(0x3041, 0x00);
467 write_cmos_sensor(0x3042, 0xf0);
468 write_cmos_sensor(0x3106, 0x11);
469 write_cmos_sensor(0x3500, 0x00);
470 write_cmos_sensor(0x3501, 0x7b);
471 write_cmos_sensor(0x3502, 0x00);
472 write_cmos_sensor(0x3503, 0x04);
473 write_cmos_sensor(0x3504, 0x03);
474 write_cmos_sensor(0x3505, 0x83);
475 write_cmos_sensor(0x3508, 0x07);
476 write_cmos_sensor(0x3509, 0x80);
477 write_cmos_sensor(0x350e, 0x04);
478 write_cmos_sensor(0x350f, 0x00);
479 write_cmos_sensor(0x3510, 0x00);
480 write_cmos_sensor(0x3511, 0x02);
481 write_cmos_sensor(0x3512, 0x00);
482 write_cmos_sensor(0x3601, 0xc8);
483 write_cmos_sensor(0x3610, 0x88);
484 write_cmos_sensor(0x3612, 0x48);
485 write_cmos_sensor(0x3614, 0x5b);
486 write_cmos_sensor(0x3615, 0x96);
487 write_cmos_sensor(0x3621, 0xd0);
488 write_cmos_sensor(0x3622, 0x00);
489 write_cmos_sensor(0x3623, 0x00);
490 write_cmos_sensor(0x3633, 0x13);
491 write_cmos_sensor(0x3634, 0x13);
492 write_cmos_sensor(0x3635, 0x13);
493 write_cmos_sensor(0x3636, 0x13);
494 write_cmos_sensor(0x3645, 0x13);
495 write_cmos_sensor(0x3646, 0x82);
496 write_cmos_sensor(0x3650, 0x00);
497 write_cmos_sensor(0x3652, 0xff);
498 write_cmos_sensor(0x3655, 0x20);
499 write_cmos_sensor(0x3656, 0xff);
500 write_cmos_sensor(0x365a, 0xff);
501 write_cmos_sensor(0x365e, 0xff);
502 write_cmos_sensor(0x3668, 0x00);
503 write_cmos_sensor(0x366a, 0x07);
504 write_cmos_sensor(0x366e, 0x10);
505 write_cmos_sensor(0x366d, 0x00);
506 write_cmos_sensor(0x366f, 0x80);
507 write_cmos_sensor(0x3700, 0x28);
508 write_cmos_sensor(0x3701, 0x10);
509 write_cmos_sensor(0x3702, 0x3a);
510 write_cmos_sensor(0x3703, 0x19);
511 write_cmos_sensor(0x3704, 0x10);
512 write_cmos_sensor(0x3705, 0x00);
513 write_cmos_sensor(0x3706, 0x66);
514 write_cmos_sensor(0x3707, 0x08);
515 write_cmos_sensor(0x3708, 0x34);
516 write_cmos_sensor(0x3709, 0x40);
517 write_cmos_sensor(0x370a, 0x01);
518 write_cmos_sensor(0x370b, 0x1b);
519 write_cmos_sensor(0x3714, 0x24);
520 write_cmos_sensor(0x371a, 0x3e);
521 write_cmos_sensor(0x3733, 0x00);
522 write_cmos_sensor(0x3734, 0x00);
523 write_cmos_sensor(0x373a, 0x05);
524 write_cmos_sensor(0x373b, 0x06);
525 write_cmos_sensor(0x373c, 0x0a);
526 write_cmos_sensor(0x373f, 0xa0);
527 write_cmos_sensor(0x3755, 0x00);
528 write_cmos_sensor(0x3758, 0x00);
529 write_cmos_sensor(0x375b, 0x0e);
530 write_cmos_sensor(0x3766, 0x5f);
531 write_cmos_sensor(0x3768, 0x00);
532 write_cmos_sensor(0x3769, 0x22);
533 write_cmos_sensor(0x3773, 0x08);
534 write_cmos_sensor(0x3774, 0x1f);
535 write_cmos_sensor(0x3776, 0x06);
536 write_cmos_sensor(0x37a0, 0x88);
537 write_cmos_sensor(0x37a1, 0x5c);
538 write_cmos_sensor(0x37a7, 0x88);
539 write_cmos_sensor(0x37a8, 0x70);
540 write_cmos_sensor(0x37aa, 0x88);
541 write_cmos_sensor(0x37ab, 0x48);
542 write_cmos_sensor(0x37b3, 0x66);
543 write_cmos_sensor(0x37c2, 0x04);
544 write_cmos_sensor(0x37c5, 0x00);
545 write_cmos_sensor(0x37c8, 0x00);
546 write_cmos_sensor(0x3800, 0x00);
547 write_cmos_sensor(0x3801, 0x0c);
548 write_cmos_sensor(0x3802, 0x00);
549 write_cmos_sensor(0x3803, 0x04);
550 write_cmos_sensor(0x3804, 0x0a);
551 write_cmos_sensor(0x3805, 0x33);
552 write_cmos_sensor(0x3806, 0x07);
553 write_cmos_sensor(0x3807, 0xa3);
554 write_cmos_sensor(0x3808, 0x0a);
555 write_cmos_sensor(0x3809, 0x20);
556 write_cmos_sensor(0x380a, 0x07);
557 write_cmos_sensor(0x380b, 0x98);
558 write_cmos_sensor(0x380c, 0x06); // ;06
559 write_cmos_sensor(0x380d, 0x8c); // ;8c
560 write_cmos_sensor(0x380e, 0x07);
561 write_cmos_sensor(0x380f, 0xb8);
562 write_cmos_sensor(0x3811, 0x04);
563 write_cmos_sensor(0x3813, 0x02);
564 write_cmos_sensor(0x3814, 0x01);
565 write_cmos_sensor(0x3815, 0x01);
566 write_cmos_sensor(0x3816, 0x00);
567 write_cmos_sensor(0x3817, 0x00);
568 write_cmos_sensor(0x3818, 0x00);
569 write_cmos_sensor(0x3819, 0x00);
570 write_cmos_sensor(0x3820, 0x80);
571 write_cmos_sensor(0x3821, 0x46);
572 write_cmos_sensor(0x3822, 0x48);
573 write_cmos_sensor(0x3826, 0x00);
574 write_cmos_sensor(0x3827, 0x08);
575 write_cmos_sensor(0x382a, 0x01);
576 write_cmos_sensor(0x382b, 0x01);
577 write_cmos_sensor(0x3830, 0x08);
578 write_cmos_sensor(0x3836, 0x02);
579 write_cmos_sensor(0x3837, 0x00);
580 write_cmos_sensor(0x3838, 0x10);
581 write_cmos_sensor(0x3841, 0xff);
582 write_cmos_sensor(0x3846, 0x48);
583 write_cmos_sensor(0x3861, 0x00);
584 write_cmos_sensor(0x3862, 0x04);
585 write_cmos_sensor(0x3863, 0x06);
586 write_cmos_sensor(0x3a11, 0x01);
587 write_cmos_sensor(0x3a12, 0x78);
588 write_cmos_sensor(0x3b00, 0x00);
589 write_cmos_sensor(0x3b02, 0x00);
590 write_cmos_sensor(0x3b03, 0x00);
591 write_cmos_sensor(0x3b04, 0x00);
592 write_cmos_sensor(0x3b05, 0x00);
593 write_cmos_sensor(0x3c00, 0x89);
594 write_cmos_sensor(0x3c01, 0xab);
595 write_cmos_sensor(0x3c02, 0x01);
596 write_cmos_sensor(0x3c03, 0x00);
597 write_cmos_sensor(0x3c04, 0x00);
598 write_cmos_sensor(0x3c05, 0x03);
599 write_cmos_sensor(0x3c06, 0x00);
600 write_cmos_sensor(0x3c07, 0x05);
601 write_cmos_sensor(0x3c0c, 0x00);
602 write_cmos_sensor(0x3c0d, 0x00);
603 write_cmos_sensor(0x3c0e, 0x00);
604 write_cmos_sensor(0x3c0f, 0x00);
605 write_cmos_sensor(0x3c40, 0x00);
606 write_cmos_sensor(0x3c41, 0xa3);
607 write_cmos_sensor(0x3c43, 0x7d);
608 write_cmos_sensor(0x3c45, 0xd7);
609 write_cmos_sensor(0x3c47, 0xfc);
610 write_cmos_sensor(0x3c50, 0x05);
611 write_cmos_sensor(0x3c52, 0xaa);
612 write_cmos_sensor(0x3c54, 0x71);
613 write_cmos_sensor(0x3c56, 0x80);
614 write_cmos_sensor(0x3d85, 0x17);
615 write_cmos_sensor(0x3f03, 0x00);
616 write_cmos_sensor(0x3f0a, 0x00);
617 write_cmos_sensor(0x3f0b, 0x00);
618 write_cmos_sensor(0x4001, 0x60);
619 write_cmos_sensor(0x4009, 0x0d);
620 write_cmos_sensor(0x4020, 0x00);
621 write_cmos_sensor(0x4021, 0x00);
622 write_cmos_sensor(0x4022, 0x00);
623 write_cmos_sensor(0x4023, 0x00);
624 write_cmos_sensor(0x4024, 0x00);
625 write_cmos_sensor(0x4025, 0x00);
626 write_cmos_sensor(0x4026, 0x00);
627 write_cmos_sensor(0x4027, 0x00);
628 write_cmos_sensor(0x4028, 0x00);
629 write_cmos_sensor(0x4029, 0x00);
630 write_cmos_sensor(0x402a, 0x00);
631 write_cmos_sensor(0x402b, 0x00);
632 write_cmos_sensor(0x402c, 0x00);
633 write_cmos_sensor(0x402d, 0x00);
634 write_cmos_sensor(0x402e, 0x00);
635 write_cmos_sensor(0x402f, 0x00);
636 write_cmos_sensor(0x4040, 0x00);
637 write_cmos_sensor(0x4041, 0x03);
638 write_cmos_sensor(0x4042, 0x00);
639 write_cmos_sensor(0x4043, 0x7a);
640 write_cmos_sensor(0x4044, 0x00);
641 write_cmos_sensor(0x4045, 0x7a);
642 write_cmos_sensor(0x4046, 0x00);
643 write_cmos_sensor(0x4047, 0x7a);
644 write_cmos_sensor(0x4048, 0x00);
645 write_cmos_sensor(0x4049, 0x7a);
646 write_cmos_sensor(0x4303, 0x00);
647 write_cmos_sensor(0x4307, 0x30);
648 write_cmos_sensor(0x4500, 0x58);
649 write_cmos_sensor(0x4501, 0x04);
650 write_cmos_sensor(0x4502, 0x40);
651 write_cmos_sensor(0x4503, 0x10);
652 write_cmos_sensor(0x4508, 0xaa);
653 write_cmos_sensor(0x4509, 0xaa);
654 write_cmos_sensor(0x450a, 0x00);
655 write_cmos_sensor(0x450b, 0x00);
656 write_cmos_sensor(0x4600, 0x01);
657 write_cmos_sensor(0x4601, 0x03);
658 write_cmos_sensor(0x4700, 0xa4);
659 write_cmos_sensor(0x4800, 0x6c);
660 write_cmos_sensor(0x4816, 0x53);
661 write_cmos_sensor(0x481f, 0x40);
662 write_cmos_sensor(0x4837, 0x13); // ;11 mipi global timing datarate 940 -> 840
663 write_cmos_sensor(0x5000, 0x56);
664 write_cmos_sensor(0x5001, 0x01);
665 write_cmos_sensor(0x5002, 0x28);
666 write_cmos_sensor(0x5004, 0x0c);
667 write_cmos_sensor(0x5006, 0x0c);
668 write_cmos_sensor(0x5007, 0xe0);
669 write_cmos_sensor(0x5008, 0x01);
670 write_cmos_sensor(0x5009, 0xb0);
671 write_cmos_sensor(0x5901, 0x00);
672 write_cmos_sensor(0x5a01, 0x00);
673 write_cmos_sensor(0x5a03, 0x00);
674 write_cmos_sensor(0x5a04, 0x0c);
675 write_cmos_sensor(0x5a05, 0xe0);
676 write_cmos_sensor(0x5a06, 0x09);
677 write_cmos_sensor(0x5a07, 0xb0);
678 write_cmos_sensor(0x5a08, 0x06);
679 write_cmos_sensor(0x5e00, 0x00);
680 write_cmos_sensor(0x3618, 0x2a);
683 write_cmos_sensor(0x3734, 0x40); // ;; Improve HFPN
684 write_cmos_sensor(0x5b00, 0x01); // ;; [2:0] otp start addr[10:8]
685 write_cmos_sensor(0x5b01, 0x10); // ;; [7:0] otp start addr[7:0]
686 write_cmos_sensor(0x5b02, 0x01); // ;; [2:0] otp end addr[10:8]
687 write_cmos_sensor(0x5b03, 0xDB); // ;; [7:0] otp end addr[7:0]
688 write_cmos_sensor(0x3d8c, 0x71); //; Header address high byte
689 write_cmos_sensor(0x3d8d, 0xEA); //; Header address low byte
690 write_cmos_sensor(0x4017, 0x08); // ; threshold= 2LSB for full size
693 write_cmos_sensor(0x5780, 0x3e);
694 write_cmos_sensor(0x5781, 0x0f);
695 write_cmos_sensor(0x5782, 0x44);
696 write_cmos_sensor(0x5783, 0x02);
697 write_cmos_sensor(0x5784, 0x01);
698 write_cmos_sensor(0x5785, 0x00);
699 write_cmos_sensor(0x5786, 0x00);
700 write_cmos_sensor(0x5787, 0x04);
701 write_cmos_sensor(0x5788, 0x02);
702 write_cmos_sensor(0x5789, 0x0f);
703 write_cmos_sensor(0x578a, 0xfd);
704 write_cmos_sensor(0x578b, 0xf5);
705 write_cmos_sensor(0x578c, 0xf5);
706 write_cmos_sensor(0x578d, 0x03);
707 write_cmos_sensor(0x578e, 0x08);
708 write_cmos_sensor(0x578f, 0x0c);
709 write_cmos_sensor(0x5790, 0x08);
710 write_cmos_sensor(0x5791, 0x04);
711 write_cmos_sensor(0x5792, 0x00);
712 write_cmos_sensor(0x5793, 0x52);
713 write_cmos_sensor(0x5794, 0xa3);
715 write_cmos_sensor(0x380e, 0x07); //; fps fine adjustment
716 write_cmos_sensor(0x380f, 0xfd); //; fps fine adjustment
717 write_cmos_sensor(0x3503, 0x00); //; real gain [2] gain no delay, shutter no delay
720 write_cmos_sensor(0x0100, 0x00); //;01
726 static void preview_setting(void)
728 LOG_INF(" OV5670PreviewSetting_2lane enter\n");
730 //@@PV_Quarter_size_30fps_800Mbps/lane_1296x972
733 //;;PCLK=HTS*VTS*fps=0x68c*0x7fd*30=1676*2045*30=102.85M
735 write_cmos_sensor(0x0100, 0x00); //
737 write_cmos_sensor(0x3501, 0x73); //
738 write_cmos_sensor(0x3502, 0x00); //
739 write_cmos_sensor(0x3508, 0x03); //
740 write_cmos_sensor(0x3509, 0x00); //
741 write_cmos_sensor(0x3623, 0x00);
742 write_cmos_sensor(0x366e, 0x08); //
743 write_cmos_sensor(0x370b, 0x1b); //
744 write_cmos_sensor(0x3808, 0x05); //
745 write_cmos_sensor(0x3809, 0x10); //
746 write_cmos_sensor(0x380a, 0x03); //
747 write_cmos_sensor(0x380b, 0xcc); //;c0
748 write_cmos_sensor(0x380c, 0x06); //
749 write_cmos_sensor(0x380d, 0x8c); //
750 write_cmos_sensor(0x380e, 0x07); //;03
751 write_cmos_sensor(0x380f, 0xfd); //;e0
755 write_cmos_sensor(0x3814, 0x03); //
756 write_cmos_sensor(0x3820, 0x90); //
757 write_cmos_sensor(0x3821, 0x47); //
758 write_cmos_sensor(0x382a, 0x03); //
761 write_cmos_sensor(0x3845, 0x02); //
762 write_cmos_sensor(0x4009, 0x05); //
763 write_cmos_sensor(0x4502, 0x44); //
764 write_cmos_sensor(0x4508, 0x55); //
765 write_cmos_sensor(0x4509, 0x55); //
766 write_cmos_sensor(0x450a, 0x00); //
767 write_cmos_sensor(0x4600, 0x00); //
768 write_cmos_sensor(0x4601, 0x81); //
769 write_cmos_sensor(0x4017, 0x10); //; threshold = 4LSB for Binning
770 write_cmos_sensor(0x400a, 0x02); //;
771 write_cmos_sensor(0x400b, 0x00); //;
773 write_cmos_sensor(0x0100, 0x01); //
776 } /* preview_setting */
778 static void capture_setting(kal_uint16 currefps
)
780 LOG_INF("OV5670CaptureSetting_2lane enter! currefps:%d\n",currefps
);
781 write_cmos_sensor(0x0100, 0x00);
783 write_cmos_sensor(0x3501, 0x5f); //long exposure
784 write_cmos_sensor(0x3502, 0xd0); //long exposure
786 write_cmos_sensor(0x3508, 0x03); //gain
787 write_cmos_sensor(0x3509, 0x00); //gain
789 write_cmos_sensor(0x3623, 0x00); //gain
790 write_cmos_sensor(0x366e, 0x10);
791 write_cmos_sensor(0x370b, 0x1b);
792 write_cmos_sensor(0x3808, 0x0a);
793 write_cmos_sensor(0x3809, 0x20);
794 write_cmos_sensor(0x380a, 0x07);
795 write_cmos_sensor(0x380b, 0x98);
796 write_cmos_sensor(0x380c, 0x06); // hts
797 write_cmos_sensor(0x380d, 0x8c); // hts
798 write_cmos_sensor(0x380e, 0x07); // vts
799 write_cmos_sensor(0x380f, 0xfd); // vts
802 write_cmos_sensor(0x3814, 0x01);
803 write_cmos_sensor(0x3820, 0x80);
804 write_cmos_sensor(0x3821, 0x46);
805 write_cmos_sensor(0x382a, 0x01);
807 write_cmos_sensor(0x3845, 0x00); //v_offset for auto size mode
809 write_cmos_sensor(0x4009, 0x0d);
810 write_cmos_sensor(0x4502, 0x40);
811 write_cmos_sensor(0x4508, 0xaa);
812 write_cmos_sensor(0x4509, 0xaa);
813 write_cmos_sensor(0x450a, 0x00); //
814 write_cmos_sensor(0x4600, 0x01);
815 write_cmos_sensor(0x4601, 0x03);
816 write_cmos_sensor(0x4017, 0x08); //threshold= 2LSB for full size
817 write_cmos_sensor(0x400a, 0x02); //
818 write_cmos_sensor(0x400b, 0x00); //
820 write_cmos_sensor(0x0100, 0x01);
826 static void normal_video_setting(kal_uint16 currefps
)
828 LOG_INF("normal_video_setting Enter! currefps:%d\n",currefps
);
830 //@@PV_Quarter_size_30fps_800Mbps/lane_1296x972
833 //;;PCLK=HTS*VTS*fps=0x68c*0x7fd*30=1676*2045*30=102.85M
835 write_cmos_sensor(0x0100, 0x00); //
837 write_cmos_sensor(0x3501, 0x73); //
838 write_cmos_sensor(0x3502, 0x00); //
839 write_cmos_sensor(0x3508, 0x01); //
840 write_cmos_sensor(0x3509, 0x80); //
841 write_cmos_sensor(0x3623, 0x00); //gain
842 write_cmos_sensor(0x366e, 0x08); //
843 write_cmos_sensor(0x370b, 0x1b); //
844 write_cmos_sensor(0x3808, 0x05); //
845 write_cmos_sensor(0x3809, 0x10); //
846 write_cmos_sensor(0x380a, 0x03); //
847 write_cmos_sensor(0x380b, 0xcc); //;c0
848 write_cmos_sensor(0x380c, ((imgsensor_info
.normal_video
.linelength
>> 8) & 0xFF)); // hts
849 write_cmos_sensor(0x380d, (imgsensor_info
.normal_video
.linelength
& 0xFF)); // hts
850 write_cmos_sensor(0x380e, ((imgsensor_info
.normal_video
.framelength
>> 8) & 0xFF)); // vts
851 write_cmos_sensor(0x380f, (imgsensor_info
.normal_video
.framelength
& 0xFF)); // vts
852 write_cmos_sensor(0x3814, 0x03); //
853 write_cmos_sensor(0x3820, 0x90); //
854 write_cmos_sensor(0x3821, 0x47); //
855 write_cmos_sensor(0x382a, 0x03); //
856 write_cmos_sensor(0x3845, 0x02); //
857 write_cmos_sensor(0x4009, 0x05); //
858 write_cmos_sensor(0x4502, 0x44); //
859 write_cmos_sensor(0x4508, 0x55); //
860 write_cmos_sensor(0x4509, 0x55); //
861 write_cmos_sensor(0x450a, 0x00); //
862 write_cmos_sensor(0x4600, 0x00); //
863 write_cmos_sensor(0x4601, 0x81); //
864 write_cmos_sensor(0x4017, 0x10); //; threshold = 4LSB for Binning
865 write_cmos_sensor(0x400a, 0x02); //;
866 write_cmos_sensor(0x400b, 0x00); //;
868 write_cmos_sensor(0x0100, 0x01); //
878 /*************************************************************************
883 * This function get the sensor ID
886 * *sensorID : return the sensor ID
893 *************************************************************************/
894 static kal_uint32
get_imgsensor_id(UINT32
*sensor_id
)
898 //sensor have two i2c address 0x6c 0x6d & 0x21 0x20, we should detect the module used i2c address
899 while (imgsensor_info
.i2c_addr_table
[i
] != 0xff) {
900 spin_lock(&imgsensor_drv_lock
);
901 imgsensor
.i2c_write_id
= imgsensor_info
.i2c_addr_table
[i
];
902 spin_unlock(&imgsensor_drv_lock
);
904 *sensor_id
= ((read_cmos_sensor(0x300B) << 8) | read_cmos_sensor(0x300C));
905 if (*sensor_id
== imgsensor_info
.sensor_id
) {
906 printk("alphaxiang i2c write id: 0x%x, sensor id: 0x%x\n", imgsensor
.i2c_write_id
,*sensor_id
);
909 LOG_INF("Read sensor id fail, id: 0x%x\n", imgsensor
.i2c_write_id
,*sensor_id
);
915 if (*sensor_id
!= imgsensor_info
.sensor_id
) {
916 // if Sensor ID is not correct, Must set *sensor_id to 0xFFFFFFFF
917 *sensor_id
= 0xFFFFFFFF;
918 return ERROR_SENSOR_CONNECT_FAIL
;
924 /*************************************************************************
929 * This function initialize the registers of CMOS sensor
939 *************************************************************************/
940 static kal_uint32
open(void)
942 //const kal_uint8 i2c_addr[] = {IMGSENSOR_WRITE_ID_1, IMGSENSOR_WRITE_ID_2};
945 kal_uint16 sensor_id
= 0;
946 LOG_INF("preview 1280*960@30fps,864Mbps/lane; video 1280*960@30fps,864Mbps/lane; capture 5M@30fps,864Mbps/lane\n");
948 //sensor have two i2c address 0x6c 0x6d & 0x21 0x20, we should detect the module used i2c address
949 while (imgsensor_info
.i2c_addr_table
[i
] != 0xff) {
950 spin_lock(&imgsensor_drv_lock
);
951 imgsensor
.i2c_write_id
= imgsensor_info
.i2c_addr_table
[i
];
952 spin_unlock(&imgsensor_drv_lock
);
954 sensor_id
= ((read_cmos_sensor(0x300B) << 8) | read_cmos_sensor(0x300C));
955 if (sensor_id
== imgsensor_info
.sensor_id
) {
956 LOG_INF("i2c write id: 0x%x, sensor id: 0x%x\n", imgsensor
.i2c_write_id
,sensor_id
);
959 LOG_INF("Read sensor id fail, id: 0x%x\n", imgsensor
.i2c_write_id
,sensor_id
);
963 if (sensor_id
== imgsensor_info
.sensor_id
)
967 if (imgsensor_info
.sensor_id
!= sensor_id
)
968 return ERROR_SENSOR_CONNECT_FAIL
;
970 /* initail sequence write in */
973 spin_lock(&imgsensor_drv_lock
);
975 imgsensor
.autoflicker_en
= KAL_FALSE
;
976 imgsensor
.sensor_mode
= IMGSENSOR_MODE_INIT
;
977 imgsensor
.shutter
= 0x4C00;
978 imgsensor
.gain
= 0x0200;
979 imgsensor
.pclk
= imgsensor_info
.pre
.pclk
;
980 imgsensor
.frame_length
= imgsensor_info
.pre
.framelength
;
981 imgsensor
.line_length
= imgsensor_info
.pre
.linelength
;
982 imgsensor
.min_frame_length
= imgsensor_info
.pre
.framelength
;
983 imgsensor
.dummy_pixel
= 0;
984 imgsensor
.dummy_line
= 0;
985 imgsensor
.ihdr_en
= 0;
986 imgsensor
.test_pattern
= KAL_FALSE
;
987 imgsensor
.current_fps
= imgsensor_info
.pre
.max_framerate
;
988 spin_unlock(&imgsensor_drv_lock
);
995 /*************************************************************************
1010 *************************************************************************/
1011 static kal_uint32
close(void)
1015 /*No Need to implement this function*/
1021 /*************************************************************************
1026 * This function start the sensor preview.
1029 * *image_window : address pointer of pixel numbers in one period of HSYNC
1030 * *sensor_config_data : address pointer of line numbers in one period of VSYNC
1037 *************************************************************************/
1038 static kal_uint32
preview(MSDK_SENSOR_EXPOSURE_WINDOW_STRUCT
*image_window
,
1039 MSDK_SENSOR_CONFIG_STRUCT
*sensor_config_data
)
1043 spin_lock(&imgsensor_drv_lock
);
1044 imgsensor
.sensor_mode
= IMGSENSOR_MODE_PREVIEW
;
1045 imgsensor
.pclk
= imgsensor_info
.pre
.pclk
;
1046 //imgsensor.video_mode = KAL_FALSE;
1047 imgsensor
.line_length
= imgsensor_info
.pre
.linelength
;
1048 imgsensor
.frame_length
= imgsensor_info
.pre
.framelength
;
1049 imgsensor
.min_frame_length
= imgsensor_info
.pre
.framelength
;
1050 imgsensor
.autoflicker_en
= KAL_FALSE
;
1051 spin_unlock(&imgsensor_drv_lock
);
1056 /*************************************************************************
1061 * This function setup the CMOS sensor in capture MY_OUTPUT mode
1070 *************************************************************************/
1071 static kal_uint32
capture(MSDK_SENSOR_EXPOSURE_WINDOW_STRUCT
*image_window
,
1072 MSDK_SENSOR_CONFIG_STRUCT
*sensor_config_data
)
1075 spin_lock(&imgsensor_drv_lock
);
1076 imgsensor
.sensor_mode
= IMGSENSOR_MODE_CAPTURE
;
1077 imgsensor
.current_fps
= 300;
1078 imgsensor
.pclk
= imgsensor_info
.cap
.pclk
;
1079 imgsensor
.line_length
= imgsensor_info
.cap
.linelength
;
1080 imgsensor
.frame_length
= imgsensor_info
.cap
.framelength
;
1081 imgsensor
.min_frame_length
= imgsensor_info
.cap
.framelength
;
1082 imgsensor
.current_fps
= imgsensor_info
.cap
.max_framerate
;
1083 imgsensor
.autoflicker_en
= KAL_FALSE
;
1085 spin_unlock(&imgsensor_drv_lock
);
1087 capture_setting(imgsensor
.current_fps
);
1092 static kal_uint32
normal_video(MSDK_SENSOR_EXPOSURE_WINDOW_STRUCT
*image_window
,
1093 MSDK_SENSOR_CONFIG_STRUCT
*sensor_config_data
)
1097 spin_lock(&imgsensor_drv_lock
);
1098 imgsensor
.sensor_mode
= IMGSENSOR_MODE_VIDEO
;
1099 imgsensor
.pclk
= imgsensor_info
.normal_video
.pclk
;
1100 imgsensor
.line_length
= imgsensor_info
.normal_video
.linelength
;
1101 imgsensor
.frame_length
= imgsensor_info
.normal_video
.framelength
;
1102 imgsensor
.min_frame_length
= imgsensor_info
.normal_video
.framelength
;
1103 //imgsensor.current_fps = 300;
1104 imgsensor
.autoflicker_en
= KAL_FALSE
;
1105 spin_unlock(&imgsensor_drv_lock
);
1106 normal_video_setting(imgsensor
.current_fps
);
1110 } /* normal_video */
1115 static kal_uint32
get_resolution(MSDK_SENSOR_RESOLUTION_INFO_STRUCT
*sensor_resolution
)
1118 sensor_resolution
->SensorFullWidth
= imgsensor_info
.cap
.grabwindow_width
;
1119 sensor_resolution
->SensorFullHeight
= imgsensor_info
.cap
.grabwindow_height
;
1121 sensor_resolution
->SensorPreviewWidth
= imgsensor_info
.pre
.grabwindow_width
;
1122 sensor_resolution
->SensorPreviewHeight
= imgsensor_info
.pre
.grabwindow_height
;
1124 sensor_resolution
->SensorVideoWidth
= imgsensor_info
.normal_video
.grabwindow_width
;
1125 sensor_resolution
->SensorVideoHeight
= imgsensor_info
.normal_video
.grabwindow_height
;
1129 } /* get_resolution */
1131 static kal_uint32
get_info(MSDK_SCENARIO_ID_ENUM scenario_id
,
1132 MSDK_SENSOR_INFO_STRUCT
*sensor_info
,
1133 MSDK_SENSOR_CONFIG_STRUCT
*sensor_config_data
)
1135 LOG_INF("scenario_id = %d\n", scenario_id
);
1138 //sensor_info->SensorVideoFrameRate = imgsensor_info.normal_video.max_framerate/10; /* not use */
1139 //sensor_info->SensorStillCaptureFrameRate= imgsensor_info.cap.max_framerate/10; /* not use */
1140 //imgsensor_info->SensorWebCamCaptureFrameRate= imgsensor_info.v.max_framerate; /* not use */
1142 sensor_info
->SensorClockPolarity
= SENSOR_CLOCK_POLARITY_LOW
;
1143 sensor_info
->SensorClockFallingPolarity
= SENSOR_CLOCK_POLARITY_LOW
; /* not use */
1144 sensor_info
->SensorHsyncPolarity
= SENSOR_CLOCK_POLARITY_LOW
; // inverse with datasheet
1145 sensor_info
->SensorVsyncPolarity
= SENSOR_CLOCK_POLARITY_LOW
;
1146 sensor_info
->SensorInterruptDelayLines
= 4; /* not use */
1147 sensor_info
->SensorResetActiveHigh
= FALSE
; /* not use */
1148 sensor_info
->SensorResetDelayCount
= 5; /* not use */
1150 sensor_info
->SensroInterfaceType
= imgsensor_info
.sensor_interface_type
;
1152 sensor_info
->SensorOutputDataFormat
= imgsensor_info
.sensor_output_dataformat
;
1153 sensor_info
->CaptureDelayFrame
= imgsensor_info
.cap_delay_frame
;
1154 sensor_info
->PreviewDelayFrame
= imgsensor_info
.pre_delay_frame
;
1155 sensor_info
->VideoDelayFrame
= imgsensor_info
.video_delay_frame
;
1157 sensor_info
->SensorMasterClockSwitch
= 0; /* not use */
1158 sensor_info
->SensorDrivingCurrent
= imgsensor_info
.isp_driving_current
;
1160 sensor_info
->AEShutDelayFrame
= imgsensor_info
.ae_shut_delay_frame
; /* The frame of setting shutter default 0 for TG int */
1161 sensor_info
->AESensorGainDelayFrame
= imgsensor_info
.ae_sensor_gain_delay_frame
; /* The frame of setting sensor gain */
1162 sensor_info
->AEISPGainDelayFrame
= imgsensor_info
.ae_ispGain_delay_frame
;
1164 sensor_info
->SensorMIPILaneNumber
= imgsensor_info
.mipi_lane_num
;
1165 sensor_info
->SensorClockFreq
= imgsensor_info
.mclk
;
1166 sensor_info
->SensorClockDividCount
= 3; /* not use */
1167 sensor_info
->SensorClockRisingCount
= 0;
1168 sensor_info
->SensorClockFallingCount
= 2; /* not use */
1169 sensor_info
->SensorPixelClockCount
= 3; /* not use */
1170 sensor_info
->SensorDataLatchCount
= 2; /* not use */
1172 sensor_info
->MIPIDataLowPwr2HighSpeedTermDelayCount
= 0;
1173 sensor_info
->MIPICLKLowPwr2HighSpeedTermDelayCount
= 0;
1174 sensor_info
->SensorWidthSampling
= 0; // 0 is default 1x
1175 sensor_info
->SensorHightSampling
= 0; // 0 is default 1x
1176 sensor_info
->SensorPacketECCOrder
= 1;
1178 switch (scenario_id
) {
1179 case MSDK_SCENARIO_ID_CAMERA_PREVIEW
:
1180 sensor_info
->SensorGrabStartX
= imgsensor_info
.pre
.startx
;
1181 sensor_info
->SensorGrabStartY
= imgsensor_info
.pre
.starty
;
1183 sensor_info
->MIPIDataLowPwr2HighSpeedSettleDelayCount
= imgsensor_info
.pre
.mipi_data_lp2hs_settle_dc
;
1186 case MSDK_SCENARIO_ID_CAMERA_CAPTURE_JPEG
:
1187 case MSDK_SCENARIO_ID_CAMERA_ZSD
:
1188 sensor_info
->SensorGrabStartX
= imgsensor_info
.cap
.startx
;
1189 sensor_info
->SensorGrabStartY
= imgsensor_info
.cap
.starty
;
1191 sensor_info
->MIPIDataLowPwr2HighSpeedSettleDelayCount
= imgsensor_info
.cap
.mipi_data_lp2hs_settle_dc
;
1194 case MSDK_SCENARIO_ID_VIDEO_PREVIEW
:
1196 sensor_info
->SensorGrabStartX
= imgsensor_info
.normal_video
.startx
;
1197 sensor_info
->SensorGrabStartY
= imgsensor_info
.normal_video
.starty
;
1199 sensor_info
->MIPIDataLowPwr2HighSpeedSettleDelayCount
= imgsensor_info
.normal_video
.mipi_data_lp2hs_settle_dc
;
1202 sensor_info
->SensorGrabStartX
= imgsensor_info
.pre
.startx
;
1203 sensor_info
->SensorGrabStartY
= imgsensor_info
.pre
.starty
;
1205 sensor_info
->MIPIDataLowPwr2HighSpeedSettleDelayCount
= imgsensor_info
.pre
.mipi_data_lp2hs_settle_dc
;
1213 static kal_uint32
control(MSDK_SCENARIO_ID_ENUM scenario_id
, MSDK_SENSOR_EXPOSURE_WINDOW_STRUCT
*image_window
,
1214 MSDK_SENSOR_CONFIG_STRUCT
*sensor_config_data
)
1216 LOG_INF("scenario_id = %d\n", scenario_id
);
1217 spin_lock(&imgsensor_drv_lock
);
1218 imgsensor
.current_scenario_id
= scenario_id
;
1219 spin_unlock(&imgsensor_drv_lock
);
1220 switch (scenario_id
) {
1221 case MSDK_SCENARIO_ID_CAMERA_PREVIEW
:
1222 preview(image_window
, sensor_config_data
);
1224 case MSDK_SCENARIO_ID_CAMERA_CAPTURE_JPEG
:
1225 case MSDK_SCENARIO_ID_CAMERA_ZSD
:
1226 capture(image_window
, sensor_config_data
);
1228 case MSDK_SCENARIO_ID_VIDEO_PREVIEW
:
1229 normal_video(image_window
, sensor_config_data
);
1232 LOG_INF("Error ScenarioId setting");
1233 preview(image_window
, sensor_config_data
);
1234 return ERROR_INVALID_SCENARIO_ID
;
1241 static kal_uint32
set_video_mode(UINT16 framerate
)
1243 LOG_INF("framerate = %d\n ", framerate
);
1244 // SetVideoMode Function should fix framerate
1246 // Dynamic frame rate
1248 spin_lock(&imgsensor_drv_lock
);
1249 if ((framerate
== 30) && (imgsensor
.autoflicker_en
== KAL_TRUE
))
1251 imgsensor
.current_fps
= 296;
1253 else if ((framerate
== 15) && (imgsensor
.autoflicker_en
== KAL_TRUE
))
1255 imgsensor
.current_fps
= 146;
1259 imgsensor
.current_fps
= framerate
*10;
1261 spin_unlock(&imgsensor_drv_lock
);
1262 LOG_INF("current framerate = %d\n ", imgsensor
.current_fps
);
1263 set_max_framerate(imgsensor
.current_fps
,1);
1268 static kal_uint32
set_auto_flicker_mode(kal_bool enable
, UINT16 framerate
)
1270 LOG_INF("enable = %d, framerate = %d \n", enable
, framerate
);
1271 spin_lock(&imgsensor_drv_lock
);
1272 if (enable
) //enable auto flicker
1273 imgsensor
.autoflicker_en
= KAL_TRUE
;
1274 else //Cancel Auto flick
1275 imgsensor
.autoflicker_en
= KAL_FALSE
;
1276 spin_unlock(&imgsensor_drv_lock
);
1281 static kal_uint32
set_max_framerate_by_scenario(MSDK_SCENARIO_ID_ENUM scenario_id
, MUINT32 framerate
)
1283 kal_uint32 frame_length
;
1285 LOG_INF("scenario_id = %d, framerate = %d\n", scenario_id
, framerate
);
1287 switch (scenario_id
) {
1288 case MSDK_SCENARIO_ID_CAMERA_PREVIEW
:
1289 frame_length
= imgsensor_info
.pre
.pclk
/ framerate
* 10 / imgsensor_info
.pre
.linelength
;
1290 spin_lock(&imgsensor_drv_lock
);
1291 imgsensor
.dummy_line
= (frame_length
> imgsensor_info
.pre
.framelength
) ? (frame_length
- imgsensor_info
.pre
.framelength
) : 0;
1292 imgsensor
.frame_length
= imgsensor_info
.pre
.framelength
+ imgsensor
.dummy_line
;
1293 imgsensor
.min_frame_length
= imgsensor
.frame_length
;
1294 spin_unlock(&imgsensor_drv_lock
);
1297 case MSDK_SCENARIO_ID_VIDEO_PREVIEW
:
1300 frame_length
= imgsensor_info
.normal_video
.pclk
/ framerate
* 10 / imgsensor_info
.normal_video
.linelength
;
1301 spin_lock(&imgsensor_drv_lock
);
1302 imgsensor
.dummy_line
= (frame_length
> imgsensor_info
.normal_video
.framelength
) ? (frame_length
- imgsensor_info
.normal_video
.framelength
) : 0;
1303 imgsensor
.frame_length
= imgsensor_info
.normal_video
.framelength
+ imgsensor
.dummy_line
;
1304 imgsensor
.min_frame_length
= imgsensor
.frame_length
;
1305 spin_unlock(&imgsensor_drv_lock
);
1308 case MSDK_SCENARIO_ID_CAMERA_CAPTURE_JPEG
:
1309 case MSDK_SCENARIO_ID_CAMERA_ZSD
:
1310 frame_length
= imgsensor_info
.cap
.pclk
/ framerate
* 10 / imgsensor_info
.cap
.linelength
;
1311 spin_lock(&imgsensor_drv_lock
);
1312 imgsensor
.dummy_line
= (frame_length
> imgsensor_info
.cap
.framelength
) ? (frame_length
- imgsensor_info
.cap
.framelength
) : 0;
1313 imgsensor
.frame_length
= imgsensor_info
.cap
.framelength
+ imgsensor
.dummy_line
;
1314 imgsensor
.min_frame_length
= imgsensor
.frame_length
;
1315 spin_unlock(&imgsensor_drv_lock
);
1318 default: //coding with preview scenario by default
1319 frame_length
= imgsensor_info
.pre
.pclk
/ framerate
* 10 / imgsensor_info
.pre
.linelength
;
1320 spin_lock(&imgsensor_drv_lock
);
1321 imgsensor
.dummy_line
= (frame_length
> imgsensor_info
.pre
.framelength
) ? (frame_length
- imgsensor_info
.pre
.framelength
) : 0;
1322 imgsensor
.frame_length
= imgsensor_info
.pre
.framelength
+ imgsensor
.dummy_line
;
1323 imgsensor
.min_frame_length
= imgsensor
.frame_length
;
1324 spin_unlock(&imgsensor_drv_lock
);
1326 LOG_INF("error scenario_id = %d, we use preview scenario \n", scenario_id
);
1333 static kal_uint32
get_default_framerate_by_scenario(MSDK_SCENARIO_ID_ENUM scenario_id
, MUINT32
*framerate
)
1335 LOG_INF("scenario_id = %d\n", scenario_id
);
1337 switch (scenario_id
) {
1338 case MSDK_SCENARIO_ID_CAMERA_PREVIEW
:
1339 *framerate
= imgsensor_info
.pre
.max_framerate
;
1341 case MSDK_SCENARIO_ID_VIDEO_PREVIEW
:
1342 *framerate
= imgsensor_info
.normal_video
.max_framerate
;
1344 case MSDK_SCENARIO_ID_CAMERA_CAPTURE_JPEG
:
1345 case MSDK_SCENARIO_ID_CAMERA_ZSD
:
1346 *framerate
= imgsensor_info
.cap
.max_framerate
;
1355 static kal_uint32
set_test_pattern_mode(kal_bool enable
)
1357 LOG_INF("enable: %d\n", enable
);
1360 // 0x4303[3]: 1 enable, 0 disable
1361 // only supports a special color bar test pattern
1362 write_cmos_sensor(0x4303,0x08);
1364 // 0x4303[3]: 1 enable, 0 disable
1365 // only supports a special color bar test pattern
1366 write_cmos_sensor(0x4303,0x00);
1368 spin_lock(&imgsensor_drv_lock
);
1369 imgsensor
.test_pattern
= enable
;
1370 spin_unlock(&imgsensor_drv_lock
);
1374 static kal_uint32
feature_control(MSDK_SENSOR_FEATURE_ENUM feature_id
,
1375 UINT8
*feature_para
,UINT32
*feature_para_len
)
1377 UINT16
*feature_return_para_16
=(UINT16
*) feature_para
;
1378 UINT16
*feature_data_16
=(UINT16
*) feature_para
;
1379 UINT32
*feature_return_para_32
=(UINT32
*) feature_para
;
1380 UINT32
*feature_data_32
=(UINT32
*) feature_para
;
1382 MSDK_SENSOR_REG_INFO_STRUCT
*sensor_reg_data
=(MSDK_SENSOR_REG_INFO_STRUCT
*) feature_para
;
1384 LOG_INF("feature_id = %d\n", feature_id
);
1385 switch (feature_id
) {
1386 case SENSOR_FEATURE_GET_PERIOD
:
1387 *feature_return_para_16
++ = imgsensor
.line_length
;
1388 *feature_return_para_16
= imgsensor
.frame_length
;
1389 *feature_para_len
=4;
1391 case SENSOR_FEATURE_GET_PIXEL_CLOCK_FREQ
:
1392 *feature_return_para_32
= imgsensor
.pclk
;
1393 *feature_para_len
=4;
1395 case SENSOR_FEATURE_SET_ESHUTTER
:
1396 set_shutter(*feature_data_16
);
1398 case SENSOR_FEATURE_SET_NIGHTMODE
:
1399 night_mode((BOOL
) *feature_data_16
);
1401 case SENSOR_FEATURE_SET_GAIN
:
1402 set_gain((UINT16
) *feature_data_16
);
1404 case SENSOR_FEATURE_SET_FLASHLIGHT
:
1406 case SENSOR_FEATURE_SET_ISP_MASTER_CLOCK_FREQ
:
1408 case SENSOR_FEATURE_SET_REGISTER
:
1409 write_cmos_sensor(sensor_reg_data
->RegAddr
, sensor_reg_data
->RegData
);
1411 case SENSOR_FEATURE_GET_REGISTER
:
1412 sensor_reg_data
->RegData
= read_cmos_sensor(sensor_reg_data
->RegAddr
);
1414 case SENSOR_FEATURE_GET_LENS_DRIVER_ID
:
1415 // get the lens driver ID from EEPROM or just return LENS_DRIVER_ID_DO_NOT_CARE
1416 // if EEPROM does not exist in camera module.
1417 *feature_return_para_32
=LENS_DRIVER_ID_DO_NOT_CARE
;
1418 *feature_para_len
=4;
1420 case SENSOR_FEATURE_SET_VIDEO_MODE
:
1421 set_video_mode(*feature_data_16
);
1423 case SENSOR_FEATURE_CHECK_SENSOR_ID
:
1424 get_imgsensor_id(feature_return_para_32
);
1426 case SENSOR_FEATURE_SET_AUTO_FLICKER_MODE
:
1427 set_auto_flicker_mode((BOOL
)*feature_data_16
,*(feature_data_16
+1));
1429 case SENSOR_FEATURE_SET_MAX_FRAME_RATE_BY_SCENARIO
:
1430 set_max_framerate_by_scenario((MSDK_SCENARIO_ID_ENUM
)*feature_data_32
, *(feature_data_32
+1));
1432 case SENSOR_FEATURE_GET_DEFAULT_FRAME_RATE_BY_SCENARIO
:
1433 get_default_framerate_by_scenario((MSDK_SCENARIO_ID_ENUM
)*feature_data_32
, (MUINT32
*)(*(feature_data_32
+1)));
1435 case SENSOR_FEATURE_SET_TEST_PATTERN
:
1436 set_test_pattern_mode((BOOL
)*feature_data_16
);
1438 case SENSOR_FEATURE_GET_TEST_PATTERN_CHECKSUM_VALUE
: //for factory mode auto testing
1439 *feature_return_para_32
= imgsensor_info
.checksum_value
;
1440 *feature_para_len
=4;
1447 } /* feature_control() */
1449 static SENSOR_FUNCTION_STRUCT sensor_func
= {
1458 UINT32
OV5670_MIPI_RAW_SensorInit(PSENSOR_FUNCTION_STRUCT
*pfFunc
)
1460 /* To Do : Check Sensor status here */
1462 *pfFunc
=&sensor_func
;
1464 } /* OV5670_MIPI_RAW_SensorInit */