Full support for Ginger Console
[linux-ginger.git] / drivers / media / video / ov3640.c
blob21d0b72e54dc8f3486fd2428e36c03d069ed65e9
1 /*
2 * ov3640.c - OV3640 sensor driver
4 * Copyright (C) 2009 Texas Instruments.
6 * Leverage ov3640.c
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <media/v4l2-int-device.h>
16 #include <media/ov3640.h>
17 #include "ov3640_regs.h"
18 #include "omap34xxcam.h"
19 #include "isp/ispcsi2.h"
21 #define OV3640_DRIVER_NAME "ov3640"
23 #define I2C_M_WR 0
25 /* Register initialization tables for ov3640 */
26 /* Terminating list entry for reg */
27 #define OV3640_REG_TERM 0xFFFF
28 /* Terminating list entry for val */
29 #define OV3640_VAL_TERM 0xFF
31 #define OV3640_CSI2_VIRTUAL_ID 0x1
33 /* FPS Capabilities */
34 #define OV3640_MIN_FPS 5
35 #define OV3640_DEF_FPS 15
36 #define OV3640_MAX_FPS 30
38 #define OV3640_MIN_BRIGHT 0
39 #define OV3640_MAX_BRIGHT 6
40 #define OV3640_DEF_BRIGHT 0
41 #define OV3640_BRIGHT_STEP 1
43 #define OV3640_DEF_CONTRAST 0
44 #define OV3640_MIN_CONTRAST 0
45 #define OV3640_MAX_CONTRAST 6
46 #define OV3640_CONTRAST_STEP 1
48 /* NOTE: Set this as 0 for enabling SoC mode */
49 #define OV3640_RAW_MODE 1
51 /* XCLK Frequency in Hz*/
52 #define OV3640_XCLK 24000000
54 /* High byte of product ID */
55 #define OV3640_PIDH_MAGIC 0x36
56 /* Low byte of product ID */
57 #define OV3640_PIDL_MAGIC1 0x41
58 #define OV3640_PIDL_MAGIC2 0x4C
60 /* define a structure for ov3640 register initialization values */
61 struct ov3640_reg {
62 unsigned int reg;
63 unsigned char val;
66 enum ov3640_image_size {
67 XGA,
68 QXGA
70 enum ov3640_pixel_format {
71 YUV,
72 RGB565,
73 RGB555,
74 RAW10
77 #define OV3640_NUM_IMAGE_SIZES 2
78 #define OV3640_NUM_PIXEL_FORMATS 4
79 #define OV3640_NUM_FPS 3
81 struct ov3640_capture_size {
82 unsigned long width;
83 unsigned long height;
86 const static struct ov3640_reg ov3640_common[2][100] = {
87 /* XGA_Default settings */
89 {OV3640_AEC_H, 0x03},
90 {OV3640_AEC_L, 0x0F},
91 {OV3640_AGC_L, 0x07},
92 {0x304d, 0x45},
93 {0x30aa, 0x45},
94 {OV3640_IO_CTRL1, 0xff},
95 {OV3640_IO_CTRL2, 0x10},
96 {OV3640_WPT_HISH, 0x38},
97 {OV3640_BPT_HISL, 0x30},
98 {OV3640_VPT, 0x61},
99 {0x3082, 0x20},
100 {OV3640_AUTO_3, OV3640_AUTO_3_DUMMYFC_1FRAME |
101 OV3640_AUTO_3_AGCGAINCEIL_32X},
102 {OV3640_AUTO_1, OV3640_AUTO_1_FASTAEC |
103 OV3640_AUTO_1_AECBIGSTEPS |
104 OV3640_AUTO_1_BANDINGFILTEREN |
105 OV3640_AUTO_1_AUTOBANDINGFILTER |
106 OV3640_AUTO_1_EXTRBRIGHTEXPEN
107 #if (OV3640_RAW_MODE == 0)
108 | OV3640_AUTO_1_AGCEN
109 | OV3640_AUTO_1_AECEN
110 #endif
112 {OV3640_AHW_H, 0x08},
113 {OV3640_AHW_L, 0x18},
114 {OV3640_AVH_H, 0x06},
115 {OV3640_AVH_L, 0x0c},
116 {OV3640_WEIGHT0, 0x62},
117 {OV3640_WEIGHT1, 0x26},
118 {OV3640_WEIGHT2, 0xe6},
119 {OV3640_WEIGHT3, 0x6e},
120 {OV3640_WEIGHT4, 0xea},
121 {OV3640_WEIGHT5, 0xae},
122 {OV3640_WEIGHT6, 0xa6},
123 {OV3640_WEIGHT7, 0x6a},
124 {OV3640_SC_SYN_CTRL0, 0x02},
125 {OV3640_SC_SYN_CTRL1, 0xfd},
126 {OV3640_SC_SYN_CTRL2, 0x00},
127 {OV3640_SC_SYN_CTRL3, 0xff},
128 {OV3640_DSP_CTRL_0, 0x13},
129 {OV3640_DSP_CTRL_1, 0xde},
130 {OV3640_DSP_CTRL_2, 0xef},
131 {0x3316, 0xff},
132 {0x3317, 0x00},
133 {0x3312, 0x26},
134 {0x3314, 0x42},
135 {0x3313, 0x2b},
136 {0x3315, 0x42},
137 {0x3310, 0xd0},
138 {0x3311, 0xbd},
139 {0x330c, 0x18},
140 {0x330d, 0x18},
141 {0x330e, 0x56},
142 {0x330f, 0x5c},
143 {0x330b, 0x1c},
144 {0x3306, 0x5c},
145 {0x3307, 0x11},
146 {OV3640_R_A1, 0x52},
147 {OV3640_G_A1, 0x46},
148 {OV3640_B_A1, 0x38},
149 {OV3640_DSPC0, 0x20},
150 {OV3640_DSPC1, 0x17},
151 {OV3640_DSPC2, 0x04},
152 {OV3640_DSPC3, 0x08},
153 {0x3507, 0x06},
154 {0x350a, 0x4f},
155 {OV3640_SC_CTRL0, 0x02},
156 {OV3640_DSP_CTRL_1, 0xde},
157 {OV3640_DSP_CTRL_4, 0xfc},
158 {OV3640_SYS, OV3640_SYS_BASERES_XGA},
159 {OV3640_VS_L, 0x06 + 1},
160 {OV3640_VH_H, 0x03},
161 {OV3640_VH_L, 0x04},
162 {OV3640_VSYNCOPT, 0x24},
163 {OV3640_PCLK, OV3640_PCLK_DIVBY2},
164 {0x30d7, 0x90},
165 {OV3640_SIZE_IN_MISC, 0x34},
166 {OV3640_HSIZE_IN_L, 0x0c},
167 {OV3640_VSIZE_IN_L, 0x04},
168 {OV3640_SIZE_OUT_MISC, 0x34},
169 {OV3640_HSIZE_OUT_L, 0x08},
170 {OV3640_VSIZE_OUT_L, 0x04},
171 {OV3640_ISP_PAD_CTR2, 0x42},
172 {OV3640_ISP_XOUT_H, 0x04},
173 {OV3640_ISP_XOUT_L, 0x00},
174 {OV3640_ISP_YOUT_H, 0x03},
175 {OV3640_ISP_YOUT_L, 0x00},
176 {OV3640_TMC13, 0x04},
177 {OV3640_OUT_CTRL00, OV3640_OUT_CTRL00_VSYNCSEL2 |
178 OV3640_OUT_CTRL00_VSYNCGATE |
179 OV3640_OUT_CTRL00_VSYNCPOL_NEG},
180 {OV3640_MISC_CTRL, 0x00},
181 {OV3640_Y_EDGE_MT, 0x60},
182 {OV3640_BASE1, 0x03},
183 {OV3640_REG_TERM, OV3640_VAL_TERM}
185 /* QXGA Default settings */
187 {OV3640_AEC_H, 0x06},
188 {OV3640_AEC_L, 0x1F},
189 {OV3640_AGC_L, 0x12},
190 {0x304d, 0x45},
191 {0x30aa, 0x45},
192 {OV3640_IO_CTRL0, 0xff},
193 {OV3640_IO_CTRL1, 0xff},
194 {OV3640_IO_CTRL2, 0x10},
195 {0x30d7, 0x10},
196 {OV3640_HISTO7, 0x00},
197 {OV3640_WPT_HISH, 0x60},
198 {OV3640_BPT_HISL, 0x58},
199 {OV3640_VPT, 0xa1},
200 {OV3640_TMC11, 0x02},
201 {0x3082, 0x20},
202 {OV3640_AHW_H, 0x08},
203 {OV3640_AHW_L, 0x18},
204 {OV3640_AVH_H, 0x06},
205 {OV3640_AVH_L, 0x0c},
206 {OV3640_WEIGHT0, 0x62},
207 {OV3640_WEIGHT1, 0x26},
208 {OV3640_WEIGHT2, 0xe6},
209 {OV3640_WEIGHT3, 0x6e},
210 {OV3640_WEIGHT4, 0xea},
211 {OV3640_WEIGHT5, 0xae},
212 {OV3640_WEIGHT6, 0xa6},
213 {OV3640_WEIGHT7, 0x6a},
214 {OV3640_AUTO_3, OV3640_AUTO_3_DUMMYFC_1FRAME |
215 OV3640_AUTO_3_AGCGAINCEIL_8X},
216 {OV3640_AUTO_1, OV3640_AUTO_1_FASTAEC |
217 OV3640_AUTO_1_AECBIGSTEPS |
218 OV3640_AUTO_1_BANDINGFILTEREN |
219 OV3640_AUTO_1_AUTOBANDINGFILTER |
220 OV3640_AUTO_1_EXTRBRIGHTEXPEN
221 #if (OV3640_RAW_MODE == 0)
222 | OV3640_AUTO_1_AGCEN
223 | OV3640_AUTO_1_AECEN
224 #endif
226 {OV3640_SC_SYN_CTRL0, 0x02},
227 {OV3640_SC_SYN_CTRL1, 0xfd},
228 {OV3640_SC_SYN_CTRL2, 0x00},
229 {OV3640_SC_SYN_CTRL3, 0xff},
230 {OV3640_AWB_CTRL_3, 0xa5},
231 {0x3316, 0xff},
232 {0x3317, 0x00},
233 {OV3640_TMC11, 0x02},
234 {0x3082, 0x20},
235 {OV3640_DSP_CTRL_0, 0x13},
236 {OV3640_DSP_CTRL_1, 0xd6},
237 {OV3640_DSP_CTRL_2, 0xef},
238 {OV3640_DSPC0, 0x20},
239 {OV3640_DSPC1, 0x17},
240 {OV3640_DSPC2, 0x04},
241 {OV3640_DSPC3, 0x08},
242 {OV3640_HS_H, 0x01},
243 {OV3640_HS_L, 0x1d},
244 {OV3640_VS_H, 0x00},
245 {OV3640_VS_L, 0x0a + 1},
246 {OV3640_HW_H, 0x08},
247 {OV3640_HW_L, 0x18},
248 {OV3640_VH_H, 0x06},
249 {OV3640_VH_L, 0x0c},
250 {OV3640_SIZE_IN_MISC, 0x68},
251 {OV3640_HSIZE_IN_L, 0x18},
252 {OV3640_VSIZE_IN_L, 0x0c},
253 {OV3640_SIZE_OUT_MISC, 0x68},
254 {OV3640_HSIZE_OUT_L, 0x08},
255 {OV3640_VSIZE_OUT_L, 0x04},
256 {OV3640_ISP_PAD_CTR2, 0x42},
257 {OV3640_ISP_XOUT_H, 0x08},
258 {OV3640_ISP_XOUT_L, 0x00},
259 {OV3640_ISP_YOUT_H, 0x06},
260 {OV3640_ISP_YOUT_L, 0x00},
261 {0x3507, 0x06},
262 {0x350a, 0x4f},
263 {OV3640_OUT_CTRL00, 0xc4},
264 /* Light Mode - Auto */
265 {OV3640_MISC_CTRL, 0x00},
266 /* Sharpness - Level 5 */
267 {OV3640_Y_EDGE_MT, 0x45},
268 /* Sharpness - Auto */
269 {OV3640_Y_EDGE_MT, 0x60},
270 {OV3640_BASE1, 0x03},
271 {OV3640_REG_TERM, OV3640_VAL_TERM}
275 const static struct ov3640_reg ov3640_common_csi2[] = {
276 /* NM OUT_CONTROL2 SOL/EOL off */
277 {OV3640_MIPI_CTRL02, 0x22},
278 /* NM OUT_CONTROL1E h_sel? */
279 {OV3640_OUT_CTRL1E, 0x00},
280 /* min_hs_zero: 6UI + 105ns */
281 {OV3640_MIPI_CTRL22, ((6 & 0x3F) << 2) | ((105 & 0x300) >> 8)},
282 {OV3640_MIPI_CTRL23, (105 & 0xFF)},
283 /* min_clk_zero: 240ns */
284 {OV3640_MIPI_CTRL26, ((0 & 0x3F) << 2) | ((240 & 0x300) >> 8)},
285 {OV3640_MIPI_CTRL27, (240 & 0xFF)},
286 /* min_clk_prepare: 38ns */
287 {OV3640_MIPI_CTRL28, ((0 & 0x3F) << 2) | ((38 & 0x300) >> 8)},
288 {OV3640_MIPI_CTRL29, (38 & 0xFF)},
289 /* max_clk_prepare: 95ns */
290 {OV3640_MIPI_CTRL2A, ((0 & 0x3F) << 2) | ((95 & 0x300) >> 8)},
291 {OV3640_MIPI_CTRL2B, (95 & 0xFF)},
292 /* min_clk_post: 52UI + 60ns */
293 {OV3640_MIPI_CTRL2C, ((52 & 0x3F) << 2) | ((60 & 0x300) >> 8)},
294 {OV3640_MIPI_CTRL2D, (60 & 0xFF)},
295 /* min_hs_prepare: 4UI + 40ns */
296 {OV3640_MIPI_CTRL32, ((4 & 0x3F) << 2) | ((40 & 0x300) >> 8)},
297 {OV3640_MIPI_CTRL33, (40 & 0xFF)},
298 /* ph_byte_order {DI,WC_h,WC_l} */
299 {OV3640_MIPI_CTRL03, 0x49 | OV3640_MIPI_CTRL03_ECC_PHBYTEORDER},
300 /* ph_byte_order2 ph={WC,DI} */
301 {OV3640_MIPI_CTRL4C, OV3640_MIPI_CTRL4C_ECC_PHBYTEORDER2},
302 {0x309e, 0x00},
303 {OV3640_REG_TERM, OV3640_VAL_TERM},
306 /* Array of image sizes supported by OV3640. These must be ordered from
307 * smallest image size to largest.
309 const static struct ov3640_capture_size ov3640_sizes[] = {
310 /* XGA */
311 { 1024, 768 },
312 /* QXGA */
313 { 2048, 1536 },
317 * struct ov3640_sensor - main structure for storage of sensor information
318 * @pdata: access functions and data for platform level information
319 * @v4l2_int_device: V4L2 device structure structure
320 * @i2c_client: iic client device structure
321 * @pix: V4L2 pixel format information structure
322 * @timeperframe: time per frame expressed as V4L fraction
323 * @isize: base image size
324 * @ver: ov3640 chip version
325 * @width: configured width
326 * @height: configuredheight
327 * @vsize: vertical size for the image
328 * @hsize: horizontal size for the image
329 * @crop_rect: crop rectangle specifying the left,top and width and height
331 struct ov3640_sensor {
332 struct device *dev;
333 struct ov3640_platform_data *pdata;
334 struct v4l2_int_device *v4l2_int_device;
335 struct v4l2_pix_format pix;
336 struct v4l2_fract timeperframe;
337 int isize;
338 int ver;
339 int fps;
340 unsigned long width;
341 unsigned long height;
342 unsigned long vsize;
343 unsigned long hsize;
344 struct v4l2_rect crop_rect;
345 int detected;
348 /* List of image formats supported by OV3640 sensor */
349 const static struct v4l2_fmtdesc ov3640_formats[] = {
350 #if OV3640_RAW_MODE
352 .description = "RAW10",
353 .pixelformat = V4L2_PIX_FMT_SGRBG10,
355 #else
357 .description = "RGB565, le",
358 .pixelformat = V4L2_PIX_FMT_RGB565,
361 .description = "RGB565, be",
362 .pixelformat = V4L2_PIX_FMT_RGB565X,
365 .description = "YUYV (YUV 4:2:2), packed",
366 .pixelformat = V4L2_PIX_FMT_YUYV,
369 .description = "UYVY, packed",
370 .pixelformat = V4L2_PIX_FMT_UYVY,
373 .description = "RGB555, le",
374 .pixelformat = V4L2_PIX_FMT_RGB555,
377 .description = "RGB555, be",
378 .pixelformat = V4L2_PIX_FMT_RGB555X,
380 #endif
383 #define OV3640_NUM_CAPTURE_FORMATS \
384 (sizeof(ov3640_formats) / sizeof(ov3640_formats[0]))
386 /* register initialization tables for ov3640 */
388 const static struct ov3640_reg ov3640_out_xga[] = {
389 {OV3640_ISP_XOUT_H, 0x04}, /* ISP_XOUT */
390 {OV3640_ISP_XOUT_L, 0x00}, /* ISP_XOUT */
391 {OV3640_ISP_YOUT_H, 0x03}, /* ISP_YOUT */
392 {OV3640_ISP_YOUT_L, 0x00}, /* ISP_YOUT */
393 {OV3640_REG_TERM, OV3640_VAL_TERM}
396 const static struct ov3640_reg ov3640_out_qxga[] = {
397 {OV3640_ISP_XOUT_H, 0x08}, /* ISP_XOUT */
398 {OV3640_ISP_XOUT_L, 0x00}, /* ISP_XOUT */
399 {OV3640_ISP_YOUT_H, 0x06}, /* ISP_YOUT */
400 {OV3640_ISP_YOUT_L, 0x00}, /* ISP_YOUT */
401 {OV3640_REG_TERM, OV3640_VAL_TERM}
404 /* Brightness Settings - 7 levels */
405 const static struct ov3640_reg brightness[7][5] = {
407 {OV3640_SDE_CTRL, 0x04},
408 {OV3640_SGNSET, 0x09},
409 {OV3640_YBRIGHT, 0x30},
410 {OV3640_REG_TERM, OV3640_VAL_TERM}
413 {OV3640_SDE_CTRL, 0x04},
414 {OV3640_SGNSET, 0x09},
415 {OV3640_YBRIGHT, 0x20},
416 {OV3640_REG_TERM, OV3640_VAL_TERM}
419 {OV3640_SDE_CTRL, 0x04},
420 {OV3640_SGNSET, 0x09},
421 {OV3640_YBRIGHT, 0x10},
422 {OV3640_REG_TERM, OV3640_VAL_TERM}
425 {OV3640_SDE_CTRL, 0x04},
426 {OV3640_SGNSET, 0x01},
427 {OV3640_YBRIGHT, 0x00},
428 {OV3640_REG_TERM, OV3640_VAL_TERM}
431 {OV3640_SDE_CTRL, 0x04},
432 {OV3640_SGNSET, 0x01},
433 {OV3640_YBRIGHT, 0x10},
434 {OV3640_REG_TERM, OV3640_VAL_TERM}
437 {OV3640_SDE_CTRL, 0x04},
438 {OV3640_SGNSET, 0x01},
439 {OV3640_YBRIGHT, 0x20},
440 {OV3640_REG_TERM, OV3640_VAL_TERM}
443 {OV3640_SDE_CTRL, 0x04},
444 {OV3640_SGNSET, 0x01},
445 {OV3640_YBRIGHT, 0x30},
446 {OV3640_REG_TERM, OV3640_VAL_TERM}
450 /* Contrast Settings - 7 levels */
451 const static struct ov3640_reg contrast[7][5] = {
453 {OV3640_SDE_CTRL, 0x04},
454 {OV3640_YOFFSET, 0x14},
455 {OV3640_YGAIN, 0x14},
456 {OV3640_REG_TERM, OV3640_VAL_TERM}
459 {OV3640_SDE_CTRL, 0x04},
460 {OV3640_YOFFSET, 0x18},
461 {OV3640_YGAIN, 0x18},
462 {OV3640_REG_TERM, OV3640_VAL_TERM}
465 {OV3640_SDE_CTRL, 0x04},
466 {OV3640_YOFFSET, 0x1c},
467 {OV3640_YGAIN, 0x1c},
468 {OV3640_REG_TERM, OV3640_VAL_TERM}
471 {OV3640_SDE_CTRL, 0x04},
472 {OV3640_YOFFSET, 0x20},
473 {OV3640_YGAIN, 0x20},
474 {OV3640_REG_TERM, OV3640_VAL_TERM}
477 {OV3640_SDE_CTRL, 0x04},
478 {OV3640_YOFFSET, 0x24},
479 {OV3640_YGAIN, 0x24},
480 {OV3640_REG_TERM, OV3640_VAL_TERM}
483 {OV3640_SDE_CTRL, 0x04},
484 {OV3640_YOFFSET, 0x28},
485 {OV3640_YGAIN, 0x28},
486 {OV3640_REG_TERM, OV3640_VAL_TERM}
489 {OV3640_SDE_CTRL, 0x04},
490 {OV3640_YOFFSET, 0x2c},
491 {OV3640_YGAIN, 0x2c},
492 {OV3640_REG_TERM, OV3640_VAL_TERM}
496 /* Color Settings - 3 colors */
497 const static struct ov3640_reg colors[3][5] = {
499 {OV3640_SDE_CTRL, 0x00},
500 {OV3640_UREG, 0x80},
501 {OV3640_VREG, 0x80},
502 {OV3640_REG_TERM, OV3640_VAL_TERM}
505 {OV3640_SDE_CTRL, 0x18},
506 {OV3640_UREG, 0x80},
507 {OV3640_VREG, 0x80},
508 {OV3640_REG_TERM, OV3640_VAL_TERM}
511 {OV3640_SDE_CTRL, 0x18},
512 {OV3640_UREG, 0x40},
513 {OV3640_VREG, 0xa6},
514 {OV3640_REG_TERM, OV3640_VAL_TERM}
518 /* Average Based Algorithm - Based on target Luminance */
519 const static struct ov3640_reg exposure_avg[11][5] = {
520 /* -1.7EV */
522 {OV3640_HISTO7, 0x00},
523 {OV3640_WPT_HISH, 0x10},
524 {OV3640_BPT_HISL, 0x08},
525 {OV3640_VPT, 0x21},
526 {OV3640_REG_TERM, OV3640_VAL_TERM}
528 /* -1.3EV */
530 {OV3640_HISTO7, 0x00},
531 {OV3640_WPT_HISH, 0x18},
532 {OV3640_BPT_HISL, 0x10},
533 {OV3640_VPT, 0x31},
534 {OV3640_REG_TERM, OV3640_VAL_TERM}
536 /* -1.0EV */
538 {OV3640_HISTO7, 0x00},
539 {OV3640_WPT_HISH, 0x20},
540 {OV3640_BPT_HISL, 0x18},
541 {OV3640_VPT, 0x41},
542 {OV3640_REG_TERM, OV3640_VAL_TERM}
544 /* -0.7EV */
546 {OV3640_HISTO7, 0x00},
547 {OV3640_WPT_HISH, 0x28},
548 {OV3640_BPT_HISL, 0x20},
549 {OV3640_VPT, 0x51},
550 {OV3640_REG_TERM, OV3640_VAL_TERM}
552 /* -0.3EV */
554 {OV3640_HISTO7, 0x00},
555 {OV3640_WPT_HISH, 0x30},
556 {OV3640_BPT_HISL, 0x28},
557 {OV3640_VPT, 0x61},
558 {OV3640_REG_TERM, OV3640_VAL_TERM}
560 /* default */
562 {OV3640_HISTO7, 0x00},
563 {OV3640_WPT_HISH, 0x38},
564 {OV3640_BPT_HISL, 0x30},
565 {OV3640_VPT, 0x61},
566 {OV3640_REG_TERM, OV3640_VAL_TERM}
568 /* 0.3EV */
570 {OV3640_HISTO7, 0x00},
571 {OV3640_WPT_HISH, 0x40},
572 {OV3640_BPT_HISL, 0x38},
573 {OV3640_VPT, 0x71},
574 {OV3640_REG_TERM, OV3640_VAL_TERM}
576 /* 0.7EV */
578 {OV3640_HISTO7, 0x00},
579 {OV3640_WPT_HISH, 0x48},
580 {OV3640_BPT_HISL, 0x40},
581 {OV3640_VPT, 0x81},
582 {OV3640_REG_TERM, OV3640_VAL_TERM}
584 /* 1.0EV */
586 {OV3640_HISTO7, 0x00},
587 {OV3640_WPT_HISH, 0x50},
588 {OV3640_BPT_HISL, 0x48},
589 {OV3640_VPT, 0x91},
590 {OV3640_REG_TERM, OV3640_VAL_TERM}
592 /* 1.3EV */
594 {OV3640_HISTO7, 0x00},
595 {OV3640_WPT_HISH, 0x58},
596 {OV3640_BPT_HISL, 0x50},
597 {OV3640_VPT, 0x91},
598 {OV3640_REG_TERM, OV3640_VAL_TERM}
600 /* 1.7EV */
602 {OV3640_HISTO7, 0x00},
603 {OV3640_WPT_HISH, 0x60},
604 {OV3640_BPT_HISL, 0x58},
605 {OV3640_VPT, 0xa1},
606 {OV3640_REG_TERM, OV3640_VAL_TERM}
610 /* Histogram Based Algorithm - Based on histogram and probability */
611 const static struct ov3640_reg exposure_hist[11][5] = {
612 /* -1.7EV */
614 {OV3640_HISTO7, 0x80},
615 {OV3640_WPT_HISH, 0x58},
616 {OV3640_BPT_HISL, 0x38},
617 {OV3640_REG_TERM, OV3640_VAL_TERM}
619 /* -1.3EV */
621 {OV3640_HISTO7, 0x80},
622 {OV3640_WPT_HISH, 0x60},
623 {OV3640_BPT_HISL, 0x40},
624 {OV3640_REG_TERM, OV3640_VAL_TERM}
626 /* -1.0EV */
628 {OV3640_HISTO7, 0x80},
629 {OV3640_WPT_HISH, 0x68},
630 {OV3640_BPT_HISL, 0x48},
631 {OV3640_REG_TERM, OV3640_VAL_TERM}
633 /* -0.7EV */
635 {OV3640_HISTO7, 0x80},
636 {OV3640_WPT_HISH, 0x70},
637 {OV3640_BPT_HISL, 0x50},
638 {OV3640_REG_TERM, OV3640_VAL_TERM}
640 /* -0.3EV */
642 {OV3640_HISTO7, 0x80},
643 {OV3640_WPT_HISH, 0x78},
644 {OV3640_BPT_HISL, 0x58},
645 {OV3640_REG_TERM, OV3640_VAL_TERM}
647 /* default */
649 {OV3640_HISTO7, 0x80},
650 {OV3640_WPT_HISH, 0x80},
651 {OV3640_BPT_HISL, 0x60},
652 {OV3640_REG_TERM, OV3640_VAL_TERM}
654 /* 0.3EV */
656 {OV3640_HISTO7, 0x80},
657 {OV3640_WPT_HISH, 0x88},
658 {OV3640_BPT_HISL, 0x68},
659 {OV3640_REG_TERM, OV3640_VAL_TERM}
661 /* 0.7EV */
663 {OV3640_HISTO7, 0x80},
664 {OV3640_WPT_HISH, 0x90},
665 {OV3640_BPT_HISL, 0x70},
666 {OV3640_REG_TERM, OV3640_VAL_TERM}
668 /* 1.0EV */
670 {OV3640_HISTO7, 0x80},
671 {OV3640_WPT_HISH, 0x98},
672 {OV3640_BPT_HISL, 0x78},
673 {OV3640_REG_TERM, OV3640_VAL_TERM}
675 /* 1.3EV */
677 {OV3640_HISTO7, 0x80},
678 {OV3640_WPT_HISH, 0xa0},
679 {OV3640_BPT_HISL, 0x80},
680 {OV3640_REG_TERM, OV3640_VAL_TERM}
682 /* 1.7EV */
684 {OV3640_HISTO7, 0x80},
685 {OV3640_WPT_HISH, 0xa8},
686 {OV3640_BPT_HISL, 0x88},
687 {OV3640_REG_TERM, OV3640_VAL_TERM}
691 /* ov3640 register configuration for combinations of pixel format and
692 * image size
695 const static struct ov3640_reg qxga_yuv[] = {
696 {OV3640_SC_CTRL0, 0x02},
697 {OV3640_DSP_CTRL_4, 0xFC},
698 {OV3640_FMT_MUX_CTRL0, 0x00},
699 {OV3640_FMT_CTRL00, 0x00},
700 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
701 {OV3640_VTS_H, 0x06},
702 {OV3640_VTS_L, 0x20},
703 {OV3640_REG_TERM, OV3640_VAL_TERM}
706 const static struct ov3640_reg qxga_565[] = {
707 {OV3640_SC_CTRL0, 0x02},
708 {OV3640_DSP_CTRL_4, 0xFC},
709 {OV3640_FMT_MUX_CTRL0, 0x01},
710 {OV3640_FMT_CTRL00, 0x11},
711 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
712 {OV3640_VTS_H, 0x06},
713 {OV3640_VTS_L, 0x20},
714 {OV3640_REG_TERM, OV3640_VAL_TERM}
717 const static struct ov3640_reg qxga_555[] = {
718 {OV3640_SC_CTRL0, 0x02},
719 {OV3640_DSP_CTRL_4, 0xFC},
720 {OV3640_FMT_MUX_CTRL0, 0x01},
721 {OV3640_FMT_CTRL00, 0x13},
722 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
723 {OV3640_VTS_H, 0x06},
724 {OV3640_VTS_L, 0x20},
725 {OV3640_REG_TERM, OV3640_VAL_TERM}
728 const static struct ov3640_reg qxga_raw10[] = {
729 {OV3640_SC_CTRL0, 0x22},
730 {OV3640_DSP_CTRL_4, 0x01},
731 {OV3640_FMT_MUX_CTRL0, 0x04},
732 {OV3640_FMT_CTRL00, 0x18},
733 {OV3640_OUT_CTRL01, 0x00},
734 {OV3640_VTS_H, 0x06},
735 {OV3640_VTS_L, 0x20},
736 {OV3640_REG_TERM, OV3640_VAL_TERM}
739 const static struct ov3640_reg xga_yuv[] = {
740 {OV3640_SC_CTRL0, 0x02},
741 {OV3640_DSP_CTRL_4, 0xFC},
742 {OV3640_FMT_MUX_CTRL0, 0x00},
743 {OV3640_FMT_CTRL00, 0x00},
744 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
745 {OV3640_VTS_H, 0x03},
746 {OV3640_VTS_L, 0x10},
747 {OV3640_REG_TERM, OV3640_VAL_TERM}
750 const static struct ov3640_reg xga_565[] = {
751 {OV3640_SC_CTRL0, 0x02},
752 {OV3640_DSP_CTRL_4, 0xFC},
753 {OV3640_FMT_MUX_CTRL0, 0x01},
754 {OV3640_FMT_CTRL00, 0x11},
755 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
756 {OV3640_VTS_H, 0x03},
757 {OV3640_VTS_L, 0x10},
758 {OV3640_REG_TERM, OV3640_VAL_TERM}
761 const static struct ov3640_reg xga_555[] = {
762 {OV3640_SC_CTRL0, 0x02},
763 {OV3640_DSP_CTRL_4, 0xFC},
764 {OV3640_FMT_MUX_CTRL0, 0x01},
765 {OV3640_FMT_CTRL00, 0x13},
766 {OV3640_OUT_CTRL01, OV3640_OUT_CTRL01_MIPIBIT8},
767 {OV3640_VTS_H, 0x03},
768 {OV3640_VTS_L, 0x10},
769 {OV3640_REG_TERM, OV3640_VAL_TERM}
772 const static struct ov3640_reg xga_raw10[] = {
773 {OV3640_SC_CTRL0, 0x22},
774 {OV3640_DSP_CTRL_4, 0x01},
775 {OV3640_FMT_MUX_CTRL0, 0x04},
776 {OV3640_FMT_CTRL00, 0x18},
777 {OV3640_OUT_CTRL01, 0x00},
778 {OV3640_VTS_H, 0x03},
779 {OV3640_VTS_L, 0x10},
780 {OV3640_REG_TERM, OV3640_VAL_TERM}
783 const static struct ov3640_reg
784 *ov3640_reg_init[OV3640_NUM_PIXEL_FORMATS][OV3640_NUM_IMAGE_SIZES] = {
785 {xga_yuv, qxga_yuv},
786 {xga_565, qxga_565},
787 {xga_555, qxga_555},
788 {xga_raw10, qxga_raw10}
792 * struct vcontrol - Video controls
793 * @v4l2_queryctrl: V4L2 VIDIOC_QUERYCTRL ioctl structure
794 * @current_value: current value of this control
796 static struct vcontrol {
797 struct v4l2_queryctrl qc;
798 int current_value;
799 } video_control[] = {
800 #if (OV3640_RAW_MODE == 0)
803 .id = V4L2_CID_BRIGHTNESS,
804 .type = V4L2_CTRL_TYPE_INTEGER,
805 .name = "Brightness",
806 .minimum = OV3640_MIN_BRIGHT,
807 .maximum = OV3640_MAX_BRIGHT,
808 .step = OV3640_BRIGHT_STEP,
809 .default_value = OV3640_DEF_BRIGHT,
811 .current_value = OV3640_DEF_BRIGHT,
815 .id = V4L2_CID_CONTRAST,
816 .type = V4L2_CTRL_TYPE_INTEGER,
817 .name = "Contrast",
818 .minimum = OV3640_MIN_CONTRAST,
819 .maximum = OV3640_MAX_CONTRAST,
820 .step = OV3640_CONTRAST_STEP,
821 .default_value = OV3640_DEF_CONTRAST,
823 .current_value = OV3640_DEF_CONTRAST,
827 .id = V4L2_CID_COLORFX,
828 .type = V4L2_CTRL_TYPE_MENU,
829 .name = "Color Effects",
830 .minimum = V4L2_COLORFX_NONE,
831 .maximum = V4L2_COLORFX_SEPIA,
832 .step = 1,
833 .default_value = V4L2_COLORFX_NONE,
835 .current_value = V4L2_COLORFX_NONE,
837 #endif
840 static struct v4l2_querymenu video_menu[] = {
841 #if (OV3640_RAW_MODE == 0)
843 .id = V4L2_CID_COLORFX,
844 .index = 0,
845 .name = "None",
848 .id = V4L2_CID_COLORFX,
849 .index = 1,
850 .name = "B&W",
853 .id = V4L2_CID_COLORFX,
854 .index = 2,
855 .name = "Sepia",
857 #endif
861 * find_vctrl - Finds the requested ID in the video control structure array
862 * @id: ID of control to search the video control array.
864 * Returns the index of the requested ID from the control structure array
866 static int find_vctrl(int id)
868 int i = 0;
870 if (id < V4L2_CID_BASE)
871 return -EDOM;
873 for (i = (ARRAY_SIZE(video_control) - 1); i >= 0; i--)
874 if (video_control[i].qc.id == id)
875 break;
876 if (i < 0)
877 i = -EINVAL;
878 return i;
882 * find_vmenu - Returns index of the menu array of the requested ctrl option.
883 * @id: Requested control ID.
884 * @index: Requested menu option index.
886 * Returns 0 if successful, -EINVAL if not found, or -EDOM if its out of
887 * domain.
889 static int find_vmenu(int id, int index)
891 int i;
893 if (id < V4L2_CID_BASE)
894 return -EDOM;
896 for (i = (ARRAY_SIZE(video_menu) - 1); i >= 0; i--) {
897 if ((video_menu[i].id != id) || (video_menu[i].index != index))
898 continue;
899 return i;
902 return -EINVAL;
906 * Read a value from a register in ov3640 sensor device.
907 * The value is returned in 'val'.
908 * Returns zero if successful, or non-zero otherwise.
910 static int ov3640_read_reg(struct i2c_client *client, u16 data_length, u16 reg,
911 u32 *val)
913 int err = 0;
914 struct i2c_msg msg[1];
915 unsigned char data[4];
917 if (!client->adapter)
918 return -ENODEV;
920 msg->addr = client->addr;
921 msg->flags = I2C_M_WR;
922 msg->len = 2;
923 msg->buf = data;
925 /* High byte goes out first */
926 data[0] = (u8) (reg >> 8);
927 data[1] = (u8) (reg & 0xff);
929 err = i2c_transfer(client->adapter, msg, 1);
930 if (err >= 0) {
931 mdelay(3);
932 msg->flags = I2C_M_RD;
933 msg->len = data_length;
934 err = i2c_transfer(client->adapter, msg, 1);
936 if (err >= 0) {
937 *val = 0;
938 /* High byte comes first */
939 if (data_length == 1)
940 *val = data[0];
941 else if (data_length == 2)
942 *val = data[1] + (data[0] << 8);
943 else
944 *val = data[3] + (data[2] << 8) +
945 (data[1] << 16) + (data[0] << 24);
946 return 0;
948 dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, err);
949 return err;
952 /* Write a value to a register in ov3640 sensor device.
953 * @client: i2c driver client structure.
954 * @reg: Address of the register to read value from.
955 * @val: Value to be written to a specific register.
956 * Returns zero if successful, or non-zero otherwise.
958 static int ov3640_write_reg(struct i2c_client *client, u16 reg, u8 val)
960 int err = 0;
961 struct i2c_msg msg[1];
962 unsigned char data[3];
963 int retries = 0;
965 if (!client->adapter)
966 return -ENODEV;
967 retry:
968 msg->addr = client->addr;
969 msg->flags = I2C_M_WR;
970 msg->len = 3;
971 msg->buf = data;
973 /* high byte goes out first */
974 data[0] = (u8) (reg >> 8);
975 data[1] = (u8) (reg & 0xff);
976 data[2] = val;
978 err = i2c_transfer(client->adapter, msg, 1);
979 udelay(50);
981 if (err >= 0)
982 return 0;
984 if (retries <= 5) {
985 dev_dbg(&client->dev, "Retrying I2C... %d\n", retries);
986 retries++;
987 set_current_state(TASK_UNINTERRUPTIBLE);
988 schedule_timeout(msecs_to_jiffies(20));
989 goto retry;
992 return err;
996 * Initialize a list of ov3640 registers.
997 * The list of registers is terminated by the pair of values
998 * {OV3640_REG_TERM, OV3640_VAL_TERM}.
999 * @client: i2c driver client structure.
1000 * @reglist[]: List of address of the registers to write data.
1001 * Returns zero if successful, or non-zero otherwise.
1003 static int ov3640_write_regs(struct i2c_client *client,
1004 const struct ov3640_reg reglist[])
1006 int err = 0;
1007 const struct ov3640_reg *next = reglist;
1009 while (!((next->reg == OV3640_REG_TERM)
1010 && (next->val == OV3640_VAL_TERM))) {
1011 err = ov3640_write_reg(client, next->reg, next->val);
1012 udelay(100);
1013 if (err)
1014 return err;
1015 next++;
1017 return 0;
1020 /* Find the best match for a requested image capture size. The best match
1021 * is chosen as the nearest match that has the same number or fewer pixels
1022 * as the requested size, or the smallest image size if the requested size
1023 * has fewer pixels than the smallest image.
1025 static enum ov3640_image_size ov3640_find_size(unsigned int width,
1026 unsigned int height)
1028 if ((width > ov3640_sizes[XGA].width) ||
1029 (height > ov3640_sizes[XGA].height))
1030 return QXGA;
1031 return XGA;
1035 * Set CSI2 Virtual ID.
1037 static int ov3640_set_virtual_id(struct i2c_client *client, u32 id)
1039 return ov3640_write_reg(client, OV3640_MIPI_CTRL0C, (0x3 & id) << 6 |
1040 0x02);
1044 * Calculates the MIPIClk.
1045 * 1) Calculate fclk
1046 * fclk = (64 - OV3640_PLL_1[5:0]) * N * Bit8Div * MCLK / M
1047 * where N = 1/1.5/2/3 for OV3640_PLL_2[7:6]=0~3
1048 * M = 1/1.5/2/3 for OV3640_PLL_2[1:0]=0~3
1049 * Bit8Div = 1/1/4/5 for OV3640_PLL_2[5:4]
1050 * 2) Calculate MIPIClk
1051 * MIPIClk = fclk / ScaleDiv / MIPIDiv
1052 * = fclk * (1/ScaleDiv) / MIPIDiv
1053 * where 1/ScaleDiv = 0x3010[3:0]*2
1054 * MIPIDiv = 0x3010[5] + 1
1055 * NOTE:
1056 * - The lookup table 'lut1' has been multiplied by 2 so all its values
1057 * are integers. Since both N & M use the same table, and they are
1058 * used as a ratio then the factor of 2 is already take into account.
1059 * i.e. 2N/2M = N/M
1061 static u32 ov3640_calc_mipiclk(struct v4l2_int_device *s)
1063 struct ov3640_sensor *sensor = s->priv;
1064 struct i2c_client *client = to_i2c_client(sensor->dev);
1065 u32 rxpll, val, n, m, bit8div;
1066 u32 sdiv_inv, mipidiv;
1067 u32 fclk, mipiclk, mclk = 24000000;
1068 u8 lut1[4] = {2, 3, 4, 6};
1069 u8 lut2[4] = {1, 1, 4, 5};
1071 /* Calculate fclk */
1072 ov3640_read_reg(client, 1, OV3640_PLL_1, &val);
1073 rxpll = val & 0x3F;
1075 ov3640_read_reg(client, 1, OV3640_PLL_2, &val);
1076 n = lut1[(val >> 6) & 0x3];
1077 m = lut1[val & 0x3];
1078 bit8div = lut2[(val >> 4) & 0x3];
1079 fclk = (64 - rxpll) * n * bit8div * mclk / m;
1081 ov3640_read_reg(client, 1, OV3640_PLL_3, &val);
1082 mipidiv = ((val >> 5) & 1) + 1;
1083 sdiv_inv = (val & 0xF) * 2;
1085 if ((val & 0xF) >= 1)
1086 mipiclk = fclk / sdiv_inv / mipidiv;
1087 else
1088 mipiclk = fclk / mipidiv;
1089 dev_dbg(&client->dev, "mipiclk=%u fclk=%u val&0xF=%u sdiv_inv=%u "
1090 "mipidiv=%u\n",
1091 mipiclk, fclk, val&0xF,
1092 sdiv_inv, mipidiv);
1093 return mipiclk;
1097 * ov3640_set_framerate
1099 static int ov3640_set_framerate(struct i2c_client *client,
1100 struct v4l2_fract *fper,
1101 enum ov3640_image_size isize)
1103 u32 tempfps1, tempfps2;
1104 u8 clkval;
1106 u32 origvts, newvts, templineperiod;
1107 u32 origvts_h, origvts_l, newvts_h, newvts_l;
1109 int err = 0;
1111 /* FIXME: QXGA framerate setting forced to 15 FPS */
1112 if (isize == QXGA) {
1113 err = ov3640_write_reg(client, OV3640_PLL_1, 0x32);
1114 err = ov3640_write_reg(client, OV3640_PLL_2, 0x21);
1115 err = ov3640_write_reg(client, OV3640_PLL_3, 0x21);
1116 err = ov3640_write_reg(client, OV3640_CLK, 0x01);
1117 err = ov3640_write_reg(client, 0x304c, 0x81);
1118 return err;
1121 tempfps1 = fper->denominator * 10000;
1122 tempfps1 /= fper->numerator;
1123 tempfps2 = fper->denominator / fper->numerator;
1124 if ((tempfps1 % 10000) != 0)
1125 tempfps2++;
1126 clkval = (u8)((30 / tempfps2) - 1);
1128 err = ov3640_write_reg(client, OV3640_CLK, clkval);
1129 /* RxPLL = 50d = 32h */
1130 err = ov3640_write_reg(client, OV3640_PLL_1, 0x32);
1131 /* RxPLL = 50d = 32h */
1132 err = ov3640_write_reg(client, OV3640_PLL_2,
1133 OV3640_PLL_2_BIT8DIV_4 |
1134 OV3640_PLL_2_INDIV_1_5);
1136 * NOTE: Sergio's Fix for MIPI CLK timings, not suggested by OV
1138 err = ov3640_write_reg(client, OV3640_PLL_3, 0x21 +
1139 (clkval & 0xF));
1140 /* Setting DVP divisor value */
1141 err = ov3640_write_reg(client, 0x304c, 0x82);
1142 /* FIXME: Time adjustment to add granularity to the available fps */
1144 ov3640_read_reg(client, 1, OV3640_VTS_H, &origvts_h);
1145 ov3640_read_reg(client, 1, OV3640_VTS_L, &origvts_l);
1146 origvts = (u32)((origvts_h << 8) + origvts_l);
1147 templineperiod = 1000000 / (tempfps2 * origvts);
1148 newvts = 1000000 / (tempfps2 * templineperiod);
1149 newvts_h = (u8)((newvts & 0xFF00) >> 8);
1150 newvts_l = (u8)(newvts & 0xFF);
1151 err = ov3640_write_reg(client, OV3640_VTS_H, newvts_h);
1152 err = ov3640_write_reg(client, OV3640_VTS_L, newvts_l);
1154 return err;
1158 * Configure the ov3640 for a specified image size, pixel format, and frame
1159 * period. xclk is the frequency (in Hz) of the xclk input to the OV3640.
1160 * fper is the frame period (in seconds) expressed as a fraction.
1161 * Returns zero if successful, or non-zero otherwise.
1162 * The actual frame period is returned in fper.
1164 static int ov3640_configure(struct v4l2_int_device *s)
1166 struct ov3640_sensor *sensor = s->priv;
1167 struct v4l2_pix_format *pix = &sensor->pix;
1168 struct i2c_client *client = to_i2c_client(sensor->dev);
1169 enum ov3640_image_size isize = XGA;
1170 unsigned char hsize_l = 0, hsize_h = 0;
1171 unsigned char vsize_l = 0, vsize_h = 0;
1172 int vsize = 0, hsize = 0, height_l = 0, height_h = 0, width_l = 0;
1173 int width_h = 0, ratio = 0, err = 0;
1174 u32 mipiclk;
1175 enum ov3640_pixel_format pfmt = YUV;
1176 u32 min_hs_zero_nui, min_hs_zero, min_hs_zero_total;
1177 u32 min_hs_prepare_nui, min_hs_prepare, min_hs_prepare_total;
1178 u32 max_hs_prepare_nui, max_hs_prepare, max_hs_prepare_total;
1179 u32 ubound_hs_settle, lbound_hs_settle;
1180 u32 val;
1182 switch (pix->pixelformat) {
1184 case V4L2_PIX_FMT_RGB565:
1185 case V4L2_PIX_FMT_RGB565X:
1186 pfmt = RGB565;
1187 break;
1189 case V4L2_PIX_FMT_RGB555:
1190 case V4L2_PIX_FMT_RGB555X:
1191 pfmt = RGB555;
1192 break;
1194 case V4L2_PIX_FMT_SGRBG10:
1195 pfmt = RAW10;
1196 break;
1198 case V4L2_PIX_FMT_YUYV:
1199 case V4L2_PIX_FMT_UYVY:
1200 default:
1201 pfmt = YUV;
1204 /* Set receivers virtual channel before sensor setup starts.
1205 * Only set the sensors virtual channel after all other setup
1206 * for the sensor is complete.
1208 isp_csi2_ctx_config_virtual_id(0, OV3640_CSI2_VIRTUAL_ID);
1209 isp_csi2_ctx_update(0, false);
1211 if (ov3640_find_size(pix->width, pix->height) == XGA)
1212 isize = XGA;
1213 else
1214 isize = QXGA;
1216 /* Reset */
1217 ov3640_write_reg(client, OV3640_SYS, 0x80);
1218 mdelay(5);
1220 /* Common registers */
1221 err = ov3640_write_regs(client, ov3640_common[isize]);
1223 /* Configure image size and pixel format */
1224 err = ov3640_write_regs(client, ov3640_reg_init[pfmt][isize]);
1226 /* Setting of frame rate (OV suggested way) */
1227 err = ov3640_set_framerate(client, &sensor->timeperframe, isize);
1228 #ifdef CONFIG_VIDEO_OV3640_CSI2
1229 /* Set CSI2 common register settings */
1230 err = ov3640_write_regs(client, ov3640_common_csi2);
1231 #endif
1232 sensor->isize = isize;
1234 /* Scale image if needed*/
1235 if (((pix->width == ov3640_sizes[QXGA].width) &&
1236 (pix->height == ov3640_sizes[QXGA].height) && (isize == QXGA)) ||
1237 ((pix->width == ov3640_sizes[XGA].width) &&
1238 (pix->height == ov3640_sizes[XGA].height) && (isize == XGA)) ||
1239 (pfmt == RAW10)) {
1240 /* if the image size correspond to one of the base image sizes
1241 then we don't need to scale the image */
1242 sensor->hsize = pix->width;
1243 sensor->vsize = pix->height;
1245 if (isize == XGA)
1246 ov3640_write_regs(client, ov3640_out_xga);
1247 else
1248 ov3640_write_regs(client, ov3640_out_qxga);
1250 } else {
1251 /* Default Ver and Hor sizes for QXGA and XGA*/
1252 if (isize == QXGA) {
1253 vsize = 0x600;/* 0x60c; */
1254 hsize = 0x800;/* 0x818; */
1255 } else {
1256 vsize = 0x304;
1257 hsize = 0x40c;
1259 /* Scaling */
1260 /* Adjust V and H sizes for image sizes not derived form VGA*/
1261 ratio = (pix->width * 1000) / pix->height;
1263 if (((vsize * ratio + 500) / 1000) > hsize)
1264 vsize = (hsize * 1000) / ratio ;
1266 else
1267 hsize = (vsize * ratio + 500) / 1000;
1269 /* We need even numbers */
1270 if (vsize & 1)
1271 vsize--;
1272 if (hsize & 1)
1273 hsize--;
1275 /* Adjusting numbers to set registers correctly */
1276 hsize_l = (0xFF & hsize);
1277 hsize_h = (0xF00 & hsize) >> 8;
1278 vsize_l = (0xFF & vsize);
1279 vsize_h = (0x700 & vsize) >> 4;
1281 /* According to Software app notes we have to add 0x08 and 0x04
1282 * in order to scale correctly
1284 width_l = (0xFF & pix->width) + 0x08;
1285 width_h = (0xF00 & pix->width) >> 8;
1286 height_l = (0xFF & pix->height) + 0x04;
1287 height_h = (0x700 & pix->height) >> 4;
1289 err = ov3640_write_reg(client, OV3640_SIZE_IN_MISC,
1290 (vsize_h | hsize_h));
1291 err = ov3640_write_reg(client, OV3640_HSIZE_IN_L, hsize_l);
1292 err = ov3640_write_reg(client, OV3640_VSIZE_IN_L, vsize_l);
1293 err = ov3640_write_reg(client, OV3640_SIZE_OUT_MISC,
1294 (height_h | width_h));
1295 err = ov3640_write_reg(client, OV3640_HSIZE_OUT_L, width_l);
1296 err = ov3640_write_reg(client, OV3640_VSIZE_OUT_L, height_l);
1297 err = ov3640_write_reg(client, OV3640_ISP_PAD_CTR2, 0x42);
1298 err = ov3640_write_reg(client, OV3640_ISP_XOUT_H, width_h);
1299 err = ov3640_write_reg(client, OV3640_ISP_XOUT_L,
1300 (width_l - 0x08));
1301 err = ov3640_write_reg(client, OV3640_ISP_YOUT_H,
1302 (height_h >> 4));
1303 err = ov3640_write_reg(client, OV3640_ISP_YOUT_L,
1304 (height_l - 0x04));
1306 sensor->hsize = hsize;
1307 sensor->vsize = vsize;
1309 dev_dbg(&client->dev, "HSIZE_IN =%i VSIZE_IN =%i\n", hsize,
1310 vsize);
1311 dev_dbg(&client->dev, "HSIZE_OUT=%u VSIZE_OUT=%u\n",
1312 (pix->width + 8),
1313 (pix->height + 4));
1314 dev_dbg(&client->dev, "ISP_XOUT =%u ISP_YOUT =%u\n",
1315 pix->width,
1316 pix->height);
1319 /* Setup the ISP VP based on image format */
1320 if (pix->pixelformat == V4L2_PIX_FMT_SGRBG10) {
1321 isp_csi2_ctrl_config_vp_out_ctrl(2);
1322 isp_csi2_ctrl_update(false);
1323 } else {
1324 isp_csi2_ctrl_config_vp_out_ctrl(1);
1325 isp_csi2_ctrl_update(false);
1328 /* Store image size */
1329 sensor->width = pix->width;
1330 sensor->height = pix->height;
1332 sensor->crop_rect.left = 0;
1333 sensor->crop_rect.width = pix->width;
1334 sensor->crop_rect.top = 0;
1335 sensor->crop_rect.height = pix->height;
1337 #ifdef CONFIG_VIDEO_OV3640_CSI2
1338 mipiclk = ov3640_calc_mipiclk(s);
1340 /* Calculate Valid bounds for High speed settle timing in UIs */
1341 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL22, &val);
1342 min_hs_zero_nui = ((val & OV3640_MIPI_CTRL22_MIN_HS_ZERO_NUI_MASK) >>
1343 OV3640_MIPI_CTRL22_MIN_HS_ZERO_NUI_SHIFT);
1344 min_hs_zero = ((val & OV3640_MIPI_CTRL22_MIN_HS_ZERO_H_MASK) << 8);
1345 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL23, &val);
1346 min_hs_zero |= (val & OV3640_MIPI_CTRL23_MIN_HS_ZERO_L_MASK);
1347 min_hs_zero_total = ((min_hs_zero_nui * 1000000 * 1000) / mipiclk) +
1348 min_hs_zero;
1350 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL32, &val);
1351 min_hs_prepare_nui = ((val &
1352 OV3640_MIPI_CTRL32_MIN_HS_PREPARE_NUI_MASK) >>
1353 OV3640_MIPI_CTRL32_MIN_HS_PREPARE_NUI_SHIFT);
1354 min_hs_prepare = ((val &
1355 OV3640_MIPI_CTRL32_MIN_HS_PREPARE_H_MASK) << 8);
1356 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL33, &val);
1357 min_hs_prepare |= (val & OV3640_MIPI_CTRL33_MIN_HS_PREPARE_L_MASK);
1358 min_hs_prepare_total = ((min_hs_prepare_nui * 1000000 * 1000) /
1359 mipiclk) + min_hs_prepare;
1361 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL34, &val);
1362 max_hs_prepare_nui = ((val &
1363 OV3640_MIPI_CTRL34_MAX_HS_PREPARE_NUI_MASK) >>
1364 OV3640_MIPI_CTRL34_MAX_HS_PREPARE_NUI_SHIFT);
1365 max_hs_prepare = ((val &
1366 OV3640_MIPI_CTRL34_MAX_HS_PREPARE_H_MASK) << 8);
1367 ov3640_read_reg(client, 1, OV3640_MIPI_CTRL35, &val);
1368 max_hs_prepare |= (val & OV3640_MIPI_CTRL35_MAX_HS_PREPARE_L_MASK);
1369 max_hs_prepare_total = ((max_hs_prepare_nui * 1000000 * 1000) /
1370 mipiclk) + max_hs_prepare;
1372 ubound_hs_settle = ((min_hs_zero_total + min_hs_prepare_total) *
1373 ((mipiclk >> 1) / 1000000)) / 1000;
1374 lbound_hs_settle = (max_hs_prepare_total * ((mipiclk >> 1) /
1375 1000000)) / 1000;
1377 /* Send settings to ISP-CSI2 Receiver PHY */
1378 isp_csi2_calc_phy_cfg0(mipiclk, lbound_hs_settle, ubound_hs_settle);
1380 /* Set sensors virtual channel*/
1381 ov3640_set_virtual_id(client, OV3640_CSI2_VIRTUAL_ID);
1382 #endif
1383 return err;
1387 /* Detect if an ov3640 is present, returns a negative error number if no
1388 * device is detected, or pidl as version number if a device is detected.
1390 static int ov3640_detect(struct i2c_client *client)
1392 u32 pidh, pidl;
1394 if (!client)
1395 return -ENODEV;
1397 if (ov3640_read_reg(client, 1, OV3640_PIDH, &pidh))
1398 return -ENODEV;
1400 if (ov3640_read_reg(client, 1, OV3640_PIDL, &pidl))
1401 return -ENODEV;
1403 if ((pidh == OV3640_PIDH_MAGIC) && ((pidl == OV3640_PIDL_MAGIC1) ||
1404 (pidl == OV3640_PIDL_MAGIC2))) {
1405 dev_info(&client->dev, "Detect success (%02X,%02X)\n", pidh,
1406 pidl);
1407 return pidl;
1410 return -ENODEV;
1413 /* To get the cropping capabilities of ov3640 sensor
1414 * Returns zero if successful, or non-zero otherwise.
1416 static int ioctl_cropcap(struct v4l2_int_device *s,
1417 struct v4l2_cropcap *cropcap)
1419 struct ov3640_sensor *sensor = s->priv;
1421 cropcap->bounds.top = 0;
1422 cropcap->bounds.left = 0;
1423 cropcap->bounds.width = sensor->width;
1424 cropcap->bounds.height = sensor->height;
1425 cropcap->defrect = cropcap->bounds;
1426 cropcap->pixelaspect.numerator = 1;
1427 cropcap->pixelaspect.denominator = 1;
1428 return 0;
1431 /* To get the current crop window for of ov3640 sensor
1432 * Returns zero if successful, or non-zero otherwise.
1434 static int ioctl_g_crop(struct v4l2_int_device *s, struct v4l2_crop *crop)
1436 struct ov3640_sensor *sensor = s->priv;
1438 crop->c = sensor->crop_rect;
1439 return 0;
1442 /* To set the crop window for of ov3640 sensor
1443 * Returns zero if successful, or non-zero otherwise.
1445 static int ioctl_s_crop(struct v4l2_int_device *s, struct v4l2_crop *crop)
1447 struct ov3640_sensor *sensor = s->priv;
1448 struct v4l2_rect *cur_rect;
1449 unsigned long *cur_width, *cur_height;
1450 int hstart, vstart, hsize, vsize, hsize_l, vsize_l, hsize_h, vsize_h;
1451 int hratio, vratio, zoomfactor, err = 0;
1453 cur_rect = &sensor->crop_rect;
1454 cur_width = &sensor->width;
1455 cur_height = &sensor->height;
1457 if ((crop->c.left == cur_rect->left) &&
1458 (crop->c.width == cur_rect->width) &&
1459 (crop->c.top == cur_rect->top) &&
1460 (crop->c.height == cur_rect->height))
1461 return 0;
1463 /* out of range? then return the current crop rectangle */
1464 if ((crop->c.left + crop->c.width) > sensor->width ||
1465 (crop->c.top + crop->c.height) > sensor->height) {
1466 crop->c = *cur_rect;
1467 return 0;
1470 if (sensor->isize == QXGA)
1471 zoomfactor = 1;
1472 else
1473 zoomfactor = 2;
1475 hratio = (sensor->hsize * 1000) / sensor->width;
1476 vratio = (sensor->vsize * 1000) / sensor->height;
1477 hstart = (((crop->c.left * hratio + 500) / 1000) * zoomfactor) + 0x11d;
1478 vstart = (((crop->c.top * vratio + 500) / 1000) + 0x0a);
1479 hsize = (crop->c.width * hratio + 500) / 1000;
1480 vsize = (crop->c.height * vratio + 500) / 1000;
1482 if (vsize & 1)
1483 vsize--;
1484 if (hsize & 1)
1485 hsize--;
1487 /* Adjusting numbers to set register correctly */
1488 hsize_l = (0xFF & hsize);
1489 hsize_h = (0xF00 & hsize) >> 8;
1490 vsize_l = (0xFF & vsize);
1491 vsize_h = (0x700 & vsize) >> 4;
1493 if ((sensor->height > vsize) || (sensor->width > hsize))
1494 return -EINVAL;
1496 hsize = hsize * zoomfactor;
1498 err = ov3640_write_reg(client, OV3640_DSP_CTRL_2, 0xEF);
1499 err = ov3640_write_reg(client, OV3640_SIZE_IN_MISC, (vsize_h |
1500 hsize_h));
1501 err = ov3640_write_reg(client, OV3640_HSIZE_IN_L, hsize_l);
1502 err = ov3640_write_reg(client, OV3640_VSIZE_IN_L, vsize_l);
1503 err = ov3640_write_reg(client, OV3640_HS_H, (hstart >> 8) & 0xFF);
1504 err = ov3640_write_reg(client, OV3640_HS_L, hstart & 0xFF);
1505 err = ov3640_write_reg(client, OV3640_VS_H, (vstart >> 8) & 0xFF);
1506 err = ov3640_write_reg(client, OV3640_VS_L, vstart & 0xFF);
1507 err = ov3640_write_reg(client, OV3640_HW_H, ((hsize) >> 8) & 0xFF);
1508 err = ov3640_write_reg(client, OV3640_HW_L, hsize & 0xFF);
1509 err = ov3640_write_reg(client, OV3640_VH_H, ((vsize) >> 8) & 0xFF);
1510 err = ov3640_write_reg(client, OV3640_VH_L, vsize & 0xFF);
1512 if (err)
1513 return err;
1515 /* save back */
1516 *cur_rect = crop->c;
1518 /* Setting crop too fast can cause frame out-of-sync. */
1520 set_current_state(TASK_UNINTERRUPTIBLE);
1521 schedule_timeout(msecs_to_jiffies(20));
1522 return 0;
1527 * ioctl_queryctrl - V4L2 sensor interface handler for VIDIOC_QUERYCTRL ioctl
1528 * @s: pointer to standard V4L2 device structure
1529 * @qc: standard V4L2 VIDIOC_QUERYCTRL ioctl structure
1531 * If the requested control is supported, returns the control information
1532 * from the video_control[] array. Otherwise, returns -EINVAL if the
1533 * control is not supported.
1535 static int ioctl_queryctrl(struct v4l2_int_device *s, struct v4l2_queryctrl *qc)
1537 int i;
1539 i = find_vctrl(qc->id);
1540 if (i == -EINVAL)
1541 qc->flags = V4L2_CTRL_FLAG_DISABLED;
1543 if (i < 0)
1544 return -EINVAL;
1546 *qc = video_control[i].qc;
1547 return 0;
1551 * ioctl_queryctrl - Query V4L2 control from existing controls in OV3640.
1552 * @a: Pointer to v4l2_queryctrl structure. It only needs the id field filled.
1554 * Returns 0 if successful, or -EINVAL if not found in OV3640.
1556 int ioctl_querymenu(struct v4l2_querymenu *a)
1558 int i;
1560 i = find_vmenu(a->id, a->index);
1562 if (i < 0)
1563 return -EINVAL;
1565 *a = video_menu[i];
1566 return 0;
1570 * ioctl_g_ctrl - V4L2 sensor interface handler for VIDIOC_G_CTRL ioctl
1571 * @s: pointer to standard V4L2 device structure
1572 * @vc: standard V4L2 VIDIOC_G_CTRL ioctl structure
1574 * If the requested control is supported, returns the control's current
1575 * value from the video_control[] array. Otherwise, returns -EINVAL
1576 * if the control is not supported.
1579 static int ioctl_g_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
1581 struct vcontrol *lvc;
1582 int i;
1584 i = find_vctrl(vc->id);
1585 if (i < 0)
1586 return -EINVAL;
1587 lvc = &video_control[i];
1589 switch (vc->id) {
1590 case V4L2_CID_BRIGHTNESS:
1591 vc->value = lvc->current_value;
1592 break;
1593 case V4L2_CID_CONTRAST:
1594 vc->value = lvc->current_value;
1595 break;
1596 case V4L2_CID_PRIVATE_BASE:
1597 vc->value = lvc->current_value;
1598 break;
1600 return 0;
1604 * ioctl_s_ctrl - V4L2 sensor interface handler for VIDIOC_S_CTRL ioctl
1605 * @s: pointer to standard V4L2 device structure
1606 * @vc: standard V4L2 VIDIOC_S_CTRL ioctl structure
1608 * If the requested control is supported, sets the control's current
1609 * value in HW (and updates the video_control[] array). Otherwise,
1610 * returns -EINVAL if the control is not supported.
1612 static int ioctl_s_ctrl(struct v4l2_int_device *s, struct v4l2_control *vc)
1614 int retval = -EINVAL;
1615 int i;
1616 struct ov3640_sensor *sensor = s->priv;
1617 struct i2c_client *client = to_i2c_client(sensor->dev);
1618 struct vcontrol *lvc;
1620 i = find_vctrl(vc->id);
1621 if (i < 0)
1622 return -EINVAL;
1624 lvc = &video_control[i];
1626 switch (vc->id) {
1627 case V4L2_CID_BRIGHTNESS:
1628 if (vc->value >= 0 && vc->value <= 6) {
1629 retval = ov3640_write_regs(client,
1630 brightness[vc->value]);
1631 } else {
1632 dev_err(&client->dev,
1633 "Brightness level not supported\n");
1634 return -EINVAL;
1636 break;
1637 case V4L2_CID_CONTRAST:
1638 if (vc->value >= 0 && vc->value <= 6)
1639 retval = ov3640_write_regs(client, contrast[vc->value]);
1640 else {
1641 dev_err(&client->dev, "Contrast level not supported\n");
1642 return -EINVAL;
1644 break;
1645 case V4L2_CID_COLORFX:
1646 if (vc->value >= 0 && vc->value <= 2)
1647 retval = ov3640_write_regs(client, colors[vc->value]);
1648 else {
1649 dev_err(&client->dev, "Color effect not supported\n");
1650 return -EINVAL;
1652 break;
1654 if (!retval)
1655 lvc->current_value = vc->value;
1656 return retval;
1660 * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
1661 * @s: pointer to standard V4L2 device structure
1662 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
1664 * Implement the VIDIOC_ENUM_FMT ioctl for the CAPTURE buffer type.
1666 static int ioctl_enum_fmt_cap(struct v4l2_int_device *s,
1667 struct v4l2_fmtdesc *fmt)
1669 int index = fmt->index;
1670 enum v4l2_buf_type type = fmt->type;
1672 memset(fmt, 0, sizeof(*fmt));
1673 fmt->index = index;
1674 fmt->type = type;
1676 switch (fmt->type) {
1677 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1678 if (index >= OV3640_NUM_CAPTURE_FORMATS)
1679 return -EINVAL;
1680 break;
1681 default:
1682 return -EINVAL;
1685 fmt->flags = ov3640_formats[index].flags;
1686 strlcpy(fmt->description, ov3640_formats[index].description,
1687 sizeof(fmt->description));
1688 fmt->pixelformat = ov3640_formats[index].pixelformat;
1690 return 0;
1694 * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
1695 * @s: pointer to standard V4L2 device structure
1696 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
1698 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
1699 * ioctl is used to negotiate the image capture size and pixel format
1700 * without actually making it take effect.
1703 static int ioctl_try_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1705 int ifmt;
1706 enum ov3640_image_size isize;
1707 struct v4l2_pix_format *pix = &f->fmt.pix;
1709 if (pix->width > ov3640_sizes[QXGA].width)
1710 pix->width = ov3640_sizes[QXGA].width;
1711 if (pix->height > ov3640_sizes[QXGA].height)
1712 pix->height = ov3640_sizes[QXGA].height;
1714 isize = ov3640_find_size(pix->width, pix->height);
1715 pix->width = ov3640_sizes[isize].width;
1716 pix->height = ov3640_sizes[isize].height;
1718 for (ifmt = 0; ifmt < OV3640_NUM_CAPTURE_FORMATS; ifmt++) {
1719 if (pix->pixelformat == ov3640_formats[ifmt].pixelformat)
1720 break;
1722 if (ifmt == OV3640_NUM_CAPTURE_FORMATS)
1723 ifmt = 0;
1724 pix->pixelformat = ov3640_formats[ifmt].pixelformat;
1725 pix->field = V4L2_FIELD_NONE;
1726 pix->bytesperline = pix->width*2;
1727 pix->sizeimage = pix->bytesperline*pix->height;
1728 pix->priv = 0;
1729 switch (pix->pixelformat) {
1730 case V4L2_PIX_FMT_YUYV:
1731 case V4L2_PIX_FMT_UYVY:
1732 default:
1733 pix->colorspace = V4L2_COLORSPACE_JPEG;
1734 break;
1735 case V4L2_PIX_FMT_SGRBG10:
1736 case V4L2_PIX_FMT_RGB565:
1737 case V4L2_PIX_FMT_RGB565X:
1738 case V4L2_PIX_FMT_RGB555:
1739 case V4L2_PIX_FMT_RGB555X:
1740 pix->colorspace = V4L2_COLORSPACE_SRGB;
1741 break;
1743 return 0;
1747 * ioctl_s_fmt_cap - V4L2 sensor interface handler for VIDIOC_S_FMT ioctl
1748 * @s: pointer to standard V4L2 device structure
1749 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1751 * If the requested format is supported, configures the HW to use that
1752 * format, returns error code if format not supported or HW can't be
1753 * correctly configured.
1755 static int ioctl_s_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1757 struct ov3640_sensor *sensor = s->priv;
1758 struct v4l2_pix_format *pix = &f->fmt.pix;
1759 int rval;
1761 rval = ioctl_try_fmt_cap(s, f);
1762 if (rval)
1763 return rval;
1765 sensor->pix = *pix;
1767 return 0;
1771 * ioctl_g_fmt_cap - V4L2 sensor interface handler for ioctl_g_fmt_cap
1772 * @s: pointer to standard V4L2 device structure
1773 * @f: pointer to standard V4L2 v4l2_format structure
1775 * Returns the sensor's current pixel format in the v4l2_format
1776 * parameter.
1778 static int ioctl_g_fmt_cap(struct v4l2_int_device *s, struct v4l2_format *f)
1780 struct ov3640_sensor *sensor = s->priv;
1781 f->fmt.pix = sensor->pix;
1783 return 0;
1787 * ioctl_g_parm - V4L2 sensor interface handler for VIDIOC_G_PARM ioctl
1788 * @s: pointer to standard V4L2 device structure
1789 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1791 * Returns the sensor's video CAPTURE parameters.
1793 static int ioctl_g_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1795 struct ov3640_sensor *sensor = s->priv;
1796 struct v4l2_captureparm *cparm = &a->parm.capture;
1798 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1799 return -EINVAL;
1801 memset(a, 0, sizeof(*a));
1802 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1804 cparm->capability = V4L2_CAP_TIMEPERFRAME;
1805 cparm->timeperframe = sensor->timeperframe;
1807 return 0;
1811 * ioctl_s_parm - V4L2 sensor interface handler for VIDIOC_S_PARM ioctl
1812 * @s: pointer to standard V4L2 device structure
1813 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1815 * Configures the sensor to use the input parameters, if possible. If
1816 * not possible, reverts to the old parameters and returns the
1817 * appropriate error code.
1819 static int ioctl_s_parm(struct v4l2_int_device *s, struct v4l2_streamparm *a)
1821 int rval = 0;
1822 struct ov3640_sensor *sensor = s->priv;
1823 struct v4l2_fract *timeperframe = &a->parm.capture.timeperframe;
1824 struct v4l2_fract timeperframe_old;
1825 int desired_fps;
1827 timeperframe_old = sensor->timeperframe;
1828 sensor->timeperframe = *timeperframe;
1830 desired_fps = timeperframe->denominator / timeperframe->numerator;
1831 if ((desired_fps < OV3640_MIN_FPS) || (desired_fps > OV3640_MAX_FPS))
1832 rval = -EINVAL;
1834 if (rval)
1835 sensor->timeperframe = timeperframe_old;
1836 else
1837 *timeperframe = sensor->timeperframe;
1839 return rval;
1843 * ioctl_g_priv - V4L2 sensor interface handler for vidioc_int_g_priv_num
1844 * @s: pointer to standard V4L2 device structure
1845 * @p: void pointer to hold sensor's private data address
1847 * Returns device's (sensor's) private data area address in p parameter
1849 static int ioctl_g_priv(struct v4l2_int_device *s, void *p)
1851 struct ov3640_sensor *sensor = s->priv;
1853 return sensor->pdata->priv_data_set(s, p);
1857 * ioctl_init - V4L2 sensor interface handler for VIDIOC_INT_INIT
1858 * @s: pointer to standard V4L2 device structure
1860 * Initialize the sensor device (call ov3640_configure())
1862 static int ioctl_init(struct v4l2_int_device *s)
1864 return 0;
1868 * ioctl_dev_exit - V4L2 sensor interface handler for vidioc_int_dev_exit_num
1869 * @s: pointer to standard V4L2 device structure
1871 * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
1873 static int ioctl_dev_exit(struct v4l2_int_device *s)
1875 return 0;
1879 * ioctl_dev_init - V4L2 sensor interface handler for vidioc_int_dev_init_num
1880 * @s: pointer to standard V4L2 device structure
1882 * Initialise the device when slave attaches to the master. Returns 0 if
1883 * ov3640 device could be found, otherwise returns appropriate error.
1885 static int ioctl_dev_init(struct v4l2_int_device *s)
1887 struct ov3640_sensor *sensor = s->priv;
1888 struct i2c_client *client = to_i2c_client(sensor->dev);
1889 int err;
1891 err = ov3640_detect(client);
1892 if (err < 0) {
1893 dev_err(&client->dev, "Unable to detect sensor, err %d\n",
1894 err);
1895 sensor->detected = 0;
1896 return err;
1898 sensor->detected = 1;
1899 sensor->ver = err;
1900 dev_dbg(&client->dev, "Chip version 0x%02x detected\n", sensor->ver);
1902 return 0;
1906 * ioctl_enum_framesizes - V4L2 sensor if handler for vidioc_int_enum_framesizes
1907 * @s: pointer to standard V4L2 device structure
1908 * @frms: pointer to standard V4L2 framesizes enumeration structure
1910 * Returns possible framesizes depending on choosen pixel format
1912 static int ioctl_enum_framesizes(struct v4l2_int_device *s,
1913 struct v4l2_frmsizeenum *frms)
1915 int ifmt;
1917 for (ifmt = 0; ifmt < OV3640_NUM_CAPTURE_FORMATS; ifmt++) {
1918 if (frms->pixel_format == ov3640_formats[ifmt].pixelformat)
1919 break;
1921 /* Is requested pixelformat not found on sensor? */
1922 if (ifmt == OV3640_NUM_CAPTURE_FORMATS)
1923 return -EINVAL;
1925 /* Do we already reached all discrete framesizes? */
1926 if (frms->index >= 2)
1927 return -EINVAL;
1929 frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1930 frms->discrete.width = ov3640_sizes[frms->index].width;
1931 frms->discrete.height = ov3640_sizes[frms->index].height;
1933 return 0;
1936 const struct v4l2_fract ov3640_frameintervals[] = {
1937 { .numerator = 2, .denominator = 15 },
1938 { .numerator = 1, .denominator = 15 },
1939 { .numerator = 1, .denominator = 30 },
1942 static int ioctl_enum_frameintervals(struct v4l2_int_device *s,
1943 struct v4l2_frmivalenum *frmi)
1945 int ifmt;
1947 for (ifmt = 0; ifmt < OV3640_NUM_CAPTURE_FORMATS; ifmt++) {
1948 if (frmi->pixel_format == ov3640_formats[ifmt].pixelformat)
1949 break;
1951 /* Is requested pixelformat not found on sensor? */
1952 if (ifmt == OV3640_NUM_CAPTURE_FORMATS)
1953 return -EINVAL;
1955 /* Do we already reached all discrete framesizes? */
1957 if ((frmi->width == ov3640_sizes[1].width) &&
1958 (frmi->height == ov3640_sizes[1].height)) {
1959 /* FIXME: The only frameinterval supported by QXGA capture is
1960 * 2/15 fps
1962 if (frmi->index != 0)
1963 return -EINVAL;
1964 } else {
1965 if (frmi->index >= 3)
1966 return -EINVAL;
1969 frmi->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1970 frmi->discrete.numerator =
1971 ov3640_frameintervals[frmi->index].numerator;
1972 frmi->discrete.denominator =
1973 ov3640_frameintervals[frmi->index].denominator;
1975 return 0;
1979 * ioctl_s_power - V4L2 sensor interface handler for vidioc_int_s_power_num
1980 * @s: pointer to standard V4L2 device structure
1981 * @on: power state to which device is to be set
1983 * Sets devices power state to requested state, if possible.
1985 static int ioctl_s_power(struct v4l2_int_device *s, enum v4l2_power new_power)
1987 struct ov3640_sensor *sensor = s->priv;
1988 int rval;
1990 switch (new_power) {
1991 case V4L2_POWER_ON:
1992 rval = sensor->pdata->set_xclk(s, OV3640_XCLK);
1993 if (rval == -EINVAL)
1994 break;
1995 rval = sensor->pdata->power_set(s, V4L2_POWER_ON);
1996 if (rval)
1997 break;
1999 if (sensor->detected)
2000 ov3640_configure(s);
2001 else {
2002 rval = ioctl_dev_init(s);
2003 if (rval)
2004 goto err_on;
2006 break;
2007 case V4L2_POWER_OFF:
2008 err_on:
2009 rval = sensor->pdata->power_set(s, V4L2_POWER_OFF);
2010 sensor->pdata->set_xclk(s, 0);
2011 break;
2012 case V4L2_POWER_STANDBY:
2013 rval = sensor->pdata->power_set(s, V4L2_POWER_STANDBY);
2014 sensor->pdata->set_xclk(s, 0);
2015 break;
2016 default:
2017 return -EINVAL;
2020 return rval;
2023 static struct v4l2_int_ioctl_desc ov3640_ioctl_desc[] = {
2024 {vidioc_int_enum_framesizes_num,
2025 (v4l2_int_ioctl_func *)ioctl_enum_framesizes},
2026 {vidioc_int_enum_frameintervals_num,
2027 (v4l2_int_ioctl_func *)ioctl_enum_frameintervals},
2028 {vidioc_int_dev_init_num,
2029 (v4l2_int_ioctl_func *)ioctl_dev_init},
2030 {vidioc_int_dev_exit_num,
2031 (v4l2_int_ioctl_func *)ioctl_dev_exit},
2032 {vidioc_int_s_power_num,
2033 (v4l2_int_ioctl_func *)ioctl_s_power},
2034 {vidioc_int_g_priv_num,
2035 (v4l2_int_ioctl_func *)ioctl_g_priv},
2036 {vidioc_int_init_num,
2037 (v4l2_int_ioctl_func *)ioctl_init},
2038 {vidioc_int_enum_fmt_cap_num,
2039 (v4l2_int_ioctl_func *)ioctl_enum_fmt_cap},
2040 {vidioc_int_try_fmt_cap_num,
2041 (v4l2_int_ioctl_func *)ioctl_try_fmt_cap},
2042 {vidioc_int_g_fmt_cap_num,
2043 (v4l2_int_ioctl_func *)ioctl_g_fmt_cap},
2044 {vidioc_int_s_fmt_cap_num,
2045 (v4l2_int_ioctl_func *)ioctl_s_fmt_cap},
2046 {vidioc_int_g_parm_num,
2047 (v4l2_int_ioctl_func *)ioctl_g_parm},
2048 {vidioc_int_s_parm_num,
2049 (v4l2_int_ioctl_func *)ioctl_s_parm},
2050 {vidioc_int_queryctrl_num,
2051 (v4l2_int_ioctl_func *)ioctl_queryctrl},
2052 {vidioc_int_querymenu_num,
2053 (v4l2_int_ioctl_func *)ioctl_querymenu},
2054 {vidioc_int_g_ctrl_num,
2055 (v4l2_int_ioctl_func *)ioctl_g_ctrl},
2056 {vidioc_int_s_ctrl_num,
2057 (v4l2_int_ioctl_func *)ioctl_s_ctrl},
2058 {vidioc_int_g_crop_num,
2059 (v4l2_int_ioctl_func *)ioctl_g_crop},
2060 {vidioc_int_s_crop_num,
2061 (v4l2_int_ioctl_func *)ioctl_s_crop},
2062 {vidioc_int_cropcap_num,
2063 (v4l2_int_ioctl_func *)ioctl_cropcap},
2066 static struct v4l2_int_slave ov3640_slave = {
2067 .ioctls = ov3640_ioctl_desc,
2068 .num_ioctls = ARRAY_SIZE(ov3640_ioctl_desc),
2071 static struct v4l2_int_device ov3640_int_device = {
2072 .module = THIS_MODULE,
2073 .name = OV3640_DRIVER_NAME,
2074 .type = v4l2_int_type_slave,
2075 .u = {
2076 .slave = &ov3640_slave,
2081 * ov3640_probe - sensor driver i2c probe handler
2082 * @client: i2c driver client device structure
2084 * Register sensor as an i2c client device and V4L2
2085 * device.
2087 static int ov3640_probe(struct i2c_client *client,
2088 const struct i2c_device_id *id)
2090 struct ov3640_sensor *sensor;
2091 struct ov3640_platform_data *pdata;
2092 int err;
2094 if (i2c_get_clientdata(client))
2095 return -EBUSY;
2097 pdata = client->dev.platform_data;
2098 if (!pdata) {
2099 dev_err(&client->dev, "no platform data?\n");
2100 return -EINVAL;
2103 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
2104 if (!sensor)
2105 return -ENOMEM;
2107 /* Don't keep pointer to platform data, copy elements instead */
2108 sensor->pdata = kzalloc(sizeof(*sensor->pdata), GFP_KERNEL);
2109 if (!sensor->pdata) {
2110 err = -ENOMEM;
2111 goto on_err1;
2114 sensor->pdata->power_set = pdata->power_set;
2115 sensor->pdata->set_xclk = pdata->set_xclk;
2116 sensor->pdata->priv_data_set = pdata->priv_data_set;
2118 /* Set sensor default values */
2119 sensor->timeperframe.numerator = 1;
2120 sensor->timeperframe.denominator = 15;
2121 sensor->pix.width = ov3640_sizes[XGA].width;
2122 sensor->pix.height = ov3640_sizes[XGA].height;
2123 sensor->pix.pixelformat = V4L2_PIX_FMT_SGRBG10;
2125 sensor->v4l2_int_device = &ov3640_int_device;
2126 sensor->v4l2_int_device->priv = sensor;
2127 sensor->dev = &client->dev;
2129 i2c_set_clientdata(client, sensor);
2131 err = v4l2_int_device_register(sensor->v4l2_int_device);
2132 if (err)
2133 goto on_err2;
2135 return 0;
2136 on_err2:
2137 i2c_set_clientdata(client, NULL);
2138 kfree(sensor->pdata);
2139 on_err1:
2140 kfree(sensor);
2141 return err;
2145 * ov3640_remove - sensor driver i2c remove handler
2146 * @client: i2c driver client device structure
2148 * Unregister sensor as an i2c client device and V4L2
2149 * device. Complement of ov3640_probe().
2151 static int ov3640_remove(struct i2c_client *client)
2153 struct ov3640_sensor *sensor = i2c_get_clientdata(client);
2155 v4l2_int_device_unregister(sensor->v4l2_int_device);
2156 i2c_set_clientdata(client, NULL);
2157 kfree(sensor->pdata);
2158 kfree(sensor);
2160 return 0;
2163 static const struct i2c_device_id ov3640_id[] = {
2164 { OV3640_DRIVER_NAME, 0 },
2165 { },
2167 MODULE_DEVICE_TABLE(i2c, ov3640_id);
2169 static struct i2c_driver ov3640sensor_i2c_driver = {
2170 .driver = {
2171 .name = OV3640_DRIVER_NAME,
2172 .owner = THIS_MODULE,
2174 .probe = ov3640_probe,
2175 .remove = ov3640_remove,
2176 .id_table = ov3640_id,
2180 * ov3640sensor_init - sensor driver module_init handler
2182 * Registers driver as an i2c client driver. Returns 0 on success,
2183 * error code otherwise.
2185 static int __init ov3640sensor_init(void)
2187 int err;
2189 err = i2c_add_driver(&ov3640sensor_i2c_driver);
2190 if (err) {
2191 printk(KERN_ERR "Failed to register " OV3640_DRIVER_NAME
2192 " sensor\n");
2193 return err;
2195 return 0;
2197 module_init(ov3640sensor_init);
2200 * ov3640sensor_cleanup - sensor driver module_exit handler
2202 * Unregisters/deletes driver as an i2c client driver.
2203 * Complement of ov3640sensor_init.
2205 static void __exit ov3640sensor_cleanup(void)
2207 i2c_del_driver(&ov3640sensor_i2c_driver);
2209 module_exit(ov3640sensor_cleanup);
2211 MODULE_LICENSE("GPL");
2212 MODULE_DESCRIPTION("OV3640 camera sensor driver");