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