camera: camera driver and all sensor driver support
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov5640.c
1 /*
2  * Driver for OV5640 CMOS Image Sensor from OmniVision
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/miscdevice.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/soc_camera.h>
22 #include <mach/rk29_camera.h>
23 #include "ov5640.h"
24
25 static int debug;
26 module_param(debug, int, S_IRUGO|S_IWUSR);
27
28 #define dprintk(level, fmt, arg...) do {                        \
29         if (debug >= level)                                     \
30         printk(KERN_WARNING fmt , ## arg); } while (0)
31
32 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
33 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
34
35 #define _CONS(a,b) a##b
36 #define CONS(a,b) _CONS(a,b)
37
38 #define __STR(x) #x
39 #define _STR(x) __STR(x)
40 #define STR(x) _STR(x)
41
42 #define MIN(x,y)   ((x<y) ? x: y)
43 #define MAX(x,y)    ((x>y) ? x: y)
44
45 /* Sensor Driver Configuration */
46 #define SENSOR_NAME RK29_CAM_SENSOR_OV5640
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV5640
48 #define SENSOR_ID 0x5640
49 #define SENSOR_MIN_WIDTH    176
50 #define SENSOR_MIN_HEIGHT   144
51 #define SENSOR_MAX_WIDTH    2592
52 #define SENSOR_MAX_HEIGHT   1944
53 #define SENSOR_INIT_WIDTH       800                     /* Sensor pixel size for sensor_init_data array */
54 #define SENSOR_INIT_HEIGHT  600
55 #define SENSOR_INIT_WINSEQADR sensor_svga
56 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
57
58 #define CONFIG_SENSOR_WhiteBalance      1
59 #define CONFIG_SENSOR_Brightness        0
60 #define CONFIG_SENSOR_Contrast      0
61 #define CONFIG_SENSOR_Saturation    0
62 #define CONFIG_SENSOR_Effect        1
63 #define CONFIG_SENSOR_Scene         1
64 #define CONFIG_SENSOR_DigitalZoom   0
65 #define CONFIG_SENSOR_Exposure      0
66 #define CONFIG_SENSOR_Flash         0
67 #define CONFIG_SENSOR_Mirror        0
68 #define CONFIG_SENSOR_Flip          0
69 #ifdef CONFIG_OV5640_AUTOFOCUS
70 #define CONFIG_SENSOR_Focus         1
71 #include "ov5640_af_firmware.c"
72 #else
73 #define CONFIG_SENSOR_Focus         0
74 #endif
75
76 #define CONFIG_SENSOR_I2C_SPEED    250000       /* Hz */
77 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
78 #define CONFIG_SENSOR_I2C_NOSCHED   0
79 #define CONFIG_SENSOR_I2C_RDWRCHK   0
80
81 #define CONFIG_SENSOR_WRITE_REGS  1
82 #define WRITE_REGS_NUM 100
83
84 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
85                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
86                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
87
88 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
89 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
90 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
91 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
92 #define COLOR_TEMPERATURE_OFFICE_DN     3500
93 #define COLOR_TEMPERATURE_OFFICE_UP     5000
94 #define COLOR_TEMPERATURE_HOME_DN       2500
95 #define COLOR_TEMPERATURE_HOME_UP       3500
96
97 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
98 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
99
100 #define SENSOR_AF_IS_ERR    (0x00<<0)
101 #define SENSOR_AF_IS_OK         (0x01<<0)
102 #define SENSOR_INIT_IS_ERR   (0x00<<28)
103 #define SENSOR_INIT_IS_OK    (0x01<<28)
104
105 #if CONFIG_SENSOR_Focus
106 /*#define SENSOR_AF_MODE_INFINITY    0
107 #define SENSOR_AF_MODE_MACRO       1
108 #define SENSOR_AF_MODE_FIXED       2
109 #define SENSOR_AF_MODE_AUTO        3
110 #define SENSOR_AF_MODE_CONTINUOUS  4
111 #define SENSOR_AF_MODE_CLOSE       5*/
112 #define SENSOR_AF_MODE_AUTO        0
113 #define SENSOR_AF_MODE_CLOSE       1
114 #endif
115
116 #if CONFIG_SENSOR_Focus
117 /* ov5640 VCM Command and Status Registers */
118 #define CMD_MAIN_Reg            0x3022
119 //#define CMD_TAG_Reg           0x3023
120 #define CMD_ACK_Reg             0x3023
121 #define CMD_PARA0_Reg           0x3024
122 #define CMD_PARA1_Reg           0x3025
123 #define CMD_PARA2_Reg           0x3026
124 #define CMD_PARA3_Reg           0x3027
125 #define CMD_PARA4_Reg           0x3028
126
127 //#define STA_ZONE_Reg          0x3026
128 #define STA_FOCUS_Reg           0x3029
129
130 /* ov5640 VCM Command  */
131 #define OverlayEn_Cmd     0x01
132 #define OverlayDis_Cmd    0x02
133 #define SingleFocus_Cmd   0x03
134 #define ConstFocus_Cmd    0x04
135 #define StepMode_Cmd      0x05
136 #define PauseFocus_Cmd    0x06
137 #define ReturnIdle_Cmd    0x08
138 #define SetZone_Cmd       0x10
139 #define UpdateZone_Cmd    0x12
140 #define SetMotor_Cmd      0x20
141
142
143 #define Zone_configuration_Cmd          0x12
144 #define Trig_autofocus_Cmd              0x03
145 #define Get_focus_result_Cmd            0x07
146
147 /* ov5640 Focus State */
148 //#define S_FIRWRE              0xFF            /*Firmware is downloaded and not run*/
149 #define S_STARTUP               0x7e            /*Firmware is initializing*/
150 #define S_ERROR                 0x7f
151 #define S_IDLE                  0x70            /*Idle state, focus is released; lens is located at the furthest position.*/
152 #define S_FOCUSING              0x00            /*Auto Focus is running.*/
153 #define S_FOCUSED               0x10            /*Auto Focus is completed.*/
154
155 #define S_CAPTURE               0x12
156 #define S_STEP                          0x20
157
158 /* ov5640 Zone State */
159 #define Zone_Is_Focused(a, zone_val)    (zone_val&(1<<(a-3)))
160 #define Zone_Get_ID(zone_val)           (zone_val&0x03)
161
162 #define Zone_CenterMode   0x01
163 #define Zone_5xMode       0x02
164 #define Zone_5PlusMode    0x03
165 #define Zone_4fMode       0x04
166
167 #define ZoneSel_Auto      0x0b
168 #define ZoneSel_SemiAuto  0x0c
169 #define ZoneSel_Manual    0x0d
170 #define ZoneSel_Rotate    0x0e
171
172 /* ov5640 Step Focus Commands */
173 #define StepFocus_Near_Tag       0x01
174 #define StepFocus_Far_Tag        0x02
175 #define StepFocus_Furthest_Tag   0x03
176 #define StepFocus_Nearest_Tag    0x04
177 #define StepFocus_Spec_Tag       0x10
178 #endif
179
180 /* init 800X600 SVGA */
181 static struct reginfo sensor_init_data[] =
182 {
183         {0x3103, 0x11},
184         {0x3008, 0x82},
185         {0x3008, 0x42},
186         {0x3103, 0x03},
187         {0x3017, 0xff},
188         {0x3018, 0xff},
189         {0x3034, 0x1a},
190         {0x3035, 0x21},
191         {0x3036, 0x46},
192         {0x3037, 0x13},
193         {0x3108, 0x01},
194         {0x3630, 0x36},
195         {0x3631, 0x0e},
196         {0x3632, 0xe2},
197         {0x3633, 0x12},
198         {0x3621, 0xe0},
199         {0x3704, 0xa0},
200         {0x3703, 0x5a},
201         {0x3715, 0x78},
202         {0x3717, 0x01},
203         {0x370b, 0x60},
204         {0x3705, 0x1a},
205         {0x3905, 0x02},
206         {0x3906, 0x10},
207         {0x3901, 0x0a},
208         {0x3731, 0x12},
209         {0x3600, 0x08},
210         {0x3601, 0x33},
211         {0x302d, 0x60},
212         {0x3620, 0x52},
213         {0x371b, 0x20},
214         {0x471c, 0x50},
215         {0x3a13, 0x43},
216         {0x3a18, 0x00},
217         {0x3a19, 0xf8},
218         {0x3635, 0x13},
219         {0x3636, 0x03},
220         {0x3634, 0x40},
221         {0x3622, 0x01},
222         {0x3c01, 0x34},
223         {0x3c04, 0x28},
224         {0x3c05, 0x98},
225         {0x3c06, 0x00},
226         {0x3c07, 0x08},
227         {0x3c08, 0x00},
228         {0x3c09, 0x1c},
229         {0x3c0a, 0x9c},
230         {0x3c0b, 0x40},
231         {0x3820, 0x41},
232         {0x3821, 0x07},
233         {0x3814, 0x31},
234         {0x3815, 0x31},
235         {0x3800, 0x00},
236         {0x3801, 0x00},
237         {0x3802, 0x00},
238         {0x3803, 0x04},
239         {0x3804, 0x0a},
240         {0x3805, 0x3f},
241         {0x3806, 0x07},
242         {0x3807, 0x9b},
243         {0x3808, 0x03},
244         {0x3809, 0x20},
245         {0x380a, 0x02},
246         {0x380b, 0x58},
247         {0x380c, 0x07},
248         {0x380d, 0x68},
249         {0x380e, 0x03},
250         {0x380f, 0xd8},
251         {0x3810, 0x00},
252         {0x3811, 0x10},
253         {0x3812, 0x00},
254         {0x3813, 0x06},
255         {0x3618, 0x00},
256         {0x3612, 0x29},
257         {0x3708, 0x64},
258         {0x3709, 0x52},
259         {0x370c, 0x03},
260         {0x3a02, 0x03},
261         {0x3a03, 0xd8},
262         {0x3a08, 0x01},
263         {0x3a09, 0x27},
264         {0x3a0a, 0x00},
265         {0x3a0b, 0xf6},
266         {0x3a0e, 0x03},
267         {0x3a0d, 0x04},
268         {0x3a14, 0x03},
269         {0x3a15, 0xd8},
270         {0x4001, 0x02},
271         {0x4004, 0x02},
272         {0x3000, 0x00},
273         {0x3002, 0x1c},
274         {0x3004, 0xff},
275         {0x3006, 0xc3},
276         {0x300e, 0x58},
277         {0x302e, 0x00},
278         {0x4740, 0x20},
279         {0x4300, 0x30},
280         {0x501f, 0x00},
281         {0x4713, 0x03},
282         {0x4407, 0x04},
283         {0x440e, 0x00},
284         {0x460b, 0x35},
285         {0x460c, 0x20},
286         {0x4837, 0x22},
287         {0x3824, 0x02},
288         {0x5000, 0xa7},
289         {0x5001, 0xa3},
290         {0x5180, 0xff},
291         {0x5181, 0xf2},
292         {0x5182, 0x00},
293         {0x5183, 0x14},
294         {0x5184, 0x25},
295         {0x5185, 0x24},
296         {0x5186, 0x09},
297         {0x5187, 0x09},
298         {0x5188, 0x09},
299         {0x5189, 0x75},
300         {0x518a, 0x54},
301         {0x518b, 0xe0},
302         {0x518c, 0xb2},
303         {0x518d, 0x42},
304         {0x518e, 0x3d},
305         {0x518f, 0x56},
306         {0x5190, 0x46},
307         {0x5191, 0xf8},
308         {0x5192, 0x04},
309         {0x5193, 0x70},
310         {0x5194, 0xf0},
311         {0x5195, 0xf0},
312         {0x5196, 0x03},
313         {0x5197, 0x01},
314         {0x5198, 0x04},
315         {0x5199, 0x12},
316         {0x519a, 0x04},
317         {0x519b, 0x00},
318         {0x519c, 0x06},
319         {0x519d, 0x82},
320         {0x519e, 0x38},
321         {0x5381, 0x1e},
322         {0x5382, 0x5b},
323         {0x5383, 0x08},
324         {0x5384, 0x0a},
325         {0x5385, 0x7e},
326         {0x5386, 0x88},
327         {0x5387, 0x7c},
328         {0x5388, 0x6c},
329         {0x5389, 0x10},
330         {0x538a, 0x01},
331         {0x538b, 0x98},
332         {0x5300, 0x08},
333         {0x5301, 0x30},
334         {0x5302, 0x10},
335         {0x5303, 0x00},
336         {0x5304, 0x08},
337         {0x5305, 0x30},
338         {0x5306, 0x08},
339         {0x5307, 0x16},
340         {0x5309, 0x08},
341         {0x530a, 0x30},
342         {0x530b, 0x04},
343         {0x530c, 0x06},
344         {0x5480, 0x01},
345         {0x5481, 0x08},
346         {0x5482, 0x14},
347         {0x5483, 0x28},
348         {0x5484, 0x51},
349         {0x5485, 0x65},
350         {0x5486, 0x71},
351         {0x5487, 0x7d},
352         {0x5488, 0x87},
353         {0x5489, 0x91},
354         {0x548a, 0x9a},
355         {0x548b, 0xaa},
356         {0x548c, 0xb8},
357         {0x548d, 0xcd},
358         {0x548e, 0xdd},
359         {0x548f, 0xea},
360         {0x5490, 0x1d},
361         {0x5580, 0x02},
362         {0x5583, 0x40},
363         {0x5584, 0x10},
364         {0x5589, 0x10},
365         {0x558a, 0x00},
366         {0x558b, 0xf8},
367         {0x5800, 0x23},
368         {0x5801, 0x14},
369         {0x5802, 0x0f},
370         {0x5803, 0x0f},
371         {0x5804, 0x12},
372         {0x5805, 0x26},
373         {0x5806, 0x0c},
374         {0x5807, 0x08},
375         {0x5808, 0x05},
376         {0x5809, 0x05},
377         {0x580a, 0x08},
378         {0x580b, 0x0d},
379         {0x580c, 0x08},
380         {0x580d, 0x03},
381         {0x580e, 0x00},
382         {0x580f, 0x00},
383         {0x5810, 0x03},
384         {0x5811, 0x09},
385         {0x5812, 0x07},
386         {0x5813, 0x03},
387         {0x5814, 0x00},
388         {0x5815, 0x01},
389         {0x5816, 0x03},
390         {0x5817, 0x08},
391         {0x5818, 0x0d},
392         {0x5819, 0x08},
393         {0x581a, 0x05},
394         {0x581b, 0x06},
395         {0x581c, 0x08},
396         {0x581d, 0x0e},
397         {0x581e, 0x29},
398         {0x581f, 0x17},
399         {0x5820, 0x11},
400         {0x5821, 0x11},
401         {0x5822, 0x15},
402         {0x5823, 0x28},
403         {0x5824, 0x46},
404         {0x5825, 0x26},
405         {0x5826, 0x08},
406         {0x5827, 0x26},
407         {0x5828, 0x64},
408         {0x5829, 0x26},
409         {0x582a, 0x24},
410         {0x582b, 0x22},
411         {0x582c, 0x24},
412         {0x582d, 0x24},
413         {0x582e, 0x06},
414         {0x582f, 0x22},
415         {0x5830, 0x40},
416         {0x5831, 0x42},
417         {0x5832, 0x24},
418         {0x5833, 0x26},
419         {0x5834, 0x24},
420         {0x5835, 0x22},
421         {0x5836, 0x22},
422         {0x5837, 0x26},
423         {0x5838, 0x44},
424         {0x5839, 0x24},
425         {0x583a, 0x26},
426         {0x583b, 0x28},
427         {0x583c, 0x42},
428         {0x583d, 0xce},
429         {0x5025, 0x00},
430         {0x3a0f, 0x30},
431         {0x3a10, 0x28},
432         {0x3a1b, 0x30},
433         {0x3a1e, 0x26},
434         {0x3a11, 0x60},
435         {0x3a1f, 0x14},
436         {0x3008, 0x02},
437         
438         {SEQUENCE_END, 0x00}
439 };
440
441 /* 720p 15fps @ 1280x720 */
442
443 static struct reginfo sensor_720p[]=
444 {
445         {0x3503, 0x00},
446                 
447         {0x3c07,0x07},
448         {0x3803,0xfa},
449         {0x3806,0x06},
450         {0x3807,0xa9},
451         {0x3808,0x05},
452         {0x3809,0x00},
453         {0x380a,0x02},
454         {0x380b,0xd0},
455         {0x380c,0x07},
456         {0x380d,0x64},
457         {0x380e,0x02},
458         {0x380f,0xe4},
459         {0x3813,0x04},
460         {0x3a02,0x02},
461         {0x3a03,0xe4},
462         {0x3a08,0x01},
463         {0x3a09,0xbc},
464         {0x3a0a,0x01},
465         {0x3a0b,0x72},
466         {0x3a0e,0x01},
467         {0x3a0d,0x02},
468         {0x3a14,0x02},
469         {0x3a15,0xe4},
470         {0x3002,0x00},
471         {0x4713,0x02},
472         {0x4837,0x16},
473         {0x3824,0x04},
474         {0x5001,0x83},
475         {0x3035,0x21},
476         {0x3036,0x46},
477
478         {0x4837, 0x22},
479         {0x5001, 0xa3},
480         
481         {SEQUENCE_END, 0x00}
482 };
483
484 /*      1080p, 0x15fps, 0xyuv @1920x1080 */
485
486 static struct reginfo sensor_1080p[]=
487 {
488
489         {SEQUENCE_END, 0x00}
490 };
491
492 /* 2592X1944 QSXGA */
493 static struct reginfo sensor_qsxga[] =
494 {
495         {0x3503, 0x07},
496         {0x3a00, 0x78},
497         {0x350c, 0x00},
498         {0x350d, 0x00},
499         {0x3c07, 0x07},
500         {0x3820, 0x40},
501         {0x3821, 0x06},
502         {0x3814, 0x11},
503         {0x3815, 0x11},
504         {0x3803, 0x00},
505         {0x3807, 0x9f},
506         {0x3808, 0x0a},
507         {0x3809, 0x20},
508         {0x380a, 0x07},
509         {0x380b, 0x98},
510         {0x380c, 0x0b},
511         {0x380d, 0x1c},
512         {0x380e, 0x07},
513         {0x380f, 0xb0},
514         {0x3813, 0x04},
515         {0x3618, 0x04},
516         {0x3612, 0x2b},
517         {0x3709, 0x12},
518         {0x370c, 0x00},
519         {0x3a02, 0x07},
520         {0x3a03, 0xb0},
521         {0x3a0e, 0x06},
522         {0x3a0d, 0x08},
523         {0x3a14, 0x07},
524         {0x3a15, 0xb0},
525         {0x4004, 0x06},
526         {0x3035, 0x21},
527         {0x3036, 0x46},
528         {0x4837, 0x2c},
529         {0x5001, 0x83},
530
531         {SEQUENCE_END, 0x00}
532 };
533 /* 2048*1536 QXGA */
534 static struct reginfo sensor_qxga[] =
535 {
536         {0x3503, 0x07},
537         {0x3a00, 0x78},
538         {0x350c, 0x00},
539         {0x350d, 0x00},
540         {0x3c07, 0x07},
541         {0x3820, 0x40},
542         {0x3821, 0x06},
543         {0x3814, 0x11},
544         {0x3815, 0x11},
545         {0x3803, 0x00},
546         {0x3807, 0x9f},
547         {0x3808, 0x08},
548         {0x3809, 0x00},
549         {0x380a, 0x06},
550         {0x380b, 0x00},
551         {0x380c, 0x0b},
552         {0x380d, 0x1c},
553         {0x380e, 0x07},
554         {0x380f, 0xb0},
555         {0x3813, 0x04},
556         {0x3618, 0x04},
557         {0x3612, 0x2b},
558         {0x3709, 0x12},
559         {0x370c, 0x00},
560         {0x3a02, 0x07},
561         {0x3a03, 0xb0},
562         {0x3a0e, 0x06},
563         {0x3a0d, 0x08},
564         {0x3a14, 0x07},
565         {0x3a15, 0xb0},
566         {0x4004, 0x06},
567         {0x3035, 0x21},
568         {0x3036, 0x46},
569         {0x4837, 0x2c},
570         {0x5001, 0xa3},
571
572         {SEQUENCE_END, 0x00}
573 };
574
575 /* 1600X1200 UXGA */
576 static struct reginfo sensor_uxga[] =
577 {
578         {0x3503, 0x07},
579         {0x3a00, 0x78},
580         {0x350c, 0x00},
581         {0x350d, 0x00},
582         {0x3c07, 0x07},
583         {0x3820, 0x40},
584         {0x3821, 0x06},
585         {0x3814, 0x11},
586         {0x3815, 0x11},
587         {0x3803, 0x00},
588         {0x3807, 0x9f},
589         {0x3808, 0x06},
590         {0x3809, 0x40},
591         {0x380a, 0x04},
592         {0x380b, 0xb0},
593         {0x380c, 0x0b},
594         {0x380d, 0x1c},
595         {0x380e, 0x07},
596         {0x380f, 0xb0},
597         {0x3813, 0x04},
598         {0x3618, 0x04},
599         {0x3612, 0x2b},
600         {0x3709, 0x12},
601         {0x370c, 0x00},
602         {0x3a02, 0x07},
603         {0x3a03, 0xb0},
604         {0x3a0e, 0x06},
605         {0x3a0d, 0x08},
606         {0x3a14, 0x07},
607         {0x3a15, 0xb0},
608         {0x4004, 0x06},
609         {0x3035, 0x21},
610         {0x3036, 0x46},
611         {0x4837, 0x2c},
612         {0x5001, 0xa3},
613
614         {SEQUENCE_END, 0x00}
615 };
616
617 /* 1280X1024 SXGA */
618 static struct reginfo sensor_sxga[] =
619 {
620         {0x3503, 0x07},
621         {0x3a00, 0x78},
622         {0x350c, 0x00},
623         {0x350d, 0x00},
624         {0x3c07, 0x07},
625         {0x3820, 0x40},
626         {0x3821, 0x06},
627         {0x3814, 0x11},
628         {0x3815, 0x11},
629         {0x3803, 0x00},
630         {0x3807, 0x9f},
631         {0x3808, 0x05},
632         {0x3809, 0x00},
633         {0x380a, 0x04},
634         {0x380b, 0x00},
635         {0x380c, 0x0b},
636         {0x380d, 0x1c},
637         {0x380e, 0x07},
638         {0x380f, 0xb0},
639         {0x3813, 0x04},
640         {0x3618, 0x04},
641         {0x3612, 0x2b},
642         {0x3709, 0x12},
643         {0x370c, 0x00},
644         {0x3a02, 0x07},
645         {0x3a03, 0xb0},
646         {0x3a0e, 0x06},
647         {0x3a0d, 0x08},
648         {0x3a14, 0x07},
649         {0x3a15, 0xb0},
650         {0x4004, 0x06},
651         {0x3035, 0x21},
652         {0x3036, 0x46},
653         {0x4837, 0x2c},
654         {0x5001, 0xa3},
655
656         {SEQUENCE_END, 0x00}
657 };
658 /* 1024X768 XGA */
659 static struct reginfo sensor_xga[] =
660 {
661         {0x3503, 0x07},
662         {0x3a00, 0x78},
663         {0x350c, 0x00},
664         {0x350d, 0x00},
665         {0x3c07, 0x07},
666         {0x3820, 0x40},
667         {0x3821, 0x06},
668         {0x3814, 0x11},
669         {0x3815, 0x11},
670         {0x3803, 0x00},
671         {0x3807, 0x9f},
672         {0x3808, 0x05},
673         {0x3809, 0x00},
674         {0x380a, 0x04},
675         {0x380b, 0x00},
676         {0x380c, 0x0b},
677         {0x380d, 0x1c},
678         {0x380e, 0x07},
679         {0x380f, 0xb0},
680         {0x3813, 0x04},
681         {0x3618, 0x04},
682         {0x3612, 0x2b},
683         {0x3709, 0x12},
684         {0x370c, 0x00},
685         {0x3a02, 0x07},
686         {0x3a03, 0xb0},
687         {0x3a0e, 0x06},
688         {0x3a0d, 0x08},
689         {0x3a14, 0x07},
690         {0x3a15, 0xb0},
691         {0x4004, 0x06},
692         {0x3035, 0x21},
693         {0x3036, 0x46},
694         {0x4837, 0x2c},
695         {0x5001, 0xa3},
696         {SEQUENCE_END, 0x00}
697 };
698 /* 800X600 SVGA*/
699 static struct reginfo sensor_svga[] =
700 {
701         {0x3503, 0x00},
702         {0x3c07, 0x08},
703         {0x3820, 0x41},
704         {0x3821, 0x07},
705         {0x3814, 0x31},
706         {0x3815, 0x31},
707         {0x3803, 0x04},
708         {0x3807, 0x9b},
709         {0x3808, 0x03},
710         {0x3809, 0x20},
711         {0x380a, 0x02},
712         {0x380b, 0x58},
713         {0x380c, 0x07},
714         {0x380d, 0x68},
715         {0x380e, 0x03},
716         {0x380f, 0xd8},
717         {0x3813, 0x06},
718         {0x3618, 0x00},
719         {0x3612, 0x29},
720         {0x3709, 0x52},
721         {0x370c, 0x03},
722         {0x3a02, 0x03},
723         {0x3a03, 0xd8},
724         {0x3a08 ,0x01},///
725         {0x3a09, 0x27},///
726         {0x3a0a, 0x00},///
727         {0x3a0b, 0xf6},///
728         {0x3a0e, 0x03},
729         {0x3a0d, 0x04},
730         {0x3a14, 0x03},
731         {0x3a15, 0xd8},
732         {0x4004, 0x02},
733         {0x3002, 0x1c},////
734         {0x4713, 0x03},////
735         {0x3035, 0x21},
736         {0x3036, 0x46},
737         {0x4837, 0x22},
738         {0x3824, 0x02},////
739         {0x5001, 0xa3},
740         {SEQUENCE_END, 0x00}
741 };
742
743 /* 640X480 VGA */
744 static struct reginfo sensor_vga[] =
745 {
746
747         {SEQUENCE_END, 0x00}
748 };
749 /* 352X288 CIF */
750 static struct reginfo sensor_cif[] =
751 {
752
753         {SEQUENCE_END, 0x00}
754 };
755
756 /* 320*240 QVGA */
757 static  struct reginfo sensor_qvga[] =
758 {
759
760         {SEQUENCE_END, 0x00}
761 };
762
763 /* 176X144 QCIF*/
764 static struct reginfo sensor_qcif[] =
765 {
766
767         {SEQUENCE_END, 0x00}
768 };
769
770 static  struct reginfo sensor_ClrFmt_YUYV[]=
771 {
772         {0x4300,0x30},
773         {SEQUENCE_END, 0x00}
774 };
775
776 static  struct reginfo sensor_ClrFmt_UYVY[]=
777 {
778         {0x4300,0x32},
779         {SEQUENCE_END, 0x00}
780 };
781
782
783 #if CONFIG_SENSOR_WhiteBalance
784 static  struct reginfo sensor_WhiteB_Auto[]=
785 {
786         {0x3406 ,0x00},
787         {0x5192 ,0x04},
788         {0x5191 ,0xf8},
789         {0x5193 ,0x70},
790         {0x5194 ,0xf0},
791         {0x5195 ,0xf0},
792         {0x518d ,0x3d},
793         {0x518f ,0x54},
794         {0x518e ,0x3d},
795         {0x5190 ,0x54},
796         {0x518b ,0xa8},
797         {0x518c ,0xa8},
798         {0x5187 ,0x18},
799         {0x5188 ,0x18},
800         {0x5189 ,0x6e},
801         {0x518a ,0x68},
802         {0x5186 ,0x1c},
803         {0x5181 ,0x50},
804         {0x5184 ,0x25},
805         {0x5182 ,0x11},
806         {0x5183 ,0x14},
807         {0x5184 ,0x25},
808         {0x5185 ,0x24},
809         {SEQUENCE_END, 0x00}
810 };
811 /* Cloudy Colour Temperature : 6500K - 8000K  */
812 static  struct reginfo sensor_WhiteB_Cloudy[]=
813 {
814         {0x3406 ,0x1 },
815         {0x3400 ,0x6 },
816         {0x3401 ,0x48},
817         {0x3402 ,0x4 },
818         {0x3403 ,0x0 },
819         {0x3404 ,0x4 },
820         {0x3405 ,0xd3 },
821         {SEQUENCE_END, 0x00}
822 };
823 /* ClearDay Colour Temperature : 5000K - 6500K  */
824 static  struct reginfo sensor_WhiteB_ClearDay[]=
825 {
826         {0x3406 ,0x1 },
827         {0x3400 ,0x6 },
828         {0x3401 ,0x1c},
829         {0x3402 ,0x4 },
830         {0x3403 ,0x0 },
831         {0x3404 ,0x4 },
832         {0x3405 ,0xf3},
833         {SEQUENCE_END, 0x00}
834 };
835 /* Office Colour Temperature : 3500K - 5000K  */
836 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
837 {
838         {0x3406 ,0x1 },
839         {0x3400 ,0x5 },
840         {0x3401 ,0x48},
841         {0x3402 ,0x4 },
842         {0x3403 ,0x0 },
843         {0x3404 ,0x7 },
844         {0x3405 ,0xcf},
845         {SEQUENCE_END, 0x00}
846 };
847 /* Home Colour Temperature : 2500K - 3500K  */
848 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
849 {
850         {0x3406 ,0x1 },
851         {0x3400 ,0x4 },
852         {0x3401 ,0x10},
853         {0x3402 ,0x4 },
854         {0x3403 ,0x0 },
855         {0x3404 ,0x8 },
856         {0x3405 ,0xb6},
857         {SEQUENCE_END, 0x00}
858 };
859 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
860     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
861 };
862 #endif
863
864 #if CONFIG_SENSOR_Brightness
865 static  struct reginfo sensor_Brightness0[]=
866 {
867         {SEQUENCE_END, 0x00}
868 };
869 static  struct reginfo sensor_Brightness1[]=
870 {
871         {SEQUENCE_END, 0x00}
872 };
873
874 static  struct reginfo sensor_Brightness2[]=
875 {
876         {SEQUENCE_END, 0x00}
877 };
878 static  struct reginfo sensor_Brightness3[]=
879 {
880         {SEQUENCE_END, 0x00}
881 };
882 static  struct reginfo sensor_Brightness4[]=
883 {
884         {SEQUENCE_END, 0x00}
885 };
886
887 static  struct reginfo sensor_Brightness5[]=
888 {
889         {SEQUENCE_END, 0x00}
890 };
891 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
892     sensor_Brightness4, sensor_Brightness5,NULL,
893 };
894
895 #endif
896
897 #if CONFIG_SENSOR_Effect
898 static  struct reginfo sensor_Effect_Normal[] =
899 {
900         {0x5001, 0x7f},
901         {0x5580, 0x00},
902         {SEQUENCE_END, 0x00}
903 };
904 static  struct reginfo sensor_Effect_WandB[] =
905 {
906         {0x5001, 0xff},
907         {0x5580, 0x18},
908         {0x5583, 0x80},
909         {0x5584, 0x80},
910         {SEQUENCE_END, 0x00}
911 };
912 static  struct reginfo sensor_Effect_Sepia[] =
913 {
914         {0x5001, 0xff},
915         {0x5580, 0x18},
916         {0x5583, 0x40},
917         {0x5584, 0xa0},
918         {SEQUENCE_END, 0x00}
919 };
920
921 static  struct reginfo sensor_Effect_Negative[] =
922 {
923     //Negative
924         {0x5001, 0xff},
925         {0x5580, 0x40},
926         {SEQUENCE_END, 0x00}
927 };static  struct reginfo sensor_Effect_Bluish[] =
928 {
929     // Bluish
930         {0x5001, 0xff},
931         {0x5580, 0x18},
932         {0x5583, 0xa0},
933         {0x5584, 0x40},
934         {SEQUENCE_END, 0x00}
935 };
936
937 static  struct reginfo sensor_Effect_Green[] =
938 {
939     //  Greenish
940         {0x5001, 0xff},
941         {0x5580, 0x18},
942         {0x5583, 0x60},
943         {0x5584, 0x60},
944         {SEQUENCE_END, 0x00}
945 };
946 /*static  struct reginfo sensor_Effect_Reddish[] =
947 {
948     //  Greenish
949         {0x5001, 0xff},
950         {0x5580, 0x18},
951         {0x5583, 0x80},
952         {0x5584, 0xc0},
953         {SEQUENCE_END, 0x00}
954 };*/
955
956 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
957     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
958 };
959 #endif
960 #if CONFIG_SENSOR_Exposure
961 static  struct reginfo sensor_Exposure0[]=
962 {
963         {SEQUENCE_END, 0x00}
964 };
965 static  struct reginfo sensor_Exposure1[]=
966 {
967         {SEQUENCE_END, 0x00}
968 };
969 static  struct reginfo sensor_Exposure2[]=
970 {
971         {SEQUENCE_END, 0x00}
972 };
973
974 static  struct reginfo sensor_Exposure3[]=
975 {
976         {SEQUENCE_END, 0x00}
977 };
978 static  struct reginfo sensor_Exposure4[]=
979 {
980         {SEQUENCE_END, 0x00}
981 };
982 static  struct reginfo sensor_Exposure5[]=
983 {
984         {SEQUENCE_END, 0x00}
985 };
986 static  struct reginfo sensor_Exposure6[]=
987 {
988         {SEQUENCE_END, 0x00}
989 };
990 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
991     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
992 };
993 #endif
994 #if CONFIG_SENSOR_Saturation
995 static  struct reginfo sensor_Saturation0[]=
996 {
997         {SEQUENCE_END, 0x00}
998 };
999
1000 static  struct reginfo sensor_Saturation1[]=
1001 {
1002         {SEQUENCE_END, 0x00}
1003 };
1004
1005 static  struct reginfo sensor_Saturation2[]=
1006 {
1007         {SEQUENCE_END, 0x00}
1008 };static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1009
1010 #endif
1011 #if CONFIG_SENSOR_Contrast
1012 static  struct reginfo sensor_Contrast0[]=
1013 {
1014         {SEQUENCE_END, 0x00}
1015 };
1016
1017 static  struct reginfo sensor_Contrast1[]=
1018 {
1019         {SEQUENCE_END, 0x00}
1020 };
1021 static  struct reginfo sensor_Contrast2[]=
1022 {
1023         {SEQUENCE_END, 0x00}
1024 };
1025
1026 static  struct reginfo sensor_Contrast3[]=
1027 {
1028         {SEQUENCE_END, 0x00}
1029 };
1030
1031 static  struct reginfo sensor_Contrast4[]=
1032 {
1033         {SEQUENCE_END, 0x00}
1034 };
1035
1036
1037 static  struct reginfo sensor_Contrast5[]=
1038 {
1039         {SEQUENCE_END, 0x00}
1040 };
1041
1042 static  struct reginfo sensor_Contrast6[]=
1043 {
1044         {SEQUENCE_END, 0x00}
1045 };
1046 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1047     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1048 };
1049
1050 #endif
1051 #if CONFIG_SENSOR_Mirror
1052 static  struct reginfo sensor_MirrorOn[]=
1053 {
1054         {SEQUENCE_END, 0x00}
1055 };
1056 static  struct reginfo sensor_MirrorOff[]=
1057 {
1058         {SEQUENCE_END, 0x00}
1059 };
1060 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1061 #endif
1062 #if CONFIG_SENSOR_Flip
1063 static  struct reginfo sensor_FlipOn[]=
1064 {
1065         {SEQUENCE_END, 0x00}
1066 };
1067
1068 static  struct reginfo sensor_FlipOff[]=
1069 {
1070         {SEQUENCE_END, 0x00}
1071 };
1072 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1073
1074 #endif
1075 #if CONFIG_SENSOR_Scene
1076 static  struct reginfo sensor_SceneAuto[] =
1077 {
1078         {0x3a00 , 0x78},
1079         {SEQUENCE_END, 0x00}
1080 };
1081 static  struct reginfo sensor_SceneNight[] =
1082 {
1083     //15fps ~ 3.75fps night mode for 60/50Hz light environment, 24Mhz clock input,24Mzh pclk
1084         {0x3034 ,0x1a},
1085         {0x3035 ,0x21},
1086         {0x3036 ,0x46},
1087         {0x3037 ,0x13},
1088         {0x3038 ,0x00},
1089         {0x3039 ,0x00},
1090         {0x3a00 ,0x7c},
1091         {0x3a08 ,0x01},
1092         {0x3a09 ,0x27},
1093         {0x3a0a ,0x00},
1094         {0x3a0b ,0xf6},
1095         {0x3a0d ,0x04},
1096         {0x3a0e ,0x04},
1097         {0x3a02 ,0x0b},
1098         {0x3a03 ,0x88},
1099         {0x3a14 ,0x0b},
1100         {0x3a15 ,0x88},
1101         {SEQUENCE_END, 0x00}
1102 };
1103 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1104
1105 #endif
1106 #if CONFIG_SENSOR_DigitalZoom
1107 static struct reginfo sensor_Zoom0[] =
1108 {
1109         {SEQUENCE_END, 0x00}
1110 };
1111 static struct reginfo sensor_Zoom1[] =
1112 {
1113         {SEQUENCE_END, 0x00}
1114 };
1115
1116 static struct reginfo sensor_Zoom2[] =
1117 {
1118         {SEQUENCE_END, 0x00}
1119 };
1120
1121
1122 static struct reginfo sensor_Zoom3[] =
1123 {
1124         {SEQUENCE_END, 0x00}
1125 };
1126 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1127 #endif
1128 static const struct v4l2_querymenu sensor_menus[] =
1129 {
1130         #if CONFIG_SENSOR_WhiteBalance
1131     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1132     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1133     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1134     #endif
1135
1136         #if CONFIG_SENSOR_Effect
1137     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1138     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1139     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
1140     #endif
1141
1142         #if CONFIG_SENSOR_Scene
1143     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1144     #endif
1145
1146         #if CONFIG_SENSOR_Flash
1147     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1148     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1149     #endif
1150 };
1151
1152 static const struct v4l2_queryctrl sensor_controls[] =
1153 {
1154         #if CONFIG_SENSOR_WhiteBalance
1155     {
1156         .id             = V4L2_CID_DO_WHITE_BALANCE,
1157         .type           = V4L2_CTRL_TYPE_MENU,
1158         .name           = "White Balance Control",
1159         .minimum        = 0,
1160         .maximum        = 4,
1161         .step           = 1,
1162         .default_value = 0,
1163     },
1164     #endif
1165
1166         #if CONFIG_SENSOR_Brightness
1167         {
1168         .id             = V4L2_CID_BRIGHTNESS,
1169         .type           = V4L2_CTRL_TYPE_INTEGER,
1170         .name           = "Brightness Control",
1171         .minimum        = -3,
1172         .maximum        = 2,
1173         .step           = 1,
1174         .default_value = 0,
1175     },
1176     #endif
1177
1178         #if CONFIG_SENSOR_Effect
1179         {
1180         .id             = V4L2_CID_EFFECT,
1181         .type           = V4L2_CTRL_TYPE_MENU,
1182         .name           = "Effect Control",
1183         .minimum        = 0,
1184         .maximum        = 5,
1185         .step           = 1,
1186         .default_value = 0,
1187     },
1188         #endif
1189
1190         #if CONFIG_SENSOR_Exposure
1191         {
1192         .id             = V4L2_CID_EXPOSURE,
1193         .type           = V4L2_CTRL_TYPE_INTEGER,
1194         .name           = "Exposure Control",
1195         .minimum        = 0,
1196         .maximum        = 6,
1197         .step           = 1,
1198         .default_value = 0,
1199     },
1200         #endif
1201
1202         #if CONFIG_SENSOR_Saturation
1203         {
1204         .id             = V4L2_CID_SATURATION,
1205         .type           = V4L2_CTRL_TYPE_INTEGER,
1206         .name           = "Saturation Control",
1207         .minimum        = 0,
1208         .maximum        = 2,
1209         .step           = 1,
1210         .default_value = 0,
1211     },
1212     #endif
1213
1214         #if CONFIG_SENSOR_Contrast
1215         {
1216         .id             = V4L2_CID_CONTRAST,
1217         .type           = V4L2_CTRL_TYPE_INTEGER,
1218         .name           = "Contrast Control",
1219         .minimum        = -3,
1220         .maximum        = 3,
1221         .step           = 1,
1222         .default_value = 0,
1223     },
1224         #endif
1225
1226         #if CONFIG_SENSOR_Mirror
1227         {
1228         .id             = V4L2_CID_HFLIP,
1229         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1230         .name           = "Mirror Control",
1231         .minimum        = 0,
1232         .maximum        = 1,
1233         .step           = 1,
1234         .default_value = 1,
1235     },
1236     #endif
1237
1238         #if CONFIG_SENSOR_Flip
1239         {
1240         .id             = V4L2_CID_VFLIP,
1241         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1242         .name           = "Flip Control",
1243         .minimum        = 0,
1244         .maximum        = 1,
1245         .step           = 1,
1246         .default_value = 1,
1247     },
1248     #endif
1249
1250         #if CONFIG_SENSOR_Scene
1251     {
1252         .id             = V4L2_CID_SCENE,
1253         .type           = V4L2_CTRL_TYPE_MENU,
1254         .name           = "Scene Control",
1255         .minimum        = 0,
1256         .maximum        = 1,
1257         .step           = 1,
1258         .default_value = 0,
1259     },
1260     #endif
1261
1262         #if CONFIG_SENSOR_DigitalZoom
1263     {
1264         .id             = V4L2_CID_ZOOM_RELATIVE,
1265         .type           = V4L2_CTRL_TYPE_INTEGER,
1266         .name           = "DigitalZoom Control",
1267         .minimum        = -1,
1268         .maximum        = 1,
1269         .step           = 1,
1270         .default_value = 0,
1271     }, {
1272         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1273         .type           = V4L2_CTRL_TYPE_INTEGER,
1274         .name           = "DigitalZoom Control",
1275         .minimum        = 0,
1276         .maximum        = 3,
1277         .step           = 1,
1278         .default_value = 0,
1279     },
1280     #endif
1281
1282         #if CONFIG_SENSOR_Focus
1283         /*{
1284         .id             = V4L2_CID_FOCUS_RELATIVE,
1285         .type           = V4L2_CTRL_TYPE_INTEGER,
1286         .name           = "Focus Control",
1287         .minimum        = -1,
1288         .maximum        = 1,
1289         .step           = 1,
1290         .default_value = 0,
1291     }, {
1292         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1293         .type           = V4L2_CTRL_TYPE_INTEGER,
1294         .name           = "Focus Control",
1295         .minimum        = 0,
1296         .maximum        = 255,
1297         .step           = 1,
1298         .default_value = 125,
1299     },*/
1300         {
1301         .id             = V4L2_CID_FOCUS_AUTO,
1302         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1303         .name           = "Focus Control",
1304         .minimum        = 0,
1305         .maximum        = 1,
1306         .step           = 1,
1307         .default_value = 0,
1308     },/*{
1309         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1310         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1311         .name           = "Focus Control",
1312         .minimum        = 0,
1313         .maximum        = 1,
1314         .step           = 1,
1315         .default_value = 0,
1316     },*/
1317     #endif
1318
1319         #if CONFIG_SENSOR_Flash
1320         {
1321         .id             = V4L2_CID_FLASH,
1322         .type           = V4L2_CTRL_TYPE_MENU,
1323         .name           = "Flash Control",
1324         .minimum        = 0,
1325         .maximum        = 3,
1326         .step           = 1,
1327         .default_value = 0,
1328     },
1329         #endif
1330 };
1331
1332 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1333 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1334 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1335 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1336 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1337 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1338 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1339 static int sensor_resume(struct soc_camera_device *icd);
1340 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1341 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1342 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1343 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1344 static int sensor_deactivate(struct i2c_client *client);
1345
1346 static struct soc_camera_ops sensor_ops =
1347 {
1348     .suspend                     = sensor_suspend,
1349     .resume                       = sensor_resume,
1350     .set_bus_param              = sensor_set_bus_param,
1351     .query_bus_param    = sensor_query_bus_param,
1352     .controls           = sensor_controls,
1353     .menus                         = sensor_menus,
1354     .num_controls               = ARRAY_SIZE(sensor_controls),
1355     .num_menus          = ARRAY_SIZE(sensor_menus),
1356 };
1357 /* only one fixed colorspace per pixelcode */
1358 struct sensor_datafmt {
1359         enum v4l2_mbus_pixelcode code;
1360         enum v4l2_colorspace colorspace;
1361 };
1362
1363 /* Find a data format by a pixel code in an array */
1364 static const struct sensor_datafmt *sensor_find_datafmt(
1365         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1366         int n)
1367 {
1368         int i;
1369         for (i = 0; i < n; i++)
1370                 if (fmt[i].code == code)
1371                         return fmt + i;
1372
1373         return NULL;
1374 }
1375
1376 static const struct sensor_datafmt sensor_colour_fmts[] = {
1377     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1378     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1379 };
1380 enum sensor_work_state
1381 {
1382         sensor_work_ready = 0,
1383         sensor_working,
1384 };
1385 struct sensor_work
1386 {
1387         struct i2c_client *client;
1388         struct delayed_work dwork;
1389         enum sensor_work_state state;
1390 };
1391
1392 typedef struct sensor_info_priv_s
1393 {
1394     int whiteBalance;
1395     int brightness;
1396     int contrast;
1397     int saturation;
1398     int effect;
1399     int scene;
1400     int digitalzoom;
1401     int focus;
1402         int auto_focus;
1403         int affm_reinit;
1404     int flash;
1405     int exposure;
1406         bool snap2preview;
1407         bool video2preview;
1408     unsigned char mirror;                                        /* HFLIP */
1409     unsigned char flip;                                          /* VFLIP */
1410     struct reginfo *winseqe_cur_addr;
1411         struct sensor_datafmt fmt;
1412         unsigned int enable;
1413         unsigned int funmodule_state;
1414 } sensor_info_priv_t;
1415
1416
1417
1418 struct sensor_parameter
1419 {
1420         unsigned short int preview_maxlines;
1421         unsigned short int preview_exposure;
1422         unsigned short int preview_line_width;
1423         unsigned short int preview_gain;
1424
1425         unsigned short int capture_framerate;
1426         unsigned short int preview_framerate;
1427         char awb[6];
1428 };
1429
1430 struct sensor
1431 {
1432     struct v4l2_subdev subdev;
1433     struct i2c_client *client;
1434     sensor_info_priv_t info_priv;
1435         struct sensor_parameter parameter;
1436         struct workqueue_struct *sensor_wq;
1437         struct sensor_work sensor_wk;
1438         struct mutex wq_lock;
1439     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1440 #if CONFIG_SENSOR_I2C_NOSCHED
1441         atomic_t tasklock_cnt;
1442 #endif
1443         struct rk29camera_platform_data *sensor_io_request;
1444     struct rk29camera_gpio_res *sensor_gpio_res;
1445 };
1446
1447 static struct sensor* to_sensor(const struct i2c_client *client)
1448 {
1449     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1450 }
1451
1452 static int sensor_task_lock(struct i2c_client *client, int lock)
1453 {
1454 #if CONFIG_SENSOR_I2C_NOSCHED
1455         int cnt = 3;
1456     struct sensor *sensor = to_sensor(client);
1457
1458         if (lock) {
1459                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1460                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1461                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1462                                 msleep(35);
1463                                 cnt--;
1464                         }
1465                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1466                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1467                                 goto sensor_task_lock_err;
1468                         }
1469                         preempt_disable();
1470                 }
1471
1472                 atomic_add(1, &sensor->tasklock_cnt);
1473         } else {
1474                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1475                         atomic_sub(1, &sensor->tasklock_cnt);
1476
1477                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1478                                 preempt_enable();
1479                 }
1480         }
1481         return 0;
1482 sensor_task_lock_err:
1483         return -1;   
1484 #else
1485     return 0;
1486 #endif
1487
1488 }
1489
1490
1491 #if CONFIG_SENSOR_WRITE_REGS
1492 static int sensor_write_regs(struct i2c_client *client,  u8 *reg_info, int num)
1493 {
1494         int err=0,cnt;
1495         struct i2c_msg msg[1];
1496
1497         msg->len = num; 
1498         msg->addr = client->addr;       
1499         msg->flags = client->flags;     
1500         msg->buf = reg_info;    
1501         msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */      
1502         msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */   
1503
1504         
1505         cnt= 3; 
1506         err = -EAGAIN;
1507         
1508         while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */             
1509                 err = i2c_transfer(client->adapter, msg, 1);            
1510                 if (err >= 0) {                     
1511                         return 0;               
1512                 } else {                            
1513                         SENSOR_TR("\n %s write reg failed, try to write again!\n",      SENSOR_NAME_STRING());                      
1514                         udelay(10);     
1515                 }       
1516         }
1517         
1518         return err;
1519
1520 }
1521
1522 #endif
1523
1524
1525
1526
1527 /* sensor register write */
1528 static int sensor_write(struct i2c_client *client, u16 reg, u8 val)
1529 {
1530     int err,cnt;
1531     u8 buf[3];
1532     struct i2c_msg msg[1];
1533
1534     buf[0] = reg >> 8;
1535     buf[1] = reg & 0xFF;
1536     buf[2] = val;
1537
1538     msg->addr = client->addr;
1539     msg->flags = client->flags;
1540     msg->buf = buf;
1541     msg->len = sizeof(buf);
1542     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1543     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1544
1545     cnt = 3;
1546     err = -EAGAIN;
1547
1548     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1549         err = i2c_transfer(client->adapter, msg, 1);
1550
1551         if (err >= 0) {
1552             return 0;
1553         } else {
1554             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1555             udelay(10);
1556         }
1557     }
1558
1559     return err;
1560 }
1561
1562 /* sensor register read */
1563 static int sensor_read(struct i2c_client *client, u16 reg, u8 *val)
1564 {
1565     int err,cnt;
1566     u8 buf[2];
1567     struct i2c_msg msg[2];
1568
1569     buf[0] = reg >> 8;
1570     buf[1] = reg & 0xFF;
1571
1572     msg[0].addr = client->addr;
1573     msg[0].flags = client->flags;
1574     msg[0].buf = buf;
1575     msg[0].len = sizeof(buf);
1576     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1577     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1578
1579     msg[1].addr = client->addr;
1580     msg[1].flags = client->flags|I2C_M_RD;
1581     msg[1].buf = buf;
1582     msg[1].len = 1;
1583     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1584     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1585
1586     cnt = 3;
1587     err = -EAGAIN;
1588     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1589         err = i2c_transfer(client->adapter, msg, 2);
1590
1591         if (err >= 0) {
1592             *val = buf[0];
1593             return 0;
1594         } else {
1595                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1596             udelay(10);
1597         }
1598     }
1599
1600     return err;
1601 }
1602
1603 /* write a array of registers  */
1604 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1605 {
1606     int err = 0, cnt;
1607     int i = 0;
1608 #if CONFIG_SENSOR_Focus
1609         struct sensor *sensor = to_sensor(client);
1610 #endif
1611 #if CONFIG_SENSOR_I2C_RDWRCHK
1612         char valchk;
1613 #endif
1614 #if CONFIG_SENSOR_WRITE_REGS    
1615         int j = 0, reg_num;
1616         u8 *ptemp, *phead;
1617         int reg_length;
1618 #endif
1619
1620         cnt = 0;
1621         if (sensor_task_lock(client, 1) < 0)
1622                 goto sensor_write_array_end;
1623     while (regarray[i].reg != SEQUENCE_END) {
1624         #if CONFIG_SENSOR_Focus
1625         if ((regarray == sensor_af_firmware) && (sensor->info_priv.enable == 0)) {
1626                         SENSOR_DG("%s disable, Download af firmware terminated!\n",SENSOR_NAME_STRING());
1627                         err = -EINVAL;
1628                         goto sensor_write_array_end;
1629         }
1630                 #endif
1631
1632 #if CONFIG_SENSOR_WRITE_REGS
1633         
1634                 j = i;          
1635                 reg_num = 2;    
1636                 reg_length = 0x0001;
1637                                                 
1638                 while((regarray[i].reg + reg_length) == regarray[i+1].reg) {            
1639                         i++;                    
1640                         reg_num++;
1641                         if(reg_num >= WRITE_REGS_NUM)
1642                                 break;
1643                 }
1644                 
1645                 if(reg_num > 2) {                       
1646                         
1647                         int size_num;
1648                         size_num = reg_num + 1;
1649                         
1650                         ptemp = phead = (u8*)kmalloc(size_num*sizeof(u8),GFP_KERNEL);
1651                         if (!phead) {
1652                                 SENSOR_DG("-------------write registers allocate memory fail!!!\n");
1653                 i = j;
1654                 err = sensor_write(client, regarray[i].reg, regarray[i].val); 
1655                         } else {                        
1656                         *phead = regarray[j].reg >> 8;                  
1657                         *(ptemp+1) = regarray[j].reg & 0xFF;
1658                         ptemp += 2;                             
1659                         for( ; reg_num > 0; reg_num --, j++) {
1660                                 *ptemp ++ = regarray[j].val;
1661                         }
1662                         
1663                         ptemp = phead;                  
1664                         err = sensor_write_regs(client, ptemp,size_num);                        
1665                         kfree(phead);   
1666                         }
1667                 }else{
1668                         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1669                 }       
1670 #else
1671                 err = sensor_write(client, regarray[i].reg, regarray[i].val);        
1672 #endif
1673         if (err < 0)
1674         {
1675             if (cnt-- > 0) {
1676                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1677                                 i = 0;
1678                                 continue;
1679             } else {
1680                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1681                 err = -EPERM;
1682                                 goto sensor_write_array_end;
1683             }
1684         } else {
1685         #if CONFIG_SENSOR_I2C_RDWRCHK
1686                         sensor_read(client, regarray[i].reg, &valchk);
1687                         if (valchk != regarray[i].val)
1688                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1689                 #endif
1690         }
1691         i++; 
1692     }
1693
1694         #if CONFIG_SENSOR_Focus
1695         if (((regarray->reg == SEQUENCE_PROPERTY) && (regarray->val == SEQUENCE_INIT))
1696                 || (regarray == sensor_init_data)) {
1697                 sensor->info_priv.affm_reinit = 1;
1698         }
1699         #endif
1700
1701 sensor_write_array_end:
1702         sensor_task_lock(client,0);
1703     return err;
1704 }
1705 #if CONFIG_SENSOR_I2C_RDWRCHK
1706 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1707 {
1708     int cnt;
1709     int i = 0;
1710         char valchk;
1711
1712         cnt = 0;
1713         valchk = 0;
1714     while (regarray[i].reg != 0)
1715     {
1716                 sensor_read(client, regarray[i].reg, &valchk);
1717                 if (valchk != regarray[i].val)
1718                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1719
1720         i++;
1721     }
1722     return 0;
1723 }
1724 #endif
1725 #if CONFIG_SENSOR_Focus
1726 struct af_cmdinfo
1727 {
1728         char cmd_tag;
1729         char cmd_para[4];
1730         char validate_bit;
1731 };
1732 static int sensor_af_cmdset(struct i2c_client *client, int cmd_main, struct af_cmdinfo *cmdinfo)
1733 {
1734         int i;
1735         char read_tag=0xff,cnt;
1736
1737         if (cmdinfo) {
1738                 if (cmdinfo->validate_bit & 0x80) {
1739                         if (sensor_write(client, CMD_ACK_Reg, cmdinfo->cmd_tag)) {
1740                                 SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1741                                 goto sensor_af_cmdset_err;
1742                         }
1743                         SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,cmdinfo->cmd_tag);
1744                 }
1745                 for (i=0; i<4; i++) {
1746                         if (cmdinfo->validate_bit & (1<<i)) {
1747                                 if (sensor_write(client, CMD_PARA0_Reg-i, cmdinfo->cmd_para[i])) {
1748                                         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]);
1749                                         goto sensor_af_cmdset_err;
1750                                 }
1751                                 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]);
1752                         }
1753                 }
1754         } else {
1755                 if (sensor_write(client, CMD_ACK_Reg, 0xff)) {
1756                         SENSOR_TR("%s write CMD_ACK_Reg(main:0x%x no tag) error!\n",SENSOR_NAME_STRING(),cmd_main);
1757                         goto sensor_af_cmdset_err;
1758                 }
1759                 SENSOR_DG("%s write CMD_ACK_Reg(main:0x%x no tag) success!\n",SENSOR_NAME_STRING(),cmd_main);
1760         }
1761
1762         if (sensor_write(client, CMD_MAIN_Reg, cmd_main)) {
1763                 SENSOR_TR("%s write CMD_MAIN_Reg(main:0x%x) error!\n",SENSOR_NAME_STRING(),cmd_main);
1764                 goto sensor_af_cmdset_err;
1765         }
1766
1767         cnt = 0;
1768         do
1769         {
1770                 msleep(5);
1771                 if (sensor_read(client,CMD_ACK_Reg,&read_tag)){
1772                    SENSOR_TR("%s[%d] read TAG failed\n",SENSOR_NAME_STRING(),__LINE__);
1773                    break;
1774                 }
1775     } while((read_tag != 0x00)&& (cnt++<100));
1776
1777         SENSOR_DG("%s write CMD_MAIN_Reg(main:0x%x read tag:0x%x) success!\n",SENSOR_NAME_STRING(),cmd_main,read_tag);
1778         return 0;
1779 sensor_af_cmdset_err:
1780         return -1;
1781 }
1782
1783 static int sensor_af_idlechk(struct i2c_client *client)
1784 {
1785         int ret = 0;
1786         char state;     
1787         struct af_cmdinfo cmdinfo;
1788         
1789         SENSOR_DG("%s , %d\n",__FUNCTION__,__LINE__);
1790         
1791         cmdinfo.cmd_tag = 0x01;
1792         cmdinfo.validate_bit = 0x80;
1793         ret = sensor_af_cmdset(client, ReturnIdle_Cmd, &cmdinfo);
1794         if(0 != ret) {
1795                 SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1796                 ret = -1;
1797                 goto sensor_af_idlechk_end;
1798         }
1799         
1800
1801         do{
1802                 ret = sensor_read(client, CMD_ACK_Reg, &state);
1803                 if (ret != 0){
1804                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1805                    ret = -1;
1806                    goto sensor_af_idlechk_end;
1807                 }
1808         }while(0x00 != state);
1809
1810
1811 sensor_af_idlechk_end:
1812         return ret;
1813 }
1814
1815 static int sensor_af_single(struct i2c_client *client)
1816 {
1817         int ret = 0;
1818         char state,cnt;
1819         struct af_cmdinfo cmdinfo;
1820
1821         cmdinfo.cmd_tag = 0x01;
1822         cmdinfo.validate_bit = 0x80;
1823         ret = sensor_af_cmdset(client, SingleFocus_Cmd, &cmdinfo);
1824         if(0 != ret) {
1825                 SENSOR_TR("%s single focus mode set error!\n",SENSOR_NAME_STRING());
1826                 ret = -1;
1827                 goto sensor_af_single_end;
1828         }
1829         
1830         cnt = 0;
1831     do
1832     {
1833         if (cnt != 0) {
1834                         msleep(1);
1835         }
1836         cnt++;
1837                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1838                 if (ret != 0){
1839                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1840                    ret = -1;
1841                    goto sensor_af_single_end;
1842                 }
1843     }while((state == S_FOCUSING) && (cnt<100));
1844
1845         if (state != S_FOCUSED) {
1846         SENSOR_TR("%s[%d] focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),__LINE__,state);
1847                 ret = -1;
1848                 goto sensor_af_single_end;
1849     }
1850
1851 sensor_af_single_end:
1852         return ret;
1853 }
1854
1855 static int sensor_af_const(struct i2c_client *client)
1856 {
1857         int ret = 0;
1858
1859
1860         return ret;
1861 }
1862
1863
1864 static int sensor_af_init(struct i2c_client *client)
1865 {
1866         int ret = 0;
1867         char state,cnt;
1868
1869         ret = sensor_write_array(client, sensor_af_firmware);
1870     if (ret != 0) {
1871         SENSOR_TR("%s Download firmware failed\n",SENSOR_NAME_STRING());
1872         ret = -1;
1873                 goto sensor_af_init_end;
1874     }
1875
1876     cnt = 0;
1877     do
1878     {
1879         if (cnt != 0) {
1880                         msleep(1);
1881         }
1882         cnt++;
1883                 ret = sensor_read(client, STA_FOCUS_Reg, &state);
1884                 if (ret != 0){
1885                    SENSOR_TR("%s[%d] read focus_status failed\n",SENSOR_NAME_STRING(),__LINE__);
1886                    ret = -1;
1887                    goto sensor_af_init_end;
1888                 }
1889     }while((state == S_STARTUP) && (cnt<100));  
1890
1891     if (state != S_IDLE) {
1892         SENSOR_TR("%s focus state(0x%x) is error!\n",SENSOR_NAME_STRING(),state);
1893         ret = -1;
1894         goto sensor_af_init_end;
1895     }
1896
1897 sensor_af_init_end:
1898         SENSOR_DG("%s %s ret:0x%x \n",SENSOR_NAME_STRING(),__FUNCTION__,ret);
1899         return ret;
1900 }
1901
1902 static int sensor_af_wq_function(struct i2c_client *client)
1903 {
1904         struct sensor *sensor = to_sensor(client);
1905         int ret=0;
1906
1907         SENSOR_DG("%s %s Enter\n",SENSOR_NAME_STRING(), __FUNCTION__);
1908
1909         mutex_lock(&sensor->wq_lock);
1910         if (sensor_af_init(client)) {
1911                 sensor->info_priv.funmodule_state &= (~SENSOR_AF_IS_OK);
1912                 ret = -1;
1913         } else {
1914                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1915                 switch (sensor->info_priv.auto_focus)
1916                 {
1917                         /*case SENSOR_AF_MODE_INFINITY:
1918                         {
1919                                 focus_pos = 0x00;
1920                         }
1921                         case SENSOR_AF_MODE_MACRO:
1922                         {
1923                                 if (focus_pos != 0x00)
1924                                         focus_pos = 0xff;
1925
1926                                 sensor_af_idlechk(client);
1927                                 cmdinfo.cmd_tag = StepFocus_Spec_Tag;
1928                                 cmdinfo.cmd_para[0] = focus_pos;
1929                                 cmdinfo.validate_bit = 0x81;
1930                                 //ret = sensor_af_cmdset(client, StepMode_Cmd, &cmdinfo);
1931                                 break;
1932                         }*/
1933                         case SENSOR_AF_MODE_AUTO:
1934                         {
1935                                 ret = sensor_af_single(client);
1936                                 break;
1937                         }
1938                         /*case SENSOR_AF_MODE_CONTINUOUS:
1939                         {
1940                                 ret = sensor_af_const(client);
1941                                 break;
1942                         }*/
1943                         case SENSOR_AF_MODE_CLOSE:
1944                         {
1945                                 ret = 0;
1946                                 break;
1947                         }
1948                         default:
1949             {
1950                                 SENSOR_DG("%s focus mode(0x%x) is unkonwn\n",SENSOR_NAME_STRING(),sensor->info_priv.auto_focus);
1951                 goto sensor_af_wq_function_end;
1952                         }
1953                 }
1954
1955                 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);
1956         }
1957
1958 sensor_af_wq_function_end:
1959         sensor->sensor_wk.state = sensor_work_ready;
1960         mutex_unlock(&sensor->wq_lock);
1961         return ret;
1962 }
1963 static void sensor_af_workqueue(struct work_struct *work)
1964 {
1965         struct sensor_work *sensor_work = container_of(work, struct sensor_work, dwork.work);
1966         struct i2c_client *client = sensor_work->client;
1967
1968         if (sensor_af_wq_function(client) < 0) {
1969                 SENSOR_TR("%s af workqueue return false\n",SENSOR_NAME_STRING());
1970         }
1971 }
1972 #endif
1973 static int sensor_parameter_record(struct i2c_client *client)
1974 {
1975         u8 ret_l,ret_m,ret_h;
1976         u8 tp_l,tp_m,tp_h;
1977         struct sensor *sensor = to_sensor(client);
1978
1979         sensor_write(client,0x3503,0x07);       //stop AE/AG
1980         sensor_write(client,0x3406,0x01);   //stop AWB
1981
1982         sensor_read(client,0x3500,&ret_h);
1983         sensor_read(client,0x3501, &ret_m);
1984         sensor_read(client,0x3502, &ret_l);
1985         tp_l = ret_l;
1986         tp_m = ret_m;
1987         tp_h = ret_h;
1988         SENSOR_DG(" %s Read 0x3500 = 0x%02x  0x3501 = 0x%02x 0x3502=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_m, ret_l);
1989         //sensor->parameter.preview_exposure = (tp_h<<12)+(tp_m<<4)+(tp_l>>4);
1990         sensor->parameter.preview_exposure = ((((tp_h & 0x0f) << 8)+ tp_m) << 4) + (tp_l>>4);
1991
1992         sensor_read(client,0x350c, &ret_h);
1993         sensor_read(client,0x350d, &ret_l);
1994         sensor->parameter.preview_line_width = ret_h & 0xff;
1995         sensor->parameter.preview_line_width = (sensor->parameter.preview_line_width << 8) +ret_l;
1996         //Read back AGC Gain for preview
1997         sensor_read(client,0x350a, &ret_h);
1998         sensor_read(client,0x350b, &tp_l);
1999         sensor->parameter.preview_gain = ((ret_h & 0x01) << 8)  + tp_l;
2000         //preview_maxlines
2001         sensor_read(client,0x380e, &ret_h);
2002         sensor->parameter.preview_maxlines = ret_h;
2003         sensor->parameter.preview_maxlines <<= 8;
2004         sensor_read(client,0x380f, &tp_l);
2005         sensor->parameter.preview_maxlines += tp_l;
2006
2007         sensor->parameter.capture_framerate = 375;
2008         sensor->parameter.preview_framerate = 1500;
2009
2010         sensor_read(client,0x3400,&sensor->parameter.awb[0]);           //record awb value
2011         sensor_read(client,0x3401,&sensor->parameter.awb[1]);
2012         sensor_read(client,0x3402,&sensor->parameter.awb[2]);
2013         sensor_read(client,0x3403,&sensor->parameter.awb[3]);
2014         sensor_read(client,0x3404,&sensor->parameter.awb[4]);
2015         sensor_read(client,0x3405,&sensor->parameter.awb[5]);
2016
2017         SENSOR_DG(" %s Read 0x350c = 0x%02x  0x350d = 0x%02x 0x350b=0x%02x \n",SENSOR_NAME_STRING(), ret_h, ret_l, sensor->parameter.preview_gain);
2018         return 0;
2019 }
2020 static int sensor_ae_transfer(struct i2c_client *client)
2021 {
2022         u8  ExposureLow;
2023         u8  ExposureMid;
2024         u8  ExposureHigh;
2025         u16 ulCapture_Exposure;
2026         u32 ulCapture_Exposure_Gain;
2027         u16  iCapture_Gain;
2028         u8   Lines_10ms;
2029         bool m_60Hz = 0;
2030         u8  reg_l = 0,reg_h =0;
2031         u16 Preview_Maxlines;
2032         u8  Gain;
2033         u32  Capture_MaxLines;
2034         struct sensor *sensor = to_sensor(client);
2035
2036         //Preview_Maxlines = sensor->parameter.preview_line_width;
2037         Preview_Maxlines = sensor->parameter.preview_maxlines;
2038         Gain = sensor->parameter.preview_gain;
2039         /*
2040         sensor_read(client,0x350c, &reg_h);
2041         sensor_read(client,0x350d, &reg_l);
2042         Capture_MaxLines = reg_h & 0xff;
2043         Capture_MaxLines = (Capture_MaxLines << 8) + reg_l;
2044         */
2045         //capture_maxlines
2046         sensor_read(client,0x380e, &reg_h);
2047         Capture_MaxLines = reg_h;
2048         Capture_MaxLines <<= 8;
2049         sensor_read(client,0x380f, &reg_l);
2050         Capture_MaxLines +=  reg_l;
2051         
2052         if(m_60Hz== 1) {
2053                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/12000;
2054         } else {
2055                 Lines_10ms = sensor->parameter.capture_framerate * Capture_MaxLines/10000;
2056         }
2057         Lines_10ms = Lines_10ms & 0xffff;
2058         
2059         if(Preview_Maxlines == 0)
2060                 Preview_Maxlines = 1;
2061
2062         //ulCapture_Exposure =
2063         //      (sensor->parameter.preview_exposure*(sensor->parameter.capture_framerate)*(Capture_MaxLines))/(((Preview_Maxlines)*(sensor->parameter.preview_framerate)));
2064
2065         ulCapture_Exposure =
2066                 ((sensor->parameter.preview_exposure*(((sensor->parameter.capture_framerate)*(Capture_MaxLines) + 50)/100)) << 1)/(((Preview_Maxlines)*(sensor->parameter.preview_framerate) + 50)/100);
2067         ulCapture_Exposure = ulCapture_Exposure & 0xffff;
2068         
2069         iCapture_Gain = (Gain & 0x0f) + 16;
2070         if (Gain & 0x10) {
2071                 iCapture_Gain = iCapture_Gain << 1;
2072         }
2073         if (Gain & 0x20) {
2074                 iCapture_Gain = iCapture_Gain << 1;
2075         }
2076         if (Gain & 0x40) {
2077                 iCapture_Gain = iCapture_Gain << 1;
2078         }
2079         if (Gain & 0x80) {
2080                 iCapture_Gain = iCapture_Gain << 1;
2081         }
2082         
2083         //ulCapture_Exposure_Gain =(u32) (11 * ulCapture_Exposure * iCapture_Gain/5);   //0ld value 2.5, ½â¾ö¹ýÁÁ
2084         ulCapture_Exposure_Gain =(u32) (ulCapture_Exposure * iCapture_Gain);
2085         
2086         if(ulCapture_Exposure_Gain < Capture_MaxLines*16) {
2087                 ulCapture_Exposure = ulCapture_Exposure_Gain/16;
2088                 if (ulCapture_Exposure > Lines_10ms)
2089                 {
2090                         //ulCapture_Exposure *= 1.7;
2091                         ulCapture_Exposure /= Lines_10ms;
2092                         ulCapture_Exposure *= Lines_10ms;
2093                 }
2094         } else {
2095                 ulCapture_Exposure = Capture_MaxLines;
2096                 //ulCapture_Exposure_Gain *= 1.5;
2097         }
2098         
2099         if(ulCapture_Exposure == 0)
2100                 ulCapture_Exposure = 1;
2101         
2102         iCapture_Gain = ((ulCapture_Exposure_Gain << 1)/ulCapture_Exposure + 1) >> 1;
2103         iCapture_Gain = iCapture_Gain & 0xffff;
2104         
2105         ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
2106         ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
2107         ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
2108
2109         //Gain = 0;
2110         Gain = 0x10;
2111         if (iCapture_Gain > 31) {
2112                 Gain |= 0x10;
2113                 iCapture_Gain = iCapture_Gain >> 1;
2114         }
2115         if (iCapture_Gain > 31) {
2116                 Gain |= 0x20;
2117                 iCapture_Gain = iCapture_Gain >> 1;
2118         }
2119         if (iCapture_Gain > 31) {
2120                 Gain |= 0x40;
2121                 iCapture_Gain = iCapture_Gain >> 1;
2122         }
2123         if (iCapture_Gain > 31) {
2124                 Gain |= 0x80;
2125                 iCapture_Gain = iCapture_Gain >> 1;
2126         }
2127         if (iCapture_Gain > 16)
2128                 Gain |= ((iCapture_Gain -16) & 0x0f);
2129         if(Gain == 0x10)
2130                 Gain = 0x11;
2131         // write the gain and exposure to 0x350* registers
2132         //m_iWrite0x350b=Gain;
2133         sensor_write(client,0x350b, Gain);
2134         Gain = (Gain >> 8) & 0x01;
2135         sensor_write(client,0x350a, Gain);
2136         //m_iWrite0x3502=ExposureLow;
2137         sensor_write(client,0x3502, ExposureLow);
2138         //m_iWrite0x3501=ExposureMid;
2139         sensor_write(client,0x3501, ExposureMid);
2140         //m_iWrite0x3500=ExposureHigh;
2141         sensor_write(client,0x3500, ExposureHigh);
2142         // SendToFile("Gain = 0x%x\r\n", Gain);
2143         // SendToFile("ExposureLow = 0x%x\r\n", ExposureLow);
2144         // SendToFile("ExposureMid = 0x%x\r\n", ExposureMid);
2145         // SendToFile("ExposureHigh = 0x%x\r\n", ExposureHigh);
2146         //¼Ó³¤ÑÓʱ£¬±ÜÃâ°µ´¦ÅÄÕÕʱµÄÃ÷°µ·Ö½çÎÊÌâ
2147         //camera_timed_wait(200);
2148         //linzhk camera_timed_wait(500);
2149
2150         sensor_write(client,0x3400,sensor->parameter.awb[0]);           // resume awb value
2151         sensor_write(client,0x3401,sensor->parameter.awb[1]);
2152         sensor_write(client,0x3402,sensor->parameter.awb[2]);
2153         sensor_write(client,0x3403,sensor->parameter.awb[3]);
2154         sensor_write(client,0x3404,sensor->parameter.awb[4]);
2155         sensor_write(client,0x3405,sensor->parameter.awb[5]);
2156
2157         SENSOR_DG(" %s Write 0x350b = 0x%02x  0x3502 = 0x%02x 0x3501=0x%02x 0x3500 = 0x%02x\n",SENSOR_NAME_STRING(), Gain, ExposureLow, ExposureMid, ExposureHigh);
2158         mdelay(100);
2159         return 0;
2160 }
2161 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2162 {
2163         struct soc_camera_link *icl = to_soc_camera_link(icd);
2164         int ret = 0;
2165
2166     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2167
2168         switch (cmd)
2169         {
2170                 case Sensor_PowerDown:
2171                 {
2172                         if (icl->powerdown) {
2173                                 ret = icl->powerdown(icd->pdev, on);
2174                                 if (ret == RK29_CAM_IO_SUCCESS) {
2175                                         if (on == 0) {
2176                                                 mdelay(2);
2177                                                 if (icl->reset)
2178                                                         icl->reset(icd->pdev);
2179                                         }
2180                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2181                                         ret = -ENODEV;
2182                                         goto sensor_power_end;
2183                                 }
2184                         }
2185                         break;
2186                 }
2187                 case Sensor_Flash:
2188                 {
2189                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2190                 struct sensor *sensor = to_sensor(client);
2191
2192                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2193                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2194                         }
2195                         break;
2196                 }
2197                 default:
2198                 {
2199                         SENSOR_TR("%s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),cmd);
2200                         break;
2201                 }
2202         }
2203
2204 sensor_power_end:
2205         return ret;
2206 }
2207 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2208 {
2209     struct i2c_client *client = sd->priv;
2210     struct soc_camera_device *icd = client->dev.platform_data;
2211     struct sensor *sensor = to_sensor(client);
2212         const struct v4l2_queryctrl *qctrl;
2213     const struct sensor_datafmt *fmt;
2214     char value;
2215     int ret,pid = 0;
2216
2217     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2218
2219         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2220                 ret = -ENODEV;
2221                 goto sensor_INIT_ERR;
2222         }
2223
2224     /* soft reset */
2225         if (sensor_task_lock(client,1)<0)
2226                 goto sensor_INIT_ERR;
2227     ret = sensor_write(client, 0x3008, 0x80);
2228     if (ret != 0) {
2229         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2230         ret = -ENODEV;
2231                 goto sensor_INIT_ERR;
2232     }
2233
2234     mdelay(5);  //delay 5 microseconds
2235         /* check if it is an sensor sensor */
2236     ret = sensor_read(client, 0x300a, &value);
2237     if (ret != 0) {
2238         SENSOR_TR("read chip id high byte failed\n");
2239         ret = -ENODEV;
2240         goto sensor_INIT_ERR;
2241     }
2242
2243     pid |= (value << 8);
2244
2245     ret = sensor_read(client, 0x300b, &value);
2246     if (ret != 0) {
2247         SENSOR_TR("read chip id low byte failed\n");
2248         ret = -ENODEV;
2249         goto sensor_INIT_ERR;
2250     }
2251
2252     pid |= (value & 0xff);
2253     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2254
2255     if (pid == SENSOR_ID) {
2256         sensor->model = SENSOR_V4L2_IDENT;
2257     } else {
2258         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2259         ret = -ENODEV;
2260         goto sensor_INIT_ERR;
2261     }
2262
2263     ret = sensor_write_array(client, sensor_init_data);
2264     if (ret != 0)
2265     {
2266         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2267         goto sensor_INIT_ERR;
2268     }
2269         sensor_task_lock(client,0);
2270     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
2271         fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2272     if (!fmt) {
2273         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2274         ret = -EINVAL;
2275         goto sensor_INIT_ERR;
2276     }
2277         sensor->info_priv.fmt = *fmt;
2278
2279     /* sensor sensor information for initialization  */
2280         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2281         if (qctrl)
2282         sensor->info_priv.whiteBalance = qctrl->default_value;
2283         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2284         if (qctrl)
2285         sensor->info_priv.brightness = qctrl->default_value;
2286         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2287         if (qctrl)
2288         sensor->info_priv.effect = qctrl->default_value;
2289         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2290         if (qctrl)
2291         sensor->info_priv.exposure = qctrl->default_value;
2292
2293         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2294         if (qctrl)
2295         sensor->info_priv.saturation = qctrl->default_value;
2296         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2297         if (qctrl)
2298         sensor->info_priv.contrast = qctrl->default_value;
2299         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2300         if (qctrl)
2301         sensor->info_priv.mirror = qctrl->default_value;
2302         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2303         if (qctrl)
2304         sensor->info_priv.flip = qctrl->default_value;
2305         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2306         if (qctrl)
2307         sensor->info_priv.scene = qctrl->default_value;
2308         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2309         if (qctrl)
2310         sensor->info_priv.digitalzoom = qctrl->default_value;
2311
2312     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2313         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2314         if (qctrl)
2315         sensor->info_priv.focus = qctrl->default_value;
2316
2317         #if CONFIG_SENSOR_Flash
2318         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2319         if (qctrl)
2320         sensor->info_priv.flash = qctrl->default_value;
2321     #endif
2322     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);
2323
2324     sensor->info_priv.funmodule_state |= SENSOR_INIT_IS_OK;
2325         
2326     return 0;
2327 sensor_INIT_ERR:
2328     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2329         sensor_task_lock(client,0);
2330         sensor_deactivate(client);
2331     return ret;
2332 }
2333 static int sensor_deactivate(struct i2c_client *client)
2334 {
2335         struct soc_camera_device *icd = client->dev.platform_data;
2336     struct sensor *sensor = to_sensor(client);
2337     
2338         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2339
2340         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2341     if (sensor->info_priv.funmodule_state & SENSOR_INIT_IS_OK) {
2342         sensor_task_lock(client, 1);
2343         sensor_write(client, 0x3017, 0x00);  // FREX,VSYNC,HREF,PCLK,D9-D6
2344         sensor_write(client, 0x3018, 0x03);  // D5-D0
2345         sensor_write(client,0x3019,0x00);    // STROBE,SDA
2346         sensor_task_lock(client, 0);
2347     } 
2348     sensor_ioctrl(icd, Sensor_PowerDown, 1);
2349     msleep(100); 
2350         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2351         icd->user_width = SENSOR_INIT_WIDTH;
2352     icd->user_height = SENSOR_INIT_HEIGHT;
2353     sensor->info_priv.funmodule_state &= ~SENSOR_INIT_IS_OK;
2354         return 0;
2355 }
2356
2357 static  struct reginfo sensor_power_down_sequence[]=
2358 {
2359     {0x00,0x00}
2360 };
2361
2362 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2363 {
2364     int ret;
2365     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2366
2367     if (pm_msg.event == PM_EVENT_SUSPEND) {
2368         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2369         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2370         if (ret != 0) {
2371             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2372             return ret;
2373         } else {
2374             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2375             if (ret < 0) {
2376                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2377                 return -EINVAL;
2378             }
2379         }
2380     } else {
2381         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2382         return -EINVAL;
2383     }
2384
2385     return 0;
2386 }
2387
2388 static int sensor_resume(struct soc_camera_device *icd)
2389 {
2390         int ret;
2391
2392     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2393     if (ret < 0) {
2394                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2395         return -EINVAL;
2396     }
2397
2398         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2399         return 0;
2400 }
2401
2402 static int sensor_set_bus_param(struct soc_camera_device *icd,
2403                                 unsigned long flags)
2404 {
2405
2406     return 0;
2407 }
2408
2409 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2410 {
2411     struct soc_camera_link *icl = to_soc_camera_link(icd);
2412     unsigned long flags = SENSOR_BUS_PARAM;
2413
2414     return soc_camera_apply_sensor_flags(icl, flags);
2415 }
2416
2417 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2418 {
2419     struct i2c_client *client = sd->priv;
2420     struct soc_camera_device *icd = client->dev.platform_data;
2421     struct sensor *sensor = to_sensor(client);
2422
2423     mf->width   = icd->user_width;
2424         mf->height      = icd->user_height;
2425         mf->code        = sensor->info_priv.fmt.code;
2426         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2427         mf->field       = V4L2_FIELD_NONE;
2428
2429     return 0;
2430 }
2431 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2432 {
2433     bool ret = false;
2434
2435         if ((mf->width == 1024) && (mf->height == 768)) {
2436                 ret = true;
2437         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2438                 ret = true;
2439         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2440                 ret = true;
2441         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2442                 ret = true;
2443         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2444                 ret = true;
2445         }
2446
2447         if (ret == true)
2448                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2449         return ret;
2450 }
2451
2452 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2453 {
2454     bool ret = false;
2455
2456         if ((mf->width == 1280) && (mf->height == 720)) {
2457                 ret = true;
2458         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2459                 ret = true;
2460         }
2461
2462         if (ret == true)
2463                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2464         return ret;
2465 }
2466 static int sensor_s_fmt(struct v4l2_subdev *sd,struct v4l2_mbus_framefmt *mf)
2467 {
2468     struct i2c_client *client = sd->priv;
2469     const struct sensor_datafmt *fmt;
2470     struct sensor *sensor = to_sensor(client);
2471         const struct v4l2_queryctrl *qctrl;
2472         struct soc_camera_device *icd = client->dev.platform_data;
2473     struct reginfo *winseqe_set_addr=NULL;
2474     int ret = 0, set_w,set_h;
2475
2476         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2477                                    ARRAY_SIZE(sensor_colour_fmts));
2478         if (!fmt) {
2479         ret = -EINVAL;
2480         goto sensor_s_fmt_end;
2481     }
2482
2483         if (sensor->info_priv.fmt.code != mf->code) {
2484                 switch (mf->code)
2485                 {
2486                         case V4L2_MBUS_FMT_YUYV8_2X8:
2487                         {
2488                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2489                                 break;
2490                         }
2491                         case V4L2_MBUS_FMT_UYVY8_2X8:
2492                         {
2493                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2494                                 break;
2495                         }
2496                         default:
2497                                 break;
2498                 }
2499                 if (winseqe_set_addr != NULL) {
2500             sensor_write_array(client, winseqe_set_addr);
2501                         sensor->info_priv.fmt.code = mf->code;
2502             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2503                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2504                 } else {
2505                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2506                 }
2507         }
2508
2509     set_w = mf->width;
2510     set_h = mf->height;
2511
2512         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2513         {
2514                 winseqe_set_addr = sensor_qcif;
2515         set_w = 176;
2516         set_h = 144;
2517         }
2518         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2519     {
2520         winseqe_set_addr = sensor_qvga;
2521         set_w = 320;
2522         set_h = 240;
2523     }
2524     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2525     {
2526         winseqe_set_addr = sensor_cif;
2527         set_w = 352;
2528         set_h = 288;
2529     }
2530     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2531     {
2532         winseqe_set_addr = sensor_vga;
2533         set_w = 640;
2534         set_h = 480;
2535     }
2536     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2537     {
2538         winseqe_set_addr = sensor_svga;
2539         set_w = 800;
2540         set_h = 600;
2541     }
2542         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2543     {
2544         winseqe_set_addr = sensor_xga;
2545         set_w = 1024;
2546         set_h = 768;
2547     }
2548         else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2549     {
2550         winseqe_set_addr = sensor_720p;
2551         set_w = 1280;
2552         set_h = 720;
2553     }
2554     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2555     {
2556         winseqe_set_addr = sensor_sxga;
2557         set_w = 1280;
2558         set_h = 1024;
2559     }
2560     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2561     {
2562         winseqe_set_addr = sensor_uxga;
2563         set_w = 1600;
2564         set_h = 1200;
2565     }
2566     else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2567     {
2568         winseqe_set_addr = sensor_1080p;
2569         set_w = 1920;
2570         set_h = 1080;
2571     }
2572         else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2573     {
2574         winseqe_set_addr = sensor_qxga;
2575         set_w = 2048;
2576         set_h = 1536;
2577     }
2578         else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2579     {
2580         winseqe_set_addr = sensor_qsxga;
2581         set_w = 2592;
2582         set_h = 1944;
2583     }
2584     else
2585     {
2586         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
2587         set_w = SENSOR_INIT_WIDTH;
2588         set_h = SENSOR_INIT_HEIGHT;
2589                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2590     }
2591
2592     if (winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr)
2593     {
2594                 if (sensor_fmt_capturechk(sd,mf) == true) {                                     /* ddl@rock-chips.com : Capture */
2595                         sensor_parameter_record(client);
2596                 /*#if CONFIG_SENSOR_Focus
2597                         sensor_af_idlechk(client);
2598                         if (sensor->info_priv.auto_focus == SENSOR_AF_MODE_CONTINUOUS)
2599                                 sensor_af_cmdset(client, PauseFocus_Cmd, NULL);
2600                 #endif*/
2601                 #if CONFIG_SENSOR_Flash
2602             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2603                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
2604                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
2605             }
2606         #endif
2607                 }else {                                        /* ddl@rock-chips.com : Video */
2608                 #if CONFIG_SENSOR_Flash 
2609             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2610                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2611                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
2612             }
2613         #endif
2614         }
2615                 if ((sensor->info_priv.winseqe_cur_addr->reg == SEQUENCE_PROPERTY) && (sensor->info_priv.winseqe_cur_addr->val == SEQUENCE_INIT)) {
2616                         if (((winseqe_set_addr->reg == SEQUENCE_PROPERTY) && (winseqe_set_addr->val == SEQUENCE_NORMAL))
2617                                 || (winseqe_set_addr->reg != SEQUENCE_PROPERTY)) {
2618                                 ret |= sensor_write_array(client,sensor_init_data);
2619                                 SENSOR_DG("\n%s reinit ret:0x%x \n",SENSOR_NAME_STRING(), ret);
2620                         }
2621                 }
2622         ret |= sensor_write_array(client, winseqe_set_addr);
2623         if (ret != 0) {
2624             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2625             #if CONFIG_SENSOR_Flash
2626             if (sensor_fmt_capturechk(sd,mf) == true) {
2627                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
2628                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2629                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
2630                 }
2631             }
2632             #endif
2633             goto sensor_s_fmt_end;
2634         }
2635         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2636
2637                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
2638                         sensor_ae_transfer(client);
2639                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2640                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2641                         if (sensor->info_priv.whiteBalance != 0) {
2642                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2643                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2644                         }
2645                         sensor->info_priv.snap2preview = true;
2646                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
2647                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2648                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2649
2650                         sensor->info_priv.video2preview = true;
2651                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
2652                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2653                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
2654                         if (sensor->info_priv.snap2preview == true) {
2655                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2656                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
2657                         }
2658             msleep(600);
2659                         sensor->info_priv.video2preview = false;
2660                         sensor->info_priv.snap2preview = false;
2661                 }
2662         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2663     }
2664     else
2665     {
2666         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
2667     }
2668         mf->width = set_w;
2669         mf->height = set_h;
2670 sensor_s_fmt_end:
2671     return ret;
2672 }
2673
2674 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2675 {
2676     struct i2c_client *client = sd->priv;
2677     struct sensor *sensor = to_sensor(client);
2678     const struct sensor_datafmt *fmt;
2679     int ret = 0;
2680    
2681         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2682                                    ARRAY_SIZE(sensor_colour_fmts));
2683         if (fmt == NULL) {
2684                 fmt = &sensor->info_priv.fmt;
2685         mf->code = fmt->code;
2686         } 
2687
2688     if (mf->height > SENSOR_MAX_HEIGHT)
2689         mf->height = SENSOR_MAX_HEIGHT;
2690     else if (mf->height < SENSOR_MIN_HEIGHT)
2691         mf->height = SENSOR_MIN_HEIGHT;
2692
2693     if (mf->width > SENSOR_MAX_WIDTH)
2694         mf->width = SENSOR_MAX_WIDTH;
2695     else if (mf->width < SENSOR_MIN_WIDTH)
2696         mf->width = SENSOR_MIN_WIDTH;
2697
2698     mf->colorspace = fmt->colorspace;
2699     
2700     return ret;
2701 }
2702
2703  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2704 {
2705     struct i2c_client *client = sd->priv;
2706
2707     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2708         return -EINVAL;
2709
2710     if (id->match.addr != client->addr)
2711         return -ENODEV;
2712
2713     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2714     id->revision = 0;
2715
2716     return 0;
2717 }
2718 #if CONFIG_SENSOR_Brightness
2719 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2720 {
2721     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2722
2723     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2724     {
2725         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2726         {
2727             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2728             {
2729                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2730                 return -EINVAL;
2731             }
2732             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2733             return 0;
2734         }
2735     }
2736         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2737     return -EINVAL;
2738 }
2739 #endif
2740 #if CONFIG_SENSOR_Effect
2741 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2742 {
2743     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2744         
2745     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2746     {
2747         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2748         {
2749             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2750             {
2751                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2752                 return -EINVAL;
2753             }
2754             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2755             return 0;
2756         }
2757     }
2758         SENSOR_TR("\n%s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2759     return -EINVAL;
2760 }
2761 #endif
2762 #if CONFIG_SENSOR_Exposure
2763 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2764 {
2765     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2766
2767     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2768     {
2769         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2770         {
2771             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2772             {
2773                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2774                 return -EINVAL;
2775             }
2776             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2777             return 0;
2778         }
2779     }
2780         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2781     return -EINVAL;
2782 }
2783 #endif
2784 #if CONFIG_SENSOR_Saturation
2785 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2786 {
2787     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2788
2789     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2790     {
2791         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2792         {
2793             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2794             {
2795                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2796                 return -EINVAL;
2797             }
2798             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2799             return 0;
2800         }
2801     }
2802     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2803     return -EINVAL;
2804 }
2805 #endif
2806 #if CONFIG_SENSOR_Contrast
2807 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2808 {
2809     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2810
2811     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2812     {
2813         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2814         {
2815             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2816             {
2817                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2818                 return -EINVAL;
2819             }
2820             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2821             return 0;
2822         }
2823     }
2824     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2825     return -EINVAL;
2826 }
2827 #endif
2828 #if CONFIG_SENSOR_Mirror
2829 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2830 {
2831     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2832
2833     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2834     {
2835         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2836         {
2837             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2838             {
2839                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2840                 return -EINVAL;
2841             }
2842             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2843             return 0;
2844         }
2845     }
2846     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2847     return -EINVAL;
2848 }
2849 #endif
2850 #if CONFIG_SENSOR_Flip
2851 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2852 {
2853     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2854
2855     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2856     {
2857         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2858         {
2859             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2860             {
2861                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2862                 return -EINVAL;
2863             }
2864             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2865             return 0;
2866         }
2867     }
2868     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2869     return -EINVAL;
2870 }
2871 #endif
2872 #if CONFIG_SENSOR_Scene
2873 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2874 {
2875     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2876
2877     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2878     {
2879         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2880         {
2881             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2882             {
2883                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2884                 return -EINVAL;
2885             }
2886             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2887             return 0;
2888         }
2889     }
2890     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2891     return -EINVAL;
2892 }
2893 #endif
2894 #if CONFIG_SENSOR_WhiteBalance
2895 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2896 {
2897     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2898
2899     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2900     {
2901         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2902         {
2903             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2904             {
2905                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2906                 return -EINVAL;
2907             }
2908             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2909             return 0;
2910         }
2911     }
2912         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2913     return -EINVAL;
2914 }
2915 #endif
2916 #if CONFIG_SENSOR_DigitalZoom
2917 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2918 {
2919     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2920     struct sensor *sensor = to_sensor(client);
2921         const struct v4l2_queryctrl *qctrl_info;
2922     int digitalzoom_cur, digitalzoom_total;
2923
2924         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2925         if (qctrl_info)
2926                 return -EINVAL;
2927
2928     digitalzoom_cur = sensor->info_priv.digitalzoom;
2929     digitalzoom_total = qctrl_info->maximum;
2930
2931     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2932     {
2933         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2934         return -EINVAL;
2935     }
2936
2937     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2938     {
2939         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2940         return -EINVAL;
2941     }
2942
2943     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2944     {
2945         *value = digitalzoom_total - digitalzoom_cur;
2946     }
2947
2948     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2949     {
2950         *value = 0 - digitalzoom_cur;
2951     }
2952
2953     digitalzoom_cur += *value;
2954
2955     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2956     {
2957         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2958         {
2959             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2960             return -EINVAL;
2961         }
2962         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2963         return 0;
2964     }
2965
2966     return -EINVAL;
2967 }
2968 #endif
2969 #if CONFIG_SENSOR_Focus
2970 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2971 {
2972         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2973     struct sensor *sensor = to_sensor(client);
2974         const struct v4l2_queryctrl *qctrl_info;
2975         int ret = 0;
2976
2977         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2978         if (!qctrl_info) {
2979                 ret = -EINVAL;
2980         goto sensor_set_focus_absolute_end;
2981         }
2982
2983         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2984                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {                 
2985                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2986                 } else {
2987                         ret = -EINVAL;
2988                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2989                 }
2990         } else {
2991                 ret = -EACCES;
2992                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2993                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2994         }
2995
2996 sensor_set_focus_absolute_end:
2997         return ret;
2998 }
2999 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3000 {
3001         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3002         struct sensor *sensor = to_sensor(client);
3003         const struct v4l2_queryctrl *qctrl_info;
3004         int ret = 0;
3005
3006         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
3007         if (!qctrl_info) {
3008                 ret = -EINVAL;
3009         goto sensor_set_focus_relative_end;
3010         }
3011
3012         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
3013                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {                 
3014                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3015                 } else {
3016                         ret = -EINVAL;
3017                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3018                 }
3019         } else {
3020                 ret = -EACCES;
3021                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3022                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3023         }
3024 sensor_set_focus_relative_end:
3025         return ret;
3026 }
3027
3028 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3029 {
3030         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3031         struct sensor *sensor = to_sensor(client);
3032         int ret = 0;
3033     
3034         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
3035                 switch (value)
3036                 {
3037                         case SENSOR_AF_MODE_AUTO:
3038                         {
3039                                 ret = sensor_af_single(client);
3040                                 break;
3041                         }
3042
3043                         /*case SENSOR_AF_MODE_MACRO:
3044                         {
3045                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
3046                                 break;
3047                         }
3048
3049                         case SENSOR_AF_MODE_INFINITY:
3050                         {
3051                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
3052                                 break;
3053                         }
3054                         
3055                         case SENSOR_AF_MODE_CONTINUOUS:
3056                         {
3057                                 ret = sensor_af_const(client);
3058                                 break;
3059                         }*/
3060                         default:
3061                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3062                                 break;
3063
3064                 }
3065
3066                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
3067         } else {
3068                 ret = -EACCES;
3069                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
3070                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
3071         }
3072
3073         return ret;
3074 }
3075 #endif
3076 #if CONFIG_SENSOR_Flash
3077 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3078 {    
3079     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3080         if (value == 3) {       /* ddl@rock-chips.com: torch */
3081             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3082         } else {
3083             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3084         }
3085         SENSOR_DG("%s..%s : %d\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3086         return 0;
3087     }
3088     
3089         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3090     return -EINVAL;
3091 }
3092 #endif
3093 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3094 {
3095     struct i2c_client *client = sd->priv;
3096     struct sensor *sensor = to_sensor(client);
3097     const struct v4l2_queryctrl *qctrl;
3098
3099     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3100
3101     if (!qctrl)
3102     {
3103         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3104         return -EINVAL;
3105     }
3106
3107     switch (ctrl->id)
3108     {
3109         case V4L2_CID_BRIGHTNESS:
3110             {
3111                 ctrl->value = sensor->info_priv.brightness;
3112                 break;
3113             }
3114         case V4L2_CID_SATURATION:
3115             {
3116                 ctrl->value = sensor->info_priv.saturation;
3117                 break;
3118             }
3119         case V4L2_CID_CONTRAST:
3120             {
3121                 ctrl->value = sensor->info_priv.contrast;
3122                 break;
3123             }
3124         case V4L2_CID_DO_WHITE_BALANCE:
3125             {
3126                 ctrl->value = sensor->info_priv.whiteBalance;
3127                 break;
3128             }
3129         case V4L2_CID_EXPOSURE:
3130             {
3131                 ctrl->value = sensor->info_priv.exposure;
3132                 break;
3133             }
3134         case V4L2_CID_HFLIP:
3135             {
3136                 ctrl->value = sensor->info_priv.mirror;
3137                 break;
3138             }
3139         case V4L2_CID_VFLIP:
3140             {
3141                 ctrl->value = sensor->info_priv.flip;
3142                 break;
3143             }
3144         default :
3145                 break;
3146     }
3147     return 0;
3148 }
3149
3150
3151
3152 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3153 {
3154     struct i2c_client *client = sd->priv;
3155     struct sensor *sensor = to_sensor(client);
3156     struct soc_camera_device *icd = client->dev.platform_data;
3157     const struct v4l2_queryctrl *qctrl;
3158
3159
3160     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3161
3162     if (!qctrl)
3163     {
3164         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3165         return -EINVAL;
3166     }
3167
3168     switch (ctrl->id)
3169     {
3170 #if CONFIG_SENSOR_Brightness
3171         case V4L2_CID_BRIGHTNESS:
3172             {
3173                 if (ctrl->value != sensor->info_priv.brightness)
3174                 {
3175                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3176                     {
3177                         return -EINVAL;
3178                     }
3179                     sensor->info_priv.brightness = ctrl->value;
3180                 }
3181                 break;
3182             }
3183 #endif
3184 #if CONFIG_SENSOR_Exposure
3185         case V4L2_CID_EXPOSURE:
3186             {
3187                 if (ctrl->value != sensor->info_priv.exposure)
3188                 {
3189                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3190                     {
3191                         return -EINVAL;
3192                     }
3193                     sensor->info_priv.exposure = ctrl->value;
3194                 }
3195                 break;
3196             }
3197 #endif
3198 #if CONFIG_SENSOR_Saturation
3199         case V4L2_CID_SATURATION:
3200             {
3201                 if (ctrl->value != sensor->info_priv.saturation)
3202                 {
3203                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3204                     {
3205                         return -EINVAL;
3206                     }
3207                     sensor->info_priv.saturation = ctrl->value;
3208                 }
3209                 break;
3210             }
3211 #endif
3212 #if CONFIG_SENSOR_Contrast
3213         case V4L2_CID_CONTRAST:
3214             {
3215                 if (ctrl->value != sensor->info_priv.contrast)
3216                 {
3217                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3218                     {
3219                         return -EINVAL;
3220                     }
3221                     sensor->info_priv.contrast = ctrl->value;
3222                 }
3223                 break;
3224             }
3225 #endif
3226 #if CONFIG_SENSOR_WhiteBalance
3227         case V4L2_CID_DO_WHITE_BALANCE:
3228             {
3229                 if (ctrl->value != sensor->info_priv.whiteBalance)
3230                 {
3231                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3232                     {
3233                         return -EINVAL;
3234                     }
3235                     sensor->info_priv.whiteBalance = ctrl->value;
3236                 }
3237                 break;
3238             }
3239 #endif
3240 #if CONFIG_SENSOR_Mirror
3241         case V4L2_CID_HFLIP:
3242             {
3243                 if (ctrl->value != sensor->info_priv.mirror)
3244                 {
3245                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3246                         return -EINVAL;
3247                     sensor->info_priv.mirror = ctrl->value;
3248                 }
3249                 break;
3250             }
3251 #endif
3252 #if CONFIG_SENSOR_Flip
3253         case V4L2_CID_VFLIP:
3254             {
3255                 if (ctrl->value != sensor->info_priv.flip)
3256                 {
3257                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3258                         return -EINVAL;
3259                     sensor->info_priv.flip = ctrl->value;
3260                 }
3261                 break;
3262             }
3263 #endif
3264         default:
3265             break;
3266     }
3267
3268     return 0;
3269 }
3270 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3271 {
3272     const struct v4l2_queryctrl *qctrl;
3273     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3274     struct sensor *sensor = to_sensor(client);
3275
3276     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3277
3278     if (!qctrl)
3279     {
3280         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3281         return -EINVAL;
3282     }
3283
3284     switch (ext_ctrl->id)
3285     {
3286         case V4L2_CID_SCENE:
3287             {
3288                 ext_ctrl->value = sensor->info_priv.scene;
3289                 break;
3290             }
3291         case V4L2_CID_EFFECT:
3292             {
3293                 ext_ctrl->value = sensor->info_priv.effect;
3294                 break;
3295             }
3296         case V4L2_CID_ZOOM_ABSOLUTE:
3297             {
3298                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3299                 break;
3300             }
3301         case V4L2_CID_ZOOM_RELATIVE:
3302             {
3303                 return -EINVAL;
3304             }
3305         case V4L2_CID_FOCUS_ABSOLUTE:
3306             {
3307                 return -EINVAL;
3308             }
3309         case V4L2_CID_FOCUS_RELATIVE:
3310             {
3311                 return -EINVAL;
3312             }
3313         case V4L2_CID_FLASH:
3314             {
3315                 ext_ctrl->value = sensor->info_priv.flash;
3316                 break;
3317             }
3318         default :
3319             break;
3320     }
3321     return 0;
3322 }
3323 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3324 {
3325     const struct v4l2_queryctrl *qctrl;
3326     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3327     struct sensor *sensor = to_sensor(client);
3328     int val_offset;
3329
3330     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3331
3332     if (!qctrl)
3333     {
3334         SENSOR_TR("\n %s ioctrl id = 0x%x  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3335         return -EINVAL;
3336     }
3337
3338         val_offset = 0;
3339     switch (ext_ctrl->id)
3340     {
3341 #if CONFIG_SENSOR_Scene
3342         case V4L2_CID_SCENE:
3343             {
3344                 if (ext_ctrl->value != sensor->info_priv.scene)
3345                 {
3346                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3347                         return -EINVAL;
3348                     sensor->info_priv.scene = ext_ctrl->value;
3349                 }
3350                 break;
3351             }
3352 #endif
3353 #if CONFIG_SENSOR_Effect
3354         case V4L2_CID_EFFECT:
3355             {                
3356                 if (ext_ctrl->value != sensor->info_priv.effect)
3357                 {                    
3358                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3359                         return -EINVAL;
3360                     sensor->info_priv.effect= ext_ctrl->value;
3361                 }
3362                 break;
3363             }
3364 #endif
3365 #if CONFIG_SENSOR_DigitalZoom
3366         case V4L2_CID_ZOOM_ABSOLUTE:
3367             {
3368                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3369                     return -EINVAL;
3370
3371                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3372                 {
3373                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3374
3375                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3376                         return -EINVAL;
3377                     sensor->info_priv.digitalzoom += val_offset;
3378
3379                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3380                 }
3381
3382                 break;
3383             }
3384         case V4L2_CID_ZOOM_RELATIVE:
3385             {
3386                 if (ext_ctrl->value)
3387                 {
3388                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3389                         return -EINVAL;
3390                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3391
3392                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3393                 }
3394                 break;
3395             }
3396 #endif
3397 #if CONFIG_SENSOR_Focus
3398         case V4L2_CID_FOCUS_ABSOLUTE:
3399             {
3400                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3401                     return -EINVAL;
3402
3403                 break;
3404             }
3405         case V4L2_CID_FOCUS_RELATIVE:
3406             {
3407                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3408                     return -EINVAL;
3409
3410                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3411                 break;
3412             }
3413                 case V4L2_CID_FOCUS_AUTO:
3414                         {
3415                                 if (ext_ctrl->value == 1) {
3416                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3417                                                 return -EINVAL;
3418                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3419                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3420                                         if (ext_ctrl->value == 0)
3421                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3422                                 }
3423                                 break;
3424                         }
3425                 case V4L2_CID_FOCUS_CONTINUOUS:
3426                         {
3427                                 /*if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3428                                         if (ext_ctrl->value == 1) {
3429                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3430                                                         return -EINVAL;
3431                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3432                                         }
3433                                 } else {
3434                                         if (ext_ctrl->value == 0)
3435                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3436                                 }*/
3437                                 break;
3438                         }
3439 #endif
3440 #if CONFIG_SENSOR_Flash
3441         case V4L2_CID_FLASH:
3442             {
3443                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3444                     return -EINVAL;
3445                 sensor->info_priv.flash = ext_ctrl->value;
3446
3447                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3448                 break;
3449             }
3450 #endif
3451         default:
3452             break;
3453     }
3454
3455     return 0;
3456 }
3457
3458 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3459 {
3460     struct i2c_client *client = sd->priv;
3461     struct soc_camera_device *icd = client->dev.platform_data;
3462     int i, error_cnt=0, error_idx=-1;
3463
3464
3465     for (i=0; i<ext_ctrl->count; i++) {
3466         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3467             error_cnt++;
3468             error_idx = i;
3469         }
3470     }
3471
3472     if (error_cnt > 1)
3473         error_idx = ext_ctrl->count;
3474
3475     if (error_idx != -1) {
3476         ext_ctrl->error_idx = error_idx;
3477         return -EINVAL;
3478     } else {
3479         return 0;
3480     }
3481 }
3482
3483 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3484 {
3485     struct i2c_client *client = sd->priv;
3486     struct soc_camera_device *icd = client->dev.platform_data;
3487     int i, error_cnt=0, error_idx=-1;
3488
3489     for (i=0; i<ext_ctrl->count; i++) {
3490         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3491             error_cnt++;
3492             error_idx = i;
3493         }
3494     }
3495
3496     if (error_cnt > 1)
3497         error_idx = ext_ctrl->count;
3498
3499     if (error_idx != -1) {
3500         ext_ctrl->error_idx = error_idx;
3501         return -EINVAL;
3502     } else {
3503         return 0;
3504     }
3505 }
3506
3507 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3508 {
3509         struct i2c_client *client = sd->priv;
3510     struct sensor *sensor = to_sensor(client);
3511         #if CONFIG_SENSOR_Focus
3512         struct soc_camera_device *icd = client->dev.platform_data;
3513         struct v4l2_mbus_framefmt mf;
3514         #endif
3515     
3516         if (enable == 1) {
3517                 sensor->info_priv.enable = 1;
3518                 #if CONFIG_SENSOR_Focus
3519                 mf.width        = icd->user_width;
3520         mf.height       = icd->user_height;
3521         mf.code = sensor->info_priv.fmt.code;
3522         mf.colorspace   = sensor->info_priv.fmt.colorspace;
3523         mf.field        = V4L2_FIELD_NONE;
3524                 /* If auto focus firmware haven't download success, must download firmware again when in video or preview stream on */
3525                 if (sensor_fmt_capturechk(sd, &mf) == false) {
3526                         if ((sensor->info_priv.affm_reinit == 1) || ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)==0)) {
3527                                 if (sensor->sensor_wq != NULL) {
3528                                         mutex_lock(&sensor->wq_lock);
3529                                         if (sensor->sensor_wk.state == sensor_working) {
3530                                                 SENSOR_DG("%s sensor af firmware thread is runing, Ingore current work",SENSOR_NAME_STRING());
3531                                                 mutex_unlock(&sensor->wq_lock);
3532                                                 goto sensor_s_stream_end;
3533                                         }
3534                                         sensor->sensor_wk.state = sensor_working;
3535                                         mutex_unlock(&sensor->wq_lock);
3536                                         sensor->sensor_wk.client = client;
3537                                         INIT_WORK(&(sensor->sensor_wk.dwork.work), sensor_af_workqueue);
3538                                         queue_delayed_work(sensor->sensor_wq,&(sensor->sensor_wk.dwork), 0);
3539                                 }
3540                                 sensor->info_priv.affm_reinit = 0;
3541                         }
3542                 }
3543                 #endif
3544         } else if (enable == 0) {
3545                 sensor->info_priv.enable = 0;
3546                 #if CONFIG_SENSOR_Focus
3547                 flush_work(&(sensor->sensor_wk.dwork.work));
3548                 mutex_lock(&sensor->wq_lock);
3549                 sensor->sensor_wk.state = sensor_work_ready;
3550                 mutex_unlock(&sensor->wq_lock);
3551                 #endif
3552         }
3553
3554 sensor_s_stream_end:
3555         return 0;
3556 }
3557
3558 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3559  * this wasn't our capture interface, so, we wait for the right one */
3560 static int sensor_video_probe(struct soc_camera_device *icd,
3561                                struct i2c_client *client)
3562 {
3563     char value;
3564     int ret,pid = 0;
3565     struct sensor *sensor = to_sensor(client);
3566
3567     /* We must have a parent by now. And it cannot be a wrong one.
3568      * So this entire test is completely redundant. */
3569     if (!icd->dev.parent ||
3570             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3571                 return -ENODEV;
3572
3573         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3574                 ret = -ENODEV;
3575                 goto sensor_video_probe_err;
3576         }
3577     /* soft reset */
3578     ret = sensor_write(client, 0x3008, 0x80);
3579     if (ret != 0) {
3580         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3581         ret = -ENODEV;
3582                 goto sensor_video_probe_err;
3583     }
3584     mdelay(5);          //delay 5 microseconds
3585
3586     /* check if it is an sensor sensor */
3587     ret = sensor_read(client, 0x300a, &value);
3588     if (ret != 0) {
3589         SENSOR_TR("read chip id high byte failed\n");
3590         ret = -ENODEV;
3591         goto sensor_video_probe_err;
3592     }
3593
3594     pid |= (value << 8);
3595
3596     ret = sensor_read(client, 0x300b, &value);
3597     if (ret != 0) {
3598         SENSOR_TR("read chip id low byte failed\n");
3599         ret = -ENODEV;
3600         goto sensor_video_probe_err;
3601     }
3602
3603     pid |= (value & 0xff);
3604     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3605     if (pid == SENSOR_ID) {
3606         sensor->model = SENSOR_V4L2_IDENT;
3607     } else {
3608         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3609         ret = -ENODEV;
3610         goto sensor_video_probe_err;
3611     }
3612
3613     return 0;
3614
3615 sensor_video_probe_err:
3616     return ret;
3617 }
3618 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3619 {
3620         struct i2c_client *client = sd->priv;
3621     struct soc_camera_device *icd = client->dev.platform_data;
3622     struct sensor *sensor = to_sensor(client);
3623     int ret = 0;
3624
3625         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3626         switch (cmd)
3627         {
3628                 case RK29_CAM_SUBDEV_DEACTIVATE:
3629                 {
3630                         sensor_deactivate(client);
3631                         break;
3632                 }
3633                 case RK29_CAM_SUBDEV_IOREQUEST:
3634                 {
3635                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3636             if (sensor->sensor_io_request != NULL) { 
3637                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
3638                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
3639                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
3640                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
3641                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
3642                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
3643                 }
3644             } else {
3645                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3646                 ret = -EINVAL;
3647                 goto sensor_ioctl_end;
3648             }
3649             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3650                for this project */
3651             #if CONFIG_SENSOR_Flash     
3652             int i;
3653                 if (sensor->sensor_gpio_res) {
3654                 printk("flash io:%d\n",sensor->sensor_gpio_res->gpio_flash);
3655                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3656                     for (i = 0; i < icd->ops->num_controls; i++) {
3657                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3658                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
3659                                 }
3660                     }
3661                     sensor->info_priv.flash = 0xff;
3662                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3663                 }
3664                 }
3665             #endif
3666                         break;
3667                 }
3668                 default:
3669                 {
3670                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3671                         break;
3672                 }
3673         }
3674
3675 sensor_ioctl_end:
3676         return ret;
3677
3678 }
3679 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3680                             enum v4l2_mbus_pixelcode *code)
3681 {
3682         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3683                 return -EINVAL;
3684
3685         *code = sensor_colour_fmts[index].code;
3686         return 0;
3687 }
3688 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3689         .init           = sensor_init,
3690         .g_ctrl         = sensor_g_control,
3691         .s_ctrl         = sensor_s_control,
3692         .g_ext_ctrls          = sensor_g_ext_controls,
3693         .s_ext_ctrls          = sensor_s_ext_controls,
3694         .g_chip_ident   = sensor_g_chip_ident,
3695         .ioctl = sensor_ioctl,
3696 };
3697
3698 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3699         .s_mbus_fmt     = sensor_s_fmt,
3700         .g_mbus_fmt     = sensor_g_fmt,
3701         .try_mbus_fmt   = sensor_try_fmt,
3702         .enum_mbus_fmt  = sensor_enum_fmt,
3703         .s_stream   = sensor_s_stream,
3704 };
3705
3706 static struct v4l2_subdev_ops sensor_subdev_ops = {
3707         .core   = &sensor_subdev_core_ops,
3708         .video = &sensor_subdev_video_ops,
3709 };
3710
3711 static int sensor_probe(struct i2c_client *client,
3712                          const struct i2c_device_id *did)
3713 {
3714     struct sensor *sensor;
3715     struct soc_camera_device *icd = client->dev.platform_data;
3716     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3717     struct soc_camera_link *icl;
3718     int ret;
3719
3720     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3721     if (!icd) {
3722         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3723         return -EINVAL;
3724     }
3725
3726     icl = to_soc_camera_link(icd);
3727     if (!icl) {
3728         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3729         return -EINVAL;
3730     }
3731
3732     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3733         dev_warn(&adapter->dev,
3734                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3735         return -EIO;
3736     }
3737
3738     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3739     if (!sensor)
3740         return -ENOMEM;
3741
3742     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3743
3744     /* Second stage probe - when a capture adapter is there */
3745     icd->ops            = &sensor_ops;
3746     sensor->info_priv.fmt = sensor_colour_fmts[0];
3747         #if CONFIG_SENSOR_I2C_NOSCHED
3748         atomic_set(&sensor->tasklock_cnt,0);
3749         #endif
3750
3751     ret = sensor_video_probe(icd, client);
3752     if (ret < 0) {
3753         icd->ops = NULL;
3754         i2c_set_clientdata(client, NULL);
3755         kfree(sensor);
3756                 sensor = NULL;
3757     } else {
3758                 #if CONFIG_SENSOR_Focus
3759                 sensor->sensor_wq = create_workqueue(SENSOR_NAME_STRING( wq));
3760                 if (sensor->sensor_wq == NULL)
3761                         SENSOR_TR("%s workqueue create fail!", SENSOR_NAME_STRING( wq));
3762                 mutex_init(&sensor->wq_lock);
3763                 sensor->sensor_wk.state = sensor_work_ready;
3764                 #endif
3765     }
3766
3767     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3768     return ret;
3769 }
3770
3771 static int sensor_remove(struct i2c_client *client)
3772 {
3773     struct sensor *sensor = to_sensor(client);
3774     struct soc_camera_device *icd = client->dev.platform_data;
3775
3776         #if CONFIG_SENSOR_Focus
3777         if (sensor->sensor_wq) {
3778                 destroy_workqueue(sensor->sensor_wq);
3779                 sensor->sensor_wq = NULL;
3780         }
3781         #endif
3782
3783     icd->ops = NULL;
3784     i2c_set_clientdata(client, NULL);
3785     client->driver = NULL;
3786     kfree(sensor);
3787         sensor = NULL;
3788     return 0;
3789 }
3790
3791 static const struct i2c_device_id sensor_id[] = {
3792         {SENSOR_NAME_STRING(), 0 },
3793         { }
3794 };
3795 MODULE_DEVICE_TABLE(i2c, sensor_id);
3796
3797 static struct i2c_driver sensor_i2c_driver = {
3798         .driver = {
3799                 .name = SENSOR_NAME_STRING(),
3800         },
3801         .probe          = sensor_probe,
3802         .remove         = sensor_remove,
3803         .id_table       = sensor_id,
3804 };
3805
3806 static int __init sensor_mod_init(void)
3807 {
3808     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3809     return i2c_add_driver(&sensor_i2c_driver);
3810 }
3811
3812 static void __exit sensor_mod_exit(void)
3813 {
3814     i2c_del_driver(&sensor_i2c_driver);
3815 }
3816
3817 device_initcall_sync(sensor_mod_init);
3818 module_exit(sensor_mod_exit);
3819
3820 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3821 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3822 MODULE_LICENSE("GPL");
3823
3824