camera: camera driver and all sensor driver support
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov3640.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 #include "ov3640.h"
24
25 static int debug;
26 module_param(debug, int, S_IRUGO|S_IWUSR);
27
28 #define dprintk(level, fmt, arg...) do {                        \
29         if (debug >= level)                                     \
30         printk(KERN_WARNING fmt , ## arg); } while (0)
31
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
34
35 #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_OV3640
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV3640
48 #define SENSOR_ID 0x364c
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    2048
52 #define SENSOR_MAX_HEIGHT   1536
53 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  480
55 #define SENSOR_INIT_WINSEQADR sensor_init_data
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_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_Exposure      0
66 #define CONFIG_SENSOR_Flash         1
67 #define CONFIG_SENSOR_Mirror        0
68 #define CONFIG_SENSOR_Flip          0
69 #ifdef CONFIG_OV3640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus         1
71 #include "ov3640_af_firmware.c"
72 #else
73 #define CONFIG_SENSOR_Focus         0
74 #endif
75
76 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED   0
79 #define CONFIG_SENSOR_I2C_RDWRCHK   0
80
81
82 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
83                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
84                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
85
86 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
87 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
88 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
89 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
90 #define COLOR_TEMPERATURE_OFFICE_DN     3500
91 #define COLOR_TEMPERATURE_OFFICE_UP     5000
92 #define COLOR_TEMPERATURE_HOME_DN       2500
93 #define COLOR_TEMPERATURE_HOME_UP       3500
94
95 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
96 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
97
98 #define SENSOR_AF_IS_ERR    (0x00<<0)
99 #define SENSOR_AF_IS_OK         (0x01<<0)
100
101 #if CONFIG_SENSOR_Focus
102 #define SENSOR_AF_MODE_INFINITY    0
103 #define SENSOR_AF_MODE_MACRO       1
104 #define SENSOR_AF_MODE_FIXED       2
105 #define SENSOR_AF_MODE_AUTO        3
106 #define SENSOR_AF_MODE_CONTINUOUS  4
107 #define SENSOR_AF_MODE_CLOSE       5
108
109 #endif
110
111 #define VCM_FIRMWARE                            1
112
113 #define VCM_STARTADDR                           0x8000
114
115 #define S_IDLE                                          0x00
116 #if (VCM_FIRMWARE==1)
117 #define S_FOCUSING                                      0x01
118 #define S_FOCUSED                                       0x02
119 #define S_CAPTURE                                       0x12
120 #define STA_FOCUS                                       0x3f07
121 #elif (VCM_FIRMWARE==2)
122 #define S_FOCUSING                                      0x01
123 #define S_FOCUSED                                       0x02
124 #define S_CAPTURE                                       0x12
125 #define STA_FOCUS                                       0x3f01
126 #else
127 #define S_FOCUSING                                      0x65
128 #define S_FOCUSED                                       0x46
129 #define S_CAPTURE                                       0x47
130 #define STA_FOCUS                                       0x3f01
131 #endif
132
133
134 #if CONFIG_SENSOR_Focus
135 /* ov3640 VCM Command and Status Registers */
136 #define CMD_MAIN_Reg      0x3F00
137 #define CMD_TAG_Reg       0x3F01
138 #define CMD_PARA0_Reg     0x3F05
139 #define CMD_PARA1_Reg     0x3F04
140 #define CMD_PARA2_Reg     0x3F03
141 #define CMD_PARA3_Reg     0x3F02
142 #define STA_ZONE_Reg      0x3F06
143 #define STA_FOCUS_Reg     0x3F07
144
145 /* ov3640 VCM Command  */
146 #define OverlayEn_Cmd     0x01
147 #define OverlayDis_Cmd    0x02
148 #define SingleFocus_Cmd   0x03
149 #define ConstFocus_Cmd    0x04
150 #define StepMode_Cmd      0x05
151 #define PauseFocus_Cmd    0x06
152 #define ReturnIdle_Cmd    0x08
153 #define SetZone_Cmd       0x10
154 #define UpdateZone_Cmd    0x12
155 #define SetMotor_Cmd      0x20
156
157 /* ov3640 Focus State */
158 #define S_FIRWRE          0xFF
159 #define S_STARTUP         0xFA
160 #define S_ERROR           0xFE
161 #define S_DRVICERR        0xEE
162 #define S_IDLE            0x00
163 #define S_FOCUSING        0x01
164 #define S_FOCUSED         0x02
165 #define S_CAPTURE         0x12
166 #define S_STEP            0x20
167
168 /* ovxxxx Zone State */
169 #define Zone_Is_Focused(a, zone_val)    (zone_val&(1<<(a-3)))
170 #define Zone_Get_ID(zone_val)           (zone_val&0x03)
171
172 #define Zone_CenterMode   0x01
173 #define Zone_5xMode       0x02
174 #define Zone_5PlusMode    0x03
175 #define Zone_4fMode       0x04
176
177 #define ZoneSel_Auto      0x0b
178 #define ZoneSel_SemiAuto  0x0c
179 #define ZoneSel_Manual    0x0d
180 #define ZoneSel_Rotate    0x0e
181
182 /* ovxxxx Step Focus Commands */
183 #define StepFocus_Near_Tag       0x01
184 #define StepFocus_Far_Tag        0x02
185 #define StepFocus_Furthest_Tag   0x03
186 #define StepFocus_Nearest_Tag    0x04
187 #define StepFocus_Spec_Tag       0x10
188 #endif
189
190 /* init VGA 640*480 */
191 static struct reginfo sensor_init_data[] =
192 {
193 #if 1
194         {0x3078, 0x02},
195         {0x304d, 0x45},
196         {0x30a7, 0x5e},
197         {0x3087, 0x16},
198         {0x309C, 0x1a},
199         {0x30a2, 0xe4},
200         {0x30aa, 0x42},
201         {0x30b0, 0xff},
202         {0x30b1, 0xff},
203         //{0x30b2, 0x10},
204         {0x30b2, 0x18}, // by FAE
205         {0x300e, 0x39},
206         {0x300f, 0x21},
207         {0x3010, 0x20},
208         {0x304c, 0x81},
209         {0x30d7, 0x10},
210         {0x30d9, 0x0d},
211         {0x30db, 0x08},
212         {0x3016, 0x82},
213         {0x3018, 0x48},
214         {0x3019, 0x40},
215         {0x301a, 0x82},
216
217        {0x30a9, 0xbd},//disable internal DVDD, by FAE.
218        
219         {0x307d, 0x00},
220         {0x3087, 0x02},
221         {0x3082, 0x20},
222         {0x3015, 0x12},
223         {0x3014, 0x84},
224         {0x3013, 0xf7},
225         {0x303c, 0x08},
226         {0x303d, 0x18},
227         {0x303e, 0x06},
228         {0x303F, 0x0c},
229         {0x3030, 0x62},
230         {0x3031, 0x26},
231         {0x3032, 0xe6},
232         {0x3033, 0x6e},
233         {0x3034, 0xea},
234         {0x3035, 0xae},
235         {0x3036, 0xa6},
236         {0x3037, 0x6a},
237         {0x3104, 0x02},
238         {0x3105, 0xfd},
239         {0x3106, 0x00},
240         {0x3107, 0xff},
241         {0x3300, 0x13},
242         {0x3301, 0xde},
243         {0x3302, 0xcf},
244         {0x3312, 0x26},
245         {0x3314, 0x42},
246         {0x3313, 0x2b},
247         {0x3315, 0x42},
248         {0x3310, 0xd0},
249         {0x3311, 0xbd},
250         {0x330c, 0x18},
251         {0x330d, 0x18},
252         {0x330e, 0x56},
253         {0x330f, 0x5c},
254         {0x330b, 0x1c},
255         {0x3306, 0x5c},
256         {0x3307, 0x11},
257         {0x336a, 0x52},
258         {0x3370, 0x46},
259         {0x3376, 0x38},
260         {0x30b8, 0x20},
261         {0x30b9, 0x17},
262         {0x30ba, 0x00},
263         {0x30bb, 0x08},
264         {0x3507, 0x06},
265         {0x350a, 0x4f},
266         {0x3100, 0x02},
267         {0x3301, 0xde},
268         {0x3304, 0xfc},
269         {0x3400, 0x00},
270         {0x3404, 0x00},//YUV, sequence
271         {0x3600, 0xc0},
272         {0x3088, 0x08},
273         {0x3089, 0x00},
274         {0x308a, 0x06},
275         {0x308b, 0x00},
276         {0x308d, 0x04},
277         {0x3086, 0x03},
278         {0x3086, 0x00},
279         {0x30a9, 0xb5},
280         {0x3317, 0x04},
281         {0x3316, 0xf8},
282         {0x3312, 0x31},
283         {0x3314, 0x57},
284         {0x3313, 0x28},
285         {0x3315, 0x3d},
286         {0x3311, 0xd0},
287         {0x3310, 0xb6},
288         {0x330c, 0x16},
289         {0x330d, 0x16},
290         {0x330e, 0x5f},
291         {0x330f, 0x5c},
292         {0x330b, 0x18},
293         {0x3306, 0x5c},
294         {0x3307, 0x11},
295         {0x3308, 0x25},
296         {0x3340, 0x20},
297         {0x3341, 0x58},
298         {0x3342, 0x08},
299         {0x3343, 0x21},
300         {0x3344, 0xbe},
301         {0x3345, 0xe0},
302         {0x3346, 0xca},
303         {0x3347, 0xc6},
304         {0x3348, 0x04},
305         {0x3349, 0x98},
306         {0x3355, 0x02},
307         {0x3358, 0x44},
308         {0x3359, 0x44},
309         {0x3300, 0x13},
310         {0x3367, 0x23},
311         {0x3368, 0xBB},
312         {0x3369, 0xD6},
313         {0x336A, 0x2A},
314         {0x336B, 0x07},
315         {0x336C, 0x00},
316         {0x336D, 0x23},
317         {0x336E, 0xC3},
318         {0x336F, 0xDE},
319         {0x3370, 0x2b},
320         {0x3371, 0x07},
321         {0x3372, 0x00},
322         {0x3373, 0x23},
323         {0x3374, 0x9e},
324         {0x3375, 0xD6},
325         {0x3376, 0x29},
326         {0x3377, 0x07},
327         {0x3378, 0x00},
328         {0x332a, 0x1d},
329         {0x331b, 0x08},
330         {0x331c, 0x16},
331         {0x331d, 0x2d},
332         {0x331e, 0x54},
333         {0x331f, 0x66},
334         {0x3320, 0x73},
335         {0x3321, 0x80},
336         {0x3322, 0x8c},
337         {0x3323, 0x95},
338         {0x3324, 0x9d},
339         {0x3325, 0xac},
340         {0x3326, 0xb8},
341         {0x3327, 0xcc},
342         {0x3328, 0xdd},
343         {0x3329, 0xee},
344         {0x332e, 0x04},
345         {0x332f, 0x06},
346         {0x3331, 0x03},
347         //{0x307c, 0x13}, // flip && mirror
348         {0x307c, 0x11}, // flip && mirror ,by FAE.
349   {0x3090, 0xc8},
350 #else
351 //640 480 ;XGA-&gt;XGA;;
352      {0x3012, 0x10},
353      {0x3023, 0x06},
354      {0x3026, 0x03},
355      {0x3027, 0x04},
356      {0x302a, 0x03},
357      {0x302b, 0x10},
358      {0x3075, 0x24},
359      {0x300d, 0x01},
360      {0x30d7, 0x90},
361      {0x3069, 0x04},
362      {0x303e, 0x00},
363      {0x303f, 0xc0},
364      {0x3302, 0xef},
365      {0x335f, 0x34},
366      {0x3360, 0x0c},
367      {0x3361, 0x04},
368      {0x3362, 0x34},
369      {0x3363, 0x08},
370      {0x3364, 0x04},
371      {0x3403, 0x42},
372      {0x3088, 0x04},
373      {0x3089, 0x00},
374      {0x308a, 0x03},
375      {0x308b, 0x00},
376      {0x300e, 0x32},
377      {0x300f, 0x21},
378      {0x3010, 0x20},
379      {0x3011, 0x01},
380      {0x304c, 0x82},
381
382 //;XGA-&gt;VGA
383      {0x3302, 0xef},
384      {0x335f, 0x34},
385      {0x3360, 0x0c},
386      {0x3361, 0x04},
387      {0x3362, 0x12},
388      {0x3363, 0x88},
389      {0x3364, 0xe4},
390      {0x3403, 0x42},
391      {0x3088, 0x12},
392      {0x3089, 0x80},
393      {0x308a, 0x01},
394      {0x308b, 0xe0},
395      {0x304c, 0x85},
396 #endif
397         {0x0000 ,0x00},
398 };
399
400 /* 2048X1536 QXGA */
401 static struct reginfo sensor_qxga_preview[] =
402 {
403 #if 1
404         {0x3012, 0x00},
405         {0x3366, 0x10},
406         {0x3020, 0x01},
407         {0x3021, 0x1d},
408         {0x3022, 0x00},
409         {0x3023, 0x0a},
410         {0x3024, 0x08},
411         {0x3025, 0x18},
412         {0x3026, 0x06},
413         {0x3027, 0x0c},
414         {0x302a, 0x06},
415         {0x302b, 0x20},
416         {0x3075, 0x44},
417         {0x300d, 0x00},
418         {0x30d7, 0x10},
419         {0x3069, 0x44},
420         {0x303e, 0x01},
421         {0x303f, 0x80},
422                 
423         {0x3302, 0xcf},
424         {0x335f, 0x68},
425         {0x3360, 0x18},
426         {0x3361, 0x0c},
427         {0x3362, 0x68},
428         {0x3363, 0x08},
429         {0x3364, 0x04},
430         {0x3366, 0x10},
431         {0x3403, 0x42},
432         {0x3088, 0x08},
433         {0x3089, 0x00},
434         {0x308a, 0x06},
435         {0x308b, 0x00},
436         {0x300e, 0x39},
437         {0x300f, 0x21},
438         {0x3010, 0x20},
439         {0x3011, 0x01},
440         {0x304c, 0x81},
441         //{0x307c, 0x13}, // flip && mirror
442         {0x307c, 0x11}, // flip && mirror, by FAE.
443   {0x3090, 0xc8},
444
445 #else
446 //[Sensor.YUV.2048x1536] XGA-&gt;QXGA
447   {0x3012, 0x00},
448   {0x3020, 0x01},
449   {0x3021, 0x1d},
450   {0x3022, 0x00},
451   {0x3023, 0x0a},
452   {0x3024, 0x08},
453   {0x3025, 0x18},
454   {0x3026, 0x06},
455   {0x3027, 0x0c},
456   {0x302a, 0x06},
457   {0x302b, 0x20},
458   {0x3075, 0x44},
459   {0x300d, 0x00},
460   {0x30d7, 0x10},
461   {0x3069, 0x44},
462   {0x303e, 0x01},
463   {0x303f, 0x80},
464   {0x3302, 0xcf},
465   {0x335f, 0x68},
466   {0x3360, 0x18},
467   {0x3361, 0x0c},
468   {0x3362, 0x68},
469   {0x3363, 0x08},
470   {0x3364, 0x04},
471   {0x3403, 0x42},
472   {0x3088, 0x08},
473   {0x3089, 0x00},
474   {0x308a, 0x06},
475   {0x308b, 0x00},
476   {0x300e, 0x39},
477   {0x300f, 0x21},
478   {0x3010, 0x20},
479   {0x3011, 0x01},
480   {0x304c, 0x81},
481 #endif          
482         {0x0000 ,0x00}
483 };
484 static struct reginfo sensor_qxga_capture[] = {
485         {0x3012, 0x00},
486         {0x3366, 0x10},
487         {0x3020, 0x01},
488         {0x3021, 0x1d},
489         {0x3022, 0x00},
490         {0x3023, 0x0a},
491         {0x3024, 0x08},
492         {0x3025, 0x18},
493         {0x3026, 0x06},
494         {0x3027, 0x0c},
495         {0x302a, 0x06},
496         {0x302b, 0x20},
497         {0x3075, 0x44},
498         //{0x307C, 0x13}, // by FAE.
499         {0x300d, 0x00},
500         {0x30d7, 0x10},
501         {0x3069, 0x44},
502         {0x303e, 0x01},
503         {0x303f, 0x80},
504                 
505         {0x3302, 0xcf},
506         {0x335f, 0x68},
507         {0x3360, 0x18},
508         {0x3361, 0x0c},
509         {0x3362, 0x68},
510         {0x3363, 0x08},
511         {0x3364, 0x04},
512         {0x3366, 0x10},
513         {0x3403, 0x42},
514         {0x3088, 0x08},
515         {0x3089, 0x00},
516         {0x308a, 0x06},
517         {0x308b, 0x00},
518         {0x300e, 0x39},
519         {0x300f, 0x21},
520         {0x3010, 0x20},
521         {0x3011, 0x01},
522         {0x304c, 0x81},
523         
524         //{0x307c, 0x13},// flip && mirror 
525         {0x307c, 0x11},// flip && mirror , by FAE.
526   {0x3090, 0xc8},
527                 
528         {0x0000 ,0x00}
529 };
530 static struct reginfo *sensor_qxga[2] = {
531         sensor_qxga_preview,
532         sensor_qxga_capture,    
533 };
534
535 /* 1600X1200 UXGA */
536 static struct reginfo sensor_uxga_preview[] =
537 {
538         {0x3012, 0x00},
539         {0x3366, 0x10},
540         {0x3020, 0x01},
541         {0x3021, 0x1d},
542         {0x3022, 0x00},
543         {0x3023, 0x0a},
544         {0x3024, 0x08},
545         {0x3025, 0x18},
546         {0x3026, 0x06},
547         {0x3027, 0x0c},
548         {0x302a, 0x06},
549         {0x302b, 0x20},
550         {0x3075, 0x44},
551         {0x300d, 0x00},
552         {0x30d7, 0x10},
553         {0x3069, 0x44},
554         {0x303e, 0x01},
555         {0x303f, 0x80},
556         //qxga -> uxga
557         {0x300e, 0x39},
558         {0x300f, 0x21},
559         {0x3010, 0x20},
560         {0x3302, 0xef},
561         {0x335f, 0x68},
562         {0x3360, 0x18},
563         {0x3361, 0x0C},
564         {0x3362, 0x46},
565         {0x3363, 0x48},
566         {0x3364, 0xb4},
567         {0x3403, 0x22},
568         {0x3088, 0x06},
569         {0x3089, 0x40},
570         {0x308a, 0x04},
571         {0x308b, 0xb0},
572         {0x304c, 0x81},//56Mhz PCLK output
573         //{0x307c, 0x13}, // flip && mirror
574         {0x307c, 0x11}, // flip && mirror, by FAE.
575   {0x3090, 0xc8},
576         
577         {0x0000 ,0x00},
578 };
579 static struct reginfo sensor_uxga_capture[] =
580 {
581         {0x3302, 0xef},
582         {0x335f, 0x68},
583         {0x3360, 0x18},
584         {0x3361, 0x0C},
585         {0x3362, 0x46},
586         {0x3363, 0x48},
587         {0x3364, 0xb4},
588         {0x3403, 0x22},
589         {0x3088, 0x06},
590         {0x3089, 0x40},
591         {0x308a, 0x04},
592         {0x308b, 0xb0},
593         {0x304c, 0x81},//56Mhz PCLK output
594         //{0x307c, 0x13}, // flip && mirror, by FAE.
595         {0x307c, 0x11},
596   {0x3090, 0xc8},
597
598         {0x0000 ,0x00},
599 };
600 static struct reginfo *sensor_uxga[2] = {
601         sensor_uxga_preview,
602         sensor_uxga_capture,    
603 };
604
605 /* 1280X960 SXGA  */
606 static struct reginfo sensor_sxga_preview[] =
607 {
608         {0x3012, 0x00},
609         {0x3366, 0x10},
610         {0x3020, 0x01},
611         {0x3021, 0x1d},
612         {0x3022, 0x00},
613         {0x3023, 0x0a},
614         {0x3024, 0x08},
615         {0x3025, 0x18},
616         {0x3026, 0x06},
617         {0x3027, 0x0c},
618         {0x302a, 0x06},
619         {0x302b, 0x20},
620         {0x3075, 0x44},
621         {0x300d, 0x00},
622         {0x30d7, 0x10},
623         {0x3069, 0x44},
624         {0x303e, 0x01},
625         {0x303f, 0x80},
626         //qxga -> sxga
627         {0x300e, 0x39},
628         {0x300f, 0x21},
629         {0x3010, 0x20},
630         {0x3302, 0xef},
631         {0x335f, 0x68},
632         {0x3360, 0x18},
633         {0x3361, 0x0c},
634         {0x3362, 0x35},
635         {0x3363, 0x08},
636         {0x3364, 0xc4},
637         {0x3403, 0x42},
638         {0x3088, 0x05},
639         {0x3089, 0x00},
640         {0x308a, 0x03},
641         {0x308b, 0xc0},
642         {0x304c, 0x81},//56Mhz PCLK output
643         //{0x307c, 0x13}, // flip && mirror
644         {0x307c, 0x11}, // by FAE.
645   {0x3090, 0xc8},
646         
647         {0x0000 ,0x00},
648 };
649 static struct reginfo sensor_sxga_capture[] =
650 {
651         {0x3302, 0xef},
652         {0x335f, 0x68},
653         {0x3360, 0x18},
654         {0x3361, 0x0c},
655         {0x3362, 0x35},
656         {0x3363, 0x08},
657         {0x3364, 0xc4},
658         {0x3403, 0x42},
659         {0x3088, 0x05},
660         {0x3089, 0x00},
661         {0x308a, 0x03},
662         {0x308b, 0xc0},
663         {0x304c, 0x81},//56Mhz PCLK output
664         //{0x307c, 0x13}, // flip && mirror
665         {0x307c, 0x11}, // by FAE.
666   {0x3090, 0xc8},
667         {0x0000 ,0x00},
668 };
669 static struct reginfo *sensor_sxga[2] = {
670         sensor_sxga_preview,
671         sensor_sxga_capture,
672 };
673
674 /* 1024*768 XGA */
675 static struct reginfo sensor_xga_preview[] =
676 {
677         {0x3012, 0x10},
678         {0x3366, 0x15},
679         {0x3023, 0x06},
680         {0x3026, 0x03},
681         {0x3027, 0x04},
682         {0x302a, 0x03},
683         {0x302b, 0x39},
684         {0x3075, 0x24},
685         {0x300d, 0x01},
686         {0x30d7, 0x90},
687         {0x3069, 0x04},
688         {0x303e, 0x00},
689         {0x303f, 0xc0},
690         {0x300e, 0x32},
691         {0x300f, 0x21},
692         {0x3010, 0x20},
693         {0x3011, 0x01},
694         //XGA--XGA
695         {0x3302, 0xef},
696         {0x335f, 0x34},
697         {0x3360, 0x0c},
698         {0x3361, 0x04},
699         {0x3362, 0x34},
700         {0x3363, 0x08},
701         {0x3364, 0x04},
702         {0x3403, 0x42},
703         {0x3088, 0x04},
704         {0x3089, 0x00},
705         {0x308a, 0x03},
706         {0x308b, 0x00},
707         {0x304c, 0x82},//28Mhz PCLK output
708         //{0x307c, 0x13}, // flip && mirror
709         {0x307c, 0x11},// by FAE.
710   {0x3090, 0xc8},
711
712         {0x0000 ,0x00},
713 };
714 static struct reginfo sensor_xga_capture[] =
715 {
716         {0x3302, 0xef},
717         {0x335f, 0x68},
718         {0x3360, 0x18},
719         {0x3361, 0x0c},
720         {0x3362, 0x34},
721         {0x3363, 0x08},
722         {0x3364, 0x06},
723         {0x3403, 0x42},
724         {0x3088, 0x04},
725         {0x3089, 0x00},
726         {0x308a, 0x03},
727         {0x308b, 0x00},
728         {0x304c, 0x82},//28Mhz PCLK output
729         //{0x307c, 0x13}, // flip && mirror
730         {0x307c, 0x11}, // by FAE.
731   {0x3090, 0xc8},
732
733         {0x0000 ,0x00},
734 };
735 static struct reginfo *sensor_xga[2] = {
736         sensor_xga_preview,
737         sensor_xga_capture,
738 };
739
740 /* 800X600 SVGA*/
741 static struct reginfo sensor_svga_preview[] =
742 {
743         {0x3012, 0x10},
744         {0x3366, 0x15},
745         {0x3023, 0x06},
746         {0x3026, 0x03},
747         {0x3027, 0x04},
748         {0x302a, 0x03},
749         {0x302b, 0x39},
750         {0x3075, 0x24},
751         {0x300d, 0x01},
752         {0x30d7, 0x90},
753         {0x3069, 0x04},
754         {0x303e, 0x00},
755         {0x303f, 0xc0},
756         {0x300e, 0x32},
757         {0x300f, 0x21},
758         {0x3010, 0x20},
759         {0x3011, 0x01},
760         // XGA-SVGA
761         {0x3302, 0xef},
762         {0x335f, 0x34},
763         {0x3360, 0x0c},
764         {0x3361, 0x04},
765         {0x3362, 0x23},
766         {0x3363, 0x28},
767         {0x3364, 0x5c},
768         {0x3403, 0x42},
769         {0x3088, 0x03},
770         {0x3089, 0x20},
771         {0x308a, 0x02},
772         {0x308b, 0x58},
773         {0x304c, 0x83},//28Mhz PCLK output
774
775         //{0x307c, 0x13}, // flip && mirror
776         {0x307c, 0x11}, // flip && mirror, by FAE.
777   {0x3090, 0xc8},
778         {0x0000 ,0x00},
779 };
780 static struct reginfo sensor_svga_capture[] =
781 {
782         {0x3302, 0xef},
783         {0x335f, 0x68},
784         {0x3360, 0x18},
785         {0x3361, 0x0c},
786         {0x3362, 0x23},
787         {0x3363, 0x28},
788         {0x3364, 0x5c},
789         {0x3403, 0x42},
790         {0x3088, 0x03},
791         {0x3089, 0x20},
792         {0x308a, 0x02},
793         {0x308b, 0x58},
794         {0x304c, 0x82},//28Mhz PCLK output
795
796         //{0x307c, 0x13}, // flip && mirror
797         {0x307c, 0x11}, // flip && mirror, by FAE.
798   {0x3090, 0xc8},
799         {0x0000 ,0x00},
800 };
801 static struct reginfo * sensor_svga[2] = {
802         sensor_svga_preview,
803         sensor_svga_capture,
804 };
805
806 /* 640X480 VGA */
807 static struct reginfo sensor_vga_preview[] =
808 {
809         {0x3012, 0x10},
810         {0x3366, 0x15},
811         {0x3023, 0x06},
812         {0x3026, 0x03},
813         {0x3027, 0x04},
814         {0x302a, 0x03},
815         {0x302b, 0x39},
816         {0x3075, 0x24},
817         {0x300d, 0x01},
818         {0x30d7, 0x90},
819         {0x3069, 0x04},
820         {0x303e, 0x00},
821         {0x303f, 0xc0},
822         {0x300e, 0x32},
823         {0x300f, 0x21},
824         {0x3010, 0x20},
825         {0x3011, 0x01},
826         // XGA-VGA
827         {0x3302, 0xef},
828         {0x335f, 0x34},
829         {0x3360, 0x0c},
830         {0x3361, 0x04},
831         {0x3362, 0x12},
832         {0x3363, 0x88},
833         {0x3364, 0xe4},
834         {0x3403, 0x42},
835         {0x3088, 0x02},
836         {0x3089, 0x88},// 0x80, by FAE.
837         {0x308a, 0x01},
838         {0x308b, 0xe4},// 0xe0, by FAE.
839         {0x304c, 0x84}, //0x83, by FAE.
840         //{0x307c, 0x13}, // flip && mirror
841         {0x307c, 0x11}, // flip && mirror, by FAE.
842         {0x3090, 0xc8},
843 // by FAE.
844 //AWB short
845       // {0x33a7, 0x60},
846         //{0x33a8, 0x40},
847         //{0x33a9, 0x68},
848         //{0x332b, 0x08},
849         //{0x330a, 0x22},
850         
851         //{0x332b, 0x00},
852         //{0x330a, 0x02},
853 //end
854         
855
856         {0x0000 ,0x00},
857 };
858
859 static struct reginfo sensor_vga_capture[] =
860 {
861         {0x3302, 0xef},
862         {0x335f, 0x68},
863         {0x3360, 0x18},
864         {0x3361, 0x0c},
865         {0x3362, 0x12},
866         {0x3363, 0x88},
867         {0x3364, 0xe4},
868         {0x3403, 0x42},
869         {0x3088, 0x02},
870         {0x3089, 0x80},
871         {0x308a, 0x01},
872         {0x308b, 0xe0},
873         {0x304c, 0x82},//14Mhz PCLK output 84
874         //{0x307c, 0x13}, // flip && mirror
875         {0x307c, 0x11},// by FAE.
876   {0x3090, 0xc8},
877
878         {0x0000 ,0x00},
879 };
880 static struct reginfo *sensor_vga[2] = {
881         sensor_vga_preview,
882         sensor_vga_capture,
883 };
884
885 /* 352X288 CIF */
886 static struct reginfo sensor_cif_preview[] =
887 {
888         {0x3012, 0x10},
889         {0x3366, 0x15}, 
890         {0x3023, 0x06},
891         {0x3026, 0x03},
892         {0x3027, 0x04},
893         {0x302a, 0x03},
894         {0x302b, 0x39},
895         {0x3075, 0x24},
896         {0x300d, 0x01},
897         {0x30d7, 0x90},
898         {0x3069, 0x04},
899         {0x303e, 0x00},
900         {0x303f, 0xc0},
901         {0x300e, 0x32},
902         {0x300f, 0x21},
903         {0x3010, 0x20},
904         {0x3011, 0x01},
905         //XGA->CIF(352*288)
906 #if 0   // by FAE.
907         {0x3302, 0xef},
908         {0x335f, 0x34},
909         {0x3360, 0x0c},
910         {0x3361, 0x04},
911         {0x3362, 0x11},
912         {0x3363, 0x6c},
913         {0x3364, 0x26},
914         {0x3403, 0x42},
915         {0x3088, 0x01},
916         {0x3089, 0x60},
917         {0x308a, 0x01},
918         {0x308b, 0x20},
919         {0x304c, 0x82}, //89
920 #else   
921         {0x3302, 0xef},
922         {0x335f, 0x34},
923         {0x3360, 0x0c},
924         {0x3361, 0x04},
925         {0x3362, 0x11},
926         {0x3363, 0x68}, //?
927         {0x3364, 0x24},//?
928         {0x3403, 0x42},
929         {0x3088, 0x01},
930         {0x3089, 0x68},
931         {0x308a, 0x01},
932         {0x308b, 0x24},
933         {0x304c, 0x85}, //89    
934 #endif  
935         //{0x307c, 0x13}, // flip && mirror
936         {0x307c, 0x11}, // flip && mirror, by FAE.
937   {0x3090, 0xc8},
938
939         {0x0000 ,0x00},
940 };
941 static struct reginfo sensor_cif_capture[] =
942 {
943         {0x3302, 0xef},
944         {0x335f, 0x68},
945         {0x3360, 0x18},
946         {0x3361, 0x0c},
947         {0x3362, 0x11},
948         {0x3363, 0x68},
949         {0x3364, 0x24},
950         {0x3403, 0x42},
951         {0x3088, 0x01},
952         {0x3089, 0x60},
953         {0x308a, 0x01},
954         {0x308b, 0x20},
955         {0x304c, 0x84},//14Mhz PCLK output
956         //{0x307c, 0x13}, // flip && mirror
957         {0x307c, 0x11},//by FAE.
958   {0x3090, 0xc8},
959
960         {0x0000 ,0x00},
961 };
962 static struct reginfo *sensor_cif[2] = {
963         sensor_cif_preview,
964         sensor_cif_capture,
965 };
966
967 /* 320*240 QVGA */
968 static struct reginfo sensor_qvga_preview[] =
969 {
970         {0x3012, 0x10},
971         {0x3366, 0x15},
972         {0x3023, 0x06},
973         {0x3026, 0x03},
974         {0x3027, 0x04},
975         {0x302a, 0x03},
976         {0x302b, 0x39},
977         {0x3075, 0x24},
978         {0x300d, 0x01},
979         {0x30d7, 0x90},
980         {0x3069, 0x04},
981         {0x303e, 0x00},
982         {0x303f, 0xc0},
983         {0x300e, 0x32},
984         {0x300f, 0x21},
985         {0x3010, 0x20},
986         {0x3011, 0x01},
987         //XGA->QVGA
988         {0x3302, 0xef},
989         {0x335f, 0x34},
990         {0x3360, 0x0c},
991         {0x3361, 0x04},
992         {0x3362, 0x01},
993         {0x3363, 0x48},
994         {0x3364, 0xf4},
995         {0x3403, 0x42},
996         {0x3088, 0x01},
997         {0x3089, 0x40},
998         {0x308a, 0x00},
999         {0x308b, 0xf0},
1000         {0x304c, 0x89},//14Mhz PCLK output 
1001         //{0x307c, 0x13}, // flip && mirror
1002         {0x307c, 0x11}, // by FAE.
1003   {0x3090, 0xc8},
1004
1005         {0x0000 ,0x00},
1006 };
1007 static struct reginfo sensor_qvga_capture[] =
1008 {
1009         {0x3302, 0xef},
1010         {0x335f, 0x68},
1011         {0x3360, 0x18},
1012         {0x3361, 0x0c},
1013         {0x3362, 0x01},
1014         {0x3363, 0x48},
1015         {0x3364, 0xf4},
1016         {0x3403, 0x42},
1017         {0x3088, 0x01},
1018         {0x3089, 0x40},
1019         {0x308a, 0x00},
1020         {0x308b, 0xf0},
1021         {0x304c, 0x84},//14Mhz PCLK output
1022
1023         //{0x307c, 0x13}, // flip && mirror
1024         {0x307c, 0x11},// by FAE.
1025   {0x3090, 0xc8},
1026         {0x0000 ,0x00},
1027 };
1028 static  struct reginfo *sensor_qvga[2] = {
1029         sensor_qvga_preview,
1030         sensor_qvga_capture,
1031 };
1032
1033 /* 176*144 QCIF */
1034 static struct reginfo sensor_qcif_preview[] =
1035 {
1036         {0x3012, 0x10},
1037         {0x3366, 0x15},
1038         {0x3023, 0x06},
1039         {0x3026, 0x03},
1040         {0x3027, 0x04},
1041         {0x302a, 0x03},
1042         {0x302b, 0x39},
1043         {0x3075, 0x24},
1044         {0x300d, 0x01},
1045         {0x30d7, 0x90},
1046         {0x3069, 0x04},
1047         {0x303e, 0x00},
1048         {0x303f, 0xc0},
1049         {0x300e, 0x32},
1050         {0x300f, 0x21},
1051         {0x3010, 0x20},
1052         {0x3011, 0x01},
1053         //XGA->QCIF     
1054         {0x3302, 0xef},
1055         {0x335f, 0x34},
1056         {0x3360, 0x0c},
1057         {0x3361, 0x04},
1058         {0x3362, 0x00},
1059         {0x3363, 0xb8},
1060         {0x3364, 0x94},
1061         {0x3403, 0x42},
1062         {0x3088, 0x00},
1063         {0x3089, 0xb0},
1064         {0x308a, 0x00},
1065         {0x308b, 0x90},
1066         {0x304c, 0x82},//14Mhz PCLK output 89
1067         //{0x307c, 0x13}, // flip && mirror
1068         {0x307c, 0x11},// by FAE.
1069   {0x3090, 0xc8},
1070         {0x0000 ,0x00},
1071 };
1072 static struct reginfo sensor_qcif_capture[] =
1073 {
1074         {0x3302, 0xef},
1075         {0x335f, 0x68},
1076         {0x3360, 0x18},
1077         {0x3361, 0x0c},
1078         {0x3362, 0x00},
1079         {0x3363, 0xb8},
1080         {0x3364, 0x94},
1081         {0x3403, 0x42},
1082         {0x3088, 0x00},
1083         {0x3089, 0xb0},
1084         {0x308a, 0x00},
1085         {0x308b, 0x90},
1086         {0x304c, 0x84},//14Mhz PCLK output
1087
1088         //{0x307c, 0x13}, // flip && mirror
1089         {0x307c, 0x11}, // by FAE.
1090   {0x3090, 0xc8},
1091         {0x0000 ,0x00},
1092 };
1093 static  struct reginfo *sensor_qcif[2] = {
1094         sensor_qcif_preview,
1095         sensor_qcif_capture,
1096 };
1097
1098 #if 0
1099
1100 static  struct reginfo ov3640_Sharpness_auto[] =
1101 {
1102     {0x3306, 0x00},
1103 };
1104
1105 static  struct reginfo ov3640_Sharpness1[] =
1106 {
1107     {0x3306, 0x08},
1108     {0x3371, 0x00},
1109 };
1110
1111 static  struct reginfo ov3640_Sharpness2[][3] =
1112 {
1113     //Sharpness 2
1114     {0x3306, 0x08},
1115     {0x3371, 0x01},
1116 };
1117
1118 static  struct reginfo ov3640_Sharpness3[] =
1119 {
1120     //default
1121     {0x3306, 0x08},
1122     {0x332d, 0x02},
1123 };
1124 static  struct reginfo ov3640_Sharpness4[]=
1125 {
1126     //Sharpness 4
1127     {0x3306, 0x08},
1128     {0x332d, 0x03},
1129 };
1130
1131 static  struct reginfo ov3640_Sharpness5[] =
1132 {
1133     //Sharpness 5
1134     {0x3306, 0x08},
1135     {0x332d, 0x04},
1136 };
1137 #endif
1138
1139 static  struct reginfo sensor_ClrFmt_YUYV[]=
1140 {
1141     {0x3400, 0x00},
1142     {0x0000, 0x00}
1143 };
1144
1145 static  struct reginfo sensor_ClrFmt_UYVY[]=
1146 {
1147     {0x3400, 0x02},
1148     {0x0000, 0x00}
1149 };
1150
1151 #if CONFIG_SENSOR_WhiteBalance
1152 static  struct reginfo sensor_WhiteB_Auto[]=
1153 {
1154                 {0x332b, 0x00},//AWB auto, bit[3]:0,auto
1155         
1156                 {0x0000, 0x00}
1157 };
1158 /* Cloudy Colour Temperature : 6500K - 8000K  */
1159 static  struct reginfo sensor_WhiteB_Cloudy[]=
1160 {
1161                 {0x332b, 0x08},
1162                 {0x33a7, 0x68},
1163                 {0x33a8, 0x40},
1164                 {0x33a9, 0x4e},
1165                 
1166                 {0x0000, 0x00}
1167 };
1168 /* ClearDay Colour Temperature : 5000K - 6500K  */
1169 static  struct reginfo sensor_WhiteB_ClearDay[]=
1170 {
1171                 //Sunny
1172                 {0x332b, 0x08}, //AWB off
1173                 {0x33a7, 0x5e},
1174                 {0x33a8, 0x40},
1175                 {0x33a9, 0x46},
1176                 
1177                 {0x0000, 0x00}
1178 };
1179 /* Office Colour Temperature : 3500K - 5000K  */
1180 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
1181 {
1182                 //Office
1183                 {0x332b, 0x08},
1184                 {0x33a7, 0x52},
1185                 {0x33a8, 0x40},
1186                 {0x33a9, 0x58},
1187                 
1188                 {0x0000, 0x00}
1189 };
1190 /* Home Colour Temperature : 2500K - 3500K  */
1191 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
1192 {
1193                 //Home
1194                 {0x332b, 0x08},
1195                 {0x33a7, 0x44},
1196                 {0x33a8, 0x40},
1197                 {0x33a9, 0x70},
1198                 
1199                 {0x0000, 0x00}
1200 };
1201 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1202     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1203 };
1204 #endif
1205
1206 #if CONFIG_SENSOR_Brightness
1207 static  struct reginfo sensor_Brightness0[]=
1208 {
1209                 // Brightness -3
1210                 {0x3302, 0xef},
1211                 {0x3355, 0x04},
1212                 {0x3354, 0x09},
1213                 {0x335e, 0x30},
1214                 
1215                 {0x0000, 0x00}
1216 };
1217
1218 static  struct reginfo sensor_Brightness1[]=
1219 {
1220                 // Brightness -2
1221                 {0x3302, 0xef},
1222                 {0x3355, 0x04},
1223                 {0x3354, 0x09},
1224                 {0x335e, 0x20},
1225                 
1226                 {0x0000, 0x00}
1227 };
1228
1229 static  struct reginfo sensor_Brightness2[]=
1230 {
1231                 // Brightness -1
1232                 {0x3302, 0xef},
1233                 {0x3355, 0x04},
1234                 {0x3354, 0x09},
1235                 {0x335e, 0x10},
1236                 
1237                 {0x0000, 0x00}
1238 };
1239
1240 static  struct reginfo sensor_Brightness3[]=
1241 {
1242                 // Brightness 0
1243                 {0x3302, 0xef},
1244                 {0x3355, 0x04},
1245                 {0x3354, 0x01},
1246                 {0x335e, 0x00},
1247                 
1248                 {0x0000, 0x00}
1249 };
1250
1251 static  struct reginfo sensor_Brightness4[]=
1252 {
1253                 //  Brightness +1
1254                 {0x3302, 0xef},
1255                 {0x3355, 0x04},
1256                 {0x3354, 0x01},
1257                 {0x335e, 0x10},
1258                 
1259                 {0x0000, 0x00}
1260 };
1261
1262 static  struct reginfo sensor_Brightness5[]=
1263 {
1264                 //  Brightness +2
1265                 {0x3302, 0xef},
1266                 {0x3355, 0x04},
1267                 {0x3354, 0x01},
1268                 {0x335e, 0x20},
1269                 
1270                 {0x0000, 0x00}
1271 };
1272
1273 static  struct reginfo sensor_Brightness6[]=
1274 {
1275                 //  Brightness +3
1276                 {0x3302, 0xef},
1277                 {0x3355, 0x04}, //bit[2] enable
1278                 {0x3354, 0x01}, //bit[3] sign of brightness
1279                 {0x335e, 0x30},
1280                 
1281                 {0x0000, 0x00}
1282 };
1283
1284 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1285     sensor_Brightness4, sensor_Brightness5,sensor_Brightness6,NULL,
1286 };
1287
1288 #endif
1289
1290 #if CONFIG_SENSOR_Effect
1291 static  struct reginfo sensor_Effect_Normal[] =
1292 {
1293                 //  Normal
1294                 {0x3302, 0xef},
1295                 {0x3355, 0x00},
1296                 
1297                 {0x0000, 0x00}
1298 };
1299
1300 static  struct reginfo sensor_Effect_WandB[] =
1301 {
1302                 // B&W
1303                 {0x3302, 0xef},
1304                 {0x3355, 0x18}, //bit[4]fix u enable, bit[3]fix v enable
1305                 {0x335a, 0x80},
1306                 {0x335b, 0x80},
1307                 
1308                 {0x0000, 0x00}
1309 };
1310
1311 static  struct reginfo sensor_Effect_Sepia[] =
1312 {
1313                 // Sepia
1314                 {0x3302, 0xef},
1315                 {0x3355, 0x18},
1316                 {0x335a, 0x40},
1317                 {0x335b, 0xa6},
1318                 
1319                 {0x0000, 0x00}
1320 };
1321
1322 static  struct reginfo sensor_Effect_Negative[] =
1323 {
1324                 //  Negative
1325                 {0x3302, 0xef},
1326                 {0x3355, 0x40}, //bit[6] negative
1327                 
1328                 {0x0000, 0x00}
1329 };
1330 static  struct reginfo sensor_Effect_Bluish[] =
1331 {
1332                 //  Bluish
1333                 {0x3302, 0xef},
1334                 {0x3355, 0x18},
1335                 {0x335a, 0xa0},
1336                 {0x335b, 0x40},
1337                 
1338                 {0x0000, 0x00}
1339 };
1340
1341 static  struct reginfo sensor_Effect_Green[] =
1342 {
1343                 // Greenish
1344                 {0x3302, 0xef},
1345                 {0x3355, 0x18},
1346                 {0x335a, 0x60},
1347                 {0x335b, 0x60},
1348                 
1349                 {0x0000, 0x00}
1350 };
1351
1352 static  struct reginfo sensor_Effect_Red[] =
1353 {
1354                 // Reddish
1355                 {0x3302, 0xef},
1356                 {0x3355, 0x18},
1357                 {0x335a, 0x80},
1358                 {0x335b, 0xc0},
1359                 
1360                 {0x0000, 0x00}
1361 };
1362 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1363     sensor_Effect_Bluish, sensor_Effect_Green,sensor_Effect_Red,NULL,
1364 };
1365 #endif
1366 #if CONFIG_SENSOR_Exposure
1367 static  struct reginfo sensor_Exposure0[]=
1368 {
1369                 // -1.7EV
1370                 {0x3047, 0x00},
1371                 {0x3018, 0x10},
1372                 {0x3019, 0x08},
1373                 {0x301a, 0x21},
1374                 
1375                 {0x0000, 0x00}
1376 };
1377
1378 static  struct reginfo sensor_Exposure1[]=
1379 {
1380                 // -1.3EV
1381                 {0x3047, 0x00},
1382                 {0x3018, 0x18},
1383                 {0x3019, 0x10},
1384                 {0x301a, 0x31},
1385                 
1386                 {0x0000, 0x00}
1387 };
1388
1389 static  struct reginfo sensor_Exposure2[]=
1390 {
1391                 // -1.0EV
1392                 {0x3047, 0x00},
1393                 {0x3018, 0x20},
1394                 {0x3019, 0x18},
1395                 {0x301a, 0x41},
1396                 
1397                 {0x0000, 0x00}
1398 };
1399
1400 static  struct reginfo sensor_Exposure3[]=
1401 {
1402                 // -0.7EV
1403                 {0x3047, 0x00},
1404                 {0x3018, 0x28},
1405                 {0x3019, 0x20},
1406                 {0x301a, 0x51},
1407                 
1408                 {0x0000, 0x00}
1409 };
1410
1411 static  struct reginfo sensor_Exposure4[]=
1412 {
1413                 // -0.3EV
1414                 {0x3047, 0x00},
1415                 {0x3018, 0x30},
1416                 {0x3019, 0x28},
1417                 {0x301a, 0x61},
1418                 
1419                 {0x0000, 0x00}
1420 };
1421
1422 static  struct reginfo sensor_Exposure5[]=
1423 {
1424                 // default
1425                 {0x3047, 0x00},
1426                 {0x3018, 0x38},
1427                 {0x3019, 0x30},
1428                 {0x301a, 0x61},
1429                 
1430                 {0x0000, 0x00}
1431 };
1432
1433 static  struct reginfo sensor_Exposure6[]=
1434 {
1435                 // +0.3EV
1436                 {0x3047, 0x00},
1437                 {0x3018, 0x40},
1438                 {0x3019, 0x38},
1439                 {0x301a, 0x71},
1440                 
1441                 {0x0000, 0x00}
1442 };
1443
1444 static  struct reginfo sensor_Exposure7[]=
1445 {
1446                 // +0.7EV
1447                 {0x3047, 0x00},
1448                 {0x3018, 0x48},
1449                 {0x3019, 0x40},
1450                 {0x301a, 0x81},
1451                 
1452                 {0x0000, 0x00}
1453 };
1454
1455 static  struct reginfo sensor_Exposure8[]=
1456 {
1457                 // +1.0EV
1458                 {0x3047, 0x00},
1459                 {0x3018, 0x50},
1460                 {0x3019, 0x48},
1461                 {0x301a, 0x91},
1462                 
1463                 {0x0000, 0x00}
1464 };
1465
1466 static  struct reginfo sensor_Exposure9[]=
1467 {
1468                 // 1.3EV
1469                 {0x3047, 0x00},
1470                 {0x3018, 0x58},
1471                 {0x3019, 0x50},
1472                 {0x301a, 0x91},
1473                 
1474                 {0x0000, 0x00}
1475 };
1476
1477 static  struct reginfo sensor_Exposure10[]=
1478 {
1479                 // 1.7EV
1480                 {0x3047, 0x00},
1481                 {0x3018, 0x60},
1482                 {0x3019, 0x58},
1483                 {0x301a, 0xa1},
1484                 
1485                 {0x0000, 0x00}
1486 };
1487
1488 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1489     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,sensor_Exposure7,sensor_Exposure8,sensor_Exposure9,
1490     sensor_Exposure10,NULL,
1491 };
1492 #endif
1493 #if CONFIG_SENSOR_Saturation
1494 static  struct reginfo sensor_Saturation0[]=
1495 {
1496                 // Saturation ¨C 2
1497                 {0x3302, 0xef},
1498                 {0x3355, 0x02},
1499                 {0x3358, 0x10},
1500                 {0x3359, 0x10},
1501                 
1502                 {0x0000, 0x00}
1503 };
1504
1505 static  struct reginfo sensor_Saturation1[]=
1506 {
1507                 // Saturation ¨C 1
1508                 {0x3302, 0xef},
1509                 {0x3355, 0x02},
1510                 {0x3358, 0x30},
1511                 {0x3359, 0x30},
1512                 
1513                 {0x0000, 0x00}
1514 };
1515
1516 static  struct reginfo sensor_Saturation2[]=
1517 {
1518                 // Saturation 0
1519                 {0x3302, 0xef},
1520                 {0x3355, 0x02},
1521                 {0x3358, 0x40},
1522                 {0x3359, 0x40},
1523                 
1524                 {0x0000, 0x00}
1525 };
1526
1527 static  struct reginfo sensor_Saturation3[]=
1528 {
1529                 // Saturation +1
1530                 {0x3302, 0xef},
1531                 {0x3355, 0x02},
1532                 {0x3358, 0x50},
1533                 {0x3359, 0x50},
1534                 
1535                 {0x0000, 0x00}
1536 };
1537
1538 static  struct reginfo sensor_Saturation4[]=
1539 {
1540                 // Saturation +2
1541                 {0x3302, 0xef}, //bit[7]:1, enable SDE
1542                 {0x3355, 0x02}, //enable color saturation
1543                 {0x3358, 0x70},
1544                 {0x3359, 0x70},
1545                 
1546                 {0x0000, 0x00}
1547 };
1548
1549 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, 
1550         sensor_Saturation3, sensor_Saturation4, NULL,};
1551
1552 #endif
1553 #if CONFIG_SENSOR_Contrast
1554 static  struct reginfo sensor_Contrast0[]=
1555 {
1556    // Contrast -3
1557         {0x3302, 0xef},
1558         {0x3355, 0x04},
1559         {0x3354, 0x01},
1560         {0x335c, 0x14},
1561         {0x335d, 0x14},
1562
1563     {0x0000, 0x00}
1564 };
1565
1566 static  struct reginfo sensor_Contrast1[]=
1567 {
1568                 // Contrast -2
1569                 {0x3302, 0xef},
1570                 {0x3355, 0x04},
1571                 {0x3354, 0x01},
1572                 {0x335c, 0x18},
1573                 {0x335d, 0x18},
1574                 
1575                 {0x0000, 0x00}
1576 };
1577
1578 static  struct reginfo sensor_Contrast2[]=
1579 {
1580                 // Contrast -1
1581                 {0x3302, 0xef},
1582                 {0x3355, 0x04},
1583                 {0x3354, 0x01},
1584                 {0x335c, 0x1c},
1585                 {0x335d, 0x1c},
1586                 
1587                 {0x0000, 0x00}
1588 };
1589
1590 static  struct reginfo sensor_Contrast3[]=
1591 {
1592                 // Contrast 0
1593                 {0x3302, 0xef},
1594                 {0x3355, 0x04},
1595                 {0x3354, 0x01},
1596                 {0x335c, 0x20},
1597                 {0x335d, 0x20},
1598                 
1599                 {0x0000, 0x00}
1600 };
1601
1602 static  struct reginfo sensor_Contrast4[]=
1603 {
1604                 // Contrast +1
1605                 {0x3302, 0xef},
1606                 {0x3355, 0x04},
1607                 {0x3354, 0x01},
1608                 {0x335c, 0x24},
1609                 {0x335d, 0x24},
1610                 
1611                 {0x0000, 0x00}
1612 };
1613
1614
1615 static  struct reginfo sensor_Contrast5[]=
1616 {
1617                 // Contrast +2
1618                 {0x3302, 0xef},
1619                 {0x3355, 0x04},
1620                 {0x3354, 0x01},
1621                 {0x335c, 0x28},
1622                 {0x335d, 0x28},
1623                 
1624                 {0x0000, 0x00}
1625 };
1626
1627 static  struct reginfo sensor_Contrast6[]=
1628 {
1629                 // Contrast +3
1630                 {0x3302, 0xef},
1631                 {0x3355, 0x04}, //bit[2] enable contrast/brightness
1632                 {0x3354, 0x01}, //bit[2] Yoffset sign
1633                 {0x335c, 0x2c},
1634                 {0x335d, 0x2c},
1635                 
1636                 {0x0000, 0x00}
1637 };
1638 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1639     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1640 };
1641
1642 #endif
1643 #if CONFIG_SENSOR_Mirror
1644 static  struct reginfo sensor_MirrorOn[]=
1645 {
1646         {0x307c, 0x12}, //mirror
1647         {0x3090, 0xc8},
1648         {0x3023, 0x0a},
1649
1650     {0x0000, 0x00}
1651 };
1652
1653 static  struct reginfo sensor_MirrorOff[]=
1654 {
1655         {0x307c, 0x10},// no mirror/flip
1656         {0x3090, 0xc0},
1657         {0x3023, 0x0a},
1658
1659     {0x0000, 0x00}
1660 };
1661
1662 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1663 #endif
1664 #if CONFIG_SENSOR_Flip
1665 static  struct reginfo sensor_FlipOn[]=
1666 {
1667                 {0x307c, 0x11}, //flip
1668                 {0x3023, 0x09},
1669                 {0x3090, 0xc0},
1670                 
1671                 {0x0000, 0x00}
1672 };
1673
1674 static  struct reginfo sensor_FlipOff[]=
1675 {
1676                 {0x307c, 0x10}, // no mirror/flip
1677                 {0x3090, 0xc0},
1678                 {0x3023, 0x0a},
1679                 
1680                 {0x0000, 0x00}
1681 };
1682 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1683
1684 #endif
1685 #if CONFIG_SENSOR_Scene
1686 static  struct reginfo sensor_SceneAuto[] =
1687 {
1688         {0x3014, 0x84},//bit[3]=0 disable auto night mode
1689         {0x3015, 0x12},// or 0x02
1690         {0x302d, 0x00},//clear dummy frame
1691         {0x302e, 0x00},//clear dummy frame
1692
1693     {0x0000, 0x00}
1694 };
1695
1696 static  struct reginfo sensor_SceneNight[] =
1697 {
1698         {0x3014, 0x8c}, //bit[3] =1 enable auto night mode ¸ù¾Ý¹âÏßÇ¿Èõ×Ô¶¯½µÖ¡
1699         {0x3015, 0x22},//bit[6:4] ÉèÖò»Í¬,½µµÄÖ¡Âʲ»Ò»Ñù.¿ÉÒÔ¿´datasheet
1700                         //bit[6:4]
1701               //000  no dummy frame
1702               //001  1 dummy frame
1703               //010  2 dummy frame
1704               //011  3 dummy frame
1705               //100  7 dummy frame
1706     {0x0000, 0x00}
1707 };
1708 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1709
1710 #endif
1711 #if CONFIG_SENSOR_DigitalZoom
1712 static struct reginfo sensor_Zoom0[] =
1713 {
1714     {0x0, 0x0},
1715 };
1716
1717 static struct reginfo sensor_Zoom1[] =
1718 {
1719      {0x0, 0x0},
1720 };
1721
1722 static struct reginfo sensor_Zoom2[] =
1723 {
1724     {0x0, 0x0},
1725 };
1726
1727
1728 static struct reginfo sensor_Zoom3[] =
1729 {
1730     {0x0, 0x0},
1731 };
1732 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
1733 #endif
1734 static const struct v4l2_querymenu sensor_menus[] =
1735 {
1736         #if CONFIG_SENSOR_WhiteBalance
1737     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1738     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1739     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1740     #endif
1741
1742         #if CONFIG_SENSOR_Effect
1743     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1744     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1745     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1746     #endif
1747
1748         #if CONFIG_SENSOR_Scene
1749     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1750     #endif
1751
1752         #if CONFIG_SENSOR_Flash
1753     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1754     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1755     #endif
1756 };
1757
1758 static const struct v4l2_queryctrl sensor_controls[] =
1759 {
1760         #if CONFIG_SENSOR_WhiteBalance
1761     {
1762         .id             = V4L2_CID_DO_WHITE_BALANCE,
1763         .type           = V4L2_CTRL_TYPE_MENU,
1764         .name           = "White Balance Control",
1765         .minimum        = 0,
1766         .maximum        = 4,
1767         .step           = 1,
1768         .default_value = 0,
1769     },
1770     #endif
1771
1772         #if CONFIG_SENSOR_Brightness
1773         {
1774         .id             = V4L2_CID_BRIGHTNESS,
1775         .type           = V4L2_CTRL_TYPE_INTEGER,
1776         .name           = "Brightness Control",
1777         .minimum        = -3,
1778         .maximum        = 3,
1779         .step           = 1,
1780         .default_value = 0,
1781     },
1782     #endif
1783
1784         #if CONFIG_SENSOR_Effect
1785         {
1786         .id             = V4L2_CID_EFFECT,
1787         .type           = V4L2_CTRL_TYPE_MENU,
1788         .name           = "Effect Control",
1789         .minimum        = 0,
1790         .maximum        = 5,
1791         .step           = 1,
1792         .default_value = 0,
1793     },
1794         #endif
1795
1796         #if CONFIG_SENSOR_Exposure
1797         {
1798         .id             = V4L2_CID_EXPOSURE,
1799         .type           = V4L2_CTRL_TYPE_INTEGER,
1800         .name           = "Exposure Control",
1801         .minimum        = -5,
1802         .maximum        = 5,
1803         .step           = 1,
1804         .default_value = 0,
1805     },
1806         #endif
1807
1808         #if CONFIG_SENSOR_Saturation
1809         {
1810         .id             = V4L2_CID_SATURATION,
1811         .type           = V4L2_CTRL_TYPE_INTEGER,
1812         .name           = "Saturation Control",
1813         .minimum        = -2,
1814         .maximum        = 2,
1815         .step           = 1,
1816         .default_value = 0,
1817     },
1818     #endif
1819
1820         #if CONFIG_SENSOR_Contrast
1821         {
1822         .id             = V4L2_CID_CONTRAST,
1823         .type           = V4L2_CTRL_TYPE_INTEGER,
1824         .name           = "Contrast Control",
1825         .minimum        = -3,
1826         .maximum        = 3,
1827         .step           = 1,
1828         .default_value = 0,
1829     },
1830         #endif
1831
1832         #if CONFIG_SENSOR_Mirror
1833         {
1834         .id             = V4L2_CID_HFLIP,
1835         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1836         .name           = "Mirror Control",
1837         .minimum        = 0,
1838         .maximum        = 1,
1839         .step           = 1,
1840         .default_value = 1,
1841     },
1842     #endif
1843
1844         #if CONFIG_SENSOR_Flip
1845         {
1846         .id             = V4L2_CID_VFLIP,
1847         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1848         .name           = "Flip Control",
1849         .minimum        = 0,
1850         .maximum        = 1,
1851         .step           = 1,
1852         .default_value = 1,
1853     },
1854     #endif
1855
1856         #if CONFIG_SENSOR_Scene
1857     {
1858         .id             = V4L2_CID_SCENE,
1859         .type           = V4L2_CTRL_TYPE_MENU,
1860         .name           = "Scene Control",
1861         .minimum        = 0,
1862         .maximum        = 1,
1863         .step           = 1,
1864         .default_value = 0,
1865     },
1866     #endif
1867
1868         #if CONFIG_SENSOR_DigitalZoom
1869     {
1870         .id             = V4L2_CID_ZOOM_RELATIVE,
1871         .type           = V4L2_CTRL_TYPE_INTEGER,
1872         .name           = "DigitalZoom Control",
1873         .minimum        = -1,
1874         .maximum        = 1,
1875         .step           = 1,
1876         .default_value = 0,
1877     }, {
1878         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1879         .type           = V4L2_CTRL_TYPE_INTEGER,
1880         .name           = "DigitalZoom Control",
1881         .minimum        = 0,
1882         .maximum        = 3,
1883         .step           = 1,
1884         .default_value = 0,
1885     },
1886     #endif
1887
1888         #if CONFIG_SENSOR_Focus
1889         {
1890         .id             = V4L2_CID_FOCUS_RELATIVE,
1891         .type           = V4L2_CTRL_TYPE_INTEGER,
1892         .name           = "Focus Control",
1893         .minimum        = -1,
1894         .maximum        = 1,
1895         .step           = 1,
1896         .default_value = 0,
1897     }, {
1898         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1899         .type           = V4L2_CTRL_TYPE_INTEGER,
1900         .name           = "Focus Control",
1901         .minimum        = 0,
1902         .maximum        = 255,
1903         .step           = 1,
1904         .default_value = 125,
1905     },
1906     {
1907         .id             = V4L2_CID_FOCUS_AUTO,
1908         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1909         .name           = "Focus Control",
1910         .minimum        = 0,
1911         .maximum        = 1,
1912         .step           = 1,
1913         .default_value = 0,
1914     },
1915 #if 0 //IF CONTINOUS IS SUPPORT, SET TO 1
1916     {
1917         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1918         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1919         .name           = "Focus Control",
1920         .minimum        = 0,
1921         .maximum        = 1,
1922         .step           = 1,
1923         .default_value = 0,
1924     },
1925 #endif  
1926     #endif
1927
1928         #if CONFIG_SENSOR_Flash
1929         {
1930         .id             = V4L2_CID_FLASH,
1931         .type           = V4L2_CTRL_TYPE_MENU,
1932         .name           = "Flash Control",
1933         .minimum        = 0,
1934         .maximum        = 3,
1935         .step           = 1,
1936         .default_value = 0,
1937     },
1938         #endif
1939 };
1940
1941 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1942 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1943 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1944 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1945 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1946 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1947 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1948 static int sensor_resume(struct soc_camera_device *icd);
1949 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1950 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1951 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1952 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1953 static int sensor_deactivate(struct i2c_client *client);
1954
1955 static struct soc_camera_ops sensor_ops =
1956 {
1957     .suspend                     = sensor_suspend,
1958     .resume                       = sensor_resume,
1959     .set_bus_param              = sensor_set_bus_param,
1960     .query_bus_param    = sensor_query_bus_param,
1961     .controls           = sensor_controls,
1962     .menus                         = sensor_menus,
1963     .num_controls               = ARRAY_SIZE(sensor_controls),
1964     .num_menus          = ARRAY_SIZE(sensor_menus),
1965 };
1966
1967 /* only one fixed colorspace per pixelcode */
1968 struct sensor_datafmt {
1969         enum v4l2_mbus_pixelcode code;
1970         enum v4l2_colorspace colorspace;
1971 };
1972
1973 /* Find a data format by a pixel code in an array */
1974 static const struct sensor_datafmt *sensor_find_datafmt(
1975         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1976         int n)
1977 {
1978         int i;
1979         for (i = 0; i < n; i++)
1980                 if (fmt[i].code == code)
1981                         return fmt + i;
1982
1983         return NULL;
1984 }
1985
1986 static const struct sensor_datafmt sensor_colour_fmts[] = {
1987     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1988     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1989 };
1990 enum sensor_work_state
1991 {
1992         sensor_work_ready = 0,
1993         sensor_working,
1994 };
1995 struct sensor_work
1996 {
1997         struct i2c_client *client;
1998         struct delayed_work dwork;
1999         enum sensor_work_state state;
2000 };
2001
2002 typedef struct sensor_info_priv_s
2003 {
2004     int whiteBalance;
2005     int brightness;
2006     int contrast;
2007     int saturation;
2008     int effect;
2009     int scene;
2010     int digitalzoom;
2011     int focus;
2012         int auto_focus;
2013         int affm_reinit;
2014     int flash;
2015     int exposure;
2016         bool snap2preview;
2017         bool video2preview;
2018     unsigned char mirror;                                        /* HFLIP */
2019     unsigned char flip;                                          /* VFLIP */
2020     unsigned int winseqe_cur_addr;
2021         struct sensor_datafmt fmt;
2022         unsigned int enable;
2023         unsigned int funmodule_state;
2024 } sensor_info_priv_t;
2025
2026 struct sensor
2027 {
2028     struct v4l2_subdev subdev;
2029     struct i2c_client *client;
2030     sensor_info_priv_t info_priv;
2031         struct workqueue_struct *sensor_wq;
2032         struct sensor_work sensor_wk;
2033         struct mutex wq_lock;
2034     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2035 #if CONFIG_SENSOR_I2C_NOSCHED
2036         atomic_t tasklock_cnt;
2037 #endif
2038         struct rk29camera_platform_data *sensor_io_request;
2039     struct rk29camera_gpio_res *sensor_gpio_res;
2040 };
2041
2042 static struct sensor* to_sensor(const struct i2c_client *client)
2043 {
2044     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2045 }
2046
2047 static int sensor_task_lock(struct i2c_client *client, int lock)
2048 {
2049 #if CONFIG_SENSOR_I2C_NOSCHED
2050         int cnt = 3;
2051     struct sensor *sensor = to_sensor(client);
2052
2053         if (lock) {
2054                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2055                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2056                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2057                                 msleep(35);
2058                                 cnt--;
2059                         }
2060                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2061                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2062                                 goto sensor_task_lock_err;
2063                         }
2064                         preempt_disable();
2065                 }
2066
2067                 atomic_add(1, &sensor->tasklock_cnt);
2068         } else {
2069                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2070                         atomic_sub(1, &sensor->tasklock_cnt);
2071
2072                         if (atomic_read(&sensor->tasklock_cnt) == 0)
2073                                 preempt_enable();
2074                 }
2075         }
2076         return 0;
2077 sensor_task_lock_err:
2078         return -1;   
2079 #else
2080     return 0;
2081 #endif
2082
2083 }
2084
2085 /* sensor register write */
2086 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
2087 {
2088     int err,cnt;
2089     u8 buf[3];
2090     struct i2c_msg msg[1];
2091
2092     buf[0] = reg >> 8;
2093     buf[1] = reg & 0xFF;
2094     buf[2] = val;
2095
2096     msg->addr = client->addr;
2097     msg->flags = client->flags;
2098     msg->buf = buf;
2099     msg->len = sizeof(buf);
2100     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2101     msg->read_type =  0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2102
2103     cnt = 1;
2104     err = -EAGAIN;
2105
2106     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2107         err = i2c_transfer(client->adapter, msg, 1);
2108
2109         if (err >= 0) {
2110             return 0;
2111         } else {
2112             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2113             udelay(10);
2114         }
2115     }
2116
2117     return err;
2118 }
2119
2120 /* sensor register read */
2121 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
2122 {
2123     int err,cnt;
2124     u8 buf[2];
2125     struct i2c_msg msg[2];
2126
2127     buf[0] = reg >> 8;
2128     buf[1] = reg & 0xFF;
2129
2130     msg[0].addr = client->addr;
2131     msg[0].flags = client->flags;
2132     msg[0].buf = buf;
2133     msg[0].len = sizeof(buf);
2134     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2135     msg[0].read_type = 2;   /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2136
2137     msg[1].addr = client->addr;
2138     msg[1].flags = client->flags|I2C_M_RD;
2139     msg[1].buf = buf;
2140     msg[1].len = 1;
2141     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2142     msg[1].read_type = 2;   /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2143
2144     cnt = 1;
2145     err = -EAGAIN;
2146     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2147         err = i2c_transfer(client->adapter, msg, 2);
2148
2149         if (err >= 0) {
2150             *val = buf[0];
2151             return 0;
2152         } else {
2153                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2154             udelay(10);
2155         }
2156     }
2157
2158     return err;
2159 }
2160
2161 /* write a array of registers  */
2162 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2163 {
2164     int err=0, cnt;
2165     int i = 0;
2166 #if CONFIG_SENSOR_Focus
2167         struct sensor *sensor = to_sensor(client);
2168 #endif
2169 #if CONFIG_SENSOR_I2C_RDWRCHK
2170         char valchk;
2171 #endif
2172
2173         cnt = 0;
2174         if (sensor_task_lock(client, 1) < 0)
2175                 goto sensor_write_array_end;
2176     while (regarray[i].reg != 0)
2177     {
2178     #if CONFIG_SENSOR_Focus
2179         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
2180                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
2181                         err = -EINVAL;
2182                         goto sensor_write_array_end;
2183         }
2184                 #endif
2185                 
2186         err = sensor_write(client, regarray[i].reg, regarray[i].val);
2187         if (err < 0)
2188         {
2189             if (cnt-- > 0) {
2190                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2191                                 i = 0;
2192                                 continue;
2193             } else {
2194                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2195
2196                 err = -EPERM;
2197                 goto sensor_write_array_end;
2198             }
2199         } else {
2200         #if CONFIG_SENSOR_I2C_RDWRCHK
2201                         sensor_read(client, regarray[i].reg, &valchk);
2202                         if (valchk != regarray[i].val)
2203                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2204                 #endif
2205         }
2206         i++;
2207     }
2208
2209                 #if CONFIG_SENSOR_Focus
2210         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
2211                 || (regarray == sensor_init_data)) {
2212                 sensor->info_priv.affm_reinit = 1;
2213         }
2214         #endif
2215         
2216 sensor_write_array_end:
2217         sensor_task_lock(client,0);
2218     return err;
2219 }
2220 #if CONFIG_SENSOR_I2C_RDWRCHK
2221 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2222 {
2223     int cnt;
2224     int i = 0;
2225         char valchk;
2226
2227         cnt = 0;
2228         valchk = 0;
2229     while (regarray[i].reg != 0)
2230     {
2231                 sensor_read(client, regarray[i].reg, &valchk);
2232                 if (valchk != regarray[i].val)
2233                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2234
2235         i++;
2236     }
2237     return 0;
2238 }
2239 #endif
2240 #if CONFIG_SENSOR_Focus
2241 struct af_cmdinfo
2242 {
2243         char cmd_tag;
2244         char cmd_para[4];
2245         char validate_bit;
2246 };
2247 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
2248 {
2249         int i;
2250         char read_tag=0xff,cnt;
2251
2252         if (cmdinfo) {
2253                 if (cmdinfo->validate_bit & 0x80) {
2254                         if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
2255                                 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2256                                 goto sensor_af_cmdset_err;
2257                         }
2258                         SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2259                 }
2260                 for (i=0; i<4; i++) {
2261                         if (cmdinfo->validate_bit & (1<<i)) {
2262                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
2263                                         SENSOR_TR("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
2264                                         goto sensor_af_cmdset_err;
2265                                 }
2266                                 SENSOR_DG("%s write CMD_PARA_Reg(main:0x%x para%d:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,i,cmdinfo->cmd_para[i]);
2267                         }
2268                 }
2269         } else {
2270                 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
2271                         SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
2272                         goto sensor_af_cmdset_err;
2273                 }
2274                 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
2275         }
2276
2277         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
2278                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
2279                 goto sensor_af_cmdset_err;
2280         }
2281
2282         cnt = 0;
2283         do
2284         {
2285                 msleep(5);
2286                 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
2287                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
2288                    break;
2289                 }
2290     } while((read_tag != 0x00)&& (cnt++<100));
2291
2292         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
2293         return 0;
2294 sensor_af_cmdset_err:
2295         return -1;
2296 }
2297
2298 static int sensor_af_idlechk(struct i2c_client *client)
2299 {
2300         int ret = 0;
2301         char state,cnt;
2302
2303         cnt = 0;
2304         do
2305         {
2306                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2307                 if (ret != 0){
2308                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2309                    ret = -1;
2310                    goto sensor_af_idlechk_end;
2311                 }
2312
2313                 if (state != S_IDLE) {
2314                         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2315                         msleep(1);
2316                         cnt++;
2317                 }
2318     } while((state != S_IDLE)&& (cnt<100));
2319
2320         ret = (state == S_IDLE) ? 0 : -1;
2321
2322 sensor_af_idlechk_end:
2323         return ret;
2324 }
2325
2326 static int sensor_af_single(struct i2c_client *client)
2327 {
2328         int ret = 0;
2329         char state,cnt;
2330
2331         if (sensor_af_idlechk(client))
2332                 goto sensor_af_single_end;
2333
2334         if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
2335                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
2336                 ret = -1;
2337                 goto sensor_af_single_end;
2338         }
2339
2340         cnt = 0;
2341     do
2342     {
2343         if (cnt != 0) {
2344                         msleep(1);
2345         }
2346         cnt++;
2347                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2348                 if (ret != 0){
2349                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2350                    ret = -1;
2351                    goto sensor_af_single_end;
2352                 }
2353     }while((state == S_FOCUSING) && (cnt<100));
2354
2355         if (state != S_FOCUSED) {
2356         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2357                 ret = -1;
2358                 goto sensor_af_single_end;
2359     }
2360
2361         //sensor_af_cmdset(client, ReturnIdle_Cmd, NULL); // by duanyp. fix af blur when taking pictures
2362 sensor_af_single_end:
2363         return ret;
2364 }
2365
2366 static int sensor_af_const(struct i2c_client *client)
2367 {
2368         int ret = 0;
2369
2370         if (sensor_af_idlechk(client))
2371                 goto sensor_af_const_end;
2372
2373         if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
2374                 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
2375                 ret = -1;
2376                 goto sensor_af_const_end;
2377         }
2378 sensor_af_const_end:
2379         return ret;
2380 }
2381 static int sensor_af_pause2capture(struct i2c_client *client)
2382 {
2383         int ret = 0;
2384         char state,cnt;
2385
2386         if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
2387                 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
2388                 ret = -1;
2389                 goto sensor_af_pause_end;
2390         }
2391
2392         cnt = 0;
2393     do
2394     {
2395         if (cnt != 0) {
2396                         msleep(1);
2397         }
2398         cnt++;
2399                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2400                 if (ret != 0){
2401                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2402                    ret = -1;
2403                    goto sensor_af_pause_end;
2404                 }
2405     }while((state != S_CAPTURE) && (cnt<100));
2406
2407         if (state != S_CAPTURE) {
2408         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2409                 ret = -1;
2410                 goto sensor_af_pause_end;
2411     }
2412 sensor_af_pause_end:
2413         return ret;
2414 }
2415 static int sensor_af_zoneupdate(struct i2c_client *client)
2416 {
2417         int ret = 0;
2418
2419         if (sensor_af_idlechk(client))
2420                 goto sensor_af_zoneupdate_end;
2421
2422         if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
2423                 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
2424                 ret = -1;
2425                 goto sensor_af_zoneupdate_end;
2426         }
2427
2428 sensor_af_zoneupdate_end:
2429         return ret;
2430 }
2431 static int sensor_af_init(struct i2c_client *client)
2432 {
2433         int ret = 0;
2434         char state,cnt;
2435
2436         ret = sensor_write_array(client, sensor_af_firmware);
2437     if (ret != 0) {
2438        SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
2439        ret = -1;
2440            goto sensor_af_init_end;
2441     }
2442
2443         cnt = 0;
2444     do
2445     {
2446         if (cnt != 0) {
2447                         msleep(1);
2448         }
2449         cnt++;
2450                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2451                 if (ret != 0){
2452                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2453                    ret = -1;
2454                    goto sensor_af_init_end;
2455                 }
2456     }while((state == S_STARTUP) && (cnt<100));
2457
2458     if (state != S_IDLE) {
2459         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
2460                 ret = -1;
2461                 goto sensor_af_init_end;
2462     }
2463
2464 sensor_af_init_end:
2465         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
2466         return ret;
2467 }
2468
2469 static int sensor_af_wq_function(struct i2c_client *client)
2470 {
2471         struct sensor *sensor = to_sensor(client);
2472         struct af_cmdinfo cmdinfo;
2473         int ret=0, focus_pos = 0xfe;
2474
2475         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2476
2477         mutex_lock(&sensor->wq_lock);
2478         if (sensor_af_init(client)) {
2479                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2480                 ret = -1;
2481         } else {
2482                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2483
2484                 switch (sensor->info_priv.auto_focus)
2485                 {
2486                         case SENSOR_AF_MODE_INFINITY:
2487                         {
2488                                 focus_pos = 0x00;
2489                         }
2490                         case SENSOR_AF_MODE_MACRO:
2491                         {
2492                                 if (focus_pos != 0x00)
2493                                         focus_pos = 0xff;
2494
2495                                 sensor_af_idlechk(client);
2496                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2497                                 cmdinfo.cmd_para[0] = focus_pos;
2498                                 cmdinfo.validate_bit = 0x81;
2499                                 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2500                                 break;
2501                         }
2502                         case SENSOR_AF_MODE_AUTO:
2503                         {
2504                                 ret = sensor_af_single(client);
2505                                 break;
2506                         }
2507                         case SENSOR_AF_MODE_CONTINUOUS:
2508                         {
2509                                 ret = sensor_af_const(client);
2510                                 break;
2511                         }
2512                         case SENSOR_AF_MODE_CLOSE:
2513                         {
2514                                 ret = 0;
2515                                 break;
2516                         }
2517                         default:
2518             {
2519                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2520                 goto sensor_af_wq_function_end;
2521                         }
2522                 }
2523
2524                 SENSOR_DG("%s sensor_af_wq_function set focus mode(0x%x) ret:0x%x\n",SENSOR_NAME_STRING(), sensor->info_priv.auto_focus,ret);
2525         }
2526
2527 sensor_af_wq_function_end:
2528         sensor->sensor_wk.state = sensor_work_ready;
2529         mutex_unlock(&sensor->wq_lock);
2530         return ret;
2531 }
2532 static void sensor_af_workqueue(struct work_struct *work)
2533 {
2534         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2535         struct i2c_client *client = sensor_work->client;
2536
2537         if (sensor_af_wq_function(client) < 0) {
2538                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
2539         }
2540 }
2541 #endif
2542
2543 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2544 {
2545         struct soc_camera_link *icl = to_soc_camera_link(icd);
2546         int ret = 0;
2547
2548     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2549
2550         switch (cmd)
2551         {
2552                 case Sensor_PowerDown:
2553                 {
2554                         if (icl->powerdown) {
2555                                 ret = icl->powerdown(icd->pdev, on);
2556                                 if (ret == RK29_CAM_IO_SUCCESS) {
2557                                         if (on == 0) {
2558                                                 mdelay(2);
2559                                                 if (icl->reset)
2560                                                         icl->reset(icd->pdev);
2561                                         }
2562                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2563                                         ret = -ENODEV;
2564                                         goto sensor_power_end;
2565                                 }
2566                         }
2567                         break;
2568                 }
2569                 case Sensor_Flash:
2570                 {
2571                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2572                 struct sensor *sensor = to_sensor(client);
2573
2574                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2575                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2576                         }
2577                         break;
2578                 }
2579                 default:
2580                 {
2581                         SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2582                         break;
2583                 }
2584         }
2585
2586 sensor_power_end:
2587         return ret;
2588 }
2589 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2590 {
2591     struct i2c_client *client = sd->priv;
2592     struct soc_camera_device *icd = client->dev.platform_data;
2593     struct sensor *sensor = to_sensor(client);
2594         const struct v4l2_queryctrl *qctrl;
2595     const struct sensor_datafmt *fmt;    
2596     char value;
2597     int ret,pid = 0;
2598
2599     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2600
2601         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2602                 ret = -ENODEV;
2603                 goto sensor_INIT_ERR;
2604         }
2605 /* ddl@rock-chips.com : ov3640_powerOn have hardware reset */
2606 #if 0
2607     /* soft reset */
2608         if (sensor_task_lock(client,1)<0)
2609                 goto sensor_INIT_ERR;
2610     ret = sensor_write(client, 0x3012, 0x80);
2611     if (ret != 0)
2612     {
2613         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2614         ret = -ENODEV;
2615                 goto sensor_INIT_ERR;
2616     }
2617
2618     mdelay(5);  //delay 5 microseconds
2619 #endif
2620         /* check if it is an sensor sensor */
2621     ret = sensor_read(client, 0x300a, &value);
2622     if (ret != 0) {
2623         SENSOR_TR("read chip id high byte failed\n");
2624         ret = -ENODEV;
2625         goto sensor_INIT_ERR;
2626     }
2627
2628     pid |= (value << 8);
2629
2630     ret = sensor_read(client, 0x300b, &value);
2631     if (ret != 0) {
2632         SENSOR_TR("read chip id low byte failed\n");
2633         ret = -ENODEV;
2634         goto sensor_INIT_ERR;
2635     }
2636
2637     pid |= (value & 0xff);
2638     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2639     if (pid == SENSOR_ID) {
2640         sensor->model = SENSOR_V4L2_IDENT;
2641     } else {
2642         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2643         ret = -ENODEV;
2644         goto sensor_INIT_ERR;
2645     }
2646
2647     ret = sensor_write_array(client, sensor_init_data);
2648     if (ret != 0)
2649     {
2650         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2651         goto sensor_INIT_ERR;
2652     }
2653         sensor_task_lock(client,0);
2654
2655     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
2656         fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2657     if (!fmt) {
2658         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2659         ret = -EINVAL;
2660         goto sensor_INIT_ERR;
2661     }
2662         sensor->info_priv.fmt = *fmt;
2663
2664     /* sensor sensor information for initialization  */
2665         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2666         if (qctrl)
2667         sensor->info_priv.whiteBalance = qctrl->default_value;
2668         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2669         if (qctrl)
2670         sensor->info_priv.brightness = qctrl->default_value;
2671         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2672         if (qctrl)
2673         sensor->info_priv.effect = qctrl->default_value;
2674         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2675         if (qctrl)
2676         sensor->info_priv.exposure = qctrl->default_value;
2677
2678         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2679         if (qctrl)
2680         sensor->info_priv.saturation = qctrl->default_value;
2681         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2682         if (qctrl)
2683         sensor->info_priv.contrast = qctrl->default_value;
2684         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2685         if (qctrl)
2686         sensor->info_priv.mirror = qctrl->default_value;
2687         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2688         if (qctrl)
2689         sensor->info_priv.flip = qctrl->default_value;
2690         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2691         if (qctrl)
2692         sensor->info_priv.scene = qctrl->default_value;
2693         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2694         if (qctrl)
2695         sensor->info_priv.digitalzoom = qctrl->default_value;
2696
2697     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2698         #if CONFIG_SENSOR_Focus
2699         //    sensor_set_focus(); 
2700        qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);//?,JYK
2701         if (qctrl)
2702         sensor->info_priv.focus = qctrl->default_value;
2703         #endif
2704
2705         #if CONFIG_SENSOR_Flash
2706         //sensor_set_flash();
2707         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2708         if (qctrl)
2709         sensor->info_priv.flash = qctrl->default_value;
2710     #endif
2711
2712     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);
2713
2714     return 0;
2715 sensor_INIT_ERR:
2716         sensor_task_lock(client,0);
2717         sensor_deactivate(client);
2718     return ret;
2719 }
2720 static int sensor_deactivate(struct i2c_client *client)
2721 {
2722         struct soc_camera_device *icd = client->dev.platform_data;
2723
2724         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2725
2726         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2727         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2728         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2729         icd->user_width = SENSOR_INIT_WIDTH;
2730     icd->user_height = SENSOR_INIT_HEIGHT;
2731         msleep(100);
2732         return 0;
2733 }
2734 static  struct reginfo sensor_power_down_sequence[]=
2735 {
2736                 {0x361e, 0x00},
2737                 {0x308d, 0x06},
2738                 {0x30ad, 0x82},
2739                 {0x308d, 0x0f},
2740                 
2741                 {0x0000, 0x00},
2742 };
2743 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2744 {
2745     int ret;
2746     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2747
2748     if (pm_msg.event == PM_EVENT_SUSPEND) {
2749         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2750         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2751         if (ret != 0) {
2752             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2753             return ret;
2754         } else {
2755             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2756             if (ret < 0) {
2757                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2758                 return -EINVAL;
2759             }
2760         }
2761     } else {
2762         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2763         return -EINVAL;
2764     }
2765
2766     return 0;
2767 }
2768
2769 static int sensor_resume(struct soc_camera_device *icd)
2770 {
2771         int ret;
2772
2773     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2774     if (ret < 0) {
2775                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2776         return -EINVAL;
2777     }
2778
2779         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2780         return 0;
2781 }
2782
2783 static int sensor_set_bus_param(struct soc_camera_device *icd,
2784                                 unsigned long flags)
2785 {
2786
2787     return 0;
2788 }
2789
2790 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2791 {
2792     struct soc_camera_link *icl = to_soc_camera_link(icd);
2793     unsigned long flags = SENSOR_BUS_PARAM;
2794
2795     return soc_camera_apply_sensor_flags(icl, flags);
2796 }
2797
2798 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2799 {
2800     struct i2c_client *client = sd->priv;
2801     struct soc_camera_device *icd = client->dev.platform_data;
2802     struct sensor *sensor = to_sensor(client);
2803
2804     mf->width   = icd->user_width;
2805         mf->height      = icd->user_height;
2806         mf->code        = sensor->info_priv.fmt.code;
2807         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2808         mf->field       = V4L2_FIELD_NONE;
2809
2810     return 0;
2811 }
2812 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2813 {
2814     bool ret = false;
2815
2816         if ((mf->width == 1024) && (mf->height == 768)) {
2817                 ret = true;
2818         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2819                 ret = true;
2820         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2821                 ret = true;
2822         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2823                 ret = true;
2824         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2825                 ret = true;
2826         }
2827
2828         if (ret == true)
2829                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2830         return ret;
2831 }
2832
2833 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2834 {
2835     bool ret = false;
2836
2837         if ((mf->width == 1280) && (mf->height == 720)) {
2838                 ret = true;
2839         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2840                 ret = true;
2841         }
2842
2843         if (ret == true)
2844                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2845         return ret;
2846 }
2847 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2848 {
2849     struct i2c_client *client = sd->priv;
2850     const struct sensor_datafmt *fmt;
2851         struct soc_camera_device *icd = client->dev.platform_data;
2852     struct sensor *sensor = to_sensor(client);
2853     const struct v4l2_queryctrl *qctrl;
2854     struct reginfo *winseqe_set_addr=NULL;
2855     int ret=0, set_w,set_h;
2856         int isCapture = 0; 
2857         
2858         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2859                                    ARRAY_SIZE(sensor_colour_fmts));
2860         if (!fmt) {
2861         ret = -EINVAL;
2862         goto sensor_s_fmt_end;
2863     }
2864
2865         if (sensor->info_priv.fmt.code != mf->code) {
2866                 switch (mf->code)
2867                 {
2868                         case V4L2_MBUS_FMT_YUYV8_2X8:
2869                         {
2870                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2871                                 break;
2872                         }
2873                         case V4L2_MBUS_FMT_UYVY8_2X8:
2874                         {
2875                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2876                                 break;
2877                         }
2878                         default:
2879                                 break;
2880                 }
2881                 if (winseqe_set_addr != NULL) {
2882             sensor_write_array(client, winseqe_set_addr);
2883                         sensor->info_priv.fmt.code = mf->code;
2884             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2885                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2886                 } else {
2887                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2888                 }
2889         }
2890
2891     set_w = mf->width;
2892     set_h = mf->height;
2893         isCapture = sensor_fmt_capturechk(sd, mf);
2894         
2895         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[isCapture][0].reg)
2896         {
2897                 winseqe_set_addr = sensor_qcif[isCapture];
2898         set_w = 176;
2899         set_h = 144;
2900         }
2901         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[isCapture][0].reg)
2902     {
2903         winseqe_set_addr = sensor_qvga[isCapture];
2904         set_w = 320;
2905         set_h = 240;
2906     }
2907     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[isCapture][0].reg)
2908     {
2909         winseqe_set_addr = sensor_cif[isCapture];
2910         set_w = 352;
2911         set_h = 288;
2912     }
2913     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[isCapture][0].reg)
2914     {
2915         winseqe_set_addr = sensor_vga[isCapture];
2916         set_w = 640;
2917         set_h = 480;
2918     }
2919     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[isCapture][0].reg)
2920     {
2921         winseqe_set_addr = sensor_svga[isCapture];
2922         set_w = 800;
2923         set_h = 600;
2924     }
2925     else if (((set_w <= 1024) && (set_h <= 768)) && sensor_sxga[isCapture][0].reg)
2926     {
2927         winseqe_set_addr = sensor_xga[isCapture];
2928         set_w = 1024;
2929         set_h = 768;
2930     }
2931     else if (((set_w <= 1280) && (set_h <= 960)) && sensor_sxga[isCapture][0].reg)
2932     {
2933         winseqe_set_addr = sensor_sxga[isCapture];
2934         set_w = 1280;
2935         set_h = 960;
2936     }
2937     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[isCapture][0].reg)
2938     {
2939         winseqe_set_addr = sensor_uxga[isCapture];
2940         set_w = 1600;
2941         set_h = 1200;
2942     }
2943     else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_uxga[isCapture][0].reg)
2944     {
2945         winseqe_set_addr = sensor_qxga[isCapture];
2946         set_w = 2048;
2947         set_h = 1536;
2948     }
2949     else
2950     {
2951         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2952         set_w = SENSOR_INIT_WIDTH;
2953         set_h = SENSOR_INIT_HEIGHT;             
2954                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2955     }
2956
2957     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr){
2958                 //srt --if capture,then should write sensor_qxga[1] first               
2959                 if((winseqe_set_addr != sensor_qxga[isCapture]) && isCapture) {
2960                         SENSOR_DG("%s  write sensor_qxga[1]\n", SENSOR_NAME_STRING());
2961                         ret = sensor_write_array(client, sensor_qxga[isCapture]);
2962                         if (ret != 0) {
2963                             SENSOR_TR("%s  write sensor_qxga[1] failed\n", SENSOR_NAME_STRING());
2964                             return ret;
2965                         }
2966                 }
2967
2968                 #if CONFIG_SENSOR_Focus
2969                         //sensor_af_idlechk(client);
2970                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2971                         {
2972                                 sensor_af_idlechk(client); // by duanyp
2973                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2974                         }
2975                 #endif
2976
2977         #if CONFIG_SENSOR_Flash
2978         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
2979             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2980                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2981                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2982             }           
2983         } else {                                        /* ddl@rock-chips.com : Video */
2984             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2985                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2986                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2987             }
2988         }
2989         #endif
2990                 
2991         ret = sensor_write_array(client, winseqe_set_addr);        
2992         if (ret != 0) {
2993             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2994             #if CONFIG_SENSOR_Flash
2995             if (sensor_fmt_capturechk(sd,mf) == true) {
2996                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2997                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2998                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2999                 }
3000             }
3001             #endif
3002             goto sensor_s_fmt_end;
3003         } else {
3004             sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
3005             if (sensor_fmt_capturechk(sd,mf) == true) {                             /* ddl@rock-chips.com : Capture */
3006                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3007                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3008                         if (sensor->info_priv.whiteBalance != 0) {
3009                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3010                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3011                         }
3012                         sensor->info_priv.snap2preview = true;
3013                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
3014                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3015                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3016                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3017                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3018                         sensor->info_priv.video2preview = true;
3019                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
3020                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3021                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3022                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3023                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3024                         sensor->info_priv.video2preview = false;
3025                         sensor->info_priv.snap2preview = false;
3026                 }
3027             mdelay(100);  // by FAE.
3028         }
3029         SENSOR_DG("\n%s..%s.. icd->width=%d..icd->height=%d..isCapture=%d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h,isCapture);
3030     } else {
3031         SENSOR_TR("\n %s .. Current Format is validate. icd->width=%d..icd->height=%d..isCapture=%d\n",SENSOR_NAME_STRING(),set_w,set_h,isCapture);
3032     }
3033     mf->width = set_w;
3034         mf->height = set_h;
3035 sensor_s_fmt_end:
3036     return ret;
3037 }
3038
3039 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3040 {
3041     struct i2c_client *client = sd->priv;
3042     struct sensor *sensor = to_sensor(client);
3043     const struct sensor_datafmt *fmt;
3044     int ret = 0;
3045    
3046         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3047                                    ARRAY_SIZE(sensor_colour_fmts));
3048         if (fmt == NULL) {
3049                 fmt = &sensor->info_priv.fmt;
3050         mf->code = fmt->code;
3051         } 
3052
3053     if (mf->height > SENSOR_MAX_HEIGHT)
3054         mf->height = SENSOR_MAX_HEIGHT;
3055     else if (mf->height < SENSOR_MIN_HEIGHT)
3056         mf->height = SENSOR_MIN_HEIGHT;
3057
3058     if (mf->width > SENSOR_MAX_WIDTH)
3059         mf->width = SENSOR_MAX_WIDTH;
3060     else if (mf->width < SENSOR_MIN_WIDTH)
3061         mf->width = SENSOR_MIN_WIDTH;
3062
3063     mf->colorspace = fmt->colorspace;
3064     
3065     return ret;
3066 }
3067
3068  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3069 {
3070     struct i2c_client *client = sd->priv;
3071
3072     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3073         return -EINVAL;
3074
3075     if (id->match.addr != client->addr)
3076         return -ENODEV;
3077
3078     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV3640  identifier */
3079     id->revision = 0;
3080
3081     return 0;
3082 }
3083 #if CONFIG_SENSOR_Brightness
3084 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3085 {
3086     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3087
3088     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3089     {
3090         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3091         {
3092             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3093             {
3094                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3095                 return -EINVAL;
3096             }
3097             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3098             return 0;
3099         }
3100     }
3101         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3102     return -EINVAL;
3103 }
3104 #endif
3105 #if CONFIG_SENSOR_Effect
3106 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3107 {
3108     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3109
3110     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3111     {
3112         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3113         {
3114             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3115             {
3116                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3117                 return -EINVAL;
3118             }
3119             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3120             return 0;
3121         }
3122     }
3123         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3124     return -EINVAL;
3125 }
3126 #endif
3127 #if CONFIG_SENSOR_Exposure
3128 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3129 {
3130     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3131
3132     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3133     {
3134         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3135         {
3136             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3137             {
3138                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3139                 return -EINVAL;
3140             }
3141             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3142             return 0;
3143         }
3144     }
3145         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3146     return -EINVAL;
3147 }
3148 #endif
3149 #if CONFIG_SENSOR_Saturation
3150 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3151 {
3152     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3153
3154     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3155     {
3156         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3157         {
3158             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3159             {
3160                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3161                 return -EINVAL;
3162             }
3163             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3164             return 0;
3165         }
3166     }
3167     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3168     return -EINVAL;
3169 }
3170 #endif
3171 #if CONFIG_SENSOR_Contrast
3172 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3173 {
3174     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3175
3176     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3177     {
3178         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3179         {
3180             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3181             {
3182                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3183                 return -EINVAL;
3184             }
3185             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3186             return 0;
3187         }
3188     }
3189     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3190     return -EINVAL;
3191 }
3192 #endif
3193 #if CONFIG_SENSOR_Mirror
3194 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3195 {
3196     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3197
3198     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3199     {
3200         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3201         {
3202             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3203             {
3204                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3205                 return -EINVAL;
3206             }
3207             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3208             return 0;
3209         }
3210     }
3211     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3212     return -EINVAL;
3213 }
3214 #endif
3215 #if CONFIG_SENSOR_Flip
3216 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3217 {
3218     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3219
3220     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3221     {
3222         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3223         {
3224             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3225             {
3226                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3227                 return -EINVAL;
3228             }
3229             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3230             return 0;
3231         }
3232     }
3233     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3234     return -EINVAL;
3235 }
3236 #endif
3237 #if CONFIG_SENSOR_Scene
3238 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3239 {
3240     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3241
3242     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3243     {
3244         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3245         {
3246             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3247             {
3248                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3249                 return -EINVAL;
3250             }
3251             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3252             return 0;
3253         }
3254     }
3255     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3256     return -EINVAL;
3257 }
3258 #endif
3259 #if CONFIG_SENSOR_WhiteBalance
3260 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3261 {
3262     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3263
3264     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3265     {
3266         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3267         {
3268             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3269             {
3270                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3271                 return -EINVAL;
3272             }
3273             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3274             return 0;
3275         }
3276     }
3277         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3278     return -EINVAL;
3279 }
3280 #endif
3281 #if CONFIG_SENSOR_DigitalZoom
3282 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3283 {
3284     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3285     struct sensor *sensor = to_sensor(client);
3286         const struct v4l2_queryctrl *qctrl_info;
3287     int digitalzoom_cur, digitalzoom_total;
3288
3289         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3290         if (qctrl_info)
3291                 return -EINVAL;
3292
3293     digitalzoom_cur = sensor->info_priv.digitalzoom;
3294     digitalzoom_total = qctrl_info->maximum;
3295
3296     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3297     {
3298         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3299         return -EINVAL;
3300     }
3301
3302     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3303     {
3304         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3305         return -EINVAL;
3306     }
3307
3308     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3309     {
3310         *value = digitalzoom_total - digitalzoom_cur;
3311     }
3312
3313     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3314     {
3315         *value = 0 - digitalzoom_cur;
3316     }
3317
3318     digitalzoom_cur += *value;
3319
3320     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3321     {
3322         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3323         {
3324             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3325             return -EINVAL;
3326         }
3327         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3328         return 0;
3329     }
3330
3331     return -EINVAL;
3332 }
3333 #endif
3334
3335 #if CONFIG_SENSOR_Focus
3336 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3337 {
3338         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3339     struct sensor *sensor = to_sensor(client);
3340         const struct v4l2_queryctrl *qctrl_info;
3341         struct af_cmdinfo cmdinfo;
3342         int ret = 0;
3343
3344         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3345         if (!qctrl_info)
3346                 return -EINVAL;
3347
3348         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3349                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3350
3351                         if (sensor_af_idlechk(client))
3352                                 goto sensor_set_focus_absolute_end;
3353
3354                         cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3355                         cmdinfo.cmd_para[0] = value;
3356                         cmdinfo.validate_bit = 0x81;
3357                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3358                         //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3359                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3360                 } else {
3361                         ret = -EINVAL;
3362                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3363                 }
3364         } else {
3365                 ret = -EACCES;
3366                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3367                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3368         }
3369
3370 sensor_set_focus_absolute_end:
3371         return ret;
3372 }
3373 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3374 {
3375         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3376         struct sensor *sensor = to_sensor(client);
3377         const struct v4l2_queryctrl *qctrl_info;
3378         struct af_cmdinfo cmdinfo;
3379         int ret = 0;
3380
3381         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3382         if (!qctrl_info)
3383                 return -EINVAL;
3384
3385         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3386                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3387
3388                         if (sensor_af_idlechk(client))
3389                                 goto sensor_set_focus_relative_end;
3390
3391                         if (value > 0) {
3392                                 cmdinfo.cmd_tag = StepFocus_Near_Tag;
3393                         } else if (value < 0) {
3394                                 cmdinfo.cmd_tag = StepFocus_Far_Tag;
3395                         }
3396                         cmdinfo.validate_bit = 0x80;
3397                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3398
3399                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3400                 } else {
3401                         ret = -EINVAL;
3402                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3403                 }
3404         } else {
3405                 ret = -EACCES;
3406                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3407                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3408         }
3409 sensor_set_focus_relative_end:
3410         return ret;
3411 }
3412
3413 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3414 {
3415         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3416         struct sensor *sensor = to_sensor(client);
3417         int ret = 0;
3418
3419         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
3420                 switch (value)
3421                 {
3422                         case SENSOR_AF_MODE_AUTO:
3423                         {
3424                                 ret = sensor_af_single(client);                         
3425                                 break;
3426                         }
3427
3428                         case SENSOR_AF_MODE_MACRO:
3429                         {
3430                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3431                                 break;
3432                         }
3433
3434                         case SENSOR_AF_MODE_INFINITY:
3435                         {
3436                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3437                                 break;
3438                         }
3439
3440                         case SENSOR_AF_MODE_CONTINUOUS:
3441                         {
3442                                 ret = sensor_af_const(client);
3443                                 break;
3444                         }
3445                         default:
3446                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3447                                 break;
3448
3449                 }
3450
3451                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3452         } else {
3453                 ret = -EACCES;
3454                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3455                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3456         }
3457
3458         return ret;
3459 }
3460 #endif
3461 #if CONFIG_SENSOR_Flash
3462 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3463 {    
3464     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3465         if (value == 3) {       /* ddl@rock-chips.com: torch */
3466             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3467         } else {
3468             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3469         }
3470         SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3471         return 0;
3472     }
3473     
3474         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3475     return -EINVAL;
3476 }
3477 #endif
3478 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3479 {
3480     struct i2c_client *client = sd->priv;
3481     struct sensor *sensor = to_sensor(client);
3482     const struct v4l2_queryctrl *qctrl;
3483
3484     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3485
3486     if (!qctrl)
3487     {
3488         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3489         return -EINVAL;
3490     }
3491
3492     switch (ctrl->id)
3493     {
3494         case V4L2_CID_BRIGHTNESS:
3495             {
3496                 ctrl->value = sensor->info_priv.brightness;
3497                 break;
3498             }
3499         case V4L2_CID_SATURATION:
3500             {
3501                 ctrl->value = sensor->info_priv.saturation;
3502                 break;
3503             }
3504         case V4L2_CID_CONTRAST:
3505             {
3506                 ctrl->value = sensor->info_priv.contrast;
3507                 break;
3508             }
3509         case V4L2_CID_DO_WHITE_BALANCE:
3510             {
3511                 ctrl->value = sensor->info_priv.whiteBalance;
3512                 break;
3513             }
3514         case V4L2_CID_EXPOSURE:
3515             {
3516                 ctrl->value = sensor->info_priv.exposure;
3517                 break;
3518             }
3519         case V4L2_CID_HFLIP:
3520             {
3521                 ctrl->value = sensor->info_priv.mirror;
3522                 break;
3523             }
3524         case V4L2_CID_VFLIP:
3525             {
3526                 ctrl->value = sensor->info_priv.flip;
3527                 break;
3528             }
3529         default :
3530                 break;
3531     }
3532     return 0;
3533 }
3534
3535
3536
3537 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3538 {
3539     struct i2c_client *client = sd->priv;
3540     struct sensor *sensor = to_sensor(client);
3541     struct soc_camera_device *icd = client->dev.platform_data;
3542     const struct v4l2_queryctrl *qctrl;
3543
3544
3545     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3546
3547     if (!qctrl)
3548     {
3549         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3550         return -EINVAL;
3551     }
3552
3553     switch (ctrl->id)
3554     {
3555 #if CONFIG_SENSOR_Brightness
3556         case V4L2_CID_BRIGHTNESS:
3557             {
3558                 if (ctrl->value != sensor->info_priv.brightness)
3559                 {
3560                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3561                     {
3562                         return -EINVAL;
3563                     }
3564                     sensor->info_priv.brightness = ctrl->value;
3565                 }
3566                 break;
3567             }
3568 #endif
3569 #if CONFIG_SENSOR_Exposure
3570         case V4L2_CID_EXPOSURE:
3571             {
3572                 if (ctrl->value != sensor->info_priv.exposure)
3573                 {
3574                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3575                     {
3576                         return -EINVAL;
3577                     }
3578                     sensor->info_priv.exposure = ctrl->value;
3579                 }
3580                 break;
3581             }
3582 #endif
3583 #if CONFIG_SENSOR_Saturation
3584         case V4L2_CID_SATURATION:
3585             {
3586                 if (ctrl->value != sensor->info_priv.saturation)
3587                 {
3588                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3589                     {
3590                         return -EINVAL;
3591                     }
3592                     sensor->info_priv.saturation = ctrl->value;
3593                 }
3594                 break;
3595             }
3596 #endif
3597 #if CONFIG_SENSOR_Contrast
3598         case V4L2_CID_CONTRAST:
3599             {
3600                 if (ctrl->value != sensor->info_priv.contrast)
3601                 {
3602                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3603                     {
3604                         return -EINVAL;
3605                     }
3606                     sensor->info_priv.contrast = ctrl->value;
3607                 }
3608                 break;
3609             }
3610 #endif
3611 #if CONFIG_SENSOR_WhiteBalance
3612         case V4L2_CID_DO_WHITE_BALANCE:
3613             {
3614                 if (ctrl->value != sensor->info_priv.whiteBalance)
3615                 {
3616                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3617                     {
3618                         return -EINVAL;
3619                     }
3620                     sensor->info_priv.whiteBalance = ctrl->value;
3621                 }
3622                 break;
3623             }
3624 #endif
3625 #if CONFIG_SENSOR_Mirror
3626         case V4L2_CID_HFLIP:
3627             {
3628                 if (ctrl->value != sensor->info_priv.mirror)
3629                 {
3630                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3631                         return -EINVAL;
3632                     sensor->info_priv.mirror = ctrl->value;
3633                 }
3634                 break;
3635             }
3636 #endif
3637 #if CONFIG_SENSOR_Flip
3638         case V4L2_CID_VFLIP:
3639             {
3640                 if (ctrl->value != sensor->info_priv.flip)
3641                 {
3642                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3643                         return -EINVAL;
3644                     sensor->info_priv.flip = ctrl->value;
3645                 }
3646                 break;
3647             }
3648 #endif
3649         default:
3650             break;
3651     }
3652
3653     return 0;
3654 }
3655 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3656 {
3657     const struct v4l2_queryctrl *qctrl;
3658     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3659     struct sensor *sensor = to_sensor(client);
3660
3661     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3662
3663     if (!qctrl)
3664     {
3665         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3666         return -EINVAL;
3667     }
3668
3669     switch (ext_ctrl->id)
3670     {
3671         case V4L2_CID_SCENE:
3672             {
3673                 ext_ctrl->value = sensor->info_priv.scene;
3674                 break;
3675             }
3676         case V4L2_CID_EFFECT:
3677             {
3678                 ext_ctrl->value = sensor->info_priv.effect;
3679                 break;
3680             }
3681         case V4L2_CID_ZOOM_ABSOLUTE:
3682             {
3683                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3684                 break;
3685             }
3686         case V4L2_CID_ZOOM_RELATIVE:
3687             {
3688                 return -EINVAL;
3689             }
3690         case V4L2_CID_FOCUS_ABSOLUTE:
3691             {
3692                 ext_ctrl->value = sensor->info_priv.focus;
3693                 break;
3694             }
3695         case V4L2_CID_FOCUS_RELATIVE:
3696             {
3697                 return -EINVAL;
3698             }
3699         case V4L2_CID_FLASH:
3700             {
3701                 ext_ctrl->value = sensor->info_priv.flash;
3702                 break;
3703             }
3704         default :
3705             break;
3706     }
3707     return 0;
3708 }
3709 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3710 {
3711     const struct v4l2_queryctrl *qctrl;
3712     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3713     struct sensor *sensor = to_sensor(client);
3714     int val_offset;
3715
3716     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3717
3718     if (!qctrl)
3719     {
3720         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3721         return -EINVAL;
3722     }
3723
3724         val_offset = 0;
3725     switch (ext_ctrl->id)
3726     {
3727 #if CONFIG_SENSOR_Scene
3728         case V4L2_CID_SCENE:
3729             {
3730                 if (ext_ctrl->value != sensor->info_priv.scene)
3731                 {
3732                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3733                         return -EINVAL;
3734                     sensor->info_priv.scene = ext_ctrl->value;
3735                 }
3736                 break;
3737             }
3738 #endif
3739 #if CONFIG_SENSOR_Effect
3740         case V4L2_CID_EFFECT:
3741             {
3742                 if (ext_ctrl->value != sensor->info_priv.effect)
3743                 {
3744                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3745                         return -EINVAL;
3746                     sensor->info_priv.effect= ext_ctrl->value;
3747                 }
3748                 break;
3749             }
3750 #endif
3751 #if CONFIG_SENSOR_DigitalZoom
3752         case V4L2_CID_ZOOM_ABSOLUTE:
3753             {
3754                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3755                     return -EINVAL;
3756
3757                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3758                 {
3759                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3760
3761                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3762                         return -EINVAL;
3763                     sensor->info_priv.digitalzoom += val_offset;
3764
3765                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3766                 }
3767
3768                 break;
3769             }
3770         case V4L2_CID_ZOOM_RELATIVE:
3771             {
3772                 if (ext_ctrl->value)
3773                 {
3774                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3775                         return -EINVAL;
3776                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3777
3778                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3779                 }
3780                 break;
3781             }
3782 #endif
3783 #if CONFIG_SENSOR_Focus
3784         case V4L2_CID_FOCUS_ABSOLUTE:
3785             {
3786                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3787                     return -EINVAL;
3788
3789                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3790                                         if (ext_ctrl->value == qctrl->minimum) {
3791                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3792                                         } else if (ext_ctrl->value == qctrl->maximum) {
3793                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3794                                         } else {
3795                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3796                                         }
3797                                 }
3798
3799                 break;
3800             }           
3801          case V4L2_CID_FOCUS_RELATIVE:
3802             {
3803                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3804                     return -EINVAL;
3805
3806                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3807                 break;
3808             }
3809                 case V4L2_CID_FOCUS_AUTO:
3810                         {
3811                                 if (ext_ctrl->value == 1) {
3812                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3813                                                 return -EINVAL;
3814                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3815                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3816                                         if (ext_ctrl->value == 0)
3817                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3818                                 }
3819                                 break;
3820                         }
3821                 /*
3822                 case V4L2_CID_FOCUS_CONTINUOUS:
3823                         {
3824                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3825                                         if (ext_ctrl->value == 1) {
3826                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3827                                                         return -EINVAL;
3828                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3829                                         }
3830                                 } else {
3831                                         if (ext_ctrl->value == 0)
3832                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3833                                 }
3834                                 break;
3835                         }*/
3836 #endif
3837 #if CONFIG_SENSOR_Flash
3838         case V4L2_CID_FLASH:
3839             {
3840                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3841                     return -EINVAL;
3842                 sensor->info_priv.flash = ext_ctrl->value;
3843
3844                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3845                 break;
3846             }
3847 #endif
3848         default:
3849             break;
3850     }
3851
3852     return 0;
3853 }
3854
3855 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3856 {
3857     struct i2c_client *client = sd->priv;
3858     struct soc_camera_device *icd = client->dev.platform_data;
3859     int i, error_cnt=0, error_idx=-1;
3860
3861
3862     for (i=0; i<ext_ctrl->count; i++) {
3863         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3864             error_cnt++;
3865             error_idx = i;
3866         }
3867     }
3868
3869     if (error_cnt > 1)
3870         error_idx = ext_ctrl->count;
3871
3872     if (error_idx != -1) {
3873         ext_ctrl->error_idx = error_idx;
3874         return -EINVAL;
3875     } else {
3876         return 0;
3877     }
3878 }
3879
3880 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3881 {
3882     struct i2c_client *client = sd->priv;
3883     struct soc_camera_device *icd = client->dev.platform_data;
3884     int i, error_cnt=0, error_idx=-1;
3885
3886
3887     for (i=0; i<ext_ctrl->count; i++) {
3888         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3889             error_cnt++;
3890             error_idx = i;
3891         }
3892     }
3893
3894     if (error_cnt > 1)
3895         error_idx = ext_ctrl->count;
3896
3897     if (error_idx != -1) {
3898         ext_ctrl->error_idx = error_idx;
3899         return -EINVAL;
3900     } else {
3901         return 0;
3902     }
3903 }
3904
3905 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3906 {
3907         struct i2c_client *client = sd->priv;
3908     struct sensor *sensor = to_sensor(client);
3909         #if CONFIG_SENSOR_Focus
3910         struct soc_camera_device *icd = client->dev.platform_data;
3911         struct v4l2_mbus_framefmt mf;
3912         #endif
3913
3914         if (enable == 1) {
3915                 sensor->info_priv.enable = 1;
3916                 #if CONFIG_SENSOR_Focus
3917                 mf.width        = icd->user_width;
3918         mf.height       = icd->user_height;
3919         mf.code = sensor->info_priv.fmt.code;
3920         mf.colorspace   = sensor->info_priv.fmt.colorspace;
3921         mf.field        = V4L2_FIELD_NONE;
3922                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3923                 if (sensor_fmt_capturechk(sd, &mf) == false) {
3924                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3925                                 if (sensor->sensor_wq != NULL) {
3926                                         mutex_lock(&sensor->wq_lock);
3927                                         if (sensor->sensor_wk.state == sensor_working) {
3928                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3929                                                 mutex_unlock(&sensor->wq_lock);
3930                                                 goto sensor_s_stream_end;
3931                                         }
3932                                         sensor->sensor_wk.state = sensor_working;
3933                                         mutex_unlock(&sensor->wq_lock);
3934                                         sensor->sensor_wk.client = client;
3935                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3936                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3937                                 }
3938                                 sensor->info_priv.affm_reinit = 0;
3939                         }
3940                 }
3941                 #endif
3942         } else if (enable == 0) {
3943                 sensor->info_priv.enable = 0;
3944                 #if CONFIG_SENSOR_Focus
3945                 flush_work(&(sensor->sensor_wk.dwork.work));
3946                 mutex_lock(&sensor->wq_lock);
3947                 sensor->sensor_wk.state = sensor_work_ready;
3948                 mutex_unlock(&sensor->wq_lock);
3949                 #endif
3950         }
3951
3952 sensor_s_stream_end:
3953         return 0;
3954 }
3955
3956 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3957  * this wasn't our capture interface, so, we wait for the right one */
3958 static int sensor_video_probe(struct soc_camera_device *icd,
3959                                struct i2c_client *client)
3960 {
3961     char value;
3962     int ret,pid = 0;
3963     struct sensor *sensor = to_sensor(client);
3964
3965     /* We must have a parent by now. And it cannot be a wrong one.
3966      * So this entire test is completely redundant. */
3967     if (!icd->dev.parent ||
3968             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3969                 return -ENODEV;
3970
3971         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3972                 ret = -ENODEV;
3973                 goto sensor_video_probe_err;
3974         }
3975     /* soft reset */
3976     ret = sensor_write(client, 0x3012, 0x80);
3977     if (ret != 0) {
3978         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3979         ret = -ENODEV;
3980                 goto sensor_video_probe_err;
3981     }
3982     mdelay(5);          //delay 5 microseconds
3983
3984     /* check if it is an sensor sensor */
3985     ret = sensor_read(client, 0x300a, &value);
3986     if (ret != 0) {
3987         SENSOR_TR("read chip id high byte failed\n");
3988         ret = -ENODEV;
3989         goto sensor_video_probe_err;
3990     }
3991
3992     pid |= (value << 8);
3993
3994     ret = sensor_read(client, 0x300b, &value);
3995     if (ret != 0) {
3996         SENSOR_TR("read chip id low byte failed\n");
3997         ret = -ENODEV;
3998         goto sensor_video_probe_err;
3999     }
4000
4001     pid |= (value & 0xff);
4002     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4003     if (pid == SENSOR_ID) {
4004         sensor->model = SENSOR_V4L2_IDENT;
4005     } else {
4006         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
4007         ret = -ENODEV;
4008         goto sensor_video_probe_err;
4009     }
4010     return 0;
4011
4012 sensor_video_probe_err:
4013     return ret;
4014 }
4015 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
4016 {
4017         struct i2c_client *client = sd->priv;
4018     struct soc_camera_device *icd = client->dev.platform_data;
4019     struct sensor *sensor = to_sensor(client);
4020     int ret = 0,i;
4021
4022         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4023         switch (cmd)
4024         {
4025                 case RK29_CAM_SUBDEV_DEACTIVATE:
4026                 {
4027                         sensor_deactivate(client);
4028                         break;
4029                 }
4030                 case RK29_CAM_SUBDEV_IOREQUEST:
4031                 {
4032                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
4033             if (sensor->sensor_io_request != NULL) { 
4034                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
4035                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
4036                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
4037                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
4038                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
4039                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
4040                 }
4041             } else {
4042                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4043                 ret = -EINVAL;
4044                 goto sensor_ioctl_end;
4045             }
4046             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
4047                for this project */
4048             #if CONFIG_SENSOR_Flash     
4049                 if (sensor->sensor_gpio_res) {
4050                 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
4051                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4052                     for (i = 0; i < icd->ops->num_controls; i++) {
4053                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4054                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
4055                                 }
4056                     }
4057                     sensor->info_priv.flash = 0xff;
4058                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4059                 }
4060                 }
4061             #endif
4062                         break;
4063                 }
4064                 default:
4065                 {
4066                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4067                         break;
4068                 }
4069         }
4070
4071 sensor_ioctl_end:
4072         return ret;
4073
4074 }
4075 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4076                             enum v4l2_mbus_pixelcode *code)
4077 {
4078         if (index >= ARRAY_SIZE(sensor_colour_fmts))
4079                 return -EINVAL;
4080
4081         *code = sensor_colour_fmts[index].code;
4082         return 0;
4083 }
4084 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4085         .init           = sensor_init,
4086         .g_ctrl         = sensor_g_control,
4087         .s_ctrl         = sensor_s_control,
4088         .g_ext_ctrls          = sensor_g_ext_controls,
4089         .s_ext_ctrls          = sensor_s_ext_controls,
4090         .g_chip_ident   = sensor_g_chip_ident,
4091         .ioctl = sensor_ioctl,
4092 };
4093
4094 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4095         .s_mbus_fmt     = sensor_s_fmt,
4096         .g_mbus_fmt     = sensor_g_fmt,
4097         .try_mbus_fmt   = sensor_try_fmt,
4098         .enum_mbus_fmt  = sensor_enum_fmt,
4099         .s_stream   = sensor_s_stream,
4100 };
4101
4102 static struct v4l2_subdev_ops sensor_subdev_ops = {
4103         .core   = &sensor_subdev_core_ops,
4104         .video = &sensor_subdev_video_ops,
4105 };
4106
4107 static int sensor_probe(struct i2c_client *client,
4108                          const struct i2c_device_id *did)
4109 {
4110     struct sensor *sensor;
4111     struct soc_camera_device *icd = client->dev.platform_data;
4112     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4113     struct soc_camera_link *icl;
4114     int ret;
4115
4116     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4117     if (!icd) {
4118         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4119         return -EINVAL;
4120     }
4121
4122     icl = to_soc_camera_link(icd);
4123     if (!icl) {
4124         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4125         return -EINVAL;
4126     }
4127
4128     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4129         dev_warn(&adapter->dev,
4130                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4131         return -EIO;
4132     }
4133
4134     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4135     if (!sensor)
4136         return -ENOMEM;
4137
4138     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4139
4140     /* Second stage probe - when a capture adapter is there */
4141     icd->ops            = &sensor_ops;
4142     sensor->info_priv.fmt = sensor_colour_fmts[0];
4143         #if CONFIG_SENSOR_I2C_NOSCHED
4144         atomic_set(&sensor->tasklock_cnt,0);
4145         #endif
4146
4147     ret = sensor_video_probe(icd, client);
4148     if (ret < 0) {
4149         icd->ops = NULL;
4150         i2c_set_clientdata(client, NULL);
4151         kfree(sensor);
4152         sensor = NULL;
4153     } else {
4154                 #if CONFIG_SENSOR_Focus
4155                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
4156                 if (sensor->sensor_wq == NULL)
4157                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
4158                 mutex_init(&sensor->wq_lock);
4159                 sensor->sensor_wk.state = sensor_work_ready;
4160                 #endif
4161     }
4162     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4163     return ret;
4164 }
4165
4166 static int sensor_remove(struct i2c_client *client)
4167 {
4168     struct sensor *sensor = to_sensor(client);
4169     struct soc_camera_device *icd = client->dev.platform_data;
4170
4171         #if CONFIG_SENSOR_Focus
4172         if (sensor->sensor_wq) {
4173                 destroy_workqueue(sensor->sensor_wq);
4174                 sensor->sensor_wq = NULL;
4175         }
4176         #endif
4177         
4178     icd->ops = NULL;
4179     i2c_set_clientdata(client, NULL);
4180     client->driver = NULL;
4181     kfree(sensor);
4182     sensor = NULL;
4183     return 0;
4184 }
4185
4186 static const struct i2c_device_id sensor_id[] = {
4187         {SENSOR_NAME_STRING(), 0 },
4188         { }
4189 };
4190 MODULE_DEVICE_TABLE(i2c, sensor_id);
4191
4192 static struct i2c_driver sensor_i2c_driver = {
4193         .driver = {
4194                 .name = SENSOR_NAME_STRING(),
4195         },
4196         .probe          = sensor_probe,
4197         .remove         = sensor_remove,
4198         .id_table       = sensor_id,
4199 };
4200
4201 static int __init sensor_mod_init(void)
4202 {
4203     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4204     return i2c_add_driver(&sensor_i2c_driver);
4205 }
4206
4207 static void __exit sensor_mod_exit(void)
4208 {
4209     i2c_del_driver(&sensor_i2c_driver);
4210 }
4211
4212 device_initcall_sync(sensor_mod_init);
4213 module_exit(sensor_mod_exit);
4214
4215 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4216 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4217 MODULE_LICENSE("GPL");
4218