camera rk30:update to v0.x.10,wake up vb when cif have't receive data etc.
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / mt9d112.c
1 /*
2  * Driver for mt9d112 CMOS Image Sensor from Aptina(micron)
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 "mt9d112.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_MT9D112
47 #define SENSOR_V4L2_IDENT V4L2_IDENT_MT9D112
48 #define SENSOR_ID 0x1580
49 #define SENSOR_ID_REG 0x3000
50 #define SENSOR_RESET_REG SEQUENCE_END
51 #define SENSOR_RESET_VAL 0x00
52 #define SENSOR_MIN_WIDTH    320
53 #define SENSOR_MIN_HEIGHT   240
54 #define SENSOR_MAX_WIDTH    1600
55 #define SENSOR_MAX_HEIGHT   1200
56 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
57 #define SENSOR_INIT_HEIGHT  480
58 #define SENSOR_INIT_WINSEQADR sensor_vga
59 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_UYVY8_2X8
60
61 #define CONFIG_SENSOR_WhiteBalance      1
62 #define CONFIG_SENSOR_Brightness        0
63 #define CONFIG_SENSOR_Contrast      0
64 #define CONFIG_SENSOR_Saturation    0
65 #define CONFIG_SENSOR_Effect        1
66 #define CONFIG_SENSOR_Scene         1
67 #define CONFIG_SENSOR_DigitalZoom   0
68 #define CONFIG_SENSOR_Exposure      0
69 #define CONFIG_SENSOR_Flash         1
70 #define CONFIG_SENSOR_Mirror        0
71 #define CONFIG_SENSOR_Flip          0
72 #define CONFIG_SENSOR_Focus         0
73
74
75 #define CONFIG_SENSOR_I2C_SPEED     250000       /* Hz */
76 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
77 #define CONFIG_SENSOR_I2C_NOSCHED   0
78 #define CONFIG_SENSOR_I2C_RDWRCHK   0
79
80
81 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
82                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |\
83                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
84
85 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
86 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
87 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
88 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
89 #define COLOR_TEMPERATURE_OFFICE_DN     3500
90 #define COLOR_TEMPERATURE_OFFICE_UP     5000
91 #define COLOR_TEMPERATURE_HOME_DN       2500
92 #define COLOR_TEMPERATURE_HOME_UP       3500
93
94 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
95 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
96
97 #define SENSOR_AF_IS_ERR    (0x00<<0)
98 #define SENSOR_AF_IS_OK         (0x01<<0)
99 #define SENSOR_INIT_IS_ERR   (0x00<<28)
100 #define SENSOR_INIT_IS_OK    (0x01<<28)
101
102 #if CONFIG_SENSOR_Focus
103 #define SENSOR_AF_MODE_INFINITY    0
104 #define SENSOR_AF_MODE_MACRO       1
105 #define SENSOR_AF_MODE_FIXED       2
106 #define SENSOR_AF_MODE_AUTO        3
107 #define SENSOR_AF_MODE_CONTINUOUS  4
108 #define SENSOR_AF_MODE_CLOSE       5
109 #endif
110 #define SENSOR_CCM_ONLY
111
112 //flash off in fixed time to prevent from too hot , zyc
113 struct  flash_timer{
114     struct soc_camera_device *icd;
115         struct hrtimer timer;
116 };
117 static enum hrtimer_restart flash_off_func(struct hrtimer *timer);
118
119 static struct  flash_timer flash_off_timer;
120 //for user defined if user want to customize the series , zyc
121 #if CONFIG_MT9D112_USER_DEFINED_SERIES
122 #include "mt9d112_user_series.c"
123 #else
124
125
126 /* init 640X480 VGA */
127 static struct reginfo sensor_init_data[] =
128 {
129         {0x301A, 0x0ACC},       // RESET_REGISTER
130         {0x3202, 0x0008},       // STANDBY_CONTROL
131         {SEQUENCE_WAIT_MS, 10},
132         {0x341E, 0x8F09},       // PLL_CLK_IN_CONTROL
133         {0x341C, 0x0218},       // PLL_DIVIDERS1
134         {SEQUENCE_WAIT_MS, 1},
135         {0x341E, 0x8F09},       // PLL_CLK_IN_CONTROL
136         {0x341E, 0x8F08},       // PLL_CLK_IN_CONTROL
137         {0x3044, 0x0540},       // DARK_CONTROL
138         {0x3216, 0x02CF},       // INTERNAL_CLOCK_CONTROL
139         {0x321C, 0x0402},       // OF_CONTROL_STATUS
140         {0x3212, 0x0001},       // FACTORY_BYPASS
141         {0x341E, 0x8F09},       // PLL_CLK_IN_CONTROL
142         {0x341C, 0x0120},       // PLL_DIVIDERS1
143         {0x341E, 0x8F09},       // PLL_CLK_IN_CONTROL
144         {0x341E, 0x8F08},       // PLL_CLK_IN_CONTROL
145         {0x3044, 0x0540},       // DARK_CONTROL
146         {0x3216, 0x02CF},       // INTERNAL_CLOCK_CONTROL
147         {0x321C, 0x0402},       // OF_CONTROL_STATUS
148         {0x3212, 0x0001},       // FACTORY_BYPASS//¹©Æ·¸ÉÈųÌÐòÀûÓÃ֮ǰµÄ07005
149 #ifdef SENSOR_CCM_ONLY
150         {0x338C, 0x2703},
151         {0x3390, 0x0320},
152         {0x338C, 0x2705},
153         {0x3390, 0x0258},
154         {0x338C, 0x2707},
155         {0x3390, 0x0640},
156         {0x338C, 0x2709},
157         {0x3390, 0x04B0},
158         {0x338C, 0x270D},
159         {0x3390, 0x0000},
160         {0x338C, 0x270F},
161         {0x3390, 0x0000},
162         {0x338C, 0x2711},
163         {0x3390, 0x4BD},
164         {0x338C, 0x2713},
165         {0x3390, 0x64D},
166         {0x3390, 0x411},
167 #else
168         {0x338C, 0x2703},       // MCU_ADDRESS
169         {0x3390, 0x0140},       // MCU_DATA_0
170         {0x338C, 0x2705},       // MCU_ADDRESS
171         {0x3390, 0x00F0},       // MCU_DATA_0
172         {0x338C, 0x2707},       // MCU_ADDRESS
173         {0x3390, 0x0640},       // MCU_DATA_0
174         {0x338C, 0x2709},       // MCU_ADDRESS
175         {0x3390, 0x04B0},       // MCU_DATA_0
176         {0x338C, 0x270D},       // MCU_ADDRESS
177         {0x3390, 0x0000},       // MCU_DATA_0
178         {0x338C, 0x270F},       // MCU_ADDRESS
179         {0x3390, 0x0000},       // MCU_DATA_0
180         {0x338C, 0x2711},       // MCU_ADDRESS
181         {0x3390, 0x04BD},       // MCU_DATA_0
182         {0x338C, 0x2713},       // MCU_ADDRESS
183         {0x3390, 0x064D},       // MCU_DATA_0
184         {0x338C, 0x2715},       // MCU_ADDRESS
185         {0x3390, 0x0022},       // MCU_DATA_0
186 #endif
187         {0x338C, 0x2717},       // MCU_ADDRESS
188         {0x3390, 0x2111},       // MCU_DATA_0
189         {0x338C, 0x2719},       // MCU_ADDRESS
190         {0x3390, 0x046C},       // MCU_DATA_0
191         {0x338C, 0x271B},       // MCU_ADDRESS
192         {0x3390, 0x024F},       // MCU_DATA_0
193         {0x338C, 0x271D},       // MCU_ADDRESS
194         {0x3390, 0x0102},       // MCU_DATA_0
195         {0x338C, 0x271F},       // MCU_ADDRESS
196         {0x3390, 0x0279},       // MCU_DATA_0
197         {0x338C, 0x2721},       // MCU_ADDRESS
198         {0x3390, 0x0155},       // MCU_DATA_0
199         {0x338C, 0x2723},       // MCU_ADDRESS
200         {0x3390, 0x0293},       // MCU_DATA_0
201         {0x338C, 0x2725},       // MCU_ADDRESS
202         {0x3390, 0x0824},       // MCU_DATA_0
203         {0x338C, 0x2727},       // MCU_ADDRESS
204         {0x3390, 0x2020},       // MCU_DATA_0
205         {0x338C, 0x2729},       // MCU_ADDRESS
206         {0x3390, 0x2020},       // MCU_DATA_0
207         {0x338C, 0x272B},       // MCU_ADDRESS
208         {0x3390, 0x1020},       // MCU_DATA_0
209         {0x338C, 0x272D},       // MCU_ADDRESS
210         {0x3390, 0x2007},       // MCU_DATA_0
211         {0x338C, 0x272F},       // MCU_ADDRESS
212         {0x3390, 0x0004},       // MCU_DATA_0
213         {0x338C, 0x2731},       // MCU_ADDRESS
214         {0x3390, 0x0004},       // MCU_DATA_0
215         {0x338C, 0x2733},       // MCU_ADDRESS
216         {0x3390, 0x04BB},       // MCU_DATA_0
217         {0x338C, 0x2735},       // MCU_ADDRESS
218         {0x3390, 0x064B},       // MCU_DATA_0
219         {0x338C, 0x2737},       // MCU_ADDRESS
220         {0x3390, 0x0000},       // MCU_DATA_0
221         {0x338C, 0x2739},       // MCU_ADDRESS
222         {0x3390, 0x2111},       // MCU_DATA_0
223         {0x338C, 0x273B},       // MCU_ADDRESS
224         {0x3390, 0x0024},       // MCU_DATA_0
225         {0x338C, 0x273D},       // MCU_ADDRESS
226         {0x3390, 0x0120},       // MCU_DATA_0
227         {0x338C, 0x273F},       // MCU_ADDRESS
228         {0x3390, 0x00A4},       // MCU_DATA_0
229         {0x338C, 0x2741},       // MCU_ADDRESS
230         {0x3390, 0x0169},       // MCU_DATA_0
231         {0x338C, 0x2743},       // MCU_ADDRESS
232         {0x3390, 0x00A4},       // MCU_DATA_0
233         {0x338C, 0x2745},       // MCU_ADDRESS
234         {0x3390, 0x04ED},       // MCU_DATA_0
235         {0x338C, 0x2747},       // MCU_ADDRESS
236         {0x3390, 0x0824},       // MCU_DATA_0
237         {0x338C, 0x2751},       // MCU_ADDRESS
238         {0x3390, 0x0000},       // MCU_DATA_0
239         {0x338C, 0x2753},       // MCU_ADDRESS
240         {0x3390, 0x0320},       // MCU_DATA_0
241         {0x338C, 0x2755},       // MCU_ADDRESS
242         {0x3390, 0x0000},       // MCU_DATA_0
243         {0x338C, 0x2757},       // MCU_ADDRESS
244         {0x3390, 0x0258},       // MCU_DATA_0
245         {0x338C, 0x275F},       // MCU_ADDRESS
246         {0x3390, 0x0000},       // MCU_DATA_0
247         {0x338C, 0x2761},       // MCU_ADDRESS
248         {0x3390, 0x0640},       // MCU_DATA_0
249         {0x338C, 0x2763},       // MCU_ADDRESS
250         {0x3390, 0x0000},       // MCU_DATA_0
251         {0x338C, 0x2765},       // MCU_ADDRESS
252         {0x3390, 0x04B0},       // MCU_DATA_0
253         {0x338C, 0x222E},       // MCU_ADDRESS
254         {0x3390, 0x0060},       // MCU_DATA_0
255         {0x338C, 0xA408},       // MCU_ADDRESS
256         {0x3390, 0x0017},       // MCU_DATA_0
257         {0x338C, 0xA409},       // MCU_ADDRESS
258         {0x3390, 0x001A},       // MCU_DATA_0
259         {0x338C, 0xA40A},       // MCU_ADDRESS
260         {0x3390, 0x001B},       // MCU_DATA_0
261         {0x338C, 0xA40B},       // MCU_ADDRESS
262         {0x3390, 0x001E},       // MCU_DATA_0
263         {0x338C, 0x2411},       // MCU_ADDRESS
264         {0x3390, 0x0060},       // MCU_DATA_0
265         {0x338C, 0x2413},       // MCU_ADDRESS
266         {0x3390, 0x0073},       // MCU_DATA_0
267         {0x338C, 0x2415},       // MCU_ADDRESS
268         {0x3390, 0x0060},       // MCU_DATA_0
269         {0x338C, 0x2417},       // MCU_ADDRESS
270         {0x3390, 0x0073},       // MCU_DATA_0
271         {0x338C, 0xA40D},       // MCU_ADDRESS
272         {0x3390, 0x0002},       // MCU_DATA_0
273         {0x338C, 0xA410},       // MCU_ADDRESS
274         {0x3390, 0x0001},       // MCU_DATA_0
275         {0x338C, 0xA103},       // MCU_ADDRESS
276         {0x3390, 0x0006},       // MCU_DATA_0
277         {0x338C, 0xA103},       // MCU_ADDRESS
278         {0x3390, 0x0005},       // MCU_DATA_0
279         {0x338C, 0x2703},       // MCU_ADDRESS
280         {0x3390, 0x0140},       // MCU_DATA_0
281         {0x338C, 0x2705},       // MCU_ADDRESS
282         {0x3390, 0x00F0},       // MCU_DATA_0
283         {0x338C, 0x2707},       // MCU_ADDRESS
284         {0x3390, 0x0640},       // MCU_DATA_0
285         {0x338C, 0x2709},       // MCU_ADDRESS
286         {0x3390, 0x04B0},       // MCU_DATA_0
287         {0x338C, 0x270D},       // MCU_ADDRESS
288         {0x3390, 0x0000},       // MCU_DATA_0
289         {0x338C, 0x270F},       // MCU_ADDRESS
290         {0x3390, 0x0000},       // MCU_DATA_0
291         {0x338C, 0x2711},       // MCU_ADDRESS
292         {0x3390, 0x04BD},       // MCU_DATA_0
293         {0x338C, 0x2713},       // MCU_ADDRESS
294         {0x3390, 0x064D},       // MCU_DATA_0
295         {0x338C, 0x2715},       // MCU_ADDRESS
296         {0x3390, 0x0022},       // MCU_DATA_0
297         {0x338C, 0x2717},       // MCU_ADDRESS
298         {0x3390, 0x2111},       // MCU_DATA_0
299         {0x338C, 0x2719},       // MCU_ADDRESS
300         {0x3390, 0x046C},       // MCU_DATA_0
301         {0x338C, 0x271B},       // MCU_ADDRESS
302         {0x3390, 0x024F},       // MCU_DATA_0
303         {0x338C, 0x271D},       // MCU_ADDRESS
304         {0x3390, 0x0102},       // MCU_DATA_0
305         {0x338C, 0x271F},       // MCU_ADDRESS
306         {0x3390, 0x0279},       // MCU_DATA_0
307         {0x338C, 0x2721},       // MCU_ADDRESS
308         {0x3390, 0x0155},       // MCU_DATA_0
309         {0x338C, 0x2723},       // MCU_ADDRESS
310         {0x3390, 0x0293},       // MCU_DATA_0
311         {0x338C, 0x2725},       // MCU_ADDRESS
312         {0x3390, 0x0824},       // MCU_DATA_0
313         {0x338C, 0x2727},       // MCU_ADDRESS
314         {0x3390, 0x2020},       // MCU_DATA_0
315         {0x338C, 0x2729},       // MCU_ADDRESS
316         {0x3390, 0x2020},       // MCU_DATA_0
317         {0x338C, 0x272B},       // MCU_ADDRESS
318         {0x3390, 0x1020},       // MCU_DATA_0
319         {0x338C, 0x272D},       // MCU_ADDRESS
320         {0x3390, 0x2007},       // MCU_DATA_0
321         {0x338C, 0x272F},       // MCU_ADDRESS
322         {0x3390, 0x0004},       // MCU_DATA_0
323         {0x338C, 0x2731},       // MCU_ADDRESS
324         {0x3390, 0x0004},       // MCU_DATA_0
325         {0x338C, 0x2733},       // MCU_ADDRESS
326         {0x3390, 0x04BB},       // MCU_DATA_0
327         {0x338C, 0x2735},       // MCU_ADDRESS
328         {0x3390, 0x064B},       // MCU_DATA_0
329         {0x338C, 0x2737},       // MCU_ADDRESS
330         {0x3390, 0x0000},       // MCU_DATA_0
331         {0x338C, 0x2739},       // MCU_ADDRESS
332         {0x3390, 0x2111},       // MCU_DATA_0
333         {0x338C, 0x273B},       // MCU_ADDRESS
334         {0x3390, 0x0024},       // MCU_DATA_0
335         {0x338C, 0x273D},       // MCU_ADDRESS
336         {0x3390, 0x0120},       // MCU_DATA_0
337         {0x338C, 0x273F},       // MCU_ADDRESS
338         {0x3390, 0x00A4},       // MCU_DATA_0
339         {0x338C, 0x2741},       // MCU_ADDRESS
340         {0x3390, 0x0169},       // MCU_DATA_0
341         {0x338C, 0x2743},       // MCU_ADDRESS
342         {0x3390, 0x00A4},       // MCU_DATA_0
343         {0x338C, 0x2745},       // MCU_ADDRESS
344         {0x3390, 0x04ED},       // MCU_DATA_0
345         {0x338C, 0x2747},       // MCU_ADDRESS
346         {0x3390, 0x0824},       // MCU_DATA_0
347         {0x338C, 0x2751},       // MCU_ADDRESS
348         {0x3390, 0x0000},       // MCU_DATA_0
349         {0x338C, 0x2753},       // MCU_ADDRESS
350         {0x3390, 0x0320},       // MCU_DATA_0
351         {0x338C, 0x2755},       // MCU_ADDRESS
352         {0x3390, 0x0000},       // MCU_DATA_0
353         {0x338C, 0x2757},       // MCU_ADDRESS
354         {0x3390, 0x0258},       // MCU_DATA_0
355         {0x338C, 0x275F},       // MCU_ADDRESS
356         {0x3390, 0x0000},       // MCU_DATA_0
357         {0x338C, 0x2761},       // MCU_ADDRESS
358         {0x3390, 0x0640},       // MCU_DATA_0
359         {0x338C, 0x2763},       // MCU_ADDRESS
360         {0x3390, 0x0000},       // MCU_DATA_0
361         {0x338C, 0x2765},       // MCU_ADDRESS
362         {0x3390, 0x04B0},       // MCU_DATA_0
363         {0x338C, 0x222E},       // MCU_ADDRESS
364         {0x3390, 0x0060},       // MCU_DATA_0
365         {0x338C, 0xA408},       // MCU_ADDRESS
366         {0x3390, 0x0017},       // MCU_DATA_0
367         {0x338C, 0xA409},       // MCU_ADDRESS
368         {0x3390, 0x001A},       // MCU_DATA_0
369         {0x338C, 0xA40A},       // MCU_ADDRESS
370         {0x3390, 0x001B},       // MCU_DATA_0
371         {0x338C, 0xA40B},       // MCU_ADDRESS
372         {0x3390, 0x001E},       // MCU_DATA_0
373         {0x338C, 0x2411},       // MCU_ADDRESS
374         {0x3390, 0x0060},       // MCU_DATA_0
375         {0x338C, 0x2413},       // MCU_ADDRESS
376         {0x3390, 0x0073},       // MCU_DATA_0
377         {0x338C, 0x2415},       // MCU_ADDRESS
378         {0x3390, 0x0060},       // MCU_DATA_0
379         {0x338C, 0x2417},       // MCU_ADDRESS
380         {0x3390, 0x0073},       // MCU_DATA_0
381         {0x338C, 0xA40D},       // MCU_ADDRESS
382         {0x3390, 0x0002},       // MCU_DATA_0
383         {0x338C, 0xA410},       // MCU_ADDRESS
384         {0x3390, 0x0001},       // MCU_DATA_0
385         {0x338C, 0xA103},       // MCU_ADDRESS
386         {0x3390, 0x0006},       // MCU_DATA_0
387         {SEQUENCE_WAIT_MS, 100},
388         {0x338C, 0xA103},       // MCU_ADDRESS
389         {0x3390, 0x0005},       // MCU_DATA_0
390         {SEQUENCE_WAIT_MS, 100},
391
392
393         //[Lens Correction 01/04/07 20:07:28]
394         {0x34CE, 0x01A0}, //LENS_CORRECTION_CONTROL
395         {0x34D0, 0x6532}, //ZONE_BOUNDS_X1_X2
396         {0x34D2, 0x3297}, //ZONE_BOUNDS_X0_X3
397         {0x34D4, 0x9664}, //ZONE_BOUNDS_X4_X5
398         {0x34D6, 0x4B25}, //ZONE_BOUNDS_Y1_Y2
399         {0x34D8, 0x2670}, //ZONE_BOUNDS_Y0_Y3
400         {0x34DA, 0x724C}, //ZONE_BOUNDS_Y4_Y5
401         {0x34DC, 0xFF01}, //CENTER_OFFSET
402         {0x34DE, 0x011F}, //FX_RED
403         {0x34E6, 0x00B3}, //FY_RED
404         {0x34EE, 0x0D8F}, //DF_DX_RED
405         {0x34F6, 0x0D6E}, //DF_DY_RED
406         {0x3500, 0xF51C}, //SECOND_DERIV_ZONE_0_RED
407         {0x3508, 0xFEF8}, //SECOND_DERIV_ZONE_1_RED
408         {0x3510, 0x212D}, //SECOND_DERIV_ZONE_2_RED
409         {0x3518, 0x212B}, //SECOND_DERIV_ZONE_3_RED
410         {0x3520, 0x1F2A}, //SECOND_DERIV_ZONE_4_RED
411         {0x3528, 0x2934}, //SECOND_DERIV_ZONE_5_RED
412         {0x3530, 0x17DB}, //SECOND_DERIV_ZONE_6_RED
413         {0x3538, 0xE7D6}, //SECOND_DERIV_ZONE_7_RED
414         {0x354C, 0x07C6}, //K_FACTOR_IN_K_FX_FY_R_TL
415         {0x3544, 0x07FF}, //K_FACTOR_IN_K_FX_FY_R_TR
416         {0x355C, 0x051C}, //K_FACTOR_IN_K_FX_FY_R_BL
417         {0x3554, 0x07FF}, //K_FACTOR_IN_K_FX_FY_R_BR
418         {0x34E0, 0x0137}, //FX_GREEN
419         {0x34E8, 0x00A8}, //FY_GREEN
420         {0x34F0, 0x0E3E}, //DF_DX_GREEN
421         {0x34F8, 0x0DCB}, //DF_DY_GREEN
422         {0x3502, 0xF20A}, //SECOND_DERIV_ZONE_0_GREEN
423         {0x350A, 0xFBE1}, //SECOND_DERIV_ZONE_1_GREEN
424         {0x3512, 0x1C26}, //SECOND_DERIV_ZONE_2_GREEN
425         {0x351A, 0x232B}, //SECOND_DERIV_ZONE_3_GREEN
426         {0x3522, 0x312E}, //SECOND_DERIV_ZONE_4_GREEN
427         {0x352A, 0x2121}, //SECOND_DERIV_ZONE_5_GREEN
428         {0x3532, 0xF106}, //SECOND_DERIV_ZONE_6_GREEN
429         {0x353A, 0x0C0D}, //SECOND_DERIV_ZONE_7_GREEN
430         {0x354E, 0x07FF}, //K_FACTOR_IN_K_FX_FY_G1_TL
431         {0x3546, 0x0638}, //K_FACTOR_IN_K_FX_FY_G1_TR
432         {0x355E, 0x07FF}, //K_FACTOR_IN_K_FX_FY_G1_BL
433         {0x3556, 0x0155}, //K_FACTOR_IN_K_FX_FY_G1_BR
434         {0x34E4, 0x0107}, //FX_BLUE
435         {0x34EC, 0x0079}, //FY_BLUE
436         {0x34F4, 0x0E19}, //DF_DX_BLUE
437         {0x34FC, 0x0D35}, //DF_DY_BLUE
438         {0x3506, 0x111C}, //SECOND_DERIV_ZONE_0_BLUE
439         {0x350E, 0x02E6}, //SECOND_DERIV_ZONE_1_BLUE
440         {0x3516, 0x2521}, //SECOND_DERIV_ZONE_2_BLUE
441         {0x351E, 0x2620}, //SECOND_DERIV_ZONE_3_BLUE
442         {0x3526, 0x1A25}, //SECOND_DERIV_ZONE_4_BLUE
443         {0x352E, 0x0B1D}, //SECOND_DERIV_ZONE_5_BLUE
444         {0x3536, 0xFD03}, //SECOND_DERIV_ZONE_6_BLUE
445         {0x353E, 0xB315}, //SECOND_DERIV_ZONE_7_BLUE
446         {0x3552, 0x06D0}, //K_FACTOR_IN_K_FX_FY_B_TL
447         {0x354A, 0x03FF}, //K_FACTOR_IN_K_FX_FY_B_TR
448         {0x3562, 0x07FF}, //K_FACTOR_IN_K_FX_FY_B_BL
449         {0x355A, 0x057B}, //K_FACTOR_IN_K_FX_FY_B_BR
450         {0x34E2, 0x011E}, //FX_GREEN2
451         {0x34EA, 0x008A}, //FY_GREEN2
452         {0x34F2, 0x0D6E}, //DF_DX_GREEN2
453         {0x34FA, 0x0D7F}, //DF_DY_GREEN2
454         {0x3504, 0xF822}, //SECOND_DERIV_ZONE_0_GREEN2
455         {0x350C, 0x0DFC}, //SECOND_DERIV_ZONE_1_GREEN2
456         {0x3514, 0x1F25}, //SECOND_DERIV_ZONE_2_GREEN2
457         {0x351C, 0x3032}, //SECOND_DERIV_ZONE_3_GREEN2
458         {0x3524, 0x2628}, //SECOND_DERIV_ZONE_4_GREEN2
459         {0x352C, 0x1523}, //SECOND_DERIV_ZONE_5_GREEN2
460         {0x3534, 0xFADF}, //SECOND_DERIV_ZONE_6_GREEN2
461         {0x353C, 0xDEF7}, //SECOND_DERIV_ZONE_7_GREEN2
462         {0x3550, 0x0109}, //K_FACTOR_IN_K_FX_FY_G2_TL
463         {0x3548, 0x0638}, //K_FACTOR_IN_K_FX_FY_G2_TR
464         {0x3560, 0x0638}, //K_FACTOR_IN_K_FX_FY_G2_BL
465         {0x3558, 0x07FF}, //K_FACTOR_IN_K_FX_FY_G2_BR
466         {0x3540, 0x0000}, //X2_FACTORS
467         {0x3542, 0x0000}, //GLOBAL_OFFSET_FXY_FUNCTION
468         {0x3210, 0x01FC},       // COLOR_PIPELINE_CONTROL
469         //CCM
470         //CCM
471         {0x338C, 0xA364},       // MCU_ADDRESS [AWB_KR_L]
472         {0x3390, 0x0080},       // MCU_DATA_0
473         {0x338C, 0xA364},       // MCU_ADDRESS [AWB_KR_L]
474         {0x3390, 0x0080},       // MCU_DATA_0
475         {0x338C, 0xA365},       // MCU_ADDRESS [AWB_KG_L]
476         {0x3390, 0x008C},       // MCU_DATA_0
477         {0x338C, 0xA365},       // MCU_ADDRESS [AWB_KG_L]
478         {0x3390, 0x008C},       // MCU_DATA_0
479         {0x338C, 0xA366},       // MCU_ADDRESS [AWB_KB_L]
480         {0x3390, 0x0082},       // MCU_DATA_0
481         {0x338C, 0xA366},       // MCU_ADDRESS [AWB_KB_L]
482         {0x3390, 0x0082},       // MCU_DATA_0
483         {0x338C, 0x2306},       // MCU_ADDRESS [AWB_CCM_L_0]
484         {0x3390, 0x0619},       // MCU_DATA_0
485         {0x338C, 0x2308},       // MCU_ADDRESS [AWB_CCM_L_1]
486         {0x3390, 0xFC80},       // MCU_DATA_0
487         {0x338C, 0x230A},       // MCU_ADDRESS [AWB_CCM_L_2]
488         {0x3390, 0xFEFB},       // MCU_DATA_0
489         {0x338C, 0x230C},       // MCU_ADDRESS [AWB_CCM_L_3]
490         {0x3390, 0xFEEE},       // MCU_DATA_0
491         {0x338C, 0x230E},       // MCU_ADDRESS [AWB_CCM_L_4]
492         {0x3390, 0x0571},       // MCU_DATA_0
493         {0x338C, 0x2310},       // MCU_ADDRESS [AWB_CCM_L_5]
494         {0x3390, 0xFE26},       // MCU_DATA_0
495         {0x338C, 0x2312},       // MCU_ADDRESS [AWB_CCM_L_6]
496         {0x3390, 0xFF0C},       // MCU_DATA_0
497         {0x338C, 0x2314},       // MCU_ADDRESS [AWB_CCM_L_7]
498         {0x3390, 0xFE48},       // MCU_DATA_0
499         {0x338C, 0x2316},       // MCU_ADDRESS [AWB_CCM_L_8]
500         {0x3390, 0x04A2},       // MCU_DATA_0
501         {0x338C, 0x2318},       // MCU_ADDRESS [AWB_CCM_L_9]
502         {0x3390, 0x0024},       // MCU_DATA_0
503         {0x338C, 0x231A},       // MCU_ADDRESS [AWB_CCM_L_10]
504         {0x3390, 0x003F},       // MCU_DATA_0
505         {0x338C, 0x231C},       // MCU_ADDRESS [AWB_CCM_RL_0]
506         {0x3390, 0xFDA7},       // MCU_DATA_0
507         {0x338C, 0x231E},       // MCU_ADDRESS [AWB_CCM_RL_1]
508         {0x3390, 0x0158},       // MCU_DATA_0
509         {0x338C, 0x2320},       // MCU_ADDRESS [AWB_CCM_RL_2]
510         {0x3390, 0x00EE},       // MCU_DATA_0
511         {0x338C, 0x2322},       // MCU_ADDRESS [AWB_CCM_RL_3]
512         {0x3390, 0x00D3},       // MCU_DATA_0
513         {0x338C, 0x2324},       // MCU_ADDRESS [AWB_CCM_RL_4]
514         {0x3390, 0xFC74},       // MCU_DATA_0
515         {0x338C, 0x2326},       // MCU_ADDRESS [AWB_CCM_RL_5]
516         {0x3390, 0x01A9},       // MCU_DATA_0
517         {0x338C, 0x2328},       // MCU_ADDRESS [AWB_CCM_RL_6]
518         {0x3390, 0x014B},       // MCU_DATA_0
519         {0x338C, 0x232A},       // MCU_ADDRESS [AWB_CCM_RL_7]
520         {0x3390, 0xFE9D},       // MCU_DATA_0
521         {0x338C, 0x232C},       // MCU_ADDRESS [AWB_CCM_RL_8]
522         {0x3390, 0xFF69},       // MCU_DATA_0
523         {0x338C, 0x232E},       // MCU_ADDRESS [AWB_CCM_RL_9]
524         {0x3390, 0x0018},       // MCU_DATA_0
525         {0x338C, 0x2330},       // MCU_ADDRESS [AWB_CCM_RL_10]
526         {0x3390, 0xFFEC},       // MCU_DATA_0
527         {0x338C, 0xA348},       // MCU_ADDRESS [AWB_GAIN_BUFFER_SPEED]
528         {0x3390, 0x0008},       // MCU_DATA_0
529         {0x338C, 0xA349},       // MCU_ADDRESS [AWB_JUMP_DIVISOR]
530         {0x3390, 0x0002},       // MCU_DATA_0
531         {0x338C, 0xA34A},       // MCU_ADDRESS [AWB_GAIN_MIN]
532         {0x3390, 0x0059},       // MCU_DATA_0
533         {0x338C, 0xA34B},       // MCU_ADDRESS [AWB_GAIN_MAX]
534         {0x3390, 0x00A6},       // MCU_DATA_0
535         {0x338C, 0xA34F},       // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
536         {0x3390, 0x0000},       // MCU_DATA_0
537         {0x338C, 0xA350},       // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
538         {0x3390, 0x007F},       // MCU_DATA_0
539         {0x338C, 0xA353},       // MCU_ADDRESS [AWB_MODE]
540         {0x3390, 0x0002},       // MCU_DATA_0
541         {0x338C, 0xA35B},       // MCU_ADDRESS [AWB_STEADY_BGAIN_OUT_MIN]
542         {0x3390, 0x0078},       // MCU_DATA_0
543         {0x338C, 0xA35C},       // MCU_ADDRESS [AWB_STEADY_BGAIN_OUT_MAX]
544         {0x3390, 0x0086},       // MCU_DATA_0
545         {0x338C, 0xA35D},       // MCU_ADDRESS [AWB_STEADY_BGAIN_IN_MIN]
546         {0x3390, 0x007E},       // MCU_DATA_0
547         {0x338C, 0xA35E},       // MCU_ADDRESS [AWB_STEADY_BGAIN_IN_MAX]
548         {0x3390, 0x0082},       // MCU_DATA_0
549         {0x338C, 0x235F},       // MCU_ADDRESS [AWB_CNT_PXL_TH]
550         {0x3390, 0x0040},       // MCU_DATA_0
551         {0x338C, 0xA361},       // MCU_ADDRESS [AWB_TG_MIN0]
552         {0x3390, 0x00C8},       // MCU_DATA_0
553         {0x338C, 0xA362},       // MCU_ADDRESS [AWB_TG_MAX0]
554         {0x3390, 0x00E1},       // MCU_DATA_0
555         {0x338C, 0xA302},       // MCU_ADDRESS [AWB_WINDOW_POS]
556         {0x3390, 0x0000},       // MCU_DATA_0
557         {0x338C, 0xA303},       // MCU_ADDRESS [AWB_WINDOW_SIZE]
558         {0x3390, 0x00EF},       // MCU_DATA_0
559         {0x338C, 0xA352},       // MCU_ADDRESS [AWB_SATURATION]
560         {0x3390, 0x001E},       // MCU_DATA_0
561         {0x338C, 0xA118},       // MCU_ADDRESS [SEQ_LLSAT1]
562         {0x3390, 0x001E},       // MCU_DATA_0
563         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
564         {0x3390, 0x0005},       // MCU_DATA_0
565          //Contrast
566         {0x338C, 0xA76D},       // MCU_ADDRESS [MODE_GAM_CONT_A]
567         {0x3390, 0x0003},       // MCU_DATA_0
568         {0x338C, 0xA76F},       // MCU_ADDRESS [MODE_GAM_TABLE_A_0]
569         {0x3390, 0x0000},       // MCU_DATA_0
570         {0x338C, 0xA770},       // MCU_ADDRESS [MODE_GAM_TABLE_A_1]
571         {0x3390, 0x000B},       // MCU_DATA_0
572         {0x338C, 0xA771},       // MCU_ADDRESS [MODE_GAM_TABLE_A_2]
573         {0x3390, 0x0023},       // MCU_DATA_0
574         {0x338C, 0xA772},       // MCU_ADDRESS [MODE_GAM_TABLE_A_3]
575         {0x3390, 0x0043},       // MCU_DATA_0
576         {0x338C, 0xA773},       // MCU_ADDRESS [MODE_GAM_TABLE_A_4]
577         {0x3390, 0x006E},       // MCU_DATA_0
578         {0x338C, 0xA774},       // MCU_ADDRESS [MODE_GAM_TABLE_A_5]
579         {0x3390, 0x0090},       // MCU_DATA_0
580         {0x338C, 0xA775},       // MCU_ADDRESS [MODE_GAM_TABLE_A_6]
581         {0x3390, 0x00A8},       // MCU_DATA_0
582         {0x338C, 0xA776},       // MCU_ADDRESS [MODE_GAM_TABLE_A_7]
583         {0x3390, 0x00B9},       // MCU_DATA_0
584         {0x338C, 0xA777},       // MCU_ADDRESS [MODE_GAM_TABLE_A_8]
585         {0x3390, 0x00C6},       // MCU_DATA_0
586         {0x338C, 0xA778},       // MCU_ADDRESS [MODE_GAM_TABLE_A_9]
587         {0x3390, 0x00D0},       // MCU_DATA_0
588         {0x338C, 0xA779},       // MCU_ADDRESS [MODE_GAM_TABLE_A_10]
589         {0x3390, 0x00D9},       // MCU_DATA_0
590         {0x338C, 0xA77A},       // MCU_ADDRESS [MODE_GAM_TABLE_A_11]
591         {0x3390, 0x00E0},       // MCU_DATA_0
592         {0x338C, 0xA77B},       // MCU_ADDRESS [MODE_GAM_TABLE_A_12]
593         {0x3390, 0x00E6},       // MCU_DATA_0
594         {0x338C, 0xA77C},       // MCU_ADDRESS [MODE_GAM_TABLE_A_13]
595         {0x3390, 0x00EB},       // MCU_DATA_0
596         {0x338C, 0xA77D},       // MCU_ADDRESS [MODE_GAM_TABLE_A_14]
597         {0x3390, 0x00F0},       // MCU_DATA_0
598         {0x338C, 0xA77E},       // MCU_ADDRESS [MODE_GAM_TABLE_A_15]
599         {0x3390, 0x00F4},       // MCU_DATA_0
600         {0x338C, 0xA77F},       // MCU_ADDRESS [MODE_GAM_TABLE_A_16]
601         {0x3390, 0x00F8},       // MCU_DATA_0
602         {0x338C, 0xA780},       // MCU_ADDRESS [MODE_GAM_TABLE_A_17]
603         {0x3390, 0x00FC},       // MCU_DATA_0
604         {0x338C, 0xA781},       // MCU_ADDRESS [MODE_GAM_TABLE_A_18]
605         {0x3390, 0x00FF},       // MCU_DATA_0
606         //  into outdoor mode
607         {0x338C, 0xA102},       // MCU_ADDRESS
608         {0x3390, 0x002F},       // MCU_DATA_0
609         {0x338C, 0xA114},       // MCU_ADDRESS
610         {0x3390, 0x0005},       // MCU_DATA_0
611         {SEQUENCE_WAIT_MS, 20},
612         {0x338C, 0xA103},  // MCU_ADDRESS
613         {0x3390, 0x0005},  // MCU_DATA_0
614         //Exposure
615         {0x338C, 0xA206},       // MCU_ADDRESS [AE_TARGET]
616         {0x3390, 0x003C},       // MCU_DATA_0
617         {SEQUENCE_END, 0x00}
618 };
619
620 /* 720p 15fps @ 1280x720 */
621
622 static struct reginfo sensor_720p[]=
623 {
624         {SEQUENCE_END, 0x00}
625 };
626
627 /*      1080p, 0x15fps, 0xyuv @1920x1080 */
628
629 static struct reginfo sensor_1080p[]=
630 {
631         {SEQUENCE_END, 0x00}
632 };
633
634 /* 2592X1944 QSXGA */
635 static struct reginfo sensor_qsxga[] =
636 {
637         {SEQUENCE_END, 0x00}
638 };
639 /* 2048*1536 QXGA */
640 static struct reginfo sensor_qxga[] =
641 {
642         {SEQUENCE_END, 0x00}
643 };
644
645 /* 1600X1200 UXGA */
646 static struct reginfo sensor_uxga[] =
647 {
648         {SEQUENCE_PROPERTY, SEQUENCE_CAPTURE},
649         {0x338C, 0x275F},
650         {0x3390, 0x0000},
651         {0x338C, 0x2761},
652         {0x3390, 0x0640},
653         {0x338C, 0x2763},
654         {0x3390, 0x0000},
655         {0x338C, 0x2765},
656         {0x3390, 0x04B0},
657         {0x338C, 0x2707},
658         {0x3390, 0x0640},
659         {0x338C, 0x2709},
660         {0x3390, 0x04B0},
661         {0x338C, 0xA103},
662         {0x3390, 0x0005},
663         {SEQUENCE_END, 0x00}
664 };
665 /* 1280X1024 SXGA */
666 static struct reginfo sensor_sxga[] =
667 {
668         {SEQUENCE_PROPERTY, SEQUENCE_CAPTURE},
669         {0x338C, 0x275F},
670         {0x3390, 0x0000},
671         {0x338C, 0x2761},
672         {0x3390, 0x0640},
673         {0x338C, 0x2763},
674         {0x3390, 0x0000},
675         {0x338C, 0x2765},
676         {0x3390, 0x04B0},
677         {0x338C, 0x2707},
678         {0x3390, 0x0500},
679         {0x338C, 0x2709},
680         {0x3390, 0x0400},
681         {0x338C, 0xA103},
682         {0x3390, 0x0005},
683         {SEQUENCE_END, 0x00}
684 };
685 /*  1024X768 XGA */
686 static struct reginfo sensor_xga[] =
687 {
688         {SEQUENCE_END, 0x00}
689 };
690
691 /* 800X600 SVGA*/
692 static struct reginfo sensor_svga[] =
693 {
694         {SEQUENCE_END, 0x00}
695 };
696
697 /* 640X480 VGA */
698 static struct reginfo sensor_vga[] =
699 {
700     {SEQUENCE_END, 0x00}
701 };
702
703 /* 352X288 CIF */
704 static struct reginfo sensor_cif[] =
705 {
706         {SEQUENCE_END, 0x00}
707 };
708
709 /* 320*240 QVGA */
710 static  struct reginfo sensor_qvga[] =
711 {
712         {SEQUENCE_END, 0x00}
713 };
714
715 /* 176X144 QCIF*/
716 static struct reginfo sensor_qcif[] =
717 {
718         {SEQUENCE_END, 0x00}
719 };
720
721 #endif
722 #if 0
723 /* 160X120 QQVGA*/
724 static struct reginfo ov2655_qqvga[] =
725 {
726
727     {0x300E, 0x34},
728     {0x3011, 0x01},
729     {0x3012, 0x10},
730     {0x302a, 0x02},
731     {0x302b, 0xE6},
732     {0x306f, 0x14},
733     {0x3362, 0x90},
734
735     {0x3070, 0x5d},
736     {0x3072, 0x5d},
737     {0x301c, 0x07},
738     {0x301d, 0x07},
739
740     {0x3020, 0x01},
741     {0x3021, 0x18},
742     {0x3022, 0x00},
743     {0x3023, 0x06},
744     {0x3024, 0x06},
745     {0x3025, 0x58},
746     {0x3026, 0x02},
747     {0x3027, 0x61},
748     {0x3088, 0x00},
749     {0x3089, 0xa0},
750     {0x308a, 0x00},
751     {0x308b, 0x78},
752     {0x3316, 0x64},
753     {0x3317, 0x25},
754     {0x3318, 0x80},
755     {0x3319, 0x08},
756     {0x331a, 0x0a},
757     {0x331b, 0x07},
758     {0x331c, 0x80},
759     {0x331d, 0x38},
760     {0x3100, 0x00},
761     {0x3302, 0x11},
762
763     {0x0, 0x0},
764 };
765
766
767
768 static  struct reginfo ov2655_Sharpness_auto[] =
769 {
770     {0x3306, 0x00},
771 };
772
773 static  struct reginfo ov2655_Sharpness1[] =
774 {
775     {0x3306, 0x08},
776     {0x3371, 0x00},
777 };
778
779 static  struct reginfo ov2655_Sharpness2[][3] =
780 {
781     //Sharpness 2
782     {0x3306, 0x08},
783     {0x3371, 0x01},
784 };
785
786 static  struct reginfo ov2655_Sharpness3[] =
787 {
788     //default
789     {0x3306, 0x08},
790     {0x332d, 0x02},
791 };
792 static  struct reginfo ov2655_Sharpness4[]=
793 {
794     //Sharpness 4
795     {0x3306, 0x08},
796     {0x332d, 0x03},
797 };
798
799 static  struct reginfo ov2655_Sharpness5[] =
800 {
801     //Sharpness 5
802     {0x3306, 0x08},
803     {0x332d, 0x04},
804 };
805 #endif
806
807 static  struct reginfo sensor_Preview2Capture[]=
808 {
809         {0x338C, 0xA120},       // MCU_ADDRESS [SEQ_CAP_MODE]
810         {0x3390, 0x0002},       // MCU_DATA_0
811         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
812         {0x3390, 0x0002},       // MCU_DATA_0
813     {0x338C, 0xA102},   // MCU_ADDRESS [SEQ_CMD]  cgz oppo 2008-09-26
814         {0x3390, 0x000E},
815         {SEQUENCE_END, 0x00}
816 };
817
818 static  struct reginfo sensor_Capture2Preview[]=
819 {
820         {0x338C, 0xA120},       // MCU_ADDRESS [SEQ_CAP_MODE]
821         {0x3390, 0x0000},       // MCU_DATA_0
822         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
823         {0x3390, 0x0001},       // MCU_DATA_0
824         {SEQUENCE_END, 0x00}
825 };
826
827 static  struct reginfo sensor_ClrFmt_YUYV[]=
828 {
829         {SEQUENCE_END, 0x00}
830 };
831
832 static  struct reginfo sensor_ClrFmt_UYVY[]=
833 {
834         {SEQUENCE_END, 0x00}
835 };
836
837
838 #if CONFIG_SENSOR_WhiteBalance
839 static  struct reginfo sensor_WhiteB_Auto[]=
840 {
841         //Auto
842         {0x338C, 0xA34A},       // MCU_ADDRESS [AWB_GAIN_MIN]
843         {0x3390, 0x0059},       // MCU_DATA_0
844         {0x338C, 0xA34B},       // MCU_ADDRESS [AWB_GAIN_MAX]
845         {0x3390, 0x00A6},       // MCU_DATA_0
846         {0x338C, 0xA34F},       // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
847         {0x3390, 0x0000},       // MCU_DATA_0
848         {0x338C, 0xA350},       // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
849         {0x3390, 0x007F},       // MCU_DATA_0
850         {SEQUENCE_END, 0x00}
851 };
852 /* Cloudy Colour Temperature : 6500K - 8000K  */
853 static  struct reginfo sensor_WhiteB_Cloudy[]=
854 {
855         //[V.       DL 7500]
856         {0x338C, 0xA34B},       // MCU_ADDRESS [AWB_GAIN_MAX]
857         {0x3390, 0x0080},       // MCU_DATA_0
858         {0x338C, 0xA34F},       // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
859         {0x3390, 0x007F},       // MCU_DATA_0
860         {0x338C, 0xA350},       // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
861         {0x3390, 0x007F},       // MCU_DATA_
862         {SEQUENCE_END, 0x00}
863 };
864 /* ClearDay Colour Temperature : 5000K - 6500K  */
865 static  struct reginfo sensor_WhiteB_ClearDay[]=
866 {
867         //[IV       Day Light]
868         {0x338C, 0xA34A},       // MCU_ADDRESS [AWB_GAIN_MIN]
869         {0x3390, 0x0080},       // MCU_DATA_0
870         {0x338C, 0xA34B},       // MCU_ADDRESS [AWB_GAIN_MAX]
871         {0x3390, 0x0080},       // MCU_DATA_0
872         {0x338C, 0xA34F},       // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
873         {0x3390, 0x007E},        // MCU_DATA_0
874         {0x338C, 0xA350},       // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
875         {0x3390, 0x007E},        // MCU_DATA_0
876         {SEQUENCE_END, 0x00}
877 };
878 /* Office Colour Temperature : 3500K - 5000K  */
879 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
880 {
881         //[III        Fluorescent]
882         {0x338C, 0xA34A},  // MCU_ADDRESS [AWB_GAIN_MIN]
883         {0x3390, 0x0080},  // MCU_DATA_0
884         {0x338C, 0xA34B},  // MCU_ADDRESS [AWB_GAIN_MAX]
885         {0x3390, 0x0080},  // MCU_DATA_0
886         {0x338C, 0xA34F},  // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
887         {0x3390, 0x0030},   // MCU_DATA_0
888         {0x338C, 0xA350},  // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
889         {0x3390, 0x0030},   // MCU_DATA_0
890         {SEQUENCE_END, 0x00}
891 };
892 /* Home Colour Temperature : 2500K - 3500K  */
893 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
894 {
895         //[II.        Incandescent]
896         {0x338C, 0xA34A},       // MCU_ADDRESS [AWB_GAIN_MIN]
897         {0x3390, 0x0080},       // MCU_DATA_0
898         {0x338C, 0xA34B},       // MCU_ADDRESS [AWB_GAIN_MAX]
899         {0x3390, 0x0080},       // MCU_DATA_0
900         {0x338C, 0xA34F},       // MCU_ADDRESS [AWB_CCM_POSITION_MIN]
901         {0x3390, 0x0032},        // MCU_DATA_0
902         {0x338C, 0xA350},       // MCU_ADDRESS [AWB_CCM_POSITION_MAX]
903         {0x3390, 0x0032},        // MCU_DATA_0
904         {SEQUENCE_END, 0x00}
905 };
906 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
907     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
908 };
909 #endif
910
911 #if CONFIG_SENSOR_Brightness
912 static  struct reginfo sensor_Brightness0[]=
913 {
914         {SEQUENCE_END, 0x00}
915 };
916
917 static  struct reginfo sensor_Brightness1[]=
918 {
919         {SEQUENCE_END, 0x00}
920 };
921
922 static  struct reginfo sensor_Brightness2[]=
923 {
924         {SEQUENCE_END, 0x00}
925 };
926
927 static  struct reginfo sensor_Brightness3[]=
928 {
929         {SEQUENCE_END, 0x00}
930 };
931
932 static  struct reginfo sensor_Brightness4[]=
933 {
934         {SEQUENCE_END, 0x00}
935 };
936
937 static  struct reginfo sensor_Brightness5[]=
938 {
939         {SEQUENCE_END, 0x00}
940 };
941 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
942     sensor_Brightness4, sensor_Brightness5,NULL,
943 };
944
945 #endif
946
947 #if CONFIG_SENSOR_Effect
948 static  struct reginfo sensor_Effect_Normal[] =
949 {
950         {0x338C, 0x2799},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_A]
951         {0x3390, 0x6408},       // MCU_DATA_0
952         {0x338C, 0x279B},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_B]
953         {0x3390, 0x6408},       // MCU_DATA_0
954         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
955         {0x3390, 0x0005},       // MCU_DATA_0
956         {SEQUENCE_END, 0x00}
957 };
958
959 static  struct reginfo sensor_Effect_WandB[] =
960 {
961         {0x338C, 0x2799},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_A]
962         {0x3390, 0x6409},       // MCU_DATA_0
963         {0x338C, 0x279B},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_B]
964         {0x3390, 0x6409},       // MCU_DATA_0
965         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
966         {0x3390, 0x0005},       // MCU_DATA_0
967         {SEQUENCE_END, 0x00}
968 };
969
970 static  struct reginfo sensor_Effect_Sepia[] =
971 {
972         {0x338C, 0x2799},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_A]
973         {0x3390, 0x640A},       // MCU_DATA_0
974         {0x338C, 0x279B},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_B]
975         {0x3390, 0x640A},       // MCU_DATA_0
976         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
977         {0x3390, 0x0005},       // MCU_DATA_0
978         {SEQUENCE_END, 0x00}
979 };
980
981 static  struct reginfo sensor_Effect_Negative[] =
982 {
983         {0x338C, 0x2799},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_A]
984         {0x3390, 0x640B},       // MCU_DATA_0
985         {0x338C, 0x279B},       // MCU_ADDRESS [MODE_SPEC_EFFECTS_B]
986         {0x3390, 0x640B},       // MCU_DATA_0
987         {0x338C, 0xA103},       // MCU_ADDRESS [SEQ_CMD]
988         {0x3390, 0x0005},       // MCU_DATA_0
989         {SEQUENCE_END, 0x00}
990 };
991 static  struct reginfo sensor_Effect_Bluish[] =
992 {
993         {SEQUENCE_END, 0x00}
994 };
995
996 static  struct reginfo sensor_Effect_Green[] =
997 {
998         {SEQUENCE_END, 0x00}
999 };
1000
1001 static struct reginfo sensor_Effect_Solarize[] =
1002 {
1003         {SEQUENCE_END, 0x00}
1004 };
1005 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Sepia,sensor_Effect_Negative,
1006     NULL,
1007 };
1008 #endif
1009 #if CONFIG_SENSOR_Exposure
1010 static  struct reginfo sensor_Exposure0[]=
1011 {
1012         {SEQUENCE_END, 0x00}
1013 };
1014
1015 static  struct reginfo sensor_Exposure1[]=
1016 {
1017         {SEQUENCE_END, 0x00}
1018 };
1019
1020 static  struct reginfo sensor_Exposure2[]=
1021 {
1022         {SEQUENCE_END, 0x00}
1023 };
1024
1025 static  struct reginfo sensor_Exposure3[]=
1026 {
1027         {SEQUENCE_END, 0x00}
1028 };
1029
1030 static  struct reginfo sensor_Exposure4[]=
1031 {
1032         {SEQUENCE_END, 0x00}
1033 };
1034
1035 static  struct reginfo sensor_Exposure5[]=
1036 {
1037         {SEQUENCE_END, 0x00}
1038 };
1039
1040 static  struct reginfo sensor_Exposure6[]=
1041 {
1042         {SEQUENCE_END, 0x00}
1043 };
1044
1045 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1046     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1047 };
1048 #endif
1049 #if CONFIG_SENSOR_Saturation
1050 static  struct reginfo sensor_Saturation0[]=
1051 {
1052         {SEQUENCE_END, 0x00}
1053 };
1054
1055 static  struct reginfo sensor_Saturation1[]=
1056 {
1057         {SEQUENCE_END, 0x00}
1058 };
1059
1060 static  struct reginfo sensor_Saturation2[]=
1061 {
1062         {SEQUENCE_END, 0x00}
1063 };
1064 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
1065
1066 #endif
1067 #if CONFIG_SENSOR_Contrast
1068 static  struct reginfo sensor_Contrast0[]=
1069 {
1070         {SEQUENCE_END, 0x00}
1071 };
1072
1073 static  struct reginfo sensor_Contrast1[]=
1074 {
1075         {SEQUENCE_END, 0x00}
1076 };
1077
1078 static  struct reginfo sensor_Contrast2[]=
1079 {
1080         {SEQUENCE_END, 0x00}
1081 };
1082
1083 static  struct reginfo sensor_Contrast3[]=
1084 {
1085         {SEQUENCE_END, 0x00}
1086 };
1087
1088 static  struct reginfo sensor_Contrast4[]=
1089 {
1090         {SEQUENCE_END, 0x00}
1091 };
1092
1093
1094 static  struct reginfo sensor_Contrast5[]=
1095 {
1096         {SEQUENCE_END, 0x00}
1097 };
1098
1099 static  struct reginfo sensor_Contrast6[]=
1100 {
1101         {SEQUENCE_END, 0x00}
1102 };
1103 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
1104     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
1105 };
1106
1107 #endif
1108 #if CONFIG_SENSOR_Mirror
1109 static  struct reginfo sensor_MirrorOn[]=
1110 {
1111         {SEQUENCE_END, 0x00}
1112 };
1113
1114 static  struct reginfo sensor_MirrorOff[]=
1115 {
1116         {SEQUENCE_END, 0x00}
1117 };
1118 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
1119 #endif
1120 #if CONFIG_SENSOR_Flip
1121 static  struct reginfo sensor_FlipOn[]=
1122 {
1123         {SEQUENCE_END, 0x00}
1124 };
1125
1126 static  struct reginfo sensor_FlipOff[]=
1127 {
1128         {SEQUENCE_END, 0x00}
1129 };
1130 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
1131
1132 #endif
1133 #if CONFIG_SENSOR_Scene
1134 static  struct reginfo sensor_SceneAuto[] =
1135 {
1136         {0x338C, 0xA102},         // MCU_ADDRESS
1137         {0x3390, 0x002F},         // MCU_DATA_0
1138         {0x338C, 0xA114},         // MCU_ADDRESS
1139         {0x3390, 0x0005},         // MCU_DATA_0
1140         {SEQUENCE_WAIT_MS, 20},
1141         {0x338C, 0xA103},  // MCU_ADDRESS
1142         {0x3390, 0x0005},  // MCU_DATA_0
1143         {SEQUENCE_END, 0x00}
1144 };
1145
1146 static  struct reginfo sensor_SceneNight[] =
1147 {
1148         {0x338C, 0xA102},     // MCU_ADDRESS
1149         {0x3390, 0x000F},     // MCU_DATA_0
1150         {SEQUENCE_WAIT_MS, 20},
1151         {0x338C, 0xA103},  // MCU_ADDRESS
1152         {0x3390, 0x0005},  // MCU_DATA_0
1153         {SEQUENCE_END, 0x00}
1154 };
1155 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
1156
1157 #endif
1158 #if CONFIG_SENSOR_DigitalZoom
1159 static struct reginfo sensor_Zoom0[] =
1160 {
1161         {SEQUENCE_END, 0x00}
1162 };
1163
1164 static struct reginfo sensor_Zoom1[] =
1165 {
1166         {SEQUENCE_END, 0x00}
1167 };
1168
1169 static struct reginfo sensor_Zoom2[] =
1170 {
1171         {SEQUENCE_END, 0x00}
1172 };
1173
1174
1175 static struct reginfo sensor_Zoom3[] =
1176 {
1177         {SEQUENCE_END, 0x00}
1178 };
1179 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
1180 #endif
1181 static const struct v4l2_querymenu sensor_menus[] =
1182 {
1183         #if CONFIG_SENSOR_WhiteBalance
1184     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
1185     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
1186     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
1187     #endif
1188
1189         #if CONFIG_SENSOR_Effect
1190     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
1191     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
1192     #endif
1193
1194         #if CONFIG_SENSOR_Scene
1195     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
1196     #endif
1197
1198         #if CONFIG_SENSOR_Flash
1199     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
1200     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
1201     #endif
1202 };
1203
1204 static  struct v4l2_queryctrl sensor_controls[] =
1205 {
1206         #if CONFIG_SENSOR_WhiteBalance
1207     {
1208         .id             = V4L2_CID_DO_WHITE_BALANCE,
1209         .type           = V4L2_CTRL_TYPE_MENU,
1210         .name           = "White Balance Control",
1211         .minimum        = 0,
1212         .maximum        = 4,
1213         .step           = 1,
1214         .default_value = 0,
1215     },
1216     #endif
1217
1218         #if CONFIG_SENSOR_Brightness
1219         {
1220         .id             = V4L2_CID_BRIGHTNESS,
1221         .type           = V4L2_CTRL_TYPE_INTEGER,
1222         .name           = "Brightness Control",
1223         .minimum        = -3,
1224         .maximum        = 2,
1225         .step           = 1,
1226         .default_value = 0,
1227     },
1228     #endif
1229
1230         #if CONFIG_SENSOR_Effect
1231         {
1232         .id             = V4L2_CID_EFFECT,
1233         .type           = V4L2_CTRL_TYPE_MENU,
1234         .name           = "Effect Control",
1235         .minimum        = 0,
1236         .maximum        = 3,
1237         .step           = 1,
1238         .default_value = 0,
1239     },
1240         #endif
1241
1242         #if CONFIG_SENSOR_Exposure
1243         {
1244         .id             = V4L2_CID_EXPOSURE,
1245         .type           = V4L2_CTRL_TYPE_INTEGER,
1246         .name           = "Exposure Control",
1247         .minimum        = 0,
1248         .maximum        = 6,
1249         .step           = 1,
1250         .default_value = 0,
1251     },
1252         #endif
1253
1254         #if CONFIG_SENSOR_Saturation
1255         {
1256         .id             = V4L2_CID_SATURATION,
1257         .type           = V4L2_CTRL_TYPE_INTEGER,
1258         .name           = "Saturation Control",
1259         .minimum        = 0,
1260         .maximum        = 2,
1261         .step           = 1,
1262         .default_value = 0,
1263     },
1264     #endif
1265
1266         #if CONFIG_SENSOR_Contrast
1267         {
1268         .id             = V4L2_CID_CONTRAST,
1269         .type           = V4L2_CTRL_TYPE_INTEGER,
1270         .name           = "Contrast Control",
1271         .minimum        = -3,
1272         .maximum        = 3,
1273         .step           = 1,
1274         .default_value = 0,
1275     },
1276         #endif
1277
1278         #if CONFIG_SENSOR_Mirror
1279         {
1280         .id             = V4L2_CID_HFLIP,
1281         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1282         .name           = "Mirror Control",
1283         .minimum        = 0,
1284         .maximum        = 1,
1285         .step           = 1,
1286         .default_value = 1,
1287     },
1288     #endif
1289
1290         #if CONFIG_SENSOR_Flip
1291         {
1292         .id             = V4L2_CID_VFLIP,
1293         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1294         .name           = "Flip Control",
1295         .minimum        = 0,
1296         .maximum        = 1,
1297         .step           = 1,
1298         .default_value = 1,
1299     },
1300     #endif
1301
1302         #if CONFIG_SENSOR_Scene
1303     {
1304         .id             = V4L2_CID_SCENE,
1305         .type           = V4L2_CTRL_TYPE_MENU,
1306         .name           = "Scene Control",
1307         .minimum        = 0,
1308         .maximum        = 1,
1309         .step           = 1,
1310         .default_value = 0,
1311     },
1312     #endif
1313
1314         #if CONFIG_SENSOR_DigitalZoom
1315     {
1316         .id             = V4L2_CID_ZOOM_RELATIVE,
1317         .type           = V4L2_CTRL_TYPE_INTEGER,
1318         .name           = "DigitalZoom Control",
1319         .minimum        = -1,
1320         .maximum        = 1,
1321         .step           = 1,
1322         .default_value = 0,
1323     }, {
1324         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1325         .type           = V4L2_CTRL_TYPE_INTEGER,
1326         .name           = "DigitalZoom Control",
1327         .minimum        = 0,
1328         .maximum        = 3,
1329         .step           = 1,
1330         .default_value = 0,
1331     },
1332     #endif
1333
1334         #if CONFIG_SENSOR_Focus
1335         {
1336         .id             = V4L2_CID_FOCUS_RELATIVE,
1337         .type           = V4L2_CTRL_TYPE_INTEGER,
1338         .name           = "Focus Control",
1339         .minimum        = -1,
1340         .maximum        = 1,
1341         .step           = 1,
1342         .default_value = 0,
1343     }, {
1344         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1345         .type           = V4L2_CTRL_TYPE_INTEGER,
1346         .name           = "Focus Control",
1347         .minimum        = 0,
1348         .maximum        = 255,
1349         .step           = 1,
1350         .default_value = 125,
1351     },
1352         {
1353         .id             = V4L2_CID_FOCUS_AUTO,
1354         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1355         .name           = "Focus Control",
1356         .minimum        = 0,
1357         .maximum        = 1,
1358         .step           = 1,
1359         .default_value = 0,
1360     },{
1361         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1362         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1363         .name           = "Focus Control",
1364         .minimum        = 0,
1365         .maximum        = 1,
1366         .step           = 1,
1367         .default_value = 0,
1368     },
1369     #endif
1370
1371         #if CONFIG_SENSOR_Flash
1372         {
1373         .id             = V4L2_CID_FLASH,
1374         .type           = V4L2_CTRL_TYPE_MENU,
1375         .name           = "Flash Control",
1376         .minimum        = 0,
1377         .maximum        = 3,
1378         .step           = 1,
1379         .default_value = 0,
1380     },
1381         #endif
1382 };
1383
1384 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1385 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1386 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1387 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1388 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1389 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1390 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1391 static int sensor_resume(struct soc_camera_device *icd);
1392 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1393 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1394 #if CONFIG_SENSOR_Effect
1395 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1396 #endif
1397 #if CONFIG_SENSOR_WhiteBalance
1398 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1399 #endif
1400 static int sensor_deactivate(struct i2c_client *client);
1401 static struct soc_camera_ops sensor_ops =
1402 {
1403     .suspend                     = sensor_suspend,
1404     .resume                       = sensor_resume,
1405     .set_bus_param              = sensor_set_bus_param,
1406     .query_bus_param    = sensor_query_bus_param,
1407     .controls           = sensor_controls,
1408     .menus                         = sensor_menus,
1409     .num_controls               = ARRAY_SIZE(sensor_controls),
1410     .num_menus          = ARRAY_SIZE(sensor_menus),
1411 };
1412 /* only one fixed colorspace per pixelcode */
1413 struct sensor_datafmt {
1414         enum v4l2_mbus_pixelcode code;
1415         enum v4l2_colorspace colorspace;
1416 };
1417
1418 /* Find a data format by a pixel code in an array */
1419 static const struct sensor_datafmt *sensor_find_datafmt(
1420         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1421         int n)
1422 {
1423         int i;
1424         for (i = 0; i < n; i++)
1425                 if (fmt[i].code == code)
1426                         return fmt + i;
1427
1428         return NULL;
1429 }
1430
1431 static const struct sensor_datafmt sensor_colour_fmts[] = {
1432     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1433     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1434 };
1435 enum sensor_work_state
1436 {
1437         sensor_work_ready = 0,
1438         sensor_working,
1439 };
1440 struct sensor_work
1441 {
1442         struct i2c_client *client;
1443         struct delayed_work dwork;
1444         enum sensor_work_state state;
1445 };
1446
1447 typedef struct sensor_info_priv_s
1448 {
1449     int whiteBalance;
1450     int brightness;
1451     int contrast;
1452     int saturation;
1453     int effect;
1454     int scene;
1455     int digitalzoom;
1456     int focus;
1457         int auto_focus;
1458         int affm_reinit;
1459     int flash;
1460     int exposure;
1461     unsigned char mirror;                                        /* HFLIP */
1462     unsigned char flip;                                          /* VFLIP */
1463         bool snap2preview;
1464         bool video2preview;
1465         int capture_w;
1466         int capture_h;
1467         int preview_w;
1468         int preview_h;
1469     struct reginfo *winseqe_cur_addr;
1470         struct sensor_datafmt fmt;
1471         unsigned int enable;
1472         unsigned int funmodule_state;
1473 } sensor_info_priv_t;
1474
1475
1476
1477 struct sensor_parameter
1478 {
1479         unsigned short int preview_maxlines;
1480         unsigned short int preview_exposure;
1481         unsigned short int preview_line_width;
1482         unsigned short int preview_gain;
1483
1484         unsigned short int capture_framerate;
1485         unsigned short int preview_framerate;
1486 };
1487
1488 struct sensor
1489 {
1490     struct v4l2_subdev subdev;
1491     struct i2c_client *client;
1492     sensor_info_priv_t info_priv;
1493         struct sensor_parameter parameter;
1494         struct workqueue_struct *sensor_wq;
1495         struct sensor_work sensor_wk;
1496         struct mutex wq_lock;
1497     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1498 #if CONFIG_SENSOR_I2C_NOSCHED
1499         atomic_t tasklock_cnt;
1500 #endif
1501         struct rk29camera_platform_data *sensor_io_request;
1502     struct rk29camera_gpio_res *sensor_gpio_res;
1503 };
1504
1505 static struct sensor* to_sensor(const struct i2c_client *client)
1506 {
1507     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1508 }
1509
1510 static int sensor_task_lock(struct i2c_client *client, int lock)
1511 {
1512 #if CONFIG_SENSOR_I2C_NOSCHED
1513         int cnt = 3;
1514     struct sensor *sensor = to_sensor(client);
1515
1516         if (lock) {
1517                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1518                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1519                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1520                                 msleep(35);
1521                                 cnt--;
1522                         }
1523                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1524                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1525                                 goto sensor_task_lock_err;
1526                         }
1527                         preempt_disable();
1528                 }
1529
1530                 atomic_add(1, &sensor->tasklock_cnt);
1531         } else {
1532                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1533                         atomic_sub(1, &sensor->tasklock_cnt);
1534
1535                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1536                                 preempt_enable();
1537                 }
1538         }
1539         return 0;
1540 sensor_task_lock_err:
1541         return -1;  
1542 #else
1543     return 0;
1544 #endif
1545
1546 }
1547
1548 /* sensor register write */
1549 static int sensor_write(struct i2c_client *client, u16 reg, u16 val)
1550 {
1551     int err=0,cnt;
1552     u8 buf[4];
1553     struct i2c_msg msg[1];
1554
1555         switch (reg)
1556         {
1557                 case SEQUENCE_WAIT_MS:
1558                 {
1559                         msleep(val);
1560                         break;
1561                 }
1562
1563                 case SEQUENCE_WAIT_US:
1564                 {
1565                         udelay(val);
1566                         break;
1567                 }
1568
1569                 case SEQUENCE_PROPERTY:
1570                 {
1571                         break;
1572                 }
1573                 default:
1574                 {
1575                     buf[0] = reg >> 8;
1576                     buf[1] = reg & 0xFF;
1577                     buf[2] = val >> 8;
1578                         buf[3] = val & 0xFF;
1579
1580                     msg->addr = client->addr;
1581                     msg->flags = client->flags;
1582                     msg->buf = buf;
1583                     msg->len = sizeof(buf);
1584                     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1585                     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1586
1587                     cnt = 3;
1588                     err = -EAGAIN;
1589
1590                     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1591                         err = i2c_transfer(client->adapter, msg, 1);
1592
1593                         if (err >= 0) {
1594                             return 0;
1595                         } else {
1596                             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
1597                             udelay(10);
1598                         }
1599                     }
1600                 }
1601         }
1602     return err;
1603 }
1604
1605 /* sensor register read */
1606 static int sensor_read(struct i2c_client *client, u16 reg, u16 *val)
1607 {
1608     int err,cnt;
1609     u8 buf[2];
1610     struct i2c_msg msg[2];
1611
1612     buf[0] = reg >> 8;
1613     buf[1] = reg & 0xFF;
1614
1615     msg[0].addr = client->addr;
1616     msg[0].flags = client->flags;
1617     msg[0].buf = buf;
1618     msg[0].len = sizeof(buf);
1619     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1620     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1621
1622     msg[1].addr = client->addr;
1623     msg[1].flags = client->flags|I2C_M_RD;
1624     msg[1].buf = buf;
1625     msg[1].len = 2;
1626     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1627     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1628
1629     cnt = 3;
1630     err = -EAGAIN;
1631     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1632         err = i2c_transfer(client->adapter, msg, 2);
1633
1634         if (err >= 0) {
1635             *val = buf[0];
1636             return 0;
1637         } else {
1638                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1639             udelay(10);
1640         }
1641     }
1642
1643     return err;
1644 }
1645
1646 /* write a array of registers  */
1647 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1648 {
1649     int err = 0, cnt;
1650     int i = 0;
1651 #if CONFIG_SENSOR_I2C_RDWRCHK
1652         char valchk;
1653 #endif
1654
1655         cnt = 0;
1656         if (sensor_task_lock(client, 1) < 0)
1657                 goto sensor_write_array_end;
1658     while (regarray[i].reg != SEQUENCE_END)
1659     {
1660
1661         err = sensor_write(client, regarray[i].reg, regarray[i].val);
1662         if (err < 0)
1663         {
1664             if (cnt-- > 0) {
1665                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1666                                 i = 0;
1667                                 continue;
1668             } else {
1669                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1670                 err = -EPERM;
1671                                 goto sensor_write_array_end;
1672             }
1673         } else {
1674         #if CONFIG_SENSOR_I2C_RDWRCHK
1675                         sensor_read(client, regarray[i].reg, &valchk);
1676                         if (valchk != regarray[i].val)
1677                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1678                 #endif
1679         }
1680
1681         i++;
1682     }
1683
1684 sensor_write_array_end:
1685         sensor_task_lock(client,0);
1686     return err;
1687 }
1688 #if CONFIG_SENSOR_I2C_RDWRCHK
1689 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1690 {
1691     int cnt;
1692     int i = 0;
1693         char valchk;
1694
1695         cnt = 0;
1696         valchk = 0;
1697     while (regarray[i].reg != SEQUENCE_END)
1698     {
1699                 sensor_read(client, regarray[i].reg, &valchk);
1700                 if (valchk != regarray[i].val)
1701                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1702
1703         i++;
1704     }
1705     return 0;
1706 }
1707 #endif
1708 #if CONFIG_SENSOR_Focus
1709
1710 static int sensor_af_single(struct i2c_client *client)
1711 {
1712         int ret = 0;
1713
1714
1715 sensor_af_single_end:
1716         return ret;
1717 }
1718
1719 static int sensor_af_const(struct i2c_client *client)
1720 {
1721         int ret = 0;
1722
1723 sensor_af_const_end:
1724         return ret;
1725 }
1726
1727 static int sensor_af_zoneupdate(struct i2c_client *client)
1728 {
1729         int ret = 0;
1730
1731 sensor_af_zoneupdate_end:
1732         return ret;
1733 }
1734
1735 static int sensor_af_init(struct i2c_client *client)
1736 {
1737         int ret = 0;
1738
1739         return ret;
1740 }
1741 #endif
1742
1743 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1744 {
1745         struct soc_camera_link *icl = to_soc_camera_link(icd);
1746         int ret = 0;
1747
1748     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1749         switch (cmd)
1750         {
1751                 case Sensor_PowerDown:
1752                 {
1753                         if (icl->powerdown) {
1754                                 ret = icl->powerdown(icd->pdev, on);
1755                                 if (ret == RK29_CAM_IO_SUCCESS) {
1756                                         if (on == 0) {
1757                                                 mdelay(2);
1758                                                 if (icl->reset)
1759                                                         icl->reset(icd->pdev);
1760                                         }
1761                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1762                                         ret = -ENODEV;
1763                                         goto sensor_power_end;
1764                                 }
1765                         }
1766                         break;
1767                 }
1768                 case Sensor_Flash:
1769                 {
1770                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1771                 struct sensor *sensor = to_sensor(client);
1772
1773                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1774                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1775                 if(on){
1776                     //flash off after 2 secs
1777                         hrtimer_cancel(&(flash_off_timer.timer));
1778                         hrtimer_start(&(flash_off_timer.timer),ktime_set(0, 800*1000*1000),HRTIMER_MODE_REL);
1779                     }
1780                         }
1781                         break;
1782                 }
1783                 default:
1784                 {
1785                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1786                         break;
1787                 }
1788         }
1789
1790 sensor_power_end:
1791         return ret;
1792 }
1793
1794 static enum hrtimer_restart flash_off_func(struct hrtimer *timer){
1795         struct flash_timer *fps_timer = container_of(timer, struct flash_timer, timer);
1796     sensor_ioctrl(fps_timer->icd,Sensor_Flash,0);
1797         SENSOR_DG("%s %s !!!!!!",SENSOR_NAME_STRING(),__FUNCTION__);
1798     return 0;
1799     
1800 }
1801
1802 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1803 {
1804     struct i2c_client *client = v4l2_get_subdevdata(sd);
1805     struct soc_camera_device *icd = client->dev.platform_data;
1806     struct sensor *sensor = to_sensor(client);
1807         const struct v4l2_queryctrl *qctrl;
1808     const struct sensor_datafmt *fmt;
1809     int ret;
1810 #if (SENSOR_ID_REG != SEQUENCE_END)    
1811     u16 pid = 0;
1812 #endif
1813
1814     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1815
1816         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1817                 ret = -ENODEV;
1818                 goto sensor_INIT_ERR;
1819         }
1820
1821     /* soft reset */
1822         if (sensor_task_lock(client,1)<0)
1823                 goto sensor_INIT_ERR;
1824
1825 #if (SENSOR_RESET_REG != SEQUENCE_END)
1826     ret = sensor_write(client, SENSOR_RESET_REG, SENSOR_RESET_VAL);
1827     if (ret != 0) {
1828         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1829         ret = -ENODEV;
1830                 goto sensor_INIT_ERR;
1831     }
1832
1833     mdelay(5);  //delay 5 microseconds
1834 #endif
1835
1836         /* check if it is an sensor sensor */
1837 #if (SENSOR_ID_REG != SEQUENCE_END)
1838     ret = sensor_read(client, SENSOR_ID_REG, &pid);
1839     if (ret != 0) {
1840         SENSOR_TR("read chip id failed\n");
1841         ret = -ENODEV;
1842         goto sensor_INIT_ERR;
1843     }
1844
1845     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
1846 #else
1847         pid = SENSOR_ID;
1848 #endif
1849     if (pid == SENSOR_ID) {
1850         sensor->model = SENSOR_V4L2_IDENT;
1851     } else {
1852         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1853         ret = -ENODEV;
1854         goto sensor_INIT_ERR;
1855     }
1856
1857     ret = sensor_write_array(client, sensor_init_data);
1858     if (ret != 0)
1859     {
1860         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1861         goto sensor_INIT_ERR;
1862     }
1863         sensor_task_lock(client,0);
1864     sensor->info_priv.preview_w = SENSOR_INIT_WIDTH;
1865     sensor->info_priv.preview_h = SENSOR_INIT_HEIGHT;
1866     sensor->info_priv.capture_w = SENSOR_MAX_WIDTH;
1867     sensor->info_priv.capture_h = SENSOR_MAX_HEIGHT;
1868     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
1869     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1870     if (!fmt) {
1871         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1872         ret = -EINVAL;
1873         goto sensor_INIT_ERR;
1874     }
1875         sensor->info_priv.fmt = *fmt;
1876
1877     /* sensor sensor information for initialization  */
1878         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1879         if (qctrl)
1880         sensor->info_priv.whiteBalance = qctrl->default_value;
1881         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1882         if (qctrl)
1883         sensor->info_priv.brightness = qctrl->default_value;
1884         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1885         if (qctrl)
1886         sensor->info_priv.effect = qctrl->default_value;
1887         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1888         if (qctrl)
1889         sensor->info_priv.exposure = qctrl->default_value;
1890
1891         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1892         if (qctrl)
1893         sensor->info_priv.saturation = qctrl->default_value;
1894         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1895         if (qctrl)
1896         sensor->info_priv.contrast = qctrl->default_value;
1897         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1898         if (qctrl)
1899         sensor->info_priv.mirror = qctrl->default_value;
1900         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1901         if (qctrl)
1902         sensor->info_priv.flip = qctrl->default_value;
1903         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1904         if (qctrl)
1905         sensor->info_priv.scene = qctrl->default_value;
1906         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1907         if (qctrl)
1908         sensor->info_priv.digitalzoom = qctrl->default_value;
1909
1910     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1911         #if CONFIG_SENSOR_Focus
1912     sensor_set_focus();
1913     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1914         if (qctrl)
1915         sensor->info_priv.focus = qctrl->default_value;
1916         #endif
1917
1918         #if CONFIG_SENSOR_Flash
1919         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1920         if (qctrl)
1921         sensor->info_priv.flash = qctrl->default_value;
1922
1923         hrtimer_init(&(flash_off_timer.timer), CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1924     flash_off_timer.icd = icd;
1925         flash_off_timer.timer.function = flash_off_func;
1926     #endif
1927     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);
1928
1929     return 0;
1930 sensor_INIT_ERR:
1931         sensor_task_lock(client,0);
1932         sensor_deactivate(client);
1933     return ret;
1934 }
1935 static int sensor_deactivate(struct i2c_client *client)
1936 {
1937         struct soc_camera_device *icd = client->dev.platform_data;
1938
1939         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1940
1941         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1942         sensor_task_lock(client, 1);
1943         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1944
1945         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1946         icd->user_width = SENSOR_INIT_WIDTH;
1947     icd->user_height = SENSOR_INIT_HEIGHT;
1948         msleep(100);
1949         return 0;
1950 }
1951 static  struct reginfo sensor_power_down_sequence[]=
1952 {
1953     {0x00,0x00}
1954 };
1955 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1956 {
1957     int ret;
1958     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1959
1960     if (pm_msg.event == PM_EVENT_SUSPEND) {
1961         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1962         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1963         if (ret != 0) {
1964             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1965             return ret;
1966         } else {
1967             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1968             if (ret < 0) {
1969                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1970                 return -EINVAL;
1971             }
1972         }
1973     } else {
1974         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1975         return -EINVAL;
1976     }
1977
1978     return 0;
1979 }
1980
1981 static int sensor_resume(struct soc_camera_device *icd)
1982 {
1983         int ret;
1984
1985     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1986     if (ret < 0) {
1987                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1988         return -EINVAL;
1989     }
1990
1991         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1992         return 0;
1993 }
1994
1995 static int sensor_set_bus_param(struct soc_camera_device *icd,
1996                                 unsigned long flags)
1997 {
1998
1999     return 0;
2000 }
2001
2002 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2003 {
2004     struct soc_camera_link *icl = to_soc_camera_link(icd);
2005     unsigned long flags = SENSOR_BUS_PARAM;
2006
2007     return soc_camera_apply_sensor_flags(icl, flags);
2008 }
2009 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2010 {
2011     struct i2c_client *client = v4l2_get_subdevdata(sd);
2012     struct soc_camera_device *icd = client->dev.platform_data;
2013     struct sensor *sensor = to_sensor(client);
2014
2015     mf->width   = icd->user_width;
2016         mf->height      = icd->user_height;
2017         mf->code        = sensor->info_priv.fmt.code;
2018         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2019         mf->field       = V4L2_FIELD_NONE;
2020
2021     return 0;
2022 }
2023 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2024 {
2025     bool ret = false;
2026
2027         if ((mf->width == 1024) && (mf->height == 768)) {
2028                 ret = true;
2029         } else if ((mf->width == 1280) && (mf->height == 1024)) {
2030                 ret = true;
2031         } else if ((mf->width == 1600) && (mf->height == 1200)) {
2032                 ret = true;
2033         } else if ((mf->width == 2048) && (mf->height == 1536)) {
2034                 ret = true;
2035         } else if ((mf->width == 2592) && (mf->height == 1944)) {
2036                 ret = true;
2037         }
2038
2039         if (ret == true)
2040                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
2041         return ret;
2042 }
2043
2044 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2045 {
2046     bool ret = false;
2047
2048         if ((mf->width == 1280) && (mf->height == 720)) {
2049                 ret = true;
2050         } else if ((mf->width == 1920) && (mf->height == 1080)) {
2051                 ret = true;
2052         }
2053
2054         if (ret == true)
2055                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
2056         return ret;
2057 }
2058 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2059 {
2060     struct i2c_client *client = v4l2_get_subdevdata(sd);
2061     struct sensor *sensor = to_sensor(client);
2062     const struct sensor_datafmt *fmt;
2063     struct reginfo *winseqe_set_addr=NULL;
2064     int ret = 0, set_w,set_h;
2065
2066         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2067                                    ARRAY_SIZE(sensor_colour_fmts));
2068         if (!fmt) {
2069         ret = -EINVAL;
2070         goto sensor_s_fmt_end;
2071     }
2072
2073         if (sensor->info_priv.fmt.code != mf->code) {
2074                 switch (mf->code)
2075                 {
2076                         case V4L2_MBUS_FMT_YUYV8_2X8:
2077                         {
2078                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
2079                                 break;
2080                         }
2081                         case V4L2_MBUS_FMT_UYVY8_2X8:
2082                         {
2083                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
2084                                 break;
2085                         }
2086                         default:
2087                                 break;
2088                 }
2089                 if (winseqe_set_addr != NULL) {
2090             sensor_write_array(client, winseqe_set_addr);
2091                         sensor->info_priv.fmt.code = mf->code;
2092             sensor->info_priv.fmt.colorspace= mf->colorspace;            
2093                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
2094                 } else {
2095                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
2096                 }
2097         }
2098
2099     set_w = mf->width;
2100     set_h = mf->height;
2101         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2102         {
2103                 winseqe_set_addr = sensor_qcif;
2104         set_w = 176;
2105         set_h = 144;
2106         }
2107         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2108     {
2109         winseqe_set_addr = sensor_qvga;
2110         set_w = 320;
2111         set_h = 240;
2112     }
2113     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2114     {
2115         winseqe_set_addr = sensor_cif;
2116         set_w = 352;
2117         set_h = 288;
2118     }
2119     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2120     {
2121         winseqe_set_addr = sensor_vga;
2122         set_w = 640;
2123         set_h = 480;
2124     }
2125     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2126     {
2127         winseqe_set_addr = sensor_svga;
2128         set_w = 800;
2129         set_h = 600;
2130     }
2131         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2132     {
2133         winseqe_set_addr = sensor_xga;
2134         set_w = 1024;
2135         set_h = 768;
2136     }
2137         else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2138     {
2139         winseqe_set_addr = sensor_720p;
2140         set_w = 1280;
2141         set_h = 720;
2142     }
2143     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2144     {
2145         winseqe_set_addr = sensor_sxga;
2146         set_w = 1280;
2147         set_h = 1024;
2148     }
2149     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2150     {
2151         winseqe_set_addr = sensor_uxga;
2152         set_w = 1600;
2153         set_h = 1200;
2154     }
2155     else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2156     {
2157         winseqe_set_addr = sensor_1080p;
2158         set_w = 1920;
2159         set_h = 1080;
2160     }
2161         else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2162     {
2163         winseqe_set_addr = sensor_qxga;
2164         set_w = 2048;
2165         set_h = 1536;
2166     }
2167         else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2168     {
2169         winseqe_set_addr = sensor_qsxga;
2170         set_w = 2592;
2171         set_h = 1944;
2172     }
2173
2174     if ((winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) && winseqe_set_addr) {
2175         ret |= sensor_write_array(client, winseqe_set_addr);
2176         if (ret != 0) {
2177             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2178             goto sensor_s_fmt_end;
2179         }
2180         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2181                 if ((winseqe_set_addr[0].reg==SEQUENCE_PROPERTY) && (winseqe_set_addr[0].val==SEQUENCE_CAPTURE)) {
2182                 SENSOR_DG("\n%s..%s..Capture icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2183                 } else {
2184                         SENSOR_DG("\n%s..%s..Video icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2185                 }
2186     }
2187
2188         if (winseqe_set_addr && (winseqe_set_addr[0].reg==SEQUENCE_PROPERTY) && (winseqe_set_addr[0].val==SEQUENCE_CAPTURE)) {
2189                 ret |= sensor_write_array(client, sensor_Preview2Capture);
2190                 if (ret != 0) {
2191                 SENSOR_TR("%s Preview 2 Capture failed\n", SENSOR_NAME_STRING());
2192                 goto sensor_s_fmt_end;
2193         }
2194                 sensor->info_priv.capture_w = set_w;
2195                 sensor->info_priv.capture_h = set_h;
2196                 sensor->info_priv.snap2preview = true;
2197         } else if (sensor->info_priv.snap2preview == true) {
2198                 if (winseqe_set_addr || ((sensor->info_priv.preview_w == mf->width) && (sensor->info_priv.preview_h == mf->height))) {
2199                         ret |= sensor_write_array(client, sensor_Capture2Preview);
2200                         if (ret != 0) {
2201                         SENSOR_TR("%s Capture 2 Preview failed\n", SENSOR_NAME_STRING());
2202                         goto sensor_s_fmt_end;
2203                 }
2204                         sensor->info_priv.preview_w = mf->width;
2205                         sensor->info_priv.preview_h = mf->height;
2206                         sensor->info_priv.snap2preview = false;
2207                 } else {
2208                         SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2209                 }
2210         }
2211
2212         mf->width = set_w;
2213         mf->height = set_h;
2214 sensor_s_fmt_end:
2215     return ret;
2216 }
2217
2218 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2219 {
2220     struct i2c_client *client = v4l2_get_subdevdata(sd);
2221     struct sensor *sensor = to_sensor(client);
2222     const struct sensor_datafmt *fmt;
2223     int ret = 0,set_w,set_h;
2224    
2225         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2226                                    ARRAY_SIZE(sensor_colour_fmts));
2227         if (fmt == NULL) {
2228                 fmt = &sensor->info_priv.fmt;
2229         mf->code = fmt->code;
2230         } 
2231
2232     if (mf->height > SENSOR_MAX_HEIGHT)
2233         mf->height = SENSOR_MAX_HEIGHT;
2234     else if (mf->height < SENSOR_MIN_HEIGHT)
2235         mf->height = SENSOR_MIN_HEIGHT;
2236
2237     if (mf->width > SENSOR_MAX_WIDTH)
2238         mf->width = SENSOR_MAX_WIDTH;
2239     else if (mf->width < SENSOR_MIN_WIDTH)
2240         mf->width = SENSOR_MIN_WIDTH;
2241
2242     set_w = mf->width;
2243     set_h = mf->height;
2244         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END))
2245         {
2246         set_w = 176;
2247         set_h = 144;
2248         }
2249         else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END))
2250     {
2251         set_w = 320;
2252         set_h = 240;
2253     }
2254     else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END))
2255     {
2256         set_w = 352;
2257         set_h = 288;
2258     }
2259     else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END))
2260     {
2261         set_w = 640;
2262         set_h = 480;
2263     }
2264     else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END))
2265     {
2266         set_w = 800;
2267         set_h = 600;
2268     }
2269         else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END))
2270     {
2271         set_w = 1024;
2272         set_h = 768;
2273     }
2274         else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END))
2275     {
2276         set_w = 1280;
2277         set_h = 720;
2278     }
2279     else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END))
2280     {
2281         set_w = 1280;
2282         set_h = 1024;
2283     }
2284     else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END))
2285     {
2286         set_w = 1600;
2287         set_h = 1200;
2288     }
2289     else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END))
2290     {
2291         set_w = 1920;
2292         set_h = 1080;
2293     }
2294         else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END))
2295     {
2296         set_w = 2048;
2297         set_h = 1536;
2298     }
2299         else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END))
2300     {
2301         set_w = 2592;
2302         set_h = 1944;
2303     }
2304
2305         mf->width = set_w;
2306         mf->height = set_h;
2307     
2308     mf->colorspace = fmt->colorspace;
2309     
2310     return ret;
2311 }
2312
2313  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2314 {
2315     struct i2c_client *client = v4l2_get_subdevdata(sd);
2316
2317     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2318         return -EINVAL;
2319
2320     if (id->match.addr != client->addr)
2321         return -ENODEV;
2322
2323     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2324     id->revision = 0;
2325
2326     return 0;
2327 }
2328 #if CONFIG_SENSOR_Brightness
2329 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2330 {
2331     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2332
2333     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2334     {
2335         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2336         {
2337             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2338             {
2339                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2340                 return -EINVAL;
2341             }
2342             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2343             return 0;
2344         }
2345     }
2346         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2347     return -EINVAL;
2348 }
2349 #endif
2350 #if CONFIG_SENSOR_Effect
2351 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2352 {
2353     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2354
2355     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2356     {
2357         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2358         {
2359             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2360             {
2361                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2362                 return -EINVAL;
2363             }
2364             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2365             return 0;
2366         }
2367     }
2368         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2369     return -EINVAL;
2370 }
2371 #endif
2372 #if CONFIG_SENSOR_Exposure
2373 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2374 {
2375     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2376
2377     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2378     {
2379         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2380         {
2381             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2382             {
2383                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2384                 return -EINVAL;
2385             }
2386             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2387             return 0;
2388         }
2389     }
2390         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2391     return -EINVAL;
2392 }
2393 #endif
2394 #if CONFIG_SENSOR_Saturation
2395 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2396 {
2397     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2398
2399     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2400     {
2401         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2402         {
2403             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2404             {
2405                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2406                 return -EINVAL;
2407             }
2408             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2409             return 0;
2410         }
2411     }
2412     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2413     return -EINVAL;
2414 }
2415 #endif
2416 #if CONFIG_SENSOR_Contrast
2417 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2418 {
2419     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2420
2421     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2422     {
2423         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2424         {
2425             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2426             {
2427                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2428                 return -EINVAL;
2429             }
2430             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2431             return 0;
2432         }
2433     }
2434     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2435     return -EINVAL;
2436 }
2437 #endif
2438 #if CONFIG_SENSOR_Mirror
2439 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2440 {
2441     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2442
2443     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2444     {
2445         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2446         {
2447             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2448             {
2449                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2450                 return -EINVAL;
2451             }
2452             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2453             return 0;
2454         }
2455     }
2456     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2457     return -EINVAL;
2458 }
2459 #endif
2460 #if CONFIG_SENSOR_Flip
2461 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2462 {
2463     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2464
2465     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2466     {
2467         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2468         {
2469             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2470             {
2471                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2472                 return -EINVAL;
2473             }
2474             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2475             return 0;
2476         }
2477     }
2478     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2479     return -EINVAL;
2480 }
2481 #endif
2482 #if CONFIG_SENSOR_Scene
2483 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2484 {
2485     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2486
2487     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2488     {
2489         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2490         {
2491             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2492             {
2493                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2494                 return -EINVAL;
2495             }
2496             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2497             return 0;
2498         }
2499     }
2500     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2501     return -EINVAL;
2502 }
2503 #endif
2504 #if CONFIG_SENSOR_WhiteBalance
2505 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2506 {
2507     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2508
2509     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2510     {
2511         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2512         {
2513             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2514             {
2515                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2516                 return -EINVAL;
2517             }
2518             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2519             return 0;
2520         }
2521     }
2522         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2523     return -EINVAL;
2524 }
2525 #endif
2526 #if CONFIG_SENSOR_DigitalZoom
2527 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2528 {
2529     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2530     struct sensor *sensor = to_sensor(client);
2531         const struct v4l2_queryctrl *qctrl_info;
2532     int digitalzoom_cur, digitalzoom_total;
2533
2534         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2535         if (qctrl_info)
2536                 return -EINVAL;
2537
2538     digitalzoom_cur = sensor->info_priv.digitalzoom;
2539     digitalzoom_total = qctrl_info->maximum;
2540
2541     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2542     {
2543         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2544         return -EINVAL;
2545     }
2546
2547     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2548     {
2549         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2550         return -EINVAL;
2551     }
2552
2553     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2554     {
2555         *value = digitalzoom_total - digitalzoom_cur;
2556     }
2557
2558     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2559     {
2560         *value = 0 - digitalzoom_cur;
2561     }
2562
2563     digitalzoom_cur += *value;
2564
2565     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2566     {
2567         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
2568         {
2569             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2570             return -EINVAL;
2571         }
2572         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
2573         return 0;
2574     }
2575
2576     return -EINVAL;
2577 }
2578 #endif
2579 #if CONFIG_SENSOR_Flash
2580 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2581 {    
2582     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2583         if (value == 3) {       /* ddl@rock-chips.com: torch */
2584             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2585         } else {
2586             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2587         }
2588         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2589         return 0;
2590     }
2591     
2592         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2593     return -EINVAL;
2594 }
2595 #endif
2596 #if CONFIG_SENSOR_Focus
2597 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2598 {
2599         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2600     struct sensor *sensor = to_sensor(client);
2601         const struct v4l2_queryctrl *qctrl_info;
2602         int ret = 0;
2603
2604         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2605         if (!qctrl_info)
2606                 return -EINVAL;
2607
2608         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2609                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2610
2611                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2612                 } else {
2613                         ret = -EINVAL;
2614                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2615                 }
2616         } else {
2617                 ret = -EACCES;
2618                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2619                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2620         }
2621
2622 sensor_set_focus_absolute_end:
2623         return ret;
2624 }
2625 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2626 {
2627         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2628         struct sensor *sensor = to_sensor(client);
2629         const struct v4l2_queryctrl *qctrl_info;
2630         int ret = 0;
2631
2632         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2633         if (!qctrl_info)
2634                 return -EINVAL;
2635
2636         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2637                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2638
2639                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2640                 } else {
2641                         ret = -EINVAL;
2642                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2643                 }
2644         } else {
2645                 ret = -EACCES;
2646                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2647                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2648         }
2649 sensor_set_focus_relative_end:
2650         return ret;
2651 }
2652
2653 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2654 {
2655         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2656         struct sensor *sensor = to_sensor(client);
2657         int ret = 0;
2658
2659         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
2660                 switch (value)
2661                 {
2662                         case SENSOR_AF_MODE_AUTO:
2663                         {
2664                                 ret = sensor_af_single(client);
2665                                 break;
2666                         }
2667
2668                         case SENSOR_AF_MODE_MACRO:
2669                         {
2670                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2671                                 break;
2672                         }
2673
2674                         case SENSOR_AF_MODE_INFINITY:
2675                         {
2676                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2677                                 break;
2678                         }
2679
2680                         case SENSOR_AF_MODE_CONTINUOUS:
2681                         {
2682                                 ret = sensor_af_const(client);
2683                                 break;
2684                         }
2685                         default:
2686                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2687                                 break;
2688
2689                 }
2690
2691                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2692         } else {
2693                 ret = -EACCES;
2694                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2695                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2696         }
2697
2698         return ret;
2699 }
2700 #endif
2701 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2702 {
2703     struct i2c_client *client = v4l2_get_subdevdata(sd);
2704     struct sensor *sensor = to_sensor(client);
2705     const struct v4l2_queryctrl *qctrl;
2706
2707     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2708
2709     if (!qctrl)
2710     {
2711         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2712         return -EINVAL;
2713     }
2714
2715     switch (ctrl->id)
2716     {
2717         case V4L2_CID_BRIGHTNESS:
2718             {
2719                 ctrl->value = sensor->info_priv.brightness;
2720                 break;
2721             }
2722         case V4L2_CID_SATURATION:
2723             {
2724                 ctrl->value = sensor->info_priv.saturation;
2725                 break;
2726             }
2727         case V4L2_CID_CONTRAST:
2728             {
2729                 ctrl->value = sensor->info_priv.contrast;
2730                 break;
2731             }
2732         case V4L2_CID_DO_WHITE_BALANCE:
2733             {
2734                 ctrl->value = sensor->info_priv.whiteBalance;
2735                 break;
2736             }
2737         case V4L2_CID_EXPOSURE:
2738             {
2739                 ctrl->value = sensor->info_priv.exposure;
2740                 break;
2741             }
2742         case V4L2_CID_HFLIP:
2743             {
2744                 ctrl->value = sensor->info_priv.mirror;
2745                 break;
2746             }
2747         case V4L2_CID_VFLIP:
2748             {
2749                 ctrl->value = sensor->info_priv.flip;
2750                 break;
2751             }
2752         default :
2753                 break;
2754     }
2755     return 0;
2756 }
2757
2758
2759
2760 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2761 {
2762     struct i2c_client *client = v4l2_get_subdevdata(sd);
2763     struct sensor *sensor = to_sensor(client);
2764     struct soc_camera_device *icd = client->dev.platform_data;
2765     const struct v4l2_queryctrl *qctrl;
2766
2767
2768     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2769
2770     if (!qctrl)
2771     {
2772         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2773         return -EINVAL;
2774     }
2775
2776     switch (ctrl->id)
2777     {
2778 #if CONFIG_SENSOR_Brightness
2779         case V4L2_CID_BRIGHTNESS:
2780             {
2781                 if (ctrl->value != sensor->info_priv.brightness)
2782                 {
2783                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2784                     {
2785                         return -EINVAL;
2786                     }
2787                     sensor->info_priv.brightness = ctrl->value;
2788                 }
2789                 break;
2790             }
2791 #endif
2792 #if CONFIG_SENSOR_Exposure
2793         case V4L2_CID_EXPOSURE:
2794             {
2795                 if (ctrl->value != sensor->info_priv.exposure)
2796                 {
2797                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2798                     {
2799                         return -EINVAL;
2800                     }
2801                     sensor->info_priv.exposure = ctrl->value;
2802                 }
2803                 break;
2804             }
2805 #endif
2806 #if CONFIG_SENSOR_Saturation
2807         case V4L2_CID_SATURATION:
2808             {
2809                 if (ctrl->value != sensor->info_priv.saturation)
2810                 {
2811                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2812                     {
2813                         return -EINVAL;
2814                     }
2815                     sensor->info_priv.saturation = ctrl->value;
2816                 }
2817                 break;
2818             }
2819 #endif
2820 #if CONFIG_SENSOR_Contrast
2821         case V4L2_CID_CONTRAST:
2822             {
2823                 if (ctrl->value != sensor->info_priv.contrast)
2824                 {
2825                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2826                     {
2827                         return -EINVAL;
2828                     }
2829                     sensor->info_priv.contrast = ctrl->value;
2830                 }
2831                 break;
2832             }
2833 #endif
2834 #if CONFIG_SENSOR_WhiteBalance
2835         case V4L2_CID_DO_WHITE_BALANCE:
2836             {
2837                 if (ctrl->value != sensor->info_priv.whiteBalance)
2838                 {
2839                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2840                     {
2841                         return -EINVAL;
2842                     }
2843                     sensor->info_priv.whiteBalance = ctrl->value;
2844                 }
2845                 break;
2846             }
2847 #endif
2848 #if CONFIG_SENSOR_Mirror
2849         case V4L2_CID_HFLIP:
2850             {
2851                 if (ctrl->value != sensor->info_priv.mirror)
2852                 {
2853                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2854                         return -EINVAL;
2855                     sensor->info_priv.mirror = ctrl->value;
2856                 }
2857                 break;
2858             }
2859 #endif
2860 #if CONFIG_SENSOR_Flip
2861         case V4L2_CID_VFLIP:
2862             {
2863                 if (ctrl->value != sensor->info_priv.flip)
2864                 {
2865                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2866                         return -EINVAL;
2867                     sensor->info_priv.flip = ctrl->value;
2868                 }
2869                 break;
2870             }
2871 #endif
2872         default:
2873             break;
2874     }
2875
2876     return 0;
2877 }
2878 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2879 {
2880     const struct v4l2_queryctrl *qctrl;
2881     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2882     struct sensor *sensor = to_sensor(client);
2883
2884     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2885
2886     if (!qctrl)
2887     {
2888         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2889         return -EINVAL;
2890     }
2891
2892     switch (ext_ctrl->id)
2893     {
2894         case V4L2_CID_SCENE:
2895             {
2896                 ext_ctrl->value = sensor->info_priv.scene;
2897                 break;
2898             }
2899         case V4L2_CID_EFFECT:
2900             {
2901                 ext_ctrl->value = sensor->info_priv.effect;
2902                 break;
2903             }
2904         case V4L2_CID_ZOOM_ABSOLUTE:
2905             {
2906                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2907                 break;
2908             }
2909         case V4L2_CID_ZOOM_RELATIVE:
2910             {
2911                 return -EINVAL;
2912             }
2913         case V4L2_CID_FOCUS_ABSOLUTE:
2914             {
2915                 ext_ctrl->value = sensor->info_priv.focus;
2916                 break;
2917             }
2918         case V4L2_CID_FOCUS_RELATIVE:
2919             {
2920                 return -EINVAL;
2921             }
2922         case V4L2_CID_FLASH:
2923             {
2924                 ext_ctrl->value = sensor->info_priv.flash;
2925                 break;
2926             }
2927         default :
2928             break;
2929     }
2930     return 0;
2931 }
2932 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2933 {
2934     const struct v4l2_queryctrl *qctrl;
2935     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2936     struct sensor *sensor = to_sensor(client);
2937     int val_offset;
2938
2939     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2940
2941     if (!qctrl)
2942     {
2943         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2944         return -EINVAL;
2945     }
2946
2947         val_offset = 0;
2948     switch (ext_ctrl->id)
2949     {
2950 #if CONFIG_SENSOR_Scene
2951         case V4L2_CID_SCENE:
2952             {
2953                 if (ext_ctrl->value != sensor->info_priv.scene)
2954                 {
2955                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2956                         return -EINVAL;
2957                     sensor->info_priv.scene = ext_ctrl->value;
2958                 }
2959                 break;
2960             }
2961 #endif
2962 #if CONFIG_SENSOR_Effect
2963         case V4L2_CID_EFFECT:
2964             {
2965                 if (ext_ctrl->value != sensor->info_priv.effect)
2966                 {
2967                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2968                         return -EINVAL;
2969                     sensor->info_priv.effect= ext_ctrl->value;
2970                 }
2971                 break;
2972             }
2973 #endif
2974 #if CONFIG_SENSOR_DigitalZoom
2975         case V4L2_CID_ZOOM_ABSOLUTE:
2976             {
2977                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2978                     return -EINVAL;
2979
2980                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2981                 {
2982                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2983
2984                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2985                         return -EINVAL;
2986                     sensor->info_priv.digitalzoom += val_offset;
2987
2988                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2989                 }
2990
2991                 break;
2992             }
2993         case V4L2_CID_ZOOM_RELATIVE:
2994             {
2995                 if (ext_ctrl->value)
2996                 {
2997                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2998                         return -EINVAL;
2999                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3000
3001                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3002                 }
3003                 break;
3004             }
3005 #endif
3006 #if CONFIG_SENSOR_Focus
3007         case V4L2_CID_FOCUS_ABSOLUTE:
3008             {
3009                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3010                     return -EINVAL;
3011
3012                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
3013                                         if (ext_ctrl->value == qctrl->minimum) {
3014                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
3015                                         } else if (ext_ctrl->value == qctrl->maximum) {
3016                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
3017                                         } else {
3018                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
3019                                         }
3020                                 }
3021
3022                 break;
3023             }
3024         case V4L2_CID_FOCUS_RELATIVE:
3025             {
3026                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3027                     return -EINVAL;
3028
3029                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
3030                 break;
3031             }
3032                 case V4L2_CID_FOCUS_AUTO:
3033                         {
3034                                 if (ext_ctrl->value == 1) {
3035                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
3036                                                 return -EINVAL;
3037                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
3038                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
3039                                         if (ext_ctrl->value == 0)
3040                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3041                                 }
3042                                 break;
3043                         }
3044                 case V4L2_CID_FOCUS_CONTINUOUS:
3045                         {
3046                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
3047                                         if (ext_ctrl->value == 1) {
3048                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
3049                                                         return -EINVAL;
3050                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
3051                                         }
3052                                 } else {
3053                                         if (ext_ctrl->value == 0)
3054                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
3055                                 }
3056                                 break;
3057                         }
3058 #endif
3059 #if CONFIG_SENSOR_Flash
3060         case V4L2_CID_FLASH:
3061             {
3062                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3063                     return -EINVAL;
3064                 sensor->info_priv.flash = ext_ctrl->value;
3065
3066                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3067                 break;
3068             }
3069 #endif
3070         default:
3071             break;
3072     }
3073
3074     return 0;
3075 }
3076
3077 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3078 {
3079     struct i2c_client *client = v4l2_get_subdevdata(sd);
3080     struct soc_camera_device *icd = client->dev.platform_data;
3081     int i, error_cnt=0, error_idx=-1;
3082
3083
3084     for (i=0; i<ext_ctrl->count; i++) {
3085         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3086             error_cnt++;
3087             error_idx = i;
3088         }
3089     }
3090
3091     if (error_cnt > 1)
3092         error_idx = ext_ctrl->count;
3093
3094     if (error_idx != -1) {
3095         ext_ctrl->error_idx = error_idx;
3096         return -EINVAL;
3097     } else {
3098         return 0;
3099     }
3100 }
3101
3102 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3103 {
3104     struct i2c_client *client = v4l2_get_subdevdata(sd);
3105     struct soc_camera_device *icd = client->dev.platform_data;
3106     int i, error_cnt=0, error_idx=-1;
3107
3108     for (i=0; i<ext_ctrl->count; i++) {
3109         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3110             error_cnt++;
3111             error_idx = i;
3112         }
3113     }
3114
3115     if (error_cnt > 1)
3116         error_idx = ext_ctrl->count;
3117
3118     if (error_idx != -1) {
3119         ext_ctrl->error_idx = error_idx;
3120         return -EINVAL;
3121     } else {
3122         return 0;
3123     }
3124 }
3125
3126 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
3127 {
3128         struct i2c_client *client = v4l2_get_subdevdata(sd);
3129     struct sensor *sensor = to_sensor(client);
3130
3131         if (enable == 1) {
3132                 sensor->info_priv.enable = 1;
3133         } else if (enable == 0) {
3134                 sensor->info_priv.enable = 0;
3135         }
3136     
3137         return 0;
3138 }
3139
3140 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3141  * this wasn't our capture interface, so, we wait for the right one */
3142 static int sensor_video_probe(struct soc_camera_device *icd,
3143                                struct i2c_client *client)
3144 {
3145     int ret;
3146 #if (SENSOR_ID_REG != SEQUENCE_END)
3147     u16 pid = 0;
3148 #endif
3149
3150     struct sensor *sensor = to_sensor(client);
3151
3152     /* We must have a parent by now. And it cannot be a wrong one.
3153      * So this entire test is completely redundant. */
3154     if (!icd->dev.parent ||
3155             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3156                 return -ENODEV;
3157
3158         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3159                 ret = -ENODEV;
3160                 goto sensor_video_probe_err;
3161         }
3162
3163     /* soft reset */
3164 #if (SENSOR_RESET_REG != SEQUENCE_END)
3165     ret = sensor_write(client, SENSOR_RESET_REG, SENSOR_RESET_VAL);
3166     if (ret != 0) {
3167         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
3168         ret = -ENODEV;
3169                 goto sensor_INIT_ERR;
3170     }
3171
3172     mdelay(5);  //delay 5 microseconds
3173 #endif
3174
3175         /* check if it is an sensor sensor */
3176 #if (SENSOR_ID_REG != SEQUENCE_END)
3177     ret = sensor_read(client, SENSOR_ID_REG, &pid);
3178     if (ret != 0) {
3179         SENSOR_TR("read chip id failed\n");
3180         ret = -ENODEV;
3181         goto sensor_video_probe_err;
3182     }
3183
3184     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
3185 #else
3186         pid = SENSOR_ID;
3187 #endif
3188
3189     if (pid == SENSOR_ID) {
3190         sensor->model = SENSOR_V4L2_IDENT;
3191     } else {
3192         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3193         ret = -ENODEV;
3194         goto sensor_video_probe_err;
3195     }
3196
3197
3198     return 0;
3199
3200 sensor_video_probe_err:
3201     return ret;
3202 }
3203 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3204 {
3205         struct i2c_client *client = v4l2_get_subdevdata(sd);
3206     struct soc_camera_device *icd = client->dev.platform_data;
3207     struct sensor *sensor = to_sensor(client);
3208     int ret = 0;
3209 #if CONFIG_SENSOR_Flash 
3210     int i;
3211 #endif
3212     
3213         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3214         switch (cmd)
3215         {
3216                 case RK29_CAM_SUBDEV_DEACTIVATE:
3217                 {
3218                         sensor_deactivate(client);
3219                         break;
3220                 }
3221                 case RK29_CAM_SUBDEV_IOREQUEST:
3222                 {
3223                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
3224             if (sensor->sensor_io_request != NULL) { 
3225                 sensor->sensor_gpio_res = NULL;
3226                 for (i=0; i<RK29_CAM_SUPPORT_NUMS;i++) {
3227                     if (sensor->sensor_io_request->gpio_res[i].dev_name && 
3228                         (strcmp(sensor->sensor_io_request->gpio_res[i].dev_name, dev_name(icd->pdev)) == 0)) {
3229                         sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[i];
3230                     }
3231                 }
3232                 if (sensor->sensor_gpio_res == NULL) {
3233                     SENSOR_TR("%s %s obtain gpio resource failed when RK29_CAM_SUBDEV_IOREQUEST \n",SENSOR_NAME_STRING(),__FUNCTION__);
3234                     ret = -EINVAL;
3235                     goto sensor_ioctl_end;
3236                 }
3237             } else {
3238                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3239                 ret = -EINVAL;
3240                 goto sensor_ioctl_end;
3241             }
3242             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3243                for this project */
3244             #if CONFIG_SENSOR_Flash     
3245                 if (sensor->sensor_gpio_res) { 
3246                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3247                     for (i = 0; i < icd->ops->num_controls; i++) {
3248                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3249                                         //memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));  
3250                               sensor_controls[i].id=0xffff;                             
3251                                 }
3252                     }
3253                     sensor->info_priv.flash = 0xff;
3254                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3255                 }else{ //two cameras are the same,need to deal diffrently ,zyc
3256                     for (i = 0; i < icd->ops->num_controls; i++) {
3257                            if(0xffff == icd->ops->controls[i].id){
3258                               sensor_controls[i].id=V4L2_CID_FLASH;
3259                            }               
3260                     }
3261                 }
3262                 }
3263             #endif
3264                         break;
3265                 }
3266                 default:
3267                 {
3268                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3269                         break;
3270                 }
3271         }
3272 sensor_ioctl_end:
3273         return ret;
3274
3275 }
3276 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3277                             enum v4l2_mbus_pixelcode *code)
3278 {
3279         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3280                 return -EINVAL;
3281
3282         *code = sensor_colour_fmts[index].code;
3283         return 0;
3284 }
3285 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3286         .init           = sensor_init,
3287         .g_ctrl         = sensor_g_control,
3288         .s_ctrl         = sensor_s_control,
3289         .g_ext_ctrls          = sensor_g_ext_controls,
3290         .s_ext_ctrls          = sensor_s_ext_controls,
3291         .g_chip_ident   = sensor_g_chip_ident,
3292         .ioctl = sensor_ioctl,
3293 };
3294
3295 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3296         .s_mbus_fmt     = sensor_s_fmt,
3297         .g_mbus_fmt     = sensor_g_fmt,
3298         .try_mbus_fmt   = sensor_try_fmt,
3299         .enum_mbus_fmt  = sensor_enum_fmt,
3300 };
3301
3302 static struct v4l2_subdev_ops sensor_subdev_ops = {
3303         .core   = &sensor_subdev_core_ops,
3304         .video = &sensor_subdev_video_ops,
3305 };
3306
3307 static int sensor_probe(struct i2c_client *client,
3308                          const struct i2c_device_id *did)
3309 {
3310     struct sensor *sensor;
3311     struct soc_camera_device *icd = client->dev.platform_data;
3312     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3313     struct soc_camera_link *icl;
3314     int ret;
3315
3316     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3317     if (!icd) {
3318         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3319         return -EINVAL;
3320     }
3321
3322     icl = to_soc_camera_link(icd);
3323     if (!icl) {
3324         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3325         return -EINVAL;
3326     }
3327
3328     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3329         dev_warn(&adapter->dev,
3330                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3331         return -EIO;
3332     }
3333
3334     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3335     if (!sensor)
3336         return -ENOMEM;
3337
3338     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3339
3340     /* Second stage probe - when a capture adapter is there */
3341     icd->ops            = &sensor_ops;
3342     sensor->info_priv.fmt = sensor_colour_fmts[0];
3343         #if CONFIG_SENSOR_I2C_NOSCHED
3344         atomic_set(&sensor->tasklock_cnt,0);
3345         #endif
3346
3347     ret = sensor_video_probe(icd, client);
3348     if (ret < 0) {
3349         icd->ops = NULL;
3350         i2c_set_clientdata(client, NULL);
3351         kfree(sensor);
3352                 sensor = NULL;
3353     }
3354     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3355     return ret;
3356 }
3357
3358 static int sensor_remove(struct i2c_client *client)
3359 {
3360     struct sensor *sensor = to_sensor(client);
3361     struct soc_camera_device *icd = client->dev.platform_data;
3362
3363     icd->ops = NULL;
3364     i2c_set_clientdata(client, NULL);
3365     client->driver = NULL;
3366     kfree(sensor);
3367         sensor = NULL;
3368     return 0;
3369 }
3370
3371 static const struct i2c_device_id sensor_id[] = {
3372         {SENSOR_NAME_STRING(), 0 },
3373         { }
3374 };
3375 MODULE_DEVICE_TABLE(i2c, sensor_id);
3376
3377 static struct i2c_driver sensor_i2c_driver = {
3378         .driver = {
3379                 .name = SENSOR_NAME_STRING(),
3380         },
3381         .probe          = sensor_probe,
3382         .remove         = sensor_remove,
3383         .id_table       = sensor_id,
3384 };
3385
3386 static int __init sensor_mod_init(void)
3387 {
3388     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3389     return i2c_add_driver(&sensor_i2c_driver);
3390 }
3391
3392 static void __exit sensor_mod_exit(void)
3393 {
3394     i2c_del_driver(&sensor_i2c_driver);
3395 }
3396
3397 device_initcall_sync(sensor_mod_init);
3398 module_exit(sensor_mod_exit);
3399
3400 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3401 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3402 MODULE_LICENSE("GPL");
3403
3404
3405