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