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