xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 2d18fbc5)
119a81c14SSteve Longerbeam /*
219a81c14SSteve Longerbeam  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
319a81c14SSteve Longerbeam  * Copyright (C) 2014-2017 Mentor Graphics Inc.
419a81c14SSteve Longerbeam  *
519a81c14SSteve Longerbeam  * This program is free software; you can redistribute it and/or modify
619a81c14SSteve Longerbeam  * it under the terms of the GNU General Public License as published by
719a81c14SSteve Longerbeam  * the Free Software Foundation; either version 2 of the License, or
819a81c14SSteve Longerbeam  * (at your option) any later version.
919a81c14SSteve Longerbeam  */
1019a81c14SSteve Longerbeam 
1119a81c14SSteve Longerbeam #include <linux/clk.h>
1219a81c14SSteve Longerbeam #include <linux/clk-provider.h>
1319a81c14SSteve Longerbeam #include <linux/clkdev.h>
1419a81c14SSteve Longerbeam #include <linux/ctype.h>
1519a81c14SSteve Longerbeam #include <linux/delay.h>
1619a81c14SSteve Longerbeam #include <linux/device.h>
1741d8d7f5SHugues Fruchet #include <linux/gpio/consumer.h>
1819a81c14SSteve Longerbeam #include <linux/i2c.h>
1919a81c14SSteve Longerbeam #include <linux/init.h>
2019a81c14SSteve Longerbeam #include <linux/module.h>
2119a81c14SSteve Longerbeam #include <linux/of_device.h>
2241d8d7f5SHugues Fruchet #include <linux/regulator/consumer.h>
2319a81c14SSteve Longerbeam #include <linux/slab.h>
2419a81c14SSteve Longerbeam #include <linux/types.h>
2519a81c14SSteve Longerbeam #include <media/v4l2-async.h>
2619a81c14SSteve Longerbeam #include <media/v4l2-ctrls.h>
2719a81c14SSteve Longerbeam #include <media/v4l2-device.h>
282d18fbc5SAkinobu Mita #include <media/v4l2-event.h>
2919a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h>
3019a81c14SSteve Longerbeam #include <media/v4l2-subdev.h>
3119a81c14SSteve Longerbeam 
3219a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */
3319a81c14SSteve Longerbeam #define OV5640_XCLK_MIN  6000000
3441cb1c73SPhilipp Puschmann #define OV5640_XCLK_MAX 54000000
3519a81c14SSteve Longerbeam 
3619a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c
3719a81c14SSteve Longerbeam 
38d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02		0x3002
39d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
40f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0		0x3008
4119a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID		0x300a
42f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00	0x300e
43f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
44f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
4519a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00		0x3019
46f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1	0x302e
4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0		0x3034
4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1		0x3035
4919a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2		0x3036
5019a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3		0x3037
5119a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID		0x3100
52f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
5319a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN		0x3400
5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN		0x3402
5619a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN		0x3404
5719a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL	0x3503
6219a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
6319a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS		0x350c
6486633417SMaxime Ripard #define OV5640_REG_TIMING_DVPHO		0x3808
6586633417SMaxime Ripard #define OV5640_REG_TIMING_DVPVO		0x380a
6619a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS		0x380c
6719a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS		0x380e
68ce85705aSHugues Fruchet #define OV5640_REG_TIMING_TC_REG20	0x3820
6919a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21	0x3821
7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00		0x3a00
7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP		0x3a08
7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP		0x3a0a
7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D		0x3a0d
7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E		0x3a0e
7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F		0x3a0f
7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10		0x3a10
7719a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11		0x3a11
7819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B		0x3a1b
7919a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E		0x3a1e
8019a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F		0x3a1f
8119a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00	0x3c00
8219a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01	0x3c01
8319a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
8419a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01		0x4202
85e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00	0x4300
86f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00	0x4740
8719a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00		0x4800
8819a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE		0x4814
89e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
9019a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
9119a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0		0x5580
9219a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1		0x5581
9319a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3		0x5583
9419a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4		0x5584
9519a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5		0x5585
9619a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT		0x56a1
9719a81c14SSteve Longerbeam 
988f57c2f8SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT	1
998f57c2f8SMaxime Ripard #define OV5640_SCLK_ROOT_DIVIDER_DEFAULT	2
1008f57c2f8SMaxime Ripard 
10119a81c14SSteve Longerbeam enum ov5640_mode_id {
10219a81c14SSteve Longerbeam 	OV5640_MODE_QCIF_176_144 = 0,
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,
11719a81c14SSteve Longerbeam 	OV5640_NUM_FRAMERATES,
11819a81c14SSteve Longerbeam };
11919a81c14SSteve Longerbeam 
120e3ee691dSHugues Fruchet struct ov5640_pixfmt {
121e3ee691dSHugues Fruchet 	u32 code;
122e3ee691dSHugues Fruchet 	u32 colorspace;
123e3ee691dSHugues Fruchet };
124e3ee691dSHugues Fruchet 
125e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = {
126d47c4126SHugues Fruchet 	{ MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
127e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
128e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
129e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
130e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
131e3ee691dSHugues Fruchet };
132e3ee691dSHugues Fruchet 
13319a81c14SSteve Longerbeam /*
13419a81c14SSteve Longerbeam  * FIXME: remove this when a subdev API becomes available
13519a81c14SSteve Longerbeam  * to set the MIPI CSI-2 virtual channel.
13619a81c14SSteve Longerbeam  */
13719a81c14SSteve Longerbeam static unsigned int virtual_channel;
1388670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444);
13919a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel,
14019a81c14SSteve Longerbeam 		 "MIPI CSI-2 virtual channel (0..3), default 0");
14119a81c14SSteve Longerbeam 
14219a81c14SSteve Longerbeam static const int ov5640_framerates[] = {
14319a81c14SSteve Longerbeam 	[OV5640_15_FPS] = 15,
14419a81c14SSteve Longerbeam 	[OV5640_30_FPS] = 30,
14519a81c14SSteve Longerbeam };
14619a81c14SSteve Longerbeam 
14719a81c14SSteve Longerbeam /* regulator supplies */
14819a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = {
14941d8d7f5SHugues Fruchet 	"DOVDD", /* Digital I/O (1.8V) supply */
15019a81c14SSteve Longerbeam 	"DVDD",  /* Digital Core (1.5V) supply */
15119a81c14SSteve Longerbeam 	"AVDD",  /* Analog (2.8V) supply */
15219a81c14SSteve Longerbeam };
15319a81c14SSteve Longerbeam 
15419a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
15519a81c14SSteve Longerbeam 
15619a81c14SSteve Longerbeam /*
15719a81c14SSteve Longerbeam  * Image size under 1280 * 960 are SUBSAMPLING
15819a81c14SSteve Longerbeam  * Image size upper 1280 * 960 are SCALING
15919a81c14SSteve Longerbeam  */
16019a81c14SSteve Longerbeam enum ov5640_downsize_mode {
16119a81c14SSteve Longerbeam 	SUBSAMPLING,
16219a81c14SSteve Longerbeam 	SCALING,
16319a81c14SSteve Longerbeam };
16419a81c14SSteve Longerbeam 
16519a81c14SSteve Longerbeam struct reg_value {
16619a81c14SSteve Longerbeam 	u16 reg_addr;
16719a81c14SSteve Longerbeam 	u8 val;
16819a81c14SSteve Longerbeam 	u8 mask;
16919a81c14SSteve Longerbeam 	u32 delay_ms;
17019a81c14SSteve Longerbeam };
17119a81c14SSteve Longerbeam 
17219a81c14SSteve Longerbeam struct ov5640_mode_info {
17319a81c14SSteve Longerbeam 	enum ov5640_mode_id id;
17419a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode;
175dba13a0bSMaxime Ripard 	u32 hact;
176476dec01SMaxime Ripard 	u32 htot;
177dba13a0bSMaxime Ripard 	u32 vact;
178476dec01SMaxime Ripard 	u32 vtot;
17919a81c14SSteve Longerbeam 	const struct reg_value *reg_data;
18019a81c14SSteve Longerbeam 	u32 reg_data_size;
18119a81c14SSteve Longerbeam };
18219a81c14SSteve Longerbeam 
18319a81c14SSteve Longerbeam struct ov5640_ctrls {
18419a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler handler;
18519a81c14SSteve Longerbeam 	struct {
18619a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_exp;
18719a81c14SSteve Longerbeam 		struct v4l2_ctrl *exposure;
18819a81c14SSteve Longerbeam 	};
18919a81c14SSteve Longerbeam 	struct {
19019a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_wb;
19119a81c14SSteve Longerbeam 		struct v4l2_ctrl *blue_balance;
19219a81c14SSteve Longerbeam 		struct v4l2_ctrl *red_balance;
19319a81c14SSteve Longerbeam 	};
19419a81c14SSteve Longerbeam 	struct {
19519a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_gain;
19619a81c14SSteve Longerbeam 		struct v4l2_ctrl *gain;
19719a81c14SSteve Longerbeam 	};
19819a81c14SSteve Longerbeam 	struct v4l2_ctrl *brightness;
1991068fecaSMylène Josserand 	struct v4l2_ctrl *light_freq;
20019a81c14SSteve Longerbeam 	struct v4l2_ctrl *saturation;
20119a81c14SSteve Longerbeam 	struct v4l2_ctrl *contrast;
20219a81c14SSteve Longerbeam 	struct v4l2_ctrl *hue;
20319a81c14SSteve Longerbeam 	struct v4l2_ctrl *test_pattern;
204ce85705aSHugues Fruchet 	struct v4l2_ctrl *hflip;
205ce85705aSHugues Fruchet 	struct v4l2_ctrl *vflip;
20619a81c14SSteve Longerbeam };
20719a81c14SSteve Longerbeam 
20819a81c14SSteve Longerbeam struct ov5640_dev {
20919a81c14SSteve Longerbeam 	struct i2c_client *i2c_client;
21019a81c14SSteve Longerbeam 	struct v4l2_subdev sd;
21119a81c14SSteve Longerbeam 	struct media_pad pad;
21219a81c14SSteve Longerbeam 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
21319a81c14SSteve Longerbeam 	struct clk *xclk; /* system clock to OV5640 */
21419a81c14SSteve Longerbeam 	u32 xclk_freq;
21519a81c14SSteve Longerbeam 
21619a81c14SSteve Longerbeam 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
21719a81c14SSteve Longerbeam 	struct gpio_desc *reset_gpio;
21819a81c14SSteve Longerbeam 	struct gpio_desc *pwdn_gpio;
219c3f3ba3eSHugues Fruchet 	bool   upside_down;
22019a81c14SSteve Longerbeam 
22119a81c14SSteve Longerbeam 	/* lock to protect all members below */
22219a81c14SSteve Longerbeam 	struct mutex lock;
22319a81c14SSteve Longerbeam 
22419a81c14SSteve Longerbeam 	int power_count;
22519a81c14SSteve Longerbeam 
22619a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt fmt;
227fb98e29fSHugues Fruchet 	bool pending_fmt_change;
22819a81c14SSteve Longerbeam 
22919a81c14SSteve Longerbeam 	const struct ov5640_mode_info *current_mode;
230985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *last_mode;
23119a81c14SSteve Longerbeam 	enum ov5640_frame_rate current_fr;
23219a81c14SSteve Longerbeam 	struct v4l2_fract frame_interval;
23319a81c14SSteve Longerbeam 
23419a81c14SSteve Longerbeam 	struct ov5640_ctrls ctrls;
23519a81c14SSteve Longerbeam 
23619a81c14SSteve Longerbeam 	u32 prev_sysclk, prev_hts;
23719a81c14SSteve Longerbeam 	u32 ae_low, ae_high, ae_target;
23819a81c14SSteve Longerbeam 
23919a81c14SSteve Longerbeam 	bool pending_mode_change;
24019a81c14SSteve Longerbeam 	bool streaming;
24119a81c14SSteve Longerbeam };
24219a81c14SSteve Longerbeam 
24319a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
24419a81c14SSteve Longerbeam {
24519a81c14SSteve Longerbeam 	return container_of(sd, struct ov5640_dev, sd);
24619a81c14SSteve Longerbeam }
24719a81c14SSteve Longerbeam 
24819a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
24919a81c14SSteve Longerbeam {
25019a81c14SSteve Longerbeam 	return &container_of(ctrl->handler, struct ov5640_dev,
25119a81c14SSteve Longerbeam 			     ctrls.handler)->sd;
25219a81c14SSteve Longerbeam }
25319a81c14SSteve Longerbeam 
25419a81c14SSteve Longerbeam /*
25519a81c14SSteve Longerbeam  * FIXME: all of these register tables are likely filled with
25619a81c14SSteve Longerbeam  * entries that set the register to their power-on default values,
25719a81c14SSteve Longerbeam  * and which are otherwise not touched by this driver. Those entries
25819a81c14SSteve Longerbeam  * should be identified and removed to speed register load time
25919a81c14SSteve Longerbeam  * over i2c.
26019a81c14SSteve Longerbeam  */
261fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */
26219a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
26319a81c14SSteve Longerbeam 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
26419a81c14SSteve Longerbeam 	{0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
26519a81c14SSteve Longerbeam 	{0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
2668f57c2f8SMaxime Ripard 	{0x3037, 0x13, 0, 0}, {0x3630, 0x36, 0, 0},
26719a81c14SSteve Longerbeam 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
26819a81c14SSteve Longerbeam 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
26919a81c14SSteve Longerbeam 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
27019a81c14SSteve Longerbeam 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
27119a81c14SSteve Longerbeam 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
27219a81c14SSteve Longerbeam 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
27319a81c14SSteve Longerbeam 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
27419a81c14SSteve Longerbeam 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
27519a81c14SSteve Longerbeam 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
27619a81c14SSteve Longerbeam 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
27719a81c14SSteve Longerbeam 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
27819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
27919a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
28019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
28119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
28219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
283476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
28419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
28519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
28619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
28719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
28819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
28919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
29019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
29119a81c14SSteve Longerbeam 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
292aa4bb8b8SJacopo Mondi 	{0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
29319a81c14SSteve Longerbeam 	{0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
29419a81c14SSteve Longerbeam 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
295aa4bb8b8SJacopo Mondi 	{0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
29619a81c14SSteve Longerbeam 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
29719a81c14SSteve Longerbeam 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
29819a81c14SSteve Longerbeam 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
29919a81c14SSteve Longerbeam 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
30019a81c14SSteve Longerbeam 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
30119a81c14SSteve Longerbeam 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
30219a81c14SSteve Longerbeam 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
30319a81c14SSteve Longerbeam 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
30419a81c14SSteve Longerbeam 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
30519a81c14SSteve Longerbeam 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
30619a81c14SSteve Longerbeam 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
30719a81c14SSteve Longerbeam 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
30819a81c14SSteve Longerbeam 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
30919a81c14SSteve Longerbeam 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
31019a81c14SSteve Longerbeam 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
31119a81c14SSteve Longerbeam 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
31219a81c14SSteve Longerbeam 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
31319a81c14SSteve Longerbeam 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
31419a81c14SSteve Longerbeam 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
31519a81c14SSteve Longerbeam 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
31619a81c14SSteve Longerbeam 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
31719a81c14SSteve Longerbeam 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
31819a81c14SSteve Longerbeam 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
31919a81c14SSteve Longerbeam 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
32019a81c14SSteve Longerbeam 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
32119a81c14SSteve Longerbeam 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
32219a81c14SSteve Longerbeam 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
32319a81c14SSteve Longerbeam 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
32419a81c14SSteve Longerbeam 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
32519a81c14SSteve Longerbeam 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
32619a81c14SSteve Longerbeam 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
32719a81c14SSteve Longerbeam 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
32819a81c14SSteve Longerbeam 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
32919a81c14SSteve Longerbeam 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
33019a81c14SSteve Longerbeam 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
33119a81c14SSteve Longerbeam 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
33219a81c14SSteve Longerbeam 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
33319a81c14SSteve Longerbeam 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
33419a81c14SSteve Longerbeam 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
33519a81c14SSteve Longerbeam 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
33619a81c14SSteve Longerbeam 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
33719a81c14SSteve Longerbeam 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
33819a81c14SSteve Longerbeam 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
33919a81c14SSteve Longerbeam 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
34019a81c14SSteve Longerbeam 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
34119a81c14SSteve Longerbeam 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
34219a81c14SSteve Longerbeam 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
34319a81c14SSteve Longerbeam 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
34419a81c14SSteve Longerbeam 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
34519a81c14SSteve Longerbeam 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
34619a81c14SSteve Longerbeam };
34719a81c14SSteve Longerbeam 
34819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
34919a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
35019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
351ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
35219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
35319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
35419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
355476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
35619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
35719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
35819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
35919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
36019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
36119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
36219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
36319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
36419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
36519a81c14SSteve Longerbeam };
36619a81c14SSteve Longerbeam 
36719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
36819a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
36919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
370ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
37119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
37219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
37319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
374476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
37519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
37619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
37719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
37819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
37919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
38019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
38119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
38219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
38319a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
38419a81c14SSteve Longerbeam };
38519a81c14SSteve Longerbeam 
38619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
38719a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
38819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
389ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
39019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
39119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
39219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
393476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
39419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
39519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
39619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
39719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
39819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
39919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
40019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
40119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
40219a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
40386633417SMaxime Ripard 	{0x3035, 0x12, 0, 0},
40419a81c14SSteve Longerbeam };
40519a81c14SSteve Longerbeam 
40619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
40719a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
40819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
409ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
41019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
41119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
41219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
413476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
41419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
41519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
41619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
41719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
41819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
41919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
42019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
42119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
42286633417SMaxime Ripard 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
42319a81c14SSteve Longerbeam };
42419a81c14SSteve Longerbeam 
42519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
42619a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
42719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
428ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
42919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
43019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
43119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
432476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
43319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
43419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
43519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
43619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
43719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
43819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
43919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
44019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
44119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
44219a81c14SSteve Longerbeam };
44319a81c14SSteve Longerbeam 
44419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
44519a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
44619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
447ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
44819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
44919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
45019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
451476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
45219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
45319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
45419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
45519a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
45619a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
45719a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
45819a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
45919a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
46019a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
46119a81c14SSteve Longerbeam };
46219a81c14SSteve Longerbeam 
46319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
46419a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
46519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
466ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
46719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
46819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
46919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
470476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
47119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
47219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
47319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
47419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
47519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
47619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
47719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
47819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
47919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
48019a81c14SSteve Longerbeam };
48141d8d7f5SHugues Fruchet 
48219a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
48319a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
48419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
485ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
48619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
48719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
48819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
489476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
49019a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
49119a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
49219a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
49319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
49419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
49519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
49619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
49719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
49819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
49919a81c14SSteve Longerbeam };
50019a81c14SSteve Longerbeam 
50119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
50219a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
50319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
504ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
50519a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
50619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
50719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
508476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
50919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
51019a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
51119a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
51219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
51319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
51419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
51519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
51619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
51719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
51819a81c14SSteve Longerbeam };
51919a81c14SSteve Longerbeam 
52019a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
52119a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
52219a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
523ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
52419a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
52519a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
52619a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
527476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
52819a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
52919a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
53019a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
53119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
53219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
53319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
53419a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
53519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
53619a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
53719a81c14SSteve Longerbeam };
53819a81c14SSteve Longerbeam 
53919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
54019a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
54119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
542ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
54319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
54419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
54519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
546476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
54719a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
54819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
54919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
55019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
55119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
55219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
55319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
55419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
55519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
55619a81c14SSteve Longerbeam };
55719a81c14SSteve Longerbeam 
55819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
55919a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
56019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
561ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
56219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
56319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
56419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
565476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
56619a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
56719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
56819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
56919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
57019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
57119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
57219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
57319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
57419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
57519a81c14SSteve Longerbeam };
57619a81c14SSteve Longerbeam 
57719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
57819a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
57919a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
58019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
581ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
58219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
58319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
58419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
585476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
58619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
58719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
58819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
58919a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
59019a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
59119a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
59219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
59319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
59419a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
59519a81c14SSteve Longerbeam 	{0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
59619a81c14SSteve Longerbeam };
59719a81c14SSteve Longerbeam 
59819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
59919a81c14SSteve Longerbeam 	{0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
60019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
601ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
60219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
60319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
60419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
605476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
60619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
60719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
60819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
60919a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
61019a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
61119a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
612b791187bSHugues Fruchet 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
61319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
61419a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
61519a81c14SSteve Longerbeam };
61619a81c14SSteve Longerbeam 
61719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
61819a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
61919a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
62019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
621ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
62219a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
62319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
62419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
625476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
62619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
62719a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
62819a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
62919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
63019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
63119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
63219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
63319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
63419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
63519a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
63619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
63719a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
63819a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
63986633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
640476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
64119a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
64219a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
64319a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
64419a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
64519a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
64619a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
64719a81c14SSteve Longerbeam 	{0x3503, 0, 0, 0},
64819a81c14SSteve Longerbeam };
64919a81c14SSteve Longerbeam 
65019a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
65119a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
65219a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
65319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
654ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
65519a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
65619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
65719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
658476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
65919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
66019a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
66119a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
66219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
66319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
66419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
66519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
66619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
66719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
66819a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
66919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
67019a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
67119a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
67286633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
673476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
67419a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
67519a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
67619a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
67719a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
67819a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
67919a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
68019a81c14SSteve Longerbeam };
68119a81c14SSteve Longerbeam 
68219a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
68319a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
68419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
685ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
68619a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
68719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
68819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
689476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
69019a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
69119a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
69219a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
69319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
69419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
69519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
69619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
69719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
69819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
69919a81c14SSteve Longerbeam };
70019a81c14SSteve Longerbeam 
70119a81c14SSteve Longerbeam /* power-on sensor init reg table */
70219a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = {
703476dec01SMaxime Ripard 	0, SUBSAMPLING, 640, 1896, 480, 984,
704476dec01SMaxime Ripard 	ov5640_init_setting_30fps_VGA,
70519a81c14SSteve Longerbeam 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
70619a81c14SSteve Longerbeam };
70719a81c14SSteve Longerbeam 
70819a81c14SSteve Longerbeam static const struct ov5640_mode_info
70919a81c14SSteve Longerbeam ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
71019a81c14SSteve Longerbeam 	{
711476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
712476dec01SMaxime Ripard 		 176, 1896, 144, 984,
71319a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QCIF_176_144,
71419a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
715476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
716476dec01SMaxime Ripard 		 320, 1896, 240, 984,
71719a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QVGA_320_240,
71819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
719476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
720476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
72119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_VGA_640_480,
72219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
723476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
724476dec01SMaxime Ripard 		 720, 1896, 480, 984,
72519a81c14SSteve Longerbeam 		 ov5640_setting_15fps_NTSC_720_480,
72619a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
727476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
728476dec01SMaxime Ripard 		 720, 1896, 576, 984,
72919a81c14SSteve Longerbeam 		 ov5640_setting_15fps_PAL_720_576,
73019a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
731476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
732476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
73319a81c14SSteve Longerbeam 		 ov5640_setting_15fps_XGA_1024_768,
73419a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
735476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
736476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
73719a81c14SSteve Longerbeam 		 ov5640_setting_15fps_720P_1280_720,
73819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
739476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
740476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
74119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_1080P_1920_1080,
74219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
743476dec01SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, SCALING,
744476dec01SMaxime Ripard 		 2592, 2844, 1944, 1968,
74519a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QSXGA_2592_1944,
74619a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
74719a81c14SSteve Longerbeam 	}, {
748476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
749476dec01SMaxime Ripard 		 176, 1896, 144, 984,
75019a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QCIF_176_144,
75119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
752476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
753476dec01SMaxime Ripard 		 320, 1896, 240, 984,
75419a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QVGA_320_240,
75519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
756476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
757476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
75819a81c14SSteve Longerbeam 		 ov5640_setting_30fps_VGA_640_480,
75919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
760476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
761476dec01SMaxime Ripard 		 720, 1896, 480, 984,
76219a81c14SSteve Longerbeam 		 ov5640_setting_30fps_NTSC_720_480,
76319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
764476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
765476dec01SMaxime Ripard 		 720, 1896, 576, 984,
76619a81c14SSteve Longerbeam 		 ov5640_setting_30fps_PAL_720_576,
76719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
768476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
769476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
77019a81c14SSteve Longerbeam 		 ov5640_setting_30fps_XGA_1024_768,
77119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
772476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
773476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
77419a81c14SSteve Longerbeam 		 ov5640_setting_30fps_720P_1280_720,
77519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
776476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
777476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
77819a81c14SSteve Longerbeam 		 ov5640_setting_30fps_1080P_1920_1080,
77919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
780476dec01SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, NULL, 0},
78119a81c14SSteve Longerbeam 	},
78219a81c14SSteve Longerbeam };
78319a81c14SSteve Longerbeam 
78419a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor)
78519a81c14SSteve Longerbeam {
78619a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
78719a81c14SSteve Longerbeam 	struct i2c_msg msg;
78819a81c14SSteve Longerbeam 	u8 buf[3];
78919a81c14SSteve Longerbeam 	int ret;
79019a81c14SSteve Longerbeam 
79119a81c14SSteve Longerbeam 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
79219a81c14SSteve Longerbeam 		return 0;
79319a81c14SSteve Longerbeam 
79419a81c14SSteve Longerbeam 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
79519a81c14SSteve Longerbeam 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
79619a81c14SSteve Longerbeam 	buf[2] = client->addr << 1;
79719a81c14SSteve Longerbeam 
79819a81c14SSteve Longerbeam 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
79919a81c14SSteve Longerbeam 	msg.flags = 0;
80019a81c14SSteve Longerbeam 	msg.buf = buf;
80119a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
80219a81c14SSteve Longerbeam 
80319a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
80419a81c14SSteve Longerbeam 	if (ret < 0) {
80519a81c14SSteve Longerbeam 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
80619a81c14SSteve Longerbeam 		return ret;
80719a81c14SSteve Longerbeam 	}
80819a81c14SSteve Longerbeam 
80919a81c14SSteve Longerbeam 	return 0;
81019a81c14SSteve Longerbeam }
81119a81c14SSteve Longerbeam 
81219a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
81319a81c14SSteve Longerbeam {
81419a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
81519a81c14SSteve Longerbeam 	struct i2c_msg msg;
81619a81c14SSteve Longerbeam 	u8 buf[3];
81719a81c14SSteve Longerbeam 	int ret;
81819a81c14SSteve Longerbeam 
81919a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
82019a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
82119a81c14SSteve Longerbeam 	buf[2] = val;
82219a81c14SSteve Longerbeam 
82319a81c14SSteve Longerbeam 	msg.addr = client->addr;
82419a81c14SSteve Longerbeam 	msg.flags = client->flags;
82519a81c14SSteve Longerbeam 	msg.buf = buf;
82619a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
82719a81c14SSteve Longerbeam 
82819a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
82919a81c14SSteve Longerbeam 	if (ret < 0) {
8303924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
83119a81c14SSteve Longerbeam 			__func__, reg, val);
83219a81c14SSteve Longerbeam 		return ret;
83319a81c14SSteve Longerbeam 	}
83419a81c14SSteve Longerbeam 
83519a81c14SSteve Longerbeam 	return 0;
83619a81c14SSteve Longerbeam }
83719a81c14SSteve Longerbeam 
83819a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
83919a81c14SSteve Longerbeam {
84019a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
84119a81c14SSteve Longerbeam 	struct i2c_msg msg[2];
84219a81c14SSteve Longerbeam 	u8 buf[2];
84319a81c14SSteve Longerbeam 	int ret;
84419a81c14SSteve Longerbeam 
84519a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
84619a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
84719a81c14SSteve Longerbeam 
84819a81c14SSteve Longerbeam 	msg[0].addr = client->addr;
84919a81c14SSteve Longerbeam 	msg[0].flags = client->flags;
85019a81c14SSteve Longerbeam 	msg[0].buf = buf;
85119a81c14SSteve Longerbeam 	msg[0].len = sizeof(buf);
85219a81c14SSteve Longerbeam 
85319a81c14SSteve Longerbeam 	msg[1].addr = client->addr;
85419a81c14SSteve Longerbeam 	msg[1].flags = client->flags | I2C_M_RD;
85519a81c14SSteve Longerbeam 	msg[1].buf = buf;
85619a81c14SSteve Longerbeam 	msg[1].len = 1;
85719a81c14SSteve Longerbeam 
85819a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, msg, 2);
8593924c623SHugues Fruchet 	if (ret < 0) {
8603924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x\n",
8613924c623SHugues Fruchet 			__func__, reg);
86219a81c14SSteve Longerbeam 		return ret;
8633924c623SHugues Fruchet 	}
86419a81c14SSteve Longerbeam 
86519a81c14SSteve Longerbeam 	*val = buf[0];
86619a81c14SSteve Longerbeam 	return 0;
86719a81c14SSteve Longerbeam }
86819a81c14SSteve Longerbeam 
86919a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
87019a81c14SSteve Longerbeam {
87119a81c14SSteve Longerbeam 	u8 hi, lo;
87219a81c14SSteve Longerbeam 	int ret;
87319a81c14SSteve Longerbeam 
87419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &hi);
87519a81c14SSteve Longerbeam 	if (ret)
87619a81c14SSteve Longerbeam 		return ret;
87719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
87819a81c14SSteve Longerbeam 	if (ret)
87919a81c14SSteve Longerbeam 		return ret;
88019a81c14SSteve Longerbeam 
88119a81c14SSteve Longerbeam 	*val = ((u16)hi << 8) | (u16)lo;
88219a81c14SSteve Longerbeam 	return 0;
88319a81c14SSteve Longerbeam }
88419a81c14SSteve Longerbeam 
88519a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
88619a81c14SSteve Longerbeam {
88719a81c14SSteve Longerbeam 	int ret;
88819a81c14SSteve Longerbeam 
88919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, reg, val >> 8);
89019a81c14SSteve Longerbeam 	if (ret)
89119a81c14SSteve Longerbeam 		return ret;
89219a81c14SSteve Longerbeam 
89319a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
89419a81c14SSteve Longerbeam }
89519a81c14SSteve Longerbeam 
89619a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
89719a81c14SSteve Longerbeam 			  u8 mask, u8 val)
89819a81c14SSteve Longerbeam {
89919a81c14SSteve Longerbeam 	u8 readval;
90019a81c14SSteve Longerbeam 	int ret;
90119a81c14SSteve Longerbeam 
90219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &readval);
90319a81c14SSteve Longerbeam 	if (ret)
90419a81c14SSteve Longerbeam 		return ret;
90519a81c14SSteve Longerbeam 
90619a81c14SSteve Longerbeam 	readval &= ~mask;
90719a81c14SSteve Longerbeam 	val &= mask;
90819a81c14SSteve Longerbeam 	val |= readval;
90919a81c14SSteve Longerbeam 
91019a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg, val);
91119a81c14SSteve Longerbeam }
91219a81c14SSteve Longerbeam 
91319a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */
914bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor,
915bad1774eSJacopo Mondi 			      const struct ov5640_mode_info *mode)
916bad1774eSJacopo Mondi {
917bad1774eSJacopo Mondi 	int ret;
918bad1774eSJacopo Mondi 
919bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
920bad1774eSJacopo Mondi 	if (ret < 0)
921bad1774eSJacopo Mondi 		return ret;
922bad1774eSJacopo Mondi 
923bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
924bad1774eSJacopo Mondi 	if (ret < 0)
925bad1774eSJacopo Mondi 		return ret;
926bad1774eSJacopo Mondi 
927bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
928bad1774eSJacopo Mondi 	if (ret < 0)
929bad1774eSJacopo Mondi 		return ret;
930bad1774eSJacopo Mondi 
931bad1774eSJacopo Mondi 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
932bad1774eSJacopo Mondi }
933bad1774eSJacopo Mondi 
93419a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor,
93519a81c14SSteve Longerbeam 			    const struct ov5640_mode_info *mode)
93619a81c14SSteve Longerbeam {
93719a81c14SSteve Longerbeam 	const struct reg_value *regs = mode->reg_data;
93819a81c14SSteve Longerbeam 	unsigned int i;
93919a81c14SSteve Longerbeam 	u32 delay_ms;
94019a81c14SSteve Longerbeam 	u16 reg_addr;
94119a81c14SSteve Longerbeam 	u8 mask, val;
94219a81c14SSteve Longerbeam 	int ret = 0;
94319a81c14SSteve Longerbeam 
94419a81c14SSteve Longerbeam 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
94519a81c14SSteve Longerbeam 		delay_ms = regs->delay_ms;
94619a81c14SSteve Longerbeam 		reg_addr = regs->reg_addr;
94719a81c14SSteve Longerbeam 		val = regs->val;
94819a81c14SSteve Longerbeam 		mask = regs->mask;
94919a81c14SSteve Longerbeam 
95019a81c14SSteve Longerbeam 		if (mask)
95119a81c14SSteve Longerbeam 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
95219a81c14SSteve Longerbeam 		else
95319a81c14SSteve Longerbeam 			ret = ov5640_write_reg(sensor, reg_addr, val);
95419a81c14SSteve Longerbeam 		if (ret)
95519a81c14SSteve Longerbeam 			break;
95619a81c14SSteve Longerbeam 
95719a81c14SSteve Longerbeam 		if (delay_ms)
95819a81c14SSteve Longerbeam 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
95919a81c14SSteve Longerbeam 	}
96019a81c14SSteve Longerbeam 
961bad1774eSJacopo Mondi 	return ov5640_set_timings(sensor, mode);
96219a81c14SSteve Longerbeam }
96319a81c14SSteve Longerbeam 
964dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
965dc29a1c1SHugues Fruchet {
966dc29a1c1SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
967dc29a1c1SHugues Fruchet 			      BIT(0), on ? 0 : BIT(0));
968dc29a1c1SHugues Fruchet }
969dc29a1c1SHugues Fruchet 
97019a81c14SSteve Longerbeam /* read exposure, in number of line periods */
97119a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor)
97219a81c14SSteve Longerbeam {
97319a81c14SSteve Longerbeam 	int exp, ret;
97419a81c14SSteve Longerbeam 	u8 temp;
97519a81c14SSteve Longerbeam 
97619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
97719a81c14SSteve Longerbeam 	if (ret)
97819a81c14SSteve Longerbeam 		return ret;
97919a81c14SSteve Longerbeam 	exp = ((int)temp & 0x0f) << 16;
98019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
98119a81c14SSteve Longerbeam 	if (ret)
98219a81c14SSteve Longerbeam 		return ret;
98319a81c14SSteve Longerbeam 	exp |= ((int)temp << 8);
98419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
98519a81c14SSteve Longerbeam 	if (ret)
98619a81c14SSteve Longerbeam 		return ret;
98719a81c14SSteve Longerbeam 	exp |= (int)temp;
98819a81c14SSteve Longerbeam 
98919a81c14SSteve Longerbeam 	return exp >> 4;
99019a81c14SSteve Longerbeam }
99119a81c14SSteve Longerbeam 
99219a81c14SSteve Longerbeam /* write exposure, given number of line periods */
99319a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
99419a81c14SSteve Longerbeam {
99519a81c14SSteve Longerbeam 	int ret;
99619a81c14SSteve Longerbeam 
99719a81c14SSteve Longerbeam 	exposure <<= 4;
99819a81c14SSteve Longerbeam 
99919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
100019a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
100119a81c14SSteve Longerbeam 			       exposure & 0xff);
100219a81c14SSteve Longerbeam 	if (ret)
100319a81c14SSteve Longerbeam 		return ret;
100419a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
100519a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
100619a81c14SSteve Longerbeam 			       (exposure >> 8) & 0xff);
100719a81c14SSteve Longerbeam 	if (ret)
100819a81c14SSteve Longerbeam 		return ret;
100919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor,
101019a81c14SSteve Longerbeam 				OV5640_REG_AEC_PK_EXPOSURE_HI,
101119a81c14SSteve Longerbeam 				(exposure >> 16) & 0x0f);
101219a81c14SSteve Longerbeam }
101319a81c14SSteve Longerbeam 
101419a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor)
101519a81c14SSteve Longerbeam {
101619a81c14SSteve Longerbeam 	u16 gain;
101719a81c14SSteve Longerbeam 	int ret;
101819a81c14SSteve Longerbeam 
101919a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
102019a81c14SSteve Longerbeam 	if (ret)
102119a81c14SSteve Longerbeam 		return ret;
102219a81c14SSteve Longerbeam 
102319a81c14SSteve Longerbeam 	return gain & 0x3ff;
102419a81c14SSteve Longerbeam }
102519a81c14SSteve Longerbeam 
10263cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
10273cca8ef5SHugues Fruchet {
10283cca8ef5SHugues Fruchet 	return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
10293cca8ef5SHugues Fruchet 				  (u16)gain & 0x3ff);
10303cca8ef5SHugues Fruchet }
10313cca8ef5SHugues Fruchet 
10323cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
10333cca8ef5SHugues Fruchet {
10343cca8ef5SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
10353cca8ef5SHugues Fruchet 			      BIT(1), on ? 0 : BIT(1));
10363cca8ef5SHugues Fruchet }
10373cca8ef5SHugues Fruchet 
1038f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1039f22996dbSHugues Fruchet {
1040f22996dbSHugues Fruchet 	int ret;
1041f22996dbSHugues Fruchet 	unsigned int flags = sensor->ep.bus.parallel.flags;
1042f22996dbSHugues Fruchet 	u8 pclk_pol = 0;
1043f22996dbSHugues Fruchet 	u8 hsync_pol = 0;
1044f22996dbSHugues Fruchet 	u8 vsync_pol = 0;
1045f22996dbSHugues Fruchet 
1046f22996dbSHugues Fruchet 	/*
1047f22996dbSHugues Fruchet 	 * Note about parallel port configuration.
1048f22996dbSHugues Fruchet 	 *
1049f22996dbSHugues Fruchet 	 * When configured in parallel mode, the OV5640 will
1050f22996dbSHugues Fruchet 	 * output 10 bits data on DVP data lines [9:0].
1051f22996dbSHugues Fruchet 	 * If only 8 bits data are wanted, the 8 bits data lines
1052f22996dbSHugues Fruchet 	 * of the camera interface must be physically connected
1053f22996dbSHugues Fruchet 	 * on the DVP data lines [9:2].
1054f22996dbSHugues Fruchet 	 *
1055f22996dbSHugues Fruchet 	 * Control lines polarity can be configured through
1056f22996dbSHugues Fruchet 	 * devicetree endpoint control lines properties.
1057f22996dbSHugues Fruchet 	 * If no endpoint control lines properties are set,
1058f22996dbSHugues Fruchet 	 * polarity will be as below:
1059f22996dbSHugues Fruchet 	 * - VSYNC:	active high
1060f22996dbSHugues Fruchet 	 * - HREF:	active low
1061f22996dbSHugues Fruchet 	 * - PCLK:	active low
1062f22996dbSHugues Fruchet 	 */
1063f22996dbSHugues Fruchet 
1064f22996dbSHugues Fruchet 	if (on) {
1065f22996dbSHugues Fruchet 		/*
1066f22996dbSHugues Fruchet 		 * reset MIPI PCLK/SERCLK divider
1067f22996dbSHugues Fruchet 		 *
1068f22996dbSHugues Fruchet 		 * SC PLL CONTRL1 0
1069f22996dbSHugues Fruchet 		 * - [3..0]:	MIPI PCLK/SERCLK divider
1070f22996dbSHugues Fruchet 		 */
1071f22996dbSHugues Fruchet 		ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1072f22996dbSHugues Fruchet 		if (ret)
1073f22996dbSHugues Fruchet 			return ret;
1074f22996dbSHugues Fruchet 
1075f22996dbSHugues Fruchet 		/*
1076f22996dbSHugues Fruchet 		 * configure parallel port control lines polarity
1077f22996dbSHugues Fruchet 		 *
1078f22996dbSHugues Fruchet 		 * POLARITY CTRL0
1079f22996dbSHugues Fruchet 		 * - [5]:	PCLK polarity (0: active low, 1: active high)
1080f22996dbSHugues Fruchet 		 * - [1]:	HREF polarity (0: active low, 1: active high)
1081f22996dbSHugues Fruchet 		 * - [0]:	VSYNC polarity (mismatch here between
1082f22996dbSHugues Fruchet 		 *		datasheet and hardware, 0 is active high
1083f22996dbSHugues Fruchet 		 *		and 1 is active low...)
1084f22996dbSHugues Fruchet 		 */
1085f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1086f22996dbSHugues Fruchet 			pclk_pol = 1;
1087f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1088f22996dbSHugues Fruchet 			hsync_pol = 1;
1089f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1090f22996dbSHugues Fruchet 			vsync_pol = 1;
1091f22996dbSHugues Fruchet 
1092f22996dbSHugues Fruchet 		ret = ov5640_write_reg(sensor,
1093f22996dbSHugues Fruchet 				       OV5640_REG_POLARITY_CTRL00,
1094f22996dbSHugues Fruchet 				       (pclk_pol << 5) |
1095f22996dbSHugues Fruchet 				       (hsync_pol << 1) |
1096f22996dbSHugues Fruchet 				       vsync_pol);
1097f22996dbSHugues Fruchet 
1098f22996dbSHugues Fruchet 		if (ret)
1099f22996dbSHugues Fruchet 			return ret;
1100f22996dbSHugues Fruchet 	}
1101f22996dbSHugues Fruchet 
1102f22996dbSHugues Fruchet 	/*
1103f22996dbSHugues Fruchet 	 * powerdown MIPI TX/RX PHY & disable MIPI
1104f22996dbSHugues Fruchet 	 *
1105f22996dbSHugues Fruchet 	 * MIPI CONTROL 00
1106f22996dbSHugues Fruchet 	 * 4:	 PWDN PHY TX
1107f22996dbSHugues Fruchet 	 * 3:	 PWDN PHY RX
1108f22996dbSHugues Fruchet 	 * 2:	 MIPI enable
1109f22996dbSHugues Fruchet 	 */
1110f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1111f22996dbSHugues Fruchet 			       OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1112f22996dbSHugues Fruchet 	if (ret)
1113f22996dbSHugues Fruchet 		return ret;
1114f22996dbSHugues Fruchet 
1115f22996dbSHugues Fruchet 	/*
1116f22996dbSHugues Fruchet 	 * enable VSYNC/HREF/PCLK DVP control lines
1117f22996dbSHugues Fruchet 	 * & D[9:6] DVP data lines
1118f22996dbSHugues Fruchet 	 *
1119f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 01
1120f22996dbSHugues Fruchet 	 * - 6:		VSYNC output enable
1121f22996dbSHugues Fruchet 	 * - 5:		HREF output enable
1122f22996dbSHugues Fruchet 	 * - 4:		PCLK output enable
1123f22996dbSHugues Fruchet 	 * - [3:0]:	D[9:6] output enable
1124f22996dbSHugues Fruchet 	 */
1125f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1126f22996dbSHugues Fruchet 			       OV5640_REG_PAD_OUTPUT_ENABLE01,
1127f22996dbSHugues Fruchet 			       on ? 0x7f : 0);
1128f22996dbSHugues Fruchet 	if (ret)
1129f22996dbSHugues Fruchet 		return ret;
1130f22996dbSHugues Fruchet 
1131f22996dbSHugues Fruchet 	/*
1132f22996dbSHugues Fruchet 	 * enable D[5:0] DVP data lines
1133f22996dbSHugues Fruchet 	 *
1134f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 02
1135f22996dbSHugues Fruchet 	 * - [7:2]:	D[5:0] output enable
1136f22996dbSHugues Fruchet 	 */
1137f22996dbSHugues Fruchet 	return ov5640_write_reg(sensor,
1138f22996dbSHugues Fruchet 				OV5640_REG_PAD_OUTPUT_ENABLE02,
1139f22996dbSHugues Fruchet 				on ? 0xfc : 0);
1140f22996dbSHugues Fruchet }
1141f22996dbSHugues Fruchet 
1142f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
114319a81c14SSteve Longerbeam {
114419a81c14SSteve Longerbeam 	int ret;
114519a81c14SSteve Longerbeam 
1146aa4bb8b8SJacopo Mondi 	/*
1147aa4bb8b8SJacopo Mondi 	 * Enable/disable the MIPI interface
1148aa4bb8b8SJacopo Mondi 	 *
1149aa4bb8b8SJacopo Mondi 	 * 0x300e = on ? 0x45 : 0x40
1150aa4bb8b8SJacopo Mondi 	 *
1151aa4bb8b8SJacopo Mondi 	 * FIXME: the sensor manual (version 2.03) reports
1152aa4bb8b8SJacopo Mondi 	 * [7:5] = 000  : 1 data lane mode
1153aa4bb8b8SJacopo Mondi 	 * [7:5] = 001  : 2 data lanes mode
1154aa4bb8b8SJacopo Mondi 	 * But this settings do not work, while the following ones
1155aa4bb8b8SJacopo Mondi 	 * have been validated for 2 data lanes mode.
1156aa4bb8b8SJacopo Mondi 	 *
1157aa4bb8b8SJacopo Mondi 	 * [7:5] = 010	: 2 data lanes mode
1158aa4bb8b8SJacopo Mondi 	 * [4] = 0	: Power up MIPI HS Tx
1159aa4bb8b8SJacopo Mondi 	 * [3] = 0	: Power up MIPI LS Rx
1160aa4bb8b8SJacopo Mondi 	 * [2] = 1/0	: MIPI interface enable/disable
1161aa4bb8b8SJacopo Mondi 	 * [1:0] = 01/00: FIXME: 'debug'
1162aa4bb8b8SJacopo Mondi 	 */
1163aa4bb8b8SJacopo Mondi 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1164aa4bb8b8SJacopo Mondi 			       on ? 0x45 : 0x40);
116519a81c14SSteve Longerbeam 	if (ret)
116619a81c14SSteve Longerbeam 		return ret;
116719a81c14SSteve Longerbeam 
116819a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
116919a81c14SSteve Longerbeam 				on ? 0x00 : 0x0f);
117019a81c14SSteve Longerbeam }
117119a81c14SSteve Longerbeam 
117219a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor)
117319a81c14SSteve Longerbeam {
117419a81c14SSteve Longerbeam 	 /* calculate sysclk */
117519a81c14SSteve Longerbeam 	u32 xvclk = sensor->xclk_freq / 10000;
117619a81c14SSteve Longerbeam 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
117719a81c14SSteve Longerbeam 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
117819a81c14SSteve Longerbeam 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
117919a81c14SSteve Longerbeam 	u8 temp1, temp2;
118019a81c14SSteve Longerbeam 	int ret;
118119a81c14SSteve Longerbeam 
118219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
118319a81c14SSteve Longerbeam 	if (ret)
118419a81c14SSteve Longerbeam 		return ret;
118519a81c14SSteve Longerbeam 	temp2 = temp1 & 0x0f;
118619a81c14SSteve Longerbeam 	if (temp2 == 8 || temp2 == 10)
118719a81c14SSteve Longerbeam 		bit_div2x = temp2 / 2;
118819a81c14SSteve Longerbeam 
118919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
119019a81c14SSteve Longerbeam 	if (ret)
119119a81c14SSteve Longerbeam 		return ret;
119219a81c14SSteve Longerbeam 	sysdiv = temp1 >> 4;
119319a81c14SSteve Longerbeam 	if (sysdiv == 0)
119419a81c14SSteve Longerbeam 		sysdiv = 16;
119519a81c14SSteve Longerbeam 
119619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
119719a81c14SSteve Longerbeam 	if (ret)
119819a81c14SSteve Longerbeam 		return ret;
119919a81c14SSteve Longerbeam 	multiplier = temp1;
120019a81c14SSteve Longerbeam 
120119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
120219a81c14SSteve Longerbeam 	if (ret)
120319a81c14SSteve Longerbeam 		return ret;
120419a81c14SSteve Longerbeam 	prediv = temp1 & 0x0f;
120519a81c14SSteve Longerbeam 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
120619a81c14SSteve Longerbeam 
120719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
120819a81c14SSteve Longerbeam 	if (ret)
120919a81c14SSteve Longerbeam 		return ret;
121019a81c14SSteve Longerbeam 	temp2 = temp1 & 0x03;
121119a81c14SSteve Longerbeam 	sclk_rdiv = sclk_rdiv_map[temp2];
121219a81c14SSteve Longerbeam 
121319a81c14SSteve Longerbeam 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
121419a81c14SSteve Longerbeam 		return -EINVAL;
121519a81c14SSteve Longerbeam 
121619a81c14SSteve Longerbeam 	VCO = xvclk * multiplier / prediv;
121719a81c14SSteve Longerbeam 
121819a81c14SSteve Longerbeam 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
121919a81c14SSteve Longerbeam 
122019a81c14SSteve Longerbeam 	return sysclk;
122119a81c14SSteve Longerbeam }
122219a81c14SSteve Longerbeam 
122319a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor)
122419a81c14SSteve Longerbeam {
122519a81c14SSteve Longerbeam 	 /* read HTS from register settings */
122619a81c14SSteve Longerbeam 	u8 mode;
122719a81c14SSteve Longerbeam 	int ret;
122819a81c14SSteve Longerbeam 
122919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
123019a81c14SSteve Longerbeam 	if (ret)
123119a81c14SSteve Longerbeam 		return ret;
123219a81c14SSteve Longerbeam 	mode &= 0xfb;
123319a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
123419a81c14SSteve Longerbeam }
123519a81c14SSteve Longerbeam 
123619a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor)
123719a81c14SSteve Longerbeam {
123819a81c14SSteve Longerbeam 	/* read HTS from register settings */
123919a81c14SSteve Longerbeam 	u16 hts;
124019a81c14SSteve Longerbeam 	int ret;
124119a81c14SSteve Longerbeam 
124219a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
124319a81c14SSteve Longerbeam 	if (ret)
124419a81c14SSteve Longerbeam 		return ret;
124519a81c14SSteve Longerbeam 	return hts;
124619a81c14SSteve Longerbeam }
124719a81c14SSteve Longerbeam 
124819a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor)
124919a81c14SSteve Longerbeam {
125019a81c14SSteve Longerbeam 	u16 vts;
125119a81c14SSteve Longerbeam 	int ret;
125219a81c14SSteve Longerbeam 
125319a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
125419a81c14SSteve Longerbeam 	if (ret)
125519a81c14SSteve Longerbeam 		return ret;
125619a81c14SSteve Longerbeam 	return vts;
125719a81c14SSteve Longerbeam }
125819a81c14SSteve Longerbeam 
125919a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
126019a81c14SSteve Longerbeam {
126119a81c14SSteve Longerbeam 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
126219a81c14SSteve Longerbeam }
126319a81c14SSteve Longerbeam 
126419a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor)
126519a81c14SSteve Longerbeam {
126619a81c14SSteve Longerbeam 	/* get banding filter value */
126719a81c14SSteve Longerbeam 	int ret, light_freq = 0;
126819a81c14SSteve Longerbeam 	u8 temp, temp1;
126919a81c14SSteve Longerbeam 
127019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
127119a81c14SSteve Longerbeam 	if (ret)
127219a81c14SSteve Longerbeam 		return ret;
127319a81c14SSteve Longerbeam 
127419a81c14SSteve Longerbeam 	if (temp & 0x80) {
127519a81c14SSteve Longerbeam 		/* manual */
127619a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
127719a81c14SSteve Longerbeam 				      &temp1);
127819a81c14SSteve Longerbeam 		if (ret)
127919a81c14SSteve Longerbeam 			return ret;
128019a81c14SSteve Longerbeam 		if (temp1 & 0x04) {
128119a81c14SSteve Longerbeam 			/* 50Hz */
128219a81c14SSteve Longerbeam 			light_freq = 50;
128319a81c14SSteve Longerbeam 		} else {
128419a81c14SSteve Longerbeam 			/* 60Hz */
128519a81c14SSteve Longerbeam 			light_freq = 60;
128619a81c14SSteve Longerbeam 		}
128719a81c14SSteve Longerbeam 	} else {
128819a81c14SSteve Longerbeam 		/* auto */
128919a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
129019a81c14SSteve Longerbeam 				      &temp1);
129119a81c14SSteve Longerbeam 		if (ret)
129219a81c14SSteve Longerbeam 			return ret;
129319a81c14SSteve Longerbeam 
129419a81c14SSteve Longerbeam 		if (temp1 & 0x01) {
129519a81c14SSteve Longerbeam 			/* 50Hz */
129619a81c14SSteve Longerbeam 			light_freq = 50;
129719a81c14SSteve Longerbeam 		} else {
129819a81c14SSteve Longerbeam 			/* 60Hz */
129919a81c14SSteve Longerbeam 		}
130019a81c14SSteve Longerbeam 	}
130119a81c14SSteve Longerbeam 
130219a81c14SSteve Longerbeam 	return light_freq;
130319a81c14SSteve Longerbeam }
130419a81c14SSteve Longerbeam 
130519a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
130619a81c14SSteve Longerbeam {
130719a81c14SSteve Longerbeam 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
130819a81c14SSteve Longerbeam 	int ret;
130919a81c14SSteve Longerbeam 
131019a81c14SSteve Longerbeam 	/* read preview PCLK */
131119a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
131219a81c14SSteve Longerbeam 	if (ret < 0)
131319a81c14SSteve Longerbeam 		return ret;
131419a81c14SSteve Longerbeam 	if (ret == 0)
131519a81c14SSteve Longerbeam 		return -EINVAL;
131619a81c14SSteve Longerbeam 	sensor->prev_sysclk = ret;
131719a81c14SSteve Longerbeam 	/* read preview HTS */
131819a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
131919a81c14SSteve Longerbeam 	if (ret < 0)
132019a81c14SSteve Longerbeam 		return ret;
132119a81c14SSteve Longerbeam 	if (ret == 0)
132219a81c14SSteve Longerbeam 		return -EINVAL;
132319a81c14SSteve Longerbeam 	sensor->prev_hts = ret;
132419a81c14SSteve Longerbeam 
132519a81c14SSteve Longerbeam 	/* read preview VTS */
132619a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
132719a81c14SSteve Longerbeam 	if (ret < 0)
132819a81c14SSteve Longerbeam 		return ret;
132919a81c14SSteve Longerbeam 	prev_vts = ret;
133019a81c14SSteve Longerbeam 
133119a81c14SSteve Longerbeam 	/* calculate banding filter */
133219a81c14SSteve Longerbeam 	/* 60Hz */
133319a81c14SSteve Longerbeam 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
133419a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
133519a81c14SSteve Longerbeam 	if (ret)
133619a81c14SSteve Longerbeam 		return ret;
133719a81c14SSteve Longerbeam 	if (!band_step60)
133819a81c14SSteve Longerbeam 		return -EINVAL;
133919a81c14SSteve Longerbeam 	max_band60 = (int)((prev_vts - 4) / band_step60);
134019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
134119a81c14SSteve Longerbeam 	if (ret)
134219a81c14SSteve Longerbeam 		return ret;
134319a81c14SSteve Longerbeam 
134419a81c14SSteve Longerbeam 	/* 50Hz */
134519a81c14SSteve Longerbeam 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
134619a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
134719a81c14SSteve Longerbeam 	if (ret)
134819a81c14SSteve Longerbeam 		return ret;
134919a81c14SSteve Longerbeam 	if (!band_step50)
135019a81c14SSteve Longerbeam 		return -EINVAL;
135119a81c14SSteve Longerbeam 	max_band50 = (int)((prev_vts - 4) / band_step50);
135219a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
135319a81c14SSteve Longerbeam }
135419a81c14SSteve Longerbeam 
135519a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
135619a81c14SSteve Longerbeam {
135719a81c14SSteve Longerbeam 	/* stable in high */
135819a81c14SSteve Longerbeam 	u32 fast_high, fast_low;
135919a81c14SSteve Longerbeam 	int ret;
136019a81c14SSteve Longerbeam 
136119a81c14SSteve Longerbeam 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
136219a81c14SSteve Longerbeam 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
136319a81c14SSteve Longerbeam 
136419a81c14SSteve Longerbeam 	fast_high = sensor->ae_high << 1;
136519a81c14SSteve Longerbeam 	if (fast_high > 255)
136619a81c14SSteve Longerbeam 		fast_high = 255;
136719a81c14SSteve Longerbeam 
136819a81c14SSteve Longerbeam 	fast_low = sensor->ae_low >> 1;
136919a81c14SSteve Longerbeam 
137019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
137119a81c14SSteve Longerbeam 	if (ret)
137219a81c14SSteve Longerbeam 		return ret;
137319a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
137419a81c14SSteve Longerbeam 	if (ret)
137519a81c14SSteve Longerbeam 		return ret;
137619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
137719a81c14SSteve Longerbeam 	if (ret)
137819a81c14SSteve Longerbeam 		return ret;
137919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
138019a81c14SSteve Longerbeam 	if (ret)
138119a81c14SSteve Longerbeam 		return ret;
138219a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
138319a81c14SSteve Longerbeam 	if (ret)
138419a81c14SSteve Longerbeam 		return ret;
138519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
138619a81c14SSteve Longerbeam }
138719a81c14SSteve Longerbeam 
1388c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor)
138919a81c14SSteve Longerbeam {
139019a81c14SSteve Longerbeam 	u8 temp;
139119a81c14SSteve Longerbeam 	int ret;
139219a81c14SSteve Longerbeam 
139319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
139419a81c14SSteve Longerbeam 	if (ret)
139519a81c14SSteve Longerbeam 		return ret;
1396c2c3f42dSHugues Fruchet 
1397c2c3f42dSHugues Fruchet 	return temp & BIT(0);
139819a81c14SSteve Longerbeam }
139919a81c14SSteve Longerbeam 
1400ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1401ce85705aSHugues Fruchet {
1402ce85705aSHugues Fruchet 	int ret;
1403ce85705aSHugues Fruchet 
1404ce85705aSHugues Fruchet 	/*
1405ce85705aSHugues Fruchet 	 * TIMING TC REG21:
1406ce85705aSHugues Fruchet 	 * - [0]:	Horizontal binning enable
1407ce85705aSHugues Fruchet 	 */
1408ce85705aSHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1409ce85705aSHugues Fruchet 			     BIT(0), enable ? BIT(0) : 0);
1410ce85705aSHugues Fruchet 	if (ret)
1411ce85705aSHugues Fruchet 		return ret;
1412ce85705aSHugues Fruchet 	/*
1413ce85705aSHugues Fruchet 	 * TIMING TC REG20:
1414ce85705aSHugues Fruchet 	 * - [0]:	Undocumented, but hardcoded init sequences
1415ce85705aSHugues Fruchet 	 *		are always setting REG21/REG20 bit 0 to same value...
1416ce85705aSHugues Fruchet 	 */
1417ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1418ce85705aSHugues Fruchet 			      BIT(0), enable ? BIT(0) : 0);
1419ce85705aSHugues Fruchet }
1420ce85705aSHugues Fruchet 
142119a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
142219a81c14SSteve Longerbeam {
14238670d70aSHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
142419a81c14SSteve Longerbeam 	u8 temp, channel = virtual_channel;
142519a81c14SSteve Longerbeam 	int ret;
142619a81c14SSteve Longerbeam 
14278670d70aSHugues Fruchet 	if (channel > 3) {
14288670d70aSHugues Fruchet 		dev_err(&client->dev,
14298670d70aSHugues Fruchet 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
14308670d70aSHugues Fruchet 			__func__, channel);
143119a81c14SSteve Longerbeam 		return -EINVAL;
14328670d70aSHugues Fruchet 	}
143319a81c14SSteve Longerbeam 
143419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
143519a81c14SSteve Longerbeam 	if (ret)
143619a81c14SSteve Longerbeam 		return ret;
143719a81c14SSteve Longerbeam 	temp &= ~(3 << 6);
143819a81c14SSteve Longerbeam 	temp |= (channel << 6);
143919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
144019a81c14SSteve Longerbeam }
144119a81c14SSteve Longerbeam 
144219a81c14SSteve Longerbeam static const struct ov5640_mode_info *
144319a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
144419a81c14SSteve Longerbeam 		 int width, int height, bool nearest)
144519a81c14SSteve Longerbeam {
14463c4a7372SHugues Fruchet 	const struct ov5640_mode_info *mode;
144719a81c14SSteve Longerbeam 
14483c4a7372SHugues Fruchet 	mode = v4l2_find_nearest_size(ov5640_mode_data[fr],
14493c4a7372SHugues Fruchet 				      ARRAY_SIZE(ov5640_mode_data[fr]),
14503c4a7372SHugues Fruchet 				      hact, vact,
14513c4a7372SHugues Fruchet 				      width, height);
145219a81c14SSteve Longerbeam 
14533c4a7372SHugues Fruchet 	if (!mode ||
14543c4a7372SHugues Fruchet 	    (!nearest && (mode->hact != width || mode->vact != height)))
14553c4a7372SHugues Fruchet 		return NULL;
145619a81c14SSteve Longerbeam 
145719a81c14SSteve Longerbeam 	return mode;
145819a81c14SSteve Longerbeam }
145919a81c14SSteve Longerbeam 
146019a81c14SSteve Longerbeam /*
146119a81c14SSteve Longerbeam  * sensor changes between scaling and subsampling, go through
146219a81c14SSteve Longerbeam  * exposure calculation
146319a81c14SSteve Longerbeam  */
146441d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
146541d8d7f5SHugues Fruchet 					 const struct ov5640_mode_info *mode)
146619a81c14SSteve Longerbeam {
146719a81c14SSteve Longerbeam 	u32 prev_shutter, prev_gain16;
146819a81c14SSteve Longerbeam 	u32 cap_shutter, cap_gain16;
146919a81c14SSteve Longerbeam 	u32 cap_sysclk, cap_hts, cap_vts;
147019a81c14SSteve Longerbeam 	u32 light_freq, cap_bandfilt, cap_maxband;
147119a81c14SSteve Longerbeam 	u32 cap_gain16_shutter;
147219a81c14SSteve Longerbeam 	u8 average;
147319a81c14SSteve Longerbeam 	int ret;
147419a81c14SSteve Longerbeam 
147541d8d7f5SHugues Fruchet 	if (!mode->reg_data)
147619a81c14SSteve Longerbeam 		return -EINVAL;
147719a81c14SSteve Longerbeam 
147819a81c14SSteve Longerbeam 	/* read preview shutter */
147919a81c14SSteve Longerbeam 	ret = ov5640_get_exposure(sensor);
148019a81c14SSteve Longerbeam 	if (ret < 0)
148119a81c14SSteve Longerbeam 		return ret;
148219a81c14SSteve Longerbeam 	prev_shutter = ret;
1483c2c3f42dSHugues Fruchet 	ret = ov5640_get_binning(sensor);
148419a81c14SSteve Longerbeam 	if (ret < 0)
148519a81c14SSteve Longerbeam 		return ret;
148619a81c14SSteve Longerbeam 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
148719a81c14SSteve Longerbeam 	    mode->id != OV5640_MODE_1080P_1920_1080)
148819a81c14SSteve Longerbeam 		prev_shutter *= 2;
148919a81c14SSteve Longerbeam 
149019a81c14SSteve Longerbeam 	/* read preview gain */
149119a81c14SSteve Longerbeam 	ret = ov5640_get_gain(sensor);
149219a81c14SSteve Longerbeam 	if (ret < 0)
149319a81c14SSteve Longerbeam 		return ret;
149419a81c14SSteve Longerbeam 	prev_gain16 = ret;
149519a81c14SSteve Longerbeam 
149619a81c14SSteve Longerbeam 	/* get average */
149719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
149819a81c14SSteve Longerbeam 	if (ret)
149919a81c14SSteve Longerbeam 		return ret;
150019a81c14SSteve Longerbeam 
150119a81c14SSteve Longerbeam 	/* turn off night mode for capture */
150219a81c14SSteve Longerbeam 	ret = ov5640_set_night_mode(sensor);
150319a81c14SSteve Longerbeam 	if (ret < 0)
150419a81c14SSteve Longerbeam 		return ret;
150519a81c14SSteve Longerbeam 
150619a81c14SSteve Longerbeam 	/* Write capture setting */
150719a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
150819a81c14SSteve Longerbeam 	if (ret < 0)
150919a81c14SSteve Longerbeam 		return ret;
151019a81c14SSteve Longerbeam 
151119a81c14SSteve Longerbeam 	/* read capture VTS */
151219a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
151319a81c14SSteve Longerbeam 	if (ret < 0)
151419a81c14SSteve Longerbeam 		return ret;
151519a81c14SSteve Longerbeam 	cap_vts = ret;
151619a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
151719a81c14SSteve Longerbeam 	if (ret < 0)
151819a81c14SSteve Longerbeam 		return ret;
151919a81c14SSteve Longerbeam 	if (ret == 0)
152019a81c14SSteve Longerbeam 		return -EINVAL;
152119a81c14SSteve Longerbeam 	cap_hts = ret;
152219a81c14SSteve Longerbeam 
152319a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
152419a81c14SSteve Longerbeam 	if (ret < 0)
152519a81c14SSteve Longerbeam 		return ret;
152619a81c14SSteve Longerbeam 	if (ret == 0)
152719a81c14SSteve Longerbeam 		return -EINVAL;
152819a81c14SSteve Longerbeam 	cap_sysclk = ret;
152919a81c14SSteve Longerbeam 
153019a81c14SSteve Longerbeam 	/* calculate capture banding filter */
153119a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
153219a81c14SSteve Longerbeam 	if (ret < 0)
153319a81c14SSteve Longerbeam 		return ret;
153419a81c14SSteve Longerbeam 	light_freq = ret;
153519a81c14SSteve Longerbeam 
153619a81c14SSteve Longerbeam 	if (light_freq == 60) {
153719a81c14SSteve Longerbeam 		/* 60Hz */
153819a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
153919a81c14SSteve Longerbeam 	} else {
154019a81c14SSteve Longerbeam 		/* 50Hz */
154119a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
154219a81c14SSteve Longerbeam 	}
154319a81c14SSteve Longerbeam 
154419a81c14SSteve Longerbeam 	if (!sensor->prev_sysclk) {
154519a81c14SSteve Longerbeam 		ret = ov5640_get_sysclk(sensor);
154619a81c14SSteve Longerbeam 		if (ret < 0)
154719a81c14SSteve Longerbeam 			return ret;
154819a81c14SSteve Longerbeam 		if (ret == 0)
154919a81c14SSteve Longerbeam 			return -EINVAL;
155019a81c14SSteve Longerbeam 		sensor->prev_sysclk = ret;
155119a81c14SSteve Longerbeam 	}
155219a81c14SSteve Longerbeam 
155319a81c14SSteve Longerbeam 	if (!cap_bandfilt)
155419a81c14SSteve Longerbeam 		return -EINVAL;
155519a81c14SSteve Longerbeam 
155619a81c14SSteve Longerbeam 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
155719a81c14SSteve Longerbeam 
155819a81c14SSteve Longerbeam 	/* calculate capture shutter/gain16 */
155919a81c14SSteve Longerbeam 	if (average > sensor->ae_low && average < sensor->ae_high) {
156019a81c14SSteve Longerbeam 		/* in stable range */
156119a81c14SSteve Longerbeam 		cap_gain16_shutter =
156219a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
156319a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
156419a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts *
156519a81c14SSteve Longerbeam 			sensor->ae_target / average;
156619a81c14SSteve Longerbeam 	} else {
156719a81c14SSteve Longerbeam 		cap_gain16_shutter =
156819a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
156919a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
157019a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts;
157119a81c14SSteve Longerbeam 	}
157219a81c14SSteve Longerbeam 
157319a81c14SSteve Longerbeam 	/* gain to shutter */
157419a81c14SSteve Longerbeam 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
157519a81c14SSteve Longerbeam 		/* shutter < 1/100 */
157619a81c14SSteve Longerbeam 		cap_shutter = cap_gain16_shutter / 16;
157719a81c14SSteve Longerbeam 		if (cap_shutter < 1)
157819a81c14SSteve Longerbeam 			cap_shutter = 1;
157919a81c14SSteve Longerbeam 
158019a81c14SSteve Longerbeam 		cap_gain16 = cap_gain16_shutter / cap_shutter;
158119a81c14SSteve Longerbeam 		if (cap_gain16 < 16)
158219a81c14SSteve Longerbeam 			cap_gain16 = 16;
158319a81c14SSteve Longerbeam 	} else {
158419a81c14SSteve Longerbeam 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
158519a81c14SSteve Longerbeam 			/* exposure reach max */
158619a81c14SSteve Longerbeam 			cap_shutter = cap_bandfilt * cap_maxband;
158719a81c14SSteve Longerbeam 			if (!cap_shutter)
158819a81c14SSteve Longerbeam 				return -EINVAL;
158919a81c14SSteve Longerbeam 
159019a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
159119a81c14SSteve Longerbeam 		} else {
159219a81c14SSteve Longerbeam 			/* 1/100 < (cap_shutter = n/100) =< max */
159319a81c14SSteve Longerbeam 			cap_shutter =
159419a81c14SSteve Longerbeam 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
159519a81c14SSteve Longerbeam 				* cap_bandfilt;
159619a81c14SSteve Longerbeam 			if (!cap_shutter)
159719a81c14SSteve Longerbeam 				return -EINVAL;
159819a81c14SSteve Longerbeam 
159919a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
160019a81c14SSteve Longerbeam 		}
160119a81c14SSteve Longerbeam 	}
160219a81c14SSteve Longerbeam 
160319a81c14SSteve Longerbeam 	/* set capture gain */
16043cca8ef5SHugues Fruchet 	ret = ov5640_set_gain(sensor, cap_gain16);
160519a81c14SSteve Longerbeam 	if (ret)
160619a81c14SSteve Longerbeam 		return ret;
160719a81c14SSteve Longerbeam 
160819a81c14SSteve Longerbeam 	/* write capture shutter */
160919a81c14SSteve Longerbeam 	if (cap_shutter > (cap_vts - 4)) {
161019a81c14SSteve Longerbeam 		cap_vts = cap_shutter + 4;
161119a81c14SSteve Longerbeam 		ret = ov5640_set_vts(sensor, cap_vts);
161219a81c14SSteve Longerbeam 		if (ret < 0)
161319a81c14SSteve Longerbeam 			return ret;
161419a81c14SSteve Longerbeam 	}
161519a81c14SSteve Longerbeam 
161619a81c14SSteve Longerbeam 	/* set exposure */
16173cca8ef5SHugues Fruchet 	return ov5640_set_exposure(sensor, cap_shutter);
161819a81c14SSteve Longerbeam }
161919a81c14SSteve Longerbeam 
162019a81c14SSteve Longerbeam /*
162119a81c14SSteve Longerbeam  * if sensor changes inside scaling or subsampling
162219a81c14SSteve Longerbeam  * change mode directly
162319a81c14SSteve Longerbeam  */
162419a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
16253cca8ef5SHugues Fruchet 				  const struct ov5640_mode_info *mode)
162619a81c14SSteve Longerbeam {
162741d8d7f5SHugues Fruchet 	if (!mode->reg_data)
162819a81c14SSteve Longerbeam 		return -EINVAL;
162919a81c14SSteve Longerbeam 
163019a81c14SSteve Longerbeam 	/* Write capture setting */
16313cca8ef5SHugues Fruchet 	return ov5640_load_regs(sensor, mode);
163219a81c14SSteve Longerbeam }
163319a81c14SSteve Longerbeam 
1634985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor)
163519a81c14SSteve Longerbeam {
163619a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = sensor->current_mode;
1637985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *orig_mode = sensor->last_mode;
163819a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
16393cca8ef5SHugues Fruchet 	bool auto_gain = sensor->ctrls.auto_gain->val == 1;
1640dc29a1c1SHugues Fruchet 	bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
164119a81c14SSteve Longerbeam 	int ret;
164219a81c14SSteve Longerbeam 
164319a81c14SSteve Longerbeam 	dn_mode = mode->dn_mode;
164419a81c14SSteve Longerbeam 	orig_dn_mode = orig_mode->dn_mode;
164519a81c14SSteve Longerbeam 
164619a81c14SSteve Longerbeam 	/* auto gain and exposure must be turned off when changing modes */
16473cca8ef5SHugues Fruchet 	if (auto_gain) {
16483cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, false);
164919a81c14SSteve Longerbeam 		if (ret)
165019a81c14SSteve Longerbeam 			return ret;
16513cca8ef5SHugues Fruchet 	}
1652bf4a4b51SMaxime Ripard 
16533cca8ef5SHugues Fruchet 	if (auto_exp) {
1654dc29a1c1SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, false);
165519a81c14SSteve Longerbeam 		if (ret)
16563cca8ef5SHugues Fruchet 			goto restore_auto_gain;
16573cca8ef5SHugues Fruchet 	}
165819a81c14SSteve Longerbeam 
165919a81c14SSteve Longerbeam 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
166019a81c14SSteve Longerbeam 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
166119a81c14SSteve Longerbeam 		/*
166219a81c14SSteve Longerbeam 		 * change between subsampling and scaling
16633cca8ef5SHugues Fruchet 		 * go through exposure calculation
166419a81c14SSteve Longerbeam 		 */
166519a81c14SSteve Longerbeam 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
166619a81c14SSteve Longerbeam 	} else {
166719a81c14SSteve Longerbeam 		/*
166819a81c14SSteve Longerbeam 		 * change inside subsampling or scaling
166919a81c14SSteve Longerbeam 		 * download firmware directly
167019a81c14SSteve Longerbeam 		 */
16713cca8ef5SHugues Fruchet 		ret = ov5640_set_mode_direct(sensor, mode);
167219a81c14SSteve Longerbeam 	}
167319a81c14SSteve Longerbeam 	if (ret < 0)
16743cca8ef5SHugues Fruchet 		goto restore_auto_exp_gain;
16753cca8ef5SHugues Fruchet 
16763cca8ef5SHugues Fruchet 	/* restore auto gain and exposure */
16773cca8ef5SHugues Fruchet 	if (auto_gain)
16783cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
16793cca8ef5SHugues Fruchet 	if (auto_exp)
16803cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
168119a81c14SSteve Longerbeam 
1682ce85705aSHugues Fruchet 	ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1683ce85705aSHugues Fruchet 	if (ret < 0)
1684ce85705aSHugues Fruchet 		return ret;
168519a81c14SSteve Longerbeam 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
168619a81c14SSteve Longerbeam 	if (ret < 0)
168719a81c14SSteve Longerbeam 		return ret;
168819a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
168919a81c14SSteve Longerbeam 	if (ret < 0)
169019a81c14SSteve Longerbeam 		return ret;
169119a81c14SSteve Longerbeam 	ret = ov5640_set_bandingfilter(sensor);
169219a81c14SSteve Longerbeam 	if (ret < 0)
169319a81c14SSteve Longerbeam 		return ret;
169419a81c14SSteve Longerbeam 	ret = ov5640_set_virtual_channel(sensor);
169519a81c14SSteve Longerbeam 	if (ret < 0)
169619a81c14SSteve Longerbeam 		return ret;
169719a81c14SSteve Longerbeam 
169819a81c14SSteve Longerbeam 	sensor->pending_mode_change = false;
1699985cdcb0SHugues Fruchet 	sensor->last_mode = mode;
170019a81c14SSteve Longerbeam 
170119a81c14SSteve Longerbeam 	return 0;
17023cca8ef5SHugues Fruchet 
17033cca8ef5SHugues Fruchet restore_auto_exp_gain:
17043cca8ef5SHugues Fruchet 	if (auto_exp)
17053cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
17063cca8ef5SHugues Fruchet restore_auto_gain:
17073cca8ef5SHugues Fruchet 	if (auto_gain)
17083cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
17093cca8ef5SHugues Fruchet 
17103cca8ef5SHugues Fruchet 	return ret;
171119a81c14SSteve Longerbeam }
171219a81c14SSteve Longerbeam 
171319ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor,
171419ad26f9SAkinobu Mita 			       struct v4l2_mbus_framefmt *format);
171519ad26f9SAkinobu Mita 
171619a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */
171719a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor)
171819a81c14SSteve Longerbeam {
171919a81c14SSteve Longerbeam 	int ret;
172019a81c14SSteve Longerbeam 
172119a81c14SSteve Longerbeam 	/* first load the initial register values */
172219a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
172319a81c14SSteve Longerbeam 	if (ret < 0)
172419a81c14SSteve Longerbeam 		return ret;
1725985cdcb0SHugues Fruchet 	sensor->last_mode = &ov5640_mode_init_data;
172619a81c14SSteve Longerbeam 
17278f57c2f8SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
17288f57c2f8SMaxime Ripard 			     (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) |
17298f57c2f8SMaxime Ripard 			     ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT));
17308f57c2f8SMaxime Ripard 	if (ret)
17318f57c2f8SMaxime Ripard 		return ret;
17328f57c2f8SMaxime Ripard 
173319a81c14SSteve Longerbeam 	/* now restore the last capture mode */
1734985cdcb0SHugues Fruchet 	ret = ov5640_set_mode(sensor);
173519ad26f9SAkinobu Mita 	if (ret < 0)
173619ad26f9SAkinobu Mita 		return ret;
173719ad26f9SAkinobu Mita 
173819ad26f9SAkinobu Mita 	return ov5640_set_framefmt(sensor, &sensor->fmt);
173919a81c14SSteve Longerbeam }
174019a81c14SSteve Longerbeam 
174119a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable)
174219a81c14SSteve Longerbeam {
17431fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
174419a81c14SSteve Longerbeam }
174519a81c14SSteve Longerbeam 
174619a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor)
174719a81c14SSteve Longerbeam {
174819a81c14SSteve Longerbeam 	if (!sensor->reset_gpio)
174919a81c14SSteve Longerbeam 		return;
175019a81c14SSteve Longerbeam 
17511fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
175219a81c14SSteve Longerbeam 
175319a81c14SSteve Longerbeam 	/* camera power cycle */
175419a81c14SSteve Longerbeam 	ov5640_power(sensor, false);
175519a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
175619a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
175719a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
175819a81c14SSteve Longerbeam 
17591fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
176019a81c14SSteve Longerbeam 	usleep_range(1000, 2000);
176119a81c14SSteve Longerbeam 
17621fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
176319a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
176419a81c14SSteve Longerbeam }
176519a81c14SSteve Longerbeam 
17660f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor)
176719a81c14SSteve Longerbeam {
17680f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
17690f7acb52SHugues Fruchet 	int ret;
177019a81c14SSteve Longerbeam 
17710f7acb52SHugues Fruchet 	ret = clk_prepare_enable(sensor->xclk);
17720f7acb52SHugues Fruchet 	if (ret) {
17730f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable clock\n",
17740f7acb52SHugues Fruchet 			__func__);
17750f7acb52SHugues Fruchet 		return ret;
17760f7acb52SHugues Fruchet 	}
177719a81c14SSteve Longerbeam 
177819a81c14SSteve Longerbeam 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
177919a81c14SSteve Longerbeam 				    sensor->supplies);
17800f7acb52SHugues Fruchet 	if (ret) {
17810f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable regulators\n",
17820f7acb52SHugues Fruchet 			__func__);
178319a81c14SSteve Longerbeam 		goto xclk_off;
17840f7acb52SHugues Fruchet 	}
178519a81c14SSteve Longerbeam 
178619a81c14SSteve Longerbeam 	ov5640_reset(sensor);
178719a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
178819a81c14SSteve Longerbeam 
178919a81c14SSteve Longerbeam 	ret = ov5640_init_slave_id(sensor);
179019a81c14SSteve Longerbeam 	if (ret)
179119a81c14SSteve Longerbeam 		goto power_off;
179219a81c14SSteve Longerbeam 
17930f7acb52SHugues Fruchet 	return 0;
17940f7acb52SHugues Fruchet 
17950f7acb52SHugues Fruchet power_off:
17960f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
17970f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
17980f7acb52SHugues Fruchet xclk_off:
17990f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
18000f7acb52SHugues Fruchet 	return ret;
18010f7acb52SHugues Fruchet }
18020f7acb52SHugues Fruchet 
18030f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor)
18040f7acb52SHugues Fruchet {
18050f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
18060f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
18070f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
18080f7acb52SHugues Fruchet }
18090f7acb52SHugues Fruchet 
18100f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
18110f7acb52SHugues Fruchet {
18120f7acb52SHugues Fruchet 	int ret = 0;
18130f7acb52SHugues Fruchet 
18140f7acb52SHugues Fruchet 	if (on) {
18150f7acb52SHugues Fruchet 		ret = ov5640_set_power_on(sensor);
18160f7acb52SHugues Fruchet 		if (ret)
18170f7acb52SHugues Fruchet 			return ret;
18180f7acb52SHugues Fruchet 
181919a81c14SSteve Longerbeam 		ret = ov5640_restore_mode(sensor);
182019a81c14SSteve Longerbeam 		if (ret)
182119a81c14SSteve Longerbeam 			goto power_off;
182219a81c14SSteve Longerbeam 
1823aa4bb8b8SJacopo Mondi 		/* We're done here for DVP bus, while CSI-2 needs setup. */
18242d95e7edSSakari Ailus 		if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
1825aa4bb8b8SJacopo Mondi 			return 0;
1826aa4bb8b8SJacopo Mondi 
182719a81c14SSteve Longerbeam 		/*
1828aa4bb8b8SJacopo Mondi 		 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
1829aa4bb8b8SJacopo Mondi 		 *
1830aa4bb8b8SJacopo Mondi 		 * 0x300e = 0x40
1831aa4bb8b8SJacopo Mondi 		 * [7:5] = 010	: 2 data lanes mode (see FIXME note in
1832aa4bb8b8SJacopo Mondi 		 *		  "ov5640_set_stream_mipi()")
1833aa4bb8b8SJacopo Mondi 		 * [4] = 0	: Power up MIPI HS Tx
1834aa4bb8b8SJacopo Mondi 		 * [3] = 0	: Power up MIPI LS Rx
1835aa4bb8b8SJacopo Mondi 		 * [2] = 0	: MIPI interface disabled
183619a81c14SSteve Longerbeam 		 */
1837aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
1838aa4bb8b8SJacopo Mondi 				       OV5640_REG_IO_MIPI_CTRL00, 0x40);
183919a81c14SSteve Longerbeam 		if (ret)
184019a81c14SSteve Longerbeam 			goto power_off;
1841aa4bb8b8SJacopo Mondi 
1842aa4bb8b8SJacopo Mondi 		/*
1843aa4bb8b8SJacopo Mondi 		 * Gate clock and set LP11 in 'no packets mode' (idle)
1844aa4bb8b8SJacopo Mondi 		 *
1845aa4bb8b8SJacopo Mondi 		 * 0x4800 = 0x24
1846aa4bb8b8SJacopo Mondi 		 * [5] = 1	: Gate clock when 'no packets'
1847aa4bb8b8SJacopo Mondi 		 * [2] = 1	: MIPI bus in LP11 when 'no packets'
1848aa4bb8b8SJacopo Mondi 		 */
1849aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
1850aa4bb8b8SJacopo Mondi 				       OV5640_REG_MIPI_CTRL00, 0x24);
185119a81c14SSteve Longerbeam 		if (ret)
185219a81c14SSteve Longerbeam 			goto power_off;
1853aa4bb8b8SJacopo Mondi 
1854aa4bb8b8SJacopo Mondi 		/*
1855aa4bb8b8SJacopo Mondi 		 * Set data lanes and clock in LP11 when 'sleeping'
1856aa4bb8b8SJacopo Mondi 		 *
1857aa4bb8b8SJacopo Mondi 		 * 0x3019 = 0x70
1858aa4bb8b8SJacopo Mondi 		 * [6] = 1	: MIPI data lane 2 in LP11 when 'sleeping'
1859aa4bb8b8SJacopo Mondi 		 * [5] = 1	: MIPI data lane 1 in LP11 when 'sleeping'
1860aa4bb8b8SJacopo Mondi 		 * [4] = 1	: MIPI clock lane in LP11 when 'sleeping'
1861aa4bb8b8SJacopo Mondi 		 */
1862aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
1863aa4bb8b8SJacopo Mondi 				       OV5640_REG_PAD_OUTPUT00, 0x70);
1864aa4bb8b8SJacopo Mondi 		if (ret)
1865aa4bb8b8SJacopo Mondi 			goto power_off;
1866aa4bb8b8SJacopo Mondi 
1867aa4bb8b8SJacopo Mondi 		/* Give lanes some time to coax into LP11 state. */
1868aa4bb8b8SJacopo Mondi 		usleep_range(500, 1000);
1869aa4bb8b8SJacopo Mondi 
1870aa4bb8b8SJacopo Mondi 	} else {
18712d95e7edSSakari Ailus 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1872aa4bb8b8SJacopo Mondi 			/* Reset MIPI bus settings to their default values. */
1873aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
1874aa4bb8b8SJacopo Mondi 					 OV5640_REG_IO_MIPI_CTRL00, 0x58);
1875aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
1876aa4bb8b8SJacopo Mondi 					 OV5640_REG_MIPI_CTRL00, 0x04);
1877aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
1878aa4bb8b8SJacopo Mondi 					 OV5640_REG_PAD_OUTPUT00, 0x00);
1879aa4bb8b8SJacopo Mondi 		}
1880aa4bb8b8SJacopo Mondi 
1881aa4bb8b8SJacopo Mondi 		ov5640_set_power_off(sensor);
1882f22996dbSHugues Fruchet 	}
188319a81c14SSteve Longerbeam 
188419a81c14SSteve Longerbeam 	return 0;
188519a81c14SSteve Longerbeam 
188619a81c14SSteve Longerbeam power_off:
18870f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
188819a81c14SSteve Longerbeam 	return ret;
188919a81c14SSteve Longerbeam }
189019a81c14SSteve Longerbeam 
189119a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */
189219a81c14SSteve Longerbeam 
189319a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on)
189419a81c14SSteve Longerbeam {
189519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
189619a81c14SSteve Longerbeam 	int ret = 0;
189719a81c14SSteve Longerbeam 
189819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
189919a81c14SSteve Longerbeam 
190019a81c14SSteve Longerbeam 	/*
190119a81c14SSteve Longerbeam 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
190219a81c14SSteve Longerbeam 	 * update the power state.
190319a81c14SSteve Longerbeam 	 */
190419a81c14SSteve Longerbeam 	if (sensor->power_count == !on) {
190519a81c14SSteve Longerbeam 		ret = ov5640_set_power(sensor, !!on);
190619a81c14SSteve Longerbeam 		if (ret)
190719a81c14SSteve Longerbeam 			goto out;
190819a81c14SSteve Longerbeam 	}
190919a81c14SSteve Longerbeam 
191019a81c14SSteve Longerbeam 	/* Update the power count. */
191119a81c14SSteve Longerbeam 	sensor->power_count += on ? 1 : -1;
191219a81c14SSteve Longerbeam 	WARN_ON(sensor->power_count < 0);
191319a81c14SSteve Longerbeam out:
191419a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
191519a81c14SSteve Longerbeam 
191619a81c14SSteve Longerbeam 	if (on && !ret && sensor->power_count == 1) {
191719a81c14SSteve Longerbeam 		/* restore controls */
191819a81c14SSteve Longerbeam 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
191919a81c14SSteve Longerbeam 	}
192019a81c14SSteve Longerbeam 
192119a81c14SSteve Longerbeam 	return ret;
192219a81c14SSteve Longerbeam }
192319a81c14SSteve Longerbeam 
192419a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
192519a81c14SSteve Longerbeam 				     struct v4l2_fract *fi,
192619a81c14SSteve Longerbeam 				     u32 width, u32 height)
192719a81c14SSteve Longerbeam {
192819a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
192919a81c14SSteve Longerbeam 	u32 minfps, maxfps, fps;
193019a81c14SSteve Longerbeam 	int ret;
193119a81c14SSteve Longerbeam 
193219a81c14SSteve Longerbeam 	minfps = ov5640_framerates[OV5640_15_FPS];
193319a81c14SSteve Longerbeam 	maxfps = ov5640_framerates[OV5640_30_FPS];
193419a81c14SSteve Longerbeam 
193519a81c14SSteve Longerbeam 	if (fi->numerator == 0) {
193619a81c14SSteve Longerbeam 		fi->denominator = maxfps;
193719a81c14SSteve Longerbeam 		fi->numerator = 1;
193819a81c14SSteve Longerbeam 		return OV5640_30_FPS;
193919a81c14SSteve Longerbeam 	}
194019a81c14SSteve Longerbeam 
194119a81c14SSteve Longerbeam 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
194219a81c14SSteve Longerbeam 
194319a81c14SSteve Longerbeam 	fi->numerator = 1;
194419a81c14SSteve Longerbeam 	if (fps > maxfps)
194519a81c14SSteve Longerbeam 		fi->denominator = maxfps;
194619a81c14SSteve Longerbeam 	else if (fps < minfps)
194719a81c14SSteve Longerbeam 		fi->denominator = minfps;
194819a81c14SSteve Longerbeam 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
194919a81c14SSteve Longerbeam 		fi->denominator = maxfps;
195019a81c14SSteve Longerbeam 	else
195119a81c14SSteve Longerbeam 		fi->denominator = minfps;
195219a81c14SSteve Longerbeam 
195319a81c14SSteve Longerbeam 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
195419a81c14SSteve Longerbeam 
195519a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, ret, width, height, false);
195619a81c14SSteve Longerbeam 	return mode ? ret : -EINVAL;
195719a81c14SSteve Longerbeam }
195819a81c14SSteve Longerbeam 
195919a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd,
196019a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
196119a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
196219a81c14SSteve Longerbeam {
196319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
196419a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt *fmt;
196519a81c14SSteve Longerbeam 
196619a81c14SSteve Longerbeam 	if (format->pad != 0)
196719a81c14SSteve Longerbeam 		return -EINVAL;
196819a81c14SSteve Longerbeam 
196919a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
197019a81c14SSteve Longerbeam 
197119a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
197219a81c14SSteve Longerbeam 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
197319a81c14SSteve Longerbeam 						 format->pad);
197419a81c14SSteve Longerbeam 	else
197519a81c14SSteve Longerbeam 		fmt = &sensor->fmt;
197619a81c14SSteve Longerbeam 
197719a81c14SSteve Longerbeam 	format->format = *fmt;
197819a81c14SSteve Longerbeam 
197919a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
198019a81c14SSteve Longerbeam 
198119a81c14SSteve Longerbeam 	return 0;
198219a81c14SSteve Longerbeam }
198319a81c14SSteve Longerbeam 
198419a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
198519a81c14SSteve Longerbeam 				   struct v4l2_mbus_framefmt *fmt,
198619a81c14SSteve Longerbeam 				   enum ov5640_frame_rate fr,
198719a81c14SSteve Longerbeam 				   const struct ov5640_mode_info **new_mode)
198819a81c14SSteve Longerbeam {
198919a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
199019a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
1991e3ee691dSHugues Fruchet 	int i;
199219a81c14SSteve Longerbeam 
199319a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
199419a81c14SSteve Longerbeam 	if (!mode)
199519a81c14SSteve Longerbeam 		return -EINVAL;
1996dba13a0bSMaxime Ripard 	fmt->width = mode->hact;
1997dba13a0bSMaxime Ripard 	fmt->height = mode->vact;
199819a81c14SSteve Longerbeam 
199919a81c14SSteve Longerbeam 	if (new_mode)
200019a81c14SSteve Longerbeam 		*new_mode = mode;
2001e3ee691dSHugues Fruchet 
2002e3ee691dSHugues Fruchet 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2003e3ee691dSHugues Fruchet 		if (ov5640_formats[i].code == fmt->code)
2004e3ee691dSHugues Fruchet 			break;
2005e3ee691dSHugues Fruchet 	if (i >= ARRAY_SIZE(ov5640_formats))
2006e6441fdeSHugues Fruchet 		i = 0;
2007e6441fdeSHugues Fruchet 
2008e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[i].code;
2009e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[i].colorspace;
2010e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2011e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2012e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2013e3ee691dSHugues Fruchet 
201419a81c14SSteve Longerbeam 	return 0;
201519a81c14SSteve Longerbeam }
201619a81c14SSteve Longerbeam 
201719a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd,
201819a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
201919a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
202019a81c14SSteve Longerbeam {
202119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
202219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *new_mode;
2023e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
202419a81c14SSteve Longerbeam 	int ret;
202519a81c14SSteve Longerbeam 
202619a81c14SSteve Longerbeam 	if (format->pad != 0)
202719a81c14SSteve Longerbeam 		return -EINVAL;
202819a81c14SSteve Longerbeam 
202919a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
203019a81c14SSteve Longerbeam 
203119a81c14SSteve Longerbeam 	if (sensor->streaming) {
203219a81c14SSteve Longerbeam 		ret = -EBUSY;
203319a81c14SSteve Longerbeam 		goto out;
203419a81c14SSteve Longerbeam 	}
203519a81c14SSteve Longerbeam 
2036e6441fdeSHugues Fruchet 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
203719a81c14SSteve Longerbeam 				      sensor->current_fr, &new_mode);
203819a81c14SSteve Longerbeam 	if (ret)
203919a81c14SSteve Longerbeam 		goto out;
204019a81c14SSteve Longerbeam 
204119a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
204219a81c14SSteve Longerbeam 		struct v4l2_mbus_framefmt *fmt =
204319a81c14SSteve Longerbeam 			v4l2_subdev_get_try_format(sd, cfg, 0);
204419a81c14SSteve Longerbeam 
2045e6441fdeSHugues Fruchet 		*fmt = *mbus_fmt;
204619a81c14SSteve Longerbeam 		goto out;
204719a81c14SSteve Longerbeam 	}
204819a81c14SSteve Longerbeam 
20496949d864SHugues Fruchet 	if (new_mode != sensor->current_mode) {
205019a81c14SSteve Longerbeam 		sensor->current_mode = new_mode;
205119a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
20526949d864SHugues Fruchet 	}
2053fb98e29fSHugues Fruchet 	if (mbus_fmt->code != sensor->fmt.code) {
2054fb98e29fSHugues Fruchet 		sensor->fmt = *mbus_fmt;
2055fb98e29fSHugues Fruchet 		sensor->pending_fmt_change = true;
2056fb98e29fSHugues Fruchet 	}
205719a81c14SSteve Longerbeam out:
205819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
205919a81c14SSteve Longerbeam 	return ret;
206019a81c14SSteve Longerbeam }
206119a81c14SSteve Longerbeam 
2062e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2063e3ee691dSHugues Fruchet 			       struct v4l2_mbus_framefmt *format)
2064e3ee691dSHugues Fruchet {
2065e3ee691dSHugues Fruchet 	int ret = 0;
2066e3ee691dSHugues Fruchet 	bool is_rgb = false;
2067d47c4126SHugues Fruchet 	bool is_jpeg = false;
2068e3ee691dSHugues Fruchet 	u8 val;
2069e3ee691dSHugues Fruchet 
2070e3ee691dSHugues Fruchet 	switch (format->code) {
2071e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_UYVY8_2X8:
2072e3ee691dSHugues Fruchet 		/* YUV422, UYVY */
2073e3ee691dSHugues Fruchet 		val = 0x3f;
2074e3ee691dSHugues Fruchet 		break;
2075e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_YUYV8_2X8:
2076e3ee691dSHugues Fruchet 		/* YUV422, YUYV */
2077e3ee691dSHugues Fruchet 		val = 0x30;
2078e3ee691dSHugues Fruchet 		break;
2079e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
2080e3ee691dSHugues Fruchet 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2081e3ee691dSHugues Fruchet 		val = 0x6F;
2082e3ee691dSHugues Fruchet 		is_rgb = true;
2083e3ee691dSHugues Fruchet 		break;
2084e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
2085e3ee691dSHugues Fruchet 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2086e3ee691dSHugues Fruchet 		val = 0x61;
2087e3ee691dSHugues Fruchet 		is_rgb = true;
2088e3ee691dSHugues Fruchet 		break;
2089d47c4126SHugues Fruchet 	case MEDIA_BUS_FMT_JPEG_1X8:
2090d47c4126SHugues Fruchet 		/* YUV422, YUYV */
2091d47c4126SHugues Fruchet 		val = 0x30;
2092d47c4126SHugues Fruchet 		is_jpeg = true;
2093d47c4126SHugues Fruchet 		break;
2094e3ee691dSHugues Fruchet 	default:
2095e3ee691dSHugues Fruchet 		return -EINVAL;
2096e3ee691dSHugues Fruchet 	}
2097e3ee691dSHugues Fruchet 
2098e3ee691dSHugues Fruchet 	/* FORMAT CONTROL00: YUV and RGB formatting */
2099e3ee691dSHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
2100e3ee691dSHugues Fruchet 	if (ret)
2101e3ee691dSHugues Fruchet 		return ret;
2102e3ee691dSHugues Fruchet 
2103e3ee691dSHugues Fruchet 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
2104d47c4126SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
2105e3ee691dSHugues Fruchet 			       is_rgb ? 0x01 : 0x00);
2106d47c4126SHugues Fruchet 	if (ret)
2107d47c4126SHugues Fruchet 		return ret;
2108d47c4126SHugues Fruchet 
2109d47c4126SHugues Fruchet 	/*
2110d47c4126SHugues Fruchet 	 * TIMING TC REG21:
2111d47c4126SHugues Fruchet 	 * - [5]:	JPEG enable
2112d47c4126SHugues Fruchet 	 */
2113d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2114d47c4126SHugues Fruchet 			     BIT(5), is_jpeg ? BIT(5) : 0);
2115d47c4126SHugues Fruchet 	if (ret)
2116d47c4126SHugues Fruchet 		return ret;
2117d47c4126SHugues Fruchet 
2118d47c4126SHugues Fruchet 	/*
2119d47c4126SHugues Fruchet 	 * SYSTEM RESET02:
2120d47c4126SHugues Fruchet 	 * - [4]:	Reset JFIFO
2121d47c4126SHugues Fruchet 	 * - [3]:	Reset SFIFO
2122d47c4126SHugues Fruchet 	 * - [2]:	Reset JPEG
2123d47c4126SHugues Fruchet 	 */
2124d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2125d47c4126SHugues Fruchet 			     BIT(4) | BIT(3) | BIT(2),
2126d47c4126SHugues Fruchet 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2127d47c4126SHugues Fruchet 	if (ret)
2128d47c4126SHugues Fruchet 		return ret;
2129d47c4126SHugues Fruchet 
2130d47c4126SHugues Fruchet 	/*
2131d47c4126SHugues Fruchet 	 * CLOCK ENABLE02:
2132d47c4126SHugues Fruchet 	 * - [5]:	Enable JPEG 2x clock
2133d47c4126SHugues Fruchet 	 * - [3]:	Enable JPEG clock
2134d47c4126SHugues Fruchet 	 */
2135d47c4126SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2136d47c4126SHugues Fruchet 			      BIT(5) | BIT(3),
2137d47c4126SHugues Fruchet 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
2138e3ee691dSHugues Fruchet }
213919a81c14SSteve Longerbeam 
214019a81c14SSteve Longerbeam /*
214119a81c14SSteve Longerbeam  * Sensor Controls.
214219a81c14SSteve Longerbeam  */
214319a81c14SSteve Longerbeam 
214419a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
214519a81c14SSteve Longerbeam {
214619a81c14SSteve Longerbeam 	int ret;
214719a81c14SSteve Longerbeam 
214819a81c14SSteve Longerbeam 	if (value) {
214919a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
215019a81c14SSteve Longerbeam 				     BIT(0), BIT(0));
215119a81c14SSteve Longerbeam 		if (ret)
215219a81c14SSteve Longerbeam 			return ret;
215319a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
215419a81c14SSteve Longerbeam 	} else {
215519a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
215619a81c14SSteve Longerbeam 	}
215719a81c14SSteve Longerbeam 
215819a81c14SSteve Longerbeam 	return ret;
215919a81c14SSteve Longerbeam }
216019a81c14SSteve Longerbeam 
216119a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
216219a81c14SSteve Longerbeam {
216319a81c14SSteve Longerbeam 	int ret;
216419a81c14SSteve Longerbeam 
216519a81c14SSteve Longerbeam 	if (value) {
216619a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
216719a81c14SSteve Longerbeam 				     BIT(2), BIT(2));
216819a81c14SSteve Longerbeam 		if (ret)
216919a81c14SSteve Longerbeam 			return ret;
217019a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
217119a81c14SSteve Longerbeam 				       value & 0xff);
217219a81c14SSteve Longerbeam 	} else {
217319a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
217419a81c14SSteve Longerbeam 	}
217519a81c14SSteve Longerbeam 
217619a81c14SSteve Longerbeam 	return ret;
217719a81c14SSteve Longerbeam }
217819a81c14SSteve Longerbeam 
217919a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
218019a81c14SSteve Longerbeam {
218119a81c14SSteve Longerbeam 	int ret;
218219a81c14SSteve Longerbeam 
218319a81c14SSteve Longerbeam 	if (value) {
218419a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
218519a81c14SSteve Longerbeam 				     BIT(1), BIT(1));
218619a81c14SSteve Longerbeam 		if (ret)
218719a81c14SSteve Longerbeam 			return ret;
218819a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
218919a81c14SSteve Longerbeam 				       value & 0xff);
219019a81c14SSteve Longerbeam 		if (ret)
219119a81c14SSteve Longerbeam 			return ret;
219219a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
219319a81c14SSteve Longerbeam 				       value & 0xff);
219419a81c14SSteve Longerbeam 	} else {
219519a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
219619a81c14SSteve Longerbeam 	}
219719a81c14SSteve Longerbeam 
219819a81c14SSteve Longerbeam 	return ret;
219919a81c14SSteve Longerbeam }
220019a81c14SSteve Longerbeam 
220119a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
220219a81c14SSteve Longerbeam {
220319a81c14SSteve Longerbeam 	int ret;
220419a81c14SSteve Longerbeam 
220519a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
220619a81c14SSteve Longerbeam 			     BIT(0), awb ? 0 : 1);
220719a81c14SSteve Longerbeam 	if (ret)
220819a81c14SSteve Longerbeam 		return ret;
220919a81c14SSteve Longerbeam 
221019a81c14SSteve Longerbeam 	if (!awb) {
221119a81c14SSteve Longerbeam 		u16 red = (u16)sensor->ctrls.red_balance->val;
221219a81c14SSteve Longerbeam 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
221319a81c14SSteve Longerbeam 
221419a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
221519a81c14SSteve Longerbeam 		if (ret)
221619a81c14SSteve Longerbeam 			return ret;
221719a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
221819a81c14SSteve Longerbeam 	}
221919a81c14SSteve Longerbeam 
222019a81c14SSteve Longerbeam 	return ret;
222119a81c14SSteve Longerbeam }
222219a81c14SSteve Longerbeam 
22233cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
22243cca8ef5SHugues Fruchet 				    enum v4l2_exposure_auto_type auto_exposure)
222519a81c14SSteve Longerbeam {
222619a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
22273cca8ef5SHugues Fruchet 	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
222819a81c14SSteve Longerbeam 	int ret = 0;
222919a81c14SSteve Longerbeam 
223019a81c14SSteve Longerbeam 	if (ctrls->auto_exp->is_new) {
22313cca8ef5SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, auto_exp);
223219a81c14SSteve Longerbeam 		if (ret)
223319a81c14SSteve Longerbeam 			return ret;
223419a81c14SSteve Longerbeam 	}
223519a81c14SSteve Longerbeam 
22363cca8ef5SHugues Fruchet 	if (!auto_exp && ctrls->exposure->is_new) {
223719a81c14SSteve Longerbeam 		u16 max_exp;
223819a81c14SSteve Longerbeam 
223919a81c14SSteve Longerbeam 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
224019a81c14SSteve Longerbeam 					&max_exp);
224119a81c14SSteve Longerbeam 		if (ret)
224219a81c14SSteve Longerbeam 			return ret;
224319a81c14SSteve Longerbeam 		ret = ov5640_get_vts(sensor);
224419a81c14SSteve Longerbeam 		if (ret < 0)
224519a81c14SSteve Longerbeam 			return ret;
224619a81c14SSteve Longerbeam 		max_exp += ret;
22476146fde3SHugues Fruchet 		ret = 0;
224819a81c14SSteve Longerbeam 
224919a81c14SSteve Longerbeam 		if (ctrls->exposure->val < max_exp)
225019a81c14SSteve Longerbeam 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
225119a81c14SSteve Longerbeam 	}
225219a81c14SSteve Longerbeam 
225319a81c14SSteve Longerbeam 	return ret;
225419a81c14SSteve Longerbeam }
225519a81c14SSteve Longerbeam 
22563cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
225719a81c14SSteve Longerbeam {
225819a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
225919a81c14SSteve Longerbeam 	int ret = 0;
226019a81c14SSteve Longerbeam 
226119a81c14SSteve Longerbeam 	if (ctrls->auto_gain->is_new) {
22623cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, auto_gain);
226319a81c14SSteve Longerbeam 		if (ret)
226419a81c14SSteve Longerbeam 			return ret;
226519a81c14SSteve Longerbeam 	}
226619a81c14SSteve Longerbeam 
22673cca8ef5SHugues Fruchet 	if (!auto_gain && ctrls->gain->is_new)
22683cca8ef5SHugues Fruchet 		ret = ov5640_set_gain(sensor, ctrls->gain->val);
226919a81c14SSteve Longerbeam 
227019a81c14SSteve Longerbeam 	return ret;
227119a81c14SSteve Longerbeam }
227219a81c14SSteve Longerbeam 
227319a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
227419a81c14SSteve Longerbeam {
227519a81c14SSteve Longerbeam 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
227619a81c14SSteve Longerbeam 			      0xa4, value ? 0xa4 : 0);
227719a81c14SSteve Longerbeam }
227819a81c14SSteve Longerbeam 
22791068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
22801068fecaSMylène Josserand {
22811068fecaSMylène Josserand 	int ret;
22821068fecaSMylène Josserand 
22831068fecaSMylène Josserand 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
22841068fecaSMylène Josserand 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
22851068fecaSMylène Josserand 			     0 : BIT(7));
22861068fecaSMylène Josserand 	if (ret)
22871068fecaSMylène Josserand 		return ret;
22881068fecaSMylène Josserand 
22891068fecaSMylène Josserand 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
22901068fecaSMylène Josserand 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
22911068fecaSMylène Josserand 			      BIT(2) : 0);
22921068fecaSMylène Josserand }
22931068fecaSMylène Josserand 
2294ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2295ce85705aSHugues Fruchet {
2296ce85705aSHugues Fruchet 	/*
2297c3f3ba3eSHugues Fruchet 	 * If sensor is mounted upside down, mirror logic is inversed.
2298c3f3ba3eSHugues Fruchet 	 *
2299ce85705aSHugues Fruchet 	 * Sensor is a BSI (Back Side Illuminated) one,
2300ce85705aSHugues Fruchet 	 * so image captured is physically mirrored.
2301ce85705aSHugues Fruchet 	 * This is why mirror logic is inversed in
2302ce85705aSHugues Fruchet 	 * order to cancel this mirror effect.
2303ce85705aSHugues Fruchet 	 */
2304ce85705aSHugues Fruchet 
2305ce85705aSHugues Fruchet 	/*
2306ce85705aSHugues Fruchet 	 * TIMING TC REG21:
2307ce85705aSHugues Fruchet 	 * - [2]:	ISP mirror
2308ce85705aSHugues Fruchet 	 * - [1]:	Sensor mirror
2309ce85705aSHugues Fruchet 	 */
2310ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2311ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2312c3f3ba3eSHugues Fruchet 			      (!(value ^ sensor->upside_down)) ?
2313c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2314ce85705aSHugues Fruchet }
2315ce85705aSHugues Fruchet 
2316ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2317ce85705aSHugues Fruchet {
2318c3f3ba3eSHugues Fruchet 	/* If sensor is mounted upside down, flip logic is inversed */
2319c3f3ba3eSHugues Fruchet 
2320ce85705aSHugues Fruchet 	/*
2321ce85705aSHugues Fruchet 	 * TIMING TC REG20:
2322ce85705aSHugues Fruchet 	 * - [2]:	ISP vflip
2323ce85705aSHugues Fruchet 	 * - [1]:	Sensor vflip
2324ce85705aSHugues Fruchet 	 */
2325ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2326ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2327c3f3ba3eSHugues Fruchet 			      (value ^ sensor->upside_down) ?
2328c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2329ce85705aSHugues Fruchet }
2330ce85705aSHugues Fruchet 
233119a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
233219a81c14SSteve Longerbeam {
233319a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
233419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
233519a81c14SSteve Longerbeam 	int val;
233619a81c14SSteve Longerbeam 
233719a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
233819a81c14SSteve Longerbeam 
233919a81c14SSteve Longerbeam 	switch (ctrl->id) {
234019a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
234119a81c14SSteve Longerbeam 		val = ov5640_get_gain(sensor);
234219a81c14SSteve Longerbeam 		if (val < 0)
234319a81c14SSteve Longerbeam 			return val;
234419a81c14SSteve Longerbeam 		sensor->ctrls.gain->val = val;
234519a81c14SSteve Longerbeam 		break;
234619a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
234719a81c14SSteve Longerbeam 		val = ov5640_get_exposure(sensor);
234819a81c14SSteve Longerbeam 		if (val < 0)
234919a81c14SSteve Longerbeam 			return val;
235019a81c14SSteve Longerbeam 		sensor->ctrls.exposure->val = val;
235119a81c14SSteve Longerbeam 		break;
235219a81c14SSteve Longerbeam 	}
235319a81c14SSteve Longerbeam 
235419a81c14SSteve Longerbeam 	return 0;
235519a81c14SSteve Longerbeam }
235619a81c14SSteve Longerbeam 
235719a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
235819a81c14SSteve Longerbeam {
235919a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
236019a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
236119a81c14SSteve Longerbeam 	int ret;
236219a81c14SSteve Longerbeam 
236319a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
236419a81c14SSteve Longerbeam 
236519a81c14SSteve Longerbeam 	/*
236619a81c14SSteve Longerbeam 	 * If the device is not powered up by the host driver do
236719a81c14SSteve Longerbeam 	 * not apply any controls to H/W at this time. Instead
236819a81c14SSteve Longerbeam 	 * the controls will be restored right after power-up.
236919a81c14SSteve Longerbeam 	 */
237019a81c14SSteve Longerbeam 	if (sensor->power_count == 0)
237119a81c14SSteve Longerbeam 		return 0;
237219a81c14SSteve Longerbeam 
237319a81c14SSteve Longerbeam 	switch (ctrl->id) {
237419a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
237519a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
237619a81c14SSteve Longerbeam 		break;
237719a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
237819a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
237919a81c14SSteve Longerbeam 		break;
238019a81c14SSteve Longerbeam 	case V4L2_CID_AUTO_WHITE_BALANCE:
238119a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
238219a81c14SSteve Longerbeam 		break;
238319a81c14SSteve Longerbeam 	case V4L2_CID_HUE:
238419a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
238519a81c14SSteve Longerbeam 		break;
238619a81c14SSteve Longerbeam 	case V4L2_CID_CONTRAST:
238719a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
238819a81c14SSteve Longerbeam 		break;
238919a81c14SSteve Longerbeam 	case V4L2_CID_SATURATION:
239019a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
239119a81c14SSteve Longerbeam 		break;
239219a81c14SSteve Longerbeam 	case V4L2_CID_TEST_PATTERN:
239319a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
239419a81c14SSteve Longerbeam 		break;
23951068fecaSMylène Josserand 	case V4L2_CID_POWER_LINE_FREQUENCY:
23961068fecaSMylène Josserand 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
23971068fecaSMylène Josserand 		break;
2398ce85705aSHugues Fruchet 	case V4L2_CID_HFLIP:
2399ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2400ce85705aSHugues Fruchet 		break;
2401ce85705aSHugues Fruchet 	case V4L2_CID_VFLIP:
2402ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2403ce85705aSHugues Fruchet 		break;
240419a81c14SSteve Longerbeam 	default:
240519a81c14SSteve Longerbeam 		ret = -EINVAL;
240619a81c14SSteve Longerbeam 		break;
240719a81c14SSteve Longerbeam 	}
240819a81c14SSteve Longerbeam 
240919a81c14SSteve Longerbeam 	return ret;
241019a81c14SSteve Longerbeam }
241119a81c14SSteve Longerbeam 
241219a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
241319a81c14SSteve Longerbeam 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
241419a81c14SSteve Longerbeam 	.s_ctrl = ov5640_s_ctrl,
241519a81c14SSteve Longerbeam };
241619a81c14SSteve Longerbeam 
241719a81c14SSteve Longerbeam static const char * const test_pattern_menu[] = {
241819a81c14SSteve Longerbeam 	"Disabled",
241919a81c14SSteve Longerbeam 	"Color bars",
242019a81c14SSteve Longerbeam };
242119a81c14SSteve Longerbeam 
242219a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor)
242319a81c14SSteve Longerbeam {
242419a81c14SSteve Longerbeam 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
242519a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
242619a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
242719a81c14SSteve Longerbeam 	int ret;
242819a81c14SSteve Longerbeam 
242919a81c14SSteve Longerbeam 	v4l2_ctrl_handler_init(hdl, 32);
243019a81c14SSteve Longerbeam 
243119a81c14SSteve Longerbeam 	/* we can use our own mutex for the ctrl lock */
243219a81c14SSteve Longerbeam 	hdl->lock = &sensor->lock;
243319a81c14SSteve Longerbeam 
243419a81c14SSteve Longerbeam 	/* Auto/manual white balance */
243519a81c14SSteve Longerbeam 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
243619a81c14SSteve Longerbeam 					   V4L2_CID_AUTO_WHITE_BALANCE,
243719a81c14SSteve Longerbeam 					   0, 1, 1, 1);
243819a81c14SSteve Longerbeam 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
243919a81c14SSteve Longerbeam 						0, 4095, 1, 0);
244019a81c14SSteve Longerbeam 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
244119a81c14SSteve Longerbeam 					       0, 4095, 1, 0);
244219a81c14SSteve Longerbeam 	/* Auto/manual exposure */
244319a81c14SSteve Longerbeam 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
244419a81c14SSteve Longerbeam 						 V4L2_CID_EXPOSURE_AUTO,
244519a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_MANUAL, 0,
244619a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_AUTO);
244719a81c14SSteve Longerbeam 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
244819a81c14SSteve Longerbeam 					    0, 65535, 1, 0);
244919a81c14SSteve Longerbeam 	/* Auto/manual gain */
245019a81c14SSteve Longerbeam 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
245119a81c14SSteve Longerbeam 					     0, 1, 1, 1);
245219a81c14SSteve Longerbeam 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
245319a81c14SSteve Longerbeam 					0, 1023, 1, 0);
245419a81c14SSteve Longerbeam 
245519a81c14SSteve Longerbeam 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
245619a81c14SSteve Longerbeam 					      0, 255, 1, 64);
245719a81c14SSteve Longerbeam 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
245819a81c14SSteve Longerbeam 				       0, 359, 1, 0);
245919a81c14SSteve Longerbeam 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
246019a81c14SSteve Longerbeam 					    0, 255, 1, 0);
246119a81c14SSteve Longerbeam 	ctrls->test_pattern =
246219a81c14SSteve Longerbeam 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
246319a81c14SSteve Longerbeam 					     ARRAY_SIZE(test_pattern_menu) - 1,
246419a81c14SSteve Longerbeam 					     0, 0, test_pattern_menu);
2465ce85705aSHugues Fruchet 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2466ce85705aSHugues Fruchet 					 0, 1, 1, 0);
2467ce85705aSHugues Fruchet 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2468ce85705aSHugues Fruchet 					 0, 1, 1, 0);
246919a81c14SSteve Longerbeam 
24701068fecaSMylène Josserand 	ctrls->light_freq =
24711068fecaSMylène Josserand 		v4l2_ctrl_new_std_menu(hdl, ops,
24721068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY,
24731068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
24741068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
24751068fecaSMylène Josserand 
247619a81c14SSteve Longerbeam 	if (hdl->error) {
247719a81c14SSteve Longerbeam 		ret = hdl->error;
247819a81c14SSteve Longerbeam 		goto free_ctrls;
247919a81c14SSteve Longerbeam 	}
248019a81c14SSteve Longerbeam 
248119a81c14SSteve Longerbeam 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
248219a81c14SSteve Longerbeam 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
248319a81c14SSteve Longerbeam 
248419a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
248519a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
248619a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
248719a81c14SSteve Longerbeam 
248819a81c14SSteve Longerbeam 	sensor->sd.ctrl_handler = hdl;
248919a81c14SSteve Longerbeam 	return 0;
249019a81c14SSteve Longerbeam 
249119a81c14SSteve Longerbeam free_ctrls:
249219a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(hdl);
249319a81c14SSteve Longerbeam 	return ret;
249419a81c14SSteve Longerbeam }
249519a81c14SSteve Longerbeam 
249619a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
249719a81c14SSteve Longerbeam 				  struct v4l2_subdev_pad_config *cfg,
249819a81c14SSteve Longerbeam 				  struct v4l2_subdev_frame_size_enum *fse)
249919a81c14SSteve Longerbeam {
250019a81c14SSteve Longerbeam 	if (fse->pad != 0)
250119a81c14SSteve Longerbeam 		return -EINVAL;
250219a81c14SSteve Longerbeam 	if (fse->index >= OV5640_NUM_MODES)
250319a81c14SSteve Longerbeam 		return -EINVAL;
250419a81c14SSteve Longerbeam 
250541d8d7f5SHugues Fruchet 	fse->min_width =
2506dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].hact;
250741d8d7f5SHugues Fruchet 	fse->max_width = fse->min_width;
250841d8d7f5SHugues Fruchet 	fse->min_height =
2509dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].vact;
251041d8d7f5SHugues Fruchet 	fse->max_height = fse->min_height;
251119a81c14SSteve Longerbeam 
251219a81c14SSteve Longerbeam 	return 0;
251319a81c14SSteve Longerbeam }
251419a81c14SSteve Longerbeam 
251519a81c14SSteve Longerbeam static int ov5640_enum_frame_interval(
251619a81c14SSteve Longerbeam 	struct v4l2_subdev *sd,
251719a81c14SSteve Longerbeam 	struct v4l2_subdev_pad_config *cfg,
251819a81c14SSteve Longerbeam 	struct v4l2_subdev_frame_interval_enum *fie)
251919a81c14SSteve Longerbeam {
252019a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
252119a81c14SSteve Longerbeam 	struct v4l2_fract tpf;
252219a81c14SSteve Longerbeam 	int ret;
252319a81c14SSteve Longerbeam 
252419a81c14SSteve Longerbeam 	if (fie->pad != 0)
252519a81c14SSteve Longerbeam 		return -EINVAL;
252619a81c14SSteve Longerbeam 	if (fie->index >= OV5640_NUM_FRAMERATES)
252719a81c14SSteve Longerbeam 		return -EINVAL;
252819a81c14SSteve Longerbeam 
252919a81c14SSteve Longerbeam 	tpf.numerator = 1;
253019a81c14SSteve Longerbeam 	tpf.denominator = ov5640_framerates[fie->index];
253119a81c14SSteve Longerbeam 
253219a81c14SSteve Longerbeam 	ret = ov5640_try_frame_interval(sensor, &tpf,
253319a81c14SSteve Longerbeam 					fie->width, fie->height);
253419a81c14SSteve Longerbeam 	if (ret < 0)
253519a81c14SSteve Longerbeam 		return -EINVAL;
253619a81c14SSteve Longerbeam 
253719a81c14SSteve Longerbeam 	fie->interval = tpf;
253819a81c14SSteve Longerbeam 	return 0;
253919a81c14SSteve Longerbeam }
254019a81c14SSteve Longerbeam 
254119a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
254219a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
254319a81c14SSteve Longerbeam {
254419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
254519a81c14SSteve Longerbeam 
254619a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
254719a81c14SSteve Longerbeam 	fi->interval = sensor->frame_interval;
254819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
254919a81c14SSteve Longerbeam 
255019a81c14SSteve Longerbeam 	return 0;
255119a81c14SSteve Longerbeam }
255219a81c14SSteve Longerbeam 
255319a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
255419a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
255519a81c14SSteve Longerbeam {
255619a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
255719a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
255819a81c14SSteve Longerbeam 	int frame_rate, ret = 0;
255919a81c14SSteve Longerbeam 
256019a81c14SSteve Longerbeam 	if (fi->pad != 0)
256119a81c14SSteve Longerbeam 		return -EINVAL;
256219a81c14SSteve Longerbeam 
256319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
256419a81c14SSteve Longerbeam 
256519a81c14SSteve Longerbeam 	if (sensor->streaming) {
256619a81c14SSteve Longerbeam 		ret = -EBUSY;
256719a81c14SSteve Longerbeam 		goto out;
256819a81c14SSteve Longerbeam 	}
256919a81c14SSteve Longerbeam 
257019a81c14SSteve Longerbeam 	mode = sensor->current_mode;
257119a81c14SSteve Longerbeam 
257219a81c14SSteve Longerbeam 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2573dba13a0bSMaxime Ripard 					       mode->hact, mode->vact);
257419a81c14SSteve Longerbeam 	if (frame_rate < 0)
257519a81c14SSteve Longerbeam 		frame_rate = OV5640_15_FPS;
257619a81c14SSteve Longerbeam 
25773c4a7372SHugues Fruchet 	mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2578dba13a0bSMaxime Ripard 				mode->vact, true);
25793c4a7372SHugues Fruchet 	if (!mode) {
25803c4a7372SHugues Fruchet 		ret = -EINVAL;
25813c4a7372SHugues Fruchet 		goto out;
25823c4a7372SHugues Fruchet 	}
25833c4a7372SHugues Fruchet 
25840929983eSHugues Fruchet 	if (mode != sensor->current_mode ||
25850929983eSHugues Fruchet 	    frame_rate != sensor->current_fr) {
25860929983eSHugues Fruchet 		sensor->current_fr = frame_rate;
25870929983eSHugues Fruchet 		sensor->frame_interval = fi->interval;
25883c4a7372SHugues Fruchet 		sensor->current_mode = mode;
258919a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
25906949d864SHugues Fruchet 	}
259119a81c14SSteve Longerbeam out:
259219a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
259319a81c14SSteve Longerbeam 	return ret;
259419a81c14SSteve Longerbeam }
259519a81c14SSteve Longerbeam 
259619a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
259719a81c14SSteve Longerbeam 				 struct v4l2_subdev_pad_config *cfg,
259819a81c14SSteve Longerbeam 				 struct v4l2_subdev_mbus_code_enum *code)
259919a81c14SSteve Longerbeam {
260019a81c14SSteve Longerbeam 	if (code->pad != 0)
260119a81c14SSteve Longerbeam 		return -EINVAL;
2602e3ee691dSHugues Fruchet 	if (code->index >= ARRAY_SIZE(ov5640_formats))
260319a81c14SSteve Longerbeam 		return -EINVAL;
260419a81c14SSteve Longerbeam 
2605e3ee691dSHugues Fruchet 	code->code = ov5640_formats[code->index].code;
260619a81c14SSteve Longerbeam 	return 0;
260719a81c14SSteve Longerbeam }
260819a81c14SSteve Longerbeam 
260919a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
261019a81c14SSteve Longerbeam {
261119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
261219a81c14SSteve Longerbeam 	int ret = 0;
261319a81c14SSteve Longerbeam 
261419a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
261519a81c14SSteve Longerbeam 
261619a81c14SSteve Longerbeam 	if (sensor->streaming == !enable) {
261719a81c14SSteve Longerbeam 		if (enable && sensor->pending_mode_change) {
2618985cdcb0SHugues Fruchet 			ret = ov5640_set_mode(sensor);
261919a81c14SSteve Longerbeam 			if (ret)
262019a81c14SSteve Longerbeam 				goto out;
2621fb98e29fSHugues Fruchet 		}
2622e3ee691dSHugues Fruchet 
2623fb98e29fSHugues Fruchet 		if (enable && sensor->pending_fmt_change) {
2624e3ee691dSHugues Fruchet 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2625e3ee691dSHugues Fruchet 			if (ret)
2626e3ee691dSHugues Fruchet 				goto out;
2627fb98e29fSHugues Fruchet 			sensor->pending_fmt_change = false;
262819a81c14SSteve Longerbeam 		}
262919a81c14SSteve Longerbeam 
26302d95e7edSSakari Ailus 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2631f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, enable);
2632f22996dbSHugues Fruchet 		else
2633f22996dbSHugues Fruchet 			ret = ov5640_set_stream_dvp(sensor, enable);
2634f22996dbSHugues Fruchet 
263519a81c14SSteve Longerbeam 		if (!ret)
263619a81c14SSteve Longerbeam 			sensor->streaming = enable;
263719a81c14SSteve Longerbeam 	}
263819a81c14SSteve Longerbeam out:
263919a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
264019a81c14SSteve Longerbeam 	return ret;
264119a81c14SSteve Longerbeam }
264219a81c14SSteve Longerbeam 
264319a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = {
264419a81c14SSteve Longerbeam 	.s_power = ov5640_s_power,
26452d18fbc5SAkinobu Mita 	.log_status = v4l2_ctrl_subdev_log_status,
26462d18fbc5SAkinobu Mita 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
26472d18fbc5SAkinobu Mita 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
264819a81c14SSteve Longerbeam };
264919a81c14SSteve Longerbeam 
265019a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = {
265119a81c14SSteve Longerbeam 	.g_frame_interval = ov5640_g_frame_interval,
265219a81c14SSteve Longerbeam 	.s_frame_interval = ov5640_s_frame_interval,
265319a81c14SSteve Longerbeam 	.s_stream = ov5640_s_stream,
265419a81c14SSteve Longerbeam };
265519a81c14SSteve Longerbeam 
265619a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
265719a81c14SSteve Longerbeam 	.enum_mbus_code = ov5640_enum_mbus_code,
265819a81c14SSteve Longerbeam 	.get_fmt = ov5640_get_fmt,
265919a81c14SSteve Longerbeam 	.set_fmt = ov5640_set_fmt,
266019a81c14SSteve Longerbeam 	.enum_frame_size = ov5640_enum_frame_size,
266119a81c14SSteve Longerbeam 	.enum_frame_interval = ov5640_enum_frame_interval,
266219a81c14SSteve Longerbeam };
266319a81c14SSteve Longerbeam 
266419a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = {
266519a81c14SSteve Longerbeam 	.core = &ov5640_core_ops,
266619a81c14SSteve Longerbeam 	.video = &ov5640_video_ops,
266719a81c14SSteve Longerbeam 	.pad = &ov5640_pad_ops,
266819a81c14SSteve Longerbeam };
266919a81c14SSteve Longerbeam 
267019a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor)
267119a81c14SSteve Longerbeam {
267219a81c14SSteve Longerbeam 	int i;
267319a81c14SSteve Longerbeam 
267419a81c14SSteve Longerbeam 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
267519a81c14SSteve Longerbeam 		sensor->supplies[i].supply = ov5640_supply_name[i];
267619a81c14SSteve Longerbeam 
267719a81c14SSteve Longerbeam 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
267819a81c14SSteve Longerbeam 				       OV5640_NUM_SUPPLIES,
267919a81c14SSteve Longerbeam 				       sensor->supplies);
268019a81c14SSteve Longerbeam }
268119a81c14SSteve Longerbeam 
26820f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor)
26830f7acb52SHugues Fruchet {
26840f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
26850f7acb52SHugues Fruchet 	int ret = 0;
26860f7acb52SHugues Fruchet 	u16 chip_id;
26870f7acb52SHugues Fruchet 
26880f7acb52SHugues Fruchet 	ret = ov5640_set_power_on(sensor);
26890f7acb52SHugues Fruchet 	if (ret)
26900f7acb52SHugues Fruchet 		return ret;
26910f7acb52SHugues Fruchet 
26920f7acb52SHugues Fruchet 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
26930f7acb52SHugues Fruchet 	if (ret) {
26940f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
26950f7acb52SHugues Fruchet 			__func__);
26960f7acb52SHugues Fruchet 		goto power_off;
26970f7acb52SHugues Fruchet 	}
26980f7acb52SHugues Fruchet 
26990f7acb52SHugues Fruchet 	if (chip_id != 0x5640) {
27000f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
27010f7acb52SHugues Fruchet 			__func__, chip_id);
27020f7acb52SHugues Fruchet 		ret = -ENXIO;
27030f7acb52SHugues Fruchet 	}
27040f7acb52SHugues Fruchet 
27050f7acb52SHugues Fruchet power_off:
27060f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
27070f7acb52SHugues Fruchet 	return ret;
27080f7acb52SHugues Fruchet }
27090f7acb52SHugues Fruchet 
271019a81c14SSteve Longerbeam static int ov5640_probe(struct i2c_client *client,
271119a81c14SSteve Longerbeam 			const struct i2c_device_id *id)
271219a81c14SSteve Longerbeam {
271319a81c14SSteve Longerbeam 	struct device *dev = &client->dev;
271419a81c14SSteve Longerbeam 	struct fwnode_handle *endpoint;
271519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor;
2716e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *fmt;
2717c3f3ba3eSHugues Fruchet 	u32 rotation;
271819a81c14SSteve Longerbeam 	int ret;
271919a81c14SSteve Longerbeam 
272019a81c14SSteve Longerbeam 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
272119a81c14SSteve Longerbeam 	if (!sensor)
272219a81c14SSteve Longerbeam 		return -ENOMEM;
272319a81c14SSteve Longerbeam 
272419a81c14SSteve Longerbeam 	sensor->i2c_client = client;
2725fb98e29fSHugues Fruchet 
2726fb98e29fSHugues Fruchet 	/*
2727fb98e29fSHugues Fruchet 	 * default init sequence initialize sensor to
2728fb98e29fSHugues Fruchet 	 * YUV422 UYVY VGA@30fps
2729fb98e29fSHugues Fruchet 	 */
2730e6441fdeSHugues Fruchet 	fmt = &sensor->fmt;
2731fb98e29fSHugues Fruchet 	fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
2732fb98e29fSHugues Fruchet 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
2733e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2734e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2735e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2736e6441fdeSHugues Fruchet 	fmt->width = 640;
2737e6441fdeSHugues Fruchet 	fmt->height = 480;
2738e6441fdeSHugues Fruchet 	fmt->field = V4L2_FIELD_NONE;
273919a81c14SSteve Longerbeam 	sensor->frame_interval.numerator = 1;
274019a81c14SSteve Longerbeam 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
274119a81c14SSteve Longerbeam 	sensor->current_fr = OV5640_30_FPS;
274219a81c14SSteve Longerbeam 	sensor->current_mode =
274319a81c14SSteve Longerbeam 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
2744985cdcb0SHugues Fruchet 	sensor->last_mode = sensor->current_mode;
274519a81c14SSteve Longerbeam 
274619a81c14SSteve Longerbeam 	sensor->ae_target = 52;
274719a81c14SSteve Longerbeam 
2748c3f3ba3eSHugues Fruchet 	/* optional indication of physical rotation of sensor */
2749c3f3ba3eSHugues Fruchet 	ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
2750c3f3ba3eSHugues Fruchet 				       &rotation);
2751c3f3ba3eSHugues Fruchet 	if (!ret) {
2752c3f3ba3eSHugues Fruchet 		switch (rotation) {
2753c3f3ba3eSHugues Fruchet 		case 180:
2754c3f3ba3eSHugues Fruchet 			sensor->upside_down = true;
2755c3f3ba3eSHugues Fruchet 			/* fall through */
2756c3f3ba3eSHugues Fruchet 		case 0:
2757c3f3ba3eSHugues Fruchet 			break;
2758c3f3ba3eSHugues Fruchet 		default:
2759c3f3ba3eSHugues Fruchet 			dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
2760c3f3ba3eSHugues Fruchet 				 rotation);
2761c3f3ba3eSHugues Fruchet 		}
2762c3f3ba3eSHugues Fruchet 	}
2763c3f3ba3eSHugues Fruchet 
2764ce96bcf5SSakari Ailus 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2765ce96bcf5SSakari Ailus 						  NULL);
276619a81c14SSteve Longerbeam 	if (!endpoint) {
276719a81c14SSteve Longerbeam 		dev_err(dev, "endpoint node not found\n");
276819a81c14SSteve Longerbeam 		return -EINVAL;
276919a81c14SSteve Longerbeam 	}
277019a81c14SSteve Longerbeam 
277119a81c14SSteve Longerbeam 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
277219a81c14SSteve Longerbeam 	fwnode_handle_put(endpoint);
277319a81c14SSteve Longerbeam 	if (ret) {
277419a81c14SSteve Longerbeam 		dev_err(dev, "Could not parse endpoint\n");
277519a81c14SSteve Longerbeam 		return ret;
277619a81c14SSteve Longerbeam 	}
277719a81c14SSteve Longerbeam 
277819a81c14SSteve Longerbeam 	/* get system clock (xclk) */
277919a81c14SSteve Longerbeam 	sensor->xclk = devm_clk_get(dev, "xclk");
278019a81c14SSteve Longerbeam 	if (IS_ERR(sensor->xclk)) {
278119a81c14SSteve Longerbeam 		dev_err(dev, "failed to get xclk\n");
278219a81c14SSteve Longerbeam 		return PTR_ERR(sensor->xclk);
278319a81c14SSteve Longerbeam 	}
278419a81c14SSteve Longerbeam 
278519a81c14SSteve Longerbeam 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
278619a81c14SSteve Longerbeam 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
278719a81c14SSteve Longerbeam 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
278819a81c14SSteve Longerbeam 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
278919a81c14SSteve Longerbeam 			sensor->xclk_freq);
279019a81c14SSteve Longerbeam 		return -EINVAL;
279119a81c14SSteve Longerbeam 	}
279219a81c14SSteve Longerbeam 
279319a81c14SSteve Longerbeam 	/* request optional power down pin */
279419a81c14SSteve Longerbeam 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
279519a81c14SSteve Longerbeam 						    GPIOD_OUT_HIGH);
279619a81c14SSteve Longerbeam 	/* request optional reset pin */
279719a81c14SSteve Longerbeam 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
279819a81c14SSteve Longerbeam 						     GPIOD_OUT_HIGH);
279919a81c14SSteve Longerbeam 
280019a81c14SSteve Longerbeam 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
280119a81c14SSteve Longerbeam 
28022d18fbc5SAkinobu Mita 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
28032d18fbc5SAkinobu Mita 			    V4L2_SUBDEV_FL_HAS_EVENTS;
280419a81c14SSteve Longerbeam 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
280519a81c14SSteve Longerbeam 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
280619a81c14SSteve Longerbeam 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
280719a81c14SSteve Longerbeam 	if (ret)
280819a81c14SSteve Longerbeam 		return ret;
280919a81c14SSteve Longerbeam 
281019a81c14SSteve Longerbeam 	ret = ov5640_get_regulators(sensor);
281119a81c14SSteve Longerbeam 	if (ret)
281219a81c14SSteve Longerbeam 		return ret;
281319a81c14SSteve Longerbeam 
281419a81c14SSteve Longerbeam 	mutex_init(&sensor->lock);
281519a81c14SSteve Longerbeam 
28160f7acb52SHugues Fruchet 	ret = ov5640_check_chip_id(sensor);
28170f7acb52SHugues Fruchet 	if (ret)
28180f7acb52SHugues Fruchet 		goto entity_cleanup;
28190f7acb52SHugues Fruchet 
282019a81c14SSteve Longerbeam 	ret = ov5640_init_controls(sensor);
282119a81c14SSteve Longerbeam 	if (ret)
282219a81c14SSteve Longerbeam 		goto entity_cleanup;
282319a81c14SSteve Longerbeam 
282419a81c14SSteve Longerbeam 	ret = v4l2_async_register_subdev(&sensor->sd);
282519a81c14SSteve Longerbeam 	if (ret)
282619a81c14SSteve Longerbeam 		goto free_ctrls;
282719a81c14SSteve Longerbeam 
282819a81c14SSteve Longerbeam 	return 0;
282919a81c14SSteve Longerbeam 
283019a81c14SSteve Longerbeam free_ctrls:
283119a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
283219a81c14SSteve Longerbeam entity_cleanup:
283319a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
283419a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
283519a81c14SSteve Longerbeam 	return ret;
283619a81c14SSteve Longerbeam }
283719a81c14SSteve Longerbeam 
283819a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client)
283919a81c14SSteve Longerbeam {
284019a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
284119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
284219a81c14SSteve Longerbeam 
284319a81c14SSteve Longerbeam 	v4l2_async_unregister_subdev(&sensor->sd);
284419a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
284519a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
284619a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
284719a81c14SSteve Longerbeam 
284819a81c14SSteve Longerbeam 	return 0;
284919a81c14SSteve Longerbeam }
285019a81c14SSteve Longerbeam 
285119a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = {
285219a81c14SSteve Longerbeam 	{"ov5640", 0},
285319a81c14SSteve Longerbeam 	{},
285419a81c14SSteve Longerbeam };
285519a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id);
285619a81c14SSteve Longerbeam 
285719a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = {
285819a81c14SSteve Longerbeam 	{ .compatible = "ovti,ov5640" },
285919a81c14SSteve Longerbeam 	{ /* sentinel */ }
286019a81c14SSteve Longerbeam };
286119a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
286219a81c14SSteve Longerbeam 
286319a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = {
286419a81c14SSteve Longerbeam 	.driver = {
286519a81c14SSteve Longerbeam 		.name  = "ov5640",
286619a81c14SSteve Longerbeam 		.of_match_table	= ov5640_dt_ids,
286719a81c14SSteve Longerbeam 	},
286819a81c14SSteve Longerbeam 	.id_table = ov5640_id,
286919a81c14SSteve Longerbeam 	.probe    = ov5640_probe,
287019a81c14SSteve Longerbeam 	.remove   = ov5640_remove,
287119a81c14SSteve Longerbeam };
287219a81c14SSteve Longerbeam 
287319a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver);
287419a81c14SSteve Longerbeam 
287519a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
287619a81c14SSteve Longerbeam MODULE_LICENSE("GPL");
2877