blob: 386c493362f37f2bb73769a10ca2c314545c348e [file] [log] [blame]
Jonathan Corbet111f3352006-11-04 09:26:00 -03001/*
2 * A V4L2 driver for OmniVision OV7670 cameras.
3 *
4 * Copyright 2006 One Laptop Per Child Association, Inc. Written
5 * by Jonathan Corbet with substantial inspiration from Mark
6 * McClelland's ovcamchip code.
7 *
Jonathan Corbet77d51402007-03-22 19:44:17 -03008 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
9 *
Jonathan Corbet111f3352006-11-04 09:26:00 -030010 * This file may be distributed under the terms of the GNU General
11 * Public License, version 2.
12 */
Hans Verkuil0a024d62016-08-16 16:00:57 -030013#include <linux/clk.h>
Jonathan Corbet111f3352006-11-04 09:26:00 -030014#include <linux/init.h>
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hans Verkuil14386c22009-03-18 13:01:06 -030017#include <linux/i2c.h>
Jonathan Corbet111f3352006-11-04 09:26:00 -030018#include <linux/delay.h>
Mauro Carvalho Chehab7e0a16f2009-03-10 05:31:34 -030019#include <linux/videodev2.h>
Hans Verkuila0c41642016-12-12 12:08:18 -020020#include <linux/gpio.h>
21#include <linux/gpio/consumer.h>
Hans Verkuil14386c22009-03-18 13:01:06 -030022#include <media/v4l2-device.h>
Akinobu Mita7852adf2018-11-12 11:00:53 -050023#include <media/v4l2-event.h>
Javier Martin492959c2013-01-29 07:31:17 -030024#include <media/v4l2-ctrls.h>
Jacopo Mondi01b84442018-01-24 04:30:50 -050025#include <media/v4l2-fwnode.h>
Hans Verkuil959f3bd2010-05-08 18:28:41 -030026#include <media/v4l2-mediabus.h>
Axel Lin4721b3e2014-08-09 02:58:40 -030027#include <media/v4l2-image-sizes.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020028#include <media/i2c/ov7670.h>
Jonathan Corbet111f3352006-11-04 09:26:00 -030029
Dave Jones5e614472006-12-12 20:15:40 +010030MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
Jonathan Corbet111f3352006-11-04 09:26:00 -030031MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors");
32MODULE_LICENSE("GPL");
33
Rusty Russell90ab5ee2012-01-13 09:32:20 +103034static bool debug;
Hans Verkuil14386c22009-03-18 13:01:06 -030035module_param(debug, bool, 0644);
36MODULE_PARM_DESC(debug, "Debug level (0-1)");
37
Jonathan Corbet111f3352006-11-04 09:26:00 -030038/*
Jonathan Corbet111f3352006-11-04 09:26:00 -030039 * The 7670 sits on i2c with ID 0x42
40 */
41#define OV7670_I2C_ADDR 0x42
42
Javier Martinf6dd9272013-01-29 07:16:59 -030043#define PLL_FACTOR 4
44
Jonathan Corbet111f3352006-11-04 09:26:00 -030045/* Registers */
46#define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
47#define REG_BLUE 0x01 /* blue gain */
48#define REG_RED 0x02 /* red gain */
49#define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
50#define REG_COM1 0x04 /* Control 1 */
51#define COM1_CCIR656 0x40 /* CCIR656 enable */
52#define REG_BAVE 0x05 /* U/B Average level */
53#define REG_GbAVE 0x06 /* Y/Gb Average level */
54#define REG_AECHH 0x07 /* AEC MS 5 bits */
55#define REG_RAVE 0x08 /* V/R Average level */
56#define REG_COM2 0x09 /* Control 2 */
57#define COM2_SSLEEP 0x10 /* Soft sleep mode */
58#define REG_PID 0x0a /* Product ID MSB */
59#define REG_VER 0x0b /* Product ID LSB */
60#define REG_COM3 0x0c /* Control 3 */
61#define COM3_SWAP 0x40 /* Byte swap */
62#define COM3_SCALEEN 0x08 /* Enable scaling */
63#define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
64#define REG_COM4 0x0d /* Control 4 */
65#define REG_COM5 0x0e /* All "reserved" */
66#define REG_COM6 0x0f /* Control 6 */
67#define REG_AECH 0x10 /* More bits of AEC value */
68#define REG_CLKRC 0x11 /* Clocl control */
69#define CLK_EXT 0x40 /* Use external clock directly */
70#define CLK_SCALE 0x3f /* Mask for internal clock scale */
71#define REG_COM7 0x12 /* Control 7 */
72#define COM7_RESET 0x80 /* Register reset */
73#define COM7_FMT_MASK 0x38
74#define COM7_FMT_VGA 0x00
75#define COM7_FMT_CIF 0x20 /* CIF format */
76#define COM7_FMT_QVGA 0x10 /* QVGA format */
77#define COM7_FMT_QCIF 0x08 /* QCIF format */
78#define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
79#define COM7_YUV 0x00 /* YUV */
80#define COM7_BAYER 0x01 /* Bayer format */
81#define COM7_PBAYER 0x05 /* "Processed bayer" */
82#define REG_COM8 0x13 /* Control 8 */
83#define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
84#define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
85#define COM8_BFILT 0x20 /* Band filter enable */
86#define COM8_AGC 0x04 /* Auto gain enable */
87#define COM8_AWB 0x02 /* White balance enable */
88#define COM8_AEC 0x01 /* Auto exposure enable */
89#define REG_COM9 0x14 /* Control 9 - gain ceiling */
90#define REG_COM10 0x15 /* Control 10 */
91#define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
92#define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
93#define COM10_HREF_REV 0x08 /* Reverse HREF */
94#define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
95#define COM10_VS_NEG 0x02 /* VSYNC negative */
96#define COM10_HS_NEG 0x01 /* HSYNC negative */
97#define REG_HSTART 0x17 /* Horiz start high bits */
98#define REG_HSTOP 0x18 /* Horiz stop high bits */
99#define REG_VSTART 0x19 /* Vert start high bits */
100#define REG_VSTOP 0x1a /* Vert stop high bits */
101#define REG_PSHFT 0x1b /* Pixel delay after HREF */
102#define REG_MIDH 0x1c /* Manuf. ID high */
103#define REG_MIDL 0x1d /* Manuf. ID low */
104#define REG_MVFP 0x1e /* Mirror / vflip */
105#define MVFP_MIRROR 0x20 /* Mirror image */
106#define MVFP_FLIP 0x10 /* Vertical flip */
107
108#define REG_AEW 0x24 /* AGC upper limit */
109#define REG_AEB 0x25 /* AGC lower limit */
110#define REG_VPT 0x26 /* AGC/AEC fast mode op region */
111#define REG_HSYST 0x30 /* HSYNC rising edge delay */
112#define REG_HSYEN 0x31 /* HSYNC falling edge delay */
113#define REG_HREF 0x32 /* HREF pieces */
114#define REG_TSLB 0x3a /* lots of stuff */
115#define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
116#define REG_COM11 0x3b /* Control 11 */
117#define COM11_NIGHT 0x80 /* NIght mode enable */
118#define COM11_NMFR 0x60 /* Two bit NM frame rate */
119#define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
120#define COM11_50HZ 0x08 /* Manual 50Hz select */
121#define COM11_EXP 0x02
122#define REG_COM12 0x3c /* Control 12 */
123#define COM12_HREF 0x80 /* HREF always */
124#define REG_COM13 0x3d /* Control 13 */
125#define COM13_GAMMA 0x80 /* Gamma enable */
126#define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
127#define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
128#define REG_COM14 0x3e /* Control 14 */
129#define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
130#define REG_EDGE 0x3f /* Edge enhancement factor */
131#define REG_COM15 0x40 /* Control 15 */
132#define COM15_R10F0 0x00 /* Data range 10 to F0 */
133#define COM15_R01FE 0x80 /* 01 to FE */
134#define COM15_R00FF 0xc0 /* 00 to FF */
135#define COM15_RGB565 0x10 /* RGB565 output */
136#define COM15_RGB555 0x30 /* RGB555 output */
137#define REG_COM16 0x41 /* Control 16 */
138#define COM16_AWBGAIN 0x08 /* AWB gain enable */
139#define REG_COM17 0x42 /* Control 17 */
140#define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
141#define COM17_CBAR 0x08 /* DSP Color bar */
142
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300143/*
144 * This matrix defines how the colors are generated, must be
145 * tweaked to adjust hue and saturation.
146 *
147 * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
148 *
149 * They are nine-bit signed quantities, with the sign bit
150 * stored in 0x58. Sign for v-red is bit 0, and up from there.
151 */
152#define REG_CMATRIX_BASE 0x4f
153#define CMATRIX_LEN 6
154#define REG_CMATRIX_SIGN 0x58
155
156
Jonathan Corbet111f3352006-11-04 09:26:00 -0300157#define REG_BRIGHT 0x55 /* Brightness */
158#define REG_CONTRAS 0x56 /* Contrast control */
159
160#define REG_GFIX 0x69 /* Fix gain control */
161
Javier Martinf6dd9272013-01-29 07:16:59 -0300162#define REG_DBLV 0x6b /* PLL control an debugging */
Jacopo Mondi61da76b2017-12-29 07:22:26 -0500163#define DBLV_BYPASS 0x0a /* Bypass PLL */
164#define DBLV_X4 0x4a /* clock x4 */
165#define DBLV_X6 0x8a /* clock x6 */
166#define DBLV_X8 0xca /* clock x8 */
Javier Martinf6dd9272013-01-29 07:16:59 -0300167
Akinobu Mitab48d9082017-11-24 09:40:45 -0500168#define REG_SCALING_XSC 0x70 /* Test pattern and horizontal scale factor */
169#define TEST_PATTTERN_0 0x80
170#define REG_SCALING_YSC 0x71 /* Test pattern and vertical scale factor */
171#define TEST_PATTTERN_1 0x80
172
Jonathan Corbet585553e2007-03-25 11:38:21 -0300173#define REG_REG76 0x76 /* OV's name */
174#define R76_BLKPCOR 0x80 /* Black pixel correction enable */
175#define R76_WHTPCOR 0x40 /* White pixel correction enable */
176
Jonathan Corbet111f3352006-11-04 09:26:00 -0300177#define REG_RGB444 0x8c /* RGB 444 control */
178#define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
179#define R444_RGBX 0x01 /* Empty nibble at end */
180
181#define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
182#define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
183
184#define REG_BD50MAX 0xa5 /* 50hz banding step limit */
185#define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
186#define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
187#define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
188#define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
189#define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
190#define REG_BD60MAX 0xab /* 60hz banding step limit */
191
Javier Martind058e232013-01-29 07:12:13 -0300192enum ov7670_model {
193 MODEL_OV7670 = 0,
194 MODEL_OV7675,
195};
196
197struct ov7670_win_size {
198 int width;
199 int height;
200 unsigned char com7_bit;
201 int hstart; /* Start/stop values for the camera. Note */
202 int hstop; /* that they do not always make complete */
203 int vstart; /* sense to humans, but evidently the sensor */
204 int vstop; /* will do the right thing... */
205 struct regval_list *regs; /* Regs to tweak */
206};
207
208struct ov7670_devtype {
209 /* formats supported for each model */
210 struct ov7670_win_size *win_sizes;
211 unsigned int n_win_sizes;
Javier Martinf6dd9272013-01-29 07:16:59 -0300212 /* callbacks for frame rate control */
213 int (*set_framerate)(struct v4l2_subdev *, struct v4l2_fract *);
214 void (*get_framerate)(struct v4l2_subdev *, struct v4l2_fract *);
Javier Martind058e232013-01-29 07:12:13 -0300215};
Jonathan Corbet111f3352006-11-04 09:26:00 -0300216
217/*
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300218 * Information we maintain about a known sensor.
219 */
220struct ov7670_format_struct; /* coming later */
221struct ov7670_info {
Hans Verkuil14386c22009-03-18 13:01:06 -0300222 struct v4l2_subdev sd;
Wenyou Yangd94a26f2017-10-16 05:14:25 +0200223#if defined(CONFIG_MEDIA_CONTROLLER)
224 struct media_pad pad;
225#endif
Javier Martin492959c2013-01-29 07:31:17 -0300226 struct v4l2_ctrl_handler hdl;
227 struct {
228 /* gain cluster */
229 struct v4l2_ctrl *auto_gain;
230 struct v4l2_ctrl *gain;
231 };
232 struct {
233 /* exposure cluster */
234 struct v4l2_ctrl *auto_exposure;
235 struct v4l2_ctrl *exposure;
236 };
237 struct {
238 /* saturation/hue cluster */
239 struct v4l2_ctrl *saturation;
240 struct v4l2_ctrl *hue;
241 };
Wenyou Yangc0662dd2017-10-16 05:14:26 +0200242 struct v4l2_mbus_framefmt format;
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300243 struct ov7670_format_struct *fmt; /* Current format */
Lubomir Rintel5556ab22019-01-15 06:54:47 -0200244 struct ov7670_win_size *wsize;
Hans Verkuil0a024d62016-08-16 16:00:57 -0300245 struct clk *clk;
Hans Verkuila0c41642016-12-12 12:08:18 -0200246 struct gpio_desc *resetb_gpio;
247 struct gpio_desc *pwdn_gpio;
Jacopo Mondi01b84442018-01-24 04:30:50 -0500248 unsigned int mbus_config; /* Media bus configuration flags */
Daniel Drake75e2bda2010-10-19 18:24:05 -0300249 int min_width; /* Filter out smaller sizes */
250 int min_height; /* Filter out smaller sizes */
251 int clock_speed; /* External clock speed (MHz) */
Jonathan Corbetd8d20152009-12-20 11:39:47 -0300252 u8 clkrc; /* Clock divider value */
Daniel Drake75e2bda2010-10-19 18:24:05 -0300253 bool use_smbus; /* Use smbus I/O instead of I2C */
Javier Martin04ee6d92013-01-29 07:23:42 -0300254 bool pll_bypass;
Javier Martinee952582013-01-29 07:26:38 -0300255 bool pclk_hb_disable;
Javier Martind058e232013-01-29 07:12:13 -0300256 const struct ov7670_devtype *devtype; /* Device specifics */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300257};
258
Hans Verkuil14386c22009-03-18 13:01:06 -0300259static inline struct ov7670_info *to_state(struct v4l2_subdev *sd)
260{
261 return container_of(sd, struct ov7670_info, sd);
262}
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300263
Javier Martin492959c2013-01-29 07:31:17 -0300264static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
265{
266 return &container_of(ctrl->handler, struct ov7670_info, hdl)->sd;
267}
268
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300269
270
271/*
Jonathan Corbet111f3352006-11-04 09:26:00 -0300272 * The default register settings, as obtained from OmniVision. There
273 * is really no making sense of most of these - lots of "reserved" values
274 * and such.
275 *
276 * These settings give VGA YUYV.
277 */
278
279struct regval_list {
280 unsigned char reg_num;
281 unsigned char value;
282};
283
284static struct regval_list ov7670_default_regs[] = {
285 { REG_COM7, COM7_RESET },
286/*
287 * Clock scale: 3 = 15fps
288 * 2 = 20fps
289 * 1 = 30fps
290 */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300291 { REG_CLKRC, 0x1 }, /* OV: clock scale (30 fps) */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300292 { REG_TSLB, 0x04 }, /* OV */
293 { REG_COM7, 0 }, /* VGA */
294 /*
295 * Set the hardware window. These values from OV don't entirely
296 * make sense - hstop is less than hstart. But they work...
297 */
298 { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },
299 { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },
300 { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },
301
302 { REG_COM3, 0 }, { REG_COM14, 0 },
303 /* Mystery scaling numbers */
Akinobu Mitab48d9082017-11-24 09:40:45 -0500304 { REG_SCALING_XSC, 0x3a },
305 { REG_SCALING_YSC, 0x35 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300306 { 0x72, 0x11 }, { 0x73, 0xf0 },
307 { 0xa2, 0x02 }, { REG_COM10, 0x0 },
308
309 /* Gamma curve values */
310 { 0x7a, 0x20 }, { 0x7b, 0x10 },
311 { 0x7c, 0x1e }, { 0x7d, 0x35 },
312 { 0x7e, 0x5a }, { 0x7f, 0x69 },
313 { 0x80, 0x76 }, { 0x81, 0x80 },
314 { 0x82, 0x88 }, { 0x83, 0x8f },
315 { 0x84, 0x96 }, { 0x85, 0xa3 },
316 { 0x86, 0xaf }, { 0x87, 0xc4 },
317 { 0x88, 0xd7 }, { 0x89, 0xe8 },
318
319 /* AGC and AEC parameters. Note we start by disabling those features,
320 then turn them only after tweaking the values. */
321 { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT },
322 { REG_GAIN, 0 }, { REG_AECH, 0 },
323 { REG_COM4, 0x40 }, /* magic reserved bit */
324 { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
325 { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },
326 { REG_AEW, 0x95 }, { REG_AEB, 0x33 },
327 { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },
328 { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */
329 { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },
330 { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },
331 { REG_HAECC7, 0x94 },
332 { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC },
333
334 /* Almost all of these are magic "reserved" values. */
335 { REG_COM5, 0x61 }, { REG_COM6, 0x4b },
Jonathan Corbet7f7b12f2007-03-25 11:36:42 -0300336 { 0x16, 0x02 }, { REG_MVFP, 0x07 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300337 { 0x21, 0x02 }, { 0x22, 0x91 },
338 { 0x29, 0x07 }, { 0x33, 0x0b },
339 { 0x35, 0x0b }, { 0x37, 0x1d },
340 { 0x38, 0x71 }, { 0x39, 0x2a },
341 { REG_COM12, 0x78 }, { 0x4d, 0x40 },
342 { 0x4e, 0x20 }, { REG_GFIX, 0 },
343 { 0x6b, 0x4a }, { 0x74, 0x10 },
344 { 0x8d, 0x4f }, { 0x8e, 0 },
345 { 0x8f, 0 }, { 0x90, 0 },
346 { 0x91, 0 }, { 0x96, 0 },
347 { 0x9a, 0 }, { 0xb0, 0x84 },
348 { 0xb1, 0x0c }, { 0xb2, 0x0e },
349 { 0xb3, 0x82 }, { 0xb8, 0x0a },
350
351 /* More reserved magic, some of which tweaks white balance */
352 { 0x43, 0x0a }, { 0x44, 0xf0 },
353 { 0x45, 0x34 }, { 0x46, 0x58 },
354 { 0x47, 0x28 }, { 0x48, 0x3a },
355 { 0x59, 0x88 }, { 0x5a, 0x88 },
356 { 0x5b, 0x44 }, { 0x5c, 0x67 },
357 { 0x5d, 0x49 }, { 0x5e, 0x0e },
358 { 0x6c, 0x0a }, { 0x6d, 0x55 },
359 { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */
360 { 0x6a, 0x40 }, { REG_BLUE, 0x40 },
361 { REG_RED, 0x60 },
362 { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB },
363
364 /* Matrix coefficients */
365 { 0x4f, 0x80 }, { 0x50, 0x80 },
366 { 0x51, 0 }, { 0x52, 0x22 },
367 { 0x53, 0x5e }, { 0x54, 0x80 },
368 { 0x58, 0x9e },
369
370 { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },
371 { 0x75, 0x05 }, { 0x76, 0xe1 },
372 { 0x4c, 0 }, { 0x77, 0x01 },
373 { REG_COM13, 0xc3 }, { 0x4b, 0x09 },
374 { 0xc9, 0x60 }, { REG_COM16, 0x38 },
375 { 0x56, 0x40 },
376
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -0300377 { 0x34, 0x11 }, { REG_COM11, COM11_EXP|COM11_HZAUTO },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300378 { 0xa4, 0x88 }, { 0x96, 0 },
379 { 0x97, 0x30 }, { 0x98, 0x20 },
380 { 0x99, 0x30 }, { 0x9a, 0x84 },
381 { 0x9b, 0x29 }, { 0x9c, 0x03 },
382 { 0x9d, 0x4c }, { 0x9e, 0x3f },
383 { 0x78, 0x04 },
384
385 /* Extra-weird stuff. Some sort of multiplexor register */
386 { 0x79, 0x01 }, { 0xc8, 0xf0 },
387 { 0x79, 0x0f }, { 0xc8, 0x00 },
388 { 0x79, 0x10 }, { 0xc8, 0x7e },
389 { 0x79, 0x0a }, { 0xc8, 0x80 },
390 { 0x79, 0x0b }, { 0xc8, 0x01 },
391 { 0x79, 0x0c }, { 0xc8, 0x0f },
392 { 0x79, 0x0d }, { 0xc8, 0x20 },
393 { 0x79, 0x09 }, { 0xc8, 0x80 },
394 { 0x79, 0x02 }, { 0xc8, 0xc0 },
395 { 0x79, 0x03 }, { 0xc8, 0x40 },
396 { 0x79, 0x05 }, { 0xc8, 0x30 },
397 { 0x79, 0x26 },
398
Jonathan Corbet111f3352006-11-04 09:26:00 -0300399 { 0xff, 0xff }, /* END MARKER */
400};
401
402
403/*
404 * Here we'll try to encapsulate the changes for just the output
405 * video format.
406 *
407 * RGB656 and YUV422 come from OV; RGB444 is homebrewed.
408 *
409 * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why.
410 */
411
412
413static struct regval_list ov7670_fmt_yuv422[] = {
414 { REG_COM7, 0x0 }, /* Selects YUV mode */
415 { REG_RGB444, 0 }, /* No RGB444 please */
Jonathan Corbet97693f92010-03-21 17:33:50 -0300416 { REG_COM1, 0 }, /* CCIR601 */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300417 { REG_COM15, COM15_R00FF },
Javier Martinc01b7422012-10-30 12:04:23 -0300418 { REG_COM9, 0x48 }, /* 32x gain ceiling; 0x8 is reserved bit */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500419 { 0x4f, 0x80 }, /* "matrix coefficient 1" */
420 { 0x50, 0x80 }, /* "matrix coefficient 2" */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300421 { 0x51, 0 }, /* vb */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500422 { 0x52, 0x22 }, /* "matrix coefficient 4" */
423 { 0x53, 0x5e }, /* "matrix coefficient 5" */
424 { 0x54, 0x80 }, /* "matrix coefficient 6" */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300425 { REG_COM13, COM13_GAMMA|COM13_UVSAT },
426 { 0xff, 0xff },
427};
428
429static struct regval_list ov7670_fmt_rgb565[] = {
430 { REG_COM7, COM7_RGB }, /* Selects RGB mode */
431 { REG_RGB444, 0 }, /* No RGB444 please */
Jonathan Corbet97693f92010-03-21 17:33:50 -0300432 { REG_COM1, 0x0 }, /* CCIR601 */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300433 { REG_COM15, COM15_RGB565 },
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500434 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
435 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
436 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300437 { 0x51, 0 }, /* vb */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500438 { 0x52, 0x3d }, /* "matrix coefficient 4" */
439 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
440 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300441 { REG_COM13, COM13_GAMMA|COM13_UVSAT },
442 { 0xff, 0xff },
443};
444
445static struct regval_list ov7670_fmt_rgb444[] = {
446 { REG_COM7, COM7_RGB }, /* Selects RGB mode */
447 { REG_RGB444, R444_ENABLE }, /* Enable xxxxrrrr ggggbbbb */
Jonathan Corbet97693f92010-03-21 17:33:50 -0300448 { REG_COM1, 0x0 }, /* CCIR601 */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300449 { REG_COM15, COM15_R01FE|COM15_RGB565 }, /* Data range needed? */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500450 { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
451 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
452 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300453 { 0x51, 0 }, /* vb */
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500454 { 0x52, 0x3d }, /* "matrix coefficient 4" */
455 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
456 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300457 { REG_COM13, COM13_GAMMA|COM13_UVSAT|0x2 }, /* Magic rsvd bit */
458 { 0xff, 0xff },
459};
460
Jonathan Corbet585553e2007-03-25 11:38:21 -0300461static struct regval_list ov7670_fmt_raw[] = {
462 { REG_COM7, COM7_BAYER },
463 { REG_COM13, 0x08 }, /* No gamma, magic rsvd bit */
464 { REG_COM16, 0x3d }, /* Edge enhancement, denoise */
465 { REG_REG76, 0xe1 }, /* Pix correction, magic rsvd */
466 { 0xff, 0xff },
467};
Jonathan Corbet111f3352006-11-04 09:26:00 -0300468
469
470
471/*
472 * Low-level register I/O.
Jonathan Corbet46714202010-03-19 13:16:28 -0300473 *
474 * Note that there are two versions of these. On the XO 1, the
475 * i2c controller only does SMBUS, so that's what we use. The
476 * ov7670 is not really an SMBUS device, though, so the communication
477 * is not always entirely reliable.
478 */
Daniel Drake75e2bda2010-10-19 18:24:05 -0300479static int ov7670_read_smbus(struct v4l2_subdev *sd, unsigned char reg,
Jonathan Corbet46714202010-03-19 13:16:28 -0300480 unsigned char *value)
481{
482 struct i2c_client *client = v4l2_get_subdevdata(sd);
483 int ret;
484
485 ret = i2c_smbus_read_byte_data(client, reg);
486 if (ret >= 0) {
487 *value = (unsigned char)ret;
488 ret = 0;
489 }
490 return ret;
491}
492
493
Daniel Drake75e2bda2010-10-19 18:24:05 -0300494static int ov7670_write_smbus(struct v4l2_subdev *sd, unsigned char reg,
Jonathan Corbet46714202010-03-19 13:16:28 -0300495 unsigned char value)
496{
497 struct i2c_client *client = v4l2_get_subdevdata(sd);
498 int ret = i2c_smbus_write_byte_data(client, reg, value);
499
500 if (reg == REG_COM7 && (value & COM7_RESET))
501 msleep(5); /* Wait for reset to run */
502 return ret;
503}
504
Jonathan Corbet46714202010-03-19 13:16:28 -0300505/*
506 * On most platforms, we'd rather do straight i2c I/O.
Jonathan Corbet111f3352006-11-04 09:26:00 -0300507 */
Daniel Drake75e2bda2010-10-19 18:24:05 -0300508static int ov7670_read_i2c(struct v4l2_subdev *sd, unsigned char reg,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300509 unsigned char *value)
510{
Hans Verkuil14386c22009-03-18 13:01:06 -0300511 struct i2c_client *client = v4l2_get_subdevdata(sd);
Jonathan Corbet2bf7de42010-02-28 21:02:55 -0300512 u8 data = reg;
513 struct i2c_msg msg;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300514 int ret;
515
Jonathan Corbet2bf7de42010-02-28 21:02:55 -0300516 /*
517 * Send out the register address...
518 */
519 msg.addr = client->addr;
520 msg.flags = 0;
521 msg.len = 1;
522 msg.buf = &data;
523 ret = i2c_transfer(client->adapter, &msg, 1);
524 if (ret < 0) {
525 printk(KERN_ERR "Error %d on register write\n", ret);
526 return ret;
527 }
528 /*
529 * ...then read back the result.
530 */
531 msg.flags = I2C_M_RD;
532 ret = i2c_transfer(client->adapter, &msg, 1);
Andres Salomonbca5c2c2008-07-12 13:47:54 -0700533 if (ret >= 0) {
Jonathan Corbet2bf7de42010-02-28 21:02:55 -0300534 *value = data;
Andres Salomonbca5c2c2008-07-12 13:47:54 -0700535 ret = 0;
536 }
Jonathan Corbet111f3352006-11-04 09:26:00 -0300537 return ret;
538}
539
540
Daniel Drake75e2bda2010-10-19 18:24:05 -0300541static int ov7670_write_i2c(struct v4l2_subdev *sd, unsigned char reg,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300542 unsigned char value)
543{
Hans Verkuil14386c22009-03-18 13:01:06 -0300544 struct i2c_client *client = v4l2_get_subdevdata(sd);
Jonathan Corbet2bf7de42010-02-28 21:02:55 -0300545 struct i2c_msg msg;
546 unsigned char data[2] = { reg, value };
547 int ret;
Hans Verkuil14386c22009-03-18 13:01:06 -0300548
Jonathan Corbet2bf7de42010-02-28 21:02:55 -0300549 msg.addr = client->addr;
550 msg.flags = 0;
551 msg.len = 2;
552 msg.buf = data;
553 ret = i2c_transfer(client->adapter, &msg, 1);
554 if (ret > 0)
555 ret = 0;
Jonathan Corbet6d774442007-08-17 01:02:33 -0300556 if (reg == REG_COM7 && (value & COM7_RESET))
Jonathan Corbet97693f92010-03-21 17:33:50 -0300557 msleep(5); /* Wait for reset to run */
Jonathan Corbet6d774442007-08-17 01:02:33 -0300558 return ret;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300559}
560
Daniel Drake75e2bda2010-10-19 18:24:05 -0300561static int ov7670_read(struct v4l2_subdev *sd, unsigned char reg,
562 unsigned char *value)
563{
564 struct ov7670_info *info = to_state(sd);
565 if (info->use_smbus)
566 return ov7670_read_smbus(sd, reg, value);
567 else
568 return ov7670_read_i2c(sd, reg, value);
569}
570
571static int ov7670_write(struct v4l2_subdev *sd, unsigned char reg,
572 unsigned char value)
573{
574 struct ov7670_info *info = to_state(sd);
575 if (info->use_smbus)
576 return ov7670_write_smbus(sd, reg, value);
577 else
578 return ov7670_write_i2c(sd, reg, value);
579}
Jonathan Corbet111f3352006-11-04 09:26:00 -0300580
Akinobu Mitab48d9082017-11-24 09:40:45 -0500581static int ov7670_update_bits(struct v4l2_subdev *sd, unsigned char reg,
582 unsigned char mask, unsigned char value)
583{
584 unsigned char orig;
585 int ret;
586
587 ret = ov7670_read(sd, reg, &orig);
588 if (ret)
589 return ret;
590
591 return ov7670_write(sd, reg, (orig & ~mask) | (value & mask));
592}
593
Jonathan Corbet111f3352006-11-04 09:26:00 -0300594/*
595 * Write a list of register settings; ff/ff stops the process.
596 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300597static int ov7670_write_array(struct v4l2_subdev *sd, struct regval_list *vals)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300598{
599 while (vals->reg_num != 0xff || vals->value != 0xff) {
Hans Verkuil14386c22009-03-18 13:01:06 -0300600 int ret = ov7670_write(sd, vals->reg_num, vals->value);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300601 if (ret < 0)
602 return ret;
603 vals++;
604 }
605 return 0;
606}
607
608
609/*
610 * Stuff that knows about the sensor.
611 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300612static int ov7670_reset(struct v4l2_subdev *sd, u32 val)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300613{
Hans Verkuil14386c22009-03-18 13:01:06 -0300614 ov7670_write(sd, REG_COM7, COM7_RESET);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300615 msleep(1);
Hans Verkuil14386c22009-03-18 13:01:06 -0300616 return 0;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300617}
618
619
Hans Verkuil14386c22009-03-18 13:01:06 -0300620static int ov7670_init(struct v4l2_subdev *sd, u32 val)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300621{
Hans Verkuil14386c22009-03-18 13:01:06 -0300622 return ov7670_write_array(sd, ov7670_default_regs);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300623}
624
Hans Verkuil14386c22009-03-18 13:01:06 -0300625static int ov7670_detect(struct v4l2_subdev *sd)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300626{
627 unsigned char v;
628 int ret;
629
Hans Verkuil14386c22009-03-18 13:01:06 -0300630 ret = ov7670_init(sd, 0);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300631 if (ret < 0)
632 return ret;
Hans Verkuil14386c22009-03-18 13:01:06 -0300633 ret = ov7670_read(sd, REG_MIDH, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300634 if (ret < 0)
635 return ret;
636 if (v != 0x7f) /* OV manuf. id. */
637 return -ENODEV;
Hans Verkuil14386c22009-03-18 13:01:06 -0300638 ret = ov7670_read(sd, REG_MIDL, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300639 if (ret < 0)
640 return ret;
641 if (v != 0xa2)
642 return -ENODEV;
643 /*
644 * OK, we know we have an OmniVision chip...but which one?
645 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300646 ret = ov7670_read(sd, REG_PID, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300647 if (ret < 0)
648 return ret;
649 if (v != 0x76) /* PID + VER = 0x76 / 0x73 */
650 return -ENODEV;
Hans Verkuil14386c22009-03-18 13:01:06 -0300651 ret = ov7670_read(sd, REG_VER, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300652 if (ret < 0)
653 return ret;
654 if (v != 0x73) /* PID + VER = 0x76 / 0x73 */
655 return -ENODEV;
656 return 0;
657}
658
659
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300660/*
661 * Store information about the video data format. The color matrix
662 * is deeply tied into the format, so keep the relevant values here.
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300663 * The magic matrix numbers come from OmniVision.
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300664 */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300665static struct ov7670_format_struct {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300666 u32 mbus_code;
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300667 enum v4l2_colorspace colorspace;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300668 struct regval_list *regs;
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300669 int cmatrix[CMATRIX_LEN];
Jonathan Corbet111f3352006-11-04 09:26:00 -0300670} ov7670_formats[] = {
671 {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300672 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
Hans Verkuil75eb9842015-03-09 17:04:12 -0300673 .colorspace = V4L2_COLORSPACE_SRGB,
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500674 .regs = ov7670_fmt_yuv422,
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300675 .cmatrix = { 128, -128, 0, -34, -94, 128 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300676 },
677 {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300678 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300679 .colorspace = V4L2_COLORSPACE_SRGB,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300680 .regs = ov7670_fmt_rgb444,
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300681 .cmatrix = { 179, -179, 0, -61, -176, 228 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300682 },
683 {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300684 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300685 .colorspace = V4L2_COLORSPACE_SRGB,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300686 .regs = ov7670_fmt_rgb565,
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300687 .cmatrix = { 179, -179, 0, -61, -176, 228 },
Jonathan Corbet585553e2007-03-25 11:38:21 -0300688 },
689 {
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300690 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300691 .colorspace = V4L2_COLORSPACE_SRGB,
Mauro Carvalho Chehab6e6a8b52018-01-04 13:08:56 -0500692 .regs = ov7670_fmt_raw,
Jonathan Corbet585553e2007-03-25 11:38:21 -0300693 .cmatrix = { 0, 0, 0, 0, 0, 0 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300694 },
Jonathan Corbet111f3352006-11-04 09:26:00 -0300695};
Jonathan Corbet585553e2007-03-25 11:38:21 -0300696#define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300697
Jonathan Corbet111f3352006-11-04 09:26:00 -0300698
699/*
700 * Then there is the issue of window sizes. Try to capture the info here.
701 */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300702
703/*
704 * QCIF mode is done (by OV) in a very strange way - it actually looks like
705 * VGA with weird scaling options - they do *not* use the canned QCIF mode
706 * which is allegedly provided by the sensor. So here's the weird register
707 * settings.
708 */
709static struct regval_list ov7670_qcif_regs[] = {
710 { REG_COM3, COM3_SCALEEN|COM3_DCWEN },
711 { REG_COM3, COM3_DCWEN },
712 { REG_COM14, COM14_DCWEN | 0x01},
713 { 0x73, 0xf1 },
714 { 0xa2, 0x52 },
715 { 0x7b, 0x1c },
716 { 0x7c, 0x28 },
717 { 0x7d, 0x3c },
718 { 0x7f, 0x69 },
719 { REG_COM9, 0x38 },
720 { 0xa1, 0x0b },
721 { 0x74, 0x19 },
722 { 0x9a, 0x80 },
723 { 0x43, 0x14 },
724 { REG_COM13, 0xc0 },
725 { 0xff, 0xff },
726};
727
Javier Martind058e232013-01-29 07:12:13 -0300728static struct ov7670_win_size ov7670_win_sizes[] = {
Jonathan Corbet111f3352006-11-04 09:26:00 -0300729 /* VGA */
730 {
731 .width = VGA_WIDTH,
732 .height = VGA_HEIGHT,
733 .com7_bit = COM7_FMT_VGA,
Javier Martind058e232013-01-29 07:12:13 -0300734 .hstart = 158, /* These values from */
735 .hstop = 14, /* Omnivision */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300736 .vstart = 10,
737 .vstop = 490,
Javier Martind058e232013-01-29 07:12:13 -0300738 .regs = NULL,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300739 },
740 /* CIF */
741 {
742 .width = CIF_WIDTH,
743 .height = CIF_HEIGHT,
744 .com7_bit = COM7_FMT_CIF,
Javier Martind058e232013-01-29 07:12:13 -0300745 .hstart = 170, /* Empirically determined */
Jonathan Corbet111f3352006-11-04 09:26:00 -0300746 .hstop = 90,
747 .vstart = 14,
748 .vstop = 494,
Javier Martind058e232013-01-29 07:12:13 -0300749 .regs = NULL,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300750 },
751 /* QVGA */
752 {
753 .width = QVGA_WIDTH,
754 .height = QVGA_HEIGHT,
755 .com7_bit = COM7_FMT_QVGA,
Javier Martind058e232013-01-29 07:12:13 -0300756 .hstart = 168, /* Empirically determined */
Daniel Drakedc4589c2010-10-18 18:07:36 -0300757 .hstop = 24,
758 .vstart = 12,
759 .vstop = 492,
Javier Martind058e232013-01-29 07:12:13 -0300760 .regs = NULL,
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300761 },
762 /* QCIF */
763 {
764 .width = QCIF_WIDTH,
765 .height = QCIF_HEIGHT,
766 .com7_bit = COM7_FMT_VGA, /* see comment above */
Javier Martind058e232013-01-29 07:12:13 -0300767 .hstart = 456, /* Empirically determined */
Jonathan Corbetf9a76152006-11-19 19:04:55 -0300768 .hstop = 24,
769 .vstart = 14,
770 .vstop = 494,
Javier Martind058e232013-01-29 07:12:13 -0300771 .regs = ov7670_qcif_regs,
772 }
Jonathan Corbet111f3352006-11-04 09:26:00 -0300773};
774
Javier Martind058e232013-01-29 07:12:13 -0300775static struct ov7670_win_size ov7675_win_sizes[] = {
776 /*
777 * Currently, only VGA is supported. Theoretically it could be possible
778 * to support CIF, QVGA and QCIF too. Taking values for ov7670 as a
779 * base and tweak them empirically could be required.
780 */
781 {
782 .width = VGA_WIDTH,
783 .height = VGA_HEIGHT,
784 .com7_bit = COM7_FMT_VGA,
785 .hstart = 158, /* These values from */
786 .hstop = 14, /* Omnivision */
787 .vstart = 14, /* Empirically determined */
788 .vstop = 494,
789 .regs = NULL,
790 }
791};
Jonathan Corbet111f3352006-11-04 09:26:00 -0300792
Javier Martinf6dd9272013-01-29 07:16:59 -0300793static void ov7675_get_framerate(struct v4l2_subdev *sd,
794 struct v4l2_fract *tpf)
795{
796 struct ov7670_info *info = to_state(sd);
797 u32 clkrc = info->clkrc;
Javier Martin04ee6d92013-01-29 07:23:42 -0300798 int pll_factor;
799
800 if (info->pll_bypass)
801 pll_factor = 1;
802 else
803 pll_factor = PLL_FACTOR;
Javier Martinf6dd9272013-01-29 07:16:59 -0300804
805 clkrc++;
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300806 if (info->fmt->mbus_code == MEDIA_BUS_FMT_SBGGR8_1X8)
Javier Martinf6dd9272013-01-29 07:16:59 -0300807 clkrc = (clkrc >> 1);
808
809 tpf->numerator = 1;
810 tpf->denominator = (5 * pll_factor * info->clock_speed) /
811 (4 * clkrc);
812}
813
814static int ov7675_set_framerate(struct v4l2_subdev *sd,
815 struct v4l2_fract *tpf)
816{
817 struct ov7670_info *info = to_state(sd);
818 u32 clkrc;
Javier Martin04ee6d92013-01-29 07:23:42 -0300819 int pll_factor;
Javier Martinf6dd9272013-01-29 07:16:59 -0300820 int ret;
821
822 /*
823 * The formula is fps = 5/4*pixclk for YUV/RGB and
824 * fps = 5/2*pixclk for RAW.
825 *
826 * pixclk = clock_speed / (clkrc + 1) * PLLfactor
827 *
828 */
Javier Martin04ee6d92013-01-29 07:23:42 -0300829 if (info->pll_bypass) {
830 pll_factor = 1;
831 ret = ov7670_write(sd, REG_DBLV, DBLV_BYPASS);
832 } else {
833 pll_factor = PLL_FACTOR;
834 ret = ov7670_write(sd, REG_DBLV, DBLV_X4);
835 }
836 if (ret < 0)
837 return ret;
838
Javier Martinf6dd9272013-01-29 07:16:59 -0300839 if (tpf->numerator == 0 || tpf->denominator == 0) {
840 clkrc = 0;
841 } else {
842 clkrc = (5 * pll_factor * info->clock_speed * tpf->numerator) /
843 (4 * tpf->denominator);
Boris BREZILLONf5fe58f2014-11-10 14:28:29 -0300844 if (info->fmt->mbus_code == MEDIA_BUS_FMT_SBGGR8_1X8)
Javier Martinf6dd9272013-01-29 07:16:59 -0300845 clkrc = (clkrc << 1);
846 clkrc--;
847 }
848
849 /*
850 * The datasheet claims that clkrc = 0 will divide the input clock by 1
851 * but we've checked with an oscilloscope that it divides by 2 instead.
852 * So, if clkrc = 0 just bypass the divider.
853 */
854 if (clkrc <= 0)
855 clkrc = CLK_EXT;
856 else if (clkrc > CLK_SCALE)
857 clkrc = CLK_SCALE;
858 info->clkrc = clkrc;
859
860 /* Recalculate frame rate */
861 ov7675_get_framerate(sd, tpf);
862
Sakari Ailusf1fb0852019-01-15 06:54:44 -0200863 return ov7670_write(sd, REG_CLKRC, info->clkrc);
Javier Martinf6dd9272013-01-29 07:16:59 -0300864}
865
866static void ov7670_get_framerate_legacy(struct v4l2_subdev *sd,
867 struct v4l2_fract *tpf)
868{
869 struct ov7670_info *info = to_state(sd);
870
871 tpf->numerator = 1;
872 tpf->denominator = info->clock_speed;
873 if ((info->clkrc & CLK_EXT) == 0 && (info->clkrc & CLK_SCALE) > 1)
874 tpf->denominator /= (info->clkrc & CLK_SCALE);
875}
876
877static int ov7670_set_framerate_legacy(struct v4l2_subdev *sd,
878 struct v4l2_fract *tpf)
879{
880 struct ov7670_info *info = to_state(sd);
881 int div;
882
883 if (tpf->numerator == 0 || tpf->denominator == 0)
884 div = 1; /* Reset to full rate */
885 else
886 div = (tpf->numerator * info->clock_speed) / tpf->denominator;
887 if (div == 0)
888 div = 1;
889 else if (div > CLK_SCALE)
890 div = CLK_SCALE;
891 info->clkrc = (info->clkrc & 0x80) | div;
892 tpf->numerator = 1;
893 tpf->denominator = info->clock_speed / div;
894 return ov7670_write(sd, REG_CLKRC, info->clkrc);
895}
896
Jonathan Corbet111f3352006-11-04 09:26:00 -0300897/*
898 * Store a set of start/stop values into the camera.
899 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300900static int ov7670_set_hw(struct v4l2_subdev *sd, int hstart, int hstop,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300901 int vstart, int vstop)
902{
903 int ret;
904 unsigned char v;
905/*
906 * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
907 * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
908 * a mystery "edge offset" value in the top two bits of href.
909 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300910 ret = ov7670_write(sd, REG_HSTART, (hstart >> 3) & 0xff);
911 ret += ov7670_write(sd, REG_HSTOP, (hstop >> 3) & 0xff);
912 ret += ov7670_read(sd, REG_HREF, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300913 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
914 msleep(10);
Hans Verkuil14386c22009-03-18 13:01:06 -0300915 ret += ov7670_write(sd, REG_HREF, v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300916/*
917 * Vertical: similar arrangement, but only 10 bits.
918 */
Hans Verkuil14386c22009-03-18 13:01:06 -0300919 ret += ov7670_write(sd, REG_VSTART, (vstart >> 2) & 0xff);
920 ret += ov7670_write(sd, REG_VSTOP, (vstop >> 2) & 0xff);
921 ret += ov7670_read(sd, REG_VREF, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300922 v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3);
923 msleep(10);
Hans Verkuil14386c22009-03-18 13:01:06 -0300924 ret += ov7670_write(sd, REG_VREF, v);
Jonathan Corbet111f3352006-11-04 09:26:00 -0300925 return ret;
926}
927
928
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300929static int ov7670_enum_mbus_code(struct v4l2_subdev *sd,
930 struct v4l2_subdev_pad_config *cfg,
931 struct v4l2_subdev_mbus_code_enum *code)
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300932{
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300933 if (code->pad || code->index >= N_OV7670_FMTS)
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300934 return -EINVAL;
935
Hans Verkuilebcff5f2015-04-09 04:01:33 -0300936 code->code = ov7670_formats[code->index].mbus_code;
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300937 return 0;
938}
Jonathan Corbet111f3352006-11-04 09:26:00 -0300939
Hans Verkuil14386c22009-03-18 13:01:06 -0300940static int ov7670_try_fmt_internal(struct v4l2_subdev *sd,
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300941 struct v4l2_mbus_framefmt *fmt,
Jonathan Corbet111f3352006-11-04 09:26:00 -0300942 struct ov7670_format_struct **ret_fmt,
943 struct ov7670_win_size **ret_wsize)
944{
Javier Martinf748cd32013-01-29 07:14:27 -0300945 int index, i;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300946 struct ov7670_win_size *wsize;
Javier Martind058e232013-01-29 07:12:13 -0300947 struct ov7670_info *info = to_state(sd);
948 unsigned int n_win_sizes = info->devtype->n_win_sizes;
Javier Martinf748cd32013-01-29 07:14:27 -0300949 unsigned int win_sizes_limit = n_win_sizes;
Jonathan Corbet111f3352006-11-04 09:26:00 -0300950
951 for (index = 0; index < N_OV7670_FMTS; index++)
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300952 if (ov7670_formats[index].mbus_code == fmt->code)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300953 break;
Daniel Drakecd257a62008-06-30 20:57:39 -0300954 if (index >= N_OV7670_FMTS) {
955 /* default to first format */
956 index = 0;
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300957 fmt->code = ov7670_formats[0].mbus_code;
Daniel Drakecd257a62008-06-30 20:57:39 -0300958 }
Jonathan Corbet111f3352006-11-04 09:26:00 -0300959 if (ret_fmt != NULL)
960 *ret_fmt = ov7670_formats + index;
961 /*
962 * Fields: the OV devices claim to be progressive.
963 */
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300964 fmt->field = V4L2_FIELD_NONE;
Javier Martinf748cd32013-01-29 07:14:27 -0300965
966 /*
967 * Don't consider values that don't match min_height and min_width
968 * constraints.
969 */
970 if (info->min_width || info->min_height)
971 for (i = 0; i < n_win_sizes; i++) {
972 wsize = info->devtype->win_sizes + i;
973
974 if (wsize->width < info->min_width ||
975 wsize->height < info->min_height) {
976 win_sizes_limit = i;
977 break;
978 }
979 }
Jonathan Corbet111f3352006-11-04 09:26:00 -0300980 /*
981 * Round requested image size down to the nearest
982 * we support, but not below the smallest.
983 */
Javier Martind058e232013-01-29 07:12:13 -0300984 for (wsize = info->devtype->win_sizes;
Javier Martinf748cd32013-01-29 07:14:27 -0300985 wsize < info->devtype->win_sizes + win_sizes_limit; wsize++)
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300986 if (fmt->width >= wsize->width && fmt->height >= wsize->height)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300987 break;
Javier Martinf748cd32013-01-29 07:14:27 -0300988 if (wsize >= info->devtype->win_sizes + win_sizes_limit)
Jonathan Corbet111f3352006-11-04 09:26:00 -0300989 wsize--; /* Take the smallest one */
990 if (ret_wsize != NULL)
991 *ret_wsize = wsize;
992 /*
993 * Note the size we'll actually handle.
994 */
Hans Verkuil959f3bd2010-05-08 18:28:41 -0300995 fmt->width = wsize->width;
996 fmt->height = wsize->height;
997 fmt->colorspace = ov7670_formats[index].colorspace;
Wenyou Yangc0662dd2017-10-16 05:14:26 +0200998
999 info->format = *fmt;
1000
Jonathan Corbet111f3352006-11-04 09:26:00 -03001001 return 0;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001002}
1003
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001004static int ov7670_apply_fmt(struct v4l2_subdev *sd)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001005{
Hans Verkuil14386c22009-03-18 13:01:06 -03001006 struct ov7670_info *info = to_state(sd);
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001007 struct ov7670_win_size *wsize = info->wsize;
Jacopo Mondi01b84442018-01-24 04:30:50 -05001008 unsigned char com7, com10 = 0;
Hans Verkuil959f3bd2010-05-08 18:28:41 -03001009 int ret;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001010
Jonathan Corbet111f3352006-11-04 09:26:00 -03001011 /*
1012 * COM7 is a pain in the ass, it doesn't like to be read then
1013 * quickly written afterward. But we have everything we need
1014 * to set it absolutely here, as long as the format-specific
1015 * register sets list it first.
1016 */
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001017 com7 = info->fmt->regs[0].value;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001018 com7 |= wsize->com7_bit;
Jacopo Mondi01b84442018-01-24 04:30:50 -05001019 ret = ov7670_write(sd, REG_COM7, com7);
1020 if (ret)
1021 return ret;
1022
1023 /*
1024 * Configure the media bus through COM10 register
1025 */
1026 if (info->mbus_config & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1027 com10 |= COM10_VS_NEG;
1028 if (info->mbus_config & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1029 com10 |= COM10_HREF_REV;
1030 if (info->pclk_hb_disable)
1031 com10 |= COM10_PCLK_HB;
1032 ret = ov7670_write(sd, REG_COM10, com10);
1033 if (ret)
1034 return ret;
1035
Jonathan Corbet111f3352006-11-04 09:26:00 -03001036 /*
1037 * Now write the rest of the array. Also store start/stops
1038 */
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001039 ret = ov7670_write_array(sd, info->fmt->regs + 1);
Jacopo Mondi01b84442018-01-24 04:30:50 -05001040 if (ret)
1041 return ret;
1042
1043 ret = ov7670_set_hw(sd, wsize->hstart, wsize->hstop, wsize->vstart,
1044 wsize->vstop);
1045 if (ret)
1046 return ret;
1047
1048 if (wsize->regs) {
Hans Verkuil14386c22009-03-18 13:01:06 -03001049 ret = ov7670_write_array(sd, wsize->regs);
Jacopo Mondi01b84442018-01-24 04:30:50 -05001050 if (ret)
1051 return ret;
1052 }
1053
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001054 /*
1055 * If we're running RGB565, we must rewrite clkrc after setting
1056 * the other parameters or the image looks poor. If we're *not*
1057 * doing RGB565, we must not rewrite clkrc or the image looks
1058 * *really* poor.
Jonathan Corbeta8e68c32010-03-18 19:10:18 -03001059 *
1060 * (Update) Now that we retain clkrc state, we should be able
1061 * to write it unconditionally, and that will make the frame
1062 * rate persistent too.
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001063 */
Jacopo Mondi01b84442018-01-24 04:30:50 -05001064 ret = ov7670_write(sd, REG_CLKRC, info->clkrc);
1065 if (ret)
1066 return ret;
1067
Hans Verkuil959f3bd2010-05-08 18:28:41 -03001068 return 0;
1069}
1070
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001071/*
1072 * Set a format.
1073 */
1074static int ov7670_set_fmt(struct v4l2_subdev *sd,
1075 struct v4l2_subdev_pad_config *cfg,
1076 struct v4l2_subdev_format *format)
1077{
1078 struct ov7670_info *info = to_state(sd);
1079#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1080 struct v4l2_mbus_framefmt *mbus_fmt;
1081#endif
1082 int ret;
1083
1084 if (format->pad)
1085 return -EINVAL;
1086
1087 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1088 ret = ov7670_try_fmt_internal(sd, &format->format, NULL, NULL);
1089 if (ret)
1090 return ret;
1091#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1092 mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
1093 *mbus_fmt = format->format;
1094 return 0;
1095#else
1096 return -ENOTTY;
1097#endif
1098 }
1099
1100 ret = ov7670_try_fmt_internal(sd, &format->format, &info->fmt, &info->wsize);
1101 if (ret)
1102 return ret;
1103
1104 ret = ov7670_apply_fmt(sd);
1105 if (ret)
1106 return ret;
1107
1108 return 0;
1109}
1110
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001111static int ov7670_get_fmt(struct v4l2_subdev *sd,
1112 struct v4l2_subdev_pad_config *cfg,
1113 struct v4l2_subdev_format *format)
1114{
1115 struct ov7670_info *info = to_state(sd);
1116#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1117 struct v4l2_mbus_framefmt *mbus_fmt;
1118#endif
1119
1120 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1121#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1122 mbus_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
1123 format->format = *mbus_fmt;
1124 return 0;
1125#else
1126 return -ENOTTY;
1127#endif
1128 } else {
1129 format->format = info->format;
1130 }
1131
1132 return 0;
1133}
1134
Jonathan Corbet111f3352006-11-04 09:26:00 -03001135/*
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001136 * Implement G/S_PARM. There is a "high quality" mode we could try
1137 * to do someday; for now, we just do the frame rate tweak.
1138 */
Hans Verkuil44711092018-01-22 04:00:45 -05001139static int ov7670_g_frame_interval(struct v4l2_subdev *sd,
1140 struct v4l2_subdev_frame_interval *ival)
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001141{
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001142 struct ov7670_info *info = to_state(sd);
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001143
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001144
Hans Verkuil44711092018-01-22 04:00:45 -05001145 info->devtype->get_framerate(sd, &ival->interval);
Javier Martinf6dd9272013-01-29 07:16:59 -03001146
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001147 return 0;
1148}
1149
Hans Verkuil44711092018-01-22 04:00:45 -05001150static int ov7670_s_frame_interval(struct v4l2_subdev *sd,
1151 struct v4l2_subdev_frame_interval *ival)
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001152{
Hans Verkuil44711092018-01-22 04:00:45 -05001153 struct v4l2_fract *tpf = &ival->interval;
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001154 struct ov7670_info *info = to_state(sd);
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001155
Jonathan Corbetd8d20152009-12-20 11:39:47 -03001156
Javier Martinf6dd9272013-01-29 07:16:59 -03001157 return info->devtype->set_framerate(sd, tpf);
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001158}
1159
1160
Jonathan Corbete99dfcf2010-09-24 14:17:29 -03001161/*
1162 * Frame intervals. Since frame rates are controlled with the clock
1163 * divider, we can only do 30/n for integer n values. So no continuous
1164 * or stepwise options. Here we just pick a handful of logical values.
1165 */
1166
1167static int ov7670_frame_rates[] = { 30, 15, 10, 5, 1 };
1168
Hans Verkuil17bef882015-03-04 01:48:00 -08001169static int ov7670_enum_frame_interval(struct v4l2_subdev *sd,
1170 struct v4l2_subdev_pad_config *cfg,
1171 struct v4l2_subdev_frame_interval_enum *fie)
Jonathan Corbete99dfcf2010-09-24 14:17:29 -03001172{
Hans Verkuilb8cc79f2015-03-05 05:15:30 -03001173 struct ov7670_info *info = to_state(sd);
1174 unsigned int n_win_sizes = info->devtype->n_win_sizes;
1175 int i;
1176
Hans Verkuil17bef882015-03-04 01:48:00 -08001177 if (fie->pad)
Jonathan Corbete99dfcf2010-09-24 14:17:29 -03001178 return -EINVAL;
Hans Verkuil17bef882015-03-04 01:48:00 -08001179 if (fie->index >= ARRAY_SIZE(ov7670_frame_rates))
1180 return -EINVAL;
Hans Verkuilb8cc79f2015-03-05 05:15:30 -03001181
1182 /*
1183 * Check if the width/height is valid.
1184 *
1185 * If a minimum width/height was requested, filter out the capture
1186 * windows that fall outside that.
1187 */
1188 for (i = 0; i < n_win_sizes; i++) {
1189 struct ov7670_win_size *win = &info->devtype->win_sizes[i];
1190
1191 if (info->min_width && win->width < info->min_width)
1192 continue;
1193 if (info->min_height && win->height < info->min_height)
1194 continue;
1195 if (fie->width == win->width && fie->height == win->height)
1196 break;
1197 }
1198 if (i == n_win_sizes)
1199 return -EINVAL;
Hans Verkuil17bef882015-03-04 01:48:00 -08001200 fie->interval.numerator = 1;
1201 fie->interval.denominator = ov7670_frame_rates[fie->index];
Jonathan Corbete99dfcf2010-09-24 14:17:29 -03001202 return 0;
1203}
Jonathan Corbetc8f5b2f52006-12-01 15:50:59 -03001204
1205/*
Daniel Drakeb0326b72010-09-24 14:17:37 -03001206 * Frame size enumeration
1207 */
Hans Verkuil17bef882015-03-04 01:48:00 -08001208static int ov7670_enum_frame_size(struct v4l2_subdev *sd,
1209 struct v4l2_subdev_pad_config *cfg,
1210 struct v4l2_subdev_frame_size_enum *fse)
Daniel Drakeb0326b72010-09-24 14:17:37 -03001211{
Daniel Drake75e2bda2010-10-19 18:24:05 -03001212 struct ov7670_info *info = to_state(sd);
1213 int i;
1214 int num_valid = -1;
Hans Verkuil17bef882015-03-04 01:48:00 -08001215 __u32 index = fse->index;
Javier Martind058e232013-01-29 07:12:13 -03001216 unsigned int n_win_sizes = info->devtype->n_win_sizes;
Daniel Drakeb0326b72010-09-24 14:17:37 -03001217
Hans Verkuil17bef882015-03-04 01:48:00 -08001218 if (fse->pad)
1219 return -EINVAL;
1220
Daniel Drake75e2bda2010-10-19 18:24:05 -03001221 /*
1222 * If a minimum width/height was requested, filter out the capture
1223 * windows that fall outside that.
1224 */
Javier Martind058e232013-01-29 07:12:13 -03001225 for (i = 0; i < n_win_sizes; i++) {
Guennadi Liakhovetski322e6d12014-04-14 10:49:34 -03001226 struct ov7670_win_size *win = &info->devtype->win_sizes[i];
Daniel Drake75e2bda2010-10-19 18:24:05 -03001227 if (info->min_width && win->width < info->min_width)
1228 continue;
1229 if (info->min_height && win->height < info->min_height)
1230 continue;
1231 if (index == ++num_valid) {
Hans Verkuil17bef882015-03-04 01:48:00 -08001232 fse->min_width = fse->max_width = win->width;
1233 fse->min_height = fse->max_height = win->height;
Daniel Drake75e2bda2010-10-19 18:24:05 -03001234 return 0;
1235 }
1236 }
1237
1238 return -EINVAL;
Daniel Drakeb0326b72010-09-24 14:17:37 -03001239}
1240
1241/*
Jonathan Corbet111f3352006-11-04 09:26:00 -03001242 * Code for dealing with controls.
1243 */
1244
Hans Verkuil14386c22009-03-18 13:01:06 -03001245static int ov7670_store_cmatrix(struct v4l2_subdev *sd,
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001246 int matrix[CMATRIX_LEN])
1247{
1248 int i, ret;
Hans Verkuile3bf20d2008-07-17 13:29:49 -03001249 unsigned char signbits = 0;
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001250
1251 /*
1252 * Weird crap seems to exist in the upper part of
1253 * the sign bits register, so let's preserve it.
1254 */
Hans Verkuil14386c22009-03-18 13:01:06 -03001255 ret = ov7670_read(sd, REG_CMATRIX_SIGN, &signbits);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001256 signbits &= 0xc0;
1257
1258 for (i = 0; i < CMATRIX_LEN; i++) {
1259 unsigned char raw;
1260
1261 if (matrix[i] < 0) {
1262 signbits |= (1 << i);
1263 if (matrix[i] < -255)
1264 raw = 0xff;
1265 else
1266 raw = (-1 * matrix[i]) & 0xff;
1267 }
1268 else {
1269 if (matrix[i] > 255)
1270 raw = 0xff;
1271 else
1272 raw = matrix[i] & 0xff;
1273 }
Hans Verkuil14386c22009-03-18 13:01:06 -03001274 ret += ov7670_write(sd, REG_CMATRIX_BASE + i, raw);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001275 }
Hans Verkuil14386c22009-03-18 13:01:06 -03001276 ret += ov7670_write(sd, REG_CMATRIX_SIGN, signbits);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001277 return ret;
1278}
1279
1280
1281/*
1282 * Hue also requires messing with the color matrix. It also requires
1283 * trig functions, which tend not to be well supported in the kernel.
1284 * So here is a simple table of sine values, 0-90 degrees, in steps
1285 * of five degrees. Values are multiplied by 1000.
1286 *
1287 * The following naive approximate trig functions require an argument
1288 * carefully limited to -180 <= theta <= 180.
1289 */
1290#define SIN_STEP 5
1291static const int ov7670_sin_table[] = {
1292 0, 87, 173, 258, 342, 422,
1293 499, 573, 642, 707, 766, 819,
1294 866, 906, 939, 965, 984, 996,
1295 1000
1296};
1297
1298static int ov7670_sine(int theta)
1299{
1300 int chs = 1;
1301 int sine;
1302
1303 if (theta < 0) {
1304 theta = -theta;
1305 chs = -1;
1306 }
1307 if (theta <= 90)
1308 sine = ov7670_sin_table[theta/SIN_STEP];
1309 else {
1310 theta -= 90;
1311 sine = 1000 - ov7670_sin_table[theta/SIN_STEP];
1312 }
1313 return sine*chs;
1314}
1315
1316static int ov7670_cosine(int theta)
1317{
1318 theta = 90 - theta;
1319 if (theta > 180)
1320 theta -= 360;
1321 else if (theta < -180)
1322 theta += 360;
1323 return ov7670_sine(theta);
1324}
1325
1326
1327
1328
1329static void ov7670_calc_cmatrix(struct ov7670_info *info,
Javier Martin492959c2013-01-29 07:31:17 -03001330 int matrix[CMATRIX_LEN], int sat, int hue)
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001331{
1332 int i;
1333 /*
1334 * Apply the current saturation setting first.
1335 */
1336 for (i = 0; i < CMATRIX_LEN; i++)
Javier Martin492959c2013-01-29 07:31:17 -03001337 matrix[i] = (info->fmt->cmatrix[i] * sat) >> 7;
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001338 /*
1339 * Then, if need be, rotate the hue value.
1340 */
Javier Martin492959c2013-01-29 07:31:17 -03001341 if (hue != 0) {
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001342 int sinth, costh, tmpmatrix[CMATRIX_LEN];
1343
1344 memcpy(tmpmatrix, matrix, CMATRIX_LEN*sizeof(int));
Javier Martin492959c2013-01-29 07:31:17 -03001345 sinth = ov7670_sine(hue);
1346 costh = ov7670_cosine(hue);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001347
1348 matrix[0] = (matrix[3]*sinth + matrix[0]*costh)/1000;
1349 matrix[1] = (matrix[4]*sinth + matrix[1]*costh)/1000;
1350 matrix[2] = (matrix[5]*sinth + matrix[2]*costh)/1000;
1351 matrix[3] = (matrix[3]*costh - matrix[0]*sinth)/1000;
1352 matrix[4] = (matrix[4]*costh - matrix[1]*sinth)/1000;
1353 matrix[5] = (matrix[5]*costh - matrix[2]*sinth)/1000;
1354 }
1355}
1356
1357
1358
Javier Martin492959c2013-01-29 07:31:17 -03001359static int ov7670_s_sat_hue(struct v4l2_subdev *sd, int sat, int hue)
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001360{
Hans Verkuil14386c22009-03-18 13:01:06 -03001361 struct ov7670_info *info = to_state(sd);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001362 int matrix[CMATRIX_LEN];
1363 int ret;
1364
Javier Martin492959c2013-01-29 07:31:17 -03001365 ov7670_calc_cmatrix(info, matrix, sat, hue);
Hans Verkuil14386c22009-03-18 13:01:06 -03001366 ret = ov7670_store_cmatrix(sd, matrix);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001367 return ret;
1368}
1369
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001370
Jonathan Corbet111f3352006-11-04 09:26:00 -03001371/*
1372 * Some weird registers seem to store values in a sign/magnitude format!
1373 */
Jonathan Corbet111f3352006-11-04 09:26:00 -03001374
1375static unsigned char ov7670_abs_to_sm(unsigned char v)
1376{
1377 if (v > 127)
1378 return v & 0x7f;
Hans Verkuil14386c22009-03-18 13:01:06 -03001379 return (128 - v) | 0x80;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001380}
1381
Hans Verkuilca075612009-03-18 13:23:13 -03001382static int ov7670_s_brightness(struct v4l2_subdev *sd, int value)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001383{
Hans Verkuile3bf20d2008-07-17 13:29:49 -03001384 unsigned char com8 = 0, v;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001385 int ret;
1386
Hans Verkuil14386c22009-03-18 13:01:06 -03001387 ov7670_read(sd, REG_COM8, &com8);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001388 com8 &= ~COM8_AEC;
Hans Verkuil14386c22009-03-18 13:01:06 -03001389 ov7670_write(sd, REG_COM8, com8);
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001390 v = ov7670_abs_to_sm(value);
Hans Verkuil14386c22009-03-18 13:01:06 -03001391 ret = ov7670_write(sd, REG_BRIGHT, v);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001392 return ret;
1393}
1394
Hans Verkuilca075612009-03-18 13:23:13 -03001395static int ov7670_s_contrast(struct v4l2_subdev *sd, int value)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001396{
Hans Verkuil14386c22009-03-18 13:01:06 -03001397 return ov7670_write(sd, REG_CONTRAS, (unsigned char) value);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001398}
1399
Hans Verkuilca075612009-03-18 13:23:13 -03001400static int ov7670_s_hflip(struct v4l2_subdev *sd, int value)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001401{
Hans Verkuile3bf20d2008-07-17 13:29:49 -03001402 unsigned char v = 0;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001403 int ret;
1404
Hans Verkuil14386c22009-03-18 13:01:06 -03001405 ret = ov7670_read(sd, REG_MVFP, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001406 if (value)
1407 v |= MVFP_MIRROR;
1408 else
1409 v &= ~MVFP_MIRROR;
1410 msleep(10); /* FIXME */
Hans Verkuil14386c22009-03-18 13:01:06 -03001411 ret += ov7670_write(sd, REG_MVFP, v);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001412 return ret;
1413}
1414
Hans Verkuilca075612009-03-18 13:23:13 -03001415static int ov7670_s_vflip(struct v4l2_subdev *sd, int value)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001416{
Hans Verkuile3bf20d2008-07-17 13:29:49 -03001417 unsigned char v = 0;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001418 int ret;
1419
Hans Verkuil14386c22009-03-18 13:01:06 -03001420 ret = ov7670_read(sd, REG_MVFP, &v);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001421 if (value)
1422 v |= MVFP_FLIP;
1423 else
1424 v &= ~MVFP_FLIP;
1425 msleep(10); /* FIXME */
Hans Verkuil14386c22009-03-18 13:01:06 -03001426 ret += ov7670_write(sd, REG_MVFP, v);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001427 return ret;
1428}
1429
Jonathan Corbet81898672010-03-04 18:50:34 -03001430/*
1431 * GAIN is split between REG_GAIN and REG_VREF[7:6]. If one believes
1432 * the data sheet, the VREF parts should be the most significant, but
1433 * experience shows otherwise. There seems to be little value in
1434 * messing with the VREF bits, so we leave them alone.
1435 */
1436static int ov7670_g_gain(struct v4l2_subdev *sd, __s32 *value)
1437{
1438 int ret;
1439 unsigned char gain;
1440
1441 ret = ov7670_read(sd, REG_GAIN, &gain);
1442 *value = gain;
1443 return ret;
1444}
1445
1446static int ov7670_s_gain(struct v4l2_subdev *sd, int value)
1447{
1448 int ret;
1449 unsigned char com8;
1450
1451 ret = ov7670_write(sd, REG_GAIN, value & 0xff);
1452 /* Have to turn off AGC as well */
1453 if (ret == 0) {
1454 ret = ov7670_read(sd, REG_COM8, &com8);
1455 ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AGC);
1456 }
1457 return ret;
1458}
1459
1460/*
1461 * Tweak autogain.
1462 */
Jonathan Corbet81898672010-03-04 18:50:34 -03001463static int ov7670_s_autogain(struct v4l2_subdev *sd, int value)
1464{
1465 int ret;
1466 unsigned char com8;
1467
1468 ret = ov7670_read(sd, REG_COM8, &com8);
1469 if (ret == 0) {
1470 if (value)
1471 com8 |= COM8_AGC;
1472 else
1473 com8 &= ~COM8_AGC;
1474 ret = ov7670_write(sd, REG_COM8, com8);
1475 }
1476 return ret;
1477}
1478
Jonathan Corbet364e9332010-03-05 16:48:39 -03001479static int ov7670_s_exp(struct v4l2_subdev *sd, int value)
1480{
1481 int ret;
1482 unsigned char com1, com8, aech, aechh;
1483
1484 ret = ov7670_read(sd, REG_COM1, &com1) +
Mauro Carvalho Chehabd487df92015-04-29 09:50:41 -03001485 ov7670_read(sd, REG_COM8, &com8) +
Jonathan Corbet364e9332010-03-05 16:48:39 -03001486 ov7670_read(sd, REG_AECHH, &aechh);
1487 if (ret)
1488 return ret;
1489
1490 com1 = (com1 & 0xfc) | (value & 0x03);
1491 aech = (value >> 2) & 0xff;
1492 aechh = (aechh & 0xc0) | ((value >> 10) & 0x3f);
1493 ret = ov7670_write(sd, REG_COM1, com1) +
1494 ov7670_write(sd, REG_AECH, aech) +
1495 ov7670_write(sd, REG_AECHH, aechh);
1496 /* Have to turn off AEC as well */
1497 if (ret == 0)
1498 ret = ov7670_write(sd, REG_COM8, com8 & ~COM8_AEC);
1499 return ret;
1500}
1501
1502/*
1503 * Tweak autoexposure.
1504 */
Jonathan Corbet364e9332010-03-05 16:48:39 -03001505static int ov7670_s_autoexp(struct v4l2_subdev *sd,
1506 enum v4l2_exposure_auto_type value)
1507{
1508 int ret;
1509 unsigned char com8;
1510
1511 ret = ov7670_read(sd, REG_COM8, &com8);
1512 if (ret == 0) {
1513 if (value == V4L2_EXPOSURE_AUTO)
1514 com8 |= COM8_AEC;
1515 else
1516 com8 &= ~COM8_AEC;
1517 ret = ov7670_write(sd, REG_COM8, com8);
1518 }
1519 return ret;
1520}
1521
Akinobu Mitab48d9082017-11-24 09:40:45 -05001522static const char * const ov7670_test_pattern_menu[] = {
1523 "No test output",
1524 "Shifting \"1\"",
1525 "8-bar color bar",
1526 "Fade to gray color bar",
1527};
1528
1529static int ov7670_s_test_pattern(struct v4l2_subdev *sd, int value)
1530{
1531 int ret;
1532
1533 ret = ov7670_update_bits(sd, REG_SCALING_XSC, TEST_PATTTERN_0,
1534 value & BIT(0) ? TEST_PATTTERN_0 : 0);
1535 if (ret)
1536 return ret;
1537
1538 return ov7670_update_bits(sd, REG_SCALING_YSC, TEST_PATTTERN_1,
1539 value & BIT(1) ? TEST_PATTTERN_1 : 0);
1540}
Jonathan Corbet81898672010-03-04 18:50:34 -03001541
Javier Martin492959c2013-01-29 07:31:17 -03001542static int ov7670_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001543{
Javier Martin492959c2013-01-29 07:31:17 -03001544 struct v4l2_subdev *sd = to_sd(ctrl);
1545 struct ov7670_info *info = to_state(sd);
1546
1547 switch (ctrl->id) {
Jonathan Corbet81898672010-03-04 18:50:34 -03001548 case V4L2_CID_AUTOGAIN:
Javier Martin492959c2013-01-29 07:31:17 -03001549 return ov7670_g_gain(sd, &info->gain->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001550 }
1551 return -EINVAL;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001552}
1553
Javier Martin492959c2013-01-29 07:31:17 -03001554static int ov7670_s_ctrl(struct v4l2_ctrl *ctrl)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001555{
Javier Martin492959c2013-01-29 07:31:17 -03001556 struct v4l2_subdev *sd = to_sd(ctrl);
1557 struct ov7670_info *info = to_state(sd);
1558
Hans Verkuilca075612009-03-18 13:23:13 -03001559 switch (ctrl->id) {
1560 case V4L2_CID_BRIGHTNESS:
Javier Martin492959c2013-01-29 07:31:17 -03001561 return ov7670_s_brightness(sd, ctrl->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001562 case V4L2_CID_CONTRAST:
Javier Martin492959c2013-01-29 07:31:17 -03001563 return ov7670_s_contrast(sd, ctrl->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001564 case V4L2_CID_SATURATION:
Javier Martin492959c2013-01-29 07:31:17 -03001565 return ov7670_s_sat_hue(sd,
1566 info->saturation->val, info->hue->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001567 case V4L2_CID_VFLIP:
Javier Martin492959c2013-01-29 07:31:17 -03001568 return ov7670_s_vflip(sd, ctrl->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001569 case V4L2_CID_HFLIP:
Javier Martin492959c2013-01-29 07:31:17 -03001570 return ov7670_s_hflip(sd, ctrl->val);
Jonathan Corbet81898672010-03-04 18:50:34 -03001571 case V4L2_CID_AUTOGAIN:
Javier Martin492959c2013-01-29 07:31:17 -03001572 /* Only set manual gain if auto gain is not explicitly
1573 turned on. */
1574 if (!ctrl->val) {
1575 /* ov7670_s_gain turns off auto gain */
1576 return ov7670_s_gain(sd, info->gain->val);
1577 }
1578 return ov7670_s_autogain(sd, ctrl->val);
Jonathan Corbet364e9332010-03-05 16:48:39 -03001579 case V4L2_CID_EXPOSURE_AUTO:
Javier Martin492959c2013-01-29 07:31:17 -03001580 /* Only set manual exposure if auto exposure is not explicitly
1581 turned on. */
1582 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
1583 /* ov7670_s_exp turns off auto exposure */
1584 return ov7670_s_exp(sd, info->exposure->val);
1585 }
1586 return ov7670_s_autoexp(sd, ctrl->val);
Akinobu Mitab48d9082017-11-24 09:40:45 -05001587 case V4L2_CID_TEST_PATTERN:
1588 return ov7670_s_test_pattern(sd, ctrl->val);
Hans Verkuilca075612009-03-18 13:23:13 -03001589 }
1590 return -EINVAL;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001591}
1592
Javier Martin492959c2013-01-29 07:31:17 -03001593static const struct v4l2_ctrl_ops ov7670_ctrl_ops = {
1594 .s_ctrl = ov7670_s_ctrl,
1595 .g_volatile_ctrl = ov7670_g_volatile_ctrl,
1596};
Jonathan Corbet111f3352006-11-04 09:26:00 -03001597
Hans Verkuilb794aab2009-03-18 13:24:05 -03001598#ifdef CONFIG_VIDEO_ADV_DEBUG
1599static int ov7670_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1600{
Hans Verkuilb794aab2009-03-18 13:24:05 -03001601 unsigned char val = 0;
1602 int ret;
1603
Hans Verkuilb794aab2009-03-18 13:24:05 -03001604 ret = ov7670_read(sd, reg->reg & 0xff, &val);
1605 reg->val = val;
1606 reg->size = 1;
1607 return ret;
1608}
1609
Hans Verkuil977ba3b12013-03-24 08:28:46 -03001610static int ov7670_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
Hans Verkuilb794aab2009-03-18 13:24:05 -03001611{
Hans Verkuilb794aab2009-03-18 13:24:05 -03001612 ov7670_write(sd, reg->reg & 0xff, reg->val & 0xff);
1613 return 0;
1614}
1615#endif
1616
Wenyou Yang71862f62017-10-16 05:14:27 +02001617static int ov7670_s_power(struct v4l2_subdev *sd, int on)
1618{
1619 struct ov7670_info *info = to_state(sd);
1620
1621 if (info->pwdn_gpio)
1622 gpiod_set_value(info->pwdn_gpio, !on);
1623 if (on && info->resetb_gpio) {
1624 gpiod_set_value(info->resetb_gpio, 1);
1625 usleep_range(500, 1000);
1626 gpiod_set_value(info->resetb_gpio, 0);
1627 usleep_range(3000, 5000);
1628 }
1629
1630 return 0;
1631}
1632
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001633static void ov7670_get_default_format(struct v4l2_subdev *sd,
1634 struct v4l2_mbus_framefmt *format)
1635{
1636 struct ov7670_info *info = to_state(sd);
1637
1638 format->width = info->devtype->win_sizes[0].width;
1639 format->height = info->devtype->win_sizes[0].height;
1640 format->colorspace = info->fmt->colorspace;
1641 format->code = info->fmt->mbus_code;
1642 format->field = V4L2_FIELD_NONE;
1643}
1644
1645#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1646static int ov7670_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1647{
1648 struct v4l2_mbus_framefmt *format =
1649 v4l2_subdev_get_try_format(sd, fh->pad, 0);
1650
1651 ov7670_get_default_format(sd, format);
1652
1653 return 0;
1654}
1655#endif
1656
Hans Verkuil14386c22009-03-18 13:01:06 -03001657/* ----------------------------------------------------------------------- */
1658
1659static const struct v4l2_subdev_core_ops ov7670_core_ops = {
Hans Verkuil14386c22009-03-18 13:01:06 -03001660 .reset = ov7670_reset,
1661 .init = ov7670_init,
Akinobu Mita7852adf2018-11-12 11:00:53 -05001662 .log_status = v4l2_ctrl_subdev_log_status,
1663 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1664 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Hans Verkuilb794aab2009-03-18 13:24:05 -03001665#ifdef CONFIG_VIDEO_ADV_DEBUG
1666 .g_register = ov7670_g_register,
1667 .s_register = ov7670_s_register,
1668#endif
Hans Verkuil14386c22009-03-18 13:01:06 -03001669};
1670
1671static const struct v4l2_subdev_video_ops ov7670_video_ops = {
Hans Verkuil44711092018-01-22 04:00:45 -05001672 .s_frame_interval = ov7670_s_frame_interval,
1673 .g_frame_interval = ov7670_g_frame_interval,
Hans Verkuil17bef882015-03-04 01:48:00 -08001674};
1675
1676static const struct v4l2_subdev_pad_ops ov7670_pad_ops = {
1677 .enum_frame_interval = ov7670_enum_frame_interval,
1678 .enum_frame_size = ov7670_enum_frame_size,
Hans Verkuilebcff5f2015-04-09 04:01:33 -03001679 .enum_mbus_code = ov7670_enum_mbus_code,
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001680 .get_fmt = ov7670_get_fmt,
Hans Verkuil717fd5b2015-04-09 06:24:36 -03001681 .set_fmt = ov7670_set_fmt,
Hans Verkuil14386c22009-03-18 13:01:06 -03001682};
1683
1684static const struct v4l2_subdev_ops ov7670_ops = {
1685 .core = &ov7670_core_ops,
1686 .video = &ov7670_video_ops,
Hans Verkuil17bef882015-03-04 01:48:00 -08001687 .pad = &ov7670_pad_ops,
Hans Verkuil14386c22009-03-18 13:01:06 -03001688};
1689
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001690#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1691static const struct v4l2_subdev_internal_ops ov7670_subdev_internal_ops = {
1692 .open = ov7670_open,
1693};
1694#endif
1695
Hans Verkuil14386c22009-03-18 13:01:06 -03001696/* ----------------------------------------------------------------------- */
1697
Javier Martind058e232013-01-29 07:12:13 -03001698static const struct ov7670_devtype ov7670_devdata[] = {
1699 [MODEL_OV7670] = {
1700 .win_sizes = ov7670_win_sizes,
1701 .n_win_sizes = ARRAY_SIZE(ov7670_win_sizes),
Javier Martinf6dd9272013-01-29 07:16:59 -03001702 .set_framerate = ov7670_set_framerate_legacy,
1703 .get_framerate = ov7670_get_framerate_legacy,
Javier Martind058e232013-01-29 07:12:13 -03001704 },
1705 [MODEL_OV7675] = {
1706 .win_sizes = ov7675_win_sizes,
1707 .n_win_sizes = ARRAY_SIZE(ov7675_win_sizes),
Javier Martinf6dd9272013-01-29 07:16:59 -03001708 .set_framerate = ov7675_set_framerate,
1709 .get_framerate = ov7675_get_framerate,
Javier Martind058e232013-01-29 07:12:13 -03001710 },
1711};
1712
Hans Verkuila0c41642016-12-12 12:08:18 -02001713static int ov7670_init_gpio(struct i2c_client *client, struct ov7670_info *info)
1714{
1715 info->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
1716 GPIOD_OUT_LOW);
1717 if (IS_ERR(info->pwdn_gpio)) {
1718 dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
1719 return PTR_ERR(info->pwdn_gpio);
1720 }
1721
1722 info->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1723 GPIOD_OUT_LOW);
1724 if (IS_ERR(info->resetb_gpio)) {
1725 dev_info(&client->dev, "can't get %s GPIO\n", "reset");
1726 return PTR_ERR(info->resetb_gpio);
1727 }
1728
1729 usleep_range(3000, 5000);
1730
1731 return 0;
1732}
1733
Jacopo Mondi01b84442018-01-24 04:30:50 -05001734/*
1735 * ov7670_parse_dt() - Parse device tree to collect mbus configuration
1736 * properties
1737 */
1738static int ov7670_parse_dt(struct device *dev,
1739 struct ov7670_info *info)
1740{
1741 struct fwnode_handle *fwnode = dev_fwnode(dev);
Sakari Ailus60359a22018-07-31 05:15:50 -04001742 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
Jacopo Mondi01b84442018-01-24 04:30:50 -05001743 struct fwnode_handle *ep;
1744 int ret;
1745
1746 if (!fwnode)
1747 return -EINVAL;
1748
1749 info->pclk_hb_disable = false;
1750 if (fwnode_property_present(fwnode, "ov7670,pclk-hb-disable"))
1751 info->pclk_hb_disable = true;
1752
1753 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1754 if (!ep)
1755 return -EINVAL;
1756
1757 ret = v4l2_fwnode_endpoint_parse(ep, &bus_cfg);
Jacopo Mondi09a48f72018-05-31 13:35:03 -04001758 fwnode_handle_put(ep);
1759 if (ret)
Jacopo Mondi01b84442018-01-24 04:30:50 -05001760 return ret;
Jacopo Mondi01b84442018-01-24 04:30:50 -05001761
1762 if (bus_cfg.bus_type != V4L2_MBUS_PARALLEL) {
1763 dev_err(dev, "Unsupported media bus type\n");
Jacopo Mondi01b84442018-01-24 04:30:50 -05001764 return ret;
1765 }
1766 info->mbus_config = bus_cfg.bus.parallel.flags;
1767
1768 return 0;
1769}
1770
Hans Verkuil14386c22009-03-18 13:01:06 -03001771static int ov7670_probe(struct i2c_client *client,
1772 const struct i2c_device_id *id)
1773{
Javier Martinf6dd9272013-01-29 07:16:59 -03001774 struct v4l2_fract tpf;
Hans Verkuil14386c22009-03-18 13:01:06 -03001775 struct v4l2_subdev *sd;
Jonathan Corbetf9a76152006-11-19 19:04:55 -03001776 struct ov7670_info *info;
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001777 int ret;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001778
Laurent Pinchartc02b2112013-05-02 08:29:43 -03001779 info = devm_kzalloc(&client->dev, sizeof(*info), GFP_KERNEL);
Hans Verkuil14386c22009-03-18 13:01:06 -03001780 if (info == NULL)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001781 return -ENOMEM;
Hans Verkuil14386c22009-03-18 13:01:06 -03001782 sd = &info->sd;
1783 v4l2_i2c_subdev_init(sd, client, &ov7670_ops);
1784
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001785#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1786 sd->internal_ops = &ov7670_subdev_internal_ops;
Akinobu Mita7852adf2018-11-12 11:00:53 -05001787 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001788#endif
1789
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001790 info->clock_speed = 30; /* default: a guess */
Jacopo Mondi01b84442018-01-24 04:30:50 -05001791
1792 if (dev_fwnode(&client->dev)) {
1793 ret = ov7670_parse_dt(&client->dev, info);
1794 if (ret)
1795 return ret;
1796
1797 } else if (client->dev.platform_data) {
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001798 struct ov7670_config *config = client->dev.platform_data;
1799
1800 /*
1801 * Must apply configuration before initializing device, because it
1802 * selects I/O method.
1803 */
1804 info->min_width = config->min_width;
1805 info->min_height = config->min_height;
1806 info->use_smbus = config->use_smbus;
1807
1808 if (config->clock_speed)
1809 info->clock_speed = config->clock_speed;
Javier Martin04ee6d92013-01-29 07:23:42 -03001810
Jacopo Mondi61da76b2017-12-29 07:22:26 -05001811 if (config->pll_bypass)
Javier Martin04ee6d92013-01-29 07:23:42 -03001812 info->pll_bypass = true;
Javier Martinee952582013-01-29 07:26:38 -03001813
1814 if (config->pclk_hb_disable)
1815 info->pclk_hb_disable = true;
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001816 }
1817
Lubomir Rintel786fa582018-10-04 17:29:03 -04001818 info->clk = devm_clk_get(&client->dev, "xclk"); /* optional */
1819 if (IS_ERR(info->clk)) {
1820 ret = PTR_ERR(info->clk);
1821 if (ret == -ENOENT)
1822 info->clk = NULL;
1823 else
1824 return ret;
1825 }
1826 if (info->clk) {
1827 ret = clk_prepare_enable(info->clk);
1828 if (ret)
1829 return ret;
Hans Verkuil0a024d62016-08-16 16:00:57 -03001830
Lubomir Rintel786fa582018-10-04 17:29:03 -04001831 info->clock_speed = clk_get_rate(info->clk) / 1000000;
1832 if (info->clock_speed < 10 || info->clock_speed > 48) {
1833 ret = -EINVAL;
1834 goto clk_disable;
1835 }
Hans Verkuil0a024d62016-08-16 16:00:57 -03001836 }
1837
Wenyou Yang71862f62017-10-16 05:14:27 +02001838 ret = ov7670_init_gpio(client, info);
1839 if (ret)
1840 goto clk_disable;
1841
1842 ov7670_s_power(sd, 1);
1843
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001844 /* Make sure it's an ov7670 */
1845 ret = ov7670_detect(sd);
1846 if (ret) {
1847 v4l_dbg(1, debug, client,
1848 "chip found @ 0x%x (%s) is not an ov7670 chip.\n",
1849 client->addr << 1, client->adapter->name);
Wenyou Yang71862f62017-10-16 05:14:27 +02001850 goto power_off;
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001851 }
1852 v4l_info(client, "chip found @ 0x%02x (%s)\n",
1853 client->addr << 1, client->adapter->name);
1854
Javier Martind058e232013-01-29 07:12:13 -03001855 info->devtype = &ov7670_devdata[id->driver_data];
Hans Verkuil3c7c9372011-01-08 07:08:02 -03001856 info->fmt = &ov7670_formats[0];
Lubomir Rintel5556ab22019-01-15 06:54:47 -02001857 info->wsize = &info->devtype->win_sizes[0];
Wenyou Yangc0662dd2017-10-16 05:14:26 +02001858
1859 ov7670_get_default_format(sd, &info->format);
1860
Javier Martinf6dd9272013-01-29 07:16:59 -03001861 info->clkrc = 0;
1862
1863 /* Set default frame rate to 30 fps */
1864 tpf.numerator = 1;
1865 tpf.denominator = 30;
1866 info->devtype->set_framerate(sd, &tpf);
1867
Javier Martin492959c2013-01-29 07:31:17 -03001868 v4l2_ctrl_handler_init(&info->hdl, 10);
1869 v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1870 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1871 v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1872 V4L2_CID_CONTRAST, 0, 127, 1, 64);
1873 v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1874 V4L2_CID_VFLIP, 0, 1, 1, 0);
1875 v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1876 V4L2_CID_HFLIP, 0, 1, 1, 0);
1877 info->saturation = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1878 V4L2_CID_SATURATION, 0, 256, 1, 128);
1879 info->hue = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1880 V4L2_CID_HUE, -180, 180, 5, 0);
1881 info->gain = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1882 V4L2_CID_GAIN, 0, 255, 1, 128);
1883 info->auto_gain = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1884 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1885 info->exposure = v4l2_ctrl_new_std(&info->hdl, &ov7670_ctrl_ops,
1886 V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
1887 info->auto_exposure = v4l2_ctrl_new_std_menu(&info->hdl, &ov7670_ctrl_ops,
1888 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
1889 V4L2_EXPOSURE_AUTO);
Akinobu Mitab48d9082017-11-24 09:40:45 -05001890 v4l2_ctrl_new_std_menu_items(&info->hdl, &ov7670_ctrl_ops,
1891 V4L2_CID_TEST_PATTERN,
1892 ARRAY_SIZE(ov7670_test_pattern_menu) - 1, 0, 0,
1893 ov7670_test_pattern_menu);
Javier Martin492959c2013-01-29 07:31:17 -03001894 sd->ctrl_handler = &info->hdl;
1895 if (info->hdl.error) {
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001896 ret = info->hdl.error;
Javier Martin492959c2013-01-29 07:31:17 -03001897
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001898 goto hdl_free;
Javier Martin492959c2013-01-29 07:31:17 -03001899 }
1900 /*
1901 * We have checked empirically that hw allows to read back the gain
1902 * value chosen by auto gain but that's not the case for auto exposure.
1903 */
1904 v4l2_ctrl_auto_cluster(2, &info->auto_gain, 0, true);
1905 v4l2_ctrl_auto_cluster(2, &info->auto_exposure,
1906 V4L2_EXPOSURE_MANUAL, false);
1907 v4l2_ctrl_cluster(2, &info->saturation);
Wenyou Yangd94a26f2017-10-16 05:14:25 +02001908
1909#if defined(CONFIG_MEDIA_CONTROLLER)
1910 info->pad.flags = MEDIA_PAD_FL_SOURCE;
1911 info->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1912 ret = media_entity_pads_init(&info->sd.entity, 1, &info->pad);
1913 if (ret < 0)
1914 goto hdl_free;
1915#endif
1916
Javier Martin492959c2013-01-29 07:31:17 -03001917 v4l2_ctrl_handler_setup(&info->hdl);
1918
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001919 ret = v4l2_async_register_subdev(&info->sd);
1920 if (ret < 0)
Wenyou Yangd94a26f2017-10-16 05:14:25 +02001921 goto entity_cleanup;
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001922
Jonathan Corbet111f3352006-11-04 09:26:00 -03001923 return 0;
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001924
Wenyou Yangd94a26f2017-10-16 05:14:25 +02001925entity_cleanup:
Wenyou Yangd94a26f2017-10-16 05:14:25 +02001926 media_entity_cleanup(&info->sd.entity);
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001927hdl_free:
1928 v4l2_ctrl_handler_free(&info->hdl);
Wenyou Yang71862f62017-10-16 05:14:27 +02001929power_off:
1930 ov7670_s_power(sd, 0);
Hans Verkuil0a024d62016-08-16 16:00:57 -03001931clk_disable:
1932 clk_disable_unprepare(info->clk);
Hans Verkuil7d1b8612016-08-16 15:55:58 -03001933 return ret;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001934}
1935
1936
Hans Verkuil14386c22009-03-18 13:01:06 -03001937static int ov7670_remove(struct i2c_client *client)
Jonathan Corbet111f3352006-11-04 09:26:00 -03001938{
Hans Verkuil14386c22009-03-18 13:01:06 -03001939 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Javier Martin492959c2013-01-29 07:31:17 -03001940 struct ov7670_info *info = to_state(sd);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001941
Akinobu Mita344aa832017-11-24 09:40:44 -05001942 v4l2_async_unregister_subdev(sd);
Javier Martin492959c2013-01-29 07:31:17 -03001943 v4l2_ctrl_handler_free(&info->hdl);
Hans Verkuil0a024d62016-08-16 16:00:57 -03001944 clk_disable_unprepare(info->clk);
Wenyou Yangd94a26f2017-10-16 05:14:25 +02001945 media_entity_cleanup(&info->sd.entity);
Wenyou Yang71862f62017-10-16 05:14:27 +02001946 ov7670_s_power(sd, 0);
Hans Verkuil14386c22009-03-18 13:01:06 -03001947 return 0;
Jonathan Corbet111f3352006-11-04 09:26:00 -03001948}
1949
Hans Verkuil14386c22009-03-18 13:01:06 -03001950static const struct i2c_device_id ov7670_id[] = {
Javier Martind058e232013-01-29 07:12:13 -03001951 { "ov7670", MODEL_OV7670 },
1952 { "ov7675", MODEL_OV7675 },
Hans Verkuil14386c22009-03-18 13:01:06 -03001953 { }
Jonathan Corbet111f3352006-11-04 09:26:00 -03001954};
Hans Verkuil14386c22009-03-18 13:01:06 -03001955MODULE_DEVICE_TABLE(i2c, ov7670_id);
Jonathan Corbet111f3352006-11-04 09:26:00 -03001956
Hans Verkuila0c41642016-12-12 12:08:18 -02001957#if IS_ENABLED(CONFIG_OF)
1958static const struct of_device_id ov7670_of_match[] = {
1959 { .compatible = "ovti,ov7670", },
1960 { /* sentinel */ },
1961};
1962MODULE_DEVICE_TABLE(of, ov7670_of_match);
1963#endif
1964
Hans Verkuilef2ac772010-09-15 15:08:09 -03001965static struct i2c_driver ov7670_driver = {
1966 .driver = {
Hans Verkuilef2ac772010-09-15 15:08:09 -03001967 .name = "ov7670",
Hans Verkuila0c41642016-12-12 12:08:18 -02001968 .of_match_table = of_match_ptr(ov7670_of_match),
Hans Verkuilef2ac772010-09-15 15:08:09 -03001969 },
1970 .probe = ov7670_probe,
1971 .remove = ov7670_remove,
1972 .id_table = ov7670_id,
Hans Verkuil14386c22009-03-18 13:01:06 -03001973};
Hans Verkuilef2ac772010-09-15 15:08:09 -03001974
Axel Linc6e8d862012-02-12 06:56:32 -03001975module_i2c_driver(ov7670_driver);