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