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