5 * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
7 * Based on ov772x, ov9640 drivers and previous non merged implementations.
9 * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
10 * Copyright (C) 2006, OmniVision
12 o* Driver for MT9M001 CMOS Image Sensor from Micron
14 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
15 >>>>>>> parent of 15f7fab... temp revert rk change
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.
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>
34 #define VAL_SET(x, mask, rshift, lshift) \
35 ((((x) >> rshift) & mask) << lshift)
38 * register offset for BANK_SEL == BANK_SEL_DSP
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 */
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)
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)
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
160 * register offset for BANK_SEL == BANK_SEL_SENS
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).
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 */
264 #define MANUFACTURER_ID 0x7FA2
265 #define PID_OV2640 0x2642
266 #define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
276 /* Supported resolutions */
299 struct ov2640_win_size {
301 enum ov2640_width width;
302 enum ov2640_height height;
303 const struct regval_list *regs;
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;
321 #define ENDMARKER { 0xff, 0xff }
323 static const struct regval_list ov2640_init_regs[] = {
324 { BANK_SEL, BANK_SEL_DSP },
327 { BANK_SEL, BANK_SEL_SENS },
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},
357 { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
361 { COM3, 0x38 | COM3_BAND_AUTO },
367 { HISTO_HIGH, 0x80 },
378 { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
382 { BANK_SEL, BANK_SEL_DSP },
384 { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
386 { RESET, RESET_JPEG | RESET_DVP },
392 { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
396 { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
493 { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
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.
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) },
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 }, \
528 static const struct regval_list ov2640_qcif_regs[] = {
529 PER_SIZE_REG_SEQ(W_QCIF, H_QCIF, 3, 3, 4),
533 static const struct regval_list ov2640_qvga_regs[] = {
534 PER_SIZE_REG_SEQ(W_QVGA, H_QVGA, 2, 2, 4),
538 static const struct regval_list ov2640_cif_regs[] = {
539 PER_SIZE_REG_SEQ(W_CIF, H_CIF, 2, 2, 8),
543 static const struct regval_list ov2640_vga_regs[] = {
544 PER_SIZE_REG_SEQ(W_VGA, H_VGA, 0, 0, 2),
548 static const struct regval_list ov2640_svga_regs[] = {
549 PER_SIZE_REG_SEQ(W_SVGA, H_SVGA, 1, 1, 2),
553 static const struct regval_list ov2640_xga_regs[] = {
554 PER_SIZE_REG_SEQ(W_XGA, H_XGA, 0, 0, 2),
559 static const struct regval_list ov2640_sxga_regs[] = {
560 PER_SIZE_REG_SEQ(W_SXGA, H_SXGA, 0, 0, 2),
562 { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
566 static const struct regval_list ov2640_uxga_regs[] = {
567 PER_SIZE_REG_SEQ(W_UXGA, H_UXGA, 0, 0, 0),
569 { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
573 #define OV2640_SIZE(n, w, h, r) \
574 {.name = n, .width = w , .height = h, .regs = r }
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),
588 * Register settings for pixel formats
590 static const struct regval_list ov2640_format_change_preamble_regs[] = {
591 { BANK_SEL, BANK_SEL_DSP },
592 { R_BYPASS, R_BYPASS_USE_DSP },
596 static const struct regval_list ov2640_yuv422_regs[] = {
597 { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
602 { R_BYPASS, R_BYPASS_USE_DSP },
606 static const struct regval_list ov2640_rgb565_regs[] = {
607 { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
610 { R_BYPASS, R_BYPASS_USE_DSP },
614 static enum v4l2_mbus_pixelcode ov2640_codes[] = {
615 V4L2_MBUS_FMT_UYVY8_2X8,
616 V4L2_MBUS_FMT_RGB565_2X8_LE,
622 static const struct v4l2_queryctrl ov2640_controls[] = {
624 .id = V4L2_CID_VFLIP,
625 .type = V4L2_CTRL_TYPE_BOOLEAN,
626 .name = "Flip Vertically",
632 .id = V4L2_CID_HFLIP,
633 .type = V4L2_CTRL_TYPE_BOOLEAN,
634 .name = "Flip Horizontally",
645 static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
647 return container_of(i2c_get_clientdata(client), struct ov2640_priv,
651 static int ov2640_write_array(struct i2c_client *client,
652 const struct regval_list *vals)
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);
669 static int ov2640_mask_set(struct i2c_client *client,
670 u8 reg, u8 mask, u8 set)
672 s32 val = i2c_smbus_read_byte_data(client, reg);
679 dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
681 return i2c_smbus_write_byte_data(client, reg, val);
684 static int ov2640_reset(struct i2c_client *client)
687 const struct regval_list reset_seq[] = {
688 {BANK_SEL, BANK_SEL_SENS},
693 ret = ov2640_write_array(client, reset_seq);
699 dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
704 * soc_camera_ops functions
706 static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
711 static int ov2640_set_bus_param(struct soc_camera_device *icd,
714 struct soc_camera_link *icl = to_soc_camera_link(icd);
715 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
717 /* Only one width bit may be set */
718 if (!is_power_of_2(width_flag))
721 if (icl->set_bus_param)
722 return icl->set_bus_param(icl, width_flag);
725 * Without board specific bus width settings we support only the
726 * sensors native bus width witch are tested working
728 if (width_flag & (SOCAM_DATAWIDTH_10 | SOCAM_DATAWIDTH_8))
734 static unsigned long ov2640_query_bus_param(struct soc_camera_device *icd)
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;
741 if (icl->query_bus_param)
742 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
744 flags |= SOCAM_DATAWIDTH_10;
746 return soc_camera_apply_sensor_flags(icl, flags);
749 static int ov2640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
751 struct i2c_client *client = v4l2_get_subdevdata(sd);
752 struct ov2640_priv *priv = to_ov2640(client);
756 ctrl->value = priv->flag_vflip;
759 ctrl->value = priv->flag_hflip;
765 static int ov2640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
767 struct i2c_client *client = v4l2_get_subdevdata(sd);
768 struct ov2640_priv *priv = to_ov2640(client);
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);
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);
788 static int ov2640_g_chip_ident(struct v4l2_subdev *sd,
789 struct v4l2_dbg_chip_ident *id)
791 struct i2c_client *client = v4l2_get_subdevdata(sd);
792 struct ov2640_priv *priv = to_ov2640(client);
794 id->ident = priv->model;
800 #ifdef CONFIG_VIDEO_ADV_DEBUG
801 static int ov2640_g_register(struct v4l2_subdev *sd,
802 struct v4l2_dbg_register *reg)
804 struct i2c_client *client = v4l2_get_subdevdata(sd);
811 ret = i2c_smbus_read_byte_data(client, reg->reg);
820 static int ov2640_s_register(struct v4l2_subdev *sd,
821 struct v4l2_dbg_register *reg)
823 struct i2c_client *client = v4l2_get_subdevdata(sd);
825 if (reg->reg > 0xff ||
829 return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
833 /* Select the nearest higher resolution for capture */
834 static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
836 int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
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];
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];
852 static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
853 enum v4l2_mbus_pixelcode code)
855 struct ov2640_priv *priv = to_ov2640(client);
856 const struct regval_list *selected_cfmt_regs;
860 priv->win = ov2640_select_win(width, height);
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;
870 case V4L2_MBUS_FMT_UYVY8_2X8:
871 dev_dbg(&client->dev, "%s: Selected cfmt YUV422", __func__);
872 selected_cfmt_regs = ov2640_yuv422_regs;
876 ov2640_reset(client);
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);
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);
891 ret = ov2640_write_array(client, priv->win->regs);
896 dev_dbg(&client->dev, "%s: Set cfmt", __func__);
897 ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
902 ret = ov2640_write_array(client, selected_cfmt_regs);
906 priv->cfmt_code = code;
907 *width = priv->win->width;
908 *height = priv->win->height;
913 dev_err(&client->dev, "%s: Error %d", __func__, ret);
914 ov2640_reset(client);
920 static int ov2640_g_fmt(struct v4l2_subdev *sd,
921 struct v4l2_mbus_framefmt *mf)
923 struct i2c_client *client = v4l2_get_subdevdata(sd);
924 struct ov2640_priv *priv = to_ov2640(client);
927 u32 width = W_SVGA, height = H_SVGA;
928 int ret = ov2640_set_params(client, &width, &height,
929 V4L2_MBUS_FMT_UYVY8_2X8);
934 mf->width = priv->win->width;
935 mf->height = priv->win->height;
936 mf->code = priv->cfmt_code;
939 case V4L2_MBUS_FMT_RGB565_2X8_LE:
940 mf->colorspace = V4L2_COLORSPACE_SRGB;
943 case V4L2_MBUS_FMT_UYVY8_2X8:
944 mf->colorspace = V4L2_COLORSPACE_JPEG;
946 mf->field = V4L2_FIELD_NONE;
951 static int ov2640_s_fmt(struct v4l2_subdev *sd,
952 struct v4l2_mbus_framefmt *mf)
954 struct i2c_client *client = v4l2_get_subdevdata(sd);
959 case V4L2_MBUS_FMT_RGB565_2X8_LE:
960 mf->colorspace = V4L2_COLORSPACE_SRGB;
963 mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
964 case V4L2_MBUS_FMT_UYVY8_2X8:
965 mf->colorspace = V4L2_COLORSPACE_JPEG;
968 ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
973 static int ov2640_try_fmt(struct v4l2_subdev *sd,
974 struct v4l2_mbus_framefmt *mf)
976 const struct ov2640_win_size *win;
979 * select suitable win
981 win = ov2640_select_win(&mf->width, &mf->height);
983 mf->field = V4L2_FIELD_NONE;
986 case V4L2_MBUS_FMT_RGB565_2X8_LE:
987 mf->colorspace = V4L2_COLORSPACE_SRGB;
990 mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
991 case V4L2_MBUS_FMT_UYVY8_2X8:
992 mf->colorspace = V4L2_COLORSPACE_JPEG;
998 static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
999 enum v4l2_mbus_pixelcode *code)
1001 if (index >= ARRAY_SIZE(ov2640_codes))
1004 *code = ov2640_codes[index];
1008 static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
1012 a->c.width = W_UXGA;
1013 a->c.height = H_UXGA;
1014 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1019 static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
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;
1033 static int ov2640_video_probe(struct soc_camera_device *icd,
1034 struct i2c_client *client)
1036 struct ov2640_priv *priv = to_ov2640(client);
1037 u8 pid, ver, midh, midl;
1038 const char *devname;
1042 * we must have a parent by now. And it cannot be a wrong one.
1043 * So this entire test is completely redundant.
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");
1053 * check and show product ID and manufacturer ID
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);
1061 switch (VERSION(pid, ver)) {
1064 priv->model = V4L2_IDENT_OV2640;
1067 dev_err(&client->dev,
1068 "Product ID error %x:%x\n", pid, ver);
1073 dev_info(&client->dev,
1074 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
1075 devname, pid, ver, midh, midl);
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),
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,
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,
1110 static struct v4l2_subdev_ops ov2640_subdev_ops = {
1111 .core = &ov2640_subdev_core_ops,
1112 .video = &ov2640_subdev_video_ops,
1116 * i2c_driver functions
1118 static int ov2640_probe(struct i2c_client *client,
1119 const struct i2c_device_id *did)
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;
1128 dev_err(&adapter->dev, "OV2640: missing soc-camera data!\n");
1132 icl = to_soc_camera_link(icd);
1134 dev_err(&adapter->dev,
1135 "OV2640: Missing platform_data for driver\n");
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");
1145 priv = kzalloc(sizeof(struct ov2640_priv), GFP_KERNEL);
1147 dev_err(&adapter->dev,
1148 "Failed to allocate memory for private data!\n");
1152 priv->info = icl->priv;
1154 v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
1156 icd->ops = &ov2640_ops;
1158 ret = ov2640_video_probe(icd, client);
1163 dev_info(&adapter->dev, "OV2640 Probed\n");
1169 static int ov2640_remove(struct i2c_client *client)
1171 struct ov2640_priv *priv = to_ov2640(client);
1172 struct soc_camera_device *icd = client->dev.platform_data;
1179 static const struct i2c_device_id ov2640_id[] = {
1183 MODULE_DEVICE_TABLE(i2c, ov2640_id);
1185 static struct i2c_driver ov2640_i2c_driver = {
1189 .probe = ov2640_probe,
1190 .remove = ov2640_remove,
1191 .id_table = ov2640_id,
1197 static int __init ov2640_module_init(void)
1199 return i2c_add_driver(&ov2640_i2c_driver);
1202 static void __exit ov2640_module_exit(void)
1204 i2c_del_driver(&ov2640_i2c_driver);
1207 module_init(ov2640_module_init);
1208 module_exit(ov2640_module_exit);
1210 MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
1211 MODULE_AUTHOR("Alberto Panizzo");
1212 MODULE_LICENSE("GPL v2");
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>
1228 module_param(debug, int, S_IRUGO|S_IWUSR);
1230 #define dprintk(level, fmt, arg...) do { \
1231 if (debug >= level) \
1232 printk(KERN_WARNING fmt , ## arg); } while (0)
1234 #define SENSOR_TR(format, ...) printk(KERN_ERR format, ## __VA_ARGS__)
1235 #define SENSOR_DG(format, ...) dprintk(1, format, ## __VA_ARGS__)
1238 #define _CONS(a,b) a##b
1239 #define CONS(a,b) _CONS(a,b)
1242 #define _STR(x) __STR(x)
1243 #define STR(x) _STR(x)
1245 #define MIN(x,y) ((x<y) ? x: y)
1246 #define MAX(x,y) ((x>y) ? x: y)
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
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
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
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)
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
1293 #define SENSOR_NAME_STRING(a) STR(CONS(SENSOR_NAME, a))
1294 #define SENSOR_NAME_VARFUN(a) CONS(SENSOR_NAME, a)
1302 /* init 800*600 SVGA */
1303 static struct reginfo sensor_init_data[] =
1320 {0x04,0xF8},//b7,b6 directs
1390 {0x7d,0x00},//0x00//0x07
1392 {0x7d,0x48},//0x48//0x40
1393 {0x7d,0x48},//0x48//0x40
1534 {0x0, 0x0} //end flag
1538 /* 1600X1200 UXGA */
1539 static struct reginfo sensor_uxga[] =
1558 {0x0, 0x0} //end flag
1561 /* 1280X1024 SXGA */
1562 static struct reginfo sensor_sxga[] =
1581 {0x0, 0x0} //end flag
1585 static struct reginfo sensor_xga[] =
1604 {0x0, 0x0} //end flag
1611 static struct reginfo sensor_svga[] =
1613 {0x0, 0x0} //end flag
1617 static struct reginfo sensor_vga[] =
1619 {0x0, 0x0} //end flag
1623 static struct reginfo sensor_cif[] =
1625 {0x0, 0x0} //end flag
1629 static struct reginfo sensor_qvga[] =
1631 {0x0, 0x0} //end flag
1635 static struct reginfo sensor_qcif[] =
1637 {0x0, 0x0} //end flag
1641 static struct reginfo ov2655_qqvga[] =
1685 static struct reginfo ov2655_Sharpness_auto[] =
1690 static struct reginfo ov2655_Sharpness1[] =
1696 static struct reginfo ov2655_Sharpness2[][3] =
1703 static struct reginfo ov2655_Sharpness3[] =
1709 static struct reginfo ov2655_Sharpness4[]=
1716 static struct reginfo ov2655_Sharpness5[] =
1724 static struct reginfo sensor_ClrFmt_YUYV[]=
1730 static struct reginfo sensor_ClrFmt_UYVY[]=
1736 #if CONFIG_SENSOR_WhiteBalance
1737 static struct reginfo sensor_WhiteB_Auto[]=
1739 {0x3406, 0x00}, //AWB auto, bit[1]:0,auto
1742 /* Cloudy Colour Temperature : 6500K - 8000K */
1743 static struct reginfo sensor_WhiteB_Cloudy[]=
1754 /* ClearDay Colour Temperature : 5000K - 6500K */
1755 static struct reginfo sensor_WhiteB_ClearDay[]=
1767 /* Office Colour Temperature : 3500K - 5000K */
1768 static struct reginfo sensor_WhiteB_TungstenLamp1[]=
1781 /* Home Colour Temperature : 2500K - 3500K */
1782 static struct reginfo sensor_WhiteB_TungstenLamp2[]=
1794 static struct reginfo *sensor_WhiteBalanceSeqe[] = {sensor_WhiteB_Auto, sensor_WhiteB_TungstenLamp1,sensor_WhiteB_TungstenLamp2,
1795 sensor_WhiteB_ClearDay, sensor_WhiteB_Cloudy,NULL,
1799 #if CONFIG_SENSOR_Brightness
1800 static struct reginfo sensor_Brightness0[]=
1807 {0x0, 0x0} //end flag
1810 static struct reginfo sensor_Brightness1[]=
1818 {0x0, 0x0} //end flag
1821 static struct reginfo sensor_Brightness2[]=
1829 {0x0, 0x0} //end flag
1832 static struct reginfo sensor_Brightness3[]=
1840 {0x0, 0x0} //end flag
1843 static struct reginfo sensor_Brightness4[]=
1851 {0x0, 0x0} //end flag
1854 static struct reginfo sensor_Brightness5[]=
1861 {0x0, 0x0} //end flag
1863 static struct reginfo *sensor_BrightnessSeqe[] = {sensor_Brightness0, sensor_Brightness1, sensor_Brightness2, sensor_Brightness3,
1864 sensor_Brightness4, sensor_Brightness5,NULL,
1869 #if CONFIG_SENSOR_Effect
1870 static struct reginfo sensor_Effect_Normal[] =
1878 {0x0, 0x0} //end flag
1881 static struct reginfo sensor_Effect_WandB[] =
1889 {0x0, 0x0} //end flag
1892 static struct reginfo sensor_Effect_Sepia[] =
1900 {0x0, 0x0} //end flag
1903 static struct reginfo sensor_Effect_Negative[] =
1911 {0x0, 0x0} //end flag
1913 static struct reginfo sensor_Effect_Bluish[] =
1921 {0x0, 0x0} //end flag
1924 static struct reginfo sensor_Effect_Green[] =
1932 {0x0, 0x0} //end flag
1935 static struct reginfo sensor_Effect_Exp_Windows_Half[] =
1942 {0x0, 0x0} //end flag
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,
1948 #if CONFIG_SENSOR_Exposure
1949 static struct reginfo sensor_Exposure0[]=
1954 static struct reginfo sensor_Exposure1[]=
1959 static struct reginfo sensor_Exposure2[]=
1964 static struct reginfo sensor_Exposure3[]=
1969 static struct reginfo sensor_Exposure4[]=
1974 static struct reginfo sensor_Exposure5[]=
1979 static struct reginfo sensor_Exposure6[]=
1984 static struct reginfo *sensor_ExposureSeqe[] = {sensor_Exposure0, sensor_Exposure1, sensor_Exposure2, sensor_Exposure3,
1985 sensor_Exposure4, sensor_Exposure5,sensor_Exposure6,NULL,
1988 #if CONFIG_SENSOR_Saturation
1989 static struct reginfo sensor_Saturation0[]=
2009 {0x0, 0x0} //end flag
2012 static struct reginfo sensor_Saturation1[]=
2032 {0x0, 0x0} //end flag
2035 static struct reginfo sensor_Saturation2[]=
2055 {0x0, 0x0} //end flag
2057 static struct reginfo *sensor_SaturationSeqe[] = {sensor_Saturation0, sensor_Saturation1, sensor_Saturation2, NULL,};
2061 #if CONFIG_SENSOR_Contrast
2062 static struct reginfo sensor_Contrast0[]=
2072 {0x0, 0x0} //end flag
2076 static struct reginfo sensor_Contrast1[]=
2086 {0x0, 0x0} //end flag
2089 static struct reginfo sensor_Contrast2[]=
2099 {0x0, 0x0} //end flag
2102 static struct reginfo sensor_Contrast3[]=
2112 {0x0, 0x0} //end flag
2115 static struct reginfo sensor_Contrast4[]=
2125 {0x0, 0x0} //end flag
2129 static struct reginfo sensor_Contrast5[]=
2139 {0x0, 0x0} //end flag
2142 static struct reginfo sensor_Contrast6[]=
2152 {0x0, 0x0} //end flag
2156 static struct reginfo sensor_Contrast7[]=
2166 {0x0, 0x0} //end flag
2169 static struct reginfo sensor_Contrast8[]=
2179 {0x0, 0x0} //end flag
2182 static struct reginfo sensor_Contrast9[]=
2192 {0x0, 0x0} //end flag
2197 static struct reginfo *sensor_ContrastSeqe[] = {sensor_Contrast0, sensor_Contrast1, sensor_Contrast2, sensor_Contrast3,
2198 sensor_Contrast4, sensor_Contrast5, sensor_Contrast6, NULL,
2202 #if CONFIG_SENSOR_Mirror
2203 static struct reginfo sensor_MirrorOn[]=
2208 static struct reginfo sensor_MirrorOff[]=
2212 static struct reginfo *sensor_MirrorSeqe[] = {sensor_MirrorOff, sensor_MirrorOn,NULL,};
2214 #if CONFIG_SENSOR_Flip
2215 static struct reginfo sensor_FlipOn[]=
2220 static struct reginfo sensor_FlipOff[]=
2224 static struct reginfo *sensor_FlipSeqe[] = {sensor_FlipOff, sensor_FlipOn,NULL,};
2227 #if CONFIG_SENSOR_Scene
2228 static struct reginfo sensor_SceneAuto[] =
2234 static struct reginfo sensor_SceneNight[] =
2247 static struct reginfo *sensor_SceneSeqe[] = {sensor_SceneAuto, sensor_SceneNight,NULL,};
2250 #if CONFIG_SENSOR_DigitalZoom
2251 static struct reginfo sensor_Zoom0[] =
2256 static struct reginfo sensor_Zoom1[] =
2261 static struct reginfo sensor_Zoom2[] =
2267 static struct reginfo sensor_Zoom3[] =
2271 static struct reginfo *sensor_ZoomSeqe[] = {sensor_Zoom0, sensor_Zoom1, sensor_Zoom2, sensor_Zoom3, NULL,};
2273 static const struct v4l2_querymenu sensor_menus[] =
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,},
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,},
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,},
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,},
2297 static const struct v4l2_queryctrl sensor_controls[] =
2299 #if CONFIG_SENSOR_WhiteBalance
2301 .id = V4L2_CID_DO_WHITE_BALANCE,
2302 .type = V4L2_CTRL_TYPE_MENU,
2303 .name = "White Balance Control",
2311 #if CONFIG_SENSOR_Brightness
2313 .id = V4L2_CID_BRIGHTNESS,
2314 .type = V4L2_CTRL_TYPE_INTEGER,
2315 .name = "Brightness Control",
2323 #if CONFIG_SENSOR_Effect
2325 .id = V4L2_CID_EFFECT,
2326 .type = V4L2_CTRL_TYPE_MENU,
2327 .name = "Effect Control",
2335 #if CONFIG_SENSOR_Exposure
2337 .id = V4L2_CID_EXPOSURE,
2338 .type = V4L2_CTRL_TYPE_INTEGER,
2339 .name = "Exposure Control",
2347 #if CONFIG_SENSOR_Saturation
2349 .id = V4L2_CID_SATURATION,
2350 .type = V4L2_CTRL_TYPE_INTEGER,
2351 .name = "Saturation Control",
2359 #if CONFIG_SENSOR_Contrast
2361 .id = V4L2_CID_CONTRAST,
2362 .type = V4L2_CTRL_TYPE_INTEGER,
2363 .name = "Contrast Control",
2371 #if CONFIG_SENSOR_Mirror
2373 .id = V4L2_CID_HFLIP,
2374 .type = V4L2_CTRL_TYPE_BOOLEAN,
2375 .name = "Mirror Control",
2383 #if CONFIG_SENSOR_Flip
2385 .id = V4L2_CID_VFLIP,
2386 .type = V4L2_CTRL_TYPE_BOOLEAN,
2387 .name = "Flip Control",
2395 #if CONFIG_SENSOR_Scene
2397 .id = V4L2_CID_SCENE,
2398 .type = V4L2_CTRL_TYPE_MENU,
2399 .name = "Scene Control",
2407 #if CONFIG_SENSOR_DigitalZoom
2409 .id = V4L2_CID_ZOOM_RELATIVE,
2410 .type = V4L2_CTRL_TYPE_INTEGER,
2411 .name = "DigitalZoom Control",
2417 .id = V4L2_CID_ZOOM_ABSOLUTE,
2418 .type = V4L2_CTRL_TYPE_INTEGER,
2419 .name = "DigitalZoom Control",
2427 #if CONFIG_SENSOR_Focus
2429 .id = V4L2_CID_FOCUS_RELATIVE,
2430 .type = V4L2_CTRL_TYPE_INTEGER,
2431 .name = "Focus Control",
2437 .id = V4L2_CID_FOCUS_ABSOLUTE,
2438 .type = V4L2_CTRL_TYPE_INTEGER,
2439 .name = "Focus Control",
2443 .default_value = 125,
2447 #if CONFIG_SENSOR_Flash
2449 .id = V4L2_CID_FLASH,
2450 .type = V4L2_CTRL_TYPE_MENU,
2451 .name = "Flash Control",
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);
2473 #if CONFIG_SENSOR_WhiteBalance
2474 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value);
2476 static int sensor_deactivate(struct i2c_client *client);
2478 static struct soc_camera_ops sensor_ops =
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),
2490 /* only one fixed colorspace per pixelcode */
2491 struct sensor_datafmt {
2492 enum v4l2_mbus_pixelcode code;
2493 enum v4l2_colorspace colorspace;
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,
2502 for (i = 0; i < n; i++)
2503 if (fmt[i].code == code)
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}
2514 typedef struct sensor_info_priv_s
2528 unsigned char mirror; /* HFLIP */
2529 unsigned char flip; /* VFLIP */
2530 unsigned int winseqe_cur_addr;
2531 struct sensor_datafmt fmt;
2533 } sensor_info_priv_t;
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;
2544 struct rk29camera_platform_data *sensor_io_request;
2545 struct rk29camera_gpio_res *sensor_gpio_res;
2548 static struct sensor* to_sensor(const struct i2c_client *client)
2550 return container_of(i2c_get_clientdata(client), struct sensor, subdev);
2553 static int sensor_task_lock(struct i2c_client *client, int lock)
2555 #if CONFIG_SENSOR_I2C_NOSCHED
2557 struct sensor *sensor = to_sensor(client);
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());
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;
2573 atomic_add(1, &sensor->tasklock_cnt);
2575 if (atomic_read(&sensor->tasklock_cnt) > 0) {
2576 atomic_sub(1, &sensor->tasklock_cnt);
2578 if (atomic_read(&sensor->tasklock_cnt) == 0)
2583 sensor_task_lock_err:
2590 static int sensor_write(struct i2c_client *client, u8 reg, u8 val)
2594 struct i2c_msg msg[1];
2596 buf[0] = reg & 0xFF;
2599 msg->addr = client->addr;
2600 msg->flags = client->flags;
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 */
2609 while ((cnt-->0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2610 err = i2c_transfer(client->adapter, msg, 1);
2615 SENSOR_TR("\n %s write reg(0x%x, val:0x%x) failed, try to write again!\n",SENSOR_NAME_STRING(),reg, val);
2623 /* sensor register read */
2624 static int sensor_read(struct i2c_client *client, u8 reg, u8 *val)
2628 struct i2c_msg msg[2];
2630 buf[0] = reg ;//>> 8;
2633 msg[0].addr = client->addr;
2634 msg[0].flags = client->flags;
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 */
2640 msg[1].addr = client->addr;
2641 msg[1].flags = client->flags|I2C_M_RD;
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 */
2649 while ((cnt-->0) && (err < 0)) { /* ddl@rock-chips.com : Transfer again if transent is failed */
2650 err = i2c_transfer(client->adapter, msg, 2);
2656 SENSOR_TR("\n %s read reg(0x%x val:0x%x) failed, try to read again! \n",SENSOR_NAME_STRING(),reg, *val);
2664 /* write a array of registers */
2665 static int sensor_write_array(struct i2c_client *client, struct reginfo *regarray)
2669 #if CONFIG_SENSOR_I2C_RDWRCHK
2674 if (sensor_task_lock(client, 1) < 0)
2675 goto sensor_write_array_end;
2676 while (regarray[i].reg != 0)
2678 err = sensor_write(client, regarray[i].reg, regarray[i].val);
2682 SENSOR_TR("%s..write failed current reg:0x%x, Write array again !\n", SENSOR_NAME_STRING(),regarray[i].reg);
2686 SENSOR_TR("%s..write array failed!!!\n", SENSOR_NAME_STRING());
2688 goto sensor_write_array_end;
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);
2700 sensor_write_array_end:
2701 sensor_task_lock(client,0);
2704 #if CONFIG_SENSOR_I2C_RDWRCHK
2705 static int sensor_readchk_array(struct i2c_client *client, struct reginfo *regarray)
2713 while (regarray[i].reg != 0)
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);
2724 static int sensor_ioctrl(struct soc_camera_device *icd,enum rk29sensor_power_cmd cmd, int on)
2726 struct soc_camera_link *icl = to_soc_camera_link(icd);
2729 SENSOR_DG("%s %s cmd(%d) on(%d)\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd,on);
2733 case Sensor_PowerDown:
2735 if (icl->powerdown) {
2736 ret = icl->powerdown(icd->pdev, on);
2737 if (ret == RK29_CAM_IO_SUCCESS) {
2741 icl->reset(icd->pdev);
2743 } else if (ret == RK29_CAM_EIO_REQUESTFAIL) {
2745 goto sensor_power_end;
2752 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
2753 struct sensor *sensor = to_sensor(client);
2755 if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
2756 sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
2762 SENSOR_TR("%s %s cmd(0x%x) is unknown!",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
2769 static int sensor_init(struct v4l2_subdev *sd, u32 val)
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;
2779 SENSOR_DG("\n%s..%s.. \n",SENSOR_NAME_STRING(),__FUNCTION__);
2781 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
2783 goto sensor_INIT_ERR;
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);
2793 SENSOR_TR("%s soft reset sensor failed\n",SENSOR_NAME_STRING());
2795 goto sensor_INIT_ERR;
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);
2803 SENSOR_TR("read chip id high byte failed\n");
2805 goto sensor_INIT_ERR;
2809 ret = sensor_read(client, 0x0b, &value);
2811 SENSOR_TR("read chip id low byte failed\n");
2813 goto sensor_INIT_ERR;
2816 pid |= (value & 0xff);
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;
2822 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
2824 goto sensor_INIT_ERR;
2827 ret = sensor_write_array(client, sensor_init_data);
2830 SENSOR_TR("error: %s initial failed\n",SENSOR_NAME_STRING());
2831 goto sensor_INIT_ERR;
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));
2837 SENSOR_TR("error: %s initial array colour fmts is not support!!",SENSOR_NAME_STRING());
2839 goto sensor_INIT_ERR;
2841 sensor->info_priv.fmt = *fmt;
2843 /* sensor sensor information for initialization */
2844 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_DO_WHITE_BALANCE);
2846 sensor->info_priv.whiteBalance = qctrl->default_value;
2847 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_BRIGHTNESS);
2849 sensor->info_priv.brightness = qctrl->default_value;
2850 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EFFECT);
2852 sensor->info_priv.effect = qctrl->default_value;
2853 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_EXPOSURE);
2855 sensor->info_priv.exposure = qctrl->default_value;
2857 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SATURATION);
2859 sensor->info_priv.saturation = qctrl->default_value;
2860 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_CONTRAST);
2862 sensor->info_priv.contrast = qctrl->default_value;
2863 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_HFLIP);
2865 sensor->info_priv.mirror = qctrl->default_value;
2866 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_VFLIP);
2868 sensor->info_priv.flip = qctrl->default_value;
2869 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_SCENE);
2871 sensor->info_priv.scene = qctrl->default_value;
2872 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
2874 sensor->info_priv.digitalzoom = qctrl->default_value;
2876 /* ddl@rock-chips.com : if sensor support auto focus and flash, programer must run focus and flash code */
2877 #if CONFIG_SENSOR_Focus
2879 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FOCUS_ABSOLUTE);
2881 sensor->info_priv.focus = qctrl->default_value;
2884 #if CONFIG_SENSOR_Flash
2885 qctrl = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_FLASH);
2887 sensor->info_priv.flash = qctrl->default_value;
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);
2894 sensor_task_lock(client,0);
2895 sensor_deactivate(client);
2899 static int sensor_deactivate(struct i2c_client *client)
2901 struct soc_camera_device *icd = client->dev.platform_data;
2903 SENSOR_DG("\n%s..%s.. Enter\n",SENSOR_NAME_STRING(),__FUNCTION__);
2905 /* ddl@rock-chips.com : all sensor output pin must change to input for other sensor */
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);
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;
2920 static struct reginfo sensor_power_down_sequence[]=
2924 static int sensor_suspend(struct soc_camera_device *icd, pm_message_t pm_msg)
2927 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
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) ;
2933 SENSOR_TR("\n %s..%s WriteReg Fail.. \n", SENSOR_NAME_STRING(),__FUNCTION__);
2936 ret = sensor_ioctrl(icd, Sensor_PowerDown, 1);
2938 SENSOR_TR("\n %s suspend fail for turn on power!\n", SENSOR_NAME_STRING());
2943 SENSOR_TR("\n %s cann't suppout Suspend..\n",SENSOR_NAME_STRING());
2949 static int sensor_resume(struct soc_camera_device *icd)
2953 ret = sensor_ioctrl(icd, Sensor_PowerDown, 0);
2955 SENSOR_TR("\n %s resume fail for turn on power!\n", SENSOR_NAME_STRING());
2959 SENSOR_DG("\n %s Enter Resume.. \n", SENSOR_NAME_STRING());
2965 static int sensor_set_bus_param(struct soc_camera_device *icd,
2966 unsigned long flags)
2972 static unsigned long sensor_query_bus_param(struct soc_camera_device *icd)
2974 struct soc_camera_link *icl = to_soc_camera_link(icd);
2975 unsigned long flags = SENSOR_BUS_PARAM;
2977 return soc_camera_apply_sensor_flags(icl, flags);
2980 static int sensor_g_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2982 struct i2c_client *client = sd->priv;
2983 struct soc_camera_device *icd = client->dev.platform_data;
2984 struct sensor *sensor = to_sensor(client);
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;
2994 static bool sensor_fmt_capturechk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
2998 if ((mf->width == 1024) && (mf->height == 768)) {
3000 } else if ((mf->width == 1280) && (mf->height == 1024)) {
3002 } else if ((mf->width == 1600) && (mf->height == 1200)) {
3004 } else if ((mf->width == 2048) && (mf->height == 1536)) {
3006 } else if ((mf->width == 2592) && (mf->height == 1944)) {
3011 SENSOR_DG("%s %dx%d is capture format\n", __FUNCTION__, mf->width, mf->height);
3015 static bool sensor_fmt_videochk(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3019 if ((mf->width == 1280) && (mf->height == 720)) {
3021 } else if ((mf->width == 1920) && (mf->height == 1080)) {
3026 SENSOR_DG("%s %dx%d is video format\n", __FUNCTION__, mf->width, mf->height);
3029 static int sensor_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
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;
3039 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3040 ARRAY_SIZE(sensor_colour_fmts));
3043 goto sensor_s_fmt_end;
3046 if (sensor->info_priv.fmt.code != mf->code) {
3049 case V4L2_MBUS_FMT_YUYV8_2X8:
3051 winseqe_set_addr = sensor_ClrFmt_YUYV;
3054 case V4L2_MBUS_FMT_UYVY8_2X8:
3056 winseqe_set_addr = sensor_ClrFmt_UYVY;
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);
3068 SENSOR_TR("%s v4l2_mbus_code:%d is invalidate!\n", SENSOR_NAME_STRING(),mf->code);
3075 if (((set_w <= 176) && (set_h <= 144)) && sensor_qcif[0].reg)
3077 winseqe_set_addr = sensor_qcif;
3081 else if (((set_w <= 320) && (set_h <= 240)) && sensor_qvga[0].reg)
3083 winseqe_set_addr = sensor_qvga;
3087 else if (((set_w <= 352) && (set_h<= 288)) && sensor_cif[0].reg)
3089 winseqe_set_addr = sensor_cif;
3093 else if (((set_w <= 640) && (set_h <= 480)) && sensor_vga[0].reg)
3095 winseqe_set_addr = sensor_vga;
3099 else if (((set_w <= 800) && (set_h <= 600)) && sensor_svga[0].reg)
3101 winseqe_set_addr = sensor_svga;
3105 else if (((set_w <= 1024) && (set_h <= 768)) && sensor_xga[0].reg)
3107 winseqe_set_addr = sensor_xga;
3111 else if (((set_w <= 1280) && (set_h <= 1024)) && sensor_sxga[0].reg)
3113 winseqe_set_addr = sensor_sxga;
3117 else if (((set_w <= 1600) && (set_h <= 1200)) && sensor_uxga[0].reg)
3119 winseqe_set_addr = sensor_uxga;
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);
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());
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());
3145 ret |= sensor_write_array(client, winseqe_set_addr);
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());
3156 goto sensor_s_fmt_end;
3159 sensor->info_priv.winseqe_cur_addr = (int)winseqe_set_addr;
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);
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);
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);
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);
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);
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);
3192 sensor->info_priv.video2preview = false;
3193 sensor->info_priv.snap2preview = false;
3195 SENSOR_DG("\n%s..%s.. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),__FUNCTION__,set_w,set_h);
3199 SENSOR_DG("\n %s .. Current Format is validate. icd->width = %d.. icd->height %d\n",SENSOR_NAME_STRING(),set_w,set_h);
3209 static int sensor_try_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
3211 struct i2c_client *client = sd->priv;
3212 struct sensor *sensor = to_sensor(client);
3213 const struct sensor_datafmt *fmt;
3216 fmt = sensor_find_datafmt(mf->code, sensor_colour_fmts,
3217 ARRAY_SIZE(sensor_colour_fmts));
3219 fmt = &sensor->info_priv.fmt;
3220 mf->code = fmt->code;
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;
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;
3233 mf->colorspace = fmt->colorspace;
3237 static int sensor_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *id)
3239 struct i2c_client *client = sd->priv;
3241 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
3244 if (id->match.addr != client->addr)
3247 id->ident = SENSOR_V4L2_IDENT; /* ddl@rock-chips.com : Return OV2655 identifier */
3252 #if CONFIG_SENSOR_Brightness
3253 static int sensor_set_brightness(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3255 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3257 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3259 if (sensor_BrightnessSeqe[value - qctrl->minimum] != NULL)
3261 if (sensor_write_array(client, sensor_BrightnessSeqe[value - qctrl->minimum]) != 0)
3263 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3266 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3270 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3274 #if CONFIG_SENSOR_Effect
3275 static int sensor_set_effect(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3277 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3279 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3281 if (sensor_EffectSeqe[value - qctrl->minimum] != NULL)
3283 if (sensor_write_array(client, sensor_EffectSeqe[value - qctrl->minimum]) != 0)
3285 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3288 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3292 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3296 #if CONFIG_SENSOR_Exposure
3297 static int sensor_set_exposure(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3299 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3301 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3303 if (sensor_ExposureSeqe[value - qctrl->minimum] != NULL)
3305 if (sensor_write_array(client, sensor_ExposureSeqe[value - qctrl->minimum]) != 0)
3307 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3310 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3314 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3318 #if CONFIG_SENSOR_Saturation
3319 static int sensor_set_saturation(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3321 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3323 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3325 if (sensor_SaturationSeqe[value - qctrl->minimum] != NULL)
3327 if (sensor_write_array(client, sensor_SaturationSeqe[value - qctrl->minimum]) != 0)
3329 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3332 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3336 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3340 #if CONFIG_SENSOR_Contrast
3341 static int sensor_set_contrast(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3343 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3345 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3347 if (sensor_ContrastSeqe[value - qctrl->minimum] != NULL)
3349 if (sensor_write_array(client, sensor_ContrastSeqe[value - qctrl->minimum]) != 0)
3351 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3354 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3358 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3362 #if CONFIG_SENSOR_Mirror
3363 static int sensor_set_mirror(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3365 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3367 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3369 if (sensor_MirrorSeqe[value - qctrl->minimum] != NULL)
3371 if (sensor_write_array(client, sensor_MirrorSeqe[value - qctrl->minimum]) != 0)
3373 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3376 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3380 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3384 #if CONFIG_SENSOR_Flip
3385 static int sensor_set_flip(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3387 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3389 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3391 if (sensor_FlipSeqe[value - qctrl->minimum] != NULL)
3393 if (sensor_write_array(client, sensor_FlipSeqe[value - qctrl->minimum]) != 0)
3395 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3398 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3402 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3406 #if CONFIG_SENSOR_Scene
3407 static int sensor_set_scene(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3409 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3411 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3413 if (sensor_SceneSeqe[value - qctrl->minimum] != NULL)
3415 if (sensor_write_array(client, sensor_SceneSeqe[value - qctrl->minimum]) != 0)
3417 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3420 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3424 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3428 #if CONFIG_SENSOR_WhiteBalance
3429 static int sensor_set_whiteBalance(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
3431 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
3433 if ((value >= qctrl->minimum) && (value <= qctrl->maximum))
3435 if (sensor_WhiteBalanceSeqe[value - qctrl->minimum] != NULL)
3437 if (sensor_write_array(client, sensor_WhiteBalanceSeqe[value - qctrl->minimum]) != 0)
3439 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3442 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3446 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3450 #if CONFIG_SENSOR_DigitalZoom
3451 static int sensor_set_digitalzoom(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int *value)
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;
3458 qctrl_info = soc_camera_find_qctrl(&sensor_ops, V4L2_CID_ZOOM_ABSOLUTE);
3462 digitalzoom_cur = sensor->info_priv.digitalzoom;
3463 digitalzoom_total = qctrl_info->maximum;
3465 if ((*value > 0) && (digitalzoom_cur >= digitalzoom_total))
3467 SENSOR_TR("%s digitalzoom is maximum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3471 if ((*value < 0) && (digitalzoom_cur <= qctrl_info->minimum))
3473 SENSOR_TR("%s digitalzoom is minimum - %x\n", SENSOR_NAME_STRING(), digitalzoom_cur);
3477 if ((*value > 0) && ((digitalzoom_cur + *value) > digitalzoom_total))
3479 *value = digitalzoom_total - digitalzoom_cur;
3482 if ((*value < 0) && ((digitalzoom_cur + *value) < 0))
3484 *value = 0 - digitalzoom_cur;
3487 digitalzoom_cur += *value;
3489 if (sensor_ZoomSeqe[digitalzoom_cur] != NULL)
3491 if (sensor_write_array(client, sensor_ZoomSeqe[digitalzoom_cur]) != 0)
3493 SENSOR_TR("%s..%s WriteReg Fail.. \n",SENSOR_NAME_STRING(), __FUNCTION__);
3496 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, *value);
3503 #if CONFIG_SENSOR_Flash
3504 static int sensor_set_flash(struct soc_camera_device *icd, const struct v4l2_queryctrl *qctrl, int value)
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 */
3510 sensor_ioctrl(icd, Sensor_Flash, Flash_Off);
3512 SENSOR_DG("%s..%s : %x\n",SENSOR_NAME_STRING(),__FUNCTION__, value);
3516 SENSOR_TR("\n %s..%s valure = %d is invalidate.. \n",SENSOR_NAME_STRING(),__FUNCTION__,value);
3521 static int sensor_g_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
3523 struct i2c_client *client = sd->priv;
3524 struct sensor *sensor = to_sensor(client);
3525 const struct v4l2_queryctrl *qctrl;
3527 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3531 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3537 case V4L2_CID_BRIGHTNESS:
3539 ctrl->value = sensor->info_priv.brightness;
3542 case V4L2_CID_SATURATION:
3544 ctrl->value = sensor->info_priv.saturation;
3547 case V4L2_CID_CONTRAST:
3549 ctrl->value = sensor->info_priv.contrast;
3552 case V4L2_CID_DO_WHITE_BALANCE:
3554 ctrl->value = sensor->info_priv.whiteBalance;
3557 case V4L2_CID_EXPOSURE:
3559 ctrl->value = sensor->info_priv.exposure;
3562 case V4L2_CID_HFLIP:
3564 ctrl->value = sensor->info_priv.mirror;
3567 case V4L2_CID_VFLIP:
3569 ctrl->value = sensor->info_priv.flip;
3580 static int sensor_s_control(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
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;
3588 qctrl = soc_camera_find_qctrl(&sensor_ops, ctrl->id);
3592 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ctrl->id);
3598 #if CONFIG_SENSOR_Brightness
3599 case V4L2_CID_BRIGHTNESS:
3601 if (ctrl->value != sensor->info_priv.brightness)
3603 if (sensor_set_brightness(icd, qctrl,ctrl->value) != 0)
3607 sensor->info_priv.brightness = ctrl->value;
3612 #if CONFIG_SENSOR_Exposure
3613 case V4L2_CID_EXPOSURE:
3615 if (ctrl->value != sensor->info_priv.exposure)
3617 if (sensor_set_exposure(icd, qctrl,ctrl->value) != 0)
3621 sensor->info_priv.exposure = ctrl->value;
3626 #if CONFIG_SENSOR_Saturation
3627 case V4L2_CID_SATURATION:
3629 if (ctrl->value != sensor->info_priv.saturation)
3631 if (sensor_set_saturation(icd, qctrl,ctrl->value) != 0)
3635 sensor->info_priv.saturation = ctrl->value;
3640 #if CONFIG_SENSOR_Contrast
3641 case V4L2_CID_CONTRAST:
3643 if (ctrl->value != sensor->info_priv.contrast)
3645 if (sensor_set_contrast(icd, qctrl,ctrl->value) != 0)
3649 sensor->info_priv.contrast = ctrl->value;
3654 #if CONFIG_SENSOR_WhiteBalance
3655 case V4L2_CID_DO_WHITE_BALANCE:
3657 if (ctrl->value != sensor->info_priv.whiteBalance)
3659 if (sensor_set_whiteBalance(icd, qctrl,ctrl->value) != 0)
3663 sensor->info_priv.whiteBalance = ctrl->value;
3668 #if CONFIG_SENSOR_Mirror
3669 case V4L2_CID_HFLIP:
3671 if (ctrl->value != sensor->info_priv.mirror)
3673 if (sensor_set_mirror(icd, qctrl,ctrl->value) != 0)
3675 sensor->info_priv.mirror = ctrl->value;
3680 #if CONFIG_SENSOR_Flip
3681 case V4L2_CID_VFLIP:
3683 if (ctrl->value != sensor->info_priv.flip)
3685 if (sensor_set_flip(icd, qctrl,ctrl->value) != 0)
3687 sensor->info_priv.flip = ctrl->value;
3698 static int sensor_g_ext_control(struct soc_camera_device *icd , struct v4l2_ext_control *ext_ctrl)
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);
3704 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3708 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3712 switch (ext_ctrl->id)
3714 case V4L2_CID_SCENE:
3716 ext_ctrl->value = sensor->info_priv.scene;
3719 case V4L2_CID_EFFECT:
3721 ext_ctrl->value = sensor->info_priv.effect;
3724 case V4L2_CID_ZOOM_ABSOLUTE:
3726 ext_ctrl->value = sensor->info_priv.digitalzoom;
3729 case V4L2_CID_ZOOM_RELATIVE:
3733 case V4L2_CID_FOCUS_ABSOLUTE:
3735 ext_ctrl->value = sensor->info_priv.focus;
3738 case V4L2_CID_FOCUS_RELATIVE:
3742 case V4L2_CID_FLASH:
3744 ext_ctrl->value = sensor->info_priv.flash;
3752 static int sensor_s_ext_control(struct soc_camera_device *icd, struct v4l2_ext_control *ext_ctrl)
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);
3759 qctrl = soc_camera_find_qctrl(&sensor_ops, ext_ctrl->id);
3763 SENSOR_TR("\n %s ioctrl id = %d is invalidate \n", SENSOR_NAME_STRING(), ext_ctrl->id);
3768 switch (ext_ctrl->id)
3770 #if CONFIG_SENSOR_Scene
3771 case V4L2_CID_SCENE:
3773 if (ext_ctrl->value != sensor->info_priv.scene)
3775 if (sensor_set_scene(icd, qctrl,ext_ctrl->value) != 0)
3777 sensor->info_priv.scene = ext_ctrl->value;
3782 #if CONFIG_SENSOR_Effect
3783 case V4L2_CID_EFFECT:
3785 if (ext_ctrl->value != sensor->info_priv.effect)
3787 if (sensor_set_effect(icd, qctrl,ext_ctrl->value) != 0)
3789 sensor->info_priv.effect= ext_ctrl->value;
3794 #if CONFIG_SENSOR_DigitalZoom
3795 case V4L2_CID_ZOOM_ABSOLUTE:
3797 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3800 if (ext_ctrl->value != sensor->info_priv.digitalzoom)
3802 val_offset = ext_ctrl->value -sensor->info_priv.digitalzoom;
3804 if (sensor_set_digitalzoom(icd, qctrl,&val_offset) != 0)
3806 sensor->info_priv.digitalzoom += val_offset;
3808 SENSOR_DG("%s digitalzoom is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3813 case V4L2_CID_ZOOM_RELATIVE:
3815 if (ext_ctrl->value)
3817 if (sensor_set_digitalzoom(icd, qctrl,&ext_ctrl->value) != 0)
3819 sensor->info_priv.digitalzoom += ext_ctrl->value;
3821 SENSOR_DG("%s digitalzoom is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.digitalzoom);
3826 #if CONFIG_SENSOR_Focus
3827 case V4L2_CID_FOCUS_ABSOLUTE:
3829 if ((ext_ctrl->value < qctrl->minimum) || (ext_ctrl->value > qctrl->maximum))
3832 if (ext_ctrl->value != sensor->info_priv.focus)
3834 val_offset = ext_ctrl->value -sensor->info_priv.focus;
3836 sensor->info_priv.focus += val_offset;
3841 case V4L2_CID_FOCUS_RELATIVE:
3843 if (ext_ctrl->value)
3845 sensor->info_priv.focus += ext_ctrl->value;
3847 SENSOR_DG("%s focus is %x\n", SENSOR_NAME_STRING(), sensor->info_priv.focus);
3852 #if CONFIG_SENSOR_Flash
3853 case V4L2_CID_FLASH:
3855 if (sensor_set_flash(icd, qctrl,ext_ctrl->value) != 0)
3857 sensor->info_priv.flash = ext_ctrl->value;
3859 SENSOR_DG("%s flash is %x\n",SENSOR_NAME_STRING(), sensor->info_priv.flash);
3870 static int sensor_g_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
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;
3877 for (i=0; i<ext_ctrl->count; i++) {
3878 if (sensor_g_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3885 error_idx = ext_ctrl->count;
3887 if (error_idx != -1) {
3888 ext_ctrl->error_idx = error_idx;
3895 static int sensor_s_ext_controls(struct v4l2_subdev *sd, struct v4l2_ext_controls *ext_ctrl)
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;
3902 for (i=0; i<ext_ctrl->count; i++) {
3903 if (sensor_s_ext_control(icd, &ext_ctrl->controls[i]) != 0) {
3910 error_idx = ext_ctrl->count;
3912 if (error_idx != -1) {
3913 ext_ctrl->error_idx = error_idx;
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)
3927 struct sensor *sensor = to_sensor(client);
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)
3935 if (sensor_ioctrl(icd, Sensor_PowerDown, 0) < 0) {
3937 goto sensor_video_probe_err;
3941 ret = sensor_write(client, 0xff, 0x1);
3943 SENSOR_TR("soft reset %s failed\n",SENSOR_NAME_STRING());
3945 goto sensor_video_probe_err;
3947 mdelay(5); //delay 5 microseconds
3949 /* check if it is an sensor sensor */
3950 ret = sensor_read(client, 0x0a, &value);
3952 SENSOR_TR("read chip id high byte failed\n");
3954 goto sensor_video_probe_err;
3958 ret = sensor_read(client, 0x0b, &value);
3960 SENSOR_TR("read chip id low byte failed\n");
3962 goto sensor_video_probe_err;
3965 pid |= (value & 0xff);
3966 SENSOR_DG("\n %s pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3968 if ((pid == SENSOR_ID)||(pid == SENSOR_ID1)) {
3969 sensor->model = SENSOR_V4L2_IDENT;
3971 SENSOR_TR("error: %s mismatched pid = 0x%x\n", SENSOR_NAME_STRING(), pid);
3973 goto sensor_video_probe_err;
3978 sensor_video_probe_err:
3983 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3985 struct i2c_client *client = sd->priv;
3986 struct soc_camera_device *icd = client->dev.platform_data;
3987 struct sensor *sensor = to_sensor(client);
3990 SENSOR_DG("\n%s..%s..cmd:%x \n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
3993 case RK29_CAM_SUBDEV_DEACTIVATE:
3995 sensor_deactivate(client);
3999 case RK29_CAM_SUBDEV_IOREQUEST:
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];
4011 SENSOR_TR("%s %s RK29_CAM_SUBDEV_IOREQUEST fail\n",SENSOR_NAME_STRING(),__FUNCTION__);
4013 goto sensor_ioctl_end;
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
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));
4025 sensor->info_priv.flash = 0xff;
4026 SENSOR_DG("%s flash gpio is invalidate!\n",SENSOR_NAME_STRING());
4034 SENSOR_TR("%s %s cmd(0x%x) is unknown !\n",SENSOR_NAME_STRING(),__FUNCTION__,cmd);
4043 static int sensor_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
4044 enum v4l2_mbus_pixelcode *code)
4046 if (index >= ARRAY_SIZE(sensor_colour_fmts))
4049 *code = sensor_colour_fmts[index].code;
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,
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,
4069 static struct v4l2_subdev_ops sensor_subdev_ops = {
4070 .core = &sensor_subdev_core_ops,
4071 .video = &sensor_subdev_video_ops,
4074 static int sensor_probe(struct i2c_client *client,
4075 const struct i2c_device_id *did)
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;
4083 SENSOR_DG("\n%s..%s..%d..\n",__FUNCTION__,__FILE__,__LINE__);
4085 dev_err(&client->dev, "%s: missing soc-camera data!\n",SENSOR_NAME_STRING());
4089 icl = to_soc_camera_link(icd);
4091 dev_err(&client->dev, "%s driver needs platform data\n", SENSOR_NAME_STRING());
4095 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
4096 dev_warn(&adapter->dev,
4097 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
4101 sensor = kzalloc(sizeof(struct sensor), GFP_KERNEL);
4105 v4l2_i2c_subdev_init(&sensor->subdev, client, &sensor_subdev_ops);
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);
4114 ret = sensor_video_probe(icd, client);
4117 i2c_set_clientdata(client, NULL);
4121 SENSOR_DG("\n%s..%s..%d ret = %x \n",__FUNCTION__,__FILE__,__LINE__,ret);
4125 static int sensor_remove(struct i2c_client *client)
4127 struct sensor *sensor = to_sensor(client);
4128 struct soc_camera_device *icd = client->dev.platform_data;
4131 i2c_set_clientdata(client, NULL);
4132 client->driver = NULL;
4138 static const struct i2c_device_id sensor_id[] = {
4139 {SENSOR_NAME_STRING(), 0 },
4142 MODULE_DEVICE_TABLE(i2c, sensor_id);
4144 static struct i2c_driver sensor_i2c_driver = {
4146 .name = SENSOR_NAME_STRING(),
4148 .probe = sensor_probe,
4149 .remove = sensor_remove,
4150 .id_table = sensor_id,
4153 static int __init sensor_mod_init(void)
4155 SENSOR_DG("\n%s..%s.. \n",__FUNCTION__,SENSOR_NAME_STRING());
4156 return i2c_add_driver(&sensor_i2c_driver);
4159 static void __exit sensor_mod_exit(void)
4161 i2c_del_driver(&sensor_i2c_driver);
4164 device_initcall_sync(sensor_mod_init);
4165 module_exit(sensor_mod_exit);
4167 MODULE_DESCRIPTION(SENSOR_NAME_STRING(Camera sensor driver));
4168 MODULE_AUTHOR("ddl <kernel@rock-chips>");
4169 MODULE_LICENSE("GPL");
4172 >>>>>>> parent of 15f7fab... temp revert rk change