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