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