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