camsys_drv : v0.9.0
[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 <plat/rk_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     struct sensor *sensor = to_sensor(client);
1569         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1570
1571         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1572         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1573     msleep(100); 
1574
1575         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1576         icd->user_width = SENSOR_INIT_WIDTH;
1577     icd->user_height = SENSOR_INIT_HEIGHT;
1578     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
1579         
1580         return 0;
1581 }
1582
1583 static  struct reginfo sensor_power_down_sequence[]=
1584 {
1585     {0x00,0x00}
1586 };
1587 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1588 {
1589     int ret;
1590     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1591
1592     if (pm_msg.event == PM_EVENT_SUSPEND) {
1593         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1594         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1595         if (ret != 0) {
1596             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1597             return ret;
1598         } else {
1599             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1600             if (ret < 0) {
1601                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1602                 return -EINVAL;
1603             }
1604         }
1605     } else {
1606         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1607         return -EINVAL;
1608     }
1609     return 0;
1610 }
1611
1612 static int sensor_resume(struct soc_camera_device *icd)
1613 {
1614         int ret;
1615
1616     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1617     if (ret < 0) {
1618                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1619         return -EINVAL;
1620     }
1621
1622         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1623
1624     return 0;
1625
1626 }
1627
1628 static int sensor_set_bus_param(struct soc_camera_device *icd,
1629                                 unsigned long flags)
1630 {
1631
1632     return 0;
1633 }
1634
1635 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1636 {
1637     struct soc_camera_link *icl = to_soc_camera_link(icd);
1638     unsigned long flags = SENSOR_BUS_PARAM;
1639
1640     return soc_camera_apply_sensor_flags(icl, flags);
1641 }
1642
1643 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1644 {
1645     struct i2c_client *client = v4l2_get_subdevdata(sd);
1646     struct soc_camera_device *icd = client->dev.platform_data;
1647     struct sensor *sensor = to_sensor(client);
1648
1649     mf->width   = icd->user_width;
1650         mf->height      = icd->user_height;
1651         mf->code        = sensor->info_priv.fmt.code;
1652         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1653         mf->field       = V4L2_FIELD_NONE;
1654
1655     return 0;
1656 }
1657 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1658 {
1659     bool ret = false;
1660
1661         if ((mf->width == 1024) && (mf->height == 768)) {
1662                 ret = true;
1663         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1664                 ret = true;
1665         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1666                 ret = true;
1667         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1668                 ret = true;
1669         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1670                 ret = true;
1671         }
1672
1673         if (ret == true)
1674                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1675         return ret;
1676 }
1677
1678 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1679 {
1680     bool ret = false;
1681
1682         if ((mf->width == 1280) && (mf->height == 720)) {
1683                 ret = true;
1684         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1685                 ret = true;
1686         }
1687
1688         if (ret == true)
1689                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1690         return ret;
1691 }
1692 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1693 {
1694     struct i2c_client *client = v4l2_get_subdevdata(sd);
1695     const struct sensor_datafmt *fmt;
1696     struct sensor *sensor = to_sensor(client);
1697         const struct v4l2_queryctrl *qctrl;
1698         struct soc_camera_device *icd = client->dev.platform_data;
1699     struct reginfo *winseqe_set_addr=NULL;
1700     int ret=0, set_w,set_h;
1701
1702         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1703                                    ARRAY_SIZE(sensor_colour_fmts));
1704         if (!fmt) {
1705         ret = -EINVAL;
1706         goto sensor_s_fmt_end;
1707     }
1708
1709         if (sensor->info_priv.fmt.code != mf->code) {
1710                 switch (mf->code)
1711                 {
1712                         case V4L2_MBUS_FMT_YUYV8_2X8:
1713                         {
1714                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1715                                 break;
1716                         }
1717                         case V4L2_MBUS_FMT_UYVY8_2X8:
1718                         {
1719                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1720                                 break;
1721                         }
1722                         default:
1723                                 break;
1724                 }
1725                 if (winseqe_set_addr != NULL) {
1726             sensor_write_array(client, winseqe_set_addr);
1727                         sensor->info_priv.fmt.code = mf->code;
1728             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1729                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1730                 } else {
1731                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1732                 }
1733         }
1734
1735     set_w = mf->width;
1736     set_h = mf->height;
1737
1738         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1739         {
1740                 winseqe_set_addr = sensor_qcif;
1741         set_w = 176;
1742         set_h = 144;
1743         }
1744         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1745     {
1746         winseqe_set_addr = sensor_qvga;
1747         set_w = 320;
1748         set_h = 240;
1749     }
1750     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1751     {
1752         winseqe_set_addr = sensor_cif;
1753         set_w = 352;
1754         set_h = 288;
1755     }
1756     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1757     {
1758         winseqe_set_addr = sensor_vga;
1759         set_w = 640;
1760         set_h = 480;
1761     }
1762     
1763     else
1764     {
1765         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
1766         set_w = SENSOR_INIT_WIDTH;
1767         set_h = SENSOR_INIT_HEIGHT;
1768                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
1769     }
1770
1771         if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
1772         #if CONFIG_SENSOR_Flash
1773         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
1774             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1775                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
1776                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
1777             }           
1778         } else {                                        /* ddl@rock-chips.com : Video */
1779             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1780                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1781                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
1782             }
1783         }
1784         #endif
1785                 ret |= sensor_write_array(client, winseqe_set_addr);
1786                 if (ret != 0) {
1787                         SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
1788             #if CONFIG_SENSOR_Flash
1789             if (sensor_fmt_capturechk(sd,mf) == true) {
1790                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
1791                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
1792                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
1793                 }
1794             }
1795             #endif
1796                         goto sensor_s_fmt_end;
1797                 }
1798
1799                 sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
1800
1801                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
1802                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1803                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1804                         if (sensor->info_priv.whiteBalance != 0) {
1805                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1806                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1807                         }
1808                         sensor->info_priv.snap2preview = true;
1809                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
1810                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1811                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1812                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1813                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1814                         sensor->info_priv.video2preview = true;
1815                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
1816                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1817                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
1818                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1819                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
1820                         sensor->info_priv.video2preview = false;
1821                         sensor->info_priv.snap2preview = false;
1822                 }
1823         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
1824     } else {
1825         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
1826     }
1827
1828         mf->width = set_w;
1829     mf->height = set_h;
1830
1831 sensor_s_fmt_end:
1832     return ret;
1833 }
1834
1835 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1836 {
1837     struct i2c_client *client = v4l2_get_subdevdata(sd);
1838     struct sensor *sensor = to_sensor(client);
1839     const struct sensor_datafmt *fmt;
1840     int ret = 0,set_w,set_h;
1841    
1842         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1843                                    ARRAY_SIZE(sensor_colour_fmts));
1844         if (fmt == NULL) {
1845                 fmt = &sensor->info_priv.fmt;
1846         mf->code = fmt->code;
1847         } 
1848
1849     if (mf->height > SENSOR_MAX_HEIGHT)
1850         mf->height = SENSOR_MAX_HEIGHT;
1851     else if (mf->height < SENSOR_MIN_HEIGHT)
1852         mf->height = SENSOR_MIN_HEIGHT;
1853
1854     if (mf->width > SENSOR_MAX_WIDTH)
1855         mf->width = SENSOR_MAX_WIDTH;
1856     else if (mf->width < SENSOR_MIN_WIDTH)
1857         mf->width = SENSOR_MIN_WIDTH;
1858
1859     set_w = mf->width;
1860     set_h = mf->height;
1861
1862         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
1863         {
1864         set_w = 176;
1865         set_h = 144;
1866         }
1867         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
1868     {
1869         set_w = 320;
1870         set_h = 240;
1871     }
1872     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
1873     {
1874         set_w = 352;
1875         set_h = 288;
1876     }
1877     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
1878     {
1879         set_w = 640;
1880         set_h = 480;
1881     }
1882     
1883     else
1884     {
1885         set_w = SENSOR_INIT_WIDTH;
1886         set_h = SENSOR_INIT_HEIGHT;
1887     }    
1888     mf->width = set_w;
1889     mf->height = set_h; 
1890     mf->colorspace = fmt->colorspace;
1891     
1892     return ret;
1893 }
1894
1895  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
1896 {
1897     struct i2c_client *client = v4l2_get_subdevdata(sd);
1898
1899     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1900         return -EINVAL;
1901
1902     if (id->match.addr != client->addr)
1903         return -ENODEV;
1904
1905     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
1906     id->revision = 0;
1907
1908     return 0;
1909 }
1910 #if CONFIG_SENSOR_Brightness
1911 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1912 {
1913     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1914
1915     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1916     {
1917         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
1918         {
1919             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
1920             {
1921                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1922                 return -EINVAL;
1923             }
1924             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1925             return 0;
1926         }
1927     }
1928         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1929     return -EINVAL;
1930 }
1931 #endif
1932 #if CONFIG_SENSOR_Effect
1933 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1934 {
1935     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1936
1937     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1938     {
1939         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
1940         {
1941             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
1942             {
1943                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1944                 return -EINVAL;
1945             }
1946             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1947             return 0;
1948         }
1949     }
1950         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1951     return -EINVAL;
1952 }
1953 #endif
1954 #if CONFIG_SENSOR_Exposure
1955 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1956 {
1957     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1958
1959     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1960     {
1961         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
1962         {
1963             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
1964             {
1965                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1966                 return -EINVAL;
1967             }
1968             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1969             return 0;
1970         }
1971     }
1972         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1973     return -EINVAL;
1974 }
1975 #endif
1976 #if CONFIG_SENSOR_Saturation
1977 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
1978 {
1979     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1980
1981     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
1982     {
1983         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
1984         {
1985             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
1986             {
1987                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
1988                 return -EINVAL;
1989             }
1990             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
1991             return 0;
1992         }
1993     }
1994     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
1995     return -EINVAL;
1996 }
1997 #endif
1998 #if CONFIG_SENSOR_Contrast
1999 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2000 {
2001     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2002
2003     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2004     {
2005         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2006         {
2007             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2008             {
2009                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2010                 return -EINVAL;
2011             }
2012             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2013             return 0;
2014         }
2015     }
2016     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2017     return -EINVAL;
2018 }
2019 #endif
2020 #if CONFIG_SENSOR_Mirror
2021 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2022 {
2023     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2024
2025     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2026     {
2027         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2028         {
2029             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2030             {
2031                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2032                 return -EINVAL;
2033             }
2034             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2035             return 0;
2036         }
2037     }
2038     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2039     return -EINVAL;
2040 }
2041 #endif
2042 #if CONFIG_SENSOR_Flip
2043 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2044 {
2045     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2046
2047     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2048     {
2049         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2050         {
2051             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2052             {
2053                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2054                 return -EINVAL;
2055             }
2056             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2057             return 0;
2058         }
2059     }
2060     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2061     return -EINVAL;
2062 }
2063 #endif
2064 #if CONFIG_SENSOR_Scene
2065 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2066 {
2067     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2068
2069     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2070     {
2071         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2072         {
2073             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2074             {
2075                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2076                 return -EINVAL;
2077             }
2078             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2079             return 0;
2080         }
2081     }
2082     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2083     return -EINVAL;
2084 }
2085 #endif
2086 #if CONFIG_SENSOR_WhiteBalance
2087 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2088 {
2089     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2090
2091     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2092     {
2093         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2094         {
2095             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2096             {
2097                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2098                 return -EINVAL;
2099             }
2100             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2101             return 0;
2102         }
2103     }
2104         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2105     return -EINVAL;
2106 }
2107 #endif
2108 #if CONFIG_SENSOR_DigitalZoom
2109 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2110 {
2111     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2112     struct sensor *sensor = to_sensor(client);
2113         const struct v4l2_queryctrl *qctrl_info;
2114     int digitalzoom_cur, digitalzoom_total;
2115
2116         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2117         if (qctrl_info)
2118                 return -EINVAL;
2119
2120     digitalzoom_cur = sensor->info_priv.digitalzoom;
2121     digitalzoom_total = qctrl_info->maximum;
2122
2123     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2124     {
2125         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2126         return -EINVAL;
2127     }
2128
2129     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2130     {
2131         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2132         return -EINVAL;
2133     }
2134
2135     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2136     {
2137         value = digitalzoom_total - digitalzoom_cur;
2138     }
2139
2140     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2141     {
2142         value = 0 - digitalzoom_cur;
2143     }
2144
2145     digitalzoom_cur += value;
2146
2147     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2148     {
2149         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2150         {
2151             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2152             return -EINVAL;
2153         }
2154         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2155         return 0;
2156     }
2157
2158     return -EINVAL;
2159 }
2160 #endif
2161 #if CONFIG_SENSOR_Flash
2162 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2163 {    
2164     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2165         if (value == 3) {       /* ddl@rock-chips.com: torch */
2166             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2167         } else {
2168             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2169         }
2170         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2171         return 0;
2172     }
2173     
2174         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2175     return -EINVAL;
2176 }
2177 #endif
2178
2179 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2180 {
2181     struct i2c_client *client = v4l2_get_subdevdata(sd);
2182     struct sensor *sensor = to_sensor(client);
2183     const struct v4l2_queryctrl *qctrl;
2184
2185     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2186
2187     if (!qctrl)
2188     {
2189         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2190         return -EINVAL;
2191     }
2192
2193     switch (ctrl->id)
2194     {
2195         case V4L2_CID_BRIGHTNESS:
2196             {
2197                 ctrl->value = sensor->info_priv.brightness;
2198                 break;
2199             }
2200         case V4L2_CID_SATURATION:
2201             {
2202                 ctrl->value = sensor->info_priv.saturation;
2203                 break;
2204             }
2205         case V4L2_CID_CONTRAST:
2206             {
2207                 ctrl->value = sensor->info_priv.contrast;
2208                 break;
2209             }
2210         case V4L2_CID_DO_WHITE_BALANCE:
2211             {
2212                 ctrl->value = sensor->info_priv.whiteBalance;
2213                 break;
2214             }
2215         case V4L2_CID_EXPOSURE:
2216             {
2217                 ctrl->value = sensor->info_priv.exposure;
2218                 break;
2219             }
2220         case V4L2_CID_HFLIP:
2221             {
2222                 ctrl->value = sensor->info_priv.mirror;
2223                 break;
2224             }
2225         case V4L2_CID_VFLIP:
2226             {
2227                 ctrl->value = sensor->info_priv.flip;
2228                 break;
2229             }
2230         default :
2231                 break;
2232     }
2233     return 0;
2234 }
2235
2236
2237
2238 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2239 {
2240     struct i2c_client *client = v4l2_get_subdevdata(sd);
2241     struct sensor *sensor = to_sensor(client);
2242     struct soc_camera_device *icd = client->dev.platform_data;
2243     const struct v4l2_queryctrl *qctrl;
2244
2245
2246     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2247
2248     if (!qctrl)
2249     {
2250         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2251         return -EINVAL;
2252     }
2253
2254     switch (ctrl->id)
2255     {
2256 #if CONFIG_SENSOR_Brightness
2257         case V4L2_CID_BRIGHTNESS:
2258             {
2259                 if (ctrl->value != sensor->info_priv.brightness)
2260                 {
2261                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2262                     {
2263                         return -EINVAL;
2264                     }
2265                     sensor->info_priv.brightness = ctrl->value;
2266                 }
2267                 break;
2268             }
2269 #endif
2270 #if CONFIG_SENSOR_Exposure
2271         case V4L2_CID_EXPOSURE:
2272             {
2273                 if (ctrl->value != sensor->info_priv.exposure)
2274                 {
2275                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2276                     {
2277                         return -EINVAL;
2278                     }
2279                     sensor->info_priv.exposure = ctrl->value;
2280                 }
2281                 break;
2282             }
2283 #endif
2284 #if CONFIG_SENSOR_Saturation
2285         case V4L2_CID_SATURATION:
2286             {
2287                 if (ctrl->value != sensor->info_priv.saturation)
2288                 {
2289                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2290                     {
2291                         return -EINVAL;
2292                     }
2293                     sensor->info_priv.saturation = ctrl->value;
2294                 }
2295                 break;
2296             }
2297 #endif
2298 #if CONFIG_SENSOR_Contrast
2299         case V4L2_CID_CONTRAST:
2300             {
2301                 if (ctrl->value != sensor->info_priv.contrast)
2302                 {
2303                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2304                     {
2305                         return -EINVAL;
2306                     }
2307                     sensor->info_priv.contrast = ctrl->value;
2308                 }
2309                 break;
2310             }
2311 #endif
2312 #if CONFIG_SENSOR_WhiteBalance
2313         case V4L2_CID_DO_WHITE_BALANCE:
2314             {
2315                 if (ctrl->value != sensor->info_priv.whiteBalance)
2316                 {
2317                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2318                     {
2319                         return -EINVAL;
2320                     }
2321                     sensor->info_priv.whiteBalance = ctrl->value;
2322                 }
2323                 break;
2324             }
2325 #endif
2326 #if CONFIG_SENSOR_Mirror
2327         case V4L2_CID_HFLIP:
2328             {
2329                 if (ctrl->value != sensor->info_priv.mirror)
2330                 {
2331                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2332                         return -EINVAL;
2333                     sensor->info_priv.mirror = ctrl->value;
2334                 }
2335                 break;
2336             }
2337 #endif
2338 #if CONFIG_SENSOR_Flip
2339         case V4L2_CID_VFLIP:
2340             {
2341                 if (ctrl->value != sensor->info_priv.flip)
2342                 {
2343                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2344                         return -EINVAL;
2345                     sensor->info_priv.flip = ctrl->value;
2346                 }
2347                 break;
2348             }
2349 #endif
2350         default:
2351             break;
2352     }
2353
2354     return 0;
2355 }
2356 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2357 {
2358     const struct v4l2_queryctrl *qctrl;
2359     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2360     struct sensor *sensor = to_sensor(client);
2361
2362     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2363
2364     if (!qctrl)
2365     {
2366         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2367         return -EINVAL;
2368     }
2369
2370     switch (ext_ctrl->id)
2371     {
2372         case V4L2_CID_SCENE:
2373             {
2374                 ext_ctrl->value = sensor->info_priv.scene;
2375                 break;
2376             }
2377         case V4L2_CID_EFFECT:
2378             {
2379                 ext_ctrl->value = sensor->info_priv.effect;
2380                 break;
2381             }
2382         case V4L2_CID_ZOOM_ABSOLUTE:
2383             {
2384                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2385                 break;
2386             }
2387         case V4L2_CID_ZOOM_RELATIVE:
2388             {
2389                 return -EINVAL;
2390             }
2391         case V4L2_CID_FOCUS_ABSOLUTE:
2392             {
2393                 ext_ctrl->value = sensor->info_priv.focus;
2394                 break;
2395             }
2396         case V4L2_CID_FOCUS_RELATIVE:
2397             {
2398                 return -EINVAL;
2399             }
2400         case V4L2_CID_FLASH:
2401             {
2402                 ext_ctrl->value = sensor->info_priv.flash;
2403                 break;
2404             }
2405         default :
2406             break;
2407     }
2408     return 0;
2409 }
2410 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2411 {
2412     const struct v4l2_queryctrl *qctrl;
2413     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2414     struct sensor *sensor = to_sensor(client);
2415     int val_offset;
2416
2417     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2418
2419     if (!qctrl)
2420     {
2421         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2422         return -EINVAL;
2423     }
2424
2425         val_offset = 0;
2426     switch (ext_ctrl->id)
2427     {
2428 #if CONFIG_SENSOR_Scene
2429         case V4L2_CID_SCENE:
2430             {
2431                 if (ext_ctrl->value != sensor->info_priv.scene)
2432                 {
2433                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2434                         return -EINVAL;
2435                     sensor->info_priv.scene = ext_ctrl->value;
2436                 }
2437                 break;
2438             }
2439 #endif
2440 #if CONFIG_SENSOR_Effect
2441         case V4L2_CID_EFFECT:
2442             {
2443                 if (ext_ctrl->value != sensor->info_priv.effect)
2444                 {
2445                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2446                         return -EINVAL;
2447                     sensor->info_priv.effect= ext_ctrl->value;
2448                 }
2449                 break;
2450             }
2451 #endif
2452 #if CONFIG_SENSOR_DigitalZoom
2453         case V4L2_CID_ZOOM_ABSOLUTE:
2454             {
2455                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2456                     return -EINVAL;
2457
2458                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2459                 {
2460                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2461
2462                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2463                         return -EINVAL;
2464                     sensor->info_priv.digitalzoom += val_offset;
2465
2466                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2467                 }
2468
2469                 break;
2470             }
2471         case V4L2_CID_ZOOM_RELATIVE:
2472             {
2473                 if (ext_ctrl->value)
2474                 {
2475                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2476                         return -EINVAL;
2477                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2478
2479                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2480                 }
2481                 break;
2482             }
2483 #endif
2484 #if CONFIG_SENSOR_Focus
2485         case V4L2_CID_FOCUS_ABSOLUTE:
2486             {
2487                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2488                     return -EINVAL;
2489
2490                 if (ext_ctrl->value != sensor->info_priv.focus)
2491                 {
2492                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2493
2494                     sensor->info_priv.focus += val_offset;
2495                 }
2496
2497                 break;
2498             }
2499         case V4L2_CID_FOCUS_RELATIVE:
2500             {
2501                 if (ext_ctrl->value)
2502                 {
2503                     sensor->info_priv.focus += ext_ctrl->value;
2504
2505                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2506                 }
2507                 break;
2508             }
2509 #endif
2510 #if CONFIG_SENSOR_Flash
2511         case V4L2_CID_FLASH:
2512             {
2513                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2514                     return -EINVAL;
2515                 sensor->info_priv.flash = ext_ctrl->value;
2516
2517                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2518                 break;
2519             }
2520 #endif
2521         default:
2522             break;
2523     }
2524
2525     return 0;
2526 }
2527
2528 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2529 {
2530     struct i2c_client *client = v4l2_get_subdevdata(sd);
2531     struct soc_camera_device *icd = client->dev.platform_data;
2532     int i, error_cnt=0, error_idx=-1;
2533
2534
2535     for (i=0; i<ext_ctrl->count; i++) {
2536         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2537             error_cnt++;
2538             error_idx = i;
2539         }
2540     }
2541
2542     if (error_cnt > 1)
2543         error_idx = ext_ctrl->count;
2544
2545     if (error_idx != -1) {
2546         ext_ctrl->error_idx = error_idx;
2547         return -EINVAL;
2548     } else {
2549         return 0;
2550     }
2551 }
2552
2553 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2554 {
2555     struct i2c_client *client = v4l2_get_subdevdata(sd);
2556     struct soc_camera_device *icd = client->dev.platform_data;
2557     int i, error_cnt=0, error_idx=-1;
2558
2559
2560     for (i=0; i<ext_ctrl->count; i++) {
2561         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2562             error_cnt++;
2563             error_idx = i;
2564         }
2565     }
2566
2567     if (error_cnt > 1)
2568         error_idx = ext_ctrl->count;
2569
2570     if (error_idx != -1) {
2571         ext_ctrl->error_idx = error_idx;
2572         return -EINVAL;
2573     } else {
2574         return 0;
2575     }
2576 }
2577
2578 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2579  * this wasn't our capture interface, so, we wait for the right one */
2580 static int sensor_video_probe(struct soc_camera_device *icd,
2581                                struct i2c_client *client)
2582 {
2583     char value;
2584     int ret;
2585     struct sensor *sensor = to_sensor(client);
2586
2587     /* We must have a parent by now. And it cannot be a wrong one.
2588      * So this entire test is completely redundant. */
2589     if (!icd->dev.parent ||
2590             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2591                 return -ENODEV;
2592
2593         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2594                 ret = -ENODEV;
2595                 goto sensor_video_probe_err;
2596         }
2597
2598     /* soft reset */
2599     ret = sensor_write(client, 0xfe, 0x80);
2600     if (ret != 0)
2601     {
2602         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2603         return -ENODEV;
2604     }
2605     mdelay(5);          //delay 5 microseconds
2606
2607     /* check if it is an sensor sensor */
2608     ret = sensor_read(client, 0x00, &value);
2609     if (ret != 0) {
2610         SENSOR_TR("read chip id high byte failed\n");
2611         ret = -ENODEV;
2612         goto sensor_video_probe_err;
2613     }
2614
2615     if (value == SENSOR_ID) {
2616         sensor->model = SENSOR_V4L2_IDENT;
2617                 SENSOR_TR("chip id:0x%x\n",value);
2618     } else {
2619         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), value);
2620         ret = -ENODEV;
2621         goto sensor_video_probe_err;
2622     }
2623
2624     return 0;
2625
2626 sensor_video_probe_err:
2627
2628     return ret;
2629 }
2630 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2631 {
2632         struct i2c_client *client = v4l2_get_subdevdata(sd);
2633     struct soc_camera_device *icd = client->dev.platform_data;
2634     struct sensor *sensor = to_sensor(client);
2635     int ret = 0;
2636 #if CONFIG_SENSOR_Flash 
2637     int i;
2638 #endif
2639     
2640         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2641         switch (cmd)
2642         {
2643                 case RK29_CAM_SUBDEV_DEACTIVATE:
2644                 {
2645                         sensor_deactivate(client);
2646                         break;
2647                 }
2648
2649                 case RK29_CAM_SUBDEV_IOREQUEST:
2650                 {
2651                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2652             if (sensor->sensor_io_request != NULL) { 
2653                 sensor->sensor_gpio_res = NULL;
2654                                 #if CONFIG_SENSOR_Flash 
2655                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2656                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2657                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2658                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2659                     }
2660                 }
2661                                 #endif
2662                 if (sensor->sensor_gpio_res == NULL) {
2663                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2664                     ret = -EINVAL;
2665                     goto sensor_ioctl_end;
2666                 }
2667             } else {
2668                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2669                 ret = -EINVAL;
2670                 goto sensor_ioctl_end;
2671             }
2672             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2673                for this project */
2674             #if CONFIG_SENSOR_Flash     
2675                 if (sensor->sensor_gpio_res) { 
2676                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2677                     for (i = 0; i < icd->ops->num_controls; i++) {
2678                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2679                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2680                                 }
2681                     }
2682                     sensor->info_priv.flash = 0xff;
2683                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2684                 }
2685                 }
2686             #endif
2687                         break;
2688                 }
2689                 default:
2690                 {
2691                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2692                         break;
2693                 }
2694         }
2695 sensor_ioctl_end:
2696         return ret;
2697
2698 }
2699 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2700                             enum v4l2_mbus_pixelcode *code)
2701 {
2702         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2703                 return -EINVAL;
2704
2705         *code = sensor_colour_fmts[index].code;
2706         return 0;
2707 }
2708 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2709         .init           = sensor_init,
2710         .g_ctrl         = sensor_g_control,
2711         .s_ctrl         = sensor_s_control,
2712         .g_ext_ctrls          = sensor_g_ext_controls,
2713         .s_ext_ctrls          = sensor_s_ext_controls,
2714         .g_chip_ident   = sensor_g_chip_ident,
2715         .ioctl = sensor_ioctl,
2716 };
2717
2718 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2719         .s_mbus_fmt     = sensor_s_fmt,
2720         .g_mbus_fmt     = sensor_g_fmt,
2721         .try_mbus_fmt   = sensor_try_fmt,
2722         .enum_mbus_fmt  = sensor_enum_fmt,
2723 };
2724
2725 static struct v4l2_subdev_ops sensor_subdev_ops = {
2726         .core   = &sensor_subdev_core_ops,
2727         .video = &sensor_subdev_video_ops,
2728 };
2729
2730 static int sensor_probe(struct i2c_client *client,
2731                          const struct i2c_device_id *did)
2732 {
2733     struct sensor *sensor;
2734     struct soc_camera_device *icd = client->dev.platform_data;
2735     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2736     struct soc_camera_link *icl;
2737     int ret;
2738
2739     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2740     if (!icd) {
2741         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2742         return -EINVAL;
2743     }
2744
2745     icl = to_soc_camera_link(icd);
2746     if (!icl) {
2747         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2748         return -EINVAL;
2749     }
2750
2751     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2752         dev_warn(&adapter->dev,
2753                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2754         return -EIO;
2755     }
2756
2757     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2758     if (!sensor)
2759         return -ENOMEM;
2760
2761     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2762
2763     /* Second stage probe - when a capture adapter is there */
2764     icd->ops            = &sensor_ops;
2765
2766     sensor->info_priv.fmt = sensor_colour_fmts[0];
2767     
2768         #if CONFIG_SENSOR_I2C_NOSCHED
2769         atomic_set(&sensor->tasklock_cnt,0);
2770         #endif
2771
2772     ret = sensor_video_probe(icd, client);
2773     if (ret < 0) {
2774         icd->ops = NULL;
2775         i2c_set_clientdata(client, NULL);
2776         kfree(sensor);
2777                 sensor = NULL;
2778     }
2779     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2780     return ret;
2781 }
2782
2783 static int sensor_remove(struct i2c_client *client)
2784 {
2785     struct sensor *sensor = to_sensor(client);
2786     struct soc_camera_device *icd = client->dev.platform_data;
2787
2788     icd->ops = NULL;
2789     i2c_set_clientdata(client, NULL);
2790     client->driver = NULL;
2791     kfree(sensor);
2792         sensor = NULL;
2793     return 0;
2794 }
2795
2796 static const struct i2c_device_id sensor_id[] = {
2797         {SENSOR_NAME_STRING(), 0 },
2798         { }
2799 };
2800 MODULE_DEVICE_TABLE(i2c, sensor_id);
2801
2802 static struct i2c_driver sensor_i2c_driver = {
2803         .driver = {
2804                 .name = SENSOR_NAME_STRING(),
2805         },
2806         .probe          = sensor_probe,
2807         .remove         = sensor_remove,
2808         .id_table       = sensor_id,
2809 };
2810
2811 static int __init sensor_mod_init(void)
2812 {
2813     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2814     return i2c_add_driver(&sensor_i2c_driver);
2815 }
2816
2817 static void __exit sensor_mod_exit(void)
2818 {
2819     i2c_del_driver(&sensor_i2c_driver);
2820 }
2821
2822 device_initcall_sync(sensor_mod_init);
2823 module_exit(sensor_mod_exit);
2824
2825 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2826 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2827 MODULE_LICENSE("GPL");
2828
2829
2830