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