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