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