Revert "camera driver v0.2.1 for RK30"
[firefly-linux-kernel-4.4.55.git] / drivers / media / video / ov2640.c
1 /*
2 <<<<<<< HEAD
3  * ov2640 Camera Driver
4  *
5  * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
6  *
7  * Based on ov772x, ov9640 drivers and previous non merged implementations.
8  *
9  * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
10  * Copyright (C) 2006, OmniVision
11 =======
12 o* Driver for MT9M001 CMOS Image Sensor from Micron
13  *
14  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
15 >>>>>>> parent of 15f7fab... temp revert rk change
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License version 2 as
19  * published by the Free Software Foundation.
20  */
21
22 <<<<<<< HEAD
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-chip-ident.h>
30 #include <media/v4l2-subdev.h>
31 #include <media/soc_camera.h>
32 #include <media/soc_mediabus.h>
33
34 #define VAL_SET(x, mask, rshift, lshift)  \
35                 ((((x) >> rshift) & mask) << lshift)
36 /*
37  * DSP registers
38  * register offset for BANK_SEL == BANK_SEL_DSP
39  */
40 #define R_BYPASS    0x05 /* Bypass DSP */
41 #define   R_BYPASS_DSP_BYPAS    0x01 /* Bypass DSP, sensor out directly */
42 #define   R_BYPASS_USE_DSP      0x00 /* Use the internal DSP */
43 #define QS          0x44 /* Quantization Scale Factor */
44 #define CTRLI       0x50
45 #define   CTRLI_LP_DP           0x80
46 #define   CTRLI_ROUND           0x40
47 #define   CTRLI_V_DIV_SET(x)    VAL_SET(x, 0x3, 0, 3)
48 #define   CTRLI_H_DIV_SET(x)    VAL_SET(x, 0x3, 0, 0)
49 #define HSIZE       0x51 /* H_SIZE[7:0] (real/4) */
50 #define   HSIZE_SET(x)          VAL_SET(x, 0xFF, 2, 0)
51 #define VSIZE       0x52 /* V_SIZE[7:0] (real/4) */
52 #define   VSIZE_SET(x)          VAL_SET(x, 0xFF, 2, 0)
53 #define XOFFL       0x53 /* OFFSET_X[7:0] */
54 #define   XOFFL_SET(x)          VAL_SET(x, 0xFF, 0, 0)
55 #define YOFFL       0x54 /* OFFSET_Y[7:0] */
56 #define   YOFFL_SET(x)          VAL_SET(x, 0xFF, 0, 0)
57 #define VHYX        0x55 /* Offset and size completion */
58 #define   VHYX_VSIZE_SET(x)     VAL_SET(x, 0x1, (8+2), 7)
59 #define   VHYX_HSIZE_SET(x)     VAL_SET(x, 0x1, (8+2), 3)
60 #define   VHYX_YOFF_SET(x)      VAL_SET(x, 0x3, 8, 4)
61 #define   VHYX_XOFF_SET(x)      VAL_SET(x, 0x3, 8, 0)
62 #define DPRP        0x56
63 #define TEST        0x57 /* Horizontal size completion */
64 #define   TEST_HSIZE_SET(x)     VAL_SET(x, 0x1, (9+2), 7)
65 #define ZMOW        0x5A /* Zoom: Out Width  OUTW[7:0] (real/4) */
66 #define   ZMOW_OUTW_SET(x)      VAL_SET(x, 0xFF, 2, 0)
67 #define ZMOH        0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
68 #define   ZMOH_OUTH_SET(x)      VAL_SET(x, 0xFF, 2, 0)
69 #define ZMHH        0x5C /* Zoom: Speed and H&W completion */
70 #define   ZMHH_ZSPEED_SET(x)    VAL_SET(x, 0x0F, 0, 4)
71 #define   ZMHH_OUTH_SET(x)      VAL_SET(x, 0x1, (8+2), 2)
72 #define   ZMHH_OUTW_SET(x)      VAL_SET(x, 0x3, (8+2), 0)
73 #define BPADDR      0x7C /* SDE Indirect Register Access: Address */
74 #define BPDATA      0x7D /* SDE Indirect Register Access: Data */
75 #define CTRL2       0x86 /* DSP Module enable 2 */
76 #define   CTRL2_DCW_EN          0x20
77 #define   CTRL2_SDE_EN          0x10
78 #define   CTRL2_UV_ADJ_EN       0x08
79 #define   CTRL2_UV_AVG_EN       0x04
80 #define   CTRL2_CMX_EN          0x01
81 #define CTRL3       0x87 /* DSP Module enable 3 */
82 #define   CTRL3_BPC_EN          0x80
83 #define   CTRL3_WPC_EN          0x40
84 #define SIZEL       0x8C /* Image Size Completion */
85 #define   SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
86 #define   SIZEL_HSIZE8_SET(x)    VAL_SET(x, 0x7, 0, 3)
87 #define   SIZEL_VSIZE8_SET(x)    VAL_SET(x, 0x7, 0, 0)
88 #define HSIZE8      0xC0 /* Image Horizontal Size HSIZE[10:3] */
89 #define   HSIZE8_SET(x)         VAL_SET(x, 0xFF, 3, 0)
90 #define VSIZE8      0xC1 /* Image Vertical Size VSIZE[10:3] */
91 #define   VSIZE8_SET(x)         VAL_SET(x, 0xFF, 3, 0)
92 #define CTRL0       0xC2 /* DSP Module enable 0 */
93 #define   CTRL0_AEC_EN       0x80
94 #define   CTRL0_AEC_SEL      0x40
95 #define   CTRL0_STAT_SEL     0x20
96 #define   CTRL0_VFIRST       0x10
97 #define   CTRL0_YUV422       0x08
98 #define   CTRL0_YUV_EN       0x04
99 #define   CTRL0_RGB_EN       0x02
100 #define   CTRL0_RAW_EN       0x01
101 #define CTRL1       0xC3 /* DSP Module enable 1 */
102 #define   CTRL1_CIP          0x80
103 #define   CTRL1_DMY          0x40
104 #define   CTRL1_RAW_GMA      0x20
105 #define   CTRL1_DG           0x10
106 #define   CTRL1_AWB          0x08
107 #define   CTRL1_AWB_GAIN     0x04
108 #define   CTRL1_LENC         0x02
109 #define   CTRL1_PRE          0x01
110 #define R_DVP_SP    0xD3 /* DVP output speed control */
111 #define   R_DVP_SP_AUTO_MODE 0x80
112 #define   R_DVP_SP_DVP_MASK  0x3F /* DVP PCLK = sysclk (48)/[6:0] (YUV0);
113                                    *          = sysclk (48)/(2*[6:0]) (RAW);*/
114 #define IMAGE_MODE  0xDA /* Image Output Format Select */
115 #define   IMAGE_MODE_Y8_DVP_EN   0x40
116 #define   IMAGE_MODE_JPEG_EN     0x10
117 #define   IMAGE_MODE_YUV422      0x00
118 #define   IMAGE_MODE_RAW10       0x04 /* (DVP) */
119 #define   IMAGE_MODE_RGB565      0x08
120 #define   IMAGE_MODE_HREF_VSYNC  0x02 /* HREF timing select in DVP JPEG output
121                                        * mode (0 for HREF is same as sensor) */
122 #define   IMAGE_MODE_LBYTE_FIRST 0x01 /* Byte swap enable for DVP
123                                        *    1: Low byte first UYVY (C2[4] =0)
124                                        *        VYUY (C2[4] =1)
125                                        *    0: High byte first YUYV (C2[4]=0)
126                                        *        YVYU (C2[4] = 1) */
127 #define RESET       0xE0 /* Reset */
128 #define   RESET_MICROC       0x40
129 #define   RESET_SCCB         0x20
130 #define   RESET_JPEG         0x10
131 #define   RESET_DVP          0x04
132 #define   RESET_IPU          0x02
133 #define   RESET_CIF          0x01
134 #define REGED       0xED /* Register ED */
135 #define   REGED_CLK_OUT_DIS  0x10
136 #define MS_SP       0xF0 /* SCCB Master Speed */
137 #define SS_ID       0xF7 /* SCCB Slave ID */
138 #define SS_CTRL     0xF8 /* SCCB Slave Control */
139 #define   SS_CTRL_ADD_AUTO_INC  0x20
140 #define   SS_CTRL_EN            0x08
141 #define   SS_CTRL_DELAY_CLK     0x04
142 #define   SS_CTRL_ACC_EN        0x02
143 #define   SS_CTRL_SEN_PASS_THR  0x01
144 #define MC_BIST     0xF9 /* Microcontroller misc register */
145 #define   MC_BIST_RESET           0x80 /* Microcontroller Reset */
146 #define   MC_BIST_BOOT_ROM_SEL    0x40
147 #define   MC_BIST_12KB_SEL        0x20
148 #define   MC_BIST_12KB_MASK       0x30
149 #define   MC_BIST_512KB_SEL       0x08
150 #define   MC_BIST_512KB_MASK      0x0C
151 #define   MC_BIST_BUSY_BIT_R      0x02
152 #define   MC_BIST_MC_RES_ONE_SH_W 0x02
153 #define   MC_BIST_LAUNCH          0x01
154 #define BANK_SEL    0xFF /* Register Bank Select */
155 #define   BANK_SEL_DSP     0x00
156 #define   BANK_SEL_SENS    0x01
157
158 /*
159  * Sensor registers
160  * register offset for BANK_SEL == BANK_SEL_SENS
161  */
162 #define GAIN        0x00 /* AGC - Gain control gain setting */
163 #define COM1        0x03 /* Common control 1 */
164 #define   COM1_1_DUMMY_FR          0x40
165 #define   COM1_3_DUMMY_FR          0x80
166 #define   COM1_7_DUMMY_FR          0xC0
167 #define   COM1_VWIN_LSB_UXGA       0x0F
168 #define   COM1_VWIN_LSB_SVGA       0x0A
169 #define   COM1_VWIN_LSB_CIF        0x06
170 #define REG04       0x04 /* Register 04 */
171 #define   REG04_DEF             0x20 /* Always set */
172 #define   REG04_HFLIP_IMG       0x80 /* Horizontal mirror image ON/OFF */
173 #define   REG04_VFLIP_IMG       0x40 /* Vertical flip image ON/OFF */
174 #define   REG04_VREF_EN         0x10
175 #define   REG04_HREF_EN         0x08
176 #define   REG04_AEC_SET(x)      VAL_SET(x, 0x3, 0, 0)
177 #define REG08       0x08 /* Frame Exposure One-pin Control Pre-charge Row Num */
178 #define COM2        0x09 /* Common control 2 */
179 #define   COM2_SOFT_SLEEP_MODE  0x10 /* Soft sleep mode */
180                                      /* Output drive capability */
181 #define   COM2_OCAP_Nx_SET(N)   (((N) - 1) & 0x03) /* N = [1x .. 4x] */
182 #define PID         0x0A /* Product ID Number MSB */
183 #define VER         0x0B /* Product ID Number LSB */
184 #define COM3        0x0C /* Common control 3 */
185 #define   COM3_BAND_50H        0x04 /* 0 For Banding at 60H */
186 #define   COM3_BAND_AUTO       0x02 /* Auto Banding */
187 #define   COM3_SING_FR_SNAPSH  0x01 /* 0 For enable live video output after the
188                                      * snapshot sequence*/
189 #define AEC         0x10 /* AEC[9:2] Exposure Value */
190 #define CLKRC       0x11 /* Internal clock */
191 #define   CLKRC_EN             0x80
192 #define   CLKRC_DIV_SET(x)     (((x) - 1) & 0x1F) /* CLK = XVCLK/(x) */
193 #define COM7        0x12 /* Common control 7 */
194 #define   COM7_SRST            0x80 /* Initiates system reset. All registers are
195                                      * set to factory default values after which
196                                      * the chip resumes normal operation */
197 #define   COM7_RES_UXGA        0x00 /* Resolution selectors for UXGA */
198 #define   COM7_RES_SVGA        0x40 /* SVGA */
199 #define   COM7_RES_CIF         0x20 /* CIF */
200 #define   COM7_ZOOM_EN         0x04 /* Enable Zoom mode */
201 #define   COM7_COLOR_BAR_TEST  0x02 /* Enable Color Bar Test Pattern */
202 #define COM8        0x13 /* Common control 8 */
203 #define   COM8_DEF             0xC0 /* Banding filter ON/OFF */
204 #define   COM8_BNDF_EN         0x20 /* Banding filter ON/OFF */
205 #define   COM8_AGC_EN          0x04 /* AGC Auto/Manual control selection */
206 #define   COM8_AEC_EN          0x01 /* Auto/Manual Exposure control */
207 #define COM9        0x14 /* Common control 9
208                           * Automatic gain ceiling - maximum AGC value [7:5]*/
209 #define   COM9_AGC_GAIN_2x     0x00 /* 000 :   2x */
210 #define   COM9_AGC_GAIN_4x     0x20 /* 001 :   4x */
211 #define   COM9_AGC_GAIN_8x     0x40 /* 010 :   8x */
212 #define   COM9_AGC_GAIN_16x    0x60 /* 011 :  16x */
213 #define   COM9_AGC_GAIN_32x    0x80 /* 100 :  32x */
214 #define   COM9_AGC_GAIN_64x    0xA0 /* 101 :  64x */
215 #define   COM9_AGC_GAIN_128x   0xC0 /* 110 : 128x */
216 #define COM10       0x15 /* Common control 10 */
217 #define   COM10_PCLK_HREF      0x20 /* PCLK output qualified by HREF */
218 #define   COM10_PCLK_RISE      0x10 /* Data is updated at the rising edge of
219                                      * PCLK (user can latch data at the next
220                                      * falling edge of PCLK).
221                                      * 0 otherwise. */
222 #define   COM10_HREF_INV       0x08 /* Invert HREF polarity:
223                                      * HREF negative for valid data*/
224 #define   COM10_VSINC_INV      0x02 /* Invert VSYNC polarity */
225 #define HSTART      0x17 /* Horizontal Window start MSB 8 bit */
226 #define HEND        0x18 /* Horizontal Window end MSB 8 bit */
227 #define VSTART      0x19 /* Vertical Window start MSB 8 bit */
228 #define VEND        0x1A /* Vertical Window end MSB 8 bit */
229 #define MIDH        0x1C /* Manufacturer ID byte - high */
230 #define MIDL        0x1D /* Manufacturer ID byte - low  */
231 #define AEW         0x24 /* AGC/AEC - Stable operating region (upper limit) */
232 #define AEB         0x25 /* AGC/AEC - Stable operating region (lower limit) */
233 #define VV          0x26 /* AGC/AEC Fast mode operating region */
234 #define   VV_HIGH_TH_SET(x)      VAL_SET(x, 0xF, 0, 4)
235 #define   VV_LOW_TH_SET(x)       VAL_SET(x, 0xF, 0, 0)
236 #define REG2A       0x2A /* Dummy pixel insert MSB */
237 #define FRARL       0x2B /* Dummy pixel insert LSB */
238 #define ADDVFL      0x2D /* LSB of insert dummy lines in Vertical direction */
239 #define ADDVFH      0x2E /* MSB of insert dummy lines in Vertical direction */
240 #define YAVG        0x2F /* Y/G Channel Average value */
241 #define REG32       0x32 /* Common Control 32 */
242 #define   REG32_PCLK_DIV_2    0x80 /* PCLK freq divided by 2 */
243 #define   REG32_PCLK_DIV_4    0xC0 /* PCLK freq divided by 4 */
244 #define ARCOM2      0x34 /* Zoom: Horizontal start point */
245 #define REG45       0x45 /* Register 45 */
246 #define FLL         0x46 /* Frame Length Adjustment LSBs */
247 #define FLH         0x47 /* Frame Length Adjustment MSBs */
248 #define COM19       0x48 /* Zoom: Vertical start point */
249 #define ZOOMS       0x49 /* Zoom: Vertical start point */
250 #define COM22       0x4B /* Flash light control */
251 #define COM25       0x4E /* For Banding operations */
252 #define BD50        0x4F /* 50Hz Banding AEC 8 LSBs */
253 #define BD60        0x50 /* 60Hz Banding AEC 8 LSBs */
254 #define REG5D       0x5D /* AVGsel[7:0],   16-zone average weight option */
255 #define REG5E       0x5E /* AVGsel[15:8],  16-zone average weight option */
256 #define REG5F       0x5F /* AVGsel[23:16], 16-zone average weight option */
257 #define REG60       0x60 /* AVGsel[31:24], 16-zone average weight option */
258 #define HISTO_LOW   0x61 /* Histogram Algorithm Low Level */
259 #define HISTO_HIGH  0x62 /* Histogram Algorithm High Level */
260
261 /*
262  * ID
263  */
264 #define MANUFACTURER_ID 0x7FA2
265 #define PID_OV2640      0x2642
266 #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
267
268 /*
269  * Struct
270  */
271 struct regval_list {
272         u8 reg_num;
273         u8 value;
274 };
275
276 /* Supported resolutions */
277 enum ov2640_width {
278         W_QCIF  = 176,
279         W_QVGA  = 320,
280         W_CIF   = 352,
281         W_VGA   = 640,
282         W_SVGA  = 800,
283         W_XGA   = 1024,
284         W_SXGA  = 1280,
285         W_UXGA  = 1600,
286 };
287
288 enum ov2640_height {
289         H_QCIF  = 144,
290         H_QVGA  = 240,
291         H_CIF   = 288,
292         H_VGA   = 480,
293         H_SVGA  = 600,
294         H_XGA   = 768,
295         H_SXGA  = 1024,
296         H_UXGA  = 1200,
297 };
298
299 struct ov2640_win_size {
300         char                            *name;
301         enum ov2640_width               width;
302         enum ov2640_height              height;
303         const struct regval_list        *regs;
304 };
305
306
307 struct ov2640_priv {
308         struct v4l2_subdev              subdev;
309         struct ov2640_camera_info       *info;
310         enum v4l2_mbus_pixelcode        cfmt_code;
311         const struct ov2640_win_size    *win;
312         int                             model;
313         u16                             flag_vflip:1;
314         u16                             flag_hflip:1;
315 };
316
317 /*
318  * Registers settings
319  */
320
321 #define ENDMARKER { 0xff, 0xff }
322
323 static const struct regval_list ov2640_init_regs[] = {
324         { BANK_SEL, BANK_SEL_DSP },
325         { 0x2c,   0xff },
326         { 0x2e,   0xdf },
327         { BANK_SEL, BANK_SEL_SENS },
328         { 0x3c,   0x32 },
329         { CLKRC, CLKRC_DIV_SET(1) },
330         { COM2, COM2_OCAP_Nx_SET(3) },
331         { REG04, REG04_DEF | REG04_HREF_EN },
332         { COM8,  COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
333         { COM9, COM9_AGC_GAIN_8x | 0x08},
334         { 0x2c,   0x0c },
335         { 0x33,   0x78 },
336         { 0x3a,   0x33 },
337         { 0x3b,   0xfb },
338         { 0x3e,   0x00 },
339         { 0x43,   0x11 },
340         { 0x16,   0x10 },
341         { 0x39,   0x02 },
342         { 0x35,   0x88 },
343         { 0x22,   0x0a },
344         { 0x37,   0x40 },
345         { 0x23,   0x00 },
346         { ARCOM2, 0xa0 },
347         { 0x06,   0x02 },
348         { 0x06,   0x88 },
349         { 0x07,   0xc0 },
350         { 0x0d,   0xb7 },
351         { 0x0e,   0x01 },
352         { 0x4c,   0x00 },
353         { 0x4a,   0x81 },
354         { 0x21,   0x99 },
355         { AEW,    0x40 },
356         { AEB,    0x38 },
357         { VV,     VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
358         { 0x5c,   0x00 },
359         { 0x63,   0x00 },
360         { FLL,    0x22 },
361         { COM3,   0x38 | COM3_BAND_AUTO },
362         { REG5D,  0x55 },
363         { REG5E,  0x7d },
364         { REG5F,  0x7d },
365         { REG60,  0x55 },
366         { HISTO_LOW,   0x70 },
367         { HISTO_HIGH,  0x80 },
368         { 0x7c,   0x05 },
369         { 0x20,   0x80 },
370         { 0x28,   0x30 },
371         { 0x6c,   0x00 },
372         { 0x6d,   0x80 },
373         { 0x6e,   0x00 },
374         { 0x70,   0x02 },
375         { 0x71,   0x94 },
376         { 0x73,   0xc1 },
377         { 0x3d,   0x34 },
378         { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
379         { 0x5a,   0x57 },
380         { BD50,   0xbb },
381         { BD60,   0x9c },
382         { BANK_SEL, BANK_SEL_DSP },
383         { 0xe5,   0x7f },
384         { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
385         { 0x41,   0x24 },
386         { RESET, RESET_JPEG | RESET_DVP },
387         { 0x76,   0xff },
388         { 0x33,   0xa0 },
389         { 0x42,   0x20 },
390         { 0x43,   0x18 },
391         { 0x4c,   0x00 },
392         { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
393         { 0x88,   0x3f },
394         { 0xd7,   0x03 },
395         { 0xd9,   0x10 },
396         { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
397         { 0xc8,   0x08 },
398         { 0xc9,   0x80 },
399         { BPADDR, 0x00 },
400         { BPDATA, 0x00 },
401         { BPADDR, 0x03 },
402         { BPDATA, 0x48 },
403         { BPDATA, 0x48 },
404         { BPADDR, 0x08 },
405         { BPDATA, 0x20 },
406         { BPDATA, 0x10 },
407         { BPDATA, 0x0e },
408         { 0x90,   0x00 },
409         { 0x91,   0x0e },
410         { 0x91,   0x1a },
411         { 0x91,   0x31 },
412         { 0x91,   0x5a },
413         { 0x91,   0x69 },
414         { 0x91,   0x75 },
415         { 0x91,   0x7e },
416         { 0x91,   0x88 },
417         { 0x91,   0x8f },
418         { 0x91,   0x96 },
419         { 0x91,   0xa3 },
420         { 0x91,   0xaf },
421         { 0x91,   0xc4 },
422         { 0x91,   0xd7 },
423         { 0x91,   0xe8 },
424         { 0x91,   0x20 },
425         { 0x92,   0x00 },
426         { 0x93,   0x06 },
427         { 0x93,   0xe3 },
428         { 0x93,   0x03 },
429         { 0x93,   0x03 },
430         { 0x93,   0x00 },
431         { 0x93,   0x02 },
432         { 0x93,   0x00 },
433         { 0x93,   0x00 },
434         { 0x93,   0x00 },
435         { 0x93,   0x00 },
436         { 0x93,   0x00 },
437         { 0x93,   0x00 },
438         { 0x93,   0x00 },
439         { 0x96,   0x00 },
440         { 0x97,   0x08 },
441         { 0x97,   0x19 },
442         { 0x97,   0x02 },
443         { 0x97,   0x0c },
444         { 0x97,   0x24 },
445         { 0x97,   0x30 },
446         { 0x97,   0x28 },
447         { 0x97,   0x26 },
448         { 0x97,   0x02 },
449         { 0x97,   0x98 },
450         { 0x97,   0x80 },
451         { 0x97,   0x00 },
452         { 0x97,   0x00 },
453         { 0xa4,   0x00 },
454         { 0xa8,   0x00 },
455         { 0xc5,   0x11 },
456         { 0xc6,   0x51 },
457         { 0xbf,   0x80 },
458         { 0xc7,   0x10 },
459         { 0xb6,   0x66 },
460         { 0xb8,   0xA5 },
461         { 0xb7,   0x64 },
462         { 0xb9,   0x7C },
463         { 0xb3,   0xaf },
464         { 0xb4,   0x97 },
465         { 0xb5,   0xFF },
466         { 0xb0,   0xC5 },
467         { 0xb1,   0x94 },
468         { 0xb2,   0x0f },
469         { 0xc4,   0x5c },
470         { 0xa6,   0x00 },
471         { 0xa7,   0x20 },
472         { 0xa7,   0xd8 },
473         { 0xa7,   0x1b },
474         { 0xa7,   0x31 },
475         { 0xa7,   0x00 },
476         { 0xa7,   0x18 },
477         { 0xa7,   0x20 },
478         { 0xa7,   0xd8 },
479         { 0xa7,   0x19 },
480         { 0xa7,   0x31 },
481         { 0xa7,   0x00 },
482         { 0xa7,   0x18 },
483         { 0xa7,   0x20 },
484         { 0xa7,   0xd8 },
485         { 0xa7,   0x19 },
486         { 0xa7,   0x31 },
487         { 0xa7,   0x00 },
488         { 0xa7,   0x18 },
489         { 0x7f,   0x00 },
490         { 0xe5,   0x1f },
491         { 0xe1,   0x77 },
492         { 0xdd,   0x7f },
493         { CTRL0,  CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
494         ENDMARKER,
495 };
496
497 /*
498  * Register settings for window size
499  * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
500  * Then the different zooming configurations will setup the output image size.
501  */
502 static const struct regval_list ov2640_size_change_preamble_regs[] = {
503         { BANK_SEL, BANK_SEL_DSP },
504         { RESET, RESET_DVP },
505         { HSIZE8, HSIZE8_SET(W_UXGA) },
506         { VSIZE8, VSIZE8_SET(H_UXGA) },
507         { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
508                  CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
509         { HSIZE, HSIZE_SET(W_UXGA) },
510         { VSIZE, VSIZE_SET(H_UXGA) },
511         { XOFFL, XOFFL_SET(0) },
512         { YOFFL, YOFFL_SET(0) },
513         { VHYX, VHYX_HSIZE_SET(W_UXGA) | VHYX_VSIZE_SET(H_UXGA) |
514                 VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
515         { TEST, TEST_HSIZE_SET(W_UXGA) },
516         ENDMARKER,
517 };
518
519 #define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div)  \
520         { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
521                  CTRLI_H_DIV_SET(h_div)},               \
522         { ZMOW, ZMOW_OUTW_SET(x) },                     \
523         { ZMOH, ZMOH_OUTH_SET(y) },                     \
524         { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) },  \
525         { R_DVP_SP, pclk_div },                         \
526         { RESET, 0x00}
527
528 static const struct regval_list ov2640_qcif_regs[] = {
529         PER_SIZE_REG_SEQ(W_QCIF, H_QCIF, 3, 3, 4),
530         ENDMARKER,
531 };
532
533 static const struct regval_list ov2640_qvga_regs[] = {
534         PER_SIZE_REG_SEQ(W_QVGA, H_QVGA, 2, 2, 4),
535         ENDMARKER,
536 };
537
538 static const struct regval_list ov2640_cif_regs[] = {
539         PER_SIZE_REG_SEQ(W_CIF, H_CIF, 2, 2, 8),
540         ENDMARKER,
541 };
542
543 static const struct regval_list ov2640_vga_regs[] = {
544         PER_SIZE_REG_SEQ(W_VGA, H_VGA, 0, 0, 2),
545         ENDMARKER,
546 };
547
548 static const struct regval_list ov2640_svga_regs[] = {
549         PER_SIZE_REG_SEQ(W_SVGA, H_SVGA, 1, 1, 2),
550         ENDMARKER,
551 };
552
553 static const struct regval_list ov2640_xga_regs[] = {
554         PER_SIZE_REG_SEQ(W_XGA, H_XGA, 0, 0, 2),
555         { CTRLI,    0x00},
556         ENDMARKER,
557 };
558
559 static const struct regval_list ov2640_sxga_regs[] = {
560         PER_SIZE_REG_SEQ(W_SXGA, H_SXGA, 0, 0, 2),
561         { CTRLI,    0x00},
562         { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
563         ENDMARKER,
564 };
565
566 static const struct regval_list ov2640_uxga_regs[] = {
567         PER_SIZE_REG_SEQ(W_UXGA, H_UXGA, 0, 0, 0),
568         { CTRLI,    0x00},
569         { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
570         ENDMARKER,
571 };
572
573 #define OV2640_SIZE(n, w, h, r) \
574         {.name = n, .width = w , .height = h, .regs = r }
575
576 static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
577         OV2640_SIZE("QCIF", W_QCIF, H_QCIF, ov2640_qcif_regs),
578         OV2640_SIZE("QVGA", W_QVGA, H_QVGA, ov2640_qvga_regs),
579         OV2640_SIZE("CIF", W_CIF, H_CIF, ov2640_cif_regs),
580         OV2640_SIZE("VGA", W_VGA, H_VGA, ov2640_vga_regs),
581         OV2640_SIZE("SVGA", W_SVGA, H_SVGA, ov2640_svga_regs),
582         OV2640_SIZE("XGA", W_XGA, H_XGA, ov2640_xga_regs),
583         OV2640_SIZE("SXGA", W_SXGA, H_SXGA, ov2640_sxga_regs),
584         OV2640_SIZE("UXGA", W_UXGA, H_UXGA, ov2640_uxga_regs),
585 };
586
587 /*
588  * Register settings for pixel formats
589  */
590 static const struct regval_list ov2640_format_change_preamble_regs[] = {
591         { BANK_SEL, BANK_SEL_DSP },
592         { R_BYPASS, R_BYPASS_USE_DSP },
593         ENDMARKER,
594 };
595
596 static const struct regval_list ov2640_yuv422_regs[] = {
597         { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
598         { 0xD7, 0x01 },
599         { 0x33, 0xa0 },
600         { 0xe1, 0x67 },
601         { RESET,  0x00 },
602         { R_BYPASS, R_BYPASS_USE_DSP },
603         ENDMARKER,
604 };
605
606 static const struct regval_list ov2640_rgb565_regs[] = {
607         { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
608         { 0xd7, 0x03 },
609         { RESET,  0x00 },
610         { R_BYPASS, R_BYPASS_USE_DSP },
611         ENDMARKER,
612 };
613
614 static enum v4l2_mbus_pixelcode ov2640_codes[] = {
615         V4L2_MBUS_FMT_UYVY8_2X8,
616         V4L2_MBUS_FMT_RGB565_2X8_LE,
617 };
618
619 /*
620  * Supported controls
621  */
622 static const struct v4l2_queryctrl ov2640_controls[] = {
623         {
624                 .id             = V4L2_CID_VFLIP,
625                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
626                 .name           = "Flip Vertically",
627                 .minimum        = 0,
628                 .maximum        = 1,
629                 .step           = 1,
630                 .default_value  = 0,
631         }, {
632                 .id             = V4L2_CID_HFLIP,
633                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
634                 .name           = "Flip Horizontally",
635                 .minimum        = 0,
636                 .maximum        = 1,
637                 .step           = 1,
638                 .default_value  = 0,
639         },
640 };
641
642 /*
643  * General functions
644  */
645 static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
646 {
647         return container_of(i2c_get_clientdata(client), struct ov2640_priv,
648                             subdev);
649 }
650
651 static int ov2640_write_array(struct i2c_client *client,
652                               const struct regval_list *vals)
653 {
654         int ret;
655
656         while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
657                 ret = i2c_smbus_write_byte_data(client,
658                                                 vals->reg_num, vals->value);
659                 dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
660                          vals->reg_num, vals->value);
661
662                 if (ret < 0)
663                         return ret;
664                 vals++;
665         }
666         return 0;
667 }
668
669 static int ov2640_mask_set(struct i2c_client *client,
670                            u8  reg, u8  mask, u8  set)
671 {
672         s32 val = i2c_smbus_read_byte_data(client, reg);
673         if (val < 0)
674                 return val;
675
676         val &= ~mask;
677         val |= set & mask;
678
679         dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
680
681         return i2c_smbus_write_byte_data(client, reg, val);
682 }
683
684 static int ov2640_reset(struct i2c_client *client)
685 {
686         int ret;
687         const struct regval_list reset_seq[] = {
688                 {BANK_SEL, BANK_SEL_SENS},
689                 {COM7, COM7_SRST},
690                 ENDMARKER,
691         };
692
693         ret = ov2640_write_array(client, reset_seq);
694         if (ret)
695                 goto err;
696
697         msleep(5);
698 err:
699         dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
700         return ret;
701 }
702
703 /*
704  * soc_camera_ops functions
705  */
706 static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
707 {
708         return 0;
709 }
710
711 static int ov2640_set_bus_param(struct soc_camera_device *icd,
712                                 unsigned long flags)
713 {
714         struct soc_camera_link *icl = to_soc_camera_link(icd);
715         unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
716
717         /* Only one width bit may be set */
718         if (!is_power_of_2(width_flag))
719                 return -EINVAL;
720
721         if (icl->set_bus_param)
722                 return icl->set_bus_param(icl, width_flag);
723
724         /*
725          * Without board specific bus width settings we support only the
726          * sensors native bus width witch are tested working
727          */
728         if (width_flag & (SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8))
729                 return 0;
730
731         return 0;
732 }
733
734 static unsigned long ov2640_query_bus_param(struct soc_camera_device *icd)
735 {
736         struct soc_camera_link *icl = to_soc_camera_link(icd);
737         unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
738                 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
739                 SOCAM_DATA_ACTIVE_HIGH;
740
741         if (icl->query_bus_param)
742                 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
743         else
744                 flags |= SOCAM_DATAWIDTH_10;
745
746         return soc_camera_apply_sensor_flags(icl, flags);
747 }
748
749 static int ov2640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
750 {
751         struct i2c_client  *client = v4l2_get_subdevdata(sd);
752         struct ov2640_priv *priv = to_ov2640(client);
753
754         switch (ctrl->id) {
755         case V4L2_CID_VFLIP:
756                 ctrl->value = priv->flag_vflip;
757                 break;
758         case V4L2_CID_HFLIP:
759                 ctrl->value = priv->flag_hflip;
760                 break;
761         }
762         return 0;
763 }
764
765 static int ov2640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
766 {
767         struct i2c_client  *client = v4l2_get_subdevdata(sd);
768         struct ov2640_priv *priv = to_ov2640(client);
769         int ret = 0;
770         u8 val;
771
772         switch (ctrl->id) {
773         case V4L2_CID_VFLIP:
774                 val = ctrl->value ? REG04_VFLIP_IMG : 0x00;
775                 priv->flag_vflip = ctrl->value ? 1 : 0;
776                 ret = ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
777                 break;
778         case V4L2_CID_HFLIP:
779                 val = ctrl->value ? REG04_HFLIP_IMG : 0x00;
780                 priv->flag_hflip = ctrl->value ? 1 : 0;
781                 ret = ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
782                 break;
783         }
784
785         return ret;
786 }
787
788 static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
789                                struct v4l2_dbg_chip_ident *id)
790 {
791         struct i2c_client *client = v4l2_get_subdevdata(sd);
792         struct ov2640_priv *priv = to_ov2640(client);
793
794         id->ident    = priv->model;
795         id->revision = 0;
796
797         return 0;
798 }
799
800 #ifdef CONFIG_VIDEO_ADV_DEBUG
801 static int ov2640_g_register(struct v4l2_subdev *sd,
802                              struct v4l2_dbg_register *reg)
803 {
804         struct i2c_client *client = v4l2_get_subdevdata(sd);
805         int ret;
806
807         reg->size = 1;
808         if (reg->reg > 0xff)
809                 return -EINVAL;
810
811         ret = i2c_smbus_read_byte_data(client, reg->reg);
812         if (ret < 0)
813                 return ret;
814
815         reg->val = ret;
816
817         return 0;
818 }
819
820 static int ov2640_s_register(struct v4l2_subdev *sd,
821                              struct v4l2_dbg_register *reg)
822 {
823         struct i2c_client *client = v4l2_get_subdevdata(sd);
824
825         if (reg->reg > 0xff ||
826             reg->val > 0xff)
827                 return -EINVAL;
828
829         return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
830 }
831 #endif
832
833 /* Select the nearest higher resolution for capture */
834 static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
835 {
836         int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
837
838         for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
839                 if (ov2640_supported_win_sizes[i].width  >= *width &&
840                     ov2640_supported_win_sizes[i].height >= *height) {
841                         *width = ov2640_supported_win_sizes[i].width;
842                         *height = ov2640_supported_win_sizes[i].height;
843                         return &ov2640_supported_win_sizes[i];
844                 }
845         }
846
847         *width = ov2640_supported_win_sizes[default_size].width;
848         *height = ov2640_supported_win_sizes[default_size].height;
849         return &ov2640_supported_win_sizes[default_size];
850 }
851
852 static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
853                              enum v4l2_mbus_pixelcode code)
854 {
855         struct ov2640_priv       *priv = to_ov2640(client);
856         const struct regval_list *selected_cfmt_regs;
857         int ret;
858
859         /* select win */
860         priv->win = ov2640_select_win(width, height);
861
862         /* select format */
863         priv->cfmt_code = 0;
864         switch (code) {
865         case V4L2_MBUS_FMT_RGB565_2X8_LE:
866                 dev_dbg(&client->dev, "%s: Selected cfmt RGB565", __func__);
867                 selected_cfmt_regs = ov2640_rgb565_regs;
868                 break;
869         default:
870         case V4L2_MBUS_FMT_UYVY8_2X8:
871                 dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
872                 selected_cfmt_regs = ov2640_yuv422_regs;
873         }
874
875         /* reset hardware */
876         ov2640_reset(client);
877
878         /* initialize the sensor with default data */
879         dev_dbg(&client->dev, "%s: Init default", __func__);
880         ret = ov2640_write_array(client, ov2640_init_regs);
881         if (ret < 0)
882                 goto err;
883
884         /* select preamble */
885         dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
886         ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
887         if (ret < 0)
888                 goto err;
889
890         /* set size win */
891         ret = ov2640_write_array(client, priv->win->regs);
892         if (ret < 0)
893                 goto err;
894
895         /* cfmt preamble */
896         dev_dbg(&client->dev, "%s: Set cfmt", __func__);
897         ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
898         if (ret < 0)
899                 goto err;
900
901         /* set cfmt */
902         ret = ov2640_write_array(client, selected_cfmt_regs);
903         if (ret < 0)
904                 goto err;
905
906         priv->cfmt_code = code;
907         *width = priv->win->width;
908         *height = priv->win->height;
909
910         return 0;
911
912 err:
913         dev_err(&client->dev, "%s: Error %d", __func__, ret);
914         ov2640_reset(client);
915         priv->win = NULL;
916
917         return ret;
918 }
919
920 static int ov2640_g_fmt(struct v4l2_subdev *sd,
921                         struct v4l2_mbus_framefmt *mf)
922 {
923         struct i2c_client  *client = v4l2_get_subdevdata(sd);
924         struct ov2640_priv *priv = to_ov2640(client);
925
926         if (!priv->win) {
927                 u32 width = W_SVGA, height = H_SVGA;
928                 int ret = ov2640_set_params(client, &width, &height,
929                                             V4L2_MBUS_FMT_UYVY8_2X8);
930                 if (ret < 0)
931                         return ret;
932         }
933
934         mf->width       = priv->win->width;
935         mf->height      = priv->win->height;
936         mf->code        = priv->cfmt_code;
937
938         switch (mf->code) {
939         case V4L2_MBUS_FMT_RGB565_2X8_LE:
940                 mf->colorspace = V4L2_COLORSPACE_SRGB;
941                 break;
942         default:
943         case V4L2_MBUS_FMT_UYVY8_2X8:
944                 mf->colorspace = V4L2_COLORSPACE_JPEG;
945         }
946         mf->field       = V4L2_FIELD_NONE;
947
948         return 0;
949 }
950
951 static int ov2640_s_fmt(struct v4l2_subdev *sd,
952                         struct v4l2_mbus_framefmt *mf)
953 {
954         struct i2c_client *client = v4l2_get_subdevdata(sd);
955         int ret;
956
957
958         switch (mf->code) {
959         case V4L2_MBUS_FMT_RGB565_2X8_LE:
960                 mf->colorspace = V4L2_COLORSPACE_SRGB;
961                 break;
962         default:
963                 mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
964         case V4L2_MBUS_FMT_UYVY8_2X8:
965                 mf->colorspace = V4L2_COLORSPACE_JPEG;
966         }
967
968         ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
969
970         return ret;
971 }
972
973 static int ov2640_try_fmt(struct v4l2_subdev *sd,
974                           struct v4l2_mbus_framefmt *mf)
975 {
976         const struct ov2640_win_size *win;
977
978         /*
979          * select suitable win
980          */
981         win = ov2640_select_win(&mf->width, &mf->height);
982
983         mf->field       = V4L2_FIELD_NONE;
984
985         switch (mf->code) {
986         case V4L2_MBUS_FMT_RGB565_2X8_LE:
987                 mf->colorspace = V4L2_COLORSPACE_SRGB;
988                 break;
989         default:
990                 mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
991         case V4L2_MBUS_FMT_UYVY8_2X8:
992                 mf->colorspace = V4L2_COLORSPACE_JPEG;
993         }
994
995         return 0;
996 }
997
998 static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
999                            enum v4l2_mbus_pixelcode *code)
1000 {
1001         if (index >= ARRAY_SIZE(ov2640_codes))
1002                 return -EINVAL;
1003
1004         *code = ov2640_codes[index];
1005         return 0;
1006 }
1007
1008 static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
1009 {
1010         a->c.left       = 0;
1011         a->c.top        = 0;
1012         a->c.width      = W_UXGA;
1013         a->c.height     = H_UXGA;
1014         a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1015
1016         return 0;
1017 }
1018
1019 static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
1020 {
1021         a->bounds.left                  = 0;
1022         a->bounds.top                   = 0;
1023         a->bounds.width                 = W_UXGA;
1024         a->bounds.height                = H_UXGA;
1025         a->defrect                      = a->bounds;
1026         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1027         a->pixelaspect.numerator        = 1;
1028         a->pixelaspect.denominator      = 1;
1029
1030         return 0;
1031 }
1032
1033 static int ov2640_video_probe(struct soc_camera_device *icd,
1034                               struct i2c_client *client)
1035 {
1036         struct ov2640_priv *priv = to_ov2640(client);
1037         u8 pid, ver, midh, midl;
1038         const char *devname;
1039         int ret;
1040
1041         /*
1042          * we must have a parent by now. And it cannot be a wrong one.
1043          * So this entire test is completely redundant.
1044          */
1045         if (!icd->dev.parent ||
1046             to_soc_camera_host(icd->dev.parent)->nr != icd->iface) {
1047                 dev_err(&client->dev, "Parent missing or invalid!\n");
1048                 ret = -ENODEV;
1049                 goto err;
1050         }
1051
1052         /*
1053          * check and show product ID and manufacturer ID
1054          */
1055         i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
1056         pid  = i2c_smbus_read_byte_data(client, PID);
1057         ver  = i2c_smbus_read_byte_data(client, VER);
1058         midh = i2c_smbus_read_byte_data(client, MIDH);
1059         midl = i2c_smbus_read_byte_data(client, MIDL);
1060
1061         switch (VERSION(pid, ver)) {
1062         case PID_OV2640:
1063                 devname     = "ov2640";
1064                 priv->model = V4L2_IDENT_OV2640;
1065                 break;
1066         default:
1067                 dev_err(&client->dev,
1068                         "Product ID error %x:%x\n", pid, ver);
1069                 ret = -ENODEV;
1070                 goto err;
1071         }
1072
1073         dev_info(&client->dev,
1074                  "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
1075                  devname, pid, ver, midh, midl);
1076
1077         return 0;
1078
1079 err:
1080         return ret;
1081 }
1082
1083 static struct soc_camera_ops ov2640_ops = {
1084         .set_bus_param          = ov2640_set_bus_param,
1085         .query_bus_param        = ov2640_query_bus_param,
1086         .controls               = ov2640_controls,
1087         .num_controls           = ARRAY_SIZE(ov2640_controls),
1088 };
1089
1090 static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
1091         .g_ctrl         = ov2640_g_ctrl,
1092         .s_ctrl         = ov2640_s_ctrl,
1093         .g_chip_ident   = ov2640_g_chip_ident,
1094 #ifdef CONFIG_VIDEO_ADV_DEBUG
1095         .g_register     = ov2640_g_register,
1096         .s_register     = ov2640_s_register,
1097 #endif
1098 };
1099
1100 static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
1101         .s_stream       = ov2640_s_stream,
1102         .g_mbus_fmt     = ov2640_g_fmt,
1103         .s_mbus_fmt     = ov2640_s_fmt,
1104         .try_mbus_fmt   = ov2640_try_fmt,
1105         .cropcap        = ov2640_cropcap,
1106         .g_crop         = ov2640_g_crop,
1107         .enum_mbus_fmt  = ov2640_enum_fmt,
1108 };
1109
1110 static struct v4l2_subdev_ops ov2640_subdev_ops = {
1111         .core   = &ov2640_subdev_core_ops,
1112         .video  = &ov2640_subdev_video_ops,
1113 };
1114
1115 /*
1116  * i2c_driver functions
1117  */
1118 static int ov2640_probe(struct i2c_client *client,
1119                         const struct i2c_device_id *did)
1120 {
1121         struct ov2640_priv        *priv;
1122         struct soc_camera_device  *icd = client->dev.platform_data;
1123         struct i2c_adapter        *adapter = to_i2c_adapter(client->dev.parent);
1124         struct soc_camera_link    *icl;
1125         int                        ret;
1126
1127         if (!icd) {
1128                 dev_err(&adapter->dev, "OV2640: missing soc-camera data!\n");
1129                 return -EINVAL;
1130         }
1131
1132         icl = to_soc_camera_link(icd);
1133         if (!icl) {
1134                 dev_err(&adapter->dev,
1135                         "OV2640: Missing platform_data for driver\n");
1136                 return -EINVAL;
1137         }
1138
1139         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1140                 dev_err(&adapter->dev,
1141                         "OV2640: I2C-Adapter doesn't support SMBUS\n");
1142                 return -EIO;
1143         }
1144
1145         priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
1146         if (!priv) {
1147                 dev_err(&adapter->dev,
1148                         "Failed to allocate memory for private data!\n");
1149                 return -ENOMEM;
1150         }
1151
1152         priv->info = icl->priv;
1153
1154         v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
1155
1156         icd->ops = &ov2640_ops;
1157
1158         ret = ov2640_video_probe(icd, client);
1159         if (ret) {
1160                 icd->ops = NULL;
1161                 kfree(priv);
1162         } else {
1163                 dev_info(&adapter->dev, "OV2640 Probed\n");
1164         }
1165
1166         return ret;
1167 }
1168
1169 static int ov2640_remove(struct i2c_client *client)
1170 {
1171         struct ov2640_priv       *priv = to_ov2640(client);
1172         struct soc_camera_device *icd = client->dev.platform_data;
1173
1174         icd->ops = NULL;
1175         kfree(priv);
1176         return 0;
1177 }
1178
1179 static const struct i2c_device_id ov2640_id[] = {
1180         { "ov2640", 0 },
1181         { }
1182 };
1183 MODULE_DEVICE_TABLE(i2c, ov2640_id);
1184
1185 static struct i2c_driver ov2640_i2c_driver = {
1186         .driver = {
1187                 .name = "ov2640",
1188         },
1189         .probe    = ov2640_probe,
1190         .remove   = ov2640_remove,
1191         .id_table = ov2640_id,
1192 };
1193
1194 /*
1195  * Module functions
1196  */
1197 static int __init ov2640_module_init(void)
1198 {
1199         return i2c_add_driver(&ov2640_i2c_driver);
1200 }
1201
1202 static void __exit ov2640_module_exit(void)
1203 {
1204         i2c_del_driver(&ov2640_i2c_driver);
1205 }
1206
1207 module_init(ov2640_module_init);
1208 module_exit(ov2640_module_exit);
1209
1210 MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
1211 MODULE_AUTHOR("Alberto Panizzo");
1212 MODULE_LICENSE("GPL v2");
1213 =======
1214 #include <linux/videodev2.h>
1215 #include <linux/slab.h>
1216 #include <linux/i2c.h>
1217 #include <linux/log2.h>
1218 #include <linux/platform_device.h>
1219 #include <linux/delay.h>
1220 #include <linux/circ_buf.h>
1221 #include <linux/miscdevice.h>
1222 #include <media/v4l2-common.h>
1223 #include <media/v4l2-chip-ident.h>
1224 #include <media/soc_camera.h>
1225 #include <mach/rk29_camera.h>
1226
1227 static int debug;
1228 module_param(debug, int, S_IRUGO|S_IWUSR);
1229
1230 #define dprintk(level, fmt, arg...) do {                        \
1231         if (debug >= level)                                     \
1232         printk(KERN_WARNING fmt , ## arg); } while (0)
1233
1234 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
1235 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
1236
1237
1238 #define _CONS(a,b) a##b
1239 #define CONS(a,b) _CONS(a,b)
1240
1241 #define __STR(x) #x
1242 #define _STR(x) __STR(x)
1243 #define STR(x) _STR(x)
1244
1245 #define MIN(x,y)   ((x<y) ? x: y)
1246 #define MAX(x,y)    ((x>y) ? x: y)
1247
1248 /* Sensor Driver Configuration */
1249 #define SENSOR_NAME RK29_CAM_SENSOR_OV2640
1250 #define SENSOR_V4L2_IDENT V4L2_IDENT_OV2640
1251 #define SENSOR_ID 0x2642
1252 #define SENSOR_ID1 0x2641
1253 #define SENSOR_MIN_WIDTH    640
1254 #define SENSOR_MIN_HEIGHT   480
1255 #define SENSOR_MAX_WIDTH    1600
1256 #define SENSOR_MAX_HEIGHT   1200
1257 #define SENSOR_INIT_WIDTH       640                     /* Sensor pixel size for sensor_init_data array */
1258 #define SENSOR_INIT_HEIGHT  480
1259 #define SENSOR_INIT_WINSEQADR sensor_vga
1260 #define SENSOR_INIT_PIXFMT V4L2_MBUS_FMT_YUYV8_2X8
1261
1262 #define CONFIG_SENSOR_WhiteBalance      0
1263 #define CONFIG_SENSOR_Brightness        1
1264 #define CONFIG_SENSOR_Contrast      0
1265 #define CONFIG_SENSOR_Saturation    1
1266 #define CONFIG_SENSOR_Effect        1
1267 #define CONFIG_SENSOR_Scene         0
1268 #define CONFIG_SENSOR_DigitalZoom   0
1269 #define CONFIG_SENSOR_Focus         0
1270 #define CONFIG_SENSOR_Exposure      0
1271 #define CONFIG_SENSOR_Flash         0
1272 #define CONFIG_SENSOR_Mirror        0
1273 #define CONFIG_SENSOR_Flip          0
1274
1275 #define CONFIG_SENSOR_I2C_SPEED     250000       /* Hz */
1276 /* Sensor write register continues by preempt_disable/preempt_enable for current process not be scheduled */
1277 #define CONFIG_SENSOR_I2C_NOSCHED   0
1278 #define CONFIG_SENSOR_I2C_RDWRCHK   0
1279
1280 #define SENSOR_BUS_PARAM  (SOCAM_MASTER | SOCAM_PCLK_SAMPLE_RISING |\
1281                           SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |\
1282                           SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8  |SOCAM_MCLK_24MHZ)
1283
1284 #define COLOR_TEMPERATURE_CLOUDY_DN  6500
1285 #define COLOR_TEMPERATURE_CLOUDY_UP    8000
1286 #define COLOR_TEMPERATURE_CLEARDAY_DN  5000
1287 #define COLOR_TEMPERATURE_CLEARDAY_UP    6500
1288 #define COLOR_TEMPERATURE_OFFICE_DN     3500
1289 #define COLOR_TEMPERATURE_OFFICE_UP     5000
1290 #define COLOR_TEMPERATURE_HOME_DN       2500
1291 #define COLOR_TEMPERATURE_HOME_UP       3500
1292
1293 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
1294 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
1295
1296 struct reginfo
1297 {
1298     u8 reg;
1299     u8 val;
1300 };
1301
1302 /* init 800*600 SVGA */
1303 static struct reginfo sensor_init_data[] =
1304 {
1305 #if 1
1306     {0xff,0x01},
1307     {0x12,0x80},
1308     {0xff,0x00},
1309     {0x2c,0xff},
1310     {0x2e,0xdf},
1311     {0xff,0x01},
1312
1313     {0x03,0x4f},
1314     {0x0f,0x4b},
1315
1316
1317     {0x3c,0x32},
1318     {0x11,0x00},
1319     {0x09,0x02},
1320     {0x04,0xF8},//b7,b6 directs
1321     {0x13,0xe5},
1322     {0x14,0x48},
1323     {0x2c,0x0c},
1324     {0x33,0x78},
1325     {0x3a,0x33},
1326     {0x3b,0xfB},
1327     {0x3e,0x00},
1328     {0x43,0x11},
1329     {0x16,0x10},
1330     {0x39,0x02},
1331     {0x35,0x88},
1332     {0x22,0x09},
1333     {0x37,0x40},
1334     {0x23,0x00},
1335     {0x34,0xa0},
1336     {0x36,0x1a},
1337     {0x06,0x02},
1338     {0x07,0xc0},
1339     {0x0d,0xb7},
1340     {0x0e,0x01},
1341     {0x4c,0x00},
1342     {0x4a,0x81},
1343     {0x21,0x99},
1344     
1345     //{0x24,0x58},
1346     //{0x25,0x50},
1347     //{0x26,0x92},
1348     
1349     {0x24, 0x70},
1350     {0x25, 0x60},
1351     {0x26, 0xa4},    
1352     
1353     {0x5c,0x00},
1354     {0x63,0x00},
1355     {0x46,0x3f},
1356     {0x0c,0x3c},
1357     {0x61,0x70},
1358     {0x62,0x80},
1359     {0x7c,0x05},
1360     {0x20,0x80},
1361     {0x28,0x30},
1362     {0x6c,0x00},
1363     {0x6d,0x80},
1364     {0x6e,0x00},
1365     {0x70,0x02},
1366     {0x71,0x94},
1367     {0x73,0xc1},
1368     {0x3d,0x34},
1369     {0x5a,0x57},
1370     {0x4f,0xbb},
1371     {0x50,0x9c},
1372     {0xff,0x00},
1373     {0xe5,0x7f},
1374     {0xf9,0xc0},
1375     {0x41,0x24},
1376     {0xe0,0x14},
1377     {0x76,0xff},
1378     {0x33,0xa0},
1379     {0x42,0x20},
1380     {0x43,0x18},
1381     {0x4c,0x00},
1382     {0x87,0xd0},
1383     {0x88,0x3f},
1384     {0xd7,0x03},
1385     {0xd9,0x10},
1386     {0xd3,0x82},
1387     {0xc8,0x08},
1388     {0xc9,0x80},
1389     {0x7c,0x00},
1390     {0x7d,0x00},//0x00//0x07
1391     {0x7c,0x03},
1392     {0x7d,0x48},//0x48//0x40
1393     {0x7d,0x48},//0x48//0x40
1394     {0x7c,0x08},
1395     {0x7d,0x20},
1396     {0x7d,0x10},//0x10
1397     {0x7d,0x0e},//0x0e
1398
1399     {0x92,0x00},
1400     {0x93,0x06},
1401     {0x93,0xc8},//e3
1402     {0x93,0x05},
1403     {0x93,0x05},
1404     {0x93,0x00},
1405     {0x93,0x04},
1406     {0x93,0x00},
1407     {0x93,0x00},
1408     {0x93,0x00},
1409     {0x93,0x00},
1410     {0x93,0x00},
1411     {0x93,0x00},
1412     {0x93,0x00},
1413     {0x96,0x00},
1414     {0x97,0x08},
1415     {0x97,0x19},
1416     {0x97,0x02},
1417     {0x97,0x0c},
1418     {0x97,0x24},
1419     {0x97,0x30},
1420     {0x97,0x28},
1421     {0x97,0x26},
1422     {0x97,0x02},
1423     {0x97,0x98},
1424     {0x97,0x80},
1425     {0x97,0x00},
1426     {0x97,0x00},
1427     {0xc3,0xef},//ed
1428     {0xa4,0x00},
1429     {0xa8,0x00},
1430
1431     {0xbf, 0x00},
1432     {0xba, 0xdc},
1433     {0xbb, 0x08},
1434     {0xb6, 0x20},
1435     {0xb8, 0x30},
1436     {0xb7, 0x20},
1437     {0xb9, 0x30},
1438     {0xb3, 0xb4},
1439     {0xb4, 0xca},
1440     {0xb5, 0x34},
1441     {0xb0, 0x46},
1442     {0xb1, 0x46},
1443     {0xb2, 0x06},
1444     {0xc7, 0x00},
1445     {0xc6, 0x51},
1446     {0xc5, 0x11},
1447     {0xc4, 0x9c},
1448 ////
1449     {0xc0,0xc8},
1450     {0xc1,0x96},
1451     {0x86,0x3d},
1452     {0x50,0x92},
1453     {0x51,0x90},
1454     {0x52,0x2c},
1455     {0x53,0x00},
1456     {0x54,0x00},
1457     {0x55,0x88},
1458     {0x57,0x00},
1459     {0x5a,0x50},
1460     {0x5b,0x3c},
1461     {0x5c,0x00},
1462     {0xc3,0xed},
1463     {0x7f,0x00},
1464     {0xda,0x01},
1465     {0xe5,0x1f},
1466     {0xe1,0x67},
1467     {0xe0,0x00},
1468     {0xdd,0xff},
1469     {0x05,0x00},
1470
1471 #endif
1472 #if 1
1473     {0xff, 0x01},
1474     {0x5d, 0x00},
1475     {0x5e, 0x3c},
1476     {0x5f, 0x28},
1477     {0x60, 0x55},
1478
1479
1480     {0xff, 0x00},
1481     {0xc3, 0xef},
1482     {0xa6, 0x00},
1483     {0xa7, 0x0f},
1484     {0xa7, 0x4e},
1485     {0xa7, 0x7a},
1486     {0xa7, 0x33},
1487     {0xa7, 0x00},
1488     {0xa7, 0x23},
1489     {0xa7, 0x27},
1490     {0xa7, 0x3a},
1491     {0xa7, 0x70},
1492     {0xa7, 0x33},
1493     {0xa7, 0x00},//L
1494     {0xa7, 0x23},
1495     {0xa7, 0x20},
1496     {0xa7, 0x0c},
1497     {0xa7, 0x66},
1498     {0xa7, 0x33},
1499     {0xa7, 0x00},
1500     {0xa7, 0x23},
1501     {0xc3, 0xef},
1502 #endif
1503
1504
1505 #if 1
1506     {0xff,0x00},
1507     {0x92,0x00},
1508     {0x93,0x06},
1509     {0x93,0xc1},//e
1510     {0x93,0x02},
1511     {0x93,0x02},
1512     {0x93,0x00},
1513     {0x93,0x04},
1514 #endif
1515
1516     {0x03, 0x0f},
1517     {0xe0, 0x04},
1518     {0xc0, 0xc8},
1519     {0xc1, 0x96},
1520     {0x86, 0x3d},
1521     {0x50, 0x89},
1522     {0x51, 0x90},
1523     {0x52, 0x2c},
1524     {0x53, 0x00},
1525     {0x54, 0x00},
1526     {0x55, 0x88},
1527     {0x57, 0x00},
1528     {0x5a, 0xa0},
1529     {0x5b, 0x78},
1530     {0x5c, 0x00},
1531     {0xd3, 0x04},
1532     {0xe0, 0x00},
1533
1534   {0x0, 0x0}   //end flag
1535
1536 };
1537
1538 /* 1600X1200 UXGA */
1539 static struct reginfo sensor_uxga[] =
1540 {
1541     {0xff, 0x00},
1542     {0xe0, 0x04},
1543     {0xc0, 0xc8},
1544     {0xc1, 0x96},
1545     {0x86, 0x3d},
1546     {0x50, 0x00},
1547     {0x51, 0x90},
1548     {0x52, 0x2c},
1549     {0x53, 0x00},
1550     {0x54, 0x00},
1551     {0x55, 0x88},
1552     {0x57, 0x00},
1553     {0x5a, 0x90},
1554     {0x5b, 0x2c},
1555     {0x5c, 0x05},
1556     {0xd3, 0x82},
1557     {0xe0, 0x00},
1558   {0x0, 0x0}   //end flag
1559 };
1560
1561 /* 1280X1024 SXGA */
1562 static struct reginfo sensor_sxga[] =
1563 {
1564     {0xff, 0x00},
1565     {0xe0, 0x04},
1566     {0xc0, 0xc8},
1567     {0xc1, 0x96},
1568     {0x86, 0x3d},
1569     {0x50, 0x00},
1570     {0x51, 0x90},
1571     {0x52, 0x2c},
1572     {0x53, 0x00},
1573     {0x54, 0x00},
1574     {0x55, 0x88},
1575     {0x57, 0x00},
1576     {0x5a, 0x40},
1577     {0x5b, 0x00},
1578     {0x5c, 0x05},
1579     {0xd3, 0x82},
1580     {0xe0, 0x00},
1581   {0x0, 0x0}   //end flag
1582 };
1583
1584
1585 static struct reginfo sensor_xga[] =
1586 {
1587     {0xff, 0x00},
1588     {0xe0, 0x04},
1589     {0xc0, 0xc8},
1590     {0xc1, 0x96},
1591     {0x86, 0x3d},
1592     {0x50, 0x00},
1593     {0x51, 0x90},
1594     {0x52, 0x2c},
1595     {0x53, 0x00},
1596     {0x54, 0x00},
1597     {0x55, 0x88},
1598     {0x57, 0x00},
1599     {0x5a, 0x40},
1600     {0x5b, 0x00},
1601     {0x5c, 0x05},
1602     {0xd3, 0x82},
1603     {0xe0, 0x00},
1604   {0x0, 0x0}   //end flag
1605
1606
1607 };
1608
1609
1610 /* 800X600 SVGA*/
1611 static struct reginfo sensor_svga[] =
1612 {
1613   {0x0, 0x0}   //end flag
1614 };
1615
1616 /* 640X480 VGA */
1617 static struct reginfo sensor_vga[] =
1618 {
1619    {0x0, 0x0}   //end flag
1620  };
1621
1622 /* 352X288 CIF */
1623 static struct reginfo sensor_cif[] =
1624 {
1625   {0x0, 0x0}   //end flag
1626 };
1627
1628 /* 320*240 QVGA */
1629 static  struct reginfo sensor_qvga[] =
1630 {
1631   {0x0, 0x0}   //end flag
1632 };
1633
1634 /* 176X144 QCIF*/
1635 static struct reginfo sensor_qcif[] =
1636 {
1637   {0x0, 0x0}   //end flag
1638 };
1639 #if 0
1640 /* 160X120 QQVGA*/
1641 static struct reginfo ov2655_qqvga[] =
1642 {
1643
1644     {0x300E, 0x34},
1645     {0x3011, 0x01},
1646     {0x3012, 0x10},
1647     {0x302a, 0x02},
1648     {0x302b, 0xE6},
1649     {0x306f, 0x14},
1650     {0x3362, 0x90},
1651
1652     {0x3070, 0x5d},
1653     {0x3072, 0x5d},
1654     {0x301c, 0x07},
1655     {0x301d, 0x07},
1656
1657     {0x3020, 0x01},
1658     {0x3021, 0x18},
1659     {0x3022, 0x00},
1660     {0x3023, 0x06},
1661     {0x3024, 0x06},
1662     {0x3025, 0x58},
1663     {0x3026, 0x02},
1664     {0x3027, 0x61},
1665     {0x3088, 0x00},
1666     {0x3089, 0xa0},
1667     {0x308a, 0x00},
1668     {0x308b, 0x78},
1669     {0x3316, 0x64},
1670     {0x3317, 0x25},
1671     {0x3318, 0x80},
1672     {0x3319, 0x08},
1673     {0x331a, 0x0a},
1674     {0x331b, 0x07},
1675     {0x331c, 0x80},
1676     {0x331d, 0x38},
1677     {0x3100, 0x00},
1678     {0x3302, 0x11},
1679
1680     {0x0, 0x0},
1681 };
1682
1683
1684
1685 static  struct reginfo ov2655_Sharpness_auto[] =
1686 {
1687     {0x3306, 0x00},
1688 };
1689
1690 static  struct reginfo ov2655_Sharpness1[] =
1691 {
1692     {0x3306, 0x08},
1693     {0x3371, 0x00},
1694 };
1695
1696 static  struct reginfo ov2655_Sharpness2[][3] =
1697 {
1698     //Sharpness 2
1699     {0x3306, 0x08},
1700     {0x3371, 0x01},
1701 };
1702
1703 static  struct reginfo ov2655_Sharpness3[] =
1704 {
1705     //default
1706     {0x3306, 0x08},
1707     {0x332d, 0x02},
1708 };
1709 static  struct reginfo ov2655_Sharpness4[]=
1710 {
1711     //Sharpness 4
1712     {0x3306, 0x08},
1713     {0x332d, 0x03},
1714 };
1715
1716 static  struct reginfo ov2655_Sharpness5[] =
1717 {
1718     //Sharpness 5
1719     {0x3306, 0x08},
1720     {0x332d, 0x04},
1721 };
1722 #endif
1723
1724 static  struct reginfo sensor_ClrFmt_YUYV[]=
1725 {
1726     //{0x4300, 0x30},
1727     {0x00, 0x00}
1728 };
1729
1730 static  struct reginfo sensor_ClrFmt_UYVY[]=
1731 {
1732     //{0x4300, 0x32},
1733     {0x00, 0x00}
1734 };
1735
1736 #if CONFIG_SENSOR_WhiteBalance
1737 static  struct reginfo sensor_WhiteB_Auto[]=
1738 {
1739     {0x3406, 0x00},  //AWB auto, bit[1]:0,auto
1740     {0x0000, 0x00}
1741 };
1742 /* Cloudy Colour Temperature : 6500K - 8000K  */
1743 static  struct reginfo sensor_WhiteB_Cloudy[]=
1744 {
1745     {0x3406, 0x01},
1746     {0x3400, 0x07},
1747     {0x3401, 0x08},
1748     {0x3402, 0x04},
1749     {0x3403, 0x00},
1750     {0x3404, 0x05},
1751     {0x3405, 0x00},
1752     {0x0000, 0x00}
1753 };
1754 /* ClearDay Colour Temperature : 5000K - 6500K  */
1755 static  struct reginfo sensor_WhiteB_ClearDay[]=
1756 {
1757     //Sunny
1758     {0x3406, 0x01},
1759     {0x3400, 0x07},
1760     {0x3401, 0x02},
1761     {0x3402, 0x04},
1762     {0x3403, 0x00},
1763     {0x3404, 0x05},
1764     {0x3405, 0x15},
1765     {0x0000, 0x00}
1766 };
1767 /* Office Colour Temperature : 3500K - 5000K  */
1768 static  struct reginfo sensor_WhiteB_TungstenLamp1[]=
1769 {
1770     //Office
1771     {0x3406, 0x01},
1772     {0x3400, 0x06},
1773     {0x3401, 0x2a},
1774     {0x3402, 0x04},
1775     {0x3403, 0x00},
1776     {0x3404, 0x07},
1777     {0x3405, 0x24},
1778     {0x0000, 0x00}
1779
1780 };
1781 /* Home Colour Temperature : 2500K - 3500K  */
1782 static  struct reginfo sensor_WhiteB_TungstenLamp2[]=
1783 {
1784     //Home
1785     {0x3406, 0x01},
1786     {0x3400, 0x04},
1787     {0x3401, 0x58},
1788     {0x3402, 0x04},
1789     {0x3403, 0x00},
1790     {0x3404, 0x07},
1791     {0x3405, 0x24},
1792     {0x0000, 0x00}
1793 };
1794 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1795     sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1796 };
1797 #endif
1798
1799 #if CONFIG_SENSOR_Brightness
1800 static  struct reginfo sensor_Brightness0[]=
1801 {
1802     // Brightness -2
1803     {0xff, 0x01},
1804     {0x24, 0x34},
1805     {0x25, 0x22},
1806     {0x26, 0x70},
1807   {0x0, 0x0}   //end flag
1808 };
1809
1810 static  struct reginfo sensor_Brightness1[]=
1811 {
1812     // Brightness -1
1813
1814     {0xff, 0x01},
1815     {0x24, 0x58},
1816     {0x25, 0x50},
1817     {0x26, 0x92},
1818   {0x0, 0x0}   //end flag
1819 };
1820
1821 static  struct reginfo sensor_Brightness2[]=
1822 {
1823     //  Brightness 0
1824
1825         {0xff, 0x01},
1826         {0x24, 0xa8},
1827         {0x25, 0x90},
1828         {0x26, 0xd6},
1829   {0x0, 0x0}   //end flag
1830 };
1831
1832 static  struct reginfo sensor_Brightness3[]=
1833 {
1834     // Brightness +1
1835
1836     {0xff, 0x01},
1837     {0x24, 0x48},
1838     {0x25, 0x40},
1839     {0x26, 0x81},
1840   {0x0, 0x0}   //end flag
1841 };
1842
1843 static  struct reginfo sensor_Brightness4[]=
1844 {
1845     //  Brightness +2
1846
1847     {0xff, 0x01},
1848     {0x24, 0x58},
1849     {0x25, 0x50},
1850     {0x26, 0x92},
1851   {0x0, 0x0}   //end flag
1852 };
1853
1854 static  struct reginfo sensor_Brightness5[]=
1855 {
1856     //  Brightness +3
1857     {0xff, 0x01},
1858     {0x24, 0x70},
1859     {0x25, 0x60},
1860     {0x26, 0xa4},
1861   {0x0, 0x0}   //end flag
1862 };
1863 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1864     sensor_Brightness4, sensor_Brightness5,NULL,
1865 };
1866
1867 #endif
1868
1869 #if CONFIG_SENSOR_Effect
1870 static  struct reginfo sensor_Effect_Normal[] =
1871 {
1872         {0xff,0x00},
1873         {0x7c,0x00},
1874         {0x7d,0x00},
1875         {0x7c,0x05},
1876         {0x7d,0x80},
1877         {0x7d,0x80},
1878   {0x0, 0x0}   //end flag
1879 };
1880
1881 static  struct reginfo sensor_Effect_WandB[] =
1882 {
1883     {0xff,0x00},
1884     {0x7c,0x00},
1885     {0x7d,0x18},
1886     {0x7c,0x05},
1887     {0x7d,0x80},
1888     {0x7d,0x80},
1889   {0x0, 0x0}   //end flag
1890 };
1891
1892 static  struct reginfo sensor_Effect_Sepia[] =
1893 {
1894     {0xff,0x00},
1895     {0x7c,0x00},
1896     {0x7d,0x18},
1897     {0x7c,0x05},
1898     {0x7d,0x40},
1899     {0x7d,0xc0},
1900   {0x0, 0x0}   //end flag
1901 };
1902
1903 static  struct reginfo sensor_Effect_Negative[] =
1904 {
1905     {0xff,0x00},
1906     {0x7c,0x00},
1907     {0x7d,0x40},
1908     {0x7c,0x05},
1909     {0x7d,0x80},
1910     {0x7d,0x80},
1911   {0x0, 0x0}   //end flag
1912 };
1913 static  struct reginfo sensor_Effect_Bluish[] =
1914 {
1915     {0Xff, 0X00},
1916     {0X7c, 0X00},
1917     {0X7d, 0X18},
1918     {0X7c, 0X05},
1919     {0X7d, 0Xa0},
1920     {0X7d, 0X40},
1921   {0x0, 0x0}   //end flag
1922 };
1923
1924 static  struct reginfo sensor_Effect_Green[] =
1925 {
1926     {0Xff, 0X00},
1927     {0X7c, 0X00},
1928     {0X7d, 0X18},
1929     {0X7c, 0X05},
1930     {0X7d, 0X40},
1931     {0X7d, 0X40},
1932   {0x0, 0x0}   //end flag
1933 };
1934
1935 static  struct reginfo sensor_Effect_Exp_Windows_Half[] =
1936 {
1937         {0xff, 0x01},
1938         {0x5d, 0x00},
1939         {0x5e, 0x3c},
1940         {0x5f, 0x28},
1941         {0x60, 0x55},
1942   {0x0, 0x0}   //end flag
1943 };
1944 static struct reginfo *sensor_EffectSeqe[] = {sensor_Effect_Normal, sensor_Effect_WandB, sensor_Effect_Negative,sensor_Effect_Sepia,
1945     sensor_Effect_Bluish, sensor_Effect_Green,NULL,
1946 };
1947 #endif
1948 #if CONFIG_SENSOR_Exposure
1949 static  struct reginfo sensor_Exposure0[]=
1950 {
1951         {0x0000, 0x00}
1952 };
1953
1954 static  struct reginfo sensor_Exposure1[]=
1955 {
1956     {0x0000, 0x00}
1957 };
1958
1959 static  struct reginfo sensor_Exposure2[]=
1960 {
1961     {0x0000, 0x00}
1962 };
1963
1964 static  struct reginfo sensor_Exposure3[]=
1965 {
1966     {0x0000, 0x00}
1967 };
1968
1969 static  struct reginfo sensor_Exposure4[]=
1970 {
1971     {0x0000, 0x00}
1972 };
1973
1974 static  struct reginfo sensor_Exposure5[]=
1975 {
1976     {0x0000, 0x00}
1977 };
1978
1979 static  struct reginfo sensor_Exposure6[]=
1980 {
1981     {0x0000, 0x00}
1982 };
1983
1984 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1985     sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1986 };
1987 #endif
1988 #if CONFIG_SENSOR_Saturation
1989 static  struct reginfo sensor_Saturation0[]=
1990 {
1991         {0xff, 0x00},
1992         {0x90, 0x00},
1993         {0x91, 0x0e},
1994         {0x91, 0x1a},
1995         {0x91, 0x31},
1996         {0x91, 0x5a},
1997         {0x91, 0x69},
1998         {0x91, 0x75},
1999         {0x91, 0x7e},
2000         {0x91, 0x88},
2001         {0x91, 0x8f},
2002         {0x91, 0x96},
2003         {0x91, 0xa3},
2004         {0x91, 0xaf},
2005         {0x91, 0xc4},
2006         {0x91, 0xd7},
2007         {0x91, 0xe8},
2008         {0x91, 0x20},
2009   {0x0, 0x0}   //end flag
2010 };
2011
2012 static  struct reginfo sensor_Saturation1[]=
2013 {
2014     {0xff, 0x00},
2015     {0x90, 0x00},
2016     {0x91, 0x03},
2017     {0x91, 0x0a},
2018     {0x91, 0x1a},
2019     {0x91, 0x3f},
2020     {0x91, 0x4e},
2021     {0x91, 0x5b},
2022     {0x91, 0x68},
2023     {0x91, 0x75},
2024     {0x91, 0x7f},
2025     {0x91, 0x89},
2026     {0x91, 0x9a},
2027     {0x91, 0xa6},
2028     {0x91, 0xbd},
2029     {0x91, 0xd3},
2030     {0x91, 0xe5},
2031     {0x91, 0x24},
2032   {0x0, 0x0}   //end flag
2033 };
2034
2035 static  struct reginfo sensor_Saturation2[]=
2036 {
2037     {0xff, 0x00},
2038     {0x90, 0x00},
2039     {0x91, 0x04},
2040     {0x91, 0x07},
2041     {0x91, 0x10},
2042     {0x91, 0x28},
2043     {0x91, 0x36},
2044     {0x91, 0x44},
2045     {0x91, 0x52},
2046     {0x91, 0x60},
2047     {0x91, 0x6c},
2048     {0x91, 0x78},
2049     {0x91, 0x8c},
2050     {0x91, 0x9e},
2051     {0x91, 0xbb},
2052     {0x91, 0xd3},
2053     {0x91, 0xe5},
2054     {0x91, 0x24},
2055   {0x0, 0x0}   //end flag       
2056 };
2057 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2058
2059
2060 #endif
2061 #if CONFIG_SENSOR_Contrast
2062 static  struct reginfo sensor_Contrast0[]=
2063 {
2064     {0xff, 0x00},
2065     {0x7c, 0x00},
2066     {0x7d, 0x04},
2067     {0x7c, 0x07},
2068     {0x7d, 0x20},
2069     {0x7d, 0x10},
2070     {0x7d, 0x4a},
2071     {0x7d, 0x06},
2072   {0x0, 0x0}   //end flag
2073
2074 };
2075
2076 static  struct reginfo sensor_Contrast1[]=
2077 {
2078     {0xff, 0x00},
2079     {0x7c, 0x00},
2080     {0x7d, 0x04},
2081     {0x7c, 0x07},
2082     {0x7d, 0x20},
2083     {0x7d, 0x14},
2084     {0x7d, 0x40},
2085     {0x7d, 0x06},
2086   {0x0, 0x0}   //end flag
2087 };
2088
2089 static  struct reginfo sensor_Contrast2[]=
2090 {
2091     {0xff, 0x00},
2092     {0x7c, 0x00},
2093     {0x7d, 0x04},
2094     {0x7c, 0x07},
2095     {0x7d, 0x20},
2096     {0x7d, 0x18},
2097     {0x7d, 0x34},
2098     {0x7d, 0x06},
2099   {0x0, 0x0}   //end flag
2100 };
2101
2102 static  struct reginfo sensor_Contrast3[]=
2103 {
2104     {0xff, 0x00},
2105     {0x7c, 0x00},
2106     {0x7d, 0x04},
2107     {0x7c, 0x07},
2108     {0x7d, 0x20},
2109     {0x7d, 0x1c},
2110     {0x7d, 0x2a},
2111     {0x7d, 0x06},
2112   {0x0, 0x0}   //end flag
2113 };
2114
2115 static  struct reginfo sensor_Contrast4[]=
2116 {
2117     {0xff,0x00},
2118     {0x7c,0x00},
2119     {0x7d,0x04},
2120     {0x7c,0x07},
2121     {0x7d,0x20},
2122     {0x7d,0x24},
2123     {0x7d,0x16},
2124     {0x7d,0x06},
2125   {0x0, 0x0}   //end flag
2126 };
2127
2128
2129 static  struct reginfo sensor_Contrast5[]=
2130 {
2131     {0xff, 0x00},
2132     {0x7c, 0x00},
2133     {0x7d, 0x04},
2134     {0x7c, 0x07},
2135     {0x7d, 0x20},
2136     {0x7d, 0x20},
2137     {0x7d, 0x20},
2138     {0x7d, 0x06},
2139   {0x0, 0x0}   //end flag
2140 };
2141
2142 static  struct reginfo sensor_Contrast6[]=
2143 {
2144     {0xff, 0x00},
2145     {0x7c, 0x00},
2146     {0x7d, 0x04},
2147     {0x7c, 0x07},
2148     {0x7d, 0x20},
2149     {0x7d, 0x24},
2150     {0x7d, 0x16},
2151     {0x7d, 0x06},
2152   {0x0, 0x0}   //end flag
2153 };
2154
2155
2156 static  struct reginfo sensor_Contrast7[]=
2157 {
2158     {0xff, 0x00},
2159     {0x7c, 0x00},
2160     {0x7d, 0x04},
2161     {0x7c, 0x07},
2162     {0x7d, 0x20},
2163     {0x7d, 0x28},
2164     {0x7d, 0x0c},
2165     {0x7d, 0x06},
2166   {0x0, 0x0}   //end flag
2167 };
2168
2169 static  struct reginfo sensor_Contrast8[]=
2170 {
2171     {0xff, 0x00},
2172     {0x7c, 0x00},
2173     {0x7d, 0x04},
2174     {0x7c, 0x07},
2175     {0x7d, 0x20},
2176     {0x7d, 0x2c},
2177     {0x7d, 0x02},
2178     {0x7d, 0x06},
2179   {0x0, 0x0}   //end flag
2180 };
2181
2182 static  struct reginfo sensor_Contrast9[]=
2183 {
2184     {0xff, 0x00},
2185     {0x7c, 0x00},
2186     {0x7d, 0x04},
2187     {0x7c, 0x07},
2188     {0x7d, 0x20},
2189     {0x7d, 0x30},
2190     {0x7d, 0x08},
2191     {0x7d, 0x0e},
2192   {0x0, 0x0}   //end flag
2193 };
2194
2195
2196
2197 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2198     sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2199 };
2200
2201 #endif
2202 #if CONFIG_SENSOR_Mirror
2203 static  struct reginfo sensor_MirrorOn[]=
2204 {
2205     {0x0000, 0x00}
2206 };
2207
2208 static  struct reginfo sensor_MirrorOff[]=
2209 {
2210     {0x0000, 0x00}
2211 };
2212 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2213 #endif
2214 #if CONFIG_SENSOR_Flip
2215 static  struct reginfo sensor_FlipOn[]=
2216 {
2217     {0x0000, 0x00}
2218 };
2219
2220 static  struct reginfo sensor_FlipOff[]=
2221 {
2222     {0x0000, 0x00}
2223 };
2224 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
2225
2226 #endif
2227 #if CONFIG_SENSOR_Scene
2228 static  struct reginfo sensor_SceneAuto[] =
2229 {
2230     {0x3a00, 0x78},
2231     {0x0000, 0x00}
2232 };
2233
2234 static  struct reginfo sensor_SceneNight[] =
2235 {
2236     {0x3003, 0x80},
2237         {0x3004, 0x20},
2238         {0x3005, 0x18},
2239         {0x3006, 0x0d},
2240         {0x3a00, 0x7c},
2241         {0x3a02 ,0x07},
2242         {0x3a03 ,0x38},
2243         {0x3a14 ,0x07},
2244         {0x3a15 ,0x38},
2245     {0x0000, 0x00}
2246 };
2247 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
2248
2249 #endif
2250 #if CONFIG_SENSOR_DigitalZoom
2251 static struct reginfo sensor_Zoom0[] =
2252 {
2253     {0x0, 0x0},
2254 };
2255
2256 static struct reginfo sensor_Zoom1[] =
2257 {
2258      {0x0, 0x0},
2259 };
2260
2261 static struct reginfo sensor_Zoom2[] =
2262 {
2263     {0x0, 0x0},
2264 };
2265
2266
2267 static struct reginfo sensor_Zoom3[] =
2268 {
2269     {0x0, 0x0},
2270 };
2271 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
2272 #endif
2273 static const struct v4l2_querymenu sensor_menus[] =
2274 {
2275         #if CONFIG_SENSOR_WhiteBalance
2276     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 0,  .name = "auto",  .reserved = 0, }, {  .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 1, .name = "incandescent",  .reserved = 0,},
2277     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 2,  .name = "fluorescent", .reserved = 0,}, {  .id = V4L2_CID_DO_WHITE_BALANCE, .index = 3,  .name = "daylight", .reserved = 0,},
2278     { .id = V4L2_CID_DO_WHITE_BALANCE,  .index = 4,  .name = "cloudy-daylight", .reserved = 0,},
2279     #endif
2280
2281         #if CONFIG_SENSOR_Effect
2282     { .id = V4L2_CID_EFFECT,  .index = 0,  .name = "none",  .reserved = 0, }, {  .id = V4L2_CID_EFFECT,  .index = 1, .name = "mono",  .reserved = 0,},
2283     { .id = V4L2_CID_EFFECT,  .index = 2,  .name = "negative", .reserved = 0,}, {  .id = V4L2_CID_EFFECT, .index = 3,  .name = "sepia", .reserved = 0,},
2284     { .id = V4L2_CID_EFFECT,  .index = 4, .name = "posterize", .reserved = 0,} ,{ .id = V4L2_CID_EFFECT,  .index = 5,  .name = "aqua", .reserved = 0,},
2285     #endif
2286
2287         #if CONFIG_SENSOR_Scene
2288     { .id = V4L2_CID_SCENE,  .index = 0, .name = "auto", .reserved = 0,} ,{ .id = V4L2_CID_SCENE,  .index = 1,  .name = "night", .reserved = 0,},
2289     #endif
2290
2291         #if CONFIG_SENSOR_Flash
2292     { .id = V4L2_CID_FLASH,  .index = 0,  .name = "off",  .reserved = 0, }, {  .id = V4L2_CID_FLASH,  .index = 1, .name = "auto",  .reserved = 0,},
2293     { .id = V4L2_CID_FLASH,  .index = 2,  .name = "on", .reserved = 0,}, {  .id = V4L2_CID_FLASH, .index = 3,  .name = "torch", .reserved = 0,},
2294     #endif
2295 };
2296
2297 static const struct v4l2_queryctrl sensor_controls[] =
2298 {
2299         #if CONFIG_SENSOR_WhiteBalance
2300     {
2301         .id             = V4L2_CID_DO_WHITE_BALANCE,
2302         .type           = V4L2_CTRL_TYPE_MENU,
2303         .name           = "White Balance Control",
2304         .minimum        = 0,
2305         .maximum        = 4,
2306         .step           = 1,
2307         .default_value = 0,
2308     },
2309     #endif
2310
2311         #if CONFIG_SENSOR_Brightness
2312         {
2313         .id             = V4L2_CID_BRIGHTNESS,
2314         .type           = V4L2_CTRL_TYPE_INTEGER,
2315         .name           = "Brightness Control",
2316         .minimum        = -3,
2317         .maximum        = 2,
2318         .step           = 1,
2319         .default_value = 0,
2320     },
2321     #endif
2322
2323         #if CONFIG_SENSOR_Effect
2324         {
2325         .id             = V4L2_CID_EFFECT,
2326         .type           = V4L2_CTRL_TYPE_MENU,
2327         .name           = "Effect Control",
2328         .minimum        = 0,
2329         .maximum        = 5,
2330         .step           = 1,
2331         .default_value = 0,
2332     },
2333         #endif
2334
2335         #if CONFIG_SENSOR_Exposure
2336         {
2337         .id             = V4L2_CID_EXPOSURE,
2338         .type           = V4L2_CTRL_TYPE_INTEGER,
2339         .name           = "Exposure Control",
2340         .minimum        = 0,
2341         .maximum        = 6,
2342         .step           = 1,
2343         .default_value = 0,
2344     },
2345         #endif
2346
2347         #if CONFIG_SENSOR_Saturation
2348         {
2349         .id             = V4L2_CID_SATURATION,
2350         .type           = V4L2_CTRL_TYPE_INTEGER,
2351         .name           = "Saturation Control",
2352         .minimum        = 0,
2353         .maximum        = 2,
2354         .step           = 1,
2355         .default_value = 0,
2356     },
2357     #endif
2358
2359         #if CONFIG_SENSOR_Contrast
2360         {
2361         .id             = V4L2_CID_CONTRAST,
2362         .type           = V4L2_CTRL_TYPE_INTEGER,
2363         .name           = "Contrast Control",
2364         .minimum        = -3,
2365         .maximum        = 3,
2366         .step           = 1,
2367         .default_value = 0,
2368     },
2369         #endif
2370
2371         #if CONFIG_SENSOR_Mirror
2372         {
2373         .id             = V4L2_CID_HFLIP,
2374         .type           = V4L2_CTRL_TYPE_BOOLEAN,
2375         .name           = "Mirror Control",
2376         .minimum        = 0,
2377         .maximum        = 1,
2378         .step           = 1,
2379         .default_value = 1,
2380     },
2381     #endif
2382
2383         #if CONFIG_SENSOR_Flip
2384         {
2385         .id             = V4L2_CID_VFLIP,
2386         .type           = V4L2_CTRL_TYPE_BOOLEAN,
2387         .name           = "Flip Control",
2388         .minimum        = 0,
2389         .maximum        = 1,
2390         .step           = 1,
2391         .default_value = 1,
2392     },
2393     #endif
2394
2395         #if CONFIG_SENSOR_Scene
2396     {
2397         .id             = V4L2_CID_SCENE,
2398         .type           = V4L2_CTRL_TYPE_MENU,
2399         .name           = "Scene Control",
2400         .minimum        = 0,
2401         .maximum        = 1,
2402         .step           = 1,
2403         .default_value = 0,
2404     },
2405     #endif
2406
2407         #if CONFIG_SENSOR_DigitalZoom
2408     {
2409         .id             = V4L2_CID_ZOOM_RELATIVE,
2410         .type           = V4L2_CTRL_TYPE_INTEGER,
2411         .name           = "DigitalZoom Control",
2412         .minimum        = -1,
2413         .maximum        = 1,
2414         .step           = 1,
2415         .default_value = 0,
2416     }, {
2417         .id             = V4L2_CID_ZOOM_ABSOLUTE,
2418         .type           = V4L2_CTRL_TYPE_INTEGER,
2419         .name           = "DigitalZoom Control",
2420         .minimum        = 0,
2421         .maximum        = 3,
2422         .step           = 1,
2423         .default_value = 0,
2424     },
2425     #endif
2426
2427         #if CONFIG_SENSOR_Focus
2428         {
2429         .id             = V4L2_CID_FOCUS_RELATIVE,
2430         .type           = V4L2_CTRL_TYPE_INTEGER,
2431         .name           = "Focus Control",
2432         .minimum        = -1,
2433         .maximum        = 1,
2434         .step           = 1,
2435         .default_value = 0,
2436     }, {
2437         .id             = V4L2_CID_FOCUS_ABSOLUTE,
2438         .type           = V4L2_CTRL_TYPE_INTEGER,
2439         .name           = "Focus Control",
2440         .minimum        = 0,
2441         .maximum        = 255,
2442         .step           = 1,
2443         .default_value = 125,
2444     },
2445     #endif
2446
2447         #if CONFIG_SENSOR_Flash
2448         {
2449         .id             = V4L2_CID_FLASH,
2450         .type           = V4L2_CTRL_TYPE_MENU,
2451         .name           = "Flash Control",
2452         .minimum        = 0,
2453         .maximum        = 3,
2454         .step           = 1,
2455         .default_value = 0,
2456     },
2457         #endif
2458 };
2459
2460 static int sensor_probe(struct i2c_client *client, const struct i2c_device_id *did);
2461 static int sensor_video_probe(struct soc_camera_device *icd, struct i2c_client *client);
2462 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
2463 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl);
2464 static int sensor_g_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
2465 static int sensor_s_ext_controls(struct v4l2_subdev *sd,  struct v4l2_ext_controls *ext_ctrl);
2466 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg);
2467 static int sensor_resume(struct soc_camera_device *icd);
2468 static int sensor_set_bus_param(struct soc_camera_device *icd,unsigned long flags);
2469 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd);
2470 #if CONFIG_SENSOR_Effect
2471 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
2472 #endif
2473 #if CONFIG_SENSOR_WhiteBalance
2474 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
2475 #endif
2476 static int sensor_deactivate(struct i2c_client *client);
2477
2478 static struct soc_camera_ops sensor_ops =
2479 {
2480     .suspend                     = sensor_suspend,
2481     .resume                       = sensor_resume,
2482     .set_bus_param              = sensor_set_bus_param,
2483     .query_bus_param    = sensor_query_bus_param,
2484     .controls           = sensor_controls,
2485     .menus                         = sensor_menus,
2486     .num_controls               = ARRAY_SIZE(sensor_controls),
2487     .num_menus          = ARRAY_SIZE(sensor_menus),
2488 };
2489
2490 /* only one fixed colorspace per pixelcode */
2491 struct sensor_datafmt {
2492         enum v4l2_mbus_pixelcode code;
2493         enum v4l2_colorspace colorspace;
2494 };
2495
2496 /* Find a data format by a pixel code in an array */
2497 static const struct sensor_datafmt *sensor_find_datafmt(
2498         enum v4l2_mbus_pixelcode code, const struct sensor_datafmt *fmt,
2499         int n)
2500 {
2501         int i;
2502         for (i = 0; i < n; i++)
2503                 if (fmt[i].code == code)
2504                         return fmt + i;
2505
2506         return NULL;
2507 }
2508
2509 static const struct sensor_datafmt sensor_colour_fmts[] = {
2510     {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
2511     {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG}     
2512 };
2513
2514 typedef struct sensor_info_priv_s
2515 {
2516     int whiteBalance;
2517     int brightness;
2518     int contrast;
2519     int saturation;
2520     int effect;
2521     int scene;
2522     int digitalzoom;
2523     int focus;
2524     int flash;
2525     int exposure;
2526         bool snap2preview;
2527         bool video2preview;
2528     unsigned char mirror;                                        /* HFLIP */
2529     unsigned char flip;                                          /* VFLIP */
2530     unsigned int winseqe_cur_addr;
2531     struct sensor_datafmt fmt;
2532
2533 } sensor_info_priv_t;
2534
2535 struct sensor
2536 {
2537     struct v4l2_subdev subdev;
2538     struct i2c_client *client;
2539     sensor_info_priv_t info_priv;
2540     int model;  /* V4L2_IDENT_OV* codes from v4l2-chip-ident.h */
2541 #if CONFIG_SENSOR_I2C_NOSCHED
2542         atomic_t tasklock_cnt;
2543 #endif
2544         struct rk29camera_platform_data *sensor_io_request;
2545     struct rk29camera_gpio_res *sensor_gpio_res;
2546 };
2547
2548 static struct sensor* to_sensor(const struct i2c_client *client)
2549 {
2550     return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2551 }
2552
2553 static int sensor_task_lock(struct i2c_client *client, int lock)
2554 {
2555 #if CONFIG_SENSOR_I2C_NOSCHED
2556         int cnt = 3;
2557     struct sensor *sensor = to_sensor(client);
2558
2559         if (lock) {
2560                 if (atomic_read(&sensor->tasklock_cnt) == 0) {
2561                         while ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt>0)) {
2562                                 SENSOR_TR("\n %s will obtain i2c in atomic, but i2c bus is locked! Wait...\n",SENSOR_NAME_STRING());
2563                                 msleep(35);
2564                                 cnt--;
2565                         }
2566                         if ((atomic_read(&client->adapter->bus_lock.count) < 1) && (cnt<=0)) {
2567                                 SENSOR_TR("\n %s obtain i2c fail in atomic!!\n",SENSOR_NAME_STRING());
2568                                 goto sensor_task_lock_err;
2569                         }
2570                         preempt_disable();
2571                 }
2572
2573                 atomic_add(1, &sensor->tasklock_cnt);
2574         } else {
2575                 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2576                         atomic_sub(1, &sensor->tasklock_cnt);
2577
2578                         if (atomic_read(&sensor->tasklock_cnt) == 0)
2579                                 preempt_enable();
2580                 }
2581         }
2582         return 0;
2583 sensor_task_lock_err:
2584         return -1;  
2585 #else
2586     return 0;
2587 #endif
2588
2589 }
2590 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
2591 {
2592     int err,cnt;
2593     u8 buf[2];
2594     struct i2c_msg msg[1];
2595
2596     buf[0] = reg & 0xFF;
2597     buf[1] = val;
2598
2599     msg->addr = client->addr;
2600     msg->flags = client->flags;
2601     msg->buf = buf;
2602     msg->len = sizeof(buf);
2603     msg->scl_rate = CONFIG_SENSOR_I2C_SPEED;                                        /* ddl@rock-chips.com : 100kHz */
2604     msg->read_type = 0;               /* fpga i2c:0==I2C_NORMAL : direct use number not enum for don't want include spi_fpga.h */
2605
2606     cnt = 3;
2607     err = -EAGAIN;
2608
2609     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2610         err = i2c_transfer(client->adapter, msg, 1);
2611
2612         if (err >= 0) {
2613             return 0;
2614         } else {
2615             SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2616             udelay(10);
2617         }
2618     }
2619
2620     return err;
2621 }
2622
2623 /* sensor register read */
2624 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
2625 {
2626     int err,cnt;
2627     u8 buf[1];
2628     struct i2c_msg msg[2];
2629
2630     buf[0] = reg ;//>> 8;
2631    // buf[1] = 0;
2632
2633     msg[0].addr = client->addr;
2634     msg[0].flags = client->flags;
2635     msg[0].buf = buf;
2636     msg[0].len = sizeof(buf);
2637     msg[0].scl_rate = CONFIG_SENSOR_I2C_SPEED;       /* ddl@rock-chips.com : 100kHz */
2638     msg[0].read_type = 2;//0x55;   /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2639
2640     msg[1].addr = client->addr;
2641     msg[1].flags = client->flags|I2C_M_RD;
2642     msg[1].buf = buf;
2643     msg[1].len = 1;
2644     msg[1].scl_rate = CONFIG_SENSOR_I2C_SPEED;                       /* ddl@rock-chips.com : 100kHz */
2645     msg[1].read_type = 2;                             /* fpga i2c:0==I2C_NO_STOP : direct use number not enum for don't want include spi_fpga.h */
2646
2647     cnt = 3;
2648     err = -EAGAIN;
2649     while ((cnt-->0) && (err < 0)) {                       /* ddl@rock-chips.com :  Transfer again if transent is failed   */
2650         err = i2c_transfer(client->adapter, msg, 2);
2651
2652         if (err >= 0) {
2653             *val = buf[0];
2654             return 0;
2655         } else {
2656                 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2657             udelay(10);
2658         }
2659     }
2660
2661     return err;
2662 }
2663
2664 /* write a array of registers  */
2665 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2666 {
2667     int err = 0, cnt;
2668     int i = 0;
2669 #if CONFIG_SENSOR_I2C_RDWRCHK    
2670         char valchk;
2671 #endif
2672
2673         cnt = 0;
2674         if (sensor_task_lock(client, 1) < 0)
2675                 goto sensor_write_array_end;
2676     while (regarray[i].reg != 0)
2677     {
2678         err = sensor_write(client, regarray[i].reg, regarray[i].val);
2679         if (err < 0)
2680         {
2681             if (cnt-- > 0) {
2682                             SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2683                                 i = 0;
2684                                 continue;
2685             } else {
2686                 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2687                 err = -EPERM;
2688                                 goto sensor_write_array_end;
2689             }
2690         } else {
2691         #if CONFIG_SENSOR_I2C_RDWRCHK
2692                         sensor_read(client, regarray[i].reg, &valchk);
2693                         if (valchk != regarray[i].val)
2694                                 SENSOR_TR("%s Reg:0x%x write(0x%x, 0x%x) fail\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2695                 #endif
2696         }
2697         i++;
2698     }
2699
2700 sensor_write_array_end:
2701         sensor_task_lock(client,0);
2702         return err;
2703 }
2704 #if CONFIG_SENSOR_I2C_RDWRCHK
2705 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2706 {
2707     int cnt;
2708     int i = 0;
2709         char valchk;
2710
2711         cnt = 0;
2712         valchk = 0;
2713     while (regarray[i].reg != 0)
2714     {
2715                 sensor_read(client, regarray[i].reg, &valchk);
2716                 if (valchk != regarray[i].val)
2717                         SENSOR_TR("%s Reg:0x%x read(0x%x, 0x%x) error\n",SENSOR_NAME_STRING(), regarray[i].reg, regarray[i].val, valchk);
2718
2719         i++;
2720     }
2721     return 0;
2722 }
2723 #endif
2724 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2725 {
2726         struct soc_camera_link *icl = to_soc_camera_link(icd);
2727         int ret = 0;
2728
2729     SENSOR_DG("%s %s  cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2730
2731         switch (cmd)
2732         {
2733                 case Sensor_PowerDown:
2734                 {
2735                         if (icl->powerdown) {
2736                                 ret = icl->powerdown(icd->pdev, on);
2737                                 if (ret == RK29_CAM_IO_SUCCESS) {
2738                                         if (on == 0) {
2739                                                 mdelay(2);
2740                                                 if (icl->reset)
2741                                                         icl->reset(icd->pdev);
2742                                         }
2743                                 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2744                                         ret = -ENODEV;
2745                                         goto sensor_power_end;
2746                                 }
2747                         }
2748                         break;
2749                 }
2750                 case Sensor_Flash:
2751                 {
2752                         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2753                 struct sensor *sensor = to_sensor(client);
2754
2755                         if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2756                                 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2757                         }
2758             break;
2759                 }
2760                 default:
2761                 {
2762                         SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2763                         break;
2764                 }
2765         }
2766 sensor_power_end:
2767         return ret;
2768 }
2769 static int sensor_init(struct v4l2_subdev *sd, u32 val)
2770 {
2771     struct i2c_client *client = sd->priv;
2772     struct soc_camera_device *icd = client->dev.platform_data;
2773     struct sensor *sensor = to_sensor(client);
2774         const struct v4l2_queryctrl *qctrl;
2775     const struct sensor_datafmt *fmt;
2776     char value;
2777     int ret,pid = 0;
2778
2779     SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2780
2781         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2782                 ret = -ENODEV;
2783                 goto sensor_INIT_ERR;
2784         }
2785
2786     /* soft reset */
2787         if (sensor_task_lock(client,1)<0)
2788                 goto sensor_INIT_ERR;
2789     ret = sensor_write(client, 0xff, 1);
2790     ret |= sensor_write(client, 0x12, 0x80);
2791     if (ret != 0)
2792     {
2793         SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2794         ret = -ENODEV;
2795                 goto sensor_INIT_ERR;
2796     }
2797
2798     mdelay(5);  //delay 5 microseconds
2799         /* check if it is an sensor sensor */
2800     ret = sensor_write(client, 0xff, 1);
2801     ret |= sensor_read(client, 0x0a, &value);
2802     if (ret != 0) {
2803         SENSOR_TR("read chip id high byte failed\n");
2804         ret = -ENODEV;
2805         goto sensor_INIT_ERR;
2806     }
2807
2808     pid = value << 8;
2809     ret = sensor_read(client, 0x0b, &value);
2810     if (ret != 0) {
2811         SENSOR_TR("read chip id low byte failed\n");
2812         ret = -ENODEV;
2813         goto sensor_INIT_ERR;
2814     }
2815
2816     pid |= (value & 0xff);
2817
2818     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2819     if ((pid == SENSOR_ID)||(pid == SENSOR_ID1)) {
2820         sensor->model = SENSOR_V4L2_IDENT;
2821     } else {
2822         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2823         ret = -ENODEV;
2824         goto sensor_INIT_ERR;
2825     }
2826
2827     ret = sensor_write_array(client, sensor_init_data);
2828     if (ret != 0)
2829     {
2830         SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2831         goto sensor_INIT_ERR;
2832     }
2833         sensor_task_lock(client,0);
2834     sensor->info_priv.winseqe_cur_addr  = (int)SENSOR_INIT_WINSEQADR;
2835     fmt = sensor_find_datafmt(SENSOR_INIT_PIXFMT,sensor_colour_fmts, ARRAY_SIZE(sensor_colour_fmts));
2836     if (!fmt) {
2837         SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2838         ret = -EINVAL;
2839         goto sensor_INIT_ERR;
2840     }
2841         sensor->info_priv.fmt = *fmt;
2842
2843     /* sensor sensor information for initialization  */
2844         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2845         if (qctrl)
2846         sensor->info_priv.whiteBalance = qctrl->default_value;
2847         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2848         if (qctrl)
2849         sensor->info_priv.brightness = qctrl->default_value;
2850         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2851         if (qctrl)
2852         sensor->info_priv.effect = qctrl->default_value;
2853         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2854         if (qctrl)
2855         sensor->info_priv.exposure = qctrl->default_value;
2856
2857         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2858         if (qctrl)
2859         sensor->info_priv.saturation = qctrl->default_value;
2860         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2861         if (qctrl)
2862         sensor->info_priv.contrast = qctrl->default_value;
2863         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2864         if (qctrl)
2865         sensor->info_priv.mirror = qctrl->default_value;
2866         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2867         if (qctrl)
2868         sensor->info_priv.flip = qctrl->default_value;
2869         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2870         if (qctrl)
2871         sensor->info_priv.scene = qctrl->default_value;
2872         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2873         if (qctrl)
2874         sensor->info_priv.digitalzoom = qctrl->default_value;
2875
2876     /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code  */
2877         #if CONFIG_SENSOR_Focus
2878     sensor_set_focus();
2879     qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2880         if (qctrl)
2881         sensor->info_priv.focus = qctrl->default_value;
2882         #endif
2883
2884         #if CONFIG_SENSOR_Flash
2885         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2886         if (qctrl)
2887         sensor->info_priv.flash = qctrl->default_value;
2888     #endif
2889
2890     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);
2891
2892     return 0;
2893 sensor_INIT_ERR:
2894         sensor_task_lock(client,0);
2895         sensor_deactivate(client);
2896     return ret;
2897 }
2898
2899 static int sensor_deactivate(struct i2c_client *client)
2900 {
2901         struct soc_camera_device *icd = client->dev.platform_data;
2902
2903         SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2904
2905         /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
2906 #if 0   
2907         sensor_task_lock(client, 1);
2908     sensor_write(client, 0x3000, reg_val&0xfc);
2909         sensor_write(client, 0x3001, 0x00);
2910         sensor_task_lock(client, 0);
2911 #endif
2912         sensor_ioctrl(icd, Sensor_PowerDown, 1);
2913         /* ddl@rock-chips.com : sensor config init width , because next open sensor quickly(soc_camera_open -> Try to configure with default parameters) */
2914         icd->user_width = SENSOR_INIT_WIDTH;
2915     icd->user_height = SENSOR_INIT_HEIGHT;
2916         msleep(100);
2917         return 0;
2918 }
2919
2920 static  struct reginfo sensor_power_down_sequence[]=
2921 {
2922     {0x00,0x00}
2923 };
2924 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2925 {
2926     int ret;
2927     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2928
2929     if (pm_msg.event == PM_EVENT_SUSPEND) {
2930         SENSOR_DG("\n %s Enter Suspend.. \n", SENSOR_NAME_STRING());
2931         ret = sensor_write_array(client, sensor_power_down_sequence) ;
2932         if (ret != 0) {
2933             SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2934             return ret;
2935         } else {
2936             ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2937             if (ret < 0) {
2938                             SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2939                 return -EINVAL;
2940             }
2941         }
2942     } else {
2943         SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2944         return -EINVAL;
2945     }
2946     return 0;
2947 }
2948
2949 static int sensor_resume(struct soc_camera_device *icd)
2950 {
2951         int ret;
2952
2953     ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2954     if (ret < 0) {
2955                 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2956         return -EINVAL;
2957     }
2958
2959         SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2960
2961     return 0;
2962
2963 }
2964
2965 static int sensor_set_bus_param(struct soc_camera_device *icd,
2966                                 unsigned long flags)
2967 {
2968
2969     return 0;
2970 }
2971
2972 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2973 {
2974     struct soc_camera_link *icl = to_soc_camera_link(icd);
2975     unsigned long flags = SENSOR_BUS_PARAM;
2976
2977     return soc_camera_apply_sensor_flags(icl, flags);
2978 }
2979
2980 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2981 {
2982     struct i2c_client *client = sd->priv;
2983     struct soc_camera_device *icd = client->dev.platform_data;
2984     struct sensor *sensor = to_sensor(client);
2985
2986     mf->width   = icd->user_width;
2987         mf->height      = icd->user_height;
2988         mf->code        = sensor->info_priv.fmt.code;
2989         mf->colorspace  = sensor->info_priv.fmt.colorspace;
2990         mf->field       = V4L2_FIELD_NONE;
2991
2992     return 0;
2993 }
2994 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2995 {
2996     bool ret = false;
2997
2998         if ((mf->width == 1024) && (mf->height == 768)) {
2999                 ret = true;
3000         } else if ((mf->width == 1280) && (mf->height == 1024)) {
3001                 ret = true;
3002         } else if ((mf->width == 1600) && (mf->height == 1200)) {
3003                 ret = true;
3004         } else if ((mf->width == 2048) && (mf->height == 1536)) {
3005                 ret = true;
3006         } else if ((mf->width == 2592) && (mf->height == 1944)) {
3007                 ret = true;
3008         }
3009
3010         if (ret == true)
3011                 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
3012         return ret;
3013 }
3014
3015 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3016 {
3017     bool ret = false;
3018
3019         if ((mf->width == 1280) && (mf->height == 720)) {
3020                 ret = true;
3021         } else if ((mf->width == 1920) && (mf->height == 1080)) {
3022                 ret = true;
3023         }
3024
3025         if (ret == true)
3026                 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
3027         return ret;
3028 }
3029 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3030 {
3031     struct i2c_client *client = sd->priv;
3032     struct sensor *sensor = to_sensor(client);
3033     const struct sensor_datafmt *fmt;
3034         const struct v4l2_queryctrl *qctrl;
3035         struct soc_camera_device *icd = client->dev.platform_data;
3036     struct reginfo *winseqe_set_addr=NULL;
3037     int ret=0, set_w,set_h;
3038
3039         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3040                                    ARRAY_SIZE(sensor_colour_fmts));
3041         if (!fmt) {
3042         ret = -EINVAL;
3043         goto sensor_s_fmt_end;
3044     }
3045
3046         if (sensor->info_priv.fmt.code != mf->code) {
3047                 switch (mf->code)
3048                 {
3049                         case V4L2_MBUS_FMT_YUYV8_2X8:
3050                         {
3051                                 winseqe_set_addr = sensor_ClrFmt_YUYV;
3052                                 break;
3053                         }
3054                         case V4L2_MBUS_FMT_UYVY8_2X8:
3055                         {
3056                                 winseqe_set_addr = sensor_ClrFmt_UYVY;
3057                                 break;
3058                         }
3059                         default:
3060                                 break;
3061                 }
3062                 if (winseqe_set_addr != NULL) {
3063             sensor_write_array(client, winseqe_set_addr);
3064                         sensor->info_priv.fmt.code = mf->code;
3065             sensor->info_priv.fmt.colorspace= mf->colorspace;            
3066                         SENSOR_DG("%s v4l2_mbus_code:%d set success!\n", SENSOR_NAME_STRING(),mf->code);
3067                 } else {
3068                         SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
3069                 }
3070         }
3071
3072     set_w = mf->width;
3073     set_h = mf->height;
3074
3075         if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
3076         {
3077                 winseqe_set_addr = sensor_qcif;
3078         set_w = 176;
3079         set_h = 144;
3080         }
3081         else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
3082     {
3083         winseqe_set_addr = sensor_qvga;
3084         set_w = 320;
3085         set_h = 240;
3086     }
3087     else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
3088     {
3089         winseqe_set_addr = sensor_cif;
3090         set_w = 352;
3091         set_h = 288;
3092     }
3093     else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
3094     {
3095         winseqe_set_addr = sensor_vga;
3096         set_w = 640;
3097         set_h = 480;
3098     }
3099     else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
3100     {
3101         winseqe_set_addr = sensor_svga;
3102         set_w = 800;
3103         set_h = 600;
3104     }
3105         else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
3106     {
3107         winseqe_set_addr = sensor_xga;
3108         set_w = 1024;
3109         set_h = 768;
3110     }
3111     else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
3112     {
3113         winseqe_set_addr = sensor_sxga;
3114         set_w = 1280;
3115         set_h = 1024;
3116     }
3117     else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
3118     {
3119         winseqe_set_addr = sensor_uxga;
3120         set_w = 1600;
3121         set_h = 1200;
3122     }
3123     else
3124     {
3125         winseqe_set_addr = SENSOR_INIT_WINSEQADR;               /* ddl@rock-chips.com : Sensor output smallest size if  isn't support app  */
3126         set_w = SENSOR_INIT_WIDTH;
3127         set_h = SENSOR_INIT_HEIGHT;
3128                 SENSOR_TR("\n %s..%s Format is Invalidate. pix->width = %d.. pix->height = %d\n",SENSOR_NAME_STRING(),__FUNCTION__,mf->width,mf->height);
3129     }
3130
3131     if ((int)winseqe_set_addr  != sensor->info_priv.winseqe_cur_addr) {
3132         #if CONFIG_SENSOR_Flash
3133         if (sensor_fmt_capturechk(sd,mf) == true) {      /* ddl@rock-chips.com : Capture */
3134             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3135                 sensor_ioctrl(icd, Sensor_Flash, Flash_On);
3136                 SENSOR_DG("%s flash on in capture!\n", SENSOR_NAME_STRING());
3137             }           
3138         } else {                                        /* ddl@rock-chips.com : Video */
3139             if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3140                 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3141                 SENSOR_DG("%s flash off in preivew!\n", SENSOR_NAME_STRING());
3142             }
3143         }
3144         #endif        
3145         ret |= sensor_write_array(client, winseqe_set_addr);
3146         if (ret != 0) {
3147             SENSOR_TR("%s set format capability failed\n", SENSOR_NAME_STRING());
3148             #if CONFIG_SENSOR_Flash
3149             if (sensor_fmt_capturechk(sd,mf) == true) {
3150                 if ((sensor->info_priv.flash == 1) || (sensor->info_priv.flash == 2)) {
3151                     sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3152                     SENSOR_TR("%s Capture format set fail, flash off !\n", SENSOR_NAME_STRING());
3153                 }
3154             }
3155             #endif
3156             goto sensor_s_fmt_end;
3157         }
3158
3159         sensor->info_priv.winseqe_cur_addr  = (int)winseqe_set_addr;
3160
3161                 if (sensor_fmt_capturechk(sd,mf) == true) {                                 /* ddl@rock-chips.com : Capture */
3162         #if CONFIG_SENSOR_Effect
3163                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3164                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3165         #endif
3166         #if CONFIG_SENSOR_WhiteBalance
3167                         if (sensor->info_priv.whiteBalance != 0) {
3168                                 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3169                                 sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3170                         }
3171         #endif
3172                         sensor->info_priv.snap2preview = true;
3173                 } else if (sensor_fmt_videochk(sd,mf) == true) {                        /* ddl@rock-chips.com : Video */
3174                 #if CONFIG_SENSOR_Effect
3175                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3176                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3177         #endif
3178         #if CONFIG_SENSOR_WhiteBalance
3179                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3180                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3181         #endif
3182                         sensor->info_priv.video2preview = true;
3183                 } else if ((sensor->info_priv.snap2preview == true) || (sensor->info_priv.video2preview == true)) {
3184                 #if CONFIG_SENSOR_Effect
3185                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
3186                         sensor_set_effect(icd, qctrl,sensor->info_priv.effect);
3187         #endif
3188         #if CONFIG_SENSOR_WhiteBalance    
3189                         qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
3190                         sensor_set_whiteBalance(icd, qctrl,sensor->info_priv.whiteBalance);
3191         #endif    
3192                         sensor->info_priv.video2preview = false;            
3193                         sensor->info_priv.snap2preview = false;
3194                 }
3195         SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
3196     }
3197     else
3198     {
3199         SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
3200     }
3201
3202         mf->width = set_w;
3203     mf->height = set_h;
3204
3205 sensor_s_fmt_end:
3206     return ret;
3207 }
3208
3209 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3210 {
3211     struct i2c_client *client = sd->priv;
3212     struct sensor *sensor = to_sensor(client);
3213     const struct sensor_datafmt *fmt;
3214     int ret = 0;
3215    
3216         fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3217                                    ARRAY_SIZE(sensor_colour_fmts));
3218         if (fmt == NULL) {
3219                 fmt = &sensor->info_priv.fmt;
3220         mf->code = fmt->code;
3221         } 
3222
3223     if (mf->height > SENSOR_MAX_HEIGHT)
3224         mf->height = SENSOR_MAX_HEIGHT;
3225     else if (mf->height < SENSOR_MIN_HEIGHT)
3226         mf->height = SENSOR_MIN_HEIGHT;
3227
3228     if (mf->width > SENSOR_MAX_WIDTH)
3229         mf->width = SENSOR_MAX_WIDTH;
3230     else if (mf->width < SENSOR_MIN_WIDTH)
3231         mf->width = SENSOR_MIN_WIDTH;
3232
3233     mf->colorspace = fmt->colorspace;
3234     
3235     return ret;
3236 }
3237  static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3238 {
3239     struct i2c_client *client = sd->priv;
3240
3241     if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3242         return -EINVAL;
3243
3244     if (id->match.addr != client->addr)
3245         return -ENODEV;
3246
3247     id->ident = SENSOR_V4L2_IDENT;      /* ddl@rock-chips.com :  Return OV2655  identifier */
3248     id->revision = 0;
3249
3250     return 0;
3251 }
3252 #if CONFIG_SENSOR_Brightness
3253 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3254 {
3255     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3256
3257     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3258     {
3259         if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3260         {
3261             if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3262             {
3263                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3264                 return -EINVAL;
3265             }
3266             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3267             return 0;
3268         }
3269     }
3270         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3271     return -EINVAL;
3272 }
3273 #endif
3274 #if CONFIG_SENSOR_Effect
3275 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3276 {
3277     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3278
3279     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3280     {
3281         if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3282         {
3283             if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3284             {
3285                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3286                 return -EINVAL;
3287             }
3288             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3289             return 0;
3290         }
3291     }
3292         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3293     return -EINVAL;
3294 }
3295 #endif
3296 #if CONFIG_SENSOR_Exposure
3297 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3298 {
3299     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3300
3301     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3302     {
3303         if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3304         {
3305             if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3306             {
3307                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3308                 return -EINVAL;
3309             }
3310             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3311             return 0;
3312         }
3313     }
3314         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3315     return -EINVAL;
3316 }
3317 #endif
3318 #if CONFIG_SENSOR_Saturation
3319 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3320 {
3321     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3322
3323     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3324     {
3325         if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3326         {
3327             if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3328             {
3329                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3330                 return -EINVAL;
3331             }
3332             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3333             return 0;
3334         }
3335     }
3336     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3337     return -EINVAL;
3338 }
3339 #endif
3340 #if CONFIG_SENSOR_Contrast
3341 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3342 {
3343     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3344
3345     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3346     {
3347         if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3348         {
3349             if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3350             {
3351                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3352                 return -EINVAL;
3353             }
3354             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3355             return 0;
3356         }
3357     }
3358     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3359     return -EINVAL;
3360 }
3361 #endif
3362 #if CONFIG_SENSOR_Mirror
3363 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3364 {
3365     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3366
3367     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3368     {
3369         if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3370         {
3371             if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3372             {
3373                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3374                 return -EINVAL;
3375             }
3376             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3377             return 0;
3378         }
3379     }
3380     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3381     return -EINVAL;
3382 }
3383 #endif
3384 #if CONFIG_SENSOR_Flip
3385 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3386 {
3387     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3388
3389     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3390     {
3391         if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3392         {
3393             if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3394             {
3395                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3396                 return -EINVAL;
3397             }
3398             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3399             return 0;
3400         }
3401     }
3402     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3403     return -EINVAL;
3404 }
3405 #endif
3406 #if CONFIG_SENSOR_Scene
3407 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3408 {
3409     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3410
3411     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3412     {
3413         if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3414         {
3415             if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3416             {
3417                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3418                 return -EINVAL;
3419             }
3420             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3421             return 0;
3422         }
3423     }
3424     SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3425     return -EINVAL;
3426 }
3427 #endif
3428 #if CONFIG_SENSOR_WhiteBalance
3429 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3430 {
3431     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3432
3433     if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3434     {
3435         if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3436         {
3437             if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3438             {
3439                 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3440                 return -EINVAL;
3441             }
3442             SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3443             return 0;
3444         }
3445     }
3446         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3447     return -EINVAL;
3448 }
3449 #endif
3450 #if CONFIG_SENSOR_DigitalZoom
3451 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
3452 {
3453     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3454     struct sensor *sensor = to_sensor(client);
3455         const struct v4l2_queryctrl *qctrl_info;
3456     int digitalzoom_cur, digitalzoom_total;
3457
3458         qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3459         if (qctrl_info)
3460                 return -EINVAL;
3461
3462     digitalzoom_cur = sensor->info_priv.digitalzoom;
3463     digitalzoom_total = qctrl_info->maximum;
3464
3465     if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3466     {
3467         SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3468         return -EINVAL;
3469     }
3470
3471     if  ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3472     {
3473         SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3474         return -EINVAL;
3475     }
3476
3477     if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3478     {
3479         *value = digitalzoom_total - digitalzoom_cur;
3480     }
3481
3482     if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3483     {
3484         *value = 0 - digitalzoom_cur;
3485     }
3486
3487     digitalzoom_cur += *value;
3488
3489     if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3490     {
3491         if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3492         {
3493             SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3494             return -EINVAL;
3495         }
3496         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3497         return 0;
3498     }
3499
3500     return -EINVAL;
3501 }
3502 #endif
3503 #if CONFIG_SENSOR_Flash
3504 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3505 {    
3506     if ((value >= qctrl->minimum) && (value <= qctrl->maximum)) {
3507         if (value == 3) {       /* ddl@rock-chips.com: torch */
3508             sensor_ioctrl(icd, Sensor_Flash, Flash_Torch);   /* Flash On */
3509         } else {
3510             sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3511         }
3512         SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3513         return 0;
3514     }
3515     
3516         SENSOR_TR("\n %s..%s valure = %d is invalidate..    \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3517     return -EINVAL;
3518 }
3519 #endif
3520
3521 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3522 {
3523     struct i2c_client *client = sd->priv;
3524     struct sensor *sensor = to_sensor(client);
3525     const struct v4l2_queryctrl *qctrl;
3526
3527     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3528
3529     if (!qctrl)
3530     {
3531         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3532         return -EINVAL;
3533     }
3534
3535     switch (ctrl->id)
3536     {
3537         case V4L2_CID_BRIGHTNESS:
3538             {
3539                 ctrl->value = sensor->info_priv.brightness;
3540                 break;
3541             }
3542         case V4L2_CID_SATURATION:
3543             {
3544                 ctrl->value = sensor->info_priv.saturation;
3545                 break;
3546             }
3547         case V4L2_CID_CONTRAST:
3548             {
3549                 ctrl->value = sensor->info_priv.contrast;
3550                 break;
3551             }
3552         case V4L2_CID_DO_WHITE_BALANCE:
3553             {
3554                 ctrl->value = sensor->info_priv.whiteBalance;
3555                 break;
3556             }
3557         case V4L2_CID_EXPOSURE:
3558             {
3559                 ctrl->value = sensor->info_priv.exposure;
3560                 break;
3561             }
3562         case V4L2_CID_HFLIP:
3563             {
3564                 ctrl->value = sensor->info_priv.mirror;
3565                 break;
3566             }
3567         case V4L2_CID_VFLIP:
3568             {
3569                 ctrl->value = sensor->info_priv.flip;
3570                 break;
3571             }
3572         default :
3573                 break;
3574     }
3575     return 0;
3576 }
3577
3578
3579
3580 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3581 {
3582     struct i2c_client *client = sd->priv;
3583     struct sensor *sensor = to_sensor(client);
3584     struct soc_camera_device *icd = client->dev.platform_data;
3585     const struct v4l2_queryctrl *qctrl;
3586
3587
3588     qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3589
3590     if (!qctrl)
3591     {
3592         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3593         return -EINVAL;
3594     }
3595
3596     switch (ctrl->id)
3597     {
3598 #if CONFIG_SENSOR_Brightness
3599         case V4L2_CID_BRIGHTNESS:
3600             {
3601                 if (ctrl->value != sensor->info_priv.brightness)
3602                 {
3603                     if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3604                     {
3605                         return -EINVAL;
3606                     }
3607                     sensor->info_priv.brightness = ctrl->value;
3608                 }
3609                 break;
3610             }
3611 #endif
3612 #if CONFIG_SENSOR_Exposure
3613         case V4L2_CID_EXPOSURE:
3614             {
3615                 if (ctrl->value != sensor->info_priv.exposure)
3616                 {
3617                     if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3618                     {
3619                         return -EINVAL;
3620                     }
3621                     sensor->info_priv.exposure = ctrl->value;
3622                 }
3623                 break;
3624             }
3625 #endif
3626 #if CONFIG_SENSOR_Saturation
3627         case V4L2_CID_SATURATION:
3628             {
3629                 if (ctrl->value != sensor->info_priv.saturation)
3630                 {
3631                     if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3632                     {
3633                         return -EINVAL;
3634                     }
3635                     sensor->info_priv.saturation = ctrl->value;
3636                 }
3637                 break;
3638             }
3639 #endif
3640 #if CONFIG_SENSOR_Contrast
3641         case V4L2_CID_CONTRAST:
3642             {
3643                 if (ctrl->value != sensor->info_priv.contrast)
3644                 {
3645                     if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3646                     {
3647                         return -EINVAL;
3648                     }
3649                     sensor->info_priv.contrast = ctrl->value;
3650                 }
3651                 break;
3652             }
3653 #endif
3654 #if CONFIG_SENSOR_WhiteBalance
3655         case V4L2_CID_DO_WHITE_BALANCE:
3656             {
3657                 if (ctrl->value != sensor->info_priv.whiteBalance)
3658                 {
3659                     if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3660                     {
3661                         return -EINVAL;
3662                     }
3663                     sensor->info_priv.whiteBalance = ctrl->value;
3664                 }
3665                 break;
3666             }
3667 #endif
3668 #if CONFIG_SENSOR_Mirror
3669         case V4L2_CID_HFLIP:
3670             {
3671                 if (ctrl->value != sensor->info_priv.mirror)
3672                 {
3673                     if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3674                         return -EINVAL;
3675                     sensor->info_priv.mirror = ctrl->value;
3676                 }
3677                 break;
3678             }
3679 #endif
3680 #if CONFIG_SENSOR_Flip
3681         case V4L2_CID_VFLIP:
3682             {
3683                 if (ctrl->value != sensor->info_priv.flip)
3684                 {
3685                     if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3686                         return -EINVAL;
3687                     sensor->info_priv.flip = ctrl->value;
3688                 }
3689                 break;
3690             }
3691 #endif
3692         default:
3693             break;
3694     }
3695
3696     return 0;
3697 }
3698 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
3699 {
3700     const struct v4l2_queryctrl *qctrl;
3701     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3702     struct sensor *sensor = to_sensor(client);
3703
3704     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3705
3706     if (!qctrl)
3707     {
3708         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3709         return -EINVAL;
3710     }
3711
3712     switch (ext_ctrl->id)
3713     {
3714         case V4L2_CID_SCENE:
3715             {
3716                 ext_ctrl->value = sensor->info_priv.scene;
3717                 break;
3718             }
3719         case V4L2_CID_EFFECT:
3720             {
3721                 ext_ctrl->value = sensor->info_priv.effect;
3722                 break;
3723             }
3724         case V4L2_CID_ZOOM_ABSOLUTE:
3725             {
3726                 ext_ctrl->value = sensor->info_priv.digitalzoom;
3727                 break;
3728             }
3729         case V4L2_CID_ZOOM_RELATIVE:
3730             {
3731                 return -EINVAL;
3732             }
3733         case V4L2_CID_FOCUS_ABSOLUTE:
3734             {
3735                 ext_ctrl->value = sensor->info_priv.focus;
3736                 break;
3737             }
3738         case V4L2_CID_FOCUS_RELATIVE:
3739             {
3740                 return -EINVAL;
3741             }
3742         case V4L2_CID_FLASH:
3743             {
3744                 ext_ctrl->value = sensor->info_priv.flash;
3745                 break;
3746             }
3747         default :
3748             break;
3749     }
3750     return 0;
3751 }
3752 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
3753 {
3754     const struct v4l2_queryctrl *qctrl;
3755     struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3756     struct sensor *sensor = to_sensor(client);
3757     int val_offset;
3758
3759     qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3760
3761     if (!qctrl)
3762     {
3763         SENSOR_TR("\n %s ioctrl id = %d  is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3764         return -EINVAL;
3765     }
3766
3767         val_offset = 0;
3768     switch (ext_ctrl->id)
3769     {
3770 #if CONFIG_SENSOR_Scene
3771         case V4L2_CID_SCENE:
3772             {
3773                 if (ext_ctrl->value != sensor->info_priv.scene)
3774                 {
3775                     if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3776                         return -EINVAL;
3777                     sensor->info_priv.scene = ext_ctrl->value;
3778                 }
3779                 break;
3780             }
3781 #endif
3782 #if CONFIG_SENSOR_Effect
3783         case V4L2_CID_EFFECT:
3784             {
3785                 if (ext_ctrl->value != sensor->info_priv.effect)
3786                 {
3787                     if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3788                         return -EINVAL;
3789                     sensor->info_priv.effect= ext_ctrl->value;
3790                 }
3791                 break;
3792             }
3793 #endif
3794 #if CONFIG_SENSOR_DigitalZoom
3795         case V4L2_CID_ZOOM_ABSOLUTE:
3796             {
3797                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3798                     return -EINVAL;
3799
3800                 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3801                 {
3802                     val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3803
3804                     if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3805                         return -EINVAL;
3806                     sensor->info_priv.digitalzoom += val_offset;
3807
3808                     SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(),  sensor->info_priv.digitalzoom);
3809                 }
3810
3811                 break;
3812             }
3813         case V4L2_CID_ZOOM_RELATIVE:
3814             {
3815                 if (ext_ctrl->value)
3816                 {
3817                     if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3818                         return -EINVAL;
3819                     sensor->info_priv.digitalzoom += ext_ctrl->value;
3820
3821                     SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3822                 }
3823                 break;
3824             }
3825 #endif
3826 #if CONFIG_SENSOR_Focus
3827         case V4L2_CID_FOCUS_ABSOLUTE:
3828             {
3829                 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3830                     return -EINVAL;
3831
3832                 if (ext_ctrl->value != sensor->info_priv.focus)
3833                 {
3834                     val_offset = ext_ctrl->value -sensor->info_priv.focus;
3835
3836                     sensor->info_priv.focus += val_offset;
3837                 }
3838
3839                 break;
3840             }
3841         case V4L2_CID_FOCUS_RELATIVE:
3842             {
3843                 if (ext_ctrl->value)
3844                 {
3845                     sensor->info_priv.focus += ext_ctrl->value;
3846
3847                     SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3848                 }
3849                 break;
3850             }
3851 #endif
3852 #if CONFIG_SENSOR_Flash
3853         case V4L2_CID_FLASH:
3854             {
3855                 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3856                     return -EINVAL;
3857                 sensor->info_priv.flash = ext_ctrl->value;
3858
3859                 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3860                 break;
3861             }
3862 #endif
3863         default:
3864             break;
3865     }
3866
3867     return 0;
3868 }
3869
3870 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3871 {
3872     struct i2c_client *client = sd->priv;
3873     struct soc_camera_device *icd = client->dev.platform_data;
3874     int i, error_cnt=0, error_idx=-1;
3875
3876
3877     for (i=0; i<ext_ctrl->count; i++) {
3878         if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3879             error_cnt++;
3880             error_idx = i;
3881         }
3882     }
3883
3884     if (error_cnt > 1)
3885         error_idx = ext_ctrl->count;
3886
3887     if (error_idx != -1) {
3888         ext_ctrl->error_idx = error_idx;
3889         return -EINVAL;
3890     } else {
3891         return 0;
3892     }
3893 }
3894
3895 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
3896 {
3897     struct i2c_client *client = sd->priv;
3898     struct soc_camera_device *icd = client->dev.platform_data;
3899     int i, error_cnt=0, error_idx=-1;
3900
3901
3902     for (i=0; i<ext_ctrl->count; i++) {
3903         if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3904             error_cnt++;
3905             error_idx = i;
3906         }
3907     }
3908
3909     if (error_cnt > 1)
3910         error_idx = ext_ctrl->count;
3911
3912     if (error_idx != -1) {
3913         ext_ctrl->error_idx = error_idx;
3914         return -EINVAL;
3915     } else {
3916         return 0;
3917     }
3918 }
3919
3920 /* Interface active, can use i2c. If it fails, it can indeed mean, that
3921  * this wasn't our capture interface, so, we wait for the right one */
3922 static int sensor_video_probe(struct soc_camera_device *icd,
3923                                struct i2c_client *client)
3924 {
3925     char value;
3926     int ret,pid = 0;
3927     struct sensor *sensor = to_sensor(client);
3928
3929     /* We must have a parent by now. And it cannot be a wrong one.
3930      * So this entire test is completely redundant. */
3931     if (!icd->dev.parent ||
3932             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
3933                 return -ENODEV;
3934
3935         if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3936                 ret = -ENODEV;
3937                 goto sensor_video_probe_err;
3938         }
3939
3940     /* soft reset */
3941     ret = sensor_write(client, 0xff, 0x1);
3942     if (ret != 0) {
3943         SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3944         ret = -ENODEV;
3945                 goto sensor_video_probe_err;
3946     }
3947     mdelay(5);          //delay 5 microseconds
3948
3949     /* check if it is an sensor sensor */
3950     ret = sensor_read(client, 0x0a, &value);
3951     if (ret != 0) {
3952         SENSOR_TR("read chip id high byte failed\n");
3953         ret = -ENODEV;
3954         goto sensor_video_probe_err;
3955     }
3956     pid = value << 8;
3957
3958     ret = sensor_read(client, 0x0b, &value);
3959     if (ret != 0) {
3960         SENSOR_TR("read chip id low byte failed\n");
3961         ret = -ENODEV;
3962         goto sensor_video_probe_err;
3963     }
3964
3965     pid |= (value & 0xff);
3966     SENSOR_DG("\n %s  pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3967
3968     if ((pid == SENSOR_ID)||(pid == SENSOR_ID1)) {
3969         sensor->model = SENSOR_V4L2_IDENT;
3970     } else {
3971         SENSOR_TR("error: %s mismatched   pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3972         ret = -ENODEV;
3973         goto sensor_video_probe_err;
3974     }
3975
3976     return 0;
3977
3978 sensor_video_probe_err:
3979
3980     return ret;
3981 }
3982
3983 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3984 {
3985         struct i2c_client *client = sd->priv;
3986     struct soc_camera_device *icd = client->dev.platform_data;  
3987     struct sensor *sensor = to_sensor(client);
3988     int ret = 0;
3989     
3990         SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3991         switch (cmd)
3992         {
3993                 case RK29_CAM_SUBDEV_DEACTIVATE:
3994                 {
3995                         sensor_deactivate(client);
3996                         break;
3997                 }
3998
3999                 case RK29_CAM_SUBDEV_IOREQUEST:
4000                 {
4001                         sensor->sensor_io_request = (struct rk29camera_platform_data*)arg;           
4002             if (sensor->sensor_io_request != NULL) { 
4003                 if (sensor->sensor_io_request->gpio_res[0].dev_name && 
4004                     (strcmp(sensor->sensor_io_request->gpio_res[0].dev_name, dev_name(icd->pdev)) == 0)) {
4005                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[0];
4006                 } else if (sensor->sensor_io_request->gpio_res[1].dev_name && 
4007                     (strcmp(sensor->sensor_io_request->gpio_res[1].dev_name, dev_name(icd->pdev)) == 0)) {
4008                     sensor->sensor_gpio_res = (struct rk29camera_gpio_res*)&sensor->sensor_io_request->gpio_res[1];
4009                 }
4010             } else {
4011                 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4012                 ret = -EINVAL;
4013                 goto sensor_ioctl_end;
4014             }
4015             /* ddl@rock-chips.com : if gpio_flash havn't been set in board-xxx.c, sensor driver must notify is not support flash control 
4016                for this project */
4017             #if CONFIG_SENSOR_Flash     
4018                 if (sensor->sensor_gpio_res) { 
4019                 if (sensor->sensor_gpio_res->gpio_flash == INVALID_GPIO) {
4020                     for (i = 0; i < icd->ops->num_controls; i++) {
4021                                 if (V4L2_CID_FLASH == icd->ops->controls[i].id) {
4022                                         memset((char*)&icd->ops->controls[i],0x00,sizeof(struct v4l2_queryctrl));                                       
4023                                 }
4024                     }
4025                     sensor->info_priv.flash = 0xff;
4026                     SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4027                 }
4028                 }
4029             #endif
4030                         break;
4031                 }
4032                 default:
4033                 {
4034                         SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4035                         break;
4036                 }
4037         }
4038
4039 sensor_ioctl_end:
4040         return ret;
4041
4042 }
4043 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4044                             enum v4l2_mbus_pixelcode *code)
4045 {
4046         if (index >= ARRAY_SIZE(sensor_colour_fmts))
4047                 return -EINVAL;
4048
4049         *code = sensor_colour_fmts[index].code;
4050         return 0;
4051 }
4052 static struct v4l2_subdev_core_ops sensor_subdev_core_ops = {
4053         .init           = sensor_init,
4054         .g_ctrl         = sensor_g_control,
4055         .s_ctrl         = sensor_s_control,
4056         .g_ext_ctrls          = sensor_g_ext_controls,
4057         .s_ext_ctrls          = sensor_s_ext_controls,
4058         .g_chip_ident   = sensor_g_chip_ident,
4059         .ioctl = sensor_ioctl,
4060 };
4061
4062 static struct v4l2_subdev_video_ops sensor_subdev_video_ops = {
4063         .s_mbus_fmt     = sensor_s_fmt,
4064         .g_mbus_fmt     = sensor_g_fmt,
4065         .try_mbus_fmt   = sensor_try_fmt,
4066         .enum_mbus_fmt  = sensor_enum_fmt,
4067 };
4068
4069 static struct v4l2_subdev_ops sensor_subdev_ops = {
4070         .core   = &sensor_subdev_core_ops,
4071         .video = &sensor_subdev_video_ops,
4072 };
4073
4074 static int sensor_probe(struct i2c_client *client,
4075                          const struct i2c_device_id *did)
4076 {
4077     struct sensor *sensor;
4078     struct soc_camera_device *icd = client->dev.platform_data;
4079     struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
4080     struct soc_camera_link *icl;
4081     int ret;
4082
4083     SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4084     if (!icd) {
4085         dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4086         return -EINVAL;
4087     }
4088
4089     icl = to_soc_camera_link(icd);
4090     if (!icl) {
4091         dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4092         return -EINVAL;
4093     }
4094
4095     if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4096         dev_warn(&adapter->dev,
4097                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4098         return -EIO;
4099     }
4100
4101     sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4102     if (!sensor)
4103         return -ENOMEM;
4104
4105     v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
4106
4107     /* Second stage probe - when a capture adapter is there */
4108     icd->ops            = &sensor_ops;
4109     sensor->info_priv.fmt = sensor_colour_fmts[0];
4110         #if CONFIG_SENSOR_I2C_NOSCHED
4111         atomic_set(&sensor->tasklock_cnt,0);
4112         #endif
4113
4114     ret = sensor_video_probe(icd, client);
4115     if (ret < 0) {
4116         icd->ops = NULL;
4117         i2c_set_clientdata(client, NULL);
4118         kfree(sensor);
4119                 sensor = NULL;
4120     }
4121     SENSOR_DG("\n%s..%s..%d  ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4122     return ret;
4123 }
4124
4125 static int sensor_remove(struct i2c_client *client)
4126 {
4127     struct sensor *sensor = to_sensor(client);
4128     struct soc_camera_device *icd = client->dev.platform_data;
4129
4130     icd->ops = NULL;
4131     i2c_set_clientdata(client, NULL);
4132     client->driver = NULL;
4133     kfree(sensor);
4134         sensor = NULL;
4135     return 0;
4136 }
4137
4138 static const struct i2c_device_id sensor_id[] = {
4139         {SENSOR_NAME_STRING(), 0 },
4140         { }
4141 };
4142 MODULE_DEVICE_TABLE(i2c, sensor_id);
4143
4144 static struct i2c_driver sensor_i2c_driver = {
4145         .driver = {
4146                 .name = SENSOR_NAME_STRING(),
4147         },
4148         .probe          = sensor_probe,
4149         .remove         = sensor_remove,
4150         .id_table       = sensor_id,
4151 };
4152
4153 static int __init sensor_mod_init(void)
4154 {
4155     SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4156     return i2c_add_driver(&sensor_i2c_driver);
4157 }
4158
4159 static void __exit sensor_mod_exit(void)
4160 {
4161     i2c_del_driver(&sensor_i2c_driver);
4162 }
4163
4164 device_initcall_sync(sensor_mod_init);
4165 module_exit(sensor_mod_exit);
4166
4167 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4168 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4169 MODULE_LICENSE("GPL");
4170
4171
4172 >>>>>>> parent of 15f7fab... temp revert rk change