xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision 6949d864)
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>
2819a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h>
2919a81c14SSteve Longerbeam #include <media/v4l2-subdev.h>
3019a81c14SSteve Longerbeam 
3119a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */
3219a81c14SSteve Longerbeam #define OV5640_XCLK_MIN  6000000
3341cb1c73SPhilipp Puschmann #define OV5640_XCLK_MAX 54000000
3419a81c14SSteve Longerbeam 
3519a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c
3619a81c14SSteve Longerbeam 
37d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02		0x3002
38d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
39f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0		0x3008
4019a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID		0x300a
41f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00	0x300e
42f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
43f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
4419a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00		0x3019
45f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1	0x302e
4619a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0		0x3034
4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1		0x3035
4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2		0x3036
4919a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3		0x3037
5019a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID		0x3100
51f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
5219a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
5319a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN		0x3400
5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN		0x3402
5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN		0x3404
5619a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
5719a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL	0x3503
6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
6219a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS		0x350c
6386633417SMaxime Ripard #define OV5640_REG_TIMING_DVPHO		0x3808
6486633417SMaxime Ripard #define OV5640_REG_TIMING_DVPVO		0x380a
6519a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS		0x380c
6619a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS		0x380e
67ce85705aSHugues Fruchet #define OV5640_REG_TIMING_TC_REG20	0x3820
6819a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21	0x3821
6919a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00		0x3a00
7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP		0x3a08
7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP		0x3a0a
7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D		0x3a0d
7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E		0x3a0e
7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F		0x3a0f
7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10		0x3a10
7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11		0x3a11
7719a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B		0x3a1b
7819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E		0x3a1e
7919a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F		0x3a1f
8019a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00	0x3c00
8119a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01	0x3c01
8219a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
8319a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01		0x4202
84e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00	0x4300
85f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00	0x4740
8619a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00		0x4800
8719a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE		0x4814
88e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
8919a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
9019a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0		0x5580
9119a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1		0x5581
9219a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3		0x5583
9319a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4		0x5584
9419a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5		0x5585
9519a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT		0x56a1
9619a81c14SSteve Longerbeam 
978f57c2f8SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT	1
988f57c2f8SMaxime Ripard #define OV5640_SCLK_ROOT_DIVIDER_DEFAULT	2
998f57c2f8SMaxime Ripard 
10019a81c14SSteve Longerbeam enum ov5640_mode_id {
10119a81c14SSteve Longerbeam 	OV5640_MODE_QCIF_176_144 = 0,
10219a81c14SSteve Longerbeam 	OV5640_MODE_QVGA_320_240,
10319a81c14SSteve Longerbeam 	OV5640_MODE_VGA_640_480,
10419a81c14SSteve Longerbeam 	OV5640_MODE_NTSC_720_480,
10519a81c14SSteve Longerbeam 	OV5640_MODE_PAL_720_576,
10619a81c14SSteve Longerbeam 	OV5640_MODE_XGA_1024_768,
10719a81c14SSteve Longerbeam 	OV5640_MODE_720P_1280_720,
10819a81c14SSteve Longerbeam 	OV5640_MODE_1080P_1920_1080,
10919a81c14SSteve Longerbeam 	OV5640_MODE_QSXGA_2592_1944,
11019a81c14SSteve Longerbeam 	OV5640_NUM_MODES,
11119a81c14SSteve Longerbeam };
11219a81c14SSteve Longerbeam 
11319a81c14SSteve Longerbeam enum ov5640_frame_rate {
11419a81c14SSteve Longerbeam 	OV5640_15_FPS = 0,
11519a81c14SSteve Longerbeam 	OV5640_30_FPS,
11619a81c14SSteve Longerbeam 	OV5640_NUM_FRAMERATES,
11719a81c14SSteve Longerbeam };
11819a81c14SSteve Longerbeam 
119e3ee691dSHugues Fruchet struct ov5640_pixfmt {
120e3ee691dSHugues Fruchet 	u32 code;
121e3ee691dSHugues Fruchet 	u32 colorspace;
122e3ee691dSHugues Fruchet };
123e3ee691dSHugues Fruchet 
124e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = {
125d47c4126SHugues Fruchet 	{ MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
126e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
127e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
128e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
129e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
130e3ee691dSHugues Fruchet };
131e3ee691dSHugues Fruchet 
13219a81c14SSteve Longerbeam /*
13319a81c14SSteve Longerbeam  * FIXME: remove this when a subdev API becomes available
13419a81c14SSteve Longerbeam  * to set the MIPI CSI-2 virtual channel.
13519a81c14SSteve Longerbeam  */
13619a81c14SSteve Longerbeam static unsigned int virtual_channel;
1378670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444);
13819a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel,
13919a81c14SSteve Longerbeam 		 "MIPI CSI-2 virtual channel (0..3), default 0");
14019a81c14SSteve Longerbeam 
14119a81c14SSteve Longerbeam static const int ov5640_framerates[] = {
14219a81c14SSteve Longerbeam 	[OV5640_15_FPS] = 15,
14319a81c14SSteve Longerbeam 	[OV5640_30_FPS] = 30,
14419a81c14SSteve Longerbeam };
14519a81c14SSteve Longerbeam 
14619a81c14SSteve Longerbeam /* regulator supplies */
14719a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = {
14841d8d7f5SHugues Fruchet 	"DOVDD", /* Digital I/O (1.8V) supply */
14919a81c14SSteve Longerbeam 	"DVDD",  /* Digital Core (1.5V) supply */
15019a81c14SSteve Longerbeam 	"AVDD",  /* Analog (2.8V) supply */
15119a81c14SSteve Longerbeam };
15219a81c14SSteve Longerbeam 
15319a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
15419a81c14SSteve Longerbeam 
15519a81c14SSteve Longerbeam /*
15619a81c14SSteve Longerbeam  * Image size under 1280 * 960 are SUBSAMPLING
15719a81c14SSteve Longerbeam  * Image size upper 1280 * 960 are SCALING
15819a81c14SSteve Longerbeam  */
15919a81c14SSteve Longerbeam enum ov5640_downsize_mode {
16019a81c14SSteve Longerbeam 	SUBSAMPLING,
16119a81c14SSteve Longerbeam 	SCALING,
16219a81c14SSteve Longerbeam };
16319a81c14SSteve Longerbeam 
16419a81c14SSteve Longerbeam struct reg_value {
16519a81c14SSteve Longerbeam 	u16 reg_addr;
16619a81c14SSteve Longerbeam 	u8 val;
16719a81c14SSteve Longerbeam 	u8 mask;
16819a81c14SSteve Longerbeam 	u32 delay_ms;
16919a81c14SSteve Longerbeam };
17019a81c14SSteve Longerbeam 
17119a81c14SSteve Longerbeam struct ov5640_mode_info {
17219a81c14SSteve Longerbeam 	enum ov5640_mode_id id;
17319a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode;
174dba13a0bSMaxime Ripard 	u32 hact;
175476dec01SMaxime Ripard 	u32 htot;
176dba13a0bSMaxime Ripard 	u32 vact;
177476dec01SMaxime Ripard 	u32 vtot;
17819a81c14SSteve Longerbeam 	const struct reg_value *reg_data;
17919a81c14SSteve Longerbeam 	u32 reg_data_size;
18019a81c14SSteve Longerbeam };
18119a81c14SSteve Longerbeam 
18219a81c14SSteve Longerbeam struct ov5640_ctrls {
18319a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler handler;
18419a81c14SSteve Longerbeam 	struct {
18519a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_exp;
18619a81c14SSteve Longerbeam 		struct v4l2_ctrl *exposure;
18719a81c14SSteve Longerbeam 	};
18819a81c14SSteve Longerbeam 	struct {
18919a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_wb;
19019a81c14SSteve Longerbeam 		struct v4l2_ctrl *blue_balance;
19119a81c14SSteve Longerbeam 		struct v4l2_ctrl *red_balance;
19219a81c14SSteve Longerbeam 	};
19319a81c14SSteve Longerbeam 	struct {
19419a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_gain;
19519a81c14SSteve Longerbeam 		struct v4l2_ctrl *gain;
19619a81c14SSteve Longerbeam 	};
19719a81c14SSteve Longerbeam 	struct v4l2_ctrl *brightness;
1981068fecaSMylène Josserand 	struct v4l2_ctrl *light_freq;
19919a81c14SSteve Longerbeam 	struct v4l2_ctrl *saturation;
20019a81c14SSteve Longerbeam 	struct v4l2_ctrl *contrast;
20119a81c14SSteve Longerbeam 	struct v4l2_ctrl *hue;
20219a81c14SSteve Longerbeam 	struct v4l2_ctrl *test_pattern;
203ce85705aSHugues Fruchet 	struct v4l2_ctrl *hflip;
204ce85705aSHugues Fruchet 	struct v4l2_ctrl *vflip;
20519a81c14SSteve Longerbeam };
20619a81c14SSteve Longerbeam 
20719a81c14SSteve Longerbeam struct ov5640_dev {
20819a81c14SSteve Longerbeam 	struct i2c_client *i2c_client;
20919a81c14SSteve Longerbeam 	struct v4l2_subdev sd;
21019a81c14SSteve Longerbeam 	struct media_pad pad;
21119a81c14SSteve Longerbeam 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
21219a81c14SSteve Longerbeam 	struct clk *xclk; /* system clock to OV5640 */
21319a81c14SSteve Longerbeam 	u32 xclk_freq;
21419a81c14SSteve Longerbeam 
21519a81c14SSteve Longerbeam 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
21619a81c14SSteve Longerbeam 	struct gpio_desc *reset_gpio;
21719a81c14SSteve Longerbeam 	struct gpio_desc *pwdn_gpio;
218c3f3ba3eSHugues Fruchet 	bool   upside_down;
21919a81c14SSteve Longerbeam 
22019a81c14SSteve Longerbeam 	/* lock to protect all members below */
22119a81c14SSteve Longerbeam 	struct mutex lock;
22219a81c14SSteve Longerbeam 
22319a81c14SSteve Longerbeam 	int power_count;
22419a81c14SSteve Longerbeam 
22519a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt fmt;
22619a81c14SSteve Longerbeam 
22719a81c14SSteve Longerbeam 	const struct ov5640_mode_info *current_mode;
22819a81c14SSteve Longerbeam 	enum ov5640_frame_rate current_fr;
22919a81c14SSteve Longerbeam 	struct v4l2_fract frame_interval;
23019a81c14SSteve Longerbeam 
23119a81c14SSteve Longerbeam 	struct ov5640_ctrls ctrls;
23219a81c14SSteve Longerbeam 
23319a81c14SSteve Longerbeam 	u32 prev_sysclk, prev_hts;
23419a81c14SSteve Longerbeam 	u32 ae_low, ae_high, ae_target;
23519a81c14SSteve Longerbeam 
23619a81c14SSteve Longerbeam 	bool pending_mode_change;
23719a81c14SSteve Longerbeam 	bool streaming;
23819a81c14SSteve Longerbeam };
23919a81c14SSteve Longerbeam 
24019a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
24119a81c14SSteve Longerbeam {
24219a81c14SSteve Longerbeam 	return container_of(sd, struct ov5640_dev, sd);
24319a81c14SSteve Longerbeam }
24419a81c14SSteve Longerbeam 
24519a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
24619a81c14SSteve Longerbeam {
24719a81c14SSteve Longerbeam 	return &container_of(ctrl->handler, struct ov5640_dev,
24819a81c14SSteve Longerbeam 			     ctrls.handler)->sd;
24919a81c14SSteve Longerbeam }
25019a81c14SSteve Longerbeam 
25119a81c14SSteve Longerbeam /*
25219a81c14SSteve Longerbeam  * FIXME: all of these register tables are likely filled with
25319a81c14SSteve Longerbeam  * entries that set the register to their power-on default values,
25419a81c14SSteve Longerbeam  * and which are otherwise not touched by this driver. Those entries
25519a81c14SSteve Longerbeam  * should be identified and removed to speed register load time
25619a81c14SSteve Longerbeam  * over i2c.
25719a81c14SSteve Longerbeam  */
25819a81c14SSteve Longerbeam 
25919a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
26019a81c14SSteve Longerbeam 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
26119a81c14SSteve Longerbeam 	{0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
26219a81c14SSteve Longerbeam 	{0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
2638f57c2f8SMaxime Ripard 	{0x3037, 0x13, 0, 0}, {0x3630, 0x36, 0, 0},
26419a81c14SSteve Longerbeam 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
26519a81c14SSteve Longerbeam 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
26619a81c14SSteve Longerbeam 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
26719a81c14SSteve Longerbeam 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
26819a81c14SSteve Longerbeam 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
26919a81c14SSteve Longerbeam 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
27019a81c14SSteve Longerbeam 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
27119a81c14SSteve Longerbeam 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
27219a81c14SSteve Longerbeam 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
27319a81c14SSteve Longerbeam 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
27419a81c14SSteve Longerbeam 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
27519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
27619a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
27719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
27819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
27919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
280476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
28119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
28219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
28319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
28419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
28519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
28619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
28719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
28819a81c14SSteve Longerbeam 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
28919a81c14SSteve Longerbeam 	{0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
29019a81c14SSteve Longerbeam 	{0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
29119a81c14SSteve Longerbeam 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
29219a81c14SSteve Longerbeam 	{0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
29319a81c14SSteve Longerbeam 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
29419a81c14SSteve Longerbeam 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
29519a81c14SSteve Longerbeam 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
29619a81c14SSteve Longerbeam 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
29719a81c14SSteve Longerbeam 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
29819a81c14SSteve Longerbeam 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
29919a81c14SSteve Longerbeam 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
30019a81c14SSteve Longerbeam 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
30119a81c14SSteve Longerbeam 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
30219a81c14SSteve Longerbeam 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
30319a81c14SSteve Longerbeam 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
30419a81c14SSteve Longerbeam 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
30519a81c14SSteve Longerbeam 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
30619a81c14SSteve Longerbeam 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
30719a81c14SSteve Longerbeam 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
30819a81c14SSteve Longerbeam 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
30919a81c14SSteve Longerbeam 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
31019a81c14SSteve Longerbeam 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
31119a81c14SSteve Longerbeam 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
31219a81c14SSteve Longerbeam 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
31319a81c14SSteve Longerbeam 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
31419a81c14SSteve Longerbeam 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
31519a81c14SSteve Longerbeam 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
31619a81c14SSteve Longerbeam 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
31719a81c14SSteve Longerbeam 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
31819a81c14SSteve Longerbeam 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
31919a81c14SSteve Longerbeam 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
32019a81c14SSteve Longerbeam 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
32119a81c14SSteve Longerbeam 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
32219a81c14SSteve Longerbeam 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
32319a81c14SSteve Longerbeam 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
32419a81c14SSteve Longerbeam 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
32519a81c14SSteve Longerbeam 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
32619a81c14SSteve Longerbeam 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
32719a81c14SSteve Longerbeam 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
32819a81c14SSteve Longerbeam 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
32919a81c14SSteve Longerbeam 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
33019a81c14SSteve Longerbeam 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
33119a81c14SSteve Longerbeam 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
33219a81c14SSteve Longerbeam 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
33319a81c14SSteve Longerbeam 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
33419a81c14SSteve Longerbeam 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
33519a81c14SSteve Longerbeam 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
33619a81c14SSteve Longerbeam 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
33719a81c14SSteve Longerbeam 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
33819a81c14SSteve Longerbeam 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
33919a81c14SSteve Longerbeam 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
34019a81c14SSteve Longerbeam 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
34119a81c14SSteve Longerbeam 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
34219a81c14SSteve Longerbeam 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
34319a81c14SSteve Longerbeam };
34419a81c14SSteve Longerbeam 
34519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
34619a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
34719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
348ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
34919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
35019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
35119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
352476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
35319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
35419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
35519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
35619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
35719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
35819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
35919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
36019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
36119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
36219a81c14SSteve Longerbeam };
36319a81c14SSteve Longerbeam 
36419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
36519a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
36619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
367ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
36819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
36919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
37019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
371476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
37219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
37319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
37419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
37519a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
37619a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
37719a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
37819a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
37919a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
38019a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
38119a81c14SSteve Longerbeam };
38219a81c14SSteve Longerbeam 
38319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
38419a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
38519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
386ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
38719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
38819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
38919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
390476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
39119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
39219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
39319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
39419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
39519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
39619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
39719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
39819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
39919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
40086633417SMaxime Ripard 	{0x3035, 0x12, 0, 0},
40119a81c14SSteve Longerbeam };
40219a81c14SSteve Longerbeam 
40319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
40419a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
40519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
406ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
40719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
40819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
40919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
410476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
41119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
41219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
41319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
41419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
41519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
41619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
41719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
41819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
41986633417SMaxime Ripard 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
42019a81c14SSteve Longerbeam };
42119a81c14SSteve Longerbeam 
42219a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
42319a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
42419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
425ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
42619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
42719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
42819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
429476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
43019a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
43119a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
43219a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
43319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
43419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
43519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
43619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
43719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
43819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
43919a81c14SSteve Longerbeam };
44019a81c14SSteve Longerbeam 
44119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
44219a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
44319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
444ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
44519a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
44619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
44719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
448476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
44919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
45019a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
45119a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
45219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
45319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
45419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
45519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
45619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
45719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
45819a81c14SSteve Longerbeam };
45919a81c14SSteve Longerbeam 
46019a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
46119a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
46219a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
463ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
46419a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
46519a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
46619a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
467476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
46819a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
46919a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
47019a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
47119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
47219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
47319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
47419a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
47519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
47619a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
47719a81c14SSteve Longerbeam };
47841d8d7f5SHugues Fruchet 
47919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
48019a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
48119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
482ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
48319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
48419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
48519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
486476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
48719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
48819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
48919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
49019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
49119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
49219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
49319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
49419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
49519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
49619a81c14SSteve Longerbeam };
49719a81c14SSteve Longerbeam 
49819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
49919a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
50019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
501ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
50219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
50319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
50419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
505476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
50619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
50719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
50819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
50919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
51019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
51119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
51219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
51319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
51419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
51519a81c14SSteve Longerbeam };
51619a81c14SSteve Longerbeam 
51719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
51819a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
51919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
520ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
52119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
52219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
52319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
524476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
52519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
52619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
52719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
52819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
52919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
53019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
53119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
53219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
53319a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
53419a81c14SSteve Longerbeam };
53519a81c14SSteve Longerbeam 
53619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
53719a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
53819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
539ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
54019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
54119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
54219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
543476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
54419a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
54519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
54619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
54719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
54819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
54919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
55019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
55119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
55219a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
55319a81c14SSteve Longerbeam };
55419a81c14SSteve Longerbeam 
55519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
55619a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
55719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
558ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
55919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
56019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
56119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
562476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
56319a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
56419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
56519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
56619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
56719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
56819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
56919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
57019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
57119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
57219a81c14SSteve Longerbeam };
57319a81c14SSteve Longerbeam 
57419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
57519a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
57619a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
57719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
578ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
57919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
58019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
58119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
582476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
58319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
58419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
58519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
58619a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
58719a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
58819a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
58919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
59019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
59119a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
59219a81c14SSteve Longerbeam 	{0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
59319a81c14SSteve Longerbeam };
59419a81c14SSteve Longerbeam 
59519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
59619a81c14SSteve Longerbeam 	{0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
59719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
598ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
59919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
60019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
60119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
602476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
60319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
60419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
60519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
60619a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
60719a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
60819a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
60919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
61019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
61119a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
61219a81c14SSteve Longerbeam };
61319a81c14SSteve Longerbeam 
61419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
61519a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
61619a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
61719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
618ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
61919a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
62019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
62119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
622476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
62319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
62419a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
62519a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
62619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
62719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
62819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
62919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
63019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
63119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
63219a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
63319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
63419a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
63519a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
63686633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
637476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
63819a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
63919a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
64019a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
64119a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
64219a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
64319a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
64419a81c14SSteve Longerbeam 	{0x3503, 0, 0, 0},
64519a81c14SSteve Longerbeam };
64619a81c14SSteve Longerbeam 
64719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
64819a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
64919a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
65019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
651ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
65219a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
65319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
65419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
655476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
65619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
65719a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
65819a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
65919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
66019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
66119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
66219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
66319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
66419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
66519a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
66619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
66719a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
66819a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
66986633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
670476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
67119a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
67219a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
67319a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
67419a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
67519a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
67619a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
67719a81c14SSteve Longerbeam };
67819a81c14SSteve Longerbeam 
67919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
68019a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
68119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
682ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
68319a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
68419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
68519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
686476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
68719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
68819a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
68919a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
69019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
69119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
69219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
69319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
69419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
69519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
69619a81c14SSteve Longerbeam };
69719a81c14SSteve Longerbeam 
69819a81c14SSteve Longerbeam /* power-on sensor init reg table */
69919a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = {
700476dec01SMaxime Ripard 	0, SUBSAMPLING, 640, 1896, 480, 984,
701476dec01SMaxime Ripard 	ov5640_init_setting_30fps_VGA,
70219a81c14SSteve Longerbeam 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
70319a81c14SSteve Longerbeam };
70419a81c14SSteve Longerbeam 
70519a81c14SSteve Longerbeam static const struct ov5640_mode_info
70619a81c14SSteve Longerbeam ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
70719a81c14SSteve Longerbeam 	{
708476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
709476dec01SMaxime Ripard 		 176, 1896, 144, 984,
71019a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QCIF_176_144,
71119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
712476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
713476dec01SMaxime Ripard 		 320, 1896, 240, 984,
71419a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QVGA_320_240,
71519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
716476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
717476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
71819a81c14SSteve Longerbeam 		 ov5640_setting_15fps_VGA_640_480,
71919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
720476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
721476dec01SMaxime Ripard 		 720, 1896, 480, 984,
72219a81c14SSteve Longerbeam 		 ov5640_setting_15fps_NTSC_720_480,
72319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
724476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
725476dec01SMaxime Ripard 		 720, 1896, 576, 984,
72619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_PAL_720_576,
72719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
728476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
729476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
73019a81c14SSteve Longerbeam 		 ov5640_setting_15fps_XGA_1024_768,
73119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
732476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
733476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
73419a81c14SSteve Longerbeam 		 ov5640_setting_15fps_720P_1280_720,
73519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
736476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
737476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
73819a81c14SSteve Longerbeam 		 ov5640_setting_15fps_1080P_1920_1080,
73919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
740476dec01SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, SCALING,
741476dec01SMaxime Ripard 		 2592, 2844, 1944, 1968,
74219a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QSXGA_2592_1944,
74319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
74419a81c14SSteve Longerbeam 	}, {
745476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
746476dec01SMaxime Ripard 		 176, 1896, 144, 984,
74719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QCIF_176_144,
74819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
749476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
750476dec01SMaxime Ripard 		 320, 1896, 240, 984,
75119a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QVGA_320_240,
75219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
753476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
754476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
75519a81c14SSteve Longerbeam 		 ov5640_setting_30fps_VGA_640_480,
75619a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
757476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
758476dec01SMaxime Ripard 		 720, 1896, 480, 984,
75919a81c14SSteve Longerbeam 		 ov5640_setting_30fps_NTSC_720_480,
76019a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
761476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
762476dec01SMaxime Ripard 		 720, 1896, 576, 984,
76319a81c14SSteve Longerbeam 		 ov5640_setting_30fps_PAL_720_576,
76419a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
765476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
766476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
76719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_XGA_1024_768,
76819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
769476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
770476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
77119a81c14SSteve Longerbeam 		 ov5640_setting_30fps_720P_1280_720,
77219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
773476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
774476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
77519a81c14SSteve Longerbeam 		 ov5640_setting_30fps_1080P_1920_1080,
77619a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
777476dec01SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, NULL, 0},
77819a81c14SSteve Longerbeam 	},
77919a81c14SSteve Longerbeam };
78019a81c14SSteve Longerbeam 
78119a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor)
78219a81c14SSteve Longerbeam {
78319a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
78419a81c14SSteve Longerbeam 	struct i2c_msg msg;
78519a81c14SSteve Longerbeam 	u8 buf[3];
78619a81c14SSteve Longerbeam 	int ret;
78719a81c14SSteve Longerbeam 
78819a81c14SSteve Longerbeam 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
78919a81c14SSteve Longerbeam 		return 0;
79019a81c14SSteve Longerbeam 
79119a81c14SSteve Longerbeam 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
79219a81c14SSteve Longerbeam 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
79319a81c14SSteve Longerbeam 	buf[2] = client->addr << 1;
79419a81c14SSteve Longerbeam 
79519a81c14SSteve Longerbeam 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
79619a81c14SSteve Longerbeam 	msg.flags = 0;
79719a81c14SSteve Longerbeam 	msg.buf = buf;
79819a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
79919a81c14SSteve Longerbeam 
80019a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
80119a81c14SSteve Longerbeam 	if (ret < 0) {
80219a81c14SSteve Longerbeam 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
80319a81c14SSteve Longerbeam 		return ret;
80419a81c14SSteve Longerbeam 	}
80519a81c14SSteve Longerbeam 
80619a81c14SSteve Longerbeam 	return 0;
80719a81c14SSteve Longerbeam }
80819a81c14SSteve Longerbeam 
80919a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
81019a81c14SSteve Longerbeam {
81119a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
81219a81c14SSteve Longerbeam 	struct i2c_msg msg;
81319a81c14SSteve Longerbeam 	u8 buf[3];
81419a81c14SSteve Longerbeam 	int ret;
81519a81c14SSteve Longerbeam 
81619a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
81719a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
81819a81c14SSteve Longerbeam 	buf[2] = val;
81919a81c14SSteve Longerbeam 
82019a81c14SSteve Longerbeam 	msg.addr = client->addr;
82119a81c14SSteve Longerbeam 	msg.flags = client->flags;
82219a81c14SSteve Longerbeam 	msg.buf = buf;
82319a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
82419a81c14SSteve Longerbeam 
82519a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
82619a81c14SSteve Longerbeam 	if (ret < 0) {
8273924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
82819a81c14SSteve Longerbeam 			__func__, reg, val);
82919a81c14SSteve Longerbeam 		return ret;
83019a81c14SSteve Longerbeam 	}
83119a81c14SSteve Longerbeam 
83219a81c14SSteve Longerbeam 	return 0;
83319a81c14SSteve Longerbeam }
83419a81c14SSteve Longerbeam 
83519a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
83619a81c14SSteve Longerbeam {
83719a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
83819a81c14SSteve Longerbeam 	struct i2c_msg msg[2];
83919a81c14SSteve Longerbeam 	u8 buf[2];
84019a81c14SSteve Longerbeam 	int ret;
84119a81c14SSteve Longerbeam 
84219a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
84319a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
84419a81c14SSteve Longerbeam 
84519a81c14SSteve Longerbeam 	msg[0].addr = client->addr;
84619a81c14SSteve Longerbeam 	msg[0].flags = client->flags;
84719a81c14SSteve Longerbeam 	msg[0].buf = buf;
84819a81c14SSteve Longerbeam 	msg[0].len = sizeof(buf);
84919a81c14SSteve Longerbeam 
85019a81c14SSteve Longerbeam 	msg[1].addr = client->addr;
85119a81c14SSteve Longerbeam 	msg[1].flags = client->flags | I2C_M_RD;
85219a81c14SSteve Longerbeam 	msg[1].buf = buf;
85319a81c14SSteve Longerbeam 	msg[1].len = 1;
85419a81c14SSteve Longerbeam 
85519a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, msg, 2);
8563924c623SHugues Fruchet 	if (ret < 0) {
8573924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x\n",
8583924c623SHugues Fruchet 			__func__, reg);
85919a81c14SSteve Longerbeam 		return ret;
8603924c623SHugues Fruchet 	}
86119a81c14SSteve Longerbeam 
86219a81c14SSteve Longerbeam 	*val = buf[0];
86319a81c14SSteve Longerbeam 	return 0;
86419a81c14SSteve Longerbeam }
86519a81c14SSteve Longerbeam 
86619a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
86719a81c14SSteve Longerbeam {
86819a81c14SSteve Longerbeam 	u8 hi, lo;
86919a81c14SSteve Longerbeam 	int ret;
87019a81c14SSteve Longerbeam 
87119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &hi);
87219a81c14SSteve Longerbeam 	if (ret)
87319a81c14SSteve Longerbeam 		return ret;
87419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
87519a81c14SSteve Longerbeam 	if (ret)
87619a81c14SSteve Longerbeam 		return ret;
87719a81c14SSteve Longerbeam 
87819a81c14SSteve Longerbeam 	*val = ((u16)hi << 8) | (u16)lo;
87919a81c14SSteve Longerbeam 	return 0;
88019a81c14SSteve Longerbeam }
88119a81c14SSteve Longerbeam 
88219a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
88319a81c14SSteve Longerbeam {
88419a81c14SSteve Longerbeam 	int ret;
88519a81c14SSteve Longerbeam 
88619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, reg, val >> 8);
88719a81c14SSteve Longerbeam 	if (ret)
88819a81c14SSteve Longerbeam 		return ret;
88919a81c14SSteve Longerbeam 
89019a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
89119a81c14SSteve Longerbeam }
89219a81c14SSteve Longerbeam 
89319a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
89419a81c14SSteve Longerbeam 			  u8 mask, u8 val)
89519a81c14SSteve Longerbeam {
89619a81c14SSteve Longerbeam 	u8 readval;
89719a81c14SSteve Longerbeam 	int ret;
89819a81c14SSteve Longerbeam 
89919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &readval);
90019a81c14SSteve Longerbeam 	if (ret)
90119a81c14SSteve Longerbeam 		return ret;
90219a81c14SSteve Longerbeam 
90319a81c14SSteve Longerbeam 	readval &= ~mask;
90419a81c14SSteve Longerbeam 	val &= mask;
90519a81c14SSteve Longerbeam 	val |= readval;
90619a81c14SSteve Longerbeam 
90719a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg, val);
90819a81c14SSteve Longerbeam }
90919a81c14SSteve Longerbeam 
91019a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */
91119a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor,
91219a81c14SSteve Longerbeam 			    const struct ov5640_mode_info *mode)
91319a81c14SSteve Longerbeam {
91419a81c14SSteve Longerbeam 	const struct reg_value *regs = mode->reg_data;
91519a81c14SSteve Longerbeam 	unsigned int i;
91619a81c14SSteve Longerbeam 	u32 delay_ms;
91719a81c14SSteve Longerbeam 	u16 reg_addr;
91819a81c14SSteve Longerbeam 	u8 mask, val;
91919a81c14SSteve Longerbeam 	int ret = 0;
92019a81c14SSteve Longerbeam 
92119a81c14SSteve Longerbeam 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
92219a81c14SSteve Longerbeam 		delay_ms = regs->delay_ms;
92319a81c14SSteve Longerbeam 		reg_addr = regs->reg_addr;
92419a81c14SSteve Longerbeam 		val = regs->val;
92519a81c14SSteve Longerbeam 		mask = regs->mask;
92619a81c14SSteve Longerbeam 
92719a81c14SSteve Longerbeam 		if (mask)
92819a81c14SSteve Longerbeam 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
92919a81c14SSteve Longerbeam 		else
93019a81c14SSteve Longerbeam 			ret = ov5640_write_reg(sensor, reg_addr, val);
93119a81c14SSteve Longerbeam 		if (ret)
93219a81c14SSteve Longerbeam 			break;
93319a81c14SSteve Longerbeam 
93419a81c14SSteve Longerbeam 		if (delay_ms)
93519a81c14SSteve Longerbeam 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
93619a81c14SSteve Longerbeam 	}
93719a81c14SSteve Longerbeam 
93819a81c14SSteve Longerbeam 	return ret;
93919a81c14SSteve Longerbeam }
94019a81c14SSteve Longerbeam 
94119a81c14SSteve Longerbeam /* read exposure, in number of line periods */
94219a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor)
94319a81c14SSteve Longerbeam {
94419a81c14SSteve Longerbeam 	int exp, ret;
94519a81c14SSteve Longerbeam 	u8 temp;
94619a81c14SSteve Longerbeam 
94719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
94819a81c14SSteve Longerbeam 	if (ret)
94919a81c14SSteve Longerbeam 		return ret;
95019a81c14SSteve Longerbeam 	exp = ((int)temp & 0x0f) << 16;
95119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
95219a81c14SSteve Longerbeam 	if (ret)
95319a81c14SSteve Longerbeam 		return ret;
95419a81c14SSteve Longerbeam 	exp |= ((int)temp << 8);
95519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
95619a81c14SSteve Longerbeam 	if (ret)
95719a81c14SSteve Longerbeam 		return ret;
95819a81c14SSteve Longerbeam 	exp |= (int)temp;
95919a81c14SSteve Longerbeam 
96019a81c14SSteve Longerbeam 	return exp >> 4;
96119a81c14SSteve Longerbeam }
96219a81c14SSteve Longerbeam 
96319a81c14SSteve Longerbeam /* write exposure, given number of line periods */
96419a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
96519a81c14SSteve Longerbeam {
96619a81c14SSteve Longerbeam 	int ret;
96719a81c14SSteve Longerbeam 
96819a81c14SSteve Longerbeam 	exposure <<= 4;
96919a81c14SSteve Longerbeam 
97019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
97119a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
97219a81c14SSteve Longerbeam 			       exposure & 0xff);
97319a81c14SSteve Longerbeam 	if (ret)
97419a81c14SSteve Longerbeam 		return ret;
97519a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
97619a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
97719a81c14SSteve Longerbeam 			       (exposure >> 8) & 0xff);
97819a81c14SSteve Longerbeam 	if (ret)
97919a81c14SSteve Longerbeam 		return ret;
98019a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor,
98119a81c14SSteve Longerbeam 				OV5640_REG_AEC_PK_EXPOSURE_HI,
98219a81c14SSteve Longerbeam 				(exposure >> 16) & 0x0f);
98319a81c14SSteve Longerbeam }
98419a81c14SSteve Longerbeam 
98519a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor)
98619a81c14SSteve Longerbeam {
98719a81c14SSteve Longerbeam 	u16 gain;
98819a81c14SSteve Longerbeam 	int ret;
98919a81c14SSteve Longerbeam 
99019a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
99119a81c14SSteve Longerbeam 	if (ret)
99219a81c14SSteve Longerbeam 		return ret;
99319a81c14SSteve Longerbeam 
99419a81c14SSteve Longerbeam 	return gain & 0x3ff;
99519a81c14SSteve Longerbeam }
99619a81c14SSteve Longerbeam 
997f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
998f22996dbSHugues Fruchet {
999f22996dbSHugues Fruchet 	int ret;
1000f22996dbSHugues Fruchet 	unsigned int flags = sensor->ep.bus.parallel.flags;
1001f22996dbSHugues Fruchet 	u8 pclk_pol = 0;
1002f22996dbSHugues Fruchet 	u8 hsync_pol = 0;
1003f22996dbSHugues Fruchet 	u8 vsync_pol = 0;
1004f22996dbSHugues Fruchet 
1005f22996dbSHugues Fruchet 	/*
1006f22996dbSHugues Fruchet 	 * Note about parallel port configuration.
1007f22996dbSHugues Fruchet 	 *
1008f22996dbSHugues Fruchet 	 * When configured in parallel mode, the OV5640 will
1009f22996dbSHugues Fruchet 	 * output 10 bits data on DVP data lines [9:0].
1010f22996dbSHugues Fruchet 	 * If only 8 bits data are wanted, the 8 bits data lines
1011f22996dbSHugues Fruchet 	 * of the camera interface must be physically connected
1012f22996dbSHugues Fruchet 	 * on the DVP data lines [9:2].
1013f22996dbSHugues Fruchet 	 *
1014f22996dbSHugues Fruchet 	 * Control lines polarity can be configured through
1015f22996dbSHugues Fruchet 	 * devicetree endpoint control lines properties.
1016f22996dbSHugues Fruchet 	 * If no endpoint control lines properties are set,
1017f22996dbSHugues Fruchet 	 * polarity will be as below:
1018f22996dbSHugues Fruchet 	 * - VSYNC:	active high
1019f22996dbSHugues Fruchet 	 * - HREF:	active low
1020f22996dbSHugues Fruchet 	 * - PCLK:	active low
1021f22996dbSHugues Fruchet 	 */
1022f22996dbSHugues Fruchet 
1023f22996dbSHugues Fruchet 	if (on) {
1024f22996dbSHugues Fruchet 		/*
1025f22996dbSHugues Fruchet 		 * reset MIPI PCLK/SERCLK divider
1026f22996dbSHugues Fruchet 		 *
1027f22996dbSHugues Fruchet 		 * SC PLL CONTRL1 0
1028f22996dbSHugues Fruchet 		 * - [3..0]:	MIPI PCLK/SERCLK divider
1029f22996dbSHugues Fruchet 		 */
1030f22996dbSHugues Fruchet 		ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1031f22996dbSHugues Fruchet 		if (ret)
1032f22996dbSHugues Fruchet 			return ret;
1033f22996dbSHugues Fruchet 
1034f22996dbSHugues Fruchet 		/*
1035f22996dbSHugues Fruchet 		 * configure parallel port control lines polarity
1036f22996dbSHugues Fruchet 		 *
1037f22996dbSHugues Fruchet 		 * POLARITY CTRL0
1038f22996dbSHugues Fruchet 		 * - [5]:	PCLK polarity (0: active low, 1: active high)
1039f22996dbSHugues Fruchet 		 * - [1]:	HREF polarity (0: active low, 1: active high)
1040f22996dbSHugues Fruchet 		 * - [0]:	VSYNC polarity (mismatch here between
1041f22996dbSHugues Fruchet 		 *		datasheet and hardware, 0 is active high
1042f22996dbSHugues Fruchet 		 *		and 1 is active low...)
1043f22996dbSHugues Fruchet 		 */
1044f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1045f22996dbSHugues Fruchet 			pclk_pol = 1;
1046f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1047f22996dbSHugues Fruchet 			hsync_pol = 1;
1048f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1049f22996dbSHugues Fruchet 			vsync_pol = 1;
1050f22996dbSHugues Fruchet 
1051f22996dbSHugues Fruchet 		ret = ov5640_write_reg(sensor,
1052f22996dbSHugues Fruchet 				       OV5640_REG_POLARITY_CTRL00,
1053f22996dbSHugues Fruchet 				       (pclk_pol << 5) |
1054f22996dbSHugues Fruchet 				       (hsync_pol << 1) |
1055f22996dbSHugues Fruchet 				       vsync_pol);
1056f22996dbSHugues Fruchet 
1057f22996dbSHugues Fruchet 		if (ret)
1058f22996dbSHugues Fruchet 			return ret;
1059f22996dbSHugues Fruchet 	}
1060f22996dbSHugues Fruchet 
1061f22996dbSHugues Fruchet 	/*
1062f22996dbSHugues Fruchet 	 * powerdown MIPI TX/RX PHY & disable MIPI
1063f22996dbSHugues Fruchet 	 *
1064f22996dbSHugues Fruchet 	 * MIPI CONTROL 00
1065f22996dbSHugues Fruchet 	 * 4:	 PWDN PHY TX
1066f22996dbSHugues Fruchet 	 * 3:	 PWDN PHY RX
1067f22996dbSHugues Fruchet 	 * 2:	 MIPI enable
1068f22996dbSHugues Fruchet 	 */
1069f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1070f22996dbSHugues Fruchet 			       OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1071f22996dbSHugues Fruchet 	if (ret)
1072f22996dbSHugues Fruchet 		return ret;
1073f22996dbSHugues Fruchet 
1074f22996dbSHugues Fruchet 	/*
1075f22996dbSHugues Fruchet 	 * enable VSYNC/HREF/PCLK DVP control lines
1076f22996dbSHugues Fruchet 	 * & D[9:6] DVP data lines
1077f22996dbSHugues Fruchet 	 *
1078f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 01
1079f22996dbSHugues Fruchet 	 * - 6:		VSYNC output enable
1080f22996dbSHugues Fruchet 	 * - 5:		HREF output enable
1081f22996dbSHugues Fruchet 	 * - 4:		PCLK output enable
1082f22996dbSHugues Fruchet 	 * - [3:0]:	D[9:6] output enable
1083f22996dbSHugues Fruchet 	 */
1084f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1085f22996dbSHugues Fruchet 			       OV5640_REG_PAD_OUTPUT_ENABLE01,
1086f22996dbSHugues Fruchet 			       on ? 0x7f : 0);
1087f22996dbSHugues Fruchet 	if (ret)
1088f22996dbSHugues Fruchet 		return ret;
1089f22996dbSHugues Fruchet 
1090f22996dbSHugues Fruchet 	/*
1091f22996dbSHugues Fruchet 	 * enable D[5:0] DVP data lines
1092f22996dbSHugues Fruchet 	 *
1093f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 02
1094f22996dbSHugues Fruchet 	 * - [7:2]:	D[5:0] output enable
1095f22996dbSHugues Fruchet 	 */
1096f22996dbSHugues Fruchet 	return ov5640_write_reg(sensor,
1097f22996dbSHugues Fruchet 				OV5640_REG_PAD_OUTPUT_ENABLE02,
1098f22996dbSHugues Fruchet 				on ? 0xfc : 0);
1099f22996dbSHugues Fruchet }
1100f22996dbSHugues Fruchet 
1101f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
110219a81c14SSteve Longerbeam {
110319a81c14SSteve Longerbeam 	int ret;
110419a81c14SSteve Longerbeam 
110519a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
110619a81c14SSteve Longerbeam 			     on ? 0 : BIT(5));
110719a81c14SSteve Longerbeam 	if (ret)
110819a81c14SSteve Longerbeam 		return ret;
110919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
111019a81c14SSteve Longerbeam 			       on ? 0x00 : 0x70);
111119a81c14SSteve Longerbeam 	if (ret)
111219a81c14SSteve Longerbeam 		return ret;
111319a81c14SSteve Longerbeam 
111419a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
111519a81c14SSteve Longerbeam 				on ? 0x00 : 0x0f);
111619a81c14SSteve Longerbeam }
111719a81c14SSteve Longerbeam 
111819a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor)
111919a81c14SSteve Longerbeam {
112019a81c14SSteve Longerbeam 	 /* calculate sysclk */
112119a81c14SSteve Longerbeam 	u32 xvclk = sensor->xclk_freq / 10000;
112219a81c14SSteve Longerbeam 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
112319a81c14SSteve Longerbeam 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
112419a81c14SSteve Longerbeam 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
112519a81c14SSteve Longerbeam 	u8 temp1, temp2;
112619a81c14SSteve Longerbeam 	int ret;
112719a81c14SSteve Longerbeam 
112819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
112919a81c14SSteve Longerbeam 	if (ret)
113019a81c14SSteve Longerbeam 		return ret;
113119a81c14SSteve Longerbeam 	temp2 = temp1 & 0x0f;
113219a81c14SSteve Longerbeam 	if (temp2 == 8 || temp2 == 10)
113319a81c14SSteve Longerbeam 		bit_div2x = temp2 / 2;
113419a81c14SSteve Longerbeam 
113519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
113619a81c14SSteve Longerbeam 	if (ret)
113719a81c14SSteve Longerbeam 		return ret;
113819a81c14SSteve Longerbeam 	sysdiv = temp1 >> 4;
113919a81c14SSteve Longerbeam 	if (sysdiv == 0)
114019a81c14SSteve Longerbeam 		sysdiv = 16;
114119a81c14SSteve Longerbeam 
114219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
114319a81c14SSteve Longerbeam 	if (ret)
114419a81c14SSteve Longerbeam 		return ret;
114519a81c14SSteve Longerbeam 	multiplier = temp1;
114619a81c14SSteve Longerbeam 
114719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
114819a81c14SSteve Longerbeam 	if (ret)
114919a81c14SSteve Longerbeam 		return ret;
115019a81c14SSteve Longerbeam 	prediv = temp1 & 0x0f;
115119a81c14SSteve Longerbeam 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
115219a81c14SSteve Longerbeam 
115319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
115419a81c14SSteve Longerbeam 	if (ret)
115519a81c14SSteve Longerbeam 		return ret;
115619a81c14SSteve Longerbeam 	temp2 = temp1 & 0x03;
115719a81c14SSteve Longerbeam 	sclk_rdiv = sclk_rdiv_map[temp2];
115819a81c14SSteve Longerbeam 
115919a81c14SSteve Longerbeam 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
116019a81c14SSteve Longerbeam 		return -EINVAL;
116119a81c14SSteve Longerbeam 
116219a81c14SSteve Longerbeam 	VCO = xvclk * multiplier / prediv;
116319a81c14SSteve Longerbeam 
116419a81c14SSteve Longerbeam 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
116519a81c14SSteve Longerbeam 
116619a81c14SSteve Longerbeam 	return sysclk;
116719a81c14SSteve Longerbeam }
116819a81c14SSteve Longerbeam 
116919a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor)
117019a81c14SSteve Longerbeam {
117119a81c14SSteve Longerbeam 	 /* read HTS from register settings */
117219a81c14SSteve Longerbeam 	u8 mode;
117319a81c14SSteve Longerbeam 	int ret;
117419a81c14SSteve Longerbeam 
117519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
117619a81c14SSteve Longerbeam 	if (ret)
117719a81c14SSteve Longerbeam 		return ret;
117819a81c14SSteve Longerbeam 	mode &= 0xfb;
117919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
118019a81c14SSteve Longerbeam }
118119a81c14SSteve Longerbeam 
118219a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor)
118319a81c14SSteve Longerbeam {
118419a81c14SSteve Longerbeam 	/* read HTS from register settings */
118519a81c14SSteve Longerbeam 	u16 hts;
118619a81c14SSteve Longerbeam 	int ret;
118719a81c14SSteve Longerbeam 
118819a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
118919a81c14SSteve Longerbeam 	if (ret)
119019a81c14SSteve Longerbeam 		return ret;
119119a81c14SSteve Longerbeam 	return hts;
119219a81c14SSteve Longerbeam }
119319a81c14SSteve Longerbeam 
119419a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor)
119519a81c14SSteve Longerbeam {
119619a81c14SSteve Longerbeam 	u16 vts;
119719a81c14SSteve Longerbeam 	int ret;
119819a81c14SSteve Longerbeam 
119919a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
120019a81c14SSteve Longerbeam 	if (ret)
120119a81c14SSteve Longerbeam 		return ret;
120219a81c14SSteve Longerbeam 	return vts;
120319a81c14SSteve Longerbeam }
120419a81c14SSteve Longerbeam 
120519a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
120619a81c14SSteve Longerbeam {
120719a81c14SSteve Longerbeam 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
120819a81c14SSteve Longerbeam }
120919a81c14SSteve Longerbeam 
121019a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor)
121119a81c14SSteve Longerbeam {
121219a81c14SSteve Longerbeam 	/* get banding filter value */
121319a81c14SSteve Longerbeam 	int ret, light_freq = 0;
121419a81c14SSteve Longerbeam 	u8 temp, temp1;
121519a81c14SSteve Longerbeam 
121619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
121719a81c14SSteve Longerbeam 	if (ret)
121819a81c14SSteve Longerbeam 		return ret;
121919a81c14SSteve Longerbeam 
122019a81c14SSteve Longerbeam 	if (temp & 0x80) {
122119a81c14SSteve Longerbeam 		/* manual */
122219a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
122319a81c14SSteve Longerbeam 				      &temp1);
122419a81c14SSteve Longerbeam 		if (ret)
122519a81c14SSteve Longerbeam 			return ret;
122619a81c14SSteve Longerbeam 		if (temp1 & 0x04) {
122719a81c14SSteve Longerbeam 			/* 50Hz */
122819a81c14SSteve Longerbeam 			light_freq = 50;
122919a81c14SSteve Longerbeam 		} else {
123019a81c14SSteve Longerbeam 			/* 60Hz */
123119a81c14SSteve Longerbeam 			light_freq = 60;
123219a81c14SSteve Longerbeam 		}
123319a81c14SSteve Longerbeam 	} else {
123419a81c14SSteve Longerbeam 		/* auto */
123519a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
123619a81c14SSteve Longerbeam 				      &temp1);
123719a81c14SSteve Longerbeam 		if (ret)
123819a81c14SSteve Longerbeam 			return ret;
123919a81c14SSteve Longerbeam 
124019a81c14SSteve Longerbeam 		if (temp1 & 0x01) {
124119a81c14SSteve Longerbeam 			/* 50Hz */
124219a81c14SSteve Longerbeam 			light_freq = 50;
124319a81c14SSteve Longerbeam 		} else {
124419a81c14SSteve Longerbeam 			/* 60Hz */
124519a81c14SSteve Longerbeam 		}
124619a81c14SSteve Longerbeam 	}
124719a81c14SSteve Longerbeam 
124819a81c14SSteve Longerbeam 	return light_freq;
124919a81c14SSteve Longerbeam }
125019a81c14SSteve Longerbeam 
125119a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
125219a81c14SSteve Longerbeam {
125319a81c14SSteve Longerbeam 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
125419a81c14SSteve Longerbeam 	int ret;
125519a81c14SSteve Longerbeam 
125619a81c14SSteve Longerbeam 	/* read preview PCLK */
125719a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
125819a81c14SSteve Longerbeam 	if (ret < 0)
125919a81c14SSteve Longerbeam 		return ret;
126019a81c14SSteve Longerbeam 	if (ret == 0)
126119a81c14SSteve Longerbeam 		return -EINVAL;
126219a81c14SSteve Longerbeam 	sensor->prev_sysclk = ret;
126319a81c14SSteve Longerbeam 	/* read preview HTS */
126419a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
126519a81c14SSteve Longerbeam 	if (ret < 0)
126619a81c14SSteve Longerbeam 		return ret;
126719a81c14SSteve Longerbeam 	if (ret == 0)
126819a81c14SSteve Longerbeam 		return -EINVAL;
126919a81c14SSteve Longerbeam 	sensor->prev_hts = ret;
127019a81c14SSteve Longerbeam 
127119a81c14SSteve Longerbeam 	/* read preview VTS */
127219a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
127319a81c14SSteve Longerbeam 	if (ret < 0)
127419a81c14SSteve Longerbeam 		return ret;
127519a81c14SSteve Longerbeam 	prev_vts = ret;
127619a81c14SSteve Longerbeam 
127719a81c14SSteve Longerbeam 	/* calculate banding filter */
127819a81c14SSteve Longerbeam 	/* 60Hz */
127919a81c14SSteve Longerbeam 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
128019a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
128119a81c14SSteve Longerbeam 	if (ret)
128219a81c14SSteve Longerbeam 		return ret;
128319a81c14SSteve Longerbeam 	if (!band_step60)
128419a81c14SSteve Longerbeam 		return -EINVAL;
128519a81c14SSteve Longerbeam 	max_band60 = (int)((prev_vts - 4) / band_step60);
128619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
128719a81c14SSteve Longerbeam 	if (ret)
128819a81c14SSteve Longerbeam 		return ret;
128919a81c14SSteve Longerbeam 
129019a81c14SSteve Longerbeam 	/* 50Hz */
129119a81c14SSteve Longerbeam 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
129219a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
129319a81c14SSteve Longerbeam 	if (ret)
129419a81c14SSteve Longerbeam 		return ret;
129519a81c14SSteve Longerbeam 	if (!band_step50)
129619a81c14SSteve Longerbeam 		return -EINVAL;
129719a81c14SSteve Longerbeam 	max_band50 = (int)((prev_vts - 4) / band_step50);
129819a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
129919a81c14SSteve Longerbeam }
130019a81c14SSteve Longerbeam 
130119a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
130219a81c14SSteve Longerbeam {
130319a81c14SSteve Longerbeam 	/* stable in high */
130419a81c14SSteve Longerbeam 	u32 fast_high, fast_low;
130519a81c14SSteve Longerbeam 	int ret;
130619a81c14SSteve Longerbeam 
130719a81c14SSteve Longerbeam 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
130819a81c14SSteve Longerbeam 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
130919a81c14SSteve Longerbeam 
131019a81c14SSteve Longerbeam 	fast_high = sensor->ae_high << 1;
131119a81c14SSteve Longerbeam 	if (fast_high > 255)
131219a81c14SSteve Longerbeam 		fast_high = 255;
131319a81c14SSteve Longerbeam 
131419a81c14SSteve Longerbeam 	fast_low = sensor->ae_low >> 1;
131519a81c14SSteve Longerbeam 
131619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
131719a81c14SSteve Longerbeam 	if (ret)
131819a81c14SSteve Longerbeam 		return ret;
131919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
132019a81c14SSteve Longerbeam 	if (ret)
132119a81c14SSteve Longerbeam 		return ret;
132219a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
132319a81c14SSteve Longerbeam 	if (ret)
132419a81c14SSteve Longerbeam 		return ret;
132519a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
132619a81c14SSteve Longerbeam 	if (ret)
132719a81c14SSteve Longerbeam 		return ret;
132819a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
132919a81c14SSteve Longerbeam 	if (ret)
133019a81c14SSteve Longerbeam 		return ret;
133119a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
133219a81c14SSteve Longerbeam }
133319a81c14SSteve Longerbeam 
133419a81c14SSteve Longerbeam static int ov5640_binning_on(struct ov5640_dev *sensor)
133519a81c14SSteve Longerbeam {
133619a81c14SSteve Longerbeam 	u8 temp;
133719a81c14SSteve Longerbeam 	int ret;
133819a81c14SSteve Longerbeam 
133919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
134019a81c14SSteve Longerbeam 	if (ret)
134119a81c14SSteve Longerbeam 		return ret;
134219a81c14SSteve Longerbeam 	temp &= 0xfe;
134319a81c14SSteve Longerbeam 	return temp ? 1 : 0;
134419a81c14SSteve Longerbeam }
134519a81c14SSteve Longerbeam 
1346ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1347ce85705aSHugues Fruchet {
1348ce85705aSHugues Fruchet 	int ret;
1349ce85705aSHugues Fruchet 
1350ce85705aSHugues Fruchet 	/*
1351ce85705aSHugues Fruchet 	 * TIMING TC REG21:
1352ce85705aSHugues Fruchet 	 * - [0]:	Horizontal binning enable
1353ce85705aSHugues Fruchet 	 */
1354ce85705aSHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1355ce85705aSHugues Fruchet 			     BIT(0), enable ? BIT(0) : 0);
1356ce85705aSHugues Fruchet 	if (ret)
1357ce85705aSHugues Fruchet 		return ret;
1358ce85705aSHugues Fruchet 	/*
1359ce85705aSHugues Fruchet 	 * TIMING TC REG20:
1360ce85705aSHugues Fruchet 	 * - [0]:	Undocumented, but hardcoded init sequences
1361ce85705aSHugues Fruchet 	 *		are always setting REG21/REG20 bit 0 to same value...
1362ce85705aSHugues Fruchet 	 */
1363ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1364ce85705aSHugues Fruchet 			      BIT(0), enable ? BIT(0) : 0);
1365ce85705aSHugues Fruchet }
1366ce85705aSHugues Fruchet 
136719a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
136819a81c14SSteve Longerbeam {
13698670d70aSHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
137019a81c14SSteve Longerbeam 	u8 temp, channel = virtual_channel;
137119a81c14SSteve Longerbeam 	int ret;
137219a81c14SSteve Longerbeam 
13738670d70aSHugues Fruchet 	if (channel > 3) {
13748670d70aSHugues Fruchet 		dev_err(&client->dev,
13758670d70aSHugues Fruchet 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
13768670d70aSHugues Fruchet 			__func__, channel);
137719a81c14SSteve Longerbeam 		return -EINVAL;
13788670d70aSHugues Fruchet 	}
137919a81c14SSteve Longerbeam 
138019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
138119a81c14SSteve Longerbeam 	if (ret)
138219a81c14SSteve Longerbeam 		return ret;
138319a81c14SSteve Longerbeam 	temp &= ~(3 << 6);
138419a81c14SSteve Longerbeam 	temp |= (channel << 6);
138519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
138619a81c14SSteve Longerbeam }
138719a81c14SSteve Longerbeam 
1388476dec01SMaxime Ripard static int ov5640_set_timings(struct ov5640_dev *sensor,
1389476dec01SMaxime Ripard 			      const struct ov5640_mode_info *mode)
1390476dec01SMaxime Ripard {
1391476dec01SMaxime Ripard 	int ret;
1392476dec01SMaxime Ripard 
139386633417SMaxime Ripard 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
139486633417SMaxime Ripard 	if (ret < 0)
139586633417SMaxime Ripard 		return ret;
139686633417SMaxime Ripard 
139786633417SMaxime Ripard 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
139886633417SMaxime Ripard 	if (ret < 0)
139986633417SMaxime Ripard 		return ret;
140086633417SMaxime Ripard 
1401476dec01SMaxime Ripard 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1402476dec01SMaxime Ripard 	if (ret < 0)
1403476dec01SMaxime Ripard 		return ret;
1404476dec01SMaxime Ripard 
1405476dec01SMaxime Ripard 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1406476dec01SMaxime Ripard 	if (ret < 0)
1407476dec01SMaxime Ripard 		return ret;
1408476dec01SMaxime Ripard 
1409476dec01SMaxime Ripard 	return 0;
1410476dec01SMaxime Ripard }
1411476dec01SMaxime Ripard 
141219a81c14SSteve Longerbeam static const struct ov5640_mode_info *
141319a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
141419a81c14SSteve Longerbeam 		 int width, int height, bool nearest)
141519a81c14SSteve Longerbeam {
14163c4a7372SHugues Fruchet 	const struct ov5640_mode_info *mode;
141719a81c14SSteve Longerbeam 
14183c4a7372SHugues Fruchet 	mode = v4l2_find_nearest_size(ov5640_mode_data[fr],
14193c4a7372SHugues Fruchet 				      ARRAY_SIZE(ov5640_mode_data[fr]),
14203c4a7372SHugues Fruchet 				      hact, vact,
14213c4a7372SHugues Fruchet 				      width, height);
142219a81c14SSteve Longerbeam 
14233c4a7372SHugues Fruchet 	if (!mode ||
14243c4a7372SHugues Fruchet 	    (!nearest && (mode->hact != width || mode->vact != height)))
14253c4a7372SHugues Fruchet 		return NULL;
142619a81c14SSteve Longerbeam 
142719a81c14SSteve Longerbeam 	return mode;
142819a81c14SSteve Longerbeam }
142919a81c14SSteve Longerbeam 
143019a81c14SSteve Longerbeam /*
143119a81c14SSteve Longerbeam  * sensor changes between scaling and subsampling, go through
143219a81c14SSteve Longerbeam  * exposure calculation
143319a81c14SSteve Longerbeam  */
143441d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
143541d8d7f5SHugues Fruchet 					 const struct ov5640_mode_info *mode)
143619a81c14SSteve Longerbeam {
143719a81c14SSteve Longerbeam 	u32 prev_shutter, prev_gain16;
143819a81c14SSteve Longerbeam 	u32 cap_shutter, cap_gain16;
143919a81c14SSteve Longerbeam 	u32 cap_sysclk, cap_hts, cap_vts;
144019a81c14SSteve Longerbeam 	u32 light_freq, cap_bandfilt, cap_maxband;
144119a81c14SSteve Longerbeam 	u32 cap_gain16_shutter;
144219a81c14SSteve Longerbeam 	u8 average;
144319a81c14SSteve Longerbeam 	int ret;
144419a81c14SSteve Longerbeam 
144541d8d7f5SHugues Fruchet 	if (!mode->reg_data)
144619a81c14SSteve Longerbeam 		return -EINVAL;
144719a81c14SSteve Longerbeam 
144819a81c14SSteve Longerbeam 	/* read preview shutter */
144919a81c14SSteve Longerbeam 	ret = ov5640_get_exposure(sensor);
145019a81c14SSteve Longerbeam 	if (ret < 0)
145119a81c14SSteve Longerbeam 		return ret;
145219a81c14SSteve Longerbeam 	prev_shutter = ret;
145319a81c14SSteve Longerbeam 	ret = ov5640_binning_on(sensor);
145419a81c14SSteve Longerbeam 	if (ret < 0)
145519a81c14SSteve Longerbeam 		return ret;
145619a81c14SSteve Longerbeam 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
145719a81c14SSteve Longerbeam 	    mode->id != OV5640_MODE_1080P_1920_1080)
145819a81c14SSteve Longerbeam 		prev_shutter *= 2;
145919a81c14SSteve Longerbeam 
146019a81c14SSteve Longerbeam 	/* read preview gain */
146119a81c14SSteve Longerbeam 	ret = ov5640_get_gain(sensor);
146219a81c14SSteve Longerbeam 	if (ret < 0)
146319a81c14SSteve Longerbeam 		return ret;
146419a81c14SSteve Longerbeam 	prev_gain16 = ret;
146519a81c14SSteve Longerbeam 
146619a81c14SSteve Longerbeam 	/* get average */
146719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
146819a81c14SSteve Longerbeam 	if (ret)
146919a81c14SSteve Longerbeam 		return ret;
147019a81c14SSteve Longerbeam 
147119a81c14SSteve Longerbeam 	/* turn off night mode for capture */
147219a81c14SSteve Longerbeam 	ret = ov5640_set_night_mode(sensor);
147319a81c14SSteve Longerbeam 	if (ret < 0)
147419a81c14SSteve Longerbeam 		return ret;
147519a81c14SSteve Longerbeam 
147619a81c14SSteve Longerbeam 	/* Write capture setting */
147719a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
147819a81c14SSteve Longerbeam 	if (ret < 0)
147919a81c14SSteve Longerbeam 		return ret;
148019a81c14SSteve Longerbeam 
148119a81c14SSteve Longerbeam 	/* read capture VTS */
148219a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
148319a81c14SSteve Longerbeam 	if (ret < 0)
148419a81c14SSteve Longerbeam 		return ret;
148519a81c14SSteve Longerbeam 	cap_vts = ret;
148619a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
148719a81c14SSteve Longerbeam 	if (ret < 0)
148819a81c14SSteve Longerbeam 		return ret;
148919a81c14SSteve Longerbeam 	if (ret == 0)
149019a81c14SSteve Longerbeam 		return -EINVAL;
149119a81c14SSteve Longerbeam 	cap_hts = ret;
149219a81c14SSteve Longerbeam 
149319a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
149419a81c14SSteve Longerbeam 	if (ret < 0)
149519a81c14SSteve Longerbeam 		return ret;
149619a81c14SSteve Longerbeam 	if (ret == 0)
149719a81c14SSteve Longerbeam 		return -EINVAL;
149819a81c14SSteve Longerbeam 	cap_sysclk = ret;
149919a81c14SSteve Longerbeam 
150019a81c14SSteve Longerbeam 	/* calculate capture banding filter */
150119a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
150219a81c14SSteve Longerbeam 	if (ret < 0)
150319a81c14SSteve Longerbeam 		return ret;
150419a81c14SSteve Longerbeam 	light_freq = ret;
150519a81c14SSteve Longerbeam 
150619a81c14SSteve Longerbeam 	if (light_freq == 60) {
150719a81c14SSteve Longerbeam 		/* 60Hz */
150819a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
150919a81c14SSteve Longerbeam 	} else {
151019a81c14SSteve Longerbeam 		/* 50Hz */
151119a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
151219a81c14SSteve Longerbeam 	}
151319a81c14SSteve Longerbeam 
151419a81c14SSteve Longerbeam 	if (!sensor->prev_sysclk) {
151519a81c14SSteve Longerbeam 		ret = ov5640_get_sysclk(sensor);
151619a81c14SSteve Longerbeam 		if (ret < 0)
151719a81c14SSteve Longerbeam 			return ret;
151819a81c14SSteve Longerbeam 		if (ret == 0)
151919a81c14SSteve Longerbeam 			return -EINVAL;
152019a81c14SSteve Longerbeam 		sensor->prev_sysclk = ret;
152119a81c14SSteve Longerbeam 	}
152219a81c14SSteve Longerbeam 
152319a81c14SSteve Longerbeam 	if (!cap_bandfilt)
152419a81c14SSteve Longerbeam 		return -EINVAL;
152519a81c14SSteve Longerbeam 
152619a81c14SSteve Longerbeam 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
152719a81c14SSteve Longerbeam 
152819a81c14SSteve Longerbeam 	/* calculate capture shutter/gain16 */
152919a81c14SSteve Longerbeam 	if (average > sensor->ae_low && average < sensor->ae_high) {
153019a81c14SSteve Longerbeam 		/* in stable range */
153119a81c14SSteve Longerbeam 		cap_gain16_shutter =
153219a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
153319a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
153419a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts *
153519a81c14SSteve Longerbeam 			sensor->ae_target / average;
153619a81c14SSteve Longerbeam 	} else {
153719a81c14SSteve Longerbeam 		cap_gain16_shutter =
153819a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
153919a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
154019a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts;
154119a81c14SSteve Longerbeam 	}
154219a81c14SSteve Longerbeam 
154319a81c14SSteve Longerbeam 	/* gain to shutter */
154419a81c14SSteve Longerbeam 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
154519a81c14SSteve Longerbeam 		/* shutter < 1/100 */
154619a81c14SSteve Longerbeam 		cap_shutter = cap_gain16_shutter / 16;
154719a81c14SSteve Longerbeam 		if (cap_shutter < 1)
154819a81c14SSteve Longerbeam 			cap_shutter = 1;
154919a81c14SSteve Longerbeam 
155019a81c14SSteve Longerbeam 		cap_gain16 = cap_gain16_shutter / cap_shutter;
155119a81c14SSteve Longerbeam 		if (cap_gain16 < 16)
155219a81c14SSteve Longerbeam 			cap_gain16 = 16;
155319a81c14SSteve Longerbeam 	} else {
155419a81c14SSteve Longerbeam 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
155519a81c14SSteve Longerbeam 			/* exposure reach max */
155619a81c14SSteve Longerbeam 			cap_shutter = cap_bandfilt * cap_maxband;
155719a81c14SSteve Longerbeam 			if (!cap_shutter)
155819a81c14SSteve Longerbeam 				return -EINVAL;
155919a81c14SSteve Longerbeam 
156019a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
156119a81c14SSteve Longerbeam 		} else {
156219a81c14SSteve Longerbeam 			/* 1/100 < (cap_shutter = n/100) =< max */
156319a81c14SSteve Longerbeam 			cap_shutter =
156419a81c14SSteve Longerbeam 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
156519a81c14SSteve Longerbeam 				* cap_bandfilt;
156619a81c14SSteve Longerbeam 			if (!cap_shutter)
156719a81c14SSteve Longerbeam 				return -EINVAL;
156819a81c14SSteve Longerbeam 
156919a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
157019a81c14SSteve Longerbeam 		}
157119a81c14SSteve Longerbeam 	}
157219a81c14SSteve Longerbeam 
157319a81c14SSteve Longerbeam 	/* set capture gain */
157419a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
157519a81c14SSteve Longerbeam 	if (ret)
157619a81c14SSteve Longerbeam 		return ret;
157719a81c14SSteve Longerbeam 
157819a81c14SSteve Longerbeam 	/* write capture shutter */
157919a81c14SSteve Longerbeam 	if (cap_shutter > (cap_vts - 4)) {
158019a81c14SSteve Longerbeam 		cap_vts = cap_shutter + 4;
158119a81c14SSteve Longerbeam 		ret = ov5640_set_vts(sensor, cap_vts);
158219a81c14SSteve Longerbeam 		if (ret < 0)
158319a81c14SSteve Longerbeam 			return ret;
158419a81c14SSteve Longerbeam 	}
158519a81c14SSteve Longerbeam 
158619a81c14SSteve Longerbeam 	/* set exposure */
158719a81c14SSteve Longerbeam 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
158819a81c14SSteve Longerbeam }
158919a81c14SSteve Longerbeam 
159019a81c14SSteve Longerbeam /*
159119a81c14SSteve Longerbeam  * if sensor changes inside scaling or subsampling
159219a81c14SSteve Longerbeam  * change mode directly
159319a81c14SSteve Longerbeam  */
159419a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1595bf4a4b51SMaxime Ripard 				  const struct ov5640_mode_info *mode,
1596bf4a4b51SMaxime Ripard 				  s32 exposure)
159719a81c14SSteve Longerbeam {
159819a81c14SSteve Longerbeam 	int ret;
159919a81c14SSteve Longerbeam 
160041d8d7f5SHugues Fruchet 	if (!mode->reg_data)
160119a81c14SSteve Longerbeam 		return -EINVAL;
160219a81c14SSteve Longerbeam 
160319a81c14SSteve Longerbeam 	/* Write capture setting */
160419a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
160519a81c14SSteve Longerbeam 	if (ret < 0)
160619a81c14SSteve Longerbeam 		return ret;
160719a81c14SSteve Longerbeam 
160819a81c14SSteve Longerbeam 	/* turn auto gain/exposure back on for direct mode */
160919a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
161019a81c14SSteve Longerbeam 	if (ret)
161119a81c14SSteve Longerbeam 		return ret;
1612bf4a4b51SMaxime Ripard 
1613bf4a4b51SMaxime Ripard 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, exposure);
161419a81c14SSteve Longerbeam }
161519a81c14SSteve Longerbeam 
161619a81c14SSteve Longerbeam static int ov5640_set_mode(struct ov5640_dev *sensor,
161719a81c14SSteve Longerbeam 			   const struct ov5640_mode_info *orig_mode)
161819a81c14SSteve Longerbeam {
161919a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = sensor->current_mode;
162019a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1621bf4a4b51SMaxime Ripard 	s32 exposure;
162219a81c14SSteve Longerbeam 	int ret;
162319a81c14SSteve Longerbeam 
162419a81c14SSteve Longerbeam 	dn_mode = mode->dn_mode;
162519a81c14SSteve Longerbeam 	orig_dn_mode = orig_mode->dn_mode;
162619a81c14SSteve Longerbeam 
162719a81c14SSteve Longerbeam 	/* auto gain and exposure must be turned off when changing modes */
162819a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
162919a81c14SSteve Longerbeam 	if (ret)
163019a81c14SSteve Longerbeam 		return ret;
1631bf4a4b51SMaxime Ripard 
1632bf4a4b51SMaxime Ripard 	exposure = sensor->ctrls.auto_exp->val;
1633bf4a4b51SMaxime Ripard 	ret = ov5640_set_exposure(sensor, V4L2_EXPOSURE_MANUAL);
163419a81c14SSteve Longerbeam 	if (ret)
163519a81c14SSteve Longerbeam 		return ret;
163619a81c14SSteve Longerbeam 
163719a81c14SSteve Longerbeam 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
163819a81c14SSteve Longerbeam 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
163919a81c14SSteve Longerbeam 		/*
164019a81c14SSteve Longerbeam 		 * change between subsampling and scaling
164119a81c14SSteve Longerbeam 		 * go through exposure calucation
164219a81c14SSteve Longerbeam 		 */
164319a81c14SSteve Longerbeam 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
164419a81c14SSteve Longerbeam 	} else {
164519a81c14SSteve Longerbeam 		/*
164619a81c14SSteve Longerbeam 		 * change inside subsampling or scaling
164719a81c14SSteve Longerbeam 		 * download firmware directly
164819a81c14SSteve Longerbeam 		 */
1649bf4a4b51SMaxime Ripard 		ret = ov5640_set_mode_direct(sensor, mode, exposure);
165019a81c14SSteve Longerbeam 	}
165119a81c14SSteve Longerbeam 
165219a81c14SSteve Longerbeam 	if (ret < 0)
165319a81c14SSteve Longerbeam 		return ret;
165419a81c14SSteve Longerbeam 
1655476dec01SMaxime Ripard 	ret = ov5640_set_timings(sensor, mode);
1656476dec01SMaxime Ripard 	if (ret < 0)
1657476dec01SMaxime Ripard 		return ret;
1658476dec01SMaxime Ripard 
1659ce85705aSHugues Fruchet 	ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1660ce85705aSHugues Fruchet 	if (ret < 0)
1661ce85705aSHugues Fruchet 		return ret;
166219a81c14SSteve Longerbeam 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
166319a81c14SSteve Longerbeam 	if (ret < 0)
166419a81c14SSteve Longerbeam 		return ret;
166519a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
166619a81c14SSteve Longerbeam 	if (ret < 0)
166719a81c14SSteve Longerbeam 		return ret;
166819a81c14SSteve Longerbeam 	ret = ov5640_set_bandingfilter(sensor);
166919a81c14SSteve Longerbeam 	if (ret < 0)
167019a81c14SSteve Longerbeam 		return ret;
167119a81c14SSteve Longerbeam 	ret = ov5640_set_virtual_channel(sensor);
167219a81c14SSteve Longerbeam 	if (ret < 0)
167319a81c14SSteve Longerbeam 		return ret;
167419a81c14SSteve Longerbeam 
167519a81c14SSteve Longerbeam 	sensor->pending_mode_change = false;
167619a81c14SSteve Longerbeam 
167719a81c14SSteve Longerbeam 	return 0;
167819a81c14SSteve Longerbeam }
167919a81c14SSteve Longerbeam 
168019ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor,
168119ad26f9SAkinobu Mita 			       struct v4l2_mbus_framefmt *format);
168219ad26f9SAkinobu Mita 
168319a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */
168419a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor)
168519a81c14SSteve Longerbeam {
168619a81c14SSteve Longerbeam 	int ret;
168719a81c14SSteve Longerbeam 
168819a81c14SSteve Longerbeam 	/* first load the initial register values */
168919a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
169019a81c14SSteve Longerbeam 	if (ret < 0)
169119a81c14SSteve Longerbeam 		return ret;
169219a81c14SSteve Longerbeam 
16938f57c2f8SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
16948f57c2f8SMaxime Ripard 			     (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) |
16958f57c2f8SMaxime Ripard 			     ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT));
16968f57c2f8SMaxime Ripard 	if (ret)
16978f57c2f8SMaxime Ripard 		return ret;
16988f57c2f8SMaxime Ripard 
169919a81c14SSteve Longerbeam 	/* now restore the last capture mode */
170019ad26f9SAkinobu Mita 	ret = ov5640_set_mode(sensor, &ov5640_mode_init_data);
170119ad26f9SAkinobu Mita 	if (ret < 0)
170219ad26f9SAkinobu Mita 		return ret;
170319ad26f9SAkinobu Mita 
170419ad26f9SAkinobu Mita 	return ov5640_set_framefmt(sensor, &sensor->fmt);
170519a81c14SSteve Longerbeam }
170619a81c14SSteve Longerbeam 
170719a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable)
170819a81c14SSteve Longerbeam {
17091fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
171019a81c14SSteve Longerbeam }
171119a81c14SSteve Longerbeam 
171219a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor)
171319a81c14SSteve Longerbeam {
171419a81c14SSteve Longerbeam 	if (!sensor->reset_gpio)
171519a81c14SSteve Longerbeam 		return;
171619a81c14SSteve Longerbeam 
17171fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
171819a81c14SSteve Longerbeam 
171919a81c14SSteve Longerbeam 	/* camera power cycle */
172019a81c14SSteve Longerbeam 	ov5640_power(sensor, false);
172119a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
172219a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
172319a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
172419a81c14SSteve Longerbeam 
17251fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
172619a81c14SSteve Longerbeam 	usleep_range(1000, 2000);
172719a81c14SSteve Longerbeam 
17281fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
172919a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
173019a81c14SSteve Longerbeam }
173119a81c14SSteve Longerbeam 
17320f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor)
173319a81c14SSteve Longerbeam {
17340f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
17350f7acb52SHugues Fruchet 	int ret;
173619a81c14SSteve Longerbeam 
17370f7acb52SHugues Fruchet 	ret = clk_prepare_enable(sensor->xclk);
17380f7acb52SHugues Fruchet 	if (ret) {
17390f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable clock\n",
17400f7acb52SHugues Fruchet 			__func__);
17410f7acb52SHugues Fruchet 		return ret;
17420f7acb52SHugues Fruchet 	}
174319a81c14SSteve Longerbeam 
174419a81c14SSteve Longerbeam 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
174519a81c14SSteve Longerbeam 				    sensor->supplies);
17460f7acb52SHugues Fruchet 	if (ret) {
17470f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable regulators\n",
17480f7acb52SHugues Fruchet 			__func__);
174919a81c14SSteve Longerbeam 		goto xclk_off;
17500f7acb52SHugues Fruchet 	}
175119a81c14SSteve Longerbeam 
175219a81c14SSteve Longerbeam 	ov5640_reset(sensor);
175319a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
175419a81c14SSteve Longerbeam 
175519a81c14SSteve Longerbeam 	ret = ov5640_init_slave_id(sensor);
175619a81c14SSteve Longerbeam 	if (ret)
175719a81c14SSteve Longerbeam 		goto power_off;
175819a81c14SSteve Longerbeam 
17590f7acb52SHugues Fruchet 	return 0;
17600f7acb52SHugues Fruchet 
17610f7acb52SHugues Fruchet power_off:
17620f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
17630f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
17640f7acb52SHugues Fruchet xclk_off:
17650f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
17660f7acb52SHugues Fruchet 	return ret;
17670f7acb52SHugues Fruchet }
17680f7acb52SHugues Fruchet 
17690f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor)
17700f7acb52SHugues Fruchet {
17710f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
17720f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
17730f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
17740f7acb52SHugues Fruchet }
17750f7acb52SHugues Fruchet 
17760f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
17770f7acb52SHugues Fruchet {
17780f7acb52SHugues Fruchet 	int ret = 0;
17790f7acb52SHugues Fruchet 
17800f7acb52SHugues Fruchet 	if (on) {
17810f7acb52SHugues Fruchet 		ret = ov5640_set_power_on(sensor);
17820f7acb52SHugues Fruchet 		if (ret)
17830f7acb52SHugues Fruchet 			return ret;
17840f7acb52SHugues Fruchet 
178519a81c14SSteve Longerbeam 		ret = ov5640_restore_mode(sensor);
178619a81c14SSteve Longerbeam 		if (ret)
178719a81c14SSteve Longerbeam 			goto power_off;
178819a81c14SSteve Longerbeam 
1789f22996dbSHugues Fruchet 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2) {
179019a81c14SSteve Longerbeam 			/*
179119a81c14SSteve Longerbeam 			 * start streaming briefly followed by stream off in
179219a81c14SSteve Longerbeam 			 * order to coax the clock lane into LP-11 state.
179319a81c14SSteve Longerbeam 			 */
1794f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, true);
179519a81c14SSteve Longerbeam 			if (ret)
179619a81c14SSteve Longerbeam 				goto power_off;
179719a81c14SSteve Longerbeam 			usleep_range(1000, 2000);
1798f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, false);
179919a81c14SSteve Longerbeam 			if (ret)
180019a81c14SSteve Longerbeam 				goto power_off;
1801f22996dbSHugues Fruchet 		}
180219a81c14SSteve Longerbeam 
180319a81c14SSteve Longerbeam 		return 0;
180419a81c14SSteve Longerbeam 	}
180519a81c14SSteve Longerbeam 
180619a81c14SSteve Longerbeam power_off:
18070f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
180819a81c14SSteve Longerbeam 	return ret;
180919a81c14SSteve Longerbeam }
181019a81c14SSteve Longerbeam 
181119a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */
181219a81c14SSteve Longerbeam 
181319a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on)
181419a81c14SSteve Longerbeam {
181519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
181619a81c14SSteve Longerbeam 	int ret = 0;
181719a81c14SSteve Longerbeam 
181819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
181919a81c14SSteve Longerbeam 
182019a81c14SSteve Longerbeam 	/*
182119a81c14SSteve Longerbeam 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
182219a81c14SSteve Longerbeam 	 * update the power state.
182319a81c14SSteve Longerbeam 	 */
182419a81c14SSteve Longerbeam 	if (sensor->power_count == !on) {
182519a81c14SSteve Longerbeam 		ret = ov5640_set_power(sensor, !!on);
182619a81c14SSteve Longerbeam 		if (ret)
182719a81c14SSteve Longerbeam 			goto out;
182819a81c14SSteve Longerbeam 	}
182919a81c14SSteve Longerbeam 
183019a81c14SSteve Longerbeam 	/* Update the power count. */
183119a81c14SSteve Longerbeam 	sensor->power_count += on ? 1 : -1;
183219a81c14SSteve Longerbeam 	WARN_ON(sensor->power_count < 0);
183319a81c14SSteve Longerbeam out:
183419a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
183519a81c14SSteve Longerbeam 
183619a81c14SSteve Longerbeam 	if (on && !ret && sensor->power_count == 1) {
183719a81c14SSteve Longerbeam 		/* restore controls */
183819a81c14SSteve Longerbeam 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
183919a81c14SSteve Longerbeam 	}
184019a81c14SSteve Longerbeam 
184119a81c14SSteve Longerbeam 	return ret;
184219a81c14SSteve Longerbeam }
184319a81c14SSteve Longerbeam 
184419a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
184519a81c14SSteve Longerbeam 				     struct v4l2_fract *fi,
184619a81c14SSteve Longerbeam 				     u32 width, u32 height)
184719a81c14SSteve Longerbeam {
184819a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
184919a81c14SSteve Longerbeam 	u32 minfps, maxfps, fps;
185019a81c14SSteve Longerbeam 	int ret;
185119a81c14SSteve Longerbeam 
185219a81c14SSteve Longerbeam 	minfps = ov5640_framerates[OV5640_15_FPS];
185319a81c14SSteve Longerbeam 	maxfps = ov5640_framerates[OV5640_30_FPS];
185419a81c14SSteve Longerbeam 
185519a81c14SSteve Longerbeam 	if (fi->numerator == 0) {
185619a81c14SSteve Longerbeam 		fi->denominator = maxfps;
185719a81c14SSteve Longerbeam 		fi->numerator = 1;
185819a81c14SSteve Longerbeam 		return OV5640_30_FPS;
185919a81c14SSteve Longerbeam 	}
186019a81c14SSteve Longerbeam 
186119a81c14SSteve Longerbeam 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
186219a81c14SSteve Longerbeam 
186319a81c14SSteve Longerbeam 	fi->numerator = 1;
186419a81c14SSteve Longerbeam 	if (fps > maxfps)
186519a81c14SSteve Longerbeam 		fi->denominator = maxfps;
186619a81c14SSteve Longerbeam 	else if (fps < minfps)
186719a81c14SSteve Longerbeam 		fi->denominator = minfps;
186819a81c14SSteve Longerbeam 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
186919a81c14SSteve Longerbeam 		fi->denominator = maxfps;
187019a81c14SSteve Longerbeam 	else
187119a81c14SSteve Longerbeam 		fi->denominator = minfps;
187219a81c14SSteve Longerbeam 
187319a81c14SSteve Longerbeam 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
187419a81c14SSteve Longerbeam 
187519a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, ret, width, height, false);
187619a81c14SSteve Longerbeam 	return mode ? ret : -EINVAL;
187719a81c14SSteve Longerbeam }
187819a81c14SSteve Longerbeam 
187919a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd,
188019a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
188119a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
188219a81c14SSteve Longerbeam {
188319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
188419a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt *fmt;
188519a81c14SSteve Longerbeam 
188619a81c14SSteve Longerbeam 	if (format->pad != 0)
188719a81c14SSteve Longerbeam 		return -EINVAL;
188819a81c14SSteve Longerbeam 
188919a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
189019a81c14SSteve Longerbeam 
189119a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
189219a81c14SSteve Longerbeam 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
189319a81c14SSteve Longerbeam 						 format->pad);
189419a81c14SSteve Longerbeam 	else
189519a81c14SSteve Longerbeam 		fmt = &sensor->fmt;
189619a81c14SSteve Longerbeam 
189719a81c14SSteve Longerbeam 	format->format = *fmt;
189819a81c14SSteve Longerbeam 
189919a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
190019a81c14SSteve Longerbeam 
190119a81c14SSteve Longerbeam 	return 0;
190219a81c14SSteve Longerbeam }
190319a81c14SSteve Longerbeam 
190419a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
190519a81c14SSteve Longerbeam 				   struct v4l2_mbus_framefmt *fmt,
190619a81c14SSteve Longerbeam 				   enum ov5640_frame_rate fr,
190719a81c14SSteve Longerbeam 				   const struct ov5640_mode_info **new_mode)
190819a81c14SSteve Longerbeam {
190919a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
191019a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
1911e3ee691dSHugues Fruchet 	int i;
191219a81c14SSteve Longerbeam 
191319a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
191419a81c14SSteve Longerbeam 	if (!mode)
191519a81c14SSteve Longerbeam 		return -EINVAL;
1916dba13a0bSMaxime Ripard 	fmt->width = mode->hact;
1917dba13a0bSMaxime Ripard 	fmt->height = mode->vact;
191819a81c14SSteve Longerbeam 
191919a81c14SSteve Longerbeam 	if (new_mode)
192019a81c14SSteve Longerbeam 		*new_mode = mode;
1921e3ee691dSHugues Fruchet 
1922e3ee691dSHugues Fruchet 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
1923e3ee691dSHugues Fruchet 		if (ov5640_formats[i].code == fmt->code)
1924e3ee691dSHugues Fruchet 			break;
1925e3ee691dSHugues Fruchet 	if (i >= ARRAY_SIZE(ov5640_formats))
1926e6441fdeSHugues Fruchet 		i = 0;
1927e6441fdeSHugues Fruchet 
1928e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[i].code;
1929e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[i].colorspace;
1930e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1931e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1932e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1933e3ee691dSHugues Fruchet 
193419a81c14SSteve Longerbeam 	return 0;
193519a81c14SSteve Longerbeam }
193619a81c14SSteve Longerbeam 
193719a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd,
193819a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
193919a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
194019a81c14SSteve Longerbeam {
194119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
194219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *new_mode;
1943e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
194419a81c14SSteve Longerbeam 	int ret;
194519a81c14SSteve Longerbeam 
194619a81c14SSteve Longerbeam 	if (format->pad != 0)
194719a81c14SSteve Longerbeam 		return -EINVAL;
194819a81c14SSteve Longerbeam 
194919a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
195019a81c14SSteve Longerbeam 
195119a81c14SSteve Longerbeam 	if (sensor->streaming) {
195219a81c14SSteve Longerbeam 		ret = -EBUSY;
195319a81c14SSteve Longerbeam 		goto out;
195419a81c14SSteve Longerbeam 	}
195519a81c14SSteve Longerbeam 
1956e6441fdeSHugues Fruchet 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
195719a81c14SSteve Longerbeam 				      sensor->current_fr, &new_mode);
195819a81c14SSteve Longerbeam 	if (ret)
195919a81c14SSteve Longerbeam 		goto out;
196019a81c14SSteve Longerbeam 
196119a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
196219a81c14SSteve Longerbeam 		struct v4l2_mbus_framefmt *fmt =
196319a81c14SSteve Longerbeam 			v4l2_subdev_get_try_format(sd, cfg, 0);
196419a81c14SSteve Longerbeam 
1965e6441fdeSHugues Fruchet 		*fmt = *mbus_fmt;
196619a81c14SSteve Longerbeam 		goto out;
196719a81c14SSteve Longerbeam 	}
196819a81c14SSteve Longerbeam 
19696949d864SHugues Fruchet 	if (new_mode != sensor->current_mode) {
197019a81c14SSteve Longerbeam 		sensor->current_mode = new_mode;
1971e6441fdeSHugues Fruchet 		sensor->fmt = *mbus_fmt;
197219a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
19736949d864SHugues Fruchet 	}
197419a81c14SSteve Longerbeam out:
197519a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
197619a81c14SSteve Longerbeam 	return ret;
197719a81c14SSteve Longerbeam }
197819a81c14SSteve Longerbeam 
1979e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1980e3ee691dSHugues Fruchet 			       struct v4l2_mbus_framefmt *format)
1981e3ee691dSHugues Fruchet {
1982e3ee691dSHugues Fruchet 	int ret = 0;
1983e3ee691dSHugues Fruchet 	bool is_rgb = false;
1984d47c4126SHugues Fruchet 	bool is_jpeg = false;
1985e3ee691dSHugues Fruchet 	u8 val;
1986e3ee691dSHugues Fruchet 
1987e3ee691dSHugues Fruchet 	switch (format->code) {
1988e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_UYVY8_2X8:
1989e3ee691dSHugues Fruchet 		/* YUV422, UYVY */
1990e3ee691dSHugues Fruchet 		val = 0x3f;
1991e3ee691dSHugues Fruchet 		break;
1992e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_YUYV8_2X8:
1993e3ee691dSHugues Fruchet 		/* YUV422, YUYV */
1994e3ee691dSHugues Fruchet 		val = 0x30;
1995e3ee691dSHugues Fruchet 		break;
1996e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
1997e3ee691dSHugues Fruchet 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
1998e3ee691dSHugues Fruchet 		val = 0x6F;
1999e3ee691dSHugues Fruchet 		is_rgb = true;
2000e3ee691dSHugues Fruchet 		break;
2001e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
2002e3ee691dSHugues Fruchet 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2003e3ee691dSHugues Fruchet 		val = 0x61;
2004e3ee691dSHugues Fruchet 		is_rgb = true;
2005e3ee691dSHugues Fruchet 		break;
2006d47c4126SHugues Fruchet 	case MEDIA_BUS_FMT_JPEG_1X8:
2007d47c4126SHugues Fruchet 		/* YUV422, YUYV */
2008d47c4126SHugues Fruchet 		val = 0x30;
2009d47c4126SHugues Fruchet 		is_jpeg = true;
2010d47c4126SHugues Fruchet 		break;
2011e3ee691dSHugues Fruchet 	default:
2012e3ee691dSHugues Fruchet 		return -EINVAL;
2013e3ee691dSHugues Fruchet 	}
2014e3ee691dSHugues Fruchet 
2015e3ee691dSHugues Fruchet 	/* FORMAT CONTROL00: YUV and RGB formatting */
2016e3ee691dSHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
2017e3ee691dSHugues Fruchet 	if (ret)
2018e3ee691dSHugues Fruchet 		return ret;
2019e3ee691dSHugues Fruchet 
2020e3ee691dSHugues Fruchet 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
2021d47c4126SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
2022e3ee691dSHugues Fruchet 			       is_rgb ? 0x01 : 0x00);
2023d47c4126SHugues Fruchet 	if (ret)
2024d47c4126SHugues Fruchet 		return ret;
2025d47c4126SHugues Fruchet 
2026d47c4126SHugues Fruchet 	/*
2027d47c4126SHugues Fruchet 	 * TIMING TC REG21:
2028d47c4126SHugues Fruchet 	 * - [5]:	JPEG enable
2029d47c4126SHugues Fruchet 	 */
2030d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2031d47c4126SHugues Fruchet 			     BIT(5), is_jpeg ? BIT(5) : 0);
2032d47c4126SHugues Fruchet 	if (ret)
2033d47c4126SHugues Fruchet 		return ret;
2034d47c4126SHugues Fruchet 
2035d47c4126SHugues Fruchet 	/*
2036d47c4126SHugues Fruchet 	 * SYSTEM RESET02:
2037d47c4126SHugues Fruchet 	 * - [4]:	Reset JFIFO
2038d47c4126SHugues Fruchet 	 * - [3]:	Reset SFIFO
2039d47c4126SHugues Fruchet 	 * - [2]:	Reset JPEG
2040d47c4126SHugues Fruchet 	 */
2041d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2042d47c4126SHugues Fruchet 			     BIT(4) | BIT(3) | BIT(2),
2043d47c4126SHugues Fruchet 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2044d47c4126SHugues Fruchet 	if (ret)
2045d47c4126SHugues Fruchet 		return ret;
2046d47c4126SHugues Fruchet 
2047d47c4126SHugues Fruchet 	/*
2048d47c4126SHugues Fruchet 	 * CLOCK ENABLE02:
2049d47c4126SHugues Fruchet 	 * - [5]:	Enable JPEG 2x clock
2050d47c4126SHugues Fruchet 	 * - [3]:	Enable JPEG clock
2051d47c4126SHugues Fruchet 	 */
2052d47c4126SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2053d47c4126SHugues Fruchet 			      BIT(5) | BIT(3),
2054d47c4126SHugues Fruchet 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
2055e3ee691dSHugues Fruchet }
205619a81c14SSteve Longerbeam 
205719a81c14SSteve Longerbeam /*
205819a81c14SSteve Longerbeam  * Sensor Controls.
205919a81c14SSteve Longerbeam  */
206019a81c14SSteve Longerbeam 
206119a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
206219a81c14SSteve Longerbeam {
206319a81c14SSteve Longerbeam 	int ret;
206419a81c14SSteve Longerbeam 
206519a81c14SSteve Longerbeam 	if (value) {
206619a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
206719a81c14SSteve Longerbeam 				     BIT(0), BIT(0));
206819a81c14SSteve Longerbeam 		if (ret)
206919a81c14SSteve Longerbeam 			return ret;
207019a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
207119a81c14SSteve Longerbeam 	} else {
207219a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
207319a81c14SSteve Longerbeam 	}
207419a81c14SSteve Longerbeam 
207519a81c14SSteve Longerbeam 	return ret;
207619a81c14SSteve Longerbeam }
207719a81c14SSteve Longerbeam 
207819a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
207919a81c14SSteve Longerbeam {
208019a81c14SSteve Longerbeam 	int ret;
208119a81c14SSteve Longerbeam 
208219a81c14SSteve Longerbeam 	if (value) {
208319a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
208419a81c14SSteve Longerbeam 				     BIT(2), BIT(2));
208519a81c14SSteve Longerbeam 		if (ret)
208619a81c14SSteve Longerbeam 			return ret;
208719a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
208819a81c14SSteve Longerbeam 				       value & 0xff);
208919a81c14SSteve Longerbeam 	} else {
209019a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
209119a81c14SSteve Longerbeam 	}
209219a81c14SSteve Longerbeam 
209319a81c14SSteve Longerbeam 	return ret;
209419a81c14SSteve Longerbeam }
209519a81c14SSteve Longerbeam 
209619a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
209719a81c14SSteve Longerbeam {
209819a81c14SSteve Longerbeam 	int ret;
209919a81c14SSteve Longerbeam 
210019a81c14SSteve Longerbeam 	if (value) {
210119a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
210219a81c14SSteve Longerbeam 				     BIT(1), BIT(1));
210319a81c14SSteve Longerbeam 		if (ret)
210419a81c14SSteve Longerbeam 			return ret;
210519a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
210619a81c14SSteve Longerbeam 				       value & 0xff);
210719a81c14SSteve Longerbeam 		if (ret)
210819a81c14SSteve Longerbeam 			return ret;
210919a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
211019a81c14SSteve Longerbeam 				       value & 0xff);
211119a81c14SSteve Longerbeam 	} else {
211219a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
211319a81c14SSteve Longerbeam 	}
211419a81c14SSteve Longerbeam 
211519a81c14SSteve Longerbeam 	return ret;
211619a81c14SSteve Longerbeam }
211719a81c14SSteve Longerbeam 
211819a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
211919a81c14SSteve Longerbeam {
212019a81c14SSteve Longerbeam 	int ret;
212119a81c14SSteve Longerbeam 
212219a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
212319a81c14SSteve Longerbeam 			     BIT(0), awb ? 0 : 1);
212419a81c14SSteve Longerbeam 	if (ret)
212519a81c14SSteve Longerbeam 		return ret;
212619a81c14SSteve Longerbeam 
212719a81c14SSteve Longerbeam 	if (!awb) {
212819a81c14SSteve Longerbeam 		u16 red = (u16)sensor->ctrls.red_balance->val;
212919a81c14SSteve Longerbeam 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
213019a81c14SSteve Longerbeam 
213119a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
213219a81c14SSteve Longerbeam 		if (ret)
213319a81c14SSteve Longerbeam 			return ret;
213419a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
213519a81c14SSteve Longerbeam 	}
213619a81c14SSteve Longerbeam 
213719a81c14SSteve Longerbeam 	return ret;
213819a81c14SSteve Longerbeam }
213919a81c14SSteve Longerbeam 
214019a81c14SSteve Longerbeam static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
214119a81c14SSteve Longerbeam {
214219a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
214319a81c14SSteve Longerbeam 	bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
214419a81c14SSteve Longerbeam 	int ret = 0;
214519a81c14SSteve Longerbeam 
214619a81c14SSteve Longerbeam 	if (ctrls->auto_exp->is_new) {
214719a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
214819a81c14SSteve Longerbeam 				     BIT(0), auto_exposure ? 0 : BIT(0));
214919a81c14SSteve Longerbeam 		if (ret)
215019a81c14SSteve Longerbeam 			return ret;
215119a81c14SSteve Longerbeam 	}
215219a81c14SSteve Longerbeam 
215319a81c14SSteve Longerbeam 	if (!auto_exposure && ctrls->exposure->is_new) {
215419a81c14SSteve Longerbeam 		u16 max_exp;
215519a81c14SSteve Longerbeam 
215619a81c14SSteve Longerbeam 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
215719a81c14SSteve Longerbeam 					&max_exp);
215819a81c14SSteve Longerbeam 		if (ret)
215919a81c14SSteve Longerbeam 			return ret;
216019a81c14SSteve Longerbeam 		ret = ov5640_get_vts(sensor);
216119a81c14SSteve Longerbeam 		if (ret < 0)
216219a81c14SSteve Longerbeam 			return ret;
216319a81c14SSteve Longerbeam 		max_exp += ret;
21646146fde3SHugues Fruchet 		ret = 0;
216519a81c14SSteve Longerbeam 
216619a81c14SSteve Longerbeam 		if (ctrls->exposure->val < max_exp)
216719a81c14SSteve Longerbeam 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
216819a81c14SSteve Longerbeam 	}
216919a81c14SSteve Longerbeam 
217019a81c14SSteve Longerbeam 	return ret;
217119a81c14SSteve Longerbeam }
217219a81c14SSteve Longerbeam 
217319a81c14SSteve Longerbeam static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
217419a81c14SSteve Longerbeam {
217519a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
217619a81c14SSteve Longerbeam 	int ret = 0;
217719a81c14SSteve Longerbeam 
217819a81c14SSteve Longerbeam 	if (ctrls->auto_gain->is_new) {
217919a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
218041d8d7f5SHugues Fruchet 				     BIT(1),
218141d8d7f5SHugues Fruchet 				     ctrls->auto_gain->val ? 0 : BIT(1));
218219a81c14SSteve Longerbeam 		if (ret)
218319a81c14SSteve Longerbeam 			return ret;
218419a81c14SSteve Longerbeam 	}
218519a81c14SSteve Longerbeam 
218619a81c14SSteve Longerbeam 	if (!auto_gain && ctrls->gain->is_new) {
218719a81c14SSteve Longerbeam 		u16 gain = (u16)ctrls->gain->val;
218819a81c14SSteve Longerbeam 
218919a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
219019a81c14SSteve Longerbeam 					 gain & 0x3ff);
219119a81c14SSteve Longerbeam 	}
219219a81c14SSteve Longerbeam 
219319a81c14SSteve Longerbeam 	return ret;
219419a81c14SSteve Longerbeam }
219519a81c14SSteve Longerbeam 
219619a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
219719a81c14SSteve Longerbeam {
219819a81c14SSteve Longerbeam 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
219919a81c14SSteve Longerbeam 			      0xa4, value ? 0xa4 : 0);
220019a81c14SSteve Longerbeam }
220119a81c14SSteve Longerbeam 
22021068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
22031068fecaSMylène Josserand {
22041068fecaSMylène Josserand 	int ret;
22051068fecaSMylène Josserand 
22061068fecaSMylène Josserand 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
22071068fecaSMylène Josserand 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
22081068fecaSMylène Josserand 			     0 : BIT(7));
22091068fecaSMylène Josserand 	if (ret)
22101068fecaSMylène Josserand 		return ret;
22111068fecaSMylène Josserand 
22121068fecaSMylène Josserand 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
22131068fecaSMylène Josserand 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
22141068fecaSMylène Josserand 			      BIT(2) : 0);
22151068fecaSMylène Josserand }
22161068fecaSMylène Josserand 
2217ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2218ce85705aSHugues Fruchet {
2219ce85705aSHugues Fruchet 	/*
2220c3f3ba3eSHugues Fruchet 	 * If sensor is mounted upside down, mirror logic is inversed.
2221c3f3ba3eSHugues Fruchet 	 *
2222ce85705aSHugues Fruchet 	 * Sensor is a BSI (Back Side Illuminated) one,
2223ce85705aSHugues Fruchet 	 * so image captured is physically mirrored.
2224ce85705aSHugues Fruchet 	 * This is why mirror logic is inversed in
2225ce85705aSHugues Fruchet 	 * order to cancel this mirror effect.
2226ce85705aSHugues Fruchet 	 */
2227ce85705aSHugues Fruchet 
2228ce85705aSHugues Fruchet 	/*
2229ce85705aSHugues Fruchet 	 * TIMING TC REG21:
2230ce85705aSHugues Fruchet 	 * - [2]:	ISP mirror
2231ce85705aSHugues Fruchet 	 * - [1]:	Sensor mirror
2232ce85705aSHugues Fruchet 	 */
2233ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2234ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2235c3f3ba3eSHugues Fruchet 			      (!(value ^ sensor->upside_down)) ?
2236c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2237ce85705aSHugues Fruchet }
2238ce85705aSHugues Fruchet 
2239ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2240ce85705aSHugues Fruchet {
2241c3f3ba3eSHugues Fruchet 	/* If sensor is mounted upside down, flip logic is inversed */
2242c3f3ba3eSHugues Fruchet 
2243ce85705aSHugues Fruchet 	/*
2244ce85705aSHugues Fruchet 	 * TIMING TC REG20:
2245ce85705aSHugues Fruchet 	 * - [2]:	ISP vflip
2246ce85705aSHugues Fruchet 	 * - [1]:	Sensor vflip
2247ce85705aSHugues Fruchet 	 */
2248ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2249ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2250c3f3ba3eSHugues Fruchet 			      (value ^ sensor->upside_down) ?
2251c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2252ce85705aSHugues Fruchet }
2253ce85705aSHugues Fruchet 
225419a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
225519a81c14SSteve Longerbeam {
225619a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
225719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
225819a81c14SSteve Longerbeam 	int val;
225919a81c14SSteve Longerbeam 
226019a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
226119a81c14SSteve Longerbeam 
226219a81c14SSteve Longerbeam 	switch (ctrl->id) {
226319a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
226419a81c14SSteve Longerbeam 		if (!ctrl->val)
226519a81c14SSteve Longerbeam 			return 0;
226619a81c14SSteve Longerbeam 		val = ov5640_get_gain(sensor);
226719a81c14SSteve Longerbeam 		if (val < 0)
226819a81c14SSteve Longerbeam 			return val;
226919a81c14SSteve Longerbeam 		sensor->ctrls.gain->val = val;
227019a81c14SSteve Longerbeam 		break;
227119a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
227219a81c14SSteve Longerbeam 		if (ctrl->val == V4L2_EXPOSURE_MANUAL)
227319a81c14SSteve Longerbeam 			return 0;
227419a81c14SSteve Longerbeam 		val = ov5640_get_exposure(sensor);
227519a81c14SSteve Longerbeam 		if (val < 0)
227619a81c14SSteve Longerbeam 			return val;
227719a81c14SSteve Longerbeam 		sensor->ctrls.exposure->val = val;
227819a81c14SSteve Longerbeam 		break;
227919a81c14SSteve Longerbeam 	}
228019a81c14SSteve Longerbeam 
228119a81c14SSteve Longerbeam 	return 0;
228219a81c14SSteve Longerbeam }
228319a81c14SSteve Longerbeam 
228419a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
228519a81c14SSteve Longerbeam {
228619a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
228719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
228819a81c14SSteve Longerbeam 	int ret;
228919a81c14SSteve Longerbeam 
229019a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
229119a81c14SSteve Longerbeam 
229219a81c14SSteve Longerbeam 	/*
229319a81c14SSteve Longerbeam 	 * If the device is not powered up by the host driver do
229419a81c14SSteve Longerbeam 	 * not apply any controls to H/W at this time. Instead
229519a81c14SSteve Longerbeam 	 * the controls will be restored right after power-up.
229619a81c14SSteve Longerbeam 	 */
229719a81c14SSteve Longerbeam 	if (sensor->power_count == 0)
229819a81c14SSteve Longerbeam 		return 0;
229919a81c14SSteve Longerbeam 
230019a81c14SSteve Longerbeam 	switch (ctrl->id) {
230119a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
230219a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
230319a81c14SSteve Longerbeam 		break;
230419a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
230519a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
230619a81c14SSteve Longerbeam 		break;
230719a81c14SSteve Longerbeam 	case V4L2_CID_AUTO_WHITE_BALANCE:
230819a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
230919a81c14SSteve Longerbeam 		break;
231019a81c14SSteve Longerbeam 	case V4L2_CID_HUE:
231119a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
231219a81c14SSteve Longerbeam 		break;
231319a81c14SSteve Longerbeam 	case V4L2_CID_CONTRAST:
231419a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
231519a81c14SSteve Longerbeam 		break;
231619a81c14SSteve Longerbeam 	case V4L2_CID_SATURATION:
231719a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
231819a81c14SSteve Longerbeam 		break;
231919a81c14SSteve Longerbeam 	case V4L2_CID_TEST_PATTERN:
232019a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
232119a81c14SSteve Longerbeam 		break;
23221068fecaSMylène Josserand 	case V4L2_CID_POWER_LINE_FREQUENCY:
23231068fecaSMylène Josserand 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
23241068fecaSMylène Josserand 		break;
2325ce85705aSHugues Fruchet 	case V4L2_CID_HFLIP:
2326ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2327ce85705aSHugues Fruchet 		break;
2328ce85705aSHugues Fruchet 	case V4L2_CID_VFLIP:
2329ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2330ce85705aSHugues Fruchet 		break;
233119a81c14SSteve Longerbeam 	default:
233219a81c14SSteve Longerbeam 		ret = -EINVAL;
233319a81c14SSteve Longerbeam 		break;
233419a81c14SSteve Longerbeam 	}
233519a81c14SSteve Longerbeam 
233619a81c14SSteve Longerbeam 	return ret;
233719a81c14SSteve Longerbeam }
233819a81c14SSteve Longerbeam 
233919a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
234019a81c14SSteve Longerbeam 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
234119a81c14SSteve Longerbeam 	.s_ctrl = ov5640_s_ctrl,
234219a81c14SSteve Longerbeam };
234319a81c14SSteve Longerbeam 
234419a81c14SSteve Longerbeam static const char * const test_pattern_menu[] = {
234519a81c14SSteve Longerbeam 	"Disabled",
234619a81c14SSteve Longerbeam 	"Color bars",
234719a81c14SSteve Longerbeam };
234819a81c14SSteve Longerbeam 
234919a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor)
235019a81c14SSteve Longerbeam {
235119a81c14SSteve Longerbeam 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
235219a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
235319a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
235419a81c14SSteve Longerbeam 	int ret;
235519a81c14SSteve Longerbeam 
235619a81c14SSteve Longerbeam 	v4l2_ctrl_handler_init(hdl, 32);
235719a81c14SSteve Longerbeam 
235819a81c14SSteve Longerbeam 	/* we can use our own mutex for the ctrl lock */
235919a81c14SSteve Longerbeam 	hdl->lock = &sensor->lock;
236019a81c14SSteve Longerbeam 
236119a81c14SSteve Longerbeam 	/* Auto/manual white balance */
236219a81c14SSteve Longerbeam 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
236319a81c14SSteve Longerbeam 					   V4L2_CID_AUTO_WHITE_BALANCE,
236419a81c14SSteve Longerbeam 					   0, 1, 1, 1);
236519a81c14SSteve Longerbeam 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
236619a81c14SSteve Longerbeam 						0, 4095, 1, 0);
236719a81c14SSteve Longerbeam 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
236819a81c14SSteve Longerbeam 					       0, 4095, 1, 0);
236919a81c14SSteve Longerbeam 	/* Auto/manual exposure */
237019a81c14SSteve Longerbeam 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
237119a81c14SSteve Longerbeam 						 V4L2_CID_EXPOSURE_AUTO,
237219a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_MANUAL, 0,
237319a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_AUTO);
237419a81c14SSteve Longerbeam 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
237519a81c14SSteve Longerbeam 					    0, 65535, 1, 0);
237619a81c14SSteve Longerbeam 	/* Auto/manual gain */
237719a81c14SSteve Longerbeam 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
237819a81c14SSteve Longerbeam 					     0, 1, 1, 1);
237919a81c14SSteve Longerbeam 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
238019a81c14SSteve Longerbeam 					0, 1023, 1, 0);
238119a81c14SSteve Longerbeam 
238219a81c14SSteve Longerbeam 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
238319a81c14SSteve Longerbeam 					      0, 255, 1, 64);
238419a81c14SSteve Longerbeam 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
238519a81c14SSteve Longerbeam 				       0, 359, 1, 0);
238619a81c14SSteve Longerbeam 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
238719a81c14SSteve Longerbeam 					    0, 255, 1, 0);
238819a81c14SSteve Longerbeam 	ctrls->test_pattern =
238919a81c14SSteve Longerbeam 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
239019a81c14SSteve Longerbeam 					     ARRAY_SIZE(test_pattern_menu) - 1,
239119a81c14SSteve Longerbeam 					     0, 0, test_pattern_menu);
2392ce85705aSHugues Fruchet 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2393ce85705aSHugues Fruchet 					 0, 1, 1, 0);
2394ce85705aSHugues Fruchet 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2395ce85705aSHugues Fruchet 					 0, 1, 1, 0);
239619a81c14SSteve Longerbeam 
23971068fecaSMylène Josserand 	ctrls->light_freq =
23981068fecaSMylène Josserand 		v4l2_ctrl_new_std_menu(hdl, ops,
23991068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY,
24001068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
24011068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
24021068fecaSMylène Josserand 
240319a81c14SSteve Longerbeam 	if (hdl->error) {
240419a81c14SSteve Longerbeam 		ret = hdl->error;
240519a81c14SSteve Longerbeam 		goto free_ctrls;
240619a81c14SSteve Longerbeam 	}
240719a81c14SSteve Longerbeam 
240819a81c14SSteve Longerbeam 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
240919a81c14SSteve Longerbeam 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
241019a81c14SSteve Longerbeam 
241119a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
241219a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
241319a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
241419a81c14SSteve Longerbeam 
241519a81c14SSteve Longerbeam 	sensor->sd.ctrl_handler = hdl;
241619a81c14SSteve Longerbeam 	return 0;
241719a81c14SSteve Longerbeam 
241819a81c14SSteve Longerbeam free_ctrls:
241919a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(hdl);
242019a81c14SSteve Longerbeam 	return ret;
242119a81c14SSteve Longerbeam }
242219a81c14SSteve Longerbeam 
242319a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
242419a81c14SSteve Longerbeam 				  struct v4l2_subdev_pad_config *cfg,
242519a81c14SSteve Longerbeam 				  struct v4l2_subdev_frame_size_enum *fse)
242619a81c14SSteve Longerbeam {
242719a81c14SSteve Longerbeam 	if (fse->pad != 0)
242819a81c14SSteve Longerbeam 		return -EINVAL;
242919a81c14SSteve Longerbeam 	if (fse->index >= OV5640_NUM_MODES)
243019a81c14SSteve Longerbeam 		return -EINVAL;
243119a81c14SSteve Longerbeam 
243241d8d7f5SHugues Fruchet 	fse->min_width =
2433dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].hact;
243441d8d7f5SHugues Fruchet 	fse->max_width = fse->min_width;
243541d8d7f5SHugues Fruchet 	fse->min_height =
2436dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].vact;
243741d8d7f5SHugues Fruchet 	fse->max_height = fse->min_height;
243819a81c14SSteve Longerbeam 
243919a81c14SSteve Longerbeam 	return 0;
244019a81c14SSteve Longerbeam }
244119a81c14SSteve Longerbeam 
244219a81c14SSteve Longerbeam static int ov5640_enum_frame_interval(
244319a81c14SSteve Longerbeam 	struct v4l2_subdev *sd,
244419a81c14SSteve Longerbeam 	struct v4l2_subdev_pad_config *cfg,
244519a81c14SSteve Longerbeam 	struct v4l2_subdev_frame_interval_enum *fie)
244619a81c14SSteve Longerbeam {
244719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
244819a81c14SSteve Longerbeam 	struct v4l2_fract tpf;
244919a81c14SSteve Longerbeam 	int ret;
245019a81c14SSteve Longerbeam 
245119a81c14SSteve Longerbeam 	if (fie->pad != 0)
245219a81c14SSteve Longerbeam 		return -EINVAL;
245319a81c14SSteve Longerbeam 	if (fie->index >= OV5640_NUM_FRAMERATES)
245419a81c14SSteve Longerbeam 		return -EINVAL;
245519a81c14SSteve Longerbeam 
245619a81c14SSteve Longerbeam 	tpf.numerator = 1;
245719a81c14SSteve Longerbeam 	tpf.denominator = ov5640_framerates[fie->index];
245819a81c14SSteve Longerbeam 
245919a81c14SSteve Longerbeam 	ret = ov5640_try_frame_interval(sensor, &tpf,
246019a81c14SSteve Longerbeam 					fie->width, fie->height);
246119a81c14SSteve Longerbeam 	if (ret < 0)
246219a81c14SSteve Longerbeam 		return -EINVAL;
246319a81c14SSteve Longerbeam 
246419a81c14SSteve Longerbeam 	fie->interval = tpf;
246519a81c14SSteve Longerbeam 	return 0;
246619a81c14SSteve Longerbeam }
246719a81c14SSteve Longerbeam 
246819a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
246919a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
247019a81c14SSteve Longerbeam {
247119a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
247219a81c14SSteve Longerbeam 
247319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
247419a81c14SSteve Longerbeam 	fi->interval = sensor->frame_interval;
247519a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
247619a81c14SSteve Longerbeam 
247719a81c14SSteve Longerbeam 	return 0;
247819a81c14SSteve Longerbeam }
247919a81c14SSteve Longerbeam 
248019a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
248119a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
248219a81c14SSteve Longerbeam {
248319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
248419a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
248519a81c14SSteve Longerbeam 	int frame_rate, ret = 0;
248619a81c14SSteve Longerbeam 
248719a81c14SSteve Longerbeam 	if (fi->pad != 0)
248819a81c14SSteve Longerbeam 		return -EINVAL;
248919a81c14SSteve Longerbeam 
249019a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
249119a81c14SSteve Longerbeam 
249219a81c14SSteve Longerbeam 	if (sensor->streaming) {
249319a81c14SSteve Longerbeam 		ret = -EBUSY;
249419a81c14SSteve Longerbeam 		goto out;
249519a81c14SSteve Longerbeam 	}
249619a81c14SSteve Longerbeam 
249719a81c14SSteve Longerbeam 	mode = sensor->current_mode;
249819a81c14SSteve Longerbeam 
249919a81c14SSteve Longerbeam 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2500dba13a0bSMaxime Ripard 					       mode->hact, mode->vact);
250119a81c14SSteve Longerbeam 	if (frame_rate < 0)
250219a81c14SSteve Longerbeam 		frame_rate = OV5640_15_FPS;
250319a81c14SSteve Longerbeam 
250419a81c14SSteve Longerbeam 	sensor->current_fr = frame_rate;
250519a81c14SSteve Longerbeam 	sensor->frame_interval = fi->interval;
25063c4a7372SHugues Fruchet 	mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2507dba13a0bSMaxime Ripard 				mode->vact, true);
25083c4a7372SHugues Fruchet 	if (!mode) {
25093c4a7372SHugues Fruchet 		ret = -EINVAL;
25103c4a7372SHugues Fruchet 		goto out;
25113c4a7372SHugues Fruchet 	}
25123c4a7372SHugues Fruchet 
25136949d864SHugues Fruchet 	if (mode != sensor->current_mode) {
25143c4a7372SHugues Fruchet 		sensor->current_mode = mode;
251519a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
25166949d864SHugues Fruchet 	}
251719a81c14SSteve Longerbeam out:
251819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
251919a81c14SSteve Longerbeam 	return ret;
252019a81c14SSteve Longerbeam }
252119a81c14SSteve Longerbeam 
252219a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
252319a81c14SSteve Longerbeam 				 struct v4l2_subdev_pad_config *cfg,
252419a81c14SSteve Longerbeam 				 struct v4l2_subdev_mbus_code_enum *code)
252519a81c14SSteve Longerbeam {
252619a81c14SSteve Longerbeam 	if (code->pad != 0)
252719a81c14SSteve Longerbeam 		return -EINVAL;
2528e3ee691dSHugues Fruchet 	if (code->index >= ARRAY_SIZE(ov5640_formats))
252919a81c14SSteve Longerbeam 		return -EINVAL;
253019a81c14SSteve Longerbeam 
2531e3ee691dSHugues Fruchet 	code->code = ov5640_formats[code->index].code;
253219a81c14SSteve Longerbeam 	return 0;
253319a81c14SSteve Longerbeam }
253419a81c14SSteve Longerbeam 
253519a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
253619a81c14SSteve Longerbeam {
253719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
253819a81c14SSteve Longerbeam 	int ret = 0;
253919a81c14SSteve Longerbeam 
254019a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
254119a81c14SSteve Longerbeam 
254219a81c14SSteve Longerbeam 	if (sensor->streaming == !enable) {
254319a81c14SSteve Longerbeam 		if (enable && sensor->pending_mode_change) {
254419a81c14SSteve Longerbeam 			ret = ov5640_set_mode(sensor, sensor->current_mode);
254519a81c14SSteve Longerbeam 			if (ret)
254619a81c14SSteve Longerbeam 				goto out;
2547e3ee691dSHugues Fruchet 
2548e3ee691dSHugues Fruchet 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2549e3ee691dSHugues Fruchet 			if (ret)
2550e3ee691dSHugues Fruchet 				goto out;
255119a81c14SSteve Longerbeam 		}
255219a81c14SSteve Longerbeam 
2553f22996dbSHugues Fruchet 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2)
2554f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, enable);
2555f22996dbSHugues Fruchet 		else
2556f22996dbSHugues Fruchet 			ret = ov5640_set_stream_dvp(sensor, enable);
2557f22996dbSHugues Fruchet 
255819a81c14SSteve Longerbeam 		if (!ret)
255919a81c14SSteve Longerbeam 			sensor->streaming = enable;
256019a81c14SSteve Longerbeam 	}
256119a81c14SSteve Longerbeam out:
256219a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
256319a81c14SSteve Longerbeam 	return ret;
256419a81c14SSteve Longerbeam }
256519a81c14SSteve Longerbeam 
256619a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = {
256719a81c14SSteve Longerbeam 	.s_power = ov5640_s_power,
256819a81c14SSteve Longerbeam };
256919a81c14SSteve Longerbeam 
257019a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = {
257119a81c14SSteve Longerbeam 	.g_frame_interval = ov5640_g_frame_interval,
257219a81c14SSteve Longerbeam 	.s_frame_interval = ov5640_s_frame_interval,
257319a81c14SSteve Longerbeam 	.s_stream = ov5640_s_stream,
257419a81c14SSteve Longerbeam };
257519a81c14SSteve Longerbeam 
257619a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
257719a81c14SSteve Longerbeam 	.enum_mbus_code = ov5640_enum_mbus_code,
257819a81c14SSteve Longerbeam 	.get_fmt = ov5640_get_fmt,
257919a81c14SSteve Longerbeam 	.set_fmt = ov5640_set_fmt,
258019a81c14SSteve Longerbeam 	.enum_frame_size = ov5640_enum_frame_size,
258119a81c14SSteve Longerbeam 	.enum_frame_interval = ov5640_enum_frame_interval,
258219a81c14SSteve Longerbeam };
258319a81c14SSteve Longerbeam 
258419a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = {
258519a81c14SSteve Longerbeam 	.core = &ov5640_core_ops,
258619a81c14SSteve Longerbeam 	.video = &ov5640_video_ops,
258719a81c14SSteve Longerbeam 	.pad = &ov5640_pad_ops,
258819a81c14SSteve Longerbeam };
258919a81c14SSteve Longerbeam 
259019a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor)
259119a81c14SSteve Longerbeam {
259219a81c14SSteve Longerbeam 	int i;
259319a81c14SSteve Longerbeam 
259419a81c14SSteve Longerbeam 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
259519a81c14SSteve Longerbeam 		sensor->supplies[i].supply = ov5640_supply_name[i];
259619a81c14SSteve Longerbeam 
259719a81c14SSteve Longerbeam 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
259819a81c14SSteve Longerbeam 				       OV5640_NUM_SUPPLIES,
259919a81c14SSteve Longerbeam 				       sensor->supplies);
260019a81c14SSteve Longerbeam }
260119a81c14SSteve Longerbeam 
26020f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor)
26030f7acb52SHugues Fruchet {
26040f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
26050f7acb52SHugues Fruchet 	int ret = 0;
26060f7acb52SHugues Fruchet 	u16 chip_id;
26070f7acb52SHugues Fruchet 
26080f7acb52SHugues Fruchet 	ret = ov5640_set_power_on(sensor);
26090f7acb52SHugues Fruchet 	if (ret)
26100f7acb52SHugues Fruchet 		return ret;
26110f7acb52SHugues Fruchet 
26120f7acb52SHugues Fruchet 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
26130f7acb52SHugues Fruchet 	if (ret) {
26140f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
26150f7acb52SHugues Fruchet 			__func__);
26160f7acb52SHugues Fruchet 		goto power_off;
26170f7acb52SHugues Fruchet 	}
26180f7acb52SHugues Fruchet 
26190f7acb52SHugues Fruchet 	if (chip_id != 0x5640) {
26200f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
26210f7acb52SHugues Fruchet 			__func__, chip_id);
26220f7acb52SHugues Fruchet 		ret = -ENXIO;
26230f7acb52SHugues Fruchet 	}
26240f7acb52SHugues Fruchet 
26250f7acb52SHugues Fruchet power_off:
26260f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
26270f7acb52SHugues Fruchet 	return ret;
26280f7acb52SHugues Fruchet }
26290f7acb52SHugues Fruchet 
263019a81c14SSteve Longerbeam static int ov5640_probe(struct i2c_client *client,
263119a81c14SSteve Longerbeam 			const struct i2c_device_id *id)
263219a81c14SSteve Longerbeam {
263319a81c14SSteve Longerbeam 	struct device *dev = &client->dev;
263419a81c14SSteve Longerbeam 	struct fwnode_handle *endpoint;
263519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor;
2636e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *fmt;
2637c3f3ba3eSHugues Fruchet 	u32 rotation;
263819a81c14SSteve Longerbeam 	int ret;
263919a81c14SSteve Longerbeam 
264019a81c14SSteve Longerbeam 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
264119a81c14SSteve Longerbeam 	if (!sensor)
264219a81c14SSteve Longerbeam 		return -ENOMEM;
264319a81c14SSteve Longerbeam 
264419a81c14SSteve Longerbeam 	sensor->i2c_client = client;
2645e6441fdeSHugues Fruchet 	fmt = &sensor->fmt;
2646e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[0].code;
2647e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[0].colorspace;
2648e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2649e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2650e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2651e6441fdeSHugues Fruchet 	fmt->width = 640;
2652e6441fdeSHugues Fruchet 	fmt->height = 480;
2653e6441fdeSHugues Fruchet 	fmt->field = V4L2_FIELD_NONE;
265419a81c14SSteve Longerbeam 	sensor->frame_interval.numerator = 1;
265519a81c14SSteve Longerbeam 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
265619a81c14SSteve Longerbeam 	sensor->current_fr = OV5640_30_FPS;
265719a81c14SSteve Longerbeam 	sensor->current_mode =
265819a81c14SSteve Longerbeam 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
265919a81c14SSteve Longerbeam 	sensor->pending_mode_change = true;
266019a81c14SSteve Longerbeam 
266119a81c14SSteve Longerbeam 	sensor->ae_target = 52;
266219a81c14SSteve Longerbeam 
2663c3f3ba3eSHugues Fruchet 	/* optional indication of physical rotation of sensor */
2664c3f3ba3eSHugues Fruchet 	ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
2665c3f3ba3eSHugues Fruchet 				       &rotation);
2666c3f3ba3eSHugues Fruchet 	if (!ret) {
2667c3f3ba3eSHugues Fruchet 		switch (rotation) {
2668c3f3ba3eSHugues Fruchet 		case 180:
2669c3f3ba3eSHugues Fruchet 			sensor->upside_down = true;
2670c3f3ba3eSHugues Fruchet 			/* fall through */
2671c3f3ba3eSHugues Fruchet 		case 0:
2672c3f3ba3eSHugues Fruchet 			break;
2673c3f3ba3eSHugues Fruchet 		default:
2674c3f3ba3eSHugues Fruchet 			dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
2675c3f3ba3eSHugues Fruchet 				 rotation);
2676c3f3ba3eSHugues Fruchet 		}
2677c3f3ba3eSHugues Fruchet 	}
2678c3f3ba3eSHugues Fruchet 
2679ce96bcf5SSakari Ailus 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2680ce96bcf5SSakari Ailus 						  NULL);
268119a81c14SSteve Longerbeam 	if (!endpoint) {
268219a81c14SSteve Longerbeam 		dev_err(dev, "endpoint node not found\n");
268319a81c14SSteve Longerbeam 		return -EINVAL;
268419a81c14SSteve Longerbeam 	}
268519a81c14SSteve Longerbeam 
268619a81c14SSteve Longerbeam 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
268719a81c14SSteve Longerbeam 	fwnode_handle_put(endpoint);
268819a81c14SSteve Longerbeam 	if (ret) {
268919a81c14SSteve Longerbeam 		dev_err(dev, "Could not parse endpoint\n");
269019a81c14SSteve Longerbeam 		return ret;
269119a81c14SSteve Longerbeam 	}
269219a81c14SSteve Longerbeam 
269319a81c14SSteve Longerbeam 	/* get system clock (xclk) */
269419a81c14SSteve Longerbeam 	sensor->xclk = devm_clk_get(dev, "xclk");
269519a81c14SSteve Longerbeam 	if (IS_ERR(sensor->xclk)) {
269619a81c14SSteve Longerbeam 		dev_err(dev, "failed to get xclk\n");
269719a81c14SSteve Longerbeam 		return PTR_ERR(sensor->xclk);
269819a81c14SSteve Longerbeam 	}
269919a81c14SSteve Longerbeam 
270019a81c14SSteve Longerbeam 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
270119a81c14SSteve Longerbeam 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
270219a81c14SSteve Longerbeam 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
270319a81c14SSteve Longerbeam 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
270419a81c14SSteve Longerbeam 			sensor->xclk_freq);
270519a81c14SSteve Longerbeam 		return -EINVAL;
270619a81c14SSteve Longerbeam 	}
270719a81c14SSteve Longerbeam 
270819a81c14SSteve Longerbeam 	/* request optional power down pin */
270919a81c14SSteve Longerbeam 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
271019a81c14SSteve Longerbeam 						    GPIOD_OUT_HIGH);
271119a81c14SSteve Longerbeam 	/* request optional reset pin */
271219a81c14SSteve Longerbeam 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
271319a81c14SSteve Longerbeam 						     GPIOD_OUT_HIGH);
271419a81c14SSteve Longerbeam 
271519a81c14SSteve Longerbeam 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
271619a81c14SSteve Longerbeam 
2717dae82d9dSAkinobu Mita 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
271819a81c14SSteve Longerbeam 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
271919a81c14SSteve Longerbeam 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
272019a81c14SSteve Longerbeam 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
272119a81c14SSteve Longerbeam 	if (ret)
272219a81c14SSteve Longerbeam 		return ret;
272319a81c14SSteve Longerbeam 
272419a81c14SSteve Longerbeam 	ret = ov5640_get_regulators(sensor);
272519a81c14SSteve Longerbeam 	if (ret)
272619a81c14SSteve Longerbeam 		return ret;
272719a81c14SSteve Longerbeam 
272819a81c14SSteve Longerbeam 	mutex_init(&sensor->lock);
272919a81c14SSteve Longerbeam 
27300f7acb52SHugues Fruchet 	ret = ov5640_check_chip_id(sensor);
27310f7acb52SHugues Fruchet 	if (ret)
27320f7acb52SHugues Fruchet 		goto entity_cleanup;
27330f7acb52SHugues Fruchet 
273419a81c14SSteve Longerbeam 	ret = ov5640_init_controls(sensor);
273519a81c14SSteve Longerbeam 	if (ret)
273619a81c14SSteve Longerbeam 		goto entity_cleanup;
273719a81c14SSteve Longerbeam 
273819a81c14SSteve Longerbeam 	ret = v4l2_async_register_subdev(&sensor->sd);
273919a81c14SSteve Longerbeam 	if (ret)
274019a81c14SSteve Longerbeam 		goto free_ctrls;
274119a81c14SSteve Longerbeam 
274219a81c14SSteve Longerbeam 	return 0;
274319a81c14SSteve Longerbeam 
274419a81c14SSteve Longerbeam free_ctrls:
274519a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
274619a81c14SSteve Longerbeam entity_cleanup:
274719a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
274819a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
274919a81c14SSteve Longerbeam 	return ret;
275019a81c14SSteve Longerbeam }
275119a81c14SSteve Longerbeam 
275219a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client)
275319a81c14SSteve Longerbeam {
275419a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
275519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
275619a81c14SSteve Longerbeam 
275719a81c14SSteve Longerbeam 	v4l2_async_unregister_subdev(&sensor->sd);
275819a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
275919a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
276019a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
276119a81c14SSteve Longerbeam 
276219a81c14SSteve Longerbeam 	return 0;
276319a81c14SSteve Longerbeam }
276419a81c14SSteve Longerbeam 
276519a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = {
276619a81c14SSteve Longerbeam 	{"ov5640", 0},
276719a81c14SSteve Longerbeam 	{},
276819a81c14SSteve Longerbeam };
276919a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id);
277019a81c14SSteve Longerbeam 
277119a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = {
277219a81c14SSteve Longerbeam 	{ .compatible = "ovti,ov5640" },
277319a81c14SSteve Longerbeam 	{ /* sentinel */ }
277419a81c14SSteve Longerbeam };
277519a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
277619a81c14SSteve Longerbeam 
277719a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = {
277819a81c14SSteve Longerbeam 	.driver = {
277919a81c14SSteve Longerbeam 		.name  = "ov5640",
278019a81c14SSteve Longerbeam 		.of_match_table	= ov5640_dt_ids,
278119a81c14SSteve Longerbeam 	},
278219a81c14SSteve Longerbeam 	.id_table = ov5640_id,
278319a81c14SSteve Longerbeam 	.probe    = ov5640_probe,
278419a81c14SSteve Longerbeam 	.remove   = ov5640_remove,
278519a81c14SSteve Longerbeam };
278619a81c14SSteve Longerbeam 
278719a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver);
278819a81c14SSteve Longerbeam 
278919a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
279019a81c14SSteve Longerbeam MODULE_LICENSE("GPL");
2791