xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 8409d017)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
219a81c14SSteve Longerbeam /*
319a81c14SSteve Longerbeam  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
419a81c14SSteve Longerbeam  * Copyright (C) 2014-2017 Mentor Graphics Inc.
519a81c14SSteve Longerbeam  */
619a81c14SSteve Longerbeam 
719a81c14SSteve Longerbeam #include <linux/clk.h>
819a81c14SSteve Longerbeam #include <linux/clk-provider.h>
919a81c14SSteve Longerbeam #include <linux/clkdev.h>
1019a81c14SSteve Longerbeam #include <linux/ctype.h>
1119a81c14SSteve Longerbeam #include <linux/delay.h>
1219a81c14SSteve Longerbeam #include <linux/device.h>
1341d8d7f5SHugues Fruchet #include <linux/gpio/consumer.h>
1419a81c14SSteve Longerbeam #include <linux/i2c.h>
1519a81c14SSteve Longerbeam #include <linux/init.h>
1619a81c14SSteve Longerbeam #include <linux/module.h>
1719a81c14SSteve Longerbeam #include <linux/of_device.h>
1841d8d7f5SHugues Fruchet #include <linux/regulator/consumer.h>
1919a81c14SSteve Longerbeam #include <linux/slab.h>
2019a81c14SSteve Longerbeam #include <linux/types.h>
2119a81c14SSteve Longerbeam #include <media/v4l2-async.h>
2219a81c14SSteve Longerbeam #include <media/v4l2-ctrls.h>
2319a81c14SSteve Longerbeam #include <media/v4l2-device.h>
242d18fbc5SAkinobu Mita #include <media/v4l2-event.h>
2519a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h>
2619a81c14SSteve Longerbeam #include <media/v4l2-subdev.h>
2719a81c14SSteve Longerbeam 
2819a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */
2919a81c14SSteve Longerbeam #define OV5640_XCLK_MIN  6000000
3041cb1c73SPhilipp Puschmann #define OV5640_XCLK_MAX 54000000
3119a81c14SSteve Longerbeam 
3219a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c
3319a81c14SSteve Longerbeam 
34d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02		0x3002
35d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
36f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0		0x3008
373b987d70SLad Prabhakar #define OV5640_REG_SYS_CTRL0_SW_PWDN	0x42
383b987d70SLad Prabhakar #define OV5640_REG_SYS_CTRL0_SW_PWUP	0x02
3919a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID		0x300a
40f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00	0x300e
41f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
42f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
4319a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00		0x3019
44f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1	0x302e
4519a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0		0x3034
4619a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1		0x3035
4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2		0x3036
4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3		0x3037
4919a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID		0x3100
50f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
5119a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
5219a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN		0x3400
5319a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN		0x3402
5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN		0x3404
5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
5619a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
5719a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL	0x3503
6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS		0x350c
6286633417SMaxime Ripard #define OV5640_REG_TIMING_DVPHO		0x3808
6386633417SMaxime Ripard #define OV5640_REG_TIMING_DVPVO		0x380a
6419a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS		0x380c
6519a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS		0x380e
66ce85705aSHugues Fruchet #define OV5640_REG_TIMING_TC_REG20	0x3820
6719a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21	0x3821
6819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00		0x3a00
6919a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP		0x3a08
7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP		0x3a0a
7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D		0x3a0d
7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E		0x3a0e
7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F		0x3a0f
7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10		0x3a10
7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11		0x3a11
7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B		0x3a1b
7719a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E		0x3a1e
7819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F		0x3a1f
7919a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00	0x3c00
8019a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01	0x3c01
8119a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
8219a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01		0x4202
83e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00	0x4300
847cb013b1SChen-Yu Tsai #define OV5640_REG_VFIFO_HSIZE		0x4602
857cb013b1SChen-Yu Tsai #define OV5640_REG_VFIFO_VSIZE		0x4604
862b5c18f9SChen-Yu Tsai #define OV5640_REG_JPG_MODE_SELECT	0x4713
874039b037SLad Prabhakar #define OV5640_REG_CCIR656_CTRL00	0x4730
88f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00	0x4740
8919a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00		0x4800
9019a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE		0x4814
91e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
9219a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
9319a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0		0x5580
9419a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1		0x5581
9519a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3		0x5583
9619a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4		0x5584
9719a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5		0x5585
9819a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT		0x56a1
9919a81c14SSteve Longerbeam 
10019a81c14SSteve Longerbeam enum ov5640_mode_id {
10132ea5e05SHugues Fruchet 	OV5640_MODE_QQVGA_160_120 = 0,
10232ea5e05SHugues Fruchet 	OV5640_MODE_QCIF_176_144,
10319a81c14SSteve Longerbeam 	OV5640_MODE_QVGA_320_240,
10419a81c14SSteve Longerbeam 	OV5640_MODE_VGA_640_480,
10519a81c14SSteve Longerbeam 	OV5640_MODE_NTSC_720_480,
10619a81c14SSteve Longerbeam 	OV5640_MODE_PAL_720_576,
10719a81c14SSteve Longerbeam 	OV5640_MODE_XGA_1024_768,
10819a81c14SSteve Longerbeam 	OV5640_MODE_720P_1280_720,
10919a81c14SSteve Longerbeam 	OV5640_MODE_1080P_1920_1080,
11019a81c14SSteve Longerbeam 	OV5640_MODE_QSXGA_2592_1944,
11119a81c14SSteve Longerbeam 	OV5640_NUM_MODES,
11219a81c14SSteve Longerbeam };
11319a81c14SSteve Longerbeam 
11419a81c14SSteve Longerbeam enum ov5640_frame_rate {
11519a81c14SSteve Longerbeam 	OV5640_15_FPS = 0,
11619a81c14SSteve Longerbeam 	OV5640_30_FPS,
117e823fb16SMaxime Ripard 	OV5640_60_FPS,
11819a81c14SSteve Longerbeam 	OV5640_NUM_FRAMERATES,
11919a81c14SSteve Longerbeam };
12019a81c14SSteve Longerbeam 
12122845bf2SJacopo Mondi enum ov5640_pixel_rate_id {
12222845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_168M,
12322845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_148M,
12422845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_124M,
12522845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_96M,
12622845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_48M,
12722845bf2SJacopo Mondi 	OV5640_NUM_PIXEL_RATES,
12822845bf2SJacopo Mondi };
12922845bf2SJacopo Mondi 
13022845bf2SJacopo Mondi /*
13122845bf2SJacopo Mondi  * The chip manual suggests 24/48/96/192 MHz pixel clocks.
13222845bf2SJacopo Mondi  *
13322845bf2SJacopo Mondi  * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
13422845bf2SJacopo Mondi  * full resolution mode @15 FPS.
13522845bf2SJacopo Mondi  */
13622845bf2SJacopo Mondi static const u32 ov5640_pixel_rates[] = {
13722845bf2SJacopo Mondi 	[OV5640_PIXEL_RATE_168M] = 168000000,
13822845bf2SJacopo Mondi 	[OV5640_PIXEL_RATE_148M] = 148000000,
13922845bf2SJacopo Mondi 	[OV5640_PIXEL_RATE_124M] = 124000000,
14022845bf2SJacopo Mondi 	[OV5640_PIXEL_RATE_96M] = 96000000,
14122845bf2SJacopo Mondi 	[OV5640_PIXEL_RATE_48M] = 48000000,
14222845bf2SJacopo Mondi };
14322845bf2SJacopo Mondi 
144b7ed3abdSLoic Poulain enum ov5640_format_mux {
145b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_YUV422 = 0,
146b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_RGB,
147b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_DITHER,
148b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_RAW_DPC,
149b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_SNR_RAW,
150b7ed3abdSLoic Poulain 	OV5640_FMT_MUX_RAW_CIP,
151b7ed3abdSLoic Poulain };
152b7ed3abdSLoic Poulain 
153e3ee691dSHugues Fruchet struct ov5640_pixfmt {
154e3ee691dSHugues Fruchet 	u32 code;
155e3ee691dSHugues Fruchet 	u32 colorspace;
156e3ee691dSHugues Fruchet };
157e3ee691dSHugues Fruchet 
158e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = {
159d47c4126SHugues Fruchet 	{ MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
160e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
1611536fbdbSXavier Roumegue 	{ MEDIA_BUS_FMT_UYVY8_1X16, V4L2_COLORSPACE_SRGB, },
162e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
1631536fbdbSXavier Roumegue 	{ MEDIA_BUS_FMT_YUYV8_1X16, V4L2_COLORSPACE_SRGB, },
164e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
165e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
166b7ed3abdSLoic Poulain 	{ MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB, },
167b7ed3abdSLoic Poulain 	{ MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_COLORSPACE_SRGB, },
168b7ed3abdSLoic Poulain 	{ MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_COLORSPACE_SRGB, },
169b7ed3abdSLoic Poulain 	{ MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB, },
170e3ee691dSHugues Fruchet };
171e3ee691dSHugues Fruchet 
17219a81c14SSteve Longerbeam /*
17319a81c14SSteve Longerbeam  * FIXME: remove this when a subdev API becomes available
17419a81c14SSteve Longerbeam  * to set the MIPI CSI-2 virtual channel.
17519a81c14SSteve Longerbeam  */
17619a81c14SSteve Longerbeam static unsigned int virtual_channel;
1778670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444);
17819a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel,
17919a81c14SSteve Longerbeam 		 "MIPI CSI-2 virtual channel (0..3), default 0");
18019a81c14SSteve Longerbeam 
18119a81c14SSteve Longerbeam static const int ov5640_framerates[] = {
18219a81c14SSteve Longerbeam 	[OV5640_15_FPS] = 15,
18319a81c14SSteve Longerbeam 	[OV5640_30_FPS] = 30,
184e823fb16SMaxime Ripard 	[OV5640_60_FPS] = 60,
18519a81c14SSteve Longerbeam };
18619a81c14SSteve Longerbeam 
18719a81c14SSteve Longerbeam /* regulator supplies */
18819a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = {
18941d8d7f5SHugues Fruchet 	"DOVDD", /* Digital I/O (1.8V) supply */
19019a81c14SSteve Longerbeam 	"AVDD",  /* Analog (2.8V) supply */
19124c8ac89SFabio Estevam 	"DVDD",  /* Digital Core (1.5V) supply */
19219a81c14SSteve Longerbeam };
19319a81c14SSteve Longerbeam 
19419a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
19519a81c14SSteve Longerbeam 
19619a81c14SSteve Longerbeam /*
19719a81c14SSteve Longerbeam  * Image size under 1280 * 960 are SUBSAMPLING
19819a81c14SSteve Longerbeam  * Image size upper 1280 * 960 are SCALING
19919a81c14SSteve Longerbeam  */
20019a81c14SSteve Longerbeam enum ov5640_downsize_mode {
20119a81c14SSteve Longerbeam 	SUBSAMPLING,
20219a81c14SSteve Longerbeam 	SCALING,
20319a81c14SSteve Longerbeam };
20419a81c14SSteve Longerbeam 
20519a81c14SSteve Longerbeam struct reg_value {
20619a81c14SSteve Longerbeam 	u16 reg_addr;
20719a81c14SSteve Longerbeam 	u8 val;
20819a81c14SSteve Longerbeam 	u8 mask;
20919a81c14SSteve Longerbeam 	u32 delay_ms;
21019a81c14SSteve Longerbeam };
21119a81c14SSteve Longerbeam 
21219a81c14SSteve Longerbeam struct ov5640_mode_info {
21319a81c14SSteve Longerbeam 	enum ov5640_mode_id id;
21419a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode;
21522845bf2SJacopo Mondi 	enum ov5640_pixel_rate_id pixel_rate;
216dba13a0bSMaxime Ripard 	u32 hact;
217476dec01SMaxime Ripard 	u32 htot;
218dba13a0bSMaxime Ripard 	u32 vact;
219476dec01SMaxime Ripard 	u32 vtot;
22019a81c14SSteve Longerbeam 	const struct reg_value *reg_data;
22119a81c14SSteve Longerbeam 	u32 reg_data_size;
2225554c80eSAdam Ford 	u32 max_fps;
22319a81c14SSteve Longerbeam };
22419a81c14SSteve Longerbeam 
22519a81c14SSteve Longerbeam struct ov5640_ctrls {
22619a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler handler;
227cc196e48SBenoit Parrot 	struct v4l2_ctrl *pixel_rate;
22819a81c14SSteve Longerbeam 	struct {
22919a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_exp;
23019a81c14SSteve Longerbeam 		struct v4l2_ctrl *exposure;
23119a81c14SSteve Longerbeam 	};
23219a81c14SSteve Longerbeam 	struct {
23319a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_wb;
23419a81c14SSteve Longerbeam 		struct v4l2_ctrl *blue_balance;
23519a81c14SSteve Longerbeam 		struct v4l2_ctrl *red_balance;
23619a81c14SSteve Longerbeam 	};
23719a81c14SSteve Longerbeam 	struct {
23819a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_gain;
23919a81c14SSteve Longerbeam 		struct v4l2_ctrl *gain;
24019a81c14SSteve Longerbeam 	};
24119a81c14SSteve Longerbeam 	struct v4l2_ctrl *brightness;
2421068fecaSMylène Josserand 	struct v4l2_ctrl *light_freq;
24319a81c14SSteve Longerbeam 	struct v4l2_ctrl *saturation;
24419a81c14SSteve Longerbeam 	struct v4l2_ctrl *contrast;
24519a81c14SSteve Longerbeam 	struct v4l2_ctrl *hue;
24619a81c14SSteve Longerbeam 	struct v4l2_ctrl *test_pattern;
247ce85705aSHugues Fruchet 	struct v4l2_ctrl *hflip;
248ce85705aSHugues Fruchet 	struct v4l2_ctrl *vflip;
24919a81c14SSteve Longerbeam };
25019a81c14SSteve Longerbeam 
25119a81c14SSteve Longerbeam struct ov5640_dev {
25219a81c14SSteve Longerbeam 	struct i2c_client *i2c_client;
25319a81c14SSteve Longerbeam 	struct v4l2_subdev sd;
25419a81c14SSteve Longerbeam 	struct media_pad pad;
25519a81c14SSteve Longerbeam 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
25619a81c14SSteve Longerbeam 	struct clk *xclk; /* system clock to OV5640 */
25719a81c14SSteve Longerbeam 	u32 xclk_freq;
25819a81c14SSteve Longerbeam 
25919a81c14SSteve Longerbeam 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
26019a81c14SSteve Longerbeam 	struct gpio_desc *reset_gpio;
26119a81c14SSteve Longerbeam 	struct gpio_desc *pwdn_gpio;
262c3f3ba3eSHugues Fruchet 	bool   upside_down;
26319a81c14SSteve Longerbeam 
26419a81c14SSteve Longerbeam 	/* lock to protect all members below */
26519a81c14SSteve Longerbeam 	struct mutex lock;
26619a81c14SSteve Longerbeam 
26719a81c14SSteve Longerbeam 	int power_count;
26819a81c14SSteve Longerbeam 
26919a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt fmt;
270fb98e29fSHugues Fruchet 	bool pending_fmt_change;
27119a81c14SSteve Longerbeam 
27219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *current_mode;
273985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *last_mode;
27419a81c14SSteve Longerbeam 	enum ov5640_frame_rate current_fr;
27519a81c14SSteve Longerbeam 	struct v4l2_fract frame_interval;
27619a81c14SSteve Longerbeam 
27719a81c14SSteve Longerbeam 	struct ov5640_ctrls ctrls;
27819a81c14SSteve Longerbeam 
27919a81c14SSteve Longerbeam 	u32 prev_sysclk, prev_hts;
28019a81c14SSteve Longerbeam 	u32 ae_low, ae_high, ae_target;
28119a81c14SSteve Longerbeam 
28219a81c14SSteve Longerbeam 	bool pending_mode_change;
28319a81c14SSteve Longerbeam 	bool streaming;
28419a81c14SSteve Longerbeam };
28519a81c14SSteve Longerbeam 
28619a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
28719a81c14SSteve Longerbeam {
28819a81c14SSteve Longerbeam 	return container_of(sd, struct ov5640_dev, sd);
28919a81c14SSteve Longerbeam }
29019a81c14SSteve Longerbeam 
29119a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
29219a81c14SSteve Longerbeam {
29319a81c14SSteve Longerbeam 	return &container_of(ctrl->handler, struct ov5640_dev,
29419a81c14SSteve Longerbeam 			     ctrls.handler)->sd;
29519a81c14SSteve Longerbeam }
29619a81c14SSteve Longerbeam 
29719a81c14SSteve Longerbeam /*
29819a81c14SSteve Longerbeam  * FIXME: all of these register tables are likely filled with
29919a81c14SSteve Longerbeam  * entries that set the register to their power-on default values,
30019a81c14SSteve Longerbeam  * and which are otherwise not touched by this driver. Those entries
30119a81c14SSteve Longerbeam  * should be identified and removed to speed register load time
30219a81c14SSteve Longerbeam  * over i2c.
30319a81c14SSteve Longerbeam  */
304fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */
30519a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
30619a81c14SSteve Longerbeam 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
307576f5d4bSLad Prabhakar 	{0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
30819a81c14SSteve Longerbeam 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
30919a81c14SSteve Longerbeam 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
31019a81c14SSteve Longerbeam 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
31119a81c14SSteve Longerbeam 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
31219a81c14SSteve Longerbeam 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
31319a81c14SSteve Longerbeam 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
31419a81c14SSteve Longerbeam 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
31519a81c14SSteve Longerbeam 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
31619a81c14SSteve Longerbeam 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
31719a81c14SSteve Longerbeam 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
31819a81c14SSteve Longerbeam 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
31919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
32019a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
32119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
32219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
32319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
324476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
32519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
32619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
32719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
32819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
32919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
33019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
33119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
33219a81c14SSteve Longerbeam 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
333aa4bb8b8SJacopo Mondi 	{0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
3342b5c18f9SChen-Yu Tsai 	{0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
33519a81c14SSteve Longerbeam 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
336aa4bb8b8SJacopo Mondi 	{0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
33719a81c14SSteve Longerbeam 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
33819a81c14SSteve Longerbeam 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
33919a81c14SSteve Longerbeam 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
34019a81c14SSteve Longerbeam 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
34119a81c14SSteve Longerbeam 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
34219a81c14SSteve Longerbeam 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
34319a81c14SSteve Longerbeam 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
34419a81c14SSteve Longerbeam 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
34519a81c14SSteve Longerbeam 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
34619a81c14SSteve Longerbeam 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
34719a81c14SSteve Longerbeam 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
34819a81c14SSteve Longerbeam 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
34919a81c14SSteve Longerbeam 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
35019a81c14SSteve Longerbeam 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
35119a81c14SSteve Longerbeam 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
35219a81c14SSteve Longerbeam 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
35319a81c14SSteve Longerbeam 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
35419a81c14SSteve Longerbeam 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
35519a81c14SSteve Longerbeam 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
35619a81c14SSteve Longerbeam 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
35719a81c14SSteve Longerbeam 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
35819a81c14SSteve Longerbeam 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
35919a81c14SSteve Longerbeam 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
36019a81c14SSteve Longerbeam 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
36119a81c14SSteve Longerbeam 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
36219a81c14SSteve Longerbeam 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
36319a81c14SSteve Longerbeam 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
36419a81c14SSteve Longerbeam 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
36519a81c14SSteve Longerbeam 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
36619a81c14SSteve Longerbeam 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
36719a81c14SSteve Longerbeam 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
36819a81c14SSteve Longerbeam 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
36919a81c14SSteve Longerbeam 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
37019a81c14SSteve Longerbeam 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
37119a81c14SSteve Longerbeam 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
37219a81c14SSteve Longerbeam 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
37319a81c14SSteve Longerbeam 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
37419a81c14SSteve Longerbeam 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
37519a81c14SSteve Longerbeam 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
37619a81c14SSteve Longerbeam 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
37719a81c14SSteve Longerbeam 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
37819a81c14SSteve Longerbeam 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
37919a81c14SSteve Longerbeam 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
38019a81c14SSteve Longerbeam 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
38119a81c14SSteve Longerbeam 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
38219a81c14SSteve Longerbeam 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
38319a81c14SSteve Longerbeam 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
38419a81c14SSteve Longerbeam 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
38519a81c14SSteve Longerbeam 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
38619a81c14SSteve Longerbeam 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
38719a81c14SSteve Longerbeam };
38819a81c14SSteve Longerbeam 
389086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_VGA_640_480[] = {
390c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
39119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
392ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
39319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
39419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
39519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
396476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
39719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
39819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
39919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
40019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
40119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
40219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
4032b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
40419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
40519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
40619a81c14SSteve Longerbeam };
40719a81c14SSteve Longerbeam 
408086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_XGA_1024_768[] = {
409c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
41019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
411ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
41219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
41319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
41419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
415476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
41619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
41719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
41819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
41919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
42019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
42119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
4222b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
42319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
42486633417SMaxime Ripard 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
42519a81c14SSteve Longerbeam };
42619a81c14SSteve Longerbeam 
427086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QVGA_320_240[] = {
428c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
42919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
430ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
43119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
43219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
43319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
434476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
43519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
43619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
43719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
43819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
43919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
44019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
4412b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
44219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
44319a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
44419a81c14SSteve Longerbeam };
44519a81c14SSteve Longerbeam 
44632ea5e05SHugues Fruchet static const struct reg_value ov5640_setting_QQVGA_160_120[] = {
44732ea5e05SHugues Fruchet 	{0x3c07, 0x08, 0, 0},
44832ea5e05SHugues Fruchet 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
44932ea5e05SHugues Fruchet 	{0x3814, 0x31, 0, 0},
45032ea5e05SHugues Fruchet 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
45132ea5e05SHugues Fruchet 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
45232ea5e05SHugues Fruchet 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
45332ea5e05SHugues Fruchet 	{0x3810, 0x00, 0, 0},
45432ea5e05SHugues Fruchet 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
45532ea5e05SHugues Fruchet 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
45632ea5e05SHugues Fruchet 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
45732ea5e05SHugues Fruchet 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
45832ea5e05SHugues Fruchet 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
45932ea5e05SHugues Fruchet 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
46032ea5e05SHugues Fruchet 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
46132ea5e05SHugues Fruchet 	{0x4407, 0x04, 0, 0}, {0x5001, 0xa3, 0, 0},
46232ea5e05SHugues Fruchet };
46332ea5e05SHugues Fruchet 
464086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QCIF_176_144[] = {
465c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
46619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
467ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
46819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
46919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
47019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
471476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
47219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
47319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
47419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
47519a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
47619a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
47719a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
4782b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
47919a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
48019a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
48119a81c14SSteve Longerbeam };
48219a81c14SSteve Longerbeam 
483086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_NTSC_720_480[] = {
484c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
48519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
486ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
48719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
48819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
48919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
490476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
49119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
49219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
49319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
49419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
49519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
49619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
4972b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
49819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
49919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
50019a81c14SSteve Longerbeam };
50119a81c14SSteve Longerbeam 
502086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_PAL_720_576[] = {
503c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
50419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
505ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
50619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
50719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
50819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
509476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
51019a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
51119a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
51219a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
51319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
51419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
51519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
5162b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
51719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
51819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
51919a81c14SSteve Longerbeam };
52019a81c14SSteve Longerbeam 
521086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_720P_1280_720[] = {
522c14d107eSMaxime Ripard 	{0x3c07, 0x07, 0, 0},
52319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
524ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
52519a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
52619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
52719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
528476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
52919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
53019a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
53119a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
53219a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
53319a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
53419a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
5352b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
53619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
53719a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
53819a81c14SSteve Longerbeam };
53919a81c14SSteve Longerbeam 
540086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
541c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
54219a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
543ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
54419a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
54519a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
54619a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
547476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
54819a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
54919a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
55019a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
55119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
55219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
55319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
5542b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
55519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
556c14d107eSMaxime Ripard 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
557c14d107eSMaxime Ripard 	{0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
55819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
55919a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
56019a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
56186633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
562476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
56319a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
56419a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
56519a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
5662b5c18f9SChen-Yu Tsai 	{0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
56719a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
56892b9096cSBenoit Parrot 	{0x4005, 0x1a, 0, 0},
56919a81c14SSteve Longerbeam };
57019a81c14SSteve Longerbeam 
571086c25f8SMaxime Ripard static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
572c14d107eSMaxime Ripard 	{0x3c07, 0x08, 0, 0},
57319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
574ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
57519a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
57619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
57719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
578476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
57919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
58019a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
58119a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
58219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
58319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
58419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
5852b5c18f9SChen-Yu Tsai 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
58619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
58719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
58819a81c14SSteve Longerbeam };
58919a81c14SSteve Longerbeam 
59019a81c14SSteve Longerbeam /* power-on sensor init reg table */
59119a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = {
59222845bf2SJacopo Mondi 	0, SUBSAMPLING,
59322845bf2SJacopo Mondi 	OV5640_PIXEL_RATE_96M,
59422845bf2SJacopo Mondi 	640, 1896, 480, 984,
595476dec01SMaxime Ripard 	ov5640_init_setting_30fps_VGA,
59619a81c14SSteve Longerbeam 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
5975554c80eSAdam Ford 	OV5640_30_FPS,
59819a81c14SSteve Longerbeam };
59919a81c14SSteve Longerbeam 
60019a81c14SSteve Longerbeam static const struct ov5640_mode_info
601086c25f8SMaxime Ripard ov5640_mode_data[OV5640_NUM_MODES] = {
602*8409d017SJacopo Mondi 	{
603*8409d017SJacopo Mondi 		/* 160x120 */
604*8409d017SJacopo Mondi 		OV5640_MODE_QQVGA_160_120, SUBSAMPLING,
60522845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_48M,
60632ea5e05SHugues Fruchet 		160, 1896, 120, 984,
60732ea5e05SHugues Fruchet 		ov5640_setting_QQVGA_160_120,
60832ea5e05SHugues Fruchet 		ARRAY_SIZE(ov5640_setting_QQVGA_160_120),
609*8409d017SJacopo Mondi 		OV5640_30_FPS
610*8409d017SJacopo Mondi 	}, {
611*8409d017SJacopo Mondi 		/* 176x144 */
612*8409d017SJacopo Mondi 		OV5640_MODE_QCIF_176_144, SUBSAMPLING,
61322845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_48M,
614476dec01SMaxime Ripard 		176, 1896, 144, 984,
615086c25f8SMaxime Ripard 		ov5640_setting_QCIF_176_144,
6165554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_QCIF_176_144),
617*8409d017SJacopo Mondi 		OV5640_30_FPS
618*8409d017SJacopo Mondi 	}, {
619*8409d017SJacopo Mondi 		/* 320x240 */
620*8409d017SJacopo Mondi 		OV5640_MODE_QVGA_320_240, SUBSAMPLING,
62122845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_48M,
622476dec01SMaxime Ripard 		320, 1896, 240, 984,
623086c25f8SMaxime Ripard 		ov5640_setting_QVGA_320_240,
6245554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_QVGA_320_240),
625*8409d017SJacopo Mondi 		OV5640_30_FPS
626*8409d017SJacopo Mondi 	}, {
627*8409d017SJacopo Mondi 		/* 640x480 */
628*8409d017SJacopo Mondi 		OV5640_MODE_VGA_640_480, SUBSAMPLING,
62922845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_48M,
630476dec01SMaxime Ripard 		640, 1896, 480, 1080,
631086c25f8SMaxime Ripard 		ov5640_setting_VGA_640_480,
6325554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_VGA_640_480),
633*8409d017SJacopo Mondi 		OV5640_60_FPS
634*8409d017SJacopo Mondi 	}, {
635*8409d017SJacopo Mondi 		/* 720x480 */
636*8409d017SJacopo Mondi 		OV5640_MODE_NTSC_720_480, SUBSAMPLING,
63722845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_96M,
638476dec01SMaxime Ripard 		720, 1896, 480, 984,
639086c25f8SMaxime Ripard 		ov5640_setting_NTSC_720_480,
6405554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_NTSC_720_480),
641*8409d017SJacopo Mondi 		OV5640_30_FPS
642*8409d017SJacopo Mondi 	}, {
643*8409d017SJacopo Mondi 		/* 720x576 */
644*8409d017SJacopo Mondi 		OV5640_MODE_PAL_720_576, SUBSAMPLING,
64522845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_96M,
646476dec01SMaxime Ripard 		720, 1896, 576, 984,
647086c25f8SMaxime Ripard 		ov5640_setting_PAL_720_576,
6485554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_PAL_720_576),
649*8409d017SJacopo Mondi 		OV5640_30_FPS
650*8409d017SJacopo Mondi 	}, {
651*8409d017SJacopo Mondi 		/* 1024x768 */
652*8409d017SJacopo Mondi 		OV5640_MODE_XGA_1024_768, SUBSAMPLING,
65322845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_96M,
654476dec01SMaxime Ripard 		1024, 1896, 768, 1080,
655086c25f8SMaxime Ripard 		ov5640_setting_XGA_1024_768,
6565554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_XGA_1024_768),
657*8409d017SJacopo Mondi 		OV5640_30_FPS
658*8409d017SJacopo Mondi 	}, {
659*8409d017SJacopo Mondi 		/* 1280x720 */
660*8409d017SJacopo Mondi 		OV5640_MODE_720P_1280_720, SUBSAMPLING,
66122845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_124M,
662476dec01SMaxime Ripard 		1280, 1892, 720, 740,
663086c25f8SMaxime Ripard 		ov5640_setting_720P_1280_720,
6645554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_720P_1280_720),
665*8409d017SJacopo Mondi 		OV5640_30_FPS
666*8409d017SJacopo Mondi 	}, {
667*8409d017SJacopo Mondi 		/* 1920x1080 */
668*8409d017SJacopo Mondi 		OV5640_MODE_1080P_1920_1080, SCALING,
66922845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_148M,
670476dec01SMaxime Ripard 		1920, 2500, 1080, 1120,
671086c25f8SMaxime Ripard 		ov5640_setting_1080P_1920_1080,
6725554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
673*8409d017SJacopo Mondi 		OV5640_30_FPS
674*8409d017SJacopo Mondi 	}, {
675*8409d017SJacopo Mondi 		/* 2592x1944 */
676*8409d017SJacopo Mondi 		OV5640_MODE_QSXGA_2592_1944, SCALING,
67722845bf2SJacopo Mondi 		OV5640_PIXEL_RATE_168M,
678476dec01SMaxime Ripard 		2592, 2844, 1944, 1968,
679086c25f8SMaxime Ripard 		ov5640_setting_QSXGA_2592_1944,
6805554c80eSAdam Ford 		ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
681*8409d017SJacopo Mondi 		OV5640_15_FPS
682*8409d017SJacopo Mondi 	},
68319a81c14SSteve Longerbeam };
68419a81c14SSteve Longerbeam 
68519a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor)
68619a81c14SSteve Longerbeam {
68719a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
68819a81c14SSteve Longerbeam 	struct i2c_msg msg;
68919a81c14SSteve Longerbeam 	u8 buf[3];
69019a81c14SSteve Longerbeam 	int ret;
69119a81c14SSteve Longerbeam 
69219a81c14SSteve Longerbeam 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
69319a81c14SSteve Longerbeam 		return 0;
69419a81c14SSteve Longerbeam 
69519a81c14SSteve Longerbeam 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
69619a81c14SSteve Longerbeam 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
69719a81c14SSteve Longerbeam 	buf[2] = client->addr << 1;
69819a81c14SSteve Longerbeam 
69919a81c14SSteve Longerbeam 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
70019a81c14SSteve Longerbeam 	msg.flags = 0;
70119a81c14SSteve Longerbeam 	msg.buf = buf;
70219a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
70319a81c14SSteve Longerbeam 
70419a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
70519a81c14SSteve Longerbeam 	if (ret < 0) {
70619a81c14SSteve Longerbeam 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
70719a81c14SSteve Longerbeam 		return ret;
70819a81c14SSteve Longerbeam 	}
70919a81c14SSteve Longerbeam 
71019a81c14SSteve Longerbeam 	return 0;
71119a81c14SSteve Longerbeam }
71219a81c14SSteve Longerbeam 
71319a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
71419a81c14SSteve Longerbeam {
71519a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
71619a81c14SSteve Longerbeam 	struct i2c_msg msg;
71719a81c14SSteve Longerbeam 	u8 buf[3];
71819a81c14SSteve Longerbeam 	int ret;
71919a81c14SSteve Longerbeam 
72019a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
72119a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
72219a81c14SSteve Longerbeam 	buf[2] = val;
72319a81c14SSteve Longerbeam 
72419a81c14SSteve Longerbeam 	msg.addr = client->addr;
72519a81c14SSteve Longerbeam 	msg.flags = client->flags;
72619a81c14SSteve Longerbeam 	msg.buf = buf;
72719a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
72819a81c14SSteve Longerbeam 
72919a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
73019a81c14SSteve Longerbeam 	if (ret < 0) {
7313924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
73219a81c14SSteve Longerbeam 			__func__, reg, val);
73319a81c14SSteve Longerbeam 		return ret;
73419a81c14SSteve Longerbeam 	}
73519a81c14SSteve Longerbeam 
73619a81c14SSteve Longerbeam 	return 0;
73719a81c14SSteve Longerbeam }
73819a81c14SSteve Longerbeam 
73919a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
74019a81c14SSteve Longerbeam {
74119a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
74219a81c14SSteve Longerbeam 	struct i2c_msg msg[2];
74319a81c14SSteve Longerbeam 	u8 buf[2];
74419a81c14SSteve Longerbeam 	int ret;
74519a81c14SSteve Longerbeam 
74619a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
74719a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
74819a81c14SSteve Longerbeam 
74919a81c14SSteve Longerbeam 	msg[0].addr = client->addr;
75019a81c14SSteve Longerbeam 	msg[0].flags = client->flags;
75119a81c14SSteve Longerbeam 	msg[0].buf = buf;
75219a81c14SSteve Longerbeam 	msg[0].len = sizeof(buf);
75319a81c14SSteve Longerbeam 
75419a81c14SSteve Longerbeam 	msg[1].addr = client->addr;
75519a81c14SSteve Longerbeam 	msg[1].flags = client->flags | I2C_M_RD;
75619a81c14SSteve Longerbeam 	msg[1].buf = buf;
75719a81c14SSteve Longerbeam 	msg[1].len = 1;
75819a81c14SSteve Longerbeam 
75919a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, msg, 2);
7603924c623SHugues Fruchet 	if (ret < 0) {
7613924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x\n",
7623924c623SHugues Fruchet 			__func__, reg);
76319a81c14SSteve Longerbeam 		return ret;
7643924c623SHugues Fruchet 	}
76519a81c14SSteve Longerbeam 
76619a81c14SSteve Longerbeam 	*val = buf[0];
76719a81c14SSteve Longerbeam 	return 0;
76819a81c14SSteve Longerbeam }
76919a81c14SSteve Longerbeam 
77019a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
77119a81c14SSteve Longerbeam {
77219a81c14SSteve Longerbeam 	u8 hi, lo;
77319a81c14SSteve Longerbeam 	int ret;
77419a81c14SSteve Longerbeam 
77519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &hi);
77619a81c14SSteve Longerbeam 	if (ret)
77719a81c14SSteve Longerbeam 		return ret;
77819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
77919a81c14SSteve Longerbeam 	if (ret)
78019a81c14SSteve Longerbeam 		return ret;
78119a81c14SSteve Longerbeam 
78219a81c14SSteve Longerbeam 	*val = ((u16)hi << 8) | (u16)lo;
78319a81c14SSteve Longerbeam 	return 0;
78419a81c14SSteve Longerbeam }
78519a81c14SSteve Longerbeam 
78619a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
78719a81c14SSteve Longerbeam {
78819a81c14SSteve Longerbeam 	int ret;
78919a81c14SSteve Longerbeam 
79019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, reg, val >> 8);
79119a81c14SSteve Longerbeam 	if (ret)
79219a81c14SSteve Longerbeam 		return ret;
79319a81c14SSteve Longerbeam 
79419a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
79519a81c14SSteve Longerbeam }
79619a81c14SSteve Longerbeam 
79719a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
79819a81c14SSteve Longerbeam 			  u8 mask, u8 val)
79919a81c14SSteve Longerbeam {
80019a81c14SSteve Longerbeam 	u8 readval;
80119a81c14SSteve Longerbeam 	int ret;
80219a81c14SSteve Longerbeam 
80319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &readval);
80419a81c14SSteve Longerbeam 	if (ret)
80519a81c14SSteve Longerbeam 		return ret;
80619a81c14SSteve Longerbeam 
80719a81c14SSteve Longerbeam 	readval &= ~mask;
80819a81c14SSteve Longerbeam 	val &= mask;
80919a81c14SSteve Longerbeam 	val |= readval;
81019a81c14SSteve Longerbeam 
81119a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg, val);
81219a81c14SSteve Longerbeam }
81319a81c14SSteve Longerbeam 
814aa288248SMaxime Ripard /*
815aa288248SMaxime Ripard  * After trying the various combinations, reading various
816f8a7647dSMauro Carvalho Chehab  * documentations spread around the net, and from the various
817aa288248SMaxime Ripard  * feedback, the clock tree is probably as follows:
818aa288248SMaxime Ripard  *
819aa288248SMaxime Ripard  *   +--------------+
820aa288248SMaxime Ripard  *   |  Ext. Clock  |
821aa288248SMaxime Ripard  *   +-+------------+
822aa288248SMaxime Ripard  *     |  +----------+
823aa288248SMaxime Ripard  *     +->|   PLL1   | - reg 0x3036, for the multiplier
824aa288248SMaxime Ripard  *        +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
825aa288248SMaxime Ripard  *          |  +--------------+
826aa288248SMaxime Ripard  *          +->| System Clock |  - reg 0x3035, bits 4-7
827aa288248SMaxime Ripard  *             +-+------------+
828aa288248SMaxime Ripard  *               |  +--------------+
829aa288248SMaxime Ripard  *               +->| MIPI Divider | - reg 0x3035, bits 0-3
830aa288248SMaxime Ripard  *               |  +-+------------+
831aa288248SMaxime Ripard  *               |    +----------------> MIPI SCLK
832aa288248SMaxime Ripard  *               |    +  +-----+
833aa288248SMaxime Ripard  *               |    +->| / 2 |-------> MIPI BIT CLK
834aa288248SMaxime Ripard  *               |       +-----+
835aa288248SMaxime Ripard  *               |  +--------------+
836aa288248SMaxime Ripard  *               +->| PLL Root Div | - reg 0x3037, bit 4
837aa288248SMaxime Ripard  *                  +-+------------+
838aa288248SMaxime Ripard  *                    |  +---------+
8394c85f628SPaul Kocialkowski  *                    +->| Bit Div | - reg 0x3034, bits 0-3
840aa288248SMaxime Ripard  *                       +-+-------+
841aa288248SMaxime Ripard  *                         |  +-------------+
842aa288248SMaxime Ripard  *                         +->| SCLK Div    | - reg 0x3108, bits 0-1
843aa288248SMaxime Ripard  *                         |  +-+-----------+
844aa288248SMaxime Ripard  *                         |    +---------------> SCLK
845aa288248SMaxime Ripard  *                         |  +-------------+
846aa288248SMaxime Ripard  *                         +->| SCLK 2X Div | - reg 0x3108, bits 2-3
847aa288248SMaxime Ripard  *                         |  +-+-----------+
848aa288248SMaxime Ripard  *                         |    +---------------> SCLK 2X
849aa288248SMaxime Ripard  *                         |  +-------------+
850aa288248SMaxime Ripard  *                         +->| PCLK Div    | - reg 0x3108, bits 4-5
851aa288248SMaxime Ripard  *                            ++------------+
852aa288248SMaxime Ripard  *                             +  +-----------+
853aa288248SMaxime Ripard  *                             +->|   P_DIV   | - reg 0x3035, bits 0-3
854aa288248SMaxime Ripard  *                                +-----+-----+
855aa288248SMaxime Ripard  *                                       +------------> PCLK
856aa288248SMaxime Ripard  *
857aa288248SMaxime Ripard  * This is deviating from the datasheet at least for the register
858aa288248SMaxime Ripard  * 0x3108, since it's said here that the PCLK would be clocked from
859aa288248SMaxime Ripard  * the PLL.
860aa288248SMaxime Ripard  *
861aa288248SMaxime Ripard  * There seems to be also (unverified) constraints:
862aa288248SMaxime Ripard  *  - the PLL pre-divider output rate should be in the 4-27MHz range
863aa288248SMaxime Ripard  *  - the PLL multiplier output rate should be in the 500-1000MHz range
864aa288248SMaxime Ripard  *  - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
865aa288248SMaxime Ripard  *
866aa288248SMaxime Ripard  * In the two latter cases, these constraints are met since our
867aa288248SMaxime Ripard  * factors are hardcoded. If we were to change that, we would need to
868aa288248SMaxime Ripard  * take this into account. The only varying parts are the PLL
869aa288248SMaxime Ripard  * multiplier and the system clock divider, which are shared between
870aa288248SMaxime Ripard  * all these clocks so won't cause any issue.
871aa288248SMaxime Ripard  */
872aa288248SMaxime Ripard 
873aa288248SMaxime Ripard /*
874aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
875aa288248SMaxime Ripard  * set to 3 in the vendor kernels.
876aa288248SMaxime Ripard  */
877aa288248SMaxime Ripard #define OV5640_PLL_PREDIV	3
878aa288248SMaxime Ripard 
879aa288248SMaxime Ripard #define OV5640_PLL_MULT_MIN	4
880aa288248SMaxime Ripard #define OV5640_PLL_MULT_MAX	252
881aa288248SMaxime Ripard 
882aa288248SMaxime Ripard /*
883aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 16, but the value is
884aa288248SMaxime Ripard  * always set to either 1 or 2 in the vendor kernels.
885aa288248SMaxime Ripard  */
886aa288248SMaxime Ripard #define OV5640_SYSDIV_MIN	1
887aa288248SMaxime Ripard #define OV5640_SYSDIV_MAX	16
888aa288248SMaxime Ripard 
889aa288248SMaxime Ripard /*
890aa288248SMaxime Ripard  * Hardcode these values for scaler and non-scaler modes.
891aa288248SMaxime Ripard  * FIXME: to be re-calcualted for 1 data lanes setups
892aa288248SMaxime Ripard  */
893aa288248SMaxime Ripard #define OV5640_MIPI_DIV_PCLK	2
894aa288248SMaxime Ripard #define OV5640_MIPI_DIV_SCLK	1
895aa288248SMaxime Ripard 
896aa288248SMaxime Ripard /*
897aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 2, but the value is always
898aa288248SMaxime Ripard  * set to 2 in the vendor kernels.
899aa288248SMaxime Ripard  */
900aa288248SMaxime Ripard #define OV5640_PLL_ROOT_DIV			2
901aa288248SMaxime Ripard #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2		BIT(4)
902aa288248SMaxime Ripard 
903aa288248SMaxime Ripard /*
904aa288248SMaxime Ripard  * We only supports 8-bit formats at the moment
905aa288248SMaxime Ripard  */
906aa288248SMaxime Ripard #define OV5640_BIT_DIV				2
907aa288248SMaxime Ripard #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT		0x08
908aa288248SMaxime Ripard 
909aa288248SMaxime Ripard /*
910aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
911aa288248SMaxime Ripard  * set to 2 in the vendor kernels.
912aa288248SMaxime Ripard  */
913aa288248SMaxime Ripard #define OV5640_SCLK_ROOT_DIV	2
914aa288248SMaxime Ripard 
915aa288248SMaxime Ripard /*
916aa288248SMaxime Ripard  * This is hardcoded so that the consistency is maintained between SCLK and
917aa288248SMaxime Ripard  * SCLK 2x.
918aa288248SMaxime Ripard  */
919aa288248SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
920aa288248SMaxime Ripard 
921aa288248SMaxime Ripard /*
922aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
923aa288248SMaxime Ripard  * set to 1 in the vendor kernels.
924aa288248SMaxime Ripard  */
925aa288248SMaxime Ripard #define OV5640_PCLK_ROOT_DIV			1
926aa288248SMaxime Ripard #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS	0x00
927aa288248SMaxime Ripard 
928aa288248SMaxime Ripard static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
929aa288248SMaxime Ripard 					    u8 pll_prediv, u8 pll_mult,
930aa288248SMaxime Ripard 					    u8 sysdiv)
931aa288248SMaxime Ripard {
932aa288248SMaxime Ripard 	unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
933aa288248SMaxime Ripard 
934aa288248SMaxime Ripard 	/* PLL1 output cannot exceed 1GHz. */
935aa288248SMaxime Ripard 	if (sysclk / 1000000 > 1000)
936aa288248SMaxime Ripard 		return 0;
937aa288248SMaxime Ripard 
938aa288248SMaxime Ripard 	return sysclk / sysdiv;
939aa288248SMaxime Ripard }
940aa288248SMaxime Ripard 
941aa288248SMaxime Ripard static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
942aa288248SMaxime Ripard 					 unsigned long rate,
943aa288248SMaxime Ripard 					 u8 *pll_prediv, u8 *pll_mult,
944aa288248SMaxime Ripard 					 u8 *sysdiv)
945aa288248SMaxime Ripard {
946aa288248SMaxime Ripard 	unsigned long best = ~0;
947aa288248SMaxime Ripard 	u8 best_sysdiv = 1, best_mult = 1;
948aa288248SMaxime Ripard 	u8 _sysdiv, _pll_mult;
949aa288248SMaxime Ripard 
950aa288248SMaxime Ripard 	for (_sysdiv = OV5640_SYSDIV_MIN;
951aa288248SMaxime Ripard 	     _sysdiv <= OV5640_SYSDIV_MAX;
952aa288248SMaxime Ripard 	     _sysdiv++) {
953aa288248SMaxime Ripard 		for (_pll_mult = OV5640_PLL_MULT_MIN;
954aa288248SMaxime Ripard 		     _pll_mult <= OV5640_PLL_MULT_MAX;
955aa288248SMaxime Ripard 		     _pll_mult++) {
956aa288248SMaxime Ripard 			unsigned long _rate;
957aa288248SMaxime Ripard 
958aa288248SMaxime Ripard 			/*
959aa288248SMaxime Ripard 			 * The PLL multiplier cannot be odd if above
960aa288248SMaxime Ripard 			 * 127.
961aa288248SMaxime Ripard 			 */
962aa288248SMaxime Ripard 			if (_pll_mult > 127 && (_pll_mult % 2))
963aa288248SMaxime Ripard 				continue;
964aa288248SMaxime Ripard 
965aa288248SMaxime Ripard 			_rate = ov5640_compute_sys_clk(sensor,
966aa288248SMaxime Ripard 						       OV5640_PLL_PREDIV,
967aa288248SMaxime Ripard 						       _pll_mult, _sysdiv);
968aa288248SMaxime Ripard 
969aa288248SMaxime Ripard 			/*
970aa288248SMaxime Ripard 			 * We have reached the maximum allowed PLL1 output,
971aa288248SMaxime Ripard 			 * increase sysdiv.
972aa288248SMaxime Ripard 			 */
9732e3df204SAdam Ford 			if (!_rate)
974aa288248SMaxime Ripard 				break;
975aa288248SMaxime Ripard 
976aa288248SMaxime Ripard 			/*
977aa288248SMaxime Ripard 			 * Prefer rates above the expected clock rate than
978aa288248SMaxime Ripard 			 * below, even if that means being less precise.
979aa288248SMaxime Ripard 			 */
980aa288248SMaxime Ripard 			if (_rate < rate)
981aa288248SMaxime Ripard 				continue;
982aa288248SMaxime Ripard 
983aa288248SMaxime Ripard 			if (abs(rate - _rate) < abs(rate - best)) {
984aa288248SMaxime Ripard 				best = _rate;
985aa288248SMaxime Ripard 				best_sysdiv = _sysdiv;
986aa288248SMaxime Ripard 				best_mult = _pll_mult;
987aa288248SMaxime Ripard 			}
988aa288248SMaxime Ripard 
989aa288248SMaxime Ripard 			if (_rate == rate)
990aa288248SMaxime Ripard 				goto out;
991aa288248SMaxime Ripard 		}
992aa288248SMaxime Ripard 	}
993aa288248SMaxime Ripard 
994aa288248SMaxime Ripard out:
995aa288248SMaxime Ripard 	*sysdiv = best_sysdiv;
996aa288248SMaxime Ripard 	*pll_prediv = OV5640_PLL_PREDIV;
997aa288248SMaxime Ripard 	*pll_mult = best_mult;
998aa288248SMaxime Ripard 
999aa288248SMaxime Ripard 	return best;
1000aa288248SMaxime Ripard }
1001aa288248SMaxime Ripard 
1002aa288248SMaxime Ripard /*
1003aa288248SMaxime Ripard  * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1004aa288248SMaxime Ripard  *			    for the MIPI CSI-2 output.
1005aa288248SMaxime Ripard  *
1006aa288248SMaxime Ripard  * @rate: The requested bandwidth per lane in bytes per second.
1007aa288248SMaxime Ripard  *	  'Bandwidth Per Lane' is calculated as:
1008aa288248SMaxime Ripard  *	  bpl = HTOT * VTOT * FPS * bpp / num_lanes;
1009aa288248SMaxime Ripard  *
1010aa288248SMaxime Ripard  * This function use the requested bandwidth to calculate:
1011aa288248SMaxime Ripard  * - sample_rate = bpl / (bpp / num_lanes);
1012aa288248SMaxime Ripard  *	         = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes);
1013aa288248SMaxime Ripard  *
1014aa288248SMaxime Ripard  * - mipi_sclk   = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
1015aa288248SMaxime Ripard  *
1016aa288248SMaxime Ripard  * with these fixed parameters:
1017aa288248SMaxime Ripard  *	PLL_RDIV	= 2;
1018aa288248SMaxime Ripard  *	BIT_DIVIDER	= 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5);
1019aa288248SMaxime Ripard  *	PCLK_DIV	= 1;
1020aa288248SMaxime Ripard  *
1021aa288248SMaxime Ripard  * The MIPI clock generation differs for modes that use the scaler and modes
1022aa288248SMaxime Ripard  * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI
1023aa288248SMaxime Ripard  * BIT CLk, and thus:
1024aa288248SMaxime Ripard  *
1025aa288248SMaxime Ripard  * - mipi_sclk = bpl / MIPI_DIV / 2;
1026aa288248SMaxime Ripard  *   MIPI_DIV = 1;
1027aa288248SMaxime Ripard  *
1028aa288248SMaxime Ripard  * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated
1029aa288248SMaxime Ripard  * from the pixel clock, and thus:
1030aa288248SMaxime Ripard  *
1031aa288248SMaxime Ripard  * - sample_rate = bpl / (bpp / num_lanes);
1032aa288248SMaxime Ripard  *	         = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes);
1033aa288248SMaxime Ripard  *		 = bpl / (4 * MIPI_DIV / num_lanes);
1034aa288248SMaxime Ripard  * - MIPI_DIV	 = bpp / (4 * num_lanes);
1035aa288248SMaxime Ripard  *
1036aa288248SMaxime Ripard  * FIXME: this have been tested with 16bpp and 2 lanes setup only.
1037aa288248SMaxime Ripard  * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
1038aa288248SMaxime Ripard  * above formula for setups with 1 lane or image formats with different bpp.
1039aa288248SMaxime Ripard  *
1040aa288248SMaxime Ripard  * FIXME: this deviates from the sensor manual documentation which is quite
1041aa288248SMaxime Ripard  * thin on the MIPI clock tree generation part.
1042aa288248SMaxime Ripard  */
1043aa288248SMaxime Ripard static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
1044aa288248SMaxime Ripard 				unsigned long rate)
1045aa288248SMaxime Ripard {
1046aa288248SMaxime Ripard 	const struct ov5640_mode_info *mode = sensor->current_mode;
1047aa288248SMaxime Ripard 	u8 prediv, mult, sysdiv;
1048aa288248SMaxime Ripard 	u8 mipi_div;
1049aa288248SMaxime Ripard 	int ret;
1050aa288248SMaxime Ripard 
1051aa288248SMaxime Ripard 	/*
1052aa288248SMaxime Ripard 	 * 1280x720 is reported to use 'SUBSAMPLING' only,
1053aa288248SMaxime Ripard 	 * but according to the sensor manual it goes through the
1054aa288248SMaxime Ripard 	 * scaler before subsampling.
1055aa288248SMaxime Ripard 	 */
1056aa288248SMaxime Ripard 	if (mode->dn_mode == SCALING ||
1057aa288248SMaxime Ripard 	   (mode->id == OV5640_MODE_720P_1280_720))
1058aa288248SMaxime Ripard 		mipi_div = OV5640_MIPI_DIV_SCLK;
1059aa288248SMaxime Ripard 	else
1060aa288248SMaxime Ripard 		mipi_div = OV5640_MIPI_DIV_PCLK;
1061aa288248SMaxime Ripard 
1062aa288248SMaxime Ripard 	ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
1063aa288248SMaxime Ripard 
1064aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1065aa288248SMaxime Ripard 			     0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
1066aa288248SMaxime Ripard 
1067aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1068aa288248SMaxime Ripard 			     0xff, sysdiv << 4 | mipi_div);
1069aa288248SMaxime Ripard 	if (ret)
1070aa288248SMaxime Ripard 		return ret;
1071aa288248SMaxime Ripard 
1072aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1073aa288248SMaxime Ripard 	if (ret)
1074aa288248SMaxime Ripard 		return ret;
1075aa288248SMaxime Ripard 
1076aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1077aa288248SMaxime Ripard 			     0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
1078aa288248SMaxime Ripard 	if (ret)
1079aa288248SMaxime Ripard 		return ret;
1080aa288248SMaxime Ripard 
1081aa288248SMaxime Ripard 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
1082aa288248SMaxime Ripard 			      0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
1083aa288248SMaxime Ripard }
1084aa288248SMaxime Ripard 
1085aa288248SMaxime Ripard static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1086aa288248SMaxime Ripard 				      unsigned long rate,
1087aa288248SMaxime Ripard 				      u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1088aa288248SMaxime Ripard 				      u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1089aa288248SMaxime Ripard {
1090aa288248SMaxime Ripard 	unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1091aa288248SMaxime Ripard 				OV5640_PCLK_ROOT_DIV;
1092aa288248SMaxime Ripard 
1093aa288248SMaxime Ripard 	_rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1094aa288248SMaxime Ripard 				    sysdiv);
1095aa288248SMaxime Ripard 	*pll_rdiv = OV5640_PLL_ROOT_DIV;
1096aa288248SMaxime Ripard 	*bit_div = OV5640_BIT_DIV;
1097aa288248SMaxime Ripard 	*pclk_div = OV5640_PCLK_ROOT_DIV;
1098aa288248SMaxime Ripard 
1099aa288248SMaxime Ripard 	return _rate / *pll_rdiv / *bit_div / *pclk_div;
1100aa288248SMaxime Ripard }
1101aa288248SMaxime Ripard 
1102aa288248SMaxime Ripard static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1103aa288248SMaxime Ripard {
1104aa288248SMaxime Ripard 	u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1105aa288248SMaxime Ripard 	int ret;
1106aa288248SMaxime Ripard 
1107aa288248SMaxime Ripard 	ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1108aa288248SMaxime Ripard 			 &bit_div, &pclk_div);
1109aa288248SMaxime Ripard 
1110aa288248SMaxime Ripard 	if (bit_div == 2)
1111aa288248SMaxime Ripard 		bit_div = 8;
1112aa288248SMaxime Ripard 
1113aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1114aa288248SMaxime Ripard 			     0x0f, bit_div);
1115aa288248SMaxime Ripard 	if (ret)
1116aa288248SMaxime Ripard 		return ret;
1117aa288248SMaxime Ripard 
1118aa288248SMaxime Ripard 	/*
1119aa288248SMaxime Ripard 	 * We need to set sysdiv according to the clock, and to clear
1120aa288248SMaxime Ripard 	 * the MIPI divider.
1121aa288248SMaxime Ripard 	 */
1122aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1123aa288248SMaxime Ripard 			     0xff, sysdiv << 4);
1124aa288248SMaxime Ripard 	if (ret)
1125aa288248SMaxime Ripard 		return ret;
1126aa288248SMaxime Ripard 
1127aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1128aa288248SMaxime Ripard 			     0xff, mult);
1129aa288248SMaxime Ripard 	if (ret)
1130aa288248SMaxime Ripard 		return ret;
1131aa288248SMaxime Ripard 
1132aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1133aa288248SMaxime Ripard 			     0x1f, prediv | ((pll_rdiv - 1) << 4));
1134aa288248SMaxime Ripard 	if (ret)
1135aa288248SMaxime Ripard 		return ret;
1136aa288248SMaxime Ripard 
1137aa288248SMaxime Ripard 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1138aa288248SMaxime Ripard 			      (ilog2(pclk_div) << 4));
1139aa288248SMaxime Ripard }
1140aa288248SMaxime Ripard 
11417cb013b1SChen-Yu Tsai /* set JPEG framing sizes */
11427cb013b1SChen-Yu Tsai static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
11437cb013b1SChen-Yu Tsai 				   const struct ov5640_mode_info *mode)
11447cb013b1SChen-Yu Tsai {
11457cb013b1SChen-Yu Tsai 	int ret;
11467cb013b1SChen-Yu Tsai 
11472b5c18f9SChen-Yu Tsai 	/*
11482b5c18f9SChen-Yu Tsai 	 * compression mode 3 timing
11492b5c18f9SChen-Yu Tsai 	 *
11502b5c18f9SChen-Yu Tsai 	 * Data is transmitted with programmable width (VFIFO_HSIZE).
11512b5c18f9SChen-Yu Tsai 	 * No padding done. Last line may have less data. Varying
11522b5c18f9SChen-Yu Tsai 	 * number of lines per frame, depending on amount of data.
11532b5c18f9SChen-Yu Tsai 	 */
11542b5c18f9SChen-Yu Tsai 	ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
11552b5c18f9SChen-Yu Tsai 	if (ret < 0)
11562b5c18f9SChen-Yu Tsai 		return ret;
11572b5c18f9SChen-Yu Tsai 
11587cb013b1SChen-Yu Tsai 	ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact);
11597cb013b1SChen-Yu Tsai 	if (ret < 0)
11607cb013b1SChen-Yu Tsai 		return ret;
11617cb013b1SChen-Yu Tsai 
11627cb013b1SChen-Yu Tsai 	return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact);
11637cb013b1SChen-Yu Tsai }
11647cb013b1SChen-Yu Tsai 
116519a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */
1166bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor,
1167bad1774eSJacopo Mondi 			      const struct ov5640_mode_info *mode)
1168bad1774eSJacopo Mondi {
1169bad1774eSJacopo Mondi 	int ret;
1170bad1774eSJacopo Mondi 
11717cb013b1SChen-Yu Tsai 	if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
11727cb013b1SChen-Yu Tsai 		ret = ov5640_set_jpeg_timings(sensor, mode);
11737cb013b1SChen-Yu Tsai 		if (ret < 0)
11747cb013b1SChen-Yu Tsai 			return ret;
11757cb013b1SChen-Yu Tsai 	}
11767cb013b1SChen-Yu Tsai 
1177bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1178bad1774eSJacopo Mondi 	if (ret < 0)
1179bad1774eSJacopo Mondi 		return ret;
1180bad1774eSJacopo Mondi 
1181bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1182bad1774eSJacopo Mondi 	if (ret < 0)
1183bad1774eSJacopo Mondi 		return ret;
1184bad1774eSJacopo Mondi 
1185bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1186bad1774eSJacopo Mondi 	if (ret < 0)
1187bad1774eSJacopo Mondi 		return ret;
1188bad1774eSJacopo Mondi 
1189bad1774eSJacopo Mondi 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1190bad1774eSJacopo Mondi }
1191bad1774eSJacopo Mondi 
119219a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor,
119319a81c14SSteve Longerbeam 			    const struct ov5640_mode_info *mode)
119419a81c14SSteve Longerbeam {
119519a81c14SSteve Longerbeam 	const struct reg_value *regs = mode->reg_data;
119619a81c14SSteve Longerbeam 	unsigned int i;
119719a81c14SSteve Longerbeam 	u32 delay_ms;
119819a81c14SSteve Longerbeam 	u16 reg_addr;
119919a81c14SSteve Longerbeam 	u8 mask, val;
120019a81c14SSteve Longerbeam 	int ret = 0;
120119a81c14SSteve Longerbeam 
120219a81c14SSteve Longerbeam 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
120319a81c14SSteve Longerbeam 		delay_ms = regs->delay_ms;
120419a81c14SSteve Longerbeam 		reg_addr = regs->reg_addr;
120519a81c14SSteve Longerbeam 		val = regs->val;
120619a81c14SSteve Longerbeam 		mask = regs->mask;
120719a81c14SSteve Longerbeam 
12083b987d70SLad Prabhakar 		/* remain in power down mode for DVP */
12093b987d70SLad Prabhakar 		if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
12103b987d70SLad Prabhakar 		    val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
12113b987d70SLad Prabhakar 		    sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
12123b987d70SLad Prabhakar 			continue;
12133b987d70SLad Prabhakar 
121419a81c14SSteve Longerbeam 		if (mask)
121519a81c14SSteve Longerbeam 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
121619a81c14SSteve Longerbeam 		else
121719a81c14SSteve Longerbeam 			ret = ov5640_write_reg(sensor, reg_addr, val);
121819a81c14SSteve Longerbeam 		if (ret)
121919a81c14SSteve Longerbeam 			break;
122019a81c14SSteve Longerbeam 
122119a81c14SSteve Longerbeam 		if (delay_ms)
122219a81c14SSteve Longerbeam 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
122319a81c14SSteve Longerbeam 	}
122419a81c14SSteve Longerbeam 
1225bad1774eSJacopo Mondi 	return ov5640_set_timings(sensor, mode);
122619a81c14SSteve Longerbeam }
122719a81c14SSteve Longerbeam 
1228dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1229dc29a1c1SHugues Fruchet {
1230dc29a1c1SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1231dc29a1c1SHugues Fruchet 			      BIT(0), on ? 0 : BIT(0));
1232dc29a1c1SHugues Fruchet }
1233dc29a1c1SHugues Fruchet 
123419a81c14SSteve Longerbeam /* read exposure, in number of line periods */
123519a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor)
123619a81c14SSteve Longerbeam {
123719a81c14SSteve Longerbeam 	int exp, ret;
123819a81c14SSteve Longerbeam 	u8 temp;
123919a81c14SSteve Longerbeam 
124019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
124119a81c14SSteve Longerbeam 	if (ret)
124219a81c14SSteve Longerbeam 		return ret;
124319a81c14SSteve Longerbeam 	exp = ((int)temp & 0x0f) << 16;
124419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
124519a81c14SSteve Longerbeam 	if (ret)
124619a81c14SSteve Longerbeam 		return ret;
124719a81c14SSteve Longerbeam 	exp |= ((int)temp << 8);
124819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
124919a81c14SSteve Longerbeam 	if (ret)
125019a81c14SSteve Longerbeam 		return ret;
125119a81c14SSteve Longerbeam 	exp |= (int)temp;
125219a81c14SSteve Longerbeam 
125319a81c14SSteve Longerbeam 	return exp >> 4;
125419a81c14SSteve Longerbeam }
125519a81c14SSteve Longerbeam 
125619a81c14SSteve Longerbeam /* write exposure, given number of line periods */
125719a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
125819a81c14SSteve Longerbeam {
125919a81c14SSteve Longerbeam 	int ret;
126019a81c14SSteve Longerbeam 
126119a81c14SSteve Longerbeam 	exposure <<= 4;
126219a81c14SSteve Longerbeam 
126319a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
126419a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
126519a81c14SSteve Longerbeam 			       exposure & 0xff);
126619a81c14SSteve Longerbeam 	if (ret)
126719a81c14SSteve Longerbeam 		return ret;
126819a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
126919a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
127019a81c14SSteve Longerbeam 			       (exposure >> 8) & 0xff);
127119a81c14SSteve Longerbeam 	if (ret)
127219a81c14SSteve Longerbeam 		return ret;
127319a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor,
127419a81c14SSteve Longerbeam 				OV5640_REG_AEC_PK_EXPOSURE_HI,
127519a81c14SSteve Longerbeam 				(exposure >> 16) & 0x0f);
127619a81c14SSteve Longerbeam }
127719a81c14SSteve Longerbeam 
127819a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor)
127919a81c14SSteve Longerbeam {
128019a81c14SSteve Longerbeam 	u16 gain;
128119a81c14SSteve Longerbeam 	int ret;
128219a81c14SSteve Longerbeam 
128319a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
128419a81c14SSteve Longerbeam 	if (ret)
128519a81c14SSteve Longerbeam 		return ret;
128619a81c14SSteve Longerbeam 
128719a81c14SSteve Longerbeam 	return gain & 0x3ff;
128819a81c14SSteve Longerbeam }
128919a81c14SSteve Longerbeam 
12903cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
12913cca8ef5SHugues Fruchet {
12923cca8ef5SHugues Fruchet 	return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
12933cca8ef5SHugues Fruchet 				  (u16)gain & 0x3ff);
12943cca8ef5SHugues Fruchet }
12953cca8ef5SHugues Fruchet 
12963cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
12973cca8ef5SHugues Fruchet {
12983cca8ef5SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
12993cca8ef5SHugues Fruchet 			      BIT(1), on ? 0 : BIT(1));
13003cca8ef5SHugues Fruchet }
13013cca8ef5SHugues Fruchet 
1302f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1303f22996dbSHugues Fruchet {
13043b987d70SLad Prabhakar 	return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
13053b987d70SLad Prabhakar 				OV5640_REG_SYS_CTRL0_SW_PWUP :
13063b987d70SLad Prabhakar 				OV5640_REG_SYS_CTRL0_SW_PWDN);
1307f22996dbSHugues Fruchet }
1308f22996dbSHugues Fruchet 
1309f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
131019a81c14SSteve Longerbeam {
131119a81c14SSteve Longerbeam 	int ret;
131219a81c14SSteve Longerbeam 
1313aa4bb8b8SJacopo Mondi 	/*
1314aa4bb8b8SJacopo Mondi 	 * Enable/disable the MIPI interface
1315aa4bb8b8SJacopo Mondi 	 *
1316aa4bb8b8SJacopo Mondi 	 * 0x300e = on ? 0x45 : 0x40
1317aa4bb8b8SJacopo Mondi 	 *
1318aa4bb8b8SJacopo Mondi 	 * FIXME: the sensor manual (version 2.03) reports
1319aa4bb8b8SJacopo Mondi 	 * [7:5] = 000  : 1 data lane mode
1320aa4bb8b8SJacopo Mondi 	 * [7:5] = 001  : 2 data lanes mode
1321aa4bb8b8SJacopo Mondi 	 * But this settings do not work, while the following ones
1322aa4bb8b8SJacopo Mondi 	 * have been validated for 2 data lanes mode.
1323aa4bb8b8SJacopo Mondi 	 *
1324aa4bb8b8SJacopo Mondi 	 * [7:5] = 010	: 2 data lanes mode
1325aa4bb8b8SJacopo Mondi 	 * [4] = 0	: Power up MIPI HS Tx
1326aa4bb8b8SJacopo Mondi 	 * [3] = 0	: Power up MIPI LS Rx
1327aa4bb8b8SJacopo Mondi 	 * [2] = 1/0	: MIPI interface enable/disable
1328aa4bb8b8SJacopo Mondi 	 * [1:0] = 01/00: FIXME: 'debug'
1329aa4bb8b8SJacopo Mondi 	 */
1330aa4bb8b8SJacopo Mondi 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1331aa4bb8b8SJacopo Mondi 			       on ? 0x45 : 0x40);
133219a81c14SSteve Longerbeam 	if (ret)
133319a81c14SSteve Longerbeam 		return ret;
133419a81c14SSteve Longerbeam 
133519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
133619a81c14SSteve Longerbeam 				on ? 0x00 : 0x0f);
133719a81c14SSteve Longerbeam }
133819a81c14SSteve Longerbeam 
133919a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor)
134019a81c14SSteve Longerbeam {
134119a81c14SSteve Longerbeam 	 /* calculate sysclk */
134219a81c14SSteve Longerbeam 	u32 xvclk = sensor->xclk_freq / 10000;
134319a81c14SSteve Longerbeam 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
134419a81c14SSteve Longerbeam 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
134519a81c14SSteve Longerbeam 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
134619a81c14SSteve Longerbeam 	u8 temp1, temp2;
134719a81c14SSteve Longerbeam 	int ret;
134819a81c14SSteve Longerbeam 
134919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
135019a81c14SSteve Longerbeam 	if (ret)
135119a81c14SSteve Longerbeam 		return ret;
135219a81c14SSteve Longerbeam 	temp2 = temp1 & 0x0f;
135319a81c14SSteve Longerbeam 	if (temp2 == 8 || temp2 == 10)
135419a81c14SSteve Longerbeam 		bit_div2x = temp2 / 2;
135519a81c14SSteve Longerbeam 
135619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
135719a81c14SSteve Longerbeam 	if (ret)
135819a81c14SSteve Longerbeam 		return ret;
135919a81c14SSteve Longerbeam 	sysdiv = temp1 >> 4;
136019a81c14SSteve Longerbeam 	if (sysdiv == 0)
136119a81c14SSteve Longerbeam 		sysdiv = 16;
136219a81c14SSteve Longerbeam 
136319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
136419a81c14SSteve Longerbeam 	if (ret)
136519a81c14SSteve Longerbeam 		return ret;
136619a81c14SSteve Longerbeam 	multiplier = temp1;
136719a81c14SSteve Longerbeam 
136819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
136919a81c14SSteve Longerbeam 	if (ret)
137019a81c14SSteve Longerbeam 		return ret;
137119a81c14SSteve Longerbeam 	prediv = temp1 & 0x0f;
137219a81c14SSteve Longerbeam 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
137319a81c14SSteve Longerbeam 
137419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
137519a81c14SSteve Longerbeam 	if (ret)
137619a81c14SSteve Longerbeam 		return ret;
137719a81c14SSteve Longerbeam 	temp2 = temp1 & 0x03;
137819a81c14SSteve Longerbeam 	sclk_rdiv = sclk_rdiv_map[temp2];
137919a81c14SSteve Longerbeam 
138019a81c14SSteve Longerbeam 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
138119a81c14SSteve Longerbeam 		return -EINVAL;
138219a81c14SSteve Longerbeam 
138319a81c14SSteve Longerbeam 	VCO = xvclk * multiplier / prediv;
138419a81c14SSteve Longerbeam 
138519a81c14SSteve Longerbeam 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
138619a81c14SSteve Longerbeam 
138719a81c14SSteve Longerbeam 	return sysclk;
138819a81c14SSteve Longerbeam }
138919a81c14SSteve Longerbeam 
139019a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor)
139119a81c14SSteve Longerbeam {
139219a81c14SSteve Longerbeam 	 /* read HTS from register settings */
139319a81c14SSteve Longerbeam 	u8 mode;
139419a81c14SSteve Longerbeam 	int ret;
139519a81c14SSteve Longerbeam 
139619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
139719a81c14SSteve Longerbeam 	if (ret)
139819a81c14SSteve Longerbeam 		return ret;
139919a81c14SSteve Longerbeam 	mode &= 0xfb;
140019a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
140119a81c14SSteve Longerbeam }
140219a81c14SSteve Longerbeam 
140319a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor)
140419a81c14SSteve Longerbeam {
140519a81c14SSteve Longerbeam 	/* read HTS from register settings */
140619a81c14SSteve Longerbeam 	u16 hts;
140719a81c14SSteve Longerbeam 	int ret;
140819a81c14SSteve Longerbeam 
140919a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
141019a81c14SSteve Longerbeam 	if (ret)
141119a81c14SSteve Longerbeam 		return ret;
141219a81c14SSteve Longerbeam 	return hts;
141319a81c14SSteve Longerbeam }
141419a81c14SSteve Longerbeam 
141519a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor)
141619a81c14SSteve Longerbeam {
141719a81c14SSteve Longerbeam 	u16 vts;
141819a81c14SSteve Longerbeam 	int ret;
141919a81c14SSteve Longerbeam 
142019a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
142119a81c14SSteve Longerbeam 	if (ret)
142219a81c14SSteve Longerbeam 		return ret;
142319a81c14SSteve Longerbeam 	return vts;
142419a81c14SSteve Longerbeam }
142519a81c14SSteve Longerbeam 
142619a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
142719a81c14SSteve Longerbeam {
142819a81c14SSteve Longerbeam 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
142919a81c14SSteve Longerbeam }
143019a81c14SSteve Longerbeam 
143119a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor)
143219a81c14SSteve Longerbeam {
143319a81c14SSteve Longerbeam 	/* get banding filter value */
143419a81c14SSteve Longerbeam 	int ret, light_freq = 0;
143519a81c14SSteve Longerbeam 	u8 temp, temp1;
143619a81c14SSteve Longerbeam 
143719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
143819a81c14SSteve Longerbeam 	if (ret)
143919a81c14SSteve Longerbeam 		return ret;
144019a81c14SSteve Longerbeam 
144119a81c14SSteve Longerbeam 	if (temp & 0x80) {
144219a81c14SSteve Longerbeam 		/* manual */
144319a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
144419a81c14SSteve Longerbeam 				      &temp1);
144519a81c14SSteve Longerbeam 		if (ret)
144619a81c14SSteve Longerbeam 			return ret;
144719a81c14SSteve Longerbeam 		if (temp1 & 0x04) {
144819a81c14SSteve Longerbeam 			/* 50Hz */
144919a81c14SSteve Longerbeam 			light_freq = 50;
145019a81c14SSteve Longerbeam 		} else {
145119a81c14SSteve Longerbeam 			/* 60Hz */
145219a81c14SSteve Longerbeam 			light_freq = 60;
145319a81c14SSteve Longerbeam 		}
145419a81c14SSteve Longerbeam 	} else {
145519a81c14SSteve Longerbeam 		/* auto */
145619a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
145719a81c14SSteve Longerbeam 				      &temp1);
145819a81c14SSteve Longerbeam 		if (ret)
145919a81c14SSteve Longerbeam 			return ret;
146019a81c14SSteve Longerbeam 
146119a81c14SSteve Longerbeam 		if (temp1 & 0x01) {
146219a81c14SSteve Longerbeam 			/* 50Hz */
146319a81c14SSteve Longerbeam 			light_freq = 50;
146419a81c14SSteve Longerbeam 		} else {
146519a81c14SSteve Longerbeam 			/* 60Hz */
146619a81c14SSteve Longerbeam 		}
146719a81c14SSteve Longerbeam 	}
146819a81c14SSteve Longerbeam 
146919a81c14SSteve Longerbeam 	return light_freq;
147019a81c14SSteve Longerbeam }
147119a81c14SSteve Longerbeam 
147219a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
147319a81c14SSteve Longerbeam {
147419a81c14SSteve Longerbeam 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
147519a81c14SSteve Longerbeam 	int ret;
147619a81c14SSteve Longerbeam 
147719a81c14SSteve Longerbeam 	/* read preview PCLK */
147819a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
147919a81c14SSteve Longerbeam 	if (ret < 0)
148019a81c14SSteve Longerbeam 		return ret;
148119a81c14SSteve Longerbeam 	if (ret == 0)
148219a81c14SSteve Longerbeam 		return -EINVAL;
148319a81c14SSteve Longerbeam 	sensor->prev_sysclk = ret;
148419a81c14SSteve Longerbeam 	/* read preview HTS */
148519a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
148619a81c14SSteve Longerbeam 	if (ret < 0)
148719a81c14SSteve Longerbeam 		return ret;
148819a81c14SSteve Longerbeam 	if (ret == 0)
148919a81c14SSteve Longerbeam 		return -EINVAL;
149019a81c14SSteve Longerbeam 	sensor->prev_hts = ret;
149119a81c14SSteve Longerbeam 
149219a81c14SSteve Longerbeam 	/* read preview VTS */
149319a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
149419a81c14SSteve Longerbeam 	if (ret < 0)
149519a81c14SSteve Longerbeam 		return ret;
149619a81c14SSteve Longerbeam 	prev_vts = ret;
149719a81c14SSteve Longerbeam 
149819a81c14SSteve Longerbeam 	/* calculate banding filter */
149919a81c14SSteve Longerbeam 	/* 60Hz */
150019a81c14SSteve Longerbeam 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
150119a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
150219a81c14SSteve Longerbeam 	if (ret)
150319a81c14SSteve Longerbeam 		return ret;
150419a81c14SSteve Longerbeam 	if (!band_step60)
150519a81c14SSteve Longerbeam 		return -EINVAL;
150619a81c14SSteve Longerbeam 	max_band60 = (int)((prev_vts - 4) / band_step60);
150719a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
150819a81c14SSteve Longerbeam 	if (ret)
150919a81c14SSteve Longerbeam 		return ret;
151019a81c14SSteve Longerbeam 
151119a81c14SSteve Longerbeam 	/* 50Hz */
151219a81c14SSteve Longerbeam 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
151319a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
151419a81c14SSteve Longerbeam 	if (ret)
151519a81c14SSteve Longerbeam 		return ret;
151619a81c14SSteve Longerbeam 	if (!band_step50)
151719a81c14SSteve Longerbeam 		return -EINVAL;
151819a81c14SSteve Longerbeam 	max_band50 = (int)((prev_vts - 4) / band_step50);
151919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
152019a81c14SSteve Longerbeam }
152119a81c14SSteve Longerbeam 
152219a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
152319a81c14SSteve Longerbeam {
152419a81c14SSteve Longerbeam 	/* stable in high */
152519a81c14SSteve Longerbeam 	u32 fast_high, fast_low;
152619a81c14SSteve Longerbeam 	int ret;
152719a81c14SSteve Longerbeam 
152819a81c14SSteve Longerbeam 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
152919a81c14SSteve Longerbeam 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
153019a81c14SSteve Longerbeam 
153119a81c14SSteve Longerbeam 	fast_high = sensor->ae_high << 1;
153219a81c14SSteve Longerbeam 	if (fast_high > 255)
153319a81c14SSteve Longerbeam 		fast_high = 255;
153419a81c14SSteve Longerbeam 
153519a81c14SSteve Longerbeam 	fast_low = sensor->ae_low >> 1;
153619a81c14SSteve Longerbeam 
153719a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
153819a81c14SSteve Longerbeam 	if (ret)
153919a81c14SSteve Longerbeam 		return ret;
154019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
154119a81c14SSteve Longerbeam 	if (ret)
154219a81c14SSteve Longerbeam 		return ret;
154319a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
154419a81c14SSteve Longerbeam 	if (ret)
154519a81c14SSteve Longerbeam 		return ret;
154619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
154719a81c14SSteve Longerbeam 	if (ret)
154819a81c14SSteve Longerbeam 		return ret;
154919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
155019a81c14SSteve Longerbeam 	if (ret)
155119a81c14SSteve Longerbeam 		return ret;
155219a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
155319a81c14SSteve Longerbeam }
155419a81c14SSteve Longerbeam 
1555c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor)
155619a81c14SSteve Longerbeam {
155719a81c14SSteve Longerbeam 	u8 temp;
155819a81c14SSteve Longerbeam 	int ret;
155919a81c14SSteve Longerbeam 
156019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
156119a81c14SSteve Longerbeam 	if (ret)
156219a81c14SSteve Longerbeam 		return ret;
1563c2c3f42dSHugues Fruchet 
1564c2c3f42dSHugues Fruchet 	return temp & BIT(0);
156519a81c14SSteve Longerbeam }
156619a81c14SSteve Longerbeam 
1567ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1568ce85705aSHugues Fruchet {
1569ce85705aSHugues Fruchet 	int ret;
1570ce85705aSHugues Fruchet 
1571ce85705aSHugues Fruchet 	/*
1572ce85705aSHugues Fruchet 	 * TIMING TC REG21:
1573ce85705aSHugues Fruchet 	 * - [0]:	Horizontal binning enable
1574ce85705aSHugues Fruchet 	 */
1575ce85705aSHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1576ce85705aSHugues Fruchet 			     BIT(0), enable ? BIT(0) : 0);
1577ce85705aSHugues Fruchet 	if (ret)
1578ce85705aSHugues Fruchet 		return ret;
1579ce85705aSHugues Fruchet 	/*
1580ce85705aSHugues Fruchet 	 * TIMING TC REG20:
1581ce85705aSHugues Fruchet 	 * - [0]:	Undocumented, but hardcoded init sequences
1582ce85705aSHugues Fruchet 	 *		are always setting REG21/REG20 bit 0 to same value...
1583ce85705aSHugues Fruchet 	 */
1584ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1585ce85705aSHugues Fruchet 			      BIT(0), enable ? BIT(0) : 0);
1586ce85705aSHugues Fruchet }
1587ce85705aSHugues Fruchet 
158819a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
158919a81c14SSteve Longerbeam {
15908670d70aSHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
159119a81c14SSteve Longerbeam 	u8 temp, channel = virtual_channel;
159219a81c14SSteve Longerbeam 	int ret;
159319a81c14SSteve Longerbeam 
15948670d70aSHugues Fruchet 	if (channel > 3) {
15958670d70aSHugues Fruchet 		dev_err(&client->dev,
15968670d70aSHugues Fruchet 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
15978670d70aSHugues Fruchet 			__func__, channel);
159819a81c14SSteve Longerbeam 		return -EINVAL;
15998670d70aSHugues Fruchet 	}
160019a81c14SSteve Longerbeam 
160119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
160219a81c14SSteve Longerbeam 	if (ret)
160319a81c14SSteve Longerbeam 		return ret;
160419a81c14SSteve Longerbeam 	temp &= ~(3 << 6);
160519a81c14SSteve Longerbeam 	temp |= (channel << 6);
160619a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
160719a81c14SSteve Longerbeam }
160819a81c14SSteve Longerbeam 
160919a81c14SSteve Longerbeam static const struct ov5640_mode_info *
161019a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
161119a81c14SSteve Longerbeam 		 int width, int height, bool nearest)
161219a81c14SSteve Longerbeam {
16133c4a7372SHugues Fruchet 	const struct ov5640_mode_info *mode;
161419a81c14SSteve Longerbeam 
1615086c25f8SMaxime Ripard 	mode = v4l2_find_nearest_size(ov5640_mode_data,
1616086c25f8SMaxime Ripard 				      ARRAY_SIZE(ov5640_mode_data),
16173c4a7372SHugues Fruchet 				      hact, vact,
16183c4a7372SHugues Fruchet 				      width, height);
161919a81c14SSteve Longerbeam 
16203c4a7372SHugues Fruchet 	if (!mode ||
16213c4a7372SHugues Fruchet 	    (!nearest && (mode->hact != width || mode->vact != height)))
16223c4a7372SHugues Fruchet 		return NULL;
162319a81c14SSteve Longerbeam 
16245554c80eSAdam Ford 	/* Check to see if the current mode exceeds the max frame rate */
16255554c80eSAdam Ford 	if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps])
1626981e4454SBenoit Parrot 		return NULL;
1627981e4454SBenoit Parrot 
162819a81c14SSteve Longerbeam 	return mode;
162919a81c14SSteve Longerbeam }
163019a81c14SSteve Longerbeam 
1631cc196e48SBenoit Parrot static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1632cc196e48SBenoit Parrot {
1633cc196e48SBenoit Parrot 	u64 rate;
1634cc196e48SBenoit Parrot 
1635cc196e48SBenoit Parrot 	rate = sensor->current_mode->vtot * sensor->current_mode->htot;
1636cc196e48SBenoit Parrot 	rate *= ov5640_framerates[sensor->current_fr];
1637cc196e48SBenoit Parrot 
1638cc196e48SBenoit Parrot 	return rate;
1639cc196e48SBenoit Parrot }
1640cc196e48SBenoit Parrot 
164119a81c14SSteve Longerbeam /*
164219a81c14SSteve Longerbeam  * sensor changes between scaling and subsampling, go through
164319a81c14SSteve Longerbeam  * exposure calculation
164419a81c14SSteve Longerbeam  */
164541d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
164641d8d7f5SHugues Fruchet 					 const struct ov5640_mode_info *mode)
164719a81c14SSteve Longerbeam {
164819a81c14SSteve Longerbeam 	u32 prev_shutter, prev_gain16;
164919a81c14SSteve Longerbeam 	u32 cap_shutter, cap_gain16;
165019a81c14SSteve Longerbeam 	u32 cap_sysclk, cap_hts, cap_vts;
165119a81c14SSteve Longerbeam 	u32 light_freq, cap_bandfilt, cap_maxband;
165219a81c14SSteve Longerbeam 	u32 cap_gain16_shutter;
165319a81c14SSteve Longerbeam 	u8 average;
165419a81c14SSteve Longerbeam 	int ret;
165519a81c14SSteve Longerbeam 
165641d8d7f5SHugues Fruchet 	if (!mode->reg_data)
165719a81c14SSteve Longerbeam 		return -EINVAL;
165819a81c14SSteve Longerbeam 
165919a81c14SSteve Longerbeam 	/* read preview shutter */
166019a81c14SSteve Longerbeam 	ret = ov5640_get_exposure(sensor);
166119a81c14SSteve Longerbeam 	if (ret < 0)
166219a81c14SSteve Longerbeam 		return ret;
166319a81c14SSteve Longerbeam 	prev_shutter = ret;
1664c2c3f42dSHugues Fruchet 	ret = ov5640_get_binning(sensor);
166519a81c14SSteve Longerbeam 	if (ret < 0)
166619a81c14SSteve Longerbeam 		return ret;
166719a81c14SSteve Longerbeam 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
166819a81c14SSteve Longerbeam 	    mode->id != OV5640_MODE_1080P_1920_1080)
166919a81c14SSteve Longerbeam 		prev_shutter *= 2;
167019a81c14SSteve Longerbeam 
167119a81c14SSteve Longerbeam 	/* read preview gain */
167219a81c14SSteve Longerbeam 	ret = ov5640_get_gain(sensor);
167319a81c14SSteve Longerbeam 	if (ret < 0)
167419a81c14SSteve Longerbeam 		return ret;
167519a81c14SSteve Longerbeam 	prev_gain16 = ret;
167619a81c14SSteve Longerbeam 
167719a81c14SSteve Longerbeam 	/* get average */
167819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
167919a81c14SSteve Longerbeam 	if (ret)
168019a81c14SSteve Longerbeam 		return ret;
168119a81c14SSteve Longerbeam 
168219a81c14SSteve Longerbeam 	/* turn off night mode for capture */
168319a81c14SSteve Longerbeam 	ret = ov5640_set_night_mode(sensor);
168419a81c14SSteve Longerbeam 	if (ret < 0)
168519a81c14SSteve Longerbeam 		return ret;
168619a81c14SSteve Longerbeam 
168719a81c14SSteve Longerbeam 	/* Write capture setting */
168819a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
168919a81c14SSteve Longerbeam 	if (ret < 0)
169019a81c14SSteve Longerbeam 		return ret;
169119a81c14SSteve Longerbeam 
169219a81c14SSteve Longerbeam 	/* read capture VTS */
169319a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
169419a81c14SSteve Longerbeam 	if (ret < 0)
169519a81c14SSteve Longerbeam 		return ret;
169619a81c14SSteve Longerbeam 	cap_vts = ret;
169719a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
169819a81c14SSteve Longerbeam 	if (ret < 0)
169919a81c14SSteve Longerbeam 		return ret;
170019a81c14SSteve Longerbeam 	if (ret == 0)
170119a81c14SSteve Longerbeam 		return -EINVAL;
170219a81c14SSteve Longerbeam 	cap_hts = ret;
170319a81c14SSteve Longerbeam 
170419a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
170519a81c14SSteve Longerbeam 	if (ret < 0)
170619a81c14SSteve Longerbeam 		return ret;
170719a81c14SSteve Longerbeam 	if (ret == 0)
170819a81c14SSteve Longerbeam 		return -EINVAL;
170919a81c14SSteve Longerbeam 	cap_sysclk = ret;
171019a81c14SSteve Longerbeam 
171119a81c14SSteve Longerbeam 	/* calculate capture banding filter */
171219a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
171319a81c14SSteve Longerbeam 	if (ret < 0)
171419a81c14SSteve Longerbeam 		return ret;
171519a81c14SSteve Longerbeam 	light_freq = ret;
171619a81c14SSteve Longerbeam 
171719a81c14SSteve Longerbeam 	if (light_freq == 60) {
171819a81c14SSteve Longerbeam 		/* 60Hz */
171919a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
172019a81c14SSteve Longerbeam 	} else {
172119a81c14SSteve Longerbeam 		/* 50Hz */
172219a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
172319a81c14SSteve Longerbeam 	}
172419a81c14SSteve Longerbeam 
172519a81c14SSteve Longerbeam 	if (!sensor->prev_sysclk) {
172619a81c14SSteve Longerbeam 		ret = ov5640_get_sysclk(sensor);
172719a81c14SSteve Longerbeam 		if (ret < 0)
172819a81c14SSteve Longerbeam 			return ret;
172919a81c14SSteve Longerbeam 		if (ret == 0)
173019a81c14SSteve Longerbeam 			return -EINVAL;
173119a81c14SSteve Longerbeam 		sensor->prev_sysclk = ret;
173219a81c14SSteve Longerbeam 	}
173319a81c14SSteve Longerbeam 
173419a81c14SSteve Longerbeam 	if (!cap_bandfilt)
173519a81c14SSteve Longerbeam 		return -EINVAL;
173619a81c14SSteve Longerbeam 
173719a81c14SSteve Longerbeam 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
173819a81c14SSteve Longerbeam 
173919a81c14SSteve Longerbeam 	/* calculate capture shutter/gain16 */
174019a81c14SSteve Longerbeam 	if (average > sensor->ae_low && average < sensor->ae_high) {
174119a81c14SSteve Longerbeam 		/* in stable range */
174219a81c14SSteve Longerbeam 		cap_gain16_shutter =
174319a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
174419a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
174519a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts *
174619a81c14SSteve Longerbeam 			sensor->ae_target / average;
174719a81c14SSteve Longerbeam 	} else {
174819a81c14SSteve Longerbeam 		cap_gain16_shutter =
174919a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
175019a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
175119a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts;
175219a81c14SSteve Longerbeam 	}
175319a81c14SSteve Longerbeam 
175419a81c14SSteve Longerbeam 	/* gain to shutter */
175519a81c14SSteve Longerbeam 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
175619a81c14SSteve Longerbeam 		/* shutter < 1/100 */
175719a81c14SSteve Longerbeam 		cap_shutter = cap_gain16_shutter / 16;
175819a81c14SSteve Longerbeam 		if (cap_shutter < 1)
175919a81c14SSteve Longerbeam 			cap_shutter = 1;
176019a81c14SSteve Longerbeam 
176119a81c14SSteve Longerbeam 		cap_gain16 = cap_gain16_shutter / cap_shutter;
176219a81c14SSteve Longerbeam 		if (cap_gain16 < 16)
176319a81c14SSteve Longerbeam 			cap_gain16 = 16;
176419a81c14SSteve Longerbeam 	} else {
176519a81c14SSteve Longerbeam 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
176619a81c14SSteve Longerbeam 			/* exposure reach max */
176719a81c14SSteve Longerbeam 			cap_shutter = cap_bandfilt * cap_maxband;
176819a81c14SSteve Longerbeam 			if (!cap_shutter)
176919a81c14SSteve Longerbeam 				return -EINVAL;
177019a81c14SSteve Longerbeam 
177119a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
177219a81c14SSteve Longerbeam 		} else {
177319a81c14SSteve Longerbeam 			/* 1/100 < (cap_shutter = n/100) =< max */
177419a81c14SSteve Longerbeam 			cap_shutter =
177519a81c14SSteve Longerbeam 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
177619a81c14SSteve Longerbeam 				* cap_bandfilt;
177719a81c14SSteve Longerbeam 			if (!cap_shutter)
177819a81c14SSteve Longerbeam 				return -EINVAL;
177919a81c14SSteve Longerbeam 
178019a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
178119a81c14SSteve Longerbeam 		}
178219a81c14SSteve Longerbeam 	}
178319a81c14SSteve Longerbeam 
178419a81c14SSteve Longerbeam 	/* set capture gain */
17853cca8ef5SHugues Fruchet 	ret = ov5640_set_gain(sensor, cap_gain16);
178619a81c14SSteve Longerbeam 	if (ret)
178719a81c14SSteve Longerbeam 		return ret;
178819a81c14SSteve Longerbeam 
178919a81c14SSteve Longerbeam 	/* write capture shutter */
179019a81c14SSteve Longerbeam 	if (cap_shutter > (cap_vts - 4)) {
179119a81c14SSteve Longerbeam 		cap_vts = cap_shutter + 4;
179219a81c14SSteve Longerbeam 		ret = ov5640_set_vts(sensor, cap_vts);
179319a81c14SSteve Longerbeam 		if (ret < 0)
179419a81c14SSteve Longerbeam 			return ret;
179519a81c14SSteve Longerbeam 	}
179619a81c14SSteve Longerbeam 
179719a81c14SSteve Longerbeam 	/* set exposure */
17983cca8ef5SHugues Fruchet 	return ov5640_set_exposure(sensor, cap_shutter);
179919a81c14SSteve Longerbeam }
180019a81c14SSteve Longerbeam 
180119a81c14SSteve Longerbeam /*
180219a81c14SSteve Longerbeam  * if sensor changes inside scaling or subsampling
180319a81c14SSteve Longerbeam  * change mode directly
180419a81c14SSteve Longerbeam  */
180519a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
18063cca8ef5SHugues Fruchet 				  const struct ov5640_mode_info *mode)
180719a81c14SSteve Longerbeam {
180841d8d7f5SHugues Fruchet 	if (!mode->reg_data)
180919a81c14SSteve Longerbeam 		return -EINVAL;
181019a81c14SSteve Longerbeam 
181119a81c14SSteve Longerbeam 	/* Write capture setting */
18123cca8ef5SHugues Fruchet 	return ov5640_load_regs(sensor, mode);
181319a81c14SSteve Longerbeam }
181419a81c14SSteve Longerbeam 
1815985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor)
181619a81c14SSteve Longerbeam {
181719a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = sensor->current_mode;
1818985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *orig_mode = sensor->last_mode;
181919a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
18203cca8ef5SHugues Fruchet 	bool auto_gain = sensor->ctrls.auto_gain->val == 1;
1821dc29a1c1SHugues Fruchet 	bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
1822aa288248SMaxime Ripard 	unsigned long rate;
182319a81c14SSteve Longerbeam 	int ret;
182419a81c14SSteve Longerbeam 
182519a81c14SSteve Longerbeam 	dn_mode = mode->dn_mode;
182619a81c14SSteve Longerbeam 	orig_dn_mode = orig_mode->dn_mode;
182719a81c14SSteve Longerbeam 
182819a81c14SSteve Longerbeam 	/* auto gain and exposure must be turned off when changing modes */
18293cca8ef5SHugues Fruchet 	if (auto_gain) {
18303cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, false);
183119a81c14SSteve Longerbeam 		if (ret)
183219a81c14SSteve Longerbeam 			return ret;
18333cca8ef5SHugues Fruchet 	}
1834bf4a4b51SMaxime Ripard 
18353cca8ef5SHugues Fruchet 	if (auto_exp) {
1836dc29a1c1SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, false);
183719a81c14SSteve Longerbeam 		if (ret)
18383cca8ef5SHugues Fruchet 			goto restore_auto_gain;
18393cca8ef5SHugues Fruchet 	}
184019a81c14SSteve Longerbeam 
1841aa288248SMaxime Ripard 	/*
1842aa288248SMaxime Ripard 	 * All the formats we support have 16 bits per pixel, seems to require
1843aa288248SMaxime Ripard 	 * the same rate than YUV, so we can just use 16 bpp all the time.
1844aa288248SMaxime Ripard 	 */
1845cc196e48SBenoit Parrot 	rate = ov5640_calc_pixel_rate(sensor) * 16;
1846aa288248SMaxime Ripard 	if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1847aa288248SMaxime Ripard 		rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
1848aa288248SMaxime Ripard 		ret = ov5640_set_mipi_pclk(sensor, rate);
1849aa288248SMaxime Ripard 	} else {
1850aa288248SMaxime Ripard 		rate = rate / sensor->ep.bus.parallel.bus_width;
1851aa288248SMaxime Ripard 		ret = ov5640_set_dvp_pclk(sensor, rate);
1852aa288248SMaxime Ripard 	}
1853aa288248SMaxime Ripard 
1854aa288248SMaxime Ripard 	if (ret < 0)
1855aa288248SMaxime Ripard 		return 0;
1856aa288248SMaxime Ripard 
185719a81c14SSteve Longerbeam 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
185819a81c14SSteve Longerbeam 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
185919a81c14SSteve Longerbeam 		/*
186019a81c14SSteve Longerbeam 		 * change between subsampling and scaling
18613cca8ef5SHugues Fruchet 		 * go through exposure calculation
186219a81c14SSteve Longerbeam 		 */
186319a81c14SSteve Longerbeam 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
186419a81c14SSteve Longerbeam 	} else {
186519a81c14SSteve Longerbeam 		/*
186619a81c14SSteve Longerbeam 		 * change inside subsampling or scaling
186719a81c14SSteve Longerbeam 		 * download firmware directly
186819a81c14SSteve Longerbeam 		 */
18693cca8ef5SHugues Fruchet 		ret = ov5640_set_mode_direct(sensor, mode);
187019a81c14SSteve Longerbeam 	}
187119a81c14SSteve Longerbeam 	if (ret < 0)
18723cca8ef5SHugues Fruchet 		goto restore_auto_exp_gain;
18733cca8ef5SHugues Fruchet 
18743cca8ef5SHugues Fruchet 	/* restore auto gain and exposure */
18753cca8ef5SHugues Fruchet 	if (auto_gain)
18763cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
18773cca8ef5SHugues Fruchet 	if (auto_exp)
18783cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
187919a81c14SSteve Longerbeam 
1880ce85705aSHugues Fruchet 	ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1881ce85705aSHugues Fruchet 	if (ret < 0)
1882ce85705aSHugues Fruchet 		return ret;
188319a81c14SSteve Longerbeam 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
188419a81c14SSteve Longerbeam 	if (ret < 0)
188519a81c14SSteve Longerbeam 		return ret;
188619a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
188719a81c14SSteve Longerbeam 	if (ret < 0)
188819a81c14SSteve Longerbeam 		return ret;
188919a81c14SSteve Longerbeam 	ret = ov5640_set_bandingfilter(sensor);
189019a81c14SSteve Longerbeam 	if (ret < 0)
189119a81c14SSteve Longerbeam 		return ret;
189219a81c14SSteve Longerbeam 	ret = ov5640_set_virtual_channel(sensor);
189319a81c14SSteve Longerbeam 	if (ret < 0)
189419a81c14SSteve Longerbeam 		return ret;
189519a81c14SSteve Longerbeam 
189619a81c14SSteve Longerbeam 	sensor->pending_mode_change = false;
1897985cdcb0SHugues Fruchet 	sensor->last_mode = mode;
189819a81c14SSteve Longerbeam 
189919a81c14SSteve Longerbeam 	return 0;
19003cca8ef5SHugues Fruchet 
19013cca8ef5SHugues Fruchet restore_auto_exp_gain:
19023cca8ef5SHugues Fruchet 	if (auto_exp)
19033cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
19043cca8ef5SHugues Fruchet restore_auto_gain:
19053cca8ef5SHugues Fruchet 	if (auto_gain)
19063cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
19073cca8ef5SHugues Fruchet 
19083cca8ef5SHugues Fruchet 	return ret;
190919a81c14SSteve Longerbeam }
191019a81c14SSteve Longerbeam 
191119ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor,
191219ad26f9SAkinobu Mita 			       struct v4l2_mbus_framefmt *format);
191319ad26f9SAkinobu Mita 
191419a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */
191519a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor)
191619a81c14SSteve Longerbeam {
191719a81c14SSteve Longerbeam 	int ret;
191819a81c14SSteve Longerbeam 
191919a81c14SSteve Longerbeam 	/* first load the initial register values */
192019a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
192119a81c14SSteve Longerbeam 	if (ret < 0)
192219a81c14SSteve Longerbeam 		return ret;
1923985cdcb0SHugues Fruchet 	sensor->last_mode = &ov5640_mode_init_data;
192419a81c14SSteve Longerbeam 
19258f57c2f8SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
19267851fe7aSMaxime Ripard 			     (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
19277851fe7aSMaxime Ripard 			     ilog2(OV5640_SCLK_ROOT_DIV));
19288f57c2f8SMaxime Ripard 	if (ret)
19298f57c2f8SMaxime Ripard 		return ret;
19308f57c2f8SMaxime Ripard 
193119a81c14SSteve Longerbeam 	/* now restore the last capture mode */
1932985cdcb0SHugues Fruchet 	ret = ov5640_set_mode(sensor);
193319ad26f9SAkinobu Mita 	if (ret < 0)
193419ad26f9SAkinobu Mita 		return ret;
193519ad26f9SAkinobu Mita 
193619ad26f9SAkinobu Mita 	return ov5640_set_framefmt(sensor, &sensor->fmt);
193719a81c14SSteve Longerbeam }
193819a81c14SSteve Longerbeam 
193919a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable)
194019a81c14SSteve Longerbeam {
19411fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
194219a81c14SSteve Longerbeam }
194319a81c14SSteve Longerbeam 
194419a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor)
194519a81c14SSteve Longerbeam {
194619a81c14SSteve Longerbeam 	if (!sensor->reset_gpio)
194719a81c14SSteve Longerbeam 		return;
194819a81c14SSteve Longerbeam 
19491fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
195019a81c14SSteve Longerbeam 
195119a81c14SSteve Longerbeam 	/* camera power cycle */
195219a81c14SSteve Longerbeam 	ov5640_power(sensor, false);
195319a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
195419a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
195519a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
195619a81c14SSteve Longerbeam 
19571fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
195819a81c14SSteve Longerbeam 	usleep_range(1000, 2000);
195919a81c14SSteve Longerbeam 
19601fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
19611d4c41f3SLoic Poulain 	usleep_range(20000, 25000);
196219a81c14SSteve Longerbeam }
196319a81c14SSteve Longerbeam 
19640f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor)
196519a81c14SSteve Longerbeam {
19660f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
19670f7acb52SHugues Fruchet 	int ret;
196819a81c14SSteve Longerbeam 
19690f7acb52SHugues Fruchet 	ret = clk_prepare_enable(sensor->xclk);
19700f7acb52SHugues Fruchet 	if (ret) {
19710f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable clock\n",
19720f7acb52SHugues Fruchet 			__func__);
19730f7acb52SHugues Fruchet 		return ret;
19740f7acb52SHugues Fruchet 	}
197519a81c14SSteve Longerbeam 
197619a81c14SSteve Longerbeam 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
197719a81c14SSteve Longerbeam 				    sensor->supplies);
19780f7acb52SHugues Fruchet 	if (ret) {
19790f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable regulators\n",
19800f7acb52SHugues Fruchet 			__func__);
198119a81c14SSteve Longerbeam 		goto xclk_off;
19820f7acb52SHugues Fruchet 	}
198319a81c14SSteve Longerbeam 
198419a81c14SSteve Longerbeam 	ov5640_reset(sensor);
198519a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
198619a81c14SSteve Longerbeam 
198719a81c14SSteve Longerbeam 	ret = ov5640_init_slave_id(sensor);
198819a81c14SSteve Longerbeam 	if (ret)
198919a81c14SSteve Longerbeam 		goto power_off;
199019a81c14SSteve Longerbeam 
19910f7acb52SHugues Fruchet 	return 0;
19920f7acb52SHugues Fruchet 
19930f7acb52SHugues Fruchet power_off:
19940f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
19950f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
19960f7acb52SHugues Fruchet xclk_off:
19970f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
19980f7acb52SHugues Fruchet 	return ret;
19990f7acb52SHugues Fruchet }
20000f7acb52SHugues Fruchet 
20010f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor)
20020f7acb52SHugues Fruchet {
20030f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
20040f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
20050f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
20060f7acb52SHugues Fruchet }
20070f7acb52SHugues Fruchet 
2008b1751ae6SLad Prabhakar static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
2009b1751ae6SLad Prabhakar {
2010b1751ae6SLad Prabhakar 	int ret;
2011b1751ae6SLad Prabhakar 
2012b1751ae6SLad Prabhakar 	if (!on) {
2013b1751ae6SLad Prabhakar 		/* Reset MIPI bus settings to their default values. */
2014b1751ae6SLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2015b1751ae6SLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
2016b1751ae6SLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
2017b1751ae6SLad Prabhakar 		return 0;
2018b1751ae6SLad Prabhakar 	}
2019b1751ae6SLad Prabhakar 
2020b1751ae6SLad Prabhakar 	/*
2021b1751ae6SLad Prabhakar 	 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2022b1751ae6SLad Prabhakar 	 *
2023b1751ae6SLad Prabhakar 	 * 0x300e = 0x40
2024b1751ae6SLad Prabhakar 	 * [7:5] = 010	: 2 data lanes mode (see FIXME note in
2025b1751ae6SLad Prabhakar 	 *		  "ov5640_set_stream_mipi()")
2026b1751ae6SLad Prabhakar 	 * [4] = 0	: Power up MIPI HS Tx
2027b1751ae6SLad Prabhakar 	 * [3] = 0	: Power up MIPI LS Rx
2028b1751ae6SLad Prabhakar 	 * [2] = 0	: MIPI interface disabled
2029b1751ae6SLad Prabhakar 	 */
2030b1751ae6SLad Prabhakar 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40);
2031b1751ae6SLad Prabhakar 	if (ret)
2032b1751ae6SLad Prabhakar 		return ret;
2033b1751ae6SLad Prabhakar 
2034b1751ae6SLad Prabhakar 	/*
2035b1751ae6SLad Prabhakar 	 * Gate clock and set LP11 in 'no packets mode' (idle)
2036b1751ae6SLad Prabhakar 	 *
2037b1751ae6SLad Prabhakar 	 * 0x4800 = 0x24
2038b1751ae6SLad Prabhakar 	 * [5] = 1	: Gate clock when 'no packets'
2039b1751ae6SLad Prabhakar 	 * [2] = 1	: MIPI bus in LP11 when 'no packets'
2040b1751ae6SLad Prabhakar 	 */
2041b1751ae6SLad Prabhakar 	ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
2042b1751ae6SLad Prabhakar 	if (ret)
2043b1751ae6SLad Prabhakar 		return ret;
2044b1751ae6SLad Prabhakar 
2045b1751ae6SLad Prabhakar 	/*
2046b1751ae6SLad Prabhakar 	 * Set data lanes and clock in LP11 when 'sleeping'
2047b1751ae6SLad Prabhakar 	 *
2048b1751ae6SLad Prabhakar 	 * 0x3019 = 0x70
2049b1751ae6SLad Prabhakar 	 * [6] = 1	: MIPI data lane 2 in LP11 when 'sleeping'
2050b1751ae6SLad Prabhakar 	 * [5] = 1	: MIPI data lane 1 in LP11 when 'sleeping'
2051b1751ae6SLad Prabhakar 	 * [4] = 1	: MIPI clock lane in LP11 when 'sleeping'
2052b1751ae6SLad Prabhakar 	 */
2053b1751ae6SLad Prabhakar 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
2054b1751ae6SLad Prabhakar 	if (ret)
2055b1751ae6SLad Prabhakar 		return ret;
2056b1751ae6SLad Prabhakar 
2057b1751ae6SLad Prabhakar 	/* Give lanes some time to coax into LP11 state. */
2058b1751ae6SLad Prabhakar 	usleep_range(500, 1000);
2059b1751ae6SLad Prabhakar 
2060b1751ae6SLad Prabhakar 	return 0;
2061b1751ae6SLad Prabhakar }
2062b1751ae6SLad Prabhakar 
2063576f5d4bSLad Prabhakar static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
2064576f5d4bSLad Prabhakar {
2065311a6408SLad Prabhakar 	unsigned int flags = sensor->ep.bus.parallel.flags;
206668579b32SHugues Fruchet 	bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
206768579b32SHugues Fruchet 	u8 polarities = 0;
2068576f5d4bSLad Prabhakar 	int ret;
2069576f5d4bSLad Prabhakar 
2070576f5d4bSLad Prabhakar 	if (!on) {
2071576f5d4bSLad Prabhakar 		/* Reset settings to their default values. */
207268579b32SHugues Fruchet 		ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
2073311a6408SLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
2074311a6408SLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
2075576f5d4bSLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
2076576f5d4bSLad Prabhakar 		ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
2077576f5d4bSLad Prabhakar 		return 0;
2078576f5d4bSLad Prabhakar 	}
2079576f5d4bSLad Prabhakar 
2080576f5d4bSLad Prabhakar 	/*
2081311a6408SLad Prabhakar 	 * Note about parallel port configuration.
2082311a6408SLad Prabhakar 	 *
2083311a6408SLad Prabhakar 	 * When configured in parallel mode, the OV5640 will
2084311a6408SLad Prabhakar 	 * output 10 bits data on DVP data lines [9:0].
2085311a6408SLad Prabhakar 	 * If only 8 bits data are wanted, the 8 bits data lines
2086311a6408SLad Prabhakar 	 * of the camera interface must be physically connected
2087311a6408SLad Prabhakar 	 * on the DVP data lines [9:2].
2088311a6408SLad Prabhakar 	 *
2089311a6408SLad Prabhakar 	 * Control lines polarity can be configured through
2090311a6408SLad Prabhakar 	 * devicetree endpoint control lines properties.
2091311a6408SLad Prabhakar 	 * If no endpoint control lines properties are set,
2092311a6408SLad Prabhakar 	 * polarity will be as below:
2093311a6408SLad Prabhakar 	 * - VSYNC:	active high
2094311a6408SLad Prabhakar 	 * - HREF:	active low
2095311a6408SLad Prabhakar 	 * - PCLK:	active low
209668579b32SHugues Fruchet 	 *
209768579b32SHugues Fruchet 	 * VSYNC & HREF are not configured if BT656 bus mode is selected
2098311a6408SLad Prabhakar 	 */
209968579b32SHugues Fruchet 
210068579b32SHugues Fruchet 	/*
210168579b32SHugues Fruchet 	 * BT656 embedded synchronization configuration
210268579b32SHugues Fruchet 	 *
210368579b32SHugues Fruchet 	 * CCIR656 CTRL00
210468579b32SHugues Fruchet 	 * - [7]:	SYNC code selection (0: auto generate sync code,
210568579b32SHugues Fruchet 	 *		1: sync code from regs 0x4732-0x4735)
210668579b32SHugues Fruchet 	 * - [6]:	f value in CCIR656 SYNC code when fixed f value
210768579b32SHugues Fruchet 	 * - [5]:	Fixed f value
210868579b32SHugues Fruchet 	 * - [4:3]:	Blank toggle data options (00: data=1'h040/1'h200,
210968579b32SHugues Fruchet 	 *		01: data from regs 0x4736-0x4738, 10: always keep 0)
211068579b32SHugues Fruchet 	 * - [1]:	Clip data disable
211168579b32SHugues Fruchet 	 * - [0]:	CCIR656 mode enable
211268579b32SHugues Fruchet 	 *
211368579b32SHugues Fruchet 	 * Default CCIR656 SAV/EAV mode with default codes
211468579b32SHugues Fruchet 	 * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
211568579b32SHugues Fruchet 	 * - CCIR656 mode enable
211668579b32SHugues Fruchet 	 * - auto generation of sync codes
211768579b32SHugues Fruchet 	 * - blank toggle data 1'h040/1'h200
211868579b32SHugues Fruchet 	 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
211968579b32SHugues Fruchet 	 */
212068579b32SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
212168579b32SHugues Fruchet 			       bt656 ? 0x01 : 0x00);
212268579b32SHugues Fruchet 	if (ret)
212368579b32SHugues Fruchet 		return ret;
212468579b32SHugues Fruchet 
2125311a6408SLad Prabhakar 	/*
2126311a6408SLad Prabhakar 	 * configure parallel port control lines polarity
2127311a6408SLad Prabhakar 	 *
2128311a6408SLad Prabhakar 	 * POLARITY CTRL0
2129311a6408SLad Prabhakar 	 * - [5]:	PCLK polarity (0: active low, 1: active high)
2130311a6408SLad Prabhakar 	 * - [1]:	HREF polarity (0: active low, 1: active high)
2131311a6408SLad Prabhakar 	 * - [0]:	VSYNC polarity (mismatch here between
2132311a6408SLad Prabhakar 	 *		datasheet and hardware, 0 is active high
2133311a6408SLad Prabhakar 	 *		and 1 is active low...)
2134311a6408SLad Prabhakar 	 */
213568579b32SHugues Fruchet 	if (!bt656) {
2136311a6408SLad Prabhakar 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
213768579b32SHugues Fruchet 			polarities |= BIT(1);
2138311a6408SLad Prabhakar 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
213968579b32SHugues Fruchet 			polarities |= BIT(0);
214068579b32SHugues Fruchet 	}
214168579b32SHugues Fruchet 	if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
214268579b32SHugues Fruchet 		polarities |= BIT(5);
2143311a6408SLad Prabhakar 
214468579b32SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
2145311a6408SLad Prabhakar 	if (ret)
2146311a6408SLad Prabhakar 		return ret;
2147311a6408SLad Prabhakar 
2148311a6408SLad Prabhakar 	/*
214968579b32SHugues Fruchet 	 * powerdown MIPI TX/RX PHY & enable DVP
2150311a6408SLad Prabhakar 	 *
2151311a6408SLad Prabhakar 	 * MIPI CONTROL 00
215268579b32SHugues Fruchet 	 * [4] = 1	: Power down MIPI HS Tx
215368579b32SHugues Fruchet 	 * [3] = 1	: Power down MIPI LS Rx
215468579b32SHugues Fruchet 	 * [2] = 0	: DVP enable (MIPI disable)
2155311a6408SLad Prabhakar 	 */
2156311a6408SLad Prabhakar 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
2157311a6408SLad Prabhakar 	if (ret)
2158311a6408SLad Prabhakar 		return ret;
2159311a6408SLad Prabhakar 
2160311a6408SLad Prabhakar 	/*
2161576f5d4bSLad Prabhakar 	 * enable VSYNC/HREF/PCLK DVP control lines
2162576f5d4bSLad Prabhakar 	 * & D[9:6] DVP data lines
2163576f5d4bSLad Prabhakar 	 *
2164576f5d4bSLad Prabhakar 	 * PAD OUTPUT ENABLE 01
2165576f5d4bSLad Prabhakar 	 * - 6:		VSYNC output enable
2166576f5d4bSLad Prabhakar 	 * - 5:		HREF output enable
2167576f5d4bSLad Prabhakar 	 * - 4:		PCLK output enable
2168576f5d4bSLad Prabhakar 	 * - [3:0]:	D[9:6] output enable
2169576f5d4bSLad Prabhakar 	 */
21704039b037SLad Prabhakar 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
217168579b32SHugues Fruchet 			       bt656 ? 0x1f : 0x7f);
2172576f5d4bSLad Prabhakar 	if (ret)
2173576f5d4bSLad Prabhakar 		return ret;
2174576f5d4bSLad Prabhakar 
2175576f5d4bSLad Prabhakar 	/*
2176576f5d4bSLad Prabhakar 	 * enable D[5:0] DVP data lines
2177576f5d4bSLad Prabhakar 	 *
2178576f5d4bSLad Prabhakar 	 * PAD OUTPUT ENABLE 02
2179576f5d4bSLad Prabhakar 	 * - [7:2]:	D[5:0] output enable
2180576f5d4bSLad Prabhakar 	 */
2181576f5d4bSLad Prabhakar 	return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
2182576f5d4bSLad Prabhakar }
2183576f5d4bSLad Prabhakar 
21840f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
21850f7acb52SHugues Fruchet {
21860f7acb52SHugues Fruchet 	int ret = 0;
21870f7acb52SHugues Fruchet 
21880f7acb52SHugues Fruchet 	if (on) {
21890f7acb52SHugues Fruchet 		ret = ov5640_set_power_on(sensor);
21900f7acb52SHugues Fruchet 		if (ret)
21910f7acb52SHugues Fruchet 			return ret;
21920f7acb52SHugues Fruchet 
219319a81c14SSteve Longerbeam 		ret = ov5640_restore_mode(sensor);
219419a81c14SSteve Longerbeam 		if (ret)
219519a81c14SSteve Longerbeam 			goto power_off;
2196b1751ae6SLad Prabhakar 	}
219719a81c14SSteve Longerbeam 
2198576f5d4bSLad Prabhakar 	if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2199b1751ae6SLad Prabhakar 		ret = ov5640_set_power_mipi(sensor, on);
2200576f5d4bSLad Prabhakar 	else
2201576f5d4bSLad Prabhakar 		ret = ov5640_set_power_dvp(sensor, on);
2202b1751ae6SLad Prabhakar 	if (ret)
2203b1751ae6SLad Prabhakar 		goto power_off;
2204aa4bb8b8SJacopo Mondi 
2205b1751ae6SLad Prabhakar 	if (!on)
2206aa4bb8b8SJacopo Mondi 		ov5640_set_power_off(sensor);
220719a81c14SSteve Longerbeam 
220819a81c14SSteve Longerbeam 	return 0;
220919a81c14SSteve Longerbeam 
221019a81c14SSteve Longerbeam power_off:
22110f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
221219a81c14SSteve Longerbeam 	return ret;
221319a81c14SSteve Longerbeam }
221419a81c14SSteve Longerbeam 
221519a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */
221619a81c14SSteve Longerbeam 
221719a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on)
221819a81c14SSteve Longerbeam {
221919a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
222019a81c14SSteve Longerbeam 	int ret = 0;
222119a81c14SSteve Longerbeam 
222219a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
222319a81c14SSteve Longerbeam 
222419a81c14SSteve Longerbeam 	/*
222519a81c14SSteve Longerbeam 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
222619a81c14SSteve Longerbeam 	 * update the power state.
222719a81c14SSteve Longerbeam 	 */
222819a81c14SSteve Longerbeam 	if (sensor->power_count == !on) {
222919a81c14SSteve Longerbeam 		ret = ov5640_set_power(sensor, !!on);
223019a81c14SSteve Longerbeam 		if (ret)
223119a81c14SSteve Longerbeam 			goto out;
223219a81c14SSteve Longerbeam 	}
223319a81c14SSteve Longerbeam 
223419a81c14SSteve Longerbeam 	/* Update the power count. */
223519a81c14SSteve Longerbeam 	sensor->power_count += on ? 1 : -1;
223619a81c14SSteve Longerbeam 	WARN_ON(sensor->power_count < 0);
223719a81c14SSteve Longerbeam out:
223819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
223919a81c14SSteve Longerbeam 
224019a81c14SSteve Longerbeam 	if (on && !ret && sensor->power_count == 1) {
224119a81c14SSteve Longerbeam 		/* restore controls */
224219a81c14SSteve Longerbeam 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
224319a81c14SSteve Longerbeam 	}
224419a81c14SSteve Longerbeam 
224519a81c14SSteve Longerbeam 	return ret;
224619a81c14SSteve Longerbeam }
224719a81c14SSteve Longerbeam 
224819a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
224919a81c14SSteve Longerbeam 				     struct v4l2_fract *fi,
225019a81c14SSteve Longerbeam 				     u32 width, u32 height)
225119a81c14SSteve Longerbeam {
225219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
22536530a5ebSJagan Teki 	enum ov5640_frame_rate rate = OV5640_15_FPS;
2254f6cc192fSMaxime Ripard 	int minfps, maxfps, best_fps, fps;
2255f6cc192fSMaxime Ripard 	int i;
225619a81c14SSteve Longerbeam 
225719a81c14SSteve Longerbeam 	minfps = ov5640_framerates[OV5640_15_FPS];
2258e823fb16SMaxime Ripard 	maxfps = ov5640_framerates[OV5640_60_FPS];
225919a81c14SSteve Longerbeam 
226019a81c14SSteve Longerbeam 	if (fi->numerator == 0) {
226119a81c14SSteve Longerbeam 		fi->denominator = maxfps;
226219a81c14SSteve Longerbeam 		fi->numerator = 1;
2263e823fb16SMaxime Ripard 		rate = OV5640_60_FPS;
2264e823fb16SMaxime Ripard 		goto find_mode;
226519a81c14SSteve Longerbeam 	}
226619a81c14SSteve Longerbeam 
2267f6cc192fSMaxime Ripard 	fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2268f6cc192fSMaxime Ripard 			minfps, maxfps);
2269f6cc192fSMaxime Ripard 
2270f6cc192fSMaxime Ripard 	best_fps = minfps;
2271f6cc192fSMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2272f6cc192fSMaxime Ripard 		int curr_fps = ov5640_framerates[i];
2273f6cc192fSMaxime Ripard 
2274f6cc192fSMaxime Ripard 		if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2275f6cc192fSMaxime Ripard 			best_fps = curr_fps;
2276f6cc192fSMaxime Ripard 			rate = i;
2277f6cc192fSMaxime Ripard 		}
2278f6cc192fSMaxime Ripard 	}
227919a81c14SSteve Longerbeam 
228019a81c14SSteve Longerbeam 	fi->numerator = 1;
2281f6cc192fSMaxime Ripard 	fi->denominator = best_fps;
228219a81c14SSteve Longerbeam 
2283e823fb16SMaxime Ripard find_mode:
22845a3ad937SMaxime Ripard 	mode = ov5640_find_mode(sensor, rate, width, height, false);
22855a3ad937SMaxime Ripard 	return mode ? rate : -EINVAL;
228619a81c14SSteve Longerbeam }
228719a81c14SSteve Longerbeam 
228819a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd,
22890d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
229019a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
229119a81c14SSteve Longerbeam {
229219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
229319a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt *fmt;
229419a81c14SSteve Longerbeam 
229519a81c14SSteve Longerbeam 	if (format->pad != 0)
229619a81c14SSteve Longerbeam 		return -EINVAL;
229719a81c14SSteve Longerbeam 
229819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
229919a81c14SSteve Longerbeam 
230019a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
23010d346d2aSTomi Valkeinen 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
230219a81c14SSteve Longerbeam 						 format->pad);
230319a81c14SSteve Longerbeam 	else
230419a81c14SSteve Longerbeam 		fmt = &sensor->fmt;
230519a81c14SSteve Longerbeam 
230619a81c14SSteve Longerbeam 	format->format = *fmt;
230719a81c14SSteve Longerbeam 
230819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
230919a81c14SSteve Longerbeam 
231019a81c14SSteve Longerbeam 	return 0;
231119a81c14SSteve Longerbeam }
231219a81c14SSteve Longerbeam 
231319a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
231419a81c14SSteve Longerbeam 				   struct v4l2_mbus_framefmt *fmt,
231519a81c14SSteve Longerbeam 				   enum ov5640_frame_rate fr,
231619a81c14SSteve Longerbeam 				   const struct ov5640_mode_info **new_mode)
231719a81c14SSteve Longerbeam {
231819a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
231919a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
2320e3ee691dSHugues Fruchet 	int i;
232119a81c14SSteve Longerbeam 
232219a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
232319a81c14SSteve Longerbeam 	if (!mode)
232419a81c14SSteve Longerbeam 		return -EINVAL;
2325dba13a0bSMaxime Ripard 	fmt->width = mode->hact;
2326dba13a0bSMaxime Ripard 	fmt->height = mode->vact;
232719a81c14SSteve Longerbeam 
232819a81c14SSteve Longerbeam 	if (new_mode)
232919a81c14SSteve Longerbeam 		*new_mode = mode;
2330e3ee691dSHugues Fruchet 
2331e3ee691dSHugues Fruchet 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2332e3ee691dSHugues Fruchet 		if (ov5640_formats[i].code == fmt->code)
2333e3ee691dSHugues Fruchet 			break;
2334e3ee691dSHugues Fruchet 	if (i >= ARRAY_SIZE(ov5640_formats))
2335e6441fdeSHugues Fruchet 		i = 0;
2336e6441fdeSHugues Fruchet 
2337e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[i].code;
2338e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[i].colorspace;
2339e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2340e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2341e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2342e3ee691dSHugues Fruchet 
234319a81c14SSteve Longerbeam 	return 0;
234419a81c14SSteve Longerbeam }
234519a81c14SSteve Longerbeam 
234619a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd,
23470d346d2aSTomi Valkeinen 			  struct v4l2_subdev_state *sd_state,
234819a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
234919a81c14SSteve Longerbeam {
235019a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
235119a81c14SSteve Longerbeam 	const struct ov5640_mode_info *new_mode;
2352e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
235319a81c14SSteve Longerbeam 	int ret;
235419a81c14SSteve Longerbeam 
235519a81c14SSteve Longerbeam 	if (format->pad != 0)
235619a81c14SSteve Longerbeam 		return -EINVAL;
235719a81c14SSteve Longerbeam 
235819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
235919a81c14SSteve Longerbeam 
236019a81c14SSteve Longerbeam 	if (sensor->streaming) {
236119a81c14SSteve Longerbeam 		ret = -EBUSY;
236219a81c14SSteve Longerbeam 		goto out;
236319a81c14SSteve Longerbeam 	}
236419a81c14SSteve Longerbeam 
2365e6441fdeSHugues Fruchet 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
236619a81c14SSteve Longerbeam 				      sensor->current_fr, &new_mode);
236719a81c14SSteve Longerbeam 	if (ret)
236819a81c14SSteve Longerbeam 		goto out;
236919a81c14SSteve Longerbeam 
2370e738f5ddSMirela Rabulea 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2371e738f5ddSMirela Rabulea 		*v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
2372e738f5ddSMirela Rabulea 		goto out;
2373e738f5ddSMirela Rabulea 	}
237419a81c14SSteve Longerbeam 
23756949d864SHugues Fruchet 	if (new_mode != sensor->current_mode) {
237619a81c14SSteve Longerbeam 		sensor->current_mode = new_mode;
237719a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
23786949d864SHugues Fruchet 	}
237907115449SJacopo Mondi 	if (mbus_fmt->code != sensor->fmt.code)
2380fb98e29fSHugues Fruchet 		sensor->pending_fmt_change = true;
238107115449SJacopo Mondi 
2382e738f5ddSMirela Rabulea 	/* update format even if code is unchanged, resolution might change */
2383e738f5ddSMirela Rabulea 	sensor->fmt = *mbus_fmt;
2384e738f5ddSMirela Rabulea 
2385cc196e48SBenoit Parrot 	__v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2386cc196e48SBenoit Parrot 				 ov5640_calc_pixel_rate(sensor));
238719a81c14SSteve Longerbeam out:
238819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
238919a81c14SSteve Longerbeam 	return ret;
239019a81c14SSteve Longerbeam }
239119a81c14SSteve Longerbeam 
2392e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2393e3ee691dSHugues Fruchet 			       struct v4l2_mbus_framefmt *format)
2394e3ee691dSHugues Fruchet {
2395e3ee691dSHugues Fruchet 	int ret = 0;
2396d47c4126SHugues Fruchet 	bool is_jpeg = false;
2397b7ed3abdSLoic Poulain 	u8 fmt, mux;
2398e3ee691dSHugues Fruchet 
2399e3ee691dSHugues Fruchet 	switch (format->code) {
24001536fbdbSXavier Roumegue 	case MEDIA_BUS_FMT_UYVY8_1X16:
2401e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_UYVY8_2X8:
2402e3ee691dSHugues Fruchet 		/* YUV422, UYVY */
2403b7ed3abdSLoic Poulain 		fmt = 0x3f;
2404b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_YUV422;
2405e3ee691dSHugues Fruchet 		break;
24061536fbdbSXavier Roumegue 	case MEDIA_BUS_FMT_YUYV8_1X16:
2407e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_YUYV8_2X8:
2408e3ee691dSHugues Fruchet 		/* YUV422, YUYV */
2409b7ed3abdSLoic Poulain 		fmt = 0x30;
2410b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_YUV422;
2411e3ee691dSHugues Fruchet 		break;
2412e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
2413e3ee691dSHugues Fruchet 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2414b7ed3abdSLoic Poulain 		fmt = 0x6F;
2415b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RGB;
2416e3ee691dSHugues Fruchet 		break;
2417e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
2418e3ee691dSHugues Fruchet 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2419b7ed3abdSLoic Poulain 		fmt = 0x61;
2420b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RGB;
2421e3ee691dSHugues Fruchet 		break;
2422d47c4126SHugues Fruchet 	case MEDIA_BUS_FMT_JPEG_1X8:
2423d47c4126SHugues Fruchet 		/* YUV422, YUYV */
2424b7ed3abdSLoic Poulain 		fmt = 0x30;
2425b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_YUV422;
2426d47c4126SHugues Fruchet 		is_jpeg = true;
2427d47c4126SHugues Fruchet 		break;
2428b7ed3abdSLoic Poulain 	case MEDIA_BUS_FMT_SBGGR8_1X8:
2429b7ed3abdSLoic Poulain 		/* Raw, BGBG... / GRGR... */
2430b7ed3abdSLoic Poulain 		fmt = 0x00;
2431b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RAW_DPC;
2432b7ed3abdSLoic Poulain 		break;
2433b7ed3abdSLoic Poulain 	case MEDIA_BUS_FMT_SGBRG8_1X8:
2434b7ed3abdSLoic Poulain 		/* Raw bayer, GBGB... / RGRG... */
2435b7ed3abdSLoic Poulain 		fmt = 0x01;
2436b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RAW_DPC;
2437b7ed3abdSLoic Poulain 		break;
2438b7ed3abdSLoic Poulain 	case MEDIA_BUS_FMT_SGRBG8_1X8:
2439b7ed3abdSLoic Poulain 		/* Raw bayer, GRGR... / BGBG... */
2440b7ed3abdSLoic Poulain 		fmt = 0x02;
2441b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RAW_DPC;
2442b7ed3abdSLoic Poulain 		break;
2443b7ed3abdSLoic Poulain 	case MEDIA_BUS_FMT_SRGGB8_1X8:
2444b7ed3abdSLoic Poulain 		/* Raw bayer, RGRG... / GBGB... */
2445b7ed3abdSLoic Poulain 		fmt = 0x03;
2446b7ed3abdSLoic Poulain 		mux = OV5640_FMT_MUX_RAW_DPC;
2447b7ed3abdSLoic Poulain 		break;
2448e3ee691dSHugues Fruchet 	default:
2449e3ee691dSHugues Fruchet 		return -EINVAL;
2450e3ee691dSHugues Fruchet 	}
2451e3ee691dSHugues Fruchet 
2452e3ee691dSHugues Fruchet 	/* FORMAT CONTROL00: YUV and RGB formatting */
2453b7ed3abdSLoic Poulain 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
2454e3ee691dSHugues Fruchet 	if (ret)
2455e3ee691dSHugues Fruchet 		return ret;
2456e3ee691dSHugues Fruchet 
2457e3ee691dSHugues Fruchet 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
2458b7ed3abdSLoic Poulain 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
2459d47c4126SHugues Fruchet 	if (ret)
2460d47c4126SHugues Fruchet 		return ret;
2461d47c4126SHugues Fruchet 
2462d47c4126SHugues Fruchet 	/*
2463d47c4126SHugues Fruchet 	 * TIMING TC REG21:
2464d47c4126SHugues Fruchet 	 * - [5]:	JPEG enable
2465d47c4126SHugues Fruchet 	 */
2466d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2467d47c4126SHugues Fruchet 			     BIT(5), is_jpeg ? BIT(5) : 0);
2468d47c4126SHugues Fruchet 	if (ret)
2469d47c4126SHugues Fruchet 		return ret;
2470d47c4126SHugues Fruchet 
2471d47c4126SHugues Fruchet 	/*
2472d47c4126SHugues Fruchet 	 * SYSTEM RESET02:
2473d47c4126SHugues Fruchet 	 * - [4]:	Reset JFIFO
2474d47c4126SHugues Fruchet 	 * - [3]:	Reset SFIFO
2475d47c4126SHugues Fruchet 	 * - [2]:	Reset JPEG
2476d47c4126SHugues Fruchet 	 */
2477d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2478d47c4126SHugues Fruchet 			     BIT(4) | BIT(3) | BIT(2),
2479d47c4126SHugues Fruchet 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2480d47c4126SHugues Fruchet 	if (ret)
2481d47c4126SHugues Fruchet 		return ret;
2482d47c4126SHugues Fruchet 
2483d47c4126SHugues Fruchet 	/*
2484d47c4126SHugues Fruchet 	 * CLOCK ENABLE02:
2485d47c4126SHugues Fruchet 	 * - [5]:	Enable JPEG 2x clock
2486d47c4126SHugues Fruchet 	 * - [3]:	Enable JPEG clock
2487d47c4126SHugues Fruchet 	 */
2488d47c4126SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2489d47c4126SHugues Fruchet 			      BIT(5) | BIT(3),
2490d47c4126SHugues Fruchet 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
2491e3ee691dSHugues Fruchet }
249219a81c14SSteve Longerbeam 
249319a81c14SSteve Longerbeam /*
249419a81c14SSteve Longerbeam  * Sensor Controls.
249519a81c14SSteve Longerbeam  */
249619a81c14SSteve Longerbeam 
249719a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
249819a81c14SSteve Longerbeam {
249919a81c14SSteve Longerbeam 	int ret;
250019a81c14SSteve Longerbeam 
250119a81c14SSteve Longerbeam 	if (value) {
250219a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
250319a81c14SSteve Longerbeam 				     BIT(0), BIT(0));
250419a81c14SSteve Longerbeam 		if (ret)
250519a81c14SSteve Longerbeam 			return ret;
250619a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
250719a81c14SSteve Longerbeam 	} else {
250819a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
250919a81c14SSteve Longerbeam 	}
251019a81c14SSteve Longerbeam 
251119a81c14SSteve Longerbeam 	return ret;
251219a81c14SSteve Longerbeam }
251319a81c14SSteve Longerbeam 
251419a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
251519a81c14SSteve Longerbeam {
251619a81c14SSteve Longerbeam 	int ret;
251719a81c14SSteve Longerbeam 
251819a81c14SSteve Longerbeam 	if (value) {
251919a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
252019a81c14SSteve Longerbeam 				     BIT(2), BIT(2));
252119a81c14SSteve Longerbeam 		if (ret)
252219a81c14SSteve Longerbeam 			return ret;
252319a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
252419a81c14SSteve Longerbeam 				       value & 0xff);
252519a81c14SSteve Longerbeam 	} else {
252619a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
252719a81c14SSteve Longerbeam 	}
252819a81c14SSteve Longerbeam 
252919a81c14SSteve Longerbeam 	return ret;
253019a81c14SSteve Longerbeam }
253119a81c14SSteve Longerbeam 
253219a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
253319a81c14SSteve Longerbeam {
253419a81c14SSteve Longerbeam 	int ret;
253519a81c14SSteve Longerbeam 
253619a81c14SSteve Longerbeam 	if (value) {
253719a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
253819a81c14SSteve Longerbeam 				     BIT(1), BIT(1));
253919a81c14SSteve Longerbeam 		if (ret)
254019a81c14SSteve Longerbeam 			return ret;
254119a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
254219a81c14SSteve Longerbeam 				       value & 0xff);
254319a81c14SSteve Longerbeam 		if (ret)
254419a81c14SSteve Longerbeam 			return ret;
254519a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
254619a81c14SSteve Longerbeam 				       value & 0xff);
254719a81c14SSteve Longerbeam 	} else {
254819a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
254919a81c14SSteve Longerbeam 	}
255019a81c14SSteve Longerbeam 
255119a81c14SSteve Longerbeam 	return ret;
255219a81c14SSteve Longerbeam }
255319a81c14SSteve Longerbeam 
255419a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
255519a81c14SSteve Longerbeam {
255619a81c14SSteve Longerbeam 	int ret;
255719a81c14SSteve Longerbeam 
255819a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
255919a81c14SSteve Longerbeam 			     BIT(0), awb ? 0 : 1);
256019a81c14SSteve Longerbeam 	if (ret)
256119a81c14SSteve Longerbeam 		return ret;
256219a81c14SSteve Longerbeam 
256319a81c14SSteve Longerbeam 	if (!awb) {
256419a81c14SSteve Longerbeam 		u16 red = (u16)sensor->ctrls.red_balance->val;
256519a81c14SSteve Longerbeam 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
256619a81c14SSteve Longerbeam 
256719a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
256819a81c14SSteve Longerbeam 		if (ret)
256919a81c14SSteve Longerbeam 			return ret;
257019a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
257119a81c14SSteve Longerbeam 	}
257219a81c14SSteve Longerbeam 
257319a81c14SSteve Longerbeam 	return ret;
257419a81c14SSteve Longerbeam }
257519a81c14SSteve Longerbeam 
25763cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
25773cca8ef5SHugues Fruchet 				    enum v4l2_exposure_auto_type auto_exposure)
257819a81c14SSteve Longerbeam {
257919a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
25803cca8ef5SHugues Fruchet 	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
258119a81c14SSteve Longerbeam 	int ret = 0;
258219a81c14SSteve Longerbeam 
258319a81c14SSteve Longerbeam 	if (ctrls->auto_exp->is_new) {
25843cca8ef5SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, auto_exp);
258519a81c14SSteve Longerbeam 		if (ret)
258619a81c14SSteve Longerbeam 			return ret;
258719a81c14SSteve Longerbeam 	}
258819a81c14SSteve Longerbeam 
25893cca8ef5SHugues Fruchet 	if (!auto_exp && ctrls->exposure->is_new) {
259019a81c14SSteve Longerbeam 		u16 max_exp;
259119a81c14SSteve Longerbeam 
259219a81c14SSteve Longerbeam 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
259319a81c14SSteve Longerbeam 					&max_exp);
259419a81c14SSteve Longerbeam 		if (ret)
259519a81c14SSteve Longerbeam 			return ret;
259619a81c14SSteve Longerbeam 		ret = ov5640_get_vts(sensor);
259719a81c14SSteve Longerbeam 		if (ret < 0)
259819a81c14SSteve Longerbeam 			return ret;
259919a81c14SSteve Longerbeam 		max_exp += ret;
26006146fde3SHugues Fruchet 		ret = 0;
260119a81c14SSteve Longerbeam 
260219a81c14SSteve Longerbeam 		if (ctrls->exposure->val < max_exp)
260319a81c14SSteve Longerbeam 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
260419a81c14SSteve Longerbeam 	}
260519a81c14SSteve Longerbeam 
260619a81c14SSteve Longerbeam 	return ret;
260719a81c14SSteve Longerbeam }
260819a81c14SSteve Longerbeam 
26093cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
261019a81c14SSteve Longerbeam {
261119a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
261219a81c14SSteve Longerbeam 	int ret = 0;
261319a81c14SSteve Longerbeam 
261419a81c14SSteve Longerbeam 	if (ctrls->auto_gain->is_new) {
26153cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, auto_gain);
261619a81c14SSteve Longerbeam 		if (ret)
261719a81c14SSteve Longerbeam 			return ret;
261819a81c14SSteve Longerbeam 	}
261919a81c14SSteve Longerbeam 
26203cca8ef5SHugues Fruchet 	if (!auto_gain && ctrls->gain->is_new)
26213cca8ef5SHugues Fruchet 		ret = ov5640_set_gain(sensor, ctrls->gain->val);
262219a81c14SSteve Longerbeam 
262319a81c14SSteve Longerbeam 	return ret;
262419a81c14SSteve Longerbeam }
262519a81c14SSteve Longerbeam 
26269f6d7bacSChen-Yu Tsai static const char * const test_pattern_menu[] = {
26279f6d7bacSChen-Yu Tsai 	"Disabled",
26289f6d7bacSChen-Yu Tsai 	"Color bars",
2629bddc5cdfSChen-Yu Tsai 	"Color bars w/ rolling bar",
2630bddc5cdfSChen-Yu Tsai 	"Color squares",
2631bddc5cdfSChen-Yu Tsai 	"Color squares w/ rolling bar",
26329f6d7bacSChen-Yu Tsai };
26339f6d7bacSChen-Yu Tsai 
2634a0c29afbSChen-Yu Tsai #define OV5640_TEST_ENABLE		BIT(7)
2635a0c29afbSChen-Yu Tsai #define OV5640_TEST_ROLLING		BIT(6)	/* rolling horizontal bar */
2636a0c29afbSChen-Yu Tsai #define OV5640_TEST_TRANSPARENT		BIT(5)
2637a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE_BW		BIT(4)	/* black & white squares */
2638a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_STANDARD	(0 << 2)
2639a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_1	(1 << 2)
2640a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_HOR_CHANGE	(2 << 2)
2641a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR_VERT_CHANGE_2	(3 << 2)
2642a0c29afbSChen-Yu Tsai #define OV5640_TEST_BAR			(0 << 0)
2643a0c29afbSChen-Yu Tsai #define OV5640_TEST_RANDOM		(1 << 0)
2644a0c29afbSChen-Yu Tsai #define OV5640_TEST_SQUARE		(2 << 0)
2645a0c29afbSChen-Yu Tsai #define OV5640_TEST_BLACK		(3 << 0)
2646a0c29afbSChen-Yu Tsai 
2647a0c29afbSChen-Yu Tsai static const u8 test_pattern_val[] = {
2648a0c29afbSChen-Yu Tsai 	0,
26492aff1fc3SChen-Yu Tsai 	OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
2650a0c29afbSChen-Yu Tsai 		OV5640_TEST_BAR,
2651bddc5cdfSChen-Yu Tsai 	OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
2652bddc5cdfSChen-Yu Tsai 		OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
2653bddc5cdfSChen-Yu Tsai 	OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
2654bddc5cdfSChen-Yu Tsai 	OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
2655a0c29afbSChen-Yu Tsai };
2656a0c29afbSChen-Yu Tsai 
265719a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
265819a81c14SSteve Longerbeam {
2659a0c29afbSChen-Yu Tsai 	return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2660a0c29afbSChen-Yu Tsai 				test_pattern_val[value]);
266119a81c14SSteve Longerbeam }
266219a81c14SSteve Longerbeam 
26631068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
26641068fecaSMylène Josserand {
26651068fecaSMylène Josserand 	int ret;
26661068fecaSMylène Josserand 
26671068fecaSMylène Josserand 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
26681068fecaSMylène Josserand 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
26691068fecaSMylène Josserand 			     0 : BIT(7));
26701068fecaSMylène Josserand 	if (ret)
26711068fecaSMylène Josserand 		return ret;
26721068fecaSMylène Josserand 
26731068fecaSMylène Josserand 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
26741068fecaSMylène Josserand 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
26751068fecaSMylène Josserand 			      BIT(2) : 0);
26761068fecaSMylène Josserand }
26771068fecaSMylène Josserand 
2678ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2679ce85705aSHugues Fruchet {
2680ce85705aSHugues Fruchet 	/*
2681c3f3ba3eSHugues Fruchet 	 * If sensor is mounted upside down, mirror logic is inversed.
2682c3f3ba3eSHugues Fruchet 	 *
2683ce85705aSHugues Fruchet 	 * Sensor is a BSI (Back Side Illuminated) one,
2684ce85705aSHugues Fruchet 	 * so image captured is physically mirrored.
2685ce85705aSHugues Fruchet 	 * This is why mirror logic is inversed in
2686ce85705aSHugues Fruchet 	 * order to cancel this mirror effect.
2687ce85705aSHugues Fruchet 	 */
2688ce85705aSHugues Fruchet 
2689ce85705aSHugues Fruchet 	/*
2690ce85705aSHugues Fruchet 	 * TIMING TC REG21:
2691ce85705aSHugues Fruchet 	 * - [2]:	ISP mirror
2692ce85705aSHugues Fruchet 	 * - [1]:	Sensor mirror
2693ce85705aSHugues Fruchet 	 */
2694ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2695ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2696c3f3ba3eSHugues Fruchet 			      (!(value ^ sensor->upside_down)) ?
2697c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2698ce85705aSHugues Fruchet }
2699ce85705aSHugues Fruchet 
2700ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2701ce85705aSHugues Fruchet {
2702c3f3ba3eSHugues Fruchet 	/* If sensor is mounted upside down, flip logic is inversed */
2703c3f3ba3eSHugues Fruchet 
2704ce85705aSHugues Fruchet 	/*
2705ce85705aSHugues Fruchet 	 * TIMING TC REG20:
2706ce85705aSHugues Fruchet 	 * - [2]:	ISP vflip
2707ce85705aSHugues Fruchet 	 * - [1]:	Sensor vflip
2708ce85705aSHugues Fruchet 	 */
2709ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2710ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2711c3f3ba3eSHugues Fruchet 			      (value ^ sensor->upside_down) ?
2712c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2713ce85705aSHugues Fruchet }
2714ce85705aSHugues Fruchet 
271519a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
271619a81c14SSteve Longerbeam {
271719a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
271819a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
271919a81c14SSteve Longerbeam 	int val;
272019a81c14SSteve Longerbeam 
272119a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
272219a81c14SSteve Longerbeam 
272319a81c14SSteve Longerbeam 	switch (ctrl->id) {
272419a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
272519a81c14SSteve Longerbeam 		val = ov5640_get_gain(sensor);
272619a81c14SSteve Longerbeam 		if (val < 0)
272719a81c14SSteve Longerbeam 			return val;
272819a81c14SSteve Longerbeam 		sensor->ctrls.gain->val = val;
272919a81c14SSteve Longerbeam 		break;
273019a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
273119a81c14SSteve Longerbeam 		val = ov5640_get_exposure(sensor);
273219a81c14SSteve Longerbeam 		if (val < 0)
273319a81c14SSteve Longerbeam 			return val;
273419a81c14SSteve Longerbeam 		sensor->ctrls.exposure->val = val;
273519a81c14SSteve Longerbeam 		break;
273619a81c14SSteve Longerbeam 	}
273719a81c14SSteve Longerbeam 
273819a81c14SSteve Longerbeam 	return 0;
273919a81c14SSteve Longerbeam }
274019a81c14SSteve Longerbeam 
274119a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
274219a81c14SSteve Longerbeam {
274319a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
274419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
274519a81c14SSteve Longerbeam 	int ret;
274619a81c14SSteve Longerbeam 
274719a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
274819a81c14SSteve Longerbeam 
274919a81c14SSteve Longerbeam 	/*
275019a81c14SSteve Longerbeam 	 * If the device is not powered up by the host driver do
275119a81c14SSteve Longerbeam 	 * not apply any controls to H/W at this time. Instead
275219a81c14SSteve Longerbeam 	 * the controls will be restored right after power-up.
275319a81c14SSteve Longerbeam 	 */
275419a81c14SSteve Longerbeam 	if (sensor->power_count == 0)
275519a81c14SSteve Longerbeam 		return 0;
275619a81c14SSteve Longerbeam 
275719a81c14SSteve Longerbeam 	switch (ctrl->id) {
275819a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
275919a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
276019a81c14SSteve Longerbeam 		break;
276119a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
276219a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
276319a81c14SSteve Longerbeam 		break;
276419a81c14SSteve Longerbeam 	case V4L2_CID_AUTO_WHITE_BALANCE:
276519a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
276619a81c14SSteve Longerbeam 		break;
276719a81c14SSteve Longerbeam 	case V4L2_CID_HUE:
276819a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
276919a81c14SSteve Longerbeam 		break;
277019a81c14SSteve Longerbeam 	case V4L2_CID_CONTRAST:
277119a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
277219a81c14SSteve Longerbeam 		break;
277319a81c14SSteve Longerbeam 	case V4L2_CID_SATURATION:
277419a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
277519a81c14SSteve Longerbeam 		break;
277619a81c14SSteve Longerbeam 	case V4L2_CID_TEST_PATTERN:
277719a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
277819a81c14SSteve Longerbeam 		break;
27791068fecaSMylène Josserand 	case V4L2_CID_POWER_LINE_FREQUENCY:
27801068fecaSMylène Josserand 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
27811068fecaSMylène Josserand 		break;
2782ce85705aSHugues Fruchet 	case V4L2_CID_HFLIP:
2783ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2784ce85705aSHugues Fruchet 		break;
2785ce85705aSHugues Fruchet 	case V4L2_CID_VFLIP:
2786ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2787ce85705aSHugues Fruchet 		break;
278819a81c14SSteve Longerbeam 	default:
278919a81c14SSteve Longerbeam 		ret = -EINVAL;
279019a81c14SSteve Longerbeam 		break;
279119a81c14SSteve Longerbeam 	}
279219a81c14SSteve Longerbeam 
279319a81c14SSteve Longerbeam 	return ret;
279419a81c14SSteve Longerbeam }
279519a81c14SSteve Longerbeam 
279619a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
279719a81c14SSteve Longerbeam 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
279819a81c14SSteve Longerbeam 	.s_ctrl = ov5640_s_ctrl,
279919a81c14SSteve Longerbeam };
280019a81c14SSteve Longerbeam 
280119a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor)
280219a81c14SSteve Longerbeam {
280322845bf2SJacopo Mondi 	const struct ov5640_mode_info *mode = sensor->current_mode;
280419a81c14SSteve Longerbeam 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
280519a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
280619a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
280719a81c14SSteve Longerbeam 	int ret;
280819a81c14SSteve Longerbeam 
280919a81c14SSteve Longerbeam 	v4l2_ctrl_handler_init(hdl, 32);
281019a81c14SSteve Longerbeam 
281119a81c14SSteve Longerbeam 	/* we can use our own mutex for the ctrl lock */
281219a81c14SSteve Longerbeam 	hdl->lock = &sensor->lock;
281319a81c14SSteve Longerbeam 
2814cc196e48SBenoit Parrot 	/* Clock related controls */
2815cc196e48SBenoit Parrot 	ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
281622845bf2SJacopo Mondi 			      ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
281722845bf2SJacopo Mondi 			      ov5640_pixel_rates[0], 1,
281822845bf2SJacopo Mondi 			      ov5640_pixel_rates[mode->pixel_rate]);
2819cc196e48SBenoit Parrot 
282019a81c14SSteve Longerbeam 	/* Auto/manual white balance */
282119a81c14SSteve Longerbeam 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
282219a81c14SSteve Longerbeam 					   V4L2_CID_AUTO_WHITE_BALANCE,
282319a81c14SSteve Longerbeam 					   0, 1, 1, 1);
282419a81c14SSteve Longerbeam 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
282519a81c14SSteve Longerbeam 						0, 4095, 1, 0);
282619a81c14SSteve Longerbeam 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
282719a81c14SSteve Longerbeam 					       0, 4095, 1, 0);
282819a81c14SSteve Longerbeam 	/* Auto/manual exposure */
282919a81c14SSteve Longerbeam 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
283019a81c14SSteve Longerbeam 						 V4L2_CID_EXPOSURE_AUTO,
283119a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_MANUAL, 0,
283219a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_AUTO);
283319a81c14SSteve Longerbeam 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
283419a81c14SSteve Longerbeam 					    0, 65535, 1, 0);
283519a81c14SSteve Longerbeam 	/* Auto/manual gain */
283619a81c14SSteve Longerbeam 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
283719a81c14SSteve Longerbeam 					     0, 1, 1, 1);
283819a81c14SSteve Longerbeam 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
283919a81c14SSteve Longerbeam 					0, 1023, 1, 0);
284019a81c14SSteve Longerbeam 
284119a81c14SSteve Longerbeam 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
284219a81c14SSteve Longerbeam 					      0, 255, 1, 64);
284319a81c14SSteve Longerbeam 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
284419a81c14SSteve Longerbeam 				       0, 359, 1, 0);
284519a81c14SSteve Longerbeam 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
284619a81c14SSteve Longerbeam 					    0, 255, 1, 0);
284719a81c14SSteve Longerbeam 	ctrls->test_pattern =
284819a81c14SSteve Longerbeam 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
284919a81c14SSteve Longerbeam 					     ARRAY_SIZE(test_pattern_menu) - 1,
285019a81c14SSteve Longerbeam 					     0, 0, test_pattern_menu);
2851ce85705aSHugues Fruchet 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2852ce85705aSHugues Fruchet 					 0, 1, 1, 0);
2853ce85705aSHugues Fruchet 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2854ce85705aSHugues Fruchet 					 0, 1, 1, 0);
285519a81c14SSteve Longerbeam 
28561068fecaSMylène Josserand 	ctrls->light_freq =
28571068fecaSMylène Josserand 		v4l2_ctrl_new_std_menu(hdl, ops,
28581068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY,
28591068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
28601068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
28611068fecaSMylène Josserand 
286219a81c14SSteve Longerbeam 	if (hdl->error) {
286319a81c14SSteve Longerbeam 		ret = hdl->error;
286419a81c14SSteve Longerbeam 		goto free_ctrls;
286519a81c14SSteve Longerbeam 	}
286619a81c14SSteve Longerbeam 
2867cc196e48SBenoit Parrot 	ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
286819a81c14SSteve Longerbeam 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
286919a81c14SSteve Longerbeam 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
287019a81c14SSteve Longerbeam 
287119a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
287219a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
287319a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
287419a81c14SSteve Longerbeam 
287519a81c14SSteve Longerbeam 	sensor->sd.ctrl_handler = hdl;
287619a81c14SSteve Longerbeam 	return 0;
287719a81c14SSteve Longerbeam 
287819a81c14SSteve Longerbeam free_ctrls:
287919a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(hdl);
288019a81c14SSteve Longerbeam 	return ret;
288119a81c14SSteve Longerbeam }
288219a81c14SSteve Longerbeam 
288319a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
28840d346d2aSTomi Valkeinen 				  struct v4l2_subdev_state *sd_state,
288519a81c14SSteve Longerbeam 				  struct v4l2_subdev_frame_size_enum *fse)
288619a81c14SSteve Longerbeam {
288719a81c14SSteve Longerbeam 	if (fse->pad != 0)
288819a81c14SSteve Longerbeam 		return -EINVAL;
288919a81c14SSteve Longerbeam 	if (fse->index >= OV5640_NUM_MODES)
289019a81c14SSteve Longerbeam 		return -EINVAL;
289119a81c14SSteve Longerbeam 
289241d8d7f5SHugues Fruchet 	fse->min_width =
2893086c25f8SMaxime Ripard 		ov5640_mode_data[fse->index].hact;
289441d8d7f5SHugues Fruchet 	fse->max_width = fse->min_width;
289541d8d7f5SHugues Fruchet 	fse->min_height =
2896086c25f8SMaxime Ripard 		ov5640_mode_data[fse->index].vact;
289741d8d7f5SHugues Fruchet 	fse->max_height = fse->min_height;
289819a81c14SSteve Longerbeam 
289919a81c14SSteve Longerbeam 	return 0;
290019a81c14SSteve Longerbeam }
290119a81c14SSteve Longerbeam 
290219a81c14SSteve Longerbeam static int ov5640_enum_frame_interval(
290319a81c14SSteve Longerbeam 	struct v4l2_subdev *sd,
29040d346d2aSTomi Valkeinen 	struct v4l2_subdev_state *sd_state,
290519a81c14SSteve Longerbeam 	struct v4l2_subdev_frame_interval_enum *fie)
290619a81c14SSteve Longerbeam {
290719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
290819a81c14SSteve Longerbeam 	struct v4l2_fract tpf;
290919a81c14SSteve Longerbeam 	int ret;
291019a81c14SSteve Longerbeam 
291119a81c14SSteve Longerbeam 	if (fie->pad != 0)
291219a81c14SSteve Longerbeam 		return -EINVAL;
291319a81c14SSteve Longerbeam 	if (fie->index >= OV5640_NUM_FRAMERATES)
291419a81c14SSteve Longerbeam 		return -EINVAL;
291519a81c14SSteve Longerbeam 
291619a81c14SSteve Longerbeam 	tpf.numerator = 1;
291719a81c14SSteve Longerbeam 	tpf.denominator = ov5640_framerates[fie->index];
291819a81c14SSteve Longerbeam 
291919a81c14SSteve Longerbeam 	ret = ov5640_try_frame_interval(sensor, &tpf,
292019a81c14SSteve Longerbeam 					fie->width, fie->height);
292119a81c14SSteve Longerbeam 	if (ret < 0)
292219a81c14SSteve Longerbeam 		return -EINVAL;
292319a81c14SSteve Longerbeam 
292419a81c14SSteve Longerbeam 	fie->interval = tpf;
292519a81c14SSteve Longerbeam 	return 0;
292619a81c14SSteve Longerbeam }
292719a81c14SSteve Longerbeam 
292819a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
292919a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
293019a81c14SSteve Longerbeam {
293119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
293219a81c14SSteve Longerbeam 
293319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
293419a81c14SSteve Longerbeam 	fi->interval = sensor->frame_interval;
293519a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
293619a81c14SSteve Longerbeam 
293719a81c14SSteve Longerbeam 	return 0;
293819a81c14SSteve Longerbeam }
293919a81c14SSteve Longerbeam 
294019a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
294119a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
294219a81c14SSteve Longerbeam {
294319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
294419a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
294519a81c14SSteve Longerbeam 	int frame_rate, ret = 0;
294619a81c14SSteve Longerbeam 
294719a81c14SSteve Longerbeam 	if (fi->pad != 0)
294819a81c14SSteve Longerbeam 		return -EINVAL;
294919a81c14SSteve Longerbeam 
295019a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
295119a81c14SSteve Longerbeam 
295219a81c14SSteve Longerbeam 	if (sensor->streaming) {
295319a81c14SSteve Longerbeam 		ret = -EBUSY;
295419a81c14SSteve Longerbeam 		goto out;
295519a81c14SSteve Longerbeam 	}
295619a81c14SSteve Longerbeam 
295719a81c14SSteve Longerbeam 	mode = sensor->current_mode;
295819a81c14SSteve Longerbeam 
295919a81c14SSteve Longerbeam 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2960dba13a0bSMaxime Ripard 					       mode->hact, mode->vact);
2961e823fb16SMaxime Ripard 	if (frame_rate < 0) {
2962e823fb16SMaxime Ripard 		/* Always return a valid frame interval value */
2963e823fb16SMaxime Ripard 		fi->interval = sensor->frame_interval;
2964e823fb16SMaxime Ripard 		goto out;
2965e823fb16SMaxime Ripard 	}
296619a81c14SSteve Longerbeam 
29673c4a7372SHugues Fruchet 	mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2968dba13a0bSMaxime Ripard 				mode->vact, true);
29693c4a7372SHugues Fruchet 	if (!mode) {
29703c4a7372SHugues Fruchet 		ret = -EINVAL;
29713c4a7372SHugues Fruchet 		goto out;
29723c4a7372SHugues Fruchet 	}
29733c4a7372SHugues Fruchet 
29740929983eSHugues Fruchet 	if (mode != sensor->current_mode ||
29750929983eSHugues Fruchet 	    frame_rate != sensor->current_fr) {
29760929983eSHugues Fruchet 		sensor->current_fr = frame_rate;
29770929983eSHugues Fruchet 		sensor->frame_interval = fi->interval;
29783c4a7372SHugues Fruchet 		sensor->current_mode = mode;
297919a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
2980cc196e48SBenoit Parrot 
2981cc196e48SBenoit Parrot 		__v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2982cc196e48SBenoit Parrot 					 ov5640_calc_pixel_rate(sensor));
29836949d864SHugues Fruchet 	}
298419a81c14SSteve Longerbeam out:
298519a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
298619a81c14SSteve Longerbeam 	return ret;
298719a81c14SSteve Longerbeam }
298819a81c14SSteve Longerbeam 
298919a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
29900d346d2aSTomi Valkeinen 				 struct v4l2_subdev_state *sd_state,
299119a81c14SSteve Longerbeam 				 struct v4l2_subdev_mbus_code_enum *code)
299219a81c14SSteve Longerbeam {
299319a81c14SSteve Longerbeam 	if (code->pad != 0)
299419a81c14SSteve Longerbeam 		return -EINVAL;
2995e3ee691dSHugues Fruchet 	if (code->index >= ARRAY_SIZE(ov5640_formats))
299619a81c14SSteve Longerbeam 		return -EINVAL;
299719a81c14SSteve Longerbeam 
2998e3ee691dSHugues Fruchet 	code->code = ov5640_formats[code->index].code;
299919a81c14SSteve Longerbeam 	return 0;
300019a81c14SSteve Longerbeam }
300119a81c14SSteve Longerbeam 
300219a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
300319a81c14SSteve Longerbeam {
300419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
300519a81c14SSteve Longerbeam 	int ret = 0;
300619a81c14SSteve Longerbeam 
300719a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
300819a81c14SSteve Longerbeam 
300919a81c14SSteve Longerbeam 	if (sensor->streaming == !enable) {
301019a81c14SSteve Longerbeam 		if (enable && sensor->pending_mode_change) {
3011985cdcb0SHugues Fruchet 			ret = ov5640_set_mode(sensor);
301219a81c14SSteve Longerbeam 			if (ret)
301319a81c14SSteve Longerbeam 				goto out;
3014fb98e29fSHugues Fruchet 		}
3015e3ee691dSHugues Fruchet 
3016fb98e29fSHugues Fruchet 		if (enable && sensor->pending_fmt_change) {
3017e3ee691dSHugues Fruchet 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
3018e3ee691dSHugues Fruchet 			if (ret)
3019e3ee691dSHugues Fruchet 				goto out;
3020fb98e29fSHugues Fruchet 			sensor->pending_fmt_change = false;
302119a81c14SSteve Longerbeam 		}
302219a81c14SSteve Longerbeam 
30232d95e7edSSakari Ailus 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
3024f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, enable);
3025f22996dbSHugues Fruchet 		else
3026f22996dbSHugues Fruchet 			ret = ov5640_set_stream_dvp(sensor, enable);
3027f22996dbSHugues Fruchet 
302819a81c14SSteve Longerbeam 		if (!ret)
302919a81c14SSteve Longerbeam 			sensor->streaming = enable;
303019a81c14SSteve Longerbeam 	}
303119a81c14SSteve Longerbeam out:
303219a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
303319a81c14SSteve Longerbeam 	return ret;
303419a81c14SSteve Longerbeam }
303519a81c14SSteve Longerbeam 
303619a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = {
303719a81c14SSteve Longerbeam 	.s_power = ov5640_s_power,
30382d18fbc5SAkinobu Mita 	.log_status = v4l2_ctrl_subdev_log_status,
30392d18fbc5SAkinobu Mita 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
30402d18fbc5SAkinobu Mita 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
304119a81c14SSteve Longerbeam };
304219a81c14SSteve Longerbeam 
304319a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = {
304419a81c14SSteve Longerbeam 	.g_frame_interval = ov5640_g_frame_interval,
304519a81c14SSteve Longerbeam 	.s_frame_interval = ov5640_s_frame_interval,
304619a81c14SSteve Longerbeam 	.s_stream = ov5640_s_stream,
304719a81c14SSteve Longerbeam };
304819a81c14SSteve Longerbeam 
304919a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
305019a81c14SSteve Longerbeam 	.enum_mbus_code = ov5640_enum_mbus_code,
305119a81c14SSteve Longerbeam 	.get_fmt = ov5640_get_fmt,
305219a81c14SSteve Longerbeam 	.set_fmt = ov5640_set_fmt,
305319a81c14SSteve Longerbeam 	.enum_frame_size = ov5640_enum_frame_size,
305419a81c14SSteve Longerbeam 	.enum_frame_interval = ov5640_enum_frame_interval,
305519a81c14SSteve Longerbeam };
305619a81c14SSteve Longerbeam 
305719a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = {
305819a81c14SSteve Longerbeam 	.core = &ov5640_core_ops,
305919a81c14SSteve Longerbeam 	.video = &ov5640_video_ops,
306019a81c14SSteve Longerbeam 	.pad = &ov5640_pad_ops,
306119a81c14SSteve Longerbeam };
306219a81c14SSteve Longerbeam 
306319a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor)
306419a81c14SSteve Longerbeam {
306519a81c14SSteve Longerbeam 	int i;
306619a81c14SSteve Longerbeam 
306719a81c14SSteve Longerbeam 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
306819a81c14SSteve Longerbeam 		sensor->supplies[i].supply = ov5640_supply_name[i];
306919a81c14SSteve Longerbeam 
307019a81c14SSteve Longerbeam 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
307119a81c14SSteve Longerbeam 				       OV5640_NUM_SUPPLIES,
307219a81c14SSteve Longerbeam 				       sensor->supplies);
307319a81c14SSteve Longerbeam }
307419a81c14SSteve Longerbeam 
30750f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor)
30760f7acb52SHugues Fruchet {
30770f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
30780f7acb52SHugues Fruchet 	int ret = 0;
30790f7acb52SHugues Fruchet 	u16 chip_id;
30800f7acb52SHugues Fruchet 
30810f7acb52SHugues Fruchet 	ret = ov5640_set_power_on(sensor);
30820f7acb52SHugues Fruchet 	if (ret)
30830f7acb52SHugues Fruchet 		return ret;
30840f7acb52SHugues Fruchet 
30850f7acb52SHugues Fruchet 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
30860f7acb52SHugues Fruchet 	if (ret) {
30870f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
30880f7acb52SHugues Fruchet 			__func__);
30890f7acb52SHugues Fruchet 		goto power_off;
30900f7acb52SHugues Fruchet 	}
30910f7acb52SHugues Fruchet 
30920f7acb52SHugues Fruchet 	if (chip_id != 0x5640) {
30930f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
30940f7acb52SHugues Fruchet 			__func__, chip_id);
30950f7acb52SHugues Fruchet 		ret = -ENXIO;
30960f7acb52SHugues Fruchet 	}
30970f7acb52SHugues Fruchet 
30980f7acb52SHugues Fruchet power_off:
30990f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
31000f7acb52SHugues Fruchet 	return ret;
31010f7acb52SHugues Fruchet }
31020f7acb52SHugues Fruchet 
3103e6714993SKieran Bingham static int ov5640_probe(struct i2c_client *client)
310419a81c14SSteve Longerbeam {
310519a81c14SSteve Longerbeam 	struct device *dev = &client->dev;
310619a81c14SSteve Longerbeam 	struct fwnode_handle *endpoint;
310719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor;
3108e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *fmt;
3109c3f3ba3eSHugues Fruchet 	u32 rotation;
311019a81c14SSteve Longerbeam 	int ret;
311119a81c14SSteve Longerbeam 
311219a81c14SSteve Longerbeam 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
311319a81c14SSteve Longerbeam 	if (!sensor)
311419a81c14SSteve Longerbeam 		return -ENOMEM;
311519a81c14SSteve Longerbeam 
311619a81c14SSteve Longerbeam 	sensor->i2c_client = client;
3117fb98e29fSHugues Fruchet 
3118fb98e29fSHugues Fruchet 	/*
3119fb98e29fSHugues Fruchet 	 * default init sequence initialize sensor to
3120fb98e29fSHugues Fruchet 	 * YUV422 UYVY VGA@30fps
3121fb98e29fSHugues Fruchet 	 */
3122e6441fdeSHugues Fruchet 	fmt = &sensor->fmt;
3123fb98e29fSHugues Fruchet 	fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
3124fb98e29fSHugues Fruchet 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
3125e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
3126e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
3127e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
3128e6441fdeSHugues Fruchet 	fmt->width = 640;
3129e6441fdeSHugues Fruchet 	fmt->height = 480;
3130e6441fdeSHugues Fruchet 	fmt->field = V4L2_FIELD_NONE;
313119a81c14SSteve Longerbeam 	sensor->frame_interval.numerator = 1;
313219a81c14SSteve Longerbeam 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
313319a81c14SSteve Longerbeam 	sensor->current_fr = OV5640_30_FPS;
313419a81c14SSteve Longerbeam 	sensor->current_mode =
3135086c25f8SMaxime Ripard 		&ov5640_mode_data[OV5640_MODE_VGA_640_480];
3136985cdcb0SHugues Fruchet 	sensor->last_mode = sensor->current_mode;
313719a81c14SSteve Longerbeam 
313819a81c14SSteve Longerbeam 	sensor->ae_target = 52;
313919a81c14SSteve Longerbeam 
3140c3f3ba3eSHugues Fruchet 	/* optional indication of physical rotation of sensor */
3141c3f3ba3eSHugues Fruchet 	ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3142c3f3ba3eSHugues Fruchet 				       &rotation);
3143c3f3ba3eSHugues Fruchet 	if (!ret) {
3144c3f3ba3eSHugues Fruchet 		switch (rotation) {
3145c3f3ba3eSHugues Fruchet 		case 180:
3146c3f3ba3eSHugues Fruchet 			sensor->upside_down = true;
31471771e9fbSGustavo A. R. Silva 			fallthrough;
3148c3f3ba3eSHugues Fruchet 		case 0:
3149c3f3ba3eSHugues Fruchet 			break;
3150c3f3ba3eSHugues Fruchet 		default:
3151c3f3ba3eSHugues Fruchet 			dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3152c3f3ba3eSHugues Fruchet 				 rotation);
3153c3f3ba3eSHugues Fruchet 		}
3154c3f3ba3eSHugues Fruchet 	}
3155c3f3ba3eSHugues Fruchet 
3156ce96bcf5SSakari Ailus 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3157ce96bcf5SSakari Ailus 						  NULL);
315819a81c14SSteve Longerbeam 	if (!endpoint) {
315919a81c14SSteve Longerbeam 		dev_err(dev, "endpoint node not found\n");
316019a81c14SSteve Longerbeam 		return -EINVAL;
316119a81c14SSteve Longerbeam 	}
316219a81c14SSteve Longerbeam 
316319a81c14SSteve Longerbeam 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
316419a81c14SSteve Longerbeam 	fwnode_handle_put(endpoint);
316519a81c14SSteve Longerbeam 	if (ret) {
316619a81c14SSteve Longerbeam 		dev_err(dev, "Could not parse endpoint\n");
316719a81c14SSteve Longerbeam 		return ret;
316819a81c14SSteve Longerbeam 	}
316919a81c14SSteve Longerbeam 
31702c61e48dSLad Prabhakar 	if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
31712c61e48dSLad Prabhakar 	    sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
31722c61e48dSLad Prabhakar 	    sensor->ep.bus_type != V4L2_MBUS_BT656) {
31732c61e48dSLad Prabhakar 		dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
31742c61e48dSLad Prabhakar 		return -EINVAL;
31752c61e48dSLad Prabhakar 	}
31762c61e48dSLad Prabhakar 
317719a81c14SSteve Longerbeam 	/* get system clock (xclk) */
317819a81c14SSteve Longerbeam 	sensor->xclk = devm_clk_get(dev, "xclk");
317919a81c14SSteve Longerbeam 	if (IS_ERR(sensor->xclk)) {
318019a81c14SSteve Longerbeam 		dev_err(dev, "failed to get xclk\n");
318119a81c14SSteve Longerbeam 		return PTR_ERR(sensor->xclk);
318219a81c14SSteve Longerbeam 	}
318319a81c14SSteve Longerbeam 
318419a81c14SSteve Longerbeam 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
318519a81c14SSteve Longerbeam 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
318619a81c14SSteve Longerbeam 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
318719a81c14SSteve Longerbeam 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
318819a81c14SSteve Longerbeam 			sensor->xclk_freq);
318919a81c14SSteve Longerbeam 		return -EINVAL;
319019a81c14SSteve Longerbeam 	}
319119a81c14SSteve Longerbeam 
319219a81c14SSteve Longerbeam 	/* request optional power down pin */
319319a81c14SSteve Longerbeam 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
319419a81c14SSteve Longerbeam 						    GPIOD_OUT_HIGH);
31958791a102SFabio Estevam 	if (IS_ERR(sensor->pwdn_gpio))
31968791a102SFabio Estevam 		return PTR_ERR(sensor->pwdn_gpio);
31978791a102SFabio Estevam 
319819a81c14SSteve Longerbeam 	/* request optional reset pin */
319919a81c14SSteve Longerbeam 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
320019a81c14SSteve Longerbeam 						     GPIOD_OUT_HIGH);
32018791a102SFabio Estevam 	if (IS_ERR(sensor->reset_gpio))
32028791a102SFabio Estevam 		return PTR_ERR(sensor->reset_gpio);
320319a81c14SSteve Longerbeam 
320419a81c14SSteve Longerbeam 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
320519a81c14SSteve Longerbeam 
32062d18fbc5SAkinobu Mita 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
32072d18fbc5SAkinobu Mita 			    V4L2_SUBDEV_FL_HAS_EVENTS;
320819a81c14SSteve Longerbeam 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
320919a81c14SSteve Longerbeam 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
321019a81c14SSteve Longerbeam 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
321119a81c14SSteve Longerbeam 	if (ret)
321219a81c14SSteve Longerbeam 		return ret;
321319a81c14SSteve Longerbeam 
321419a81c14SSteve Longerbeam 	ret = ov5640_get_regulators(sensor);
321519a81c14SSteve Longerbeam 	if (ret)
321619a81c14SSteve Longerbeam 		return ret;
321719a81c14SSteve Longerbeam 
321819a81c14SSteve Longerbeam 	mutex_init(&sensor->lock);
321919a81c14SSteve Longerbeam 
32200f7acb52SHugues Fruchet 	ret = ov5640_check_chip_id(sensor);
32210f7acb52SHugues Fruchet 	if (ret)
32220f7acb52SHugues Fruchet 		goto entity_cleanup;
32230f7acb52SHugues Fruchet 
322419a81c14SSteve Longerbeam 	ret = ov5640_init_controls(sensor);
322519a81c14SSteve Longerbeam 	if (ret)
322619a81c14SSteve Longerbeam 		goto entity_cleanup;
322719a81c14SSteve Longerbeam 
322815786f7bSSakari Ailus 	ret = v4l2_async_register_subdev_sensor(&sensor->sd);
322919a81c14SSteve Longerbeam 	if (ret)
323019a81c14SSteve Longerbeam 		goto free_ctrls;
323119a81c14SSteve Longerbeam 
323219a81c14SSteve Longerbeam 	return 0;
323319a81c14SSteve Longerbeam 
323419a81c14SSteve Longerbeam free_ctrls:
323519a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
323619a81c14SSteve Longerbeam entity_cleanup:
323719a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
3238bfcba38dSTomi Valkeinen 	mutex_destroy(&sensor->lock);
323919a81c14SSteve Longerbeam 	return ret;
324019a81c14SSteve Longerbeam }
324119a81c14SSteve Longerbeam 
324219a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client)
324319a81c14SSteve Longerbeam {
324419a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
324519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
324619a81c14SSteve Longerbeam 
324719a81c14SSteve Longerbeam 	v4l2_async_unregister_subdev(&sensor->sd);
324819a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
324919a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3250bfcba38dSTomi Valkeinen 	mutex_destroy(&sensor->lock);
325119a81c14SSteve Longerbeam 
325219a81c14SSteve Longerbeam 	return 0;
325319a81c14SSteve Longerbeam }
325419a81c14SSteve Longerbeam 
325519a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = {
325619a81c14SSteve Longerbeam 	{"ov5640", 0},
325719a81c14SSteve Longerbeam 	{},
325819a81c14SSteve Longerbeam };
325919a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id);
326019a81c14SSteve Longerbeam 
326119a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = {
326219a81c14SSteve Longerbeam 	{ .compatible = "ovti,ov5640" },
326319a81c14SSteve Longerbeam 	{ /* sentinel */ }
326419a81c14SSteve Longerbeam };
326519a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
326619a81c14SSteve Longerbeam 
326719a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = {
326819a81c14SSteve Longerbeam 	.driver = {
326919a81c14SSteve Longerbeam 		.name  = "ov5640",
327019a81c14SSteve Longerbeam 		.of_match_table	= ov5640_dt_ids,
327119a81c14SSteve Longerbeam 	},
327219a81c14SSteve Longerbeam 	.id_table = ov5640_id,
3273e6714993SKieran Bingham 	.probe_new = ov5640_probe,
327419a81c14SSteve Longerbeam 	.remove   = ov5640_remove,
327519a81c14SSteve Longerbeam };
327619a81c14SSteve Longerbeam 
327719a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver);
327819a81c14SSteve Longerbeam 
327919a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
328019a81c14SSteve Longerbeam MODULE_LICENSE("GPL");
3281