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