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