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