xref: /openbmc/linux/drivers/media/i2c/ov2659.c (revision aaeb31c0)
1503e5936SBenoit Parrot // SPDX-License-Identifier: GPL-2.0-only
2c4c0283aSBenoit Parrot /*
3c4c0283aSBenoit Parrot  * Omnivision OV2659 CMOS Image Sensor driver
4c4c0283aSBenoit Parrot  *
5c4c0283aSBenoit Parrot  * Copyright (C) 2015 Texas Instruments, Inc.
6c4c0283aSBenoit Parrot  *
7c4c0283aSBenoit Parrot  * Benoit Parrot <bparrot@ti.com>
8c4c0283aSBenoit Parrot  * Lad, Prabhakar <prabhakar.csengg@gmail.com>
9c4c0283aSBenoit Parrot  */
10c4c0283aSBenoit Parrot 
11c4c0283aSBenoit Parrot #include <linux/clk.h>
12c4c0283aSBenoit Parrot #include <linux/delay.h>
132b4a07a0SBenoit Parrot #include <linux/gpio/consumer.h>
14c4c0283aSBenoit Parrot #include <linux/i2c.h>
15c4c0283aSBenoit Parrot #include <linux/module.h>
16c4c0283aSBenoit Parrot #include <linux/of_graph.h>
172b4a07a0SBenoit Parrot #include <linux/pm_runtime.h>
18c4c0283aSBenoit Parrot 
19b5dcee22SMauro Carvalho Chehab #include <media/i2c/ov2659.h>
20c4c0283aSBenoit Parrot #include <media/v4l2-ctrls.h>
21c4c0283aSBenoit Parrot #include <media/v4l2-event.h>
22859969b3SSakari Ailus #include <media/v4l2-fwnode.h>
23c4c0283aSBenoit Parrot #include <media/v4l2-image-sizes.h>
24c4c0283aSBenoit Parrot #include <media/v4l2-subdev.h>
25c4c0283aSBenoit Parrot 
26c4c0283aSBenoit Parrot #define DRIVER_NAME "ov2659"
27c4c0283aSBenoit Parrot 
28c4c0283aSBenoit Parrot /*
29c4c0283aSBenoit Parrot  * OV2659 register definitions
30c4c0283aSBenoit Parrot  */
31c4c0283aSBenoit Parrot #define REG_SOFTWARE_STANDBY		0x0100
32c4c0283aSBenoit Parrot #define REG_SOFTWARE_RESET		0x0103
33c4c0283aSBenoit Parrot #define REG_IO_CTRL00			0x3000
34c4c0283aSBenoit Parrot #define REG_IO_CTRL01			0x3001
35c4c0283aSBenoit Parrot #define REG_IO_CTRL02			0x3002
36c4c0283aSBenoit Parrot #define REG_OUTPUT_VALUE00		0x3008
37c4c0283aSBenoit Parrot #define REG_OUTPUT_VALUE01		0x3009
38c4c0283aSBenoit Parrot #define REG_OUTPUT_VALUE02		0x300d
39c4c0283aSBenoit Parrot #define REG_OUTPUT_SELECT00		0x300e
40c4c0283aSBenoit Parrot #define REG_OUTPUT_SELECT01		0x300f
41c4c0283aSBenoit Parrot #define REG_OUTPUT_SELECT02		0x3010
42c4c0283aSBenoit Parrot #define REG_OUTPUT_DRIVE		0x3011
43c4c0283aSBenoit Parrot #define REG_INPUT_READOUT00		0x302d
44c4c0283aSBenoit Parrot #define REG_INPUT_READOUT01		0x302e
45c4c0283aSBenoit Parrot #define REG_INPUT_READOUT02		0x302f
46c4c0283aSBenoit Parrot 
47c4c0283aSBenoit Parrot #define REG_SC_PLL_CTRL0		0x3003
48c4c0283aSBenoit Parrot #define REG_SC_PLL_CTRL1		0x3004
49c4c0283aSBenoit Parrot #define REG_SC_PLL_CTRL2		0x3005
50c4c0283aSBenoit Parrot #define REG_SC_PLL_CTRL3		0x3006
51c4c0283aSBenoit Parrot #define REG_SC_CHIP_ID_H		0x300a
52c4c0283aSBenoit Parrot #define REG_SC_CHIP_ID_L		0x300b
53c4c0283aSBenoit Parrot #define REG_SC_PWC			0x3014
54c4c0283aSBenoit Parrot #define REG_SC_CLKRST0			0x301a
55c4c0283aSBenoit Parrot #define REG_SC_CLKRST1			0x301b
56c4c0283aSBenoit Parrot #define REG_SC_CLKRST2			0x301c
57c4c0283aSBenoit Parrot #define REG_SC_CLKRST3			0x301d
58c4c0283aSBenoit Parrot #define REG_SC_SUB_ID			0x302a
59c4c0283aSBenoit Parrot #define REG_SC_SCCB_ID			0x302b
60c4c0283aSBenoit Parrot 
61c4c0283aSBenoit Parrot #define REG_GROUP_ADDRESS_00		0x3200
62c4c0283aSBenoit Parrot #define REG_GROUP_ADDRESS_01		0x3201
63c4c0283aSBenoit Parrot #define REG_GROUP_ADDRESS_02		0x3202
64c4c0283aSBenoit Parrot #define REG_GROUP_ADDRESS_03		0x3203
65c4c0283aSBenoit Parrot #define REG_GROUP_ACCESS		0x3208
66c4c0283aSBenoit Parrot 
67c4c0283aSBenoit Parrot #define REG_AWB_R_GAIN_H		0x3400
68c4c0283aSBenoit Parrot #define REG_AWB_R_GAIN_L		0x3401
69c4c0283aSBenoit Parrot #define REG_AWB_G_GAIN_H		0x3402
70c4c0283aSBenoit Parrot #define REG_AWB_G_GAIN_L		0x3403
71c4c0283aSBenoit Parrot #define REG_AWB_B_GAIN_H		0x3404
72c4c0283aSBenoit Parrot #define REG_AWB_B_GAIN_L		0x3405
73c4c0283aSBenoit Parrot #define REG_AWB_MANUAL_CONTROL		0x3406
74c4c0283aSBenoit Parrot 
75c4c0283aSBenoit Parrot #define REG_TIMING_HS_H			0x3800
76c4c0283aSBenoit Parrot #define REG_TIMING_HS_L			0x3801
77c4c0283aSBenoit Parrot #define REG_TIMING_VS_H			0x3802
78c4c0283aSBenoit Parrot #define REG_TIMING_VS_L			0x3803
79c4c0283aSBenoit Parrot #define REG_TIMING_HW_H			0x3804
80c4c0283aSBenoit Parrot #define REG_TIMING_HW_L			0x3805
81c4c0283aSBenoit Parrot #define REG_TIMING_VH_H			0x3806
82c4c0283aSBenoit Parrot #define REG_TIMING_VH_L			0x3807
83c4c0283aSBenoit Parrot #define REG_TIMING_DVPHO_H		0x3808
84c4c0283aSBenoit Parrot #define REG_TIMING_DVPHO_L		0x3809
85c4c0283aSBenoit Parrot #define REG_TIMING_DVPVO_H		0x380a
86c4c0283aSBenoit Parrot #define REG_TIMING_DVPVO_L		0x380b
87c4c0283aSBenoit Parrot #define REG_TIMING_HTS_H		0x380c
88c4c0283aSBenoit Parrot #define REG_TIMING_HTS_L		0x380d
89c4c0283aSBenoit Parrot #define REG_TIMING_VTS_H		0x380e
90c4c0283aSBenoit Parrot #define REG_TIMING_VTS_L		0x380f
91c4c0283aSBenoit Parrot #define REG_TIMING_HOFFS_H		0x3810
92c4c0283aSBenoit Parrot #define REG_TIMING_HOFFS_L		0x3811
93c4c0283aSBenoit Parrot #define REG_TIMING_VOFFS_H		0x3812
94c4c0283aSBenoit Parrot #define REG_TIMING_VOFFS_L		0x3813
95c4c0283aSBenoit Parrot #define REG_TIMING_XINC			0x3814
96c4c0283aSBenoit Parrot #define REG_TIMING_YINC			0x3815
97c4c0283aSBenoit Parrot #define REG_TIMING_VERT_FORMAT		0x3820
98c4c0283aSBenoit Parrot #define REG_TIMING_HORIZ_FORMAT		0x3821
99c4c0283aSBenoit Parrot 
100c4c0283aSBenoit Parrot #define REG_FORMAT_CTRL00		0x4300
101c4c0283aSBenoit Parrot 
102c4c0283aSBenoit Parrot #define REG_VFIFO_READ_START_H		0x4608
103c4c0283aSBenoit Parrot #define REG_VFIFO_READ_START_L		0x4609
104c4c0283aSBenoit Parrot 
105c4c0283aSBenoit Parrot #define REG_DVP_CTRL02			0x4708
106c4c0283aSBenoit Parrot 
107c4c0283aSBenoit Parrot #define REG_ISP_CTRL00			0x5000
108c4c0283aSBenoit Parrot #define REG_ISP_CTRL01			0x5001
109c4c0283aSBenoit Parrot #define REG_ISP_CTRL02			0x5002
110c4c0283aSBenoit Parrot 
111c4c0283aSBenoit Parrot #define REG_LENC_RED_X0_H		0x500c
112c4c0283aSBenoit Parrot #define REG_LENC_RED_X0_L		0x500d
113c4c0283aSBenoit Parrot #define REG_LENC_RED_Y0_H		0x500e
114c4c0283aSBenoit Parrot #define REG_LENC_RED_Y0_L		0x500f
115c4c0283aSBenoit Parrot #define REG_LENC_RED_A1			0x5010
116c4c0283aSBenoit Parrot #define REG_LENC_RED_B1			0x5011
117c4c0283aSBenoit Parrot #define REG_LENC_RED_A2_B2		0x5012
118c4c0283aSBenoit Parrot #define REG_LENC_GREEN_X0_H		0x5013
119c4c0283aSBenoit Parrot #define REG_LENC_GREEN_X0_L		0x5014
120c4c0283aSBenoit Parrot #define REG_LENC_GREEN_Y0_H		0x5015
121c4c0283aSBenoit Parrot #define REG_LENC_GREEN_Y0_L		0x5016
122c4c0283aSBenoit Parrot #define REG_LENC_GREEN_A1		0x5017
123c4c0283aSBenoit Parrot #define REG_LENC_GREEN_B1		0x5018
124c4c0283aSBenoit Parrot #define REG_LENC_GREEN_A2_B2		0x5019
125c4c0283aSBenoit Parrot #define REG_LENC_BLUE_X0_H		0x501a
126c4c0283aSBenoit Parrot #define REG_LENC_BLUE_X0_L		0x501b
127c4c0283aSBenoit Parrot #define REG_LENC_BLUE_Y0_H		0x501c
128c4c0283aSBenoit Parrot #define REG_LENC_BLUE_Y0_L		0x501d
129c4c0283aSBenoit Parrot #define REG_LENC_BLUE_A1		0x501e
130c4c0283aSBenoit Parrot #define REG_LENC_BLUE_B1		0x501f
131c4c0283aSBenoit Parrot #define REG_LENC_BLUE_A2_B2		0x5020
132c4c0283aSBenoit Parrot 
133c4c0283aSBenoit Parrot #define REG_AWB_CTRL00			0x5035
134c4c0283aSBenoit Parrot #define REG_AWB_CTRL01			0x5036
135c4c0283aSBenoit Parrot #define REG_AWB_CTRL02			0x5037
136c4c0283aSBenoit Parrot #define REG_AWB_CTRL03			0x5038
137c4c0283aSBenoit Parrot #define REG_AWB_CTRL04			0x5039
138c4c0283aSBenoit Parrot #define REG_AWB_LOCAL_LIMIT		0x503a
139c4c0283aSBenoit Parrot #define REG_AWB_CTRL12			0x5049
140c4c0283aSBenoit Parrot #define REG_AWB_CTRL13			0x504a
141c4c0283aSBenoit Parrot #define REG_AWB_CTRL14			0x504b
142c4c0283aSBenoit Parrot 
143c4c0283aSBenoit Parrot #define REG_SHARPENMT_THRESH1		0x5064
144c4c0283aSBenoit Parrot #define REG_SHARPENMT_THRESH2		0x5065
145c4c0283aSBenoit Parrot #define REG_SHARPENMT_OFFSET1		0x5066
146c4c0283aSBenoit Parrot #define REG_SHARPENMT_OFFSET2		0x5067
147c4c0283aSBenoit Parrot #define REG_DENOISE_THRESH1		0x5068
148c4c0283aSBenoit Parrot #define REG_DENOISE_THRESH2		0x5069
149c4c0283aSBenoit Parrot #define REG_DENOISE_OFFSET1		0x506a
150c4c0283aSBenoit Parrot #define REG_DENOISE_OFFSET2		0x506b
151c4c0283aSBenoit Parrot #define REG_SHARPEN_THRESH1		0x506c
152c4c0283aSBenoit Parrot #define REG_SHARPEN_THRESH2		0x506d
153c4c0283aSBenoit Parrot #define REG_CIP_CTRL00			0x506e
154c4c0283aSBenoit Parrot #define REG_CIP_CTRL01			0x506f
155c4c0283aSBenoit Parrot 
156c4c0283aSBenoit Parrot #define REG_CMX_SIGN			0x5079
157c4c0283aSBenoit Parrot #define REG_CMX_MISC_CTRL		0x507a
158c4c0283aSBenoit Parrot 
159c4c0283aSBenoit Parrot #define REG_PRE_ISP_CTRL00		0x50a0
160c4c0283aSBenoit Parrot #define TEST_PATTERN_ENABLE		BIT(7)
161c4c0283aSBenoit Parrot #define VERTICAL_COLOR_BAR_MASK		0x53
162c4c0283aSBenoit Parrot 
163c4c0283aSBenoit Parrot #define REG_NULL			0x0000	/* Array end token */
164c4c0283aSBenoit Parrot 
165c4c0283aSBenoit Parrot #define OV265X_ID(_msb, _lsb)		((_msb) << 8 | (_lsb))
166c4c0283aSBenoit Parrot #define OV2659_ID			0x2656
167c4c0283aSBenoit Parrot 
168c4c0283aSBenoit Parrot struct sensor_register {
169c4c0283aSBenoit Parrot 	u16 addr;
170c4c0283aSBenoit Parrot 	u8 value;
171c4c0283aSBenoit Parrot };
172c4c0283aSBenoit Parrot 
173c4c0283aSBenoit Parrot struct ov2659_framesize {
174c4c0283aSBenoit Parrot 	u16 width;
175c4c0283aSBenoit Parrot 	u16 height;
176c4c0283aSBenoit Parrot 	u16 max_exp_lines;
177c4c0283aSBenoit Parrot 	const struct sensor_register *regs;
178c4c0283aSBenoit Parrot };
179c4c0283aSBenoit Parrot 
180c4c0283aSBenoit Parrot struct ov2659_pll_ctrl {
181c4c0283aSBenoit Parrot 	u8 ctrl1;
182c4c0283aSBenoit Parrot 	u8 ctrl2;
183c4c0283aSBenoit Parrot 	u8 ctrl3;
184c4c0283aSBenoit Parrot };
185c4c0283aSBenoit Parrot 
186c4c0283aSBenoit Parrot struct ov2659_pixfmt {
187c4c0283aSBenoit Parrot 	u32 code;
188c4c0283aSBenoit Parrot 	/* Output format Register Value (REG_FORMAT_CTRL00) */
189c4c0283aSBenoit Parrot 	struct sensor_register *format_ctrl_regs;
190c4c0283aSBenoit Parrot };
191c4c0283aSBenoit Parrot 
192c4c0283aSBenoit Parrot struct pll_ctrl_reg {
193c4c0283aSBenoit Parrot 	unsigned int div;
194c4c0283aSBenoit Parrot 	unsigned char reg;
195c4c0283aSBenoit Parrot };
196c4c0283aSBenoit Parrot 
197c4c0283aSBenoit Parrot struct ov2659 {
198c4c0283aSBenoit Parrot 	struct v4l2_subdev sd;
199c4c0283aSBenoit Parrot 	struct media_pad pad;
200c4c0283aSBenoit Parrot 	struct v4l2_mbus_framefmt format;
201c4c0283aSBenoit Parrot 	unsigned int xvclk_frequency;
202c4c0283aSBenoit Parrot 	const struct ov2659_platform_data *pdata;
203c4c0283aSBenoit Parrot 	struct mutex lock;
204c4c0283aSBenoit Parrot 	struct i2c_client *client;
205c4c0283aSBenoit Parrot 	struct v4l2_ctrl_handler ctrls;
206c4c0283aSBenoit Parrot 	struct v4l2_ctrl *link_frequency;
20724786ccdSDillon Min 	struct clk *clk;
208c4c0283aSBenoit Parrot 	const struct ov2659_framesize *frame_size;
209c4c0283aSBenoit Parrot 	struct sensor_register *format_ctrl_regs;
210c4c0283aSBenoit Parrot 	struct ov2659_pll_ctrl pll;
211c4c0283aSBenoit Parrot 	int streaming;
2122b4a07a0SBenoit Parrot 	/* used to control the sensor PWDN pin */
2132b4a07a0SBenoit Parrot 	struct gpio_desc *pwdn_gpio;
2142b4a07a0SBenoit Parrot 	/* used to control the sensor RESETB pin */
2152b4a07a0SBenoit Parrot 	struct gpio_desc *resetb_gpio;
216c4c0283aSBenoit Parrot };
217c4c0283aSBenoit Parrot 
218c4c0283aSBenoit Parrot static const struct sensor_register ov2659_init_regs[] = {
219c4c0283aSBenoit Parrot 	{ REG_IO_CTRL00, 0x03 },
220c4c0283aSBenoit Parrot 	{ REG_IO_CTRL01, 0xff },
221c4c0283aSBenoit Parrot 	{ REG_IO_CTRL02, 0xe0 },
222c4c0283aSBenoit Parrot 	{ 0x3633, 0x3d },
223c4c0283aSBenoit Parrot 	{ 0x3620, 0x02 },
224c4c0283aSBenoit Parrot 	{ 0x3631, 0x11 },
225c4c0283aSBenoit Parrot 	{ 0x3612, 0x04 },
226c4c0283aSBenoit Parrot 	{ 0x3630, 0x20 },
227c4c0283aSBenoit Parrot 	{ 0x4702, 0x02 },
228c4c0283aSBenoit Parrot 	{ 0x370c, 0x34 },
229c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
230c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
231c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
232c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
233c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
234c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
235c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
236c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
237c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x03 },
238c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x20 },
239c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x02 },
240c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0x58 },
241c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x05 },
242c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x14 },
243c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x02 },
244c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0x68 },
245c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x08 },
246c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x02 },
247c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x31 },
248c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x31 },
249c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
250c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
251c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
252c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
253c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
254c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
255c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
256c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
257c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
258c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
259c4c0283aSBenoit Parrot 	{ REG_DVP_CTRL02, 0x01 },
260c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
261c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
262c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
263c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
264c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
265c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
266c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
267c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x81 },
268c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x01 },
269c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
270c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
271c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
272c4c0283aSBenoit Parrot 	{ REG_FORMAT_CTRL00, 0x30 },
273c4c0283aSBenoit Parrot 	{ 0x5086, 0x02 },
274c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL00, 0xfb },
275c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL01, 0x1f },
276c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
277c4c0283aSBenoit Parrot 	{ 0x5025, 0x0e },
278c4c0283aSBenoit Parrot 	{ 0x5026, 0x18 },
279c4c0283aSBenoit Parrot 	{ 0x5027, 0x34 },
280c4c0283aSBenoit Parrot 	{ 0x5028, 0x4c },
281c4c0283aSBenoit Parrot 	{ 0x5029, 0x62 },
282c4c0283aSBenoit Parrot 	{ 0x502a, 0x74 },
283c4c0283aSBenoit Parrot 	{ 0x502b, 0x85 },
284c4c0283aSBenoit Parrot 	{ 0x502c, 0x92 },
285c4c0283aSBenoit Parrot 	{ 0x502d, 0x9e },
286c4c0283aSBenoit Parrot 	{ 0x502e, 0xb2 },
287c4c0283aSBenoit Parrot 	{ 0x502f, 0xc0 },
288c4c0283aSBenoit Parrot 	{ 0x5030, 0xcc },
289c4c0283aSBenoit Parrot 	{ 0x5031, 0xe0 },
290c4c0283aSBenoit Parrot 	{ 0x5032, 0xee },
291c4c0283aSBenoit Parrot 	{ 0x5033, 0xf6 },
292c4c0283aSBenoit Parrot 	{ 0x5034, 0x11 },
293c4c0283aSBenoit Parrot 	{ 0x5070, 0x1c },
294c4c0283aSBenoit Parrot 	{ 0x5071, 0x5b },
295c4c0283aSBenoit Parrot 	{ 0x5072, 0x05 },
296c4c0283aSBenoit Parrot 	{ 0x5073, 0x20 },
297c4c0283aSBenoit Parrot 	{ 0x5074, 0x94 },
298c4c0283aSBenoit Parrot 	{ 0x5075, 0xb4 },
299c4c0283aSBenoit Parrot 	{ 0x5076, 0xb4 },
300c4c0283aSBenoit Parrot 	{ 0x5077, 0xaf },
301c4c0283aSBenoit Parrot 	{ 0x5078, 0x05 },
302c4c0283aSBenoit Parrot 	{ REG_CMX_SIGN, 0x98 },
303c4c0283aSBenoit Parrot 	{ REG_CMX_MISC_CTRL, 0x21 },
304c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL00, 0x6a },
305c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL01, 0x11 },
306c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL02, 0x92 },
307c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL03, 0x21 },
308c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL04, 0xe1 },
309c4c0283aSBenoit Parrot 	{ REG_AWB_LOCAL_LIMIT, 0x01 },
310c4c0283aSBenoit Parrot 	{ 0x503c, 0x05 },
311c4c0283aSBenoit Parrot 	{ 0x503d, 0x08 },
312c4c0283aSBenoit Parrot 	{ 0x503e, 0x08 },
313c4c0283aSBenoit Parrot 	{ 0x503f, 0x64 },
314c4c0283aSBenoit Parrot 	{ 0x5040, 0x58 },
315c4c0283aSBenoit Parrot 	{ 0x5041, 0x2a },
316c4c0283aSBenoit Parrot 	{ 0x5042, 0xc5 },
317c4c0283aSBenoit Parrot 	{ 0x5043, 0x2e },
318c4c0283aSBenoit Parrot 	{ 0x5044, 0x3a },
319c4c0283aSBenoit Parrot 	{ 0x5045, 0x3c },
320c4c0283aSBenoit Parrot 	{ 0x5046, 0x44 },
321c4c0283aSBenoit Parrot 	{ 0x5047, 0xf8 },
322c4c0283aSBenoit Parrot 	{ 0x5048, 0x08 },
323c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL12, 0x70 },
324c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL13, 0xf0 },
325c4c0283aSBenoit Parrot 	{ REG_AWB_CTRL14, 0xf0 },
326c4c0283aSBenoit Parrot 	{ REG_LENC_RED_X0_H, 0x03 },
327c4c0283aSBenoit Parrot 	{ REG_LENC_RED_X0_L, 0x20 },
328c4c0283aSBenoit Parrot 	{ REG_LENC_RED_Y0_H, 0x02 },
329c4c0283aSBenoit Parrot 	{ REG_LENC_RED_Y0_L, 0x5c },
330c4c0283aSBenoit Parrot 	{ REG_LENC_RED_A1, 0x48 },
331c4c0283aSBenoit Parrot 	{ REG_LENC_RED_B1, 0x00 },
332c4c0283aSBenoit Parrot 	{ REG_LENC_RED_A2_B2, 0x66 },
333c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_X0_H, 0x03 },
334c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_X0_L, 0x30 },
335c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_Y0_H, 0x02 },
336c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_Y0_L, 0x7c },
337c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_A1, 0x40 },
338c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_B1, 0x00 },
339c4c0283aSBenoit Parrot 	{ REG_LENC_GREEN_A2_B2, 0x66 },
340c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_X0_H, 0x03 },
341c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_X0_L, 0x10 },
342c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_Y0_H, 0x02 },
343c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_Y0_L, 0x7c },
344c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_A1, 0x3a },
345c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_B1, 0x00 },
346c4c0283aSBenoit Parrot 	{ REG_LENC_BLUE_A2_B2, 0x66 },
347c4c0283aSBenoit Parrot 	{ REG_CIP_CTRL00, 0x44 },
348c4c0283aSBenoit Parrot 	{ REG_SHARPENMT_THRESH1, 0x08 },
349c4c0283aSBenoit Parrot 	{ REG_SHARPENMT_THRESH2, 0x10 },
350c4c0283aSBenoit Parrot 	{ REG_SHARPENMT_OFFSET1, 0x12 },
351c4c0283aSBenoit Parrot 	{ REG_SHARPENMT_OFFSET2, 0x02 },
352c4c0283aSBenoit Parrot 	{ REG_SHARPEN_THRESH1, 0x08 },
353c4c0283aSBenoit Parrot 	{ REG_SHARPEN_THRESH2, 0x10 },
354c4c0283aSBenoit Parrot 	{ REG_CIP_CTRL01, 0xa6 },
355c4c0283aSBenoit Parrot 	{ REG_DENOISE_THRESH1, 0x08 },
356c4c0283aSBenoit Parrot 	{ REG_DENOISE_THRESH2, 0x10 },
357c4c0283aSBenoit Parrot 	{ REG_DENOISE_OFFSET1, 0x04 },
358c4c0283aSBenoit Parrot 	{ REG_DENOISE_OFFSET2, 0x12 },
359c4c0283aSBenoit Parrot 	{ 0x507e, 0x40 },
360c4c0283aSBenoit Parrot 	{ 0x507f, 0x20 },
361c4c0283aSBenoit Parrot 	{ 0x507b, 0x02 },
362c4c0283aSBenoit Parrot 	{ REG_CMX_MISC_CTRL, 0x01 },
363c4c0283aSBenoit Parrot 	{ 0x5084, 0x0c },
364c4c0283aSBenoit Parrot 	{ 0x5085, 0x3e },
365c4c0283aSBenoit Parrot 	{ 0x5005, 0x80 },
366c4c0283aSBenoit Parrot 	{ 0x3a0f, 0x30 },
367c4c0283aSBenoit Parrot 	{ 0x3a10, 0x28 },
368c4c0283aSBenoit Parrot 	{ 0x3a1b, 0x32 },
369c4c0283aSBenoit Parrot 	{ 0x3a1e, 0x26 },
370c4c0283aSBenoit Parrot 	{ 0x3a11, 0x60 },
371c4c0283aSBenoit Parrot 	{ 0x3a1f, 0x14 },
372c4c0283aSBenoit Parrot 	{ 0x5060, 0x69 },
373c4c0283aSBenoit Parrot 	{ 0x5061, 0x7d },
374c4c0283aSBenoit Parrot 	{ 0x5062, 0x7d },
375c4c0283aSBenoit Parrot 	{ 0x5063, 0x69 },
376c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
377c4c0283aSBenoit Parrot };
378c4c0283aSBenoit Parrot 
379c4c0283aSBenoit Parrot /* 1280X720 720p */
380c4c0283aSBenoit Parrot static struct sensor_register ov2659_720p[] = {
381c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
382c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0xa0 },
383c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
384c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0xf0 },
385c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x05 },
386c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0xbf },
387c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x03 },
388c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xcb },
389c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x05 },
390c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x00 },
391c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x02 },
392c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0xd0 },
393c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x06 },
394c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x4c },
395c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x02 },
396c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0xe8 },
397c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x10 },
398c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x06 },
399c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x11 },
400c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x11 },
401c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x80 },
402c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x00 },
4039d669fbfSBenoit Parrot 	{ 0x370a, 0x12 },
404c4c0283aSBenoit Parrot 	{ 0x3a03, 0xe8 },
405c4c0283aSBenoit Parrot 	{ 0x3a09, 0x6f },
406c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x5d },
407c4c0283aSBenoit Parrot 	{ 0x3a15, 0x9a },
4089d669fbfSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
4099d669fbfSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
4109d669fbfSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
411c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
412c4c0283aSBenoit Parrot };
413c4c0283aSBenoit Parrot 
414c4c0283aSBenoit Parrot /* 1600X1200 UXGA */
415c4c0283aSBenoit Parrot static struct sensor_register ov2659_uxga[] = {
416c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
417c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
418c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
419c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
420c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
421c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
422c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
423c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xbb },
424c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x06 },
425c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x40 },
426c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x04 },
427c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0xb0 },
428c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x07 },
429c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x9f },
430c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x04 },
431c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0xd0 },
432c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x10 },
433c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x06 },
434c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x11 },
435c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x11 },
436c4c0283aSBenoit Parrot 	{ 0x3a02, 0x04 },
437c4c0283aSBenoit Parrot 	{ 0x3a03, 0xd0 },
438c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
439c4c0283aSBenoit Parrot 	{ 0x3a09, 0xb8 },
440c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
441c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x9a },
442c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
443c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
444c4c0283aSBenoit Parrot 	{ 0x3a14, 0x04 },
445c4c0283aSBenoit Parrot 	{ 0x3a15, 0x50 },
446c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
447c4c0283aSBenoit Parrot 	{ 0x3634, 0x44 },
448c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
449c4c0283aSBenoit Parrot 	{ 0x3702, 0x30 },
450c4c0283aSBenoit Parrot 	{ 0x3703, 0x48 },
451c4c0283aSBenoit Parrot 	{ 0x3704, 0x48 },
452c4c0283aSBenoit Parrot 	{ 0x3705, 0x18 },
453c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x80 },
454c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x00 },
455c4c0283aSBenoit Parrot 	{ 0x370a, 0x12 },
456c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
457c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
458c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
459c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
460c4c0283aSBenoit Parrot };
461c4c0283aSBenoit Parrot 
462c4c0283aSBenoit Parrot /* 1280X1024 SXGA */
463c4c0283aSBenoit Parrot static struct sensor_register ov2659_sxga[] = {
464c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
465c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
466c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
467c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
468c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
469c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
470c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
471c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
472c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x05 },
473c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x00 },
474c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x04 },
475c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0x00 },
476c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x07 },
477c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x9c },
478c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x04 },
479c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0xd0 },
480c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x10 },
481c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x06 },
482c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x11 },
483c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x11 },
484c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
485c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
486c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
487c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
488c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
489c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
490c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
491c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
492c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
493c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
494c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
495c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
496c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
497c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
498c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
499c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
500c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
501c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x80 },
502c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x00 },
503c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
504c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
505c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
506c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
507c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
508c4c0283aSBenoit Parrot };
509c4c0283aSBenoit Parrot 
510c4c0283aSBenoit Parrot /* 1024X768 SXGA */
511c4c0283aSBenoit Parrot static struct sensor_register ov2659_xga[] = {
512c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
513c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
514c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
515c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
516c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
517c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
518c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
519c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
520c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x04 },
521c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x00 },
522c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x03 },
523c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0x00 },
524c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x07 },
525c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x9c },
526c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x04 },
527c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0xd0 },
528c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x10 },
529c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x06 },
530c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x11 },
531c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x11 },
532c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
533c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
534c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
535c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
536c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
537c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
538c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
539c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
540c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
541c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
542c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
543c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
544c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
545c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
546c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
547c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
548c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
549c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x80 },
550c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x00 },
551c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
552c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
553c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
554c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
555c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
556c4c0283aSBenoit Parrot };
557c4c0283aSBenoit Parrot 
558c4c0283aSBenoit Parrot /* 800X600 SVGA */
559c4c0283aSBenoit Parrot static struct sensor_register ov2659_svga[] = {
560c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
561c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
562c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
563c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
564c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
565c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
566c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
567c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
568c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x03 },
569c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x20 },
570c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x02 },
571c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0x58 },
572c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x05 },
573c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x14 },
574c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x02 },
575c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0x68 },
576c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x08 },
577c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x02 },
578c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x31 },
579c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x31 },
580c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
581c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
582c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
583c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
584c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
585c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
586c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
587c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
588c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
589c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
590c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
591c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
592c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
593c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
594c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
595c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
596c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
597c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x81 },
598c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x01 },
599c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
600c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
601c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0x80 },
602c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x00 },
603c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
604c4c0283aSBenoit Parrot };
605c4c0283aSBenoit Parrot 
606c4c0283aSBenoit Parrot /* 640X480 VGA */
607c4c0283aSBenoit Parrot static struct sensor_register ov2659_vga[] = {
608c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
609c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
610c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
611c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
612c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
613c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
614c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
615c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
616c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x02 },
617c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x80 },
618c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x01 },
619c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0xe0 },
620c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x05 },
621c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x14 },
622c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x02 },
623c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0x68 },
624c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x08 },
625c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x02 },
626c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x31 },
627c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x31 },
628c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
629c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
630c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
631c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
632c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
633c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
634c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
635c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
636c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
637c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
638c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
639c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
640c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
641c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
642c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
643c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
644c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
645c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x81 },
646c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x01 },
647c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
648c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
6497359fac5SBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0xa0 },
650c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x10 },
651c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
652c4c0283aSBenoit Parrot };
653c4c0283aSBenoit Parrot 
654c4c0283aSBenoit Parrot /* 320X240 QVGA */
655c4c0283aSBenoit Parrot static  struct sensor_register ov2659_qvga[] = {
656c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_H, 0x00 },
657c4c0283aSBenoit Parrot 	{ REG_TIMING_HS_L, 0x00 },
658c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_H, 0x00 },
659c4c0283aSBenoit Parrot 	{ REG_TIMING_VS_L, 0x00 },
660c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_H, 0x06 },
661c4c0283aSBenoit Parrot 	{ REG_TIMING_HW_L, 0x5f },
662c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_H, 0x04 },
663c4c0283aSBenoit Parrot 	{ REG_TIMING_VH_L, 0xb7 },
664c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_H, 0x01 },
665c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPHO_L, 0x40 },
666c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_H, 0x00 },
667c4c0283aSBenoit Parrot 	{ REG_TIMING_DVPVO_L, 0xf0 },
668c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_H, 0x05 },
669c4c0283aSBenoit Parrot 	{ REG_TIMING_HTS_L, 0x14 },
670c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_H, 0x02 },
671c4c0283aSBenoit Parrot 	{ REG_TIMING_VTS_L, 0x68 },
672c4c0283aSBenoit Parrot 	{ REG_TIMING_HOFFS_L, 0x08 },
673c4c0283aSBenoit Parrot 	{ REG_TIMING_VOFFS_L, 0x02 },
674c4c0283aSBenoit Parrot 	{ REG_TIMING_XINC, 0x31 },
675c4c0283aSBenoit Parrot 	{ REG_TIMING_YINC, 0x31 },
676c4c0283aSBenoit Parrot 	{ 0x3a02, 0x02 },
677c4c0283aSBenoit Parrot 	{ 0x3a03, 0x68 },
678c4c0283aSBenoit Parrot 	{ 0x3a08, 0x00 },
679c4c0283aSBenoit Parrot 	{ 0x3a09, 0x5c },
680c4c0283aSBenoit Parrot 	{ 0x3a0a, 0x00 },
681c4c0283aSBenoit Parrot 	{ 0x3a0b, 0x4d },
682c4c0283aSBenoit Parrot 	{ 0x3a0d, 0x08 },
683c4c0283aSBenoit Parrot 	{ 0x3a0e, 0x06 },
684c4c0283aSBenoit Parrot 	{ 0x3a14, 0x02 },
685c4c0283aSBenoit Parrot 	{ 0x3a15, 0x28 },
686c4c0283aSBenoit Parrot 	{ 0x3623, 0x00 },
687c4c0283aSBenoit Parrot 	{ 0x3634, 0x76 },
688c4c0283aSBenoit Parrot 	{ 0x3701, 0x44 },
689c4c0283aSBenoit Parrot 	{ 0x3702, 0x18 },
690c4c0283aSBenoit Parrot 	{ 0x3703, 0x24 },
691c4c0283aSBenoit Parrot 	{ 0x3704, 0x24 },
692c4c0283aSBenoit Parrot 	{ 0x3705, 0x0c },
693c4c0283aSBenoit Parrot 	{ REG_TIMING_VERT_FORMAT, 0x81 },
694c4c0283aSBenoit Parrot 	{ REG_TIMING_HORIZ_FORMAT, 0x01 },
695c4c0283aSBenoit Parrot 	{ 0x370a, 0x52 },
696c4c0283aSBenoit Parrot 	{ REG_VFIFO_READ_START_H, 0x00 },
6977359fac5SBenoit Parrot 	{ REG_VFIFO_READ_START_L, 0xa0 },
698c4c0283aSBenoit Parrot 	{ REG_ISP_CTRL02, 0x10 },
699c4c0283aSBenoit Parrot 	{ REG_NULL, 0x00 },
700c4c0283aSBenoit Parrot };
701c4c0283aSBenoit Parrot 
702c4c0283aSBenoit Parrot static const struct pll_ctrl_reg ctrl3[] = {
703c4c0283aSBenoit Parrot 	{ 1, 0x00 },
704c4c0283aSBenoit Parrot 	{ 2, 0x02 },
705c4c0283aSBenoit Parrot 	{ 3, 0x03 },
706c4c0283aSBenoit Parrot 	{ 4, 0x06 },
707c4c0283aSBenoit Parrot 	{ 6, 0x0d },
708c4c0283aSBenoit Parrot 	{ 8, 0x0e },
709c4c0283aSBenoit Parrot 	{ 12, 0x0f },
710c4c0283aSBenoit Parrot 	{ 16, 0x12 },
711c4c0283aSBenoit Parrot 	{ 24, 0x13 },
712c4c0283aSBenoit Parrot 	{ 32, 0x16 },
713c4c0283aSBenoit Parrot 	{ 48, 0x1b },
714c4c0283aSBenoit Parrot 	{ 64, 0x1e },
715c4c0283aSBenoit Parrot 	{ 96, 0x1f },
716c4c0283aSBenoit Parrot 	{ 0, 0x00 },
717c4c0283aSBenoit Parrot };
718c4c0283aSBenoit Parrot 
719c4c0283aSBenoit Parrot static const struct pll_ctrl_reg ctrl1[] = {
720c4c0283aSBenoit Parrot 	{ 2, 0x10 },
721c4c0283aSBenoit Parrot 	{ 4, 0x20 },
722c4c0283aSBenoit Parrot 	{ 6, 0x30 },
723c4c0283aSBenoit Parrot 	{ 8, 0x40 },
724c4c0283aSBenoit Parrot 	{ 10, 0x50 },
725c4c0283aSBenoit Parrot 	{ 12, 0x60 },
726c4c0283aSBenoit Parrot 	{ 14, 0x70 },
727c4c0283aSBenoit Parrot 	{ 16, 0x80 },
728c4c0283aSBenoit Parrot 	{ 18, 0x90 },
729c4c0283aSBenoit Parrot 	{ 20, 0xa0 },
730c4c0283aSBenoit Parrot 	{ 22, 0xb0 },
731c4c0283aSBenoit Parrot 	{ 24, 0xc0 },
732c4c0283aSBenoit Parrot 	{ 26, 0xd0 },
733c4c0283aSBenoit Parrot 	{ 28, 0xe0 },
734c4c0283aSBenoit Parrot 	{ 30, 0xf0 },
735c4c0283aSBenoit Parrot 	{ 0, 0x00 },
736c4c0283aSBenoit Parrot };
737c4c0283aSBenoit Parrot 
738c4c0283aSBenoit Parrot static const struct ov2659_framesize ov2659_framesizes[] = {
739c4c0283aSBenoit Parrot 	{ /* QVGA */
740c4c0283aSBenoit Parrot 		.width		= 320,
741c4c0283aSBenoit Parrot 		.height		= 240,
742c4c0283aSBenoit Parrot 		.regs		= ov2659_qvga,
743c4c0283aSBenoit Parrot 		.max_exp_lines	= 248,
744c4c0283aSBenoit Parrot 	}, { /* VGA */
745c4c0283aSBenoit Parrot 		.width		= 640,
746c4c0283aSBenoit Parrot 		.height		= 480,
747c4c0283aSBenoit Parrot 		.regs		= ov2659_vga,
748c4c0283aSBenoit Parrot 		.max_exp_lines	= 498,
749c4c0283aSBenoit Parrot 	}, { /* SVGA */
750c4c0283aSBenoit Parrot 		.width		= 800,
751c4c0283aSBenoit Parrot 		.height		= 600,
752c4c0283aSBenoit Parrot 		.regs		= ov2659_svga,
753c4c0283aSBenoit Parrot 		.max_exp_lines	= 498,
754c4c0283aSBenoit Parrot 	}, { /* XGA */
755c4c0283aSBenoit Parrot 		.width		= 1024,
756c4c0283aSBenoit Parrot 		.height		= 768,
757c4c0283aSBenoit Parrot 		.regs		= ov2659_xga,
758c4c0283aSBenoit Parrot 		.max_exp_lines	= 498,
759c4c0283aSBenoit Parrot 	}, { /* 720P */
760c4c0283aSBenoit Parrot 		.width		= 1280,
761c4c0283aSBenoit Parrot 		.height		= 720,
762c4c0283aSBenoit Parrot 		.regs		= ov2659_720p,
763c4c0283aSBenoit Parrot 		.max_exp_lines	= 498,
764c4c0283aSBenoit Parrot 	}, { /* SXGA */
765c4c0283aSBenoit Parrot 		.width		= 1280,
766c4c0283aSBenoit Parrot 		.height		= 1024,
767c4c0283aSBenoit Parrot 		.regs		= ov2659_sxga,
768c4c0283aSBenoit Parrot 		.max_exp_lines	= 1048,
769c4c0283aSBenoit Parrot 	}, { /* UXGA */
770c4c0283aSBenoit Parrot 		.width		= 1600,
771c4c0283aSBenoit Parrot 		.height		= 1200,
772c4c0283aSBenoit Parrot 		.regs		= ov2659_uxga,
773c4c0283aSBenoit Parrot 		.max_exp_lines	= 498,
774c4c0283aSBenoit Parrot 	},
775c4c0283aSBenoit Parrot };
776c4c0283aSBenoit Parrot 
777c4c0283aSBenoit Parrot /* YUV422 YUYV*/
778c4c0283aSBenoit Parrot static struct sensor_register ov2659_format_yuyv[] = {
779c4c0283aSBenoit Parrot 	{ REG_FORMAT_CTRL00, 0x30 },
780c4c0283aSBenoit Parrot 	{ REG_NULL, 0x0 },
781c4c0283aSBenoit Parrot };
782c4c0283aSBenoit Parrot 
783c4c0283aSBenoit Parrot /* YUV422 UYVY  */
784c4c0283aSBenoit Parrot static struct sensor_register ov2659_format_uyvy[] = {
785c4c0283aSBenoit Parrot 	{ REG_FORMAT_CTRL00, 0x32 },
786c4c0283aSBenoit Parrot 	{ REG_NULL, 0x0 },
787c4c0283aSBenoit Parrot };
788c4c0283aSBenoit Parrot 
789c4c0283aSBenoit Parrot /* Raw Bayer BGGR */
790c4c0283aSBenoit Parrot static struct sensor_register ov2659_format_bggr[] = {
791c4c0283aSBenoit Parrot 	{ REG_FORMAT_CTRL00, 0x00 },
792c4c0283aSBenoit Parrot 	{ REG_NULL, 0x0 },
793c4c0283aSBenoit Parrot };
794c4c0283aSBenoit Parrot 
795c4c0283aSBenoit Parrot /* RGB565 */
796c4c0283aSBenoit Parrot static struct sensor_register ov2659_format_rgb565[] = {
797c4c0283aSBenoit Parrot 	{ REG_FORMAT_CTRL00, 0x60 },
798c4c0283aSBenoit Parrot 	{ REG_NULL, 0x0 },
799c4c0283aSBenoit Parrot };
800c4c0283aSBenoit Parrot 
801c4c0283aSBenoit Parrot static const struct ov2659_pixfmt ov2659_formats[] = {
802c4c0283aSBenoit Parrot 	{
803c4c0283aSBenoit Parrot 		.code = MEDIA_BUS_FMT_YUYV8_2X8,
804c4c0283aSBenoit Parrot 		.format_ctrl_regs = ov2659_format_yuyv,
805c4c0283aSBenoit Parrot 	}, {
806c4c0283aSBenoit Parrot 		.code = MEDIA_BUS_FMT_UYVY8_2X8,
807c4c0283aSBenoit Parrot 		.format_ctrl_regs = ov2659_format_uyvy,
808c4c0283aSBenoit Parrot 	}, {
809c4c0283aSBenoit Parrot 		.code = MEDIA_BUS_FMT_RGB565_2X8_BE,
810c4c0283aSBenoit Parrot 		.format_ctrl_regs = ov2659_format_rgb565,
811c4c0283aSBenoit Parrot 	}, {
812c4c0283aSBenoit Parrot 		.code = MEDIA_BUS_FMT_SBGGR8_1X8,
813c4c0283aSBenoit Parrot 		.format_ctrl_regs = ov2659_format_bggr,
814c4c0283aSBenoit Parrot 	},
815c4c0283aSBenoit Parrot };
816c4c0283aSBenoit Parrot 
to_ov2659(struct v4l2_subdev * sd)817c4c0283aSBenoit Parrot static inline struct ov2659 *to_ov2659(struct v4l2_subdev *sd)
818c4c0283aSBenoit Parrot {
819c4c0283aSBenoit Parrot 	return container_of(sd, struct ov2659, sd);
820c4c0283aSBenoit Parrot }
821c4c0283aSBenoit Parrot 
822c4c0283aSBenoit Parrot /* sensor register write */
ov2659_write(struct i2c_client * client,u16 reg,u8 val)823c4c0283aSBenoit Parrot static int ov2659_write(struct i2c_client *client, u16 reg, u8 val)
824c4c0283aSBenoit Parrot {
825c4c0283aSBenoit Parrot 	struct i2c_msg msg;
826c4c0283aSBenoit Parrot 	u8 buf[3];
827c4c0283aSBenoit Parrot 	int ret;
828c4c0283aSBenoit Parrot 
829c4c0283aSBenoit Parrot 	buf[0] = reg >> 8;
830c4c0283aSBenoit Parrot 	buf[1] = reg & 0xFF;
831c4c0283aSBenoit Parrot 	buf[2] = val;
832c4c0283aSBenoit Parrot 
833c4c0283aSBenoit Parrot 	msg.addr = client->addr;
834c4c0283aSBenoit Parrot 	msg.flags = client->flags;
835c4c0283aSBenoit Parrot 	msg.buf = buf;
836c4c0283aSBenoit Parrot 	msg.len = sizeof(buf);
837c4c0283aSBenoit Parrot 
838c4c0283aSBenoit Parrot 	ret = i2c_transfer(client->adapter, &msg, 1);
839c4c0283aSBenoit Parrot 	if (ret >= 0)
840c4c0283aSBenoit Parrot 		return 0;
841c4c0283aSBenoit Parrot 
842c4c0283aSBenoit Parrot 	dev_dbg(&client->dev,
843c4c0283aSBenoit Parrot 		"ov2659 write reg(0x%x val:0x%x) failed !\n", reg, val);
844c4c0283aSBenoit Parrot 
845c4c0283aSBenoit Parrot 	return ret;
846c4c0283aSBenoit Parrot }
847c4c0283aSBenoit Parrot 
848c4c0283aSBenoit Parrot /* sensor register read */
ov2659_read(struct i2c_client * client,u16 reg,u8 * val)849c4c0283aSBenoit Parrot static int ov2659_read(struct i2c_client *client, u16 reg, u8 *val)
850c4c0283aSBenoit Parrot {
851c4c0283aSBenoit Parrot 	struct i2c_msg msg[2];
852c4c0283aSBenoit Parrot 	u8 buf[2];
853c4c0283aSBenoit Parrot 	int ret;
854c4c0283aSBenoit Parrot 
855c4c0283aSBenoit Parrot 	buf[0] = reg >> 8;
856c4c0283aSBenoit Parrot 	buf[1] = reg & 0xFF;
857c4c0283aSBenoit Parrot 
858c4c0283aSBenoit Parrot 	msg[0].addr = client->addr;
859c4c0283aSBenoit Parrot 	msg[0].flags = client->flags;
860c4c0283aSBenoit Parrot 	msg[0].buf = buf;
861c4c0283aSBenoit Parrot 	msg[0].len = sizeof(buf);
862c4c0283aSBenoit Parrot 
863c4c0283aSBenoit Parrot 	msg[1].addr = client->addr;
864c4c0283aSBenoit Parrot 	msg[1].flags = client->flags | I2C_M_RD;
865c4c0283aSBenoit Parrot 	msg[1].buf = buf;
866c4c0283aSBenoit Parrot 	msg[1].len = 1;
867c4c0283aSBenoit Parrot 
868c4c0283aSBenoit Parrot 	ret = i2c_transfer(client->adapter, msg, 2);
869c4c0283aSBenoit Parrot 	if (ret >= 0) {
870c4c0283aSBenoit Parrot 		*val = buf[0];
871c4c0283aSBenoit Parrot 		return 0;
872c4c0283aSBenoit Parrot 	}
873c4c0283aSBenoit Parrot 
874c4c0283aSBenoit Parrot 	dev_dbg(&client->dev,
875c4c0283aSBenoit Parrot 		"ov2659 read reg(0x%x val:0x%x) failed !\n", reg, *val);
876c4c0283aSBenoit Parrot 
877c4c0283aSBenoit Parrot 	return ret;
878c4c0283aSBenoit Parrot }
879c4c0283aSBenoit Parrot 
ov2659_write_array(struct i2c_client * client,const struct sensor_register * regs)880c4c0283aSBenoit Parrot static int ov2659_write_array(struct i2c_client *client,
881c4c0283aSBenoit Parrot 			      const struct sensor_register *regs)
882c4c0283aSBenoit Parrot {
883c4c0283aSBenoit Parrot 	int i, ret = 0;
884c4c0283aSBenoit Parrot 
885c4c0283aSBenoit Parrot 	for (i = 0; ret == 0 && regs[i].addr; i++)
886c4c0283aSBenoit Parrot 		ret = ov2659_write(client, regs[i].addr, regs[i].value);
887c4c0283aSBenoit Parrot 
888c4c0283aSBenoit Parrot 	return ret;
889c4c0283aSBenoit Parrot }
890c4c0283aSBenoit Parrot 
ov2659_pll_calc_params(struct ov2659 * ov2659)891c4c0283aSBenoit Parrot static void ov2659_pll_calc_params(struct ov2659 *ov2659)
892c4c0283aSBenoit Parrot {
893c4c0283aSBenoit Parrot 	const struct ov2659_platform_data *pdata = ov2659->pdata;
894c4c0283aSBenoit Parrot 	u8 ctrl1_reg = 0, ctrl2_reg = 0, ctrl3_reg = 0;
895c4c0283aSBenoit Parrot 	struct i2c_client *client = ov2659->client;
896c4c0283aSBenoit Parrot 	unsigned int desired = pdata->link_frequency;
897c4c0283aSBenoit Parrot 	u32 prediv, postdiv, mult;
898c4c0283aSBenoit Parrot 	u32 bestdelta = -1;
899c4c0283aSBenoit Parrot 	u32 delta, actual;
900c4c0283aSBenoit Parrot 	int i, j;
901c4c0283aSBenoit Parrot 
902c4c0283aSBenoit Parrot 	for (i = 0; ctrl1[i].div != 0; i++) {
903c4c0283aSBenoit Parrot 		postdiv = ctrl1[i].div;
904c4c0283aSBenoit Parrot 		for (j = 0; ctrl3[j].div != 0; j++) {
905c4c0283aSBenoit Parrot 			prediv = ctrl3[j].div;
906c4c0283aSBenoit Parrot 			for (mult = 1; mult <= 63; mult++) {
907c4c0283aSBenoit Parrot 				actual  = ov2659->xvclk_frequency;
908c4c0283aSBenoit Parrot 				actual *= mult;
909c4c0283aSBenoit Parrot 				actual /= prediv;
910c4c0283aSBenoit Parrot 				actual /= postdiv;
911c4c0283aSBenoit Parrot 				delta = actual - desired;
912c4c0283aSBenoit Parrot 				delta = abs(delta);
913c4c0283aSBenoit Parrot 
914c4c0283aSBenoit Parrot 				if ((delta < bestdelta) || (bestdelta == -1)) {
915c4c0283aSBenoit Parrot 					bestdelta = delta;
916c4c0283aSBenoit Parrot 					ctrl1_reg = ctrl1[i].reg;
917c4c0283aSBenoit Parrot 					ctrl2_reg = mult;
918c4c0283aSBenoit Parrot 					ctrl3_reg = ctrl3[j].reg;
919c4c0283aSBenoit Parrot 				}
920c4c0283aSBenoit Parrot 			}
921c4c0283aSBenoit Parrot 		}
922c4c0283aSBenoit Parrot 	}
923c4c0283aSBenoit Parrot 
924c4c0283aSBenoit Parrot 	ov2659->pll.ctrl1 = ctrl1_reg;
925c4c0283aSBenoit Parrot 	ov2659->pll.ctrl2 = ctrl2_reg;
926c4c0283aSBenoit Parrot 	ov2659->pll.ctrl3 = ctrl3_reg;
927c4c0283aSBenoit Parrot 
928c4c0283aSBenoit Parrot 	dev_dbg(&client->dev,
929c4c0283aSBenoit Parrot 		"Actual reg config: ctrl1_reg: %02x ctrl2_reg: %02x ctrl3_reg: %02x\n",
930c4c0283aSBenoit Parrot 		ctrl1_reg, ctrl2_reg, ctrl3_reg);
931c4c0283aSBenoit Parrot }
932c4c0283aSBenoit Parrot 
ov2659_set_pixel_clock(struct ov2659 * ov2659)933c4c0283aSBenoit Parrot static int ov2659_set_pixel_clock(struct ov2659 *ov2659)
934c4c0283aSBenoit Parrot {
935c4c0283aSBenoit Parrot 	struct i2c_client *client = ov2659->client;
936c4c0283aSBenoit Parrot 	struct sensor_register pll_regs[] = {
937c4c0283aSBenoit Parrot 		{REG_SC_PLL_CTRL1, ov2659->pll.ctrl1},
938c4c0283aSBenoit Parrot 		{REG_SC_PLL_CTRL2, ov2659->pll.ctrl2},
939c4c0283aSBenoit Parrot 		{REG_SC_PLL_CTRL3, ov2659->pll.ctrl3},
940c4c0283aSBenoit Parrot 		{REG_NULL, 0x00},
941c4c0283aSBenoit Parrot 	};
942c4c0283aSBenoit Parrot 
943c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s\n", __func__);
944c4c0283aSBenoit Parrot 
945c4c0283aSBenoit Parrot 	return ov2659_write_array(client, pll_regs);
946c4c0283aSBenoit Parrot };
947c4c0283aSBenoit Parrot 
ov2659_get_default_format(struct v4l2_mbus_framefmt * format)948c4c0283aSBenoit Parrot static void ov2659_get_default_format(struct v4l2_mbus_framefmt *format)
949c4c0283aSBenoit Parrot {
950c4c0283aSBenoit Parrot 	format->width = ov2659_framesizes[2].width;
951c4c0283aSBenoit Parrot 	format->height = ov2659_framesizes[2].height;
952c4c0283aSBenoit Parrot 	format->colorspace = V4L2_COLORSPACE_SRGB;
953c4c0283aSBenoit Parrot 	format->code = ov2659_formats[0].code;
954c4c0283aSBenoit Parrot 	format->field = V4L2_FIELD_NONE;
955c4c0283aSBenoit Parrot }
956c4c0283aSBenoit Parrot 
ov2659_set_streaming(struct ov2659 * ov2659,int on)957c4c0283aSBenoit Parrot static void ov2659_set_streaming(struct ov2659 *ov2659, int on)
958c4c0283aSBenoit Parrot {
959c4c0283aSBenoit Parrot 	struct i2c_client *client = ov2659->client;
960c4c0283aSBenoit Parrot 	int ret;
961c4c0283aSBenoit Parrot 
962c4c0283aSBenoit Parrot 	on = !!on;
963c4c0283aSBenoit Parrot 
964c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
965c4c0283aSBenoit Parrot 
966c4c0283aSBenoit Parrot 	ret = ov2659_write(client, REG_SOFTWARE_STANDBY, on);
967c4c0283aSBenoit Parrot 	if (ret)
968c4c0283aSBenoit Parrot 		dev_err(&client->dev, "ov2659 soft standby failed\n");
969c4c0283aSBenoit Parrot }
970c4c0283aSBenoit Parrot 
ov2659_init(struct v4l2_subdev * sd,u32 val)971c4c0283aSBenoit Parrot static int ov2659_init(struct v4l2_subdev *sd, u32 val)
972c4c0283aSBenoit Parrot {
973c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
974c4c0283aSBenoit Parrot 
975c4c0283aSBenoit Parrot 	return ov2659_write_array(client, ov2659_init_regs);
976c4c0283aSBenoit Parrot }
977c4c0283aSBenoit Parrot 
978c4c0283aSBenoit Parrot /*
979c4c0283aSBenoit Parrot  * V4L2 subdev video and pad level operations
980c4c0283aSBenoit Parrot  */
981c4c0283aSBenoit Parrot 
ov2659_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)982c4c0283aSBenoit Parrot static int ov2659_enum_mbus_code(struct v4l2_subdev *sd,
9830d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
984c4c0283aSBenoit Parrot 				 struct v4l2_subdev_mbus_code_enum *code)
985c4c0283aSBenoit Parrot {
986c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
987c4c0283aSBenoit Parrot 
988c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
989c4c0283aSBenoit Parrot 
990c4c0283aSBenoit Parrot 	if (code->index >= ARRAY_SIZE(ov2659_formats))
991c4c0283aSBenoit Parrot 		return -EINVAL;
992c4c0283aSBenoit Parrot 
993c4c0283aSBenoit Parrot 	code->code = ov2659_formats[code->index].code;
994c4c0283aSBenoit Parrot 
995c4c0283aSBenoit Parrot 	return 0;
996c4c0283aSBenoit Parrot }
997c4c0283aSBenoit Parrot 
ov2659_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)998c4c0283aSBenoit Parrot static int ov2659_enum_frame_sizes(struct v4l2_subdev *sd,
9990d346d2aSTomi Valkeinen 				   struct v4l2_subdev_state *sd_state,
1000c4c0283aSBenoit Parrot 				   struct v4l2_subdev_frame_size_enum *fse)
1001c4c0283aSBenoit Parrot {
1002c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1003c4c0283aSBenoit Parrot 	int i = ARRAY_SIZE(ov2659_formats);
1004c4c0283aSBenoit Parrot 
1005c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
1006c4c0283aSBenoit Parrot 
1007c4c0283aSBenoit Parrot 	if (fse->index >= ARRAY_SIZE(ov2659_framesizes))
1008c4c0283aSBenoit Parrot 		return -EINVAL;
1009c4c0283aSBenoit Parrot 
1010c4c0283aSBenoit Parrot 	while (--i)
1011c4c0283aSBenoit Parrot 		if (fse->code == ov2659_formats[i].code)
1012c4c0283aSBenoit Parrot 			break;
1013c4c0283aSBenoit Parrot 
1014c4c0283aSBenoit Parrot 	fse->code = ov2659_formats[i].code;
1015c4c0283aSBenoit Parrot 
1016c4c0283aSBenoit Parrot 	fse->min_width  = ov2659_framesizes[fse->index].width;
1017c4c0283aSBenoit Parrot 	fse->max_width  = fse->min_width;
1018c4c0283aSBenoit Parrot 	fse->max_height = ov2659_framesizes[fse->index].height;
1019c4c0283aSBenoit Parrot 	fse->min_height = fse->max_height;
1020c4c0283aSBenoit Parrot 
1021c4c0283aSBenoit Parrot 	return 0;
1022c4c0283aSBenoit Parrot }
1023c4c0283aSBenoit Parrot 
ov2659_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1024c4c0283aSBenoit Parrot static int ov2659_get_fmt(struct v4l2_subdev *sd,
10250d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
1026c4c0283aSBenoit Parrot 			  struct v4l2_subdev_format *fmt)
1027c4c0283aSBenoit Parrot {
1028c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1029c4c0283aSBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
1030c4c0283aSBenoit Parrot 
1031c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "ov2659_get_fmt\n");
1032c4c0283aSBenoit Parrot 
1033c4c0283aSBenoit Parrot 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1034fa8cb644SMauro Carvalho Chehab #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1035fa8cb644SMauro Carvalho Chehab 		struct v4l2_mbus_framefmt *mf;
1036fa8cb644SMauro Carvalho Chehab 
10370d346d2aSTomi Valkeinen 		mf = v4l2_subdev_get_try_format(sd, sd_state, 0);
1038c4c0283aSBenoit Parrot 		mutex_lock(&ov2659->lock);
1039c4c0283aSBenoit Parrot 		fmt->format = *mf;
1040c4c0283aSBenoit Parrot 		mutex_unlock(&ov2659->lock);
1041c4c0283aSBenoit Parrot 		return 0;
1042fa8cb644SMauro Carvalho Chehab #else
10432dbcb6fbSHans Verkuil 		return -EINVAL;
1044fa8cb644SMauro Carvalho Chehab #endif
1045c4c0283aSBenoit Parrot 	}
1046c4c0283aSBenoit Parrot 
1047c4c0283aSBenoit Parrot 	mutex_lock(&ov2659->lock);
1048c4c0283aSBenoit Parrot 	fmt->format = ov2659->format;
1049c4c0283aSBenoit Parrot 	mutex_unlock(&ov2659->lock);
1050c4c0283aSBenoit Parrot 
1051c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "ov2659_get_fmt: %x %dx%d\n",
1052c4c0283aSBenoit Parrot 		ov2659->format.code, ov2659->format.width,
1053c4c0283aSBenoit Parrot 		ov2659->format.height);
1054c4c0283aSBenoit Parrot 
1055c4c0283aSBenoit Parrot 	return 0;
1056c4c0283aSBenoit Parrot }
1057c4c0283aSBenoit Parrot 
__ov2659_try_frame_size(struct v4l2_mbus_framefmt * mf,const struct ov2659_framesize ** size)1058c4c0283aSBenoit Parrot static void __ov2659_try_frame_size(struct v4l2_mbus_framefmt *mf,
1059c4c0283aSBenoit Parrot 				    const struct ov2659_framesize **size)
1060c4c0283aSBenoit Parrot {
1061c4c0283aSBenoit Parrot 	const struct ov2659_framesize *fsize = &ov2659_framesizes[0];
1062c4c0283aSBenoit Parrot 	const struct ov2659_framesize *match = NULL;
1063c4c0283aSBenoit Parrot 	int i = ARRAY_SIZE(ov2659_framesizes);
1064c4c0283aSBenoit Parrot 	unsigned int min_err = UINT_MAX;
1065c4c0283aSBenoit Parrot 
1066c4c0283aSBenoit Parrot 	while (i--) {
1067c4c0283aSBenoit Parrot 		int err = abs(fsize->width - mf->width)
1068c4c0283aSBenoit Parrot 				+ abs(fsize->height - mf->height);
1069c4c0283aSBenoit Parrot 		if ((err < min_err) && (fsize->regs[0].addr)) {
1070c4c0283aSBenoit Parrot 			min_err = err;
1071c4c0283aSBenoit Parrot 			match = fsize;
1072c4c0283aSBenoit Parrot 		}
1073c4c0283aSBenoit Parrot 		fsize++;
1074c4c0283aSBenoit Parrot 	}
1075c4c0283aSBenoit Parrot 
1076c4c0283aSBenoit Parrot 	if (!match)
1077c4c0283aSBenoit Parrot 		match = &ov2659_framesizes[2];
1078c4c0283aSBenoit Parrot 
1079c4c0283aSBenoit Parrot 	mf->width  = match->width;
1080c4c0283aSBenoit Parrot 	mf->height = match->height;
1081c4c0283aSBenoit Parrot 
1082c4c0283aSBenoit Parrot 	if (size)
1083c4c0283aSBenoit Parrot 		*size = match;
1084c4c0283aSBenoit Parrot }
1085c4c0283aSBenoit Parrot 
ov2659_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)1086c4c0283aSBenoit Parrot static int ov2659_set_fmt(struct v4l2_subdev *sd,
10870d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
1088c4c0283aSBenoit Parrot 			  struct v4l2_subdev_format *fmt)
1089c4c0283aSBenoit Parrot {
1090c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
10915f5859d1SDan Carpenter 	int index = ARRAY_SIZE(ov2659_formats);
1092c4c0283aSBenoit Parrot 	struct v4l2_mbus_framefmt *mf = &fmt->format;
1093c4c0283aSBenoit Parrot 	const struct ov2659_framesize *size = NULL;
1094c4c0283aSBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
1095c4c0283aSBenoit Parrot 	int ret = 0;
1096c4c0283aSBenoit Parrot 
1097c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "ov2659_set_fmt\n");
1098c4c0283aSBenoit Parrot 
1099c4c0283aSBenoit Parrot 	__ov2659_try_frame_size(mf, &size);
1100c4c0283aSBenoit Parrot 
1101c4c0283aSBenoit Parrot 	while (--index >= 0)
1102c4c0283aSBenoit Parrot 		if (ov2659_formats[index].code == mf->code)
1103c4c0283aSBenoit Parrot 			break;
1104c4c0283aSBenoit Parrot 
1105bccb89cfSAkinobu Mita 	if (index < 0) {
1106bccb89cfSAkinobu Mita 		index = 0;
1107bccb89cfSAkinobu Mita 		mf->code = ov2659_formats[index].code;
1108bccb89cfSAkinobu Mita 	}
1109c4c0283aSBenoit Parrot 
1110c4c0283aSBenoit Parrot 	mf->colorspace = V4L2_COLORSPACE_SRGB;
1111c4c0283aSBenoit Parrot 	mf->field = V4L2_FIELD_NONE;
1112c4c0283aSBenoit Parrot 
1113c4c0283aSBenoit Parrot 	mutex_lock(&ov2659->lock);
1114c4c0283aSBenoit Parrot 
1115c4c0283aSBenoit Parrot 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1116fa8cb644SMauro Carvalho Chehab #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
11170d346d2aSTomi Valkeinen 		mf = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
1118c4c0283aSBenoit Parrot 		*mf = fmt->format;
1119fa8cb644SMauro Carvalho Chehab #endif
1120c4c0283aSBenoit Parrot 	} else {
1121c4c0283aSBenoit Parrot 		s64 val;
1122c4c0283aSBenoit Parrot 
1123c4c0283aSBenoit Parrot 		if (ov2659->streaming) {
1124c4c0283aSBenoit Parrot 			mutex_unlock(&ov2659->lock);
1125c4c0283aSBenoit Parrot 			return -EBUSY;
1126c4c0283aSBenoit Parrot 		}
1127c4c0283aSBenoit Parrot 
1128c4c0283aSBenoit Parrot 		ov2659->frame_size = size;
1129c4c0283aSBenoit Parrot 		ov2659->format = fmt->format;
1130c4c0283aSBenoit Parrot 		ov2659->format_ctrl_regs =
1131c4c0283aSBenoit Parrot 			ov2659_formats[index].format_ctrl_regs;
1132c4c0283aSBenoit Parrot 
1133c4c0283aSBenoit Parrot 		if (ov2659->format.code != MEDIA_BUS_FMT_SBGGR8_1X8)
1134c4c0283aSBenoit Parrot 			val = ov2659->pdata->link_frequency / 2;
1135c4c0283aSBenoit Parrot 		else
1136c4c0283aSBenoit Parrot 			val = ov2659->pdata->link_frequency;
1137c4c0283aSBenoit Parrot 
1138c4c0283aSBenoit Parrot 		ret = v4l2_ctrl_s_ctrl_int64(ov2659->link_frequency, val);
1139c4c0283aSBenoit Parrot 		if (ret < 0)
1140c4c0283aSBenoit Parrot 			dev_warn(&client->dev,
1141c4c0283aSBenoit Parrot 				 "failed to set link_frequency rate (%d)\n",
1142c4c0283aSBenoit Parrot 				 ret);
1143c4c0283aSBenoit Parrot 	}
1144c4c0283aSBenoit Parrot 
1145c4c0283aSBenoit Parrot 	mutex_unlock(&ov2659->lock);
1146c4c0283aSBenoit Parrot 	return ret;
1147c4c0283aSBenoit Parrot }
1148c4c0283aSBenoit Parrot 
ov2659_set_frame_size(struct ov2659 * ov2659)1149c4c0283aSBenoit Parrot static int ov2659_set_frame_size(struct ov2659 *ov2659)
1150c4c0283aSBenoit Parrot {
1151c4c0283aSBenoit Parrot 	struct i2c_client *client = ov2659->client;
1152c4c0283aSBenoit Parrot 
1153c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s\n", __func__);
1154c4c0283aSBenoit Parrot 
1155c4c0283aSBenoit Parrot 	return ov2659_write_array(ov2659->client, ov2659->frame_size->regs);
1156c4c0283aSBenoit Parrot }
1157c4c0283aSBenoit Parrot 
ov2659_set_format(struct ov2659 * ov2659)1158c4c0283aSBenoit Parrot static int ov2659_set_format(struct ov2659 *ov2659)
1159c4c0283aSBenoit Parrot {
1160c4c0283aSBenoit Parrot 	struct i2c_client *client = ov2659->client;
1161c4c0283aSBenoit Parrot 
1162c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s\n", __func__);
1163c4c0283aSBenoit Parrot 
1164c4c0283aSBenoit Parrot 	return ov2659_write_array(ov2659->client, ov2659->format_ctrl_regs);
1165c4c0283aSBenoit Parrot }
1166c4c0283aSBenoit Parrot 
ov2659_s_stream(struct v4l2_subdev * sd,int on)1167c4c0283aSBenoit Parrot static int ov2659_s_stream(struct v4l2_subdev *sd, int on)
1168c4c0283aSBenoit Parrot {
1169c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1170c4c0283aSBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
1171c4c0283aSBenoit Parrot 	int ret = 0;
1172c4c0283aSBenoit Parrot 
1173c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
1174c4c0283aSBenoit Parrot 
1175c4c0283aSBenoit Parrot 	mutex_lock(&ov2659->lock);
1176c4c0283aSBenoit Parrot 
1177c4c0283aSBenoit Parrot 	on = !!on;
1178c4c0283aSBenoit Parrot 
1179c4c0283aSBenoit Parrot 	if (ov2659->streaming == on)
1180c4c0283aSBenoit Parrot 		goto unlock;
1181c4c0283aSBenoit Parrot 
1182c4c0283aSBenoit Parrot 	if (!on) {
1183c4c0283aSBenoit Parrot 		/* Stop Streaming Sequence */
1184c4c0283aSBenoit Parrot 		ov2659_set_streaming(ov2659, 0);
1185c4c0283aSBenoit Parrot 		ov2659->streaming = on;
11862b4a07a0SBenoit Parrot 		pm_runtime_put(&client->dev);
1187c4c0283aSBenoit Parrot 		goto unlock;
1188c4c0283aSBenoit Parrot 	}
1189c4c0283aSBenoit Parrot 
1190c12ede18SMauro Carvalho Chehab 	ret = pm_runtime_resume_and_get(&client->dev);
1191c12ede18SMauro Carvalho Chehab 	if (ret < 0)
11922b4a07a0SBenoit Parrot 		goto unlock;
11932b4a07a0SBenoit Parrot 
11942b4a07a0SBenoit Parrot 	ret = ov2659_init(sd, 0);
11952b4a07a0SBenoit Parrot 	if (!ret)
119685c4043fSBenoit Parrot 		ret = ov2659_set_pixel_clock(ov2659);
119785c4043fSBenoit Parrot 	if (!ret)
119885c4043fSBenoit Parrot 		ret = ov2659_set_frame_size(ov2659);
119985c4043fSBenoit Parrot 	if (!ret)
120085c4043fSBenoit Parrot 		ret = ov2659_set_format(ov2659);
120185c4043fSBenoit Parrot 	if (!ret) {
1202c4c0283aSBenoit Parrot 		ov2659_set_streaming(ov2659, 1);
1203c4c0283aSBenoit Parrot 		ov2659->streaming = on;
120485c4043fSBenoit Parrot 	}
1205c4c0283aSBenoit Parrot 
1206c4c0283aSBenoit Parrot unlock:
1207c4c0283aSBenoit Parrot 	mutex_unlock(&ov2659->lock);
1208c4c0283aSBenoit Parrot 	return ret;
1209c4c0283aSBenoit Parrot }
1210c4c0283aSBenoit Parrot 
ov2659_set_test_pattern(struct ov2659 * ov2659,int value)1211c4c0283aSBenoit Parrot static int ov2659_set_test_pattern(struct ov2659 *ov2659, int value)
1212c4c0283aSBenoit Parrot {
1213c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(&ov2659->sd);
1214c4c0283aSBenoit Parrot 	int ret;
1215c4c0283aSBenoit Parrot 	u8 val;
1216c4c0283aSBenoit Parrot 
1217c4c0283aSBenoit Parrot 	ret = ov2659_read(client, REG_PRE_ISP_CTRL00, &val);
1218c4c0283aSBenoit Parrot 	if (ret < 0)
1219c4c0283aSBenoit Parrot 		return ret;
1220c4c0283aSBenoit Parrot 
1221c4c0283aSBenoit Parrot 	switch (value) {
1222c4c0283aSBenoit Parrot 	case 0:
1223c4c0283aSBenoit Parrot 		val &= ~TEST_PATTERN_ENABLE;
1224c4c0283aSBenoit Parrot 		break;
1225c4c0283aSBenoit Parrot 	case 1:
1226c4c0283aSBenoit Parrot 		val &= VERTICAL_COLOR_BAR_MASK;
1227c4c0283aSBenoit Parrot 		val |= TEST_PATTERN_ENABLE;
1228c4c0283aSBenoit Parrot 		break;
1229c4c0283aSBenoit Parrot 	}
1230c4c0283aSBenoit Parrot 
1231c4c0283aSBenoit Parrot 	return ov2659_write(client, REG_PRE_ISP_CTRL00, val);
1232c4c0283aSBenoit Parrot }
1233c4c0283aSBenoit Parrot 
ov2659_s_ctrl(struct v4l2_ctrl * ctrl)1234c4c0283aSBenoit Parrot static int ov2659_s_ctrl(struct v4l2_ctrl *ctrl)
1235c4c0283aSBenoit Parrot {
1236c4c0283aSBenoit Parrot 	struct ov2659 *ov2659 =
1237c4c0283aSBenoit Parrot 			container_of(ctrl->handler, struct ov2659, ctrls);
12382b4a07a0SBenoit Parrot 	struct i2c_client *client = ov2659->client;
12392b4a07a0SBenoit Parrot 
12402b4a07a0SBenoit Parrot 	/* V4L2 controls values will be applied only when power is already up */
12412b4a07a0SBenoit Parrot 	if (!pm_runtime_get_if_in_use(&client->dev))
12422b4a07a0SBenoit Parrot 		return 0;
1243c4c0283aSBenoit Parrot 
1244c4c0283aSBenoit Parrot 	switch (ctrl->id) {
1245c4c0283aSBenoit Parrot 	case V4L2_CID_TEST_PATTERN:
1246c4c0283aSBenoit Parrot 		return ov2659_set_test_pattern(ov2659, ctrl->val);
1247c4c0283aSBenoit Parrot 	}
1248c4c0283aSBenoit Parrot 
12492b4a07a0SBenoit Parrot 	pm_runtime_put(&client->dev);
1250c4c0283aSBenoit Parrot 	return 0;
1251c4c0283aSBenoit Parrot }
1252c4c0283aSBenoit Parrot 
1253217bdb07SJulia Lawall static const struct v4l2_ctrl_ops ov2659_ctrl_ops = {
1254c4c0283aSBenoit Parrot 	.s_ctrl = ov2659_s_ctrl,
1255c4c0283aSBenoit Parrot };
1256c4c0283aSBenoit Parrot 
1257c4c0283aSBenoit Parrot static const char * const ov2659_test_pattern_menu[] = {
1258c4c0283aSBenoit Parrot 	"Disabled",
1259c4c0283aSBenoit Parrot 	"Vertical Color Bars",
1260c4c0283aSBenoit Parrot };
1261c4c0283aSBenoit Parrot 
ov2659_power_off(struct device * dev)12622b4a07a0SBenoit Parrot static int ov2659_power_off(struct device *dev)
12632b4a07a0SBenoit Parrot {
12642b4a07a0SBenoit Parrot 	struct i2c_client *client = to_i2c_client(dev);
12652b4a07a0SBenoit Parrot 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
12662b4a07a0SBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
12672b4a07a0SBenoit Parrot 
12682b4a07a0SBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
12692b4a07a0SBenoit Parrot 
12702b4a07a0SBenoit Parrot 	gpiod_set_value(ov2659->pwdn_gpio, 1);
12712b4a07a0SBenoit Parrot 
127224786ccdSDillon Min 	clk_disable_unprepare(ov2659->clk);
127324786ccdSDillon Min 
12742b4a07a0SBenoit Parrot 	return 0;
12752b4a07a0SBenoit Parrot }
12762b4a07a0SBenoit Parrot 
ov2659_power_on(struct device * dev)12772b4a07a0SBenoit Parrot static int ov2659_power_on(struct device *dev)
12782b4a07a0SBenoit Parrot {
12792b4a07a0SBenoit Parrot 	struct i2c_client *client = to_i2c_client(dev);
12802b4a07a0SBenoit Parrot 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
12812b4a07a0SBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
128224786ccdSDillon Min 	int ret;
12832b4a07a0SBenoit Parrot 
12842b4a07a0SBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
12852b4a07a0SBenoit Parrot 
128624786ccdSDillon Min 	ret = clk_prepare_enable(ov2659->clk);
128724786ccdSDillon Min 	if (ret) {
128824786ccdSDillon Min 		dev_err(&client->dev, "%s: failed to enable clock\n",
128924786ccdSDillon Min 			__func__);
129024786ccdSDillon Min 		return ret;
129124786ccdSDillon Min 	}
129224786ccdSDillon Min 
12932b4a07a0SBenoit Parrot 	gpiod_set_value(ov2659->pwdn_gpio, 0);
12942b4a07a0SBenoit Parrot 
12952b4a07a0SBenoit Parrot 	if (ov2659->resetb_gpio) {
12962b4a07a0SBenoit Parrot 		gpiod_set_value(ov2659->resetb_gpio, 1);
12972b4a07a0SBenoit Parrot 		usleep_range(500, 1000);
12982b4a07a0SBenoit Parrot 		gpiod_set_value(ov2659->resetb_gpio, 0);
12992b4a07a0SBenoit Parrot 		usleep_range(3000, 5000);
13002b4a07a0SBenoit Parrot 	}
13012b4a07a0SBenoit Parrot 
13022b4a07a0SBenoit Parrot 	return 0;
13032b4a07a0SBenoit Parrot }
13042b4a07a0SBenoit Parrot 
1305c4c0283aSBenoit Parrot /* -----------------------------------------------------------------------------
1306c4c0283aSBenoit Parrot  * V4L2 subdev internal operations
1307c4c0283aSBenoit Parrot  */
1308c4c0283aSBenoit Parrot 
1309fa8cb644SMauro Carvalho Chehab #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
ov2659_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1310c4c0283aSBenoit Parrot static int ov2659_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1311c4c0283aSBenoit Parrot {
1312c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1313c4c0283aSBenoit Parrot 	struct v4l2_mbus_framefmt *format =
13140d346d2aSTomi Valkeinen 				v4l2_subdev_get_try_format(sd, fh->state, 0);
1315c4c0283aSBenoit Parrot 
1316c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
1317c4c0283aSBenoit Parrot 
1318c4c0283aSBenoit Parrot 	ov2659_get_default_format(format);
1319c4c0283aSBenoit Parrot 
1320c4c0283aSBenoit Parrot 	return 0;
1321c4c0283aSBenoit Parrot }
1322fa8cb644SMauro Carvalho Chehab #endif
1323c4c0283aSBenoit Parrot 
1324c4c0283aSBenoit Parrot static const struct v4l2_subdev_core_ops ov2659_subdev_core_ops = {
1325c4c0283aSBenoit Parrot 	.log_status = v4l2_ctrl_subdev_log_status,
1326c4c0283aSBenoit Parrot 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1327c4c0283aSBenoit Parrot 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
1328c4c0283aSBenoit Parrot };
1329c4c0283aSBenoit Parrot 
1330c4c0283aSBenoit Parrot static const struct v4l2_subdev_video_ops ov2659_subdev_video_ops = {
1331c4c0283aSBenoit Parrot 	.s_stream = ov2659_s_stream,
1332c4c0283aSBenoit Parrot };
1333c4c0283aSBenoit Parrot 
1334c4c0283aSBenoit Parrot static const struct v4l2_subdev_pad_ops ov2659_subdev_pad_ops = {
1335c4c0283aSBenoit Parrot 	.enum_mbus_code = ov2659_enum_mbus_code,
1336c4c0283aSBenoit Parrot 	.enum_frame_size = ov2659_enum_frame_sizes,
1337c4c0283aSBenoit Parrot 	.get_fmt = ov2659_get_fmt,
1338c4c0283aSBenoit Parrot 	.set_fmt = ov2659_set_fmt,
1339c4c0283aSBenoit Parrot };
1340c4c0283aSBenoit Parrot 
1341fa8cb644SMauro Carvalho Chehab #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1342c4c0283aSBenoit Parrot static const struct v4l2_subdev_ops ov2659_subdev_ops = {
1343c4c0283aSBenoit Parrot 	.core  = &ov2659_subdev_core_ops,
1344c4c0283aSBenoit Parrot 	.video = &ov2659_subdev_video_ops,
1345c4c0283aSBenoit Parrot 	.pad   = &ov2659_subdev_pad_ops,
1346c4c0283aSBenoit Parrot };
1347c4c0283aSBenoit Parrot 
1348c4c0283aSBenoit Parrot static const struct v4l2_subdev_internal_ops ov2659_subdev_internal_ops = {
1349c4c0283aSBenoit Parrot 	.open = ov2659_open,
1350c4c0283aSBenoit Parrot };
1351fa8cb644SMauro Carvalho Chehab #endif
1352c4c0283aSBenoit Parrot 
ov2659_detect(struct v4l2_subdev * sd)1353c4c0283aSBenoit Parrot static int ov2659_detect(struct v4l2_subdev *sd)
1354c4c0283aSBenoit Parrot {
1355c4c0283aSBenoit Parrot 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1356e1082d28SGustavo A. R. Silva 	u8 pid = 0;
1357e1082d28SGustavo A. R. Silva 	u8 ver = 0;
1358c4c0283aSBenoit Parrot 	int ret;
1359c4c0283aSBenoit Parrot 
1360c4c0283aSBenoit Parrot 	dev_dbg(&client->dev, "%s:\n", __func__);
1361c4c0283aSBenoit Parrot 
1362c4c0283aSBenoit Parrot 	ret = ov2659_write(client, REG_SOFTWARE_RESET, 0x01);
1363c4c0283aSBenoit Parrot 	if (ret != 0) {
1364c4c0283aSBenoit Parrot 		dev_err(&client->dev, "Sensor soft reset failed\n");
1365c4c0283aSBenoit Parrot 		return -ENODEV;
1366c4c0283aSBenoit Parrot 	}
1367c4c0283aSBenoit Parrot 	usleep_range(1000, 2000);
1368c4c0283aSBenoit Parrot 
1369c4c0283aSBenoit Parrot 	/* Check sensor revision */
1370c4c0283aSBenoit Parrot 	ret = ov2659_read(client, REG_SC_CHIP_ID_H, &pid);
1371c4c0283aSBenoit Parrot 	if (!ret)
1372c4c0283aSBenoit Parrot 		ret = ov2659_read(client, REG_SC_CHIP_ID_L, &ver);
1373c4c0283aSBenoit Parrot 
1374c4c0283aSBenoit Parrot 	if (!ret) {
1375c4c0283aSBenoit Parrot 		unsigned short id;
1376c4c0283aSBenoit Parrot 
1377c4c0283aSBenoit Parrot 		id = OV265X_ID(pid, ver);
13781bc06fdbSBenoit Parrot 		if (id != OV2659_ID) {
1379c4c0283aSBenoit Parrot 			dev_err(&client->dev,
138047926106SChristophe JAILLET 				"Sensor detection failed (%04X)\n", id);
13811bc06fdbSBenoit Parrot 			ret = -ENODEV;
13821bc06fdbSBenoit Parrot 		} else {
1383c4c0283aSBenoit Parrot 			dev_info(&client->dev, "Found OV%04X sensor\n", id);
13842e490139SGrygorii Strashko 		}
1385c4c0283aSBenoit Parrot 	}
1386c4c0283aSBenoit Parrot 
1387c4c0283aSBenoit Parrot 	return ret;
1388c4c0283aSBenoit Parrot }
1389c4c0283aSBenoit Parrot 
1390c4c0283aSBenoit Parrot static struct ov2659_platform_data *
ov2659_get_pdata(struct i2c_client * client)1391c4c0283aSBenoit Parrot ov2659_get_pdata(struct i2c_client *client)
1392c4c0283aSBenoit Parrot {
1393c4c0283aSBenoit Parrot 	struct ov2659_platform_data *pdata;
13946970d37cSSakari Ailus 	struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
1395c4c0283aSBenoit Parrot 	struct device_node *endpoint;
13966970d37cSSakari Ailus 	int ret;
1397c4c0283aSBenoit Parrot 
1398c4c0283aSBenoit Parrot 	if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
1399c4c0283aSBenoit Parrot 		return client->dev.platform_data;
1400c4c0283aSBenoit Parrot 
1401c4c0283aSBenoit Parrot 	endpoint = of_graph_get_next_endpoint(client->dev.of_node, NULL);
1402c4c0283aSBenoit Parrot 	if (!endpoint)
1403c4c0283aSBenoit Parrot 		return NULL;
1404c4c0283aSBenoit Parrot 
14056970d37cSSakari Ailus 	ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(endpoint),
14066970d37cSSakari Ailus 					       &bus_cfg);
14076970d37cSSakari Ailus 	if (ret) {
1408b3ab190fSLad, Prabhakar 		pdata = NULL;
1409b3ab190fSLad, Prabhakar 		goto done;
1410b3ab190fSLad, Prabhakar 	}
1411b3ab190fSLad, Prabhakar 
1412c4c0283aSBenoit Parrot 	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1413c4c0283aSBenoit Parrot 	if (!pdata)
1414c4c0283aSBenoit Parrot 		goto done;
1415c4c0283aSBenoit Parrot 
14166970d37cSSakari Ailus 	if (!bus_cfg.nr_of_link_frequencies) {
1417b3ab190fSLad, Prabhakar 		dev_err(&client->dev,
1418b3ab190fSLad, Prabhakar 			"link-frequencies property not found or too many\n");
1419c4c0283aSBenoit Parrot 		pdata = NULL;
1420b3ab190fSLad, Prabhakar 		goto done;
1421c4c0283aSBenoit Parrot 	}
1422c4c0283aSBenoit Parrot 
14236970d37cSSakari Ailus 	pdata->link_frequency = bus_cfg.link_frequencies[0];
1424b3ab190fSLad, Prabhakar 
1425c4c0283aSBenoit Parrot done:
14266970d37cSSakari Ailus 	v4l2_fwnode_endpoint_free(&bus_cfg);
1427c4c0283aSBenoit Parrot 	of_node_put(endpoint);
1428c4c0283aSBenoit Parrot 	return pdata;
1429c4c0283aSBenoit Parrot }
1430c4c0283aSBenoit Parrot 
ov2659_probe(struct i2c_client * client)1431e6714993SKieran Bingham static int ov2659_probe(struct i2c_client *client)
1432c4c0283aSBenoit Parrot {
1433c4c0283aSBenoit Parrot 	const struct ov2659_platform_data *pdata = ov2659_get_pdata(client);
1434c4c0283aSBenoit Parrot 	struct v4l2_subdev *sd;
1435c4c0283aSBenoit Parrot 	struct ov2659 *ov2659;
1436c4c0283aSBenoit Parrot 	int ret;
1437c4c0283aSBenoit Parrot 
1438c4c0283aSBenoit Parrot 	if (!pdata) {
1439c4c0283aSBenoit Parrot 		dev_err(&client->dev, "platform data not specified\n");
1440c4c0283aSBenoit Parrot 		return -EINVAL;
1441c4c0283aSBenoit Parrot 	}
1442c4c0283aSBenoit Parrot 
1443c4c0283aSBenoit Parrot 	ov2659 = devm_kzalloc(&client->dev, sizeof(*ov2659), GFP_KERNEL);
1444c4c0283aSBenoit Parrot 	if (!ov2659)
1445c4c0283aSBenoit Parrot 		return -ENOMEM;
1446c4c0283aSBenoit Parrot 
1447c4c0283aSBenoit Parrot 	ov2659->pdata = pdata;
1448c4c0283aSBenoit Parrot 	ov2659->client = client;
1449c4c0283aSBenoit Parrot 
145024786ccdSDillon Min 	ov2659->clk = devm_clk_get(&client->dev, "xvclk");
145124786ccdSDillon Min 	if (IS_ERR(ov2659->clk))
145224786ccdSDillon Min 		return PTR_ERR(ov2659->clk);
1453c4c0283aSBenoit Parrot 
145424786ccdSDillon Min 	ov2659->xvclk_frequency = clk_get_rate(ov2659->clk);
1455c4c0283aSBenoit Parrot 	if (ov2659->xvclk_frequency < 6000000 ||
1456c4c0283aSBenoit Parrot 	    ov2659->xvclk_frequency > 27000000)
1457c4c0283aSBenoit Parrot 		return -EINVAL;
1458c4c0283aSBenoit Parrot 
14592b4a07a0SBenoit Parrot 	/* Optional gpio don't fail if not present */
14602b4a07a0SBenoit Parrot 	ov2659->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
14612b4a07a0SBenoit Parrot 						    GPIOD_OUT_LOW);
14622b4a07a0SBenoit Parrot 	if (IS_ERR(ov2659->pwdn_gpio))
14632b4a07a0SBenoit Parrot 		return PTR_ERR(ov2659->pwdn_gpio);
14642b4a07a0SBenoit Parrot 
14652b4a07a0SBenoit Parrot 	/* Optional gpio don't fail if not present */
14662b4a07a0SBenoit Parrot 	ov2659->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
14672b4a07a0SBenoit Parrot 						      GPIOD_OUT_HIGH);
14682b4a07a0SBenoit Parrot 	if (IS_ERR(ov2659->resetb_gpio))
14692b4a07a0SBenoit Parrot 		return PTR_ERR(ov2659->resetb_gpio);
14702b4a07a0SBenoit Parrot 
1471c4c0283aSBenoit Parrot 	v4l2_ctrl_handler_init(&ov2659->ctrls, 2);
1472c4c0283aSBenoit Parrot 	ov2659->link_frequency =
1473c4c0283aSBenoit Parrot 			v4l2_ctrl_new_std(&ov2659->ctrls, &ov2659_ctrl_ops,
1474c4c0283aSBenoit Parrot 					  V4L2_CID_PIXEL_RATE,
1475c4c0283aSBenoit Parrot 					  pdata->link_frequency / 2,
1476c4c0283aSBenoit Parrot 					  pdata->link_frequency, 1,
1477c4c0283aSBenoit Parrot 					  pdata->link_frequency);
1478c4c0283aSBenoit Parrot 	v4l2_ctrl_new_std_menu_items(&ov2659->ctrls, &ov2659_ctrl_ops,
1479c4c0283aSBenoit Parrot 				     V4L2_CID_TEST_PATTERN,
1480c4c0283aSBenoit Parrot 				     ARRAY_SIZE(ov2659_test_pattern_menu) - 1,
1481c4c0283aSBenoit Parrot 				     0, 0, ov2659_test_pattern_menu);
1482c4c0283aSBenoit Parrot 	ov2659->sd.ctrl_handler = &ov2659->ctrls;
1483c4c0283aSBenoit Parrot 
1484c4c0283aSBenoit Parrot 	if (ov2659->ctrls.error) {
1485c4c0283aSBenoit Parrot 		dev_err(&client->dev, "%s: control initialization error %d\n",
1486c4c0283aSBenoit Parrot 			__func__, ov2659->ctrls.error);
1487c4c0283aSBenoit Parrot 		return  ov2659->ctrls.error;
1488c4c0283aSBenoit Parrot 	}
1489c4c0283aSBenoit Parrot 
1490c4c0283aSBenoit Parrot 	sd = &ov2659->sd;
1491c4c0283aSBenoit Parrot 	client->flags |= I2C_CLIENT_SCCB;
1492fa8cb644SMauro Carvalho Chehab #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1493c4c0283aSBenoit Parrot 	v4l2_i2c_subdev_init(sd, client, &ov2659_subdev_ops);
1494c4c0283aSBenoit Parrot 
1495c4c0283aSBenoit Parrot 	sd->internal_ops = &ov2659_subdev_internal_ops;
1496c4c0283aSBenoit Parrot 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1497c4c0283aSBenoit Parrot 		     V4L2_SUBDEV_FL_HAS_EVENTS;
1498fa8cb644SMauro Carvalho Chehab #endif
1499c4c0283aSBenoit Parrot 
1500c4c0283aSBenoit Parrot #if defined(CONFIG_MEDIA_CONTROLLER)
1501c4c0283aSBenoit Parrot 	ov2659->pad.flags = MEDIA_PAD_FL_SOURCE;
15024ca72efaSMauro Carvalho Chehab 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1503ab22e77cSMauro Carvalho Chehab 	ret = media_entity_pads_init(&sd->entity, 1, &ov2659->pad);
1504c4c0283aSBenoit Parrot 	if (ret < 0) {
1505c4c0283aSBenoit Parrot 		v4l2_ctrl_handler_free(&ov2659->ctrls);
1506c4c0283aSBenoit Parrot 		return ret;
1507c4c0283aSBenoit Parrot 	}
1508c4c0283aSBenoit Parrot #endif
1509c4c0283aSBenoit Parrot 
1510c4c0283aSBenoit Parrot 	mutex_init(&ov2659->lock);
1511c4c0283aSBenoit Parrot 
1512c4c0283aSBenoit Parrot 	ov2659_get_default_format(&ov2659->format);
1513c4c0283aSBenoit Parrot 	ov2659->frame_size = &ov2659_framesizes[2];
1514c4c0283aSBenoit Parrot 	ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs;
1515c4c0283aSBenoit Parrot 
151624786ccdSDillon Min 	ret = ov2659_power_on(&client->dev);
151724786ccdSDillon Min 	if (ret < 0)
151824786ccdSDillon Min 		goto error;
15192b4a07a0SBenoit Parrot 
1520c4c0283aSBenoit Parrot 	ret = ov2659_detect(sd);
1521c4c0283aSBenoit Parrot 	if (ret < 0)
1522c4c0283aSBenoit Parrot 		goto error;
1523c4c0283aSBenoit Parrot 
1524c4c0283aSBenoit Parrot 	/* Calculate the PLL register value needed */
1525c4c0283aSBenoit Parrot 	ov2659_pll_calc_params(ov2659);
1526c4c0283aSBenoit Parrot 
1527c4c0283aSBenoit Parrot 	ret = v4l2_async_register_subdev(&ov2659->sd);
1528c4c0283aSBenoit Parrot 	if (ret)
1529c4c0283aSBenoit Parrot 		goto error;
1530c4c0283aSBenoit Parrot 
1531c4c0283aSBenoit Parrot 	dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
1532c4c0283aSBenoit Parrot 
15332b4a07a0SBenoit Parrot 	pm_runtime_set_active(&client->dev);
15342b4a07a0SBenoit Parrot 	pm_runtime_enable(&client->dev);
15352b4a07a0SBenoit Parrot 	pm_runtime_idle(&client->dev);
15362b4a07a0SBenoit Parrot 
1537c4c0283aSBenoit Parrot 	return 0;
1538c4c0283aSBenoit Parrot 
1539c4c0283aSBenoit Parrot error:
1540c4c0283aSBenoit Parrot 	v4l2_ctrl_handler_free(&ov2659->ctrls);
15412b4a07a0SBenoit Parrot 	ov2659_power_off(&client->dev);
1542c4c0283aSBenoit Parrot 	media_entity_cleanup(&sd->entity);
1543c4c0283aSBenoit Parrot 	mutex_destroy(&ov2659->lock);
1544c4c0283aSBenoit Parrot 	return ret;
1545c4c0283aSBenoit Parrot }
1546c4c0283aSBenoit Parrot 
ov2659_remove(struct i2c_client * client)1547ed5c2f5fSUwe Kleine-König static void ov2659_remove(struct i2c_client *client)
1548c4c0283aSBenoit Parrot {
1549c4c0283aSBenoit Parrot 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1550c4c0283aSBenoit Parrot 	struct ov2659 *ov2659 = to_ov2659(sd);
1551c4c0283aSBenoit Parrot 
1552c4c0283aSBenoit Parrot 	v4l2_ctrl_handler_free(&ov2659->ctrls);
1553c4c0283aSBenoit Parrot 	v4l2_async_unregister_subdev(sd);
1554c4c0283aSBenoit Parrot 	media_entity_cleanup(&sd->entity);
1555c4c0283aSBenoit Parrot 	mutex_destroy(&ov2659->lock);
1556c4c0283aSBenoit Parrot 
15572b4a07a0SBenoit Parrot 	pm_runtime_disable(&client->dev);
15582b4a07a0SBenoit Parrot 	if (!pm_runtime_status_suspended(&client->dev))
15592b4a07a0SBenoit Parrot 		ov2659_power_off(&client->dev);
15602b4a07a0SBenoit Parrot 	pm_runtime_set_suspended(&client->dev);
1561c4c0283aSBenoit Parrot }
1562c4c0283aSBenoit Parrot 
15632b4a07a0SBenoit Parrot static const struct dev_pm_ops ov2659_pm_ops = {
15642b4a07a0SBenoit Parrot 	SET_RUNTIME_PM_OPS(ov2659_power_off, ov2659_power_on, NULL)
15652b4a07a0SBenoit Parrot };
15662b4a07a0SBenoit Parrot 
1567c4c0283aSBenoit Parrot static const struct i2c_device_id ov2659_id[] = {
1568c4c0283aSBenoit Parrot 	{ "ov2659", 0 },
1569c4c0283aSBenoit Parrot 	{ /* sentinel */ },
1570c4c0283aSBenoit Parrot };
1571c4c0283aSBenoit Parrot MODULE_DEVICE_TABLE(i2c, ov2659_id);
1572c4c0283aSBenoit Parrot 
1573c4c0283aSBenoit Parrot #if IS_ENABLED(CONFIG_OF)
1574c4c0283aSBenoit Parrot static const struct of_device_id ov2659_of_match[] = {
1575c4c0283aSBenoit Parrot 	{ .compatible = "ovti,ov2659", },
1576c4c0283aSBenoit Parrot 	{ /* sentinel */ },
1577c4c0283aSBenoit Parrot };
1578c4c0283aSBenoit Parrot MODULE_DEVICE_TABLE(of, ov2659_of_match);
1579c4c0283aSBenoit Parrot #endif
1580c4c0283aSBenoit Parrot 
1581c4c0283aSBenoit Parrot static struct i2c_driver ov2659_i2c_driver = {
1582c4c0283aSBenoit Parrot 	.driver = {
1583c4c0283aSBenoit Parrot 		.name	= DRIVER_NAME,
15842b4a07a0SBenoit Parrot 		.pm	= &ov2659_pm_ops,
1585c4c0283aSBenoit Parrot 		.of_match_table = of_match_ptr(ov2659_of_match),
1586c4c0283aSBenoit Parrot 	},
1587*aaeb31c0SUwe Kleine-König 	.probe		= ov2659_probe,
1588c4c0283aSBenoit Parrot 	.remove		= ov2659_remove,
1589c4c0283aSBenoit Parrot 	.id_table	= ov2659_id,
1590c4c0283aSBenoit Parrot };
1591c4c0283aSBenoit Parrot 
1592c4c0283aSBenoit Parrot module_i2c_driver(ov2659_i2c_driver);
1593c4c0283aSBenoit Parrot 
1594c4c0283aSBenoit Parrot MODULE_AUTHOR("Benoit Parrot <bparrot@ti.com>");
1595c4c0283aSBenoit Parrot MODULE_DESCRIPTION("OV2659 CMOS Image Sensor driver");
1596c4c0283aSBenoit Parrot MODULE_LICENSE("GPL v2");
1597