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