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