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