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