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