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