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