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