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