xref: /linux/drivers/media/i2c/ov7251.c (revision 0d346d2a6f54f06f36b224fd27cd6eafe8c83be9)
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>
17d30bb512STodor Tomov #include <linux/regulator/consumer.h>
18d30bb512STodor Tomov #include <linux/slab.h>
19d30bb512STodor Tomov #include <linux/types.h>
20d30bb512STodor Tomov #include <media/v4l2-ctrls.h>
21d30bb512STodor Tomov #include <media/v4l2-fwnode.h>
22d30bb512STodor Tomov #include <media/v4l2-subdev.h>
23d30bb512STodor Tomov 
24d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT		0x0100
25d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_SW_STANDBY	0x0
26d30bb512STodor Tomov #define OV7251_SC_MODE_SELECT_STREAMING		0x1
27d30bb512STodor Tomov 
28d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH		0x300a
29d30bb512STodor Tomov #define OV7251_CHIP_ID_HIGH_BYTE	0x77
30d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW		0x300b
31d30bb512STodor Tomov #define OV7251_CHIP_ID_LOW_BYTE		0x50
32d30bb512STodor Tomov #define OV7251_SC_GP_IO_IN1		0x3029
33d30bb512STodor Tomov #define OV7251_AEC_EXPO_0		0x3500
34d30bb512STodor Tomov #define OV7251_AEC_EXPO_1		0x3501
35d30bb512STodor Tomov #define OV7251_AEC_EXPO_2		0x3502
36d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_0		0x350a
37d30bb512STodor Tomov #define OV7251_AEC_AGC_ADJ_1		0x350b
38d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1		0x3820
39d30bb512STodor Tomov #define OV7251_TIMING_FORMAT1_VFLIP	BIT(2)
40d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2		0x3821
41d30bb512STodor Tomov #define OV7251_TIMING_FORMAT2_MIRROR	BIT(2)
42d30bb512STodor Tomov #define OV7251_PRE_ISP_00		0x5e00
43d30bb512STodor Tomov #define OV7251_PRE_ISP_00_TEST_PATTERN	BIT(7)
44d30bb512STodor Tomov 
45d30bb512STodor Tomov struct reg_value {
46d30bb512STodor Tomov 	u16 reg;
47d30bb512STodor Tomov 	u8 val;
48d30bb512STodor Tomov };
49d30bb512STodor Tomov 
50d30bb512STodor Tomov struct ov7251_mode_info {
51d30bb512STodor Tomov 	u32 width;
52d30bb512STodor Tomov 	u32 height;
53d30bb512STodor Tomov 	const struct reg_value *data;
54d30bb512STodor Tomov 	u32 data_size;
55d30bb512STodor Tomov 	u32 pixel_clock;
56d30bb512STodor Tomov 	u32 link_freq;
57d30bb512STodor Tomov 	u16 exposure_max;
58d30bb512STodor Tomov 	u16 exposure_def;
59d30bb512STodor Tomov 	struct v4l2_fract timeperframe;
60d30bb512STodor Tomov };
61d30bb512STodor Tomov 
62d30bb512STodor Tomov struct ov7251 {
63d30bb512STodor Tomov 	struct i2c_client *i2c_client;
64d30bb512STodor Tomov 	struct device *dev;
65d30bb512STodor Tomov 	struct v4l2_subdev sd;
66d30bb512STodor Tomov 	struct media_pad pad;
67d30bb512STodor Tomov 	struct v4l2_fwnode_endpoint ep;
68d30bb512STodor Tomov 	struct v4l2_mbus_framefmt fmt;
69d30bb512STodor Tomov 	struct v4l2_rect crop;
70d30bb512STodor Tomov 	struct clk *xclk;
71d30bb512STodor Tomov 	u32 xclk_freq;
72d30bb512STodor Tomov 
73d30bb512STodor Tomov 	struct regulator *io_regulator;
74d30bb512STodor Tomov 	struct regulator *core_regulator;
75d30bb512STodor Tomov 	struct regulator *analog_regulator;
76d30bb512STodor Tomov 
77d30bb512STodor Tomov 	const struct ov7251_mode_info *current_mode;
78d30bb512STodor Tomov 
79d30bb512STodor Tomov 	struct v4l2_ctrl_handler ctrls;
80d30bb512STodor Tomov 	struct v4l2_ctrl *pixel_clock;
81d30bb512STodor Tomov 	struct v4l2_ctrl *link_freq;
82d30bb512STodor Tomov 	struct v4l2_ctrl *exposure;
83d30bb512STodor Tomov 	struct v4l2_ctrl *gain;
84d30bb512STodor Tomov 
85d30bb512STodor Tomov 	/* Cached register values */
86d30bb512STodor Tomov 	u8 aec_pk_manual;
87d30bb512STodor Tomov 	u8 pre_isp_00;
88d30bb512STodor Tomov 	u8 timing_format1;
89d30bb512STodor Tomov 	u8 timing_format2;
90d30bb512STodor Tomov 
91d30bb512STodor Tomov 	struct mutex lock; /* lock to protect power state, ctrls and mode */
92d30bb512STodor Tomov 	bool power_on;
93d30bb512STodor Tomov 
94d30bb512STodor Tomov 	struct gpio_desc *enable_gpio;
95d30bb512STodor Tomov };
96d30bb512STodor Tomov 
97d30bb512STodor Tomov static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd)
98d30bb512STodor Tomov {
99d30bb512STodor Tomov 	return container_of(sd, struct ov7251, sd);
100d30bb512STodor Tomov }
101d30bb512STodor Tomov 
102d30bb512STodor Tomov static const struct reg_value ov7251_global_init_setting[] = {
103d30bb512STodor Tomov 	{ 0x0103, 0x01 },
104d30bb512STodor Tomov 	{ 0x303b, 0x02 },
105d30bb512STodor Tomov };
106d30bb512STodor Tomov 
107d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_30fps[] = {
108d30bb512STodor Tomov 	{ 0x3005, 0x00 },
109d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
110d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
111d30bb512STodor Tomov 	{ 0x3014, 0x04 },
112d30bb512STodor Tomov 	{ 0x3016, 0xf0 },
113d30bb512STodor Tomov 	{ 0x3017, 0xf0 },
114d30bb512STodor Tomov 	{ 0x3018, 0xf0 },
115d30bb512STodor Tomov 	{ 0x301a, 0xf0 },
116d30bb512STodor Tomov 	{ 0x301b, 0xf0 },
117d30bb512STodor Tomov 	{ 0x301c, 0xf0 },
118d30bb512STodor Tomov 	{ 0x3023, 0x05 },
119d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
120d30bb512STodor Tomov 	{ 0x3098, 0x04 }, /* pll2 pre divider */
121d30bb512STodor Tomov 	{ 0x3099, 0x28 }, /* pll2 multiplier */
122d30bb512STodor Tomov 	{ 0x309a, 0x05 }, /* pll2 sys divider */
123d30bb512STodor Tomov 	{ 0x309b, 0x04 }, /* pll2 adc divider */
124d30bb512STodor Tomov 	{ 0x309d, 0x00 }, /* pll2 divider */
125d30bb512STodor Tomov 	{ 0x30b0, 0x0a }, /* pll1 pix divider */
126d30bb512STodor Tomov 	{ 0x30b1, 0x01 }, /* pll1 divider */
127d30bb512STodor Tomov 	{ 0x30b3, 0x64 }, /* pll1 multiplier */
128d30bb512STodor Tomov 	{ 0x30b4, 0x03 }, /* pll1 pre divider */
129d30bb512STodor Tomov 	{ 0x30b5, 0x05 }, /* pll1 mipi divider */
130d30bb512STodor Tomov 	{ 0x3106, 0xda },
131d30bb512STodor Tomov 	{ 0x3503, 0x07 },
132d30bb512STodor Tomov 	{ 0x3509, 0x10 },
133d30bb512STodor Tomov 	{ 0x3600, 0x1c },
134d30bb512STodor Tomov 	{ 0x3602, 0x62 },
135d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
136d30bb512STodor Tomov 	{ 0x3622, 0x04 },
137d30bb512STodor Tomov 	{ 0x3626, 0x21 },
138d30bb512STodor Tomov 	{ 0x3627, 0x30 },
139d30bb512STodor Tomov 	{ 0x3630, 0x44 },
140d30bb512STodor Tomov 	{ 0x3631, 0x35 },
141d30bb512STodor Tomov 	{ 0x3634, 0x60 },
142d30bb512STodor Tomov 	{ 0x3636, 0x00 },
143d30bb512STodor Tomov 	{ 0x3662, 0x01 },
144d30bb512STodor Tomov 	{ 0x3663, 0x70 },
145d30bb512STodor Tomov 	{ 0x3664, 0x50 },
146d30bb512STodor Tomov 	{ 0x3666, 0x0a },
147d30bb512STodor Tomov 	{ 0x3669, 0x1a },
148d30bb512STodor Tomov 	{ 0x366a, 0x00 },
149d30bb512STodor Tomov 	{ 0x366b, 0x50 },
150d30bb512STodor Tomov 	{ 0x3673, 0x01 },
151d30bb512STodor Tomov 	{ 0x3674, 0xff },
152d30bb512STodor Tomov 	{ 0x3675, 0x03 },
153d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
154d30bb512STodor Tomov 	{ 0x3709, 0x40 },
155d30bb512STodor Tomov 	{ 0x373c, 0x08 },
156d30bb512STodor Tomov 	{ 0x3742, 0x00 },
157d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
158d30bb512STodor Tomov 	{ 0x3788, 0x00 },
159d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
160d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
161d30bb512STodor Tomov 	{ 0x3800, 0x00 },
162d30bb512STodor Tomov 	{ 0x3801, 0x04 },
163d30bb512STodor Tomov 	{ 0x3802, 0x00 },
164d30bb512STodor Tomov 	{ 0x3803, 0x04 },
165d30bb512STodor Tomov 	{ 0x3804, 0x02 },
166d30bb512STodor Tomov 	{ 0x3805, 0x8b },
167d30bb512STodor Tomov 	{ 0x3806, 0x01 },
168d30bb512STodor Tomov 	{ 0x3807, 0xeb },
169d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
170d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
171d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
172d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
173d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
174d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
175d30bb512STodor Tomov 	{ 0x380e, 0x06 }, /* total vertical timing high */
176d30bb512STodor Tomov 	{ 0x380f, 0xbc }, /* total vertical timing low */
177d30bb512STodor Tomov 	{ 0x3810, 0x00 },
178d30bb512STodor Tomov 	{ 0x3811, 0x04 },
179d30bb512STodor Tomov 	{ 0x3812, 0x00 },
180d30bb512STodor Tomov 	{ 0x3813, 0x05 },
181d30bb512STodor Tomov 	{ 0x3814, 0x11 },
182d30bb512STodor Tomov 	{ 0x3815, 0x11 },
183d30bb512STodor Tomov 	{ 0x3820, 0x40 },
184d30bb512STodor Tomov 	{ 0x3821, 0x00 },
185d30bb512STodor Tomov 	{ 0x382f, 0x0e },
186d30bb512STodor Tomov 	{ 0x3832, 0x00 },
187d30bb512STodor Tomov 	{ 0x3833, 0x05 },
188d30bb512STodor Tomov 	{ 0x3834, 0x00 },
189d30bb512STodor Tomov 	{ 0x3835, 0x0c },
190d30bb512STodor Tomov 	{ 0x3837, 0x00 },
191d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
192d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
193d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
194d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
195d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
196d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
197d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
198d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
199d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
200d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
201d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
202d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
203d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
204d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
205d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
206d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
207d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
208d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
209d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
210d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
211d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
212d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
213d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
214d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
215d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
216d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
217d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
218d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
219d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
220d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
221d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
222d30bb512STodor Tomov 	{ 0x4001, 0x42 },
223d30bb512STodor Tomov 	{ 0x4004, 0x04 },
224d30bb512STodor Tomov 	{ 0x4005, 0x00 },
225d30bb512STodor Tomov 	{ 0x404e, 0x01 },
226d30bb512STodor Tomov 	{ 0x4300, 0xff },
227d30bb512STodor Tomov 	{ 0x4301, 0x00 },
228d30bb512STodor Tomov 	{ 0x4315, 0x00 },
229d30bb512STodor Tomov 	{ 0x4501, 0x48 },
230d30bb512STodor Tomov 	{ 0x4600, 0x00 },
231d30bb512STodor Tomov 	{ 0x4601, 0x4e },
232d30bb512STodor Tomov 	{ 0x4801, 0x0f },
233d30bb512STodor Tomov 	{ 0x4806, 0x0f },
234d30bb512STodor Tomov 	{ 0x4819, 0xaa },
235d30bb512STodor Tomov 	{ 0x4823, 0x3e },
236d30bb512STodor Tomov 	{ 0x4837, 0x19 },
237d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
238d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
239d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
240d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
241d30bb512STodor Tomov 	{ 0x5000, 0x85 },
242d30bb512STodor Tomov 	{ 0x5001, 0x80 },
243d30bb512STodor Tomov };
244d30bb512STodor Tomov 
245d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_60fps[] = {
246d30bb512STodor Tomov 	{ 0x3005, 0x00 },
247d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
248d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
249d30bb512STodor Tomov 	{ 0x3014, 0x04 },
250d30bb512STodor Tomov 	{ 0x3016, 0x10 },
251d30bb512STodor Tomov 	{ 0x3017, 0x00 },
252d30bb512STodor Tomov 	{ 0x3018, 0x00 },
253d30bb512STodor Tomov 	{ 0x301a, 0x00 },
254d30bb512STodor Tomov 	{ 0x301b, 0x00 },
255d30bb512STodor Tomov 	{ 0x301c, 0x00 },
256d30bb512STodor Tomov 	{ 0x3023, 0x05 },
257d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
258d30bb512STodor Tomov 	{ 0x3098, 0x04 }, /* pll2 pre divider */
259d30bb512STodor Tomov 	{ 0x3099, 0x28 }, /* pll2 multiplier */
260d30bb512STodor Tomov 	{ 0x309a, 0x05 }, /* pll2 sys divider */
261d30bb512STodor Tomov 	{ 0x309b, 0x04 }, /* pll2 adc divider */
262d30bb512STodor Tomov 	{ 0x309d, 0x00 }, /* pll2 divider */
263d30bb512STodor Tomov 	{ 0x30b0, 0x0a }, /* pll1 pix divider */
264d30bb512STodor Tomov 	{ 0x30b1, 0x01 }, /* pll1 divider */
265d30bb512STodor Tomov 	{ 0x30b3, 0x64 }, /* pll1 multiplier */
266d30bb512STodor Tomov 	{ 0x30b4, 0x03 }, /* pll1 pre divider */
267d30bb512STodor Tomov 	{ 0x30b5, 0x05 }, /* pll1 mipi divider */
268d30bb512STodor Tomov 	{ 0x3106, 0xda },
269d30bb512STodor Tomov 	{ 0x3503, 0x07 },
270d30bb512STodor Tomov 	{ 0x3509, 0x10 },
271d30bb512STodor Tomov 	{ 0x3600, 0x1c },
272d30bb512STodor Tomov 	{ 0x3602, 0x62 },
273d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
274d30bb512STodor Tomov 	{ 0x3622, 0x04 },
275d30bb512STodor Tomov 	{ 0x3626, 0x21 },
276d30bb512STodor Tomov 	{ 0x3627, 0x30 },
277d30bb512STodor Tomov 	{ 0x3630, 0x44 },
278d30bb512STodor Tomov 	{ 0x3631, 0x35 },
279d30bb512STodor Tomov 	{ 0x3634, 0x60 },
280d30bb512STodor Tomov 	{ 0x3636, 0x00 },
281d30bb512STodor Tomov 	{ 0x3662, 0x01 },
282d30bb512STodor Tomov 	{ 0x3663, 0x70 },
283d30bb512STodor Tomov 	{ 0x3664, 0x50 },
284d30bb512STodor Tomov 	{ 0x3666, 0x0a },
285d30bb512STodor Tomov 	{ 0x3669, 0x1a },
286d30bb512STodor Tomov 	{ 0x366a, 0x00 },
287d30bb512STodor Tomov 	{ 0x366b, 0x50 },
288d30bb512STodor Tomov 	{ 0x3673, 0x01 },
289d30bb512STodor Tomov 	{ 0x3674, 0xff },
290d30bb512STodor Tomov 	{ 0x3675, 0x03 },
291d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
292d30bb512STodor Tomov 	{ 0x3709, 0x40 },
293d30bb512STodor Tomov 	{ 0x373c, 0x08 },
294d30bb512STodor Tomov 	{ 0x3742, 0x00 },
295d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
296d30bb512STodor Tomov 	{ 0x3788, 0x00 },
297d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
298d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
299d30bb512STodor Tomov 	{ 0x3800, 0x00 },
300d30bb512STodor Tomov 	{ 0x3801, 0x04 },
301d30bb512STodor Tomov 	{ 0x3802, 0x00 },
302d30bb512STodor Tomov 	{ 0x3803, 0x04 },
303d30bb512STodor Tomov 	{ 0x3804, 0x02 },
304d30bb512STodor Tomov 	{ 0x3805, 0x8b },
305d30bb512STodor Tomov 	{ 0x3806, 0x01 },
306d30bb512STodor Tomov 	{ 0x3807, 0xeb },
307d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
308d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
309d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
310d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
311d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
312d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
313d30bb512STodor Tomov 	{ 0x380e, 0x03 }, /* total vertical timing high */
314d30bb512STodor Tomov 	{ 0x380f, 0x5c }, /* total vertical timing low */
315d30bb512STodor Tomov 	{ 0x3810, 0x00 },
316d30bb512STodor Tomov 	{ 0x3811, 0x04 },
317d30bb512STodor Tomov 	{ 0x3812, 0x00 },
318d30bb512STodor Tomov 	{ 0x3813, 0x05 },
319d30bb512STodor Tomov 	{ 0x3814, 0x11 },
320d30bb512STodor Tomov 	{ 0x3815, 0x11 },
321d30bb512STodor Tomov 	{ 0x3820, 0x40 },
322d30bb512STodor Tomov 	{ 0x3821, 0x00 },
323d30bb512STodor Tomov 	{ 0x382f, 0x0e },
324d30bb512STodor Tomov 	{ 0x3832, 0x00 },
325d30bb512STodor Tomov 	{ 0x3833, 0x05 },
326d30bb512STodor Tomov 	{ 0x3834, 0x00 },
327d30bb512STodor Tomov 	{ 0x3835, 0x0c },
328d30bb512STodor Tomov 	{ 0x3837, 0x00 },
329d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
330d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
331d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
332d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
333d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
334d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
335d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
336d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
337d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
338d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
339d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
340d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
341d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
342d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
343d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
344d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
345d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
346d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
347d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
348d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
349d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
350d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
351d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
352d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
353d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
354d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
355d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
356d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
357d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
358d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
359d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
360d30bb512STodor Tomov 	{ 0x4001, 0x42 },
361d30bb512STodor Tomov 	{ 0x4004, 0x04 },
362d30bb512STodor Tomov 	{ 0x4005, 0x00 },
363d30bb512STodor Tomov 	{ 0x404e, 0x01 },
364d30bb512STodor Tomov 	{ 0x4300, 0xff },
365d30bb512STodor Tomov 	{ 0x4301, 0x00 },
366d30bb512STodor Tomov 	{ 0x4315, 0x00 },
367d30bb512STodor Tomov 	{ 0x4501, 0x48 },
368d30bb512STodor Tomov 	{ 0x4600, 0x00 },
369d30bb512STodor Tomov 	{ 0x4601, 0x4e },
370d30bb512STodor Tomov 	{ 0x4801, 0x0f },
371d30bb512STodor Tomov 	{ 0x4806, 0x0f },
372d30bb512STodor Tomov 	{ 0x4819, 0xaa },
373d30bb512STodor Tomov 	{ 0x4823, 0x3e },
374d30bb512STodor Tomov 	{ 0x4837, 0x19 },
375d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
376d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
377d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
378d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
379d30bb512STodor Tomov 	{ 0x5000, 0x85 },
380d30bb512STodor Tomov 	{ 0x5001, 0x80 },
381d30bb512STodor Tomov };
382d30bb512STodor Tomov 
383d30bb512STodor Tomov static const struct reg_value ov7251_setting_vga_90fps[] = {
384d30bb512STodor Tomov 	{ 0x3005, 0x00 },
385d30bb512STodor Tomov 	{ 0x3012, 0xc0 },
386d30bb512STodor Tomov 	{ 0x3013, 0xd2 },
387d30bb512STodor Tomov 	{ 0x3014, 0x04 },
388d30bb512STodor Tomov 	{ 0x3016, 0x10 },
389d30bb512STodor Tomov 	{ 0x3017, 0x00 },
390d30bb512STodor Tomov 	{ 0x3018, 0x00 },
391d30bb512STodor Tomov 	{ 0x301a, 0x00 },
392d30bb512STodor Tomov 	{ 0x301b, 0x00 },
393d30bb512STodor Tomov 	{ 0x301c, 0x00 },
394d30bb512STodor Tomov 	{ 0x3023, 0x05 },
395d30bb512STodor Tomov 	{ 0x3037, 0xf0 },
396d30bb512STodor Tomov 	{ 0x3098, 0x04 }, /* pll2 pre divider */
397d30bb512STodor Tomov 	{ 0x3099, 0x28 }, /* pll2 multiplier */
398d30bb512STodor Tomov 	{ 0x309a, 0x05 }, /* pll2 sys divider */
399d30bb512STodor Tomov 	{ 0x309b, 0x04 }, /* pll2 adc divider */
400d30bb512STodor Tomov 	{ 0x309d, 0x00 }, /* pll2 divider */
401d30bb512STodor Tomov 	{ 0x30b0, 0x0a }, /* pll1 pix divider */
402d30bb512STodor Tomov 	{ 0x30b1, 0x01 }, /* pll1 divider */
403d30bb512STodor Tomov 	{ 0x30b3, 0x64 }, /* pll1 multiplier */
404d30bb512STodor Tomov 	{ 0x30b4, 0x03 }, /* pll1 pre divider */
405d30bb512STodor Tomov 	{ 0x30b5, 0x05 }, /* pll1 mipi divider */
406d30bb512STodor Tomov 	{ 0x3106, 0xda },
407d30bb512STodor Tomov 	{ 0x3503, 0x07 },
408d30bb512STodor Tomov 	{ 0x3509, 0x10 },
409d30bb512STodor Tomov 	{ 0x3600, 0x1c },
410d30bb512STodor Tomov 	{ 0x3602, 0x62 },
411d30bb512STodor Tomov 	{ 0x3620, 0xb7 },
412d30bb512STodor Tomov 	{ 0x3622, 0x04 },
413d30bb512STodor Tomov 	{ 0x3626, 0x21 },
414d30bb512STodor Tomov 	{ 0x3627, 0x30 },
415d30bb512STodor Tomov 	{ 0x3630, 0x44 },
416d30bb512STodor Tomov 	{ 0x3631, 0x35 },
417d30bb512STodor Tomov 	{ 0x3634, 0x60 },
418d30bb512STodor Tomov 	{ 0x3636, 0x00 },
419d30bb512STodor Tomov 	{ 0x3662, 0x01 },
420d30bb512STodor Tomov 	{ 0x3663, 0x70 },
421d30bb512STodor Tomov 	{ 0x3664, 0x50 },
422d30bb512STodor Tomov 	{ 0x3666, 0x0a },
423d30bb512STodor Tomov 	{ 0x3669, 0x1a },
424d30bb512STodor Tomov 	{ 0x366a, 0x00 },
425d30bb512STodor Tomov 	{ 0x366b, 0x50 },
426d30bb512STodor Tomov 	{ 0x3673, 0x01 },
427d30bb512STodor Tomov 	{ 0x3674, 0xff },
428d30bb512STodor Tomov 	{ 0x3675, 0x03 },
429d30bb512STodor Tomov 	{ 0x3705, 0xc1 },
430d30bb512STodor Tomov 	{ 0x3709, 0x40 },
431d30bb512STodor Tomov 	{ 0x373c, 0x08 },
432d30bb512STodor Tomov 	{ 0x3742, 0x00 },
433d30bb512STodor Tomov 	{ 0x3757, 0xb3 },
434d30bb512STodor Tomov 	{ 0x3788, 0x00 },
435d30bb512STodor Tomov 	{ 0x37a8, 0x01 },
436d30bb512STodor Tomov 	{ 0x37a9, 0xc0 },
437d30bb512STodor Tomov 	{ 0x3800, 0x00 },
438d30bb512STodor Tomov 	{ 0x3801, 0x04 },
439d30bb512STodor Tomov 	{ 0x3802, 0x00 },
440d30bb512STodor Tomov 	{ 0x3803, 0x04 },
441d30bb512STodor Tomov 	{ 0x3804, 0x02 },
442d30bb512STodor Tomov 	{ 0x3805, 0x8b },
443d30bb512STodor Tomov 	{ 0x3806, 0x01 },
444d30bb512STodor Tomov 	{ 0x3807, 0xeb },
445d30bb512STodor Tomov 	{ 0x3808, 0x02 }, /* width high */
446d30bb512STodor Tomov 	{ 0x3809, 0x80 }, /* width low */
447d30bb512STodor Tomov 	{ 0x380a, 0x01 }, /* height high */
448d30bb512STodor Tomov 	{ 0x380b, 0xe0 }, /* height low */
449d30bb512STodor Tomov 	{ 0x380c, 0x03 }, /* total horiz timing high */
450d30bb512STodor Tomov 	{ 0x380d, 0xa0 }, /* total horiz timing low */
451d30bb512STodor Tomov 	{ 0x380e, 0x02 }, /* total vertical timing high */
452d30bb512STodor Tomov 	{ 0x380f, 0x3c }, /* total vertical timing low */
453d30bb512STodor Tomov 	{ 0x3810, 0x00 },
454d30bb512STodor Tomov 	{ 0x3811, 0x04 },
455d30bb512STodor Tomov 	{ 0x3812, 0x00 },
456d30bb512STodor Tomov 	{ 0x3813, 0x05 },
457d30bb512STodor Tomov 	{ 0x3814, 0x11 },
458d30bb512STodor Tomov 	{ 0x3815, 0x11 },
459d30bb512STodor Tomov 	{ 0x3820, 0x40 },
460d30bb512STodor Tomov 	{ 0x3821, 0x00 },
461d30bb512STodor Tomov 	{ 0x382f, 0x0e },
462d30bb512STodor Tomov 	{ 0x3832, 0x00 },
463d30bb512STodor Tomov 	{ 0x3833, 0x05 },
464d30bb512STodor Tomov 	{ 0x3834, 0x00 },
465d30bb512STodor Tomov 	{ 0x3835, 0x0c },
466d30bb512STodor Tomov 	{ 0x3837, 0x00 },
467d30bb512STodor Tomov 	{ 0x3b80, 0x00 },
468d30bb512STodor Tomov 	{ 0x3b81, 0xa5 },
469d30bb512STodor Tomov 	{ 0x3b82, 0x10 },
470d30bb512STodor Tomov 	{ 0x3b83, 0x00 },
471d30bb512STodor Tomov 	{ 0x3b84, 0x08 },
472d30bb512STodor Tomov 	{ 0x3b85, 0x00 },
473d30bb512STodor Tomov 	{ 0x3b86, 0x01 },
474d30bb512STodor Tomov 	{ 0x3b87, 0x00 },
475d30bb512STodor Tomov 	{ 0x3b88, 0x00 },
476d30bb512STodor Tomov 	{ 0x3b89, 0x00 },
477d30bb512STodor Tomov 	{ 0x3b8a, 0x00 },
478d30bb512STodor Tomov 	{ 0x3b8b, 0x05 },
479d30bb512STodor Tomov 	{ 0x3b8c, 0x00 },
480d30bb512STodor Tomov 	{ 0x3b8d, 0x00 },
481d30bb512STodor Tomov 	{ 0x3b8e, 0x00 },
482d30bb512STodor Tomov 	{ 0x3b8f, 0x1a },
483d30bb512STodor Tomov 	{ 0x3b94, 0x05 },
484d30bb512STodor Tomov 	{ 0x3b95, 0xf2 },
485d30bb512STodor Tomov 	{ 0x3b96, 0x40 },
486d30bb512STodor Tomov 	{ 0x3c00, 0x89 },
487d30bb512STodor Tomov 	{ 0x3c01, 0x63 },
488d30bb512STodor Tomov 	{ 0x3c02, 0x01 },
489d30bb512STodor Tomov 	{ 0x3c03, 0x00 },
490d30bb512STodor Tomov 	{ 0x3c04, 0x00 },
491d30bb512STodor Tomov 	{ 0x3c05, 0x03 },
492d30bb512STodor Tomov 	{ 0x3c06, 0x00 },
493d30bb512STodor Tomov 	{ 0x3c07, 0x06 },
494d30bb512STodor Tomov 	{ 0x3c0c, 0x01 },
495d30bb512STodor Tomov 	{ 0x3c0d, 0xd0 },
496d30bb512STodor Tomov 	{ 0x3c0e, 0x02 },
497d30bb512STodor Tomov 	{ 0x3c0f, 0x0a },
498d30bb512STodor Tomov 	{ 0x4001, 0x42 },
499d30bb512STodor Tomov 	{ 0x4004, 0x04 },
500d30bb512STodor Tomov 	{ 0x4005, 0x00 },
501d30bb512STodor Tomov 	{ 0x404e, 0x01 },
502d30bb512STodor Tomov 	{ 0x4300, 0xff },
503d30bb512STodor Tomov 	{ 0x4301, 0x00 },
504d30bb512STodor Tomov 	{ 0x4315, 0x00 },
505d30bb512STodor Tomov 	{ 0x4501, 0x48 },
506d30bb512STodor Tomov 	{ 0x4600, 0x00 },
507d30bb512STodor Tomov 	{ 0x4601, 0x4e },
508d30bb512STodor Tomov 	{ 0x4801, 0x0f },
509d30bb512STodor Tomov 	{ 0x4806, 0x0f },
510d30bb512STodor Tomov 	{ 0x4819, 0xaa },
511d30bb512STodor Tomov 	{ 0x4823, 0x3e },
512d30bb512STodor Tomov 	{ 0x4837, 0x19 },
513d30bb512STodor Tomov 	{ 0x4a0d, 0x00 },
514d30bb512STodor Tomov 	{ 0x4a47, 0x7f },
515d30bb512STodor Tomov 	{ 0x4a49, 0xf0 },
516d30bb512STodor Tomov 	{ 0x4a4b, 0x30 },
517d30bb512STodor Tomov 	{ 0x5000, 0x85 },
518d30bb512STodor Tomov 	{ 0x5001, 0x80 },
519d30bb512STodor Tomov };
520d30bb512STodor Tomov 
521d30bb512STodor Tomov static const s64 link_freq[] = {
522d30bb512STodor Tomov 	240000000,
523d30bb512STodor Tomov };
524d30bb512STodor Tomov 
525d30bb512STodor Tomov static const struct ov7251_mode_info ov7251_mode_info_data[] = {
526d30bb512STodor Tomov 	{
527d30bb512STodor Tomov 		.width = 640,
528d30bb512STodor Tomov 		.height = 480,
529d30bb512STodor Tomov 		.data = ov7251_setting_vga_30fps,
530d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_30fps),
531d30bb512STodor Tomov 		.pixel_clock = 48000000,
532d30bb512STodor Tomov 		.link_freq = 0, /* an index in link_freq[] */
533d30bb512STodor Tomov 		.exposure_max = 1704,
534d30bb512STodor Tomov 		.exposure_def = 504,
535d30bb512STodor Tomov 		.timeperframe = {
536d30bb512STodor Tomov 			.numerator = 100,
537d30bb512STodor Tomov 			.denominator = 3000
538d30bb512STodor Tomov 		}
539d30bb512STodor Tomov 	},
540d30bb512STodor Tomov 	{
541d30bb512STodor Tomov 		.width = 640,
542d30bb512STodor Tomov 		.height = 480,
543d30bb512STodor Tomov 		.data = ov7251_setting_vga_60fps,
544d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_60fps),
545d30bb512STodor Tomov 		.pixel_clock = 48000000,
546d30bb512STodor Tomov 		.link_freq = 0, /* an index in link_freq[] */
547d30bb512STodor Tomov 		.exposure_max = 840,
548d30bb512STodor Tomov 		.exposure_def = 504,
549d30bb512STodor Tomov 		.timeperframe = {
550d30bb512STodor Tomov 			.numerator = 100,
551d30bb512STodor Tomov 			.denominator = 6014
552d30bb512STodor Tomov 		}
553d30bb512STodor Tomov 	},
554d30bb512STodor Tomov 	{
555d30bb512STodor Tomov 		.width = 640,
556d30bb512STodor Tomov 		.height = 480,
557d30bb512STodor Tomov 		.data = ov7251_setting_vga_90fps,
558d30bb512STodor Tomov 		.data_size = ARRAY_SIZE(ov7251_setting_vga_90fps),
559d30bb512STodor Tomov 		.pixel_clock = 48000000,
560d30bb512STodor Tomov 		.link_freq = 0, /* an index in link_freq[] */
561d30bb512STodor Tomov 		.exposure_max = 552,
562d30bb512STodor Tomov 		.exposure_def = 504,
563d30bb512STodor Tomov 		.timeperframe = {
564d30bb512STodor Tomov 			.numerator = 100,
565d30bb512STodor Tomov 			.denominator = 9043
566d30bb512STodor Tomov 		}
567d30bb512STodor Tomov 	},
568d30bb512STodor Tomov };
569d30bb512STodor Tomov 
570d30bb512STodor Tomov static int ov7251_regulators_enable(struct ov7251 *ov7251)
571d30bb512STodor Tomov {
572d30bb512STodor Tomov 	int ret;
573d30bb512STodor Tomov 
574d30bb512STodor Tomov 	/* OV7251 power up sequence requires core regulator
575d30bb512STodor Tomov 	 * to be enabled not earlier than io regulator
576d30bb512STodor Tomov 	 */
577d30bb512STodor Tomov 
578d30bb512STodor Tomov 	ret = regulator_enable(ov7251->io_regulator);
579d30bb512STodor Tomov 	if (ret < 0) {
580d30bb512STodor Tomov 		dev_err(ov7251->dev, "set io voltage failed\n");
581d30bb512STodor Tomov 		return ret;
582d30bb512STodor Tomov 	}
583d30bb512STodor Tomov 
584d30bb512STodor Tomov 	ret = regulator_enable(ov7251->analog_regulator);
585d30bb512STodor Tomov 	if (ret) {
586d30bb512STodor Tomov 		dev_err(ov7251->dev, "set analog voltage failed\n");
587d30bb512STodor Tomov 		goto err_disable_io;
588d30bb512STodor Tomov 	}
589d30bb512STodor Tomov 
590d30bb512STodor Tomov 	ret = regulator_enable(ov7251->core_regulator);
591d30bb512STodor Tomov 	if (ret) {
592d30bb512STodor Tomov 		dev_err(ov7251->dev, "set core voltage failed\n");
593d30bb512STodor Tomov 		goto err_disable_analog;
594d30bb512STodor Tomov 	}
595d30bb512STodor Tomov 
596d30bb512STodor Tomov 	return 0;
597d30bb512STodor Tomov 
598d30bb512STodor Tomov err_disable_analog:
599d30bb512STodor Tomov 	regulator_disable(ov7251->analog_regulator);
600d30bb512STodor Tomov 
601d30bb512STodor Tomov err_disable_io:
602d30bb512STodor Tomov 	regulator_disable(ov7251->io_regulator);
603d30bb512STodor Tomov 
604d30bb512STodor Tomov 	return ret;
605d30bb512STodor Tomov }
606d30bb512STodor Tomov 
607d30bb512STodor Tomov static void ov7251_regulators_disable(struct ov7251 *ov7251)
608d30bb512STodor Tomov {
609d30bb512STodor Tomov 	int ret;
610d30bb512STodor Tomov 
611d30bb512STodor Tomov 	ret = regulator_disable(ov7251->core_regulator);
612d30bb512STodor Tomov 	if (ret < 0)
613d30bb512STodor Tomov 		dev_err(ov7251->dev, "core regulator disable failed\n");
614d30bb512STodor Tomov 
615d30bb512STodor Tomov 	ret = regulator_disable(ov7251->analog_regulator);
616d30bb512STodor Tomov 	if (ret < 0)
617d30bb512STodor Tomov 		dev_err(ov7251->dev, "analog regulator disable failed\n");
618d30bb512STodor Tomov 
619d30bb512STodor Tomov 	ret = regulator_disable(ov7251->io_regulator);
620d30bb512STodor Tomov 	if (ret < 0)
621d30bb512STodor Tomov 		dev_err(ov7251->dev, "io regulator disable failed\n");
622d30bb512STodor Tomov }
623d30bb512STodor Tomov 
624d30bb512STodor Tomov static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)
625d30bb512STodor Tomov {
626d30bb512STodor Tomov 	u8 regbuf[3];
627d30bb512STodor Tomov 	int ret;
628d30bb512STodor Tomov 
629d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
630d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
631d30bb512STodor Tomov 	regbuf[2] = val;
632d30bb512STodor Tomov 
633d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, 3);
634d30bb512STodor Tomov 	if (ret < 0) {
635d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n",
636d30bb512STodor Tomov 			__func__, ret, reg, val);
637d30bb512STodor Tomov 		return ret;
638d30bb512STodor Tomov 	}
639d30bb512STodor Tomov 
640d30bb512STodor Tomov 	return 0;
641d30bb512STodor Tomov }
642d30bb512STodor Tomov 
643d30bb512STodor Tomov static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val,
644d30bb512STodor Tomov 				 u8 num)
645d30bb512STodor Tomov {
646d30bb512STodor Tomov 	u8 regbuf[5];
647d30bb512STodor Tomov 	u8 nregbuf = sizeof(reg) + num * sizeof(*val);
648d30bb512STodor Tomov 	int ret = 0;
649d30bb512STodor Tomov 
650d30bb512STodor Tomov 	if (nregbuf > sizeof(regbuf))
651d30bb512STodor Tomov 		return -EINVAL;
652d30bb512STodor Tomov 
653d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
654d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
655d30bb512STodor Tomov 
656d30bb512STodor Tomov 	memcpy(regbuf + 2, val, num);
657d30bb512STodor Tomov 
658d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf);
659d30bb512STodor Tomov 	if (ret < 0) {
660d30bb512STodor Tomov 		dev_err(ov7251->dev,
661d30bb512STodor Tomov 			"%s: write seq regs error %d: first reg=%x\n",
662d30bb512STodor Tomov 			__func__, ret, reg);
663d30bb512STodor Tomov 		return ret;
664d30bb512STodor Tomov 	}
665d30bb512STodor Tomov 
666d30bb512STodor Tomov 	return 0;
667d30bb512STodor Tomov }
668d30bb512STodor Tomov 
669d30bb512STodor Tomov static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val)
670d30bb512STodor Tomov {
671d30bb512STodor Tomov 	u8 regbuf[2];
672d30bb512STodor Tomov 	int ret;
673d30bb512STodor Tomov 
674d30bb512STodor Tomov 	regbuf[0] = reg >> 8;
675d30bb512STodor Tomov 	regbuf[1] = reg & 0xff;
676d30bb512STodor Tomov 
677d30bb512STodor Tomov 	ret = i2c_master_send(ov7251->i2c_client, regbuf, 2);
678d30bb512STodor Tomov 	if (ret < 0) {
679d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n",
680d30bb512STodor Tomov 			__func__, ret, reg);
681d30bb512STodor Tomov 		return ret;
682d30bb512STodor Tomov 	}
683d30bb512STodor Tomov 
684d30bb512STodor Tomov 	ret = i2c_master_recv(ov7251->i2c_client, val, 1);
685d30bb512STodor Tomov 	if (ret < 0) {
686d30bb512STodor Tomov 		dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n",
687d30bb512STodor Tomov 			__func__, ret, reg);
688d30bb512STodor Tomov 		return ret;
689d30bb512STodor Tomov 	}
690d30bb512STodor Tomov 
691d30bb512STodor Tomov 	return 0;
692d30bb512STodor Tomov }
693d30bb512STodor Tomov 
694d30bb512STodor Tomov static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure)
695d30bb512STodor Tomov {
696d30bb512STodor Tomov 	u16 reg;
697d30bb512STodor Tomov 	u8 val[3];
698d30bb512STodor Tomov 
699d30bb512STodor Tomov 	reg = OV7251_AEC_EXPO_0;
700d30bb512STodor Tomov 	val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */
701d30bb512STodor Tomov 	val[1] = (exposure & 0x0ff0) >> 4;  /* goes to OV7251_AEC_EXPO_1 */
702d30bb512STodor Tomov 	val[2] = (exposure & 0x000f) << 4;  /* goes to OV7251_AEC_EXPO_2 */
703d30bb512STodor Tomov 
704d30bb512STodor Tomov 	return ov7251_write_seq_regs(ov7251, reg, val, 3);
705d30bb512STodor Tomov }
706d30bb512STodor Tomov 
707d30bb512STodor Tomov static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain)
708d30bb512STodor Tomov {
709d30bb512STodor Tomov 	u16 reg;
710d30bb512STodor Tomov 	u8 val[2];
711d30bb512STodor Tomov 
712d30bb512STodor Tomov 	reg = OV7251_AEC_AGC_ADJ_0;
713d30bb512STodor Tomov 	val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */
714d30bb512STodor Tomov 	val[1] = gain & 0xff;          /* goes to OV7251_AEC_AGC_ADJ_1 */
715d30bb512STodor Tomov 
716d30bb512STodor Tomov 	return ov7251_write_seq_regs(ov7251, reg, val, 2);
717d30bb512STodor Tomov }
718d30bb512STodor Tomov 
719d30bb512STodor Tomov static int ov7251_set_register_array(struct ov7251 *ov7251,
720d30bb512STodor Tomov 				     const struct reg_value *settings,
721d30bb512STodor Tomov 				     unsigned int num_settings)
722d30bb512STodor Tomov {
723d30bb512STodor Tomov 	unsigned int i;
724d30bb512STodor Tomov 	int ret;
725d30bb512STodor Tomov 
726d30bb512STodor Tomov 	for (i = 0; i < num_settings; ++i, ++settings) {
727d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, settings->reg, settings->val);
728d30bb512STodor Tomov 		if (ret < 0)
729d30bb512STodor Tomov 			return ret;
730d30bb512STodor Tomov 	}
731d30bb512STodor Tomov 
732d30bb512STodor Tomov 	return 0;
733d30bb512STodor Tomov }
734d30bb512STodor Tomov 
735d30bb512STodor Tomov static int ov7251_set_power_on(struct ov7251 *ov7251)
736d30bb512STodor Tomov {
737d30bb512STodor Tomov 	int ret;
738d30bb512STodor Tomov 	u32 wait_us;
739d30bb512STodor Tomov 
740d30bb512STodor Tomov 	ret = ov7251_regulators_enable(ov7251);
741d30bb512STodor Tomov 	if (ret < 0)
742d30bb512STodor Tomov 		return ret;
743d30bb512STodor Tomov 
744d30bb512STodor Tomov 	ret = clk_prepare_enable(ov7251->xclk);
745d30bb512STodor Tomov 	if (ret < 0) {
746d30bb512STodor Tomov 		dev_err(ov7251->dev, "clk prepare enable failed\n");
747d30bb512STodor Tomov 		ov7251_regulators_disable(ov7251);
748d30bb512STodor Tomov 		return ret;
749d30bb512STodor Tomov 	}
750d30bb512STodor Tomov 
751d30bb512STodor Tomov 	gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
752d30bb512STodor Tomov 
753d30bb512STodor Tomov 	/* wait at least 65536 external clock cycles */
754d30bb512STodor Tomov 	wait_us = DIV_ROUND_UP(65536 * 1000,
755d30bb512STodor Tomov 			       DIV_ROUND_UP(ov7251->xclk_freq, 1000));
756d30bb512STodor Tomov 	usleep_range(wait_us, wait_us + 1000);
757d30bb512STodor Tomov 
758d30bb512STodor Tomov 	return 0;
759d30bb512STodor Tomov }
760d30bb512STodor Tomov 
761d30bb512STodor Tomov static void ov7251_set_power_off(struct ov7251 *ov7251)
762d30bb512STodor Tomov {
763d30bb512STodor Tomov 	clk_disable_unprepare(ov7251->xclk);
764d30bb512STodor Tomov 	gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
765d30bb512STodor Tomov 	ov7251_regulators_disable(ov7251);
766d30bb512STodor Tomov }
767d30bb512STodor Tomov 
768d30bb512STodor Tomov static int ov7251_s_power(struct v4l2_subdev *sd, int on)
769d30bb512STodor Tomov {
770d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
771d30bb512STodor Tomov 	int ret = 0;
772d30bb512STodor Tomov 
773d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
774d30bb512STodor Tomov 
775d30bb512STodor Tomov 	/* If the power state is not modified - no work to do. */
776d30bb512STodor Tomov 	if (ov7251->power_on == !!on)
777d30bb512STodor Tomov 		goto exit;
778d30bb512STodor Tomov 
779d30bb512STodor Tomov 	if (on) {
780d30bb512STodor Tomov 		ret = ov7251_set_power_on(ov7251);
781d30bb512STodor Tomov 		if (ret < 0)
782d30bb512STodor Tomov 			goto exit;
783d30bb512STodor Tomov 
784d30bb512STodor Tomov 		ret = ov7251_set_register_array(ov7251,
785d30bb512STodor Tomov 					ov7251_global_init_setting,
786d30bb512STodor Tomov 					ARRAY_SIZE(ov7251_global_init_setting));
787d30bb512STodor Tomov 		if (ret < 0) {
788d30bb512STodor Tomov 			dev_err(ov7251->dev, "could not set init registers\n");
789d30bb512STodor Tomov 			ov7251_set_power_off(ov7251);
790d30bb512STodor Tomov 			goto exit;
791d30bb512STodor Tomov 		}
792d30bb512STodor Tomov 
793d30bb512STodor Tomov 		ov7251->power_on = true;
794d30bb512STodor Tomov 	} else {
795d30bb512STodor Tomov 		ov7251_set_power_off(ov7251);
796d30bb512STodor Tomov 		ov7251->power_on = false;
797d30bb512STodor Tomov 	}
798d30bb512STodor Tomov 
799d30bb512STodor Tomov exit:
800d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
801d30bb512STodor Tomov 
802d30bb512STodor Tomov 	return ret;
803d30bb512STodor Tomov }
804d30bb512STodor Tomov 
805d30bb512STodor Tomov static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
806d30bb512STodor Tomov {
807d30bb512STodor Tomov 	u8 val = ov7251->timing_format2;
808d30bb512STodor Tomov 	int ret;
809d30bb512STodor Tomov 
810d30bb512STodor Tomov 	if (value)
811d30bb512STodor Tomov 		val |= OV7251_TIMING_FORMAT2_MIRROR;
812d30bb512STodor Tomov 	else
813d30bb512STodor Tomov 		val &= ~OV7251_TIMING_FORMAT2_MIRROR;
814d30bb512STodor Tomov 
815d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
816d30bb512STodor Tomov 	if (!ret)
817d30bb512STodor Tomov 		ov7251->timing_format2 = val;
818d30bb512STodor Tomov 
819d30bb512STodor Tomov 	return ret;
820d30bb512STodor Tomov }
821d30bb512STodor Tomov 
822d30bb512STodor Tomov static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
823d30bb512STodor Tomov {
824d30bb512STodor Tomov 	u8 val = ov7251->timing_format1;
825d30bb512STodor Tomov 	int ret;
826d30bb512STodor Tomov 
827d30bb512STodor Tomov 	if (value)
828d30bb512STodor Tomov 		val |= OV7251_TIMING_FORMAT1_VFLIP;
829d30bb512STodor Tomov 	else
830d30bb512STodor Tomov 		val &= ~OV7251_TIMING_FORMAT1_VFLIP;
831d30bb512STodor Tomov 
832d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
833d30bb512STodor Tomov 	if (!ret)
834d30bb512STodor Tomov 		ov7251->timing_format1 = val;
835d30bb512STodor Tomov 
836d30bb512STodor Tomov 	return ret;
837d30bb512STodor Tomov }
838d30bb512STodor Tomov 
839d30bb512STodor Tomov static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
840d30bb512STodor Tomov {
841d30bb512STodor Tomov 	u8 val = ov7251->pre_isp_00;
842d30bb512STodor Tomov 	int ret;
843d30bb512STodor Tomov 
844d30bb512STodor Tomov 	if (value)
845d30bb512STodor Tomov 		val |= OV7251_PRE_ISP_00_TEST_PATTERN;
846d30bb512STodor Tomov 	else
847d30bb512STodor Tomov 		val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
848d30bb512STodor Tomov 
849d30bb512STodor Tomov 	ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
850d30bb512STodor Tomov 	if (!ret)
851d30bb512STodor Tomov 		ov7251->pre_isp_00 = val;
852d30bb512STodor Tomov 
853d30bb512STodor Tomov 	return ret;
854d30bb512STodor Tomov }
855d30bb512STodor Tomov 
856d30bb512STodor Tomov static const char * const ov7251_test_pattern_menu[] = {
857d30bb512STodor Tomov 	"Disabled",
858d30bb512STodor Tomov 	"Vertical Pattern Bars",
859d30bb512STodor Tomov };
860d30bb512STodor Tomov 
861d30bb512STodor Tomov static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
862d30bb512STodor Tomov {
863d30bb512STodor Tomov 	struct ov7251 *ov7251 = container_of(ctrl->handler,
864d30bb512STodor Tomov 					     struct ov7251, ctrls);
865d30bb512STodor Tomov 	int ret;
866d30bb512STodor Tomov 
867d30bb512STodor Tomov 	/* v4l2_ctrl_lock() locks our mutex */
868d30bb512STodor Tomov 
869d30bb512STodor Tomov 	if (!ov7251->power_on)
870d30bb512STodor Tomov 		return 0;
871d30bb512STodor Tomov 
872d30bb512STodor Tomov 	switch (ctrl->id) {
873d30bb512STodor Tomov 	case V4L2_CID_EXPOSURE:
874d30bb512STodor Tomov 		ret = ov7251_set_exposure(ov7251, ctrl->val);
875d30bb512STodor Tomov 		break;
876d30bb512STodor Tomov 	case V4L2_CID_GAIN:
877d30bb512STodor Tomov 		ret = ov7251_set_gain(ov7251, ctrl->val);
878d30bb512STodor Tomov 		break;
879d30bb512STodor Tomov 	case V4L2_CID_TEST_PATTERN:
880d30bb512STodor Tomov 		ret = ov7251_set_test_pattern(ov7251, ctrl->val);
881d30bb512STodor Tomov 		break;
882d30bb512STodor Tomov 	case V4L2_CID_HFLIP:
883d30bb512STodor Tomov 		ret = ov7251_set_hflip(ov7251, ctrl->val);
884d30bb512STodor Tomov 		break;
885d30bb512STodor Tomov 	case V4L2_CID_VFLIP:
886d30bb512STodor Tomov 		ret = ov7251_set_vflip(ov7251, ctrl->val);
887d30bb512STodor Tomov 		break;
888d30bb512STodor Tomov 	default:
889d30bb512STodor Tomov 		ret = -EINVAL;
890d30bb512STodor Tomov 		break;
891d30bb512STodor Tomov 	}
892d30bb512STodor Tomov 
893d30bb512STodor Tomov 	return ret;
894d30bb512STodor Tomov }
895d30bb512STodor Tomov 
896d30bb512STodor Tomov static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
897d30bb512STodor Tomov 	.s_ctrl = ov7251_s_ctrl,
898d30bb512STodor Tomov };
899d30bb512STodor Tomov 
900d30bb512STodor Tomov static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
901*0d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
902d30bb512STodor Tomov 				 struct v4l2_subdev_mbus_code_enum *code)
903d30bb512STodor Tomov {
904d30bb512STodor Tomov 	if (code->index > 0)
905d30bb512STodor Tomov 		return -EINVAL;
906d30bb512STodor Tomov 
907d30bb512STodor Tomov 	code->code = MEDIA_BUS_FMT_Y10_1X10;
908d30bb512STodor Tomov 
909d30bb512STodor Tomov 	return 0;
910d30bb512STodor Tomov }
911d30bb512STodor Tomov 
912d30bb512STodor Tomov static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
913*0d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
914d30bb512STodor Tomov 				  struct v4l2_subdev_frame_size_enum *fse)
915d30bb512STodor Tomov {
916d30bb512STodor Tomov 	if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
917d30bb512STodor Tomov 		return -EINVAL;
918d30bb512STodor Tomov 
919d30bb512STodor Tomov 	if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
920d30bb512STodor Tomov 		return -EINVAL;
921d30bb512STodor Tomov 
922d30bb512STodor Tomov 	fse->min_width = ov7251_mode_info_data[fse->index].width;
923d30bb512STodor Tomov 	fse->max_width = ov7251_mode_info_data[fse->index].width;
924d30bb512STodor Tomov 	fse->min_height = ov7251_mode_info_data[fse->index].height;
925d30bb512STodor Tomov 	fse->max_height = ov7251_mode_info_data[fse->index].height;
926d30bb512STodor Tomov 
927d30bb512STodor Tomov 	return 0;
928d30bb512STodor Tomov }
929d30bb512STodor Tomov 
930d30bb512STodor Tomov static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
931*0d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
932d30bb512STodor Tomov 				  struct v4l2_subdev_frame_interval_enum *fie)
933d30bb512STodor Tomov {
934d30bb512STodor Tomov 	unsigned int index = fie->index;
935d30bb512STodor Tomov 	unsigned int i;
936d30bb512STodor Tomov 
937d30bb512STodor Tomov 	for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
938d30bb512STodor Tomov 		if (fie->width != ov7251_mode_info_data[i].width ||
939d30bb512STodor Tomov 		    fie->height != ov7251_mode_info_data[i].height)
940d30bb512STodor Tomov 			continue;
941d30bb512STodor Tomov 
942d30bb512STodor Tomov 		if (index-- == 0) {
943d30bb512STodor Tomov 			fie->interval = ov7251_mode_info_data[i].timeperframe;
944d30bb512STodor Tomov 			return 0;
945d30bb512STodor Tomov 		}
946d30bb512STodor Tomov 	}
947d30bb512STodor Tomov 
948d30bb512STodor Tomov 	return -EINVAL;
949d30bb512STodor Tomov }
950d30bb512STodor Tomov 
951d30bb512STodor Tomov static struct v4l2_mbus_framefmt *
952d30bb512STodor Tomov __ov7251_get_pad_format(struct ov7251 *ov7251,
953*0d346d2aSTomi Valkeinen 			struct v4l2_subdev_state *sd_state,
954d30bb512STodor Tomov 			unsigned int pad,
955d30bb512STodor Tomov 			enum v4l2_subdev_format_whence which)
956d30bb512STodor Tomov {
957d30bb512STodor Tomov 	switch (which) {
958d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_TRY:
959*0d346d2aSTomi Valkeinen 		return v4l2_subdev_get_try_format(&ov7251->sd, sd_state, pad);
960d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_ACTIVE:
961d30bb512STodor Tomov 		return &ov7251->fmt;
962d30bb512STodor Tomov 	default:
963d30bb512STodor Tomov 		return NULL;
964d30bb512STodor Tomov 	}
965d30bb512STodor Tomov }
966d30bb512STodor Tomov 
967d30bb512STodor Tomov static int ov7251_get_format(struct v4l2_subdev *sd,
968*0d346d2aSTomi Valkeinen 			     struct v4l2_subdev_state *sd_state,
969d30bb512STodor Tomov 			     struct v4l2_subdev_format *format)
970d30bb512STodor Tomov {
971d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
972d30bb512STodor Tomov 
973d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
974*0d346d2aSTomi Valkeinen 	format->format = *__ov7251_get_pad_format(ov7251, sd_state,
975*0d346d2aSTomi Valkeinen 						  format->pad,
976d30bb512STodor Tomov 						  format->which);
977d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
978d30bb512STodor Tomov 
979d30bb512STodor Tomov 	return 0;
980d30bb512STodor Tomov }
981d30bb512STodor Tomov 
982d30bb512STodor Tomov static struct v4l2_rect *
983*0d346d2aSTomi Valkeinen __ov7251_get_pad_crop(struct ov7251 *ov7251,
984*0d346d2aSTomi Valkeinen 		      struct v4l2_subdev_state *sd_state,
985d30bb512STodor Tomov 		      unsigned int pad, enum v4l2_subdev_format_whence which)
986d30bb512STodor Tomov {
987d30bb512STodor Tomov 	switch (which) {
988d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_TRY:
989*0d346d2aSTomi Valkeinen 		return v4l2_subdev_get_try_crop(&ov7251->sd, sd_state, pad);
990d30bb512STodor Tomov 	case V4L2_SUBDEV_FORMAT_ACTIVE:
991d30bb512STodor Tomov 		return &ov7251->crop;
992d30bb512STodor Tomov 	default:
993d30bb512STodor Tomov 		return NULL;
994d30bb512STodor Tomov 	}
995d30bb512STodor Tomov }
996d30bb512STodor Tomov 
997d30bb512STodor Tomov static inline u32 avg_fps(const struct v4l2_fract *t)
998d30bb512STodor Tomov {
999d30bb512STodor Tomov 	return (t->denominator + (t->numerator >> 1)) / t->numerator;
1000d30bb512STodor Tomov }
1001d30bb512STodor Tomov 
1002d30bb512STodor Tomov static const struct ov7251_mode_info *
1003d30bb512STodor Tomov ov7251_find_mode_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
1004d30bb512STodor Tomov {
1005d30bb512STodor Tomov 	const struct ov7251_mode_info *mode = ov7251->current_mode;
1006d30bb512STodor Tomov 	unsigned int fps_req = avg_fps(timeperframe);
1007d30bb512STodor Tomov 	unsigned int max_dist_match = (unsigned int) -1;
1008d30bb512STodor Tomov 	unsigned int i, n = 0;
1009d30bb512STodor Tomov 
1010d30bb512STodor Tomov 	for (i = 0; i < ARRAY_SIZE(ov7251_mode_info_data); i++) {
1011d30bb512STodor Tomov 		unsigned int dist;
1012d30bb512STodor Tomov 		unsigned int fps_tmp;
1013d30bb512STodor Tomov 
1014d30bb512STodor Tomov 		if (mode->width != ov7251_mode_info_data[i].width ||
1015d30bb512STodor Tomov 		    mode->height != ov7251_mode_info_data[i].height)
1016d30bb512STodor Tomov 			continue;
1017d30bb512STodor Tomov 
1018d30bb512STodor Tomov 		fps_tmp = avg_fps(&ov7251_mode_info_data[i].timeperframe);
1019d30bb512STodor Tomov 
1020d30bb512STodor Tomov 		dist = abs(fps_req - fps_tmp);
1021d30bb512STodor Tomov 
1022d30bb512STodor Tomov 		if (dist < max_dist_match) {
1023d30bb512STodor Tomov 			n = i;
1024d30bb512STodor Tomov 			max_dist_match = dist;
1025d30bb512STodor Tomov 		}
1026d30bb512STodor Tomov 	}
1027d30bb512STodor Tomov 
1028d30bb512STodor Tomov 	return &ov7251_mode_info_data[n];
1029d30bb512STodor Tomov }
1030d30bb512STodor Tomov 
1031d30bb512STodor Tomov static int ov7251_set_format(struct v4l2_subdev *sd,
1032*0d346d2aSTomi Valkeinen 			     struct v4l2_subdev_state *sd_state,
1033d30bb512STodor Tomov 			     struct v4l2_subdev_format *format)
1034d30bb512STodor Tomov {
1035d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1036d30bb512STodor Tomov 	struct v4l2_mbus_framefmt *__format;
1037d30bb512STodor Tomov 	struct v4l2_rect *__crop;
1038d30bb512STodor Tomov 	const struct ov7251_mode_info *new_mode;
1039d30bb512STodor Tomov 	int ret = 0;
1040d30bb512STodor Tomov 
1041d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1042d30bb512STodor Tomov 
1043*0d346d2aSTomi Valkeinen 	__crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad,
1044*0d346d2aSTomi Valkeinen 				       format->which);
1045d30bb512STodor Tomov 
1046d30bb512STodor Tomov 	new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
1047d30bb512STodor Tomov 				ARRAY_SIZE(ov7251_mode_info_data),
1048d30bb512STodor Tomov 				width, height,
1049d30bb512STodor Tomov 				format->format.width, format->format.height);
1050d30bb512STodor Tomov 
1051d30bb512STodor Tomov 	__crop->width = new_mode->width;
1052d30bb512STodor Tomov 	__crop->height = new_mode->height;
1053d30bb512STodor Tomov 
1054d30bb512STodor Tomov 	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1055d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl_int64(ov7251->pixel_clock,
1056d30bb512STodor Tomov 					       new_mode->pixel_clock);
1057d30bb512STodor Tomov 		if (ret < 0)
1058d30bb512STodor Tomov 			goto exit;
1059d30bb512STodor Tomov 
1060d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->link_freq,
1061d30bb512STodor Tomov 					 new_mode->link_freq);
1062d30bb512STodor Tomov 		if (ret < 0)
1063d30bb512STodor Tomov 			goto exit;
1064d30bb512STodor Tomov 
1065d30bb512STodor Tomov 		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
1066d30bb512STodor Tomov 					       1, new_mode->exposure_max,
1067d30bb512STodor Tomov 					       1, new_mode->exposure_def);
1068d30bb512STodor Tomov 		if (ret < 0)
1069d30bb512STodor Tomov 			goto exit;
1070d30bb512STodor Tomov 
1071d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
1072d30bb512STodor Tomov 					 new_mode->exposure_def);
1073d30bb512STodor Tomov 		if (ret < 0)
1074d30bb512STodor Tomov 			goto exit;
1075d30bb512STodor Tomov 
1076d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
1077d30bb512STodor Tomov 		if (ret < 0)
1078d30bb512STodor Tomov 			goto exit;
1079d30bb512STodor Tomov 
1080d30bb512STodor Tomov 		ov7251->current_mode = new_mode;
1081d30bb512STodor Tomov 	}
1082d30bb512STodor Tomov 
1083*0d346d2aSTomi Valkeinen 	__format = __ov7251_get_pad_format(ov7251, sd_state, format->pad,
1084d30bb512STodor Tomov 					   format->which);
1085d30bb512STodor Tomov 	__format->width = __crop->width;
1086d30bb512STodor Tomov 	__format->height = __crop->height;
1087d30bb512STodor Tomov 	__format->code = MEDIA_BUS_FMT_Y10_1X10;
1088d30bb512STodor Tomov 	__format->field = V4L2_FIELD_NONE;
1089d30bb512STodor Tomov 	__format->colorspace = V4L2_COLORSPACE_SRGB;
1090d30bb512STodor Tomov 	__format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
1091d30bb512STodor Tomov 	__format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1092d30bb512STodor Tomov 				__format->colorspace, __format->ycbcr_enc);
1093d30bb512STodor Tomov 	__format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
1094d30bb512STodor Tomov 
1095d30bb512STodor Tomov 	format->format = *__format;
1096d30bb512STodor Tomov 
1097d30bb512STodor Tomov exit:
1098d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1099d30bb512STodor Tomov 
1100d30bb512STodor Tomov 	return ret;
1101d30bb512STodor Tomov }
1102d30bb512STodor Tomov 
1103d30bb512STodor Tomov static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev,
1104*0d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state)
1105d30bb512STodor Tomov {
1106d30bb512STodor Tomov 	struct v4l2_subdev_format fmt = {
1107*0d346d2aSTomi Valkeinen 		.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
1108d30bb512STodor Tomov 		: V4L2_SUBDEV_FORMAT_ACTIVE,
1109d30bb512STodor Tomov 		.format = {
1110d30bb512STodor Tomov 			.width = 640,
1111d30bb512STodor Tomov 			.height = 480
1112d30bb512STodor Tomov 		}
1113d30bb512STodor Tomov 	};
1114d30bb512STodor Tomov 
1115*0d346d2aSTomi Valkeinen 	ov7251_set_format(subdev, sd_state, &fmt);
1116d30bb512STodor Tomov 
1117d30bb512STodor Tomov 	return 0;
1118d30bb512STodor Tomov }
1119d30bb512STodor Tomov 
1120d30bb512STodor Tomov static int ov7251_get_selection(struct v4l2_subdev *sd,
1121*0d346d2aSTomi Valkeinen 				struct v4l2_subdev_state *sd_state,
1122d30bb512STodor Tomov 				struct v4l2_subdev_selection *sel)
1123d30bb512STodor Tomov {
1124d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1125d30bb512STodor Tomov 
1126d30bb512STodor Tomov 	if (sel->target != V4L2_SEL_TGT_CROP)
1127d30bb512STodor Tomov 		return -EINVAL;
1128d30bb512STodor Tomov 
1129d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1130*0d346d2aSTomi Valkeinen 	sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad,
1131d30bb512STodor Tomov 					sel->which);
1132d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1133d30bb512STodor Tomov 
1134d30bb512STodor Tomov 	return 0;
1135d30bb512STodor Tomov }
1136d30bb512STodor Tomov 
1137d30bb512STodor Tomov static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
1138d30bb512STodor Tomov {
1139d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1140d30bb512STodor Tomov 	int ret;
1141d30bb512STodor Tomov 
1142d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1143d30bb512STodor Tomov 
1144d30bb512STodor Tomov 	if (enable) {
1145d30bb512STodor Tomov 		ret = ov7251_set_register_array(ov7251,
1146d30bb512STodor Tomov 					ov7251->current_mode->data,
1147d30bb512STodor Tomov 					ov7251->current_mode->data_size);
1148d30bb512STodor Tomov 		if (ret < 0) {
1149d30bb512STodor Tomov 			dev_err(ov7251->dev, "could not set mode %dx%d\n",
1150d30bb512STodor Tomov 				ov7251->current_mode->width,
1151d30bb512STodor Tomov 				ov7251->current_mode->height);
1152d30bb512STodor Tomov 			goto exit;
1153d30bb512STodor Tomov 		}
1154d30bb512STodor Tomov 		ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
1155d30bb512STodor Tomov 		if (ret < 0) {
1156d30bb512STodor Tomov 			dev_err(ov7251->dev, "could not sync v4l2 controls\n");
1157d30bb512STodor Tomov 			goto exit;
1158d30bb512STodor Tomov 		}
1159d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1160d30bb512STodor Tomov 				       OV7251_SC_MODE_SELECT_STREAMING);
1161d30bb512STodor Tomov 	} else {
1162d30bb512STodor Tomov 		ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1163d30bb512STodor Tomov 				       OV7251_SC_MODE_SELECT_SW_STANDBY);
1164d30bb512STodor Tomov 	}
1165d30bb512STodor Tomov 
1166d30bb512STodor Tomov exit:
1167d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1168d30bb512STodor Tomov 
1169d30bb512STodor Tomov 	return ret;
1170d30bb512STodor Tomov }
1171d30bb512STodor Tomov 
1172d30bb512STodor Tomov static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
1173d30bb512STodor Tomov 				     struct v4l2_subdev_frame_interval *fi)
1174d30bb512STodor Tomov {
1175d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1176d30bb512STodor Tomov 
1177d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1178d30bb512STodor Tomov 	fi->interval = ov7251->current_mode->timeperframe;
1179d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1180d30bb512STodor Tomov 
1181d30bb512STodor Tomov 	return 0;
1182d30bb512STodor Tomov }
1183d30bb512STodor Tomov 
1184d30bb512STodor Tomov static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
1185d30bb512STodor Tomov 				     struct v4l2_subdev_frame_interval *fi)
1186d30bb512STodor Tomov {
1187d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(subdev);
1188d30bb512STodor Tomov 	const struct ov7251_mode_info *new_mode;
1189d30bb512STodor Tomov 	int ret = 0;
1190d30bb512STodor Tomov 
1191d30bb512STodor Tomov 	mutex_lock(&ov7251->lock);
1192d30bb512STodor Tomov 	new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
1193d30bb512STodor Tomov 
1194d30bb512STodor Tomov 	if (new_mode != ov7251->current_mode) {
1195d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl_int64(ov7251->pixel_clock,
1196d30bb512STodor Tomov 					       new_mode->pixel_clock);
1197d30bb512STodor Tomov 		if (ret < 0)
1198d30bb512STodor Tomov 			goto exit;
1199d30bb512STodor Tomov 
1200d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->link_freq,
1201d30bb512STodor Tomov 					 new_mode->link_freq);
1202d30bb512STodor Tomov 		if (ret < 0)
1203d30bb512STodor Tomov 			goto exit;
1204d30bb512STodor Tomov 
1205d30bb512STodor Tomov 		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
1206d30bb512STodor Tomov 					       1, new_mode->exposure_max,
1207d30bb512STodor Tomov 					       1, new_mode->exposure_def);
1208d30bb512STodor Tomov 		if (ret < 0)
1209d30bb512STodor Tomov 			goto exit;
1210d30bb512STodor Tomov 
1211d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
1212d30bb512STodor Tomov 					 new_mode->exposure_def);
1213d30bb512STodor Tomov 		if (ret < 0)
1214d30bb512STodor Tomov 			goto exit;
1215d30bb512STodor Tomov 
1216d30bb512STodor Tomov 		ret = __v4l2_ctrl_s_ctrl(ov7251->gain, 16);
1217d30bb512STodor Tomov 		if (ret < 0)
1218d30bb512STodor Tomov 			goto exit;
1219d30bb512STodor Tomov 
1220d30bb512STodor Tomov 		ov7251->current_mode = new_mode;
1221d30bb512STodor Tomov 	}
1222d30bb512STodor Tomov 
1223d30bb512STodor Tomov 	fi->interval = ov7251->current_mode->timeperframe;
1224d30bb512STodor Tomov 
1225d30bb512STodor Tomov exit:
1226d30bb512STodor Tomov 	mutex_unlock(&ov7251->lock);
1227d30bb512STodor Tomov 
1228d30bb512STodor Tomov 	return ret;
1229d30bb512STodor Tomov }
1230d30bb512STodor Tomov 
1231d30bb512STodor Tomov static const struct v4l2_subdev_core_ops ov7251_core_ops = {
1232d30bb512STodor Tomov 	.s_power = ov7251_s_power,
1233d30bb512STodor Tomov };
1234d30bb512STodor Tomov 
1235d30bb512STodor Tomov static const struct v4l2_subdev_video_ops ov7251_video_ops = {
1236d30bb512STodor Tomov 	.s_stream = ov7251_s_stream,
1237d30bb512STodor Tomov 	.g_frame_interval = ov7251_get_frame_interval,
1238d30bb512STodor Tomov 	.s_frame_interval = ov7251_set_frame_interval,
1239d30bb512STodor Tomov };
1240d30bb512STodor Tomov 
1241d30bb512STodor Tomov static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
1242d30bb512STodor Tomov 	.init_cfg = ov7251_entity_init_cfg,
1243d30bb512STodor Tomov 	.enum_mbus_code = ov7251_enum_mbus_code,
1244d30bb512STodor Tomov 	.enum_frame_size = ov7251_enum_frame_size,
1245d30bb512STodor Tomov 	.enum_frame_interval = ov7251_enum_frame_ival,
1246d30bb512STodor Tomov 	.get_fmt = ov7251_get_format,
1247d30bb512STodor Tomov 	.set_fmt = ov7251_set_format,
1248d30bb512STodor Tomov 	.get_selection = ov7251_get_selection,
1249d30bb512STodor Tomov };
1250d30bb512STodor Tomov 
1251d30bb512STodor Tomov static const struct v4l2_subdev_ops ov7251_subdev_ops = {
1252d30bb512STodor Tomov 	.core = &ov7251_core_ops,
1253d30bb512STodor Tomov 	.video = &ov7251_video_ops,
1254d30bb512STodor Tomov 	.pad = &ov7251_subdev_pad_ops,
1255d30bb512STodor Tomov };
1256d30bb512STodor Tomov 
1257d30bb512STodor Tomov static int ov7251_probe(struct i2c_client *client)
1258d30bb512STodor Tomov {
1259d30bb512STodor Tomov 	struct device *dev = &client->dev;
1260d30bb512STodor Tomov 	struct fwnode_handle *endpoint;
1261d30bb512STodor Tomov 	struct ov7251 *ov7251;
1262d30bb512STodor Tomov 	u8 chip_id_high, chip_id_low, chip_rev;
1263d30bb512STodor Tomov 	int ret;
1264d30bb512STodor Tomov 
1265d30bb512STodor Tomov 	ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
1266d30bb512STodor Tomov 	if (!ov7251)
1267d30bb512STodor Tomov 		return -ENOMEM;
1268d30bb512STodor Tomov 
1269d30bb512STodor Tomov 	ov7251->i2c_client = client;
1270d30bb512STodor Tomov 	ov7251->dev = dev;
1271d30bb512STodor Tomov 
1272d30bb512STodor Tomov 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1273d30bb512STodor Tomov 	if (!endpoint) {
1274d30bb512STodor Tomov 		dev_err(dev, "endpoint node not found\n");
1275d30bb512STodor Tomov 		return -EINVAL;
1276d30bb512STodor Tomov 	}
1277d30bb512STodor Tomov 
1278d30bb512STodor Tomov 	ret = v4l2_fwnode_endpoint_parse(endpoint, &ov7251->ep);
1279d30bb512STodor Tomov 	fwnode_handle_put(endpoint);
1280d30bb512STodor Tomov 	if (ret < 0) {
1281d30bb512STodor Tomov 		dev_err(dev, "parsing endpoint node failed\n");
1282d30bb512STodor Tomov 		return ret;
1283d30bb512STodor Tomov 	}
1284d30bb512STodor Tomov 
12852d95e7edSSakari Ailus 	if (ov7251->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
1286d30bb512STodor Tomov 		dev_err(dev, "invalid bus type (%u), must be CSI2 (%u)\n",
12872d95e7edSSakari Ailus 			ov7251->ep.bus_type, V4L2_MBUS_CSI2_DPHY);
1288d30bb512STodor Tomov 		return -EINVAL;
1289d30bb512STodor Tomov 	}
1290d30bb512STodor Tomov 
1291d30bb512STodor Tomov 	/* get system clock (xclk) */
1292d30bb512STodor Tomov 	ov7251->xclk = devm_clk_get(dev, "xclk");
1293d30bb512STodor Tomov 	if (IS_ERR(ov7251->xclk)) {
1294d30bb512STodor Tomov 		dev_err(dev, "could not get xclk");
1295d30bb512STodor Tomov 		return PTR_ERR(ov7251->xclk);
1296d30bb512STodor Tomov 	}
1297d30bb512STodor Tomov 
1298d30bb512STodor Tomov 	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1299d30bb512STodor Tomov 				       &ov7251->xclk_freq);
1300d30bb512STodor Tomov 	if (ret) {
1301d30bb512STodor Tomov 		dev_err(dev, "could not get xclk frequency\n");
1302d30bb512STodor Tomov 		return ret;
1303d30bb512STodor Tomov 	}
1304d30bb512STodor Tomov 
1305d30bb512STodor Tomov 	/* external clock must be 24MHz, allow 1% tolerance */
1306d30bb512STodor Tomov 	if (ov7251->xclk_freq < 23760000 || ov7251->xclk_freq > 24240000) {
1307d30bb512STodor Tomov 		dev_err(dev, "external clock frequency %u is not supported\n",
1308d30bb512STodor Tomov 			ov7251->xclk_freq);
1309d30bb512STodor Tomov 		return -EINVAL;
1310d30bb512STodor Tomov 	}
1311d30bb512STodor Tomov 
1312d30bb512STodor Tomov 	ret = clk_set_rate(ov7251->xclk, ov7251->xclk_freq);
1313d30bb512STodor Tomov 	if (ret) {
1314d30bb512STodor Tomov 		dev_err(dev, "could not set xclk frequency\n");
1315d30bb512STodor Tomov 		return ret;
1316d30bb512STodor Tomov 	}
1317d30bb512STodor Tomov 
1318d30bb512STodor Tomov 	ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
1319d30bb512STodor Tomov 	if (IS_ERR(ov7251->io_regulator)) {
1320d30bb512STodor Tomov 		dev_err(dev, "cannot get io regulator\n");
1321d30bb512STodor Tomov 		return PTR_ERR(ov7251->io_regulator);
1322d30bb512STodor Tomov 	}
1323d30bb512STodor Tomov 
1324d30bb512STodor Tomov 	ov7251->core_regulator = devm_regulator_get(dev, "vddd");
1325d30bb512STodor Tomov 	if (IS_ERR(ov7251->core_regulator)) {
1326d30bb512STodor Tomov 		dev_err(dev, "cannot get core regulator\n");
1327d30bb512STodor Tomov 		return PTR_ERR(ov7251->core_regulator);
1328d30bb512STodor Tomov 	}
1329d30bb512STodor Tomov 
1330d30bb512STodor Tomov 	ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
1331d30bb512STodor Tomov 	if (IS_ERR(ov7251->analog_regulator)) {
1332d30bb512STodor Tomov 		dev_err(dev, "cannot get analog regulator\n");
1333d30bb512STodor Tomov 		return PTR_ERR(ov7251->analog_regulator);
1334d30bb512STodor Tomov 	}
1335d30bb512STodor Tomov 
1336d30bb512STodor Tomov 	ov7251->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1337d30bb512STodor Tomov 	if (IS_ERR(ov7251->enable_gpio)) {
1338d30bb512STodor Tomov 		dev_err(dev, "cannot get enable gpio\n");
1339d30bb512STodor Tomov 		return PTR_ERR(ov7251->enable_gpio);
1340d30bb512STodor Tomov 	}
1341d30bb512STodor Tomov 
1342d30bb512STodor Tomov 	mutex_init(&ov7251->lock);
1343d30bb512STodor Tomov 
1344d30bb512STodor Tomov 	v4l2_ctrl_handler_init(&ov7251->ctrls, 7);
1345d30bb512STodor Tomov 	ov7251->ctrls.lock = &ov7251->lock;
1346d30bb512STodor Tomov 
1347d30bb512STodor Tomov 	v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1348d30bb512STodor Tomov 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
1349d30bb512STodor Tomov 	v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1350d30bb512STodor Tomov 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
1351d30bb512STodor Tomov 	ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1352d30bb512STodor Tomov 					     V4L2_CID_EXPOSURE, 1, 32, 1, 32);
1353d30bb512STodor Tomov 	ov7251->gain = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1354d30bb512STodor Tomov 					 V4L2_CID_GAIN, 16, 1023, 1, 16);
1355d30bb512STodor Tomov 	v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
1356d30bb512STodor Tomov 				     V4L2_CID_TEST_PATTERN,
1357d30bb512STodor Tomov 				     ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
1358d30bb512STodor Tomov 				     0, 0, ov7251_test_pattern_menu);
1359d30bb512STodor Tomov 	ov7251->pixel_clock = v4l2_ctrl_new_std(&ov7251->ctrls,
1360d30bb512STodor Tomov 						&ov7251_ctrl_ops,
1361d30bb512STodor Tomov 						V4L2_CID_PIXEL_RATE,
1362d30bb512STodor Tomov 						1, INT_MAX, 1, 1);
1363d30bb512STodor Tomov 	ov7251->link_freq = v4l2_ctrl_new_int_menu(&ov7251->ctrls,
1364d30bb512STodor Tomov 						   &ov7251_ctrl_ops,
1365d30bb512STodor Tomov 						   V4L2_CID_LINK_FREQ,
1366d30bb512STodor Tomov 						   ARRAY_SIZE(link_freq) - 1,
1367d30bb512STodor Tomov 						   0, link_freq);
1368d30bb512STodor Tomov 	if (ov7251->link_freq)
1369d30bb512STodor Tomov 		ov7251->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1370d30bb512STodor Tomov 
1371d30bb512STodor Tomov 	ov7251->sd.ctrl_handler = &ov7251->ctrls;
1372d30bb512STodor Tomov 
1373d30bb512STodor Tomov 	if (ov7251->ctrls.error) {
1374d30bb512STodor Tomov 		dev_err(dev, "%s: control initialization error %d\n",
1375d30bb512STodor Tomov 			__func__, ov7251->ctrls.error);
1376d30bb512STodor Tomov 		ret = ov7251->ctrls.error;
1377d30bb512STodor Tomov 		goto free_ctrl;
1378d30bb512STodor Tomov 	}
1379d30bb512STodor Tomov 
1380d30bb512STodor Tomov 	v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
1381d30bb512STodor Tomov 	ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1382d30bb512STodor Tomov 	ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
1383d30bb512STodor Tomov 	ov7251->sd.dev = &client->dev;
1384d30bb512STodor Tomov 	ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1385d30bb512STodor Tomov 
1386d30bb512STodor Tomov 	ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
1387d30bb512STodor Tomov 	if (ret < 0) {
1388d30bb512STodor Tomov 		dev_err(dev, "could not register media entity\n");
1389d30bb512STodor Tomov 		goto free_ctrl;
1390d30bb512STodor Tomov 	}
1391d30bb512STodor Tomov 
1392d30bb512STodor Tomov 	ret = ov7251_s_power(&ov7251->sd, true);
1393d30bb512STodor Tomov 	if (ret < 0) {
1394d30bb512STodor Tomov 		dev_err(dev, "could not power up OV7251\n");
1395d30bb512STodor Tomov 		goto free_entity;
1396d30bb512STodor Tomov 	}
1397d30bb512STodor Tomov 
1398d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
1399d30bb512STodor Tomov 	if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE) {
1400d30bb512STodor Tomov 		dev_err(dev, "could not read ID high\n");
1401d30bb512STodor Tomov 		ret = -ENODEV;
1402d30bb512STodor Tomov 		goto power_down;
1403d30bb512STodor Tomov 	}
1404d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
1405d30bb512STodor Tomov 	if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE) {
1406d30bb512STodor Tomov 		dev_err(dev, "could not read ID low\n");
1407d30bb512STodor Tomov 		ret = -ENODEV;
1408d30bb512STodor Tomov 		goto power_down;
1409d30bb512STodor Tomov 	}
1410d30bb512STodor Tomov 
1411d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
1412d30bb512STodor Tomov 	if (ret < 0) {
1413d30bb512STodor Tomov 		dev_err(dev, "could not read revision\n");
1414d30bb512STodor Tomov 		ret = -ENODEV;
1415d30bb512STodor Tomov 		goto power_down;
1416d30bb512STodor Tomov 	}
1417d30bb512STodor Tomov 	chip_rev >>= 4;
1418d30bb512STodor Tomov 
1419d30bb512STodor Tomov 	dev_info(dev, "OV7251 revision %x (%s) detected at address 0x%02x\n",
1420d30bb512STodor Tomov 		 chip_rev,
1421d30bb512STodor Tomov 		 chip_rev == 0x4 ? "1A / 1B" :
1422d30bb512STodor Tomov 		 chip_rev == 0x5 ? "1C / 1D" :
1423d30bb512STodor Tomov 		 chip_rev == 0x6 ? "1E" :
1424d30bb512STodor Tomov 		 chip_rev == 0x7 ? "1F" : "unknown",
1425d30bb512STodor Tomov 		 client->addr);
1426d30bb512STodor Tomov 
1427d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
1428d30bb512STodor Tomov 			      &ov7251->pre_isp_00);
1429d30bb512STodor Tomov 	if (ret < 0) {
1430d30bb512STodor Tomov 		dev_err(dev, "could not read test pattern value\n");
1431d30bb512STodor Tomov 		ret = -ENODEV;
1432d30bb512STodor Tomov 		goto power_down;
1433d30bb512STodor Tomov 	}
1434d30bb512STodor Tomov 
1435d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
1436d30bb512STodor Tomov 			      &ov7251->timing_format1);
1437d30bb512STodor Tomov 	if (ret < 0) {
1438d30bb512STodor Tomov 		dev_err(dev, "could not read vflip value\n");
1439d30bb512STodor Tomov 		ret = -ENODEV;
1440d30bb512STodor Tomov 		goto power_down;
1441d30bb512STodor Tomov 	}
1442d30bb512STodor Tomov 
1443d30bb512STodor Tomov 	ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
1444d30bb512STodor Tomov 			      &ov7251->timing_format2);
1445d30bb512STodor Tomov 	if (ret < 0) {
1446d30bb512STodor Tomov 		dev_err(dev, "could not read hflip value\n");
1447d30bb512STodor Tomov 		ret = -ENODEV;
1448d30bb512STodor Tomov 		goto power_down;
1449d30bb512STodor Tomov 	}
1450d30bb512STodor Tomov 
1451d30bb512STodor Tomov 	ov7251_s_power(&ov7251->sd, false);
1452d30bb512STodor Tomov 
1453d30bb512STodor Tomov 	ret = v4l2_async_register_subdev(&ov7251->sd);
1454d30bb512STodor Tomov 	if (ret < 0) {
1455d30bb512STodor Tomov 		dev_err(dev, "could not register v4l2 device\n");
1456d30bb512STodor Tomov 		goto free_entity;
1457d30bb512STodor Tomov 	}
1458d30bb512STodor Tomov 
1459d30bb512STodor Tomov 	ov7251_entity_init_cfg(&ov7251->sd, NULL);
1460d30bb512STodor Tomov 
1461d30bb512STodor Tomov 	return 0;
1462d30bb512STodor Tomov 
1463d30bb512STodor Tomov power_down:
1464d30bb512STodor Tomov 	ov7251_s_power(&ov7251->sd, false);
1465d30bb512STodor Tomov free_entity:
1466d30bb512STodor Tomov 	media_entity_cleanup(&ov7251->sd.entity);
1467d30bb512STodor Tomov free_ctrl:
1468d30bb512STodor Tomov 	v4l2_ctrl_handler_free(&ov7251->ctrls);
1469d30bb512STodor Tomov 	mutex_destroy(&ov7251->lock);
1470d30bb512STodor Tomov 
1471d30bb512STodor Tomov 	return ret;
1472d30bb512STodor Tomov }
1473d30bb512STodor Tomov 
1474d30bb512STodor Tomov static int ov7251_remove(struct i2c_client *client)
1475d30bb512STodor Tomov {
1476d30bb512STodor Tomov 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1477d30bb512STodor Tomov 	struct ov7251 *ov7251 = to_ov7251(sd);
1478d30bb512STodor Tomov 
1479d30bb512STodor Tomov 	v4l2_async_unregister_subdev(&ov7251->sd);
1480d30bb512STodor Tomov 	media_entity_cleanup(&ov7251->sd.entity);
1481d30bb512STodor Tomov 	v4l2_ctrl_handler_free(&ov7251->ctrls);
1482d30bb512STodor Tomov 	mutex_destroy(&ov7251->lock);
1483d30bb512STodor Tomov 
1484d30bb512STodor Tomov 	return 0;
1485d30bb512STodor Tomov }
1486d30bb512STodor Tomov 
1487d30bb512STodor Tomov static const struct of_device_id ov7251_of_match[] = {
1488d30bb512STodor Tomov 	{ .compatible = "ovti,ov7251" },
1489d30bb512STodor Tomov 	{ /* sentinel */ }
1490d30bb512STodor Tomov };
1491d30bb512STodor Tomov MODULE_DEVICE_TABLE(of, ov7251_of_match);
1492d30bb512STodor Tomov 
1493d30bb512STodor Tomov static struct i2c_driver ov7251_i2c_driver = {
1494d30bb512STodor Tomov 	.driver = {
1495d30bb512STodor Tomov 		.of_match_table = ov7251_of_match,
1496d30bb512STodor Tomov 		.name  = "ov7251",
1497d30bb512STodor Tomov 	},
1498d30bb512STodor Tomov 	.probe_new  = ov7251_probe,
1499d30bb512STodor Tomov 	.remove = ov7251_remove,
1500d30bb512STodor Tomov };
1501d30bb512STodor Tomov 
1502d30bb512STodor Tomov module_i2c_driver(ov7251_i2c_driver);
1503d30bb512STodor Tomov 
1504d30bb512STodor Tomov MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
1505d30bb512STodor Tomov MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1506d30bb512STodor Tomov MODULE_LICENSE("GPL v2");
1507