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