51f16390be054fba53c68a5f0b44678d8f092233
[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(0, 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_PIX_FMT_YUYV
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 #define COL_FMT(_name, _depth, _fourcc, _colorspace) \
1968         { .name = _name, .depth = _depth, .fourcc = _fourcc, \
1969         .colorspace = _colorspace }
1970
1971 #define JPG_FMT(_name, _depth, _fourcc) \
1972         COL_FMT(_name, _depth, _fourcc, V4L2_COLORSPACE_JPEG)
1973
1974 static const struct soc_camera_data_format sensor_colour_formats[] = {
1975         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1976         JPG_FMT(SENSOR_NAME_STRING(YUYV), 16, V4L2_PIX_FMT_YUYV),
1977 };
1978
1979 enum sensor_work_state
1980 {
1981         sensor_work_ready = 0,
1982         sensor_working,
1983 };
1984 struct sensor_work
1985 {
1986         struct i2c_client *client;
1987         struct delayed_work dwork;
1988         enum sensor_work_state state;
1989 };
1990
1991 typedef struct sensor_info_priv_s
1992 {
1993     int whiteBalance;
1994     int brightness;
1995     int contrast;
1996     int saturation;
1997     int effect;
1998     int scene;
1999     int digitalzoom;
2000     int focus;
2001         int auto_focus;
2002         int affm_reinit;
2003     int flash;
2004     int exposure;
2005         bool snap2preview;
2006         bool video2preview;
2007     unsigned char mirror;                                        /* HFLIP */
2008     unsigned char flip;                                          /* VFLIP */
2009     unsigned int winseqe_cur_addr;
2010         unsigned int pixfmt;
2011         unsigned int enable;
2012         unsigned int funmodule_state;
2013 } sensor_info_priv_t;
2014
2015 struct sensor
2016 {
2017     struct v4l2_subdev subdev;
2018     struct i2c_client *client;
2019     sensor_info_priv_t info_priv;
2020         struct workqueue_struct *sensor_wq;
2021         struct sensor_work sensor_wk;
2022         struct mutex wq_lock;
2023     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2024 #if CONFIG_SENSOR_I2C_NOSCHED
2025         atomic_t tasklock_cnt;
2026 #endif
2027         struct rk29camera_platform_data *sensor_io_request;
2028     struct rk29camera_gpio_res *sensor_gpio_res;
2029 };
2030
2031 static struct sensor* to_sensor(const struct i2c_client *client)
2032 {
2033     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2034 }
2035
2036 static int sensor_task_lock(struct i2c_client *client, int lock)
2037 {
2038 #if CONFIG_SENSOR_I2C_NOSCHED
2039         int cnt = 3;
2040     struct sensor *sensor = to_sensor(client);
2041
2042         if (lock) {
2043                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2044                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2045                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2046                                 msleep(35);
2047                                 cnt--;
2048                         }
2049                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2050                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2051                                 goto sensor_task_lock_err;
2052                         }
2053                         preempt_disable();
2054                 }
2055
2056                 atomic_add(1, &sensor->tasklock_cnt);
2057         } else {
2058                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2059                         atomic_sub(1, &sensor->tasklock_cnt);
2060
2061                         if (atomic_read(&sensor->tasklock_cnt) == 0)
2062                                 preempt_enable();
2063                 }
2064         }
2065         return 0;
2066 sensor_task_lock_err:
2067         return -1;   
2068 #else
2069     return 0;
2070 #endif
2071
2072 }
2073
2074 /* sensor register write */
2075 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
2076 {
2077     int err,cnt;
2078     u8 buf[3];
2079     struct i2c_msg msg[1];
2080
2081     buf[0] = reg >> 8;
2082     buf[1] = reg & 0xFF;
2083     buf[2] = val;
2084
2085     msg->addr = client->addr;
2086     msg->flags = client->flags;
2087     msg->buf = buf;
2088     msg->len = sizeof(buf);
2089     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2090     msg->read_type =  0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2091
2092     cnt = 1;
2093     err = -EAGAIN;
2094
2095     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2096         err = i2c_transfer(client->adapter, msg, 1);
2097
2098         if (err >= 0) {
2099             return 0;
2100         } else {
2101             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2102             udelay(10);
2103         }
2104     }
2105
2106     return err;
2107 }
2108
2109 /* sensor register read */
2110 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
2111 {
2112     int err,cnt;
2113     u8 buf[2];
2114     struct i2c_msg msg[2];
2115
2116     buf[0] = reg >> 8;
2117     buf[1] = reg & 0xFF;
2118
2119     msg[0].addr = client->addr;
2120     msg[0].flags = client->flags;
2121     msg[0].buf = buf;
2122     msg[0].len = sizeof(buf);
2123     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2124     msg[0].read_type = 2;   /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2125
2126     msg[1].addr = client->addr;
2127     msg[1].flags = client->flags|I2C_M_RD;
2128     msg[1].buf = buf;
2129     msg[1].len = 1;
2130     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2131     msg[1].read_type = 2;   /* fpga i2c:2==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2132
2133     cnt = 1;
2134     err = -EAGAIN;
2135     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2136         err = i2c_transfer(client->adapter, msg, 2);
2137
2138         if (err >= 0) {
2139             *val = buf[0];
2140             return 0;
2141         } else {
2142                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2143             udelay(10);
2144         }
2145     }
2146
2147     return err;
2148 }
2149
2150 /* write a array of registers  */
2151 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2152 {
2153     int err=0, cnt;
2154     int i = 0;
2155 #if CONFIG_SENSOR_Focus
2156         struct sensor *sensor = to_sensor(client);
2157 #endif
2158 #if CONFIG_SENSOR_I2C_RDWRCHK
2159         char valchk;
2160 #endif
2161
2162         cnt = 0;
2163         if (sensor_task_lock(client, 1) < 0)
2164                 goto sensor_write_array_end;
2165     while (regarray[i].reg != 0)
2166     {
2167     #if CONFIG_SENSOR_Focus
2168         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
2169                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
2170                         err = -EINVAL;
2171                         goto sensor_write_array_end;
2172         }
2173                 #endif
2174                 
2175         err = sensor_write(client, regarray[i].reg, regarray[i].val);
2176         if (err < 0)
2177         {
2178             if (cnt-- > 0) {
2179                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2180                                 i = 0;
2181                                 continue;
2182             } else {
2183                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2184
2185                 err = -EPERM;
2186                 goto sensor_write_array_end;
2187             }
2188         } else {
2189         #if CONFIG_SENSOR_I2C_RDWRCHK
2190                         sensor_read(client, regarray[i].reg, &valchk);
2191                         if (valchk != regarray[i].val)
2192                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2193                 #endif
2194         }
2195         i++;
2196     }
2197
2198                 #if CONFIG_SENSOR_Focus
2199         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
2200                 || (regarray == sensor_init_data)) {
2201                 sensor->info_priv.affm_reinit = 1;
2202         }
2203         #endif
2204         
2205 sensor_write_array_end:
2206         sensor_task_lock(client,0);
2207     return err;
2208 }
2209 #if CONFIG_SENSOR_I2C_RDWRCHK
2210 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2211 {
2212     int cnt;
2213     int i = 0;
2214         char valchk;
2215
2216         cnt = 0;
2217         valchk = 0;
2218     while (regarray[i].reg != 0)
2219     {
2220                 sensor_read(client, regarray[i].reg, &valchk);
2221                 if (valchk != regarray[i].val)
2222                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2223
2224         i++;
2225     }
2226     return 0;
2227 }
2228 #endif
2229 #if CONFIG_SENSOR_Focus
2230 struct af_cmdinfo
2231 {
2232         char cmd_tag;
2233         char cmd_para[4];
2234         char validate_bit;
2235 };
2236 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
2237 {
2238         int i;
2239         char read_tag=0xff,cnt;
2240
2241         if (cmdinfo) {
2242                 if (cmdinfo->validate_bit & 0x80) {
2243                         if (sensor_write(client, CMD_TAG_Reg, cmdinfo->cmd_tag)) {
2244                                 SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2245                                 goto sensor_af_cmdset_err;
2246                         }
2247                         SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
2248                 }
2249                 for (i=0; i<4; i++) {
2250                         if (cmdinfo->validate_bit & (1<<i)) {
2251                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
2252                                         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]);
2253                                         goto sensor_af_cmdset_err;
2254                                 }
2255                                 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]);
2256                         }
2257                 }
2258         } else {
2259                 if (sensor_write(client, CMD_TAG_Reg, 0xff)) {
2260                         SENSOR_TR("%s write CMD_TAG_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
2261                         goto sensor_af_cmdset_err;
2262                 }
2263                 SENSOR_DG("%s write CMD_TAG_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
2264         }
2265
2266         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
2267                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
2268                 goto sensor_af_cmdset_err;
2269         }
2270
2271         cnt = 0;
2272         do
2273         {
2274                 msleep(5);
2275                 if (sensor_read(client,CMD_TAG_Reg,&read_tag)){
2276                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
2277                    break;
2278                 }
2279     } while((read_tag != 0x00)&& (cnt++<100));
2280
2281         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
2282         return 0;
2283 sensor_af_cmdset_err:
2284         return -1;
2285 }
2286
2287 static int sensor_af_idlechk(struct i2c_client *client)
2288 {
2289         int ret = 0;
2290         char state,cnt;
2291
2292         cnt = 0;
2293         do
2294         {
2295                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2296                 if (ret != 0){
2297                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2298                    ret = -1;
2299                    goto sensor_af_idlechk_end;
2300                 }
2301
2302                 if (state != S_IDLE) {
2303                         sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
2304                         msleep(1);
2305                         cnt++;
2306                 }
2307     } while((state != S_IDLE)&& (cnt<100));
2308
2309         ret = (state == S_IDLE) ? 0 : -1;
2310
2311 sensor_af_idlechk_end:
2312         return ret;
2313 }
2314
2315 static int sensor_af_single(struct i2c_client *client)
2316 {
2317         int ret = 0;
2318         char state,cnt;
2319
2320         if (sensor_af_idlechk(client))
2321                 goto sensor_af_single_end;
2322
2323         if (sensor_af_cmdset(client, SingleFocus_Cmd, NULL)) {
2324                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
2325                 ret = -1;
2326                 goto sensor_af_single_end;
2327         }
2328
2329         cnt = 0;
2330     do
2331     {
2332         if (cnt != 0) {
2333                         msleep(1);
2334         }
2335         cnt++;
2336                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2337                 if (ret != 0){
2338                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2339                    ret = -1;
2340                    goto sensor_af_single_end;
2341                 }
2342     }while((state == S_FOCUSING) && (cnt<100));
2343
2344         if (state != S_FOCUSED) {
2345         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2346                 ret = -1;
2347                 goto sensor_af_single_end;
2348     }
2349
2350         //sensor_af_cmdset(client, ReturnIdle_Cmd, NULL); // by duanyp. fix af blur when taking pictures
2351 sensor_af_single_end:
2352         return ret;
2353 }
2354
2355 static int sensor_af_const(struct i2c_client *client)
2356 {
2357         int ret = 0;
2358
2359         if (sensor_af_idlechk(client))
2360                 goto sensor_af_const_end;
2361
2362         if (sensor_af_cmdset(client, ConstFocus_Cmd, NULL)) {
2363                 SENSOR_TR("%s const focus mode set error!\n",SENSOR_NAME_STRING());
2364                 ret = -1;
2365                 goto sensor_af_const_end;
2366         }
2367 sensor_af_const_end:
2368         return ret;
2369 }
2370 static int sensor_af_pause2capture(struct i2c_client *client)
2371 {
2372         int ret = 0;
2373         char state,cnt;
2374
2375         if (sensor_af_cmdset(client, PauseFocus_Cmd, NULL)) {
2376                 SENSOR_TR("%s pause focus mode set error!\n",SENSOR_NAME_STRING());
2377                 ret = -1;
2378                 goto sensor_af_pause_end;
2379         }
2380
2381         cnt = 0;
2382     do
2383     {
2384         if (cnt != 0) {
2385                         msleep(1);
2386         }
2387         cnt++;
2388                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2389                 if (ret != 0){
2390                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2391                    ret = -1;
2392                    goto sensor_af_pause_end;
2393                 }
2394     }while((state != S_CAPTURE) && (cnt<100));
2395
2396         if (state != S_CAPTURE) {
2397         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
2398                 ret = -1;
2399                 goto sensor_af_pause_end;
2400     }
2401 sensor_af_pause_end:
2402         return ret;
2403 }
2404 static int sensor_af_zoneupdate(struct i2c_client *client)
2405 {
2406         int ret = 0;
2407
2408         if (sensor_af_idlechk(client))
2409                 goto sensor_af_zoneupdate_end;
2410
2411         if (sensor_af_cmdset(client, UpdateZone_Cmd, NULL)) {
2412                 SENSOR_TR("%s update zone fail!\n",SENSOR_NAME_STRING());
2413                 ret = -1;
2414                 goto sensor_af_zoneupdate_end;
2415         }
2416
2417 sensor_af_zoneupdate_end:
2418         return ret;
2419 }
2420 static int sensor_af_init(struct i2c_client *client)
2421 {
2422         int ret = 0;
2423         char state,cnt;
2424
2425         ret = sensor_write_array(client, sensor_af_firmware);
2426     if (ret != 0) {
2427        SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
2428        ret = -1;
2429            goto sensor_af_init_end;
2430     }
2431
2432         cnt = 0;
2433     do
2434     {
2435         if (cnt != 0) {
2436                         msleep(1);
2437         }
2438         cnt++;
2439                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
2440                 if (ret != 0){
2441                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
2442                    ret = -1;
2443                    goto sensor_af_init_end;
2444                 }
2445     }while((state == S_STARTUP) && (cnt<100));
2446
2447     if (state != S_IDLE) {
2448         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
2449                 ret = -1;
2450                 goto sensor_af_init_end;
2451     }
2452
2453 sensor_af_init_end:
2454         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
2455         return ret;
2456 }
2457
2458 static int sensor_af_wq_function(struct i2c_client *client)
2459 {
2460         struct sensor *sensor = to_sensor(client);
2461         struct af_cmdinfo cmdinfo;
2462         int ret=0, focus_pos = 0xfe;
2463
2464         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
2465
2466         mutex_lock(&sensor->wq_lock);
2467         if (sensor_af_init(client)) {
2468                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
2469                 ret = -1;
2470         } else {
2471                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
2472
2473                 switch (sensor->info_priv.auto_focus)
2474                 {
2475                         case SENSOR_AF_MODE_INFINITY:
2476                         {
2477                                 focus_pos = 0x00;
2478                         }
2479                         case SENSOR_AF_MODE_MACRO:
2480                         {
2481                                 if (focus_pos != 0x00)
2482                                         focus_pos = 0xff;
2483
2484                                 sensor_af_idlechk(client);
2485                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
2486                                 cmdinfo.cmd_para[0] = focus_pos;
2487                                 cmdinfo.validate_bit = 0x81;
2488                                 ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
2489                                 break;
2490                         }
2491                         case SENSOR_AF_MODE_AUTO:
2492                         {
2493                                 ret = sensor_af_single(client);
2494                                 break;
2495                         }
2496                         case SENSOR_AF_MODE_CONTINUOUS:
2497                         {
2498                                 ret = sensor_af_const(client);
2499                                 break;
2500                         }
2501                         case SENSOR_AF_MODE_CLOSE:
2502                         {
2503                                 ret = 0;
2504                                 break;
2505                         }
2506                         default:
2507             {
2508                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
2509                 goto sensor_af_wq_function_end;
2510                         }
2511                 }
2512
2513                 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);
2514         }
2515
2516 sensor_af_wq_function_end:
2517         sensor->sensor_wk.state = sensor_work_ready;
2518         mutex_unlock(&sensor->wq_lock);
2519         return ret;
2520 }
2521 static void sensor_af_workqueue(struct work_struct *work)
2522 {
2523         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
2524         struct i2c_client *client = sensor_work->client;
2525
2526         if (sensor_af_wq_function(client) < 0) {
2527                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
2528         }
2529 }
2530 #endif
2531
2532 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2533 {
2534         struct soc_camera_link *icl = to_soc_camera_link(icd);
2535         int ret = 0;
2536
2537     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2538
2539         switch (cmd)
2540         {
2541                 case Sensor_PowerDown:
2542                 {
2543                         if (icl->powerdown) {
2544                                 ret = icl->powerdown(icd->pdev, on);
2545                                 if (ret == RK29_CAM_IO_SUCCESS) {
2546                                         if (on == 0) {
2547                                                 mdelay(2);
2548                                                 if (icl->reset)
2549                                                         icl->reset(icd->pdev);
2550                                         }
2551                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2552                                         ret = -ENODEV;
2553                                         goto sensor_power_end;
2554                                 }
2555                         }
2556                         break;
2557                 }
2558                 case Sensor_Flash:
2559                 {
2560                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2561                 struct sensor *sensor = to_sensor(client);
2562
2563                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2564                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2565                         }
2566                         break;
2567                 }
2568                 default:
2569                 {
2570                         SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2571                         break;
2572                 }
2573         }
2574
2575 sensor_power_end:
2576         return ret;
2577 }
2578 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2579 {
2580     struct i2c_client *client = sd->priv;
2581     struct soc_camera_device *icd = client->dev.platform_data;
2582     struct sensor *sensor = to_sensor(client);
2583         const struct v4l2_queryctrl *qctrl;
2584     char value;
2585     int ret,pid = 0;
2586
2587     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2588
2589         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2590                 ret = -ENODEV;
2591                 goto sensor_INIT_ERR;
2592         }
2593 /* ddl@rock-chips.com : ov3640_powerOn have hardware reset */
2594 #if 0
2595     /* soft reset */
2596         if (sensor_task_lock(client,1)<0)
2597                 goto sensor_INIT_ERR;
2598     ret = sensor_write(client, 0x3012, 0x80);
2599     if (ret != 0)
2600     {
2601         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2602         ret = -ENODEV;
2603                 goto sensor_INIT_ERR;
2604     }
2605
2606     mdelay(5);  //delay 5 microseconds
2607 #endif
2608         /* check if it is an sensor sensor */
2609     ret = sensor_read(client, 0x300a, &value);
2610     if (ret != 0) {
2611         SENSOR_TR("read chip id high byte failed\n");
2612         ret = -ENODEV;
2613         goto sensor_INIT_ERR;
2614     }
2615
2616     pid |= (value << 8);
2617
2618     ret = sensor_read(client, 0x300b, &value);
2619     if (ret != 0) {
2620         SENSOR_TR("read chip id low byte failed\n");
2621         ret = -ENODEV;
2622         goto sensor_INIT_ERR;
2623     }
2624
2625     pid |= (value & 0xff);
2626     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2627     if (pid == SENSOR_ID) {
2628         sensor->model = SENSOR_V4L2_IDENT;
2629     } else {
2630         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2631         ret = -ENODEV;
2632         goto sensor_INIT_ERR;
2633     }
2634
2635     ret = sensor_write_array(client, sensor_init_data);
2636     if (ret != 0)
2637     {
2638         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2639         goto sensor_INIT_ERR;
2640     }
2641         sensor_task_lock(client,0);
2642     //icd->user_width = SENSOR_INIT_WIDTH;
2643     //icd->user_height = SENSOR_INIT_HEIGHT;
2644     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
2645         sensor->info_priv.pixfmt = SENSOR_INIT_PIXFMT;
2646
2647     /* sensor sensor information for initialization  */
2648         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2649         if (qctrl)
2650         sensor->info_priv.whiteBalance = qctrl->default_value;
2651         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2652         if (qctrl)
2653         sensor->info_priv.brightness = qctrl->default_value;
2654         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2655         if (qctrl)
2656         sensor->info_priv.effect = qctrl->default_value;
2657         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2658         if (qctrl)
2659         sensor->info_priv.exposure = qctrl->default_value;
2660
2661         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2662         if (qctrl)
2663         sensor->info_priv.saturation = qctrl->default_value;
2664         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2665         if (qctrl)
2666         sensor->info_priv.contrast = qctrl->default_value;
2667         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2668         if (qctrl)
2669         sensor->info_priv.mirror = qctrl->default_value;
2670         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2671         if (qctrl)
2672         sensor->info_priv.flip = qctrl->default_value;
2673         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2674         if (qctrl)
2675         sensor->info_priv.scene = qctrl->default_value;
2676         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2677         if (qctrl)
2678         sensor->info_priv.digitalzoom = qctrl->default_value;
2679
2680     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2681         #if CONFIG_SENSOR_Focus
2682         //    sensor_set_focus(); 
2683        qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);//?,JYK
2684         if (qctrl)
2685         sensor->info_priv.focus = qctrl->default_value;
2686         #endif
2687
2688         #if CONFIG_SENSOR_Flash
2689         //sensor_set_flash();
2690         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2691         if (qctrl)
2692         sensor->info_priv.flash = qctrl->default_value;
2693     #endif
2694
2695     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);
2696
2697     return 0;
2698 sensor_INIT_ERR:
2699         sensor_task_lock(client,0);
2700         sensor_deactivate(client);
2701     return ret;
2702 }
2703 static int sensor_deactivate(struct i2c_client *client)
2704 {
2705         struct soc_camera_device *icd = client->dev.platform_data;
2706
2707         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2708
2709         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2710         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2711         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2712         icd->user_width = SENSOR_INIT_WIDTH;
2713     icd->user_height = SENSOR_INIT_HEIGHT;
2714         msleep(100);
2715         return 0;
2716 }
2717 static  struct reginfo sensor_power_down_sequence[]=
2718 {
2719                 {0x361e, 0x00},
2720                 {0x308d, 0x06},
2721                 {0x30ad, 0x82},
2722                 {0x308d, 0x0f},
2723                 
2724                 {0x0000, 0x00},
2725 };
2726 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2727 {
2728     int ret;
2729     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2730
2731     if (pm_msg.event == PM_EVENT_SUSPEND) {
2732         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2733         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2734         if (ret != 0) {
2735             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2736             return ret;
2737         } else {
2738             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2739             if (ret < 0) {
2740                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2741                 return -EINVAL;
2742             }
2743         }
2744     } else {
2745         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2746         return -EINVAL;
2747     }
2748
2749     return 0;
2750 }
2751
2752 static int sensor_resume(struct soc_camera_device *icd)
2753 {
2754         int ret;
2755
2756     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2757     if (ret < 0) {
2758                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2759         return -EINVAL;
2760     }
2761
2762         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2763         return 0;
2764 }
2765
2766 static int sensor_set_bus_param(struct soc_camera_device *icd,
2767                                 unsigned long flags)
2768 {
2769
2770     return 0;
2771 }
2772
2773 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2774 {
2775     struct soc_camera_link *icl = to_soc_camera_link(icd);
2776     unsigned long flags = SENSOR_BUS_PARAM;
2777
2778     return soc_camera_apply_sensor_flags(icl, flags);
2779 }
2780
2781 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2782 {
2783     struct i2c_client *client = sd->priv;
2784     struct soc_camera_device *icd = client->dev.platform_data;
2785     struct sensor *sensor = to_sensor(client);
2786     struct v4l2_pix_format *pix = &f->fmt.pix;
2787
2788     pix->width          = icd->user_width;
2789     pix->height         = icd->user_height;
2790     pix->pixelformat    = sensor->info_priv.pixfmt;
2791     pix->field          = V4L2_FIELD_NONE;
2792     pix->colorspace             = V4L2_COLORSPACE_JPEG;
2793
2794     return 0;
2795 }
2796
2797
2798 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_format *f)
2799 {
2800     bool ret = false;
2801
2802         if ((f->fmt.pix.width == 1024) && (f->fmt.pix.height == 768)) {
2803                 ret = true;
2804         } else if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 1024)) {
2805                 ret = true;
2806         } else if ((f->fmt.pix.width == 1600) && (f->fmt.pix.height == 1200)) {
2807                 ret = true;
2808         } else if ((f->fmt.pix.width == 2048) && (f->fmt.pix.height == 1536)) {
2809                 ret = true;
2810         } else if ((f->fmt.pix.width == 2592) && (f->fmt.pix.height == 1944)) {
2811                 ret = true;
2812         }
2813
2814         if (ret == true)
2815                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2816         return ret;
2817 }
2818 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_format *f)
2819 {
2820     bool ret = false;
2821
2822         if ((f->fmt.pix.width == 1280) && (f->fmt.pix.height == 720)) {
2823                 ret = true;
2824         } else if ((f->fmt.pix.width == 1920) && (f->fmt.pix.height == 1080)) {
2825                 ret = true;
2826         }
2827
2828         if (ret == true)
2829                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, f->fmt.pix.width, f->fmt.pix.height);
2830         return ret;
2831 }
2832 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
2833 {
2834     struct i2c_client *client = sd->priv;
2835         struct soc_camera_device *icd = client->dev.platform_data;
2836     struct sensor *sensor = to_sensor(client);
2837     struct v4l2_pix_format *pix = &f->fmt.pix;
2838     const struct v4l2_queryctrl *qctrl;
2839     struct reginfo *winseqe_set_addr=NULL;
2840     int ret=0, set_w,set_h;
2841         int isCapture = 0; 
2842         
2843         if (sensor->info_priv.pixfmt != pix->pixelformat) {
2844                 switch (pix->pixelformat)
2845                 {
2846                         case V4L2_PIX_FMT_YUYV:
2847                         {
2848                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2849                                 break;
2850                         }
2851                         case V4L2_PIX_FMT_UYVY:
2852                         {
2853                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2854                                 break;
2855                         }
2856                         default:
2857                                 break;
2858                 }
2859                 if (winseqe_set_addr != NULL) {
2860             sensor_write_array(client, winseqe_set_addr);
2861                         sensor->info_priv.pixfmt = pix->pixelformat;
2862
2863                         SENSOR_DG("%s Pixelformat(0x%x) set success!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2864                 } else {
2865                         SENSOR_TR("%s Pixelformat(0x%x) is invalidate!\n", SENSOR_NAME_STRING(),pix->pixelformat);
2866                 }
2867         }
2868
2869     set_w = pix->width;
2870     set_h = pix->height;
2871         isCapture = sensor_fmt_capturechk(sd, f);
2872         
2873         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[isCapture][0].reg)
2874         {
2875                 winseqe_set_addr = sensor_qcif[isCapture];
2876         set_w = 176;
2877         set_h = 144;
2878         }
2879         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[isCapture][0].reg)
2880     {
2881         winseqe_set_addr = sensor_qvga[isCapture];
2882         set_w = 320;
2883         set_h = 240;
2884     }
2885     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[isCapture][0].reg)
2886     {
2887         //printk("===> isCapture: %d!\n", isCapture);
2888         winseqe_set_addr = sensor_cif[isCapture];
2889         set_w = 352;
2890         set_h = 288;
2891     }
2892     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[isCapture][0].reg)
2893     {
2894         winseqe_set_addr = sensor_vga[isCapture];
2895         set_w = 640;
2896         set_h = 480;
2897     }
2898     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[isCapture][0].reg)
2899     {
2900         winseqe_set_addr = sensor_svga[isCapture];
2901         set_w = 800;
2902         set_h = 600;
2903     }
2904     else if (((set_w <= 1024) && (set_h <= 768)) && sensor_sxga[isCapture][0].reg)
2905     {
2906         winseqe_set_addr = sensor_xga[isCapture];
2907         set_w = 1024;
2908         set_h = 768;
2909     }
2910     else if (((set_w <= 1280) && (set_h <= 960)) && sensor_sxga[isCapture][0].reg)
2911     {
2912         winseqe_set_addr = sensor_sxga[isCapture];
2913         set_w = 1280;
2914         set_h = 960;
2915     }
2916     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[isCapture][0].reg)
2917     {
2918         winseqe_set_addr = sensor_uxga[isCapture];
2919         set_w = 1600;
2920         set_h = 1200;
2921     }
2922     else if (((set_w <= 2048) && (set_h <= 1536)) && sensor_uxga[isCapture][0].reg)
2923     {
2924         winseqe_set_addr = sensor_qxga[isCapture];
2925         set_w = 2048;
2926         set_h = 1536;
2927     }
2928     else
2929     {
2930         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2931         set_w = SENSOR_INIT_WIDTH;
2932         set_h = SENSOR_INIT_HEIGHT;             
2933                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,pix->width,pix->height);
2934     }
2935
2936     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
2937     {
2938                 //srt --if capture,then should write sensor_qxga[1] first
2939                 
2940                 if((winseqe_set_addr != sensor_qxga[isCapture]) && isCapture)
2941                 {
2942                         SENSOR_DG("%s  write sensor_qxga[1]\n", SENSOR_NAME_STRING());
2943                         ret = sensor_write_array(client, sensor_qxga[isCapture]);
2944                         if (ret != 0)
2945                         {
2946                             SENSOR_TR("%s  write sensor_qxga[1] failed\n", SENSOR_NAME_STRING());
2947                             return ret;
2948                         }
2949                 }
2950
2951                 #if CONFIG_SENSOR_Focus
2952                         //sensor_af_idlechk(client);
2953                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2954                         {
2955                                 sensor_af_idlechk(client); // by duanyp
2956                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2957                         }
2958                 #endif
2959
2960         #if CONFIG_SENSOR_Flash
2961         if (sensor_fmt_capturechk(sd,f) == true) {      /* ddl@rock-chips.com : Capture */
2962             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2963                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2964                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2965             }           
2966         } else {                                        /* ddl@rock-chips.com : Video */
2967             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2968                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2969                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2970             }
2971         }
2972         #endif
2973                 
2974         ret = sensor_write_array(client, winseqe_set_addr);        
2975         if (ret != 0) {
2976             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2977             #if CONFIG_SENSOR_Flash
2978             if (sensor_fmt_capturechk(sd,f) == true) {
2979                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2980                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2981                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2982                 }
2983             }
2984             #endif
2985             goto sensor_s_fmt_end;
2986         } else {
2987             sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
2988             if (sensor_fmt_capturechk(sd,f) == true) {                              /* ddl@rock-chips.com : Capture */
2989                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2990                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2991                         if (sensor->info_priv.whiteBalance != 0) {
2992                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2993                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2994                         }
2995                         sensor->info_priv.snap2preview = true;
2996                 } else if (sensor_fmt_videochk(sd,f) == true) {                 /* ddl@rock-chips.com : Video */
2997                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2998                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2999                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3000                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3001                         sensor->info_priv.video2preview = true;
3002                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
3003                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3004                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3005                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3006                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3007                         sensor->info_priv.video2preview = false;
3008                         sensor->info_priv.snap2preview = false;
3009                 }
3010             mdelay(100);  // by FAE.
3011         }
3012         SENSOR_DG("\n%s..%s.. icd->width=%d..icd->height=%d..isCapture=%d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h,isCapture);
3013     }
3014     else
3015     {
3016         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);
3017     }
3018
3019     //add by duanyp. Improve the green phenomenon when startup camera every time.
3020     //mdelay(500);
3021
3022 sensor_s_fmt_end:
3023     return ret;
3024 }
3025
3026 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
3027 {
3028     struct v4l2_pix_format *pix = &f->fmt.pix;
3029     bool bayer = pix->pixelformat == V4L2_PIX_FMT_UYVY ||
3030         pix->pixelformat == V4L2_PIX_FMT_YUYV;
3031
3032     /*
3033     * With Bayer format enforce even side lengths, but let the user play
3034     * with the starting pixel
3035     */
3036
3037     if (pix->height > SENSOR_MAX_HEIGHT)
3038         pix->height = SENSOR_MAX_HEIGHT;
3039     else if (pix->height < SENSOR_MIN_HEIGHT)
3040         pix->height = SENSOR_MIN_HEIGHT;
3041     else if (bayer)
3042         pix->height = ALIGN(pix->height, 2);
3043
3044     if (pix->width > SENSOR_MAX_WIDTH)
3045         pix->width = SENSOR_MAX_WIDTH;
3046     else if (pix->width < SENSOR_MIN_WIDTH)
3047         pix->width = SENSOR_MIN_WIDTH;
3048     else if (bayer)
3049         pix->width = ALIGN(pix->width, 2);
3050
3051     return 0;
3052 }
3053
3054  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3055 {
3056     struct i2c_client *client = sd->priv;
3057
3058     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3059         return -EINVAL;
3060
3061     if (id->match.addr != client->addr)
3062         return -ENODEV;
3063
3064     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV3640  identifier */
3065     id->revision = 0;
3066
3067     return 0;
3068 }
3069 #if CONFIG_SENSOR_Brightness
3070 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3071 {
3072     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3073
3074     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3075     {
3076         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3077         {
3078             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3079             {
3080                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3081                 return -EINVAL;
3082             }
3083             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3084             return 0;
3085         }
3086     }
3087         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3088     return -EINVAL;
3089 }
3090 #endif
3091 #if CONFIG_SENSOR_Effect
3092 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3093 {
3094     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3095
3096     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3097     {
3098         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3099         {
3100             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3101             {
3102                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3103                 return -EINVAL;
3104             }
3105             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3106             return 0;
3107         }
3108     }
3109         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3110     return -EINVAL;
3111 }
3112 #endif
3113 #if CONFIG_SENSOR_Exposure
3114 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3115 {
3116     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3117
3118     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3119     {
3120         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3121         {
3122             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3123             {
3124                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3125                 return -EINVAL;
3126             }
3127             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3128             return 0;
3129         }
3130     }
3131         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3132     return -EINVAL;
3133 }
3134 #endif
3135 #if CONFIG_SENSOR_Saturation
3136 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3137 {
3138     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3139
3140     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3141     {
3142         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3143         {
3144             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3145             {
3146                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3147                 return -EINVAL;
3148             }
3149             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3150             return 0;
3151         }
3152     }
3153     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3154     return -EINVAL;
3155 }
3156 #endif
3157 #if CONFIG_SENSOR_Contrast
3158 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3159 {
3160     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3161
3162     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3163     {
3164         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3165         {
3166             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3167             {
3168                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3169                 return -EINVAL;
3170             }
3171             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3172             return 0;
3173         }
3174     }
3175     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3176     return -EINVAL;
3177 }
3178 #endif
3179 #if CONFIG_SENSOR_Mirror
3180 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3181 {
3182     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3183
3184     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3185     {
3186         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3187         {
3188             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3189             {
3190                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3191                 return -EINVAL;
3192             }
3193             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3194             return 0;
3195         }
3196     }
3197     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3198     return -EINVAL;
3199 }
3200 #endif
3201 #if CONFIG_SENSOR_Flip
3202 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3203 {
3204     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3205
3206     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3207     {
3208         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3209         {
3210             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3211             {
3212                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3213                 return -EINVAL;
3214             }
3215             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3216             return 0;
3217         }
3218     }
3219     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3220     return -EINVAL;
3221 }
3222 #endif
3223 #if CONFIG_SENSOR_Scene
3224 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3225 {
3226     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3227
3228     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3229     {
3230         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3231         {
3232             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3233             {
3234                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3235                 return -EINVAL;
3236             }
3237             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3238             return 0;
3239         }
3240     }
3241     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3242     return -EINVAL;
3243 }
3244 #endif
3245 #if CONFIG_SENSOR_WhiteBalance
3246 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3247 {
3248     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3249
3250     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3251     {
3252         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3253         {
3254             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3255             {
3256                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3257                 return -EINVAL;
3258             }
3259             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3260             return 0;
3261         }
3262     }
3263         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3264     return -EINVAL;
3265 }
3266 #endif
3267 #if CONFIG_SENSOR_DigitalZoom
3268 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3269 {
3270     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3271     struct sensor *sensor = to_sensor(client);
3272         const struct v4l2_queryctrl *qctrl_info;
3273     int digitalzoom_cur, digitalzoom_total;
3274
3275         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3276         if (qctrl_info)
3277                 return -EINVAL;
3278
3279     digitalzoom_cur = sensor->info_priv.digitalzoom;
3280     digitalzoom_total = qctrl_info->maximum;
3281
3282     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3283     {
3284         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3285         return -EINVAL;
3286     }
3287
3288     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3289     {
3290         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3291         return -EINVAL;
3292     }
3293
3294     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3295     {
3296         *value = digitalzoom_total - digitalzoom_cur;
3297     }
3298
3299     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3300     {
3301         *value = 0 - digitalzoom_cur;
3302     }
3303
3304     digitalzoom_cur += *value;
3305
3306     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3307     {
3308         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3309         {
3310             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3311             return -EINVAL;
3312         }
3313         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3314         return 0;
3315     }
3316
3317     return -EINVAL;
3318 }
3319 #endif
3320
3321 #if CONFIG_SENSOR_Focus
3322 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3323 {
3324         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3325     struct sensor *sensor = to_sensor(client);
3326         const struct v4l2_queryctrl *qctrl_info;
3327         struct af_cmdinfo cmdinfo;
3328         int ret = 0;
3329
3330         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
3331         if (!qctrl_info)
3332                 return -EINVAL;
3333
3334         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3335                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3336
3337                         if (sensor_af_idlechk(client))
3338                                 goto sensor_set_focus_absolute_end;
3339
3340                         cmdinfo.cmd_tag = StepFocus_Spec_Tag;
3341                         cmdinfo.cmd_para[0] = value;
3342                         cmdinfo.validate_bit = 0x81;
3343                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3344                         //ret |= sensor_af_cmdset(client, ReturnIdle_Cmd, NULL);
3345                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3346                 } else {
3347                         ret = -EINVAL;
3348                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3349                 }
3350         } else {
3351                 ret = -EACCES;
3352                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3353                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3354         }
3355
3356 sensor_set_focus_absolute_end:
3357         return ret;
3358 }
3359 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3360 {
3361         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3362         struct sensor *sensor = to_sensor(client);
3363         const struct v4l2_queryctrl *qctrl_info;
3364         struct af_cmdinfo cmdinfo;
3365         int ret = 0;
3366
3367         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3368         if (!qctrl_info)
3369                 return -EINVAL;
3370
3371         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3372                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
3373
3374                         if (sensor_af_idlechk(client))
3375                                 goto sensor_set_focus_relative_end;
3376
3377                         if (value > 0) {
3378                                 cmdinfo.cmd_tag = StepFocus_Near_Tag;
3379                         } else if (value < 0) {
3380                                 cmdinfo.cmd_tag = StepFocus_Far_Tag;
3381                         }
3382                         cmdinfo.validate_bit = 0x80;
3383                         ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
3384
3385                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3386                 } else {
3387                         ret = -EINVAL;
3388                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3389                 }
3390         } else {
3391                 ret = -EACCES;
3392                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3393                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3394         }
3395 sensor_set_focus_relative_end:
3396         return ret;
3397 }
3398
3399 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3400 {
3401         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3402         struct sensor *sensor = to_sensor(client);
3403         int ret = 0;
3404
3405         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
3406                 switch (value)
3407                 {
3408                         case SENSOR_AF_MODE_AUTO:
3409                         {
3410                                 ret = sensor_af_single(client);                         
3411                                 break;
3412                         }
3413
3414                         case SENSOR_AF_MODE_MACRO:
3415                         {
3416                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3417                                 break;
3418                         }
3419
3420                         case SENSOR_AF_MODE_INFINITY:
3421                         {
3422                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3423                                 break;
3424                         }
3425
3426                         case SENSOR_AF_MODE_CONTINUOUS:
3427                         {
3428                                 ret = sensor_af_const(client);
3429                                 break;
3430                         }
3431                         default:
3432                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3433                                 break;
3434
3435                 }
3436
3437                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3438         } else {
3439                 ret = -EACCES;
3440                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3441                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3442         }
3443
3444         return ret;
3445 }
3446 #endif
3447 #if CONFIG_SENSOR_Flash
3448 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3449 {    
3450     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3451         if (value == 3) {       /* ddl@rock-chips.com: torch */
3452             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3453         } else {
3454             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3455         }
3456         SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3457         return 0;
3458     }
3459     
3460         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3461     return -EINVAL;
3462 }
3463 #endif
3464 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3465 {
3466     struct i2c_client *client = sd->priv;
3467     struct sensor *sensor = to_sensor(client);
3468     const struct v4l2_queryctrl *qctrl;
3469
3470     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3471
3472     if (!qctrl)
3473     {
3474         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3475         return -EINVAL;
3476     }
3477
3478     switch (ctrl->id)
3479     {
3480         case V4L2_CID_BRIGHTNESS:
3481             {
3482                 ctrl->value = sensor->info_priv.brightness;
3483                 break;
3484             }
3485         case V4L2_CID_SATURATION:
3486             {
3487                 ctrl->value = sensor->info_priv.saturation;
3488                 break;
3489             }
3490         case V4L2_CID_CONTRAST:
3491             {
3492                 ctrl->value = sensor->info_priv.contrast;
3493                 break;
3494             }
3495         case V4L2_CID_DO_WHITE_BALANCE:
3496             {
3497                 ctrl->value = sensor->info_priv.whiteBalance;
3498                 break;
3499             }
3500         case V4L2_CID_EXPOSURE:
3501             {
3502                 ctrl->value = sensor->info_priv.exposure;
3503                 break;
3504             }
3505         case V4L2_CID_HFLIP:
3506             {
3507                 ctrl->value = sensor->info_priv.mirror;
3508                 break;
3509             }
3510         case V4L2_CID_VFLIP:
3511             {
3512                 ctrl->value = sensor->info_priv.flip;
3513                 break;
3514             }
3515         default :
3516                 break;
3517     }
3518     return 0;
3519 }
3520
3521
3522
3523 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3524 {
3525     struct i2c_client *client = sd->priv;
3526     struct sensor *sensor = to_sensor(client);
3527     struct soc_camera_device *icd = client->dev.platform_data;
3528     const struct v4l2_queryctrl *qctrl;
3529
3530
3531     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3532
3533     if (!qctrl)
3534     {
3535         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3536         return -EINVAL;
3537     }
3538
3539     switch (ctrl->id)
3540     {
3541 #if CONFIG_SENSOR_Brightness
3542         case V4L2_CID_BRIGHTNESS:
3543             {
3544                 if (ctrl->value != sensor->info_priv.brightness)
3545                 {
3546                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3547                     {
3548                         return -EINVAL;
3549                     }
3550                     sensor->info_priv.brightness = ctrl->value;
3551                 }
3552                 break;
3553             }
3554 #endif
3555 #if CONFIG_SENSOR_Exposure
3556         case V4L2_CID_EXPOSURE:
3557             {
3558                 if (ctrl->value != sensor->info_priv.exposure)
3559                 {
3560                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3561                     {
3562                         return -EINVAL;
3563                     }
3564                     sensor->info_priv.exposure = ctrl->value;
3565                 }
3566                 break;
3567             }
3568 #endif
3569 #if CONFIG_SENSOR_Saturation
3570         case V4L2_CID_SATURATION:
3571             {
3572                 if (ctrl->value != sensor->info_priv.saturation)
3573                 {
3574                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3575                     {
3576                         return -EINVAL;
3577                     }
3578                     sensor->info_priv.saturation = ctrl->value;
3579                 }
3580                 break;
3581             }
3582 #endif
3583 #if CONFIG_SENSOR_Contrast
3584         case V4L2_CID_CONTRAST:
3585             {
3586                 if (ctrl->value != sensor->info_priv.contrast)
3587                 {
3588                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3589                     {
3590                         return -EINVAL;
3591                     }
3592                     sensor->info_priv.contrast = ctrl->value;
3593                 }
3594                 break;
3595             }
3596 #endif
3597 #if CONFIG_SENSOR_WhiteBalance
3598         case V4L2_CID_DO_WHITE_BALANCE:
3599             {
3600                 if (ctrl->value != sensor->info_priv.whiteBalance)
3601                 {
3602                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3603                     {
3604                         return -EINVAL;
3605                     }
3606                     sensor->info_priv.whiteBalance = ctrl->value;
3607                 }
3608                 break;
3609             }
3610 #endif
3611 #if CONFIG_SENSOR_Mirror
3612         case V4L2_CID_HFLIP:
3613             {
3614                 if (ctrl->value != sensor->info_priv.mirror)
3615                 {
3616                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3617                         return -EINVAL;
3618                     sensor->info_priv.mirror = ctrl->value;
3619                 }
3620                 break;
3621             }
3622 #endif
3623 #if CONFIG_SENSOR_Flip
3624         case V4L2_CID_VFLIP:
3625             {
3626                 if (ctrl->value != sensor->info_priv.flip)
3627                 {
3628                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3629                         return -EINVAL;
3630                     sensor->info_priv.flip = ctrl->value;
3631                 }
3632                 break;
3633             }
3634 #endif
3635         default:
3636             break;
3637     }
3638
3639     return 0;
3640 }
3641 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3642 {
3643     const struct v4l2_queryctrl *qctrl;
3644     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3645     struct sensor *sensor = to_sensor(client);
3646
3647     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3648
3649     if (!qctrl)
3650     {
3651         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3652         return -EINVAL;
3653     }
3654
3655     switch (ext_ctrl->id)
3656     {
3657         case V4L2_CID_SCENE:
3658             {
3659                 ext_ctrl->value = sensor->info_priv.scene;
3660                 break;
3661             }
3662         case V4L2_CID_EFFECT:
3663             {
3664                 ext_ctrl->value = sensor->info_priv.effect;
3665                 break;
3666             }
3667         case V4L2_CID_ZOOM_ABSOLUTE:
3668             {
3669                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3670                 break;
3671             }
3672         case V4L2_CID_ZOOM_RELATIVE:
3673             {
3674                 return -EINVAL;
3675             }
3676         case V4L2_CID_FOCUS_ABSOLUTE:
3677             {
3678                 ext_ctrl->value = sensor->info_priv.focus;
3679                 break;
3680             }
3681         case V4L2_CID_FOCUS_RELATIVE:
3682             {
3683                 return -EINVAL;
3684             }
3685         case V4L2_CID_FLASH:
3686             {
3687                 ext_ctrl->value = sensor->info_priv.flash;
3688                 break;
3689             }
3690         default :
3691             break;
3692     }
3693     return 0;
3694 }
3695 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3696 {
3697     const struct v4l2_queryctrl *qctrl;
3698     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3699     struct sensor *sensor = to_sensor(client);
3700     int val_offset;
3701
3702     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3703
3704     if (!qctrl)
3705     {
3706         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3707         return -EINVAL;
3708     }
3709
3710         val_offset = 0;
3711     switch (ext_ctrl->id)
3712     {
3713 #if CONFIG_SENSOR_Scene
3714         case V4L2_CID_SCENE:
3715             {
3716                 if (ext_ctrl->value != sensor->info_priv.scene)
3717                 {
3718                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3719                         return -EINVAL;
3720                     sensor->info_priv.scene = ext_ctrl->value;
3721                 }
3722                 break;
3723             }
3724 #endif
3725 #if CONFIG_SENSOR_Effect
3726         case V4L2_CID_EFFECT:
3727             {
3728                 if (ext_ctrl->value != sensor->info_priv.effect)
3729                 {
3730                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3731                         return -EINVAL;
3732                     sensor->info_priv.effect= ext_ctrl->value;
3733                 }
3734                 break;
3735             }
3736 #endif
3737 #if CONFIG_SENSOR_DigitalZoom
3738         case V4L2_CID_ZOOM_ABSOLUTE:
3739             {
3740                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3741                     return -EINVAL;
3742
3743                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3744                 {
3745                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3746
3747                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3748                         return -EINVAL;
3749                     sensor->info_priv.digitalzoom += val_offset;
3750
3751                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3752                 }
3753
3754                 break;
3755             }
3756         case V4L2_CID_ZOOM_RELATIVE:
3757             {
3758                 if (ext_ctrl->value)
3759                 {
3760                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3761                         return -EINVAL;
3762                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3763
3764                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3765                 }
3766                 break;
3767             }
3768 #endif
3769 #if CONFIG_SENSOR_Focus
3770         case V4L2_CID_FOCUS_ABSOLUTE:
3771             {
3772                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3773                     return -EINVAL;
3774
3775                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3776                                         if (ext_ctrl->value == qctrl->minimum) {
3777                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3778                                         } else if (ext_ctrl->value == qctrl->maximum) {
3779                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3780                                         } else {
3781                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3782                                         }
3783                                 }
3784
3785                 break;
3786             }           
3787          case V4L2_CID_FOCUS_RELATIVE:
3788             {
3789                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3790                     return -EINVAL;
3791
3792                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3793                 break;
3794             }
3795                 case V4L2_CID_FOCUS_AUTO:
3796                         {
3797                                 if (ext_ctrl->value == 1) {
3798                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3799                                                 return -EINVAL;
3800                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3801                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3802                                         if (ext_ctrl->value == 0)
3803                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3804                                 }
3805                                 break;
3806                         }
3807                 /*
3808                 case V4L2_CID_FOCUS_CONTINUOUS:
3809                         {
3810                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3811                                         if (ext_ctrl->value == 1) {
3812                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3813                                                         return -EINVAL;
3814                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3815                                         }
3816                                 } else {
3817                                         if (ext_ctrl->value == 0)
3818                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3819                                 }
3820                                 break;
3821                         }*/
3822 #endif
3823 #if CONFIG_SENSOR_Flash
3824         case V4L2_CID_FLASH:
3825             {
3826                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3827                     return -EINVAL;
3828                 sensor->info_priv.flash = ext_ctrl->value;
3829
3830                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3831                 break;
3832             }
3833 #endif
3834         default:
3835             break;
3836     }
3837
3838     return 0;
3839 }
3840
3841 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3842 {
3843     struct i2c_client *client = sd->priv;
3844     struct soc_camera_device *icd = client->dev.platform_data;
3845     int i, error_cnt=0, error_idx=-1;
3846
3847
3848     for (i=0; i<ext_ctrl->count; i++) {
3849         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3850             error_cnt++;
3851             error_idx = i;
3852         }
3853     }
3854
3855     if (error_cnt > 1)
3856         error_idx = ext_ctrl->count;
3857
3858     if (error_idx != -1) {
3859         ext_ctrl->error_idx = error_idx;
3860         return -EINVAL;
3861     } else {
3862         return 0;
3863     }
3864 }
3865
3866 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3867 {
3868     struct i2c_client *client = sd->priv;
3869     struct soc_camera_device *icd = client->dev.platform_data;
3870     int i, error_cnt=0, error_idx=-1;
3871
3872
3873     for (i=0; i<ext_ctrl->count; i++) {
3874         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3875             error_cnt++;
3876             error_idx = i;
3877         }
3878     }
3879
3880     if (error_cnt > 1)
3881         error_idx = ext_ctrl->count;
3882
3883     if (error_idx != -1) {
3884         ext_ctrl->error_idx = error_idx;
3885         return -EINVAL;
3886     } else {
3887         return 0;
3888     }
3889 }
3890
3891 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3892 {
3893         struct i2c_client *client = sd->priv;
3894     struct sensor *sensor = to_sensor(client);
3895     #if CONFIG_SENSOR_Focus
3896         struct soc_camera_device *icd = client->dev.platform_data;
3897         struct v4l2_format fmt;
3898     #endif
3899
3900         if (enable == 1) {
3901                 sensor->info_priv.enable = 1;
3902                 #if CONFIG_SENSOR_Focus
3903                 fmt.fmt.pix.width = icd->user_width;
3904                 fmt.fmt.pix.height = icd->user_height;
3905                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3906                 if (sensor_fmt_capturechk(sd, &fmt) == false) {
3907                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3908                                 if (sensor->sensor_wq != NULL) {
3909                                         mutex_lock(&sensor->wq_lock);
3910                                         if (sensor->sensor_wk.state == sensor_working) {
3911                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3912                                                 mutex_unlock(&sensor->wq_lock);
3913                                                 goto sensor_s_stream_end;
3914                                         }
3915                                         sensor->sensor_wk.state = sensor_working;
3916                                         mutex_unlock(&sensor->wq_lock);
3917                                         sensor->sensor_wk.client = client;
3918                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3919                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3920                                 }
3921                                 sensor->info_priv.affm_reinit = 0;
3922                         }
3923                 }
3924                 #endif
3925         } else if (enable == 0) {
3926                 sensor->info_priv.enable = 0;
3927                 #if CONFIG_SENSOR_Focus
3928                 flush_work(&(sensor->sensor_wk.dwork.work));
3929                 mutex_lock(&sensor->wq_lock);
3930                 sensor->sensor_wk.state = sensor_work_ready;
3931                 mutex_unlock(&sensor->wq_lock);
3932                 #endif
3933         }
3934
3935 sensor_s_stream_end:
3936         return 0;
3937 }
3938
3939 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3940  * this wasn't our capture interface, so, we wait for the right one */
3941 static int sensor_video_probe(struct soc_camera_device *icd,
3942                                struct i2c_client *client)
3943 {
3944     char value;
3945     int ret,pid = 0;
3946     struct sensor *sensor = to_sensor(client);
3947
3948     /* We must have a parent by now. And it cannot be a wrong one.
3949      * So this entire test is completely redundant. */
3950     if (!icd->dev.parent ||
3951             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3952                 return -ENODEV;
3953
3954         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3955                 ret = -ENODEV;
3956                 goto sensor_video_probe_err;
3957         }
3958     /* soft reset */
3959     ret = sensor_write(client, 0x3012, 0x80);
3960     if (ret != 0) {
3961         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3962         ret = -ENODEV;
3963                 goto sensor_video_probe_err;
3964     }
3965     mdelay(5);          //delay 5 microseconds
3966
3967     /* check if it is an sensor sensor */
3968     ret = sensor_read(client, 0x300a, &value);
3969     if (ret != 0) {
3970         SENSOR_TR("read chip id high byte failed\n");
3971         ret = -ENODEV;
3972         goto sensor_video_probe_err;
3973     }
3974
3975     pid |= (value << 8);
3976
3977     ret = sensor_read(client, 0x300b, &value);
3978     if (ret != 0) {
3979         SENSOR_TR("read chip id low byte failed\n");
3980         ret = -ENODEV;
3981         goto sensor_video_probe_err;
3982     }
3983
3984     pid |= (value & 0xff);
3985     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3986     if (pid == SENSOR_ID) {
3987         sensor->model = SENSOR_V4L2_IDENT;
3988     } else {
3989         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3990         ret = -ENODEV;
3991         goto sensor_video_probe_err;
3992     }
3993
3994     icd->formats = sensor_colour_formats;
3995     icd->num_formats = ARRAY_SIZE(sensor_colour_formats);
3996
3997     return 0;
3998
3999 sensor_video_probe_err:
4000     return ret;
4001 }
4002 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
4003 {
4004         struct i2c_client *client = sd->priv;
4005     struct soc_camera_device *icd = client->dev.platform_data;
4006     struct sensor *sensor = to_sensor(client);
4007     int ret = 0,i;
4008
4009         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4010         switch (cmd)
4011         {
4012                 case RK29_CAM_SUBDEV_DEACTIVATE:
4013                 {
4014                         sensor_deactivate(client);
4015                         break;
4016                 }
4017                 case RK29_CAM_SUBDEV_IOREQUEST:
4018                 {
4019                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
4020             if (sensor->sensor_io_request != NULL) { 
4021                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
4022                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
4023                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
4024                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
4025                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
4026                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
4027                 }
4028             } else {
4029                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4030                 ret = -EINVAL;
4031                 goto sensor_ioctl_end;
4032             }
4033             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
4034                for this project */
4035             #if CONFIG_SENSOR_Flash     
4036                 if (sensor->sensor_gpio_res) {
4037                 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
4038                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4039                     for (i = 0; i < icd->ops->num_controls; i++) {
4040                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4041                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
4042                                 }
4043                     }
4044                     sensor->info_priv.flash = 0xff;
4045                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4046                 }
4047                 }
4048             #endif
4049                         break;
4050                 }
4051                 default:
4052                 {
4053                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4054                         break;
4055                 }
4056         }
4057
4058 sensor_ioctl_end:
4059         return ret;
4060
4061 }
4062
4063 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4064         .init           = sensor_init,
4065         .g_ctrl         = sensor_g_control,
4066         .s_ctrl         = sensor_s_control,
4067         .g_ext_ctrls          = sensor_g_ext_controls,
4068         .s_ext_ctrls          = sensor_s_ext_controls,
4069         .g_chip_ident   = sensor_g_chip_ident,
4070         .ioctl = sensor_ioctl,
4071 };
4072
4073 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4074         .s_fmt          = sensor_s_fmt,
4075         .g_fmt          = sensor_g_fmt,
4076         .try_fmt        = sensor_try_fmt,
4077         .s_stream   = sensor_s_stream,
4078 };
4079
4080 static struct v4l2_subdev_ops sensor_subdev_ops = {
4081         .core   = &sensor_subdev_core_ops,
4082         .video = &sensor_subdev_video_ops,
4083 };
4084
4085 static int sensor_probe(struct i2c_client *client,
4086                          const struct i2c_device_id *did)
4087 {
4088     struct sensor *sensor;
4089     struct soc_camera_device *icd = client->dev.platform_data;
4090     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4091     struct soc_camera_link *icl;
4092     int ret;
4093
4094     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4095     if (!icd) {
4096         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4097         return -EINVAL;
4098     }
4099
4100     icl = to_soc_camera_link(icd);
4101     if (!icl) {
4102         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4103         return -EINVAL;
4104     }
4105
4106     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4107         dev_warn(&adapter->dev,
4108                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4109         return -EIO;
4110     }
4111
4112     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4113     if (!sensor)
4114         return -ENOMEM;
4115
4116     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4117
4118     /* Second stage probe - when a capture adapter is there */
4119     icd->ops            = &sensor_ops;
4120     icd->y_skip_top             = 0;
4121         #if CONFIG_SENSOR_I2C_NOSCHED
4122         atomic_set(&sensor->tasklock_cnt,0);
4123         #endif
4124
4125     ret = sensor_video_probe(icd, client);
4126     if (ret < 0) {
4127         icd->ops = NULL;
4128         i2c_set_clientdata(client, NULL);
4129         kfree(sensor);
4130         sensor = NULL;
4131     } else {
4132                 #if CONFIG_SENSOR_Focus
4133                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
4134                 if (sensor->sensor_wq == NULL)
4135                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
4136                 mutex_init(&sensor->wq_lock);
4137                 sensor->sensor_wk.state = sensor_work_ready;
4138                 #endif
4139     }
4140     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4141     return ret;
4142 }
4143
4144 static int sensor_remove(struct i2c_client *client)
4145 {
4146     struct sensor *sensor = to_sensor(client);
4147     struct soc_camera_device *icd = client->dev.platform_data;
4148
4149         #if CONFIG_SENSOR_Focus
4150         if (sensor->sensor_wq) {
4151                 destroy_workqueue(sensor->sensor_wq);
4152                 sensor->sensor_wq = NULL;
4153         }
4154         #endif
4155         
4156     icd->ops = NULL;
4157     i2c_set_clientdata(client, NULL);
4158     client->driver = NULL;
4159     kfree(sensor);
4160     sensor = NULL;
4161     return 0;
4162 }
4163
4164 static const struct i2c_device_id sensor_id[] = {
4165         {SENSOR_NAME_STRING(), 0 },
4166         { }
4167 };
4168 MODULE_DEVICE_TABLE(i2c, sensor_id);
4169
4170 static struct i2c_driver sensor_i2c_driver = {
4171         .driver = {
4172                 .name = SENSOR_NAME_STRING(),
4173         },
4174         .probe          = sensor_probe,
4175         .remove         = sensor_remove,
4176         .id_table       = sensor_id,
4177 };
4178
4179 static int __init sensor_mod_init(void)
4180 {
4181     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4182     return i2c_add_driver(&sensor_i2c_driver);
4183 }
4184
4185 static void __exit sensor_mod_exit(void)
4186 {
4187     i2c_del_driver(&sensor_i2c_driver);
4188 }
4189
4190 device_initcall_sync(sensor_mod_init);
4191 module_exit(sensor_mod_exit);
4192
4193 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4194 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4195 MODULE_LICENSE("GPL");
4196