xref: /OK3568_Linux_fs/kernel/drivers/media/i2c/ov9650.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Omnivision OV9650/OV9652 CMOS Image Sensor driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2013, Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Register definitions and initial settings based on a driver written
8*4882a593Smuzhiyun  * by Vladimir Fonov.
9*4882a593Smuzhiyun  * Copyright (c) 2010, Vladimir Fonov
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #include <linux/clk.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/gpio.h>
14*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
15*4882a593Smuzhiyun #include <linux/i2c.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/media.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/ratelimit.h>
20*4882a593Smuzhiyun #include <linux/regmap.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include <linux/string.h>
23*4882a593Smuzhiyun #include <linux/videodev2.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <media/media-entity.h>
26*4882a593Smuzhiyun #include <media/v4l2-async.h>
27*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
28*4882a593Smuzhiyun #include <media/v4l2-device.h>
29*4882a593Smuzhiyun #include <media/v4l2-event.h>
30*4882a593Smuzhiyun #include <media/v4l2-image-sizes.h>
31*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
32*4882a593Smuzhiyun #include <media/v4l2-mediabus.h>
33*4882a593Smuzhiyun #include <media/i2c/ov9650.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static int debug;
36*4882a593Smuzhiyun module_param(debug, int, 0644);
37*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Debug level (0-2)");
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define DRIVER_NAME "OV9650"
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * OV9650/OV9652 register definitions
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun #define REG_GAIN		0x00	/* Gain control, AGC[7:0] */
45*4882a593Smuzhiyun #define REG_BLUE		0x01	/* AWB - Blue channel gain */
46*4882a593Smuzhiyun #define REG_RED			0x02	/* AWB - Red channel gain */
47*4882a593Smuzhiyun #define REG_VREF		0x03	/* [7:6] - AGC[9:8], [5:3]/[2:0] */
48*4882a593Smuzhiyun #define  VREF_GAIN_MASK		0xc0	/* - VREF end/start low 3 bits */
49*4882a593Smuzhiyun #define REG_COM1		0x04
50*4882a593Smuzhiyun #define  COM1_CCIR656		0x40
51*4882a593Smuzhiyun #define REG_B_AVE		0x05
52*4882a593Smuzhiyun #define REG_GB_AVE		0x06
53*4882a593Smuzhiyun #define REG_GR_AVE		0x07
54*4882a593Smuzhiyun #define REG_R_AVE		0x08
55*4882a593Smuzhiyun #define REG_COM2		0x09
56*4882a593Smuzhiyun #define REG_PID			0x0a	/* Product ID MSB */
57*4882a593Smuzhiyun #define REG_VER			0x0b	/* Product ID LSB */
58*4882a593Smuzhiyun #define REG_COM3		0x0c
59*4882a593Smuzhiyun #define  COM3_SWAP		0x40
60*4882a593Smuzhiyun #define  COM3_VARIOPIXEL1	0x04
61*4882a593Smuzhiyun #define REG_COM4		0x0d	/* Vario Pixels  */
62*4882a593Smuzhiyun #define  COM4_VARIOPIXEL2	0x80
63*4882a593Smuzhiyun #define REG_COM5		0x0e	/* System clock options */
64*4882a593Smuzhiyun #define  COM5_SLAVE_MODE	0x10
65*4882a593Smuzhiyun #define  COM5_SYSTEMCLOCK48MHZ	0x80
66*4882a593Smuzhiyun #define REG_COM6		0x0f	/* HREF & ADBLC options */
67*4882a593Smuzhiyun #define REG_AECH		0x10	/* Exposure value, AEC[9:2] */
68*4882a593Smuzhiyun #define REG_CLKRC		0x11	/* Clock control */
69*4882a593Smuzhiyun #define  CLK_EXT		0x40	/* Use external clock directly */
70*4882a593Smuzhiyun #define  CLK_SCALE		0x3f	/* Mask for internal clock scale */
71*4882a593Smuzhiyun #define REG_COM7		0x12	/* SCCB reset, output format */
72*4882a593Smuzhiyun #define  COM7_RESET		0x80
73*4882a593Smuzhiyun #define  COM7_FMT_MASK		0x38
74*4882a593Smuzhiyun #define  COM7_FMT_VGA		0x40
75*4882a593Smuzhiyun #define	 COM7_FMT_CIF		0x20
76*4882a593Smuzhiyun #define  COM7_FMT_QVGA		0x10
77*4882a593Smuzhiyun #define  COM7_FMT_QCIF		0x08
78*4882a593Smuzhiyun #define	 COM7_RGB		0x04
79*4882a593Smuzhiyun #define	 COM7_YUV		0x00
80*4882a593Smuzhiyun #define	 COM7_BAYER		0x01
81*4882a593Smuzhiyun #define	 COM7_PBAYER		0x05
82*4882a593Smuzhiyun #define REG_COM8		0x13	/* AGC/AEC options */
83*4882a593Smuzhiyun #define  COM8_FASTAEC		0x80	/* Enable fast AGC/AEC */
84*4882a593Smuzhiyun #define  COM8_AECSTEP		0x40	/* Unlimited AEC step size */
85*4882a593Smuzhiyun #define  COM8_BFILT		0x20	/* Band filter enable */
86*4882a593Smuzhiyun #define  COM8_AGC		0x04	/* Auto gain enable */
87*4882a593Smuzhiyun #define  COM8_AWB		0x02	/* White balance enable */
88*4882a593Smuzhiyun #define  COM8_AEC		0x01	/* Auto exposure enable */
89*4882a593Smuzhiyun #define REG_COM9		0x14	/* Gain ceiling */
90*4882a593Smuzhiyun #define  COM9_GAIN_CEIL_MASK	0x70	/* */
91*4882a593Smuzhiyun #define REG_COM10		0x15	/* PCLK, HREF, HSYNC signals polarity */
92*4882a593Smuzhiyun #define  COM10_HSYNC		0x40	/* HSYNC instead of HREF */
93*4882a593Smuzhiyun #define  COM10_PCLK_HB		0x20	/* Suppress PCLK on horiz blank */
94*4882a593Smuzhiyun #define  COM10_HREF_REV		0x08	/* Reverse HREF */
95*4882a593Smuzhiyun #define  COM10_VS_LEAD		0x04	/* VSYNC on clock leading edge */
96*4882a593Smuzhiyun #define  COM10_VS_NEG		0x02	/* VSYNC negative */
97*4882a593Smuzhiyun #define  COM10_HS_NEG		0x01	/* HSYNC negative */
98*4882a593Smuzhiyun #define REG_HSTART		0x17	/* Horiz start high bits */
99*4882a593Smuzhiyun #define REG_HSTOP		0x18	/* Horiz stop high bits */
100*4882a593Smuzhiyun #define REG_VSTART		0x19	/* Vert start high bits */
101*4882a593Smuzhiyun #define REG_VSTOP		0x1a	/* Vert stop high bits */
102*4882a593Smuzhiyun #define REG_PSHFT		0x1b	/* Pixel delay after HREF */
103*4882a593Smuzhiyun #define REG_MIDH		0x1c	/* Manufacturer ID MSB */
104*4882a593Smuzhiyun #define REG_MIDL		0x1d	/* Manufufacturer ID LSB */
105*4882a593Smuzhiyun #define REG_MVFP		0x1e	/* Image mirror/flip */
106*4882a593Smuzhiyun #define  MVFP_MIRROR		0x20	/* Mirror image */
107*4882a593Smuzhiyun #define  MVFP_FLIP		0x10	/* Vertical flip */
108*4882a593Smuzhiyun #define REG_BOS			0x20	/* B channel Offset */
109*4882a593Smuzhiyun #define REG_GBOS		0x21	/* Gb channel Offset */
110*4882a593Smuzhiyun #define REG_GROS		0x22	/* Gr channel Offset */
111*4882a593Smuzhiyun #define REG_ROS			0x23	/* R channel Offset */
112*4882a593Smuzhiyun #define REG_AEW			0x24	/* AGC upper limit */
113*4882a593Smuzhiyun #define REG_AEB			0x25	/* AGC lower limit */
114*4882a593Smuzhiyun #define REG_VPT			0x26	/* AGC/AEC fast mode op region */
115*4882a593Smuzhiyun #define REG_BBIAS		0x27	/* B channel output bias */
116*4882a593Smuzhiyun #define REG_GBBIAS		0x28	/* Gb channel output bias */
117*4882a593Smuzhiyun #define REG_GRCOM		0x29	/* Analog BLC & regulator */
118*4882a593Smuzhiyun #define REG_EXHCH		0x2a	/* Dummy pixel insert MSB */
119*4882a593Smuzhiyun #define REG_EXHCL		0x2b	/* Dummy pixel insert LSB */
120*4882a593Smuzhiyun #define REG_RBIAS		0x2c	/* R channel output bias */
121*4882a593Smuzhiyun #define REG_ADVFL		0x2d	/* LSB of dummy line insert */
122*4882a593Smuzhiyun #define REG_ADVFH		0x2e	/* MSB of dummy line insert */
123*4882a593Smuzhiyun #define REG_YAVE		0x2f	/* Y/G channel average value */
124*4882a593Smuzhiyun #define REG_HSYST		0x30	/* HSYNC rising edge delay LSB*/
125*4882a593Smuzhiyun #define REG_HSYEN		0x31	/* HSYNC falling edge delay LSB*/
126*4882a593Smuzhiyun #define REG_HREF		0x32	/* HREF pieces */
127*4882a593Smuzhiyun #define REG_CHLF		0x33	/* reserved */
128*4882a593Smuzhiyun #define REG_ADC			0x37	/* reserved */
129*4882a593Smuzhiyun #define REG_ACOM		0x38	/* reserved */
130*4882a593Smuzhiyun #define REG_OFON		0x39	/* Power down register */
131*4882a593Smuzhiyun #define  OFON_PWRDN		0x08	/* Power down bit */
132*4882a593Smuzhiyun #define REG_TSLB		0x3a	/* YUVU format */
133*4882a593Smuzhiyun #define  TSLB_YUYV_MASK		0x0c	/* UYVY or VYUY - see com13 */
134*4882a593Smuzhiyun #define REG_COM11		0x3b	/* Night mode, banding filter enable */
135*4882a593Smuzhiyun #define  COM11_NIGHT		0x80	/* Night mode enable */
136*4882a593Smuzhiyun #define  COM11_NMFR		0x60	/* Two bit NM frame rate */
137*4882a593Smuzhiyun #define  COM11_BANDING		0x01	/* Banding filter */
138*4882a593Smuzhiyun #define  COM11_AEC_REF_MASK	0x18	/* AEC reference area selection */
139*4882a593Smuzhiyun #define REG_COM12		0x3c	/* HREF option, UV average */
140*4882a593Smuzhiyun #define  COM12_HREF		0x80	/* HREF always */
141*4882a593Smuzhiyun #define REG_COM13		0x3d	/* Gamma selection, Color matrix en. */
142*4882a593Smuzhiyun #define  COM13_GAMMA		0x80	/* Gamma enable */
143*4882a593Smuzhiyun #define	 COM13_UVSAT		0x40	/* UV saturation auto adjustment */
144*4882a593Smuzhiyun #define  COM13_UVSWAP		0x01	/* V before U - w/TSLB */
145*4882a593Smuzhiyun #define REG_COM14		0x3e	/* Edge enhancement options */
146*4882a593Smuzhiyun #define  COM14_EDGE_EN		0x02
147*4882a593Smuzhiyun #define  COM14_EEF_X2		0x01
148*4882a593Smuzhiyun #define REG_EDGE		0x3f	/* Edge enhancement factor */
149*4882a593Smuzhiyun #define  EDGE_FACTOR_MASK	0x0f
150*4882a593Smuzhiyun #define REG_COM15		0x40	/* Output range, RGB 555/565 */
151*4882a593Smuzhiyun #define  COM15_R10F0		0x00	/* Data range 10 to F0 */
152*4882a593Smuzhiyun #define	 COM15_R01FE		0x80	/* 01 to FE */
153*4882a593Smuzhiyun #define  COM15_R00FF		0xc0	/* 00 to FF */
154*4882a593Smuzhiyun #define  COM15_RGB565		0x10	/* RGB565 output */
155*4882a593Smuzhiyun #define  COM15_RGB555		0x30	/* RGB555 output */
156*4882a593Smuzhiyun #define  COM15_SWAPRB		0x04	/* Swap R&B */
157*4882a593Smuzhiyun #define REG_COM16		0x41	/* Color matrix coeff options */
158*4882a593Smuzhiyun #define REG_COM17		0x42	/* Single frame out, banding filter */
159*4882a593Smuzhiyun /* n = 1...9, 0x4f..0x57 */
160*4882a593Smuzhiyun #define	REG_MTX(__n)		(0x4f + (__n) - 1)
161*4882a593Smuzhiyun #define REG_MTXS		0x58
162*4882a593Smuzhiyun /* Lens Correction Option 1...5, __n = 0...5 */
163*4882a593Smuzhiyun #define REG_LCC(__n)		(0x62 + (__n) - 1)
164*4882a593Smuzhiyun #define  LCC5_LCC_ENABLE	0x01	/* LCC5, enable lens correction */
165*4882a593Smuzhiyun #define  LCC5_LCC_COLOR		0x04
166*4882a593Smuzhiyun #define REG_MANU		0x67	/* Manual U value */
167*4882a593Smuzhiyun #define REG_MANV		0x68	/* Manual V value */
168*4882a593Smuzhiyun #define REG_HV			0x69	/* Manual banding filter MSB */
169*4882a593Smuzhiyun #define REG_MBD			0x6a	/* Manual banding filter value */
170*4882a593Smuzhiyun #define REG_DBLV		0x6b	/* reserved */
171*4882a593Smuzhiyun #define REG_GSP			0x6c	/* Gamma curve */
172*4882a593Smuzhiyun #define  GSP_LEN		15
173*4882a593Smuzhiyun #define REG_GST			0x7c	/* Gamma curve */
174*4882a593Smuzhiyun #define  GST_LEN		15
175*4882a593Smuzhiyun #define REG_COM21		0x8b
176*4882a593Smuzhiyun #define REG_COM22		0x8c	/* Edge enhancement, denoising */
177*4882a593Smuzhiyun #define  COM22_WHTPCOR		0x02	/* White pixel correction enable */
178*4882a593Smuzhiyun #define  COM22_WHTPCOROPT	0x01	/* White pixel correction option */
179*4882a593Smuzhiyun #define  COM22_DENOISE		0x10	/* White pixel correction option */
180*4882a593Smuzhiyun #define REG_COM23		0x8d	/* Color bar test, color gain */
181*4882a593Smuzhiyun #define  COM23_TEST_MODE	0x10
182*4882a593Smuzhiyun #define REG_DBLC1		0x8f	/* Digital BLC */
183*4882a593Smuzhiyun #define REG_DBLC_B		0x90	/* Digital BLC B channel offset */
184*4882a593Smuzhiyun #define REG_DBLC_R		0x91	/* Digital BLC R channel offset */
185*4882a593Smuzhiyun #define REG_DM_LNL		0x92	/* Dummy line low 8 bits */
186*4882a593Smuzhiyun #define REG_DM_LNH		0x93	/* Dummy line high 8 bits */
187*4882a593Smuzhiyun #define REG_LCCFB		0x9d	/* Lens Correction B channel */
188*4882a593Smuzhiyun #define REG_LCCFR		0x9e	/* Lens Correction R channel */
189*4882a593Smuzhiyun #define REG_DBLC_GB		0x9f	/* Digital BLC GB chan offset */
190*4882a593Smuzhiyun #define REG_DBLC_GR		0xa0	/* Digital BLC GR chan offset */
191*4882a593Smuzhiyun #define REG_AECHM		0xa1	/* Exposure value - bits AEC[15:10] */
192*4882a593Smuzhiyun #define REG_BD50ST		0xa2	/* Banding filter value for 50Hz */
193*4882a593Smuzhiyun #define REG_BD60ST		0xa3	/* Banding filter value for 60Hz */
194*4882a593Smuzhiyun #define REG_NULL		0xff	/* Array end token */
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun #define DEF_CLKRC		0x80
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun #define OV965X_ID(_msb, _lsb)	((_msb) << 8 | (_lsb))
199*4882a593Smuzhiyun #define OV9650_ID		0x9650
200*4882a593Smuzhiyun #define OV9652_ID		0x9652
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun struct ov965x_ctrls {
203*4882a593Smuzhiyun 	struct v4l2_ctrl_handler handler;
204*4882a593Smuzhiyun 	struct {
205*4882a593Smuzhiyun 		struct v4l2_ctrl *auto_exp;
206*4882a593Smuzhiyun 		struct v4l2_ctrl *exposure;
207*4882a593Smuzhiyun 	};
208*4882a593Smuzhiyun 	struct {
209*4882a593Smuzhiyun 		struct v4l2_ctrl *auto_wb;
210*4882a593Smuzhiyun 		struct v4l2_ctrl *blue_balance;
211*4882a593Smuzhiyun 		struct v4l2_ctrl *red_balance;
212*4882a593Smuzhiyun 	};
213*4882a593Smuzhiyun 	struct {
214*4882a593Smuzhiyun 		struct v4l2_ctrl *hflip;
215*4882a593Smuzhiyun 		struct v4l2_ctrl *vflip;
216*4882a593Smuzhiyun 	};
217*4882a593Smuzhiyun 	struct {
218*4882a593Smuzhiyun 		struct v4l2_ctrl *auto_gain;
219*4882a593Smuzhiyun 		struct v4l2_ctrl *gain;
220*4882a593Smuzhiyun 	};
221*4882a593Smuzhiyun 	struct v4l2_ctrl *brightness;
222*4882a593Smuzhiyun 	struct v4l2_ctrl *saturation;
223*4882a593Smuzhiyun 	struct v4l2_ctrl *sharpness;
224*4882a593Smuzhiyun 	struct v4l2_ctrl *light_freq;
225*4882a593Smuzhiyun 	u8 update;
226*4882a593Smuzhiyun };
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun struct ov965x_framesize {
229*4882a593Smuzhiyun 	u16 width;
230*4882a593Smuzhiyun 	u16 height;
231*4882a593Smuzhiyun 	u16 max_exp_lines;
232*4882a593Smuzhiyun 	const u8 *regs;
233*4882a593Smuzhiyun };
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun struct ov965x_interval {
236*4882a593Smuzhiyun 	struct v4l2_fract interval;
237*4882a593Smuzhiyun 	/* Maximum resolution for this interval */
238*4882a593Smuzhiyun 	struct v4l2_frmsize_discrete size;
239*4882a593Smuzhiyun 	u8 clkrc_div;
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun enum gpio_id {
243*4882a593Smuzhiyun 	GPIO_PWDN,
244*4882a593Smuzhiyun 	GPIO_RST,
245*4882a593Smuzhiyun 	NUM_GPIOS,
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun struct ov965x {
249*4882a593Smuzhiyun 	struct v4l2_subdev sd;
250*4882a593Smuzhiyun 	struct media_pad pad;
251*4882a593Smuzhiyun 	enum v4l2_mbus_type bus_type;
252*4882a593Smuzhiyun 	struct gpio_desc *gpios[NUM_GPIOS];
253*4882a593Smuzhiyun 	/* External master clock frequency */
254*4882a593Smuzhiyun 	unsigned long mclk_frequency;
255*4882a593Smuzhiyun 	struct clk *clk;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* Protects the struct fields below */
258*4882a593Smuzhiyun 	struct mutex lock;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	struct regmap *regmap;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Exposure row interval in us */
263*4882a593Smuzhiyun 	unsigned int exp_row_interval;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	unsigned short id;
266*4882a593Smuzhiyun 	const struct ov965x_framesize *frame_size;
267*4882a593Smuzhiyun 	/* YUYV sequence (pixel format) control register */
268*4882a593Smuzhiyun 	u8 tslb_reg;
269*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt format;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	struct ov965x_ctrls ctrls;
272*4882a593Smuzhiyun 	/* Pointer to frame rate control data structure */
273*4882a593Smuzhiyun 	const struct ov965x_interval *fiv;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	int streaming;
276*4882a593Smuzhiyun 	int power;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	u8 apply_frame_fmt;
279*4882a593Smuzhiyun };
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun struct i2c_rv {
282*4882a593Smuzhiyun 	u8 addr;
283*4882a593Smuzhiyun 	u8 value;
284*4882a593Smuzhiyun };
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun static const struct i2c_rv ov965x_init_regs[] = {
287*4882a593Smuzhiyun 	{ REG_COM2, 0x10 },	/* Set soft sleep mode */
288*4882a593Smuzhiyun 	{ REG_COM5, 0x00 },	/* System clock options */
289*4882a593Smuzhiyun 	{ REG_COM2, 0x01 },	/* Output drive, soft sleep mode */
290*4882a593Smuzhiyun 	{ REG_COM10, 0x00 },	/* Slave mode, HREF vs HSYNC, signals negate */
291*4882a593Smuzhiyun 	{ REG_EDGE, 0xa6 },	/* Edge enhancement treshhold and factor */
292*4882a593Smuzhiyun 	{ REG_COM16, 0x02 },	/* Color matrix coeff double option */
293*4882a593Smuzhiyun 	{ REG_COM17, 0x08 },	/* Single frame out, banding filter */
294*4882a593Smuzhiyun 	{ 0x16, 0x06 },
295*4882a593Smuzhiyun 	{ REG_CHLF, 0xc0 },	/* Reserved  */
296*4882a593Smuzhiyun 	{ 0x34, 0xbf },
297*4882a593Smuzhiyun 	{ 0xa8, 0x80 },
298*4882a593Smuzhiyun 	{ 0x96, 0x04 },
299*4882a593Smuzhiyun 	{ 0x8e, 0x00 },
300*4882a593Smuzhiyun 	{ REG_COM12, 0x77 },	/* HREF option, UV average  */
301*4882a593Smuzhiyun 	{ 0x8b, 0x06 },
302*4882a593Smuzhiyun 	{ 0x35, 0x91 },
303*4882a593Smuzhiyun 	{ 0x94, 0x88 },
304*4882a593Smuzhiyun 	{ 0x95, 0x88 },
305*4882a593Smuzhiyun 	{ REG_COM15, 0xc1 },	/* Output range, RGB 555/565 */
306*4882a593Smuzhiyun 	{ REG_GRCOM, 0x2f },	/* Analog BLC & regulator */
307*4882a593Smuzhiyun 	{ REG_COM6, 0x43 },	/* HREF & ADBLC options */
308*4882a593Smuzhiyun 	{ REG_COM8, 0xe5 },	/* AGC/AEC options */
309*4882a593Smuzhiyun 	{ REG_COM13, 0x90 },	/* Gamma selection, colour matrix, UV delay */
310*4882a593Smuzhiyun 	{ REG_HV, 0x80 },	/* Manual banding filter MSB  */
311*4882a593Smuzhiyun 	{ 0x5c, 0x96 },		/* Reserved up to 0xa5 */
312*4882a593Smuzhiyun 	{ 0x5d, 0x96 },
313*4882a593Smuzhiyun 	{ 0x5e, 0x10 },
314*4882a593Smuzhiyun 	{ 0x59, 0xeb },
315*4882a593Smuzhiyun 	{ 0x5a, 0x9c },
316*4882a593Smuzhiyun 	{ 0x5b, 0x55 },
317*4882a593Smuzhiyun 	{ 0x43, 0xf0 },
318*4882a593Smuzhiyun 	{ 0x44, 0x10 },
319*4882a593Smuzhiyun 	{ 0x45, 0x55 },
320*4882a593Smuzhiyun 	{ 0x46, 0x86 },
321*4882a593Smuzhiyun 	{ 0x47, 0x64 },
322*4882a593Smuzhiyun 	{ 0x48, 0x86 },
323*4882a593Smuzhiyun 	{ 0x5f, 0xe0 },
324*4882a593Smuzhiyun 	{ 0x60, 0x8c },
325*4882a593Smuzhiyun 	{ 0x61, 0x20 },
326*4882a593Smuzhiyun 	{ 0xa5, 0xd9 },
327*4882a593Smuzhiyun 	{ 0xa4, 0x74 },		/* reserved */
328*4882a593Smuzhiyun 	{ REG_COM23, 0x02 },	/* Color gain analog/_digital_ */
329*4882a593Smuzhiyun 	{ REG_COM8, 0xe7 },	/* Enable AEC, AWB, AEC */
330*4882a593Smuzhiyun 	{ REG_COM22, 0x23 },	/* Edge enhancement, denoising */
331*4882a593Smuzhiyun 	{ 0xa9, 0xb8 },
332*4882a593Smuzhiyun 	{ 0xaa, 0x92 },
333*4882a593Smuzhiyun 	{ 0xab, 0x0a },
334*4882a593Smuzhiyun 	{ REG_DBLC1, 0xdf },	/* Digital BLC */
335*4882a593Smuzhiyun 	{ REG_DBLC_B, 0x00 },	/* Digital BLC B chan offset */
336*4882a593Smuzhiyun 	{ REG_DBLC_R, 0x00 },	/* Digital BLC R chan offset */
337*4882a593Smuzhiyun 	{ REG_DBLC_GB, 0x00 },	/* Digital BLC GB chan offset */
338*4882a593Smuzhiyun 	{ REG_DBLC_GR, 0x00 },
339*4882a593Smuzhiyun 	{ REG_COM9, 0x3a },	/* Gain ceiling 16x */
340*4882a593Smuzhiyun 	{ REG_NULL, 0 }
341*4882a593Smuzhiyun };
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun #define NUM_FMT_REGS 14
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun  * COM7,  COM3,  COM4, HSTART, HSTOP, HREF, VSTART, VSTOP, VREF,
346*4882a593Smuzhiyun  * EXHCH, EXHCL, ADC,  OCOM,   OFON
347*4882a593Smuzhiyun  */
348*4882a593Smuzhiyun static const u8 frame_size_reg_addr[NUM_FMT_REGS] = {
349*4882a593Smuzhiyun 	0x12, 0x0c, 0x0d, 0x17, 0x18, 0x32, 0x19, 0x1a, 0x03,
350*4882a593Smuzhiyun 	0x2a, 0x2b, 0x37, 0x38, 0x39,
351*4882a593Smuzhiyun };
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun static const u8 ov965x_sxga_regs[NUM_FMT_REGS] = {
354*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x1e, 0xbe, 0xbf, 0x01, 0x81, 0x12,
355*4882a593Smuzhiyun 	0x10, 0x34, 0x81, 0x93, 0x51,
356*4882a593Smuzhiyun };
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun static const u8 ov965x_vga_regs[NUM_FMT_REGS] = {
359*4882a593Smuzhiyun 	0x40, 0x04, 0x80, 0x26, 0xc6, 0xed, 0x01, 0x3d, 0x00,
360*4882a593Smuzhiyun 	0x10, 0x40, 0x91, 0x12, 0x43,
361*4882a593Smuzhiyun };
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /* Determined empirically. */
364*4882a593Smuzhiyun static const u8 ov965x_qvga_regs[NUM_FMT_REGS] = {
365*4882a593Smuzhiyun 	0x10, 0x04, 0x80, 0x25, 0xc5, 0xbf, 0x00, 0x80, 0x12,
366*4882a593Smuzhiyun 	0x10, 0x40, 0x91, 0x12, 0x43,
367*4882a593Smuzhiyun };
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun static const struct ov965x_framesize ov965x_framesizes[] = {
370*4882a593Smuzhiyun 	{
371*4882a593Smuzhiyun 		.width		= SXGA_WIDTH,
372*4882a593Smuzhiyun 		.height		= SXGA_HEIGHT,
373*4882a593Smuzhiyun 		.regs		= ov965x_sxga_regs,
374*4882a593Smuzhiyun 		.max_exp_lines	= 1048,
375*4882a593Smuzhiyun 	}, {
376*4882a593Smuzhiyun 		.width		= VGA_WIDTH,
377*4882a593Smuzhiyun 		.height		= VGA_HEIGHT,
378*4882a593Smuzhiyun 		.regs		= ov965x_vga_regs,
379*4882a593Smuzhiyun 		.max_exp_lines	= 498,
380*4882a593Smuzhiyun 	}, {
381*4882a593Smuzhiyun 		.width		= QVGA_WIDTH,
382*4882a593Smuzhiyun 		.height		= QVGA_HEIGHT,
383*4882a593Smuzhiyun 		.regs		= ov965x_qvga_regs,
384*4882a593Smuzhiyun 		.max_exp_lines	= 248,
385*4882a593Smuzhiyun 	},
386*4882a593Smuzhiyun };
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun struct ov965x_pixfmt {
389*4882a593Smuzhiyun 	u32 code;
390*4882a593Smuzhiyun 	u32 colorspace;
391*4882a593Smuzhiyun 	/* REG_TSLB value, only bits [3:2] may be set. */
392*4882a593Smuzhiyun 	u8 tslb_reg;
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun static const struct ov965x_pixfmt ov965x_formats[] = {
396*4882a593Smuzhiyun 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 0x00},
397*4882a593Smuzhiyun 	{ MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG, 0x04},
398*4882a593Smuzhiyun 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG, 0x0c},
399*4882a593Smuzhiyun 	{ MEDIA_BUS_FMT_VYUY8_2X8, V4L2_COLORSPACE_JPEG, 0x08},
400*4882a593Smuzhiyun };
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun /*
403*4882a593Smuzhiyun  * This table specifies possible frame resolution and interval
404*4882a593Smuzhiyun  * combinations. Default CLKRC[5:0] divider values are valid
405*4882a593Smuzhiyun  * only for 24 MHz external clock frequency.
406*4882a593Smuzhiyun  */
407*4882a593Smuzhiyun static struct ov965x_interval ov965x_intervals[] = {
408*4882a593Smuzhiyun 	{{ 100, 625 }, { SXGA_WIDTH, SXGA_HEIGHT }, 0 },  /* 6.25 fps */
409*4882a593Smuzhiyun 	{{ 10,  125 }, { VGA_WIDTH, VGA_HEIGHT },   1 },  /* 12.5 fps */
410*4882a593Smuzhiyun 	{{ 10,  125 }, { QVGA_WIDTH, QVGA_HEIGHT }, 3 },  /* 12.5 fps */
411*4882a593Smuzhiyun 	{{ 1,   25  }, { VGA_WIDTH, VGA_HEIGHT },   0 },  /* 25 fps */
412*4882a593Smuzhiyun 	{{ 1,   25  }, { QVGA_WIDTH, QVGA_HEIGHT }, 1 },  /* 25 fps */
413*4882a593Smuzhiyun };
414*4882a593Smuzhiyun 
ctrl_to_sd(struct v4l2_ctrl * ctrl)415*4882a593Smuzhiyun static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	return &container_of(ctrl->handler, struct ov965x, ctrls.handler)->sd;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
to_ov965x(struct v4l2_subdev * sd)420*4882a593Smuzhiyun static inline struct ov965x *to_ov965x(struct v4l2_subdev *sd)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	return container_of(sd, struct ov965x, sd);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
ov965x_read(struct ov965x * ov965x,u8 addr,u8 * val)425*4882a593Smuzhiyun static int ov965x_read(struct ov965x *ov965x, u8 addr, u8 *val)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	int ret;
428*4882a593Smuzhiyun 	unsigned int buf;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	ret = regmap_read(ov965x->regmap, addr, &buf);
431*4882a593Smuzhiyun 	if (!ret)
432*4882a593Smuzhiyun 		*val = buf;
433*4882a593Smuzhiyun 	else
434*4882a593Smuzhiyun 		*val = -1;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	v4l2_dbg(2, debug, &ov965x->sd, "%s: 0x%02x @ 0x%02x. (%d)\n",
437*4882a593Smuzhiyun 		 __func__, *val, addr, ret);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	return ret;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
ov965x_write(struct ov965x * ov965x,u8 addr,u8 val)442*4882a593Smuzhiyun static int ov965x_write(struct ov965x *ov965x, u8 addr, u8 val)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	int ret;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	ret = regmap_write(ov965x->regmap, addr, val);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	v4l2_dbg(2, debug, &ov965x->sd, "%s: 0x%02x @ 0x%02X (%d)\n",
449*4882a593Smuzhiyun 		 __func__, val, addr, ret);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	return ret;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun 
ov965x_write_array(struct ov965x * ov965x,const struct i2c_rv * regs)454*4882a593Smuzhiyun static int ov965x_write_array(struct ov965x *ov965x,
455*4882a593Smuzhiyun 			      const struct i2c_rv *regs)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	int i, ret = 0;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
460*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, regs[i].addr, regs[i].value);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	return ret;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
ov965x_set_default_gamma_curve(struct ov965x * ov965x)465*4882a593Smuzhiyun static int ov965x_set_default_gamma_curve(struct ov965x *ov965x)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun 	static const u8 gamma_curve[] = {
468*4882a593Smuzhiyun 		/* Values taken from OV application note. */
469*4882a593Smuzhiyun 		0x40, 0x30, 0x4b, 0x60, 0x70, 0x70, 0x70, 0x70,
470*4882a593Smuzhiyun 		0x60, 0x60, 0x50, 0x48, 0x3a, 0x2e, 0x28, 0x22,
471*4882a593Smuzhiyun 		0x04, 0x07, 0x10, 0x28,	0x36, 0x44, 0x52, 0x60,
472*4882a593Smuzhiyun 		0x6c, 0x78, 0x8c, 0x9e, 0xbb, 0xd2, 0xe6
473*4882a593Smuzhiyun 	};
474*4882a593Smuzhiyun 	u8 addr = REG_GSP;
475*4882a593Smuzhiyun 	unsigned int i;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(gamma_curve); i++) {
478*4882a593Smuzhiyun 		int ret = ov965x_write(ov965x, addr, gamma_curve[i]);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		if (ret < 0)
481*4882a593Smuzhiyun 			return ret;
482*4882a593Smuzhiyun 		addr++;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	return 0;
486*4882a593Smuzhiyun };
487*4882a593Smuzhiyun 
ov965x_set_color_matrix(struct ov965x * ov965x)488*4882a593Smuzhiyun static int ov965x_set_color_matrix(struct ov965x *ov965x)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun 	static const u8 mtx[] = {
491*4882a593Smuzhiyun 		/* MTX1..MTX9, MTXS */
492*4882a593Smuzhiyun 		0x3a, 0x3d, 0x03, 0x12, 0x26, 0x38, 0x40, 0x40, 0x40, 0x0d
493*4882a593Smuzhiyun 	};
494*4882a593Smuzhiyun 	u8 addr = REG_MTX(1);
495*4882a593Smuzhiyun 	unsigned int i;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(mtx); i++) {
498*4882a593Smuzhiyun 		int ret = ov965x_write(ov965x, addr, mtx[i]);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		if (ret < 0)
501*4882a593Smuzhiyun 			return ret;
502*4882a593Smuzhiyun 		addr++;
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	return 0;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
__ov965x_set_power(struct ov965x * ov965x,int on)508*4882a593Smuzhiyun static int __ov965x_set_power(struct ov965x *ov965x, int on)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	if (on) {
511*4882a593Smuzhiyun 		int ret = clk_prepare_enable(ov965x->clk);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		if (ret)
514*4882a593Smuzhiyun 			return ret;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 		gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 0);
517*4882a593Smuzhiyun 		gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 0);
518*4882a593Smuzhiyun 		msleep(25);
519*4882a593Smuzhiyun 	} else {
520*4882a593Smuzhiyun 		gpiod_set_value_cansleep(ov965x->gpios[GPIO_RST], 1);
521*4882a593Smuzhiyun 		gpiod_set_value_cansleep(ov965x->gpios[GPIO_PWDN], 1);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 		clk_disable_unprepare(ov965x->clk);
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	ov965x->streaming = 0;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return 0;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
ov965x_s_power(struct v4l2_subdev * sd,int on)531*4882a593Smuzhiyun static int ov965x_s_power(struct v4l2_subdev *sd, int on)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
534*4882a593Smuzhiyun 	int ret = 0;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: on: %d\n", __func__, on);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
539*4882a593Smuzhiyun 	if (ov965x->power == !on) {
540*4882a593Smuzhiyun 		ret = __ov965x_set_power(ov965x, on);
541*4882a593Smuzhiyun 		if (!ret && on) {
542*4882a593Smuzhiyun 			ret = ov965x_write_array(ov965x,
543*4882a593Smuzhiyun 						 ov965x_init_regs);
544*4882a593Smuzhiyun 			ov965x->apply_frame_fmt = 1;
545*4882a593Smuzhiyun 			ov965x->ctrls.update = 1;
546*4882a593Smuzhiyun 		}
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 	if (!ret)
549*4882a593Smuzhiyun 		ov965x->power += on ? 1 : -1;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	WARN_ON(ov965x->power < 0);
552*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
553*4882a593Smuzhiyun 	return ret;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun  * V4L2 controls
558*4882a593Smuzhiyun  */
559*4882a593Smuzhiyun 
ov965x_update_exposure_ctrl(struct ov965x * ov965x)560*4882a593Smuzhiyun static void ov965x_update_exposure_ctrl(struct ov965x *ov965x)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	struct v4l2_ctrl *ctrl = ov965x->ctrls.exposure;
563*4882a593Smuzhiyun 	unsigned long fint, trow;
564*4882a593Smuzhiyun 	int min, max, def;
565*4882a593Smuzhiyun 	u8 clkrc;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
568*4882a593Smuzhiyun 	if (WARN_ON(!ctrl || !ov965x->frame_size)) {
569*4882a593Smuzhiyun 		mutex_unlock(&ov965x->lock);
570*4882a593Smuzhiyun 		return;
571*4882a593Smuzhiyun 	}
572*4882a593Smuzhiyun 	clkrc = DEF_CLKRC + ov965x->fiv->clkrc_div;
573*4882a593Smuzhiyun 	/* Calculate internal clock frequency */
574*4882a593Smuzhiyun 	fint = ov965x->mclk_frequency * ((clkrc >> 7) + 1) /
575*4882a593Smuzhiyun 				((2 * ((clkrc & 0x3f) + 1)));
576*4882a593Smuzhiyun 	/* and the row interval (in us). */
577*4882a593Smuzhiyun 	trow = (2 * 1520 * 1000000UL) / fint;
578*4882a593Smuzhiyun 	max = ov965x->frame_size->max_exp_lines * trow;
579*4882a593Smuzhiyun 	ov965x->exp_row_interval = trow;
580*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	v4l2_dbg(1, debug, &ov965x->sd, "clkrc: %#x, fi: %lu, tr: %lu, %d\n",
583*4882a593Smuzhiyun 		 clkrc, fint, trow, max);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	/* Update exposure time range to match current frame format. */
586*4882a593Smuzhiyun 	min = (trow + 100) / 100;
587*4882a593Smuzhiyun 	max = (max - 100) / 100;
588*4882a593Smuzhiyun 	def = min + (max - min) / 2;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	if (v4l2_ctrl_modify_range(ctrl, min, max, 1, def))
591*4882a593Smuzhiyun 		v4l2_err(&ov965x->sd, "Exposure ctrl range update failed\n");
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
ov965x_set_banding_filter(struct ov965x * ov965x,int value)594*4882a593Smuzhiyun static int ov965x_set_banding_filter(struct ov965x *ov965x, int value)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	unsigned long mbd, light_freq;
597*4882a593Smuzhiyun 	int ret;
598*4882a593Smuzhiyun 	u8 reg;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_COM8, &reg);
601*4882a593Smuzhiyun 	if (!ret) {
602*4882a593Smuzhiyun 		if (value == V4L2_CID_POWER_LINE_FREQUENCY_DISABLED)
603*4882a593Smuzhiyun 			reg &= ~COM8_BFILT;
604*4882a593Smuzhiyun 		else
605*4882a593Smuzhiyun 			reg |= COM8_BFILT;
606*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_COM8, reg);
607*4882a593Smuzhiyun 	}
608*4882a593Smuzhiyun 	if (value == V4L2_CID_POWER_LINE_FREQUENCY_DISABLED)
609*4882a593Smuzhiyun 		return 0;
610*4882a593Smuzhiyun 	if (WARN_ON(!ov965x->fiv))
611*4882a593Smuzhiyun 		return -EINVAL;
612*4882a593Smuzhiyun 	/* Set minimal exposure time for 50/60 HZ lighting */
613*4882a593Smuzhiyun 	if (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ)
614*4882a593Smuzhiyun 		light_freq = 50;
615*4882a593Smuzhiyun 	else
616*4882a593Smuzhiyun 		light_freq = 60;
617*4882a593Smuzhiyun 	mbd = (1000UL * ov965x->fiv->interval.denominator *
618*4882a593Smuzhiyun 	       ov965x->frame_size->max_exp_lines) /
619*4882a593Smuzhiyun 	       ov965x->fiv->interval.numerator;
620*4882a593Smuzhiyun 	mbd = ((mbd / (light_freq * 2)) + 500) / 1000UL;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	return ov965x_write(ov965x, REG_MBD, mbd);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
ov965x_set_white_balance(struct ov965x * ov965x,int awb)625*4882a593Smuzhiyun static int ov965x_set_white_balance(struct ov965x *ov965x, int awb)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	int ret;
628*4882a593Smuzhiyun 	u8 reg;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_COM8, &reg);
631*4882a593Smuzhiyun 	if (!ret) {
632*4882a593Smuzhiyun 		reg = awb ? reg | REG_COM8 : reg & ~REG_COM8;
633*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_COM8, reg);
634*4882a593Smuzhiyun 	}
635*4882a593Smuzhiyun 	if (!ret && !awb) {
636*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_BLUE,
637*4882a593Smuzhiyun 				   ov965x->ctrls.blue_balance->val);
638*4882a593Smuzhiyun 		if (ret < 0)
639*4882a593Smuzhiyun 			return ret;
640*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_RED,
641*4882a593Smuzhiyun 				   ov965x->ctrls.red_balance->val);
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 	return ret;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun #define NUM_BR_LEVELS	7
647*4882a593Smuzhiyun #define NUM_BR_REGS	3
648*4882a593Smuzhiyun 
ov965x_set_brightness(struct ov965x * ov965x,int val)649*4882a593Smuzhiyun static int ov965x_set_brightness(struct ov965x *ov965x, int val)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun 	static const u8 regs[NUM_BR_LEVELS + 1][NUM_BR_REGS] = {
652*4882a593Smuzhiyun 		{ REG_AEW, REG_AEB, REG_VPT },
653*4882a593Smuzhiyun 		{ 0x1c, 0x12, 0x50 }, /* -3 */
654*4882a593Smuzhiyun 		{ 0x3d, 0x30, 0x71 }, /* -2 */
655*4882a593Smuzhiyun 		{ 0x50, 0x44, 0x92 }, /* -1 */
656*4882a593Smuzhiyun 		{ 0x70, 0x64, 0xc3 }, /*  0 */
657*4882a593Smuzhiyun 		{ 0x90, 0x84, 0xd4 }, /* +1 */
658*4882a593Smuzhiyun 		{ 0xc4, 0xbf, 0xf9 }, /* +2 */
659*4882a593Smuzhiyun 		{ 0xd8, 0xd0, 0xfa }, /* +3 */
660*4882a593Smuzhiyun 	};
661*4882a593Smuzhiyun 	int i, ret = 0;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	val += (NUM_BR_LEVELS / 2 + 1);
664*4882a593Smuzhiyun 	if (val > NUM_BR_LEVELS)
665*4882a593Smuzhiyun 		return -EINVAL;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	for (i = 0; i < NUM_BR_REGS && !ret; i++)
668*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, regs[0][i],
669*4882a593Smuzhiyun 				   regs[val][i]);
670*4882a593Smuzhiyun 	return ret;
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun 
ov965x_set_gain(struct ov965x * ov965x,int auto_gain)673*4882a593Smuzhiyun static int ov965x_set_gain(struct ov965x *ov965x, int auto_gain)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct ov965x_ctrls *ctrls = &ov965x->ctrls;
676*4882a593Smuzhiyun 	int ret = 0;
677*4882a593Smuzhiyun 	u8 reg;
678*4882a593Smuzhiyun 	/*
679*4882a593Smuzhiyun 	 * For manual mode we need to disable AGC first, so
680*4882a593Smuzhiyun 	 * gain value in REG_VREF, REG_GAIN is not overwritten.
681*4882a593Smuzhiyun 	 */
682*4882a593Smuzhiyun 	if (ctrls->auto_gain->is_new) {
683*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_COM8, &reg);
684*4882a593Smuzhiyun 		if (ret < 0)
685*4882a593Smuzhiyun 			return ret;
686*4882a593Smuzhiyun 		if (ctrls->auto_gain->val)
687*4882a593Smuzhiyun 			reg |= COM8_AGC;
688*4882a593Smuzhiyun 		else
689*4882a593Smuzhiyun 			reg &= ~COM8_AGC;
690*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_COM8, reg);
691*4882a593Smuzhiyun 		if (ret < 0)
692*4882a593Smuzhiyun 			return ret;
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (ctrls->gain->is_new && !auto_gain) {
696*4882a593Smuzhiyun 		unsigned int gain = ctrls->gain->val;
697*4882a593Smuzhiyun 		unsigned int rgain;
698*4882a593Smuzhiyun 		int m;
699*4882a593Smuzhiyun 		/*
700*4882a593Smuzhiyun 		 * Convert gain control value to the sensor's gain
701*4882a593Smuzhiyun 		 * registers (VREF[7:6], GAIN[7:0]) format.
702*4882a593Smuzhiyun 		 */
703*4882a593Smuzhiyun 		for (m = 6; m >= 0; m--)
704*4882a593Smuzhiyun 			if (gain >= (1 << m) * 16)
705*4882a593Smuzhiyun 				break;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		/* Sanity check: don't adjust the gain with a negative value */
708*4882a593Smuzhiyun 		if (m < 0)
709*4882a593Smuzhiyun 			return -EINVAL;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		rgain = (gain - ((1 << m) * 16)) / (1 << m);
712*4882a593Smuzhiyun 		rgain |= (((1 << m) - 1) << 4);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_GAIN, rgain & 0xff);
715*4882a593Smuzhiyun 		if (ret < 0)
716*4882a593Smuzhiyun 			return ret;
717*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_VREF, &reg);
718*4882a593Smuzhiyun 		if (ret < 0)
719*4882a593Smuzhiyun 			return ret;
720*4882a593Smuzhiyun 		reg &= ~VREF_GAIN_MASK;
721*4882a593Smuzhiyun 		reg |= (((rgain >> 8) & 0x3) << 6);
722*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_VREF, reg);
723*4882a593Smuzhiyun 		if (ret < 0)
724*4882a593Smuzhiyun 			return ret;
725*4882a593Smuzhiyun 		/* Return updated control's value to userspace */
726*4882a593Smuzhiyun 		ctrls->gain->val = (1 << m) * (16 + (rgain & 0xf));
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	return ret;
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun 
ov965x_set_sharpness(struct ov965x * ov965x,unsigned int value)732*4882a593Smuzhiyun static int ov965x_set_sharpness(struct ov965x *ov965x, unsigned int value)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun 	u8 com14, edge;
735*4882a593Smuzhiyun 	int ret;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_COM14, &com14);
738*4882a593Smuzhiyun 	if (ret < 0)
739*4882a593Smuzhiyun 		return ret;
740*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_EDGE, &edge);
741*4882a593Smuzhiyun 	if (ret < 0)
742*4882a593Smuzhiyun 		return ret;
743*4882a593Smuzhiyun 	com14 = value ? com14 | COM14_EDGE_EN : com14 & ~COM14_EDGE_EN;
744*4882a593Smuzhiyun 	value--;
745*4882a593Smuzhiyun 	if (value > 0x0f) {
746*4882a593Smuzhiyun 		com14 |= COM14_EEF_X2;
747*4882a593Smuzhiyun 		value >>= 1;
748*4882a593Smuzhiyun 	} else {
749*4882a593Smuzhiyun 		com14 &= ~COM14_EEF_X2;
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 	ret = ov965x_write(ov965x, REG_COM14, com14);
752*4882a593Smuzhiyun 	if (ret < 0)
753*4882a593Smuzhiyun 		return ret;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	edge &= ~EDGE_FACTOR_MASK;
756*4882a593Smuzhiyun 	edge |= ((u8)value & 0x0f);
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	return ov965x_write(ov965x, REG_EDGE, edge);
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
ov965x_set_exposure(struct ov965x * ov965x,int exp)761*4882a593Smuzhiyun static int ov965x_set_exposure(struct ov965x *ov965x, int exp)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	struct ov965x_ctrls *ctrls = &ov965x->ctrls;
764*4882a593Smuzhiyun 	bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
765*4882a593Smuzhiyun 	int ret;
766*4882a593Smuzhiyun 	u8 reg;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	if (ctrls->auto_exp->is_new) {
769*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_COM8, &reg);
770*4882a593Smuzhiyun 		if (ret < 0)
771*4882a593Smuzhiyun 			return ret;
772*4882a593Smuzhiyun 		if (auto_exposure)
773*4882a593Smuzhiyun 			reg |= (COM8_AEC | COM8_AGC);
774*4882a593Smuzhiyun 		else
775*4882a593Smuzhiyun 			reg &= ~(COM8_AEC | COM8_AGC);
776*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_COM8, reg);
777*4882a593Smuzhiyun 		if (ret < 0)
778*4882a593Smuzhiyun 			return ret;
779*4882a593Smuzhiyun 	}
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	if (!auto_exposure && ctrls->exposure->is_new) {
782*4882a593Smuzhiyun 		unsigned int exposure = (ctrls->exposure->val * 100)
783*4882a593Smuzhiyun 					 / ov965x->exp_row_interval;
784*4882a593Smuzhiyun 		/*
785*4882a593Smuzhiyun 		 * Manual exposure value
786*4882a593Smuzhiyun 		 * [b15:b0] - AECHM (b15:b10), AECH (b9:b2), COM1 (b1:b0)
787*4882a593Smuzhiyun 		 */
788*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_COM1, exposure & 0x3);
789*4882a593Smuzhiyun 		if (!ret)
790*4882a593Smuzhiyun 			ret = ov965x_write(ov965x, REG_AECH,
791*4882a593Smuzhiyun 					   (exposure >> 2) & 0xff);
792*4882a593Smuzhiyun 		if (!ret)
793*4882a593Smuzhiyun 			ret = ov965x_write(ov965x, REG_AECHM,
794*4882a593Smuzhiyun 					   (exposure >> 10) & 0x3f);
795*4882a593Smuzhiyun 		/* Update the value to minimize rounding errors */
796*4882a593Smuzhiyun 		ctrls->exposure->val = ((exposure * ov965x->exp_row_interval)
797*4882a593Smuzhiyun 							+ 50) / 100;
798*4882a593Smuzhiyun 		if (ret < 0)
799*4882a593Smuzhiyun 			return ret;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	v4l2_ctrl_activate(ov965x->ctrls.brightness, !exp);
803*4882a593Smuzhiyun 	return 0;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
ov965x_set_flip(struct ov965x * ov965x)806*4882a593Smuzhiyun static int ov965x_set_flip(struct ov965x *ov965x)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	u8 mvfp = 0;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	if (ov965x->ctrls.hflip->val)
811*4882a593Smuzhiyun 		mvfp |= MVFP_MIRROR;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	if (ov965x->ctrls.vflip->val)
814*4882a593Smuzhiyun 		mvfp |= MVFP_FLIP;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return ov965x_write(ov965x, REG_MVFP, mvfp);
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun #define NUM_SAT_LEVELS	5
820*4882a593Smuzhiyun #define NUM_SAT_REGS	6
821*4882a593Smuzhiyun 
ov965x_set_saturation(struct ov965x * ov965x,int val)822*4882a593Smuzhiyun static int ov965x_set_saturation(struct ov965x *ov965x, int val)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	static const u8 regs[NUM_SAT_LEVELS][NUM_SAT_REGS] = {
825*4882a593Smuzhiyun 		/* MTX(1)...MTX(6) */
826*4882a593Smuzhiyun 		{ 0x1d, 0x1f, 0x02, 0x09, 0x13, 0x1c }, /* -2 */
827*4882a593Smuzhiyun 		{ 0x2e, 0x31, 0x02, 0x0e, 0x1e, 0x2d }, /* -1 */
828*4882a593Smuzhiyun 		{ 0x3a, 0x3d, 0x03, 0x12, 0x26, 0x38 }, /*  0 */
829*4882a593Smuzhiyun 		{ 0x46, 0x49, 0x04, 0x16, 0x2e, 0x43 }, /* +1 */
830*4882a593Smuzhiyun 		{ 0x57, 0x5c, 0x05, 0x1b, 0x39, 0x54 }, /* +2 */
831*4882a593Smuzhiyun 	};
832*4882a593Smuzhiyun 	u8 addr = REG_MTX(1);
833*4882a593Smuzhiyun 	int i, ret = 0;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	val += (NUM_SAT_LEVELS / 2);
836*4882a593Smuzhiyun 	if (val >= NUM_SAT_LEVELS)
837*4882a593Smuzhiyun 		return -EINVAL;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	for (i = 0; i < NUM_SAT_REGS && !ret; i++)
840*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, addr + i, regs[val][i]);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	return ret;
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun 
ov965x_set_test_pattern(struct ov965x * ov965x,int value)845*4882a593Smuzhiyun static int ov965x_set_test_pattern(struct ov965x *ov965x, int value)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	int ret;
848*4882a593Smuzhiyun 	u8 reg;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_COM23, &reg);
851*4882a593Smuzhiyun 	if (ret < 0)
852*4882a593Smuzhiyun 		return ret;
853*4882a593Smuzhiyun 	reg = value ? reg | COM23_TEST_MODE : reg & ~COM23_TEST_MODE;
854*4882a593Smuzhiyun 	return ov965x_write(ov965x, REG_COM23, reg);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
__g_volatile_ctrl(struct ov965x * ov965x,struct v4l2_ctrl * ctrl)857*4882a593Smuzhiyun static int __g_volatile_ctrl(struct ov965x *ov965x, struct v4l2_ctrl *ctrl)
858*4882a593Smuzhiyun {
859*4882a593Smuzhiyun 	unsigned int exposure, gain, m;
860*4882a593Smuzhiyun 	u8 reg0, reg1, reg2;
861*4882a593Smuzhiyun 	int ret;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	if (!ov965x->power)
864*4882a593Smuzhiyun 		return 0;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	switch (ctrl->id) {
867*4882a593Smuzhiyun 	case V4L2_CID_AUTOGAIN:
868*4882a593Smuzhiyun 		if (!ctrl->val)
869*4882a593Smuzhiyun 			return 0;
870*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_GAIN, &reg0);
871*4882a593Smuzhiyun 		if (ret < 0)
872*4882a593Smuzhiyun 			return ret;
873*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_VREF, &reg1);
874*4882a593Smuzhiyun 		if (ret < 0)
875*4882a593Smuzhiyun 			return ret;
876*4882a593Smuzhiyun 		gain = ((reg1 >> 6) << 8) | reg0;
877*4882a593Smuzhiyun 		m = 0x01 << fls(gain >> 4);
878*4882a593Smuzhiyun 		ov965x->ctrls.gain->val = m * (16 + (gain & 0xf));
879*4882a593Smuzhiyun 		break;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	case V4L2_CID_EXPOSURE_AUTO:
882*4882a593Smuzhiyun 		if (ctrl->val == V4L2_EXPOSURE_MANUAL)
883*4882a593Smuzhiyun 			return 0;
884*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_COM1, &reg0);
885*4882a593Smuzhiyun 		if (ret < 0)
886*4882a593Smuzhiyun 			return ret;
887*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_AECH, &reg1);
888*4882a593Smuzhiyun 		if (ret < 0)
889*4882a593Smuzhiyun 			return ret;
890*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_AECHM, &reg2);
891*4882a593Smuzhiyun 		if (ret < 0)
892*4882a593Smuzhiyun 			return ret;
893*4882a593Smuzhiyun 		exposure = ((reg2 & 0x3f) << 10) | (reg1 << 2) |
894*4882a593Smuzhiyun 						(reg0 & 0x3);
895*4882a593Smuzhiyun 		ov965x->ctrls.exposure->val = ((exposure *
896*4882a593Smuzhiyun 				ov965x->exp_row_interval) + 50) / 100;
897*4882a593Smuzhiyun 		break;
898*4882a593Smuzhiyun 	}
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	return 0;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun 
ov965x_g_volatile_ctrl(struct v4l2_ctrl * ctrl)903*4882a593Smuzhiyun static int ov965x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
906*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
907*4882a593Smuzhiyun 	int ret;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "g_ctrl: %s\n", ctrl->name);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
912*4882a593Smuzhiyun 	ret = __g_volatile_ctrl(ov965x, ctrl);
913*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
914*4882a593Smuzhiyun 	return ret;
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun 
ov965x_s_ctrl(struct v4l2_ctrl * ctrl)917*4882a593Smuzhiyun static int ov965x_s_ctrl(struct v4l2_ctrl *ctrl)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
920*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
921*4882a593Smuzhiyun 	int ret = -EINVAL;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "s_ctrl: %s, value: %d. power: %d\n",
924*4882a593Smuzhiyun 		 ctrl->name, ctrl->val, ov965x->power);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
927*4882a593Smuzhiyun 	/*
928*4882a593Smuzhiyun 	 * If the device is not powered up now postpone applying control's
929*4882a593Smuzhiyun 	 * value to the hardware, until it is ready to accept commands.
930*4882a593Smuzhiyun 	 */
931*4882a593Smuzhiyun 	if (ov965x->power == 0) {
932*4882a593Smuzhiyun 		mutex_unlock(&ov965x->lock);
933*4882a593Smuzhiyun 		return 0;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	switch (ctrl->id) {
937*4882a593Smuzhiyun 	case V4L2_CID_AUTO_WHITE_BALANCE:
938*4882a593Smuzhiyun 		ret = ov965x_set_white_balance(ov965x, ctrl->val);
939*4882a593Smuzhiyun 		break;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	case V4L2_CID_BRIGHTNESS:
942*4882a593Smuzhiyun 		ret = ov965x_set_brightness(ov965x, ctrl->val);
943*4882a593Smuzhiyun 		break;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	case V4L2_CID_EXPOSURE_AUTO:
946*4882a593Smuzhiyun 		ret = ov965x_set_exposure(ov965x, ctrl->val);
947*4882a593Smuzhiyun 		break;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	case V4L2_CID_AUTOGAIN:
950*4882a593Smuzhiyun 		ret = ov965x_set_gain(ov965x, ctrl->val);
951*4882a593Smuzhiyun 		break;
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	case V4L2_CID_HFLIP:
954*4882a593Smuzhiyun 		ret = ov965x_set_flip(ov965x);
955*4882a593Smuzhiyun 		break;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	case V4L2_CID_POWER_LINE_FREQUENCY:
958*4882a593Smuzhiyun 		ret = ov965x_set_banding_filter(ov965x, ctrl->val);
959*4882a593Smuzhiyun 		break;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	case V4L2_CID_SATURATION:
962*4882a593Smuzhiyun 		ret = ov965x_set_saturation(ov965x, ctrl->val);
963*4882a593Smuzhiyun 		break;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	case V4L2_CID_SHARPNESS:
966*4882a593Smuzhiyun 		ret = ov965x_set_sharpness(ov965x, ctrl->val);
967*4882a593Smuzhiyun 		break;
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	case V4L2_CID_TEST_PATTERN:
970*4882a593Smuzhiyun 		ret = ov965x_set_test_pattern(ov965x, ctrl->val);
971*4882a593Smuzhiyun 		break;
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
975*4882a593Smuzhiyun 	return ret;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun static const struct v4l2_ctrl_ops ov965x_ctrl_ops = {
979*4882a593Smuzhiyun 	.g_volatile_ctrl = ov965x_g_volatile_ctrl,
980*4882a593Smuzhiyun 	.s_ctrl	= ov965x_s_ctrl,
981*4882a593Smuzhiyun };
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun static const char * const test_pattern_menu[] = {
984*4882a593Smuzhiyun 	"Disabled",
985*4882a593Smuzhiyun 	"Color bars",
986*4882a593Smuzhiyun };
987*4882a593Smuzhiyun 
ov965x_initialize_controls(struct ov965x * ov965x)988*4882a593Smuzhiyun static int ov965x_initialize_controls(struct ov965x *ov965x)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun 	const struct v4l2_ctrl_ops *ops = &ov965x_ctrl_ops;
991*4882a593Smuzhiyun 	struct ov965x_ctrls *ctrls = &ov965x->ctrls;
992*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
993*4882a593Smuzhiyun 	int ret;
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun 	ret = v4l2_ctrl_handler_init(hdl, 16);
996*4882a593Smuzhiyun 	if (ret < 0)
997*4882a593Smuzhiyun 		return ret;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	/* Auto/manual white balance */
1000*4882a593Smuzhiyun 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
1001*4882a593Smuzhiyun 					   V4L2_CID_AUTO_WHITE_BALANCE,
1002*4882a593Smuzhiyun 					   0, 1, 1, 1);
1003*4882a593Smuzhiyun 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
1004*4882a593Smuzhiyun 						0, 0xff, 1, 0x80);
1005*4882a593Smuzhiyun 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
1006*4882a593Smuzhiyun 					       0, 0xff, 1, 0x80);
1007*4882a593Smuzhiyun 	/* Auto/manual exposure */
1008*4882a593Smuzhiyun 	ctrls->auto_exp =
1009*4882a593Smuzhiyun 		v4l2_ctrl_new_std_menu(hdl, ops,
1010*4882a593Smuzhiyun 				       V4L2_CID_EXPOSURE_AUTO,
1011*4882a593Smuzhiyun 				       V4L2_EXPOSURE_MANUAL, 0,
1012*4882a593Smuzhiyun 				       V4L2_EXPOSURE_AUTO);
1013*4882a593Smuzhiyun 	/* Exposure time, in 100 us units. min/max is updated dynamically. */
1014*4882a593Smuzhiyun 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops,
1015*4882a593Smuzhiyun 					    V4L2_CID_EXPOSURE_ABSOLUTE,
1016*4882a593Smuzhiyun 					    2, 1500, 1, 500);
1017*4882a593Smuzhiyun 	/* Auto/manual gain */
1018*4882a593Smuzhiyun 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
1019*4882a593Smuzhiyun 					     0, 1, 1, 1);
1020*4882a593Smuzhiyun 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
1021*4882a593Smuzhiyun 					16, 64 * (16 + 15), 1, 64 * 16);
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
1024*4882a593Smuzhiyun 					      -2, 2, 1, 0);
1025*4882a593Smuzhiyun 	ctrls->brightness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS,
1026*4882a593Smuzhiyun 					      -3, 3, 1, 0);
1027*4882a593Smuzhiyun 	ctrls->sharpness = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS,
1028*4882a593Smuzhiyun 					     0, 32, 1, 6);
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1031*4882a593Smuzhiyun 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	ctrls->light_freq =
1034*4882a593Smuzhiyun 		v4l2_ctrl_new_std_menu(hdl, ops,
1035*4882a593Smuzhiyun 				       V4L2_CID_POWER_LINE_FREQUENCY,
1036*4882a593Smuzhiyun 				       V4L2_CID_POWER_LINE_FREQUENCY_60HZ, ~0x7,
1037*4882a593Smuzhiyun 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
1040*4882a593Smuzhiyun 				     ARRAY_SIZE(test_pattern_menu) - 1, 0, 0,
1041*4882a593Smuzhiyun 				     test_pattern_menu);
1042*4882a593Smuzhiyun 	if (hdl->error) {
1043*4882a593Smuzhiyun 		ret = hdl->error;
1044*4882a593Smuzhiyun 		v4l2_ctrl_handler_free(hdl);
1045*4882a593Smuzhiyun 		return ret;
1046*4882a593Smuzhiyun 	}
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
1049*4882a593Smuzhiyun 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
1052*4882a593Smuzhiyun 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
1053*4882a593Smuzhiyun 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
1054*4882a593Smuzhiyun 	v4l2_ctrl_cluster(2, &ctrls->hflip);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	ov965x->sd.ctrl_handler = hdl;
1057*4882a593Smuzhiyun 	return 0;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun /*
1061*4882a593Smuzhiyun  * V4L2 subdev video and pad level operations
1062*4882a593Smuzhiyun  */
ov965x_get_default_format(struct v4l2_mbus_framefmt * mf)1063*4882a593Smuzhiyun static void ov965x_get_default_format(struct v4l2_mbus_framefmt *mf)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun 	mf->width = ov965x_framesizes[0].width;
1066*4882a593Smuzhiyun 	mf->height = ov965x_framesizes[0].height;
1067*4882a593Smuzhiyun 	mf->colorspace = ov965x_formats[0].colorspace;
1068*4882a593Smuzhiyun 	mf->code = ov965x_formats[0].code;
1069*4882a593Smuzhiyun 	mf->field = V4L2_FIELD_NONE;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun 
ov965x_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1072*4882a593Smuzhiyun static int ov965x_enum_mbus_code(struct v4l2_subdev *sd,
1073*4882a593Smuzhiyun 				 struct v4l2_subdev_pad_config *cfg,
1074*4882a593Smuzhiyun 				 struct v4l2_subdev_mbus_code_enum *code)
1075*4882a593Smuzhiyun {
1076*4882a593Smuzhiyun 	if (code->index >= ARRAY_SIZE(ov965x_formats))
1077*4882a593Smuzhiyun 		return -EINVAL;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	code->code = ov965x_formats[code->index].code;
1080*4882a593Smuzhiyun 	return 0;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun 
ov965x_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1083*4882a593Smuzhiyun static int ov965x_enum_frame_sizes(struct v4l2_subdev *sd,
1084*4882a593Smuzhiyun 				   struct v4l2_subdev_pad_config *cfg,
1085*4882a593Smuzhiyun 				   struct v4l2_subdev_frame_size_enum *fse)
1086*4882a593Smuzhiyun {
1087*4882a593Smuzhiyun 	int i = ARRAY_SIZE(ov965x_formats);
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	if (fse->index >= ARRAY_SIZE(ov965x_framesizes))
1090*4882a593Smuzhiyun 		return -EINVAL;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	while (--i)
1093*4882a593Smuzhiyun 		if (fse->code == ov965x_formats[i].code)
1094*4882a593Smuzhiyun 			break;
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	fse->code = ov965x_formats[i].code;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	fse->min_width  = ov965x_framesizes[fse->index].width;
1099*4882a593Smuzhiyun 	fse->max_width  = fse->min_width;
1100*4882a593Smuzhiyun 	fse->max_height = ov965x_framesizes[fse->index].height;
1101*4882a593Smuzhiyun 	fse->min_height = fse->max_height;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	return 0;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
ov965x_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1106*4882a593Smuzhiyun static int ov965x_g_frame_interval(struct v4l2_subdev *sd,
1107*4882a593Smuzhiyun 				   struct v4l2_subdev_frame_interval *fi)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	fi->interval = ov965x->fiv->interval;
1112*4882a593Smuzhiyun 
1113*4882a593Smuzhiyun 	return 0;
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun 
__ov965x_set_frame_interval(struct ov965x * ov965x,struct v4l2_subdev_frame_interval * fi)1116*4882a593Smuzhiyun static int __ov965x_set_frame_interval(struct ov965x *ov965x,
1117*4882a593Smuzhiyun 				       struct v4l2_subdev_frame_interval *fi)
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *mbus_fmt = &ov965x->format;
1120*4882a593Smuzhiyun 	const struct ov965x_interval *fiv = &ov965x_intervals[0];
1121*4882a593Smuzhiyun 	u64 req_int, err, min_err = ~0ULL;
1122*4882a593Smuzhiyun 	unsigned int i;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (fi->interval.denominator == 0)
1125*4882a593Smuzhiyun 		return -EINVAL;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	req_int = (u64)fi->interval.numerator * 10000;
1128*4882a593Smuzhiyun 	do_div(req_int, fi->interval.denominator);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ov965x_intervals); i++) {
1131*4882a593Smuzhiyun 		const struct ov965x_interval *iv = &ov965x_intervals[i];
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 		if (mbus_fmt->width != iv->size.width ||
1134*4882a593Smuzhiyun 		    mbus_fmt->height != iv->size.height)
1135*4882a593Smuzhiyun 			continue;
1136*4882a593Smuzhiyun 		err = abs((u64)(iv->interval.numerator * 10000) /
1137*4882a593Smuzhiyun 			    iv->interval.denominator - req_int);
1138*4882a593Smuzhiyun 		if (err < min_err) {
1139*4882a593Smuzhiyun 			fiv = iv;
1140*4882a593Smuzhiyun 			min_err = err;
1141*4882a593Smuzhiyun 		}
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 	ov965x->fiv = fiv;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	v4l2_dbg(1, debug, &ov965x->sd, "Changed frame interval to %u us\n",
1146*4882a593Smuzhiyun 		 fiv->interval.numerator * 1000000 / fiv->interval.denominator);
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	return 0;
1149*4882a593Smuzhiyun }
1150*4882a593Smuzhiyun 
ov965x_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1151*4882a593Smuzhiyun static int ov965x_s_frame_interval(struct v4l2_subdev *sd,
1152*4882a593Smuzhiyun 				   struct v4l2_subdev_frame_interval *fi)
1153*4882a593Smuzhiyun {
1154*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1155*4882a593Smuzhiyun 	int ret;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "Setting %d/%d frame interval\n",
1158*4882a593Smuzhiyun 		 fi->interval.numerator, fi->interval.denominator);
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
1161*4882a593Smuzhiyun 	ret = __ov965x_set_frame_interval(ov965x, fi);
1162*4882a593Smuzhiyun 	ov965x->apply_frame_fmt = 1;
1163*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
1164*4882a593Smuzhiyun 	return ret;
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
ov965x_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1167*4882a593Smuzhiyun static int ov965x_get_fmt(struct v4l2_subdev *sd,
1168*4882a593Smuzhiyun 			  struct v4l2_subdev_pad_config *cfg,
1169*4882a593Smuzhiyun 			  struct v4l2_subdev_format *fmt)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1172*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *mf;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1175*4882a593Smuzhiyun 		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
1176*4882a593Smuzhiyun 		fmt->format = *mf;
1177*4882a593Smuzhiyun 		return 0;
1178*4882a593Smuzhiyun 	}
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
1181*4882a593Smuzhiyun 	fmt->format = ov965x->format;
1182*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	return 0;
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun 
__ov965x_try_frame_size(struct v4l2_mbus_framefmt * mf,const struct ov965x_framesize ** size)1187*4882a593Smuzhiyun static void __ov965x_try_frame_size(struct v4l2_mbus_framefmt *mf,
1188*4882a593Smuzhiyun 				    const struct ov965x_framesize **size)
1189*4882a593Smuzhiyun {
1190*4882a593Smuzhiyun 	const struct ov965x_framesize *fsize = &ov965x_framesizes[0],
1191*4882a593Smuzhiyun 		*match = NULL;
1192*4882a593Smuzhiyun 	int i = ARRAY_SIZE(ov965x_framesizes);
1193*4882a593Smuzhiyun 	unsigned int min_err = UINT_MAX;
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	while (i--) {
1196*4882a593Smuzhiyun 		int err = abs(fsize->width - mf->width)
1197*4882a593Smuzhiyun 				+ abs(fsize->height - mf->height);
1198*4882a593Smuzhiyun 		if (err < min_err) {
1199*4882a593Smuzhiyun 			min_err = err;
1200*4882a593Smuzhiyun 			match = fsize;
1201*4882a593Smuzhiyun 		}
1202*4882a593Smuzhiyun 		fsize++;
1203*4882a593Smuzhiyun 	}
1204*4882a593Smuzhiyun 	if (!match)
1205*4882a593Smuzhiyun 		match = &ov965x_framesizes[0];
1206*4882a593Smuzhiyun 	mf->width  = match->width;
1207*4882a593Smuzhiyun 	mf->height = match->height;
1208*4882a593Smuzhiyun 	if (size)
1209*4882a593Smuzhiyun 		*size = match;
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun 
ov965x_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1212*4882a593Smuzhiyun static int ov965x_set_fmt(struct v4l2_subdev *sd,
1213*4882a593Smuzhiyun 			  struct v4l2_subdev_pad_config *cfg,
1214*4882a593Smuzhiyun 			  struct v4l2_subdev_format *fmt)
1215*4882a593Smuzhiyun {
1216*4882a593Smuzhiyun 	unsigned int index = ARRAY_SIZE(ov965x_formats);
1217*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1218*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1219*4882a593Smuzhiyun 	const struct ov965x_framesize *size = NULL;
1220*4882a593Smuzhiyun 	int ret = 0;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	__ov965x_try_frame_size(mf, &size);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	while (--index)
1225*4882a593Smuzhiyun 		if (ov965x_formats[index].code == mf->code)
1226*4882a593Smuzhiyun 			break;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	mf->colorspace	= V4L2_COLORSPACE_JPEG;
1229*4882a593Smuzhiyun 	mf->code	= ov965x_formats[index].code;
1230*4882a593Smuzhiyun 	mf->field	= V4L2_FIELD_NONE;
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1235*4882a593Smuzhiyun 		if (cfg) {
1236*4882a593Smuzhiyun 			mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1237*4882a593Smuzhiyun 			*mf = fmt->format;
1238*4882a593Smuzhiyun 		}
1239*4882a593Smuzhiyun 	} else {
1240*4882a593Smuzhiyun 		if (ov965x->streaming) {
1241*4882a593Smuzhiyun 			ret = -EBUSY;
1242*4882a593Smuzhiyun 		} else {
1243*4882a593Smuzhiyun 			ov965x->frame_size = size;
1244*4882a593Smuzhiyun 			ov965x->format = fmt->format;
1245*4882a593Smuzhiyun 			ov965x->tslb_reg = ov965x_formats[index].tslb_reg;
1246*4882a593Smuzhiyun 			ov965x->apply_frame_fmt = 1;
1247*4882a593Smuzhiyun 		}
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	if (!ret && fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1251*4882a593Smuzhiyun 		struct v4l2_subdev_frame_interval fiv = {
1252*4882a593Smuzhiyun 			.interval = { 0, 1 }
1253*4882a593Smuzhiyun 		};
1254*4882a593Smuzhiyun 		/* Reset to minimum possible frame interval */
1255*4882a593Smuzhiyun 		__ov965x_set_frame_interval(ov965x, &fiv);
1256*4882a593Smuzhiyun 	}
1257*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	if (!ret)
1260*4882a593Smuzhiyun 		ov965x_update_exposure_ctrl(ov965x);
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	return ret;
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun 
ov965x_set_frame_size(struct ov965x * ov965x)1265*4882a593Smuzhiyun static int ov965x_set_frame_size(struct ov965x *ov965x)
1266*4882a593Smuzhiyun {
1267*4882a593Smuzhiyun 	int i, ret = 0;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	for (i = 0; ret == 0 && i < NUM_FMT_REGS; i++)
1270*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, frame_size_reg_addr[i],
1271*4882a593Smuzhiyun 				   ov965x->frame_size->regs[i]);
1272*4882a593Smuzhiyun 	return ret;
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun 
__ov965x_set_params(struct ov965x * ov965x)1275*4882a593Smuzhiyun static int __ov965x_set_params(struct ov965x *ov965x)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun 	struct ov965x_ctrls *ctrls = &ov965x->ctrls;
1278*4882a593Smuzhiyun 	int ret = 0;
1279*4882a593Smuzhiyun 	u8 reg;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	if (ov965x->apply_frame_fmt) {
1282*4882a593Smuzhiyun 		reg = DEF_CLKRC + ov965x->fiv->clkrc_div;
1283*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_CLKRC, reg);
1284*4882a593Smuzhiyun 		if (ret < 0)
1285*4882a593Smuzhiyun 			return ret;
1286*4882a593Smuzhiyun 		ret = ov965x_set_frame_size(ov965x);
1287*4882a593Smuzhiyun 		if (ret < 0)
1288*4882a593Smuzhiyun 			return ret;
1289*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_TSLB, &reg);
1290*4882a593Smuzhiyun 		if (ret < 0)
1291*4882a593Smuzhiyun 			return ret;
1292*4882a593Smuzhiyun 		reg &= ~TSLB_YUYV_MASK;
1293*4882a593Smuzhiyun 		reg |= ov965x->tslb_reg;
1294*4882a593Smuzhiyun 		ret = ov965x_write(ov965x, REG_TSLB, reg);
1295*4882a593Smuzhiyun 		if (ret < 0)
1296*4882a593Smuzhiyun 			return ret;
1297*4882a593Smuzhiyun 	}
1298*4882a593Smuzhiyun 	ret = ov965x_set_default_gamma_curve(ov965x);
1299*4882a593Smuzhiyun 	if (ret < 0)
1300*4882a593Smuzhiyun 		return ret;
1301*4882a593Smuzhiyun 	ret = ov965x_set_color_matrix(ov965x);
1302*4882a593Smuzhiyun 	if (ret < 0)
1303*4882a593Smuzhiyun 		return ret;
1304*4882a593Smuzhiyun 	/*
1305*4882a593Smuzhiyun 	 * Select manual banding filter, the filter will
1306*4882a593Smuzhiyun 	 * be enabled further if required.
1307*4882a593Smuzhiyun 	 */
1308*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_COM11, &reg);
1309*4882a593Smuzhiyun 	if (!ret)
1310*4882a593Smuzhiyun 		reg |= COM11_BANDING;
1311*4882a593Smuzhiyun 	ret = ov965x_write(ov965x, REG_COM11, reg);
1312*4882a593Smuzhiyun 	if (ret < 0)
1313*4882a593Smuzhiyun 		return ret;
1314*4882a593Smuzhiyun 	/*
1315*4882a593Smuzhiyun 	 * Banding filter (REG_MBD value) needs to match selected
1316*4882a593Smuzhiyun 	 * resolution and frame rate, so it's always updated here.
1317*4882a593Smuzhiyun 	 */
1318*4882a593Smuzhiyun 	return ov965x_set_banding_filter(ov965x, ctrls->light_freq->val);
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun 
ov965x_s_stream(struct v4l2_subdev * sd,int on)1321*4882a593Smuzhiyun static int ov965x_s_stream(struct v4l2_subdev *sd, int on)
1322*4882a593Smuzhiyun {
1323*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1324*4882a593Smuzhiyun 	struct ov965x_ctrls *ctrls = &ov965x->ctrls;
1325*4882a593Smuzhiyun 	int ret = 0;
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	v4l2_dbg(1, debug, sd, "%s: on: %d\n", __func__, on);
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
1330*4882a593Smuzhiyun 	if (ov965x->streaming == !on) {
1331*4882a593Smuzhiyun 		if (on)
1332*4882a593Smuzhiyun 			ret = __ov965x_set_params(ov965x);
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 		if (!ret && ctrls->update) {
1335*4882a593Smuzhiyun 			/*
1336*4882a593Smuzhiyun 			 * ov965x_s_ctrl callback takes the mutex
1337*4882a593Smuzhiyun 			 * so it needs to be released here.
1338*4882a593Smuzhiyun 			 */
1339*4882a593Smuzhiyun 			mutex_unlock(&ov965x->lock);
1340*4882a593Smuzhiyun 			ret = v4l2_ctrl_handler_setup(&ctrls->handler);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 			mutex_lock(&ov965x->lock);
1343*4882a593Smuzhiyun 			if (!ret)
1344*4882a593Smuzhiyun 				ctrls->update = 0;
1345*4882a593Smuzhiyun 		}
1346*4882a593Smuzhiyun 		if (!ret)
1347*4882a593Smuzhiyun 			ret = ov965x_write(ov965x, REG_COM2,
1348*4882a593Smuzhiyun 					   on ? 0x01 : 0x11);
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 	if (!ret)
1351*4882a593Smuzhiyun 		ov965x->streaming += on ? 1 : -1;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	WARN_ON(ov965x->streaming < 0);
1354*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	return ret;
1357*4882a593Smuzhiyun }
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun /*
1360*4882a593Smuzhiyun  * V4L2 subdev internal operations
1361*4882a593Smuzhiyun  */
ov965x_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1362*4882a593Smuzhiyun static int ov965x_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *mf =
1365*4882a593Smuzhiyun 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	ov965x_get_default_format(mf);
1368*4882a593Smuzhiyun 	return 0;
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops ov965x_pad_ops = {
1372*4882a593Smuzhiyun 	.enum_mbus_code = ov965x_enum_mbus_code,
1373*4882a593Smuzhiyun 	.enum_frame_size = ov965x_enum_frame_sizes,
1374*4882a593Smuzhiyun 	.get_fmt = ov965x_get_fmt,
1375*4882a593Smuzhiyun 	.set_fmt = ov965x_set_fmt,
1376*4882a593Smuzhiyun };
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun static const struct v4l2_subdev_video_ops ov965x_video_ops = {
1379*4882a593Smuzhiyun 	.s_stream = ov965x_s_stream,
1380*4882a593Smuzhiyun 	.g_frame_interval = ov965x_g_frame_interval,
1381*4882a593Smuzhiyun 	.s_frame_interval = ov965x_s_frame_interval,
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun };
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun static const struct v4l2_subdev_internal_ops ov965x_sd_internal_ops = {
1386*4882a593Smuzhiyun 	.open = ov965x_open,
1387*4882a593Smuzhiyun };
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops ov965x_core_ops = {
1390*4882a593Smuzhiyun 	.s_power = ov965x_s_power,
1391*4882a593Smuzhiyun 	.log_status = v4l2_ctrl_subdev_log_status,
1392*4882a593Smuzhiyun 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1393*4882a593Smuzhiyun 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
1394*4882a593Smuzhiyun };
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun static const struct v4l2_subdev_ops ov965x_subdev_ops = {
1397*4882a593Smuzhiyun 	.core = &ov965x_core_ops,
1398*4882a593Smuzhiyun 	.pad = &ov965x_pad_ops,
1399*4882a593Smuzhiyun 	.video = &ov965x_video_ops,
1400*4882a593Smuzhiyun };
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun /*
1403*4882a593Smuzhiyun  * Reset and power down GPIOs configuration
1404*4882a593Smuzhiyun  */
ov965x_configure_gpios_pdata(struct ov965x * ov965x,const struct ov9650_platform_data * pdata)1405*4882a593Smuzhiyun static int ov965x_configure_gpios_pdata(struct ov965x *ov965x,
1406*4882a593Smuzhiyun 				const struct ov9650_platform_data *pdata)
1407*4882a593Smuzhiyun {
1408*4882a593Smuzhiyun 	int ret, i;
1409*4882a593Smuzhiyun 	int gpios[NUM_GPIOS];
1410*4882a593Smuzhiyun 	struct device *dev = regmap_get_device(ov965x->regmap);
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	gpios[GPIO_PWDN] = pdata->gpio_pwdn;
1413*4882a593Smuzhiyun 	gpios[GPIO_RST]  = pdata->gpio_reset;
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(ov965x->gpios); i++) {
1416*4882a593Smuzhiyun 		int gpio = gpios[i];
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 		if (!gpio_is_valid(gpio))
1419*4882a593Smuzhiyun 			continue;
1420*4882a593Smuzhiyun 		ret = devm_gpio_request_one(dev, gpio,
1421*4882a593Smuzhiyun 					    GPIOF_OUT_INIT_HIGH, "OV965X");
1422*4882a593Smuzhiyun 		if (ret < 0)
1423*4882a593Smuzhiyun 			return ret;
1424*4882a593Smuzhiyun 		v4l2_dbg(1, debug, &ov965x->sd, "set gpio %d to 1\n", gpio);
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 		gpio_set_value_cansleep(gpio, 1);
1427*4882a593Smuzhiyun 		gpio_export(gpio, 0);
1428*4882a593Smuzhiyun 		ov965x->gpios[i] = gpio_to_desc(gpio);
1429*4882a593Smuzhiyun 	}
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	return 0;
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun 
ov965x_configure_gpios(struct ov965x * ov965x)1434*4882a593Smuzhiyun static int ov965x_configure_gpios(struct ov965x *ov965x)
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun 	struct device *dev = regmap_get_device(ov965x->regmap);
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	ov965x->gpios[GPIO_PWDN] = devm_gpiod_get_optional(dev, "powerdown",
1439*4882a593Smuzhiyun 							GPIOD_OUT_HIGH);
1440*4882a593Smuzhiyun 	if (IS_ERR(ov965x->gpios[GPIO_PWDN])) {
1441*4882a593Smuzhiyun 		dev_info(dev, "can't get %s GPIO\n", "powerdown");
1442*4882a593Smuzhiyun 		return PTR_ERR(ov965x->gpios[GPIO_PWDN]);
1443*4882a593Smuzhiyun 	}
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	ov965x->gpios[GPIO_RST] = devm_gpiod_get_optional(dev, "reset",
1446*4882a593Smuzhiyun 							GPIOD_OUT_HIGH);
1447*4882a593Smuzhiyun 	if (IS_ERR(ov965x->gpios[GPIO_RST])) {
1448*4882a593Smuzhiyun 		dev_info(dev, "can't get %s GPIO\n", "reset");
1449*4882a593Smuzhiyun 		return PTR_ERR(ov965x->gpios[GPIO_RST]);
1450*4882a593Smuzhiyun 	}
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	return 0;
1453*4882a593Smuzhiyun }
1454*4882a593Smuzhiyun 
ov965x_detect_sensor(struct v4l2_subdev * sd)1455*4882a593Smuzhiyun static int ov965x_detect_sensor(struct v4l2_subdev *sd)
1456*4882a593Smuzhiyun {
1457*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1458*4882a593Smuzhiyun 	u8 pid, ver;
1459*4882a593Smuzhiyun 	int ret;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	mutex_lock(&ov965x->lock);
1462*4882a593Smuzhiyun 	ret = __ov965x_set_power(ov965x, 1);
1463*4882a593Smuzhiyun 	if (ret)
1464*4882a593Smuzhiyun 		goto out;
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 	msleep(25);
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun 	/* Check sensor revision */
1469*4882a593Smuzhiyun 	ret = ov965x_read(ov965x, REG_PID, &pid);
1470*4882a593Smuzhiyun 	if (!ret)
1471*4882a593Smuzhiyun 		ret = ov965x_read(ov965x, REG_VER, &ver);
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 	__ov965x_set_power(ov965x, 0);
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	if (!ret) {
1476*4882a593Smuzhiyun 		ov965x->id = OV965X_ID(pid, ver);
1477*4882a593Smuzhiyun 		if (ov965x->id == OV9650_ID || ov965x->id == OV9652_ID) {
1478*4882a593Smuzhiyun 			v4l2_info(sd, "Found OV%04X sensor\n", ov965x->id);
1479*4882a593Smuzhiyun 		} else {
1480*4882a593Smuzhiyun 			v4l2_err(sd, "Sensor detection failed (%04X, %d)\n",
1481*4882a593Smuzhiyun 				 ov965x->id, ret);
1482*4882a593Smuzhiyun 			ret = -ENODEV;
1483*4882a593Smuzhiyun 		}
1484*4882a593Smuzhiyun 	}
1485*4882a593Smuzhiyun out:
1486*4882a593Smuzhiyun 	mutex_unlock(&ov965x->lock);
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	return ret;
1489*4882a593Smuzhiyun }
1490*4882a593Smuzhiyun 
ov965x_probe(struct i2c_client * client)1491*4882a593Smuzhiyun static int ov965x_probe(struct i2c_client *client)
1492*4882a593Smuzhiyun {
1493*4882a593Smuzhiyun 	const struct ov9650_platform_data *pdata = client->dev.platform_data;
1494*4882a593Smuzhiyun 	struct v4l2_subdev *sd;
1495*4882a593Smuzhiyun 	struct ov965x *ov965x;
1496*4882a593Smuzhiyun 	int ret;
1497*4882a593Smuzhiyun 	static const struct regmap_config ov965x_regmap_config = {
1498*4882a593Smuzhiyun 		.reg_bits = 8,
1499*4882a593Smuzhiyun 		.val_bits = 8,
1500*4882a593Smuzhiyun 		.max_register = 0xab,
1501*4882a593Smuzhiyun 	};
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	ov965x = devm_kzalloc(&client->dev, sizeof(*ov965x), GFP_KERNEL);
1504*4882a593Smuzhiyun 	if (!ov965x)
1505*4882a593Smuzhiyun 		return -ENOMEM;
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	ov965x->regmap = devm_regmap_init_sccb(client, &ov965x_regmap_config);
1508*4882a593Smuzhiyun 	if (IS_ERR(ov965x->regmap)) {
1509*4882a593Smuzhiyun 		dev_err(&client->dev, "Failed to allocate register map\n");
1510*4882a593Smuzhiyun 		return PTR_ERR(ov965x->regmap);
1511*4882a593Smuzhiyun 	}
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	if (pdata) {
1514*4882a593Smuzhiyun 		if (pdata->mclk_frequency == 0) {
1515*4882a593Smuzhiyun 			dev_err(&client->dev, "MCLK frequency not specified\n");
1516*4882a593Smuzhiyun 			return -EINVAL;
1517*4882a593Smuzhiyun 		}
1518*4882a593Smuzhiyun 		ov965x->mclk_frequency = pdata->mclk_frequency;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 		ret = ov965x_configure_gpios_pdata(ov965x, pdata);
1521*4882a593Smuzhiyun 		if (ret < 0)
1522*4882a593Smuzhiyun 			return ret;
1523*4882a593Smuzhiyun 	} else if (dev_fwnode(&client->dev)) {
1524*4882a593Smuzhiyun 		ov965x->clk = devm_clk_get(&client->dev, NULL);
1525*4882a593Smuzhiyun 		if (IS_ERR(ov965x->clk))
1526*4882a593Smuzhiyun 			return PTR_ERR(ov965x->clk);
1527*4882a593Smuzhiyun 		ov965x->mclk_frequency = clk_get_rate(ov965x->clk);
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun 		ret = ov965x_configure_gpios(ov965x);
1530*4882a593Smuzhiyun 		if (ret < 0)
1531*4882a593Smuzhiyun 			return ret;
1532*4882a593Smuzhiyun 	} else {
1533*4882a593Smuzhiyun 		dev_err(&client->dev,
1534*4882a593Smuzhiyun 			"Neither platform data nor device property specified\n");
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 		return -EINVAL;
1537*4882a593Smuzhiyun 	}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	mutex_init(&ov965x->lock);
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	sd = &ov965x->sd;
1542*4882a593Smuzhiyun 	v4l2_i2c_subdev_init(sd, client, &ov965x_subdev_ops);
1543*4882a593Smuzhiyun 	strscpy(sd->name, DRIVER_NAME, sizeof(sd->name));
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	sd->internal_ops = &ov965x_sd_internal_ops;
1546*4882a593Smuzhiyun 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1547*4882a593Smuzhiyun 		     V4L2_SUBDEV_FL_HAS_EVENTS;
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	ov965x->pad.flags = MEDIA_PAD_FL_SOURCE;
1550*4882a593Smuzhiyun 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1551*4882a593Smuzhiyun 	ret = media_entity_pads_init(&sd->entity, 1, &ov965x->pad);
1552*4882a593Smuzhiyun 	if (ret < 0)
1553*4882a593Smuzhiyun 		goto err_mutex;
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	ret = ov965x_initialize_controls(ov965x);
1556*4882a593Smuzhiyun 	if (ret < 0)
1557*4882a593Smuzhiyun 		goto err_me;
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	ov965x_get_default_format(&ov965x->format);
1560*4882a593Smuzhiyun 	ov965x->frame_size = &ov965x_framesizes[0];
1561*4882a593Smuzhiyun 	ov965x->fiv = &ov965x_intervals[0];
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	ret = ov965x_detect_sensor(sd);
1564*4882a593Smuzhiyun 	if (ret < 0)
1565*4882a593Smuzhiyun 		goto err_ctrls;
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 	/* Update exposure time min/max to match frame format */
1568*4882a593Smuzhiyun 	ov965x_update_exposure_ctrl(ov965x);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	ret = v4l2_async_register_subdev(sd);
1571*4882a593Smuzhiyun 	if (ret < 0)
1572*4882a593Smuzhiyun 		goto err_ctrls;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	return 0;
1575*4882a593Smuzhiyun err_ctrls:
1576*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1577*4882a593Smuzhiyun err_me:
1578*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
1579*4882a593Smuzhiyun err_mutex:
1580*4882a593Smuzhiyun 	mutex_destroy(&ov965x->lock);
1581*4882a593Smuzhiyun 	return ret;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun 
ov965x_remove(struct i2c_client * client)1584*4882a593Smuzhiyun static int ov965x_remove(struct i2c_client *client)
1585*4882a593Smuzhiyun {
1586*4882a593Smuzhiyun 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1587*4882a593Smuzhiyun 	struct ov965x *ov965x = to_ov965x(sd);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	v4l2_async_unregister_subdev(sd);
1590*4882a593Smuzhiyun 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1591*4882a593Smuzhiyun 	media_entity_cleanup(&sd->entity);
1592*4882a593Smuzhiyun 	mutex_destroy(&ov965x->lock);
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	return 0;
1595*4882a593Smuzhiyun }
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun static const struct i2c_device_id ov965x_id[] = {
1598*4882a593Smuzhiyun 	{ "OV9650", 0 },
1599*4882a593Smuzhiyun 	{ "OV9652", 0 },
1600*4882a593Smuzhiyun 	{ /* sentinel */ }
1601*4882a593Smuzhiyun };
1602*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, ov965x_id);
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_OF)
1605*4882a593Smuzhiyun static const struct of_device_id ov965x_of_match[] = {
1606*4882a593Smuzhiyun 	{ .compatible = "ovti,ov9650", },
1607*4882a593Smuzhiyun 	{ .compatible = "ovti,ov9652", },
1608*4882a593Smuzhiyun 	{ /* sentinel */ }
1609*4882a593Smuzhiyun };
1610*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ov965x_of_match);
1611*4882a593Smuzhiyun #endif
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun static struct i2c_driver ov965x_i2c_driver = {
1614*4882a593Smuzhiyun 	.driver = {
1615*4882a593Smuzhiyun 		.name	= DRIVER_NAME,
1616*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(ov965x_of_match),
1617*4882a593Smuzhiyun 	},
1618*4882a593Smuzhiyun 	.probe_new	= ov965x_probe,
1619*4882a593Smuzhiyun 	.remove		= ov965x_remove,
1620*4882a593Smuzhiyun 	.id_table	= ov965x_id,
1621*4882a593Smuzhiyun };
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun module_i2c_driver(ov965x_i2c_driver);
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun MODULE_AUTHOR("Sylwester Nawrocki <sylvester.nawrocki@gmail.com>");
1626*4882a593Smuzhiyun MODULE_DESCRIPTION("OV9650/OV9652 CMOS Image Sensor driver");
1627*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1628