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