b31ed13207be8e77ddb3eead0c1affca6306d630
[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 = v4l2_get_subdevdata(sd);
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 = v4l2_get_subdevdata(sd);
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 = v4l2_get_subdevdata(sd);
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 = v4l2_get_subdevdata(sd);
2041     struct sensor *sensor = to_sensor(client);
2042     const struct sensor_datafmt *fmt;
2043     int ret = 0,set_w,set_h;
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     set_w = mf->width;
2063     set_h = mf->height;
2064
2065         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
2066         {
2067         set_w = 176;
2068         set_h = 144;
2069         }
2070         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
2071     {
2072         set_w = 320;
2073         set_h = 240;
2074     }
2075     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
2076     {
2077         set_w = 352;
2078         set_h = 288;
2079     }
2080     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
2081     {
2082         set_w = 640;
2083         set_h = 480;
2084     }
2085     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
2086     {
2087         set_w = 800-32;
2088         set_h = 600;
2089     }
2090         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
2091     {           
2092         set_w = 1024;
2093         set_h = 768;
2094     }
2095     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
2096     {    
2097         set_w = 1280;
2098         set_h = 1024;
2099     }
2100     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
2101     {
2102         set_w = 1600-32;
2103         set_h = 1200;
2104     }
2105     else
2106     {
2107         set_w = SENSOR_INIT_WIDTH;
2108         set_h = SENSOR_INIT_HEIGHT;     
2109     }
2110     mf->width = set_w;
2111     mf->height = set_h;     
2112     mf->colorspace = fmt->colorspace;
2113     
2114     return ret;
2115 }
2116
2117  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2118 {
2119     struct i2c_client *client = v4l2_get_subdevdata(sd);
2120
2121     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2122         return -EINVAL;
2123
2124     if (id->match.addr != client->addr)
2125         return -ENODEV;
2126
2127     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2128     id->revision = 0;
2129
2130     return 0;
2131 }
2132 #if CONFIG_SENSOR_Brightness
2133 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2134 {
2135     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2136
2137     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2138     {
2139         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2140         {
2141             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2142             {
2143                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2144                 return -EINVAL;
2145             }
2146             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2147             return 0;
2148         }
2149     }
2150         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2151     return -EINVAL;
2152 }
2153 #endif
2154 #if CONFIG_SENSOR_Effect
2155 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2156 {
2157     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2158
2159     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2160     {
2161         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2162         {
2163             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2164             {
2165                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2166                 return -EINVAL;
2167             }
2168             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2169             return 0;
2170         }
2171     }
2172         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2173     return -EINVAL;
2174 }
2175 #endif
2176 #if CONFIG_SENSOR_Exposure
2177 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2178 {
2179     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2180
2181     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2182     {
2183         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2184         {
2185             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2186             {
2187                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2188                 return -EINVAL;
2189             }
2190             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2191             return 0;
2192         }
2193     }
2194         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2195     return -EINVAL;
2196 }
2197 #endif
2198 #if CONFIG_SENSOR_Saturation
2199 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2200 {
2201     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2202
2203     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2204     {
2205         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2206         {
2207             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2208             {
2209                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2210                 return -EINVAL;
2211             }
2212             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2213             return 0;
2214         }
2215     }
2216     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2217     return -EINVAL;
2218 }
2219 #endif
2220 #if CONFIG_SENSOR_Contrast
2221 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2222 {
2223     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2224
2225     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2226     {
2227         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2228         {
2229             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2230             {
2231                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2232                 return -EINVAL;
2233             }
2234             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2235             return 0;
2236         }
2237     }
2238     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2239     return -EINVAL;
2240 }
2241 #endif
2242 #if CONFIG_SENSOR_Mirror
2243 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2244 {
2245     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2246
2247     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2248     {
2249         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2250         {
2251             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2252             {
2253                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2254                 return -EINVAL;
2255             }
2256             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2257             return 0;
2258         }
2259     }
2260     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2261     return -EINVAL;
2262 }
2263 #endif
2264 #if CONFIG_SENSOR_Flip
2265 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2266 {
2267     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2268
2269     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2270     {
2271         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2272         {
2273             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2274             {
2275                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2276                 return -EINVAL;
2277             }
2278             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2279             return 0;
2280         }
2281     }
2282     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2283     return -EINVAL;
2284 }
2285 #endif
2286 #if CONFIG_SENSOR_Scene
2287 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2288 {
2289     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2290
2291     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2292     {
2293         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2294         {
2295             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2296             {
2297                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2298                 return -EINVAL;
2299             }
2300             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2301             return 0;
2302         }
2303     }
2304     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2305     return -EINVAL;
2306 }
2307 #endif
2308 #if CONFIG_SENSOR_WhiteBalance
2309 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2310 {
2311     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2312
2313     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2314     {
2315         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2316         {
2317             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2318             {
2319                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2320                 return -EINVAL;
2321             }
2322             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2323             return 0;
2324         }
2325     }
2326         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2327     return -EINVAL;
2328 }
2329 #endif
2330 #if CONFIG_SENSOR_DigitalZoom
2331 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2332 {
2333     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2334     struct sensor *sensor = to_sensor(client);
2335         const struct v4l2_queryctrl *qctrl_info;
2336     int digitalzoom_cur, digitalzoom_total;
2337
2338         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2339         if (qctrl_info)
2340                 return -EINVAL;
2341
2342     digitalzoom_cur = sensor->info_priv.digitalzoom;
2343     digitalzoom_total = qctrl_info->maximum;
2344
2345     if ((value > 0) && (digitalzoom_cur >= digitalzoom_total))
2346     {
2347         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2348         return -EINVAL;
2349     }
2350
2351     if  ((value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2352     {
2353         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2354         return -EINVAL;
2355     }
2356
2357     if ((value > 0) && ((digitalzoom_cur + value) > digitalzoom_total))
2358     {
2359         value = digitalzoom_total - digitalzoom_cur;
2360     }
2361
2362     if ((value < 0) && ((digitalzoom_cur + value) < 0))
2363     {
2364         value = 0 - digitalzoom_cur;
2365     }
2366
2367     digitalzoom_cur += value;
2368
2369     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2370     {
2371         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2372         {
2373             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2374             return -EINVAL;
2375         }
2376         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2377         return 0;
2378     }
2379
2380     return -EINVAL;
2381 }
2382 #endif
2383 #if CONFIG_SENSOR_Flash
2384 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2385 {    
2386     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2387         if (value == 3) {       /* ddl@rock-chips.com: torch */
2388             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2389         } else {
2390             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2391         }
2392         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2393         return 0;
2394     }
2395     
2396         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2397     return -EINVAL;
2398 }
2399 #endif
2400
2401 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2402 {
2403     struct i2c_client *client = v4l2_get_subdevdata(sd);
2404     struct sensor *sensor = to_sensor(client);
2405     const struct v4l2_queryctrl *qctrl;
2406
2407     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2408
2409     if (!qctrl)
2410     {
2411         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2412         return -EINVAL;
2413     }
2414
2415     switch (ctrl->id)
2416     {
2417         case V4L2_CID_BRIGHTNESS:
2418             {
2419                 ctrl->value = sensor->info_priv.brightness;
2420                 break;
2421             }
2422         case V4L2_CID_SATURATION:
2423             {
2424                 ctrl->value = sensor->info_priv.saturation;
2425                 break;
2426             }
2427         case V4L2_CID_CONTRAST:
2428             {
2429                 ctrl->value = sensor->info_priv.contrast;
2430                 break;
2431             }
2432         case V4L2_CID_DO_WHITE_BALANCE:
2433             {
2434                 ctrl->value = sensor->info_priv.whiteBalance;
2435                 break;
2436             }
2437         case V4L2_CID_EXPOSURE:
2438             {
2439                 ctrl->value = sensor->info_priv.exposure;
2440                 break;
2441             }
2442         case V4L2_CID_HFLIP:
2443             {
2444                 ctrl->value = sensor->info_priv.mirror;
2445                 break;
2446             }
2447         case V4L2_CID_VFLIP:
2448             {
2449                 ctrl->value = sensor->info_priv.flip;
2450                 break;
2451             }
2452         default :
2453                 break;
2454     }
2455     return 0;
2456 }
2457
2458
2459
2460 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2461 {
2462     struct i2c_client *client = v4l2_get_subdevdata(sd);
2463     struct sensor *sensor = to_sensor(client);
2464     struct soc_camera_device *icd = client->dev.platform_data;
2465     const struct v4l2_queryctrl *qctrl;
2466
2467
2468     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2469
2470     if (!qctrl)
2471     {
2472         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2473         return -EINVAL;
2474     }
2475
2476     switch (ctrl->id)
2477     {
2478 #if CONFIG_SENSOR_Brightness
2479         case V4L2_CID_BRIGHTNESS:
2480             {
2481                 if (ctrl->value != sensor->info_priv.brightness)
2482                 {
2483                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2484                     {
2485                         return -EINVAL;
2486                     }
2487                     sensor->info_priv.brightness = ctrl->value;
2488                 }
2489                 break;
2490             }
2491 #endif
2492 #if CONFIG_SENSOR_Exposure
2493         case V4L2_CID_EXPOSURE:
2494             {
2495                 if (ctrl->value != sensor->info_priv.exposure)
2496                 {
2497                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2498                     {
2499                         return -EINVAL;
2500                     }
2501                     sensor->info_priv.exposure = ctrl->value;
2502                 }
2503                 break;
2504             }
2505 #endif
2506 #if CONFIG_SENSOR_Saturation
2507         case V4L2_CID_SATURATION:
2508             {
2509                 if (ctrl->value != sensor->info_priv.saturation)
2510                 {
2511                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2512                     {
2513                         return -EINVAL;
2514                     }
2515                     sensor->info_priv.saturation = ctrl->value;
2516                 }
2517                 break;
2518             }
2519 #endif
2520 #if CONFIG_SENSOR_Contrast
2521         case V4L2_CID_CONTRAST:
2522             {
2523                 if (ctrl->value != sensor->info_priv.contrast)
2524                 {
2525                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2526                     {
2527                         return -EINVAL;
2528                     }
2529                     sensor->info_priv.contrast = ctrl->value;
2530                 }
2531                 break;
2532             }
2533 #endif
2534 #if CONFIG_SENSOR_WhiteBalance
2535         case V4L2_CID_DO_WHITE_BALANCE:
2536             {
2537                 if (ctrl->value != sensor->info_priv.whiteBalance)
2538                 {
2539                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2540                     {
2541                         return -EINVAL;
2542                     }
2543                     sensor->info_priv.whiteBalance = ctrl->value;
2544                 }
2545                 break;
2546             }
2547 #endif
2548 #if CONFIG_SENSOR_Mirror
2549         case V4L2_CID_HFLIP:
2550             {
2551                 if (ctrl->value != sensor->info_priv.mirror)
2552                 {
2553                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2554                         return -EINVAL;
2555                     sensor->info_priv.mirror = ctrl->value;
2556                 }
2557                 break;
2558             }
2559 #endif
2560 #if CONFIG_SENSOR_Flip
2561         case V4L2_CID_VFLIP:
2562             {
2563                 if (ctrl->value != sensor->info_priv.flip)
2564                 {
2565                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2566                         return -EINVAL;
2567                     sensor->info_priv.flip = ctrl->value;
2568                 }
2569                 break;
2570             }
2571 #endif
2572         default:
2573             break;
2574     }
2575
2576     return 0;
2577 }
2578 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2579 {
2580     const struct v4l2_queryctrl *qctrl;
2581     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2582     struct sensor *sensor = to_sensor(client);
2583
2584     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2585
2586     if (!qctrl)
2587     {
2588         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2589         return -EINVAL;
2590     }
2591
2592     switch (ext_ctrl->id)
2593     {
2594         case V4L2_CID_SCENE:
2595             {
2596                 ext_ctrl->value = sensor->info_priv.scene;
2597                 break;
2598             }
2599         case V4L2_CID_EFFECT:
2600             {
2601                 ext_ctrl->value = sensor->info_priv.effect;
2602                 break;
2603             }
2604         case V4L2_CID_ZOOM_ABSOLUTE:
2605             {
2606                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2607                 break;
2608             }
2609         case V4L2_CID_ZOOM_RELATIVE:
2610             {
2611                 return -EINVAL;
2612             }
2613         case V4L2_CID_FOCUS_ABSOLUTE:
2614             {
2615                 ext_ctrl->value = sensor->info_priv.focus;
2616                 break;
2617             }
2618         case V4L2_CID_FOCUS_RELATIVE:
2619             {
2620                 return -EINVAL;
2621             }
2622         case V4L2_CID_FLASH:
2623             {
2624                 ext_ctrl->value = sensor->info_priv.flash;
2625                 break;
2626             }
2627         default :
2628             break;
2629     }
2630     return 0;
2631 }
2632 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2633 {
2634     const struct v4l2_queryctrl *qctrl;
2635     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2636     struct sensor *sensor = to_sensor(client);
2637     int val_offset;
2638
2639     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2640
2641     if (!qctrl)
2642     {
2643         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2644         return -EINVAL;
2645     }
2646
2647         val_offset = 0;
2648     switch (ext_ctrl->id)
2649     {
2650 #if CONFIG_SENSOR_Scene
2651         case V4L2_CID_SCENE:
2652             {
2653                 if (ext_ctrl->value != sensor->info_priv.scene)
2654                 {
2655                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2656                         return -EINVAL;
2657                     sensor->info_priv.scene = ext_ctrl->value;
2658                 }
2659                 break;
2660             }
2661 #endif
2662 #if CONFIG_SENSOR_Effect
2663         case V4L2_CID_EFFECT:
2664             {
2665                 if (ext_ctrl->value != sensor->info_priv.effect)
2666                 {
2667                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2668                         return -EINVAL;
2669                     sensor->info_priv.effect= ext_ctrl->value;
2670                 }
2671                 break;
2672             }
2673 #endif
2674 #if CONFIG_SENSOR_DigitalZoom
2675         case V4L2_CID_ZOOM_ABSOLUTE:
2676             {
2677                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2678                     return -EINVAL;
2679
2680                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2681                 {
2682                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2683
2684                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2685                         return -EINVAL;
2686                     sensor->info_priv.digitalzoom += val_offset;
2687
2688                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2689                 }
2690
2691                 break;
2692             }
2693         case V4L2_CID_ZOOM_RELATIVE:
2694             {
2695                 if (ext_ctrl->value)
2696                 {
2697                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2698                         return -EINVAL;
2699                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2700
2701                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2702                 }
2703                 break;
2704             }
2705 #endif
2706 #if CONFIG_SENSOR_Focus
2707         case V4L2_CID_FOCUS_ABSOLUTE:
2708             {
2709                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2710                     return -EINVAL;
2711
2712                 if (ext_ctrl->value != sensor->info_priv.focus)
2713                 {
2714                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
2715
2716                     sensor->info_priv.focus += val_offset;
2717                 }
2718
2719                 break;
2720             }
2721         case V4L2_CID_FOCUS_RELATIVE:
2722             {
2723                 if (ext_ctrl->value)
2724                 {
2725                     sensor->info_priv.focus += ext_ctrl->value;
2726
2727                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
2728                 }
2729                 break;
2730             }
2731 #endif
2732 #if CONFIG_SENSOR_Flash
2733         case V4L2_CID_FLASH:
2734             {
2735                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2736                     return -EINVAL;
2737                 sensor->info_priv.flash = ext_ctrl->value;
2738
2739                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2740                 break;
2741             }
2742 #endif
2743         default:
2744             break;
2745     }
2746
2747     return 0;
2748 }
2749
2750 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2751 {
2752     struct i2c_client *client = v4l2_get_subdevdata(sd);
2753     struct soc_camera_device *icd = client->dev.platform_data;
2754     int i, error_cnt=0, error_idx=-1;
2755
2756
2757     for (i=0; i<ext_ctrl->count; i++) {
2758         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2759             error_cnt++;
2760             error_idx = i;
2761         }
2762     }
2763
2764     if (error_cnt > 1)
2765         error_idx = ext_ctrl->count;
2766
2767     if (error_idx != -1) {
2768         ext_ctrl->error_idx = error_idx;
2769         return -EINVAL;
2770     } else {
2771         return 0;
2772     }
2773 }
2774
2775 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2776 {
2777     struct i2c_client *client = v4l2_get_subdevdata(sd);
2778     struct soc_camera_device *icd = client->dev.platform_data;
2779     int i, error_cnt=0, error_idx=-1;
2780
2781
2782     for (i=0; i<ext_ctrl->count; i++) {
2783         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2784             error_cnt++;
2785             error_idx = i;
2786         }
2787     }
2788
2789     if (error_cnt > 1)
2790         error_idx = ext_ctrl->count;
2791
2792     if (error_idx != -1) {
2793         ext_ctrl->error_idx = error_idx;
2794         return -EINVAL;
2795     } else {
2796         return 0;
2797     }
2798 }
2799
2800 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2801  * this wasn't our capture interface, so, we wait for the right one */
2802 static int sensor_video_probe(struct soc_camera_device *icd,
2803                                struct i2c_client *client)
2804 {
2805     char value;
2806     int ret,pid=0;
2807     struct sensor *sensor = to_sensor(client);
2808
2809     /* We must have a parent by now. And it cannot be a wrong one.
2810      * So this entire test is completely redundant. */
2811     if (!icd->dev.parent ||
2812             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2813                 return -ENODEV;
2814
2815         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2816                 ret = -ENODEV;
2817                 goto sensor_video_probe_err;
2818         }
2819
2820     /* soft reset */
2821     ret = sensor_write(client, 0xfe, 0x80);
2822     if (ret != 0)
2823     {
2824         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
2825         return -ENODEV;
2826     }
2827     mdelay(5);          //delay 5 microseconds
2828
2829     /* check if it is an sensor sensor */
2830     ret = sensor_read(client, 0x00, &value);
2831     if (ret != 0) {
2832         SENSOR_TR("read chip id high byte failed\n");
2833         ret = -ENODEV;
2834         goto sensor_video_probe_err;
2835     }
2836
2837     pid |= (value << 8);
2838
2839     ret = sensor_read(client, 0x01, &value);
2840     if (ret != 0) {
2841         SENSOR_TR("read chip id low byte failed\n");
2842         ret = -ENODEV;
2843         goto sensor_video_probe_err;
2844     }
2845
2846     pid |= (value & 0xff);
2847     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2848     if (pid == SENSOR_ID) {
2849         sensor->model = SENSOR_V4L2_IDENT;
2850     } else {
2851         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2852         ret = -ENODEV;
2853         goto sensor_video_probe_err;
2854     }
2855
2856     return 0;
2857
2858 sensor_video_probe_err:
2859
2860     return ret;
2861 }
2862 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2863 {
2864         struct i2c_client *client = v4l2_get_subdevdata(sd);
2865     struct soc_camera_device *icd = client->dev.platform_data;
2866     struct sensor *sensor = to_sensor(client);
2867     int ret = 0;
2868 #if CONFIG_SENSOR_Flash 
2869     int i;
2870 #endif
2871     
2872         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2873         switch (cmd)
2874         {
2875                 case RK29_CAM_SUBDEV_DEACTIVATE:
2876                 {
2877                         sensor_deactivate(client);
2878                         break;
2879                 }
2880
2881                 case RK29_CAM_SUBDEV_IOREQUEST:
2882                 {
2883                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
2884             if (sensor->sensor_io_request != NULL) { 
2885                 sensor->sensor_gpio_res = NULL;
2886                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
2887                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
2888                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
2889                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
2890                     }
2891                 }
2892                 if (sensor->sensor_gpio_res == NULL) {
2893                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
2894                     ret = -EINVAL;
2895                     goto sensor_ioctl_end;
2896                 }
2897             } else {
2898                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
2899                 ret = -EINVAL;
2900                 goto sensor_ioctl_end;
2901             }
2902             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
2903                for this project */
2904             #if CONFIG_SENSOR_Flash     
2905                 if (sensor->sensor_gpio_res) { 
2906                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
2907                     for (i = 0; i < icd->ops->num_controls; i++) {
2908                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
2909                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
2910                                 }
2911                     }
2912                     sensor->info_priv.flash = 0xff;
2913                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
2914                 }
2915                 }
2916             #endif
2917                         break;
2918                 }
2919                 default:
2920                 {
2921                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2922                         break;
2923                 }
2924         }
2925 sensor_ioctl_end:
2926         return ret;
2927
2928 }
2929 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
2930                             enum v4l2_mbus_pixelcode *code)
2931 {
2932         if (index >= ARRAY_SIZE(sensor_colour_fmts))
2933                 return -EINVAL;
2934
2935         *code = sensor_colour_fmts[index].code;
2936         return 0;
2937 }
2938 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
2939         .init           = sensor_init,
2940         .g_ctrl         = sensor_g_control,
2941         .s_ctrl         = sensor_s_control,
2942         .g_ext_ctrls          = sensor_g_ext_controls,
2943         .s_ext_ctrls          = sensor_s_ext_controls,
2944         .g_chip_ident   = sensor_g_chip_ident,
2945         .ioctl = sensor_ioctl,  
2946 };
2947
2948 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
2949         .s_mbus_fmt     = sensor_s_fmt,
2950         .g_mbus_fmt     = sensor_g_fmt,
2951         .try_mbus_fmt   = sensor_try_fmt,
2952         .enum_mbus_fmt  = sensor_enum_fmt,
2953 };
2954
2955 static struct v4l2_subdev_ops sensor_subdev_ops = {
2956         .core   = &sensor_subdev_core_ops,
2957         .video = &sensor_subdev_video_ops,
2958 };
2959
2960 static int sensor_probe(struct i2c_client *client,
2961                          const struct i2c_device_id *did)
2962 {
2963     struct sensor *sensor;
2964     struct soc_camera_device *icd = client->dev.platform_data;
2965     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
2966     struct soc_camera_link *icl;
2967     int ret;
2968
2969     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
2970     if (!icd) {
2971         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
2972         return -EINVAL;
2973     }
2974
2975     icl = to_soc_camera_link(icd);
2976     if (!icl) {
2977         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
2978         return -EINVAL;
2979     }
2980
2981     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
2982         dev_warn(&adapter->dev,
2983                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
2984         return -EIO;
2985     }
2986
2987     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
2988     if (!sensor)
2989         return -ENOMEM;
2990
2991     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
2992
2993     /* Second stage probe - when a capture adapter is there */
2994     icd->ops            = &sensor_ops;
2995
2996     sensor->info_priv.fmt = sensor_colour_fmts[0];
2997     
2998         #if CONFIG_SENSOR_I2C_NOSCHED
2999         atomic_set(&sensor->tasklock_cnt,0);
3000         #endif
3001
3002     ret = sensor_video_probe(icd, client);
3003     if (ret < 0) {
3004         icd->ops = NULL;
3005         i2c_set_clientdata(client, NULL);
3006         kfree(sensor);
3007                 sensor = NULL;
3008     }
3009     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3010     return ret;
3011 }
3012
3013 static int sensor_remove(struct i2c_client *client)
3014 {
3015     struct sensor *sensor = to_sensor(client);
3016     struct soc_camera_device *icd = client->dev.platform_data;
3017
3018     icd->ops = NULL;
3019     i2c_set_clientdata(client, NULL);
3020     client->driver = NULL;
3021     kfree(sensor);
3022         sensor = NULL;
3023     return 0;
3024 }
3025
3026 static const struct i2c_device_id sensor_id[] = {
3027         {SENSOR_NAME_STRING(), 0 },
3028         { }
3029 };
3030 MODULE_DEVICE_TABLE(i2c, sensor_id);
3031
3032 static struct i2c_driver sensor_i2c_driver = {
3033         .driver = {
3034                 .name = SENSOR_NAME_STRING(),
3035         },
3036         .probe          = sensor_probe,
3037         .remove         = sensor_remove,
3038         .id_table       = sensor_id,
3039 };
3040
3041 static int __init sensor_mod_init(void)
3042 {
3043     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3044     return i2c_add_driver(&sensor_i2c_driver);
3045 }
3046
3047 static void __exit sensor_mod_exit(void)
3048 {
3049     i2c_del_driver(&sensor_i2c_driver);
3050 }
3051
3052 device_initcall_sync(sensor_mod_init);
3053 module_exit(sensor_mod_exit);
3054
3055 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3056 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3057 MODULE_LICENSE("GPL");
3058
3059