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