camera rk30:commit v0.2.6,support two cif controls.
[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 <plat/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 RK29_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 /* init 640X480 VGA */
102 static struct reginfo sensor_init_data[] =
103 {
104
105     {0xfe , 0x80},      
106                 
107         {0xfe , 0x00},   // set page0
108         
109         {0xd2 , 0x10},   // close AEC
110         {0x22 , 0x55},   // close AWB
111
112         {0x03 , 0x01},                                  
113         {0x04 , 0x2c},                                  
114         {0x5a , 0x56},
115         {0x5b , 0x40},
116         {0x5c , 0x4a},                  
117
118         {0x22 , 0x57},   // Open AWB
119
120         {0x01 , 0xfa},                                    
121         {0x02 , 0x70},                                  
122         {0x0f , 0x01},                                  
123                                                                    
124                                                                    
125         {0xe2 , 0x00},   //anti-flicker step [11:8]     
126         {0xe3 , 0x64},   //anti-flicker step [7:0]      
127                                                                                
128         {0xe4 , 0x02},   //exp level 1  16.67fps        
129         {0xe5 , 0x58},                                  
130         {0xe6 , 0x03},   //exp level 2  12.5fps         
131         {0xe7 , 0x20},                                  
132         {0xe8 , 0x04},   //exp level 3  8.33fps         
133         {0xe9 , 0xb0},                                  
134         {0xea , 0x09},   //exp level 4  4.00fps         
135         {0xeb , 0xc4},                                  
136
137         //{0xec , 0x20},
138            
139         {0x05 , 0x00},                                  
140         {0x06 , 0x00},                                  
141         {0x07 , 0x00},                                  
142         {0x08 , 0x00},                                  
143         {0x09 , 0x01},                                  
144         {0x0a , 0xe8},                                  
145         {0x0b , 0x02},                                  
146         {0x0c , 0x88},                                  
147         {0x0d , 0x02},                                  
148         {0x0e , 0x02},                                  
149         {0x10 , 0x22},                                  
150         {0x11 , 0xfd},                                  
151         {0x12 , 0x2a},                                  
152         {0x13 , 0x00},                                  
153         //{0x14 , 0x10},                                
154         {0x15 , 0x0a},                                  
155         {0x16 , 0x05},                                  
156         {0x17 , 0x01},                                  
157         {0x18 , 0x44},                                  
158         {0x19 , 0x44},                                  
159         {0x1a , 0x1e},                                  
160         {0x1b , 0x00},                                  
161         {0x1c , 0xc1},                                  
162         {0x1d , 0x08},                                  
163         {0x1e , 0x60},                                  
164         {0x1f , 0x17},                                  
165                                                                    
166                                                                          
167         {0x20 , 0xff},                                  
168         {0x21 , 0xf8},                                  
169         {0x22 , 0x57},                                  
170         {0x24 , 0xa2},                                  
171         {0x25 , 0x0f},                                  
172                                                                          
173         //output sync_mode                                               
174         {0x26 , 0x02},   //0x03  20101016 zhj                                 
175         {0x2f , 0x01},                                  
176         {0x30 , 0xf7},                                  
177         {0x31 , 0x50},
178         {0x32 , 0x00},
179         {0x39 , 0x04},
180         {0x3a , 0x18},
181         {0x3b , 0x20},                                  
182         {0x3c , 0x00},                                  
183         {0x3d , 0x00},                                  
184         {0x3e , 0x00},                                  
185         {0x3f , 0x00},                                  
186         {0x50 , 0x10},                                  
187         {0x53 , 0x82},                                  
188         {0x54 , 0x80},                                  
189         {0x55 , 0x80},                                  
190         {0x56 , 0x82},                                  
191         {0x8b , 0x40},                                  
192         {0x8c , 0x40},                                  
193         {0x8d , 0x40},                                  
194         {0x8e , 0x2e},                                  
195         {0x8f , 0x2e},                                  
196         {0x90 , 0x2e},                                  
197         {0x91 , 0x3c},                                  
198         {0x92 , 0x50},                                  
199         {0x5d , 0x12},                                  
200         {0x5e , 0x1a},                                  
201         {0x5f , 0x24},                                  
202         {0x60 , 0x07},                                  
203         {0x61 , 0x15},                                  
204         {0x62 , 0x08},                                  
205         {0x64 , 0x03},                                  
206         {0x66 , 0xe8},                                  
207         {0x67 , 0x86},                                  
208         {0x68 , 0xa2},                                  
209         {0x69 , 0x18},                                  
210         {0x6a , 0x0f},                                  
211         {0x6b , 0x00},                                  
212         {0x6c , 0x5f},                                  
213         {0x6d , 0x8f},                                  
214         {0x6e , 0x55},                                  
215         {0x6f , 0x38},                                  
216         {0x70 , 0x15},                                  
217         {0x71 , 0x33},                                  
218         {0x72 , 0xdc},                                  
219         {0x73 , 0x80},                                  
220         {0x74 , 0x02},                                  
221         {0x75 , 0x3f},                                  
222         {0x76 , 0x02},                                  
223         {0x77 , 0x36},                                  
224         {0x78 , 0x88},                                  
225         {0x79 , 0x81},                                  
226         {0x7a , 0x81},                                  
227         {0x7b , 0x22},                                  
228         {0x7c , 0xff},                                  
229         {0x93 , 0x48},                                  
230         {0x94 , 0x00},                                  
231         {0x95 , 0x05},                                  
232         {0x96 , 0xe8},                                  
233         {0x97 , 0x40},                                  
234         {0x98 , 0xf0},                                  
235         {0xb1 , 0x38},                                  
236         {0xb2 , 0x38},                                  
237         {0xbd , 0x38},                                  
238         {0xbe , 0x36},                                  
239         {0xd0 , 0xc9},                                  
240         {0xd1 , 0x10},                                  
241         //{0xd2 , 0x90},                                
242         {0xd3 , 0x80},                                  
243         {0xd5 , 0xf2},                                  
244         {0xd6 , 0x16},                                  
245         {0xdb , 0x92},                                  
246         {0xdc , 0xa5},                                  
247         {0xdf , 0x23},                                  
248         {0xd9 , 0x00},                                  
249         {0xda , 0x00},                                  
250         {0xe0 , 0x09},                                  
251                                
252         {0xed , 0x04},                                  
253         {0xee , 0xa0},                                  
254         {0xef , 0x40},                                  
255         {0x80 , 0x03},                                  
256         {0x80 , 0x03},                                  
257         {0x9F , 0x10},                                  
258         {0xA0 , 0x20},                                  
259         {0xA1 , 0x38},                                  
260         {0xA2 , 0x4E},                                  
261         {0xA3 , 0x63},                                  
262         {0xA4 , 0x76},                                  
263         {0xA5 , 0x87},                                  
264         {0xA6 , 0xA2},                                  
265         {0xA7 , 0xB8},                                  
266         {0xA8 , 0xCA},                                  
267         {0xA9 , 0xD8},                                  
268         {0xAA , 0xE3},                                  
269         {0xAB , 0xEB},                                  
270         {0xAC , 0xF0},                                  
271         {0xAD , 0xF8},                                  
272         {0xAE , 0xFD},                                  
273         {0xAF , 0xFF},                                  
274         {0xc0 , 0x00},                                  
275         {0xc1 , 0x10},                                  
276         {0xc2 , 0x1C},                                  
277         {0xc3 , 0x30},                                  
278         {0xc4 , 0x43},                                  
279         {0xc5 , 0x54},                                  
280         {0xc6 , 0x65},                                  
281         {0xc7 , 0x75},                                  
282         {0xc8 , 0x93},                                  
283         {0xc9 , 0xB0},                                  
284         {0xca , 0xCB},                                  
285         {0xcb , 0xE6},                                  
286         {0xcc , 0xFF},                                  
287         {0xf0 , 0x02},                                  
288         {0xf1 , 0x01},                                  
289         {0xf2 , 0x01},                                  
290         {0xf3 , 0x30},                                  
291         {0xf9 , 0x9f},                                  
292         {0xfa , 0x78},                                  
293                                                                    
294         //---------------------------------------------------------------
295         {0xfe , 0x01},// set page1                                            
296                                                                    
297         {0x00 , 0xf5},                                  
298         {0x02 , 0x1a},                                  
299         {0x0a , 0xa0},                                  
300         {0x0b , 0x60},                                  
301         {0x0c , 0x08},                                  
302         {0x0e , 0x4c},                                  
303         {0x0f , 0x39},                                  
304         {0x11 , 0x3f},                                  
305         {0x12 , 0x72},                                  
306         {0x13 , 0x13},                                  
307         {0x14 , 0x42},                                  
308         {0x15 , 0x43},                                  
309         {0x16 , 0xc2},                                  
310         {0x17 , 0xa8},                                  
311         {0x18 , 0x18},                                  
312         {0x19 , 0x40},                                  
313         {0x1a , 0xd0},                                  
314         {0x1b , 0xf5},                                  
315         {0x70 , 0x40},                                  
316         {0x71 , 0x58},                                  
317         {0x72 , 0x30},                                  
318         {0x73 , 0x48},                                  
319         {0x74 , 0x20},                                  
320         {0x75 , 0x60},                                  
321         {0x77 , 0x20},                                  
322         {0x78 , 0x32},                                  
323         {0x30 , 0x03},                                  
324         {0x31 , 0x40},                                  
325         {0x32 , 0xe0},                                  
326         {0x33 , 0xe0},                                  
327         {0x34 , 0xe0},                                  
328         {0x35 , 0xb0},                                  
329         {0x36 , 0xc0},                                  
330         {0x37 , 0xc0},                                  
331         {0x38 , 0x04},                                  
332         {0x39 , 0x09},                                  
333         {0x3a , 0x12},                                  
334         {0x3b , 0x1C},                                  
335         {0x3c , 0x28},                                  
336         {0x3d , 0x31},                                  
337         {0x3e , 0x44},                                  
338         {0x3f , 0x57},                                  
339         {0x40 , 0x6C},                                  
340         {0x41 , 0x81},                                  
341         {0x42 , 0x94},                                  
342         {0x43 , 0xA7},                                  
343         {0x44 , 0xB8},                                  
344         {0x45 , 0xD6},                                  
345         {0x46 , 0xEE},                                  
346         {0x47 , 0x0d},                                  
347         {0xfe , 0x00}, // set page0
348          
349         //-----------Update the registers 2010/07/06-------------//
350         //Registers of Page0
351         {0xfe , 0x00}, // set page0
352         {0x10 , 0x26},                                 
353         {0x11 , 0x0d},  // fd,modified by mormo 2010/07/06                               
354         {0x1a , 0x2a},  // 1e,modified by mormo 2010/07/06                                  
355
356         {0x1c , 0x49}, // c1,modified by mormo 2010/07/06                                 
357         {0x1d , 0x9a}, // 08,modified by mormo 2010/07/06                                 
358         {0x1e , 0x61}, // 60,modified by mormo 2010/07/06                                 
359
360         {0x3a , 0x20},
361
362         {0x50 , 0x14},  // 10,modified by mormo 2010/07/06                               
363         {0x53 , 0x80},                                  
364         {0x56 , 0x80},
365         
366         {0x8b , 0x20}, //LSC                                 
367         {0x8c , 0x20},                                  
368         {0x8d , 0x20},                                  
369         {0x8e , 0x14},                                  
370         {0x8f , 0x10},                                  
371         {0x90 , 0x14},                                  
372
373         {0x94 , 0x02},                                  
374         {0x95 , 0x07},                                  
375         {0x96 , 0xe0},                                  
376
377         {0xb1 , 0x40}, // YCPT                                 
378         {0xb2 , 0x40},                                  
379         {0xb3 , 0x40},
380         {0xb6 , 0xe0},
381
382         {0xd0 , 0xcb}, // AECT  c9,modifed by mormo 2010/07/06                                
383         {0xd3 , 0x48}, // 80,modified by mormor 2010/07/06                           
384
385         {0xf2 , 0x02},                                  
386         {0xf7 , 0x12},
387         {0xf8 , 0x0a},
388
389         //Registers of Page1
390         {0xfe , 0x01},// set page1    
391         {0x02 , 0x20},
392         {0x04 , 0x10},
393         {0x05 , 0x08},
394         {0x06 , 0x20},
395         {0x08 , 0x0a},
396
397         {0x0e , 0x44},                                  
398         {0x0f , 0x32},
399         {0x10 , 0x41},                                  
400         {0x11 , 0x37},                                  
401         {0x12 , 0x22},                                  
402         {0x13 , 0x19},                                  
403         {0x14 , 0x44},                                  
404         {0x15 , 0x44},  
405         
406         {0x19 , 0x50},                                  
407         {0x1a , 0xd8}, 
408         
409         {0x32 , 0x10}, 
410         
411         {0x35 , 0x00},                                  
412         {0x36 , 0x80},                                  
413         {0x37 , 0x00}, 
414         //-----------Update the registers end---------//
415
416
417         {0xfe , 0x00}, // set page0
418         {0xd2 , 0x90},
419
420
421         //-----------GAMMA Select(3)---------------//
422         {0x9F , 0x10},
423         {0xA0 , 0x20},
424         {0xA1 , 0x38},
425         {0xA2 , 0x4E},
426         {0xA3 , 0x63},
427         {0xA4 , 0x76},
428         {0xA5 , 0x87},
429         {0xA6 , 0xA2},
430         {0xA7 , 0xB8},
431         {0xA8 , 0xCA},
432         {0xA9 , 0xD8},
433         {0xAA , 0xE3},
434         {0xAB , 0xEB},
435         {0xAC , 0xF0},
436         {0xAD , 0xF8},
437         {0xAE , 0xFD},
438         {0xAF , 0xFF},
439
440          /*GC0308_GAMMA_Select,
441                 1:                                             //smallest gamma curve
442                         {0x9F , 0x0B},
443                         {0xA0 , 0x16},
444                         {0xA1 , 0x29},
445                         {0xA2 , 0x3C},
446                         {0xA3 , 0x4F},
447                         {0xA4 , 0x5F},
448                         {0xA5 , 0x6F},
449                         {0xA6 , 0x8A},
450                         {0xA7 , 0x9F},
451                         {0xA8 , 0xB4}, 
452                         {0xA9 , 0xC6},
453                         {0xAA , 0xD3},
454                         {0xAB , 0xDD},
455                         {0xAC , 0xE5},
456                         {0xAD , 0xF1},
457                         {0xAE , 0xFA},
458                         {0xAF , 0xFF},  
459                         
460                 2:                      
461                         {0x9F , 0x0E},
462                         {0xA0 , 0x1C},
463                         {0xA1 , 0x34},
464                         {0xA2 , 0x48},
465                         {0xA3 , 0x5A},
466                         {0xA4 , 0x6B},
467                         {0xA5 , 0x7B},
468                         {0xA6 , 0x95},
469                         {0xA7 , 0xAB},
470                         {0xA8 , 0xBF},
471                         {0xA9 , 0xCE},
472                         {0xAA , 0xD9},
473                         {0xAB , 0xE4},
474                         {0xAC , 0xEC},
475                         {0xAD , 0xF7},
476                         {0xAE , 0xFD},
477                         {0xAF , 0xFF},
478                         
479                 3:
480                         {0x9F , 0x10},
481                         {0xA0 , 0x20},
482                         {0xA1 , 0x38},
483                         {0xA2 , 0x4E},
484                         {0xA3 , 0x63},
485                         {0xA4 , 0x76},
486                         {0xA5 , 0x87},
487                         {0xA6 , 0xA2},
488                         {0xA7 , 0xB8},
489                         {0xA8 , 0xCA},
490                         {0xA9 , 0xD8},
491                         {0xAA , 0xE3},
492                         {0xAB , 0xEB},
493                         {0xAC , 0xF0},
494                         {0xAD , 0xF8},
495                         {0xAE , 0xFD},
496                         {0xAF , 0xFF},
497
498                 4:
499                         {0x9F , 0x14},
500                         {0xA0 , 0x28},
501                         {0xA1 , 0x44},
502                         {0xA2 , 0x5D},
503                         {0xA3 , 0x72},
504                         {0xA4 , 0x86},
505                         {0xA5 , 0x95},
506                         {0xA6 , 0xB1},
507                         {0xA7 , 0xC6},
508                         {0xA8 , 0xD5},
509                         {0xA9 , 0xE1},
510                         {0xAA , 0xEA},
511                         {0xAB , 0xF1},
512                         {0xAC , 0xF5},
513                         {0xAD , 0xFB},
514                         {0xAE , 0xFE},
515                         {0xAF , 0xFF},
516                         
517                 5:                                                              //largest gamma curve
518                         {0x9F , 0x15},
519                         {0xA0 , 0x2A},
520                         {0xA1 , 0x4A},
521                         {0xA2 , 0x67},
522                         {0xA3 , 0x79},
523                         {0xA4 , 0x8C},
524                         {0xA5 , 0x9A},
525                         {0xA6 , 0xB3},
526                         {0xA7 , 0xC5},
527                         {0xA8 , 0xD5},
528                         {0xA9 , 0xDF},
529                         {0xAA , 0xE8},
530                         {0xAB , 0xEE},
531                         {0xAC , 0xF3},
532                         {0xAD , 0xFA},
533                         {0xAE , 0xFD},
534                         {0xAF , 0xFF}, */
535         //-----------GAMMA Select End--------------//
536
537
538
539
540         //-------------H_V_Switch(4)---------------//
541                         {0x14 , 0x12},  //0x10
542
543          /*GC0308_H_V_Switch,
544
545                 1:  // normal
546                         {0x14 , 0x10},
547                         
548                 2:  // IMAGE_H_MIRROR
549                         {0x14 , 0x11},
550                         
551                 3:  // IMAGE_V_MIRROR
552                         {0x14 , 0x12},
553                         
554                 4:  // IMAGE_HV_MIRROR
555                         {0x14 , 0x13},
556         */                  
557         //-------------H_V_Select End--------------//
558
559 };
560
561 /* 1280X1024 SXGA */
562 static struct reginfo sensor_sxga[] =
563 {
564         {0x00,0x00}
565 };
566
567 /* 800X600 SVGA*/
568 static struct reginfo sensor_svga[] =
569 {
570     {0x0, 0x0},
571 };
572
573 /* 640X480 VGA */
574 static struct reginfo sensor_vga[] =
575 {
576     {0x17, 0x13},
577     {0x18, 0x01},
578     {0x32, 0xbf},
579     {0x19, 0x03},
580     {0x1a, 0x7b},
581     {0x03, 0x0a},
582         {0x00,0x00}
583 };
584
585 /* 352X288 CIF */
586 static struct reginfo sensor_cif[] =
587 {
588     {0x00,0x00}
589 };
590
591 /* 320*240 QVGA */
592 static  struct reginfo sensor_qvga[] =
593 {
594     {0x00,0x00}
595 };
596
597 /* 176X144 QCIF*/
598 static struct reginfo sensor_qcif[] =
599 {
600     {0x00,0x00}
601 };
602
603 static  struct reginfo sensor_ClrFmt_YUYV[]=
604 {
605     {0x00, 0x00}
606 };
607
608 static  struct reginfo sensor_ClrFmt_UYVY[]=
609 {
610     {0x00, 0x00}
611 };
612
613
614 #if CONFIG_SENSOR_WhiteBalance
615 static  struct reginfo sensor_WhiteB_Auto[]=
616 {
617         {0x5a, 0x4c}, 
618         {0x5b, 0x40},
619         {0x5c, 0x4a},
620         {0x22, 0x57},
621         {0x00, 0x00}
622 };
623 /* Cloudy Colour Temperature : 6500K - 8000K  */
624 static  struct reginfo sensor_WhiteB_Cloudy[]=
625 {
626         {0x22, 0x55},   // Disable AWB 
627         {0x5a, 0x5a},
628         {0x5b, 0x42},
629         {0x5c, 0x40},
630     {0x00, 0x00}
631 };
632 /* ClearDay Colour Temperature : 5000K - 6500K  */
633 static  struct reginfo sensor_WhiteB_ClearDay[]=
634 {
635     //Sunny
636         {0x22, 0x55},   // Disable AWB 
637     {0x5a, 0x50},
638     {0x5b, 0x45},
639     {0x5c, 0x40},
640     {0x00, 0x00}
641 };
642 /* Office Colour Temperature : 3500K - 5000K  */
643 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
644 {
645     //Office
646         {0x22, 0x55},    // Disable AWB 
647         {0x5a, 0x48},
648         {0x5b, 0x40},
649         {0x5c, 0x5c},
650     {0x00, 0x00}
651
652 };
653 /* Home Colour Temperature : 2500K - 3500K  */
654 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
655 {
656     //Home
657         {0x22, 0x55},   // Disable AWB 
658         {0x5a, 0x40},
659         {0x5b, 0x42}, 
660         {0x5c, 0x50},
661     {0x00, 0x00}
662 };
663 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
664     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
665 };
666 #endif
667
668 #if CONFIG_SENSOR_Brightness
669 static  struct reginfo sensor_Brightness0[]=
670 {
671     // Brightness -2
672     {0xb5, 0xe0},
673     {0x00, 0x00}
674 };
675
676 static  struct reginfo sensor_Brightness1[]=
677 {
678     // Brightness -1
679     {0xb5, 0xf0},
680     {0x00, 0x00}
681 };
682
683 static  struct reginfo sensor_Brightness2[]=
684 {
685     //  Brightness 0
686     {0xb5, 0x00},
687     {0x00, 0x00}
688 };
689
690 static  struct reginfo sensor_Brightness3[]=
691 {
692     // Brightness +1
693     {0xb5, 0x20},
694     {0x00, 0x00}
695 };
696
697 static  struct reginfo sensor_Brightness4[]=
698 {
699     //  Brightness +2
700     {0xb5, 0x30},
701     {0x00, 0x00}
702 };
703
704 static  struct reginfo sensor_Brightness5[]=
705 {
706     //  Brightness +3
707     {0xb5, 0x40},
708     {0x00, 0x00}
709 };
710 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
711     sensor_Brightness4, sensor_Brightness5,NULL,
712 };
713
714 #endif
715
716 #if CONFIG_SENSOR_Effect
717 static  struct reginfo sensor_Effect_Normal[] =
718 {
719         {0x23,0x00}, 
720         {0x2d,0x0a}, 
721         {0x20,0x7f}, 
722         {0xd2,0x90}, 
723         {0x73,0x00}, 
724         {0x77,0x38},
725         {0xb3,0x40}, 
726         {0xb4,0x80}, 
727         {0xba,0x00}, 
728         {0xbb,0x00},
729     {0x00,0x00}
730 };
731
732 static  struct reginfo sensor_Effect_WandB[] =
733 {
734         {0x23,0x02}, 
735         {0x2d,0x0a}, 
736         {0x20,0x7f}, 
737         {0xd2,0x90}, 
738         {0x73,0x00}, 
739         {0xb3,0x40},
740         {0xb4,0x80}, 
741         {0xba,0x00}, 
742         {0xbb,0x00}, 
743     {0x00,0x00}
744 };
745
746 static  struct reginfo sensor_Effect_Sepia[] =
747 {
748         {0x23,0x02},
749         {0x2d,0x0a},
750         {0x20,0x7f},
751         {0xd2,0x90},
752         {0x73,0x00},
753         {0xb3,0x40},
754         {0xb4,0x80},
755         {0xba,0xd2},
756         {0xbb,0x28},
757     {0x00,0x00}
758 };
759
760 static  struct reginfo sensor_Effect_Negative[] =
761 {
762     //Negative
763         {0x23,0x01},
764         {0x2d,0x0a},
765         {0x20,0x7f},
766         {0xd2,0x90},
767         {0x73,0x00},
768         {0xb3,0x40},
769         {0xb4,0x80},
770         {0xba,0x00},
771         {0xbb,0x00},
772     {0x00,0x00}
773 };
774 static  struct reginfo sensor_Effect_Bluish[] =
775 {
776     // Bluish
777     {0x23,0x02},
778     {0x2d,0x0a},
779     {0x20,0x7f},
780     {0xd2,0x90},
781     {0x73,0x00},
782         {0xb3,0x40},
783         {0xb4,0x80},
784         {0xba,0x50},
785         {0xbb,0xe0},
786     {0x00,0x00}
787 };
788
789 static  struct reginfo sensor_Effect_Green[] =
790 {
791     //  Greenish
792     {0x23,0x02},
793     {0x2d,0x0a},
794     {0x20,0x7f},
795     {0xd2,0x90},
796     {0x77,0x88},
797         {0xb3,0x40},
798         {0xb4,0x80},
799         {0xba,0xc0},
800         {0xbb,0xc0},
801     {0x00,0x00}
802 };
803 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
804     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
805 };
806 #endif
807 #if CONFIG_SENSOR_Exposure
808 static  struct reginfo sensor_Exposure0[]=
809 {
810         {0xd3, 0x30},
811     {0x00, 0x00}
812 };
813
814 static  struct reginfo sensor_Exposure1[]=
815 {
816         {0xd3, 0x38},
817     {0x00, 0x00}
818 };
819
820 static  struct reginfo sensor_Exposure2[]=
821 {
822         {0xd3, 0x40},
823     {0x00, 0x00}
824 };
825
826 static  struct reginfo sensor_Exposure3[]=
827 {
828         {0xd3, 0x48},
829     {0x00, 0x00}
830 };
831
832 static  struct reginfo sensor_Exposure4[]=
833 {
834         {0xd3, 0x50},
835     {0x00, 0x00}
836 };
837
838 static  struct reginfo sensor_Exposure5[]=
839 {
840         {0xd3, 0x58},
841     {0x00, 0x00}
842 };
843
844 static  struct reginfo sensor_Exposure6[]=
845 {
846         {0xd3, 0x60},
847     {0x00, 0x00}
848 };
849
850 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
851     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
852 };
853 #endif
854 #if CONFIG_SENSOR_Saturation
855 static  struct reginfo sensor_Saturation0[]=
856 {
857     {0x00, 0x00}
858 };
859
860 static  struct reginfo sensor_Saturation1[]=
861 {
862     {0x00, 0x00}
863 };
864
865 static  struct reginfo sensor_Saturation2[]=
866 {
867     {0x00, 0x00}
868 };
869 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
870
871 #endif
872 #if CONFIG_SENSOR_Contrast
873 static  struct reginfo sensor_Contrast0[]=
874 {
875         {0xb3,0x34},
876     {0x00, 0x00}
877 };
878
879 static  struct reginfo sensor_Contrast1[]=
880 {
881         {0xb3,0x38},
882     {0x00, 0x00}
883 };
884
885 static  struct reginfo sensor_Contrast2[]=
886 {
887         {0xb3,0x3d},
888     {0x00, 0x00}
889 };
890
891 static  struct reginfo sensor_Contrast3[]=
892 {
893         {0xb3,0x40},
894     {0x00, 0x00}
895 };
896
897 static  struct reginfo sensor_Contrast4[]=
898 {
899         {0xb3,0x44},
900     {0x00, 0x00}
901 };
902
903
904 static  struct reginfo sensor_Contrast5[]=
905 {
906         {0xb3,0x48},
907     {0x00, 0x00}
908 };
909
910 static  struct reginfo sensor_Contrast6[]=
911 {
912         {0xb3,0x50},
913     {0x00, 0x00}
914 };
915 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
916     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
917 };
918
919 #endif
920 #if CONFIG_SENSOR_Mirror
921 static  struct reginfo sensor_MirrorOn[]=
922 {
923     {0x00, 0x00}
924 };
925
926 static  struct reginfo sensor_MirrorOff[]=
927 {
928     {0x00, 0x00}
929 };
930 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
931 #endif
932 #if CONFIG_SENSOR_Flip
933 static  struct reginfo sensor_FlipOn[]=
934 {
935     {0x00, 0x00}
936 };
937
938 static  struct reginfo sensor_FlipOff[]=
939 {
940     {0x00, 0x00}
941 };
942 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
943
944 #endif
945 #if CONFIG_SENSOR_Scene
946 static  struct reginfo sensor_SceneAuto[] =
947 {
948         {0xec, 0x00},
949         {0x00, 0x00}
950 };
951
952 static  struct reginfo sensor_SceneNight[] =
953 {
954         {0xec, 0x30},
955         {0x00, 0x00}
956 };
957 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
958
959 #endif
960 #if CONFIG_SENSOR_DigitalZoom
961 static struct reginfo sensor_Zoom0[] =
962 {
963     {0x0, 0x0},
964 };
965
966 static struct reginfo sensor_Zoom1[] =
967 {
968      {0x0, 0x0},
969 };
970
971 static struct reginfo sensor_Zoom2[] =
972 {
973     {0x0, 0x0},
974 };
975
976
977 static struct reginfo sensor_Zoom3[] =
978 {
979     {0x0, 0x0},
980 };
981 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
982 #endif
983 static const struct v4l2_querymenu sensor_menus[] =
984 {
985         #if CONFIG_SENSOR_WhiteBalance
986     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
987     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
988     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
989     #endif
990
991         #if CONFIG_SENSOR_Effect
992     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
993     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
994     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
995     #endif
996
997         #if CONFIG_SENSOR_Scene
998     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
999     #endif
1000
1001         #if CONFIG_SENSOR_Flash
1002     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1003     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1004     #endif
1005 };
1006
1007 static const struct v4l2_queryctrl sensor_controls[] =
1008 {
1009         #if CONFIG_SENSOR_WhiteBalance
1010     {
1011         .id             = V4L2_CID_DO_WHITE_BALANCE,
1012         .type           = V4L2_CTRL_TYPE_MENU,
1013         .name           = "White Balance Control",
1014         .minimum        = 0,
1015         .maximum        = 4,
1016         .step           = 1,
1017         .default_value = 0,
1018     },
1019     #endif
1020
1021         #if CONFIG_SENSOR_Brightness
1022         {
1023         .id             = V4L2_CID_BRIGHTNESS,
1024         .type           = V4L2_CTRL_TYPE_INTEGER,
1025         .name           = "Brightness Control",
1026         .minimum        = -3,
1027         .maximum        = 2,
1028         .step           = 1,
1029         .default_value = 0,
1030     },
1031     #endif
1032
1033         #if CONFIG_SENSOR_Effect
1034         {
1035         .id             = V4L2_CID_EFFECT,
1036         .type           = V4L2_CTRL_TYPE_MENU,
1037         .name           = "Effect Control",
1038         .minimum        = 0,
1039         .maximum        = 5,
1040         .step           = 1,
1041         .default_value = 0,
1042     },
1043         #endif
1044
1045         #if CONFIG_SENSOR_Exposure
1046         {
1047         .id             = V4L2_CID_EXPOSURE,
1048         .type           = V4L2_CTRL_TYPE_INTEGER,
1049         .name           = "Exposure Control",
1050         .minimum        = 0,
1051         .maximum        = 6,
1052         .step           = 1,
1053         .default_value = 0,
1054     },
1055         #endif
1056
1057         #if CONFIG_SENSOR_Saturation
1058         {
1059         .id             = V4L2_CID_SATURATION,
1060         .type           = V4L2_CTRL_TYPE_INTEGER,
1061         .name           = "Saturation Control",
1062         .minimum        = 0,
1063         .maximum        = 2,
1064         .step           = 1,
1065         .default_value = 0,
1066     },
1067     #endif
1068
1069         #if CONFIG_SENSOR_Contrast
1070         {
1071         .id             = V4L2_CID_CONTRAST,
1072         .type           = V4L2_CTRL_TYPE_INTEGER,
1073         .name           = "Contrast Control",
1074         .minimum        = -3,
1075         .maximum        = 3,
1076         .step           = 1,
1077         .default_value = 0,
1078     },
1079         #endif
1080
1081         #if CONFIG_SENSOR_Mirror
1082         {
1083         .id             = V4L2_CID_HFLIP,
1084         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1085         .name           = "Mirror Control",
1086         .minimum        = 0,
1087         .maximum        = 1,
1088         .step           = 1,
1089         .default_value = 1,
1090     },
1091     #endif
1092
1093         #if CONFIG_SENSOR_Flip
1094         {
1095         .id             = V4L2_CID_VFLIP,
1096         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1097         .name           = "Flip Control",
1098         .minimum        = 0,
1099         .maximum        = 1,
1100         .step           = 1,
1101         .default_value = 1,
1102     },
1103     #endif
1104
1105         #if CONFIG_SENSOR_Scene
1106     {
1107         .id             = V4L2_CID_SCENE,
1108         .type           = V4L2_CTRL_TYPE_MENU,
1109         .name           = "Scene Control",
1110         .minimum        = 0,
1111         .maximum        = 1,
1112         .step           = 1,
1113         .default_value = 0,
1114     },
1115     #endif
1116
1117         #if CONFIG_SENSOR_DigitalZoom
1118     {
1119         .id             = V4L2_CID_ZOOM_RELATIVE,
1120         .type           = V4L2_CTRL_TYPE_INTEGER,
1121         .name           = "DigitalZoom Control",
1122         .minimum        = -1,
1123         .maximum        = 1,
1124         .step           = 1,
1125         .default_value = 0,
1126     }, {
1127         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1128         .type           = V4L2_CTRL_TYPE_INTEGER,
1129         .name           = "DigitalZoom Control",
1130         .minimum        = 0,
1131         .maximum        = 3,
1132         .step           = 1,
1133         .default_value = 0,
1134     },
1135     #endif
1136
1137         #if CONFIG_SENSOR_Focus
1138         {
1139         .id             = V4L2_CID_FOCUS_RELATIVE,
1140         .type           = V4L2_CTRL_TYPE_INTEGER,
1141         .name           = "Focus Control",
1142         .minimum        = -1,
1143         .maximum        = 1,
1144         .step           = 1,
1145         .default_value = 0,
1146     }, {
1147         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1148         .type           = V4L2_CTRL_TYPE_INTEGER,
1149         .name           = "Focus Control",
1150         .minimum        = 0,
1151         .maximum        = 255,
1152         .step           = 1,
1153         .default_value = 125,
1154     },
1155     #endif
1156
1157         #if CONFIG_SENSOR_Flash
1158         {
1159         .id             = V4L2_CID_FLASH,
1160         .type           = V4L2_CTRL_TYPE_MENU,
1161         .name           = "Flash Control",
1162         .minimum        = 0,
1163         .maximum        = 3,
1164         .step           = 1,
1165         .default_value = 0,
1166     },
1167         #endif
1168 };
1169
1170 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1171 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1172 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1173 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1174 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1175 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1176 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1177 static int sensor_resume(struct soc_camera_device *icd);
1178 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1179 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1180 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1181 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1182 static int sensor_deactivate(struct i2c_client *client);
1183
1184 static struct soc_camera_ops sensor_ops =
1185 {
1186     .suspend                     = sensor_suspend,
1187     .resume                       = sensor_resume,
1188     .set_bus_param              = sensor_set_bus_param,
1189     .query_bus_param    = sensor_query_bus_param,
1190     .controls           = sensor_controls,
1191     .menus                         = sensor_menus,
1192     .num_controls               = ARRAY_SIZE(sensor_controls),
1193     .num_menus          = ARRAY_SIZE(sensor_menus),
1194 };
1195
1196 /* only one fixed colorspace per pixelcode */
1197 struct sensor_datafmt {
1198         enum v4l2_mbus_pixelcode code;
1199         enum v4l2_colorspace colorspace;
1200 };
1201
1202 /* Find a data format by a pixel code in an array */
1203 static const struct sensor_datafmt *sensor_find_datafmt(
1204         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1205         int n)
1206 {
1207         int i;
1208         for (i = 0; i < n; i++)
1209                 if (fmt[i].code == code)
1210                         return fmt + i;
1211
1212         return NULL;
1213 }
1214
1215 static const struct sensor_datafmt sensor_colour_fmts[] = {
1216     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1217 };
1218
1219 typedef struct sensor_info_priv_s
1220 {
1221     int whiteBalance;
1222     int brightness;
1223     int contrast;
1224     int saturation;
1225     int effect;
1226     int scene;
1227     int digitalzoom;
1228     int focus;
1229     int flash;
1230     int exposure;
1231         bool snap2preview;
1232         bool video2preview;
1233     unsigned char mirror;                                        /* HFLIP */
1234     unsigned char flip;                                          /* VFLIP */
1235     unsigned int winseqe_cur_addr;
1236     struct sensor_datafmt fmt;
1237     unsigned int funmodule_state;
1238 } sensor_info_priv_t;
1239
1240 struct sensor
1241 {
1242     struct v4l2_subdev subdev;
1243     struct i2c_client *client;
1244     sensor_info_priv_t info_priv;
1245     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1246 #if CONFIG_SENSOR_I2C_NOSCHED
1247         atomic_t tasklock_cnt;
1248 #endif
1249         struct rk29camera_platform_data *sensor_io_request;
1250     struct rk29camera_gpio_res *sensor_gpio_res;
1251 };
1252
1253 static struct sensor* to_sensor(const struct i2c_client *client)
1254 {
1255     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1256 }
1257
1258 static int sensor_task_lock(struct i2c_client *client, int lock)
1259 {
1260 #if CONFIG_SENSOR_I2C_NOSCHED
1261         int cnt = 3;
1262     struct sensor *sensor = to_sensor(client);
1263
1264         if (lock) {
1265                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1266                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1267                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1268                                 msleep(35);
1269                                 cnt--;
1270                         }
1271                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1272                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1273                                 goto sensor_task_lock_err;
1274                         }
1275                         preempt_disable();
1276                 }
1277
1278                 atomic_add(1, &sensor->tasklock_cnt);
1279         } else {
1280                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1281                         atomic_sub(1, &sensor->tasklock_cnt);
1282
1283                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1284                                 preempt_enable();
1285                 }
1286         }
1287         return 0;
1288 sensor_task_lock_err:
1289         return -1;  
1290 #else
1291     return 0;
1292 #endif
1293
1294 }
1295
1296 /* sensor register write */
1297 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
1298 {
1299     int err,cnt;
1300     u8 buf[2];
1301     struct i2c_msg msg[1];
1302
1303     buf[0] = reg & 0xFF;
1304     buf[1] = val;
1305
1306     msg->addr = client->addr;
1307     msg->flags = client->flags;
1308     msg->buf = buf;
1309     msg->len = sizeof(buf);
1310     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
1311     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1312
1313     cnt = 3;
1314     err = -EAGAIN;
1315
1316     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1317         err = i2c_transfer(client->adapter, msg, 1);
1318
1319         if (err >= 0) {
1320             return 0;
1321         } else {
1322                 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1323                 udelay(10);
1324         }
1325     }
1326
1327     return err;
1328 }
1329
1330 /* sensor register read */
1331 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
1332 {
1333     int err,cnt;
1334     //u8 buf[2];
1335     u8 buf[1];
1336     struct i2c_msg msg[2];
1337
1338     //buf[0] = reg >> 8;
1339     buf[0] = reg;
1340     buf[1] = reg & 0xFF;
1341
1342     msg[0].addr = client->addr;
1343     msg[0].flags = client->flags;
1344     msg[0].buf = buf;
1345     msg[0].len = sizeof(buf);
1346     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1347     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1348
1349     msg[1].addr = client->addr;
1350     msg[1].flags = client->flags|I2C_M_RD;
1351     msg[1].buf = buf;
1352     msg[1].len = 1;
1353     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1354     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1355
1356     cnt = 1;
1357     err = -EAGAIN;
1358     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1359         err = i2c_transfer(client->adapter, msg, 2);
1360
1361         if (err >= 0) {
1362             *val = buf[0];
1363             return 0;
1364         } else {
1365                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1366             udelay(10);
1367         }
1368     }
1369
1370     return err;
1371 }
1372
1373 /* write a array of registers  */
1374 #if 1
1375 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1376 {
1377     int err;
1378     int i = 0;
1379
1380     //for(i=0; i < sizeof(sensor_init_data) / 2;i++)
1381         while((regarray[i].reg != 0) || (regarray[i].val != 0))
1382     {
1383         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1384         if (err != 0)
1385         {
1386             SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1387             return err;
1388         }
1389                 i++;
1390     }
1391     
1392     return 0;
1393 }
1394 #else
1395 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1396 {
1397     int err;
1398     int i = 0;
1399         u8 val_read;
1400     while (regarray[i].reg != 0)
1401     {
1402         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1403         if (err != 0)
1404         {
1405             SENSOR_TR("%s..write failed current i = %d\n", SENSOR_NAME_STRING(),i);
1406             return err;
1407         }
1408                 err = sensor_read(client, regarray[i].reg, &val_read);
1409                 SENSOR_TR("%s..reg[0x%x]=0x%x,0x%x\n", SENSOR_NAME_STRING(),regarray[i].reg, val_read, regarray[i].val);
1410         i++;
1411     }
1412     return 0;
1413 }
1414 #endif
1415
1416 #if CONFIG_SENSOR_I2C_RDWRCHK
1417 static int sensor_check_array(struct i2c_client *client, struct reginfo *regarray)
1418 {
1419   int ret;
1420   int i = 0;
1421   
1422   u8 value;
1423   
1424   SENSOR_DG("%s >>>>>>>>>>>>>>>>>>>>>>\n",__FUNCTION__);
1425   for(i=0;i<sizeof(sensor_init_data) / 2;i++)
1426         {
1427      ret = sensor_read(client,regarray[i].reg,&value);
1428          if(ret !=0)
1429          {
1430           SENSOR_TR("read value failed\n");
1431
1432          }
1433          if(regarray[i].val != value)
1434          {
1435           SENSOR_DG("%s reg[0x%x] check err,writte :0x%x  read:0x%x\n",__FUNCTION__,regarray[i].reg,regarray[i].val,value);
1436          }
1437          
1438   }
1439   
1440         
1441   return 0;
1442 }
1443 #endif
1444 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1445 {
1446         struct soc_camera_link *icl = to_soc_camera_link(icd);
1447         int ret = 0;
1448
1449     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1450         switch (cmd)
1451         {
1452                 case Sensor_PowerDown:
1453                 {
1454                         if (icl->powerdown) {
1455                                 ret = icl->powerdown(icd->pdev, on);
1456                                 if (ret == RK29_CAM_IO_SUCCESS) {
1457                                         if (on == 0) {
1458                                                 mdelay(2);
1459                                                 if (icl->reset)
1460                                                         icl->reset(icd->pdev);
1461                                         }
1462                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1463                                         ret = -ENODEV;
1464                                         goto sensor_power_end;
1465                                 }
1466                         }
1467                         break;
1468                 }
1469                 case Sensor_Flash:
1470                 {
1471                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1472                 struct sensor *sensor = to_sensor(client);
1473
1474                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1475                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1476                         }
1477             break;
1478                 }
1479                 default:
1480                 {
1481                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1482                         break;
1483                 }
1484         }
1485 sensor_power_end:
1486         return ret;
1487 }
1488 static s32 sensor_init_width = 640;
1489 static s32 sensor_init_height = 480;
1490 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);
1491 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
1492 static struct reginfo* sensor_init_data_p = NULL;
1493 static struct reginfo* sensor_init_winseq_p = NULL;
1494 static struct reginfo* sensor_init_winseq_board = NULL;
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 rk29camera_platform_data* tmp_plat_data =(struct rk29camera_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 && (u32)tmp_plat_data != 1) { 
1511                 for(i = 0;i < RK_CAM_NUM;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  && ((u32)tmp_plat_data != 1) &&(i < RK_CAM_NUM) && 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 RK29_CAM_SUBDEV_DEACTIVATE:
2719                 {
2720                         sensor_deactivate(client);
2721                         break;
2722                 }
2723
2724                 case RK29_CAM_SUBDEV_IOREQUEST:
2725                 {
2726                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;                       
2727                         if (sensor->sensor_io_request != NULL) { 
2728                         int j = 0;
2729                         for(j = 0;j < RK_CAM_NUM;j++){
2730                                         if (sensor->sensor_io_request->gpio_res[j].dev_name && 
2731                                                 (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
2732                                                 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
2733                                 break;
2734                                           } 
2735                         }
2736                         if(j == RK_CAM_NUM){
2737                                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2738                                 ret = -EINVAL;
2739                                 goto sensor_ioctl_end;
2740                                 }
2741                                 }
2742             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2743                for this project */
2744             #if CONFIG_SENSOR_Flash     
2745                 if (sensor->sensor_gpio_res) { 
2746                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2747                     for (i = 0; i < icd->ops->num_controls; i++) {
2748                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2749                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2750                                 }
2751                     }
2752                     sensor->info_priv.flash = 0xff;
2753                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2754                 }
2755                 }
2756             #endif
2757                         break;
2758                 }
2759                 default:
2760                 {
2761                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2762                         break;
2763                 }
2764         }
2765 sensor_ioctl_end:
2766         return ret;
2767
2768 }
2769 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2770                             enum v4l2_mbus_pixelcode *code)
2771 {
2772         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2773                 return -EINVAL;
2774
2775         *code = sensor_colour_fmts[index].code;
2776         return 0;
2777 }
2778 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2779         .init           = sensor_init,
2780         .g_ctrl         = sensor_g_control,
2781         .s_ctrl         = sensor_s_control,
2782         .g_ext_ctrls          = sensor_g_ext_controls,
2783         .s_ext_ctrls          = sensor_s_ext_controls,
2784         .g_chip_ident   = sensor_g_chip_ident,
2785         .ioctl = sensor_ioctl,
2786 };
2787
2788 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2789         .s_mbus_fmt     = sensor_s_fmt,
2790         .g_mbus_fmt     = sensor_g_fmt,
2791         .try_mbus_fmt   = sensor_try_fmt,
2792         .enum_mbus_fmt  = sensor_enum_fmt,
2793 };
2794
2795 static struct v4l2_subdev_ops sensor_subdev_ops = {
2796         .core   = &sensor_subdev_core_ops,
2797         .video = &sensor_subdev_video_ops,
2798 };
2799
2800 static int sensor_probe(struct i2c_client *client,
2801                          const struct i2c_device_id *did)
2802 {
2803     struct sensor *sensor;
2804     struct soc_camera_device *icd = client->dev.platform_data;
2805     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2806     struct soc_camera_link *icl;
2807     int ret;
2808
2809     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2810     if (!icd) {
2811         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2812         return -EINVAL;
2813     }
2814
2815     icl = to_soc_camera_link(icd);
2816     if (!icl) {
2817         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2818         return -EINVAL;
2819     }
2820
2821     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2822         dev_warn(&adapter->dev,
2823                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2824         return -EIO;
2825     }
2826
2827     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2828     if (!sensor)
2829         return -ENOMEM;
2830
2831     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2832
2833     /* Second stage probe - when a capture adapter is there */
2834     icd->ops            = &sensor_ops;
2835
2836     sensor->info_priv.fmt = sensor_colour_fmts[0];
2837     
2838         #if CONFIG_SENSOR_I2C_NOSCHED
2839         atomic_set(&sensor->tasklock_cnt,0);
2840         #endif
2841
2842     ret = sensor_video_probe(icd, client);
2843     if (ret < 0) {
2844         icd->ops = NULL;
2845         i2c_set_clientdata(client, NULL);
2846         kfree(sensor);
2847                 sensor = NULL;
2848     }
2849     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
2850     return ret;
2851 }
2852
2853 static int sensor_remove(struct i2c_client *client)
2854 {
2855     struct sensor *sensor = to_sensor(client);
2856     struct soc_camera_device *icd = client->dev.platform_data;
2857
2858     icd->ops = NULL;
2859     i2c_set_clientdata(client, NULL);
2860     client->driver = NULL;
2861     kfree(sensor);
2862         sensor = NULL;
2863     return 0;
2864 }
2865
2866 static const struct i2c_device_id sensor_id[] = {
2867         {SENSOR_NAME_STRING(), 0 },
2868         { }
2869 };
2870 MODULE_DEVICE_TABLE(i2c, sensor_id);
2871
2872 static struct i2c_driver sensor_i2c_driver = {
2873         .driver = {
2874                 .name = SENSOR_NAME_STRING(),
2875         },
2876         .probe          = sensor_probe,
2877         .remove         = sensor_remove,
2878         .id_table       = sensor_id,
2879 };
2880
2881 static int __init sensor_mod_init(void)
2882 {
2883     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
2884     return i2c_add_driver(&sensor_i2c_driver);
2885 }
2886
2887 static void __exit sensor_mod_exit(void)
2888 {
2889     i2c_del_driver(&sensor_i2c_driver);
2890 }
2891
2892 device_initcall_sync(sensor_mod_init);
2893 module_exit(sensor_mod_exit);
2894
2895 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
2896 MODULE_AUTHOR("ddl <kernel@rock-chips>");
2897 MODULE_LICENSE("GPL");
2898
2899