xref: /linux/drivers/media/i2c/ov7251.c (revision 9e1d3012cc10e53fe0ca389d0d22d3ed68e8d63a)
1d30bb512STodor Tomov // SPDX-License-Identifier: GPL-2.0
2d30bb512STodor Tomov /*
3d30bb512STodor Tomov  * Driver for the OV7251 camera sensor.
4d30bb512STodor Tomov  *
5d30bb512STodor Tomov  * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
6d30bb512STodor Tomov  * Copyright (c) 2017-2018, Linaro Ltd.
7d30bb512STodor Tomov  */
8d30bb512STodor Tomov 
9d30bb512STodor Tomov #include <linux/bitops.h>
10d30bb512STodor Tomov #include <linux/clk.h>
11d30bb512STodor Tomov #include <linux/delay.h>
12d30bb512STodor Tomov #include <linux/device.h>
13d30bb512STodor Tomov #include <linux/gpio/consumer.h>
14d30bb512STodor Tomov #include <linux/i2c.h>
15d30bb512STodor Tomov #include <linux/init.h>
16d30bb512STodor Tomov #include <linux/module.h>
176766cff6SDaniel Scally #include <linux/mod_devicetable.h>
18207f4162SDaniel Scally #include <linux/pm_runtime.h>
19d30bb512STodor Tomov #include <linux/regulator/consumer.h>
20d30bb512STodor Tomov #include <linux/slab.h>
21d30bb512STodor Tomov #include <linux/types.h>
22d30bb512STodor Tomov #include <media/v4l2-ctrls.h>
23d30bb512STodor Tomov #include <media/v4l2-fwnode.h>
24d30bb512STodor Tomov #include <media/v4l2-subdev.h>
25d30bb512STodor Tomov 
26d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT		0x0100
27d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_SW_STANDBY	0x0
28d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_STREAMING		0x1
29d30bb512STodor Tomov 
30d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH		0x300a
31d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH_BYTE	0x77
32d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW		0x300b
33d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW_BYTE		0x50
34d30bb512STodor Tomov #define OV7251_SC_GP_IO_IN1		0x3029
35d30bb512STodor Tomov #define OV7251_AEC_EXPO_0		0x3500
36d30bb512STodor Tomov #define OV7251_AEC_EXPO_1		0x3501
37d30bb512STodor Tomov #define OV7251_AEC_EXPO_2		0x3502
38d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_0		0x350a
39d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_1		0x350b
40d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1		0x3820
41d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1_VFLIP	BIT(2)
42d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2		0x3821
43d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2_MIRROR	BIT(2)
44d30bb512STodor Tomov #define OV7251_PRE_ISP_00		0x5e00
45d30bb512STodor Tomov #define OV7251_PRE_ISP_00_TEST_PATTERN	BIT(7)
46df057b0dSDaniel Scally #define OV7251_PLL1_PRE_DIV_REG		0x30b4
47df057b0dSDaniel Scally #define OV7251_PLL1_MULT_REG		0x30b3
48df057b0dSDaniel Scally #define OV7251_PLL1_DIVIDER_REG		0x30b1
49df057b0dSDaniel Scally #define OV7251_PLL1_PIX_DIV_REG		0x30b0
50df057b0dSDaniel Scally #define OV7251_PLL1_MIPI_DIV_REG	0x30b5
51df057b0dSDaniel Scally #define OV7251_PLL2_PRE_DIV_REG		0x3098
52df057b0dSDaniel Scally #define OV7251_PLL2_MULT_REG		0x3099
53df057b0dSDaniel Scally #define OV7251_PLL2_DIVIDER_REG		0x309d
54df057b0dSDaniel Scally #define OV7251_PLL2_SYS_DIV_REG		0x309a
55df057b0dSDaniel Scally #define OV7251_PLL2_ADC_DIV_REG		0x309b
56d30bb512STodor Tomov 
57d30bb512STodor Tomov struct reg_value {
58d30bb512STodor Tomov 	u16 reg;
59d30bb512STodor Tomov 	u8 val;
60d30bb512STodor Tomov };
61d30bb512STodor Tomov 
62d30bb512STodor Tomov struct ov7251_mode_info {
63d30bb512STodor Tomov 	u32 width;
64d30bb512STodor Tomov 	u32 height;
65d30bb512STodor Tomov 	const struct reg_value *data;
66d30bb512STodor Tomov 	u32 data_size;
67d30bb512STodor Tomov 	u32 pixel_clock;
68d30bb512STodor Tomov 	u32 link_freq;
69d30bb512STodor Tomov 	u16 exposure_max;
70d30bb512STodor Tomov 	u16 exposure_def;
71d30bb512STodor Tomov 	struct v4l2_fract timeperframe;
72d30bb512STodor Tomov };
73d30bb512STodor Tomov 
74df057b0dSDaniel Scally struct ov7251_pll1_cfg {
75df057b0dSDaniel Scally 	unsigned int pre_div;
76df057b0dSDaniel Scally 	unsigned int mult;
77df057b0dSDaniel Scally 	unsigned int div;
78df057b0dSDaniel Scally 	unsigned int pix_div;
79df057b0dSDaniel Scally 	unsigned int mipi_div;
80df057b0dSDaniel Scally };
81df057b0dSDaniel Scally 
82df057b0dSDaniel Scally struct ov7251_pll2_cfg {
83df057b0dSDaniel Scally 	unsigned int pre_div;
84df057b0dSDaniel Scally 	unsigned int mult;
85df057b0dSDaniel Scally 	unsigned int div;
86df057b0dSDaniel Scally 	unsigned int sys_div;
87df057b0dSDaniel Scally 	unsigned int adc_div;
88df057b0dSDaniel Scally };
89df057b0dSDaniel Scally 
90df057b0dSDaniel Scally /*
91df057b0dSDaniel Scally  * Rubbish ordering, but only PLL1 needs to have a separate configuration per
92df057b0dSDaniel Scally  * link frequency and the array member needs to be last.
93df057b0dSDaniel Scally  */
94df057b0dSDaniel Scally struct ov7251_pll_cfgs {
95df057b0dSDaniel Scally 	const struct ov7251_pll2_cfg *pll2;
96df057b0dSDaniel Scally 	const struct ov7251_pll1_cfg *pll1[];
97df057b0dSDaniel Scally };
98df057b0dSDaniel Scally 
99df057b0dSDaniel Scally enum xclk_rate {
100ed9566ceSDaniel Scally 	OV7251_19_2_MHZ,
101df057b0dSDaniel Scally 	OV7251_24_MHZ,
102df057b0dSDaniel Scally 	OV7251_NUM_SUPPORTED_RATES
103df057b0dSDaniel Scally };
104df057b0dSDaniel Scally 
105cc125aaaSDaniel Scally enum supported_link_freqs {
106cc125aaaSDaniel Scally 	OV7251_LINK_FREQ_240_MHZ,
107ed9566ceSDaniel Scally 	OV7251_LINK_FREQ_319_2_MHZ,
108cc125aaaSDaniel Scally 	OV7251_NUM_SUPPORTED_LINK_FREQS
109cc125aaaSDaniel Scally };
110cc125aaaSDaniel Scally 
111d30bb512STodor Tomov struct ov7251 {
112d30bb512STodor Tomov 	struct i2c_client *i2c_client;
113d30bb512STodor Tomov 	struct device *dev;
114d30bb512STodor Tomov 	struct v4l2_subdev sd;
115d30bb512STodor Tomov 	struct media_pad pad;
116d30bb512STodor Tomov 	struct v4l2_fwnode_endpoint ep;
117d30bb512STodor Tomov 	struct v4l2_mbus_framefmt fmt;
118d30bb512STodor Tomov 	struct v4l2_rect crop;
119d30bb512STodor Tomov 	struct clk *xclk;
120d30bb512STodor Tomov 	u32 xclk_freq;
121d30bb512STodor Tomov 
122d30bb512STodor Tomov 	struct regulator *io_regulator;
123d30bb512STodor Tomov 	struct regulator *core_regulator;
124d30bb512STodor Tomov 	struct regulator *analog_regulator;
125d30bb512STodor Tomov 
126df057b0dSDaniel Scally 	const struct ov7251_pll_cfgs *pll_cfgs;
127cc125aaaSDaniel Scally 	enum supported_link_freqs link_freq_idx;
128d30bb512STodor Tomov 	const struct ov7251_mode_info *current_mode;
129d30bb512STodor Tomov 
130d30bb512STodor Tomov 	struct v4l2_ctrl_handler ctrls;
131d30bb512STodor Tomov 	struct v4l2_ctrl *pixel_clock;
132d30bb512STodor Tomov 	struct v4l2_ctrl *link_freq;
133d30bb512STodor Tomov 	struct v4l2_ctrl *exposure;
134d30bb512STodor Tomov 	struct v4l2_ctrl *gain;
135d30bb512STodor Tomov 
136d30bb512STodor Tomov 	/* Cached register values */
137d30bb512STodor Tomov 	u8 aec_pk_manual;
138d30bb512STodor Tomov 	u8 pre_isp_00;
139d30bb512STodor Tomov 	u8 timing_format1;
140d30bb512STodor Tomov 	u8 timing_format2;
141d30bb512STodor Tomov 
142d30bb512STodor Tomov 	struct mutex lock; /* lock to protect power state, ctrls and mode */
143d30bb512STodor Tomov 	bool power_on;
144d30bb512STodor Tomov 
145d30bb512STodor Tomov 	struct gpio_desc *enable_gpio;
146d30bb512STodor Tomov };
147d30bb512STodor Tomov 
148d30bb512STodor Tomov static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd)
149d30bb512STodor Tomov {
150d30bb512STodor Tomov 	return container_of(sd, struct ov7251, sd);
151d30bb512STodor Tomov }
152d30bb512STodor Tomov 
153ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_240_mhz = {
154ed9566ceSDaniel Scally 	.pre_div = 0x03,
155ed9566ceSDaniel Scally 	.mult = 0x4b,
156ed9566ceSDaniel Scally 	.div = 0x01,
157ed9566ceSDaniel Scally 	.pix_div = 0x0a,
158ed9566ceSDaniel Scally 	.mipi_div = 0x05,
159ed9566ceSDaniel Scally };
160ed9566ceSDaniel Scally 
161ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_19_2_mhz_319_2_mhz = {
162ed9566ceSDaniel Scally 	.pre_div = 0x01,
163ed9566ceSDaniel Scally 	.mult = 0x85,
164ed9566ceSDaniel Scally 	.div = 0x04,
165ed9566ceSDaniel Scally 	.pix_div = 0x0a,
166ed9566ceSDaniel Scally 	.mipi_div = 0x05,
167ed9566ceSDaniel Scally };
168ed9566ceSDaniel Scally 
169df057b0dSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_240_mhz = {
170df057b0dSDaniel Scally 	.pre_div = 0x03,
171df057b0dSDaniel Scally 	.mult = 0x64,
172df057b0dSDaniel Scally 	.div = 0x01,
173df057b0dSDaniel Scally 	.pix_div = 0x0a,
174df057b0dSDaniel Scally 	.mipi_div = 0x05,
175df057b0dSDaniel Scally };
176df057b0dSDaniel Scally 
177ed9566ceSDaniel Scally static const struct ov7251_pll1_cfg ov7251_pll1_cfg_24_mhz_319_2_mhz = {
178ed9566ceSDaniel Scally 	.pre_div = 0x05,
179ed9566ceSDaniel Scally 	.mult = 0x85,
180ed9566ceSDaniel Scally 	.div = 0x02,
181ed9566ceSDaniel Scally 	.pix_div = 0x0a,
182ed9566ceSDaniel Scally 	.mipi_div = 0x05,
183ed9566ceSDaniel Scally };
184ed9566ceSDaniel Scally 
185ed9566ceSDaniel Scally static const struct ov7251_pll2_cfg ov7251_pll2_cfg_19_2_mhz = {
186ed9566ceSDaniel Scally 	.pre_div = 0x04,
187ed9566ceSDaniel Scally 	.mult = 0x32,
188ed9566ceSDaniel Scally 	.div = 0x00,
189ed9566ceSDaniel Scally 	.sys_div = 0x05,
190ed9566ceSDaniel Scally 	.adc_div = 0x04,
191ed9566ceSDaniel Scally };
192ed9566ceSDaniel Scally 
193df057b0dSDaniel Scally static const struct ov7251_pll2_cfg ov7251_pll2_cfg_24_mhz = {
194df057b0dSDaniel Scally 	.pre_div = 0x04,
195df057b0dSDaniel Scally 	.mult = 0x28,
196df057b0dSDaniel Scally 	.div = 0x00,
197df057b0dSDaniel Scally 	.sys_div = 0x05,
198df057b0dSDaniel Scally 	.adc_div = 0x04,
199df057b0dSDaniel Scally };
200df057b0dSDaniel Scally 
201ed9566ceSDaniel Scally static const struct ov7251_pll_cfgs ov7251_pll_cfgs_19_2_mhz = {
202ed9566ceSDaniel Scally 	.pll2 = &ov7251_pll2_cfg_19_2_mhz,
203ed9566ceSDaniel Scally 	.pll1 = {
204ed9566ceSDaniel Scally 		[OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_19_2_mhz_240_mhz,
205ed9566ceSDaniel Scally 		[OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_19_2_mhz_319_2_mhz,
206ed9566ceSDaniel Scally 	},
207ed9566ceSDaniel Scally };
208ed9566ceSDaniel Scally 
209df057b0dSDaniel Scally static const struct ov7251_pll_cfgs ov7251_pll_cfgs_24_mhz = {
210df057b0dSDaniel Scally 	.pll2 = &ov7251_pll2_cfg_24_mhz,
211df057b0dSDaniel Scally 	.pll1 = {
212df057b0dSDaniel Scally 		[OV7251_LINK_FREQ_240_MHZ] = &ov7251_pll1_cfg_24_mhz_240_mhz,
213ed9566ceSDaniel Scally 		[OV7251_LINK_FREQ_319_2_MHZ] = &ov7251_pll1_cfg_24_mhz_319_2_mhz,
214df057b0dSDaniel Scally 	},
215df057b0dSDaniel Scally };
216df057b0dSDaniel Scally 
217df057b0dSDaniel Scally static const struct ov7251_pll_cfgs *ov7251_pll_cfgs[] = {
218ed9566ceSDaniel Scally 	[OV7251_19_2_MHZ] = &ov7251_pll_cfgs_19_2_mhz,
219df057b0dSDaniel Scally 	[OV7251_24_MHZ] = &ov7251_pll_cfgs_24_mhz,
220df057b0dSDaniel Scally };
221df057b0dSDaniel Scally 
222d30bb512STodor Tomov static const struct reg_value ov7251_global_init_setting[] = {
223d30bb512STodor Tomov 	{ 0x0103, 0x01 },
224d30bb512STodor Tomov 	{ 0x303b, 0x02 },
225d30bb512STodor Tomov };
226d30bb512STodor Tomov 
227d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_30fps[] = {
228d30bb512STodor Tomov 	{ 0x3005, 0x00 },
229d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
230d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
231d30bb512STodor Tomov 	{ 0x3014, 0x04 },
232d30bb512STodor Tomov 	{ 0x3016, 0xf0 },
233d30bb512STodor Tomov 	{ 0x3017, 0xf0 },
234d30bb512STodor Tomov 	{ 0x3018, 0xf0 },
235d30bb512STodor Tomov 	{ 0x301a, 0xf0 },
236d30bb512STodor Tomov 	{ 0x301b, 0xf0 },
237d30bb512STodor Tomov 	{ 0x301c, 0xf0 },
238d30bb512STodor Tomov 	{ 0x3023, 0x05 },
239d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
240d30bb512STodor Tomov 	{ 0x3106, 0xda },
241d30bb512STodor Tomov 	{ 0x3503, 0x07 },
242d30bb512STodor Tomov 	{ 0x3509, 0x10 },
243d30bb512STodor Tomov 	{ 0x3600, 0x1c },
244d30bb512STodor Tomov 	{ 0x3602, 0x62 },
245d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
246d30bb512STodor Tomov 	{ 0x3622, 0x04 },
247d30bb512STodor Tomov 	{ 0x3626, 0x21 },
248d30bb512STodor Tomov 	{ 0x3627, 0x30 },
249d30bb512STodor Tomov 	{ 0x3630, 0x44 },
250d30bb512STodor Tomov 	{ 0x3631, 0x35 },
251d30bb512STodor Tomov 	{ 0x3634, 0x60 },
252d30bb512STodor Tomov 	{ 0x3636, 0x00 },
253d30bb512STodor Tomov 	{ 0x3662, 0x01 },
254d30bb512STodor Tomov 	{ 0x3663, 0x70 },
255d30bb512STodor Tomov 	{ 0x3664, 0x50 },
256d30bb512STodor Tomov 	{ 0x3666, 0x0a },
257d30bb512STodor Tomov 	{ 0x3669, 0x1a },
258d30bb512STodor Tomov 	{ 0x366a, 0x00 },
259d30bb512STodor Tomov 	{ 0x366b, 0x50 },
260d30bb512STodor Tomov 	{ 0x3673, 0x01 },
261d30bb512STodor Tomov 	{ 0x3674, 0xff },
262d30bb512STodor Tomov 	{ 0x3675, 0x03 },
263d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
264d30bb512STodor Tomov 	{ 0x3709, 0x40 },
265d30bb512STodor Tomov 	{ 0x373c, 0x08 },
266d30bb512STodor Tomov 	{ 0x3742, 0x00 },
267d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
268d30bb512STodor Tomov 	{ 0x3788, 0x00 },
269d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
270d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
271d30bb512STodor Tomov 	{ 0x3800, 0x00 },
272d30bb512STodor Tomov 	{ 0x3801, 0x04 },
273d30bb512STodor Tomov 	{ 0x3802, 0x00 },
274d30bb512STodor Tomov 	{ 0x3803, 0x04 },
275d30bb512STodor Tomov 	{ 0x3804, 0x02 },
276d30bb512STodor Tomov 	{ 0x3805, 0x8b },
277d30bb512STodor Tomov 	{ 0x3806, 0x01 },
278d30bb512STodor Tomov 	{ 0x3807, 0xeb },
279d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
280d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
281d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
282d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
283d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
284d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
285d30bb512STodor Tomov 	{ 0x380e, 0x06 }, /* total vertical timing high */
286d30bb512STodor Tomov 	{ 0x380f, 0xbc }, /* total vertical timing low */
287d30bb512STodor Tomov 	{ 0x3810, 0x00 },
288d30bb512STodor Tomov 	{ 0x3811, 0x04 },
289d30bb512STodor Tomov 	{ 0x3812, 0x00 },
290d30bb512STodor Tomov 	{ 0x3813, 0x05 },
291d30bb512STodor Tomov 	{ 0x3814, 0x11 },
292d30bb512STodor Tomov 	{ 0x3815, 0x11 },
293d30bb512STodor Tomov 	{ 0x3820, 0x40 },
294d30bb512STodor Tomov 	{ 0x3821, 0x00 },
295d30bb512STodor Tomov 	{ 0x382f, 0x0e },
296d30bb512STodor Tomov 	{ 0x3832, 0x00 },
297d30bb512STodor Tomov 	{ 0x3833, 0x05 },
298d30bb512STodor Tomov 	{ 0x3834, 0x00 },
299d30bb512STodor Tomov 	{ 0x3835, 0x0c },
300d30bb512STodor Tomov 	{ 0x3837, 0x00 },
301d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
302d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
303d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
304d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
305d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
306d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
307d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
308d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
309d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
310d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
311d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
312d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
313d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
314d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
315d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
316d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
317d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
318d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
319d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
320d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
321d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
322d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
323d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
324d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
325d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
326d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
327d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
328d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
329d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
330d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
331d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
332d30bb512STodor Tomov 	{ 0x4001, 0x42 },
333d30bb512STodor Tomov 	{ 0x4004, 0x04 },
334d30bb512STodor Tomov 	{ 0x4005, 0x00 },
335d30bb512STodor Tomov 	{ 0x404e, 0x01 },
336d30bb512STodor Tomov 	{ 0x4300, 0xff },
337d30bb512STodor Tomov 	{ 0x4301, 0x00 },
338d30bb512STodor Tomov 	{ 0x4315, 0x00 },
339d30bb512STodor Tomov 	{ 0x4501, 0x48 },
340d30bb512STodor Tomov 	{ 0x4600, 0x00 },
341d30bb512STodor Tomov 	{ 0x4601, 0x4e },
342d30bb512STodor Tomov 	{ 0x4801, 0x0f },
343d30bb512STodor Tomov 	{ 0x4806, 0x0f },
344d30bb512STodor Tomov 	{ 0x4819, 0xaa },
345d30bb512STodor Tomov 	{ 0x4823, 0x3e },
346d30bb512STodor Tomov 	{ 0x4837, 0x19 },
347d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
348d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
349d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
350d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
351d30bb512STodor Tomov 	{ 0x5000, 0x85 },
352d30bb512STodor Tomov 	{ 0x5001, 0x80 },
353d30bb512STodor Tomov };
354d30bb512STodor Tomov 
355d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_60fps[] = {
356d30bb512STodor Tomov 	{ 0x3005, 0x00 },
357d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
358d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
359d30bb512STodor Tomov 	{ 0x3014, 0x04 },
360d30bb512STodor Tomov 	{ 0x3016, 0x10 },
361d30bb512STodor Tomov 	{ 0x3017, 0x00 },
362d30bb512STodor Tomov 	{ 0x3018, 0x00 },
363d30bb512STodor Tomov 	{ 0x301a, 0x00 },
364d30bb512STodor Tomov 	{ 0x301b, 0x00 },
365d30bb512STodor Tomov 	{ 0x301c, 0x00 },
366d30bb512STodor Tomov 	{ 0x3023, 0x05 },
367d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
368d30bb512STodor Tomov 	{ 0x3106, 0xda },
369d30bb512STodor Tomov 	{ 0x3503, 0x07 },
370d30bb512STodor Tomov 	{ 0x3509, 0x10 },
371d30bb512STodor Tomov 	{ 0x3600, 0x1c },
372d30bb512STodor Tomov 	{ 0x3602, 0x62 },
373d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
374d30bb512STodor Tomov 	{ 0x3622, 0x04 },
375d30bb512STodor Tomov 	{ 0x3626, 0x21 },
376d30bb512STodor Tomov 	{ 0x3627, 0x30 },
377d30bb512STodor Tomov 	{ 0x3630, 0x44 },
378d30bb512STodor Tomov 	{ 0x3631, 0x35 },
379d30bb512STodor Tomov 	{ 0x3634, 0x60 },
380d30bb512STodor Tomov 	{ 0x3636, 0x00 },
381d30bb512STodor Tomov 	{ 0x3662, 0x01 },
382d30bb512STodor Tomov 	{ 0x3663, 0x70 },
383d30bb512STodor Tomov 	{ 0x3664, 0x50 },
384d30bb512STodor Tomov 	{ 0x3666, 0x0a },
385d30bb512STodor Tomov 	{ 0x3669, 0x1a },
386d30bb512STodor Tomov 	{ 0x366a, 0x00 },
387d30bb512STodor Tomov 	{ 0x366b, 0x50 },
388d30bb512STodor Tomov 	{ 0x3673, 0x01 },
389d30bb512STodor Tomov 	{ 0x3674, 0xff },
390d30bb512STodor Tomov 	{ 0x3675, 0x03 },
391d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
392d30bb512STodor Tomov 	{ 0x3709, 0x40 },
393d30bb512STodor Tomov 	{ 0x373c, 0x08 },
394d30bb512STodor Tomov 	{ 0x3742, 0x00 },
395d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
396d30bb512STodor Tomov 	{ 0x3788, 0x00 },
397d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
398d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
399d30bb512STodor Tomov 	{ 0x3800, 0x00 },
400d30bb512STodor Tomov 	{ 0x3801, 0x04 },
401d30bb512STodor Tomov 	{ 0x3802, 0x00 },
402d30bb512STodor Tomov 	{ 0x3803, 0x04 },
403d30bb512STodor Tomov 	{ 0x3804, 0x02 },
404d30bb512STodor Tomov 	{ 0x3805, 0x8b },
405d30bb512STodor Tomov 	{ 0x3806, 0x01 },
406d30bb512STodor Tomov 	{ 0x3807, 0xeb },
407d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
408d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
409d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
410d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
411d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
412d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
413d30bb512STodor Tomov 	{ 0x380e, 0x03 }, /* total vertical timing high */
414d30bb512STodor Tomov 	{ 0x380f, 0x5c }, /* total vertical timing low */
415d30bb512STodor Tomov 	{ 0x3810, 0x00 },
416d30bb512STodor Tomov 	{ 0x3811, 0x04 },
417d30bb512STodor Tomov 	{ 0x3812, 0x00 },
418d30bb512STodor Tomov 	{ 0x3813, 0x05 },
419d30bb512STodor Tomov 	{ 0x3814, 0x11 },
420d30bb512STodor Tomov 	{ 0x3815, 0x11 },
421d30bb512STodor Tomov 	{ 0x3820, 0x40 },
422d30bb512STodor Tomov 	{ 0x3821, 0x00 },
423d30bb512STodor Tomov 	{ 0x382f, 0x0e },
424d30bb512STodor Tomov 	{ 0x3832, 0x00 },
425d30bb512STodor Tomov 	{ 0x3833, 0x05 },
426d30bb512STodor Tomov 	{ 0x3834, 0x00 },
427d30bb512STodor Tomov 	{ 0x3835, 0x0c },
428d30bb512STodor Tomov 	{ 0x3837, 0x00 },
429d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
430d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
431d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
432d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
433d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
434d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
435d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
436d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
437d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
438d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
439d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
440d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
441d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
442d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
443d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
444d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
445d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
446d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
447d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
448d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
449d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
450d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
451d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
452d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
453d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
454d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
455d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
456d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
457d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
458d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
459d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
460d30bb512STodor Tomov 	{ 0x4001, 0x42 },
461d30bb512STodor Tomov 	{ 0x4004, 0x04 },
462d30bb512STodor Tomov 	{ 0x4005, 0x00 },
463d30bb512STodor Tomov 	{ 0x404e, 0x01 },
464d30bb512STodor Tomov 	{ 0x4300, 0xff },
465d30bb512STodor Tomov 	{ 0x4301, 0x00 },
466d30bb512STodor Tomov 	{ 0x4315, 0x00 },
467d30bb512STodor Tomov 	{ 0x4501, 0x48 },
468d30bb512STodor Tomov 	{ 0x4600, 0x00 },
469d30bb512STodor Tomov 	{ 0x4601, 0x4e },
470d30bb512STodor Tomov 	{ 0x4801, 0x0f },
471d30bb512STodor Tomov 	{ 0x4806, 0x0f },
472d30bb512STodor Tomov 	{ 0x4819, 0xaa },
473d30bb512STodor Tomov 	{ 0x4823, 0x3e },
474d30bb512STodor Tomov 	{ 0x4837, 0x19 },
475d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
476d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
477d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
478d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
479d30bb512STodor Tomov 	{ 0x5000, 0x85 },
480d30bb512STodor Tomov 	{ 0x5001, 0x80 },
481d30bb512STodor Tomov };
482d30bb512STodor Tomov 
483d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_90fps[] = {
484d30bb512STodor Tomov 	{ 0x3005, 0x00 },
485d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
486d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
487d30bb512STodor Tomov 	{ 0x3014, 0x04 },
488d30bb512STodor Tomov 	{ 0x3016, 0x10 },
489d30bb512STodor Tomov 	{ 0x3017, 0x00 },
490d30bb512STodor Tomov 	{ 0x3018, 0x00 },
491d30bb512STodor Tomov 	{ 0x301a, 0x00 },
492d30bb512STodor Tomov 	{ 0x301b, 0x00 },
493d30bb512STodor Tomov 	{ 0x301c, 0x00 },
494d30bb512STodor Tomov 	{ 0x3023, 0x05 },
495d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
496d30bb512STodor Tomov 	{ 0x3106, 0xda },
497d30bb512STodor Tomov 	{ 0x3503, 0x07 },
498d30bb512STodor Tomov 	{ 0x3509, 0x10 },
499d30bb512STodor Tomov 	{ 0x3600, 0x1c },
500d30bb512STodor Tomov 	{ 0x3602, 0x62 },
501d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
502d30bb512STodor Tomov 	{ 0x3622, 0x04 },
503d30bb512STodor Tomov 	{ 0x3626, 0x21 },
504d30bb512STodor Tomov 	{ 0x3627, 0x30 },
505d30bb512STodor Tomov 	{ 0x3630, 0x44 },
506d30bb512STodor Tomov 	{ 0x3631, 0x35 },
507d30bb512STodor Tomov 	{ 0x3634, 0x60 },
508d30bb512STodor Tomov 	{ 0x3636, 0x00 },
509d30bb512STodor Tomov 	{ 0x3662, 0x01 },
510d30bb512STodor Tomov 	{ 0x3663, 0x70 },
511d30bb512STodor Tomov 	{ 0x3664, 0x50 },
512d30bb512STodor Tomov 	{ 0x3666, 0x0a },
513d30bb512STodor Tomov 	{ 0x3669, 0x1a },
514d30bb512STodor Tomov 	{ 0x366a, 0x00 },
515d30bb512STodor Tomov 	{ 0x366b, 0x50 },
516d30bb512STodor Tomov 	{ 0x3673, 0x01 },
517d30bb512STodor Tomov 	{ 0x3674, 0xff },
518d30bb512STodor Tomov 	{ 0x3675, 0x03 },
519d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
520d30bb512STodor Tomov 	{ 0x3709, 0x40 },
521d30bb512STodor Tomov 	{ 0x373c, 0x08 },
522d30bb512STodor Tomov 	{ 0x3742, 0x00 },
523d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
524d30bb512STodor Tomov 	{ 0x3788, 0x00 },
525d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
526d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
527d30bb512STodor Tomov 	{ 0x3800, 0x00 },
528d30bb512STodor Tomov 	{ 0x3801, 0x04 },
529d30bb512STodor Tomov 	{ 0x3802, 0x00 },
530d30bb512STodor Tomov 	{ 0x3803, 0x04 },
531d30bb512STodor Tomov 	{ 0x3804, 0x02 },
532d30bb512STodor Tomov 	{ 0x3805, 0x8b },
533d30bb512STodor Tomov 	{ 0x3806, 0x01 },
534d30bb512STodor Tomov 	{ 0x3807, 0xeb },
535d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
536d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
537d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
538d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
539d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
540d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
541d30bb512STodor Tomov 	{ 0x380e, 0x02 }, /* total vertical timing high */
542d30bb512STodor Tomov 	{ 0x380f, 0x3c }, /* total vertical timing low */
543d30bb512STodor Tomov 	{ 0x3810, 0x00 },
544d30bb512STodor Tomov 	{ 0x3811, 0x04 },
545d30bb512STodor Tomov 	{ 0x3812, 0x00 },
546d30bb512STodor Tomov 	{ 0x3813, 0x05 },
547d30bb512STodor Tomov 	{ 0x3814, 0x11 },
548d30bb512STodor Tomov 	{ 0x3815, 0x11 },
549d30bb512STodor Tomov 	{ 0x3820, 0x40 },
550d30bb512STodor Tomov 	{ 0x3821, 0x00 },
551d30bb512STodor Tomov 	{ 0x382f, 0x0e },
552d30bb512STodor Tomov 	{ 0x3832, 0x00 },
553d30bb512STodor Tomov 	{ 0x3833, 0x05 },
554d30bb512STodor Tomov 	{ 0x3834, 0x00 },
555d30bb512STodor Tomov 	{ 0x3835, 0x0c },
556d30bb512STodor Tomov 	{ 0x3837, 0x00 },
557d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
558d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
559d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
560d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
561d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
562d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
563d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
564d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
565d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
566d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
567d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
568d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
569d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
570d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
571d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
572d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
573d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
574d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
575d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
576d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
577d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
578d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
579d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
580d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
581d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
582d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
583d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
584d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
585d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
586d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
587d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
588d30bb512STodor Tomov 	{ 0x4001, 0x42 },
589d30bb512STodor Tomov 	{ 0x4004, 0x04 },
590d30bb512STodor Tomov 	{ 0x4005, 0x00 },
591d30bb512STodor Tomov 	{ 0x404e, 0x01 },
592d30bb512STodor Tomov 	{ 0x4300, 0xff },
593d30bb512STodor Tomov 	{ 0x4301, 0x00 },
594d30bb512STodor Tomov 	{ 0x4315, 0x00 },
595d30bb512STodor Tomov 	{ 0x4501, 0x48 },
596d30bb512STodor Tomov 	{ 0x4600, 0x00 },
597d30bb512STodor Tomov 	{ 0x4601, 0x4e },
598d30bb512STodor Tomov 	{ 0x4801, 0x0f },
599d30bb512STodor Tomov 	{ 0x4806, 0x0f },
600d30bb512STodor Tomov 	{ 0x4819, 0xaa },
601d30bb512STodor Tomov 	{ 0x4823, 0x3e },
602d30bb512STodor Tomov 	{ 0x4837, 0x19 },
603d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
604d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
605d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
606d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
607d30bb512STodor Tomov 	{ 0x5000, 0x85 },
608d30bb512STodor Tomov 	{ 0x5001, 0x80 },
609d30bb512STodor Tomov };
610d30bb512STodor Tomov 
611df057b0dSDaniel Scally static const unsigned long supported_xclk_rates[] = {
612ed9566ceSDaniel Scally 	[OV7251_19_2_MHZ] = 19200000,
613df057b0dSDaniel Scally 	[OV7251_24_MHZ] = 24000000,
614df057b0dSDaniel Scally };
615df057b0dSDaniel Scally 
616d30bb512STodor Tomov static const s64 link_freq[] = {
6171757b44eSDaniel Scally 	[OV7251_LINK_FREQ_240_MHZ] = 240000000,
618ed9566ceSDaniel Scally 	[OV7251_LINK_FREQ_319_2_MHZ] = 319200000,
6191757b44eSDaniel Scally };
6201757b44eSDaniel Scally 
6211757b44eSDaniel Scally static const s64 pixel_rates[] = {
6221757b44eSDaniel Scally 	[OV7251_LINK_FREQ_240_MHZ] = 48000000,
623ed9566ceSDaniel Scally 	[OV7251_LINK_FREQ_319_2_MHZ] = 63840000,
624d30bb512STodor Tomov };
625d30bb512STodor Tomov 
626d30bb512STodor Tomov static const struct ov7251_mode_info ov7251_mode_info_data[] = {
627d30bb512STodor Tomov 	{
628d30bb512STodor Tomov 		.width = 640,
629d30bb512STodor Tomov 		.height = 480,
630d30bb512STodor Tomov 		.data = ov7251_setting_vga_30fps,
631d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_30fps),
632d30bb512STodor Tomov 		.exposure_max = 1704,
633d30bb512STodor Tomov 		.exposure_def = 504,
634d30bb512STodor Tomov 		.timeperframe = {
635d30bb512STodor Tomov 			.numerator = 100,
636d30bb512STodor Tomov 			.denominator = 3000
637d30bb512STodor Tomov 		}
638d30bb512STodor Tomov 	},
639d30bb512STodor Tomov 	{
640d30bb512STodor Tomov 		.width = 640,
641d30bb512STodor Tomov 		.height = 480,
642d30bb512STodor Tomov 		.data = ov7251_setting_vga_60fps,
643d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_60fps),
644d30bb512STodor Tomov 		.exposure_max = 840,
645d30bb512STodor Tomov 		.exposure_def = 504,
646d30bb512STodor Tomov 		.timeperframe = {
647d30bb512STodor Tomov 			.numerator = 100,
648d30bb512STodor Tomov 			.denominator = 6014
649d30bb512STodor Tomov 		}
650d30bb512STodor Tomov 	},
651d30bb512STodor Tomov 	{
652d30bb512STodor Tomov 		.width = 640,
653d30bb512STodor Tomov 		.height = 480,
654d30bb512STodor Tomov 		.data = ov7251_setting_vga_90fps,
655d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_90fps),
656d30bb512STodor Tomov 		.exposure_max = 552,
657d30bb512STodor Tomov 		.exposure_def = 504,
658d30bb512STodor Tomov 		.timeperframe = {
659d30bb512STodor Tomov 			.numerator = 100,
660d30bb512STodor Tomov 			.denominator = 9043
661d30bb512STodor Tomov 		}
662d30bb512STodor Tomov 	},
663d30bb512STodor Tomov };
664d30bb512STodor Tomov 
665d30bb512STodor Tomov static int ov7251_regulators_enable(struct ov7251 *ov7251)
666d30bb512STodor Tomov {
667d30bb512STodor Tomov 	int ret;
668d30bb512STodor Tomov 
669d30bb512STodor Tomov 	/* OV7251 power up sequence requires core regulator
670d30bb512STodor Tomov 	 * to be enabled not earlier than io regulator
671d30bb512STodor Tomov 	 */
672d30bb512STodor Tomov 
673d30bb512STodor Tomov 	ret = regulator_enable(ov7251->io_regulator);
674d30bb512STodor Tomov 	if (ret < 0) {
675d30bb512STodor Tomov 		dev_err(ov7251->dev, "set io voltage failed\n");
676d30bb512STodor Tomov 		return ret;
677d30bb512STodor Tomov 	}
678d30bb512STodor Tomov 
679d30bb512STodor Tomov 	ret = regulator_enable(ov7251->analog_regulator);
680d30bb512STodor Tomov 	if (ret) {
681d30bb512STodor Tomov 		dev_err(ov7251->dev, "set analog voltage failed\n");
682d30bb512STodor Tomov 		goto err_disable_io;
683d30bb512STodor Tomov 	}
684d30bb512STodor Tomov 
685d30bb512STodor Tomov 	ret = regulator_enable(ov7251->core_regulator);
686d30bb512STodor Tomov 	if (ret) {
687d30bb512STodor Tomov 		dev_err(ov7251->dev, "set core voltage failed\n");
688d30bb512STodor Tomov 		goto err_disable_analog;
689d30bb512STodor Tomov 	}
690d30bb512STodor Tomov 
691d30bb512STodor Tomov 	return 0;
692d30bb512STodor Tomov 
693d30bb512STodor Tomov err_disable_analog:
694d30bb512STodor Tomov 	regulator_disable(ov7251->analog_regulator);
695d30bb512STodor Tomov 
696d30bb512STodor Tomov err_disable_io:
697d30bb512STodor Tomov 	regulator_disable(ov7251->io_regulator);
698d30bb512STodor Tomov 
699d30bb512STodor Tomov 	return ret;
700d30bb512STodor Tomov }
701d30bb512STodor Tomov 
702d30bb512STodor Tomov static void ov7251_regulators_disable(struct ov7251 *ov7251)
703d30bb512STodor Tomov {
704d30bb512STodor Tomov 	int ret;
705d30bb512STodor Tomov 
706d30bb512STodor Tomov 	ret = regulator_disable(ov7251->core_regulator);
707d30bb512STodor Tomov 	if (ret < 0)
708d30bb512STodor Tomov 		dev_err(ov7251->dev, "core regulator disable failed\n");
709d30bb512STodor Tomov 
710d30bb512STodor Tomov 	ret = regulator_disable(ov7251->analog_regulator);
711d30bb512STodor Tomov 	if (ret < 0)
712d30bb512STodor Tomov 		dev_err(ov7251->dev, "analog regulator disable failed\n");
713d30bb512STodor Tomov 
714d30bb512STodor Tomov 	ret = regulator_disable(ov7251->io_regulator);
715d30bb512STodor Tomov 	if (ret < 0)
716d30bb512STodor Tomov 		dev_err(ov7251->dev, "io regulator disable failed\n");
717d30bb512STodor Tomov }
718d30bb512STodor Tomov 
719d30bb512STodor Tomov static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)
720d30bb512STodor Tomov {
721d30bb512STodor Tomov 	u8 regbuf[3];
722d30bb512STodor Tomov 	int ret;
723d30bb512STodor Tomov 
724d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
725d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
726d30bb512STodor Tomov 	regbuf[2] = val;
727d30bb512STodor Tomov 
728d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, 3);
729d30bb512STodor Tomov 	if (ret < 0) {
730d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n",
731d30bb512STodor Tomov 			__func__, ret, reg, val);
732d30bb512STodor Tomov 		return ret;
733d30bb512STodor Tomov 	}
734d30bb512STodor Tomov 
735d30bb512STodor Tomov 	return 0;
736d30bb512STodor Tomov }
737d30bb512STodor Tomov 
738d30bb512STodor Tomov static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val,
739d30bb512STodor Tomov 				 u8 num)
740d30bb512STodor Tomov {
741d30bb512STodor Tomov 	u8 regbuf[5];
742d30bb512STodor Tomov 	u8 nregbuf = sizeof(reg) + num * sizeof(*val);
743d30bb512STodor Tomov 	int ret = 0;
744d30bb512STodor Tomov 
745d30bb512STodor Tomov 	if (nregbuf > sizeof(regbuf))
746d30bb512STodor Tomov 		return -EINVAL;
747d30bb512STodor Tomov 
748d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
749d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
750d30bb512STodor Tomov 
751d30bb512STodor Tomov 	memcpy(regbuf + 2, val, num);
752d30bb512STodor Tomov 
753d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf);
754d30bb512STodor Tomov 	if (ret < 0) {
755d30bb512STodor Tomov 		dev_err(ov7251->dev,
756d30bb512STodor Tomov 			"%s: write seq regs error %d: first reg=%x\n",
757d30bb512STodor Tomov 			__func__, ret, reg);
758d30bb512STodor Tomov 		return ret;
759d30bb512STodor Tomov 	}
760d30bb512STodor Tomov 
761d30bb512STodor Tomov 	return 0;
762d30bb512STodor Tomov }
763d30bb512STodor Tomov 
764d30bb512STodor Tomov static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val)
765d30bb512STodor Tomov {
766d30bb512STodor Tomov 	u8 regbuf[2];
767d30bb512STodor Tomov 	int ret;
768d30bb512STodor Tomov 
769d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
770d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
771d30bb512STodor Tomov 
772d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, 2);
773d30bb512STodor Tomov 	if (ret < 0) {
774d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n",
775d30bb512STodor Tomov 			__func__, ret, reg);
776d30bb512STodor Tomov 		return ret;
777d30bb512STodor Tomov 	}
778d30bb512STodor Tomov 
779d30bb512STodor Tomov 	ret = i2c_master_recv(ov7251->i2c_client, val, 1);
780d30bb512STodor Tomov 	if (ret < 0) {
781d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n",
782d30bb512STodor Tomov 			__func__, ret, reg);
783d30bb512STodor Tomov 		return ret;
784d30bb512STodor Tomov 	}
785d30bb512STodor Tomov 
786d30bb512STodor Tomov 	return 0;
787d30bb512STodor Tomov }
788d30bb512STodor Tomov 
789df057b0dSDaniel Scally static int ov7251_pll_configure(struct ov7251 *ov7251)
790df057b0dSDaniel Scally {
791df057b0dSDaniel Scally 	const struct ov7251_pll_cfgs *configs;
792df057b0dSDaniel Scally 	int ret;
793df057b0dSDaniel Scally 
794df057b0dSDaniel Scally 	configs = ov7251->pll_cfgs;
795df057b0dSDaniel Scally 
796df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL1_PRE_DIV_REG,
797df057b0dSDaniel Scally 			       configs->pll1[ov7251->link_freq_idx]->pre_div);
798df057b0dSDaniel Scally 	if (ret < 0)
799df057b0dSDaniel Scally 		return ret;
800df057b0dSDaniel Scally 
801df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL1_MULT_REG,
802df057b0dSDaniel Scally 			       configs->pll1[ov7251->link_freq_idx]->mult);
803df057b0dSDaniel Scally 	if (ret < 0)
804df057b0dSDaniel Scally 		return ret;
805df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL1_DIVIDER_REG,
806df057b0dSDaniel Scally 			       configs->pll1[ov7251->link_freq_idx]->div);
807df057b0dSDaniel Scally 	if (ret < 0)
808df057b0dSDaniel Scally 		return ret;
809df057b0dSDaniel Scally 
810df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL1_PIX_DIV_REG,
811df057b0dSDaniel Scally 			       configs->pll1[ov7251->link_freq_idx]->pix_div);
812df057b0dSDaniel Scally 	if (ret < 0)
813df057b0dSDaniel Scally 		return ret;
814df057b0dSDaniel Scally 
815df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL1_MIPI_DIV_REG,
816df057b0dSDaniel Scally 			       configs->pll1[ov7251->link_freq_idx]->mipi_div);
817df057b0dSDaniel Scally 	if (ret < 0)
818df057b0dSDaniel Scally 		return ret;
819df057b0dSDaniel Scally 
820df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL2_PRE_DIV_REG,
821df057b0dSDaniel Scally 			       configs->pll2->pre_div);
822df057b0dSDaniel Scally 	if (ret < 0)
823df057b0dSDaniel Scally 		return ret;
824df057b0dSDaniel Scally 
825df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL2_MULT_REG,
826df057b0dSDaniel Scally 			       configs->pll2->mult);
827df057b0dSDaniel Scally 	if (ret < 0)
828df057b0dSDaniel Scally 		return ret;
829df057b0dSDaniel Scally 
830df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL2_DIVIDER_REG,
831df057b0dSDaniel Scally 			       configs->pll2->div);
832df057b0dSDaniel Scally 	if (ret < 0)
833df057b0dSDaniel Scally 		return ret;
834df057b0dSDaniel Scally 
835df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL2_SYS_DIV_REG,
836df057b0dSDaniel Scally 			       configs->pll2->sys_div);
837df057b0dSDaniel Scally 	if (ret < 0)
838df057b0dSDaniel Scally 		return ret;
839df057b0dSDaniel Scally 
840df057b0dSDaniel Scally 	ret = ov7251_write_reg(ov7251, OV7251_PLL2_ADC_DIV_REG,
841df057b0dSDaniel Scally 			       configs->pll2->adc_div);
842df057b0dSDaniel Scally 
843df057b0dSDaniel Scally 	return ret;
844df057b0dSDaniel Scally }
845df057b0dSDaniel Scally 
846d30bb512STodor Tomov static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure)
847d30bb512STodor Tomov {
848d30bb512STodor Tomov 	u16 reg;
849d30bb512STodor Tomov 	u8 val[3];
850d30bb512STodor Tomov 
851d30bb512STodor Tomov 	reg = OV7251_AEC_EXPO_0;
852d30bb512STodor Tomov 	val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */
853d30bb512STodor Tomov 	val[1] = (exposure & 0x0ff0) >> 4;  /* goes to OV7251_AEC_EXPO_1 */
854d30bb512STodor Tomov 	val[2] = (exposure & 0x000f) << 4;  /* goes to OV7251_AEC_EXPO_2 */
855d30bb512STodor Tomov 
856d30bb512STodor Tomov 	return ov7251_write_seq_regs(ov7251, reg, val, 3);
857d30bb512STodor Tomov }
858d30bb512STodor Tomov 
859d30bb512STodor Tomov static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain)
860d30bb512STodor Tomov {
861d30bb512STodor Tomov 	u16 reg;
862d30bb512STodor Tomov 	u8 val[2];
863d30bb512STodor Tomov 
864d30bb512STodor Tomov 	reg = OV7251_AEC_AGC_ADJ_0;
865d30bb512STodor Tomov 	val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */
866d30bb512STodor Tomov 	val[1] = gain & 0xff;          /* goes to OV7251_AEC_AGC_ADJ_1 */
867d30bb512STodor Tomov 
868d30bb512STodor Tomov 	return ov7251_write_seq_regs(ov7251, reg, val, 2);
869d30bb512STodor Tomov }
870d30bb512STodor Tomov 
871d30bb512STodor Tomov static int ov7251_set_register_array(struct ov7251 *ov7251,
872d30bb512STodor Tomov 				     const struct reg_value *settings,
873d30bb512STodor Tomov 				     unsigned int num_settings)
874d30bb512STodor Tomov {
875d30bb512STodor Tomov 	unsigned int i;
876d30bb512STodor Tomov 	int ret;
877d30bb512STodor Tomov 
878d30bb512STodor Tomov 	for (i = 0; i < num_settings; ++i, ++settings) {
879d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, settings->reg, settings->val);
880d30bb512STodor Tomov 		if (ret < 0)
881d30bb512STodor Tomov 			return ret;
882d30bb512STodor Tomov 	}
883d30bb512STodor Tomov 
884d30bb512STodor Tomov 	return 0;
885d30bb512STodor Tomov }
886d30bb512STodor Tomov 
887207f4162SDaniel Scally static int ov7251_set_power_on(struct device *dev)
888d30bb512STodor Tomov {
889207f4162SDaniel Scally 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
890207f4162SDaniel Scally 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
891207f4162SDaniel Scally 	struct ov7251 *ov7251 = to_ov7251(sd);
892d30bb512STodor Tomov 	int ret;
893d30bb512STodor Tomov 	u32 wait_us;
894d30bb512STodor Tomov 
895d30bb512STodor Tomov 	ret = ov7251_regulators_enable(ov7251);
896d30bb512STodor Tomov 	if (ret < 0)
897d30bb512STodor Tomov 		return ret;
898d30bb512STodor Tomov 
899d30bb512STodor Tomov 	ret = clk_prepare_enable(ov7251->xclk);
900d30bb512STodor Tomov 	if (ret < 0) {
901d30bb512STodor Tomov 		dev_err(ov7251->dev, "clk prepare enable failed\n");
902d30bb512STodor Tomov 		ov7251_regulators_disable(ov7251);
903d30bb512STodor Tomov 		return ret;
904d30bb512STodor Tomov 	}
905d30bb512STodor Tomov 
906d30bb512STodor Tomov 	gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
907d30bb512STodor Tomov 
908d30bb512STodor Tomov 	/* wait at least 65536 external clock cycles */
909d30bb512STodor Tomov 	wait_us = DIV_ROUND_UP(65536 * 1000,
910d30bb512STodor Tomov 			       DIV_ROUND_UP(ov7251->xclk_freq, 1000));
911d30bb512STodor Tomov 	usleep_range(wait_us, wait_us + 1000);
912d30bb512STodor Tomov 
913*9e1d3012SDaniel Scally 	ret = ov7251_set_register_array(ov7251,
914*9e1d3012SDaniel Scally 					ov7251_global_init_setting,
915*9e1d3012SDaniel Scally 					ARRAY_SIZE(ov7251_global_init_setting));
916*9e1d3012SDaniel Scally 	if (ret < 0) {
917*9e1d3012SDaniel Scally 		dev_err(ov7251->dev, "error during global init\n");
918*9e1d3012SDaniel Scally 		ov7251_regulators_disable(ov7251);
919*9e1d3012SDaniel Scally 		return ret;
920*9e1d3012SDaniel Scally 	}
921*9e1d3012SDaniel Scally 
922*9e1d3012SDaniel Scally 	return ret;
923d30bb512STodor Tomov }
924d30bb512STodor Tomov 
925207f4162SDaniel Scally static int ov7251_set_power_off(struct device *dev)
926d30bb512STodor Tomov {
927207f4162SDaniel Scally 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
928207f4162SDaniel Scally 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
929207f4162SDaniel Scally 	struct ov7251 *ov7251 = to_ov7251(sd);
930207f4162SDaniel Scally 
931d30bb512STodor Tomov 	clk_disable_unprepare(ov7251->xclk);
932d30bb512STodor Tomov 	gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
933d30bb512STodor Tomov 	ov7251_regulators_disable(ov7251);
934207f4162SDaniel Scally 
935207f4162SDaniel Scally 	return 0;
936d30bb512STodor Tomov }
937d30bb512STodor Tomov 
938d30bb512STodor Tomov static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
939d30bb512STodor Tomov {
940d30bb512STodor Tomov 	u8 val = ov7251->timing_format2;
941d30bb512STodor Tomov 	int ret;
942d30bb512STodor Tomov 
943d30bb512STodor Tomov 	if (value)
944d30bb512STodor Tomov 		val |= OV7251_TIMING_FORMAT2_MIRROR;
945d30bb512STodor Tomov 	else
946d30bb512STodor Tomov 		val &= ~OV7251_TIMING_FORMAT2_MIRROR;
947d30bb512STodor Tomov 
948d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
949d30bb512STodor Tomov 	if (!ret)
950d30bb512STodor Tomov 		ov7251->timing_format2 = val;
951d30bb512STodor Tomov 
952d30bb512STodor Tomov 	return ret;
953d30bb512STodor Tomov }
954d30bb512STodor Tomov 
955d30bb512STodor Tomov static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
956d30bb512STodor Tomov {
957d30bb512STodor Tomov 	u8 val = ov7251->timing_format1;
958d30bb512STodor Tomov 	int ret;
959d30bb512STodor Tomov 
960d30bb512STodor Tomov 	if (value)
961d30bb512STodor Tomov 		val |= OV7251_TIMING_FORMAT1_VFLIP;
962d30bb512STodor Tomov 	else
963d30bb512STodor Tomov 		val &= ~OV7251_TIMING_FORMAT1_VFLIP;
964d30bb512STodor Tomov 
965d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
966d30bb512STodor Tomov 	if (!ret)
967d30bb512STodor Tomov 		ov7251->timing_format1 = val;
968d30bb512STodor Tomov 
969d30bb512STodor Tomov 	return ret;
970d30bb512STodor Tomov }
971d30bb512STodor Tomov 
972d30bb512STodor Tomov static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
973d30bb512STodor Tomov {
974d30bb512STodor Tomov 	u8 val = ov7251->pre_isp_00;
975d30bb512STodor Tomov 	int ret;
976d30bb512STodor Tomov 
977d30bb512STodor Tomov 	if (value)
978d30bb512STodor Tomov 		val |= OV7251_PRE_ISP_00_TEST_PATTERN;
979d30bb512STodor Tomov 	else
980d30bb512STodor Tomov 		val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
981d30bb512STodor Tomov 
982d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
983d30bb512STodor Tomov 	if (!ret)
984d30bb512STodor Tomov 		ov7251->pre_isp_00 = val;
985d30bb512STodor Tomov 
986d30bb512STodor Tomov 	return ret;
987d30bb512STodor Tomov }
988d30bb512STodor Tomov 
989d30bb512STodor Tomov static const char * const ov7251_test_pattern_menu[] = {
990d30bb512STodor Tomov 	"Disabled",
991d30bb512STodor Tomov 	"Vertical Pattern Bars",
992d30bb512STodor Tomov };
993d30bb512STodor Tomov 
994d30bb512STodor Tomov static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
995d30bb512STodor Tomov {
996d30bb512STodor Tomov 	struct ov7251 *ov7251 = container_of(ctrl->handler,
997d30bb512STodor Tomov 					     struct ov7251, ctrls);
998d30bb512STodor Tomov 	int ret;
999d30bb512STodor Tomov 
1000d30bb512STodor Tomov 	/* v4l2_ctrl_lock() locks our mutex */
1001d30bb512STodor Tomov 
1002207f4162SDaniel Scally 	if (!pm_runtime_get_if_in_use(ov7251->dev))
1003d30bb512STodor Tomov 		return 0;
1004d30bb512STodor Tomov 
1005d30bb512STodor Tomov 	switch (ctrl->id) {
1006d30bb512STodor Tomov 	case V4L2_CID_EXPOSURE:
1007d30bb512STodor Tomov 		ret = ov7251_set_exposure(ov7251, ctrl->val);
1008d30bb512STodor Tomov 		break;
1009d30bb512STodor Tomov 	case V4L2_CID_GAIN:
1010d30bb512STodor Tomov 		ret = ov7251_set_gain(ov7251, ctrl->val);
1011d30bb512STodor Tomov 		break;
1012d30bb512STodor Tomov 	case V4L2_CID_TEST_PATTERN:
1013d30bb512STodor Tomov 		ret = ov7251_set_test_pattern(ov7251, ctrl->val);
1014d30bb512STodor Tomov 		break;
1015d30bb512STodor Tomov 	case V4L2_CID_HFLIP:
1016d30bb512STodor Tomov 		ret = ov7251_set_hflip(ov7251, ctrl->val);
1017d30bb512STodor Tomov 		break;
1018d30bb512STodor Tomov 	case V4L2_CID_VFLIP:
1019d30bb512STodor Tomov 		ret = ov7251_set_vflip(ov7251, ctrl->val);
1020d30bb512STodor Tomov 		break;
1021d30bb512STodor Tomov 	default:
1022d30bb512STodor Tomov 		ret = -EINVAL;
1023d30bb512STodor Tomov 		break;
1024d30bb512STodor Tomov 	}
1025d30bb512STodor Tomov 
1026207f4162SDaniel Scally 	pm_runtime_put(ov7251->dev);
1027207f4162SDaniel Scally 
1028d30bb512STodor Tomov 	return ret;
1029d30bb512STodor Tomov }
1030d30bb512STodor Tomov 
1031d30bb512STodor Tomov static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
1032d30bb512STodor Tomov 	.s_ctrl = ov7251_s_ctrl,
1033d30bb512STodor Tomov };
1034d30bb512STodor Tomov 
1035d30bb512STodor Tomov static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
10360d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
1037d30bb512STodor Tomov 				 struct v4l2_subdev_mbus_code_enum *code)
1038d30bb512STodor Tomov {
1039d30bb512STodor Tomov 	if (code->index > 0)
1040d30bb512STodor Tomov 		return -EINVAL;
1041d30bb512STodor Tomov 
1042d30bb512STodor Tomov 	code->code = MEDIA_BUS_FMT_Y10_1X10;
1043d30bb512STodor Tomov 
1044d30bb512STodor Tomov 	return 0;
1045d30bb512STodor Tomov }
1046d30bb512STodor Tomov 
1047d30bb512STodor Tomov static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
10480d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
1049d30bb512STodor Tomov 				  struct v4l2_subdev_frame_size_enum *fse)
1050d30bb512STodor Tomov {
1051d30bb512STodor Tomov 	if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
1052d30bb512STodor Tomov 		return -EINVAL;
1053d30bb512STodor Tomov 
1054d30bb512STodor Tomov 	if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
1055d30bb512STodor Tomov 		return -EINVAL;
1056d30bb512STodor Tomov 
1057d30bb512STodor Tomov 	fse->min_width = ov7251_mode_info_data[fse->index].width;
1058d30bb512STodor Tomov 	fse->max_width = ov7251_mode_info_data[fse->index].width;
1059d30bb512STodor Tomov 	fse->min_height = ov7251_mode_info_data[fse->index].height;
1060d30bb512STodor Tomov 	fse->max_height = ov7251_mode_info_data[fse->index].height;
1061d30bb512STodor Tomov 
1062d30bb512STodor Tomov 	return 0;
1063d30bb512STodor Tomov }
1064d30bb512STodor Tomov 
1065d30bb512STodor Tomov static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
10660d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
1067d30bb512STodor Tomov 				  struct v4l2_subdev_frame_interval_enum *fie)
1068d30bb512STodor Tomov {
1069d30bb512STodor Tomov 	unsigned int index = fie->index;
1070d30bb512STodor Tomov 	unsigned int i;
1071d30bb512STodor Tomov 
1072d30bb512STodor Tomov 	for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
1073d30bb512STodor Tomov 		if (fie->width != ov7251_mode_info_data[i].width ||
1074d30bb512STodor Tomov 		    fie->height != ov7251_mode_info_data[i].height)
1075d30bb512STodor Tomov 			continue;
1076d30bb512STodor Tomov 
1077d30bb512STodor Tomov 		if (index-- == 0) {
1078d30bb512STodor Tomov 			fie->interval = ov7251_mode_info_data[i].timeperframe;
1079d30bb512STodor Tomov 			return 0;
1080d30bb512STodor Tomov 		}
1081d30bb512STodor Tomov 	}
1082d30bb512STodor Tomov 
1083d30bb512STodor Tomov 	return -EINVAL;
1084d30bb512STodor Tomov }
1085d30bb512STodor Tomov 
1086d30bb512STodor Tomov static struct v4l2_mbus_framefmt *
1087d30bb512STodor Tomov __ov7251_get_pad_format(struct ov7251 *ov7251,
10880d346d2aSTomi Valkeinen 			struct v4l2_subdev_state *sd_state,
1089d30bb512STodor Tomov 			unsigned int pad,
1090d30bb512STodor Tomov 			enum v4l2_subdev_format_whence which)
1091d30bb512STodor Tomov {
1092d30bb512STodor Tomov 	switch (which) {
1093d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_TRY:
10940d346d2aSTomi Valkeinen 		return v4l2_subdev_get_try_format(&ov7251->sd, sd_state, pad);
1095d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_ACTIVE:
1096d30bb512STodor Tomov 		return &ov7251->fmt;
1097d30bb512STodor Tomov 	default:
1098d30bb512STodor Tomov 		return NULL;
1099d30bb512STodor Tomov 	}
1100d30bb512STodor Tomov }
1101d30bb512STodor Tomov 
1102d30bb512STodor Tomov static int ov7251_get_format(struct v4l2_subdev *sd,
11030d346d2aSTomi Valkeinen 			     struct v4l2_subdev_state *sd_state,
1104d30bb512STodor Tomov 			     struct v4l2_subdev_format *format)
1105d30bb512STodor Tomov {
1106d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1107d30bb512STodor Tomov 
1108d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
11090d346d2aSTomi Valkeinen 	format->format = *__ov7251_get_pad_format(ov7251, sd_state,
11100d346d2aSTomi Valkeinen 						  format->pad,
1111d30bb512STodor Tomov 						  format->which);
1112d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1113d30bb512STodor Tomov 
1114d30bb512STodor Tomov 	return 0;
1115d30bb512STodor Tomov }
1116d30bb512STodor Tomov 
1117d30bb512STodor Tomov static struct v4l2_rect *
11180d346d2aSTomi Valkeinen __ov7251_get_pad_crop(struct ov7251 *ov7251,
11190d346d2aSTomi Valkeinen 		      struct v4l2_subdev_state *sd_state,
1120d30bb512STodor Tomov 		      unsigned int pad, enum v4l2_subdev_format_whence which)
1121d30bb512STodor Tomov {
1122d30bb512STodor Tomov 	switch (which) {
1123d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_TRY:
11240d346d2aSTomi Valkeinen 		return v4l2_subdev_get_try_crop(&ov7251->sd, sd_state, pad);
1125d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_ACTIVE:
1126d30bb512STodor Tomov 		return &ov7251->crop;
1127d30bb512STodor Tomov 	default:
1128d30bb512STodor Tomov 		return NULL;
1129d30bb512STodor Tomov 	}
1130d30bb512STodor Tomov }
1131d30bb512STodor Tomov 
1132d30bb512STodor Tomov static inline u32 avg_fps(const struct v4l2_fract *t)
1133d30bb512STodor Tomov {
1134d30bb512STodor Tomov 	return (t->denominator + (t->numerator >> 1)) / t->numerator;
1135d30bb512STodor Tomov }
1136d30bb512STodor Tomov 
1137d30bb512STodor Tomov static const struct ov7251_mode_info *
1138d30bb512STodor Tomov ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
1139d30bb512STodor Tomov {
1140d30bb512STodor Tomov 	const struct ov7251_mode_info *mode = ov7251->current_mode;
1141d30bb512STodor Tomov 	unsigned int fps_req = avg_fps(timeperframe);
1142d30bb512STodor Tomov 	unsigned int max_dist_match = (unsigned int) -1;
1143d30bb512STodor Tomov 	unsigned int i, n = 0;
1144d30bb512STodor Tomov 
1145d30bb512STodor Tomov 	for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
1146d30bb512STodor Tomov 		unsigned int dist;
1147d30bb512STodor Tomov 		unsigned int fps_tmp;
1148d30bb512STodor Tomov 
1149d30bb512STodor Tomov 		if (mode->width != ov7251_mode_info_data[i].width ||
1150d30bb512STodor Tomov 		    mode->height != ov7251_mode_info_data[i].height)
1151d30bb512STodor Tomov 			continue;
1152d30bb512STodor Tomov 
1153d30bb512STodor Tomov 		fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
1154d30bb512STodor Tomov 
1155d30bb512STodor Tomov 		dist = abs(fps_req - fps_tmp);
1156d30bb512STodor Tomov 
1157d30bb512STodor Tomov 		if (dist < max_dist_match) {
1158d30bb512STodor Tomov 			n = i;
1159d30bb512STodor Tomov 			max_dist_match = dist;
1160d30bb512STodor Tomov 		}
1161d30bb512STodor Tomov 	}
1162d30bb512STodor Tomov 
1163d30bb512STodor Tomov 	return &ov7251_mode_info_data[n];
1164d30bb512STodor Tomov }
1165d30bb512STodor Tomov 
1166d30bb512STodor Tomov static int ov7251_set_format(struct v4l2_subdev *sd,
11670d346d2aSTomi Valkeinen 			     struct v4l2_subdev_state *sd_state,
1168d30bb512STodor Tomov 			     struct v4l2_subdev_format *format)
1169d30bb512STodor Tomov {
1170d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1171d30bb512STodor Tomov 	struct v4l2_mbus_framefmt *__format;
1172d30bb512STodor Tomov 	struct v4l2_rect *__crop;
1173d30bb512STodor Tomov 	const struct ov7251_mode_info *new_mode;
1174d30bb512STodor Tomov 	int ret = 0;
1175d30bb512STodor Tomov 
1176d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1177d30bb512STodor Tomov 
11780d346d2aSTomi Valkeinen 	__crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad,
11790d346d2aSTomi Valkeinen 				       format->which);
1180d30bb512STodor Tomov 
1181d30bb512STodor Tomov 	new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
1182d30bb512STodor Tomov 				ARRAY_SIZE(ov7251_mode_info_data),
1183d30bb512STodor Tomov 				width, height,
1184d30bb512STodor Tomov 				format->format.width, format->format.height);
1185d30bb512STodor Tomov 
1186d30bb512STodor Tomov 	__crop->width = new_mode->width;
1187d30bb512STodor Tomov 	__crop->height = new_mode->height;
1188d30bb512STodor Tomov 
1189d30bb512STodor Tomov 	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1190d30bb512STodor Tomov 		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
1191d30bb512STodor Tomov 					       1, new_mode->exposure_max,
1192d30bb512STodor Tomov 					       1, new_mode->exposure_def);
1193d30bb512STodor Tomov 		if (ret < 0)
1194d30bb512STodor Tomov 			goto exit;
1195d30bb512STodor Tomov 
1196d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
1197d30bb512STodor Tomov 					 new_mode->exposure_def);
1198d30bb512STodor Tomov 		if (ret < 0)
1199d30bb512STodor Tomov 			goto exit;
1200d30bb512STodor Tomov 
1201d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
1202d30bb512STodor Tomov 		if (ret < 0)
1203d30bb512STodor Tomov 			goto exit;
1204d30bb512STodor Tomov 
1205d30bb512STodor Tomov 		ov7251->current_mode = new_mode;
1206d30bb512STodor Tomov 	}
1207d30bb512STodor Tomov 
12080d346d2aSTomi Valkeinen 	__format = __ov7251_get_pad_format(ov7251, sd_state, format->pad,
1209d30bb512STodor Tomov 					   format->which);
1210d30bb512STodor Tomov 	__format->width = __crop->width;
1211d30bb512STodor Tomov 	__format->height = __crop->height;
1212d30bb512STodor Tomov 	__format->code = MEDIA_BUS_FMT_Y10_1X10;
1213d30bb512STodor Tomov 	__format->field = V4L2_FIELD_NONE;
1214d30bb512STodor Tomov 	__format->colorspace = V4L2_COLORSPACE_SRGB;
1215d30bb512STodor Tomov 	__format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
1216d30bb512STodor Tomov 	__format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1217d30bb512STodor Tomov 				__format->colorspace, __format->ycbcr_enc);
1218d30bb512STodor Tomov 	__format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
1219d30bb512STodor Tomov 
1220d30bb512STodor Tomov 	format->format = *__format;
1221d30bb512STodor Tomov 
1222d30bb512STodor Tomov exit:
1223d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1224d30bb512STodor Tomov 
1225d30bb512STodor Tomov 	return ret;
1226d30bb512STodor Tomov }
1227d30bb512STodor Tomov 
1228d30bb512STodor Tomov static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev,
12290d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state)
1230d30bb512STodor Tomov {
1231d30bb512STodor Tomov 	struct v4l2_subdev_format fmt = {
12320d346d2aSTomi Valkeinen 		.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
1233d30bb512STodor Tomov 		: V4L2_SUBDEV_FORMAT_ACTIVE,
1234d30bb512STodor Tomov 		.format = {
1235d30bb512STodor Tomov 			.width = 640,
1236d30bb512STodor Tomov 			.height = 480
1237d30bb512STodor Tomov 		}
1238d30bb512STodor Tomov 	};
1239d30bb512STodor Tomov 
12400d346d2aSTomi Valkeinen 	ov7251_set_format(subdev, sd_state, &fmt);
1241d30bb512STodor Tomov 
1242d30bb512STodor Tomov 	return 0;
1243d30bb512STodor Tomov }
1244d30bb512STodor Tomov 
1245d30bb512STodor Tomov static int ov7251_get_selection(struct v4l2_subdev *sd,
12460d346d2aSTomi Valkeinen 				struct v4l2_subdev_state *sd_state,
1247d30bb512STodor Tomov 				struct v4l2_subdev_selection *sel)
1248d30bb512STodor Tomov {
1249d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1250d30bb512STodor Tomov 
1251d30bb512STodor Tomov 	if (sel->target != V4L2_SEL_TGT_CROP)
1252d30bb512STodor Tomov 		return -EINVAL;
1253d30bb512STodor Tomov 
1254d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
12550d346d2aSTomi Valkeinen 	sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad,
1256d30bb512STodor Tomov 					sel->which);
1257d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1258d30bb512STodor Tomov 
1259d30bb512STodor Tomov 	return 0;
1260d30bb512STodor Tomov }
1261d30bb512STodor Tomov 
1262d30bb512STodor Tomov static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
1263d30bb512STodor Tomov {
1264d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1265d30bb512STodor Tomov 	int ret;
1266d30bb512STodor Tomov 
1267d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1268d30bb512STodor Tomov 
1269d30bb512STodor Tomov 	if (enable) {
1270207f4162SDaniel Scally 		ret = pm_runtime_get_sync(ov7251->dev);
1271207f4162SDaniel Scally 		if (ret < 0)
1272207f4162SDaniel Scally 			goto unlock_out;
1273207f4162SDaniel Scally 
1274df057b0dSDaniel Scally 		ret = ov7251_pll_configure(ov7251);
1275207f4162SDaniel Scally 		if (ret) {
1276207f4162SDaniel Scally 			dev_err(ov7251->dev, "error configuring PLLs\n");
1277207f4162SDaniel Scally 			goto err_power_down;
1278207f4162SDaniel Scally 		}
1279df057b0dSDaniel Scally 
1280d30bb512STodor Tomov 		ret = ov7251_set_register_array(ov7251,
1281d30bb512STodor Tomov 					ov7251->current_mode->data,
1282d30bb512STodor Tomov 					ov7251->current_mode->data_size);
1283d30bb512STodor Tomov 		if (ret < 0) {
1284d30bb512STodor Tomov 			dev_err(ov7251->dev, "could not set mode %dx%d\n",
1285d30bb512STodor Tomov 				ov7251->current_mode->width,
1286d30bb512STodor Tomov 				ov7251->current_mode->height);
1287207f4162SDaniel Scally 			goto err_power_down;
1288d30bb512STodor Tomov 		}
1289d30bb512STodor Tomov 		ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
1290d30bb512STodor Tomov 		if (ret < 0) {
1291d30bb512STodor Tomov 			dev_err(ov7251->dev, "could not sync v4l2 controls\n");
1292207f4162SDaniel Scally 			goto err_power_down;
1293d30bb512STodor Tomov 		}
1294d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1295d30bb512STodor Tomov 				       OV7251_SC_MODE_SELECT_STREAMING);
1296207f4162SDaniel Scally 		if (ret)
1297207f4162SDaniel Scally 			goto err_power_down;
1298d30bb512STodor Tomov 	} else {
1299d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1300d30bb512STodor Tomov 				       OV7251_SC_MODE_SELECT_SW_STANDBY);
1301207f4162SDaniel Scally 		pm_runtime_put(ov7251->dev);
1302d30bb512STodor Tomov 	}
1303d30bb512STodor Tomov 
1304207f4162SDaniel Scally unlock_out:
1305d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1306207f4162SDaniel Scally 	return ret;
1307d30bb512STodor Tomov 
1308207f4162SDaniel Scally err_power_down:
1309207f4162SDaniel Scally 	pm_runtime_put_noidle(ov7251->dev);
1310d30bb512STodor Tomov 	return ret;
1311d30bb512STodor Tomov }
1312d30bb512STodor Tomov 
1313d30bb512STodor Tomov static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
1314d30bb512STodor Tomov 				     struct v4l2_subdev_frame_interval *fi)
1315d30bb512STodor Tomov {
1316d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1317d30bb512STodor Tomov 
1318d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1319d30bb512STodor Tomov 	fi->interval = ov7251->current_mode->timeperframe;
1320d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1321d30bb512STodor Tomov 
1322d30bb512STodor Tomov 	return 0;
1323d30bb512STodor Tomov }
1324d30bb512STodor Tomov 
1325d30bb512STodor Tomov static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
1326d30bb512STodor Tomov 				     struct v4l2_subdev_frame_interval *fi)
1327d30bb512STodor Tomov {
1328d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1329d30bb512STodor Tomov 	const struct ov7251_mode_info *new_mode;
1330d30bb512STodor Tomov 	int ret = 0;
1331d30bb512STodor Tomov 
1332d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1333d30bb512STodor Tomov 	new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
1334d30bb512STodor Tomov 
1335d30bb512STodor Tomov 	if (new_mode != ov7251->current_mode) {
1336d30bb512STodor Tomov 		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
1337d30bb512STodor Tomov 					       1, new_mode->exposure_max,
1338d30bb512STodor Tomov 					       1, new_mode->exposure_def);
1339d30bb512STodor Tomov 		if (ret < 0)
1340d30bb512STodor Tomov 			goto exit;
1341d30bb512STodor Tomov 
1342d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
1343d30bb512STodor Tomov 					 new_mode->exposure_def);
1344d30bb512STodor Tomov 		if (ret < 0)
1345d30bb512STodor Tomov 			goto exit;
1346d30bb512STodor Tomov 
1347d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
1348d30bb512STodor Tomov 		if (ret < 0)
1349d30bb512STodor Tomov 			goto exit;
1350d30bb512STodor Tomov 
1351d30bb512STodor Tomov 		ov7251->current_mode = new_mode;
1352d30bb512STodor Tomov 	}
1353d30bb512STodor Tomov 
1354d30bb512STodor Tomov 	fi->interval = ov7251->current_mode->timeperframe;
1355d30bb512STodor Tomov 
1356d30bb512STodor Tomov exit:
1357d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1358d30bb512STodor Tomov 
1359d30bb512STodor Tomov 	return ret;
1360d30bb512STodor Tomov }
1361d30bb512STodor Tomov 
1362d30bb512STodor Tomov static const struct v4l2_subdev_video_ops ov7251_video_ops = {
1363d30bb512STodor Tomov 	.s_stream = ov7251_s_stream,
1364d30bb512STodor Tomov 	.g_frame_interval = ov7251_get_frame_interval,
1365d30bb512STodor Tomov 	.s_frame_interval = ov7251_set_frame_interval,
1366d30bb512STodor Tomov };
1367d30bb512STodor Tomov 
1368d30bb512STodor Tomov static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
1369d30bb512STodor Tomov 	.init_cfg = ov7251_entity_init_cfg,
1370d30bb512STodor Tomov 	.enum_mbus_code = ov7251_enum_mbus_code,
1371d30bb512STodor Tomov 	.enum_frame_size = ov7251_enum_frame_size,
1372d30bb512STodor Tomov 	.enum_frame_interval = ov7251_enum_frame_ival,
1373d30bb512STodor Tomov 	.get_fmt = ov7251_get_format,
1374d30bb512STodor Tomov 	.set_fmt = ov7251_set_format,
1375d30bb512STodor Tomov 	.get_selection = ov7251_get_selection,
1376d30bb512STodor Tomov };
1377d30bb512STodor Tomov 
1378d30bb512STodor Tomov static const struct v4l2_subdev_ops ov7251_subdev_ops = {
1379d30bb512STodor Tomov 	.video = &ov7251_video_ops,
1380d30bb512STodor Tomov 	.pad = &ov7251_subdev_pad_ops,
1381d30bb512STodor Tomov };
1382d30bb512STodor Tomov 
1383cc125aaaSDaniel Scally static int ov7251_check_hwcfg(struct ov7251 *ov7251)
1384cc125aaaSDaniel Scally {
1385cc125aaaSDaniel Scally 	struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev);
1386cc125aaaSDaniel Scally 	struct v4l2_fwnode_endpoint bus_cfg = {
1387cc125aaaSDaniel Scally 		.bus_type = V4L2_MBUS_CSI2_DPHY,
1388cc125aaaSDaniel Scally 	};
1389cc125aaaSDaniel Scally 	struct fwnode_handle *endpoint;
1390cc125aaaSDaniel Scally 	unsigned int i, j;
1391cc125aaaSDaniel Scally 	int ret;
1392cc125aaaSDaniel Scally 
1393cc125aaaSDaniel Scally 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
1394cc125aaaSDaniel Scally 	if (!endpoint)
1395cc125aaaSDaniel Scally 		return -EPROBE_DEFER; /* could be provided by cio2-bridge */
1396cc125aaaSDaniel Scally 
1397cc125aaaSDaniel Scally 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
1398cc125aaaSDaniel Scally 	fwnode_handle_put(endpoint);
1399cc125aaaSDaniel Scally 	if (ret)
1400cc125aaaSDaniel Scally 		return dev_err_probe(ov7251->dev, ret,
1401cc125aaaSDaniel Scally 				     "parsing endpoint node failed\n");
1402cc125aaaSDaniel Scally 
1403cc125aaaSDaniel Scally 	if (!bus_cfg.nr_of_link_frequencies) {
1404cc125aaaSDaniel Scally 		ret = dev_err_probe(ov7251->dev, -EINVAL,
1405cc125aaaSDaniel Scally 				    "no link frequencies defined\n");
1406cc125aaaSDaniel Scally 		goto out_free_bus_cfg;
1407cc125aaaSDaniel Scally 	}
1408cc125aaaSDaniel Scally 
1409cc125aaaSDaniel Scally 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
1410cc125aaaSDaniel Scally 		for (j = 0; j < ARRAY_SIZE(link_freq); j++)
1411cc125aaaSDaniel Scally 			if (bus_cfg.link_frequencies[i] == link_freq[j])
1412cc125aaaSDaniel Scally 				break;
1413cc125aaaSDaniel Scally 
1414cc125aaaSDaniel Scally 		if (j < ARRAY_SIZE(link_freq))
1415cc125aaaSDaniel Scally 			break;
1416cc125aaaSDaniel Scally 	}
1417cc125aaaSDaniel Scally 
1418cc125aaaSDaniel Scally 	if (i == bus_cfg.nr_of_link_frequencies) {
1419cc125aaaSDaniel Scally 		ret = dev_err_probe(ov7251->dev, -EINVAL,
1420cc125aaaSDaniel Scally 				    "no supported link freq found\n");
1421cc125aaaSDaniel Scally 		goto out_free_bus_cfg;
1422cc125aaaSDaniel Scally 	}
1423cc125aaaSDaniel Scally 
1424cc125aaaSDaniel Scally 	ov7251->link_freq_idx = i;
1425cc125aaaSDaniel Scally 
1426cc125aaaSDaniel Scally out_free_bus_cfg:
1427cc125aaaSDaniel Scally 	v4l2_fwnode_endpoint_free(&bus_cfg);
1428cc125aaaSDaniel Scally 
1429cc125aaaSDaniel Scally 	return ret;
1430cc125aaaSDaniel Scally }
1431cc125aaaSDaniel Scally 
1432e92932c3SDaniel Scally static int ov7251_detect_chip(struct ov7251 *ov7251)
1433e92932c3SDaniel Scally {
1434e92932c3SDaniel Scally 	u8 chip_id_high, chip_id_low, chip_rev;
1435e92932c3SDaniel Scally 	int ret;
1436e92932c3SDaniel Scally 
1437e92932c3SDaniel Scally 	ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
1438e92932c3SDaniel Scally 	if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE)
1439e92932c3SDaniel Scally 		return dev_err_probe(ov7251->dev, -ENODEV,
1440e92932c3SDaniel Scally 				     "could not read ID high\n");
1441e92932c3SDaniel Scally 
1442e92932c3SDaniel Scally 	ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
1443e92932c3SDaniel Scally 	if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE)
1444e92932c3SDaniel Scally 		return dev_err_probe(ov7251->dev, -ENODEV,
1445e92932c3SDaniel Scally 				     "could not read ID low\n");
1446e92932c3SDaniel Scally 
1447e92932c3SDaniel Scally 	ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
1448e92932c3SDaniel Scally 	if (ret < 0)
1449e92932c3SDaniel Scally 		return dev_err_probe(ov7251->dev, -ENODEV,
1450e92932c3SDaniel Scally 				     "could not read revision\n");
1451e92932c3SDaniel Scally 	chip_rev >>= 4;
1452e92932c3SDaniel Scally 
1453e92932c3SDaniel Scally 	dev_info(ov7251->dev,
1454e92932c3SDaniel Scally 		 "OV7251 revision %x (%s) detected at address 0x%02x\n",
1455e92932c3SDaniel Scally 		 chip_rev,
1456e92932c3SDaniel Scally 		 chip_rev == 0x4 ? "1A / 1B" :
1457e92932c3SDaniel Scally 		 chip_rev == 0x5 ? "1C / 1D" :
1458e92932c3SDaniel Scally 		 chip_rev == 0x6 ? "1E" :
1459e92932c3SDaniel Scally 		 chip_rev == 0x7 ? "1F" : "unknown",
1460e92932c3SDaniel Scally 		 ov7251->i2c_client->addr);
1461e92932c3SDaniel Scally 
1462e92932c3SDaniel Scally 	return 0;
1463e92932c3SDaniel Scally }
1464e92932c3SDaniel Scally 
1465d30bb512STodor Tomov static int ov7251_probe(struct i2c_client *client)
1466d30bb512STodor Tomov {
1467d30bb512STodor Tomov 	struct device *dev = &client->dev;
1468d30bb512STodor Tomov 	struct ov7251 *ov7251;
1469ed9566ceSDaniel Scally 	unsigned int rate = 0, clk_rate = 0;
14701757b44eSDaniel Scally 	s64 pixel_rate;
1471d30bb512STodor Tomov 	int ret;
1472df057b0dSDaniel Scally 	int i;
1473d30bb512STodor Tomov 
1474d30bb512STodor Tomov 	ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
1475d30bb512STodor Tomov 	if (!ov7251)
1476d30bb512STodor Tomov 		return -ENOMEM;
1477d30bb512STodor Tomov 
1478d30bb512STodor Tomov 	ov7251->i2c_client = client;
1479d30bb512STodor Tomov 	ov7251->dev = dev;
1480d30bb512STodor Tomov 
1481cc125aaaSDaniel Scally 	ret = ov7251_check_hwcfg(ov7251);
1482cc125aaaSDaniel Scally 	if (ret)
1483d30bb512STodor Tomov 		return ret;
1484d30bb512STodor Tomov 
1485d30bb512STodor Tomov 	/* get system clock (xclk) */
1486ed9566ceSDaniel Scally 	ov7251->xclk = devm_clk_get_optional(dev, NULL);
1487ed9566ceSDaniel Scally 	if (IS_ERR(ov7251->xclk))
1488ed9566ceSDaniel Scally 		return dev_err_probe(dev, PTR_ERR(ov7251->xclk),
1489ed9566ceSDaniel Scally 				     "could not get xclk");
1490d30bb512STodor Tomov 
1491ed9566ceSDaniel Scally 	/*
1492ed9566ceSDaniel Scally 	 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
1493ed9566ceSDaniel Scally 	 * ACPI. We also need to support the IPU3 case which will have both an
1494ed9566ceSDaniel Scally 	 * external clock AND a clock-frequency property.
1495ed9566ceSDaniel Scally 	 */
1496d30bb512STodor Tomov 	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1497ed9566ceSDaniel Scally 				       &rate);
1498ed9566ceSDaniel Scally 	if (ret && !ov7251->xclk)
1499ed9566ceSDaniel Scally 		return dev_err_probe(dev, ret, "invalid clock config\n");
1500ed9566ceSDaniel Scally 
1501ed9566ceSDaniel Scally 	clk_rate = clk_get_rate(ov7251->xclk);
1502ed9566ceSDaniel Scally 	ov7251->xclk_freq = clk_rate ? clk_rate : rate;
1503ed9566ceSDaniel Scally 
1504ed9566ceSDaniel Scally 	if (ov7251->xclk_freq == 0)
1505ed9566ceSDaniel Scally 		return dev_err_probe(dev, -EINVAL, "invalid clock frequency\n");
1506ed9566ceSDaniel Scally 
1507ed9566ceSDaniel Scally 	if (!ret && ov7251->xclk) {
1508ed9566ceSDaniel Scally 		ret = clk_set_rate(ov7251->xclk, rate);
1509ed9566ceSDaniel Scally 		if (ret)
1510ed9566ceSDaniel Scally 			return dev_err_probe(dev, ret,
1511ed9566ceSDaniel Scally 					     "failed to set clock rate\n");
1512d30bb512STodor Tomov 	}
1513d30bb512STodor Tomov 
1514df057b0dSDaniel Scally 	for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++)
1515df057b0dSDaniel Scally 		if (ov7251->xclk_freq == supported_xclk_rates[i])
1516df057b0dSDaniel Scally 			break;
1517df057b0dSDaniel Scally 
1518df057b0dSDaniel Scally 	if (i == ARRAY_SIZE(supported_xclk_rates))
1519df057b0dSDaniel Scally 		return dev_err_probe(dev, -EINVAL,
1520df057b0dSDaniel Scally 				     "clock rate %u Hz is unsupported\n",
1521df057b0dSDaniel Scally 				     ov7251->xclk_freq);
1522df057b0dSDaniel Scally 
1523df057b0dSDaniel Scally 	ov7251->pll_cfgs = ov7251_pll_cfgs[i];
1524d30bb512STodor Tomov 
1525d30bb512STodor Tomov 	ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
1526d30bb512STodor Tomov 	if (IS_ERR(ov7251->io_regulator)) {
1527d30bb512STodor Tomov 		dev_err(dev, "cannot get io regulator\n");
1528d30bb512STodor Tomov 		return PTR_ERR(ov7251->io_regulator);
1529d30bb512STodor Tomov 	}
1530d30bb512STodor Tomov 
1531d30bb512STodor Tomov 	ov7251->core_regulator = devm_regulator_get(dev, "vddd");
1532d30bb512STodor Tomov 	if (IS_ERR(ov7251->core_regulator)) {
1533d30bb512STodor Tomov 		dev_err(dev, "cannot get core regulator\n");
1534d30bb512STodor Tomov 		return PTR_ERR(ov7251->core_regulator);
1535d30bb512STodor Tomov 	}
1536d30bb512STodor Tomov 
1537d30bb512STodor Tomov 	ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
1538d30bb512STodor Tomov 	if (IS_ERR(ov7251->analog_regulator)) {
1539d30bb512STodor Tomov 		dev_err(dev, "cannot get analog regulator\n");
1540d30bb512STodor Tomov 		return PTR_ERR(ov7251->analog_regulator);
1541d30bb512STodor Tomov 	}
1542d30bb512STodor Tomov 
1543d30bb512STodor Tomov 	ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1544d30bb512STodor Tomov 	if (IS_ERR(ov7251->enable_gpio)) {
1545d30bb512STodor Tomov 		dev_err(dev, "cannot get enable gpio\n");
1546d30bb512STodor Tomov 		return PTR_ERR(ov7251->enable_gpio);
1547d30bb512STodor Tomov 	}
1548d30bb512STodor Tomov 
1549d30bb512STodor Tomov 	mutex_init(&ov7251->lock);
1550d30bb512STodor Tomov 
1551d30bb512STodor Tomov 	v4l2_ctrl_handler_init(&ov7251->ctrls, 7);
1552d30bb512STodor Tomov 	ov7251->ctrls.lock = &ov7251->lock;
1553d30bb512STodor Tomov 
1554d30bb512STodor Tomov 	v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1555d30bb512STodor Tomov 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
1556d30bb512STodor Tomov 	v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1557d30bb512STodor Tomov 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
1558d30bb512STodor Tomov 	ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1559d30bb512STodor Tomov 					     V4L2_CID_EXPOSURE, 1, 32, 1, 32);
1560d30bb512STodor Tomov 	ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1561d30bb512STodor Tomov 					 V4L2_CID_GAIN, 16, 1023, 1, 16);
1562d30bb512STodor Tomov 	v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
1563d30bb512STodor Tomov 				     V4L2_CID_TEST_PATTERN,
1564d30bb512STodor Tomov 				     ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
1565d30bb512STodor Tomov 				     0, 0, ov7251_test_pattern_menu);
15661757b44eSDaniel Scally 
15671757b44eSDaniel Scally 	pixel_rate = pixel_rates[ov7251->link_freq_idx];
1568d30bb512STodor Tomov 	ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls,
1569d30bb512STodor Tomov 						&ov7251_ctrl_ops,
1570d30bb512STodor Tomov 						V4L2_CID_PIXEL_RATE,
15711757b44eSDaniel Scally 						pixel_rate, INT_MAX,
15721757b44eSDaniel Scally 						pixel_rate, pixel_rate);
1573d30bb512STodor Tomov 	ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls,
1574d30bb512STodor Tomov 						   &ov7251_ctrl_ops,
1575d30bb512STodor Tomov 						   V4L2_CID_LINK_FREQ,
1576d30bb512STodor Tomov 						   ARRAY_SIZE(link_freq) - 1,
15771757b44eSDaniel Scally 						   ov7251->link_freq_idx,
15781757b44eSDaniel Scally 						   link_freq);
1579d30bb512STodor Tomov 	if (ov7251->link_freq)
1580d30bb512STodor Tomov 		ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
15811757b44eSDaniel Scally 	if (ov7251->pixel_clock)
15821757b44eSDaniel Scally 		ov7251->pixel_clock->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1583d30bb512STodor Tomov 
1584d30bb512STodor Tomov 	ov7251->sd.ctrl_handler = &ov7251->ctrls;
1585d30bb512STodor Tomov 
1586d30bb512STodor Tomov 	if (ov7251->ctrls.error) {
1587d30bb512STodor Tomov 		dev_err(dev, "%s: control initialization error %d\n",
1588d30bb512STodor Tomov 			__func__, ov7251->ctrls.error);
1589d30bb512STodor Tomov 		ret = ov7251->ctrls.error;
1590d30bb512STodor Tomov 		goto free_ctrl;
1591d30bb512STodor Tomov 	}
1592d30bb512STodor Tomov 
1593d30bb512STodor Tomov 	v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
1594d30bb512STodor Tomov 	ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1595d30bb512STodor Tomov 	ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
1596d30bb512STodor Tomov 	ov7251->sd.dev = &client->dev;
1597d30bb512STodor Tomov 	ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1598d30bb512STodor Tomov 
1599d30bb512STodor Tomov 	ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
1600d30bb512STodor Tomov 	if (ret < 0) {
1601d30bb512STodor Tomov 		dev_err(dev, "could not register media entity\n");
1602d30bb512STodor Tomov 		goto free_ctrl;
1603d30bb512STodor Tomov 	}
1604d30bb512STodor Tomov 
1605207f4162SDaniel Scally 	ret = ov7251_set_power_on(ov7251->dev);
1606207f4162SDaniel Scally 	if (ret)
1607d30bb512STodor Tomov 		goto free_entity;
1608d30bb512STodor Tomov 
1609e92932c3SDaniel Scally 	ret = ov7251_detect_chip(ov7251);
1610e92932c3SDaniel Scally 	if (ret)
1611d30bb512STodor Tomov 		goto power_down;
1612d30bb512STodor Tomov 
1613207f4162SDaniel Scally 	pm_runtime_set_active(&client->dev);
1614207f4162SDaniel Scally 	pm_runtime_get_noresume(&client->dev);
1615207f4162SDaniel Scally 	pm_runtime_enable(&client->dev);
1616d30bb512STodor Tomov 
1617d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
1618d30bb512STodor Tomov 			      &ov7251->pre_isp_00);
1619d30bb512STodor Tomov 	if (ret < 0) {
1620d30bb512STodor Tomov 		dev_err(dev, "could not read test pattern value\n");
1621d30bb512STodor Tomov 		ret = -ENODEV;
1622207f4162SDaniel Scally 		goto err_pm_runtime;
1623d30bb512STodor Tomov 	}
1624d30bb512STodor Tomov 
1625d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
1626d30bb512STodor Tomov 			      &ov7251->timing_format1);
1627d30bb512STodor Tomov 	if (ret < 0) {
1628d30bb512STodor Tomov 		dev_err(dev, "could not read vflip value\n");
1629d30bb512STodor Tomov 		ret = -ENODEV;
1630207f4162SDaniel Scally 		goto err_pm_runtime;
1631d30bb512STodor Tomov 	}
1632d30bb512STodor Tomov 
1633d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
1634d30bb512STodor Tomov 			      &ov7251->timing_format2);
1635d30bb512STodor Tomov 	if (ret < 0) {
1636d30bb512STodor Tomov 		dev_err(dev, "could not read hflip value\n");
1637d30bb512STodor Tomov 		ret = -ENODEV;
1638207f4162SDaniel Scally 		goto err_pm_runtime;
1639d30bb512STodor Tomov 	}
1640d30bb512STodor Tomov 
1641207f4162SDaniel Scally 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1642207f4162SDaniel Scally 	pm_runtime_use_autosuspend(&client->dev);
1643207f4162SDaniel Scally 	pm_runtime_put_autosuspend(&client->dev);
1644d30bb512STodor Tomov 
1645d30bb512STodor Tomov 	ret = v4l2_async_register_subdev(&ov7251->sd);
1646d30bb512STodor Tomov 	if (ret < 0) {
1647d30bb512STodor Tomov 		dev_err(dev, "could not register v4l2 device\n");
1648d30bb512STodor Tomov 		goto free_entity;
1649d30bb512STodor Tomov 	}
1650d30bb512STodor Tomov 
1651d30bb512STodor Tomov 	ov7251_entity_init_cfg(&ov7251->sd, NULL);
1652d30bb512STodor Tomov 
1653d30bb512STodor Tomov 	return 0;
1654d30bb512STodor Tomov 
1655207f4162SDaniel Scally err_pm_runtime:
1656207f4162SDaniel Scally 	pm_runtime_disable(ov7251->dev);
1657207f4162SDaniel Scally 	pm_runtime_put_noidle(ov7251->dev);
1658d30bb512STodor Tomov power_down:
1659207f4162SDaniel Scally 	ov7251_set_power_off(ov7251->dev);
1660d30bb512STodor Tomov free_entity:
1661d30bb512STodor Tomov 	media_entity_cleanup(&ov7251->sd.entity);
1662d30bb512STodor Tomov free_ctrl:
1663d30bb512STodor Tomov 	v4l2_ctrl_handler_free(&ov7251->ctrls);
1664d30bb512STodor Tomov 	mutex_destroy(&ov7251->lock);
1665d30bb512STodor Tomov 
1666d30bb512STodor Tomov 	return ret;
1667d30bb512STodor Tomov }
1668d30bb512STodor Tomov 
1669d30bb512STodor Tomov static int ov7251_remove(struct i2c_client *client)
1670d30bb512STodor Tomov {
1671d30bb512STodor Tomov 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1672d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1673d30bb512STodor Tomov 
1674d30bb512STodor Tomov 	v4l2_async_unregister_subdev(&ov7251->sd);
1675d30bb512STodor Tomov 	media_entity_cleanup(&ov7251->sd.entity);
1676d30bb512STodor Tomov 	v4l2_ctrl_handler_free(&ov7251->ctrls);
1677d30bb512STodor Tomov 	mutex_destroy(&ov7251->lock);
1678d30bb512STodor Tomov 
1679207f4162SDaniel Scally 	pm_runtime_disable(ov7251->dev);
1680207f4162SDaniel Scally 	if (!pm_runtime_status_suspended(ov7251->dev))
1681207f4162SDaniel Scally 		ov7251_set_power_off(ov7251->dev);
1682207f4162SDaniel Scally 	pm_runtime_set_suspended(ov7251->dev);
1683207f4162SDaniel Scally 
1684d30bb512STodor Tomov 	return 0;
1685d30bb512STodor Tomov }
1686d30bb512STodor Tomov 
1687207f4162SDaniel Scally static const struct dev_pm_ops ov7251_pm_ops = {
1688207f4162SDaniel Scally 	SET_RUNTIME_PM_OPS(ov7251_set_power_off, ov7251_set_power_on, NULL)
1689207f4162SDaniel Scally };
1690207f4162SDaniel Scally 
1691d30bb512STodor Tomov static const struct of_device_id ov7251_of_match[] = {
1692d30bb512STodor Tomov 	{ .compatible = "ovti,ov7251" },
1693d30bb512STodor Tomov 	{ /* sentinel */ }
1694d30bb512STodor Tomov };
1695d30bb512STodor Tomov MODULE_DEVICE_TABLE(of, ov7251_of_match);
1696d30bb512STodor Tomov 
16976766cff6SDaniel Scally static const struct acpi_device_id ov7251_acpi_match[] = {
16986766cff6SDaniel Scally 	{ "INT347E" },
16996766cff6SDaniel Scally 	{ }
17006766cff6SDaniel Scally };
17016766cff6SDaniel Scally MODULE_DEVICE_TABLE(acpi, ov7251_acpi_match);
17026766cff6SDaniel Scally 
1703d30bb512STodor Tomov static struct i2c_driver ov7251_i2c_driver = {
1704d30bb512STodor Tomov 	.driver = {
1705d30bb512STodor Tomov 		.of_match_table = ov7251_of_match,
17066766cff6SDaniel Scally 		.acpi_match_table = ov7251_acpi_match,
1707d30bb512STodor Tomov 		.name  = "ov7251",
1708207f4162SDaniel Scally 		.pm = &ov7251_pm_ops,
1709d30bb512STodor Tomov 	},
1710d30bb512STodor Tomov 	.probe_new  = ov7251_probe,
1711d30bb512STodor Tomov 	.remove = ov7251_remove,
1712d30bb512STodor Tomov };
1713d30bb512STodor Tomov 
1714d30bb512STodor Tomov module_i2c_driver(ov7251_i2c_driver);
1715d30bb512STodor Tomov 
1716d30bb512STodor Tomov MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
1717d30bb512STodor Tomov MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1718d30bb512STodor Tomov MODULE_LICENSE("GPL v2");
1719