blob: 1fdce2f6f880bdbac3cd8d389f0b4c91cb7e333a [file] [log] [blame]
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001/*
2 * ov2640 Camera Driver
3 *
4 * Copyright (C) 2010 Alberto Panizzo <maramaopercheseimorto@gmail.com>
5 *
6 * Based on ov772x, ov9640 drivers and previous non merged implementations.
7 *
8 * Copyright 2005-2009 Freescale Semiconductor, Inc. All Rights Reserved.
9 * Copyright (C) 2006, OmniVision
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
Guennadi Liakhovetski95d20102011-09-09 13:56:04 -030021#include <linux/v4l2-mediabus.h>
Alberto Panizzo3153ac92010-12-02 11:53:28 -030022#include <linux/videodev2.h>
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -030023
Alberto Panizzo3153ac92010-12-02 11:53:28 -030024#include <media/soc_camera.h>
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -030025#include <media/v4l2-clk.h>
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -030026#include <media/v4l2-subdev.h>
Hans Verkuilf0266712011-09-07 05:43:05 -030027#include <media/v4l2-ctrls.h>
Josh Wu650b1812014-11-25 05:54:28 -030028#include <media/v4l2-image-sizes.h>
Alberto Panizzo3153ac92010-12-02 11:53:28 -030029
30#define VAL_SET(x, mask, rshift, lshift) \
31 ((((x) >> rshift) & mask) << lshift)
32/*
33 * DSP registers
34 * register offset for BANK_SEL == BANK_SEL_DSP
35 */
36#define R_BYPASS 0x05 /* Bypass DSP */
37#define R_BYPASS_DSP_BYPAS 0x01 /* Bypass DSP, sensor out directly */
38#define R_BYPASS_USE_DSP 0x00 /* Use the internal DSP */
39#define QS 0x44 /* Quantization Scale Factor */
40#define CTRLI 0x50
41#define CTRLI_LP_DP 0x80
42#define CTRLI_ROUND 0x40
43#define CTRLI_V_DIV_SET(x) VAL_SET(x, 0x3, 0, 3)
44#define CTRLI_H_DIV_SET(x) VAL_SET(x, 0x3, 0, 0)
45#define HSIZE 0x51 /* H_SIZE[7:0] (real/4) */
46#define HSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
47#define VSIZE 0x52 /* V_SIZE[7:0] (real/4) */
48#define VSIZE_SET(x) VAL_SET(x, 0xFF, 2, 0)
49#define XOFFL 0x53 /* OFFSET_X[7:0] */
50#define XOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
51#define YOFFL 0x54 /* OFFSET_Y[7:0] */
52#define YOFFL_SET(x) VAL_SET(x, 0xFF, 0, 0)
53#define VHYX 0x55 /* Offset and size completion */
54#define VHYX_VSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 7)
55#define VHYX_HSIZE_SET(x) VAL_SET(x, 0x1, (8+2), 3)
56#define VHYX_YOFF_SET(x) VAL_SET(x, 0x3, 8, 4)
57#define VHYX_XOFF_SET(x) VAL_SET(x, 0x3, 8, 0)
58#define DPRP 0x56
59#define TEST 0x57 /* Horizontal size completion */
60#define TEST_HSIZE_SET(x) VAL_SET(x, 0x1, (9+2), 7)
61#define ZMOW 0x5A /* Zoom: Out Width OUTW[7:0] (real/4) */
62#define ZMOW_OUTW_SET(x) VAL_SET(x, 0xFF, 2, 0)
63#define ZMOH 0x5B /* Zoom: Out Height OUTH[7:0] (real/4) */
64#define ZMOH_OUTH_SET(x) VAL_SET(x, 0xFF, 2, 0)
65#define ZMHH 0x5C /* Zoom: Speed and H&W completion */
66#define ZMHH_ZSPEED_SET(x) VAL_SET(x, 0x0F, 0, 4)
67#define ZMHH_OUTH_SET(x) VAL_SET(x, 0x1, (8+2), 2)
68#define ZMHH_OUTW_SET(x) VAL_SET(x, 0x3, (8+2), 0)
69#define BPADDR 0x7C /* SDE Indirect Register Access: Address */
70#define BPDATA 0x7D /* SDE Indirect Register Access: Data */
71#define CTRL2 0x86 /* DSP Module enable 2 */
72#define CTRL2_DCW_EN 0x20
73#define CTRL2_SDE_EN 0x10
74#define CTRL2_UV_ADJ_EN 0x08
75#define CTRL2_UV_AVG_EN 0x04
76#define CTRL2_CMX_EN 0x01
77#define CTRL3 0x87 /* DSP Module enable 3 */
78#define CTRL3_BPC_EN 0x80
79#define CTRL3_WPC_EN 0x40
80#define SIZEL 0x8C /* Image Size Completion */
81#define SIZEL_HSIZE8_11_SET(x) VAL_SET(x, 0x1, 11, 6)
82#define SIZEL_HSIZE8_SET(x) VAL_SET(x, 0x7, 0, 3)
83#define SIZEL_VSIZE8_SET(x) VAL_SET(x, 0x7, 0, 0)
84#define HSIZE8 0xC0 /* Image Horizontal Size HSIZE[10:3] */
85#define HSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
86#define VSIZE8 0xC1 /* Image Vertical Size VSIZE[10:3] */
87#define VSIZE8_SET(x) VAL_SET(x, 0xFF, 3, 0)
88#define CTRL0 0xC2 /* DSP Module enable 0 */
89#define CTRL0_AEC_EN 0x80
90#define CTRL0_AEC_SEL 0x40
91#define CTRL0_STAT_SEL 0x20
92#define CTRL0_VFIRST 0x10
93#define CTRL0_YUV422 0x08
94#define CTRL0_YUV_EN 0x04
95#define CTRL0_RGB_EN 0x02
96#define CTRL0_RAW_EN 0x01
97#define CTRL1 0xC3 /* DSP Module enable 1 */
98#define CTRL1_CIP 0x80
99#define CTRL1_DMY 0x40
100#define CTRL1_RAW_GMA 0x20
101#define CTRL1_DG 0x10
102#define CTRL1_AWB 0x08
103#define CTRL1_AWB_GAIN 0x04
104#define CTRL1_LENC 0x02
105#define CTRL1_PRE 0x01
106#define R_DVP_SP 0xD3 /* DVP output speed control */
107#define R_DVP_SP_AUTO_MODE 0x80
108#define R_DVP_SP_DVP_MASK 0x3F /* DVP PCLK = sysclk (48)/[6:0] (YUV0);
109 * = sysclk (48)/(2*[6:0]) (RAW);*/
110#define IMAGE_MODE 0xDA /* Image Output Format Select */
111#define IMAGE_MODE_Y8_DVP_EN 0x40
112#define IMAGE_MODE_JPEG_EN 0x10
113#define IMAGE_MODE_YUV422 0x00
114#define IMAGE_MODE_RAW10 0x04 /* (DVP) */
115#define IMAGE_MODE_RGB565 0x08
116#define IMAGE_MODE_HREF_VSYNC 0x02 /* HREF timing select in DVP JPEG output
117 * mode (0 for HREF is same as sensor) */
118#define IMAGE_MODE_LBYTE_FIRST 0x01 /* Byte swap enable for DVP
119 * 1: Low byte first UYVY (C2[4] =0)
120 * VYUY (C2[4] =1)
121 * 0: High byte first YUYV (C2[4]=0)
122 * YVYU (C2[4] = 1) */
123#define RESET 0xE0 /* Reset */
124#define RESET_MICROC 0x40
125#define RESET_SCCB 0x20
126#define RESET_JPEG 0x10
127#define RESET_DVP 0x04
128#define RESET_IPU 0x02
129#define RESET_CIF 0x01
130#define REGED 0xED /* Register ED */
131#define REGED_CLK_OUT_DIS 0x10
132#define MS_SP 0xF0 /* SCCB Master Speed */
133#define SS_ID 0xF7 /* SCCB Slave ID */
134#define SS_CTRL 0xF8 /* SCCB Slave Control */
135#define SS_CTRL_ADD_AUTO_INC 0x20
136#define SS_CTRL_EN 0x08
137#define SS_CTRL_DELAY_CLK 0x04
138#define SS_CTRL_ACC_EN 0x02
139#define SS_CTRL_SEN_PASS_THR 0x01
140#define MC_BIST 0xF9 /* Microcontroller misc register */
141#define MC_BIST_RESET 0x80 /* Microcontroller Reset */
142#define MC_BIST_BOOT_ROM_SEL 0x40
143#define MC_BIST_12KB_SEL 0x20
144#define MC_BIST_12KB_MASK 0x30
145#define MC_BIST_512KB_SEL 0x08
146#define MC_BIST_512KB_MASK 0x0C
147#define MC_BIST_BUSY_BIT_R 0x02
148#define MC_BIST_MC_RES_ONE_SH_W 0x02
149#define MC_BIST_LAUNCH 0x01
150#define BANK_SEL 0xFF /* Register Bank Select */
151#define BANK_SEL_DSP 0x00
152#define BANK_SEL_SENS 0x01
153
154/*
155 * Sensor registers
156 * register offset for BANK_SEL == BANK_SEL_SENS
157 */
158#define GAIN 0x00 /* AGC - Gain control gain setting */
159#define COM1 0x03 /* Common control 1 */
160#define COM1_1_DUMMY_FR 0x40
161#define COM1_3_DUMMY_FR 0x80
162#define COM1_7_DUMMY_FR 0xC0
163#define COM1_VWIN_LSB_UXGA 0x0F
164#define COM1_VWIN_LSB_SVGA 0x0A
165#define COM1_VWIN_LSB_CIF 0x06
166#define REG04 0x04 /* Register 04 */
167#define REG04_DEF 0x20 /* Always set */
168#define REG04_HFLIP_IMG 0x80 /* Horizontal mirror image ON/OFF */
169#define REG04_VFLIP_IMG 0x40 /* Vertical flip image ON/OFF */
170#define REG04_VREF_EN 0x10
171#define REG04_HREF_EN 0x08
172#define REG04_AEC_SET(x) VAL_SET(x, 0x3, 0, 0)
173#define REG08 0x08 /* Frame Exposure One-pin Control Pre-charge Row Num */
174#define COM2 0x09 /* Common control 2 */
175#define COM2_SOFT_SLEEP_MODE 0x10 /* Soft sleep mode */
176 /* Output drive capability */
177#define COM2_OCAP_Nx_SET(N) (((N) - 1) & 0x03) /* N = [1x .. 4x] */
178#define PID 0x0A /* Product ID Number MSB */
179#define VER 0x0B /* Product ID Number LSB */
180#define COM3 0x0C /* Common control 3 */
181#define COM3_BAND_50H 0x04 /* 0 For Banding at 60H */
182#define COM3_BAND_AUTO 0x02 /* Auto Banding */
183#define COM3_SING_FR_SNAPSH 0x01 /* 0 For enable live video output after the
184 * snapshot sequence*/
185#define AEC 0x10 /* AEC[9:2] Exposure Value */
186#define CLKRC 0x11 /* Internal clock */
187#define CLKRC_EN 0x80
188#define CLKRC_DIV_SET(x) (((x) - 1) & 0x1F) /* CLK = XVCLK/(x) */
189#define COM7 0x12 /* Common control 7 */
190#define COM7_SRST 0x80 /* Initiates system reset. All registers are
191 * set to factory default values after which
192 * the chip resumes normal operation */
193#define COM7_RES_UXGA 0x00 /* Resolution selectors for UXGA */
194#define COM7_RES_SVGA 0x40 /* SVGA */
195#define COM7_RES_CIF 0x20 /* CIF */
196#define COM7_ZOOM_EN 0x04 /* Enable Zoom mode */
197#define COM7_COLOR_BAR_TEST 0x02 /* Enable Color Bar Test Pattern */
198#define COM8 0x13 /* Common control 8 */
199#define COM8_DEF 0xC0 /* Banding filter ON/OFF */
200#define COM8_BNDF_EN 0x20 /* Banding filter ON/OFF */
201#define COM8_AGC_EN 0x04 /* AGC Auto/Manual control selection */
202#define COM8_AEC_EN 0x01 /* Auto/Manual Exposure control */
203#define COM9 0x14 /* Common control 9
204 * Automatic gain ceiling - maximum AGC value [7:5]*/
205#define COM9_AGC_GAIN_2x 0x00 /* 000 : 2x */
206#define COM9_AGC_GAIN_4x 0x20 /* 001 : 4x */
207#define COM9_AGC_GAIN_8x 0x40 /* 010 : 8x */
208#define COM9_AGC_GAIN_16x 0x60 /* 011 : 16x */
209#define COM9_AGC_GAIN_32x 0x80 /* 100 : 32x */
210#define COM9_AGC_GAIN_64x 0xA0 /* 101 : 64x */
211#define COM9_AGC_GAIN_128x 0xC0 /* 110 : 128x */
212#define COM10 0x15 /* Common control 10 */
213#define COM10_PCLK_HREF 0x20 /* PCLK output qualified by HREF */
214#define COM10_PCLK_RISE 0x10 /* Data is updated at the rising edge of
215 * PCLK (user can latch data at the next
216 * falling edge of PCLK).
217 * 0 otherwise. */
218#define COM10_HREF_INV 0x08 /* Invert HREF polarity:
219 * HREF negative for valid data*/
220#define COM10_VSINC_INV 0x02 /* Invert VSYNC polarity */
221#define HSTART 0x17 /* Horizontal Window start MSB 8 bit */
222#define HEND 0x18 /* Horizontal Window end MSB 8 bit */
223#define VSTART 0x19 /* Vertical Window start MSB 8 bit */
224#define VEND 0x1A /* Vertical Window end MSB 8 bit */
225#define MIDH 0x1C /* Manufacturer ID byte - high */
226#define MIDL 0x1D /* Manufacturer ID byte - low */
227#define AEW 0x24 /* AGC/AEC - Stable operating region (upper limit) */
228#define AEB 0x25 /* AGC/AEC - Stable operating region (lower limit) */
229#define VV 0x26 /* AGC/AEC Fast mode operating region */
230#define VV_HIGH_TH_SET(x) VAL_SET(x, 0xF, 0, 4)
231#define VV_LOW_TH_SET(x) VAL_SET(x, 0xF, 0, 0)
232#define REG2A 0x2A /* Dummy pixel insert MSB */
233#define FRARL 0x2B /* Dummy pixel insert LSB */
234#define ADDVFL 0x2D /* LSB of insert dummy lines in Vertical direction */
235#define ADDVFH 0x2E /* MSB of insert dummy lines in Vertical direction */
236#define YAVG 0x2F /* Y/G Channel Average value */
237#define REG32 0x32 /* Common Control 32 */
238#define REG32_PCLK_DIV_2 0x80 /* PCLK freq divided by 2 */
239#define REG32_PCLK_DIV_4 0xC0 /* PCLK freq divided by 4 */
240#define ARCOM2 0x34 /* Zoom: Horizontal start point */
241#define REG45 0x45 /* Register 45 */
242#define FLL 0x46 /* Frame Length Adjustment LSBs */
243#define FLH 0x47 /* Frame Length Adjustment MSBs */
244#define COM19 0x48 /* Zoom: Vertical start point */
245#define ZOOMS 0x49 /* Zoom: Vertical start point */
246#define COM22 0x4B /* Flash light control */
247#define COM25 0x4E /* For Banding operations */
248#define BD50 0x4F /* 50Hz Banding AEC 8 LSBs */
249#define BD60 0x50 /* 60Hz Banding AEC 8 LSBs */
250#define REG5D 0x5D /* AVGsel[7:0], 16-zone average weight option */
251#define REG5E 0x5E /* AVGsel[15:8], 16-zone average weight option */
252#define REG5F 0x5F /* AVGsel[23:16], 16-zone average weight option */
253#define REG60 0x60 /* AVGsel[31:24], 16-zone average weight option */
254#define HISTO_LOW 0x61 /* Histogram Algorithm Low Level */
255#define HISTO_HIGH 0x62 /* Histogram Algorithm High Level */
256
257/*
258 * ID
259 */
260#define MANUFACTURER_ID 0x7FA2
261#define PID_OV2640 0x2642
262#define VERSION(pid, ver) ((pid << 8) | (ver & 0xFF))
263
264/*
265 * Struct
266 */
267struct regval_list {
268 u8 reg_num;
269 u8 value;
270};
271
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300272struct ov2640_win_size {
273 char *name;
Josh Wu650b1812014-11-25 05:54:28 -0300274 u32 width;
275 u32 height;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300276 const struct regval_list *regs;
277};
278
279
280struct ov2640_priv {
281 struct v4l2_subdev subdev;
Hans Verkuilf0266712011-09-07 05:43:05 -0300282 struct v4l2_ctrl_handler hdl;
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300283 u32 cfmt_code;
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -0300284 struct v4l2_clk *clk;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300285 const struct ov2640_win_size *win;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300286};
287
288/*
289 * Registers settings
290 */
291
292#define ENDMARKER { 0xff, 0xff }
293
294static const struct regval_list ov2640_init_regs[] = {
295 { BANK_SEL, BANK_SEL_DSP },
296 { 0x2c, 0xff },
297 { 0x2e, 0xdf },
298 { BANK_SEL, BANK_SEL_SENS },
299 { 0x3c, 0x32 },
300 { CLKRC, CLKRC_DIV_SET(1) },
301 { COM2, COM2_OCAP_Nx_SET(3) },
302 { REG04, REG04_DEF | REG04_HREF_EN },
303 { COM8, COM8_DEF | COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN },
304 { COM9, COM9_AGC_GAIN_8x | 0x08},
305 { 0x2c, 0x0c },
306 { 0x33, 0x78 },
307 { 0x3a, 0x33 },
308 { 0x3b, 0xfb },
309 { 0x3e, 0x00 },
310 { 0x43, 0x11 },
311 { 0x16, 0x10 },
312 { 0x39, 0x02 },
313 { 0x35, 0x88 },
314 { 0x22, 0x0a },
315 { 0x37, 0x40 },
316 { 0x23, 0x00 },
317 { ARCOM2, 0xa0 },
318 { 0x06, 0x02 },
319 { 0x06, 0x88 },
320 { 0x07, 0xc0 },
321 { 0x0d, 0xb7 },
322 { 0x0e, 0x01 },
323 { 0x4c, 0x00 },
324 { 0x4a, 0x81 },
325 { 0x21, 0x99 },
326 { AEW, 0x40 },
327 { AEB, 0x38 },
328 { VV, VV_HIGH_TH_SET(0x08) | VV_LOW_TH_SET(0x02) },
329 { 0x5c, 0x00 },
330 { 0x63, 0x00 },
331 { FLL, 0x22 },
332 { COM3, 0x38 | COM3_BAND_AUTO },
333 { REG5D, 0x55 },
334 { REG5E, 0x7d },
335 { REG5F, 0x7d },
336 { REG60, 0x55 },
337 { HISTO_LOW, 0x70 },
338 { HISTO_HIGH, 0x80 },
339 { 0x7c, 0x05 },
340 { 0x20, 0x80 },
341 { 0x28, 0x30 },
342 { 0x6c, 0x00 },
343 { 0x6d, 0x80 },
344 { 0x6e, 0x00 },
345 { 0x70, 0x02 },
346 { 0x71, 0x94 },
347 { 0x73, 0xc1 },
348 { 0x3d, 0x34 },
349 { COM7, COM7_RES_UXGA | COM7_ZOOM_EN },
350 { 0x5a, 0x57 },
351 { BD50, 0xbb },
352 { BD60, 0x9c },
353 { BANK_SEL, BANK_SEL_DSP },
354 { 0xe5, 0x7f },
355 { MC_BIST, MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
356 { 0x41, 0x24 },
357 { RESET, RESET_JPEG | RESET_DVP },
358 { 0x76, 0xff },
359 { 0x33, 0xa0 },
360 { 0x42, 0x20 },
361 { 0x43, 0x18 },
362 { 0x4c, 0x00 },
363 { CTRL3, CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
364 { 0x88, 0x3f },
365 { 0xd7, 0x03 },
366 { 0xd9, 0x10 },
367 { R_DVP_SP , R_DVP_SP_AUTO_MODE | 0x2 },
368 { 0xc8, 0x08 },
369 { 0xc9, 0x80 },
370 { BPADDR, 0x00 },
371 { BPDATA, 0x00 },
372 { BPADDR, 0x03 },
373 { BPDATA, 0x48 },
374 { BPDATA, 0x48 },
375 { BPADDR, 0x08 },
376 { BPDATA, 0x20 },
377 { BPDATA, 0x10 },
378 { BPDATA, 0x0e },
379 { 0x90, 0x00 },
380 { 0x91, 0x0e },
381 { 0x91, 0x1a },
382 { 0x91, 0x31 },
383 { 0x91, 0x5a },
384 { 0x91, 0x69 },
385 { 0x91, 0x75 },
386 { 0x91, 0x7e },
387 { 0x91, 0x88 },
388 { 0x91, 0x8f },
389 { 0x91, 0x96 },
390 { 0x91, 0xa3 },
391 { 0x91, 0xaf },
392 { 0x91, 0xc4 },
393 { 0x91, 0xd7 },
394 { 0x91, 0xe8 },
395 { 0x91, 0x20 },
396 { 0x92, 0x00 },
397 { 0x93, 0x06 },
398 { 0x93, 0xe3 },
399 { 0x93, 0x03 },
400 { 0x93, 0x03 },
401 { 0x93, 0x00 },
402 { 0x93, 0x02 },
403 { 0x93, 0x00 },
404 { 0x93, 0x00 },
405 { 0x93, 0x00 },
406 { 0x93, 0x00 },
407 { 0x93, 0x00 },
408 { 0x93, 0x00 },
409 { 0x93, 0x00 },
410 { 0x96, 0x00 },
411 { 0x97, 0x08 },
412 { 0x97, 0x19 },
413 { 0x97, 0x02 },
414 { 0x97, 0x0c },
415 { 0x97, 0x24 },
416 { 0x97, 0x30 },
417 { 0x97, 0x28 },
418 { 0x97, 0x26 },
419 { 0x97, 0x02 },
420 { 0x97, 0x98 },
421 { 0x97, 0x80 },
422 { 0x97, 0x00 },
423 { 0x97, 0x00 },
424 { 0xa4, 0x00 },
425 { 0xa8, 0x00 },
426 { 0xc5, 0x11 },
427 { 0xc6, 0x51 },
428 { 0xbf, 0x80 },
429 { 0xc7, 0x10 },
430 { 0xb6, 0x66 },
431 { 0xb8, 0xA5 },
432 { 0xb7, 0x64 },
433 { 0xb9, 0x7C },
434 { 0xb3, 0xaf },
435 { 0xb4, 0x97 },
436 { 0xb5, 0xFF },
437 { 0xb0, 0xC5 },
438 { 0xb1, 0x94 },
439 { 0xb2, 0x0f },
440 { 0xc4, 0x5c },
441 { 0xa6, 0x00 },
442 { 0xa7, 0x20 },
443 { 0xa7, 0xd8 },
444 { 0xa7, 0x1b },
445 { 0xa7, 0x31 },
446 { 0xa7, 0x00 },
447 { 0xa7, 0x18 },
448 { 0xa7, 0x20 },
449 { 0xa7, 0xd8 },
450 { 0xa7, 0x19 },
451 { 0xa7, 0x31 },
452 { 0xa7, 0x00 },
453 { 0xa7, 0x18 },
454 { 0xa7, 0x20 },
455 { 0xa7, 0xd8 },
456 { 0xa7, 0x19 },
457 { 0xa7, 0x31 },
458 { 0xa7, 0x00 },
459 { 0xa7, 0x18 },
460 { 0x7f, 0x00 },
461 { 0xe5, 0x1f },
462 { 0xe1, 0x77 },
463 { 0xdd, 0x7f },
464 { CTRL0, CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
465 ENDMARKER,
466};
467
468/*
469 * Register settings for window size
470 * The preamble, setup the internal DSP to input an UXGA (1600x1200) image.
471 * Then the different zooming configurations will setup the output image size.
472 */
473static const struct regval_list ov2640_size_change_preamble_regs[] = {
474 { BANK_SEL, BANK_SEL_DSP },
475 { RESET, RESET_DVP },
Josh Wu650b1812014-11-25 05:54:28 -0300476 { HSIZE8, HSIZE8_SET(UXGA_WIDTH) },
477 { VSIZE8, VSIZE8_SET(UXGA_HEIGHT) },
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300478 { CTRL2, CTRL2_DCW_EN | CTRL2_SDE_EN |
479 CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
Josh Wu650b1812014-11-25 05:54:28 -0300480 { HSIZE, HSIZE_SET(UXGA_WIDTH) },
481 { VSIZE, VSIZE_SET(UXGA_HEIGHT) },
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300482 { XOFFL, XOFFL_SET(0) },
483 { YOFFL, YOFFL_SET(0) },
Josh Wu650b1812014-11-25 05:54:28 -0300484 { VHYX, VHYX_HSIZE_SET(UXGA_WIDTH) | VHYX_VSIZE_SET(UXGA_HEIGHT) |
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300485 VHYX_XOFF_SET(0) | VHYX_YOFF_SET(0)},
Josh Wu650b1812014-11-25 05:54:28 -0300486 { TEST, TEST_HSIZE_SET(UXGA_WIDTH) },
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300487 ENDMARKER,
488};
489
490#define PER_SIZE_REG_SEQ(x, y, v_div, h_div, pclk_div) \
491 { CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(v_div) | \
492 CTRLI_H_DIV_SET(h_div)}, \
493 { ZMOW, ZMOW_OUTW_SET(x) }, \
494 { ZMOH, ZMOH_OUTH_SET(y) }, \
495 { ZMHH, ZMHH_OUTW_SET(x) | ZMHH_OUTH_SET(y) }, \
496 { R_DVP_SP, pclk_div }, \
497 { RESET, 0x00}
498
499static const struct regval_list ov2640_qcif_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300500 PER_SIZE_REG_SEQ(QCIF_WIDTH, QCIF_HEIGHT, 3, 3, 4),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300501 ENDMARKER,
502};
503
504static const struct regval_list ov2640_qvga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300505 PER_SIZE_REG_SEQ(QVGA_WIDTH, QVGA_HEIGHT, 2, 2, 4),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300506 ENDMARKER,
507};
508
509static const struct regval_list ov2640_cif_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300510 PER_SIZE_REG_SEQ(CIF_WIDTH, CIF_HEIGHT, 2, 2, 8),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300511 ENDMARKER,
512};
513
514static const struct regval_list ov2640_vga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300515 PER_SIZE_REG_SEQ(VGA_WIDTH, VGA_HEIGHT, 0, 0, 2),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300516 ENDMARKER,
517};
518
519static const struct regval_list ov2640_svga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300520 PER_SIZE_REG_SEQ(SVGA_WIDTH, SVGA_HEIGHT, 1, 1, 2),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300521 ENDMARKER,
522};
523
524static const struct regval_list ov2640_xga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300525 PER_SIZE_REG_SEQ(XGA_WIDTH, XGA_HEIGHT, 0, 0, 2),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300526 { CTRLI, 0x00},
527 ENDMARKER,
528};
529
530static const struct regval_list ov2640_sxga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300531 PER_SIZE_REG_SEQ(SXGA_WIDTH, SXGA_HEIGHT, 0, 0, 2),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300532 { CTRLI, 0x00},
533 { R_DVP_SP, 2 | R_DVP_SP_AUTO_MODE },
534 ENDMARKER,
535};
536
537static const struct regval_list ov2640_uxga_regs[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300538 PER_SIZE_REG_SEQ(UXGA_WIDTH, UXGA_HEIGHT, 0, 0, 0),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300539 { CTRLI, 0x00},
540 { R_DVP_SP, 0 | R_DVP_SP_AUTO_MODE },
541 ENDMARKER,
542};
543
544#define OV2640_SIZE(n, w, h, r) \
545 {.name = n, .width = w , .height = h, .regs = r }
546
547static const struct ov2640_win_size ov2640_supported_win_sizes[] = {
Josh Wu650b1812014-11-25 05:54:28 -0300548 OV2640_SIZE("QCIF", QCIF_WIDTH, QCIF_HEIGHT, ov2640_qcif_regs),
549 OV2640_SIZE("QVGA", QVGA_WIDTH, QVGA_HEIGHT, ov2640_qvga_regs),
550 OV2640_SIZE("CIF", CIF_WIDTH, CIF_HEIGHT, ov2640_cif_regs),
551 OV2640_SIZE("VGA", VGA_WIDTH, VGA_HEIGHT, ov2640_vga_regs),
552 OV2640_SIZE("SVGA", SVGA_WIDTH, SVGA_HEIGHT, ov2640_svga_regs),
553 OV2640_SIZE("XGA", XGA_WIDTH, XGA_HEIGHT, ov2640_xga_regs),
554 OV2640_SIZE("SXGA", SXGA_WIDTH, SXGA_HEIGHT, ov2640_sxga_regs),
555 OV2640_SIZE("UXGA", UXGA_WIDTH, UXGA_HEIGHT, ov2640_uxga_regs),
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300556};
557
558/*
559 * Register settings for pixel formats
560 */
561static const struct regval_list ov2640_format_change_preamble_regs[] = {
562 { BANK_SEL, BANK_SEL_DSP },
563 { R_BYPASS, R_BYPASS_USE_DSP },
564 ENDMARKER,
565};
566
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300567static const struct regval_list ov2640_yuyv_regs[] = {
568 { IMAGE_MODE, IMAGE_MODE_YUV422 },
569 { 0xd7, 0x03 },
570 { 0x33, 0xa0 },
571 { 0xe5, 0x1f },
572 { 0xe1, 0x67 },
573 { RESET, 0x00 },
574 { R_BYPASS, R_BYPASS_USE_DSP },
575 ENDMARKER,
576};
577
578static const struct regval_list ov2640_uyvy_regs[] = {
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300579 { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_YUV422 },
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300580 { 0xd7, 0x01 },
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300581 { 0x33, 0xa0 },
582 { 0xe1, 0x67 },
583 { RESET, 0x00 },
584 { R_BYPASS, R_BYPASS_USE_DSP },
585 ENDMARKER,
586};
587
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300588static const struct regval_list ov2640_rgb565_be_regs[] = {
589 { IMAGE_MODE, IMAGE_MODE_RGB565 },
590 { 0xd7, 0x03 },
591 { RESET, 0x00 },
592 { R_BYPASS, R_BYPASS_USE_DSP },
593 ENDMARKER,
594};
595
596static const struct regval_list ov2640_rgb565_le_regs[] = {
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300597 { IMAGE_MODE, IMAGE_MODE_LBYTE_FIRST | IMAGE_MODE_RGB565 },
598 { 0xd7, 0x03 },
599 { RESET, 0x00 },
600 { R_BYPASS, R_BYPASS_USE_DSP },
601 ENDMARKER,
602};
603
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300604static u32 ov2640_codes[] = {
605 MEDIA_BUS_FMT_YUYV8_2X8,
606 MEDIA_BUS_FMT_UYVY8_2X8,
607 MEDIA_BUS_FMT_RGB565_2X8_BE,
608 MEDIA_BUS_FMT_RGB565_2X8_LE,
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300609};
610
611/*
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300612 * General functions
613 */
614static struct ov2640_priv *to_ov2640(const struct i2c_client *client)
615{
616 return container_of(i2c_get_clientdata(client), struct ov2640_priv,
617 subdev);
618}
619
620static int ov2640_write_array(struct i2c_client *client,
621 const struct regval_list *vals)
622{
623 int ret;
624
625 while ((vals->reg_num != 0xff) || (vals->value != 0xff)) {
626 ret = i2c_smbus_write_byte_data(client,
627 vals->reg_num, vals->value);
628 dev_vdbg(&client->dev, "array: 0x%02x, 0x%02x",
629 vals->reg_num, vals->value);
630
631 if (ret < 0)
632 return ret;
633 vals++;
634 }
635 return 0;
636}
637
638static int ov2640_mask_set(struct i2c_client *client,
639 u8 reg, u8 mask, u8 set)
640{
641 s32 val = i2c_smbus_read_byte_data(client, reg);
642 if (val < 0)
643 return val;
644
645 val &= ~mask;
646 val |= set & mask;
647
648 dev_vdbg(&client->dev, "masks: 0x%02x, 0x%02x", reg, val);
649
650 return i2c_smbus_write_byte_data(client, reg, val);
651}
652
653static int ov2640_reset(struct i2c_client *client)
654{
655 int ret;
656 const struct regval_list reset_seq[] = {
657 {BANK_SEL, BANK_SEL_SENS},
658 {COM7, COM7_SRST},
659 ENDMARKER,
660 };
661
662 ret = ov2640_write_array(client, reset_seq);
663 if (ret)
664 goto err;
665
666 msleep(5);
667err:
668 dev_dbg(&client->dev, "%s: (ret %d)", __func__, ret);
669 return ret;
670}
671
672/*
673 * soc_camera_ops functions
674 */
675static int ov2640_s_stream(struct v4l2_subdev *sd, int enable)
676{
677 return 0;
678}
679
Hans Verkuilf0266712011-09-07 05:43:05 -0300680static int ov2640_s_ctrl(struct v4l2_ctrl *ctrl)
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300681{
Hans Verkuilf0266712011-09-07 05:43:05 -0300682 struct v4l2_subdev *sd =
683 &container_of(ctrl->handler, struct ov2640_priv, hdl)->subdev;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300684 struct i2c_client *client = v4l2_get_subdevdata(sd);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300685 u8 val;
Frank Schäfera52eb6c2012-09-23 17:16:34 -0300686 int ret;
687
688 ret = i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
689 if (ret < 0)
690 return ret;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300691
692 switch (ctrl->id) {
693 case V4L2_CID_VFLIP:
Hans Verkuilf0266712011-09-07 05:43:05 -0300694 val = ctrl->val ? REG04_VFLIP_IMG : 0x00;
695 return ov2640_mask_set(client, REG04, REG04_VFLIP_IMG, val);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300696 case V4L2_CID_HFLIP:
Hans Verkuilf0266712011-09-07 05:43:05 -0300697 val = ctrl->val ? REG04_HFLIP_IMG : 0x00;
698 return ov2640_mask_set(client, REG04, REG04_HFLIP_IMG, val);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300699 }
700
Hans Verkuilf0266712011-09-07 05:43:05 -0300701 return -EINVAL;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300702}
703
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300704#ifdef CONFIG_VIDEO_ADV_DEBUG
705static int ov2640_g_register(struct v4l2_subdev *sd,
706 struct v4l2_dbg_register *reg)
707{
708 struct i2c_client *client = v4l2_get_subdevdata(sd);
709 int ret;
710
711 reg->size = 1;
712 if (reg->reg > 0xff)
713 return -EINVAL;
714
715 ret = i2c_smbus_read_byte_data(client, reg->reg);
716 if (ret < 0)
717 return ret;
718
719 reg->val = ret;
720
721 return 0;
722}
723
724static int ov2640_s_register(struct v4l2_subdev *sd,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300725 const struct v4l2_dbg_register *reg)
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300726{
727 struct i2c_client *client = v4l2_get_subdevdata(sd);
728
729 if (reg->reg > 0xff ||
730 reg->val > 0xff)
731 return -EINVAL;
732
733 return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
734}
735#endif
736
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -0300737static int ov2640_s_power(struct v4l2_subdev *sd, int on)
738{
739 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski25a34812012-12-21 08:11:48 -0300740 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -0300741 struct ov2640_priv *priv = to_ov2640(client);
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -0300742
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -0300743 return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -0300744}
745
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300746/* Select the nearest higher resolution for capture */
747static const struct ov2640_win_size *ov2640_select_win(u32 *width, u32 *height)
748{
749 int i, default_size = ARRAY_SIZE(ov2640_supported_win_sizes) - 1;
750
751 for (i = 0; i < ARRAY_SIZE(ov2640_supported_win_sizes); i++) {
752 if (ov2640_supported_win_sizes[i].width >= *width &&
753 ov2640_supported_win_sizes[i].height >= *height) {
754 *width = ov2640_supported_win_sizes[i].width;
755 *height = ov2640_supported_win_sizes[i].height;
756 return &ov2640_supported_win_sizes[i];
757 }
758 }
759
760 *width = ov2640_supported_win_sizes[default_size].width;
761 *height = ov2640_supported_win_sizes[default_size].height;
762 return &ov2640_supported_win_sizes[default_size];
763}
764
765static int ov2640_set_params(struct i2c_client *client, u32 *width, u32 *height,
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300766 u32 code)
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300767{
768 struct ov2640_priv *priv = to_ov2640(client);
769 const struct regval_list *selected_cfmt_regs;
770 int ret;
771
772 /* select win */
773 priv->win = ov2640_select_win(width, height);
774
775 /* select format */
776 priv->cfmt_code = 0;
777 switch (code) {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300778 case MEDIA_BUS_FMT_RGB565_2X8_BE:
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300779 dev_dbg(&client->dev, "%s: Selected cfmt RGB565 BE", __func__);
780 selected_cfmt_regs = ov2640_rgb565_be_regs;
781 break;
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300782 case MEDIA_BUS_FMT_RGB565_2X8_LE:
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300783 dev_dbg(&client->dev, "%s: Selected cfmt RGB565 LE", __func__);
784 selected_cfmt_regs = ov2640_rgb565_le_regs;
785 break;
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300786 case MEDIA_BUS_FMT_YUYV8_2X8:
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300787 dev_dbg(&client->dev, "%s: Selected cfmt YUYV (YUV422)", __func__);
788 selected_cfmt_regs = ov2640_yuyv_regs;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300789 break;
790 default:
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300791 case MEDIA_BUS_FMT_UYVY8_2X8:
Frank Schäferd1a49ea2012-09-23 15:28:45 -0300792 dev_dbg(&client->dev, "%s: Selected cfmt UYVY", __func__);
793 selected_cfmt_regs = ov2640_uyvy_regs;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300794 }
795
796 /* reset hardware */
797 ov2640_reset(client);
798
799 /* initialize the sensor with default data */
800 dev_dbg(&client->dev, "%s: Init default", __func__);
801 ret = ov2640_write_array(client, ov2640_init_regs);
802 if (ret < 0)
803 goto err;
804
805 /* select preamble */
806 dev_dbg(&client->dev, "%s: Set size to %s", __func__, priv->win->name);
807 ret = ov2640_write_array(client, ov2640_size_change_preamble_regs);
808 if (ret < 0)
809 goto err;
810
811 /* set size win */
812 ret = ov2640_write_array(client, priv->win->regs);
813 if (ret < 0)
814 goto err;
815
816 /* cfmt preamble */
817 dev_dbg(&client->dev, "%s: Set cfmt", __func__);
818 ret = ov2640_write_array(client, ov2640_format_change_preamble_regs);
819 if (ret < 0)
820 goto err;
821
822 /* set cfmt */
823 ret = ov2640_write_array(client, selected_cfmt_regs);
824 if (ret < 0)
825 goto err;
826
827 priv->cfmt_code = code;
828 *width = priv->win->width;
829 *height = priv->win->height;
830
831 return 0;
832
833err:
834 dev_err(&client->dev, "%s: Error %d", __func__, ret);
835 ov2640_reset(client);
836 priv->win = NULL;
837
838 return ret;
839}
840
841static int ov2640_g_fmt(struct v4l2_subdev *sd,
842 struct v4l2_mbus_framefmt *mf)
843{
844 struct i2c_client *client = v4l2_get_subdevdata(sd);
845 struct ov2640_priv *priv = to_ov2640(client);
846
847 if (!priv->win) {
Josh Wu650b1812014-11-25 05:54:28 -0300848 u32 width = SVGA_WIDTH, height = SVGA_HEIGHT;
Laurent Pinchartf9859832012-07-18 10:53:58 -0300849 priv->win = ov2640_select_win(&width, &height);
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300850 priv->cfmt_code = MEDIA_BUS_FMT_UYVY8_2X8;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300851 }
852
853 mf->width = priv->win->width;
854 mf->height = priv->win->height;
855 mf->code = priv->cfmt_code;
856
857 switch (mf->code) {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300858 case MEDIA_BUS_FMT_RGB565_2X8_BE:
859 case MEDIA_BUS_FMT_RGB565_2X8_LE:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300860 mf->colorspace = V4L2_COLORSPACE_SRGB;
861 break;
862 default:
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300863 case MEDIA_BUS_FMT_YUYV8_2X8:
864 case MEDIA_BUS_FMT_UYVY8_2X8:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300865 mf->colorspace = V4L2_COLORSPACE_JPEG;
866 }
867 mf->field = V4L2_FIELD_NONE;
868
869 return 0;
870}
871
872static int ov2640_s_fmt(struct v4l2_subdev *sd,
873 struct v4l2_mbus_framefmt *mf)
874{
875 struct i2c_client *client = v4l2_get_subdevdata(sd);
876 int ret;
877
878
879 switch (mf->code) {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300880 case MEDIA_BUS_FMT_RGB565_2X8_BE:
881 case MEDIA_BUS_FMT_RGB565_2X8_LE:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300882 mf->colorspace = V4L2_COLORSPACE_SRGB;
883 break;
884 default:
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300885 mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
886 case MEDIA_BUS_FMT_YUYV8_2X8:
887 case MEDIA_BUS_FMT_UYVY8_2X8:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300888 mf->colorspace = V4L2_COLORSPACE_JPEG;
889 }
890
891 ret = ov2640_set_params(client, &mf->width, &mf->height, mf->code);
892
893 return ret;
894}
895
896static int ov2640_try_fmt(struct v4l2_subdev *sd,
897 struct v4l2_mbus_framefmt *mf)
898{
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300899 /*
Mauro Carvalho Chehabba400892012-10-27 16:16:34 -0300900 * select suitable win, but don't store it
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300901 */
Mauro Carvalho Chehabba400892012-10-27 16:16:34 -0300902 ov2640_select_win(&mf->width, &mf->height);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300903
904 mf->field = V4L2_FIELD_NONE;
905
906 switch (mf->code) {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300907 case MEDIA_BUS_FMT_RGB565_2X8_BE:
908 case MEDIA_BUS_FMT_RGB565_2X8_LE:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300909 mf->colorspace = V4L2_COLORSPACE_SRGB;
910 break;
911 default:
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300912 mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
913 case MEDIA_BUS_FMT_YUYV8_2X8:
914 case MEDIA_BUS_FMT_UYVY8_2X8:
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300915 mf->colorspace = V4L2_COLORSPACE_JPEG;
916 }
917
918 return 0;
919}
920
921static int ov2640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300922 u32 *code)
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300923{
924 if (index >= ARRAY_SIZE(ov2640_codes))
925 return -EINVAL;
926
927 *code = ov2640_codes[index];
928 return 0;
929}
930
931static int ov2640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
932{
933 a->c.left = 0;
934 a->c.top = 0;
Josh Wu650b1812014-11-25 05:54:28 -0300935 a->c.width = UXGA_WIDTH;
936 a->c.height = UXGA_HEIGHT;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300937 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
938
939 return 0;
940}
941
942static int ov2640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
943{
944 a->bounds.left = 0;
945 a->bounds.top = 0;
Josh Wu650b1812014-11-25 05:54:28 -0300946 a->bounds.width = UXGA_WIDTH;
947 a->bounds.height = UXGA_HEIGHT;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300948 a->defrect = a->bounds;
949 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
950 a->pixelaspect.numerator = 1;
951 a->pixelaspect.denominator = 1;
952
953 return 0;
954}
955
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -0300956static int ov2640_video_probe(struct i2c_client *client)
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300957{
958 struct ov2640_priv *priv = to_ov2640(client);
959 u8 pid, ver, midh, midl;
960 const char *devname;
961 int ret;
962
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300963 ret = ov2640_s_power(&priv->subdev, 1);
964 if (ret < 0)
965 return ret;
966
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300967 /*
968 * check and show product ID and manufacturer ID
969 */
970 i2c_smbus_write_byte_data(client, BANK_SEL, BANK_SEL_SENS);
971 pid = i2c_smbus_read_byte_data(client, PID);
972 ver = i2c_smbus_read_byte_data(client, VER);
973 midh = i2c_smbus_read_byte_data(client, MIDH);
974 midl = i2c_smbus_read_byte_data(client, MIDL);
975
976 switch (VERSION(pid, ver)) {
977 case PID_OV2640:
978 devname = "ov2640";
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300979 break;
980 default:
981 dev_err(&client->dev,
982 "Product ID error %x:%x\n", pid, ver);
983 ret = -ENODEV;
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300984 goto done;
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300985 }
986
987 dev_info(&client->dev,
988 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
989 devname, pid, ver, midh, midl);
990
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300991 ret = v4l2_ctrl_handler_setup(&priv->hdl);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300992
Laurent Pinchart4bbc6d52012-07-18 10:54:04 -0300993done:
994 ov2640_s_power(&priv->subdev, 0);
Alberto Panizzo3153ac92010-12-02 11:53:28 -0300995 return ret;
996}
997
Hans Verkuilf0266712011-09-07 05:43:05 -0300998static const struct v4l2_ctrl_ops ov2640_ctrl_ops = {
999 .s_ctrl = ov2640_s_ctrl,
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001000};
1001
1002static struct v4l2_subdev_core_ops ov2640_subdev_core_ops = {
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001003#ifdef CONFIG_VIDEO_ADV_DEBUG
1004 .g_register = ov2640_g_register,
1005 .s_register = ov2640_s_register,
1006#endif
Laurent Pinchart4ec10ba2012-07-20 10:19:50 -03001007 .s_power = ov2640_s_power,
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001008};
1009
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -03001010static int ov2640_g_mbus_config(struct v4l2_subdev *sd,
1011 struct v4l2_mbus_config *cfg)
1012{
1013 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetski25a34812012-12-21 08:11:48 -03001014 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -03001015
1016 cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
1017 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
1018 V4L2_MBUS_DATA_ACTIVE_HIGH;
1019 cfg->type = V4L2_MBUS_PARALLEL;
Guennadi Liakhovetski25a34812012-12-21 08:11:48 -03001020 cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -03001021
1022 return 0;
1023}
1024
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001025static struct v4l2_subdev_video_ops ov2640_subdev_video_ops = {
1026 .s_stream = ov2640_s_stream,
1027 .g_mbus_fmt = ov2640_g_fmt,
1028 .s_mbus_fmt = ov2640_s_fmt,
1029 .try_mbus_fmt = ov2640_try_fmt,
1030 .cropcap = ov2640_cropcap,
1031 .g_crop = ov2640_g_crop,
1032 .enum_mbus_fmt = ov2640_enum_fmt,
Guennadi Liakhovetskid4630032011-07-26 11:59:53 -03001033 .g_mbus_config = ov2640_g_mbus_config,
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001034};
1035
1036static struct v4l2_subdev_ops ov2640_subdev_ops = {
1037 .core = &ov2640_subdev_core_ops,
1038 .video = &ov2640_subdev_video_ops,
1039};
1040
1041/*
1042 * i2c_driver functions
1043 */
1044static int ov2640_probe(struct i2c_client *client,
1045 const struct i2c_device_id *did)
1046{
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -03001047 struct ov2640_priv *priv;
Guennadi Liakhovetski25a34812012-12-21 08:11:48 -03001048 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -03001049 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1050 int ret;
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001051
Guennadi Liakhovetski25a34812012-12-21 08:11:48 -03001052 if (!ssdd) {
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001053 dev_err(&adapter->dev,
1054 "OV2640: Missing platform_data for driver\n");
1055 return -EINVAL;
1056 }
1057
1058 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1059 dev_err(&adapter->dev,
1060 "OV2640: I2C-Adapter doesn't support SMBUS\n");
1061 return -EIO;
1062 }
1063
Guennadi Liakhovetski70e176a2012-12-21 10:28:43 -03001064 priv = devm_kzalloc(&client->dev, sizeof(struct ov2640_priv), GFP_KERNEL);
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001065 if (!priv) {
1066 dev_err(&adapter->dev,
1067 "Failed to allocate memory for private data!\n");
1068 return -ENOMEM;
1069 }
1070
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001071 v4l2_i2c_subdev_init(&priv->subdev, client, &ov2640_subdev_ops);
Hans Verkuilf0266712011-09-07 05:43:05 -03001072 v4l2_ctrl_handler_init(&priv->hdl, 2);
1073 v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
1074 V4L2_CID_VFLIP, 0, 1, 1, 0);
1075 v4l2_ctrl_new_std(&priv->hdl, &ov2640_ctrl_ops,
1076 V4L2_CID_HFLIP, 0, 1, 1, 0);
1077 priv->subdev.ctrl_handler = &priv->hdl;
Guennadi Liakhovetski70e176a2012-12-21 10:28:43 -03001078 if (priv->hdl.error)
1079 return priv->hdl.error;
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001080
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -03001081 priv->clk = v4l2_clk_get(&client->dev, "mclk");
1082 if (IS_ERR(priv->clk)) {
1083 ret = PTR_ERR(priv->clk);
1084 goto eclkget;
1085 }
1086
Guennadi Liakhovetski14178aa2011-09-21 15:16:30 -03001087 ret = ov2640_video_probe(client);
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -03001088 if (ret) {
1089 v4l2_clk_put(priv->clk);
1090eclkget:
Hans Verkuilf0266712011-09-07 05:43:05 -03001091 v4l2_ctrl_handler_free(&priv->hdl);
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -03001092 } else {
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001093 dev_info(&adapter->dev, "OV2640 Probed\n");
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -03001094 }
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001095
1096 return ret;
1097}
1098
1099static int ov2640_remove(struct i2c_client *client)
1100{
1101 struct ov2640_priv *priv = to_ov2640(client);
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001102
Guennadi Liakhovetski9aea4702012-12-21 13:01:55 -03001103 v4l2_clk_put(priv->clk);
Hans Verkuilf0266712011-09-07 05:43:05 -03001104 v4l2_device_unregister_subdev(&priv->subdev);
1105 v4l2_ctrl_handler_free(&priv->hdl);
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001106 return 0;
1107}
1108
1109static const struct i2c_device_id ov2640_id[] = {
1110 { "ov2640", 0 },
1111 { }
1112};
1113MODULE_DEVICE_TABLE(i2c, ov2640_id);
1114
1115static struct i2c_driver ov2640_i2c_driver = {
1116 .driver = {
1117 .name = "ov2640",
1118 },
1119 .probe = ov2640_probe,
1120 .remove = ov2640_remove,
1121 .id_table = ov2640_id,
1122};
1123
Axel Linc6e8d862012-02-12 06:56:32 -03001124module_i2c_driver(ov2640_i2c_driver);
Alberto Panizzo3153ac92010-12-02 11:53:28 -03001125
1126MODULE_DESCRIPTION("SoC Camera driver for Omni Vision 2640 sensor");
1127MODULE_AUTHOR("Alberto Panizzo");
1128MODULE_LICENSE("GPL v2");