camera: camera driver and all sensor driver support
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / gc2015.c
1 /*
2 o* Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk29_camera.h>
23
24 static int debug;
25 module_param(debug, int, S_IRUGO|S_IWUSR);
26
27 #define dprintk(level, fmt, arg...) do {                        \
28         if (debug >= level)                                     \
29         printk(KERN_WARNING fmt , ## arg); } while (0)
30
31 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
32 #define SENSOR_DG(format, ...) dprintk(0, format, ## __VA_ARGS__)
33
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_GC2015
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_GC2015
48 #define SENSOR_ID 0x2005
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    1600
52 #define SENSOR_MAX_HEIGHT   1200
53 #define SENSOR_INIT_WIDTH       1024                    /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  768
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_UYVY8_2X8
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Focus         0
66 #define CONFIG_SENSOR_Exposure      0
67 #define CONFIG_SENSOR_Flash         0
68 #define CONFIG_SENSOR_Mirror        0
69 #define CONFIG_SENSOR_Flip          0
70
71 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
72 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
73 #define CONFIG_SENSOR_I2C_NOSCHED   0
74 #define CONFIG_SENSOR_I2C_RDWRCHK   0
75
76 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|\
77                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
78                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
79
80 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
81 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
82 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
83 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
84 #define COLOR_TEMPERATURE_OFFICE_DN     3500
85 #define COLOR_TEMPERATURE_OFFICE_UP     5000
86 #define COLOR_TEMPERATURE_HOME_DN       2500
87 #define COLOR_TEMPERATURE_HOME_UP       3500
88
89 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
90 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
91
92 #define SENSOR_AF_IS_ERR    (0x00<<0)
93 #define SENSOR_AF_IS_OK         (0x01<<0)
94 #define SENSOR_INIT_IS_ERR   (0x00<<28)
95 #define SENSOR_INIT_IS_OK    (0x01<<28)
96
97 struct reginfo
98 {
99     u8 reg;
100     u8 val;
101 };
102
103 /* init SVGA preview */
104 static struct reginfo sensor_init_data[] =
105 {
106 //{0xfe , 0x80}, //soft reset
107 {0x45 , 0x00}, //output_enable
108         //////////////////////////////////////////////////////////////////////////////////////
109         //////////////////////////preview capture switch /////////////////////////////////////
110         //////////////////////////////////////////////////////////////////////////////////////
111         //preview
112 {0x02 , 0x01}, //preview mode
113 {0x2a , 0xca}, //[7]col_binning , 0x[6]even skip
114 {0x48 , 0x40}, //manual_gain
115
116         ////////////////////////////////////////////////////////////////////////
117         ////////////////////////// preview LSC /////////////////////////////////
118         ////////////////////////////////////////////////////////////////////////
119         
120 {0xb0 , 0x13}, //[4]Y_LSC_en [3]lsc_compensate [2]signed_b4 [1:0]pixel array select
121 {0xb1 , 0x20}, //P_LSC_red_b2
122 {0xb2 , 0x20}, //P_LSC_green_b2
123 {0xb3 , 0x20}, //P_LSC_blue_b2
124 {0xb4 , 0x20}, //P_LSC_red_b4
125 {0xb5 , 0x20}, //P_LSC_green_b4
126 {0xb6 , 0x20}, //P_LSC_blue_b4
127 {0xb7 , 0x00}, //P_LSC_compensate_b2
128 {0xb8 , 0x80}, //P_LSC_row_center , 0x344 , 0x (0x600/2-100)/2=100
129 {0xb9 , 0x80}, //P_LSC_col_center , 0x544 , 0x (0x800/2-200)/2=100
130
131         ////////////////////////////////////////////////////////////////////////
132         ////////////////////////// capture LSC ///////////////////////////
133         ////////////////////////////////////////////////////////////////////////
134 {0xba , 0x13}, //[4]Y_LSC_en [3]lsc_compensate [2]signed_b4 [1:0]pixel array select
135 {0xbb , 0x20}, //C_LSC_red_b2
136 {0xbc , 0x20}, //C_LSC_green_b2
137 {0xbd , 0x20}, //C_LSC_blue_b2
138 {0xbe , 0x20}, //C_LSC_red_b4
139 {0xbf , 0x20}, //C_LSC_green_b4
140 {0xc0 , 0x20}, //C_LSC_blue_b4
141 {0xc1 , 0x00}, //C_Lsc_compensate_b2
142 {0xc2 , 0x80}, //C_LSC_row_center , 0x344 , 0x (0x1200/2-344)/2=128
143 {0xc3 , 0x80}, //C_LSC_col_center , 0x544 , 0x (0x1600/2-544)/2=128
144
145         //GC2015_SET_PAGE0; //page0
146
147         ////////////////////////////////////////////////////////////////////////
148         ////////////////////////// analog configure ///////////////////////////
149         ////////////////////////////////////////////////////////////////////////
150 {0x29 , 0x00}, //cisctl mode 1
151 {0x2b , 0x06}, //cisctl mode 3  
152 {0x32 , 0x0c}, //analog mode 1
153 {0x33 , 0x0f}, //analog mode 2
154 {0x34 , 0x00}, //[6:4]da_rsg
155         
156 {0x35 , 0x88}, //Vref_A25
157 {0x37 , 0x16}, //Drive Current
158
159         /////////////////////////////////////////////////////////////////////
160         ///////////////////////////ISP Related//////////////////////////////
161         /////////////////////////////////////////////////////////////////////
162 {0x40 , 0xff}, 
163 {0x41 , 0x24}, //[5]skin_detectionenable[2]auto_gray , 0x[1]y_gamma
164 {0x42 , 0x76}, //[7]auto_sa[6]auto_ee[5]auto_dndd[4]auto_lsc[3]na[2]abs , 0x[1]awb
165 {0x4b , 0xea}, //[1]AWB_gain_mode , 0x1:atpregain0:atpostgain
166 {0x4d , 0x03}, //[1]inbf_en
167 {0x4f , 0x01}, //AEC enable
168
169         ////////////////////////////////////////////////////////////////////
170         /////////////////////////// BLK  ///////////////////////////////////
171         ////////////////////////////////////////////////////////////////////
172 {0x63 , 0x77}, //BLK mode 1
173 {0x66 , 0x00}, //BLK global offset
174 {0x6d , 0x04}, 
175 {0x6e , 0x18}, //BLK offset submode,offset R
176 {0x6f , 0x10},
177 {0x70 , 0x18},
178 {0x71 , 0x10},
179 {0x73 , 0x03}, 
180
181
182         ////////////////////////////////////////////////////////////////////
183         /////////////////////////// DNDD ////////////////////////////////
184         ////////////////////////////////////////////////////////////////////
185 {0x80 , 0x07}, //[7]dn_inc_or_dec [4]zero_weight_mode[3]share [2]c_weight_adap [1]dn_lsc_mode [0]dn_b
186 {0x82 , 0x08}, //DN lilat b base
187
188         ////////////////////////////////////////////////////////////////////
189         /////////////////////////// EEINTP ////////////////////////////////
190         ////////////////////////////////////////////////////////////////////
191 {0x8a , 0x7c},
192 {0x8c , 0x02},
193 {0x8e , 0x02},
194 {0x8f , 0x48},
195
196         /////////////////////////////////////////////////////////////////////
197         /////////////////////////// CC_t ///////////////////////////////
198         /////////////////////////////////////////////////////////////////////
199 {0xb0 , 0x44},
200 {0xb1 , 0xfe},
201 {0xb2 , 0x00},
202 {0xb3 , 0xf8},
203 {0xb4 , 0x48},
204 {0xb5 , 0xf8},
205 {0xb6 , 0x00},
206 {0xb7 , 0x04},
207 {0xb8 , 0x00},
208
209         /////////////////////////////////////////////////////////////////////
210         /////////////////////////// GAMMA ///////////////////////////////////
211         /////////////////////////////////////////////////////////////////////
212         //RGB_GAMMA
213 {0xbf , 0x0e},
214 {0xc0 , 0x1c},
215 {0xc1 , 0x34},
216 {0xc2 , 0x48},
217 {0xc3 , 0x5a},
218 {0xc4 , 0x6b},
219 {0xc5 , 0x7b},
220 {0xc6 , 0x95},
221 {0xc7 , 0xab},
222 {0xc8 , 0xbf},
223 {0xc9 , 0xce},
224 {0xca , 0xd9},
225 {0xcb , 0xe4},
226 {0xcc , 0xec},
227 {0xcd , 0xf7},
228 {0xce , 0xfd},
229 {0xcf , 0xff},
230
231         /////////////////////////////////////////////////////////////////////
232         /////////////////////////// YCP_t  ///////////////////////////////
233         /////////////////////////////////////////////////////////////////////
234 {0xd1 , 0x38}, //saturation
235 {0xd2 , 0x38}, //saturation
236 {0xde , 0x21}, //auto_gray
237
238         ////////////////////////////////////////////////////////////////////
239         /////////////////////////// ASDE ////////////////////////////////
240         ////////////////////////////////////////////////////////////////////
241 {0x98 , 0x30},
242 {0x99 , 0xf0},
243 {0x9b , 0x00},
244
245         //GC2015_SET_PAGE1; //page1
246         ////////////////////////////////////////////////////////////////////
247         /////////////////////////// AEC  ////////////////////////////////
248         ////////////////////////////////////////////////////////////////////
249 {0x10 , 0x45}, //AEC mode 1
250 {0x11 , 0x32}, //[7]fix target
251 {0x13 , 0x60},
252 {0x17 , 0x00},
253 {0x1c , 0x96},
254 {0x1e , 0x11},
255 {0x21 , 0xc0}, //max_post_gain
256 {0x22 , 0x40}, //max_pre_gain
257 {0x2d , 0x06}, //P_N_AEC_exp_level_1[12:8]
258 {0x2e , 0x00}, //P_N_AEC_exp_level_1[7:0]
259 {0x1e , 0x32},
260 {0x33 , 0x00}, //[6:5]max_exp_level [4:0]min_exp_level
261
262         ////////////////////////////////////////////////////////////////////
263         ///////////////////////////  AWB  ////////////////////////////////
264         ////////////////////////////////////////////////////////////////////
265 {0x57 , 0x40}, //number limit
266 {0x5d , 0x44}, //
267 {0x5c , 0x35}, //show mode,close dark_mode
268 {0x5e , 0x29}, //close color temp
269 {0x5f , 0x50},
270 {0x60 , 0x50}, 
271 {0x65 , 0xc0},
272
273         ////////////////////////////////////////////////////////////////////
274         ///////////////////////////  ABS  ////////////////////////////////
275         ////////////////////////////////////////////////////////////////////
276 {0x80 , 0x82},
277 {0x81 , 0x00},
278 {0x83 , 0x00}, //ABS Y stretch limit
279
280         //GC2015_SET_PAGE0;
281
282         //////////////////////////////////////////////////////////////////////////////////////
283         ///////////////////////////// Crop //////////////////////////////////////////////////
284         //////////////////////////////////////////////////////////////////////////////////////
285 {0x50 , 0x01},//out window
286 {0x51 , 0x00},
287 {0x52 , 0x00},
288 {0x53 , 0x00},
289 {0x54 , 0x00},
290 {0x55 , 0x02},
291 {0x56 , 0x58},
292 {0x57 , 0x03},
293 {0x58 , 0x20},
294
295         ////////////////////////////////////////////////////////////////////
296         ///////////////////////////  OUT  ////////////////////////////////
297         ////////////////////////////////////////////////////////////////////
298 {0x44 , 0xa0}, //YUV sequence
299 {0x45 , 0x0f}, //output enable
300 {0x46 , 0x02}, //sync mode
301 };
302
303 /* 1600X1200 UXGA capture */
304 static struct reginfo sensor_uxga[] =
305 {
306 {0x45 , 0x0f}, //output enable
307  {0x0,0x0}
308 };
309
310 /* 1280X1024 SXGA */
311 static struct reginfo sensor_sxga[] =
312 {
313   {0x0, 0x0}
314 };
315 static struct reginfo sensor_xga[] =
316 {
317         {0x0, 0x0}
318 };
319 /* 800X600 SVGA,30fps*/
320 static struct reginfo sensor_svga[] =
321 {
322 {0x45 , 0x0f}, //output enable
323  {0x0,0x0}
324 };
325
326 /* 640X480 VGA */
327 static struct reginfo sensor_vga[] =
328 {
329 {0x45 , 0x0f}, //output enable
330  {0x0,0x0}
331 };
332
333 /* 352X288 CIF */
334 static struct reginfo sensor_cif[] =
335 {};
336
337 /* 320*240 QVGA */
338 static  struct reginfo sensor_qvga[] =
339 {};
340
341 /* 176X144 QCIF*/
342 static struct reginfo sensor_qcif[] =
343 {};
344 #if 0
345 /* 160X120 QQVGA*/
346 static struct reginfo ov2655_qqvga[] =
347 {
348
349     {0x300E, 0x34},
350     {0x3011, 0x01},
351     {0x3012, 0x10},
352     {0x302a, 0x02},
353     {0x302b, 0xE6},
354     {0x306f, 0x14},
355     {0x3362, 0x90},
356
357     {0x3070, 0x5d},
358     {0x3072, 0x5d},
359     {0x301c, 0x07},
360     {0x301d, 0x07},
361
362     {0x3020, 0x01},
363     {0x3021, 0x18},
364     {0x3022, 0x00},
365     {0x3023, 0x06},
366     {0x3024, 0x06},
367     {0x3025, 0x58},
368     {0x3026, 0x02},
369     {0x3027, 0x61},
370     {0x3088, 0x00},
371     {0x3089, 0xa0},
372     {0x308a, 0x00},
373     {0x308b, 0x78},
374     {0x3316, 0x64},
375     {0x3317, 0x25},
376     {0x3318, 0x80},
377     {0x3319, 0x08},
378     {0x331a, 0x0a},
379     {0x331b, 0x07},
380     {0x331c, 0x80},
381     {0x331d, 0x38},
382     {0x3100, 0x00},
383     {0x3302, 0x11},
384
385     {0x0, 0x0},
386 };
387
388
389
390 static  struct reginfo ov2655_Sharpness_auto[] =
391 {
392     {0x3306, 0x00},
393 };
394
395 static  struct reginfo ov2655_Sharpness1[] =
396 {
397     {0x3306, 0x08},
398     {0x3371, 0x00},
399 };
400
401 static  struct reginfo ov2655_Sharpness2[][3] =
402 {
403     //Sharpness 2
404     {0x3306, 0x08},
405     {0x3371, 0x01},
406 };
407
408 static  struct reginfo ov2655_Sharpness3[] =
409 {
410     //default
411     {0x3306, 0x08},
412     {0x332d, 0x02},
413 };
414 static  struct reginfo ov2655_Sharpness4[]=
415 {
416     //Sharpness 4
417     {0x3306, 0x08},
418     {0x332d, 0x03},
419 };
420
421 static  struct reginfo ov2655_Sharpness5[] =
422 {
423     //Sharpness 5
424     {0x3306, 0x08},
425     {0x332d, 0x04},
426 };
427 #endif
428
429 static  struct reginfo sensor_ClrFmt_YUYV[]=
430 {
431
432     {0x00, 0x00}
433 };
434
435 static  struct reginfo sensor_ClrFmt_UYVY[]=
436 {
437
438     {0x00, 0x00}
439 };
440
441 #if CONFIG_SENSOR_WhiteBalance
442 static  struct reginfo sensor_WhiteB_Auto[]=
443 {
444
445     {0x00, 0x00}
446 };
447 /* Cloudy Colour Temperature : 6500K - 8000K  */
448 static  struct reginfo sensor_WhiteB_Cloudy[]=
449 {
450
451     {0x00, 0x00}
452 };
453 /* ClearDay Colour Temperature : 5000K - 6500K  */
454 static  struct reginfo sensor_WhiteB_ClearDay[]=
455 {
456     //Sunny
457
458     {0x00, 0x00}
459 };
460 /* Office Colour Temperature : 3500K - 5000K  */
461 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
462 {
463     //Office
464   
465     {0x00, 0x00}
466
467 };
468 /* Home Colour Temperature : 2500K - 3500K  */
469 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
470 {
471     //Home
472
473     {0x00, 0x00}
474 };
475 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
476     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
477 };
478 #endif
479
480 #if CONFIG_SENSOR_Brightness
481 static  struct reginfo sensor_Brightness0[]=
482 {
483     // Brightness -2
484  
485     {0x00, 0x00}
486 };
487
488 static  struct reginfo sensor_Brightness1[]=
489 {
490     // Brightness -1
491
492     {0x00, 0x00}
493 };
494
495 static  struct reginfo sensor_Brightness2[]=
496 {
497     //  Brightness 0
498
499     {0x00, 0x00}
500 };
501
502 static  struct reginfo sensor_Brightness3[]=
503 {
504     // Brightness +1
505
506     {0x00, 0x00}
507 };
508
509 static  struct reginfo sensor_Brightness4[]=
510 {
511     //  Brightness +2
512
513     {0x00, 0x00}
514 };
515
516 static  struct reginfo sensor_Brightness5[]=
517 {
518     //  Brightness +3
519
520     {0x00, 0x00}
521 };
522 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
523     sensor_Brightness4, sensor_Brightness5,NULL,
524 };
525
526 #endif
527
528 #if CONFIG_SENSOR_Effect
529 static  struct reginfo sensor_Effect_Normal[] =
530 {
531
532     {0x00, 0x00}
533 };
534
535 static  struct reginfo sensor_Effect_WandB[] =
536 {
537
538     {0x00, 0x00}
539 };
540
541 static  struct reginfo sensor_Effect_Sepia[] =
542 {
543
544     {0x00, 0x00}
545 };
546
547 static  struct reginfo sensor_Effect_Negative[] =
548 {
549     //Negative
550
551     {0x00, 0x00}
552 };
553 static  struct reginfo sensor_Effect_Bluish[] =
554 {
555     // Bluish
556
557     {0x00, 0x00}
558 };
559
560 static  struct reginfo sensor_Effect_Green[] =
561 {
562     //  Greenish
563
564     {0x00, 0x00}
565 };
566 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
567     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
568 };
569 #endif
570 #if CONFIG_SENSOR_Exposure
571 static  struct reginfo sensor_Exposure0[]=
572 {
573     //-3
574
575 };
576
577 static  struct reginfo sensor_Exposure1[]=
578 {
579     //-2
580
581     {0x00, 0x00}
582 };
583
584 static  struct reginfo sensor_Exposure2[]=
585 {
586     //-0.3EV
587
588     {0x00, 0x00}
589 };
590
591 static  struct reginfo sensor_Exposure3[]=
592 {
593     //default
594
595     {0x00, 0x00}
596 };
597
598 static  struct reginfo sensor_Exposure4[]=
599 {
600     // 1
601
602     {0x00, 0x00}
603 };
604
605 static  struct reginfo sensor_Exposure5[]=
606 {
607     // 2
608
609     {0x00, 0x00}
610 };
611
612 static  struct reginfo sensor_Exposure6[]=
613 {
614     // 3
615
616     {0x00, 0x00}
617 };
618
619 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
620     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
621 };
622 #endif
623 #if CONFIG_SENSOR_Saturation
624 static  struct reginfo sensor_Saturation0[]=
625 {
626
627     {0x00, 0x00}
628 };
629
630 static  struct reginfo sensor_Saturation1[]=
631 {
632
633     {0x00, 0x00}
634 };
635
636 static  struct reginfo sensor_Saturation2[]=
637 {
638
639     {0x00, 0x00}
640 };
641 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
642
643 #endif
644 #if CONFIG_SENSOR_Contrast
645 static  struct reginfo sensor_Contrast0[]=
646 {
647     //Contrast -3
648   
649     {0x00, 0x00}
650 };
651
652 static  struct reginfo sensor_Contrast1[]=
653 {
654     //Contrast -2
655
656     {0x00, 0x00}
657 };
658
659 static  struct reginfo sensor_Contrast2[]=
660 {
661     // Contrast -1
662
663     {0x00, 0x00}
664 };
665
666 static  struct reginfo sensor_Contrast3[]=
667 {
668     //Contrast 0
669
670     {0x00, 0x00}
671 };
672
673 static  struct reginfo sensor_Contrast4[]=
674 {
675     //Contrast +1
676
677     {0x00, 0x00}
678 };
679
680
681 static  struct reginfo sensor_Contrast5[]=
682 {
683     //Contrast +2
684
685     {0x00, 0x00}
686 };
687
688 static  struct reginfo sensor_Contrast6[]=
689 {
690     //Contrast +3
691
692     {0x00, 0x00}
693 };
694 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
695     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
696 };
697
698 #endif
699 #if CONFIG_SENSOR_Mirror
700 static  struct reginfo sensor_MirrorOn[]=
701 {
702
703     {0x00, 0x00}
704 };
705
706 static  struct reginfo sensor_MirrorOff[]=
707 {
708
709     {0x00, 0x00}
710 };
711 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
712 #endif
713 #if CONFIG_SENSOR_Flip
714 static  struct reginfo sensor_FlipOn[]=
715 {
716
717     {0x00, 0x00}
718 };
719
720 static  struct reginfo sensor_FlipOff[]=
721 {
722
723     {0x00, 0x00}
724 };
725 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
726
727 #endif
728 #if CONFIG_SENSOR_Scene
729 static  struct reginfo sensor_SceneAuto[] =
730 {
731 #if 0                           /* ddl@rock-chips.com : */
732     {0x3014, 0x04},
733     {0x3015, 0x00},
734     {0x302e, 0x00},
735     {0x302d, 0x00},
736     {0x00, 0x00}
737 #else
738
739     {0x00, 0x00}
740 #endif
741 };
742
743 static  struct reginfo sensor_SceneNight[] =
744 {
745 #if 1
746     //30fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,36Mzh pclk
747
748     {0x00, 0x00}
749 #else
750     //15fps ~ 5fps night mode for 60/50Hz light environment, 24Mhz clock input,18Mhz pclk
751     {0x300e, 0x34},
752     {0x3011, 0x01},
753     {0x302c, 0x00},
754     {0x3071, 0x00},
755     {0x3070, 0x5d},
756     {0x301c, 0x05},
757     {0x3073, 0x00},
758     {0x3072, 0x4d},
759     {0x301d, 0x07},
760     {0x3014, 0x0c},
761     {0x3015, 0x50},
762     {0x302e, 0x00},
763     {0x302d, 0x00},
764 #endif
765 };
766 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
767
768 #endif
769 #if CONFIG_SENSOR_DigitalZoom
770 static struct reginfo sensor_Zoom0[] =
771 {
772     {0x0, 0x0},
773 };
774
775 static struct reginfo sensor_Zoom1[] =
776 {
777      {0x0, 0x0},
778 };
779
780 static struct reginfo sensor_Zoom2[] =
781 {
782     {0x0, 0x0},
783 };
784
785
786 static struct reginfo sensor_Zoom3[] =
787 {
788     {0x0, 0x0},
789 };
790 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
791 #endif
792 static const struct v4l2_querymenu sensor_menus[] =
793 {
794         #if CONFIG_SENSOR_WhiteBalance
795     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
796     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
797     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
798     #endif
799
800         #if CONFIG_SENSOR_Effect
801     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
802     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
803     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
804     #endif
805
806         #if CONFIG_SENSOR_Scene
807     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
808     #endif
809
810         #if CONFIG_SENSOR_Flash
811     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
812     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
813     #endif
814 };
815
816 static const struct v4l2_queryctrl sensor_controls[] =
817 {
818         #if CONFIG_SENSOR_WhiteBalance
819     {
820         .id             = V4L2_CID_DO_WHITE_BALANCE,
821         .type           = V4L2_CTRL_TYPE_MENU,
822         .name           = "White Balance Control",
823         .minimum        = 0,
824         .maximum        = 4,
825         .step           = 1,
826         .default_value = 0,
827     },
828     #endif
829
830         #if CONFIG_SENSOR_Brightness
831         {
832         .id             = V4L2_CID_BRIGHTNESS,
833         .type           = V4L2_CTRL_TYPE_INTEGER,
834         .name           = "Brightness Control",
835         .minimum        = -3,
836         .maximum        = 2,
837         .step           = 1,
838         .default_value = 0,
839     },
840     #endif
841
842         #if CONFIG_SENSOR_Effect
843         {
844         .id             = V4L2_CID_EFFECT,
845         .type           = V4L2_CTRL_TYPE_MENU,
846         .name           = "Effect Control",
847         .minimum        = 0,
848         .maximum        = 5,
849         .step           = 1,
850         .default_value = 0,
851     },
852         #endif
853
854         #if CONFIG_SENSOR_Exposure
855         {
856         .id             = V4L2_CID_EXPOSURE,
857         .type           = V4L2_CTRL_TYPE_INTEGER,
858         .name           = "Exposure Control",
859         .minimum        = 0,
860         .maximum        = 6,
861         .step           = 1,
862         .default_value = 0,
863     },
864         #endif
865
866         #if CONFIG_SENSOR_Saturation
867         {
868         .id             = V4L2_CID_SATURATION,
869         .type           = V4L2_CTRL_TYPE_INTEGER,
870         .name           = "Saturation Control",
871         .minimum        = 0,
872         .maximum        = 2,
873         .step           = 1,
874         .default_value = 0,
875     },
876     #endif
877
878         #if CONFIG_SENSOR_Contrast
879         {
880         .id             = V4L2_CID_CONTRAST,
881         .type           = V4L2_CTRL_TYPE_INTEGER,
882         .name           = "Contrast Control",
883         .minimum        = -3,
884         .maximum        = 3,
885         .step           = 1,
886         .default_value = 0,
887     },
888         #endif
889
890         #if CONFIG_SENSOR_Mirror
891         {
892         .id             = V4L2_CID_HFLIP,
893         .type           = V4L2_CTRL_TYPE_BOOLEAN,
894         .name           = "Mirror Control",
895         .minimum        = 0,
896         .maximum        = 1,
897         .step           = 1,
898         .default_value = 1,
899     },
900     #endif
901
902         #if CONFIG_SENSOR_Flip
903         {
904         .id             = V4L2_CID_VFLIP,
905         .type           = V4L2_CTRL_TYPE_BOOLEAN,
906         .name           = "Flip Control",
907         .minimum        = 0,
908         .maximum        = 1,
909         .step           = 1,
910         .default_value = 1,
911     },
912     #endif
913
914         #if CONFIG_SENSOR_Scene
915     {
916         .id             = V4L2_CID_SCENE,
917         .type           = V4L2_CTRL_TYPE_MENU,
918         .name           = "Scene Control",
919         .minimum        = 0,
920         .maximum        = 1,
921         .step           = 1,
922         .default_value = 0,
923     },
924     #endif
925
926         #if CONFIG_SENSOR_DigitalZoom
927     {
928         .id             = V4L2_CID_ZOOM_RELATIVE,
929         .type           = V4L2_CTRL_TYPE_INTEGER,
930         .name           = "DigitalZoom Control",
931         .minimum        = -1,
932         .maximum        = 1,
933         .step           = 1,
934         .default_value = 0,
935     }, {
936         .id             = V4L2_CID_ZOOM_ABSOLUTE,
937         .type           = V4L2_CTRL_TYPE_INTEGER,
938         .name           = "DigitalZoom Control",
939         .minimum        = 0,
940         .maximum        = 3,
941         .step           = 1,
942         .default_value = 0,
943     },
944     #endif
945
946         #if CONFIG_SENSOR_Focus
947         {
948         .id             = V4L2_CID_FOCUS_RELATIVE,
949         .type           = V4L2_CTRL_TYPE_INTEGER,
950         .name           = "Focus Control",
951         .minimum        = -1,
952         .maximum        = 1,
953         .step           = 1,
954         .default_value = 0,
955     }, {
956         .id             = V4L2_CID_FOCUS_ABSOLUTE,
957         .type           = V4L2_CTRL_TYPE_INTEGER,
958         .name           = "Focus Control",
959         .minimum        = 0,
960         .maximum        = 255,
961         .step           = 1,
962         .default_value = 125,
963     },
964     #endif
965
966         #if CONFIG_SENSOR_Flash
967         {
968         .id             = V4L2_CID_FLASH,
969         .type           = V4L2_CTRL_TYPE_MENU,
970         .name           = "Flash Control",
971         .minimum        = 0,
972         .maximum        = 3,
973         .step           = 1,
974         .default_value = 0,
975     },
976         #endif
977 };
978
979 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
980 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
981 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
982 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
983 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
984 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
985 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
986 static int sensor_resume(struct soc_camera_device *icd);
987 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
988 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
989 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
990 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
991 static int sensor_deactivate(struct i2c_client *client);
992
993 static struct soc_camera_ops sensor_ops =
994 {
995     .suspend                     = sensor_suspend,
996     .resume                       = sensor_resume,
997     .set_bus_param              = sensor_set_bus_param,
998     .query_bus_param    = sensor_query_bus_param,
999     .controls           = sensor_controls,
1000     .menus                         = sensor_menus,
1001     .num_controls               = ARRAY_SIZE(sensor_controls),
1002     .num_menus          = ARRAY_SIZE(sensor_menus),
1003 };
1004
1005 /* only one fixed colorspace per pixelcode */
1006 struct sensor_datafmt {
1007         enum v4l2_mbus_pixelcode code;
1008         enum v4l2_colorspace colorspace;
1009 };
1010
1011 /* Find a data format by a pixel code in an array */
1012 static const struct sensor_datafmt *sensor_find_datafmt(
1013         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1014         int n)
1015 {
1016         int i;
1017         for (i = 0; i < n; i++)
1018                 if (fmt[i].code == code)
1019                         return fmt + i;
1020
1021         return NULL;
1022 }
1023
1024 static const struct sensor_datafmt sensor_colour_fmts[] = {
1025     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1026     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1027 };
1028
1029 typedef struct sensor_info_priv_s
1030 {
1031     int whiteBalance;
1032     int brightness;
1033     int contrast;
1034     int saturation;
1035     int effect;
1036     int scene;
1037     int digitalzoom;
1038     int focus;
1039     int flash;
1040     int exposure;
1041         bool snap2preview;
1042         bool video2preview;
1043     unsigned char mirror;                                        /* HFLIP */
1044     unsigned char flip;                                          /* VFLIP */
1045     unsigned int winseqe_cur_addr;
1046     struct sensor_datafmt fmt;
1047     unsigned int funmodule_state;
1048 } sensor_info_priv_t;
1049
1050 struct sensor
1051 {
1052     struct v4l2_subdev subdev;
1053     struct i2c_client *client;
1054     sensor_info_priv_t info_priv;
1055     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1056 #if CONFIG_SENSOR_I2C_NOSCHED
1057         atomic_t tasklock_cnt;
1058 #endif
1059         struct rk29camera_platform_data *sensor_io_request;
1060     struct rk29camera_gpio_res *sensor_gpio_res;
1061 };
1062
1063 static struct sensor* to_sensor(const struct i2c_client *client)
1064 {
1065     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1066 }
1067
1068 static int sensor_task_lock(struct i2c_client *client, int lock)
1069 {
1070 #if CONFIG_SENSOR_I2C_NOSCHED
1071         int cnt = 3;
1072     struct sensor *sensor = to_sensor(client);
1073
1074         if (lock) {
1075                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1076                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1077                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1078                                 msleep(35);
1079                                 cnt--;
1080                         }
1081                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1082                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1083                                 goto sensor_task_lock_err;
1084                         }
1085                         preempt_disable();
1086                 }
1087
1088                 atomic_add(1, &sensor->tasklock_cnt);
1089         } else {
1090                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1091                         atomic_sub(1, &sensor->tasklock_cnt);
1092
1093                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1094                                 preempt_enable();
1095                 }
1096         }
1097 #endif
1098         return 0;
1099 sensor_task_lock_err:
1100         return -1;
1101 }
1102
1103 #if 0
1104 /* sensor register */
1105 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1106 {
1107         int ret = 0;
1108
1109         ret = i2c_master_reg8_recv(client, reg, val, 1,  CONFIG_SENSOR_I2C_SPEED);
1110
1111         return (ret > 0)? 0 : ret;
1112 }
1113
1114 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1115 {
1116         int ret = 0;
1117         
1118         ret = i2c_master_reg8_send(client, reg, &val, 1, CONFIG_SENSOR_I2C_SPEED);
1119
1120         return (ret > 0)? 0 : ret;
1121 }
1122 #else
1123 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1124 {
1125     int err,cnt;
1126     u8 buf[2];
1127     struct i2c_msg msg[1];
1128
1129     buf[0] = reg;
1130     buf[1] = val;
1131
1132         if (reg == 0xfe)
1133                 mdelay(20);
1134         
1135     msg->addr = client->addr;
1136     msg->flags = client->flags;
1137     msg->buf = buf;
1138     msg->len = sizeof(buf);
1139     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1140     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1141
1142     cnt = 3;
1143     err = -EAGAIN;
1144
1145     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1146         err = i2c_transfer(client->adapter, msg, 1);
1147
1148         if (err >= 0) {
1149             return 0;
1150         } else {
1151             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1152             udelay(10);
1153         }
1154     }
1155
1156     return err;
1157 }
1158
1159 /* sensor register read */
1160 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1161 {
1162     int err,cnt;
1163     u8 buf[1];
1164     struct i2c_msg msg[2];
1165
1166     buf[0] = reg ;
1167
1168     msg[0].addr = client->addr;
1169     msg[0].flags = client->flags;
1170     msg[0].buf = buf;
1171     msg[0].len = sizeof(buf);
1172     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1173     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1174
1175     msg[1].addr = client->addr;
1176     msg[1].flags = client->flags|I2C_M_RD;
1177     msg[1].buf = buf;
1178     msg[1].len = 1;
1179     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1180     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1181
1182     cnt = 3;
1183     err = -EAGAIN;
1184     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1185         err = i2c_transfer(client->adapter, msg, 2);
1186
1187         if (err >= 0) {
1188             *val = buf[0];
1189             return 0;
1190         } else {
1191                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1192             udelay(10);
1193         }
1194     }
1195
1196     return err;
1197 }
1198
1199 #endif
1200
1201 /* write a array of registers  */
1202 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1203 {
1204     int err = 0, cnt;
1205     int i = 0;
1206 #if CONFIG_SENSOR_I2C_RDWRCHK    
1207         int j = 0;
1208         char valchk;
1209 #endif    
1210
1211         cnt = 0;
1212         if (sensor_task_lock(client, 1) < 0)
1213                 goto sensor_write_array_end;
1214     while (regarray[i].reg != 0)
1215     {
1216         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1217         if (err < 0)
1218         {
1219             if (cnt-- > 0) {
1220                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1221                                 i = 0;
1222                                 continue;
1223             } else {
1224                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1225                 err = -EPERM;
1226                                 goto sensor_write_array_end;
1227             }
1228         } else {
1229         #if CONFIG_SENSOR_I2C_RDWRCHK
1230                         //mdelay(5);
1231                         sensor_read(client, regarray[i].reg, &valchk);
1232                         if (valchk != regarray[i].val)
1233                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1234                 #endif
1235         }
1236         i++;
1237     }
1238
1239 sensor_write_array_end:
1240         sensor_task_lock(client,0);
1241         return err;
1242 }
1243 #if CONFIG_SENSOR_I2C_RDWRCHK
1244 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1245 {
1246     int cnt;
1247     int i = 0;
1248         char valchk;
1249
1250         cnt = 0;
1251         valchk = 0;
1252     while (regarray[i].reg != 0)
1253     {
1254                 sensor_read(client, regarray[i].reg, &valchk);
1255                 if (valchk != regarray[i].val)
1256                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1257
1258         i++;
1259     }
1260     return 0;
1261 }
1262 #endif
1263 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1264 {
1265         struct soc_camera_link *icl = to_soc_camera_link(icd);
1266         int ret = 0;
1267
1268     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1269         switch (cmd)
1270         {
1271                 case Sensor_PowerDown:
1272                 {
1273                         if (icl->powerdown) {
1274                                 ret = icl->powerdown(icd->pdev, on);
1275                                 if (ret == RK29_CAM_IO_SUCCESS) {
1276                                         if (on == 0) {
1277                                                 mdelay(2);
1278                                                 if (icl->reset)
1279                                                         icl->reset(icd->pdev);
1280                                         }
1281                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1282                                         ret = -ENODEV;
1283                                         goto sensor_power_end;
1284                                 }
1285                         }
1286                         break;
1287                 }
1288                 case Sensor_Flash:
1289                 {
1290                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1291                 struct sensor *sensor = to_sensor(client);
1292
1293                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1294                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1295                         }
1296             break;
1297                 }
1298                 default:
1299                 {
1300                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1301                         break;
1302                 }
1303         }
1304 sensor_power_end:
1305         return ret;
1306 }
1307 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1308 {
1309     struct i2c_client *client = sd->priv;
1310     struct soc_camera_device *icd = client->dev.platform_data;
1311     struct sensor *sensor = to_sensor(client);
1312         const struct v4l2_queryctrl *qctrl;
1313     const struct sensor_datafmt *fmt;
1314     char value;
1315     int ret,pid = 0;
1316
1317     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1318
1319         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1320                 ret = -ENODEV;
1321                 goto sensor_INIT_ERR;
1322         }
1323
1324     /* soft reset */
1325         if (sensor_task_lock(client,1)<0)
1326                 goto sensor_INIT_ERR;
1327     ret = sensor_write(client, 0xfe, 0x80);
1328     if (ret != 0)
1329     {
1330         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1331         ret = -ENODEV;
1332                 goto sensor_INIT_ERR;
1333     }
1334
1335     mdelay(5);  //delay 5 microseconds
1336         /* check if it is an sensor sensor */
1337     ret = sensor_read(client, 0x00, &value);
1338     if (ret != 0) {
1339         SENSOR_TR("read chip id high byte failed\n");
1340         ret = -ENODEV;
1341         goto sensor_INIT_ERR;
1342     }
1343
1344     pid |= (value << 8);
1345
1346     ret = sensor_read(client, 0x01, &value);
1347     if (ret != 0) {
1348         SENSOR_TR("read chip id low byte failed\n");
1349         ret = -ENODEV;
1350         goto sensor_INIT_ERR;
1351     }
1352
1353     pid |= (value & 0xff);
1354     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1355     if (pid == SENSOR_ID) {
1356         sensor->model = SENSOR_V4L2_IDENT;
1357     } else {
1358         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1359         ret = -ENODEV;
1360         goto sensor_INIT_ERR;
1361     }
1362
1363     ret = sensor_write_array(client, sensor_init_data);
1364     if (ret != 0)
1365     {
1366         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1367         goto sensor_INIT_ERR;
1368     }
1369         sensor_task_lock(client,0);
1370     
1371     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
1372     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1373     if (!fmt) {
1374         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1375         ret = -EINVAL;
1376         goto sensor_INIT_ERR;
1377     }
1378         sensor->info_priv.fmt = *fmt;
1379
1380     /* sensor sensor information for initialization  */
1381         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1382         if (qctrl)
1383         sensor->info_priv.whiteBalance = qctrl->default_value;
1384         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1385         if (qctrl)
1386         sensor->info_priv.brightness = qctrl->default_value;
1387         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1388         if (qctrl)
1389         sensor->info_priv.effect = qctrl->default_value;
1390         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1391         if (qctrl)
1392         sensor->info_priv.exposure = qctrl->default_value;
1393
1394         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1395         if (qctrl)
1396         sensor->info_priv.saturation = qctrl->default_value;
1397         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1398         if (qctrl)
1399         sensor->info_priv.contrast = qctrl->default_value;
1400         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1401         if (qctrl)
1402         sensor->info_priv.mirror = qctrl->default_value;
1403         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1404         if (qctrl)
1405         sensor->info_priv.flip = qctrl->default_value;
1406         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1407         if (qctrl)
1408         sensor->info_priv.scene = qctrl->default_value;
1409         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1410         if (qctrl)
1411         sensor->info_priv.digitalzoom = qctrl->default_value;
1412
1413     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1414         #if CONFIG_SENSOR_Focus
1415     sensor_set_focus();
1416     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1417         if (qctrl)
1418         sensor->info_priv.focus = qctrl->default_value;
1419         #endif
1420
1421         #if CONFIG_SENSOR_Flash
1422         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1423         if (qctrl)
1424         sensor->info_priv.flash = qctrl->default_value;
1425     #endif
1426
1427     SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),((val == 0)?__FUNCTION__:"sensor_reinit"),icd->user_width,icd->user_height);
1428     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
1429     return 0;
1430 sensor_INIT_ERR:
1431     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1432         sensor_task_lock(client,0);
1433         sensor_deactivate(client);
1434     return ret;
1435 }
1436
1437 static int sensor_deactivate(struct i2c_client *client)
1438 {
1439         struct soc_camera_device *icd = client->dev.platform_data;
1440
1441     struct sensor *sensor = to_sensor(client);
1442         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1443
1444         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1445     sensor_ioctrl(icd, Sensor_PowerDown, 1); 
1446     msleep(100); 
1447
1448         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1449         icd->user_width = SENSOR_INIT_WIDTH;
1450     icd->user_height = SENSOR_INIT_HEIGHT;
1451     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1452         
1453         return 0;
1454 }
1455
1456 static  struct reginfo sensor_power_down_sequence[]=
1457 {
1458     {0x00,0x00}
1459 };
1460 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1461 {
1462     int ret;
1463     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1464
1465     if (pm_msg.event == PM_EVENT_SUSPEND) {
1466         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1467         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1468         if (ret != 0) {
1469             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1470             return ret;
1471         } else {
1472             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1473             if (ret < 0) {
1474                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1475                 return -EINVAL;
1476             }
1477         }
1478     } else {
1479         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1480         return -EINVAL;
1481     }
1482     return 0;
1483 }
1484
1485 static int sensor_resume(struct soc_camera_device *icd)
1486 {
1487         int ret;
1488
1489     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1490     if (ret < 0) {
1491                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1492         return -EINVAL;
1493     }
1494
1495         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1496
1497     return 0;
1498
1499 }
1500
1501 static int sensor_set_bus_param(struct soc_camera_device *icd,
1502                                 unsigned long flags)
1503 {
1504
1505     return 0;
1506 }
1507
1508 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1509 {
1510     struct soc_camera_link *icl = to_soc_camera_link(icd);
1511     unsigned long flags = SENSOR_BUS_PARAM;
1512
1513     return soc_camera_apply_sensor_flags(icl, flags);
1514 }
1515
1516 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1517 {
1518     struct i2c_client *client = sd->priv;
1519     struct soc_camera_device *icd = client->dev.platform_data;
1520     struct sensor *sensor = to_sensor(client);
1521
1522     mf->width   = icd->user_width;
1523         mf->height      = icd->user_height;
1524         mf->code        = sensor->info_priv.fmt.code;
1525         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1526         mf->field       = V4L2_FIELD_NONE;
1527
1528     return 0;
1529 }
1530 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1531 {
1532     bool ret = false;
1533
1534         if ((mf->width == 1024) && (mf->height == 768)) {
1535                 ret = true;
1536         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1537                 ret = true;
1538         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1539                 ret = true;
1540         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1541                 ret = true;
1542         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1543                 ret = true;
1544         }
1545
1546         if (ret == true)
1547                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1548         return ret;
1549 }
1550
1551 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1552 {
1553     bool ret = false;
1554
1555         if ((mf->width == 1280) && (mf->height == 720)) {
1556                 ret = true;
1557         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1558                 ret = true;
1559         }
1560
1561         if (ret == true)
1562                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1563         return ret;
1564 }
1565 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1566 {
1567     struct i2c_client *client = sd->priv;
1568     const struct sensor_datafmt *fmt;
1569     struct sensor *sensor = to_sensor(client);
1570         const struct v4l2_queryctrl *qctrl;
1571         struct soc_camera_device *icd = client->dev.platform_data;
1572     struct reginfo *winseqe_set_addr=NULL;
1573     int ret=0, set_w,set_h;
1574
1575         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1576                                    ARRAY_SIZE(sensor_colour_fmts));
1577         if (!fmt) {
1578         ret = -EINVAL;
1579         goto sensor_s_fmt_end;
1580     }
1581
1582         if (sensor->info_priv.fmt.code != mf->code) {
1583                 switch (mf->code)
1584                 {
1585                         case V4L2_MBUS_FMT_YUYV8_2X8:
1586                         {
1587                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1588                                 break;
1589                         }
1590                         case V4L2_MBUS_FMT_UYVY8_2X8:
1591                         {
1592                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1593                                 break;
1594                         }
1595                         default:
1596                                 break;
1597                 }
1598                 if (winseqe_set_addr != NULL) {
1599             sensor_write_array(client, winseqe_set_addr);
1600                         sensor->info_priv.fmt.code = mf->code;
1601             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1602                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1603                 } else {
1604                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1605                 }
1606         }
1607
1608     set_w = mf->width;
1609     set_h = mf->height;
1610
1611         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1612         {
1613                 winseqe_set_addr = sensor_qcif;
1614         set_w = 176;
1615         set_h = 144;
1616         }
1617         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1618     {
1619         winseqe_set_addr = sensor_qvga;
1620         set_w = 320;
1621         set_h = 240;
1622     }
1623     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1624     {
1625         winseqe_set_addr = sensor_cif;
1626         set_w = 352;
1627         set_h = 288;
1628     }
1629     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1630     {
1631         winseqe_set_addr = sensor_vga;
1632         set_w = 640;
1633         set_h = 480;
1634     }
1635     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
1636     {
1637         winseqe_set_addr = sensor_svga;
1638         set_w = 800;
1639         set_h = 600;
1640     }
1641         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
1642     {
1643         winseqe_set_addr = sensor_xga;
1644         set_w = 1024;
1645         set_h = 768;
1646     }
1647     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
1648     {
1649         winseqe_set_addr = sensor_sxga;
1650         set_w = 1280;
1651         set_h = 1024;
1652     }
1653     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
1654     {
1655         winseqe_set_addr = sensor_uxga;
1656         set_w = 1600;
1657         set_h = 1200;
1658     }
1659     else
1660     {
1661         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1662         set_w = SENSOR_INIT_WIDTH;
1663         set_h = SENSOR_INIT_HEIGHT;     
1664                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1665     }
1666
1667     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1668         #if CONFIG_SENSOR_Flash
1669         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1670             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1671                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1672                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1673             }           
1674         } else {                                        /* ddl@rock-chips.com : Video */
1675             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1676                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1677                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1678             }
1679         }
1680         #endif
1681         ret |= sensor_write_array(client, winseqe_set_addr);
1682         if (ret != 0) {
1683             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1684             #if CONFIG_SENSOR_Flash
1685             if (sensor_fmt_capturechk(sd,mf) == true) {
1686                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1687                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1688                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1689                 }
1690             }
1691             #endif
1692             goto sensor_s_fmt_end;
1693         }
1694
1695         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1696
1697                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1698                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1699                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1700                         if (sensor->info_priv.whiteBalance != 0) {
1701                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1702                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1703                         }
1704                         sensor->info_priv.snap2preview = true;
1705                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1706                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1707                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1708                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1709                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1710                         sensor->info_priv.video2preview = true;
1711                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1712                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1713                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1714                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1715                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1716                         sensor->info_priv.video2preview = false;
1717                         sensor->info_priv.snap2preview = false;
1718                 }
1719         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1720     }
1721     else
1722     {
1723         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1724     }
1725
1726         mf->width = set_w;
1727     mf->height = set_h;
1728
1729 sensor_s_fmt_end:
1730     return ret;
1731 }
1732
1733 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1734 {
1735     struct i2c_client *client = sd->priv;
1736     struct sensor *sensor = to_sensor(client);
1737     const struct sensor_datafmt *fmt;
1738     int ret = 0;
1739    
1740         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1741                                    ARRAY_SIZE(sensor_colour_fmts));
1742         if (fmt == NULL) {
1743                 fmt = &sensor->info_priv.fmt;
1744         mf->code = fmt->code;
1745         } 
1746
1747     if (mf->height > SENSOR_MAX_HEIGHT)
1748         mf->height = SENSOR_MAX_HEIGHT;
1749     else if (mf->height < SENSOR_MIN_HEIGHT)
1750         mf->height = SENSOR_MIN_HEIGHT;
1751
1752     if (mf->width > SENSOR_MAX_WIDTH)
1753         mf->width = SENSOR_MAX_WIDTH;
1754     else if (mf->width < SENSOR_MIN_WIDTH)
1755         mf->width = SENSOR_MIN_WIDTH;
1756
1757     mf->colorspace = fmt->colorspace;
1758     
1759     return ret;
1760 }
1761
1762  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1763 {
1764     struct i2c_client *client = sd->priv;
1765
1766     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1767         return -EINVAL;
1768
1769     if (id->match.addr != client->addr)
1770         return -ENODEV;
1771
1772     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1773     id->revision = 0;
1774
1775     return 0;
1776 }
1777 #if CONFIG_SENSOR_Brightness
1778 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1779 {
1780     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1781
1782     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1783     {
1784         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1785         {
1786             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1787             {
1788                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1789                 return -EINVAL;
1790             }
1791             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1792             return 0;
1793         }
1794     }
1795         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1796     return -EINVAL;
1797 }
1798 #endif
1799 #if CONFIG_SENSOR_Effect
1800 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1801 {
1802     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1803
1804     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1805     {
1806         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1807         {
1808             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1809             {
1810                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1811                 return -EINVAL;
1812             }
1813             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1814             return 0;
1815         }
1816     }
1817         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1818     return -EINVAL;
1819 }
1820 #endif
1821 #if CONFIG_SENSOR_Exposure
1822 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1823 {
1824     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1825
1826     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1827     {
1828         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1829         {
1830             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1831             {
1832                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1833                 return -EINVAL;
1834             }
1835             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1836             return 0;
1837         }
1838     }
1839         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1840     return -EINVAL;
1841 }
1842 #endif
1843 #if CONFIG_SENSOR_Saturation
1844 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1845 {
1846     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1847
1848     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1849     {
1850         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1851         {
1852             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1853             {
1854                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1855                 return -EINVAL;
1856             }
1857             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1858             return 0;
1859         }
1860     }
1861     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1862     return -EINVAL;
1863 }
1864 #endif
1865 #if CONFIG_SENSOR_Contrast
1866 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1867 {
1868     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1869
1870     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1871     {
1872         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
1873         {
1874             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
1875             {
1876                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1877                 return -EINVAL;
1878             }
1879             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1880             return 0;
1881         }
1882     }
1883     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1884     return -EINVAL;
1885 }
1886 #endif
1887 #if CONFIG_SENSOR_Mirror
1888 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1889 {
1890     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1891
1892     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1893     {
1894         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
1895         {
1896             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
1897             {
1898                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1899                 return -EINVAL;
1900             }
1901             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1902             return 0;
1903         }
1904     }
1905     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1906     return -EINVAL;
1907 }
1908 #endif
1909 #if CONFIG_SENSOR_Flip
1910 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1911 {
1912     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1913
1914     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1915     {
1916         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
1917         {
1918             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
1919             {
1920                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1921                 return -EINVAL;
1922             }
1923             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1924             return 0;
1925         }
1926     }
1927     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1928     return -EINVAL;
1929 }
1930 #endif
1931 #if CONFIG_SENSOR_Scene
1932 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1933 {
1934     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1935
1936     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1937     {
1938         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
1939         {
1940             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
1941             {
1942                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1943                 return -EINVAL;
1944             }
1945             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1946             return 0;
1947         }
1948     }
1949     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1950     return -EINVAL;
1951 }
1952 #endif
1953 #if CONFIG_SENSOR_WhiteBalance
1954 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1955 {
1956     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1957
1958     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1959     {
1960         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
1961         {
1962             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
1963             {
1964                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1965                 return -EINVAL;
1966             }
1967             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1968             return 0;
1969         }
1970     }
1971         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1972     return -EINVAL;
1973 }
1974 #endif
1975 #if CONFIG_SENSOR_DigitalZoom
1976 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
1977 {
1978     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1979     struct sensor *sensor = to_sensor(client);
1980         const struct v4l2_queryctrl *qctrl_info;
1981     int digitalzoom_cur, digitalzoom_total;
1982
1983         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1984         if (qctrl_info)
1985                 return -EINVAL;
1986
1987     digitalzoom_cur = sensor->info_priv.digitalzoom;
1988     digitalzoom_total = qctrl_info->maximum;
1989
1990     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
1991     {
1992         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1993         return -EINVAL;
1994     }
1995
1996     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
1997     {
1998         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
1999         return -EINVAL;
2000     }
2001
2002     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2003     {
2004         value = digitalzoom_total - digitalzoom_cur;
2005     }
2006
2007     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2008     {
2009         value = 0 - digitalzoom_cur;
2010     }
2011
2012     digitalzoom_cur += value;
2013
2014     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2015     {
2016         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2017         {
2018             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2019             return -EINVAL;
2020         }
2021         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2022         return 0;
2023     }
2024
2025     return -EINVAL;
2026 }
2027 #endif
2028 #if CONFIG_SENSOR_Flash
2029 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2030 {    
2031     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2032         if (value == 3) {       /* ddl@rock-chips.com: torch */
2033             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2034         } else {
2035             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2036         }
2037         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2038         return 0;
2039     }
2040     
2041         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2042     return -EINVAL;
2043 }
2044 #endif
2045
2046 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2047 {
2048     struct i2c_client *client = sd->priv;
2049     struct sensor *sensor = to_sensor(client);
2050     const struct v4l2_queryctrl *qctrl;
2051
2052     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2053
2054     if (!qctrl)
2055     {
2056         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2057         return -EINVAL;
2058     }
2059
2060     switch (ctrl->id)
2061     {
2062         case V4L2_CID_BRIGHTNESS:
2063             {
2064                 ctrl->value = sensor->info_priv.brightness;
2065                 break;
2066             }
2067         case V4L2_CID_SATURATION:
2068             {
2069                 ctrl->value = sensor->info_priv.saturation;
2070                 break;
2071             }
2072         case V4L2_CID_CONTRAST:
2073             {
2074                 ctrl->value = sensor->info_priv.contrast;
2075                 break;
2076             }
2077         case V4L2_CID_DO_WHITE_BALANCE:
2078             {
2079                 ctrl->value = sensor->info_priv.whiteBalance;
2080                 break;
2081             }
2082         case V4L2_CID_EXPOSURE:
2083             {
2084                 ctrl->value = sensor->info_priv.exposure;
2085                 break;
2086             }
2087         case V4L2_CID_HFLIP:
2088             {
2089                 ctrl->value = sensor->info_priv.mirror;
2090                 break;
2091             }
2092         case V4L2_CID_VFLIP:
2093             {
2094                 ctrl->value = sensor->info_priv.flip;
2095                 break;
2096             }
2097         default :
2098                 break;
2099     }
2100     return 0;
2101 }
2102
2103
2104
2105 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2106 {
2107     struct i2c_client *client = sd->priv;
2108     struct sensor *sensor = to_sensor(client);
2109     struct soc_camera_device *icd = client->dev.platform_data;
2110     const struct v4l2_queryctrl *qctrl;
2111
2112
2113     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2114
2115     if (!qctrl)
2116     {
2117         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2118         return -EINVAL;
2119     }
2120
2121     switch (ctrl->id)
2122     {
2123 #if CONFIG_SENSOR_Brightness
2124         case V4L2_CID_BRIGHTNESS:
2125             {
2126                 if (ctrl->value != sensor->info_priv.brightness)
2127                 {
2128                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2129                     {
2130                         return -EINVAL;
2131                     }
2132                     sensor->info_priv.brightness = ctrl->value;
2133                 }
2134                 break;
2135             }
2136 #endif
2137 #if CONFIG_SENSOR_Exposure
2138         case V4L2_CID_EXPOSURE:
2139             {
2140                 if (ctrl->value != sensor->info_priv.exposure)
2141                 {
2142                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2143                     {
2144                         return -EINVAL;
2145                     }
2146                     sensor->info_priv.exposure = ctrl->value;
2147                 }
2148                 break;
2149             }
2150 #endif
2151 #if CONFIG_SENSOR_Saturation
2152         case V4L2_CID_SATURATION:
2153             {
2154                 if (ctrl->value != sensor->info_priv.saturation)
2155                 {
2156                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2157                     {
2158                         return -EINVAL;
2159                     }
2160                     sensor->info_priv.saturation = ctrl->value;
2161                 }
2162                 break;
2163             }
2164 #endif
2165 #if CONFIG_SENSOR_Contrast
2166         case V4L2_CID_CONTRAST:
2167             {
2168                 if (ctrl->value != sensor->info_priv.contrast)
2169                 {
2170                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2171                     {
2172                         return -EINVAL;
2173                     }
2174                     sensor->info_priv.contrast = ctrl->value;
2175                 }
2176                 break;
2177             }
2178 #endif
2179 #if CONFIG_SENSOR_WhiteBalance
2180         case V4L2_CID_DO_WHITE_BALANCE:
2181             {
2182                 if (ctrl->value != sensor->info_priv.whiteBalance)
2183                 {
2184                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2185                     {
2186                         return -EINVAL;
2187                     }
2188                     sensor->info_priv.whiteBalance = ctrl->value;
2189                 }
2190                 break;
2191             }
2192 #endif
2193 #if CONFIG_SENSOR_Mirror
2194         case V4L2_CID_HFLIP:
2195             {
2196                 if (ctrl->value != sensor->info_priv.mirror)
2197                 {
2198                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2199                         return -EINVAL;
2200                     sensor->info_priv.mirror = ctrl->value;
2201                 }
2202                 break;
2203             }
2204 #endif
2205 #if CONFIG_SENSOR_Flip
2206         case V4L2_CID_VFLIP:
2207             {
2208                 if (ctrl->value != sensor->info_priv.flip)
2209                 {
2210                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2211                         return -EINVAL;
2212                     sensor->info_priv.flip = ctrl->value;
2213                 }
2214                 break;
2215             }
2216 #endif
2217         default:
2218             break;
2219     }
2220
2221     return 0;
2222 }
2223 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2224 {
2225     const struct v4l2_queryctrl *qctrl;
2226     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2227     struct sensor *sensor = to_sensor(client);
2228
2229     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2230
2231     if (!qctrl)
2232     {
2233         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2234         return -EINVAL;
2235     }
2236
2237     switch (ext_ctrl->id)
2238     {
2239         case V4L2_CID_SCENE:
2240             {
2241                 ext_ctrl->value = sensor->info_priv.scene;
2242                 break;
2243             }
2244         case V4L2_CID_EFFECT:
2245             {
2246                 ext_ctrl->value = sensor->info_priv.effect;
2247                 break;
2248             }
2249         case V4L2_CID_ZOOM_ABSOLUTE:
2250             {
2251                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2252                 break;
2253             }
2254         case V4L2_CID_ZOOM_RELATIVE:
2255             {
2256                 return -EINVAL;
2257             }
2258         case V4L2_CID_FOCUS_ABSOLUTE:
2259             {
2260                 ext_ctrl->value = sensor->info_priv.focus;
2261                 break;
2262             }
2263         case V4L2_CID_FOCUS_RELATIVE:
2264             {
2265                 return -EINVAL;
2266             }
2267         case V4L2_CID_FLASH:
2268             {
2269                 ext_ctrl->value = sensor->info_priv.flash;
2270                 break;
2271             }
2272         default :
2273             break;
2274     }
2275     return 0;
2276 }
2277 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2278 {
2279     const struct v4l2_queryctrl *qctrl;
2280     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2281     struct sensor *sensor = to_sensor(client);
2282     int val_offset;
2283
2284     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2285
2286     if (!qctrl)
2287     {
2288         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2289         return -EINVAL;
2290     }
2291
2292         val_offset = 0;
2293     switch (ext_ctrl->id)
2294     {
2295 #if CONFIG_SENSOR_Scene
2296         case V4L2_CID_SCENE:
2297             {
2298                 if (ext_ctrl->value != sensor->info_priv.scene)
2299                 {
2300                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2301                         return -EINVAL;
2302                     sensor->info_priv.scene = ext_ctrl->value;
2303                 }
2304                 break;
2305             }
2306 #endif
2307 #if CONFIG_SENSOR_Effect
2308         case V4L2_CID_EFFECT:
2309             {
2310                 if (ext_ctrl->value != sensor->info_priv.effect)
2311                 {
2312                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2313                         return -EINVAL;
2314                     sensor->info_priv.effect= ext_ctrl->value;
2315                 }
2316                 break;
2317             }
2318 #endif
2319 #if CONFIG_SENSOR_DigitalZoom
2320         case V4L2_CID_ZOOM_ABSOLUTE:
2321             {
2322                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2323                     return -EINVAL;
2324
2325                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2326                 {
2327                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2328
2329                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2330                         return -EINVAL;
2331                     sensor->info_priv.digitalzoom += val_offset;
2332
2333                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2334                 }
2335
2336                 break;
2337             }
2338         case V4L2_CID_ZOOM_RELATIVE:
2339             {
2340                 if (ext_ctrl->value)
2341                 {
2342                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2343                         return -EINVAL;
2344                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2345
2346                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2347                 }
2348                 break;
2349             }
2350 #endif
2351 #if CONFIG_SENSOR_Focus
2352         case V4L2_CID_FOCUS_ABSOLUTE:
2353             {
2354                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2355                     return -EINVAL;
2356
2357                 if (ext_ctrl->value != sensor->info_priv.focus)
2358                 {
2359                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2360
2361                     sensor->info_priv.focus += val_offset;
2362                 }
2363
2364                 break;
2365             }
2366         case V4L2_CID_FOCUS_RELATIVE:
2367             {
2368                 if (ext_ctrl->value)
2369                 {
2370                     sensor->info_priv.focus += ext_ctrl->value;
2371
2372                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2373                 }
2374                 break;
2375             }
2376 #endif
2377 #if CONFIG_SENSOR_Flash
2378         case V4L2_CID_FLASH:
2379             {
2380                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2381                     return -EINVAL;
2382                 sensor->info_priv.flash = ext_ctrl->value;
2383
2384                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2385                 break;
2386             }
2387 #endif
2388         default:
2389             break;
2390     }
2391
2392     return 0;
2393 }
2394
2395 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2396 {
2397     struct i2c_client *client = sd->priv;
2398     struct soc_camera_device *icd = client->dev.platform_data;
2399     int i, error_cnt=0, error_idx=-1;
2400
2401
2402     for (i=0; i<ext_ctrl->count; i++) {
2403         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2404             error_cnt++;
2405             error_idx = i;
2406         }
2407     }
2408
2409     if (error_cnt > 1)
2410         error_idx = ext_ctrl->count;
2411
2412     if (error_idx != -1) {
2413         ext_ctrl->error_idx = error_idx;
2414         return -EINVAL;
2415     } else {
2416         return 0;
2417     }
2418 }
2419
2420 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2421 {
2422     struct i2c_client *client = sd->priv;
2423     struct soc_camera_device *icd = client->dev.platform_data;
2424     int i, error_cnt=0, error_idx=-1;
2425
2426
2427     for (i=0; i<ext_ctrl->count; i++) {
2428         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2429             error_cnt++;
2430             error_idx = i;
2431         }
2432     }
2433
2434     if (error_cnt > 1)
2435         error_idx = ext_ctrl->count;
2436
2437     if (error_idx != -1) {
2438         ext_ctrl->error_idx = error_idx;
2439         return -EINVAL;
2440     } else {
2441         return 0;
2442     }
2443 }
2444
2445 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2446  * this wasn't our capture interface, so, we wait for the right one */
2447 static int sensor_video_probe(struct soc_camera_device *icd,
2448                                struct i2c_client *client)
2449 {
2450     char value;
2451     int ret,pid=0;
2452     struct sensor *sensor = to_sensor(client);
2453
2454     /* We must have a parent by now. And it cannot be a wrong one.
2455      * So this entire test is completely redundant. */
2456     if (!icd->dev.parent ||
2457             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2458                 return -ENODEV;
2459
2460         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2461                 ret = -ENODEV;
2462                 goto sensor_video_probe_err;
2463         }
2464
2465     /* soft reset */
2466     ret = sensor_write(client, 0xfe, 0x80);
2467     if (ret != 0)
2468     {
2469         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2470         return -ENODEV;
2471     }
2472     mdelay(5);          //delay 5 microseconds
2473
2474     /* check if it is an sensor sensor */
2475     ret = sensor_read(client, 0x00, &value);
2476     if (ret != 0) {
2477         SENSOR_TR("read chip id high byte failed\n");
2478         ret = -ENODEV;
2479         goto sensor_video_probe_err;
2480     }
2481
2482     pid |= (value << 8);
2483
2484     ret = sensor_read(client, 0x01, &value);
2485     if (ret != 0) {
2486         SENSOR_TR("read chip id low byte failed\n");
2487         ret = -ENODEV;
2488         goto sensor_video_probe_err;
2489     }
2490
2491     pid |= (value & 0xff);
2492     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2493     if (pid == SENSOR_ID) {
2494         sensor->model = SENSOR_V4L2_IDENT;
2495     } else {
2496         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2497         ret = -ENODEV;
2498         goto sensor_video_probe_err;
2499     }
2500
2501     return 0;
2502
2503 sensor_video_probe_err:
2504
2505     return ret;
2506 }
2507 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2508 {
2509         struct i2c_client *client = sd->priv;
2510     struct soc_camera_device *icd = client->dev.platform_data;
2511     struct sensor *sensor = to_sensor(client);
2512     int ret = 0;
2513 #if CONFIG_SENSOR_Flash 
2514     int i;
2515 #endif
2516     
2517         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2518         switch (cmd)
2519         {
2520                 case RK29_CAM_SUBDEV_DEACTIVATE:
2521                 {
2522                         sensor_deactivate(client);
2523                         break;
2524                 }
2525
2526                 case RK29_CAM_SUBDEV_IOREQUEST:
2527                 {
2528                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2529             if (sensor->sensor_io_request != NULL) { 
2530                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
2531                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
2532                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
2533                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
2534                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
2535                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
2536                 }
2537             } else {
2538                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2539                 ret = -EINVAL;
2540                 goto sensor_ioctl_end;
2541             }
2542             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2543                for this project */
2544             #if CONFIG_SENSOR_Flash     
2545                 if (sensor->sensor_gpio_res) { 
2546                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2547                     for (i = 0; i < icd->ops->num_controls; i++) {
2548                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2549                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2550                                 }
2551                     }
2552                     sensor->info_priv.flash = 0xff;
2553                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2554                 }
2555                 }
2556             #endif
2557                         break;
2558                 }
2559                 default:
2560                 {
2561                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2562                         break;
2563                 }
2564         }
2565 sensor_ioctl_end:
2566         return ret;
2567
2568 }
2569 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2570                             enum v4l2_mbus_pixelcode *code)
2571 {
2572         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2573                 return -EINVAL;
2574
2575         *code = sensor_colour_fmts[index].code;
2576         return 0;
2577 }
2578 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2579         .init           = sensor_init,
2580         .g_ctrl         = sensor_g_control,
2581         .s_ctrl         = sensor_s_control,
2582         .g_ext_ctrls          = sensor_g_ext_controls,
2583         .s_ext_ctrls          = sensor_s_ext_controls,
2584         .g_chip_ident   = sensor_g_chip_ident,
2585         .ioctl = sensor_ioctl,  
2586 };
2587
2588 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2589         .s_mbus_fmt     = sensor_s_fmt,
2590         .g_mbus_fmt     = sensor_g_fmt,
2591         .try_mbus_fmt   = sensor_try_fmt,
2592         .enum_mbus_fmt  = sensor_enum_fmt,
2593 };
2594
2595 static struct v4l2_subdev_ops sensor_subdev_ops = {
2596         .core   = &sensor_subdev_core_ops,
2597         .video = &sensor_subdev_video_ops,
2598 };
2599
2600 static int sensor_probe(struct i2c_client *client,
2601                          const struct i2c_device_id *did)
2602 {
2603     struct sensor *sensor;
2604     struct soc_camera_device *icd = client->dev.platform_data;
2605     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2606     struct soc_camera_link *icl;
2607     int ret;
2608
2609     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2610     if (!icd) {
2611         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2612         return -EINVAL;
2613     }
2614
2615     icl = to_soc_camera_link(icd);
2616     if (!icl) {
2617         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2618         return -EINVAL;
2619     }
2620
2621     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2622         dev_warn(&adapter->dev,
2623                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2624         return -EIO;
2625     }
2626
2627     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2628     if (!sensor)
2629         return -ENOMEM;
2630
2631     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2632
2633     /* Second stage probe - when a capture adapter is there */
2634     icd->ops            = &sensor_ops;
2635
2636     sensor->info_priv.fmt = sensor_colour_fmts[0];
2637     
2638         #if CONFIG_SENSOR_I2C_NOSCHED
2639         atomic_set(&sensor->tasklock_cnt,0);
2640         #endif
2641
2642     ret = sensor_video_probe(icd, client);
2643     if (ret < 0) {
2644         icd->ops = NULL;
2645         i2c_set_clientdata(client, NULL);
2646         kfree(sensor);
2647                 sensor = NULL;
2648     }
2649     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2650     return ret;
2651 }
2652
2653 static int sensor_remove(struct i2c_client *client)
2654 {
2655     struct sensor *sensor = to_sensor(client);
2656     struct soc_camera_device *icd = client->dev.platform_data;
2657
2658     icd->ops = NULL;
2659     i2c_set_clientdata(client, NULL);
2660     client->driver = NULL;
2661     kfree(sensor);
2662         sensor = NULL;
2663     return 0;
2664 }
2665
2666 static const struct i2c_device_id sensor_id[] = {
2667         {SENSOR_NAME_STRING(), 0 },
2668         { }
2669 };
2670 MODULE_DEVICE_TABLE(i2c, sensor_id);
2671
2672 static struct i2c_driver sensor_i2c_driver = {
2673         .driver = {
2674                 .name = SENSOR_NAME_STRING(),
2675         },
2676         .probe          = sensor_probe,
2677         .remove         = sensor_remove,
2678         .id_table       = sensor_id,
2679 };
2680
2681 static int __init sensor_mod_init(void)
2682 {
2683     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2684     return i2c_add_driver(&sensor_i2c_driver);
2685 }
2686
2687 static void __exit sensor_mod_exit(void)
2688 {
2689     i2c_del_driver(&sensor_i2c_driver);
2690 }
2691
2692 device_initcall_sync(sensor_mod_init);
2693 module_exit(sensor_mod_exit);
2694
2695 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2696 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2697 MODULE_LICENSE("GPL");
2698
2699