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