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