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