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