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