xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 7587cdef)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4  * Copyright (C) 2014-2017 Mentor Graphics Inc.
5  */
6 
7 #include <linux/clk.h>
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
28 
29 /* min/typical/max system clock (xclk) frequencies */
30 #define OV5640_XCLK_MIN  6000000
31 #define OV5640_XCLK_MAX 54000000
32 
33 #define OV5640_NATIVE_WIDTH		2624
34 #define OV5640_NATIVE_HEIGHT		1964
35 #define OV5640_PIXEL_ARRAY_TOP		14
36 #define OV5640_PIXEL_ARRAY_LEFT		16
37 #define OV5640_PIXEL_ARRAY_WIDTH	2592
38 #define OV5640_PIXEL_ARRAY_HEIGHT	1944
39 
40 /* FIXME: not documented. */
41 #define OV5640_MIN_VBLANK	24
42 #define OV5640_MAX_VTS		3375
43 
44 #define OV5640_DEFAULT_SLAVE_ID 0x3c
45 
46 #define OV5640_LINK_RATE_MAX		490000000U
47 
48 #define OV5640_REG_SYS_RESET02		0x3002
49 #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
50 #define OV5640_REG_SYS_CTRL0		0x3008
51 #define OV5640_REG_SYS_CTRL0_SW_PWDN	0x42
52 #define OV5640_REG_SYS_CTRL0_SW_PWUP	0x02
53 #define OV5640_REG_CHIP_ID		0x300a
54 #define OV5640_REG_IO_MIPI_CTRL00	0x300e
55 #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
56 #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
57 #define OV5640_REG_PAD_OUTPUT00		0x3019
58 #define OV5640_REG_SYSTEM_CONTROL1	0x302e
59 #define OV5640_REG_SC_PLL_CTRL0		0x3034
60 #define OV5640_REG_SC_PLL_CTRL1		0x3035
61 #define OV5640_REG_SC_PLL_CTRL2		0x3036
62 #define OV5640_REG_SC_PLL_CTRL3		0x3037
63 #define OV5640_REG_SLAVE_ID		0x3100
64 #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
65 #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
66 #define OV5640_REG_AWB_R_GAIN		0x3400
67 #define OV5640_REG_AWB_G_GAIN		0x3402
68 #define OV5640_REG_AWB_B_GAIN		0x3404
69 #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
70 #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
71 #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
72 #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
73 #define OV5640_REG_AEC_PK_MANUAL	0x3503
74 #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
75 #define OV5640_REG_AEC_PK_VTS		0x350c
76 #define OV5640_REG_TIMING_HS		0x3800
77 #define OV5640_REG_TIMING_VS		0x3802
78 #define OV5640_REG_TIMING_HW		0x3804
79 #define OV5640_REG_TIMING_VH		0x3806
80 #define OV5640_REG_TIMING_DVPHO		0x3808
81 #define OV5640_REG_TIMING_DVPVO		0x380a
82 #define OV5640_REG_TIMING_HTS		0x380c
83 #define OV5640_REG_TIMING_VTS		0x380e
84 #define OV5640_REG_TIMING_HOFFS		0x3810
85 #define OV5640_REG_TIMING_VOFFS		0x3812
86 #define OV5640_REG_TIMING_TC_REG20	0x3820
87 #define OV5640_REG_TIMING_TC_REG21	0x3821
88 #define OV5640_REG_AEC_CTRL00		0x3a00
89 #define OV5640_REG_AEC_B50_STEP		0x3a08
90 #define OV5640_REG_AEC_B60_STEP		0x3a0a
91 #define OV5640_REG_AEC_CTRL0D		0x3a0d
92 #define OV5640_REG_AEC_CTRL0E		0x3a0e
93 #define OV5640_REG_AEC_CTRL0F		0x3a0f
94 #define OV5640_REG_AEC_CTRL10		0x3a10
95 #define OV5640_REG_AEC_CTRL11		0x3a11
96 #define OV5640_REG_AEC_CTRL1B		0x3a1b
97 #define OV5640_REG_AEC_CTRL1E		0x3a1e
98 #define OV5640_REG_AEC_CTRL1F		0x3a1f
99 #define OV5640_REG_HZ5060_CTRL00	0x3c00
100 #define OV5640_REG_HZ5060_CTRL01	0x3c01
101 #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
102 #define OV5640_REG_FRAME_CTRL01		0x4202
103 #define OV5640_REG_FORMAT_CONTROL00	0x4300
104 #define OV5640_REG_VFIFO_HSIZE		0x4602
105 #define OV5640_REG_VFIFO_VSIZE		0x4604
106 #define OV5640_REG_JPG_MODE_SELECT	0x4713
107 #define OV5640_REG_CCIR656_CTRL00	0x4730
108 #define OV5640_REG_POLARITY_CTRL00	0x4740
109 #define OV5640_REG_MIPI_CTRL00		0x4800
110 #define OV5640_REG_DEBUG_MODE		0x4814
111 #define OV5640_REG_PCLK_PERIOD		0x4837
112 #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
113 #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
114 #define OV5640_REG_SDE_CTRL0		0x5580
115 #define OV5640_REG_SDE_CTRL1		0x5581
116 #define OV5640_REG_SDE_CTRL3		0x5583
117 #define OV5640_REG_SDE_CTRL4		0x5584
118 #define OV5640_REG_SDE_CTRL5		0x5585
119 #define OV5640_REG_AVG_READOUT		0x56a1
120 
121 enum ov5640_mode_id {
122 	OV5640_MODE_QQVGA_160_120 = 0,
123 	OV5640_MODE_QCIF_176_144,
124 	OV5640_MODE_QVGA_320_240,
125 	OV5640_MODE_VGA_640_480,
126 	OV5640_MODE_NTSC_720_480,
127 	OV5640_MODE_PAL_720_576,
128 	OV5640_MODE_XGA_1024_768,
129 	OV5640_MODE_720P_1280_720,
130 	OV5640_MODE_1080P_1920_1080,
131 	OV5640_MODE_QSXGA_2592_1944,
132 	OV5640_NUM_MODES,
133 };
134 
135 enum ov5640_frame_rate {
136 	OV5640_15_FPS = 0,
137 	OV5640_30_FPS,
138 	OV5640_60_FPS,
139 	OV5640_NUM_FRAMERATES,
140 };
141 
142 enum ov5640_pixel_rate_id {
143 	OV5640_PIXEL_RATE_168M,
144 	OV5640_PIXEL_RATE_148M,
145 	OV5640_PIXEL_RATE_124M,
146 	OV5640_PIXEL_RATE_96M,
147 	OV5640_PIXEL_RATE_48M,
148 	OV5640_NUM_PIXEL_RATES,
149 };
150 
151 /*
152  * The chip manual suggests 24/48/96/192 MHz pixel clocks.
153  *
154  * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
155  * full resolution mode @15 FPS.
156  */
157 static const u32 ov5640_pixel_rates[] = {
158 	[OV5640_PIXEL_RATE_168M] = 168000000,
159 	[OV5640_PIXEL_RATE_148M] = 148000000,
160 	[OV5640_PIXEL_RATE_124M] = 124000000,
161 	[OV5640_PIXEL_RATE_96M] = 96000000,
162 	[OV5640_PIXEL_RATE_48M] = 48000000,
163 };
164 
165 /*
166  * MIPI CSI-2 link frequencies.
167  *
168  * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
169  * data_lanes = (1, 2)
170  *
171  * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
172  */
173 static const s64 ov5640_csi2_link_freqs[] = {
174 	992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
175 	592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
176 	384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
177 	248000000, 192000000, 192000000, 192000000, 96000000,
178 };
179 
180 /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
181 #define OV5640_DEFAULT_LINK_FREQ	13
182 
183 enum ov5640_format_mux {
184 	OV5640_FMT_MUX_YUV422 = 0,
185 	OV5640_FMT_MUX_RGB,
186 	OV5640_FMT_MUX_DITHER,
187 	OV5640_FMT_MUX_RAW_DPC,
188 	OV5640_FMT_MUX_SNR_RAW,
189 	OV5640_FMT_MUX_RAW_CIP,
190 };
191 
192 struct ov5640_pixfmt {
193 	u32 code;
194 	u32 colorspace;
195 	u8 bpp;
196 	u8 ctrl00;
197 	enum ov5640_format_mux mux;
198 };
199 
200 static const struct ov5640_pixfmt ov5640_dvp_formats[] = {
201 	{
202 		/* YUV422, YUYV */
203 		.code		= MEDIA_BUS_FMT_JPEG_1X8,
204 		.colorspace	= V4L2_COLORSPACE_JPEG,
205 		.bpp		= 16,
206 		.ctrl00		= 0x30,
207 		.mux		= OV5640_FMT_MUX_YUV422,
208 	}, {
209 		/* YUV422, UYVY */
210 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
211 		.colorspace	= V4L2_COLORSPACE_SRGB,
212 		.bpp		= 16,
213 		.ctrl00		= 0x3f,
214 		.mux		= OV5640_FMT_MUX_YUV422,
215 	}, {
216 		/* YUV422, YUYV */
217 		.code		= MEDIA_BUS_FMT_YUYV8_2X8,
218 		.colorspace	= V4L2_COLORSPACE_SRGB,
219 		.bpp		= 16,
220 		.ctrl00		= 0x30,
221 		.mux		= OV5640_FMT_MUX_YUV422,
222 	}, {
223 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
224 		.code		= MEDIA_BUS_FMT_RGB565_2X8_LE,
225 		.colorspace	= V4L2_COLORSPACE_SRGB,
226 		.bpp		= 16,
227 		.ctrl00		= 0x6f,
228 		.mux		= OV5640_FMT_MUX_RGB,
229 	}, {
230 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
231 		.code		= MEDIA_BUS_FMT_RGB565_2X8_BE,
232 		.colorspace	= V4L2_COLORSPACE_SRGB,
233 		.bpp		= 16,
234 		.ctrl00		= 0x61,
235 		.mux		= OV5640_FMT_MUX_RGB,
236 	}, {
237 		/* Raw, BGBG... / GRGR... */
238 		.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
239 		.colorspace	= V4L2_COLORSPACE_SRGB,
240 		.bpp		= 8,
241 		.ctrl00		= 0x00,
242 		.mux		= OV5640_FMT_MUX_RAW_DPC,
243 	}, {
244 		/* Raw bayer, GBGB... / RGRG... */
245 		.code		= MEDIA_BUS_FMT_SGBRG8_1X8,
246 		.colorspace	= V4L2_COLORSPACE_SRGB,
247 		.bpp		= 8,
248 		.ctrl00		= 0x01,
249 		.mux		= OV5640_FMT_MUX_RAW_DPC,
250 	}, {
251 		/* Raw bayer, GRGR... / BGBG... */
252 		.code		= MEDIA_BUS_FMT_SGRBG8_1X8,
253 		.colorspace	= V4L2_COLORSPACE_SRGB,
254 		.bpp		= 8,
255 		.ctrl00		= 0x02,
256 		.mux		= OV5640_FMT_MUX_RAW_DPC,
257 	}, {
258 		/* Raw bayer, RGRG... / GBGB... */
259 		.code		= MEDIA_BUS_FMT_SRGGB8_1X8,
260 		.colorspace	= V4L2_COLORSPACE_SRGB,
261 		.bpp		= 8,
262 		.ctrl00		= 0x03,
263 		.mux		= OV5640_FMT_MUX_RAW_DPC,
264 	},
265 	{ /* sentinel */ }
266 };
267 
268 static const struct ov5640_pixfmt ov5640_csi2_formats[] = {
269 	{
270 		/* YUV422, YUYV */
271 		.code		= MEDIA_BUS_FMT_JPEG_1X8,
272 		.colorspace	= V4L2_COLORSPACE_JPEG,
273 		.bpp		= 16,
274 		.ctrl00		= 0x30,
275 		.mux		= OV5640_FMT_MUX_YUV422,
276 	}, {
277 		/* YUV422, UYVY */
278 		.code		= MEDIA_BUS_FMT_UYVY8_1X16,
279 		.colorspace	= V4L2_COLORSPACE_SRGB,
280 		.bpp		= 16,
281 		.ctrl00		= 0x3f,
282 		.mux		= OV5640_FMT_MUX_YUV422,
283 	}, {
284 		/* YUV422, YUYV */
285 		.code		= MEDIA_BUS_FMT_YUYV8_1X16,
286 		.colorspace	= V4L2_COLORSPACE_SRGB,
287 		.bpp		= 16,
288 		.ctrl00		= 0x30,
289 		.mux		= OV5640_FMT_MUX_YUV422,
290 	}, {
291 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
292 		.code		= MEDIA_BUS_FMT_RGB565_1X16,
293 		.colorspace	= V4L2_COLORSPACE_SRGB,
294 		.bpp		= 16,
295 		.ctrl00		= 0x6f,
296 		.mux		= OV5640_FMT_MUX_RGB,
297 	}, {
298 		/* BGR888: RGB */
299 		.code		= MEDIA_BUS_FMT_BGR888_1X24,
300 		.colorspace	= V4L2_COLORSPACE_SRGB,
301 		.bpp		= 24,
302 		.ctrl00		= 0x23,
303 		.mux		= OV5640_FMT_MUX_RGB,
304 	}, {
305 		/* Raw, BGBG... / GRGR... */
306 		.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
307 		.colorspace	= V4L2_COLORSPACE_SRGB,
308 		.bpp		= 8,
309 		.ctrl00		= 0x00,
310 		.mux		= OV5640_FMT_MUX_RAW_DPC,
311 	}, {
312 		/* Raw bayer, GBGB... / RGRG... */
313 		.code		= MEDIA_BUS_FMT_SGBRG8_1X8,
314 		.colorspace	= V4L2_COLORSPACE_SRGB,
315 		.bpp		= 8,
316 		.ctrl00		= 0x01,
317 		.mux		= OV5640_FMT_MUX_RAW_DPC,
318 	}, {
319 		/* Raw bayer, GRGR... / BGBG... */
320 		.code		= MEDIA_BUS_FMT_SGRBG8_1X8,
321 		.colorspace	= V4L2_COLORSPACE_SRGB,
322 		.bpp		= 8,
323 		.ctrl00		= 0x02,
324 		.mux		= OV5640_FMT_MUX_RAW_DPC,
325 	}, {
326 		/* Raw bayer, RGRG... / GBGB... */
327 		.code		= MEDIA_BUS_FMT_SRGGB8_1X8,
328 		.colorspace	= V4L2_COLORSPACE_SRGB,
329 		.bpp		= 8,
330 		.ctrl00		= 0x03,
331 		.mux		= OV5640_FMT_MUX_RAW_DPC,
332 	},
333 	{ /* sentinel */ }
334 };
335 
336 /*
337  * FIXME: remove this when a subdev API becomes available
338  * to set the MIPI CSI-2 virtual channel.
339  */
340 static unsigned int virtual_channel;
341 module_param(virtual_channel, uint, 0444);
342 MODULE_PARM_DESC(virtual_channel,
343 		 "MIPI CSI-2 virtual channel (0..3), default 0");
344 
345 static const int ov5640_framerates[] = {
346 	[OV5640_15_FPS] = 15,
347 	[OV5640_30_FPS] = 30,
348 	[OV5640_60_FPS] = 60,
349 };
350 
351 /* regulator supplies */
352 static const char * const ov5640_supply_name[] = {
353 	"DOVDD", /* Digital I/O (1.8V) supply */
354 	"AVDD",  /* Analog (2.8V) supply */
355 	"DVDD",  /* Digital Core (1.5V) supply */
356 };
357 
358 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
359 
360 /*
361  * Image size under 1280 * 960 are SUBSAMPLING
362  * Image size upper 1280 * 960 are SCALING
363  */
364 enum ov5640_downsize_mode {
365 	SUBSAMPLING,
366 	SCALING,
367 };
368 
369 struct reg_value {
370 	u16 reg_addr;
371 	u8 val;
372 	u8 mask;
373 	u32 delay_ms;
374 };
375 
376 struct ov5640_timings {
377 	/* Analog crop rectangle. */
378 	struct v4l2_rect analog_crop;
379 	/* Visibile crop: from analog crop top-left corner. */
380 	struct v4l2_rect crop;
381 	/* Total pixels per line: width + fixed hblank. */
382 	u32 htot;
383 	/* Default vertical blanking: frame height = height + vblank. */
384 	u32 vblank_def;
385 };
386 
387 struct ov5640_mode_info {
388 	enum ov5640_mode_id id;
389 	enum ov5640_downsize_mode dn_mode;
390 	enum ov5640_pixel_rate_id pixel_rate;
391 
392 	unsigned int width;
393 	unsigned int height;
394 
395 	struct ov5640_timings dvp_timings;
396 	struct ov5640_timings csi2_timings;
397 
398 	const struct reg_value *reg_data;
399 	u32 reg_data_size;
400 
401 	/* Used by s_frame_interval only. */
402 	u32 max_fps;
403 	u32 def_fps;
404 };
405 
406 struct ov5640_ctrls {
407 	struct v4l2_ctrl_handler handler;
408 	struct v4l2_ctrl *pixel_rate;
409 	struct v4l2_ctrl *link_freq;
410 	struct v4l2_ctrl *hblank;
411 	struct v4l2_ctrl *vblank;
412 	struct {
413 		struct v4l2_ctrl *auto_exp;
414 		struct v4l2_ctrl *exposure;
415 	};
416 	struct {
417 		struct v4l2_ctrl *auto_wb;
418 		struct v4l2_ctrl *blue_balance;
419 		struct v4l2_ctrl *red_balance;
420 	};
421 	struct {
422 		struct v4l2_ctrl *auto_gain;
423 		struct v4l2_ctrl *gain;
424 	};
425 	struct v4l2_ctrl *brightness;
426 	struct v4l2_ctrl *light_freq;
427 	struct v4l2_ctrl *saturation;
428 	struct v4l2_ctrl *contrast;
429 	struct v4l2_ctrl *hue;
430 	struct v4l2_ctrl *test_pattern;
431 	struct v4l2_ctrl *hflip;
432 	struct v4l2_ctrl *vflip;
433 };
434 
435 struct ov5640_dev {
436 	struct i2c_client *i2c_client;
437 	struct v4l2_subdev sd;
438 	struct media_pad pad;
439 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
440 	struct clk *xclk; /* system clock to OV5640 */
441 	u32 xclk_freq;
442 
443 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
444 	struct gpio_desc *reset_gpio;
445 	struct gpio_desc *pwdn_gpio;
446 	bool   upside_down;
447 
448 	/* lock to protect all members below */
449 	struct mutex lock;
450 
451 	struct v4l2_mbus_framefmt fmt;
452 	bool pending_fmt_change;
453 
454 	const struct ov5640_mode_info *current_mode;
455 	const struct ov5640_mode_info *last_mode;
456 	enum ov5640_frame_rate current_fr;
457 	struct v4l2_fract frame_interval;
458 	s64 current_link_freq;
459 
460 	struct ov5640_ctrls ctrls;
461 
462 	u32 prev_sysclk, prev_hts;
463 	u32 ae_low, ae_high, ae_target;
464 
465 	bool pending_mode_change;
466 	bool streaming;
467 };
468 
469 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
470 {
471 	return container_of(sd, struct ov5640_dev, sd);
472 }
473 
474 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
475 {
476 	return &container_of(ctrl->handler, struct ov5640_dev,
477 			     ctrls.handler)->sd;
478 }
479 
480 static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
481 {
482 	return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
483 }
484 
485 static inline const struct ov5640_pixfmt *
486 ov5640_formats(struct ov5640_dev *sensor)
487 {
488 	return ov5640_is_csi2(sensor) ? ov5640_csi2_formats
489 				      : ov5640_dvp_formats;
490 }
491 
492 static const struct ov5640_pixfmt *
493 ov5640_code_to_pixfmt(struct ov5640_dev *sensor, u32 code)
494 {
495 	const struct ov5640_pixfmt *formats = ov5640_formats(sensor);
496 	unsigned int i;
497 
498 	for (i = 0; formats[i].code; ++i) {
499 		if (formats[i].code == code)
500 			return &formats[i];
501 	}
502 
503 	return &formats[0];
504 }
505 
506 static u32 ov5640_code_to_bpp(struct ov5640_dev *sensor, u32 code)
507 {
508 	const struct ov5640_pixfmt *format = ov5640_code_to_pixfmt(sensor,
509 								   code);
510 
511 	return format->bpp;
512 }
513 
514 /*
515  * FIXME: all of these register tables are likely filled with
516  * entries that set the register to their power-on default values,
517  * and which are otherwise not touched by this driver. Those entries
518  * should be identified and removed to speed register load time
519  * over i2c.
520  */
521 /* YUV422 UYVY VGA@30fps */
522 
523 static const struct v4l2_mbus_framefmt ov5640_default_fmt = {
524 	.code = MEDIA_BUS_FMT_UYVY8_2X8,
525 	.width = 640,
526 	.height = 480,
527 	.colorspace = V4L2_COLORSPACE_SRGB,
528 	.ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
529 	.quantization = V4L2_QUANTIZATION_FULL_RANGE,
530 	.xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
531 	.field = V4L2_FIELD_NONE,
532 };
533 
534 static const struct reg_value ov5640_init_setting[] = {
535 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
536 	{0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
537 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
538 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
539 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
540 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
541 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
542 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
543 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
544 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
545 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
546 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
547 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
548 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
549 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
550 	{0x3815, 0x31, 0, 0},
551 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
552 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
553 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
554 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
555 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
556 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
557 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
558 	{0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
559 	{0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
560 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
561 	{0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
562 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
563 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
564 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
565 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
566 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
567 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
568 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
569 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
570 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
571 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
572 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
573 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
574 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
575 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
576 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
577 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
578 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
579 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
580 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
581 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
582 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
583 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
584 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
585 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
586 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
587 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
588 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
589 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
590 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
591 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
592 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
593 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
594 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
595 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
596 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
597 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
598 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
599 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
600 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
601 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
602 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
603 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
604 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
605 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
606 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
607 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
608 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
609 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
610 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
611 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
612 };
613 
614 static const struct reg_value ov5640_setting_low_res[] = {
615 	{0x3c07, 0x08, 0, 0},
616 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
617 	{0x3814, 0x31, 0, 0},
618 	{0x3815, 0x31, 0, 0},
619 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
620 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
621 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
622 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
623 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
624 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
625 	{0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
626 };
627 
628 static const struct reg_value ov5640_setting_720P_1280_720[] = {
629 	{0x3c07, 0x07, 0, 0},
630 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
631 	{0x3814, 0x31, 0, 0},
632 	{0x3815, 0x31, 0, 0},
633 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
634 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
635 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
636 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
637 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
638 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
639 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
640 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
641 };
642 
643 static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
644 	{0x3c07, 0x08, 0, 0},
645 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
646 	{0x3814, 0x11, 0, 0},
647 	{0x3815, 0x11, 0, 0},
648 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
649 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
650 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
651 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
652 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
653 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
654 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
655 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
656 	{0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
657 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
658 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
659 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
660 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
661 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
662 	{0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
663 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
664 	{0x4005, 0x1a, 0, 0},
665 };
666 
667 static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
668 	{0x3c07, 0x08, 0, 0},
669 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
670 	{0x3814, 0x11, 0, 0},
671 	{0x3815, 0x11, 0, 0},
672 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
673 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
674 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
675 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
676 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
677 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
678 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
679 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
680 };
681 
682 static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
683 	{
684 		/* 160x120 */
685 		.id		= OV5640_MODE_QQVGA_160_120,
686 		.dn_mode	= SUBSAMPLING,
687 		.pixel_rate	= OV5640_PIXEL_RATE_48M,
688 		.width		= 160,
689 		.height		= 120,
690 		.dvp_timings = {
691 			.analog_crop = {
692 				.left	= 0,
693 				.top	= 4,
694 				.width	= 2624,
695 				.height	= 1944,
696 			},
697 			.crop = {
698 				.left	= 16,
699 				.top	= 6,
700 				.width	= 160,
701 				.height	= 120,
702 			},
703 			.htot		= 1896,
704 			.vblank_def	= 864,
705 		},
706 		.csi2_timings = {
707 			/* Feed the full valid pixel array to the ISP. */
708 			.analog_crop = {
709 				.left	= OV5640_PIXEL_ARRAY_LEFT,
710 				.top	= OV5640_PIXEL_ARRAY_TOP,
711 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
712 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
713 			},
714 			/* Maintain a minimum processing margin. */
715 			.crop = {
716 				.left	= 2,
717 				.top	= 4,
718 				.width	= 160,
719 				.height	= 120,
720 			},
721 			.htot		= 1600,
722 			.vblank_def	= 878,
723 		},
724 		.reg_data	= ov5640_setting_low_res,
725 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
726 		.max_fps	= OV5640_30_FPS,
727 		.def_fps	= OV5640_30_FPS
728 	}, {
729 		/* 176x144 */
730 		.id		= OV5640_MODE_QCIF_176_144,
731 		.dn_mode	= SUBSAMPLING,
732 		.pixel_rate	= OV5640_PIXEL_RATE_48M,
733 		.width		= 176,
734 		.height		= 144,
735 		.dvp_timings = {
736 			.analog_crop = {
737 				.left	= 0,
738 				.top	= 4,
739 				.width	= 2624,
740 				.height	= 1944,
741 			},
742 			.crop = {
743 				.left	= 16,
744 				.top	= 6,
745 				.width	= 176,
746 				.height	= 144,
747 			},
748 			.htot		= 1896,
749 			.vblank_def	= 840,
750 		},
751 		.csi2_timings = {
752 			/* Feed the full valid pixel array to the ISP. */
753 			.analog_crop = {
754 				.left	= OV5640_PIXEL_ARRAY_LEFT,
755 				.top	= OV5640_PIXEL_ARRAY_TOP,
756 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
757 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
758 			},
759 			/* Maintain a minimum processing margin. */
760 			.crop = {
761 				.left	= 2,
762 				.top	= 4,
763 				.width	= 176,
764 				.height	= 144,
765 			},
766 			.htot		= 1600,
767 			.vblank_def	= 854,
768 		},
769 		.reg_data	= ov5640_setting_low_res,
770 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
771 		.max_fps	= OV5640_30_FPS,
772 		.def_fps	= OV5640_30_FPS
773 	}, {
774 		/* 320x240 */
775 		.id		= OV5640_MODE_QVGA_320_240,
776 		.dn_mode	= SUBSAMPLING,
777 		.width		= 320,
778 		.height		= 240,
779 		.pixel_rate	= OV5640_PIXEL_RATE_48M,
780 		.dvp_timings = {
781 			.analog_crop = {
782 				.left	= 0,
783 				.top	= 4,
784 				.width	= 2624,
785 				.height	= 1944,
786 			},
787 			.crop = {
788 				.left	= 16,
789 				.top	= 6,
790 				.width	= 320,
791 				.height	= 240,
792 			},
793 			.htot		= 1896,
794 			.vblank_def	= 744,
795 		},
796 		.csi2_timings = {
797 			/* Feed the full valid pixel array to the ISP. */
798 			.analog_crop = {
799 				.left	= OV5640_PIXEL_ARRAY_LEFT,
800 				.top	= OV5640_PIXEL_ARRAY_TOP,
801 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
802 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
803 			},
804 			/* Maintain a minimum processing margin. */
805 			.crop = {
806 				.left	= 2,
807 				.top	= 4,
808 				.width	= 320,
809 				.height	= 240,
810 			},
811 			.htot		= 1600,
812 			.vblank_def	= 760,
813 		},
814 		.reg_data	= ov5640_setting_low_res,
815 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
816 		.max_fps	= OV5640_30_FPS,
817 		.def_fps	= OV5640_30_FPS
818 	}, {
819 		/* 640x480 */
820 		.id		= OV5640_MODE_VGA_640_480,
821 		.dn_mode	= SUBSAMPLING,
822 		.pixel_rate	= OV5640_PIXEL_RATE_48M,
823 		.width		= 640,
824 		.height		= 480,
825 		.dvp_timings = {
826 			.analog_crop = {
827 				.left	= 0,
828 				.top	= 4,
829 				.width	= 2624,
830 				.height	= 1944,
831 			},
832 			.crop = {
833 				.left	= 16,
834 				.top	= 6,
835 				.width	= 640,
836 				.height	= 480,
837 			},
838 			.htot		= 1896,
839 			.vblank_def	= 600,
840 		},
841 		.csi2_timings = {
842 			/* Feed the full valid pixel array to the ISP. */
843 			.analog_crop = {
844 				.left	= OV5640_PIXEL_ARRAY_LEFT,
845 				.top	= OV5640_PIXEL_ARRAY_TOP,
846 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
847 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
848 			},
849 			/* Maintain a minimum processing margin. */
850 			.crop = {
851 				.left	= 2,
852 				.top	= 4,
853 				.width	= 640,
854 				.height	= 480,
855 			},
856 			.htot		= 1600,
857 			.vblank_def	= 520,
858 		},
859 		.reg_data	= ov5640_setting_low_res,
860 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
861 		.max_fps	= OV5640_60_FPS,
862 		.def_fps	= OV5640_30_FPS
863 	}, {
864 		/* 720x480 */
865 		.id		= OV5640_MODE_NTSC_720_480,
866 		.dn_mode	= SUBSAMPLING,
867 		.width		= 720,
868 		.height		= 480,
869 		.pixel_rate	= OV5640_PIXEL_RATE_96M,
870 		.dvp_timings = {
871 			.analog_crop = {
872 				.left	= 0,
873 				.top	= 4,
874 				.width	= 2624,
875 				.height	= 1944,
876 			},
877 			.crop = {
878 				.left	= 56,
879 				.top	= 60,
880 				.width	= 720,
881 				.height	= 480,
882 			},
883 			.htot		= 1896,
884 			.vblank_def	= 504,
885 		},
886 		.csi2_timings = {
887 			/* Feed the full valid pixel array to the ISP. */
888 			.analog_crop = {
889 				.left	= OV5640_PIXEL_ARRAY_LEFT,
890 				.top	= OV5640_PIXEL_ARRAY_TOP,
891 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
892 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
893 			},
894 			.crop = {
895 				.left	= 56,
896 				.top	= 60,
897 				.width	= 720,
898 				.height	= 480,
899 			},
900 			.htot		= 1896,
901 			.vblank_def	= 1206,
902 		},
903 		.reg_data	= ov5640_setting_low_res,
904 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
905 		.max_fps	= OV5640_30_FPS,
906 		.def_fps	= OV5640_30_FPS
907 	}, {
908 		/* 720x576 */
909 		.id		= OV5640_MODE_PAL_720_576,
910 		.dn_mode	= SUBSAMPLING,
911 		.width		= 720,
912 		.height		= 576,
913 		.pixel_rate	= OV5640_PIXEL_RATE_96M,
914 		.dvp_timings = {
915 			.analog_crop = {
916 				.left	= 0,
917 				.top	= 4,
918 				.width	= 2624,
919 				.height	= 1944,
920 			},
921 			.crop = {
922 				.left	= 56,
923 				.top	= 6,
924 				.width	= 720,
925 				.height	= 576,
926 			},
927 			.htot		= 1896,
928 			.vblank_def	= 408,
929 		},
930 		.csi2_timings = {
931 			/* Feed the full valid pixel array to the ISP. */
932 			.analog_crop = {
933 				.left	= OV5640_PIXEL_ARRAY_LEFT,
934 				.top	= OV5640_PIXEL_ARRAY_TOP,
935 				.width	= OV5640_PIXEL_ARRAY_WIDTH,
936 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
937 			},
938 			.crop = {
939 				.left	= 56,
940 				.top	= 6,
941 				.width	= 720,
942 				.height	= 576,
943 			},
944 			.htot		= 1896,
945 			.vblank_def	= 1110,
946 		},
947 		.reg_data	= ov5640_setting_low_res,
948 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
949 		.max_fps	= OV5640_30_FPS,
950 		.def_fps	= OV5640_30_FPS
951 	}, {
952 		/* 1024x768 */
953 		.id		= OV5640_MODE_XGA_1024_768,
954 		.dn_mode	= SUBSAMPLING,
955 		.pixel_rate	= OV5640_PIXEL_RATE_96M,
956 		.width		= 1024,
957 		.height		= 768,
958 		.dvp_timings = {
959 			.analog_crop = {
960 				.left	= 0,
961 				.top	= 4,
962 				.width	= 2624,
963 				.height	= 1944,
964 			},
965 			.crop = {
966 				.left	= 16,
967 				.top	= 6,
968 				.width	= 1024,
969 				.height	= 768,
970 			},
971 			.htot		= 1896,
972 			.vblank_def	= 312,
973 		},
974 		.csi2_timings = {
975 			.analog_crop = {
976 				.left	= 0,
977 				.top	= 4,
978 				.width	= OV5640_NATIVE_WIDTH,
979 				.height	= OV5640_PIXEL_ARRAY_HEIGHT,
980 			},
981 			.crop = {
982 				.left	= 16,
983 				.top	= 6,
984 				.width	= 1024,
985 				.height	= 768,
986 			},
987 			.htot		= 1896,
988 			.vblank_def	= 918,
989 		},
990 		.reg_data	= ov5640_setting_low_res,
991 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_low_res),
992 		.max_fps	= OV5640_30_FPS,
993 		.def_fps	= OV5640_30_FPS
994 	}, {
995 		/* 1280x720 */
996 		.id		= OV5640_MODE_720P_1280_720,
997 		.dn_mode	= SUBSAMPLING,
998 		.pixel_rate	= OV5640_PIXEL_RATE_124M,
999 		.width		= 1280,
1000 		.height		= 720,
1001 		.dvp_timings = {
1002 			.analog_crop = {
1003 				.left	= 0,
1004 				.top	= 250,
1005 				.width	= 2624,
1006 				.height	= 1456,
1007 			},
1008 			.crop = {
1009 				.left	= 16,
1010 				.top	= 4,
1011 				.width	= 1280,
1012 				.height	= 720,
1013 			},
1014 			.htot		= 1892,
1015 			.vblank_def	= 20,
1016 		},
1017 		.csi2_timings = {
1018 			.analog_crop = {
1019 				.left	= 0,
1020 				.top	= 250,
1021 				.width	= 2624,
1022 				.height	= 1456,
1023 			},
1024 			.crop = {
1025 				.left	= 16,
1026 				.top	= 4,
1027 				.width	= 1280,
1028 				.height	= 720,
1029 			},
1030 			.htot		= 1600,
1031 			.vblank_def	= 560,
1032 		},
1033 		.reg_data	= ov5640_setting_720P_1280_720,
1034 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_720P_1280_720),
1035 		.max_fps	= OV5640_30_FPS,
1036 		.def_fps	= OV5640_30_FPS
1037 	}, {
1038 		/* 1920x1080 */
1039 		.id		= OV5640_MODE_1080P_1920_1080,
1040 		.dn_mode	= SCALING,
1041 		.pixel_rate	= OV5640_PIXEL_RATE_148M,
1042 		.width		= 1920,
1043 		.height		= 1080,
1044 		.dvp_timings = {
1045 			.analog_crop = {
1046 				.left	= 336,
1047 				.top	= 434,
1048 				.width	= 1952,
1049 				.height	= 1088,
1050 			},
1051 			.crop = {
1052 				.left	= 16,
1053 				.top	= 4,
1054 				.width	= 1920,
1055 				.height	= 1080,
1056 			},
1057 			.htot		= 2500,
1058 			.vblank_def	= 40,
1059 		},
1060 		.csi2_timings = {
1061 			/* Crop the full valid pixel array in the center. */
1062 			.analog_crop = {
1063 				.left	= 336,
1064 				.top	= 434,
1065 				.width	= 1952,
1066 				.height	= 1088,
1067 			},
1068 			/* Maintain a larger processing margins. */
1069 			.crop = {
1070 				.left	= 16,
1071 				.top	= 4,
1072 				.width	= 1920,
1073 				.height	= 1080,
1074 			},
1075 			.htot		= 2234,
1076 			.vblank_def	= 24,
1077 		},
1078 		.reg_data	= ov5640_setting_1080P_1920_1080,
1079 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
1080 		.max_fps	= OV5640_30_FPS,
1081 		.def_fps	= OV5640_30_FPS
1082 	}, {
1083 		/* 2592x1944 */
1084 		.id		= OV5640_MODE_QSXGA_2592_1944,
1085 		.dn_mode	= SCALING,
1086 		.pixel_rate	= OV5640_PIXEL_RATE_168M,
1087 		.width		= OV5640_PIXEL_ARRAY_WIDTH,
1088 		.height		= OV5640_PIXEL_ARRAY_HEIGHT,
1089 		.dvp_timings = {
1090 			.analog_crop = {
1091 				.left	= 0,
1092 				.top	= 0,
1093 				.width	= 2624,
1094 				.height	= 1952,
1095 			},
1096 			.crop = {
1097 				.left	= 16,
1098 				.top	= 4,
1099 				.width	= 2592,
1100 				.height	= 1944,
1101 			},
1102 			.htot		= 2844,
1103 			.vblank_def	= 24,
1104 		},
1105 		.csi2_timings = {
1106 			/* Give more processing margin to full resolution. */
1107 			.analog_crop = {
1108 				.left	= 0,
1109 				.top	= 0,
1110 				.width	= OV5640_NATIVE_WIDTH,
1111 				.height	= 1952,
1112 			},
1113 			.crop = {
1114 				.left	= 16,
1115 				.top	= 4,
1116 				.width	= 2592,
1117 				.height	= 1944,
1118 			},
1119 			.htot		= 2844,
1120 			.vblank_def	= 24,
1121 		},
1122 		.reg_data	= ov5640_setting_QSXGA_2592_1944,
1123 		.reg_data_size	= ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
1124 		.max_fps	= OV5640_15_FPS,
1125 		.def_fps	= OV5640_15_FPS
1126 	},
1127 };
1128 
1129 static const struct ov5640_timings *
1130 ov5640_timings(const struct ov5640_dev *sensor,
1131 	       const struct ov5640_mode_info *mode)
1132 {
1133 	if (ov5640_is_csi2(sensor))
1134 		return &mode->csi2_timings;
1135 
1136 	return &mode->dvp_timings;
1137 }
1138 
1139 static int ov5640_init_slave_id(struct ov5640_dev *sensor)
1140 {
1141 	struct i2c_client *client = sensor->i2c_client;
1142 	struct i2c_msg msg;
1143 	u8 buf[3];
1144 	int ret;
1145 
1146 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
1147 		return 0;
1148 
1149 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
1150 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
1151 	buf[2] = client->addr << 1;
1152 
1153 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
1154 	msg.flags = 0;
1155 	msg.buf = buf;
1156 	msg.len = sizeof(buf);
1157 
1158 	ret = i2c_transfer(client->adapter, &msg, 1);
1159 	if (ret < 0) {
1160 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
1161 		return ret;
1162 	}
1163 
1164 	return 0;
1165 }
1166 
1167 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
1168 {
1169 	struct i2c_client *client = sensor->i2c_client;
1170 	struct i2c_msg msg;
1171 	u8 buf[3];
1172 	int ret;
1173 
1174 	buf[0] = reg >> 8;
1175 	buf[1] = reg & 0xff;
1176 	buf[2] = val;
1177 
1178 	msg.addr = client->addr;
1179 	msg.flags = client->flags;
1180 	msg.buf = buf;
1181 	msg.len = sizeof(buf);
1182 
1183 	ret = i2c_transfer(client->adapter, &msg, 1);
1184 	if (ret < 0) {
1185 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
1186 			__func__, reg, val);
1187 		return ret;
1188 	}
1189 
1190 	return 0;
1191 }
1192 
1193 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
1194 {
1195 	struct i2c_client *client = sensor->i2c_client;
1196 	struct i2c_msg msg[2];
1197 	u8 buf[2];
1198 	int ret;
1199 
1200 	buf[0] = reg >> 8;
1201 	buf[1] = reg & 0xff;
1202 
1203 	msg[0].addr = client->addr;
1204 	msg[0].flags = client->flags;
1205 	msg[0].buf = buf;
1206 	msg[0].len = sizeof(buf);
1207 
1208 	msg[1].addr = client->addr;
1209 	msg[1].flags = client->flags | I2C_M_RD;
1210 	msg[1].buf = buf;
1211 	msg[1].len = 1;
1212 
1213 	ret = i2c_transfer(client->adapter, msg, 2);
1214 	if (ret < 0) {
1215 		dev_err(&client->dev, "%s: error: reg=%x\n",
1216 			__func__, reg);
1217 		return ret;
1218 	}
1219 
1220 	*val = buf[0];
1221 	return 0;
1222 }
1223 
1224 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
1225 {
1226 	u8 hi, lo;
1227 	int ret;
1228 
1229 	ret = ov5640_read_reg(sensor, reg, &hi);
1230 	if (ret)
1231 		return ret;
1232 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
1233 	if (ret)
1234 		return ret;
1235 
1236 	*val = ((u16)hi << 8) | (u16)lo;
1237 	return 0;
1238 }
1239 
1240 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
1241 {
1242 	int ret;
1243 
1244 	ret = ov5640_write_reg(sensor, reg, val >> 8);
1245 	if (ret)
1246 		return ret;
1247 
1248 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
1249 }
1250 
1251 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
1252 			  u8 mask, u8 val)
1253 {
1254 	u8 readval;
1255 	int ret;
1256 
1257 	ret = ov5640_read_reg(sensor, reg, &readval);
1258 	if (ret)
1259 		return ret;
1260 
1261 	readval &= ~mask;
1262 	val &= mask;
1263 	val |= readval;
1264 
1265 	return ov5640_write_reg(sensor, reg, val);
1266 }
1267 
1268 /*
1269  * After trying the various combinations, reading various
1270  * documentations spread around the net, and from the various
1271  * feedback, the clock tree is probably as follows:
1272  *
1273  *   +--------------+
1274  *   |  Ext. Clock  |
1275  *   +-+------------+
1276  *     |  +----------+
1277  *     +->|   PLL1   | - reg 0x3036, for the multiplier
1278  *        +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1279  *          |  +--------------+
1280  *          +->| System Clock |  - reg 0x3035, bits 4-7
1281  *             +-+------------+
1282  *               |  +--------------+
1283  *               +->| MIPI Divider | - reg 0x3035, bits 0-3
1284  *               |  +-+------------+
1285  *               |    +----------------> MIPI SCLK
1286  *               |    +  +-----+
1287  *               |    +->| / 2 |-------> MIPI BIT CLK
1288  *               |       +-----+
1289  *               |  +--------------+
1290  *               +->| PLL Root Div | - reg 0x3037, bit 4
1291  *                  +-+------------+
1292  *                    |  +---------+
1293  *                    +->| Bit Div | - reg 0x3034, bits 0-3
1294  *                       +-+-------+
1295  *                         |  +-------------+
1296  *                         +->| SCLK Div    | - reg 0x3108, bits 0-1
1297  *                         |  +-+-----------+
1298  *                         |    +---------------> SCLK
1299  *                         |  +-------------+
1300  *                         +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1301  *                         |  +-+-----------+
1302  *                         |    +---------------> SCLK 2X
1303  *                         |  +-------------+
1304  *                         +->| PCLK Div    | - reg 0x3108, bits 4-5
1305  *                            ++------------+
1306  *                             +  +-----------+
1307  *                             +->|   P_DIV   | - reg 0x3035, bits 0-3
1308  *                                +-----+-----+
1309  *                                       +------------> PCLK
1310  *
1311  * There seems to be also constraints:
1312  *  - the PLL pre-divider output rate should be in the 4-27MHz range
1313  *  - the PLL multiplier output rate should be in the 500-1000MHz range
1314  *  - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1315  */
1316 
1317 /*
1318  * This is supposed to be ranging from 1 to 8, but the value is always
1319  * set to 3 in the vendor kernels.
1320  */
1321 #define OV5640_PLL_PREDIV	3
1322 
1323 #define OV5640_PLL_MULT_MIN	4
1324 #define OV5640_PLL_MULT_MAX	252
1325 
1326 /*
1327  * This is supposed to be ranging from 1 to 16, but the value is
1328  * always set to either 1 or 2 in the vendor kernels.
1329  */
1330 #define OV5640_SYSDIV_MIN	1
1331 #define OV5640_SYSDIV_MAX	16
1332 
1333 /*
1334  * This is supposed to be ranging from 1 to 2, but the value is always
1335  * set to 2 in the vendor kernels.
1336  */
1337 #define OV5640_PLL_ROOT_DIV			2
1338 #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2		BIT(4)
1339 
1340 /*
1341  * We only supports 8-bit formats at the moment
1342  */
1343 #define OV5640_BIT_DIV				2
1344 #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT		0x08
1345 
1346 /*
1347  * This is supposed to be ranging from 1 to 8, but the value is always
1348  * set to 2 in the vendor kernels.
1349  */
1350 #define OV5640_SCLK_ROOT_DIV	2
1351 
1352 /*
1353  * This is hardcoded so that the consistency is maintained between SCLK and
1354  * SCLK 2x.
1355  */
1356 #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1357 
1358 /*
1359  * This is supposed to be ranging from 1 to 8, but the value is always
1360  * set to 1 in the vendor kernels.
1361  */
1362 #define OV5640_PCLK_ROOT_DIV			1
1363 #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS	0x00
1364 
1365 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1366 					    u8 pll_prediv, u8 pll_mult,
1367 					    u8 sysdiv)
1368 {
1369 	unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1370 
1371 	/* PLL1 output cannot exceed 1GHz. */
1372 	if (sysclk / 1000000 > 1000)
1373 		return 0;
1374 
1375 	return sysclk / sysdiv;
1376 }
1377 
1378 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1379 					 unsigned long rate,
1380 					 u8 *pll_prediv, u8 *pll_mult,
1381 					 u8 *sysdiv)
1382 {
1383 	unsigned long best = ~0;
1384 	u8 best_sysdiv = 1, best_mult = 1;
1385 	u8 _sysdiv, _pll_mult;
1386 
1387 	for (_sysdiv = OV5640_SYSDIV_MIN;
1388 	     _sysdiv <= OV5640_SYSDIV_MAX;
1389 	     _sysdiv++) {
1390 		for (_pll_mult = OV5640_PLL_MULT_MIN;
1391 		     _pll_mult <= OV5640_PLL_MULT_MAX;
1392 		     _pll_mult++) {
1393 			unsigned long _rate;
1394 
1395 			/*
1396 			 * The PLL multiplier cannot be odd if above
1397 			 * 127.
1398 			 */
1399 			if (_pll_mult > 127 && (_pll_mult % 2))
1400 				continue;
1401 
1402 			_rate = ov5640_compute_sys_clk(sensor,
1403 						       OV5640_PLL_PREDIV,
1404 						       _pll_mult, _sysdiv);
1405 
1406 			/*
1407 			 * We have reached the maximum allowed PLL1 output,
1408 			 * increase sysdiv.
1409 			 */
1410 			if (!_rate)
1411 				break;
1412 
1413 			/*
1414 			 * Prefer rates above the expected clock rate than
1415 			 * below, even if that means being less precise.
1416 			 */
1417 			if (_rate < rate)
1418 				continue;
1419 
1420 			if (abs(rate - _rate) < abs(rate - best)) {
1421 				best = _rate;
1422 				best_sysdiv = _sysdiv;
1423 				best_mult = _pll_mult;
1424 			}
1425 
1426 			if (_rate == rate)
1427 				goto out;
1428 		}
1429 	}
1430 
1431 out:
1432 	*sysdiv = best_sysdiv;
1433 	*pll_prediv = OV5640_PLL_PREDIV;
1434 	*pll_mult = best_mult;
1435 
1436 	return best;
1437 }
1438 
1439 /*
1440  * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1441  *			    for the MIPI CSI-2 output.
1442  */
1443 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
1444 {
1445 	u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
1446 	u8 prediv, mult, sysdiv;
1447 	unsigned long link_freq;
1448 	unsigned long sysclk;
1449 	u8 pclk_period;
1450 	u32 sample_rate;
1451 	u32 num_lanes;
1452 	int ret;
1453 
1454 	/* Use the link freq computed at ov5640_update_pixel_rate() time. */
1455 	link_freq = sensor->current_link_freq;
1456 
1457 	/*
1458 	 * - mipi_div - Additional divider for the MIPI lane clock.
1459 	 *
1460 	 * Higher link frequencies would make sysclk > 1GHz.
1461 	 * Keep the sysclk low and do not divide in the MIPI domain.
1462 	 */
1463 	if (link_freq > OV5640_LINK_RATE_MAX)
1464 		mipi_div = 1;
1465 	else
1466 		mipi_div = 2;
1467 
1468 	sysclk = link_freq * mipi_div;
1469 	ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
1470 
1471 	/*
1472 	 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
1473 	 *
1474 	 * - root_div = 2 (fixed)
1475 	 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
1476 	 * - pclk_div = 1 (fixed)
1477 	 * - p_div  = (2 lanes ? mipi_div : 2 * mipi_div)
1478 	 *
1479 	 * This results in the following MIPI_SCLK depending on the number
1480 	 * of lanes:
1481 	 *
1482 	 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
1483 	 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
1484 	 */
1485 	root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
1486 	bit_div =  OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
1487 	pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
1488 
1489 	/*
1490 	 * Scaler clock:
1491 	 * - YUV: PCLK >= 2 * SCLK
1492 	 * - RAW or JPEG: PCLK >= SCLK
1493 	 * - sclk2x_div = sclk_div / 2
1494 	 */
1495 	sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
1496 	sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
1497 
1498 	/*
1499 	 * Set the pixel clock period expressed in ns with 1-bit decimal
1500 	 * (0x01=0.5ns).
1501 	 *
1502 	 * The register is very briefly documented. In the OV5645 datasheet it
1503 	 * is described as (2 * pclk period), and from testing it seems the
1504 	 * actual definition is 2 * 8-bit sample period.
1505 	 *
1506 	 * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
1507 	 */
1508 	num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
1509 	sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
1510 	pclk_period = 2000000000UL / sample_rate;
1511 
1512 	/* Program the clock tree registers. */
1513 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
1514 	if (ret)
1515 		return ret;
1516 
1517 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
1518 			     (sysdiv << 4) | mipi_div);
1519 	if (ret)
1520 		return ret;
1521 
1522 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1523 	if (ret)
1524 		return ret;
1525 
1526 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
1527 			     root_div | prediv);
1528 	if (ret)
1529 		return ret;
1530 
1531 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1532 			     (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
1533 	if (ret)
1534 		return ret;
1535 
1536 	return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
1537 }
1538 
1539 static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1540 {
1541 	const struct ov5640_mode_info *mode = sensor->current_mode;
1542 	const struct ov5640_timings *timings = &mode->dvp_timings;
1543 	u32 rate;
1544 
1545 	rate = timings->htot * (timings->crop.height + timings->vblank_def);
1546 	rate *= ov5640_framerates[sensor->current_fr];
1547 
1548 	return rate;
1549 }
1550 
1551 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1552 				      unsigned long rate,
1553 				      u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1554 				      u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1555 {
1556 	unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1557 				OV5640_PCLK_ROOT_DIV;
1558 
1559 	_rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1560 				    sysdiv);
1561 	*pll_rdiv = OV5640_PLL_ROOT_DIV;
1562 	*bit_div = OV5640_BIT_DIV;
1563 	*pclk_div = OV5640_PCLK_ROOT_DIV;
1564 
1565 	return _rate / *pll_rdiv / *bit_div / *pclk_div;
1566 }
1567 
1568 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
1569 {
1570 	u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1571 	u32 rate;
1572 	int ret;
1573 
1574 	rate = ov5640_calc_pixel_rate(sensor);
1575 	rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code);
1576 	rate /= sensor->ep.bus.parallel.bus_width;
1577 
1578 	ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1579 			 &bit_div, &pclk_div);
1580 
1581 	if (bit_div == 2)
1582 		bit_div = 8;
1583 
1584 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1585 			     0x0f, bit_div);
1586 	if (ret)
1587 		return ret;
1588 
1589 	/*
1590 	 * We need to set sysdiv according to the clock, and to clear
1591 	 * the MIPI divider.
1592 	 */
1593 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1594 			     0xff, sysdiv << 4);
1595 	if (ret)
1596 		return ret;
1597 
1598 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1599 			     0xff, mult);
1600 	if (ret)
1601 		return ret;
1602 
1603 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1604 			     0x1f, prediv | ((pll_rdiv - 1) << 4));
1605 	if (ret)
1606 		return ret;
1607 
1608 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1609 			      (ilog2(pclk_div) << 4));
1610 }
1611 
1612 /* set JPEG framing sizes */
1613 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1614 				   const struct ov5640_mode_info *mode)
1615 {
1616 	int ret;
1617 
1618 	/*
1619 	 * compression mode 3 timing
1620 	 *
1621 	 * Data is transmitted with programmable width (VFIFO_HSIZE).
1622 	 * No padding done. Last line may have less data. Varying
1623 	 * number of lines per frame, depending on amount of data.
1624 	 */
1625 	ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1626 	if (ret < 0)
1627 		return ret;
1628 
1629 	ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
1630 	if (ret < 0)
1631 		return ret;
1632 
1633 	return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
1634 }
1635 
1636 /* download ov5640 settings to sensor through i2c */
1637 static int ov5640_set_timings(struct ov5640_dev *sensor,
1638 			      const struct ov5640_mode_info *mode)
1639 {
1640 	const struct ov5640_timings *timings;
1641 	const struct v4l2_rect *analog_crop;
1642 	const struct v4l2_rect *crop;
1643 	int ret;
1644 
1645 	if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1646 		ret = ov5640_set_jpeg_timings(sensor, mode);
1647 		if (ret < 0)
1648 			return ret;
1649 	}
1650 
1651 	timings = ov5640_timings(sensor, mode);
1652 	analog_crop = &timings->analog_crop;
1653 	crop = &timings->crop;
1654 
1655 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
1656 				 analog_crop->left);
1657 	if (ret < 0)
1658 		return ret;
1659 
1660 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
1661 				 analog_crop->top);
1662 	if (ret < 0)
1663 		return ret;
1664 
1665 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
1666 				 analog_crop->left + analog_crop->width - 1);
1667 	if (ret < 0)
1668 		return ret;
1669 
1670 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
1671 				 analog_crop->top + analog_crop->height - 1);
1672 	if (ret < 0)
1673 		return ret;
1674 
1675 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
1676 	if (ret < 0)
1677 		return ret;
1678 
1679 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
1680 	if (ret < 0)
1681 		return ret;
1682 
1683 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
1684 	if (ret < 0)
1685 		return ret;
1686 
1687 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
1688 	if (ret < 0)
1689 		return ret;
1690 
1691 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
1692 	if (ret < 0)
1693 		return ret;
1694 
1695 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
1696 				 mode->height + timings->vblank_def);
1697 	if (ret < 0)
1698 		return ret;
1699 
1700 	return 0;
1701 }
1702 
1703 static void ov5640_load_regs(struct ov5640_dev *sensor,
1704 			     const struct reg_value *regs, unsigned int regnum)
1705 {
1706 	unsigned int i;
1707 	u32 delay_ms;
1708 	u16 reg_addr;
1709 	u8 mask, val;
1710 	int ret = 0;
1711 
1712 	for (i = 0; i < regnum; ++i, ++regs) {
1713 		delay_ms = regs->delay_ms;
1714 		reg_addr = regs->reg_addr;
1715 		val = regs->val;
1716 		mask = regs->mask;
1717 
1718 		/* remain in power down mode for DVP */
1719 		if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
1720 		    val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
1721 		    !ov5640_is_csi2(sensor))
1722 			continue;
1723 
1724 		if (mask)
1725 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1726 		else
1727 			ret = ov5640_write_reg(sensor, reg_addr, val);
1728 		if (ret)
1729 			break;
1730 
1731 		if (delay_ms)
1732 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1733 	}
1734 }
1735 
1736 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1737 {
1738 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1739 			      BIT(0), on ? 0 : BIT(0));
1740 }
1741 
1742 /* read exposure, in number of line periods */
1743 static int ov5640_get_exposure(struct ov5640_dev *sensor)
1744 {
1745 	int exp, ret;
1746 	u8 temp;
1747 
1748 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1749 	if (ret)
1750 		return ret;
1751 	exp = ((int)temp & 0x0f) << 16;
1752 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1753 	if (ret)
1754 		return ret;
1755 	exp |= ((int)temp << 8);
1756 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1757 	if (ret)
1758 		return ret;
1759 	exp |= (int)temp;
1760 
1761 	return exp >> 4;
1762 }
1763 
1764 /* write exposure, given number of line periods */
1765 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1766 {
1767 	int ret;
1768 
1769 	exposure <<= 4;
1770 
1771 	ret = ov5640_write_reg(sensor,
1772 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
1773 			       exposure & 0xff);
1774 	if (ret)
1775 		return ret;
1776 	ret = ov5640_write_reg(sensor,
1777 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
1778 			       (exposure >> 8) & 0xff);
1779 	if (ret)
1780 		return ret;
1781 	return ov5640_write_reg(sensor,
1782 				OV5640_REG_AEC_PK_EXPOSURE_HI,
1783 				(exposure >> 16) & 0x0f);
1784 }
1785 
1786 static int ov5640_get_gain(struct ov5640_dev *sensor)
1787 {
1788 	u16 gain;
1789 	int ret;
1790 
1791 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1792 	if (ret)
1793 		return ret;
1794 
1795 	return gain & 0x3ff;
1796 }
1797 
1798 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1799 {
1800 	return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1801 				  (u16)gain & 0x3ff);
1802 }
1803 
1804 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1805 {
1806 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1807 			      BIT(1), on ? 0 : BIT(1));
1808 }
1809 
1810 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1811 {
1812 	return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
1813 				OV5640_REG_SYS_CTRL0_SW_PWUP :
1814 				OV5640_REG_SYS_CTRL0_SW_PWDN);
1815 }
1816 
1817 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1818 {
1819 	int ret;
1820 
1821 	/*
1822 	 * Enable/disable the MIPI interface
1823 	 *
1824 	 * 0x300e = on ? 0x45 : 0x40
1825 	 *
1826 	 * FIXME: the sensor manual (version 2.03) reports
1827 	 * [7:5] = 000  : 1 data lane mode
1828 	 * [7:5] = 001  : 2 data lanes mode
1829 	 * But this settings do not work, while the following ones
1830 	 * have been validated for 2 data lanes mode.
1831 	 *
1832 	 * [7:5] = 010	: 2 data lanes mode
1833 	 * [4] = 0	: Power up MIPI HS Tx
1834 	 * [3] = 0	: Power up MIPI LS Rx
1835 	 * [2] = 1/0	: MIPI interface enable/disable
1836 	 * [1:0] = 01/00: FIXME: 'debug'
1837 	 */
1838 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1839 			       on ? 0x45 : 0x40);
1840 	if (ret)
1841 		return ret;
1842 
1843 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1844 				on ? 0x00 : 0x0f);
1845 }
1846 
1847 static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1848 {
1849 	 /* calculate sysclk */
1850 	u32 xvclk = sensor->xclk_freq / 10000;
1851 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1852 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1853 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
1854 	u8 temp1, temp2;
1855 	int ret;
1856 
1857 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1858 	if (ret)
1859 		return ret;
1860 	temp2 = temp1 & 0x0f;
1861 	if (temp2 == 8 || temp2 == 10)
1862 		bit_div2x = temp2 / 2;
1863 
1864 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1865 	if (ret)
1866 		return ret;
1867 	sysdiv = temp1 >> 4;
1868 	if (sysdiv == 0)
1869 		sysdiv = 16;
1870 
1871 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1872 	if (ret)
1873 		return ret;
1874 	multiplier = temp1;
1875 
1876 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1877 	if (ret)
1878 		return ret;
1879 	prediv = temp1 & 0x0f;
1880 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1881 
1882 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1883 	if (ret)
1884 		return ret;
1885 	temp2 = temp1 & 0x03;
1886 	sclk_rdiv = sclk_rdiv_map[temp2];
1887 
1888 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1889 		return -EINVAL;
1890 
1891 	VCO = xvclk * multiplier / prediv;
1892 
1893 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1894 
1895 	return sysclk;
1896 }
1897 
1898 static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1899 {
1900 	 /* read HTS from register settings */
1901 	u8 mode;
1902 	int ret;
1903 
1904 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1905 	if (ret)
1906 		return ret;
1907 	mode &= 0xfb;
1908 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1909 }
1910 
1911 static int ov5640_get_hts(struct ov5640_dev *sensor)
1912 {
1913 	/* read HTS from register settings */
1914 	u16 hts;
1915 	int ret;
1916 
1917 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1918 	if (ret)
1919 		return ret;
1920 	return hts;
1921 }
1922 
1923 static int ov5640_get_vts(struct ov5640_dev *sensor)
1924 {
1925 	u16 vts;
1926 	int ret;
1927 
1928 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1929 	if (ret)
1930 		return ret;
1931 	return vts;
1932 }
1933 
1934 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1935 {
1936 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1937 }
1938 
1939 static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1940 {
1941 	/* get banding filter value */
1942 	int ret, light_freq = 0;
1943 	u8 temp, temp1;
1944 
1945 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1946 	if (ret)
1947 		return ret;
1948 
1949 	if (temp & 0x80) {
1950 		/* manual */
1951 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1952 				      &temp1);
1953 		if (ret)
1954 			return ret;
1955 		if (temp1 & 0x04) {
1956 			/* 50Hz */
1957 			light_freq = 50;
1958 		} else {
1959 			/* 60Hz */
1960 			light_freq = 60;
1961 		}
1962 	} else {
1963 		/* auto */
1964 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1965 				      &temp1);
1966 		if (ret)
1967 			return ret;
1968 
1969 		if (temp1 & 0x01) {
1970 			/* 50Hz */
1971 			light_freq = 50;
1972 		} else {
1973 			/* 60Hz */
1974 		}
1975 	}
1976 
1977 	return light_freq;
1978 }
1979 
1980 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1981 {
1982 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1983 	int ret;
1984 
1985 	/* read preview PCLK */
1986 	ret = ov5640_get_sysclk(sensor);
1987 	if (ret < 0)
1988 		return ret;
1989 	if (ret == 0)
1990 		return -EINVAL;
1991 	sensor->prev_sysclk = ret;
1992 	/* read preview HTS */
1993 	ret = ov5640_get_hts(sensor);
1994 	if (ret < 0)
1995 		return ret;
1996 	if (ret == 0)
1997 		return -EINVAL;
1998 	sensor->prev_hts = ret;
1999 
2000 	/* read preview VTS */
2001 	ret = ov5640_get_vts(sensor);
2002 	if (ret < 0)
2003 		return ret;
2004 	prev_vts = ret;
2005 
2006 	/* calculate banding filter */
2007 	/* 60Hz */
2008 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
2009 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
2010 	if (ret)
2011 		return ret;
2012 	if (!band_step60)
2013 		return -EINVAL;
2014 	max_band60 = (int)((prev_vts - 4) / band_step60);
2015 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
2016 	if (ret)
2017 		return ret;
2018 
2019 	/* 50Hz */
2020 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
2021 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
2022 	if (ret)
2023 		return ret;
2024 	if (!band_step50)
2025 		return -EINVAL;
2026 	max_band50 = (int)((prev_vts - 4) / band_step50);
2027 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
2028 }
2029 
2030 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
2031 {
2032 	/* stable in high */
2033 	u32 fast_high, fast_low;
2034 	int ret;
2035 
2036 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
2037 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
2038 
2039 	fast_high = sensor->ae_high << 1;
2040 	if (fast_high > 255)
2041 		fast_high = 255;
2042 
2043 	fast_low = sensor->ae_low >> 1;
2044 
2045 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
2046 	if (ret)
2047 		return ret;
2048 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
2049 	if (ret)
2050 		return ret;
2051 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
2052 	if (ret)
2053 		return ret;
2054 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
2055 	if (ret)
2056 		return ret;
2057 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
2058 	if (ret)
2059 		return ret;
2060 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
2061 }
2062 
2063 static int ov5640_get_binning(struct ov5640_dev *sensor)
2064 {
2065 	u8 temp;
2066 	int ret;
2067 
2068 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
2069 	if (ret)
2070 		return ret;
2071 
2072 	return temp & BIT(0);
2073 }
2074 
2075 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
2076 {
2077 	int ret;
2078 
2079 	/*
2080 	 * TIMING TC REG21:
2081 	 * - [0]:	Horizontal binning enable
2082 	 */
2083 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2084 			     BIT(0), enable ? BIT(0) : 0);
2085 	if (ret)
2086 		return ret;
2087 	/*
2088 	 * TIMING TC REG20:
2089 	 * - [0]:	Undocumented, but hardcoded init sequences
2090 	 *		are always setting REG21/REG20 bit 0 to same value...
2091 	 */
2092 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2093 			      BIT(0), enable ? BIT(0) : 0);
2094 }
2095 
2096 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
2097 {
2098 	struct i2c_client *client = sensor->i2c_client;
2099 	u8 temp, channel = virtual_channel;
2100 	int ret;
2101 
2102 	if (channel > 3) {
2103 		dev_err(&client->dev,
2104 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
2105 			__func__, channel);
2106 		return -EINVAL;
2107 	}
2108 
2109 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
2110 	if (ret)
2111 		return ret;
2112 	temp &= ~(3 << 6);
2113 	temp |= (channel << 6);
2114 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
2115 }
2116 
2117 static const struct ov5640_mode_info *
2118 ov5640_find_mode(struct ov5640_dev *sensor, int width, int height, bool nearest)
2119 {
2120 	const struct ov5640_mode_info *mode;
2121 
2122 	mode = v4l2_find_nearest_size(ov5640_mode_data,
2123 				      ARRAY_SIZE(ov5640_mode_data),
2124 				      width, height, width, height);
2125 
2126 	if (!mode ||
2127 	    (!nearest &&
2128 	     (mode->width != width || mode->height != height)))
2129 		return NULL;
2130 
2131 	return mode;
2132 }
2133 
2134 /*
2135  * sensor changes between scaling and subsampling, go through
2136  * exposure calculation
2137  */
2138 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
2139 					 const struct ov5640_mode_info *mode)
2140 {
2141 	u32 prev_shutter, prev_gain16;
2142 	u32 cap_shutter, cap_gain16;
2143 	u32 cap_sysclk, cap_hts, cap_vts;
2144 	u32 light_freq, cap_bandfilt, cap_maxband;
2145 	u32 cap_gain16_shutter;
2146 	u8 average;
2147 	int ret;
2148 
2149 	if (!mode->reg_data)
2150 		return -EINVAL;
2151 
2152 	/* read preview shutter */
2153 	ret = ov5640_get_exposure(sensor);
2154 	if (ret < 0)
2155 		return ret;
2156 	prev_shutter = ret;
2157 	ret = ov5640_get_binning(sensor);
2158 	if (ret < 0)
2159 		return ret;
2160 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
2161 	    mode->id != OV5640_MODE_1080P_1920_1080)
2162 		prev_shutter *= 2;
2163 
2164 	/* read preview gain */
2165 	ret = ov5640_get_gain(sensor);
2166 	if (ret < 0)
2167 		return ret;
2168 	prev_gain16 = ret;
2169 
2170 	/* get average */
2171 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
2172 	if (ret)
2173 		return ret;
2174 
2175 	/* turn off night mode for capture */
2176 	ret = ov5640_set_night_mode(sensor);
2177 	if (ret < 0)
2178 		return ret;
2179 
2180 	/* Write capture setting */
2181 	ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2182 	ret = ov5640_set_timings(sensor, mode);
2183 	if (ret < 0)
2184 		return ret;
2185 
2186 	/* read capture VTS */
2187 	ret = ov5640_get_vts(sensor);
2188 	if (ret < 0)
2189 		return ret;
2190 	cap_vts = ret;
2191 	ret = ov5640_get_hts(sensor);
2192 	if (ret < 0)
2193 		return ret;
2194 	if (ret == 0)
2195 		return -EINVAL;
2196 	cap_hts = ret;
2197 
2198 	ret = ov5640_get_sysclk(sensor);
2199 	if (ret < 0)
2200 		return ret;
2201 	if (ret == 0)
2202 		return -EINVAL;
2203 	cap_sysclk = ret;
2204 
2205 	/* calculate capture banding filter */
2206 	ret = ov5640_get_light_freq(sensor);
2207 	if (ret < 0)
2208 		return ret;
2209 	light_freq = ret;
2210 
2211 	if (light_freq == 60) {
2212 		/* 60Hz */
2213 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
2214 	} else {
2215 		/* 50Hz */
2216 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
2217 	}
2218 
2219 	if (!sensor->prev_sysclk) {
2220 		ret = ov5640_get_sysclk(sensor);
2221 		if (ret < 0)
2222 			return ret;
2223 		if (ret == 0)
2224 			return -EINVAL;
2225 		sensor->prev_sysclk = ret;
2226 	}
2227 
2228 	if (!cap_bandfilt)
2229 		return -EINVAL;
2230 
2231 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
2232 
2233 	/* calculate capture shutter/gain16 */
2234 	if (average > sensor->ae_low && average < sensor->ae_high) {
2235 		/* in stable range */
2236 		cap_gain16_shutter =
2237 			prev_gain16 * prev_shutter *
2238 			cap_sysclk / sensor->prev_sysclk *
2239 			sensor->prev_hts / cap_hts *
2240 			sensor->ae_target / average;
2241 	} else {
2242 		cap_gain16_shutter =
2243 			prev_gain16 * prev_shutter *
2244 			cap_sysclk / sensor->prev_sysclk *
2245 			sensor->prev_hts / cap_hts;
2246 	}
2247 
2248 	/* gain to shutter */
2249 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
2250 		/* shutter < 1/100 */
2251 		cap_shutter = cap_gain16_shutter / 16;
2252 		if (cap_shutter < 1)
2253 			cap_shutter = 1;
2254 
2255 		cap_gain16 = cap_gain16_shutter / cap_shutter;
2256 		if (cap_gain16 < 16)
2257 			cap_gain16 = 16;
2258 	} else {
2259 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
2260 			/* exposure reach max */
2261 			cap_shutter = cap_bandfilt * cap_maxband;
2262 			if (!cap_shutter)
2263 				return -EINVAL;
2264 
2265 			cap_gain16 = cap_gain16_shutter / cap_shutter;
2266 		} else {
2267 			/* 1/100 < (cap_shutter = n/100) =< max */
2268 			cap_shutter =
2269 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
2270 				* cap_bandfilt;
2271 			if (!cap_shutter)
2272 				return -EINVAL;
2273 
2274 			cap_gain16 = cap_gain16_shutter / cap_shutter;
2275 		}
2276 	}
2277 
2278 	/* set capture gain */
2279 	ret = ov5640_set_gain(sensor, cap_gain16);
2280 	if (ret)
2281 		return ret;
2282 
2283 	/* write capture shutter */
2284 	if (cap_shutter > (cap_vts - 4)) {
2285 		cap_vts = cap_shutter + 4;
2286 		ret = ov5640_set_vts(sensor, cap_vts);
2287 		if (ret < 0)
2288 			return ret;
2289 	}
2290 
2291 	/* set exposure */
2292 	return ov5640_set_exposure(sensor, cap_shutter);
2293 }
2294 
2295 /*
2296  * if sensor changes inside scaling or subsampling
2297  * change mode directly
2298  */
2299 static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
2300 				  const struct ov5640_mode_info *mode)
2301 {
2302 	if (!mode->reg_data)
2303 		return -EINVAL;
2304 
2305 	/* Write capture setting */
2306 	ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
2307 	return ov5640_set_timings(sensor, mode);
2308 }
2309 
2310 static int ov5640_set_mode(struct ov5640_dev *sensor)
2311 {
2312 	const struct ov5640_mode_info *mode = sensor->current_mode;
2313 	const struct ov5640_mode_info *orig_mode = sensor->last_mode;
2314 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
2315 	bool auto_gain = sensor->ctrls.auto_gain->val == 1;
2316 	bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
2317 	int ret;
2318 
2319 	dn_mode = mode->dn_mode;
2320 	orig_dn_mode = orig_mode->dn_mode;
2321 
2322 	/* auto gain and exposure must be turned off when changing modes */
2323 	if (auto_gain) {
2324 		ret = ov5640_set_autogain(sensor, false);
2325 		if (ret)
2326 			return ret;
2327 	}
2328 
2329 	if (auto_exp) {
2330 		ret = ov5640_set_autoexposure(sensor, false);
2331 		if (ret)
2332 			goto restore_auto_gain;
2333 	}
2334 
2335 	if (ov5640_is_csi2(sensor))
2336 		ret = ov5640_set_mipi_pclk(sensor);
2337 	else
2338 		ret = ov5640_set_dvp_pclk(sensor);
2339 	if (ret < 0)
2340 		return 0;
2341 
2342 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2343 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2344 		/*
2345 		 * change between subsampling and scaling
2346 		 * go through exposure calculation
2347 		 */
2348 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
2349 	} else {
2350 		/*
2351 		 * change inside subsampling or scaling
2352 		 * download firmware directly
2353 		 */
2354 		ret = ov5640_set_mode_direct(sensor, mode);
2355 	}
2356 	if (ret < 0)
2357 		goto restore_auto_exp_gain;
2358 
2359 	/* restore auto gain and exposure */
2360 	if (auto_gain)
2361 		ov5640_set_autogain(sensor, true);
2362 	if (auto_exp)
2363 		ov5640_set_autoexposure(sensor, true);
2364 
2365 	ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2366 	if (ret < 0)
2367 		return ret;
2368 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2369 	if (ret < 0)
2370 		return ret;
2371 	ret = ov5640_get_light_freq(sensor);
2372 	if (ret < 0)
2373 		return ret;
2374 	ret = ov5640_set_bandingfilter(sensor);
2375 	if (ret < 0)
2376 		return ret;
2377 	ret = ov5640_set_virtual_channel(sensor);
2378 	if (ret < 0)
2379 		return ret;
2380 
2381 	sensor->pending_mode_change = false;
2382 	sensor->last_mode = mode;
2383 
2384 	return 0;
2385 
2386 restore_auto_exp_gain:
2387 	if (auto_exp)
2388 		ov5640_set_autoexposure(sensor, true);
2389 restore_auto_gain:
2390 	if (auto_gain)
2391 		ov5640_set_autogain(sensor, true);
2392 
2393 	return ret;
2394 }
2395 
2396 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2397 			       struct v4l2_mbus_framefmt *format);
2398 
2399 /* restore the last set video mode after chip power-on */
2400 static int ov5640_restore_mode(struct ov5640_dev *sensor)
2401 {
2402 	int ret;
2403 
2404 	/* first load the initial register values */
2405 	ov5640_load_regs(sensor, ov5640_init_setting,
2406 			 ARRAY_SIZE(ov5640_init_setting));
2407 
2408 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2409 			     (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
2410 			     ilog2(OV5640_SCLK_ROOT_DIV));
2411 	if (ret)
2412 		return ret;
2413 
2414 	/* now restore the last capture mode */
2415 	ret = ov5640_set_mode(sensor);
2416 	if (ret < 0)
2417 		return ret;
2418 
2419 	return ov5640_set_framefmt(sensor, &sensor->fmt);
2420 }
2421 
2422 static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2423 {
2424 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
2425 }
2426 
2427 static void ov5640_reset(struct ov5640_dev *sensor)
2428 {
2429 	if (!sensor->reset_gpio)
2430 		return;
2431 
2432 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2433 
2434 	/* camera power cycle */
2435 	ov5640_power(sensor, false);
2436 	usleep_range(5000, 10000);
2437 	ov5640_power(sensor, true);
2438 	usleep_range(5000, 10000);
2439 
2440 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
2441 	usleep_range(1000, 2000);
2442 
2443 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
2444 	usleep_range(20000, 25000);
2445 }
2446 
2447 static int ov5640_set_power_on(struct ov5640_dev *sensor)
2448 {
2449 	struct i2c_client *client = sensor->i2c_client;
2450 	int ret;
2451 
2452 	ret = clk_prepare_enable(sensor->xclk);
2453 	if (ret) {
2454 		dev_err(&client->dev, "%s: failed to enable clock\n",
2455 			__func__);
2456 		return ret;
2457 	}
2458 
2459 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2460 				    sensor->supplies);
2461 	if (ret) {
2462 		dev_err(&client->dev, "%s: failed to enable regulators\n",
2463 			__func__);
2464 		goto xclk_off;
2465 	}
2466 
2467 	ov5640_reset(sensor);
2468 	ov5640_power(sensor, true);
2469 
2470 	ret = ov5640_init_slave_id(sensor);
2471 	if (ret)
2472 		goto power_off;
2473 
2474 	return 0;
2475 
2476 power_off:
2477 	ov5640_power(sensor, false);
2478 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2479 xclk_off:
2480 	clk_disable_unprepare(sensor->xclk);
2481 	return ret;
2482 }
2483 
2484 static void ov5640_set_power_off(struct ov5640_dev *sensor)
2485 {
2486 	ov5640_power(sensor, false);
2487 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2488 	clk_disable_unprepare(sensor->xclk);
2489 }
2490 
2491 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2492 {
2493 	int ret;
2494 
2495 	if (!on) {
2496 		/* Reset MIPI bus settings to their default values. */
2497 		ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2498 		ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2499 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2500 		return 0;
2501 	}
2502 
2503 	/*
2504 	 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2505 	 *
2506 	 * 0x300e = 0x40
2507 	 * [7:5] = 010	: 2 data lanes mode (see FIXME note in
2508 	 *		  "ov5640_set_stream_mipi()")
2509 	 * [4] = 0	: Power up MIPI HS Tx
2510 	 * [3] = 0	: Power up MIPI LS Rx
2511 	 * [2] = 0	: MIPI interface disabled
2512 	 */
2513 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2514 	if (ret)
2515 		return ret;
2516 
2517 	/*
2518 	 * Gate clock and set LP11 in 'no packets mode' (idle)
2519 	 *
2520 	 * 0x4800 = 0x24
2521 	 * [5] = 1	: Gate clock when 'no packets'
2522 	 * [2] = 1	: MIPI bus in LP11 when 'no packets'
2523 	 */
2524 	ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2525 	if (ret)
2526 		return ret;
2527 
2528 	/*
2529 	 * Set data lanes and clock in LP11 when 'sleeping'
2530 	 *
2531 	 * 0x3019 = 0x70
2532 	 * [6] = 1	: MIPI data lane 2 in LP11 when 'sleeping'
2533 	 * [5] = 1	: MIPI data lane 1 in LP11 when 'sleeping'
2534 	 * [4] = 1	: MIPI clock lane in LP11 when 'sleeping'
2535 	 */
2536 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2537 	if (ret)
2538 		return ret;
2539 
2540 	/* Give lanes some time to coax into LP11 state. */
2541 	usleep_range(500, 1000);
2542 
2543 	return 0;
2544 }
2545 
2546 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2547 {
2548 	unsigned int flags = sensor->ep.bus.parallel.flags;
2549 	bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
2550 	u8 polarities = 0;
2551 	int ret;
2552 
2553 	if (!on) {
2554 		/* Reset settings to their default values. */
2555 		ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2556 		ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2557 		ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2558 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2559 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2560 		return 0;
2561 	}
2562 
2563 	/*
2564 	 * Note about parallel port configuration.
2565 	 *
2566 	 * When configured in parallel mode, the OV5640 will
2567 	 * output 10 bits data on DVP data lines [9:0].
2568 	 * If only 8 bits data are wanted, the 8 bits data lines
2569 	 * of the camera interface must be physically connected
2570 	 * on the DVP data lines [9:2].
2571 	 *
2572 	 * Control lines polarity can be configured through
2573 	 * devicetree endpoint control lines properties.
2574 	 * If no endpoint control lines properties are set,
2575 	 * polarity will be as below:
2576 	 * - VSYNC:	active high
2577 	 * - HREF:	active low
2578 	 * - PCLK:	active low
2579 	 *
2580 	 * VSYNC & HREF are not configured if BT656 bus mode is selected
2581 	 */
2582 
2583 	/*
2584 	 * BT656 embedded synchronization configuration
2585 	 *
2586 	 * CCIR656 CTRL00
2587 	 * - [7]:	SYNC code selection (0: auto generate sync code,
2588 	 *		1: sync code from regs 0x4732-0x4735)
2589 	 * - [6]:	f value in CCIR656 SYNC code when fixed f value
2590 	 * - [5]:	Fixed f value
2591 	 * - [4:3]:	Blank toggle data options (00: data=1'h040/1'h200,
2592 	 *		01: data from regs 0x4736-0x4738, 10: always keep 0)
2593 	 * - [1]:	Clip data disable
2594 	 * - [0]:	CCIR656 mode enable
2595 	 *
2596 	 * Default CCIR656 SAV/EAV mode with default codes
2597 	 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
2598 	 * - CCIR656 mode enable
2599 	 * - auto generation of sync codes
2600 	 * - blank toggle data 1'h040/1'h200
2601 	 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
2602 	 */
2603 	ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
2604 			       bt656 ? 0x01 : 0x00);
2605 	if (ret)
2606 		return ret;
2607 
2608 	/*
2609 	 * configure parallel port control lines polarity
2610 	 *
2611 	 * POLARITY CTRL0
2612 	 * - [5]:	PCLK polarity (0: active low, 1: active high)
2613 	 * - [1]:	HREF polarity (0: active low, 1: active high)
2614 	 * - [0]:	VSYNC polarity (mismatch here between
2615 	 *		datasheet and hardware, 0 is active high
2616 	 *		and 1 is active low...)
2617 	 */
2618 	if (!bt656) {
2619 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2620 			polarities |= BIT(1);
2621 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2622 			polarities |= BIT(0);
2623 	}
2624 	if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2625 		polarities |= BIT(5);
2626 
2627 	ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2628 	if (ret)
2629 		return ret;
2630 
2631 	/*
2632 	 * powerdown MIPI TX/RX PHY & enable DVP
2633 	 *
2634 	 * MIPI CONTROL 00
2635 	 * [4] = 1	: Power down MIPI HS Tx
2636 	 * [3] = 1	: Power down MIPI LS Rx
2637 	 * [2] = 0	: DVP enable (MIPI disable)
2638 	 */
2639 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2640 	if (ret)
2641 		return ret;
2642 
2643 	/*
2644 	 * enable VSYNC/HREF/PCLK DVP control lines
2645 	 * & D[9:6] DVP data lines
2646 	 *
2647 	 * PAD OUTPUT ENABLE 01
2648 	 * - 6:		VSYNC output enable
2649 	 * - 5:		HREF output enable
2650 	 * - 4:		PCLK output enable
2651 	 * - [3:0]:	D[9:6] output enable
2652 	 */
2653 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
2654 			       bt656 ? 0x1f : 0x7f);
2655 	if (ret)
2656 		return ret;
2657 
2658 	/*
2659 	 * enable D[5:0] DVP data lines
2660 	 *
2661 	 * PAD OUTPUT ENABLE 02
2662 	 * - [7:2]:	D[5:0] output enable
2663 	 */
2664 	return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2665 }
2666 
2667 static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2668 {
2669 	int ret = 0;
2670 
2671 	if (on) {
2672 		ret = ov5640_set_power_on(sensor);
2673 		if (ret)
2674 			return ret;
2675 
2676 		ret = ov5640_restore_mode(sensor);
2677 		if (ret)
2678 			goto power_off;
2679 	}
2680 
2681 	if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2682 		ret = ov5640_set_power_mipi(sensor, on);
2683 	else
2684 		ret = ov5640_set_power_dvp(sensor, on);
2685 	if (ret)
2686 		goto power_off;
2687 
2688 	if (!on)
2689 		ov5640_set_power_off(sensor);
2690 
2691 	return 0;
2692 
2693 power_off:
2694 	ov5640_set_power_off(sensor);
2695 	return ret;
2696 }
2697 
2698 static int ov5640_sensor_suspend(struct device *dev)
2699 {
2700 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
2701 	struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2702 
2703 	return ov5640_set_power(ov5640, false);
2704 }
2705 
2706 static int ov5640_sensor_resume(struct device *dev)
2707 {
2708 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
2709 	struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
2710 
2711 	return ov5640_set_power(ov5640, true);
2712 }
2713 
2714 /* --------------- Subdev Operations --------------- */
2715 
2716 static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2717 				     struct v4l2_fract *fi,
2718 				     u32 width, u32 height)
2719 {
2720 	const struct ov5640_mode_info *mode;
2721 	enum ov5640_frame_rate rate = OV5640_15_FPS;
2722 	int minfps, maxfps, best_fps, fps;
2723 	int i;
2724 
2725 	minfps = ov5640_framerates[OV5640_15_FPS];
2726 	maxfps = ov5640_framerates[OV5640_60_FPS];
2727 
2728 	if (fi->numerator == 0) {
2729 		fi->denominator = maxfps;
2730 		fi->numerator = 1;
2731 		rate = OV5640_60_FPS;
2732 		goto find_mode;
2733 	}
2734 
2735 	fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2736 			minfps, maxfps);
2737 
2738 	best_fps = minfps;
2739 	for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2740 		int curr_fps = ov5640_framerates[i];
2741 
2742 		if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2743 			best_fps = curr_fps;
2744 			rate = i;
2745 		}
2746 	}
2747 
2748 	fi->numerator = 1;
2749 	fi->denominator = best_fps;
2750 
2751 find_mode:
2752 	mode = ov5640_find_mode(sensor, width, height, false);
2753 	return mode ? rate : -EINVAL;
2754 }
2755 
2756 static int ov5640_get_fmt(struct v4l2_subdev *sd,
2757 			  struct v4l2_subdev_state *sd_state,
2758 			  struct v4l2_subdev_format *format)
2759 {
2760 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2761 	struct v4l2_mbus_framefmt *fmt;
2762 
2763 	if (format->pad != 0)
2764 		return -EINVAL;
2765 
2766 	mutex_lock(&sensor->lock);
2767 
2768 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2769 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
2770 						 format->pad);
2771 	else
2772 		fmt = &sensor->fmt;
2773 
2774 	format->format = *fmt;
2775 
2776 	mutex_unlock(&sensor->lock);
2777 
2778 	return 0;
2779 }
2780 
2781 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2782 				   struct v4l2_mbus_framefmt *fmt,
2783 				   enum ov5640_frame_rate fr,
2784 				   const struct ov5640_mode_info **new_mode)
2785 {
2786 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2787 	const struct ov5640_mode_info *mode;
2788 	const struct ov5640_pixfmt *pixfmt;
2789 	unsigned int bpp;
2790 
2791 	mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true);
2792 	if (!mode)
2793 		return -EINVAL;
2794 
2795 	pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code);
2796 	bpp = pixfmt->bpp;
2797 
2798 	/*
2799 	 * Adjust mode according to bpp:
2800 	 * - 8bpp modes work for resolution >= 1280x720
2801 	 * - 24bpp modes work resolution < 1280x720
2802 	 */
2803 	if (bpp == 8 && mode->width < 1280)
2804 		mode = &ov5640_mode_data[OV5640_MODE_720P_1280_720];
2805 	else if (bpp == 24 && mode->width > 1024)
2806 		mode = &ov5640_mode_data[OV5640_MODE_XGA_1024_768];
2807 
2808 	fmt->width = mode->width;
2809 	fmt->height = mode->height;
2810 
2811 	if (new_mode)
2812 		*new_mode = mode;
2813 
2814 	fmt->code = pixfmt->code;
2815 	fmt->colorspace = pixfmt->colorspace;
2816 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2817 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2818 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2819 
2820 	return 0;
2821 }
2822 
2823 static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
2824 {
2825 	const struct ov5640_mode_info *mode = sensor->current_mode;
2826 	enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
2827 	struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
2828 	const struct ov5640_timings *timings;
2829 	s32 exposure_val, exposure_max;
2830 	unsigned int hblank;
2831 	unsigned int i = 0;
2832 	u32 pixel_rate;
2833 	s64 link_freq;
2834 	u32 num_lanes;
2835 	u32 vblank;
2836 	u32 bpp;
2837 
2838 	/*
2839 	 * Update the pixel rate control value.
2840 	 *
2841 	 * For DVP mode, maintain the pixel rate calculation using fixed FPS.
2842 	 */
2843 	if (!ov5640_is_csi2(sensor)) {
2844 		__v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2845 					 ov5640_calc_pixel_rate(sensor));
2846 
2847 		return 0;
2848 	}
2849 
2850 	/*
2851 	 * The MIPI CSI-2 link frequency should comply with the CSI-2
2852 	 * specification and be lower than 1GHz.
2853 	 *
2854 	 * Start from the suggested pixel_rate for the current mode and
2855 	 * progressively slow it down if it exceeds 1GHz.
2856 	 */
2857 	num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
2858 	bpp = ov5640_code_to_bpp(sensor, fmt->code);
2859 	do {
2860 		pixel_rate = ov5640_pixel_rates[pixel_rate_id];
2861 		link_freq = pixel_rate * bpp / (2 * num_lanes);
2862 	} while (link_freq >= 1000000000U &&
2863 		 ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
2864 
2865 	sensor->current_link_freq = link_freq;
2866 
2867 	/*
2868 	 * Higher link rates require the clock tree to be programmed with
2869 	 * 'mipi_div' = 1; this has the effect of halving the actual output
2870 	 * pixel rate in the MIPI domain.
2871 	 *
2872 	 * Adjust the pixel rate and link frequency control value to report it
2873 	 * correctly to userspace.
2874 	 */
2875 	if (link_freq > OV5640_LINK_RATE_MAX) {
2876 		pixel_rate /= 2;
2877 		link_freq /= 2;
2878 	}
2879 
2880 	for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
2881 		if (ov5640_csi2_link_freqs[i] == link_freq)
2882 			break;
2883 	}
2884 	WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
2885 
2886 	__v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
2887 	__v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
2888 
2889 	timings = ov5640_timings(sensor, mode);
2890 	hblank = timings->htot - mode->width;
2891 	__v4l2_ctrl_modify_range(sensor->ctrls.hblank,
2892 				 hblank, hblank, 1, hblank);
2893 
2894 	vblank = timings->vblank_def;
2895 
2896 	if (sensor->current_fr != mode->def_fps) {
2897 		/*
2898 		 * Compute the vertical blanking according to the framerate
2899 		 * configured with s_frame_interval.
2900 		 */
2901 		int fie_num = sensor->frame_interval.numerator;
2902 		int fie_denom = sensor->frame_interval.denominator;
2903 
2904 		vblank = ((fie_num * pixel_rate / fie_denom) / timings->htot) -
2905 			mode->height;
2906 	}
2907 
2908 	__v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
2909 				 OV5640_MAX_VTS - mode->height, 1, vblank);
2910 	__v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank);
2911 
2912 	exposure_max = timings->crop.height + vblank - 4;
2913 	exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
2914 			       sensor->ctrls.exposure->minimum,
2915 			       exposure_max);
2916 
2917 	__v4l2_ctrl_modify_range(sensor->ctrls.exposure,
2918 				 sensor->ctrls.exposure->minimum,
2919 				 exposure_max, 1, exposure_val);
2920 
2921 	return 0;
2922 }
2923 
2924 static int ov5640_set_fmt(struct v4l2_subdev *sd,
2925 			  struct v4l2_subdev_state *sd_state,
2926 			  struct v4l2_subdev_format *format)
2927 {
2928 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2929 	const struct ov5640_mode_info *new_mode;
2930 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2931 	int ret;
2932 
2933 	if (format->pad != 0)
2934 		return -EINVAL;
2935 
2936 	mutex_lock(&sensor->lock);
2937 
2938 	if (sensor->streaming) {
2939 		ret = -EBUSY;
2940 		goto out;
2941 	}
2942 
2943 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2944 				      sensor->current_fr, &new_mode);
2945 	if (ret)
2946 		goto out;
2947 
2948 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2949 		*v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2950 		goto out;
2951 	}
2952 
2953 	if (new_mode != sensor->current_mode) {
2954 		sensor->current_fr = new_mode->def_fps;
2955 		sensor->current_mode = new_mode;
2956 		sensor->pending_mode_change = true;
2957 	}
2958 	if (mbus_fmt->code != sensor->fmt.code)
2959 		sensor->pending_fmt_change = true;
2960 
2961 	/* update format even if code is unchanged, resolution might change */
2962 	sensor->fmt = *mbus_fmt;
2963 
2964 	ov5640_update_pixel_rate(sensor);
2965 
2966 out:
2967 	mutex_unlock(&sensor->lock);
2968 	return ret;
2969 }
2970 
2971 static int ov5640_get_selection(struct v4l2_subdev *sd,
2972 				struct v4l2_subdev_state *sd_state,
2973 				struct v4l2_subdev_selection *sel)
2974 {
2975 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
2976 	const struct ov5640_mode_info *mode = sensor->current_mode;
2977 	const struct ov5640_timings *timings;
2978 
2979 	switch (sel->target) {
2980 	case V4L2_SEL_TGT_CROP: {
2981 		mutex_lock(&sensor->lock);
2982 		timings = ov5640_timings(sensor, mode);
2983 		sel->r = timings->analog_crop;
2984 		mutex_unlock(&sensor->lock);
2985 
2986 		return 0;
2987 	}
2988 
2989 	case V4L2_SEL_TGT_NATIVE_SIZE:
2990 	case V4L2_SEL_TGT_CROP_BOUNDS:
2991 		sel->r.top = 0;
2992 		sel->r.left = 0;
2993 		sel->r.width = OV5640_NATIVE_WIDTH;
2994 		sel->r.height = OV5640_NATIVE_HEIGHT;
2995 
2996 		return 0;
2997 
2998 	case V4L2_SEL_TGT_CROP_DEFAULT:
2999 		sel->r.top = OV5640_PIXEL_ARRAY_TOP;
3000 		sel->r.left = OV5640_PIXEL_ARRAY_LEFT;
3001 		sel->r.width = OV5640_PIXEL_ARRAY_WIDTH;
3002 		sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT;
3003 
3004 		return 0;
3005 	}
3006 
3007 	return -EINVAL;
3008 }
3009 
3010 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
3011 			       struct v4l2_mbus_framefmt *format)
3012 {
3013 	bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8;
3014 	const struct ov5640_pixfmt *pixfmt;
3015 	int ret = 0;
3016 
3017 	pixfmt = ov5640_code_to_pixfmt(sensor, format->code);
3018 
3019 	/* FORMAT CONTROL00: YUV and RGB formatting */
3020 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00,
3021 			       pixfmt->ctrl00);
3022 	if (ret)
3023 		return ret;
3024 
3025 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
3026 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
3027 			       pixfmt->mux);
3028 	if (ret)
3029 		return ret;
3030 
3031 	/*
3032 	 * TIMING TC REG21:
3033 	 * - [5]:	JPEG enable
3034 	 */
3035 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3036 			     BIT(5), is_jpeg ? BIT(5) : 0);
3037 	if (ret)
3038 		return ret;
3039 
3040 	/*
3041 	 * SYSTEM RESET02:
3042 	 * - [4]:	Reset JFIFO
3043 	 * - [3]:	Reset SFIFO
3044 	 * - [2]:	Reset JPEG
3045 	 */
3046 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
3047 			     BIT(4) | BIT(3) | BIT(2),
3048 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
3049 	if (ret)
3050 		return ret;
3051 
3052 	/*
3053 	 * CLOCK ENABLE02:
3054 	 * - [5]:	Enable JPEG 2x clock
3055 	 * - [3]:	Enable JPEG clock
3056 	 */
3057 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
3058 			      BIT(5) | BIT(3),
3059 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
3060 }
3061 
3062 /*
3063  * Sensor Controls.
3064  */
3065 
3066 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
3067 {
3068 	int ret;
3069 
3070 	if (value) {
3071 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3072 				     BIT(0), BIT(0));
3073 		if (ret)
3074 			return ret;
3075 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
3076 	} else {
3077 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
3078 	}
3079 
3080 	return ret;
3081 }
3082 
3083 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
3084 {
3085 	int ret;
3086 
3087 	if (value) {
3088 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3089 				     BIT(2), BIT(2));
3090 		if (ret)
3091 			return ret;
3092 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
3093 				       value & 0xff);
3094 	} else {
3095 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
3096 	}
3097 
3098 	return ret;
3099 }
3100 
3101 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
3102 {
3103 	int ret;
3104 
3105 	if (value) {
3106 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
3107 				     BIT(1), BIT(1));
3108 		if (ret)
3109 			return ret;
3110 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
3111 				       value & 0xff);
3112 		if (ret)
3113 			return ret;
3114 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
3115 				       value & 0xff);
3116 	} else {
3117 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
3118 	}
3119 
3120 	return ret;
3121 }
3122 
3123 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
3124 {
3125 	int ret;
3126 
3127 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
3128 			     BIT(0), awb ? 0 : 1);
3129 	if (ret)
3130 		return ret;
3131 
3132 	if (!awb) {
3133 		u16 red = (u16)sensor->ctrls.red_balance->val;
3134 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
3135 
3136 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
3137 		if (ret)
3138 			return ret;
3139 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
3140 	}
3141 
3142 	return ret;
3143 }
3144 
3145 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
3146 				    enum v4l2_exposure_auto_type auto_exposure)
3147 {
3148 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
3149 	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
3150 	int ret = 0;
3151 
3152 	if (ctrls->auto_exp->is_new) {
3153 		ret = ov5640_set_autoexposure(sensor, auto_exp);
3154 		if (ret)
3155 			return ret;
3156 	}
3157 
3158 	if (!auto_exp && ctrls->exposure->is_new) {
3159 		u16 max_exp;
3160 
3161 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
3162 					&max_exp);
3163 		if (ret)
3164 			return ret;
3165 		ret = ov5640_get_vts(sensor);
3166 		if (ret < 0)
3167 			return ret;
3168 		max_exp += ret;
3169 		ret = 0;
3170 
3171 		if (ctrls->exposure->val < max_exp)
3172 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
3173 	}
3174 
3175 	return ret;
3176 }
3177 
3178 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
3179 {
3180 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
3181 	int ret = 0;
3182 
3183 	if (ctrls->auto_gain->is_new) {
3184 		ret = ov5640_set_autogain(sensor, auto_gain);
3185 		if (ret)
3186 			return ret;
3187 	}
3188 
3189 	if (!auto_gain && ctrls->gain->is_new)
3190 		ret = ov5640_set_gain(sensor, ctrls->gain->val);
3191 
3192 	return ret;
3193 }
3194 
3195 static const char * const test_pattern_menu[] = {
3196 	"Disabled",
3197 	"Color bars",
3198 	"Color bars w/ rolling bar",
3199 	"Color squares",
3200 	"Color squares w/ rolling bar",
3201 };
3202 
3203 #define OV5640_TEST_ENABLE		BIT(7)
3204 #define OV5640_TEST_ROLLING		BIT(6)	/* rolling horizontal bar */
3205 #define OV5640_TEST_TRANSPARENT		BIT(5)
3206 #define OV5640_TEST_SQUARE_BW		BIT(4)	/* black & white squares */
3207 #define OV5640_TEST_BAR_STANDARD	(0 << 2)
3208 #define OV5640_TEST_BAR_VERT_CHANGE_1	(1 << 2)
3209 #define OV5640_TEST_BAR_HOR_CHANGE	(2 << 2)
3210 #define OV5640_TEST_BAR_VERT_CHANGE_2	(3 << 2)
3211 #define OV5640_TEST_BAR			(0 << 0)
3212 #define OV5640_TEST_RANDOM		(1 << 0)
3213 #define OV5640_TEST_SQUARE		(2 << 0)
3214 #define OV5640_TEST_BLACK		(3 << 0)
3215 
3216 static const u8 test_pattern_val[] = {
3217 	0,
3218 	OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
3219 		OV5640_TEST_BAR,
3220 	OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
3221 		OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
3222 	OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
3223 	OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
3224 };
3225 
3226 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
3227 {
3228 	return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
3229 				test_pattern_val[value]);
3230 }
3231 
3232 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
3233 {
3234 	int ret;
3235 
3236 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
3237 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
3238 			     0 : BIT(7));
3239 	if (ret)
3240 		return ret;
3241 
3242 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
3243 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
3244 			      BIT(2) : 0);
3245 }
3246 
3247 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
3248 {
3249 	/*
3250 	 * If sensor is mounted upside down, mirror logic is inversed.
3251 	 *
3252 	 * Sensor is a BSI (Back Side Illuminated) one,
3253 	 * so image captured is physically mirrored.
3254 	 * This is why mirror logic is inversed in
3255 	 * order to cancel this mirror effect.
3256 	 */
3257 
3258 	/*
3259 	 * TIMING TC REG21:
3260 	 * - [2]:	ISP mirror
3261 	 * - [1]:	Sensor mirror
3262 	 */
3263 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
3264 			      BIT(2) | BIT(1),
3265 			      (!(value ^ sensor->upside_down)) ?
3266 			      (BIT(2) | BIT(1)) : 0);
3267 }
3268 
3269 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
3270 {
3271 	/* If sensor is mounted upside down, flip logic is inversed */
3272 
3273 	/*
3274 	 * TIMING TC REG20:
3275 	 * - [2]:	ISP vflip
3276 	 * - [1]:	Sensor vflip
3277 	 */
3278 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
3279 			      BIT(2) | BIT(1),
3280 			      (value ^ sensor->upside_down) ?
3281 			      (BIT(2) | BIT(1)) : 0);
3282 }
3283 
3284 static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
3285 {
3286 	const struct ov5640_mode_info *mode = sensor->current_mode;
3287 
3288 	/* Update the VTOT timing register value. */
3289 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
3290 				  mode->height + value);
3291 }
3292 
3293 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3294 {
3295 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3296 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3297 	int val;
3298 
3299 	/* v4l2_ctrl_lock() locks our own mutex */
3300 
3301 	if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3302 		return 0;
3303 
3304 	switch (ctrl->id) {
3305 	case V4L2_CID_AUTOGAIN:
3306 		val = ov5640_get_gain(sensor);
3307 		if (val < 0)
3308 			return val;
3309 		sensor->ctrls.gain->val = val;
3310 		break;
3311 	case V4L2_CID_EXPOSURE_AUTO:
3312 		val = ov5640_get_exposure(sensor);
3313 		if (val < 0)
3314 			return val;
3315 		sensor->ctrls.exposure->val = val;
3316 		break;
3317 	}
3318 
3319 	pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3320 
3321 	return 0;
3322 }
3323 
3324 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
3325 {
3326 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
3327 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3328 	const struct ov5640_mode_info *mode = sensor->current_mode;
3329 	const struct ov5640_timings *timings;
3330 	unsigned int exp_max;
3331 	int ret;
3332 
3333 	/* v4l2_ctrl_lock() locks our own mutex */
3334 
3335 	switch (ctrl->id) {
3336 	case V4L2_CID_VBLANK:
3337 		/* Update the exposure range to the newly programmed vblank. */
3338 		timings = ov5640_timings(sensor, mode);
3339 		exp_max = mode->height + ctrl->val - 4;
3340 		__v4l2_ctrl_modify_range(sensor->ctrls.exposure,
3341 					 sensor->ctrls.exposure->minimum,
3342 					 exp_max, sensor->ctrls.exposure->step,
3343 					 timings->vblank_def);
3344 		break;
3345 	}
3346 
3347 	/*
3348 	 * If the device is not powered up by the host driver do
3349 	 * not apply any controls to H/W at this time. Instead
3350 	 * the controls will be restored at start streaming time.
3351 	 */
3352 	if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
3353 		return 0;
3354 
3355 	switch (ctrl->id) {
3356 	case V4L2_CID_AUTOGAIN:
3357 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
3358 		break;
3359 	case V4L2_CID_EXPOSURE_AUTO:
3360 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
3361 		break;
3362 	case V4L2_CID_AUTO_WHITE_BALANCE:
3363 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
3364 		break;
3365 	case V4L2_CID_HUE:
3366 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
3367 		break;
3368 	case V4L2_CID_CONTRAST:
3369 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
3370 		break;
3371 	case V4L2_CID_SATURATION:
3372 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
3373 		break;
3374 	case V4L2_CID_TEST_PATTERN:
3375 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
3376 		break;
3377 	case V4L2_CID_POWER_LINE_FREQUENCY:
3378 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
3379 		break;
3380 	case V4L2_CID_HFLIP:
3381 		ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
3382 		break;
3383 	case V4L2_CID_VFLIP:
3384 		ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
3385 		break;
3386 	case V4L2_CID_VBLANK:
3387 		ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
3388 		break;
3389 	default:
3390 		ret = -EINVAL;
3391 		break;
3392 	}
3393 
3394 	pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3395 
3396 	return ret;
3397 }
3398 
3399 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
3400 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
3401 	.s_ctrl = ov5640_s_ctrl,
3402 };
3403 
3404 static int ov5640_init_controls(struct ov5640_dev *sensor)
3405 {
3406 	const struct ov5640_mode_info *mode = sensor->current_mode;
3407 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
3408 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
3409 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
3410 	struct v4l2_fwnode_device_properties props;
3411 	const struct ov5640_timings *timings;
3412 	unsigned int max_vblank;
3413 	unsigned int hblank;
3414 	int ret;
3415 
3416 	v4l2_ctrl_handler_init(hdl, 32);
3417 
3418 	/* we can use our own mutex for the ctrl lock */
3419 	hdl->lock = &sensor->lock;
3420 
3421 	/* Clock related controls */
3422 	ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
3423 			      ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
3424 			      ov5640_pixel_rates[0], 1,
3425 			      ov5640_pixel_rates[mode->pixel_rate]);
3426 
3427 	ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
3428 					V4L2_CID_LINK_FREQ,
3429 					ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
3430 					OV5640_DEFAULT_LINK_FREQ,
3431 					ov5640_csi2_link_freqs);
3432 
3433 	timings = ov5640_timings(sensor, mode);
3434 	hblank = timings->htot - mode->width;
3435 	ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
3436 					  hblank, 1, hblank);
3437 
3438 	max_vblank = OV5640_MAX_VTS - mode->height;
3439 	ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
3440 					  OV5640_MIN_VBLANK, max_vblank,
3441 					  1, timings->vblank_def);
3442 
3443 	/* Auto/manual white balance */
3444 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
3445 					   V4L2_CID_AUTO_WHITE_BALANCE,
3446 					   0, 1, 1, 1);
3447 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
3448 						0, 4095, 1, 0);
3449 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
3450 					       0, 4095, 1, 0);
3451 	/* Auto/manual exposure */
3452 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
3453 						 V4L2_CID_EXPOSURE_AUTO,
3454 						 V4L2_EXPOSURE_MANUAL, 0,
3455 						 V4L2_EXPOSURE_AUTO);
3456 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
3457 					    0, 65535, 1, 0);
3458 	/* Auto/manual gain */
3459 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
3460 					     0, 1, 1, 1);
3461 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
3462 					0, 1023, 1, 0);
3463 
3464 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
3465 					      0, 255, 1, 64);
3466 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
3467 				       0, 359, 1, 0);
3468 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
3469 					    0, 255, 1, 0);
3470 	ctrls->test_pattern =
3471 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
3472 					     ARRAY_SIZE(test_pattern_menu) - 1,
3473 					     0, 0, test_pattern_menu);
3474 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
3475 					 0, 1, 1, 0);
3476 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
3477 					 0, 1, 1, 0);
3478 
3479 	ctrls->light_freq =
3480 		v4l2_ctrl_new_std_menu(hdl, ops,
3481 				       V4L2_CID_POWER_LINE_FREQUENCY,
3482 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
3483 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
3484 
3485 	if (hdl->error) {
3486 		ret = hdl->error;
3487 		goto free_ctrls;
3488 	}
3489 
3490 	ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props);
3491 	if (ret)
3492 		goto free_ctrls;
3493 
3494 	if (props.rotation == 180)
3495 		sensor->upside_down = true;
3496 
3497 	ret = v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
3498 	if (ret)
3499 		goto free_ctrls;
3500 
3501 	ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3502 	ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3503 	ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
3504 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
3505 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
3506 
3507 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
3508 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
3509 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
3510 
3511 	sensor->sd.ctrl_handler = hdl;
3512 	return 0;
3513 
3514 free_ctrls:
3515 	v4l2_ctrl_handler_free(hdl);
3516 	return ret;
3517 }
3518 
3519 static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
3520 				  struct v4l2_subdev_state *sd_state,
3521 				  struct v4l2_subdev_frame_size_enum *fse)
3522 {
3523 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3524 	u32 bpp = ov5640_code_to_bpp(sensor, fse->code);
3525 	unsigned int index = fse->index;
3526 
3527 	if (fse->pad != 0)
3528 		return -EINVAL;
3529 	if (!bpp)
3530 		return -EINVAL;
3531 
3532 	/* Only low-resolution modes are supported for 24bpp formats. */
3533 	if (bpp == 24 && index >= OV5640_MODE_720P_1280_720)
3534 		return -EINVAL;
3535 
3536 	/* FIXME: Low resolution modes don't work in 8bpp formats. */
3537 	if (bpp == 8)
3538 		index += OV5640_MODE_720P_1280_720;
3539 
3540 	if (index >= OV5640_NUM_MODES)
3541 		return -EINVAL;
3542 
3543 	fse->min_width = ov5640_mode_data[index].width;
3544 	fse->max_width = fse->min_width;
3545 	fse->min_height = ov5640_mode_data[index].height;
3546 	fse->max_height = fse->min_height;
3547 
3548 	return 0;
3549 }
3550 
3551 static int ov5640_enum_frame_interval(
3552 	struct v4l2_subdev *sd,
3553 	struct v4l2_subdev_state *sd_state,
3554 	struct v4l2_subdev_frame_interval_enum *fie)
3555 {
3556 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3557 	struct v4l2_fract tpf;
3558 	int ret;
3559 
3560 	if (fie->pad != 0)
3561 		return -EINVAL;
3562 	if (fie->index >= OV5640_NUM_FRAMERATES)
3563 		return -EINVAL;
3564 
3565 	tpf.numerator = 1;
3566 	tpf.denominator = ov5640_framerates[fie->index];
3567 
3568 	ret = ov5640_try_frame_interval(sensor, &tpf,
3569 					fie->width, fie->height);
3570 	if (ret < 0)
3571 		return -EINVAL;
3572 
3573 	fie->interval = tpf;
3574 	return 0;
3575 }
3576 
3577 static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
3578 				   struct v4l2_subdev_frame_interval *fi)
3579 {
3580 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3581 
3582 	mutex_lock(&sensor->lock);
3583 	fi->interval = sensor->frame_interval;
3584 	mutex_unlock(&sensor->lock);
3585 
3586 	return 0;
3587 }
3588 
3589 static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
3590 				   struct v4l2_subdev_frame_interval *fi)
3591 {
3592 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3593 	const struct ov5640_mode_info *mode;
3594 	int frame_rate, ret = 0;
3595 
3596 	if (fi->pad != 0)
3597 		return -EINVAL;
3598 
3599 	mutex_lock(&sensor->lock);
3600 
3601 	if (sensor->streaming) {
3602 		ret = -EBUSY;
3603 		goto out;
3604 	}
3605 
3606 	mode = sensor->current_mode;
3607 
3608 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
3609 					       mode->width,
3610 					       mode->height);
3611 	if (frame_rate < 0) {
3612 		/* Always return a valid frame interval value */
3613 		fi->interval = sensor->frame_interval;
3614 		goto out;
3615 	}
3616 
3617 	mode = ov5640_find_mode(sensor, mode->width, mode->height, true);
3618 	if (!mode) {
3619 		ret = -EINVAL;
3620 		goto out;
3621 	}
3622 
3623 	if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) {
3624 		ret = -EINVAL;
3625 		goto out;
3626 	}
3627 
3628 	if (mode != sensor->current_mode ||
3629 	    frame_rate != sensor->current_fr) {
3630 		sensor->current_fr = frame_rate;
3631 		sensor->frame_interval = fi->interval;
3632 		sensor->current_mode = mode;
3633 		sensor->pending_mode_change = true;
3634 
3635 		ov5640_update_pixel_rate(sensor);
3636 	}
3637 out:
3638 	mutex_unlock(&sensor->lock);
3639 	return ret;
3640 }
3641 
3642 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
3643 				 struct v4l2_subdev_state *sd_state,
3644 				 struct v4l2_subdev_mbus_code_enum *code)
3645 {
3646 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3647 	const struct ov5640_pixfmt *formats;
3648 	unsigned int num_formats;
3649 
3650 	if (ov5640_is_csi2(sensor)) {
3651 		formats = ov5640_csi2_formats;
3652 		num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1;
3653 	} else {
3654 		formats = ov5640_dvp_formats;
3655 		num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1;
3656 	}
3657 
3658 	if (code->index >= num_formats)
3659 		return -EINVAL;
3660 
3661 	code->code = formats[code->index].code;
3662 
3663 	return 0;
3664 }
3665 
3666 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
3667 {
3668 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3669 	int ret = 0;
3670 
3671 	if (enable) {
3672 		ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
3673 		if (ret < 0)
3674 			return ret;
3675 
3676 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
3677 		if (ret) {
3678 			pm_runtime_put(&sensor->i2c_client->dev);
3679 			return ret;
3680 		}
3681 	}
3682 
3683 	mutex_lock(&sensor->lock);
3684 
3685 	if (sensor->streaming == !enable) {
3686 		if (enable && sensor->pending_mode_change) {
3687 			ret = ov5640_set_mode(sensor);
3688 			if (ret)
3689 				goto out;
3690 		}
3691 
3692 		if (enable && sensor->pending_fmt_change) {
3693 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3694 			if (ret)
3695 				goto out;
3696 			sensor->pending_fmt_change = false;
3697 		}
3698 
3699 		if (ov5640_is_csi2(sensor))
3700 			ret = ov5640_set_stream_mipi(sensor, enable);
3701 		else
3702 			ret = ov5640_set_stream_dvp(sensor, enable);
3703 
3704 		if (!ret)
3705 			sensor->streaming = enable;
3706 	}
3707 
3708 out:
3709 	mutex_unlock(&sensor->lock);
3710 
3711 	if (!enable || ret)
3712 		pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
3713 
3714 	return ret;
3715 }
3716 
3717 static int ov5640_init_cfg(struct v4l2_subdev *sd,
3718 			   struct v4l2_subdev_state *state)
3719 {
3720 	struct v4l2_mbus_framefmt *fmt =
3721 				v4l2_subdev_get_try_format(sd, state, 0);
3722 	struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
3723 
3724 	*fmt = ov5640_default_fmt;
3725 
3726 	crop->left = OV5640_PIXEL_ARRAY_LEFT;
3727 	crop->top = OV5640_PIXEL_ARRAY_TOP;
3728 	crop->width = OV5640_PIXEL_ARRAY_WIDTH;
3729 	crop->height = OV5640_PIXEL_ARRAY_HEIGHT;
3730 
3731 	return 0;
3732 }
3733 
3734 static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3735 	.log_status = v4l2_ctrl_subdev_log_status,
3736 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3737 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
3738 };
3739 
3740 static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3741 	.g_frame_interval = ov5640_g_frame_interval,
3742 	.s_frame_interval = ov5640_s_frame_interval,
3743 	.s_stream = ov5640_s_stream,
3744 };
3745 
3746 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3747 	.init_cfg = ov5640_init_cfg,
3748 	.enum_mbus_code = ov5640_enum_mbus_code,
3749 	.get_fmt = ov5640_get_fmt,
3750 	.set_fmt = ov5640_set_fmt,
3751 	.get_selection = ov5640_get_selection,
3752 	.enum_frame_size = ov5640_enum_frame_size,
3753 	.enum_frame_interval = ov5640_enum_frame_interval,
3754 };
3755 
3756 static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3757 	.core = &ov5640_core_ops,
3758 	.video = &ov5640_video_ops,
3759 	.pad = &ov5640_pad_ops,
3760 };
3761 
3762 static int ov5640_get_regulators(struct ov5640_dev *sensor)
3763 {
3764 	int i;
3765 
3766 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3767 		sensor->supplies[i].supply = ov5640_supply_name[i];
3768 
3769 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3770 				       OV5640_NUM_SUPPLIES,
3771 				       sensor->supplies);
3772 }
3773 
3774 static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3775 {
3776 	struct i2c_client *client = sensor->i2c_client;
3777 	int ret = 0;
3778 	u16 chip_id;
3779 
3780 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3781 	if (ret) {
3782 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
3783 			__func__);
3784 		return ret;
3785 	}
3786 
3787 	if (chip_id != 0x5640) {
3788 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3789 			__func__, chip_id);
3790 		return -ENXIO;
3791 	}
3792 
3793 	return 0;
3794 }
3795 
3796 static int ov5640_probe(struct i2c_client *client)
3797 {
3798 	struct device *dev = &client->dev;
3799 	struct fwnode_handle *endpoint;
3800 	struct ov5640_dev *sensor;
3801 	int ret;
3802 
3803 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3804 	if (!sensor)
3805 		return -ENOMEM;
3806 
3807 	sensor->i2c_client = client;
3808 
3809 	/*
3810 	 * default init sequence initialize sensor to
3811 	 * YUV422 UYVY VGA@30fps
3812 	 */
3813 	sensor->fmt = ov5640_default_fmt;
3814 	sensor->frame_interval.numerator = 1;
3815 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3816 	sensor->current_fr = OV5640_30_FPS;
3817 	sensor->current_mode =
3818 		&ov5640_mode_data[OV5640_MODE_VGA_640_480];
3819 	sensor->last_mode = sensor->current_mode;
3820 	sensor->current_link_freq = OV5640_DEFAULT_LINK_FREQ;
3821 
3822 	sensor->ae_target = 52;
3823 
3824 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3825 						  NULL);
3826 	if (!endpoint) {
3827 		dev_err(dev, "endpoint node not found\n");
3828 		return -EINVAL;
3829 	}
3830 
3831 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3832 	fwnode_handle_put(endpoint);
3833 	if (ret) {
3834 		dev_err(dev, "Could not parse endpoint\n");
3835 		return ret;
3836 	}
3837 
3838 	if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
3839 	    sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
3840 	    sensor->ep.bus_type != V4L2_MBUS_BT656) {
3841 		dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
3842 		return -EINVAL;
3843 	}
3844 
3845 	/* get system clock (xclk) */
3846 	sensor->xclk = devm_clk_get(dev, "xclk");
3847 	if (IS_ERR(sensor->xclk)) {
3848 		dev_err(dev, "failed to get xclk\n");
3849 		return PTR_ERR(sensor->xclk);
3850 	}
3851 
3852 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
3853 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3854 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
3855 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
3856 			sensor->xclk_freq);
3857 		return -EINVAL;
3858 	}
3859 
3860 	/* request optional power down pin */
3861 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3862 						    GPIOD_OUT_HIGH);
3863 	if (IS_ERR(sensor->pwdn_gpio))
3864 		return PTR_ERR(sensor->pwdn_gpio);
3865 
3866 	/* request optional reset pin */
3867 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3868 						     GPIOD_OUT_HIGH);
3869 	if (IS_ERR(sensor->reset_gpio))
3870 		return PTR_ERR(sensor->reset_gpio);
3871 
3872 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3873 
3874 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3875 			    V4L2_SUBDEV_FL_HAS_EVENTS;
3876 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3877 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3878 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3879 	if (ret)
3880 		return ret;
3881 
3882 	ret = ov5640_get_regulators(sensor);
3883 	if (ret)
3884 		goto entity_cleanup;
3885 
3886 	mutex_init(&sensor->lock);
3887 
3888 	ret = ov5640_init_controls(sensor);
3889 	if (ret)
3890 		goto entity_cleanup;
3891 
3892 	ret = ov5640_sensor_resume(dev);
3893 	if (ret) {
3894 		dev_err(dev, "failed to power on\n");
3895 		goto entity_cleanup;
3896 	}
3897 
3898 	pm_runtime_set_active(dev);
3899 	pm_runtime_get_noresume(dev);
3900 	pm_runtime_enable(dev);
3901 
3902 	ret = ov5640_check_chip_id(sensor);
3903 	if (ret)
3904 		goto err_pm_runtime;
3905 
3906 	ret = v4l2_async_register_subdev_sensor(&sensor->sd);
3907 	if (ret)
3908 		goto err_pm_runtime;
3909 
3910 	pm_runtime_set_autosuspend_delay(dev, 1000);
3911 	pm_runtime_use_autosuspend(dev);
3912 	pm_runtime_put_autosuspend(dev);
3913 
3914 	return 0;
3915 
3916 err_pm_runtime:
3917 	pm_runtime_put_noidle(dev);
3918 	pm_runtime_disable(dev);
3919 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3920 	ov5640_sensor_suspend(dev);
3921 entity_cleanup:
3922 	media_entity_cleanup(&sensor->sd.entity);
3923 	mutex_destroy(&sensor->lock);
3924 	return ret;
3925 }
3926 
3927 static void ov5640_remove(struct i2c_client *client)
3928 {
3929 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
3930 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
3931 	struct device *dev = &client->dev;
3932 
3933 	pm_runtime_disable(dev);
3934 	if (!pm_runtime_status_suspended(dev))
3935 		ov5640_sensor_suspend(dev);
3936 	pm_runtime_set_suspended(dev);
3937 
3938 	v4l2_async_unregister_subdev(&sensor->sd);
3939 	media_entity_cleanup(&sensor->sd.entity);
3940 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3941 	mutex_destroy(&sensor->lock);
3942 }
3943 
3944 static const struct dev_pm_ops ov5640_pm_ops = {
3945 	SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
3946 };
3947 
3948 static const struct i2c_device_id ov5640_id[] = {
3949 	{"ov5640", 0},
3950 	{},
3951 };
3952 MODULE_DEVICE_TABLE(i2c, ov5640_id);
3953 
3954 static const struct of_device_id ov5640_dt_ids[] = {
3955 	{ .compatible = "ovti,ov5640" },
3956 	{ /* sentinel */ }
3957 };
3958 MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3959 
3960 static struct i2c_driver ov5640_i2c_driver = {
3961 	.driver = {
3962 		.name  = "ov5640",
3963 		.of_match_table	= ov5640_dt_ids,
3964 		.pm = &ov5640_pm_ops,
3965 	},
3966 	.id_table = ov5640_id,
3967 	.probe_new = ov5640_probe,
3968 	.remove   = ov5640_remove,
3969 };
3970 
3971 module_i2c_driver(ov5640_i2c_driver);
3972 
3973 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3974 MODULE_LICENSE("GPL");
3975