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