camera rk30:commit v0.2.6,support two cif controls.
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / mt9d113.c
1 /*
2  * Driver for OV5642 CMOS Image Sensor from OmniVision
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
15 #include <linux/platform_device.h>
16 #include <linux/delay.h>
17 #include <linux/circ_buf.h>
18 #include <linux/hardirq.h>
19 #include <linux/miscdevice.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/soc_camera.h>
23 #include <plat/rk_camera.h>
24 #include <linux/vmalloc.h>
25 #include "mt9d113.h"
26
27 static int debug;
28 module_param(debug, int, S_IRUGO|S_IWUSR);
29
30 #define dprintk(level, fmt, arg...) do {                        \
31         if (debug >= level)                                     \
32         printk(KERN_WARNING fmt , ## arg); } while (0)
33
34 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
35 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
36
37 #define _CONS(a,b) a##b
38 #define CONS(a,b) _CONS(a,b)
39
40 #define __STR(x) #x
41 #define _STR(x) __STR(x)
42 #define STR(x) _STR(x)
43
44 #define MIN(x,y)   ((x<y) ? x: y)
45 #define MAX(x,y)    ((x>y) ? x: y)
46
47 /* Sensor Driver Configuration */
48 #define SENSOR_NAME RK29_CAM_SENSOR_MT9D113
49 #define SENSOR_V4L2_IDENT V4L2_IDENT_MT9D113
50 #define SENSOR_ID 0x2580
51 #define SENSOR_ID_REG 0x00
52 #define SENSOR_RESET_REG SEQUENCE_END
53 #define SENSOR_RESET_VAL 0
54 #define SENSOR_DERESET_VAL 0
55 #define SENSOR_RESET_REG_LEN  WORD_LEN
56 #define SENSOR_MIN_WIDTH    800
57 #define SENSOR_MIN_HEIGHT   600
58 #define SENSOR_MAX_WIDTH    1600
59 #define SENSOR_MAX_HEIGHT   1200
60 #define SENSOR_INIT_WIDTH       sensor_init_width                       /* Sensor pixel size for sensor_init_data array */
61 #define SENSOR_INIT_HEIGHT      sensor_init_height
62 #define SENSOR_INIT_WINSEQADR  sensor_init_winseq_p
63 #define SENSOR_INIT_PIXFMT sensor_init_pixelcode
64 #define SENSOR_BUS_PARAM  sensor_init_busparam
65
66 #define CONFIG_SENSOR_WhiteBalance      0
67 #define CONFIG_SENSOR_Brightness        0
68 #define CONFIG_SENSOR_Contrast      0
69 #define CONFIG_SENSOR_Saturation    0
70 #define CONFIG_SENSOR_Effect        0
71 #define CONFIG_SENSOR_Scene         0
72 #define CONFIG_SENSOR_DigitalZoom   0
73 #define CONFIG_SENSOR_Exposure      0
74 #define CONFIG_SENSOR_Flash         0
75 #define CONFIG_SENSOR_Mirror        0
76 #define CONFIG_SENSOR_Flip          0
77 #define CONFIG_SENSOR_Focus         0
78
79 #define CONFIG_SENSOR_I2C_SPEED     100000       /* Hz */
80 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
81 #define CONFIG_SENSOR_I2C_NOSCHED   0
82 #define CONFIG_SENSOR_I2C_RDWRCHK   1
83
84 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
85 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
86 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
87 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
88 #define COLOR_TEMPERATURE_OFFICE_DN     3500
89 #define COLOR_TEMPERATURE_OFFICE_UP     5000
90 #define COLOR_TEMPERATURE_HOME_DN       2500
91 #define COLOR_TEMPERATURE_HOME_UP       3500
92
93 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
94 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
95
96 #define SENSOR_AF_IS_ERR    (0x00<<0)
97 #define SENSOR_AF_IS_OK         (0x01<<0)
98
99 #if CONFIG_SENSOR_Focus
100 #define SENSOR_AF_MODE_INFINITY    0
101 #define SENSOR_AF_MODE_MACRO       1
102 #define SENSOR_AF_MODE_FIXED       2
103 #define SENSOR_AF_MODE_AUTO        3
104 #define SENSOR_AF_MODE_CONTINUOUS  4
105 #define SENSOR_AF_MODE_CLOSE       5
106 #endif
107
108 /* init 800x600 SVGA */
109 static struct reginfo sensor_init_data[] =
110 {
111     #if 0
112     //[Step2-PLL_Timing]
113     //for 24MHz input, VCO=MAX PCLK=76.8MHz
114     { 0x0014, 0x21F9, WORD_LEN, 0},     //PLL Control: BYPASS PLL = 8697
115     { 0x0010, 0x0110, WORD_LEN, 0},     //PLL Dividers = 272
116     { 0x0012, 0x1FF7, WORD_LEN, 0},      //PLL P Dividers = 8183
117     { 0x0014, 0x21FB, WORD_LEN, 0},     //PLL Control: PLL_ENABLE on = 8699
118     { 0x0014, 0x20FB, WORD_LEN, 0},     //PLL Control: SEL_LOCK_DET on = 8443
119
120     { SEQUENCE_WAIT_US, 1, WORD_LEN, 0},                // Allow PLL to lock
121
122     { 0x0014, 0x20FA, WORD_LEN, 0},     //PLL Control: PLL_BYPASS off = 8442
123     { 0x0018, 0x402D, WORD_LEN, 0},
124
125     //GO
126     { 0x0018, 0x402C, WORD_LEN, 0},
127
128     { SEQUENCE_WAIT_US, 100, WORD_LEN, 0},
129
130     { 0x321C, 0x0003, WORD_LEN, 0},     //By Pass TxFIFO = 3
131     { 0x098C, 0x2703, WORD_LEN, 0},     //Output Width (A)
132     { 0x0990, 0x0320, WORD_LEN, 0},     //      = 800
133     { 0x98C, 0x2705, WORD_LEN, 0},        //Output Height (A)
134     { 0x990, 0x0258    , WORD_LEN, 0},   //      = 600
135     { 0x98C, 0x2707, WORD_LEN, 0},        //Output Width (B)
136     { 0x990, 0x0640    , WORD_LEN, 0},   //      = 1600
137     { 0x98C, 0x2709     , WORD_LEN, 0},   //Output Height (B)
138     { 0x990, 0x04B0    , WORD_LEN, 0},   //      = 1200
139     { 0x98C, 0x270D  , WORD_LEN, 0},     //Row Start (A)
140     { 0x990, 0x000      , WORD_LEN, 0},   //      = 0
141     { 0x98C, 0x270F   , WORD_LEN, 0},     //Column Start (A)
142     { 0x990, 0x000      , WORD_LEN, 0},   //      = 0
143     { 0x98C, 0x2711    , WORD_LEN, 0},    //Row End (A)
144     { 0x990, 0x4BD     , WORD_LEN, 0},   //      = 1213
145     { 0x98C, 0x2713  , WORD_LEN, 0},      //Column End (A)
146     { 0x990, 0x64D      , WORD_LEN, 0},   //      = 1613
147     { 0x98C, 0x2715  , WORD_LEN, 0},      //Row Speed (A)
148     { 0x990, 0x0111    , WORD_LEN, 0},   //      = 273
149     { 0x98C, 0x2717  , WORD_LEN, 0},      //Read Mode (A)
150 //miaozh modify    
151     { 0x0990, 0x046C      , WORD_LEN, 0}, //  MCU_DATA_0
152 //    { 0x0990, 0x046E      , WORD_LEN, 0}, //  MCU_DATA_0
153 //    { 0x098C, 0xA103      , WORD_LEN, 0}, // MCU_ADDRESS [SEQ_CMD]
154 //    { 0x0990, 0x0006      , WORD_LEN, 0}, // MCU_DATA_0
155
156     { 0x98C, 0x2719  , WORD_LEN, 0},      //sensor_fine_correction (A)
157     { 0x990, 0x005A   , WORD_LEN, 0},   //      = 90
158     { 0x98C, 0x271B , WORD_LEN, 0},      //sensor_fine_IT_min (A)
159     { 0x990, 0x01BE    , WORD_LEN, 0},   //      = 446
160     { 0x98C, 0x271D , WORD_LEN, 0},      //sensor_fine_IT_max_margin (A)
161     { 0x990, 0x0131    , WORD_LEN, 0},   //      = 305
162     { 0x98C, 0x271F , WORD_LEN, 0},       //Frame Lines (A)
163     { 0x990, 0x02B3    , WORD_LEN, 0},   //      = 691
164     { 0x98C, 0x2721 , WORD_LEN, 0},       //Line Length (A)
165     { 0x990, 0x056D   , WORD_LEN, 0},   //      = 1389
166     { 0x98C, 0x2723 , WORD_LEN, 0},       //Row Start (B)
167     { 0x990, 0x004      , WORD_LEN, 0},   //      = 4
168     { 0x98C, 0x2725 , WORD_LEN, 0},       //Column Start (B)
169     { 0x990, 0x004      , WORD_LEN, 0},   //      = 4
170     { 0x98C, 0x2727 , WORD_LEN, 0},       //Row End (B)
171     { 0x990, 0x4BB      , WORD_LEN, 0},   //      = 1211
172     { 0x98C, 0x2729 , WORD_LEN, 0},       //Column End (B)
173     { 0x990, 0x64B      , WORD_LEN, 0},   //      = 1611
174     { 0x98C, 0x272B , WORD_LEN, 0},      //Row Speed (B)
175     { 0x990, 0x0111    , WORD_LEN, 0},   //      = 273
176     { 0x98C, 0x272D , WORD_LEN, 0},      //Read Mode (B)
177     { 0x990, 0x0024    , WORD_LEN, 0},   //      = 36
178     { 0x98C, 0x272F , WORD_LEN, 0},       //sensor_fine_correction (B)
179     { 0x990, 0x003A   , WORD_LEN, 0},   //      = 58
180     { 0x98C, 0x2731 , WORD_LEN, 0},       //sensor_fine_IT_min (B)
181     { 0x990, 0x00F6    , WORD_LEN, 0},   //      = 246
182     { 0x98C, 0x2733 , WORD_LEN, 0},       //sensor_fine_IT_max_margin (B)
183     { 0x990, 0x008B    , WORD_LEN, 0},   //      = 139
184     { 0x98C, 0x2735  , WORD_LEN, 0},      //Frame Lines (B)
185     { 0x990, 0x050D   , WORD_LEN, 0},   //      = 1293
186     { 0x98C, 0x2737  , WORD_LEN, 0},      //Line Length (B)
187     { 0x990, 0x0894    , WORD_LEN, 0},   //      = 2196
188     { 0x98C, 0x2739  , WORD_LEN, 0},      //Crop_X0 (A)
189     { 0x990, 0x0000    , WORD_LEN, 0},   //      = 0
190     { 0x98C, 0x273B  , WORD_LEN, 0},     //Crop_X1 (A)
191     { 0x990, 0x031F    , WORD_LEN, 0},   //      = 799
192     { 0x98C, 0x273D , WORD_LEN, 0},      //Crop_Y0 (A)
193     { 0x990, 0x0000    , WORD_LEN, 0},   //      = 0
194     { 0x98C, 0x273F , WORD_LEN, 0},       //Crop_Y1 (A)
195     { 0x990, 0x0257    , WORD_LEN, 0},   //      = 599
196     { 0x98C, 0x2747  , WORD_LEN, 0},      //Crop_X0 (B)
197     { 0x990, 0x0000    , WORD_LEN, 0},   //      = 0
198     { 0x98C, 0x2749   , WORD_LEN, 0},     //Crop_X1 (B)
199     { 0x990, 0x063F    , WORD_LEN, 0},   //      = 1599
200     { 0x98C, 0x274B   , WORD_LEN, 0},    //Crop_Y0 (B)
201     { 0x990, 0x0000    , WORD_LEN, 0},   //      = 0
202     { 0x98C, 0x274D   , WORD_LEN, 0},    //Crop_Y1 (B)
203     { 0x990, 0x04AF   , WORD_LEN, 0},   //      = 1199
204     { 0x98C, 0x222D   , WORD_LEN, 0},    //R9 Step
205     { 0x990, 0x0090    , WORD_LEN, 0},   //      = 144
206     { 0x98C, 0xA408   , WORD_LEN, 0},    //search_f1_50
207     { 0x990, 0x23     , WORD_LEN, 0},     //      = 35
208     { 0x98C, 0xA409   , WORD_LEN, 0},    //search_f2_50
209     { 0x990, 0x25         , WORD_LEN, 0},   //      = 37
210     { 0x98C, 0xA40A    , WORD_LEN, 0},  //search_f1_60
211     { 0x990, 0x2A        , WORD_LEN, 0},   //      = 42
212     { 0x98C, 0xA40B    , WORD_LEN, 0},   //search_f2_60
213     { 0x990, 0x2C        , WORD_LEN, 0},   //      = 44
214     { 0x98C, 0x2411     , WORD_LEN, 0},   //R9_Step_60 (A)
215     { 0x990, 0x0090    , WORD_LEN, 0},   //      = 144
216     { 0x98C, 0x2413    , WORD_LEN, 0},    //R9_Step_50 (A)
217     { 0x990, 0x00AD   , WORD_LEN, 0},   //      = 173
218     { 0x98C, 0x2415     , WORD_LEN, 0},   //R9_Step_60 (B)
219     { 0x990, 0x005B    , WORD_LEN, 0},   //      = 91
220     { 0x98C, 0x2417    , WORD_LEN, 0},    //R9_Step_50 (B)
221     { 0x990, 0x006D   , WORD_LEN, 0},   //      = 109
222     { 0x98C, 0xA404   , WORD_LEN, 0},    //FD Mode
223     { 0x990, 0x10         , WORD_LEN, 0},   //      = 16
224     { 0x98C, 0xA40D   , WORD_LEN, 0},   //Stat_min
225     { 0x990, 0x02         , WORD_LEN, 0},   //      = 2
226     { 0x98C, 0xA40E    , WORD_LEN, 0},   //Stat_max
227     { 0x990, 0x03         , WORD_LEN, 0},   //      = 3
228     { 0x98C, 0xA410   , WORD_LEN, 0},    //Min_amplitude
229     { 0x990, 0x0A        , WORD_LEN, 0},   //      = 10
230     { 0x98C, 0xA103   , WORD_LEN, 0},    //Refresh Sequencer Mode
231     { 0x990, 0x06         , WORD_LEN, 0},   //      = 6
232     //POLL_FIELD=SEQ_CMD, !=0, DELAY=10, TIMEOUT=100  // wait for command to be processed
233
234     { SEQUENCE_WAIT_US, 100, WORD_LEN, 0},
235
236     { 0x98C, 0xA103  , WORD_LEN, 0},     //Refresh Sequencer
237     { 0x990, 0x05   , WORD_LEN, 0},   //      = 5
238
239     { SEQUENCE_WAIT_US, 100, WORD_LEN, 0},
240
241     //POLL_FIELD=SEQ_CMD, !=0, DELAY=10, TIMEOUT=100  // wait for command to be processed
242     {SEQUENCE_END, 0x00}
243 #else
244     //[Step2-PLL_Timing]
245     //for 24MHz input, VCO=MAX PCLK=76.8MHz
246     { 0x001A, 0x0051, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
247     { 0x001A, 0x0050, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
248     { 0x0014, 0x2545, WORD_LEN, 0}, //  PLL_CONTROL
249     { 0x0010, 0x011C, WORD_LEN, 0}, //  PLL_DIVIDERS
250     { 0x0012, 0x1FF7, WORD_LEN, 0}, //  PLL_P_DIVIDERS
251     { 0x0014, 0x2547, WORD_LEN, 0}, //  PLL_CONTROL
252     { 0x0014, 0x2447, WORD_LEN, 0}, //  PLL_CONTROL
253     { 0x0014, 0x2047, WORD_LEN, 0}, //  PLL_CONTROL
254     { 0x0014, 0x2046, WORD_LEN, 0}, //  PLL_CONTROL
255     { 0x001A, 0x0050, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
256     { 0x0018, 0x4028      , WORD_LEN, 0}, //  STANDBY_CONTROL
257     { 0x321C, 0x0003      , WORD_LEN, 0}, //  OFIFO_CONTROL_STATUS
258     { 0x098C, 0x2703      , WORD_LEN, 0}, //  MCU_ADDRESS
259     { 0x0990, 0x0320      , WORD_LEN, 0}, //  MCU_DATA_0
260     { 0x098C, 0x2705      , WORD_LEN, 0}, //  MCU_ADDRESS
261     { 0x0990, 0x0258      , WORD_LEN, 0}, //  MCU_DATA_0
262     { 0x098C, 0x2707      , WORD_LEN, 0}, //  MCU_ADDRESS
263     { 0x0990, 0x0640      , WORD_LEN, 0}, //  MCU_DATA_0
264     { 0x098C, 0x2709      , WORD_LEN, 0}, //  MCU_ADDRESS
265     { 0x0990, 0x04B0      , WORD_LEN, 0}, //  MCU_DATA_0
266     { 0x098C, 0x270D      , WORD_LEN, 0}, //  MCU_ADDRESS
267     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
268     { 0x098C, 0x270F      , WORD_LEN, 0}, //  MCU_ADDRESS
269     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
270     { 0x098C, 0x2711      , WORD_LEN, 0}, //  MCU_ADDRESS
271     { 0x0990, 0x04BD      , WORD_LEN, 0}, //  MCU_DATA_0
272     { 0x098C, 0x2713      , WORD_LEN, 0}, //  MCU_ADDRESS
273     { 0x0990, 0x064D      , WORD_LEN, 0}, //  MCU_DATA_0
274     { 0x098C, 0x2715      , WORD_LEN, 0}, //  MCU_ADDRESS
275     { 0x0990, 0x0111      , WORD_LEN, 0}, //  MCU_DATA_0
276     { 0x098C, 0x2717      , WORD_LEN, 0}, //  MCU_ADDRESS
277 //miaozh modify    
278     { 0x0990, 0x046C      , WORD_LEN, 0}, //  MCU_DATA_0
279 //    { 0x0990, 0x046E      , WORD_LEN, 0}, //  MCU_DATA_0
280 //    { 0x098C, 0xA103      , WORD_LEN, 0}, // MCU_ADDRESS [SEQ_CMD]
281 //    { 0x0990, 0x0006      , WORD_LEN, 0}, // MCU_DATA_0
282
283     { 0x098C, 0x2719      , WORD_LEN, 0}, //  MCU_ADDRESS
284     { 0x0990, 0x005A      , WORD_LEN, 0}, //  MCU_DATA_0
285     { 0x098C, 0x271B      , WORD_LEN, 0}, //  MCU_ADDRESS
286     { 0x0990, 0x01BE      , WORD_LEN, 0}, //  MCU_DATA_0
287     { 0x098C, 0x271D      , WORD_LEN, 0}, //  MCU_ADDRESS
288     { 0x0990, 0x0131      , WORD_LEN, 0}, //  MCU_DATA_0
289     { 0x098C, 0x271F      , WORD_LEN, 0}, //  MCU_ADDRESS
290     { 0x0990, 0x02B3      , WORD_LEN, 0}, //  MCU_DATA_0
291     { 0x098C, 0x2721      , WORD_LEN, 0}, //  MCU_ADDRESS
292     { 0x0990, 0x09B0      , WORD_LEN, 0}, //  MCU_DATA_0
293     { 0x098C, 0x2723      , WORD_LEN, 0}, //  MCU_ADDRESS
294     { 0x0990, 0x0004      , WORD_LEN, 0}, //  MCU_DATA_0
295     { 0x098C, 0x2725      , WORD_LEN, 0}, //  MCU_ADDRESS
296     { 0x0990, 0x0004      , WORD_LEN, 0}, //  MCU_DATA_0
297     { 0x098C, 0x2727      , WORD_LEN, 0}, //  MCU_ADDRESS
298     { 0x0990, 0x04BB      , WORD_LEN, 0}, //  MCU_DATA_0
299     { 0x098C, 0x2729      , WORD_LEN, 0}, //  MCU_ADDRESS
300     { 0x0990, 0x064B      , WORD_LEN, 0}, //  MCU_DATA_0
301     { 0x098C, 0x272B      , WORD_LEN, 0}, //  MCU_ADDRESS
302     { 0x0990, 0x0111      , WORD_LEN, 0}, //  MCU_DATA_0
303     { 0x098C, 0x272D      , WORD_LEN, 0}, //  MCU_ADDRESS
304     { 0x0990, 0x0024      , WORD_LEN, 0}, //  MCU_DATA_0
305     { 0x098C, 0x272F      , WORD_LEN, 0}, //  MCU_ADDRESS
306     { 0x0990, 0x003A      , WORD_LEN, 0}, //  MCU_DATA_0
307     { 0x098C, 0x2731      , WORD_LEN, 0}, //  MCU_ADDRESS
308     { 0x0990, 0x00F6      , WORD_LEN, 0}, //  MCU_DATA_0
309     { 0x098C, 0x2733      , WORD_LEN, 0}, //  MCU_ADDRESS
310     { 0x0990, 0x008B      , WORD_LEN, 0}, //  MCU_DATA_0
311     { 0x098C, 0x2735      , WORD_LEN, 0}, //  MCU_ADDRESS
312     { 0x0990, 0x050D      , WORD_LEN, 0}, //  MCU_DATA_0
313     { 0x098C, 0x2737      , WORD_LEN, 0}, //  MCU_ADDRESS
314     { 0x0990, 0x0807      , WORD_LEN, 0}, //  MCU_DATA_0
315     { 0x098C, 0x2739      , WORD_LEN, 0}, //  MCU_ADDRESS
316     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
317     { 0x098C, 0x273B      , WORD_LEN, 0}, //  MCU_ADDRESS
318     { 0x0990, 0x031F      , WORD_LEN, 0}, //  MCU_DATA_0
319     { 0x098C, 0x273D      , WORD_LEN, 0}, //  MCU_ADDRESS
320     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
321     { 0x098C, 0x273F      , WORD_LEN, 0}, //  MCU_ADDRESS
322     { 0x0990, 0x0257      , WORD_LEN, 0}, //  MCU_DATA_0
323     { 0x098C, 0x2747      , WORD_LEN, 0}, //  MCU_ADDRESS
324     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
325     { 0x098C, 0x2749      , WORD_LEN, 0}, //  MCU_ADDRESS
326     { 0x0990, 0x063F      , WORD_LEN, 0}, //  MCU_DATA_0
327     { 0x098C, 0x274B      , WORD_LEN, 0}, //  MCU_ADDRESS
328     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
329     { 0x098C, 0x274D      , WORD_LEN, 0}, //  MCU_ADDRESS
330     { 0x0990, 0x04AF      , WORD_LEN, 0}, //  MCU_DATA_0
331     { 0x098C, 0x222D      , WORD_LEN, 0}, //  MCU_ADDRESS
332     { 0x0990, 0x008D      , WORD_LEN, 0}, //  MCU_DATA_0
333     { 0x098C, 0xA408      , WORD_LEN, 0}, //  MCU_ADDRESS
334     { 0x0990, 0x0022      , WORD_LEN, 0}, //  MCU_DATA_0
335     { 0x098C, 0xA409      , WORD_LEN, 0}, //  MCU_ADDRESS
336     { 0x0990, 0x0024      , WORD_LEN, 0}, //  MCU_DATA_0
337     { 0x098C, 0xA40A      , WORD_LEN, 0}, //  MCU_ADDRESS
338     { 0x0990, 0x0029      , WORD_LEN, 0}, //  MCU_DATA_0
339     { 0x098C, 0xA40B      , WORD_LEN, 0}, //  MCU_ADDRESS
340     { 0x0990, 0x002B      , WORD_LEN, 0}, //  MCU_DATA_0
341     { 0x098C, 0x2411      , WORD_LEN, 0}, //  MCU_ADDRESS
342     { 0x0990, 0x008D      , WORD_LEN, 0}, //  MCU_DATA_0
343     { 0x098C, 0x2413      , WORD_LEN, 0}, //  MCU_ADDRESS
344     { 0x0990, 0x00A9      , WORD_LEN, 0}, //  MCU_DATA_0
345     { 0x098C, 0x2415      , WORD_LEN, 0}, //  MCU_ADDRESS
346     { 0x0990, 0x00AA      , WORD_LEN, 0}, //  MCU_DATA_0
347     { 0x098C, 0x2417      , WORD_LEN, 0}, //  MCU_ADDRESS
348     { 0x0990, 0x00CC      , WORD_LEN, 0}, //  MCU_DATA_0
349     { 0x098C, 0xA404      , WORD_LEN, 0}, //  MCU_ADDRESS
350     { 0x0990, 0x0010      , WORD_LEN, 0}, //  MCU_DATA_0
351     { 0x098C, 0xA40D      , WORD_LEN, 0}, //  MCU_ADDRESS
352     { 0x0990, 0x0002      , WORD_LEN, 0}, //  MCU_DATA_0
353     { 0x098C, 0xA40E      , WORD_LEN, 0}, //  MCU_ADDRESS
354     { 0x0990, 0x0003      , WORD_LEN, 0}, //  MCU_DATA_0
355     { 0x098C, 0xA410      , WORD_LEN, 0}, //  MCU_ADDRESS
356     { 0x0990, 0x000A      , WORD_LEN, 0}, //  MCU_DATA_0
357     { 0x098C, 0xA103      , WORD_LEN, 0}, //  MCU_ADDRESS
358     { 0x0990, 0x0006      , WORD_LEN, 0}, //  MCU_DATA_0
359     { 0x098C, 0xA103      , WORD_LEN, 0}, //  MCU_ADDRESS
360     { 0x0990, 0x0005      , WORD_LEN, 0}, //  MCU_DATA_0
361     {SEQUENCE_END, 0x00}
362 #endif
363 };
364
365 /* 720p 15fps @ 1280x720 */
366
367 static struct reginfo sensor_720p[]=
368 {
369         {SEQUENCE_END, 0x00}
370 };
371
372 /*      1080p, 0x15fps, 0xyuv @1920x1080 */
373
374 static struct reginfo sensor_1080p[]=
375 {
376         {SEQUENCE_END, 0x00}
377 };
378
379 /* 2592X1944 QSXGA */
380 static struct reginfo sensor_qsxga[] =
381 {
382         {SEQUENCE_END, 0x00}
383 };
384 /* 2048*1536 QXGA */
385 static struct reginfo sensor_qxga[] =
386 {
387         {SEQUENCE_END, 0x00}
388 };
389
390 /* 1600X1200 UXGA */
391 static struct reginfo sensor_uxga[] =
392 {
393         //capture2preview
394         { 0x098C, 0xA115, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CAP_MODE]
395     { 0x0990, 0x0002, WORD_LEN, 0},     // MCU_DATA_0
396     { 0x098C, 0xA103, WORD_LEN, 0},     // MCU_ADDRESS [SEQ_CMD]
397     { 0x0990, 0x0002, WORD_LEN, 0},     // MCU_DATA_0
398         {SEQUENCE_END, 0x00}
399 };
400 /* 1280X1024 SXGA */
401 static struct reginfo sensor_sxga[] =
402 {
403 #if     0
404         { 0x098C, 0xA115, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CAP_MODE]
405         { 0x0990, 0x0000, WORD_LEN, 0},         // MCU_DATA_0
406         { 0x098C, 0xA103, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CMD]
407         { 0x0990, 0x0001, WORD_LEN, 0},         // MCU_DATA_0
408
409 //miaozh add
410 //    { 0x098C, 0x2717      , WORD_LEN, 0}, //  MCU_ADDRESS    
411 //    { 0x0990, 0x046E      , WORD_LEN, 0}, //  MCU_DATA_0
412 //    { 0x098C, 0xA103      , WORD_LEN, 0}, // MCU_ADDRESS [SEQ_CMD]
413 //    { 0x0990, 0x0006      , WORD_LEN, 0}, // MCU_DATA_0
414
415         { 0x098C, 0x2747, WORD_LEN, 0},         // MCU_ADDRESS [MODE_CROP_X0_B]
416         { 0x0990, 0x0000, WORD_LEN, 0},         // MCU_DATA_0
417         { 0x098C, 0x2749, WORD_LEN, 0},         // MCU_ADDRESS [MODE_CROP_X1_B]
418         { 0x0990, 0x063F, WORD_LEN, 0},         // MCU_DATA_0
419         { 0x098C, 0x274B, WORD_LEN, 0},         // MCU_ADDRESS [MODE_CROP_Y0_B]
420         { 0x0990, 0x0000, WORD_LEN, 0},         // MCU_DATA_0
421         { 0x098C, 0x274D, WORD_LEN, 0},         // MCU_ADDRESS [MODE_CROP_Y1_B]
422         { 0x0990, 0x04AF, WORD_LEN, 0},         // MCU_DATA_0
423         { 0x098C, 0x2707, WORD_LEN, 0},         // MCU_ADDRESS [MODE_OUTPUT_WIDTH_B]
424         { 0x0990, 0x0500, WORD_LEN, 0},         // MCU_DATA_0
425         { 0x098C, 0x2709, WORD_LEN, 0},         // MCU_ADDRESS [MODE_OUTPUT_HEIGHT_B]
426         { 0x0990, 0x0400, WORD_LEN, 0},         // MCU_DATA_0
427
428         { 0x098C, 0xA103, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CMD]
429         { 0x0990, 0x0005, WORD_LEN, 0},         // MCU_DATA_0
430
431         { 0x098C, 0xA115, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CAP_MODE]
432         { 0x0990, 0x0002, WORD_LEN, 0},         // MCU_DATA_0
433         { 0x098C, 0xA103, WORD_LEN, 0},         // MCU_ADDRESS [SEQ_CMD]
434         { 0x0990, 0x0002, WORD_LEN, 0},         // MCU_DATA_0
435
436 #endif
437         {SEQUENCE_END, 0x00}    
438 };
439 /*  1024X768 XGA */
440 static struct reginfo sensor_xga[] =
441 {
442         {SEQUENCE_END, 0x00}
443 };
444
445 /* 800X600 SVGA*/
446 static struct reginfo sensor_svga[] =
447 {
448         //[Step2-PLL_Timing]
449     //for 24MHz input, VCO=MAX PCLK=76.8MHz
450     { 0x001A, 0x0051, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
451     { 0x001A, 0x0050, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
452     { 0x0014, 0x2545, WORD_LEN, 0}, //  PLL_CONTROL
453     { 0x0010, 0x011C, WORD_LEN, 0}, //  PLL_DIVIDERS
454     { 0x0012, 0x1FF7, WORD_LEN, 0}, //  PLL_P_DIVIDERS
455     { 0x0014, 0x2547, WORD_LEN, 0}, //  PLL_CONTROL
456     { 0x0014, 0x2447, WORD_LEN, 0}, //  PLL_CONTROL
457     { 0x0014, 0x2047, WORD_LEN, 0}, //  PLL_CONTROL
458     { 0x0014, 0x2046, WORD_LEN, 0}, //  PLL_CONTROL
459     { 0x001A, 0x0050, WORD_LEN, 0}, //  RESET_AND_MISC_CONTROL
460     { 0x0018, 0x4028      , WORD_LEN, 0}, //  STANDBY_CONTROL
461     { 0x321C, 0x0003      , WORD_LEN, 0}, //  OFIFO_CONTROL_STATUS
462     { 0x098C, 0x2703      , WORD_LEN, 0}, //  MCU_ADDRESS
463     { 0x0990, 0x0320      , WORD_LEN, 0}, //  MCU_DATA_0
464     { 0x098C, 0x2705      , WORD_LEN, 0}, //  MCU_ADDRESS
465     { 0x0990, 0x0258      , WORD_LEN, 0}, //  MCU_DATA_0
466     { 0x098C, 0x2707      , WORD_LEN, 0}, //  MCU_ADDRESS
467     { 0x0990, 0x0640      , WORD_LEN, 0}, //  MCU_DATA_0
468     { 0x098C, 0x2709      , WORD_LEN, 0}, //  MCU_ADDRESS
469     { 0x0990, 0x04B0      , WORD_LEN, 0}, //  MCU_DATA_0
470     { 0x098C, 0x270D      , WORD_LEN, 0}, //  MCU_ADDRESS
471     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
472     { 0x098C, 0x270F      , WORD_LEN, 0}, //  MCU_ADDRESS
473     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
474     { 0x098C, 0x2711      , WORD_LEN, 0}, //  MCU_ADDRESS
475     { 0x0990, 0x04BD      , WORD_LEN, 0}, //  MCU_DATA_0
476     { 0x098C, 0x2713      , WORD_LEN, 0}, //  MCU_ADDRESS
477     { 0x0990, 0x064D      , WORD_LEN, 0}, //  MCU_DATA_0
478     { 0x098C, 0x2715      , WORD_LEN, 0}, //  MCU_ADDRESS
479     { 0x0990, 0x0111      , WORD_LEN, 0}, //  MCU_DATA_0
480     { 0x098C, 0x2717      , WORD_LEN, 0}, //  MCU_ADDRESS
481 //miaozh modify    
482     { 0x0990, 0x046C      , WORD_LEN, 0}, //  MCU_DATA_0
483 //    { 0x0990, 0x046E      , WORD_LEN, 0}, //  MCU_DATA_0
484 //    { 0x098C, 0xA103      , WORD_LEN, 0}, // MCU_ADDRESS [SEQ_CMD]
485 //    { 0x0990, 0x0006      , WORD_LEN, 0}, // MCU_DATA_0
486
487     
488     { 0x098C, 0x2719      , WORD_LEN, 0}, //  MCU_ADDRESS
489     { 0x0990, 0x005A      , WORD_LEN, 0}, //  MCU_DATA_0
490     { 0x098C, 0x271B      , WORD_LEN, 0}, //  MCU_ADDRESS
491     { 0x0990, 0x01BE      , WORD_LEN, 0}, //  MCU_DATA_0
492     { 0x098C, 0x271D      , WORD_LEN, 0}, //  MCU_ADDRESS
493     { 0x0990, 0x0131      , WORD_LEN, 0}, //  MCU_DATA_0
494     { 0x098C, 0x271F      , WORD_LEN, 0}, //  MCU_ADDRESS
495     { 0x0990, 0x02B3      , WORD_LEN, 0}, //  MCU_DATA_0
496     { 0x098C, 0x2721      , WORD_LEN, 0}, //  MCU_ADDRESS
497     { 0x0990, 0x09B0      , WORD_LEN, 0}, //  MCU_DATA_0
498     { 0x098C, 0x2723      , WORD_LEN, 0}, //  MCU_ADDRESS
499     { 0x0990, 0x0004      , WORD_LEN, 0}, //  MCU_DATA_0
500     { 0x098C, 0x2725      , WORD_LEN, 0}, //  MCU_ADDRESS
501     { 0x0990, 0x0004      , WORD_LEN, 0}, //  MCU_DATA_0
502     { 0x098C, 0x2727      , WORD_LEN, 0}, //  MCU_ADDRESS
503     { 0x0990, 0x04BB      , WORD_LEN, 0}, //  MCU_DATA_0
504     { 0x098C, 0x2729      , WORD_LEN, 0}, //  MCU_ADDRESS
505     { 0x0990, 0x064B      , WORD_LEN, 0}, //  MCU_DATA_0
506     { 0x098C, 0x272B      , WORD_LEN, 0}, //  MCU_ADDRESS
507     { 0x0990, 0x0111      , WORD_LEN, 0}, //  MCU_DATA_0
508     { 0x098C, 0x272D      , WORD_LEN, 0}, //  MCU_ADDRESS
509     { 0x0990, 0x0024      , WORD_LEN, 0}, //  MCU_DATA_0
510     { 0x098C, 0x272F      , WORD_LEN, 0}, //  MCU_ADDRESS
511     { 0x0990, 0x003A      , WORD_LEN, 0}, //  MCU_DATA_0
512     { 0x098C, 0x2731      , WORD_LEN, 0}, //  MCU_ADDRESS
513     { 0x0990, 0x00F6      , WORD_LEN, 0}, //  MCU_DATA_0
514     { 0x098C, 0x2733      , WORD_LEN, 0}, //  MCU_ADDRESS
515     { 0x0990, 0x008B      , WORD_LEN, 0}, //  MCU_DATA_0
516     { 0x098C, 0x2735      , WORD_LEN, 0}, //  MCU_ADDRESS
517     { 0x0990, 0x050D      , WORD_LEN, 0}, //  MCU_DATA_0
518     { 0x098C, 0x2737      , WORD_LEN, 0}, //  MCU_ADDRESS
519     { 0x0990, 0x0807      , WORD_LEN, 0}, //  MCU_DATA_0
520     { 0x098C, 0x2739      , WORD_LEN, 0}, //  MCU_ADDRESS
521     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
522     { 0x098C, 0x273B      , WORD_LEN, 0}, //  MCU_ADDRESS
523     { 0x0990, 0x031F      , WORD_LEN, 0}, //  MCU_DATA_0
524     { 0x098C, 0x273D      , WORD_LEN, 0}, //  MCU_ADDRESS
525     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
526     { 0x098C, 0x273F      , WORD_LEN, 0}, //  MCU_ADDRESS
527     { 0x0990, 0x0257      , WORD_LEN, 0}, //  MCU_DATA_0
528     { 0x098C, 0x2747      , WORD_LEN, 0}, //  MCU_ADDRESS
529     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
530     { 0x098C, 0x2749      , WORD_LEN, 0}, //  MCU_ADDRESS
531     { 0x0990, 0x063F      , WORD_LEN, 0}, //  MCU_DATA_0
532     { 0x098C, 0x274B      , WORD_LEN, 0}, //  MCU_ADDRESS
533     { 0x0990, 0x0000      , WORD_LEN, 0}, //  MCU_DATA_0
534     { 0x098C, 0x274D      , WORD_LEN, 0}, //  MCU_ADDRESS
535     { 0x0990, 0x04AF      , WORD_LEN, 0}, //  MCU_DATA_0
536     { 0x098C, 0x222D      , WORD_LEN, 0}, //  MCU_ADDRESS
537     { 0x0990, 0x008D      , WORD_LEN, 0}, //  MCU_DATA_0
538     { 0x098C, 0xA408      , WORD_LEN, 0}, //  MCU_ADDRESS
539     { 0x0990, 0x0022      , WORD_LEN, 0}, //  MCU_DATA_0
540     { 0x098C, 0xA409      , WORD_LEN, 0}, //  MCU_ADDRESS
541     { 0x0990, 0x0024      , WORD_LEN, 0}, //  MCU_DATA_0
542     { 0x098C, 0xA40A      , WORD_LEN, 0}, //  MCU_ADDRESS
543     { 0x0990, 0x0029      , WORD_LEN, 0}, //  MCU_DATA_0
544     { 0x098C, 0xA40B      , WORD_LEN, 0}, //  MCU_ADDRESS
545     { 0x0990, 0x002B      , WORD_LEN, 0}, //  MCU_DATA_0
546     { 0x098C, 0x2411      , WORD_LEN, 0}, //  MCU_ADDRESS
547     { 0x0990, 0x008D      , WORD_LEN, 0}, //  MCU_DATA_0
548     { 0x098C, 0x2413      , WORD_LEN, 0}, //  MCU_ADDRESS
549     { 0x0990, 0x00A9      , WORD_LEN, 0}, //  MCU_DATA_0
550     { 0x098C, 0x2415      , WORD_LEN, 0}, //  MCU_ADDRESS
551     { 0x0990, 0x00AA      , WORD_LEN, 0}, //  MCU_DATA_0
552     { 0x098C, 0x2417      , WORD_LEN, 0}, //  MCU_ADDRESS
553     { 0x0990, 0x00CC      , WORD_LEN, 0}, //  MCU_DATA_0
554     { 0x098C, 0xA404      , WORD_LEN, 0}, //  MCU_ADDRESS
555     { 0x0990, 0x0010      , WORD_LEN, 0}, //  MCU_DATA_0
556     { 0x098C, 0xA40D      , WORD_LEN, 0}, //  MCU_ADDRESS
557     { 0x0990, 0x0002      , WORD_LEN, 0}, //  MCU_DATA_0
558     { 0x098C, 0xA40E      , WORD_LEN, 0}, //  MCU_ADDRESS
559     { 0x0990, 0x0003      , WORD_LEN, 0}, //  MCU_DATA_0
560     { 0x098C, 0xA410      , WORD_LEN, 0}, //  MCU_ADDRESS
561     { 0x0990, 0x000A      , WORD_LEN, 0}, //  MCU_DATA_0
562     { 0x098C, 0xA103      , WORD_LEN, 0}, //  MCU_ADDRESS
563     { 0x0990, 0x0006      , WORD_LEN, 0}, //  MCU_DATA_0
564     { 0x098C, 0xA103      , WORD_LEN, 0}, //  MCU_ADDRESS
565     { 0x0990, 0x0005      , WORD_LEN, 0}, //  MCU_DATA_0
566     {SEQUENCE_END, 0x00}
567 };
568
569 /* 640X480 VGA */
570 static struct reginfo sensor_vga[] =
571 {
572     {SEQUENCE_END, 0x00}
573 };
574
575 /* 352X288 CIF */
576 static struct reginfo sensor_cif[] =
577 {
578         {SEQUENCE_END, 0x00}
579 };
580
581 /* 320*240 QVGA */
582 static  struct reginfo sensor_qvga[] =
583 {
584         {SEQUENCE_END, 0x00}
585 };
586
587 /* 176X144 QCIF*/
588 static struct reginfo sensor_qcif[] =
589 {
590         {SEQUENCE_END, 0x00}
591 };
592
593 static  struct reginfo sensor_Preview2Capture[]=
594 {
595         //capture2preview
596         {0x098E, 0x843C, WORD_LEN, 0},  // LOGICAL_ADDRESS_ACCESS [SEQ_STATE_CFG_5_MAX_FRAME_CNT]
597         {0x843C, 0xFF, BYTE_LEN, 0 },   // SEQ_STATE_CFG_5_MAX_FRAME_CNT
598         {0x8404, 0x02, BYTE_LEN, 0 },   // SEQ_CMD
599         {SEQUENCE_END, 0x00}
600 };
601
602 static  struct reginfo sensor_Capture2Preview[]=
603 {
604         //snap2preview
605         {0x098E, 0x843C, WORD_LEN, 0},  // LOGICAL_ADDRESS_ACCESS [SEQ_STATE_CFG_5_MAX_FRAME_CNT]
606         {0x843C, 0x01, BYTE_LEN, 0 },   // SEQ_STATE_CFG_5_MAX_FRAME_CNT
607         {0x8404, 0x01, BYTE_LEN, 0 },   // SEQ_CMD
608         {0x0016, 0x0047, WORD_LEN, 0},  // CLOCKS_CONTRO
609         {SEQUENCE_END, 0x00}
610 };
611
612 static  struct reginfo sensor_ClrFmt_YUYV[]=
613 {
614         {SEQUENCE_END, 0x00}
615 };
616
617 static  struct reginfo sensor_ClrFmt_UYVY[]=
618 {
619         {SEQUENCE_END, 0x00}
620 };
621
622
623 #if CONFIG_SENSOR_WhiteBalance
624 static  struct reginfo sensor_WhiteB_Auto[]=
625 {
626         //Auto
627         {SEQUENCE_END, 0x00}
628 };
629 /* Cloudy Colour Temperature : 6500K - 8000K  */
630 static  struct reginfo sensor_WhiteB_Cloudy[]=
631 {
632         //[V.       DL 7500]
633         {SEQUENCE_END, 0x00}
634 };
635 /* ClearDay Colour Temperature : 5000K - 6500K  */
636 static  struct reginfo sensor_WhiteB_ClearDay[]=
637 {
638         //[IV       Day Light]
639         {SEQUENCE_END, 0x00}
640 };
641 /* Office Colour Temperature : 3500K - 5000K  */
642 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
643 {
644         //[III        Fluorescent]
645         {SEQUENCE_END, 0x00}
646 };
647 /* Home Colour Temperature : 2500K - 3500K  */
648 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
649 {
650         //[II.        Incandescent]
651         {SEQUENCE_END, 0x00}
652 };
653 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
654     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
655 };
656 #endif
657
658 #if CONFIG_SENSOR_Brightness
659 static  struct reginfo sensor_Brightness0[]=
660 {
661         {SEQUENCE_END, 0x00}
662 };
663
664 static  struct reginfo sensor_Brightness1[]=
665 {
666         {SEQUENCE_END, 0x00}
667 };
668
669 static  struct reginfo sensor_Brightness2[]=
670 {
671         {SEQUENCE_END, 0x00}
672 };
673
674 static  struct reginfo sensor_Brightness3[]=
675 {
676         {SEQUENCE_END, 0x00}
677 };
678
679 static  struct reginfo sensor_Brightness4[]=
680 {
681         {SEQUENCE_END, 0x00}
682 };
683
684 static  struct reginfo sensor_Brightness5[]=
685 {
686         {SEQUENCE_END, 0x00}
687 };
688 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
689     sensor_Brightness4, sensor_Brightness5,NULL,
690 };
691
692 #endif
693
694 #if CONFIG_SENSOR_Effect
695 static  struct reginfo sensor_Effect_Normal[] =
696 {
697         {SEQUENCE_END, 0x00}
698 };
699
700 static  struct reginfo sensor_Effect_WandB[] =
701 {
702         {SEQUENCE_END, 0x00}
703 };
704
705 static  struct reginfo sensor_Effect_Sepia[] =
706 {
707         {SEQUENCE_END, 0x00}
708 };
709
710 static  struct reginfo sensor_Effect_Negative[] =
711 {
712         {SEQUENCE_END, 0x00}
713 };
714 static  struct reginfo sensor_Effect_Bluish[] =
715 {
716         {SEQUENCE_END, 0x00}
717 };
718
719 static  struct reginfo sensor_Effect_Green[] =
720 {
721         {SEQUENCE_END, 0x00}
722 };
723
724 static struct reginfo sensor_Effect_Solarize[] =
725 {
726         {SEQUENCE_END, 0x00}
727 };
728 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_Negative,sensor_Effect_Sepia,
729     sensor_Effect_Solarize,NULL,
730 };
731 #endif
732 #if CONFIG_SENSOR_Exposure
733 static  struct reginfo sensor_Exposure0[]=
734 {
735         {SEQUENCE_END, 0x00}
736 };
737
738 static  struct reginfo sensor_Exposure1[]=
739 {
740         {SEQUENCE_END, 0x00}
741 };
742
743 static  struct reginfo sensor_Exposure2[]=
744 {
745         {SEQUENCE_END, 0x00}
746 };
747
748 static  struct reginfo sensor_Exposure3[]=
749 {
750         {SEQUENCE_END, 0x00}
751 };
752
753 static  struct reginfo sensor_Exposure4[]=
754 {
755         {SEQUENCE_END, 0x00}
756 };
757
758 static  struct reginfo sensor_Exposure5[]=
759 {
760         {SEQUENCE_END, 0x00}
761 };
762
763 static  struct reginfo sensor_Exposure6[]=
764 {
765         {SEQUENCE_END, 0x00}
766 };
767
768 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
769     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
770 };
771 #endif
772 #if CONFIG_SENSOR_Saturation
773 static  struct reginfo sensor_Saturation0[]=
774 {
775         {SEQUENCE_END, 0x00}
776 };
777
778 static  struct reginfo sensor_Saturation1[]=
779 {
780         {SEQUENCE_END, 0x00}
781 };
782
783 static  struct reginfo sensor_Saturation2[]=
784 {
785         {SEQUENCE_END, 0x00}
786 };
787 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
788 #endif
789 #if CONFIG_SENSOR_Contrast
790 static  struct reginfo sensor_Contrast0[]=
791 {
792         {SEQUENCE_END, 0x00}
793 };
794
795 static  struct reginfo sensor_Contrast1[]=
796 {
797         {SEQUENCE_END, 0x00}
798 };
799
800 static  struct reginfo sensor_Contrast2[]=
801 {
802         {SEQUENCE_END, 0x00}
803 };
804
805 static  struct reginfo sensor_Contrast3[]=
806 {
807         {SEQUENCE_END, 0x00}
808 };
809
810 static  struct reginfo sensor_Contrast4[]=
811 {
812         {SEQUENCE_END, 0x00}
813 };
814
815
816 static  struct reginfo sensor_Contrast5[]=
817 {
818         {SEQUENCE_END, 0x00}
819 };
820
821 static  struct reginfo sensor_Contrast6[]=
822 {
823         {SEQUENCE_END, 0x00}
824 };
825 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
826     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
827 };
828
829 #endif
830 #if CONFIG_SENSOR_Mirror
831 static  struct reginfo sensor_MirrorOn[]=
832 {
833         {SEQUENCE_END, 0x00}
834 };
835
836 static  struct reginfo sensor_MirrorOff[]=
837 {
838         {SEQUENCE_END, 0x00}
839 };
840 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
841 #endif
842 #if CONFIG_SENSOR_Flip
843 static  struct reginfo sensor_FlipOn[]=
844 {
845         {SEQUENCE_END, 0x00}
846 };
847
848 static  struct reginfo sensor_FlipOff[]=
849 {
850         {SEQUENCE_END, 0x00}
851 };
852 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
853
854 #endif
855 #if CONFIG_SENSOR_Scene
856 static  struct reginfo sensor_SceneAuto[] =
857 {
858         {SEQUENCE_END, 0x00}
859 };
860
861 static  struct reginfo sensor_SceneNight[] =
862 {
863         {SEQUENCE_END, 0x00}
864 };
865 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
866
867 #endif
868 #if CONFIG_SENSOR_DigitalZoom
869 static struct reginfo sensor_Zoom0[] =
870 {
871         {SEQUENCE_END, 0x00}
872 };
873
874 static struct reginfo sensor_Zoom1[] =
875 {
876         {SEQUENCE_END, 0x00}
877 };
878
879 static struct reginfo sensor_Zoom2[] =
880 {
881         {SEQUENCE_END, 0x00}
882 };
883
884
885 static struct reginfo sensor_Zoom3[] =
886 {
887         {SEQUENCE_END, 0x00}
888 };
889 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL};
890 #endif
891 static const struct v4l2_querymenu sensor_menus[] =
892 {
893         #if CONFIG_SENSOR_WhiteBalance
894     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
895     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
896     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
897     #endif
898
899         #if CONFIG_SENSOR_Effect
900     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "negative",  .reserved = 0,},
901     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "sepia", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "solarize", .reserved = 0,},
902     #endif
903
904         #if CONFIG_SENSOR_Scene
905     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
906     #endif
907
908         #if CONFIG_SENSOR_Flash
909     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
910     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
911     #endif
912 };
913
914 static const struct v4l2_queryctrl sensor_controls[] =
915 {
916         #if CONFIG_SENSOR_WhiteBalance
917     {
918         .id             = V4L2_CID_DO_WHITE_BALANCE,
919         .type           = V4L2_CTRL_TYPE_MENU,
920         .name           = "White Balance Control",
921         .minimum        = 0,
922         .maximum        = 4,
923         .step           = 1,
924         .default_value = 0,
925     },
926     #endif
927
928         #if CONFIG_SENSOR_Brightness
929         {
930         .id             = V4L2_CID_BRIGHTNESS,
931         .type           = V4L2_CTRL_TYPE_INTEGER,
932         .name           = "Brightness Control",
933         .minimum        = -3,
934         .maximum        = 2,
935         .step           = 1,
936         .default_value = 0,
937     },
938     #endif
939
940         #if CONFIG_SENSOR_Effect
941         {
942         .id             = V4L2_CID_EFFECT,
943         .type           = V4L2_CTRL_TYPE_MENU,
944         .name           = "Effect Control",
945         .minimum        = 0,
946         .maximum        = 3,
947         .step           = 1,
948         .default_value = 0,
949     },
950         #endif
951
952         #if CONFIG_SENSOR_Exposure
953         {
954         .id             = V4L2_CID_EXPOSURE,
955         .type           = V4L2_CTRL_TYPE_INTEGER,
956         .name           = "Exposure Control",
957         .minimum        = 0,
958         .maximum        = 6,
959         .step           = 1,
960         .default_value = 0,
961     },
962         #endif
963
964         #if CONFIG_SENSOR_Saturation
965         {
966         .id             = V4L2_CID_SATURATION,
967         .type           = V4L2_CTRL_TYPE_INTEGER,
968         .name           = "Saturation Control",
969         .minimum        = 0,
970         .maximum        = 2,
971         .step           = 1,
972         .default_value = 0,
973     },
974     #endif
975
976         #if CONFIG_SENSOR_Contrast
977         {
978         .id             = V4L2_CID_CONTRAST,
979         .type           = V4L2_CTRL_TYPE_INTEGER,
980         .name           = "Contrast Control",
981         .minimum        = -3,
982         .maximum        = 3,
983         .step           = 1,
984         .default_value = 0,
985     },
986         #endif
987
988         #if CONFIG_SENSOR_Mirror
989         {
990         .id             = V4L2_CID_HFLIP,
991         .type           = V4L2_CTRL_TYPE_BOOLEAN,
992         .name           = "Mirror Control",
993         .minimum        = 0,
994         .maximum        = 1,
995         .step           = 1,
996         .default_value = 1,
997     },
998     #endif
999
1000         #if CONFIG_SENSOR_Flip
1001         {
1002         .id             = V4L2_CID_VFLIP,
1003         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1004         .name           = "Flip Control",
1005         .minimum        = 0,
1006         .maximum        = 1,
1007         .step           = 1,
1008         .default_value = 1,
1009     },
1010     #endif
1011
1012         #if CONFIG_SENSOR_Scene
1013     {
1014         .id             = V4L2_CID_SCENE,
1015         .type           = V4L2_CTRL_TYPE_MENU,
1016         .name           = "Scene Control",
1017         .minimum        = 0,
1018         .maximum        = 1,
1019         .step           = 1,
1020         .default_value = 0,
1021     },
1022     #endif
1023
1024         #if CONFIG_SENSOR_DigitalZoom
1025     {
1026         .id             = V4L2_CID_ZOOM_RELATIVE,
1027         .type           = V4L2_CTRL_TYPE_INTEGER,
1028         .name           = "DigitalZoom Control",
1029         .minimum        = -1,
1030         .maximum        = 1,
1031         .step           = 1,
1032         .default_value = 0,
1033     }, {
1034         .id             = V4L2_CID_ZOOM_ABSOLUTE,
1035         .type           = V4L2_CTRL_TYPE_INTEGER,
1036         .name           = "DigitalZoom Control",
1037         .minimum        = 0,
1038         .maximum        = 3,
1039         .step           = 1,
1040         .default_value = 0,
1041     },
1042     #endif
1043
1044         #if CONFIG_SENSOR_Focus
1045         {
1046         .id             = V4L2_CID_FOCUS_RELATIVE,
1047         .type           = V4L2_CTRL_TYPE_INTEGER,
1048         .name           = "Focus Control",
1049         .minimum        = -1,
1050         .maximum        = 1,
1051         .step           = 1,
1052         .default_value = 0,
1053     }, {
1054         .id             = V4L2_CID_FOCUS_ABSOLUTE,
1055         .type           = V4L2_CTRL_TYPE_INTEGER,
1056         .name           = "Focus Control",
1057         .minimum        = 0,
1058         .maximum        = 255,
1059         .step           = 1,
1060         .default_value = 125,
1061     },
1062         {
1063         .id             = V4L2_CID_FOCUS_AUTO,
1064         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1065         .name           = "Focus Control",
1066         .minimum        = 0,
1067         .maximum        = 1,
1068         .step           = 1,
1069         .default_value = 0,
1070     },{
1071         .id             = V4L2_CID_FOCUS_CONTINUOUS,
1072         .type           = V4L2_CTRL_TYPE_BOOLEAN,
1073         .name           = "Focus Control",
1074         .minimum        = 0,
1075         .maximum        = 1,
1076         .step           = 1,
1077         .default_value = 0,
1078     },
1079     #endif
1080
1081         #if CONFIG_SENSOR_Flash
1082         {
1083         .id             = V4L2_CID_FLASH,
1084         .type           = V4L2_CTRL_TYPE_MENU,
1085         .name           = "Flash Control",
1086         .minimum        = 0,
1087         .maximum        = 3,
1088         .step           = 1,
1089         .default_value = 0,
1090     },
1091         #endif
1092 };
1093
1094 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
1095 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
1096 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1097 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
1098 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1099 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
1100 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
1101 static int sensor_resume(struct soc_camera_device *icd);
1102 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
1103 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
1104 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1105 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
1106 static int sensor_deactivate(struct i2c_client *client);
1107
1108 static struct soc_camera_ops sensor_ops =
1109 {
1110     .suspend                     = sensor_suspend,
1111     .resume                       = sensor_resume,
1112     .set_bus_param              = sensor_set_bus_param,
1113     .query_bus_param    = sensor_query_bus_param,
1114     .controls           = sensor_controls,
1115     .menus                         = sensor_menus,
1116     .num_controls               = ARRAY_SIZE(sensor_controls),
1117     .num_menus          = ARRAY_SIZE(sensor_menus),
1118 };
1119
1120 /* only one fixed colorspace per pixelcode */
1121 struct sensor_datafmt {
1122         enum v4l2_mbus_pixelcode code;
1123         enum v4l2_colorspace colorspace;
1124 };
1125
1126 /* Find a data format by a pixel code in an array */
1127 static const struct sensor_datafmt *sensor_find_datafmt(
1128         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
1129         int n)
1130 {
1131         int i;
1132         for (i = 0; i < n; i++)
1133                 if (fmt[i].code == code)
1134                         return fmt + i;
1135
1136         return NULL;
1137 }
1138
1139 static const struct sensor_datafmt sensor_colour_fmts[] = {
1140     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
1141     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
1142 };
1143 enum sensor_work_state
1144 {
1145         sensor_work_ready = 0,
1146         sensor_working,
1147 };
1148 struct sensor_work
1149 {
1150         struct i2c_client *client;
1151         struct delayed_work dwork;
1152         enum sensor_work_state state;
1153 };
1154
1155 typedef struct sensor_info_priv_s
1156 {
1157     int whiteBalance;
1158     int brightness;
1159     int contrast;
1160     int saturation;
1161     int effect;
1162     int scene;
1163     int digitalzoom;
1164     int focus;
1165         int auto_focus;
1166         int affm_reinit;
1167     int flash;
1168     int exposure;
1169     unsigned char mirror;                                        /* HFLIP */
1170     unsigned char flip;                                          /* VFLIP */
1171         bool snap2preview;
1172         bool video2preview;
1173         int capture_w;
1174         int capture_h;
1175         int preview_w;
1176         int preview_h;
1177     struct reginfo *winseqe_cur_addr;
1178         struct sensor_datafmt fmt;
1179         unsigned int enable;
1180         unsigned int funmodule_state;
1181 } sensor_info_priv_t;
1182
1183
1184
1185 struct sensor_parameter
1186 {
1187         unsigned short int preview_maxlines;
1188         unsigned short int preview_exposure;
1189         unsigned short int preview_line_width;
1190         unsigned short int preview_gain;
1191
1192         unsigned short int capture_framerate;
1193         unsigned short int preview_framerate;
1194 };
1195
1196 struct sensor
1197 {
1198     struct v4l2_subdev subdev;
1199     struct i2c_client *client;
1200     sensor_info_priv_t info_priv;
1201         struct sensor_parameter parameter;
1202         struct workqueue_struct *sensor_wq;
1203         struct sensor_work sensor_wk;
1204         struct mutex wq_lock;
1205     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
1206 #if CONFIG_SENSOR_I2C_NOSCHED
1207         atomic_t tasklock_cnt;
1208 #endif
1209         struct rk29camera_platform_data *sensor_io_request;
1210     struct rk29camera_gpio_res *sensor_gpio_res;
1211 };
1212
1213 static struct sensor* to_sensor(const struct i2c_client *client)
1214 {
1215     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
1216 }
1217
1218 static int sensor_task_lock(struct i2c_client *client, int lock)
1219 {
1220 #if CONFIG_SENSOR_I2C_NOSCHED
1221         int cnt = 3;
1222     struct sensor *sensor = to_sensor(client);
1223
1224         if (lock) {
1225                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
1226                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
1227                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
1228                                 msleep(35);
1229                                 cnt--;
1230                         }
1231                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
1232                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
1233                                 goto sensor_task_lock_err;
1234                         }
1235                         preempt_disable();
1236                 }
1237
1238                 atomic_add(1, &sensor->tasklock_cnt);
1239         } else {
1240                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
1241                         atomic_sub(1, &sensor->tasklock_cnt);
1242
1243                         if (atomic_read(&sensor->tasklock_cnt) == 0)
1244                                 preempt_enable();
1245                 }
1246         }
1247     
1248         return 0;
1249 sensor_task_lock_err:
1250         return -1;    
1251 #else
1252     return 0;
1253 #endif
1254 }
1255
1256 static int sensor_read(struct i2c_client *client, u16 reg, u16 *val);
1257
1258 /* sensor register write */
1259 static int sensor_write(struct i2c_client *client, struct reginfo *reg_info)
1260 {
1261     int err=0,cnt;
1262     u8 buf[4];
1263     struct i2c_msg msg[1];
1264
1265         switch (reg_info->reg)
1266         {
1267                 case SEQUENCE_WAIT_MS:
1268                 {
1269                         if (in_atomic())
1270                                 mdelay(reg_info->val);
1271                         else
1272                                 msleep(reg_info->val);
1273                         break;
1274                 }
1275
1276                 case SEQUENCE_WAIT_US:
1277                 {
1278                         udelay(reg_info->val);
1279                         break;
1280                 }
1281
1282                 case SEQUENCE_PROPERTY:
1283                 {
1284                         break;
1285                 }
1286                 default:
1287                 {
1288                     buf[0] = reg_info->reg >> 8;
1289                     buf[1] = reg_info->reg & 0xFF;
1290                         if (reg_info->reg_len == WORD_LEN) {
1291                                 buf[2] = reg_info->val >> 8;
1292                                 buf[3] = reg_info->val & 0xFF;
1293
1294                                 msg->len = 4;
1295                         } else if (reg_info->reg_len == BYTE_LEN) {
1296                                 buf[2] = reg_info->val;
1297                                 msg->len = 3;
1298                         }
1299
1300                     msg->addr = client->addr;
1301                     msg->flags = client->flags;
1302                     msg->buf = buf;
1303                     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1304                     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1305
1306                     cnt = 3;
1307                     err = -EAGAIN;
1308
1309                     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1310                         err = i2c_transfer(client->adapter, msg, 1);
1311
1312                         if (err >= 0) {
1313                             return 0;
1314                         } else {
1315                             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg_info->reg, reg_info->val);
1316                             udelay(10);
1317                         }
1318                     }
1319                 }
1320         }
1321     return err;
1322 }
1323
1324 /* sensor register read */
1325 static int sensor_read(struct i2c_client *client, u16 reg, u16 *val)
1326 {
1327     int err,cnt;
1328     u8 buf[2];
1329     struct i2c_msg msg[2];
1330
1331     buf[0] = reg >> 8;
1332     buf[1] = reg & 0xFF;
1333
1334     msg[0].addr = client->addr;
1335     msg[0].flags = client->flags;
1336     msg[0].buf = buf;
1337     msg[0].len = sizeof(buf);
1338     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
1339     msg[0].read_type = 2;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1340
1341     msg[1].addr = client->addr;
1342     msg[1].flags = client->flags|I2C_M_RD;
1343     msg[1].buf = buf;
1344     msg[1].len = 2;
1345     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
1346     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
1347
1348     cnt = 3;
1349     err = -EAGAIN;
1350     while ((cnt-- > 0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
1351         err = i2c_transfer(client->adapter, msg, 2);
1352
1353         if (err >= 0) {
1354             *val = buf[0]<<8 | buf[1];
1355             return 0;
1356         } else {
1357                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
1358             udelay(10);
1359         }
1360     }
1361
1362     return err;
1363 }
1364
1365 /* write a array of registers  */
1366 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
1367 {
1368     int err = 0, cnt;
1369     int i = 0;
1370 #if CONFIG_SENSOR_I2C_RDWRCHK
1371         u16 valchk;
1372 #endif
1373
1374         cnt = 0;
1375         if (sensor_task_lock(client, 1) < 0)
1376                 goto sensor_write_array_end;
1377     while (regarray[i].reg != SEQUENCE_END) {
1378         err = sensor_write(client, &regarray[i]);
1379         if (err < 0)
1380         {
1381             if (cnt-- > 0) {
1382                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
1383                                 i = 0;
1384                                 continue;
1385             } else {
1386                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
1387                 err = -EPERM;
1388                                 goto sensor_write_array_end;
1389             }
1390         } else {
1391         #if CONFIG_SENSOR_I2C_RDWRCHK
1392                         sensor_read(client, regarray[i].reg, &valchk);
1393                         if (valchk != regarray[i].val)
1394                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1395                 #endif
1396         }
1397
1398         i++;
1399     }
1400
1401 sensor_write_array_end:
1402         sensor_task_lock(client,0);
1403     return err;
1404 }
1405 #if CONFIG_SENSOR_I2C_RDWRCHK
1406 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
1407 {
1408     int cnt;
1409     int i = 0;
1410         u16 valchk;
1411
1412         cnt = 0;
1413         valchk = 0;
1414     while (regarray[i].reg != SEQUENCE_END)
1415     {
1416                 sensor_read(client, regarray[i].reg, &valchk);
1417                 if (valchk != regarray[i].val)
1418                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
1419
1420         i++;
1421     }
1422     return 0;
1423 }
1424 #endif
1425 #if CONFIG_SENSOR_Focus
1426 static struct reginfo sensor_af_init0[] =
1427 {
1428
1429         {SEQUENCE_END, 0x00}
1430 };
1431 static struct reginfo sensor_af_init1[] =
1432 {
1433     {SEQUENCE_END, 0x00}
1434 };
1435
1436
1437 static struct reginfo sensor_af_trigger[] =
1438 {
1439         {SEQUENCE_END, 0x00}
1440 };
1441 static int sensor_af_single(struct i2c_client *client)
1442 {
1443         int ret = 0;
1444
1445         ret = sensor_write_array(client, sensor_af_trigger);
1446         if (ret<0)
1447                 SENSOR_TR("%s sensor auto focus trigger fail!!\n",SENSOR_NAME_STRING());
1448         else
1449                 SENSOR_DG("%s sensor auto focus trigger success!\n",SENSOR_NAME_STRING());
1450 sensor_af_single_end:
1451         return ret;
1452 }
1453
1454 static int sensor_af_const(struct i2c_client *client)
1455 {
1456         int ret = 0;
1457
1458 sensor_af_const_end:
1459         return ret;
1460 }
1461
1462 static int sensor_af_zoneupdate(struct i2c_client *client)
1463 {
1464         int ret = 0;
1465         struct i2c_msg msg[2];
1466     u8 buf[2][6] =
1467         {
1468                 {0xb0,0x08,0x00,0x03,0xff,0xff},
1469                 {0xb0,0x0c,0xff,0xff,0xff,0xff},
1470         };
1471
1472     msg[0].addr = client->addr;
1473     msg[0].flags = client->flags;
1474     msg[0].buf = buf[0];
1475     msg[0].len = sizeof(buf);
1476     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1477     msg[0].read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1478
1479     msg[1].addr = client->addr;
1480     msg[1].flags = client->flags;
1481     msg[1].buf = buf[1];
1482     msg[1].len = sizeof(buf);
1483     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;         /* ddl@rock-chips.com : 100kHz */
1484     msg[1].read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
1485
1486     ret = i2c_transfer(client->adapter, &msg[0], 1);
1487         ret |= i2c_transfer(client->adapter, &msg[1], 1);
1488     if (ret >= 0) {
1489         return 0;
1490     } else {
1491         SENSOR_TR("\n %s sensor auto focus zone set fail!!\n",SENSOR_NAME_STRING());
1492     }
1493
1494 sensor_af_zoneupdate_end:
1495         return ret;
1496 }
1497
1498 static int sensor_af_init(struct i2c_client *client)
1499 {
1500         int ret = 0;
1501
1502         ret = sensor_write_array(client, sensor_af_init0);
1503         if (ret<0) {
1504                 SENSOR_TR("%s sensor auto focus init_0 fail!!",SENSOR_NAME_STRING());
1505         } else {
1506                 if (sensor_af_zoneupdate(client) == 0) {
1507                         ret = sensor_write_array(client, sensor_af_init1);
1508                         if (ret<0) {
1509                                 SENSOR_TR("%s sensor auto focus init_1 fail!!",SENSOR_NAME_STRING());
1510                         }
1511                 }
1512         }
1513
1514         return ret;
1515 }
1516 #endif
1517
1518 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
1519 {
1520         struct soc_camera_link *icl = to_soc_camera_link(icd);
1521         int ret = 0;
1522
1523     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
1524         switch (cmd)
1525         {
1526                 case Sensor_PowerDown:
1527                 {
1528                         if (icl->powerdown) {
1529                                 ret = icl->powerdown(icd->pdev, on);
1530                                 if (ret == RK29_CAM_IO_SUCCESS) {
1531                                         if (on == 0) {
1532                                                 mdelay(2);
1533                                                 if (icl->reset)
1534                                                         icl->reset(icd->pdev);
1535                                         }
1536                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
1537                                         ret = -ENODEV;
1538                                         goto sensor_power_end;
1539                                 }
1540                         }
1541                         break;
1542                 }
1543                 case Sensor_Flash:
1544                 {
1545                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1546                 struct sensor *sensor = to_sensor(client);
1547
1548                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
1549                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
1550                         }
1551                         break;
1552                 }
1553                 default:
1554                 {
1555                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
1556                         break;
1557                 }
1558         }
1559
1560 sensor_power_end:
1561         return ret;
1562 }
1563 static s32 sensor_init_width = 800;
1564 static s32 sensor_init_height = 600;
1565 static unsigned long sensor_init_busparam = (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING|SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ);
1566 static enum v4l2_mbus_pixelcode sensor_init_pixelcode = V4L2_MBUS_FMT_YUYV8_2X8;
1567 static struct reginfo* sensor_init_data_p = NULL;
1568 static struct reginfo* sensor_init_winseq_p = NULL;
1569 static struct reginfo* sensor_init_winseq_board = NULL;
1570 static int sensor_init(struct v4l2_subdev *sd, u32 val)
1571 {
1572     struct i2c_client *client = v4l2_get_subdevdata(sd);
1573     struct soc_camera_device *icd = client->dev.platform_data;
1574     struct sensor *sensor = to_sensor(client);
1575         const struct v4l2_queryctrl *qctrl;
1576     const struct sensor_datafmt *fmt;
1577     char value;
1578     int ret,pid = 0,i = 0,j=0;
1579         struct rk29camera_platform_data* tmp_plat_data =(struct rk29camera_platform_data*)val;
1580     sensor_init_data_p = sensor_init_data;
1581         sensor_init_winseq_p = sensor_svga;
1582         sensor_init_width = 800;
1583         sensor_init_height = 600;
1584         if (tmp_plat_data != NULL && (u32)tmp_plat_data != 1) { 
1585                 for(i = 0;i < RK_CAM_NUM;i++){
1586                         if ((tmp_plat_data->sensor_init_data[i])&& tmp_plat_data->info[i].dev_name &&
1587                                 (strcmp(tmp_plat_data->info[i].dev_name, dev_name(icd->pdev)) == 0))
1588                                         break;
1589                         }
1590                 }
1591         if(tmp_plat_data  && ((u32)tmp_plat_data != 1) &&(i < RK_CAM_NUM) && tmp_plat_data->sensor_init_data[i]){
1592         //user has defined the init data
1593                 //init reg
1594                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1595                         for(j = 0;j< sizeof(sensor_init_data)/sizeof(struct reginfo);j++){
1596                                 sensor_init_data[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].reg;
1597                                 sensor_init_data[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data[j].val;
1598                                 }
1599                         sensor_init_data_p = sensor_init_data;
1600                         }
1601                 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data){
1602                         sensor_init_data_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_data);
1603                         }
1604                 //init winseq
1605                 if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq && (sizeof(struct reginfo) != sizeof(struct reginfo_t))){
1606                         int tmp_winseq_size = tmp_plat_data->sensor_init_data[i]->rk_sensor_winseq_size;
1607                         if(sensor_init_winseq_board)
1608                                 {
1609                                 vfree(sensor_init_winseq_board);
1610                                 sensor_init_winseq_board = NULL;
1611                                 }
1612                         sensor_init_winseq_board = (struct reginfo*)vmalloc(tmp_winseq_size);
1613                         if(!sensor_init_winseq_board)
1614                                 SENSOR_TR("%s :vmalloc erro !",__FUNCTION__);
1615                         for(j = 0;j< tmp_winseq_size;j++){
1616                                 sensor_init_winseq_board[j].reg = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].reg;
1617                                 sensor_init_winseq_board[j].val = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq[j].val;
1618                                 }
1619                         sensor_init_winseq_p = sensor_init_winseq_board;
1620                         }
1621                 else if(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq){
1622                         sensor_init_winseq_p = (struct reginfo*)(tmp_plat_data->sensor_init_data[i]->rk_sensor_init_winseq);
1623                         }
1624                 //init width,height,bus,pixelcode
1625                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width != INVALID_VALUE)
1626                         sensor_init_width = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_width;
1627                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height != INVALID_VALUE)
1628                         sensor_init_height = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_height;
1629                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param != INVALID_VALUE)
1630                         sensor_init_busparam = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_bus_param;
1631                 if(tmp_plat_data->sensor_init_data[i] && tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode != INVALID_VALUE)
1632                         sensor_init_pixelcode = tmp_plat_data->sensor_init_data[i]->rk_sensor_init_pixelcode;
1633         }
1634     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
1635
1636         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
1637                 ret = -ENODEV;
1638                 goto sensor_INIT_ERR;
1639         }
1640     mdelay(100);
1641     /* soft reset */
1642         if (sensor_task_lock(client,1)<0)
1643                 goto sensor_INIT_ERR;
1644
1645 #if (SENSOR_RESET_REG != SEQUENCE_END)
1646     struct reginfo reg_info;
1647         reg_info.reg = SENSOR_RESET_REG;
1648         reg_info.val = SENSOR_RESET_VAL;
1649         reg_info.reg_len = SENSOR_RESET_REG_LEN;
1650     ret = sensor_write(client, &reg_info);
1651     if (ret != 0) {
1652         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
1653         ret = -ENODEV;
1654                 goto sensor_INIT_ERR;
1655     }
1656     mdelay(5);  //delay 5 microseconds
1657     reg_info.val = SENSOR_DERESET_VAL;
1658     ret = sensor_write(client, &reg_info);
1659     if (ret != 0) {
1660         SENSOR_TR("%s soft dereset sensor failed\n",SENSOR_NAME_STRING());
1661         ret = -ENODEV;
1662                 goto sensor_INIT_ERR;
1663     }
1664 #endif
1665
1666         /* check if it is an sensor sensor */
1667 #if (SENSOR_ID_REG != SEQUENCE_END)
1668     ret = sensor_read(client, SENSOR_ID_REG, &pid);
1669     if (ret != 0) {
1670         SENSOR_TR("read chip id failed\n");
1671         ret = -ENODEV;
1672         goto sensor_INIT_ERR;
1673     }
1674
1675     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
1676 #else
1677         pid = SENSOR_ID;
1678 #endif
1679     if (pid == SENSOR_ID) {
1680         sensor->model = SENSOR_V4L2_IDENT;
1681     } else {
1682         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
1683         ret = -ENODEV;
1684         goto sensor_INIT_ERR;
1685     }
1686
1687     ret = sensor_write_array(client, sensor_init_data_p);
1688     if (ret != 0)
1689     {
1690         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
1691         goto sensor_INIT_ERR;
1692     }
1693         sensor_task_lock(client,0);
1694     sensor->info_priv.preview_w = SENSOR_INIT_WIDTH;
1695     sensor->info_priv.preview_h = SENSOR_INIT_HEIGHT;
1696     sensor->info_priv.capture_w = SENSOR_MAX_WIDTH;
1697     sensor->info_priv.capture_h = SENSOR_MAX_HEIGHT;
1698     sensor->info_priv.winseqe_cur_addr  = SENSOR_INIT_WINSEQADR;
1699     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
1700     if (!fmt) {
1701         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
1702         ret = -EINVAL;
1703         goto sensor_INIT_ERR;
1704     }
1705         sensor->info_priv.fmt = *fmt;
1706
1707     /* sensor sensor information for initialization  */
1708         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
1709         if (qctrl)
1710         sensor->info_priv.whiteBalance = qctrl->default_value;
1711         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
1712         if (qctrl)
1713         sensor->info_priv.brightness = qctrl->default_value;
1714         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
1715         if (qctrl)
1716         sensor->info_priv.effect = qctrl->default_value;
1717         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
1718         if (qctrl)
1719         sensor->info_priv.exposure = qctrl->default_value;
1720
1721         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
1722         if (qctrl)
1723         sensor->info_priv.saturation = qctrl->default_value;
1724         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
1725         if (qctrl)
1726         sensor->info_priv.contrast = qctrl->default_value;
1727         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
1728         if (qctrl)
1729         sensor->info_priv.mirror = qctrl->default_value;
1730         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
1731         if (qctrl)
1732         sensor->info_priv.flip = qctrl->default_value;
1733         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
1734         if (qctrl)
1735         sensor->info_priv.scene = qctrl->default_value;
1736         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
1737         if (qctrl)
1738         sensor->info_priv.digitalzoom = qctrl->default_value;
1739
1740     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
1741         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
1742         if (qctrl)
1743         sensor->info_priv.focus = qctrl->default_value;
1744         #if CONFIG_SENSOR_Focus
1745         if (sensor_af_init(client) < 0) {
1746                 sensor->info_priv.funmodule_state &= ~SENSOR_AF_IS_OK;
1747                 SENSOR_TR("%s auto focus module init is fail!\n",SENSOR_NAME_STRING());
1748         } else {
1749                 sensor->info_priv.funmodule_state |= SENSOR_AF_IS_OK;
1750                 SENSOR_DG("%s auto focus module init is success!\n",SENSOR_NAME_STRING());
1751         }
1752         #endif
1753         #if CONFIG_SENSOR_Flash
1754         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
1755         if (qctrl)
1756         sensor->info_priv.flash = qctrl->default_value;
1757     #endif
1758     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);
1759
1760     return 0;
1761 sensor_INIT_ERR:
1762         sensor_task_lock(client,0);
1763         sensor_deactivate(client);
1764     return ret;
1765 }
1766 static int sensor_deactivate(struct i2c_client *client)
1767 {
1768         struct soc_camera_device *icd = client->dev.platform_data;
1769
1770         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
1771
1772         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
1773         sensor_ioctrl(icd, Sensor_PowerDown, 1);
1774
1775         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
1776         icd->user_width = SENSOR_INIT_WIDTH;
1777     icd->user_height = SENSOR_INIT_HEIGHT;
1778         msleep(100);
1779         return 0;
1780 }
1781 static  struct reginfo sensor_power_down_sequence[]=
1782 {
1783     {0x00,0x00}
1784 };
1785 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
1786 {
1787     int ret;
1788     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
1789
1790     if (pm_msg.event == PM_EVENT_SUSPEND) {
1791         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
1792         ret = sensor_write_array(client, sensor_power_down_sequence) ;
1793         if (ret != 0) {
1794             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
1795             return ret;
1796         } else {
1797             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
1798             if (ret < 0) {
1799                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
1800                 return -EINVAL;
1801             }
1802         }
1803     } else {
1804         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
1805         return -EINVAL;
1806     }
1807
1808     return 0;
1809 }
1810
1811 static int sensor_resume(struct soc_camera_device *icd)
1812 {
1813         int ret;
1814
1815     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
1816     if (ret < 0) {
1817                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
1818         return -EINVAL;
1819     }
1820
1821         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
1822         return 0;
1823 }
1824
1825 static int sensor_set_bus_param(struct soc_camera_device *icd,
1826                                 unsigned long flags)
1827 {
1828
1829     return 0;
1830 }
1831
1832 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
1833 {
1834     struct soc_camera_link *icl = to_soc_camera_link(icd);
1835     unsigned long flags = SENSOR_BUS_PARAM;
1836
1837     return soc_camera_apply_sensor_flags(icl, flags);
1838 }
1839 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1840 {
1841     struct i2c_client *client = v4l2_get_subdevdata(sd);
1842     struct soc_camera_device *icd = client->dev.platform_data;
1843     struct sensor *sensor = to_sensor(client);
1844
1845     mf->width   = icd->user_width;
1846         mf->height      = icd->user_height;
1847         mf->code        = sensor->info_priv.fmt.code;
1848         mf->colorspace  = sensor->info_priv.fmt.colorspace;
1849         mf->field       = V4L2_FIELD_NONE;
1850
1851     return 0;
1852 }
1853 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1854 {
1855     bool ret = false;
1856
1857         if ((mf->width == 1024) && (mf->height == 768)) {
1858                 ret = true;
1859         } else if ((mf->width == 1280) && (mf->height == 1024)) {
1860                 ret = true;
1861         } else if ((mf->width == 1600) && (mf->height == 1200)) {
1862                 ret = true;
1863         } else if ((mf->width == 2048) && (mf->height == 1536)) {
1864                 ret = true;
1865         } else if ((mf->width == 2592) && (mf->height == 1944)) {
1866                 ret = true;
1867         }
1868
1869         if (ret == true)
1870                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
1871         return ret;
1872 }
1873
1874 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1875 {
1876     bool ret = false;
1877
1878         if ((mf->width == 1280) && (mf->height == 720)) {
1879                 ret = true;
1880         } else if ((mf->width == 1920) && (mf->height == 1080)) {
1881                 ret = true;
1882         }
1883
1884         if (ret == true)
1885                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
1886         return ret;
1887 }
1888 static struct reginfo* sensor_fmt_catch(int set_w, int set_h, int *ret_w, int *ret_h)
1889 {
1890         struct reginfo *winseqe_set_addr = NULL;
1891
1892         if (((set_w <= 176) && (set_h <= 144)) && (sensor_qcif[0].reg!=SEQUENCE_END)) {
1893                 winseqe_set_addr = sensor_qcif;
1894         *ret_w = 176;
1895         *ret_h = 144;
1896         } else if (((set_w <= 320) && (set_h <= 240)) && (sensor_qvga[0].reg!=SEQUENCE_END)) {
1897         winseqe_set_addr = sensor_qvga;
1898         *ret_w = 320;
1899         *ret_h = 240;
1900         } else if (((set_w <= 352) && (set_h<= 288)) && (sensor_cif[0].reg!=SEQUENCE_END)) {
1901         winseqe_set_addr = sensor_cif;
1902         *ret_w = 352;
1903         *ret_h = 288;
1904     } else if (((set_w <= 640) && (set_h <= 480)) && (sensor_vga[0].reg!=SEQUENCE_END)) {
1905         winseqe_set_addr = sensor_vga;
1906         *ret_w = 640;
1907         *ret_h = 480;
1908     } else if (((set_w <= 800) && (set_h <= 600)) && (sensor_svga[0].reg!=SEQUENCE_END)) {
1909         printk("enter 800x600\n");
1910         winseqe_set_addr = sensor_svga;
1911         *ret_w = 800;
1912         *ret_h = 600;
1913     } else if (((set_w <= 1024) && (set_h <= 768)) && (sensor_xga[0].reg!=SEQUENCE_END)) {
1914         winseqe_set_addr = sensor_xga;
1915         *ret_w = 1024;
1916         *ret_h = 768;
1917         } else if (((set_w <= 1280) && (set_h <= 720)) && (sensor_720p[0].reg!=SEQUENCE_END)) {
1918         printk("enter 1280X720\n");
1919         winseqe_set_addr = sensor_720p;
1920         *ret_w = 1280;
1921         *ret_h = 720;
1922     } else if (((set_w <= 1280) && (set_h <= 1024)) && (sensor_sxga[0].reg!=SEQUENCE_END)) {
1923         printk("enter 1280X1024\n");
1924         winseqe_set_addr = sensor_sxga;
1925         *ret_w = 1280;
1926         *ret_h = 1024;
1927     } else if (((set_w <= 1600) && (set_h <= 1200)) && (sensor_uxga[0].reg!=SEQUENCE_END)) {
1928         winseqe_set_addr = sensor_uxga;
1929         *ret_w = 1600;
1930         *ret_h = 1200;
1931         } else if (((set_w <= 1920) && (set_h <= 1080)) && (sensor_1080p[0].reg!=SEQUENCE_END)) {
1932       winseqe_set_addr = sensor_1080p;
1933         *ret_w = 1920;
1934         *ret_h = 1080;
1935     } else if (((set_w <= 2048) && (set_h <= 1536)) && (sensor_qxga[0].reg!=SEQUENCE_END)) {
1936         winseqe_set_addr = sensor_qxga;
1937         *ret_w = 2048;
1938         *ret_h = 1536;
1939     } else if (((set_w <= 2592) && (set_h <= 1944)) && (sensor_qsxga[0].reg!=SEQUENCE_END)) {
1940         winseqe_set_addr = sensor_qsxga;
1941         *ret_w = 2592;
1942         *ret_h = 1944;
1943     }
1944
1945         return winseqe_set_addr;
1946 }
1947
1948 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
1949 {
1950     struct i2c_client *client = v4l2_get_subdevdata(sd);
1951     struct sensor *sensor = to_sensor(client);
1952     const struct sensor_datafmt *fmt;
1953     struct reginfo *winseqe_set_addr=NULL;
1954     int ret = 0, set_w,set_h;
1955
1956     SENSOR_TR("%s pix->width=%d,pix->height=%d\n",__FUNCTION__,mf->width,mf->height);
1957
1958         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
1959                                    ARRAY_SIZE(sensor_colour_fmts));
1960         if (!fmt) {
1961         ret = -EINVAL;
1962         goto sensor_s_fmt_end;
1963     }
1964
1965         if (sensor->info_priv.fmt.code != mf->code) {
1966                 switch (mf->code)
1967                 {
1968                         case V4L2_MBUS_FMT_YUYV8_2X8:
1969                         {
1970                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
1971                                 break;
1972                         }
1973                         case V4L2_MBUS_FMT_UYVY8_2X8:
1974                         {
1975                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
1976                                 break;
1977                         }
1978                         default:
1979                                 break;
1980                 }
1981                 if (winseqe_set_addr != NULL) {
1982             sensor_write_array(client, winseqe_set_addr);
1983                         sensor->info_priv.fmt.code = mf->code;
1984             sensor->info_priv.fmt.colorspace= mf->colorspace;            
1985                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
1986                 } else {
1987                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
1988                 }
1989         }
1990
1991     set_w = mf->width;
1992     set_h = mf->height;
1993
1994         winseqe_set_addr = sensor_fmt_catch(set_w, set_h, &set_w, &set_h);
1995
1996     if ((winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) && winseqe_set_addr) {
1997         ret |= sensor_write_array(client, winseqe_set_addr);
1998         if (ret != 0) {
1999             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
2000             goto sensor_s_fmt_end;
2001         }
2002         sensor->info_priv.winseqe_cur_addr  = winseqe_set_addr;
2003                 if ((winseqe_set_addr[0].reg==SEQUENCE_PROPERTY) && (winseqe_set_addr[0].val==SEQUENCE_CAPTURE)) {
2004                 SENSOR_DG("\n%s..%s..Capture icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2005                 } else {
2006                         SENSOR_DG("\n%s..%s..Video icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
2007                         sensor->info_priv.preview_w = mf->width;
2008                         sensor->info_priv.preview_h = mf->height;
2009                 }
2010     }
2011
2012         if (winseqe_set_addr && (winseqe_set_addr[0].reg==SEQUENCE_PROPERTY) && (winseqe_set_addr[0].val==SEQUENCE_CAPTURE)) {
2013                 ret |= sensor_write_array(client, sensor_Preview2Capture);
2014                 if (ret != 0) {
2015                 SENSOR_TR("%s Preview 2 Capture failed\n", SENSOR_NAME_STRING());
2016                 goto sensor_s_fmt_end;
2017         }
2018                 sensor->info_priv.capture_w = set_w;
2019                 sensor->info_priv.capture_h = set_h;
2020                 sensor->info_priv.snap2preview = true;
2021         } else if (sensor->info_priv.snap2preview == true) {
2022                 if (winseqe_set_addr || ((sensor->info_priv.preview_w == mf->width) && (sensor->info_priv.preview_h == mf->height))) {
2023                         ret |= sensor_write_array(client, sensor_Capture2Preview);
2024                         if (ret != 0) {
2025                         SENSOR_TR("%s Capture 2 Preview failed\n", SENSOR_NAME_STRING());
2026                         goto sensor_s_fmt_end;
2027                 }
2028                         sensor->info_priv.preview_w = mf->width;
2029                         sensor->info_priv.preview_h = mf->height;
2030                         sensor->info_priv.snap2preview = false;
2031                 } else {
2032                         SENSOR_TR("\n %s..%s Format is Invalidate. mf->width = %d.. mf->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
2033                 }
2034         }
2035
2036         mf->width = set_w;
2037         mf->height = set_h;
2038 sensor_s_fmt_end:
2039     return ret;
2040 }
2041
2042 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2043 {
2044         int ret = 0;
2045     struct i2c_client *client = v4l2_get_subdevdata(sd);
2046     struct sensor *sensor = to_sensor(client);
2047     const struct sensor_datafmt *fmt;
2048    
2049         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
2050                                    ARRAY_SIZE(sensor_colour_fmts));
2051         if (fmt == NULL) {
2052                 fmt = &sensor->info_priv.fmt;
2053         mf->code = fmt->code;
2054         } 
2055
2056     SENSOR_TR("%s pix->width=%d,pix->height=%d\n",__FUNCTION__,mf->width,mf->height);
2057
2058     /*
2059     * With Bayer format enforce even side lengths, but let the user play
2060     * with the starting pixel
2061     */
2062
2063     if (mf->height > SENSOR_MAX_HEIGHT)
2064         mf->height = SENSOR_MAX_HEIGHT;
2065     else if (mf->height < SENSOR_MIN_HEIGHT)
2066         mf->height = SENSOR_MIN_HEIGHT;
2067
2068     if (mf->width > SENSOR_MAX_WIDTH)
2069         mf->width = SENSOR_MAX_WIDTH;
2070     else if (mf->width < SENSOR_MIN_WIDTH)
2071         mf->width = SENSOR_MIN_WIDTH;
2072
2073         if (sensor_fmt_catch(mf->width, mf->height, &mf->width, &mf->height) == NULL) {
2074         printk("%s[%d] ERR!!!!!!",__FUNCTION__,__LINE__);
2075                 mf->width = 0;
2076                 mf->height = 0;
2077         }
2078
2079     return ret;
2080 }
2081
2082  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
2083 {
2084     struct i2c_client *client = v4l2_get_subdevdata(sd);
2085
2086     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
2087         return -EINVAL;
2088
2089     if (id->match.addr != client->addr)
2090         return -ENODEV;
2091
2092     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
2093     id->revision = 0;
2094
2095     return 0;
2096 }
2097 #if CONFIG_SENSOR_Brightness
2098 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2099 {
2100     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2101
2102     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2103     {
2104         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
2105         {
2106             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
2107             {
2108                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2109                 return -EINVAL;
2110             }
2111             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2112             return 0;
2113         }
2114     }
2115         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2116     return -EINVAL;
2117 }
2118 #endif
2119 #if CONFIG_SENSOR_Effect
2120 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2121 {
2122     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2123
2124     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2125     {
2126         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
2127         {
2128             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
2129             {
2130                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2131                 return -EINVAL;
2132             }
2133             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2134             return 0;
2135         }
2136     }
2137         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2138     return -EINVAL;
2139 }
2140 #endif
2141 #if CONFIG_SENSOR_Exposure
2142 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2143 {
2144     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2145
2146     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2147     {
2148         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
2149         {
2150             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
2151             {
2152                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2153                 return -EINVAL;
2154             }
2155             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2156             return 0;
2157         }
2158     }
2159         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2160     return -EINVAL;
2161 }
2162 #endif
2163 #if CONFIG_SENSOR_Saturation
2164 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2165 {
2166     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2167
2168     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2169     {
2170         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
2171         {
2172             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
2173             {
2174                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2175                 return -EINVAL;
2176             }
2177             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2178             return 0;
2179         }
2180     }
2181     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2182     return -EINVAL;
2183 }
2184 #endif
2185 #if CONFIG_SENSOR_Contrast
2186 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2187 {
2188     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2189
2190     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2191     {
2192         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
2193         {
2194             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
2195             {
2196                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2197                 return -EINVAL;
2198             }
2199             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2200             return 0;
2201         }
2202     }
2203     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2204     return -EINVAL;
2205 }
2206 #endif
2207 #if CONFIG_SENSOR_Mirror
2208 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2209 {
2210     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2211
2212     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2213     {
2214         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
2215         {
2216             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
2217             {
2218                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2219                 return -EINVAL;
2220             }
2221             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2222             return 0;
2223         }
2224     }
2225     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2226     return -EINVAL;
2227 }
2228 #endif
2229 #if CONFIG_SENSOR_Flip
2230 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2231 {
2232     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2233
2234     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2235     {
2236         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
2237         {
2238             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
2239             {
2240                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2241                 return -EINVAL;
2242             }
2243             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2244             return 0;
2245         }
2246     }
2247     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2248     return -EINVAL;
2249 }
2250 #endif
2251 #if CONFIG_SENSOR_Scene
2252 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2253 {
2254     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2255
2256     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2257     {
2258         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
2259         {
2260             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
2261             {
2262                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2263                 return -EINVAL;
2264             }
2265             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2266             return 0;
2267         }
2268     }
2269     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2270     return -EINVAL;
2271 }
2272 #endif
2273 #if CONFIG_SENSOR_WhiteBalance
2274 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2275 {
2276     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2277
2278     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
2279     {
2280         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
2281         {
2282             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
2283             {
2284                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
2285                 return -EINVAL;
2286             }
2287             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2288             return 0;
2289         }
2290     }
2291         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2292     return -EINVAL;
2293 }
2294 #endif
2295 #if CONFIG_SENSOR_DigitalZoom
2296 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2297 {
2298     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2299     struct sensor *sensor = to_sensor(client);
2300         const struct v4l2_queryctrl *qctrl_info;
2301     int digitalzoom_cur, digitalzoom_total;
2302
2303         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2304         if (qctrl_info)
2305                 return -EINVAL;
2306
2307     digitalzoom_cur = sensor->info_priv.digitalzoom;
2308     digitalzoom_total = qctrl_info->maximum;
2309
2310     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
2311     {
2312         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2313         return -EINVAL;
2314     }
2315
2316     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
2317     {
2318         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
2319         return -EINVAL;
2320     }
2321
2322     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
2323     {
2324         *value = digitalzoom_total - digitalzoom_cur;
2325     }
2326
2327     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
2328     {
2329         *value = 0 - digitalzoom_cur;
2330     }
2331
2332     digitalzoom_cur += *value;
2333
2334     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
2335     {
2336         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 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     return -EINVAL;
2346 }
2347 #endif
2348 #if CONFIG_SENSOR_Flash
2349 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
2350 {
2351     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2352     struct sensor *sensor = to_sensor(client);
2353         const struct v4l2_queryctrl *qctrl_info;
2354     
2355     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
2356         if (value == 3) {       /* ddl@rock-chips.com: torch */
2357             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
2358         } else {
2359             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
2360         }
2361         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
2362         return 0;
2363     }
2364     
2365         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2366     return -EINVAL;
2367 }
2368 #endif
2369 #if CONFIG_SENSOR_Focus
2370 static int sensor_set_focus_absolute(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2371 {
2372         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2373     struct sensor *sensor = to_sensor(client);
2374         const struct v4l2_queryctrl *qctrl_info;
2375         int ret = 0;
2376
2377         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2378         if (!qctrl_info)
2379                 return -EINVAL;
2380
2381         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2382                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2383
2384                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2385                 } else {
2386                         ret = -EINVAL;
2387                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2388                 }
2389         } else {
2390                 ret = -EACCES;
2391                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2392                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2393         }
2394
2395 sensor_set_focus_absolute_end:
2396         return ret;
2397 }
2398 static int sensor_set_focus_relative(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2399 {
2400         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2401         struct sensor *sensor = to_sensor(client);
2402         const struct v4l2_queryctrl *qctrl_info;
2403         int ret = 0;
2404
2405         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_RELATIVE);
2406         if (!qctrl_info)
2407                 return -EINVAL;
2408
2409         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK) && (sensor->info_priv.affm_reinit == 0)) {
2410                 if ((value >= qctrl_info->minimum) && (value <= qctrl_info->maximum)) {
2411
2412                         SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2413                 } else {
2414                         ret = -EINVAL;
2415                         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2416                 }
2417         } else {
2418                 ret = -EACCES;
2419                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2420                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2421         }
2422 sensor_set_focus_relative_end:
2423         return ret;
2424 }
2425
2426 static int sensor_set_focus_mode(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
2427 {
2428         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2429         struct sensor *sensor = to_sensor(client);
2430         int ret = 0;
2431
2432         if ((sensor->info_priv.funmodule_state & SENSOR_AF_IS_OK)  && (sensor->info_priv.affm_reinit == 0)) {
2433                 switch (value)
2434                 {
2435                         case SENSOR_AF_MODE_AUTO:
2436                         {
2437                                 ret = sensor_af_single(client);
2438                                 break;
2439                         }
2440
2441                         case SENSOR_AF_MODE_MACRO:
2442                         {
2443                                 ret = sensor_set_focus_absolute(icd, qctrl, 0xff);
2444                                 break;
2445                         }
2446
2447                         case SENSOR_AF_MODE_INFINITY:
2448                         {
2449                                 ret = sensor_set_focus_absolute(icd, qctrl, 0x00);
2450                                 break;
2451                         }
2452
2453                         case SENSOR_AF_MODE_CONTINUOUS:
2454                         {
2455                                 ret = sensor_af_const(client);
2456                                 break;
2457                         }
2458                         default:
2459                                 SENSOR_TR("\n %s..%s AF value(0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,value);
2460                                 break;
2461
2462                 }
2463
2464                 SENSOR_DG("%s..%s : %d  ret:0x%x\n",SENSOR_NAME_STRING(),__FUNCTION__, value,ret);
2465         } else {
2466                 ret = -EACCES;
2467                 SENSOR_TR("\n %s..%s AF module state(0x%x, 0x%x) is error!\n",SENSOR_NAME_STRING(),__FUNCTION__,
2468                         sensor->info_priv.funmodule_state,sensor->info_priv.affm_reinit);
2469         }
2470
2471         return ret;
2472 }
2473 #endif
2474 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2475 {
2476     struct i2c_client *client = v4l2_get_subdevdata(sd);
2477     struct sensor *sensor = to_sensor(client);
2478     const struct v4l2_queryctrl *qctrl;
2479
2480     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2481
2482     if (!qctrl)
2483     {
2484         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2485         return -EINVAL;
2486     }
2487
2488     switch (ctrl->id)
2489     {
2490         case V4L2_CID_BRIGHTNESS:
2491             {
2492                 ctrl->value = sensor->info_priv.brightness;
2493                 break;
2494             }
2495         case V4L2_CID_SATURATION:
2496             {
2497                 ctrl->value = sensor->info_priv.saturation;
2498                 break;
2499             }
2500         case V4L2_CID_CONTRAST:
2501             {
2502                 ctrl->value = sensor->info_priv.contrast;
2503                 break;
2504             }
2505         case V4L2_CID_DO_WHITE_BALANCE:
2506             {
2507                 ctrl->value = sensor->info_priv.whiteBalance;
2508                 break;
2509             }
2510         case V4L2_CID_EXPOSURE:
2511             {
2512                 ctrl->value = sensor->info_priv.exposure;
2513                 break;
2514             }
2515         case V4L2_CID_HFLIP:
2516             {
2517                 ctrl->value = sensor->info_priv.mirror;
2518                 break;
2519             }
2520         case V4L2_CID_VFLIP:
2521             {
2522                 ctrl->value = sensor->info_priv.flip;
2523                 break;
2524             }
2525         default :
2526                 break;
2527     }
2528     return 0;
2529 }
2530
2531
2532
2533 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
2534 {
2535     struct i2c_client *client = v4l2_get_subdevdata(sd);
2536     const struct v4l2_queryctrl *qctrl;   
2537     struct sensor *sensor = to_sensor(client);
2538     struct soc_camera_device *icd = client->dev.platform_data;
2539     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
2540
2541     if (!qctrl)
2542     {
2543         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
2544         return -EINVAL;
2545     }
2546
2547     switch (ctrl->id)
2548     {
2549 #if CONFIG_SENSOR_Brightness
2550         case V4L2_CID_BRIGHTNESS:
2551             {
2552                 if (ctrl->value != sensor->info_priv.brightness)
2553                 {
2554                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
2555                     {
2556                         return -EINVAL;
2557                     }
2558                     sensor->info_priv.brightness = ctrl->value;
2559                 }
2560                 break;
2561             }
2562 #endif
2563 #if CONFIG_SENSOR_Exposure
2564         case V4L2_CID_EXPOSURE:
2565             {
2566                 if (ctrl->value != sensor->info_priv.exposure)
2567                 {
2568                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
2569                     {
2570                         return -EINVAL;
2571                     }
2572                     sensor->info_priv.exposure = ctrl->value;
2573                 }
2574                 break;
2575             }
2576 #endif
2577 #if CONFIG_SENSOR_Saturation
2578         case V4L2_CID_SATURATION:
2579             {
2580                 if (ctrl->value != sensor->info_priv.saturation)
2581                 {
2582                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
2583                     {
2584                         return -EINVAL;
2585                     }
2586                     sensor->info_priv.saturation = ctrl->value;
2587                 }
2588                 break;
2589             }
2590 #endif
2591 #if CONFIG_SENSOR_Contrast
2592         case V4L2_CID_CONTRAST:
2593             {
2594                 if (ctrl->value != sensor->info_priv.contrast)
2595                 {
2596                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
2597                     {
2598                         return -EINVAL;
2599                     }
2600                     sensor->info_priv.contrast = ctrl->value;
2601                 }
2602                 break;
2603             }
2604 #endif
2605 #if CONFIG_SENSOR_WhiteBalance
2606         case V4L2_CID_DO_WHITE_BALANCE:
2607             {
2608                 if (ctrl->value != sensor->info_priv.whiteBalance)
2609                 {
2610                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
2611                     {
2612                         return -EINVAL;
2613                     }
2614                     sensor->info_priv.whiteBalance = ctrl->value;
2615                 }
2616                 break;
2617             }
2618 #endif
2619 #if CONFIG_SENSOR_Mirror
2620         case V4L2_CID_HFLIP:
2621             {
2622                 if (ctrl->value != sensor->info_priv.mirror)
2623                 {
2624                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
2625                         return -EINVAL;
2626                     sensor->info_priv.mirror = ctrl->value;
2627                 }
2628                 break;
2629             }
2630 #endif
2631 #if CONFIG_SENSOR_Flip
2632         case V4L2_CID_VFLIP:
2633             {
2634                 if (ctrl->value != sensor->info_priv.flip)
2635                 {
2636                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
2637                         return -EINVAL;
2638                     sensor->info_priv.flip = ctrl->value;
2639                 }
2640                 break;
2641             }
2642 #endif
2643         default:
2644             break;
2645     }
2646
2647     return 0;
2648 }
2649 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
2650 {
2651     const struct v4l2_queryctrl *qctrl;
2652     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2653     struct sensor *sensor = to_sensor(client);
2654
2655     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2656
2657     if (!qctrl)
2658     {
2659         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2660         return -EINVAL;
2661     }
2662
2663     switch (ext_ctrl->id)
2664     {
2665         case V4L2_CID_SCENE:
2666             {
2667                 ext_ctrl->value = sensor->info_priv.scene;
2668                 break;
2669             }
2670         case V4L2_CID_EFFECT:
2671             {
2672                 ext_ctrl->value = sensor->info_priv.effect;
2673                 break;
2674             }
2675         case V4L2_CID_ZOOM_ABSOLUTE:
2676             {
2677                 ext_ctrl->value = sensor->info_priv.digitalzoom;
2678                 break;
2679             }
2680         case V4L2_CID_ZOOM_RELATIVE:
2681             {
2682                 return -EINVAL;
2683             }
2684         case V4L2_CID_FOCUS_ABSOLUTE:
2685             {
2686                 return -EINVAL;
2687             }
2688         case V4L2_CID_FOCUS_RELATIVE:
2689             {
2690                 return -EINVAL;
2691             }
2692         case V4L2_CID_FLASH:
2693             {
2694                 ext_ctrl->value = sensor->info_priv.flash;
2695                 break;
2696             }
2697         default :
2698             break;
2699     }
2700     return 0;
2701 }
2702 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
2703 {
2704     const struct v4l2_queryctrl *qctrl;
2705     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2706     struct sensor *sensor = to_sensor(client);
2707     int val_offset;      
2708
2709     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
2710
2711     if (!qctrl)
2712     {
2713         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
2714         return -EINVAL;
2715     }
2716
2717         val_offset = 0;
2718     switch (ext_ctrl->id)
2719     {
2720 #if CONFIG_SENSOR_Scene
2721         case V4L2_CID_SCENE:
2722             {
2723                 if (ext_ctrl->value != sensor->info_priv.scene)
2724                 {
2725                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
2726                         return -EINVAL;
2727                     sensor->info_priv.scene = ext_ctrl->value;
2728                 }
2729                 break;
2730             }
2731 #endif
2732 #if CONFIG_SENSOR_Effect
2733         case V4L2_CID_EFFECT:
2734             {
2735                 if (ext_ctrl->value != sensor->info_priv.effect)
2736                 {
2737                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
2738                         return -EINVAL;
2739                     sensor->info_priv.effect= ext_ctrl->value;
2740                 }
2741                 break;
2742             }
2743 #endif
2744 #if CONFIG_SENSOR_DigitalZoom
2745         case V4L2_CID_ZOOM_ABSOLUTE:
2746             {
2747                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2748                     return -EINVAL;
2749
2750                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
2751                 {
2752                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
2753
2754                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
2755                         return -EINVAL;
2756                     sensor->info_priv.digitalzoom += val_offset;
2757
2758                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
2759                 }
2760
2761                 break;
2762             }
2763         case V4L2_CID_ZOOM_RELATIVE:
2764             {
2765                 if (ext_ctrl->value)
2766                 {
2767                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
2768                         return -EINVAL;
2769                     sensor->info_priv.digitalzoom += ext_ctrl->value;
2770
2771                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
2772                 }
2773                 break;
2774             }
2775 #endif
2776 #if CONFIG_SENSOR_Focus
2777         case V4L2_CID_FOCUS_ABSOLUTE:
2778             {
2779                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2780                     return -EINVAL;
2781
2782                                 if (sensor_set_focus_absolute(icd, qctrl,ext_ctrl->value) == 0) {
2783                                         if (ext_ctrl->value == qctrl->minimum) {
2784                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_INFINITY;
2785                                         } else if (ext_ctrl->value == qctrl->maximum) {
2786                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_MACRO;
2787                                         } else {
2788                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_FIXED;
2789                                         }
2790                                 }
2791
2792                 break;
2793             }
2794         case V4L2_CID_FOCUS_RELATIVE:
2795             {
2796                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
2797                     return -EINVAL;
2798
2799                 sensor_set_focus_relative(icd, qctrl,ext_ctrl->value);
2800                 break;
2801             }
2802                 case V4L2_CID_FOCUS_AUTO:
2803                         {
2804                                 if (ext_ctrl->value == 1) {
2805                                         if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_AUTO) != 0)
2806                                                 return -EINVAL;
2807                                         sensor->info_priv.auto_focus = SENSOR_AF_MODE_AUTO;
2808                                 } else if (SENSOR_AF_MODE_AUTO == sensor->info_priv.auto_focus){
2809                                         if (ext_ctrl->value == 0)
2810                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
2811                                 }
2812                                 break;
2813                         }
2814                 case V4L2_CID_FOCUS_CONTINUOUS:
2815                         {
2816                                 if (SENSOR_AF_MODE_CONTINUOUS != sensor->info_priv.auto_focus) {
2817                                         if (ext_ctrl->value == 1) {
2818                                                 if (sensor_set_focus_mode(icd, qctrl,SENSOR_AF_MODE_CONTINUOUS) != 0)
2819                                                         return -EINVAL;
2820                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CONTINUOUS;
2821                                         }
2822                                 } else {
2823                                         if (ext_ctrl->value == 0)
2824                                                 sensor->info_priv.auto_focus = SENSOR_AF_MODE_CLOSE;
2825                                 }
2826                                 break;
2827                         }
2828 #endif
2829 #if CONFIG_SENSOR_Flash
2830         case V4L2_CID_FLASH:
2831             {
2832                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
2833                     return -EINVAL;
2834                 sensor->info_priv.flash = ext_ctrl->value;
2835
2836                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
2837                 break;
2838             }
2839 #endif
2840         default:
2841             break;
2842     }
2843
2844     return 0;
2845 }
2846
2847 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2848 {
2849     struct i2c_client *client = v4l2_get_subdevdata(sd);
2850     struct soc_camera_device *icd = client->dev.platform_data;
2851     int i, error_cnt=0, error_idx=-1;
2852
2853
2854     for (i=0; i<ext_ctrl->count; i++) {
2855         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2856             error_cnt++;
2857             error_idx = i;
2858         }
2859     }
2860
2861     if (error_cnt > 1)
2862         error_idx = ext_ctrl->count;
2863
2864     if (error_idx != -1) {
2865         ext_ctrl->error_idx = error_idx;
2866         return -EINVAL;
2867     } else {
2868         return 0;
2869     }
2870 }
2871
2872 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
2873 {
2874     struct i2c_client *client = v4l2_get_subdevdata(sd);
2875     struct soc_camera_device *icd = client->dev.platform_data;
2876     int i, error_cnt=0, error_idx=-1;
2877
2878     for (i=0; i<ext_ctrl->count; i++) {
2879         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
2880             error_cnt++;
2881             error_idx = i;
2882         }
2883     }
2884
2885     if (error_cnt > 1)
2886         error_idx = ext_ctrl->count;
2887
2888     if (error_idx != -1) {
2889         ext_ctrl->error_idx = error_idx;
2890         return -EINVAL;
2891     } else {
2892         return 0;
2893     }
2894 }
2895
2896 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
2897 {
2898         struct i2c_client *client = v4l2_get_subdevdata(sd);
2899     struct sensor *sensor = to_sensor(client);
2900
2901         if (enable == 1) {
2902                 sensor->info_priv.enable = 1;
2903         } else if (enable == 0) {
2904                 sensor->info_priv.enable = 0;
2905         }
2906
2907         return 0;
2908 }
2909
2910 /* Interface active, can use i2c. If it fails, it can indeed mean, that
2911  * this wasn't our capture interface, so, we wait for the right one */
2912 static int sensor_video_probe(struct soc_camera_device *icd,
2913                                struct i2c_client *client)
2914 {
2915     int ret=0;
2916     struct sensor *sensor = to_sensor(client);
2917 #if (SENSOR_ID_REG != SEQUENCE_END)
2918     u16 pid;
2919 #endif
2920
2921     /* We must have a parent by now. And it cannot be a wrong one.
2922      * So this entire test is completely redundant. */
2923     if (!icd->dev.parent ||
2924             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
2925                 return -ENODEV;
2926
2927         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2928                 ret = -ENODEV;
2929                 goto sensor_video_probe_err;
2930         }
2931
2932     /* soft reset */
2933 #if (SENSOR_RESET_REG != SEQUENCE_END)
2934     struct reginfo reg_info;
2935         reg_info.reg = SENSOR_RESET_REG;
2936         reg_info.val = SENSOR_RESET_VAL;
2937         reg_info.reg_len = SENSOR_RESET_REG_LEN;
2938     ret = sensor_write(client, &reg_info);
2939     if (ret != 0) {
2940         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2941         ret = -ENODEV;
2942                 goto sensor_video_probe_err;
2943     }
2944
2945     mdelay(5);  //delay 5 microseconds
2946 #endif
2947
2948         /* check if it is an sensor sensor */
2949 #if (SENSOR_ID_REG != SEQUENCE_END)
2950     ret = sensor_read(client, SENSOR_ID_REG, &pid);
2951     if (ret != 0) {
2952         SENSOR_TR("read chip id failed\n");
2953         ret = -ENODEV;
2954         goto sensor_video_probe_err;
2955     }
2956
2957     SENSOR_DG("\n %s  pid = 0x%x \n", SENSOR_NAME_STRING(), pid);
2958 #else
2959         pid = SENSOR_ID;
2960 #endif
2961
2962     if (pid == SENSOR_ID) {
2963         sensor->model = SENSOR_V4L2_IDENT;
2964     } else {
2965         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2966         ret = -ENODEV;
2967         goto sensor_video_probe_err;
2968     }
2969
2970
2971
2972     return 0;
2973
2974 sensor_video_probe_err:
2975
2976     return ret;
2977 }
2978 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
2979 {
2980         struct i2c_client *client = v4l2_get_subdevdata(sd);
2981     struct soc_camera_device *icd = client->dev.platform_data;
2982     struct sensor *sensor = to_sensor(client);
2983     int ret = 0;
2984     
2985         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2986         switch (cmd)
2987         {
2988                 case RK29_CAM_SUBDEV_DEACTIVATE:
2989                 {
2990                         sensor_deactivate(client);
2991                         break;
2992                 }
2993                 case RK29_CAM_SUBDEV_IOREQUEST:
2994                 {
2995                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;                       
2996                         if (sensor->sensor_io_request != NULL) { 
2997                         int j = 0;
2998                         for(j = 0;j < RK_CAM_NUM;j++){
2999                                         if (sensor->sensor_io_request->gpio_res[j].dev_name && 
3000                                                 (strcmp(sensor->sensor_io_request->gpio_res[j].dev_name, dev_name(icd->pdev)) == 0)) {
3001                                                 sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[j];
3002                                 break;
3003                                           } 
3004                         }
3005                         if(j == RK_CAM_NUM){
3006                                 SENSOR_TR("%s %s RK_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
3007                                 ret = -EINVAL;
3008                                 goto sensor_ioctl_end;
3009                                 }
3010                                 }
3011             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
3012                for this project */
3013             #if CONFIG_SENSOR_Flash     
3014             int i;
3015                 if (sensor->sensor_gpio_res) {
3016                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
3017                     for (i = 0; i < icd->ops->num_controls; i++) {
3018                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
3019                                         memset(&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                      
3020                                 }
3021                     }
3022                     sensor->info_priv.flash = 0xff;
3023                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
3024                 }
3025                 }
3026             #endif
3027                         break;
3028                 }
3029                 default:
3030                 {
3031                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3032                         break;
3033                 }
3034         }
3035 sensor_ioctl_end:
3036         return ret;
3037
3038 }
3039 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
3040                             enum v4l2_mbus_pixelcode *code)
3041 {
3042         if (index >= ARRAY_SIZE(sensor_colour_fmts))
3043                 return -EINVAL;
3044
3045         *code = sensor_colour_fmts[index].code;
3046         return 0;
3047 }
3048 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
3049         .init           = sensor_init,
3050         .g_ctrl         = sensor_g_control,
3051         .s_ctrl         = sensor_s_control,
3052         .g_ext_ctrls          = sensor_g_ext_controls,
3053         .s_ext_ctrls          = sensor_s_ext_controls,
3054         .g_chip_ident   = sensor_g_chip_ident,
3055         .ioctl = sensor_ioctl,
3056 };
3057
3058 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
3059         .s_mbus_fmt     = sensor_s_fmt,
3060         .g_mbus_fmt     = sensor_g_fmt,
3061         .try_mbus_fmt   = sensor_try_fmt,
3062         .enum_mbus_fmt  = sensor_enum_fmt,
3063 };
3064 static struct v4l2_subdev_ops sensor_subdev_ops = {
3065         .core   = &sensor_subdev_core_ops,
3066         .video = &sensor_subdev_video_ops,
3067 };
3068
3069 static int sensor_probe(struct i2c_client *client,
3070                          const struct i2c_device_id *did)
3071 {
3072     struct sensor *sensor;
3073     struct soc_camera_device *icd = client->dev.platform_data;
3074     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
3075     struct soc_camera_link *icl;
3076     int ret;
3077
3078     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
3079     if (!icd) {
3080         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
3081         return -EINVAL;
3082     }
3083
3084     icl = to_soc_camera_link(icd);
3085     if (!icl) {
3086         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
3087         return -EINVAL;
3088     }
3089
3090     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
3091         dev_warn(&adapter->dev,
3092                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
3093         return -EIO;
3094     }
3095
3096     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
3097     if (!sensor)
3098         return -ENOMEM;
3099
3100     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
3101
3102     /* Second stage probe - when a capture adapter is there */
3103     icd->ops            = &sensor_ops;
3104     sensor->info_priv.fmt = sensor_colour_fmts[0];
3105         #if CONFIG_SENSOR_I2C_NOSCHED
3106         atomic_set(&sensor->tasklock_cnt,0);
3107         #endif
3108
3109     ret = sensor_video_probe(icd, client);
3110     if (ret < 0) {
3111         icd->ops = NULL;
3112         i2c_set_clientdata(client, NULL);
3113         kfree(sensor);
3114                 sensor = NULL;
3115     }
3116     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
3117     return ret;
3118 }
3119
3120 static int sensor_remove(struct i2c_client *client)
3121 {
3122     struct sensor *sensor = to_sensor(client);
3123     struct soc_camera_device *icd = client->dev.platform_data;
3124
3125         #if CONFIG_SENSOR_Focus
3126         if (sensor->sensor_wq) {
3127                 destroy_workqueue(sensor->sensor_wq);
3128                 sensor->sensor_wq = NULL;
3129         }
3130         #endif
3131
3132     icd->ops = NULL;
3133     i2c_set_clientdata(client, NULL);
3134     client->driver = NULL;
3135     kfree(sensor);
3136         sensor = NULL;
3137     return 0;
3138 }
3139
3140 static const struct i2c_device_id sensor_id[] = {
3141         {SENSOR_NAME_STRING(), 0 },
3142         { }
3143 };
3144 MODULE_DEVICE_TABLE(i2c, sensor_id);
3145
3146 static struct i2c_driver sensor_i2c_driver = {
3147         .driver = {
3148                 .name = SENSOR_NAME_STRING(),
3149         },
3150         .probe          = sensor_probe,
3151         .remove         = sensor_remove,
3152         .id_table       = sensor_id,
3153 };
3154
3155 static int __init sensor_mod_init(void)
3156 {
3157     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
3158     return i2c_add_driver(&sensor_i2c_driver);
3159 }
3160
3161 static void __exit sensor_mod_exit(void)
3162 {
3163     i2c_del_driver(&sensor_i2c_driver);
3164 }
3165
3166 device_initcall_sync(sensor_mod_init);
3167 module_exit(sensor_mod_exit);
3168
3169 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
3170 MODULE_AUTHOR("ddl <kernel@rock-chips>");
3171 MODULE_LICENSE("GPL");
3172
3173