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