xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision aa288248)
119a81c14SSteve Longerbeam /*
219a81c14SSteve Longerbeam  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
319a81c14SSteve Longerbeam  * Copyright (C) 2014-2017 Mentor Graphics Inc.
419a81c14SSteve Longerbeam  *
519a81c14SSteve Longerbeam  * This program is free software; you can redistribute it and/or modify
619a81c14SSteve Longerbeam  * it under the terms of the GNU General Public License as published by
719a81c14SSteve Longerbeam  * the Free Software Foundation; either version 2 of the License, or
819a81c14SSteve Longerbeam  * (at your option) any later version.
919a81c14SSteve Longerbeam  */
1019a81c14SSteve Longerbeam 
1119a81c14SSteve Longerbeam #include <linux/clk.h>
1219a81c14SSteve Longerbeam #include <linux/clk-provider.h>
1319a81c14SSteve Longerbeam #include <linux/clkdev.h>
1419a81c14SSteve Longerbeam #include <linux/ctype.h>
1519a81c14SSteve Longerbeam #include <linux/delay.h>
1619a81c14SSteve Longerbeam #include <linux/device.h>
1741d8d7f5SHugues Fruchet #include <linux/gpio/consumer.h>
1819a81c14SSteve Longerbeam #include <linux/i2c.h>
1919a81c14SSteve Longerbeam #include <linux/init.h>
2019a81c14SSteve Longerbeam #include <linux/module.h>
2119a81c14SSteve Longerbeam #include <linux/of_device.h>
2241d8d7f5SHugues Fruchet #include <linux/regulator/consumer.h>
2319a81c14SSteve Longerbeam #include <linux/slab.h>
2419a81c14SSteve Longerbeam #include <linux/types.h>
2519a81c14SSteve Longerbeam #include <media/v4l2-async.h>
2619a81c14SSteve Longerbeam #include <media/v4l2-ctrls.h>
2719a81c14SSteve Longerbeam #include <media/v4l2-device.h>
282d18fbc5SAkinobu Mita #include <media/v4l2-event.h>
2919a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h>
3019a81c14SSteve Longerbeam #include <media/v4l2-subdev.h>
3119a81c14SSteve Longerbeam 
3219a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */
3319a81c14SSteve Longerbeam #define OV5640_XCLK_MIN  6000000
3441cb1c73SPhilipp Puschmann #define OV5640_XCLK_MAX 54000000
3519a81c14SSteve Longerbeam 
3619a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c
3719a81c14SSteve Longerbeam 
38d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02		0x3002
39d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
40f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0		0x3008
4119a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID		0x300a
42f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00	0x300e
43f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
44f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
4519a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00		0x3019
46f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1	0x302e
4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0		0x3034
4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1		0x3035
4919a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2		0x3036
5019a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3		0x3037
5119a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID		0x3100
52f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
5319a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN		0x3400
5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN		0x3402
5619a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN		0x3404
5719a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL	0x3503
6219a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
6319a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS		0x350c
6486633417SMaxime Ripard #define OV5640_REG_TIMING_DVPHO		0x3808
6586633417SMaxime Ripard #define OV5640_REG_TIMING_DVPVO		0x380a
6619a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS		0x380c
6719a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS		0x380e
68ce85705aSHugues Fruchet #define OV5640_REG_TIMING_TC_REG20	0x3820
6919a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21	0x3821
7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00		0x3a00
7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP		0x3a08
7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP		0x3a0a
7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D		0x3a0d
7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E		0x3a0e
7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F		0x3a0f
7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10		0x3a10
7719a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11		0x3a11
7819a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B		0x3a1b
7919a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E		0x3a1e
8019a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F		0x3a1f
8119a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00	0x3c00
8219a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01	0x3c01
8319a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
8419a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01		0x4202
85e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00	0x4300
86f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00	0x4740
8719a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00		0x4800
8819a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE		0x4814
89e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
9019a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
9119a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0		0x5580
9219a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1		0x5581
9319a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3		0x5583
9419a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4		0x5584
9519a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5		0x5585
9619a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT		0x56a1
9719a81c14SSteve Longerbeam 
988f57c2f8SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT	1
998f57c2f8SMaxime Ripard #define OV5640_SCLK_ROOT_DIVIDER_DEFAULT	2
1008f57c2f8SMaxime Ripard 
10119a81c14SSteve Longerbeam enum ov5640_mode_id {
10219a81c14SSteve Longerbeam 	OV5640_MODE_QCIF_176_144 = 0,
10319a81c14SSteve Longerbeam 	OV5640_MODE_QVGA_320_240,
10419a81c14SSteve Longerbeam 	OV5640_MODE_VGA_640_480,
10519a81c14SSteve Longerbeam 	OV5640_MODE_NTSC_720_480,
10619a81c14SSteve Longerbeam 	OV5640_MODE_PAL_720_576,
10719a81c14SSteve Longerbeam 	OV5640_MODE_XGA_1024_768,
10819a81c14SSteve Longerbeam 	OV5640_MODE_720P_1280_720,
10919a81c14SSteve Longerbeam 	OV5640_MODE_1080P_1920_1080,
11019a81c14SSteve Longerbeam 	OV5640_MODE_QSXGA_2592_1944,
11119a81c14SSteve Longerbeam 	OV5640_NUM_MODES,
11219a81c14SSteve Longerbeam };
11319a81c14SSteve Longerbeam 
11419a81c14SSteve Longerbeam enum ov5640_frame_rate {
11519a81c14SSteve Longerbeam 	OV5640_15_FPS = 0,
11619a81c14SSteve Longerbeam 	OV5640_30_FPS,
11719a81c14SSteve Longerbeam 	OV5640_NUM_FRAMERATES,
11819a81c14SSteve Longerbeam };
11919a81c14SSteve Longerbeam 
120e3ee691dSHugues Fruchet struct ov5640_pixfmt {
121e3ee691dSHugues Fruchet 	u32 code;
122e3ee691dSHugues Fruchet 	u32 colorspace;
123e3ee691dSHugues Fruchet };
124e3ee691dSHugues Fruchet 
125e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = {
126d47c4126SHugues Fruchet 	{ MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
127e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
128e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
129e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
130e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
131e3ee691dSHugues Fruchet };
132e3ee691dSHugues Fruchet 
13319a81c14SSteve Longerbeam /*
13419a81c14SSteve Longerbeam  * FIXME: remove this when a subdev API becomes available
13519a81c14SSteve Longerbeam  * to set the MIPI CSI-2 virtual channel.
13619a81c14SSteve Longerbeam  */
13719a81c14SSteve Longerbeam static unsigned int virtual_channel;
1388670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444);
13919a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel,
14019a81c14SSteve Longerbeam 		 "MIPI CSI-2 virtual channel (0..3), default 0");
14119a81c14SSteve Longerbeam 
14219a81c14SSteve Longerbeam static const int ov5640_framerates[] = {
14319a81c14SSteve Longerbeam 	[OV5640_15_FPS] = 15,
14419a81c14SSteve Longerbeam 	[OV5640_30_FPS] = 30,
14519a81c14SSteve Longerbeam };
14619a81c14SSteve Longerbeam 
14719a81c14SSteve Longerbeam /* regulator supplies */
14819a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = {
14941d8d7f5SHugues Fruchet 	"DOVDD", /* Digital I/O (1.8V) supply */
15019a81c14SSteve Longerbeam 	"DVDD",  /* Digital Core (1.5V) supply */
15119a81c14SSteve Longerbeam 	"AVDD",  /* Analog (2.8V) supply */
15219a81c14SSteve Longerbeam };
15319a81c14SSteve Longerbeam 
15419a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
15519a81c14SSteve Longerbeam 
15619a81c14SSteve Longerbeam /*
15719a81c14SSteve Longerbeam  * Image size under 1280 * 960 are SUBSAMPLING
15819a81c14SSteve Longerbeam  * Image size upper 1280 * 960 are SCALING
15919a81c14SSteve Longerbeam  */
16019a81c14SSteve Longerbeam enum ov5640_downsize_mode {
16119a81c14SSteve Longerbeam 	SUBSAMPLING,
16219a81c14SSteve Longerbeam 	SCALING,
16319a81c14SSteve Longerbeam };
16419a81c14SSteve Longerbeam 
16519a81c14SSteve Longerbeam struct reg_value {
16619a81c14SSteve Longerbeam 	u16 reg_addr;
16719a81c14SSteve Longerbeam 	u8 val;
16819a81c14SSteve Longerbeam 	u8 mask;
16919a81c14SSteve Longerbeam 	u32 delay_ms;
17019a81c14SSteve Longerbeam };
17119a81c14SSteve Longerbeam 
17219a81c14SSteve Longerbeam struct ov5640_mode_info {
17319a81c14SSteve Longerbeam 	enum ov5640_mode_id id;
17419a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode;
175dba13a0bSMaxime Ripard 	u32 hact;
176476dec01SMaxime Ripard 	u32 htot;
177dba13a0bSMaxime Ripard 	u32 vact;
178476dec01SMaxime Ripard 	u32 vtot;
179aa288248SMaxime Ripard 	u32 pixel_clock;
18019a81c14SSteve Longerbeam 	const struct reg_value *reg_data;
18119a81c14SSteve Longerbeam 	u32 reg_data_size;
18219a81c14SSteve Longerbeam };
18319a81c14SSteve Longerbeam 
18419a81c14SSteve Longerbeam struct ov5640_ctrls {
18519a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler handler;
18619a81c14SSteve Longerbeam 	struct {
18719a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_exp;
18819a81c14SSteve Longerbeam 		struct v4l2_ctrl *exposure;
18919a81c14SSteve Longerbeam 	};
19019a81c14SSteve Longerbeam 	struct {
19119a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_wb;
19219a81c14SSteve Longerbeam 		struct v4l2_ctrl *blue_balance;
19319a81c14SSteve Longerbeam 		struct v4l2_ctrl *red_balance;
19419a81c14SSteve Longerbeam 	};
19519a81c14SSteve Longerbeam 	struct {
19619a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_gain;
19719a81c14SSteve Longerbeam 		struct v4l2_ctrl *gain;
19819a81c14SSteve Longerbeam 	};
19919a81c14SSteve Longerbeam 	struct v4l2_ctrl *brightness;
2001068fecaSMylène Josserand 	struct v4l2_ctrl *light_freq;
20119a81c14SSteve Longerbeam 	struct v4l2_ctrl *saturation;
20219a81c14SSteve Longerbeam 	struct v4l2_ctrl *contrast;
20319a81c14SSteve Longerbeam 	struct v4l2_ctrl *hue;
20419a81c14SSteve Longerbeam 	struct v4l2_ctrl *test_pattern;
205ce85705aSHugues Fruchet 	struct v4l2_ctrl *hflip;
206ce85705aSHugues Fruchet 	struct v4l2_ctrl *vflip;
20719a81c14SSteve Longerbeam };
20819a81c14SSteve Longerbeam 
20919a81c14SSteve Longerbeam struct ov5640_dev {
21019a81c14SSteve Longerbeam 	struct i2c_client *i2c_client;
21119a81c14SSteve Longerbeam 	struct v4l2_subdev sd;
21219a81c14SSteve Longerbeam 	struct media_pad pad;
21319a81c14SSteve Longerbeam 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
21419a81c14SSteve Longerbeam 	struct clk *xclk; /* system clock to OV5640 */
21519a81c14SSteve Longerbeam 	u32 xclk_freq;
21619a81c14SSteve Longerbeam 
21719a81c14SSteve Longerbeam 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
21819a81c14SSteve Longerbeam 	struct gpio_desc *reset_gpio;
21919a81c14SSteve Longerbeam 	struct gpio_desc *pwdn_gpio;
220c3f3ba3eSHugues Fruchet 	bool   upside_down;
22119a81c14SSteve Longerbeam 
22219a81c14SSteve Longerbeam 	/* lock to protect all members below */
22319a81c14SSteve Longerbeam 	struct mutex lock;
22419a81c14SSteve Longerbeam 
22519a81c14SSteve Longerbeam 	int power_count;
22619a81c14SSteve Longerbeam 
22719a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt fmt;
228fb98e29fSHugues Fruchet 	bool pending_fmt_change;
22919a81c14SSteve Longerbeam 
23019a81c14SSteve Longerbeam 	const struct ov5640_mode_info *current_mode;
231985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *last_mode;
23219a81c14SSteve Longerbeam 	enum ov5640_frame_rate current_fr;
23319a81c14SSteve Longerbeam 	struct v4l2_fract frame_interval;
23419a81c14SSteve Longerbeam 
23519a81c14SSteve Longerbeam 	struct ov5640_ctrls ctrls;
23619a81c14SSteve Longerbeam 
23719a81c14SSteve Longerbeam 	u32 prev_sysclk, prev_hts;
23819a81c14SSteve Longerbeam 	u32 ae_low, ae_high, ae_target;
23919a81c14SSteve Longerbeam 
24019a81c14SSteve Longerbeam 	bool pending_mode_change;
24119a81c14SSteve Longerbeam 	bool streaming;
24219a81c14SSteve Longerbeam };
24319a81c14SSteve Longerbeam 
24419a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
24519a81c14SSteve Longerbeam {
24619a81c14SSteve Longerbeam 	return container_of(sd, struct ov5640_dev, sd);
24719a81c14SSteve Longerbeam }
24819a81c14SSteve Longerbeam 
24919a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
25019a81c14SSteve Longerbeam {
25119a81c14SSteve Longerbeam 	return &container_of(ctrl->handler, struct ov5640_dev,
25219a81c14SSteve Longerbeam 			     ctrls.handler)->sd;
25319a81c14SSteve Longerbeam }
25419a81c14SSteve Longerbeam 
25519a81c14SSteve Longerbeam /*
25619a81c14SSteve Longerbeam  * FIXME: all of these register tables are likely filled with
25719a81c14SSteve Longerbeam  * entries that set the register to their power-on default values,
25819a81c14SSteve Longerbeam  * and which are otherwise not touched by this driver. Those entries
25919a81c14SSteve Longerbeam  * should be identified and removed to speed register load time
26019a81c14SSteve Longerbeam  * over i2c.
26119a81c14SSteve Longerbeam  */
262fb98e29fSHugues Fruchet /* YUV422 UYVY VGA@30fps */
26319a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
26419a81c14SSteve Longerbeam 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
26519a81c14SSteve Longerbeam 	{0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
26619a81c14SSteve Longerbeam 	{0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
2678f57c2f8SMaxime Ripard 	{0x3037, 0x13, 0, 0}, {0x3630, 0x36, 0, 0},
26819a81c14SSteve Longerbeam 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
26919a81c14SSteve Longerbeam 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
27019a81c14SSteve Longerbeam 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
27119a81c14SSteve Longerbeam 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
27219a81c14SSteve Longerbeam 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
27319a81c14SSteve Longerbeam 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
27419a81c14SSteve Longerbeam 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
27519a81c14SSteve Longerbeam 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
27619a81c14SSteve Longerbeam 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
27719a81c14SSteve Longerbeam 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
27819a81c14SSteve Longerbeam 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
27919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
28019a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
28119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
28219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
28319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
284476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
28519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
28619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
28719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
28819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
28919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
29019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
29119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
29219a81c14SSteve Longerbeam 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
293aa4bb8b8SJacopo Mondi 	{0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
29419a81c14SSteve Longerbeam 	{0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
29519a81c14SSteve Longerbeam 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
296aa4bb8b8SJacopo Mondi 	{0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
29719a81c14SSteve Longerbeam 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
29819a81c14SSteve Longerbeam 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
29919a81c14SSteve Longerbeam 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
30019a81c14SSteve Longerbeam 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
30119a81c14SSteve Longerbeam 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
30219a81c14SSteve Longerbeam 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
30319a81c14SSteve Longerbeam 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
30419a81c14SSteve Longerbeam 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
30519a81c14SSteve Longerbeam 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
30619a81c14SSteve Longerbeam 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
30719a81c14SSteve Longerbeam 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
30819a81c14SSteve Longerbeam 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
30919a81c14SSteve Longerbeam 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
31019a81c14SSteve Longerbeam 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
31119a81c14SSteve Longerbeam 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
31219a81c14SSteve Longerbeam 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
31319a81c14SSteve Longerbeam 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
31419a81c14SSteve Longerbeam 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
31519a81c14SSteve Longerbeam 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
31619a81c14SSteve Longerbeam 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
31719a81c14SSteve Longerbeam 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
31819a81c14SSteve Longerbeam 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
31919a81c14SSteve Longerbeam 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
32019a81c14SSteve Longerbeam 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
32119a81c14SSteve Longerbeam 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
32219a81c14SSteve Longerbeam 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
32319a81c14SSteve Longerbeam 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
32419a81c14SSteve Longerbeam 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
32519a81c14SSteve Longerbeam 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
32619a81c14SSteve Longerbeam 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
32719a81c14SSteve Longerbeam 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
32819a81c14SSteve Longerbeam 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
32919a81c14SSteve Longerbeam 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
33019a81c14SSteve Longerbeam 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
33119a81c14SSteve Longerbeam 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
33219a81c14SSteve Longerbeam 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
33319a81c14SSteve Longerbeam 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
33419a81c14SSteve Longerbeam 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
33519a81c14SSteve Longerbeam 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
33619a81c14SSteve Longerbeam 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
33719a81c14SSteve Longerbeam 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
33819a81c14SSteve Longerbeam 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
33919a81c14SSteve Longerbeam 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
34019a81c14SSteve Longerbeam 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
34119a81c14SSteve Longerbeam 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
34219a81c14SSteve Longerbeam 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
34319a81c14SSteve Longerbeam 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
34419a81c14SSteve Longerbeam 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
34519a81c14SSteve Longerbeam 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
34619a81c14SSteve Longerbeam 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
34719a81c14SSteve Longerbeam };
34819a81c14SSteve Longerbeam 
34919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
35019a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
35119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
352ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
35319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
35419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
35519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
356476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
35719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
35819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
35919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
36019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
36119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
36219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
36319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
36419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
36519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
36619a81c14SSteve Longerbeam };
36719a81c14SSteve Longerbeam 
36819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
36919a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
37019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
371ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
37219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
37319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
37419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
375476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
37619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
37719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
37819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
37919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
38019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
38119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
38219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
38319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
38419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
38519a81c14SSteve Longerbeam };
38619a81c14SSteve Longerbeam 
38719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
38819a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
38919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
390ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
39119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
39219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
39319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
394476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
39519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
39619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
39719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
39819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
39919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
40019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
40119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
40219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
40319a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
40486633417SMaxime Ripard 	{0x3035, 0x12, 0, 0},
40519a81c14SSteve Longerbeam };
40619a81c14SSteve Longerbeam 
40719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
40819a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
40919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
410ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
41119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
41219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
41319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
414476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
41519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
41619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
41719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
41819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
41919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
42019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
42119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
42219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
42386633417SMaxime Ripard 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
42419a81c14SSteve Longerbeam };
42519a81c14SSteve Longerbeam 
42619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
42719a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
42819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
429ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
43019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
43119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
43219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
433476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
43419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
43519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
43619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
43719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
43819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
43919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
44019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
44119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
44219a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
44319a81c14SSteve Longerbeam };
44419a81c14SSteve Longerbeam 
44519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
44619a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
44719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
448ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
44919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
45019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
45119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
452476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
45319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
45419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
45519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
45619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
45719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
45819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
45919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
46019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
46119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
46219a81c14SSteve Longerbeam };
46319a81c14SSteve Longerbeam 
46419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
46519a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
46619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
467ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
46819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
46919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
47019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
471476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
47219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
47319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
47419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
47519a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
47619a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
47719a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
47819a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
47919a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
48019a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
48119a81c14SSteve Longerbeam };
48241d8d7f5SHugues Fruchet 
48319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
48419a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
48519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
486ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
48719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
48819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
48919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
490476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
49119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
49219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
49319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
49419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
49519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
49619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
49719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
49819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
49919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
50019a81c14SSteve Longerbeam };
50119a81c14SSteve Longerbeam 
50219a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
50319a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
50419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
505ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
50619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
50719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
50819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
509476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
51019a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
51119a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
51219a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
51319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
51419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
51519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
51619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
51719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
51819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
51919a81c14SSteve Longerbeam };
52019a81c14SSteve Longerbeam 
52119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
52219a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
52319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
524ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
52519a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
52619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
52719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
528476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
52919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
53019a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
53119a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
53219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
53319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
53419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
53519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
53619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
53719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
53819a81c14SSteve Longerbeam };
53919a81c14SSteve Longerbeam 
54019a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
54119a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
54219a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
543ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
54419a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
54519a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
54619a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
547476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
54819a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
54919a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
55019a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
55119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
55219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
55319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
55419a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
55519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
55619a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
55719a81c14SSteve Longerbeam };
55819a81c14SSteve Longerbeam 
55919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
56019a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
56119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
562ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
56319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
56419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
56519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
566476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
56719a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
56819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
56919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
57019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
57119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
57219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
57319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
57419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
57519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
57619a81c14SSteve Longerbeam };
57719a81c14SSteve Longerbeam 
57819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
57919a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
58019a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
58119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
582ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
58319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
58419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
58519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
586476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
58719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
58819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
58919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
59019a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
59119a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
59219a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
59319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
59419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
59519a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
59619a81c14SSteve Longerbeam 	{0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
59719a81c14SSteve Longerbeam };
59819a81c14SSteve Longerbeam 
59919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
60019a81c14SSteve Longerbeam 	{0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
60119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
602ce85705aSHugues Fruchet 	{0x3814, 0x31, 0, 0},
60319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
60419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
60519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
606476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
60719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
60819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
60919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
61019a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
61119a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
61219a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
613b791187bSHugues Fruchet 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
61419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
61519a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
61619a81c14SSteve Longerbeam };
61719a81c14SSteve Longerbeam 
61819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
61919a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
62019a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
62119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
622ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
62319a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
62419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
62519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
626476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
62719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
62819a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
62919a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
63019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
63119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
63219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
63319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
63419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
63519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
63619a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
63719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
63819a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
63919a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
64086633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
641476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
64219a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
64319a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
64419a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
64519a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
64619a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
64719a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
64819a81c14SSteve Longerbeam 	{0x3503, 0, 0, 0},
64919a81c14SSteve Longerbeam };
65019a81c14SSteve Longerbeam 
65119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
65219a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
65319a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
65419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
655ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
65619a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
65719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
65819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
659476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
66019a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
66119a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
66219a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
66319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
66419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
66519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
66619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
66719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
66819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
66919a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
67019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
67119a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
67219a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
67386633417SMaxime Ripard 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
674476dec01SMaxime Ripard 	{0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
67519a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
67619a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
67719a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
67819a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
67919a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
68019a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
68119a81c14SSteve Longerbeam };
68219a81c14SSteve Longerbeam 
68319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
68419a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
68519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
686ce85705aSHugues Fruchet 	{0x3814, 0x11, 0, 0},
68719a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
68819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
68919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
690476dec01SMaxime Ripard 	{0x3810, 0x00, 0, 0},
69119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
69219a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
69319a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
69419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
69519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
69619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
69719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
69819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
69919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
70019a81c14SSteve Longerbeam };
70119a81c14SSteve Longerbeam 
70219a81c14SSteve Longerbeam /* power-on sensor init reg table */
70319a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = {
704476dec01SMaxime Ripard 	0, SUBSAMPLING, 640, 1896, 480, 984,
705aa288248SMaxime Ripard 	56000000,
706476dec01SMaxime Ripard 	ov5640_init_setting_30fps_VGA,
70719a81c14SSteve Longerbeam 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
70819a81c14SSteve Longerbeam };
70919a81c14SSteve Longerbeam 
71019a81c14SSteve Longerbeam static const struct ov5640_mode_info
71119a81c14SSteve Longerbeam ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
71219a81c14SSteve Longerbeam 	{
713476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
714476dec01SMaxime Ripard 		 176, 1896, 144, 984,
715aa288248SMaxime Ripard 		 28000000,
71619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QCIF_176_144,
71719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
718476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
719476dec01SMaxime Ripard 		 320, 1896, 240, 984,
720aa288248SMaxime Ripard 		 28000000,
72119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QVGA_320_240,
72219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
723476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
724476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
725aa288248SMaxime Ripard 		 28000000,
72619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_VGA_640_480,
72719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
728476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
729476dec01SMaxime Ripard 		 720, 1896, 480, 984,
730aa288248SMaxime Ripard 		 28000000,
73119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_NTSC_720_480,
73219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
733476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
734476dec01SMaxime Ripard 		 720, 1896, 576, 984,
735aa288248SMaxime Ripard 		 28000000,
73619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_PAL_720_576,
73719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
738476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
739476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
740aa288248SMaxime Ripard 		 28000000,
74119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_XGA_1024_768,
74219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
743476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
744476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
745aa288248SMaxime Ripard 		 21000000,
74619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_720P_1280_720,
74719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
748476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
749476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
750aa288248SMaxime Ripard 		 42000000,
75119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_1080P_1920_1080,
75219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
753476dec01SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, SCALING,
754476dec01SMaxime Ripard 		 2592, 2844, 1944, 1968,
755aa288248SMaxime Ripard 		 84000000,
75619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QSXGA_2592_1944,
75719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
75819a81c14SSteve Longerbeam 	}, {
759476dec01SMaxime Ripard 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING,
760476dec01SMaxime Ripard 		 176, 1896, 144, 984,
761aa288248SMaxime Ripard 		 56000000,
76219a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QCIF_176_144,
76319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
764476dec01SMaxime Ripard 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING,
765476dec01SMaxime Ripard 		 320, 1896, 240, 984,
766aa288248SMaxime Ripard 		 56000000,
76719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QVGA_320_240,
76819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
769476dec01SMaxime Ripard 		{OV5640_MODE_VGA_640_480, SUBSAMPLING,
770476dec01SMaxime Ripard 		 640, 1896, 480, 1080,
771aa288248SMaxime Ripard 		 56000000,
77219a81c14SSteve Longerbeam 		 ov5640_setting_30fps_VGA_640_480,
77319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
774476dec01SMaxime Ripard 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING,
775476dec01SMaxime Ripard 		 720, 1896, 480, 984,
776aa288248SMaxime Ripard 		 56000000,
77719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_NTSC_720_480,
77819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
779476dec01SMaxime Ripard 		{OV5640_MODE_PAL_720_576, SUBSAMPLING,
780476dec01SMaxime Ripard 		 720, 1896, 576, 984,
781aa288248SMaxime Ripard 		 56000000,
78219a81c14SSteve Longerbeam 		 ov5640_setting_30fps_PAL_720_576,
78319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
784476dec01SMaxime Ripard 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING,
785476dec01SMaxime Ripard 		 1024, 1896, 768, 1080,
786aa288248SMaxime Ripard 		 56000000,
78719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_XGA_1024_768,
78819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
789476dec01SMaxime Ripard 		{OV5640_MODE_720P_1280_720, SUBSAMPLING,
790476dec01SMaxime Ripard 		 1280, 1892, 720, 740,
791aa288248SMaxime Ripard 		 42000000,
79219a81c14SSteve Longerbeam 		 ov5640_setting_30fps_720P_1280_720,
79319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
794476dec01SMaxime Ripard 		{OV5640_MODE_1080P_1920_1080, SCALING,
795476dec01SMaxime Ripard 		 1920, 2500, 1080, 1120,
796aa288248SMaxime Ripard 		 84000000,
79719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_1080P_1920_1080,
79819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
799aa288248SMaxime Ripard 		{OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, 0, NULL, 0},
80019a81c14SSteve Longerbeam 	},
80119a81c14SSteve Longerbeam };
80219a81c14SSteve Longerbeam 
80319a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor)
80419a81c14SSteve Longerbeam {
80519a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
80619a81c14SSteve Longerbeam 	struct i2c_msg msg;
80719a81c14SSteve Longerbeam 	u8 buf[3];
80819a81c14SSteve Longerbeam 	int ret;
80919a81c14SSteve Longerbeam 
81019a81c14SSteve Longerbeam 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
81119a81c14SSteve Longerbeam 		return 0;
81219a81c14SSteve Longerbeam 
81319a81c14SSteve Longerbeam 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
81419a81c14SSteve Longerbeam 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
81519a81c14SSteve Longerbeam 	buf[2] = client->addr << 1;
81619a81c14SSteve Longerbeam 
81719a81c14SSteve Longerbeam 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
81819a81c14SSteve Longerbeam 	msg.flags = 0;
81919a81c14SSteve Longerbeam 	msg.buf = buf;
82019a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
82119a81c14SSteve Longerbeam 
82219a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
82319a81c14SSteve Longerbeam 	if (ret < 0) {
82419a81c14SSteve Longerbeam 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
82519a81c14SSteve Longerbeam 		return ret;
82619a81c14SSteve Longerbeam 	}
82719a81c14SSteve Longerbeam 
82819a81c14SSteve Longerbeam 	return 0;
82919a81c14SSteve Longerbeam }
83019a81c14SSteve Longerbeam 
83119a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
83219a81c14SSteve Longerbeam {
83319a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
83419a81c14SSteve Longerbeam 	struct i2c_msg msg;
83519a81c14SSteve Longerbeam 	u8 buf[3];
83619a81c14SSteve Longerbeam 	int ret;
83719a81c14SSteve Longerbeam 
83819a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
83919a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
84019a81c14SSteve Longerbeam 	buf[2] = val;
84119a81c14SSteve Longerbeam 
84219a81c14SSteve Longerbeam 	msg.addr = client->addr;
84319a81c14SSteve Longerbeam 	msg.flags = client->flags;
84419a81c14SSteve Longerbeam 	msg.buf = buf;
84519a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
84619a81c14SSteve Longerbeam 
84719a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
84819a81c14SSteve Longerbeam 	if (ret < 0) {
8493924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
85019a81c14SSteve Longerbeam 			__func__, reg, val);
85119a81c14SSteve Longerbeam 		return ret;
85219a81c14SSteve Longerbeam 	}
85319a81c14SSteve Longerbeam 
85419a81c14SSteve Longerbeam 	return 0;
85519a81c14SSteve Longerbeam }
85619a81c14SSteve Longerbeam 
85719a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
85819a81c14SSteve Longerbeam {
85919a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
86019a81c14SSteve Longerbeam 	struct i2c_msg msg[2];
86119a81c14SSteve Longerbeam 	u8 buf[2];
86219a81c14SSteve Longerbeam 	int ret;
86319a81c14SSteve Longerbeam 
86419a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
86519a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
86619a81c14SSteve Longerbeam 
86719a81c14SSteve Longerbeam 	msg[0].addr = client->addr;
86819a81c14SSteve Longerbeam 	msg[0].flags = client->flags;
86919a81c14SSteve Longerbeam 	msg[0].buf = buf;
87019a81c14SSteve Longerbeam 	msg[0].len = sizeof(buf);
87119a81c14SSteve Longerbeam 
87219a81c14SSteve Longerbeam 	msg[1].addr = client->addr;
87319a81c14SSteve Longerbeam 	msg[1].flags = client->flags | I2C_M_RD;
87419a81c14SSteve Longerbeam 	msg[1].buf = buf;
87519a81c14SSteve Longerbeam 	msg[1].len = 1;
87619a81c14SSteve Longerbeam 
87719a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, msg, 2);
8783924c623SHugues Fruchet 	if (ret < 0) {
8793924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x\n",
8803924c623SHugues Fruchet 			__func__, reg);
88119a81c14SSteve Longerbeam 		return ret;
8823924c623SHugues Fruchet 	}
88319a81c14SSteve Longerbeam 
88419a81c14SSteve Longerbeam 	*val = buf[0];
88519a81c14SSteve Longerbeam 	return 0;
88619a81c14SSteve Longerbeam }
88719a81c14SSteve Longerbeam 
88819a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
88919a81c14SSteve Longerbeam {
89019a81c14SSteve Longerbeam 	u8 hi, lo;
89119a81c14SSteve Longerbeam 	int ret;
89219a81c14SSteve Longerbeam 
89319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &hi);
89419a81c14SSteve Longerbeam 	if (ret)
89519a81c14SSteve Longerbeam 		return ret;
89619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
89719a81c14SSteve Longerbeam 	if (ret)
89819a81c14SSteve Longerbeam 		return ret;
89919a81c14SSteve Longerbeam 
90019a81c14SSteve Longerbeam 	*val = ((u16)hi << 8) | (u16)lo;
90119a81c14SSteve Longerbeam 	return 0;
90219a81c14SSteve Longerbeam }
90319a81c14SSteve Longerbeam 
90419a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
90519a81c14SSteve Longerbeam {
90619a81c14SSteve Longerbeam 	int ret;
90719a81c14SSteve Longerbeam 
90819a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, reg, val >> 8);
90919a81c14SSteve Longerbeam 	if (ret)
91019a81c14SSteve Longerbeam 		return ret;
91119a81c14SSteve Longerbeam 
91219a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
91319a81c14SSteve Longerbeam }
91419a81c14SSteve Longerbeam 
91519a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
91619a81c14SSteve Longerbeam 			  u8 mask, u8 val)
91719a81c14SSteve Longerbeam {
91819a81c14SSteve Longerbeam 	u8 readval;
91919a81c14SSteve Longerbeam 	int ret;
92019a81c14SSteve Longerbeam 
92119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &readval);
92219a81c14SSteve Longerbeam 	if (ret)
92319a81c14SSteve Longerbeam 		return ret;
92419a81c14SSteve Longerbeam 
92519a81c14SSteve Longerbeam 	readval &= ~mask;
92619a81c14SSteve Longerbeam 	val &= mask;
92719a81c14SSteve Longerbeam 	val |= readval;
92819a81c14SSteve Longerbeam 
92919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg, val);
93019a81c14SSteve Longerbeam }
93119a81c14SSteve Longerbeam 
932aa288248SMaxime Ripard /*
933aa288248SMaxime Ripard  * After trying the various combinations, reading various
934aa288248SMaxime Ripard  * documentations spreaded around the net, and from the various
935aa288248SMaxime Ripard  * feedback, the clock tree is probably as follows:
936aa288248SMaxime Ripard  *
937aa288248SMaxime Ripard  *   +--------------+
938aa288248SMaxime Ripard  *   |  Ext. Clock  |
939aa288248SMaxime Ripard  *   +-+------------+
940aa288248SMaxime Ripard  *     |  +----------+
941aa288248SMaxime Ripard  *     +->|   PLL1   | - reg 0x3036, for the multiplier
942aa288248SMaxime Ripard  *        +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
943aa288248SMaxime Ripard  *          |  +--------------+
944aa288248SMaxime Ripard  *          +->| System Clock |  - reg 0x3035, bits 4-7
945aa288248SMaxime Ripard  *             +-+------------+
946aa288248SMaxime Ripard  *               |  +--------------+
947aa288248SMaxime Ripard  *               +->| MIPI Divider | - reg 0x3035, bits 0-3
948aa288248SMaxime Ripard  *               |  +-+------------+
949aa288248SMaxime Ripard  *               |    +----------------> MIPI SCLK
950aa288248SMaxime Ripard  *               |    +  +-----+
951aa288248SMaxime Ripard  *               |    +->| / 2 |-------> MIPI BIT CLK
952aa288248SMaxime Ripard  *               |       +-----+
953aa288248SMaxime Ripard  *               |  +--------------+
954aa288248SMaxime Ripard  *               +->| PLL Root Div | - reg 0x3037, bit 4
955aa288248SMaxime Ripard  *                  +-+------------+
956aa288248SMaxime Ripard  *                    |  +---------+
957aa288248SMaxime Ripard  *                    +->| Bit Div | - reg 0x3035, bits 0-3
958aa288248SMaxime Ripard  *                       +-+-------+
959aa288248SMaxime Ripard  *                         |  +-------------+
960aa288248SMaxime Ripard  *                         +->| SCLK Div    | - reg 0x3108, bits 0-1
961aa288248SMaxime Ripard  *                         |  +-+-----------+
962aa288248SMaxime Ripard  *                         |    +---------------> SCLK
963aa288248SMaxime Ripard  *                         |  +-------------+
964aa288248SMaxime Ripard  *                         +->| SCLK 2X Div | - reg 0x3108, bits 2-3
965aa288248SMaxime Ripard  *                         |  +-+-----------+
966aa288248SMaxime Ripard  *                         |    +---------------> SCLK 2X
967aa288248SMaxime Ripard  *                         |  +-------------+
968aa288248SMaxime Ripard  *                         +->| PCLK Div    | - reg 0x3108, bits 4-5
969aa288248SMaxime Ripard  *                            ++------------+
970aa288248SMaxime Ripard  *                             +  +-----------+
971aa288248SMaxime Ripard  *                             +->|   P_DIV   | - reg 0x3035, bits 0-3
972aa288248SMaxime Ripard  *                                +-----+-----+
973aa288248SMaxime Ripard  *                                       +------------> PCLK
974aa288248SMaxime Ripard  *
975aa288248SMaxime Ripard  * This is deviating from the datasheet at least for the register
976aa288248SMaxime Ripard  * 0x3108, since it's said here that the PCLK would be clocked from
977aa288248SMaxime Ripard  * the PLL.
978aa288248SMaxime Ripard  *
979aa288248SMaxime Ripard  * There seems to be also (unverified) constraints:
980aa288248SMaxime Ripard  *  - the PLL pre-divider output rate should be in the 4-27MHz range
981aa288248SMaxime Ripard  *  - the PLL multiplier output rate should be in the 500-1000MHz range
982aa288248SMaxime Ripard  *  - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
983aa288248SMaxime Ripard  *
984aa288248SMaxime Ripard  * In the two latter cases, these constraints are met since our
985aa288248SMaxime Ripard  * factors are hardcoded. If we were to change that, we would need to
986aa288248SMaxime Ripard  * take this into account. The only varying parts are the PLL
987aa288248SMaxime Ripard  * multiplier and the system clock divider, which are shared between
988aa288248SMaxime Ripard  * all these clocks so won't cause any issue.
989aa288248SMaxime Ripard  */
990aa288248SMaxime Ripard 
991aa288248SMaxime Ripard /*
992aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
993aa288248SMaxime Ripard  * set to 3 in the vendor kernels.
994aa288248SMaxime Ripard  */
995aa288248SMaxime Ripard #define OV5640_PLL_PREDIV	3
996aa288248SMaxime Ripard 
997aa288248SMaxime Ripard #define OV5640_PLL_MULT_MIN	4
998aa288248SMaxime Ripard #define OV5640_PLL_MULT_MAX	252
999aa288248SMaxime Ripard 
1000aa288248SMaxime Ripard /*
1001aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 16, but the value is
1002aa288248SMaxime Ripard  * always set to either 1 or 2 in the vendor kernels.
1003aa288248SMaxime Ripard  */
1004aa288248SMaxime Ripard #define OV5640_SYSDIV_MIN	1
1005aa288248SMaxime Ripard #define OV5640_SYSDIV_MAX	16
1006aa288248SMaxime Ripard 
1007aa288248SMaxime Ripard /*
1008aa288248SMaxime Ripard  * Hardcode these values for scaler and non-scaler modes.
1009aa288248SMaxime Ripard  * FIXME: to be re-calcualted for 1 data lanes setups
1010aa288248SMaxime Ripard  */
1011aa288248SMaxime Ripard #define OV5640_MIPI_DIV_PCLK	2
1012aa288248SMaxime Ripard #define OV5640_MIPI_DIV_SCLK	1
1013aa288248SMaxime Ripard 
1014aa288248SMaxime Ripard /*
1015aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 2, but the value is always
1016aa288248SMaxime Ripard  * set to 2 in the vendor kernels.
1017aa288248SMaxime Ripard  */
1018aa288248SMaxime Ripard #define OV5640_PLL_ROOT_DIV			2
1019aa288248SMaxime Ripard #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2		BIT(4)
1020aa288248SMaxime Ripard 
1021aa288248SMaxime Ripard /*
1022aa288248SMaxime Ripard  * We only supports 8-bit formats at the moment
1023aa288248SMaxime Ripard  */
1024aa288248SMaxime Ripard #define OV5640_BIT_DIV				2
1025aa288248SMaxime Ripard #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT		0x08
1026aa288248SMaxime Ripard 
1027aa288248SMaxime Ripard /*
1028aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
1029aa288248SMaxime Ripard  * set to 2 in the vendor kernels.
1030aa288248SMaxime Ripard  */
1031aa288248SMaxime Ripard #define OV5640_SCLK_ROOT_DIV	2
1032aa288248SMaxime Ripard 
1033aa288248SMaxime Ripard /*
1034aa288248SMaxime Ripard  * This is hardcoded so that the consistency is maintained between SCLK and
1035aa288248SMaxime Ripard  * SCLK 2x.
1036aa288248SMaxime Ripard  */
1037aa288248SMaxime Ripard #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1038aa288248SMaxime Ripard 
1039aa288248SMaxime Ripard /*
1040aa288248SMaxime Ripard  * This is supposed to be ranging from 1 to 8, but the value is always
1041aa288248SMaxime Ripard  * set to 1 in the vendor kernels.
1042aa288248SMaxime Ripard  */
1043aa288248SMaxime Ripard #define OV5640_PCLK_ROOT_DIV			1
1044aa288248SMaxime Ripard #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS	0x00
1045aa288248SMaxime Ripard 
1046aa288248SMaxime Ripard static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1047aa288248SMaxime Ripard 					    u8 pll_prediv, u8 pll_mult,
1048aa288248SMaxime Ripard 					    u8 sysdiv)
1049aa288248SMaxime Ripard {
1050aa288248SMaxime Ripard 	unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1051aa288248SMaxime Ripard 
1052aa288248SMaxime Ripard 	/* PLL1 output cannot exceed 1GHz. */
1053aa288248SMaxime Ripard 	if (sysclk / 1000000 > 1000)
1054aa288248SMaxime Ripard 		return 0;
1055aa288248SMaxime Ripard 
1056aa288248SMaxime Ripard 	return sysclk / sysdiv;
1057aa288248SMaxime Ripard }
1058aa288248SMaxime Ripard 
1059aa288248SMaxime Ripard static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1060aa288248SMaxime Ripard 					 unsigned long rate,
1061aa288248SMaxime Ripard 					 u8 *pll_prediv, u8 *pll_mult,
1062aa288248SMaxime Ripard 					 u8 *sysdiv)
1063aa288248SMaxime Ripard {
1064aa288248SMaxime Ripard 	unsigned long best = ~0;
1065aa288248SMaxime Ripard 	u8 best_sysdiv = 1, best_mult = 1;
1066aa288248SMaxime Ripard 	u8 _sysdiv, _pll_mult;
1067aa288248SMaxime Ripard 
1068aa288248SMaxime Ripard 	for (_sysdiv = OV5640_SYSDIV_MIN;
1069aa288248SMaxime Ripard 	     _sysdiv <= OV5640_SYSDIV_MAX;
1070aa288248SMaxime Ripard 	     _sysdiv++) {
1071aa288248SMaxime Ripard 		for (_pll_mult = OV5640_PLL_MULT_MIN;
1072aa288248SMaxime Ripard 		     _pll_mult <= OV5640_PLL_MULT_MAX;
1073aa288248SMaxime Ripard 		     _pll_mult++) {
1074aa288248SMaxime Ripard 			unsigned long _rate;
1075aa288248SMaxime Ripard 
1076aa288248SMaxime Ripard 			/*
1077aa288248SMaxime Ripard 			 * The PLL multiplier cannot be odd if above
1078aa288248SMaxime Ripard 			 * 127.
1079aa288248SMaxime Ripard 			 */
1080aa288248SMaxime Ripard 			if (_pll_mult > 127 && (_pll_mult % 2))
1081aa288248SMaxime Ripard 				continue;
1082aa288248SMaxime Ripard 
1083aa288248SMaxime Ripard 			_rate = ov5640_compute_sys_clk(sensor,
1084aa288248SMaxime Ripard 						       OV5640_PLL_PREDIV,
1085aa288248SMaxime Ripard 						       _pll_mult, _sysdiv);
1086aa288248SMaxime Ripard 
1087aa288248SMaxime Ripard 			/*
1088aa288248SMaxime Ripard 			 * We have reached the maximum allowed PLL1 output,
1089aa288248SMaxime Ripard 			 * increase sysdiv.
1090aa288248SMaxime Ripard 			 */
1091aa288248SMaxime Ripard 			if (!rate)
1092aa288248SMaxime Ripard 				break;
1093aa288248SMaxime Ripard 
1094aa288248SMaxime Ripard 			/*
1095aa288248SMaxime Ripard 			 * Prefer rates above the expected clock rate than
1096aa288248SMaxime Ripard 			 * below, even if that means being less precise.
1097aa288248SMaxime Ripard 			 */
1098aa288248SMaxime Ripard 			if (_rate < rate)
1099aa288248SMaxime Ripard 				continue;
1100aa288248SMaxime Ripard 
1101aa288248SMaxime Ripard 			if (abs(rate - _rate) < abs(rate - best)) {
1102aa288248SMaxime Ripard 				best = _rate;
1103aa288248SMaxime Ripard 				best_sysdiv = _sysdiv;
1104aa288248SMaxime Ripard 				best_mult = _pll_mult;
1105aa288248SMaxime Ripard 			}
1106aa288248SMaxime Ripard 
1107aa288248SMaxime Ripard 			if (_rate == rate)
1108aa288248SMaxime Ripard 				goto out;
1109aa288248SMaxime Ripard 		}
1110aa288248SMaxime Ripard 	}
1111aa288248SMaxime Ripard 
1112aa288248SMaxime Ripard out:
1113aa288248SMaxime Ripard 	*sysdiv = best_sysdiv;
1114aa288248SMaxime Ripard 	*pll_prediv = OV5640_PLL_PREDIV;
1115aa288248SMaxime Ripard 	*pll_mult = best_mult;
1116aa288248SMaxime Ripard 
1117aa288248SMaxime Ripard 	return best;
1118aa288248SMaxime Ripard }
1119aa288248SMaxime Ripard 
1120aa288248SMaxime Ripard /*
1121aa288248SMaxime Ripard  * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1122aa288248SMaxime Ripard  *			    for the MIPI CSI-2 output.
1123aa288248SMaxime Ripard  *
1124aa288248SMaxime Ripard  * @rate: The requested bandwidth per lane in bytes per second.
1125aa288248SMaxime Ripard  *	  'Bandwidth Per Lane' is calculated as:
1126aa288248SMaxime Ripard  *	  bpl = HTOT * VTOT * FPS * bpp / num_lanes;
1127aa288248SMaxime Ripard  *
1128aa288248SMaxime Ripard  * This function use the requested bandwidth to calculate:
1129aa288248SMaxime Ripard  * - sample_rate = bpl / (bpp / num_lanes);
1130aa288248SMaxime Ripard  *	         = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes);
1131aa288248SMaxime Ripard  *
1132aa288248SMaxime Ripard  * - mipi_sclk   = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
1133aa288248SMaxime Ripard  *
1134aa288248SMaxime Ripard  * with these fixed parameters:
1135aa288248SMaxime Ripard  *	PLL_RDIV	= 2;
1136aa288248SMaxime Ripard  *	BIT_DIVIDER	= 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5);
1137aa288248SMaxime Ripard  *	PCLK_DIV	= 1;
1138aa288248SMaxime Ripard  *
1139aa288248SMaxime Ripard  * The MIPI clock generation differs for modes that use the scaler and modes
1140aa288248SMaxime Ripard  * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI
1141aa288248SMaxime Ripard  * BIT CLk, and thus:
1142aa288248SMaxime Ripard  *
1143aa288248SMaxime Ripard  * - mipi_sclk = bpl / MIPI_DIV / 2;
1144aa288248SMaxime Ripard  *   MIPI_DIV = 1;
1145aa288248SMaxime Ripard  *
1146aa288248SMaxime Ripard  * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated
1147aa288248SMaxime Ripard  * from the pixel clock, and thus:
1148aa288248SMaxime Ripard  *
1149aa288248SMaxime Ripard  * - sample_rate = bpl / (bpp / num_lanes);
1150aa288248SMaxime Ripard  *	         = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes);
1151aa288248SMaxime Ripard  *		 = bpl / (4 * MIPI_DIV / num_lanes);
1152aa288248SMaxime Ripard  * - MIPI_DIV	 = bpp / (4 * num_lanes);
1153aa288248SMaxime Ripard  *
1154aa288248SMaxime Ripard  * FIXME: this have been tested with 16bpp and 2 lanes setup only.
1155aa288248SMaxime Ripard  * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
1156aa288248SMaxime Ripard  * above formula for setups with 1 lane or image formats with different bpp.
1157aa288248SMaxime Ripard  *
1158aa288248SMaxime Ripard  * FIXME: this deviates from the sensor manual documentation which is quite
1159aa288248SMaxime Ripard  * thin on the MIPI clock tree generation part.
1160aa288248SMaxime Ripard  */
1161aa288248SMaxime Ripard static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
1162aa288248SMaxime Ripard 				unsigned long rate)
1163aa288248SMaxime Ripard {
1164aa288248SMaxime Ripard 	const struct ov5640_mode_info *mode = sensor->current_mode;
1165aa288248SMaxime Ripard 	u8 prediv, mult, sysdiv;
1166aa288248SMaxime Ripard 	u8 mipi_div;
1167aa288248SMaxime Ripard 	int ret;
1168aa288248SMaxime Ripard 
1169aa288248SMaxime Ripard 	/*
1170aa288248SMaxime Ripard 	 * 1280x720 is reported to use 'SUBSAMPLING' only,
1171aa288248SMaxime Ripard 	 * but according to the sensor manual it goes through the
1172aa288248SMaxime Ripard 	 * scaler before subsampling.
1173aa288248SMaxime Ripard 	 */
1174aa288248SMaxime Ripard 	if (mode->dn_mode == SCALING ||
1175aa288248SMaxime Ripard 	   (mode->id == OV5640_MODE_720P_1280_720))
1176aa288248SMaxime Ripard 		mipi_div = OV5640_MIPI_DIV_SCLK;
1177aa288248SMaxime Ripard 	else
1178aa288248SMaxime Ripard 		mipi_div = OV5640_MIPI_DIV_PCLK;
1179aa288248SMaxime Ripard 
1180aa288248SMaxime Ripard 	ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
1181aa288248SMaxime Ripard 
1182aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1183aa288248SMaxime Ripard 			     0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
1184aa288248SMaxime Ripard 
1185aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1186aa288248SMaxime Ripard 			     0xff, sysdiv << 4 | mipi_div);
1187aa288248SMaxime Ripard 	if (ret)
1188aa288248SMaxime Ripard 		return ret;
1189aa288248SMaxime Ripard 
1190aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1191aa288248SMaxime Ripard 	if (ret)
1192aa288248SMaxime Ripard 		return ret;
1193aa288248SMaxime Ripard 
1194aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1195aa288248SMaxime Ripard 			     0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
1196aa288248SMaxime Ripard 	if (ret)
1197aa288248SMaxime Ripard 		return ret;
1198aa288248SMaxime Ripard 
1199aa288248SMaxime Ripard 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
1200aa288248SMaxime Ripard 			      0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
1201aa288248SMaxime Ripard }
1202aa288248SMaxime Ripard 
1203aa288248SMaxime Ripard static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1204aa288248SMaxime Ripard 				      unsigned long rate,
1205aa288248SMaxime Ripard 				      u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1206aa288248SMaxime Ripard 				      u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1207aa288248SMaxime Ripard {
1208aa288248SMaxime Ripard 	unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1209aa288248SMaxime Ripard 				OV5640_PCLK_ROOT_DIV;
1210aa288248SMaxime Ripard 
1211aa288248SMaxime Ripard 	_rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1212aa288248SMaxime Ripard 				    sysdiv);
1213aa288248SMaxime Ripard 	*pll_rdiv = OV5640_PLL_ROOT_DIV;
1214aa288248SMaxime Ripard 	*bit_div = OV5640_BIT_DIV;
1215aa288248SMaxime Ripard 	*pclk_div = OV5640_PCLK_ROOT_DIV;
1216aa288248SMaxime Ripard 
1217aa288248SMaxime Ripard 	return _rate / *pll_rdiv / *bit_div / *pclk_div;
1218aa288248SMaxime Ripard }
1219aa288248SMaxime Ripard 
1220aa288248SMaxime Ripard static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1221aa288248SMaxime Ripard {
1222aa288248SMaxime Ripard 	u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1223aa288248SMaxime Ripard 	int ret;
1224aa288248SMaxime Ripard 
1225aa288248SMaxime Ripard 	ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1226aa288248SMaxime Ripard 			 &bit_div, &pclk_div);
1227aa288248SMaxime Ripard 
1228aa288248SMaxime Ripard 	if (bit_div == 2)
1229aa288248SMaxime Ripard 		bit_div = 8;
1230aa288248SMaxime Ripard 
1231aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1232aa288248SMaxime Ripard 			     0x0f, bit_div);
1233aa288248SMaxime Ripard 	if (ret)
1234aa288248SMaxime Ripard 		return ret;
1235aa288248SMaxime Ripard 
1236aa288248SMaxime Ripard 	/*
1237aa288248SMaxime Ripard 	 * We need to set sysdiv according to the clock, and to clear
1238aa288248SMaxime Ripard 	 * the MIPI divider.
1239aa288248SMaxime Ripard 	 */
1240aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1241aa288248SMaxime Ripard 			     0xff, sysdiv << 4);
1242aa288248SMaxime Ripard 	if (ret)
1243aa288248SMaxime Ripard 		return ret;
1244aa288248SMaxime Ripard 
1245aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1246aa288248SMaxime Ripard 			     0xff, mult);
1247aa288248SMaxime Ripard 	if (ret)
1248aa288248SMaxime Ripard 		return ret;
1249aa288248SMaxime Ripard 
1250aa288248SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1251aa288248SMaxime Ripard 			     0x1f, prediv | ((pll_rdiv - 1) << 4));
1252aa288248SMaxime Ripard 	if (ret)
1253aa288248SMaxime Ripard 		return ret;
1254aa288248SMaxime Ripard 
1255aa288248SMaxime Ripard 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1256aa288248SMaxime Ripard 			      (ilog2(pclk_div) << 4));
1257aa288248SMaxime Ripard }
1258aa288248SMaxime Ripard 
125919a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */
1260bad1774eSJacopo Mondi static int ov5640_set_timings(struct ov5640_dev *sensor,
1261bad1774eSJacopo Mondi 			      const struct ov5640_mode_info *mode)
1262bad1774eSJacopo Mondi {
1263bad1774eSJacopo Mondi 	int ret;
1264bad1774eSJacopo Mondi 
1265bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1266bad1774eSJacopo Mondi 	if (ret < 0)
1267bad1774eSJacopo Mondi 		return ret;
1268bad1774eSJacopo Mondi 
1269bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1270bad1774eSJacopo Mondi 	if (ret < 0)
1271bad1774eSJacopo Mondi 		return ret;
1272bad1774eSJacopo Mondi 
1273bad1774eSJacopo Mondi 	ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1274bad1774eSJacopo Mondi 	if (ret < 0)
1275bad1774eSJacopo Mondi 		return ret;
1276bad1774eSJacopo Mondi 
1277bad1774eSJacopo Mondi 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1278bad1774eSJacopo Mondi }
1279bad1774eSJacopo Mondi 
128019a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor,
128119a81c14SSteve Longerbeam 			    const struct ov5640_mode_info *mode)
128219a81c14SSteve Longerbeam {
128319a81c14SSteve Longerbeam 	const struct reg_value *regs = mode->reg_data;
128419a81c14SSteve Longerbeam 	unsigned int i;
128519a81c14SSteve Longerbeam 	u32 delay_ms;
128619a81c14SSteve Longerbeam 	u16 reg_addr;
128719a81c14SSteve Longerbeam 	u8 mask, val;
128819a81c14SSteve Longerbeam 	int ret = 0;
128919a81c14SSteve Longerbeam 
129019a81c14SSteve Longerbeam 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
129119a81c14SSteve Longerbeam 		delay_ms = regs->delay_ms;
129219a81c14SSteve Longerbeam 		reg_addr = regs->reg_addr;
129319a81c14SSteve Longerbeam 		val = regs->val;
129419a81c14SSteve Longerbeam 		mask = regs->mask;
129519a81c14SSteve Longerbeam 
129619a81c14SSteve Longerbeam 		if (mask)
129719a81c14SSteve Longerbeam 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
129819a81c14SSteve Longerbeam 		else
129919a81c14SSteve Longerbeam 			ret = ov5640_write_reg(sensor, reg_addr, val);
130019a81c14SSteve Longerbeam 		if (ret)
130119a81c14SSteve Longerbeam 			break;
130219a81c14SSteve Longerbeam 
130319a81c14SSteve Longerbeam 		if (delay_ms)
130419a81c14SSteve Longerbeam 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
130519a81c14SSteve Longerbeam 	}
130619a81c14SSteve Longerbeam 
1307bad1774eSJacopo Mondi 	return ov5640_set_timings(sensor, mode);
130819a81c14SSteve Longerbeam }
130919a81c14SSteve Longerbeam 
1310dc29a1c1SHugues Fruchet static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1311dc29a1c1SHugues Fruchet {
1312dc29a1c1SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1313dc29a1c1SHugues Fruchet 			      BIT(0), on ? 0 : BIT(0));
1314dc29a1c1SHugues Fruchet }
1315dc29a1c1SHugues Fruchet 
131619a81c14SSteve Longerbeam /* read exposure, in number of line periods */
131719a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor)
131819a81c14SSteve Longerbeam {
131919a81c14SSteve Longerbeam 	int exp, ret;
132019a81c14SSteve Longerbeam 	u8 temp;
132119a81c14SSteve Longerbeam 
132219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
132319a81c14SSteve Longerbeam 	if (ret)
132419a81c14SSteve Longerbeam 		return ret;
132519a81c14SSteve Longerbeam 	exp = ((int)temp & 0x0f) << 16;
132619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
132719a81c14SSteve Longerbeam 	if (ret)
132819a81c14SSteve Longerbeam 		return ret;
132919a81c14SSteve Longerbeam 	exp |= ((int)temp << 8);
133019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
133119a81c14SSteve Longerbeam 	if (ret)
133219a81c14SSteve Longerbeam 		return ret;
133319a81c14SSteve Longerbeam 	exp |= (int)temp;
133419a81c14SSteve Longerbeam 
133519a81c14SSteve Longerbeam 	return exp >> 4;
133619a81c14SSteve Longerbeam }
133719a81c14SSteve Longerbeam 
133819a81c14SSteve Longerbeam /* write exposure, given number of line periods */
133919a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
134019a81c14SSteve Longerbeam {
134119a81c14SSteve Longerbeam 	int ret;
134219a81c14SSteve Longerbeam 
134319a81c14SSteve Longerbeam 	exposure <<= 4;
134419a81c14SSteve Longerbeam 
134519a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
134619a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
134719a81c14SSteve Longerbeam 			       exposure & 0xff);
134819a81c14SSteve Longerbeam 	if (ret)
134919a81c14SSteve Longerbeam 		return ret;
135019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
135119a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
135219a81c14SSteve Longerbeam 			       (exposure >> 8) & 0xff);
135319a81c14SSteve Longerbeam 	if (ret)
135419a81c14SSteve Longerbeam 		return ret;
135519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor,
135619a81c14SSteve Longerbeam 				OV5640_REG_AEC_PK_EXPOSURE_HI,
135719a81c14SSteve Longerbeam 				(exposure >> 16) & 0x0f);
135819a81c14SSteve Longerbeam }
135919a81c14SSteve Longerbeam 
136019a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor)
136119a81c14SSteve Longerbeam {
136219a81c14SSteve Longerbeam 	u16 gain;
136319a81c14SSteve Longerbeam 	int ret;
136419a81c14SSteve Longerbeam 
136519a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
136619a81c14SSteve Longerbeam 	if (ret)
136719a81c14SSteve Longerbeam 		return ret;
136819a81c14SSteve Longerbeam 
136919a81c14SSteve Longerbeam 	return gain & 0x3ff;
137019a81c14SSteve Longerbeam }
137119a81c14SSteve Longerbeam 
13723cca8ef5SHugues Fruchet static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
13733cca8ef5SHugues Fruchet {
13743cca8ef5SHugues Fruchet 	return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
13753cca8ef5SHugues Fruchet 				  (u16)gain & 0x3ff);
13763cca8ef5SHugues Fruchet }
13773cca8ef5SHugues Fruchet 
13783cca8ef5SHugues Fruchet static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
13793cca8ef5SHugues Fruchet {
13803cca8ef5SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
13813cca8ef5SHugues Fruchet 			      BIT(1), on ? 0 : BIT(1));
13823cca8ef5SHugues Fruchet }
13833cca8ef5SHugues Fruchet 
1384f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1385f22996dbSHugues Fruchet {
1386f22996dbSHugues Fruchet 	int ret;
1387f22996dbSHugues Fruchet 	unsigned int flags = sensor->ep.bus.parallel.flags;
1388f22996dbSHugues Fruchet 	u8 pclk_pol = 0;
1389f22996dbSHugues Fruchet 	u8 hsync_pol = 0;
1390f22996dbSHugues Fruchet 	u8 vsync_pol = 0;
1391f22996dbSHugues Fruchet 
1392f22996dbSHugues Fruchet 	/*
1393f22996dbSHugues Fruchet 	 * Note about parallel port configuration.
1394f22996dbSHugues Fruchet 	 *
1395f22996dbSHugues Fruchet 	 * When configured in parallel mode, the OV5640 will
1396f22996dbSHugues Fruchet 	 * output 10 bits data on DVP data lines [9:0].
1397f22996dbSHugues Fruchet 	 * If only 8 bits data are wanted, the 8 bits data lines
1398f22996dbSHugues Fruchet 	 * of the camera interface must be physically connected
1399f22996dbSHugues Fruchet 	 * on the DVP data lines [9:2].
1400f22996dbSHugues Fruchet 	 *
1401f22996dbSHugues Fruchet 	 * Control lines polarity can be configured through
1402f22996dbSHugues Fruchet 	 * devicetree endpoint control lines properties.
1403f22996dbSHugues Fruchet 	 * If no endpoint control lines properties are set,
1404f22996dbSHugues Fruchet 	 * polarity will be as below:
1405f22996dbSHugues Fruchet 	 * - VSYNC:	active high
1406f22996dbSHugues Fruchet 	 * - HREF:	active low
1407f22996dbSHugues Fruchet 	 * - PCLK:	active low
1408f22996dbSHugues Fruchet 	 */
1409f22996dbSHugues Fruchet 
1410f22996dbSHugues Fruchet 	if (on) {
1411f22996dbSHugues Fruchet 		/*
1412f22996dbSHugues Fruchet 		 * reset MIPI PCLK/SERCLK divider
1413f22996dbSHugues Fruchet 		 *
1414f22996dbSHugues Fruchet 		 * SC PLL CONTRL1 0
1415f22996dbSHugues Fruchet 		 * - [3..0]:	MIPI PCLK/SERCLK divider
1416f22996dbSHugues Fruchet 		 */
1417f22996dbSHugues Fruchet 		ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1418f22996dbSHugues Fruchet 		if (ret)
1419f22996dbSHugues Fruchet 			return ret;
1420f22996dbSHugues Fruchet 
1421f22996dbSHugues Fruchet 		/*
1422f22996dbSHugues Fruchet 		 * configure parallel port control lines polarity
1423f22996dbSHugues Fruchet 		 *
1424f22996dbSHugues Fruchet 		 * POLARITY CTRL0
1425f22996dbSHugues Fruchet 		 * - [5]:	PCLK polarity (0: active low, 1: active high)
1426f22996dbSHugues Fruchet 		 * - [1]:	HREF polarity (0: active low, 1: active high)
1427f22996dbSHugues Fruchet 		 * - [0]:	VSYNC polarity (mismatch here between
1428f22996dbSHugues Fruchet 		 *		datasheet and hardware, 0 is active high
1429f22996dbSHugues Fruchet 		 *		and 1 is active low...)
1430f22996dbSHugues Fruchet 		 */
1431f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1432f22996dbSHugues Fruchet 			pclk_pol = 1;
1433f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1434f22996dbSHugues Fruchet 			hsync_pol = 1;
1435f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1436f22996dbSHugues Fruchet 			vsync_pol = 1;
1437f22996dbSHugues Fruchet 
1438f22996dbSHugues Fruchet 		ret = ov5640_write_reg(sensor,
1439f22996dbSHugues Fruchet 				       OV5640_REG_POLARITY_CTRL00,
1440f22996dbSHugues Fruchet 				       (pclk_pol << 5) |
1441f22996dbSHugues Fruchet 				       (hsync_pol << 1) |
1442f22996dbSHugues Fruchet 				       vsync_pol);
1443f22996dbSHugues Fruchet 
1444f22996dbSHugues Fruchet 		if (ret)
1445f22996dbSHugues Fruchet 			return ret;
1446f22996dbSHugues Fruchet 	}
1447f22996dbSHugues Fruchet 
1448f22996dbSHugues Fruchet 	/*
1449f22996dbSHugues Fruchet 	 * powerdown MIPI TX/RX PHY & disable MIPI
1450f22996dbSHugues Fruchet 	 *
1451f22996dbSHugues Fruchet 	 * MIPI CONTROL 00
1452f22996dbSHugues Fruchet 	 * 4:	 PWDN PHY TX
1453f22996dbSHugues Fruchet 	 * 3:	 PWDN PHY RX
1454f22996dbSHugues Fruchet 	 * 2:	 MIPI enable
1455f22996dbSHugues Fruchet 	 */
1456f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1457f22996dbSHugues Fruchet 			       OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1458f22996dbSHugues Fruchet 	if (ret)
1459f22996dbSHugues Fruchet 		return ret;
1460f22996dbSHugues Fruchet 
1461f22996dbSHugues Fruchet 	/*
1462f22996dbSHugues Fruchet 	 * enable VSYNC/HREF/PCLK DVP control lines
1463f22996dbSHugues Fruchet 	 * & D[9:6] DVP data lines
1464f22996dbSHugues Fruchet 	 *
1465f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 01
1466f22996dbSHugues Fruchet 	 * - 6:		VSYNC output enable
1467f22996dbSHugues Fruchet 	 * - 5:		HREF output enable
1468f22996dbSHugues Fruchet 	 * - 4:		PCLK output enable
1469f22996dbSHugues Fruchet 	 * - [3:0]:	D[9:6] output enable
1470f22996dbSHugues Fruchet 	 */
1471f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1472f22996dbSHugues Fruchet 			       OV5640_REG_PAD_OUTPUT_ENABLE01,
1473f22996dbSHugues Fruchet 			       on ? 0x7f : 0);
1474f22996dbSHugues Fruchet 	if (ret)
1475f22996dbSHugues Fruchet 		return ret;
1476f22996dbSHugues Fruchet 
1477f22996dbSHugues Fruchet 	/*
1478f22996dbSHugues Fruchet 	 * enable D[5:0] DVP data lines
1479f22996dbSHugues Fruchet 	 *
1480f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 02
1481f22996dbSHugues Fruchet 	 * - [7:2]:	D[5:0] output enable
1482f22996dbSHugues Fruchet 	 */
1483f22996dbSHugues Fruchet 	return ov5640_write_reg(sensor,
1484f22996dbSHugues Fruchet 				OV5640_REG_PAD_OUTPUT_ENABLE02,
1485f22996dbSHugues Fruchet 				on ? 0xfc : 0);
1486f22996dbSHugues Fruchet }
1487f22996dbSHugues Fruchet 
1488f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
148919a81c14SSteve Longerbeam {
149019a81c14SSteve Longerbeam 	int ret;
149119a81c14SSteve Longerbeam 
1492aa4bb8b8SJacopo Mondi 	/*
1493aa4bb8b8SJacopo Mondi 	 * Enable/disable the MIPI interface
1494aa4bb8b8SJacopo Mondi 	 *
1495aa4bb8b8SJacopo Mondi 	 * 0x300e = on ? 0x45 : 0x40
1496aa4bb8b8SJacopo Mondi 	 *
1497aa4bb8b8SJacopo Mondi 	 * FIXME: the sensor manual (version 2.03) reports
1498aa4bb8b8SJacopo Mondi 	 * [7:5] = 000  : 1 data lane mode
1499aa4bb8b8SJacopo Mondi 	 * [7:5] = 001  : 2 data lanes mode
1500aa4bb8b8SJacopo Mondi 	 * But this settings do not work, while the following ones
1501aa4bb8b8SJacopo Mondi 	 * have been validated for 2 data lanes mode.
1502aa4bb8b8SJacopo Mondi 	 *
1503aa4bb8b8SJacopo Mondi 	 * [7:5] = 010	: 2 data lanes mode
1504aa4bb8b8SJacopo Mondi 	 * [4] = 0	: Power up MIPI HS Tx
1505aa4bb8b8SJacopo Mondi 	 * [3] = 0	: Power up MIPI LS Rx
1506aa4bb8b8SJacopo Mondi 	 * [2] = 1/0	: MIPI interface enable/disable
1507aa4bb8b8SJacopo Mondi 	 * [1:0] = 01/00: FIXME: 'debug'
1508aa4bb8b8SJacopo Mondi 	 */
1509aa4bb8b8SJacopo Mondi 	ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1510aa4bb8b8SJacopo Mondi 			       on ? 0x45 : 0x40);
151119a81c14SSteve Longerbeam 	if (ret)
151219a81c14SSteve Longerbeam 		return ret;
151319a81c14SSteve Longerbeam 
151419a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
151519a81c14SSteve Longerbeam 				on ? 0x00 : 0x0f);
151619a81c14SSteve Longerbeam }
151719a81c14SSteve Longerbeam 
151819a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor)
151919a81c14SSteve Longerbeam {
152019a81c14SSteve Longerbeam 	 /* calculate sysclk */
152119a81c14SSteve Longerbeam 	u32 xvclk = sensor->xclk_freq / 10000;
152219a81c14SSteve Longerbeam 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
152319a81c14SSteve Longerbeam 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
152419a81c14SSteve Longerbeam 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
152519a81c14SSteve Longerbeam 	u8 temp1, temp2;
152619a81c14SSteve Longerbeam 	int ret;
152719a81c14SSteve Longerbeam 
152819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
152919a81c14SSteve Longerbeam 	if (ret)
153019a81c14SSteve Longerbeam 		return ret;
153119a81c14SSteve Longerbeam 	temp2 = temp1 & 0x0f;
153219a81c14SSteve Longerbeam 	if (temp2 == 8 || temp2 == 10)
153319a81c14SSteve Longerbeam 		bit_div2x = temp2 / 2;
153419a81c14SSteve Longerbeam 
153519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
153619a81c14SSteve Longerbeam 	if (ret)
153719a81c14SSteve Longerbeam 		return ret;
153819a81c14SSteve Longerbeam 	sysdiv = temp1 >> 4;
153919a81c14SSteve Longerbeam 	if (sysdiv == 0)
154019a81c14SSteve Longerbeam 		sysdiv = 16;
154119a81c14SSteve Longerbeam 
154219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
154319a81c14SSteve Longerbeam 	if (ret)
154419a81c14SSteve Longerbeam 		return ret;
154519a81c14SSteve Longerbeam 	multiplier = temp1;
154619a81c14SSteve Longerbeam 
154719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
154819a81c14SSteve Longerbeam 	if (ret)
154919a81c14SSteve Longerbeam 		return ret;
155019a81c14SSteve Longerbeam 	prediv = temp1 & 0x0f;
155119a81c14SSteve Longerbeam 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
155219a81c14SSteve Longerbeam 
155319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
155419a81c14SSteve Longerbeam 	if (ret)
155519a81c14SSteve Longerbeam 		return ret;
155619a81c14SSteve Longerbeam 	temp2 = temp1 & 0x03;
155719a81c14SSteve Longerbeam 	sclk_rdiv = sclk_rdiv_map[temp2];
155819a81c14SSteve Longerbeam 
155919a81c14SSteve Longerbeam 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
156019a81c14SSteve Longerbeam 		return -EINVAL;
156119a81c14SSteve Longerbeam 
156219a81c14SSteve Longerbeam 	VCO = xvclk * multiplier / prediv;
156319a81c14SSteve Longerbeam 
156419a81c14SSteve Longerbeam 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
156519a81c14SSteve Longerbeam 
156619a81c14SSteve Longerbeam 	return sysclk;
156719a81c14SSteve Longerbeam }
156819a81c14SSteve Longerbeam 
156919a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor)
157019a81c14SSteve Longerbeam {
157119a81c14SSteve Longerbeam 	 /* read HTS from register settings */
157219a81c14SSteve Longerbeam 	u8 mode;
157319a81c14SSteve Longerbeam 	int ret;
157419a81c14SSteve Longerbeam 
157519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
157619a81c14SSteve Longerbeam 	if (ret)
157719a81c14SSteve Longerbeam 		return ret;
157819a81c14SSteve Longerbeam 	mode &= 0xfb;
157919a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
158019a81c14SSteve Longerbeam }
158119a81c14SSteve Longerbeam 
158219a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor)
158319a81c14SSteve Longerbeam {
158419a81c14SSteve Longerbeam 	/* read HTS from register settings */
158519a81c14SSteve Longerbeam 	u16 hts;
158619a81c14SSteve Longerbeam 	int ret;
158719a81c14SSteve Longerbeam 
158819a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
158919a81c14SSteve Longerbeam 	if (ret)
159019a81c14SSteve Longerbeam 		return ret;
159119a81c14SSteve Longerbeam 	return hts;
159219a81c14SSteve Longerbeam }
159319a81c14SSteve Longerbeam 
159419a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor)
159519a81c14SSteve Longerbeam {
159619a81c14SSteve Longerbeam 	u16 vts;
159719a81c14SSteve Longerbeam 	int ret;
159819a81c14SSteve Longerbeam 
159919a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
160019a81c14SSteve Longerbeam 	if (ret)
160119a81c14SSteve Longerbeam 		return ret;
160219a81c14SSteve Longerbeam 	return vts;
160319a81c14SSteve Longerbeam }
160419a81c14SSteve Longerbeam 
160519a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
160619a81c14SSteve Longerbeam {
160719a81c14SSteve Longerbeam 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
160819a81c14SSteve Longerbeam }
160919a81c14SSteve Longerbeam 
161019a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor)
161119a81c14SSteve Longerbeam {
161219a81c14SSteve Longerbeam 	/* get banding filter value */
161319a81c14SSteve Longerbeam 	int ret, light_freq = 0;
161419a81c14SSteve Longerbeam 	u8 temp, temp1;
161519a81c14SSteve Longerbeam 
161619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
161719a81c14SSteve Longerbeam 	if (ret)
161819a81c14SSteve Longerbeam 		return ret;
161919a81c14SSteve Longerbeam 
162019a81c14SSteve Longerbeam 	if (temp & 0x80) {
162119a81c14SSteve Longerbeam 		/* manual */
162219a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
162319a81c14SSteve Longerbeam 				      &temp1);
162419a81c14SSteve Longerbeam 		if (ret)
162519a81c14SSteve Longerbeam 			return ret;
162619a81c14SSteve Longerbeam 		if (temp1 & 0x04) {
162719a81c14SSteve Longerbeam 			/* 50Hz */
162819a81c14SSteve Longerbeam 			light_freq = 50;
162919a81c14SSteve Longerbeam 		} else {
163019a81c14SSteve Longerbeam 			/* 60Hz */
163119a81c14SSteve Longerbeam 			light_freq = 60;
163219a81c14SSteve Longerbeam 		}
163319a81c14SSteve Longerbeam 	} else {
163419a81c14SSteve Longerbeam 		/* auto */
163519a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
163619a81c14SSteve Longerbeam 				      &temp1);
163719a81c14SSteve Longerbeam 		if (ret)
163819a81c14SSteve Longerbeam 			return ret;
163919a81c14SSteve Longerbeam 
164019a81c14SSteve Longerbeam 		if (temp1 & 0x01) {
164119a81c14SSteve Longerbeam 			/* 50Hz */
164219a81c14SSteve Longerbeam 			light_freq = 50;
164319a81c14SSteve Longerbeam 		} else {
164419a81c14SSteve Longerbeam 			/* 60Hz */
164519a81c14SSteve Longerbeam 		}
164619a81c14SSteve Longerbeam 	}
164719a81c14SSteve Longerbeam 
164819a81c14SSteve Longerbeam 	return light_freq;
164919a81c14SSteve Longerbeam }
165019a81c14SSteve Longerbeam 
165119a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
165219a81c14SSteve Longerbeam {
165319a81c14SSteve Longerbeam 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
165419a81c14SSteve Longerbeam 	int ret;
165519a81c14SSteve Longerbeam 
165619a81c14SSteve Longerbeam 	/* read preview PCLK */
165719a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
165819a81c14SSteve Longerbeam 	if (ret < 0)
165919a81c14SSteve Longerbeam 		return ret;
166019a81c14SSteve Longerbeam 	if (ret == 0)
166119a81c14SSteve Longerbeam 		return -EINVAL;
166219a81c14SSteve Longerbeam 	sensor->prev_sysclk = ret;
166319a81c14SSteve Longerbeam 	/* read preview HTS */
166419a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
166519a81c14SSteve Longerbeam 	if (ret < 0)
166619a81c14SSteve Longerbeam 		return ret;
166719a81c14SSteve Longerbeam 	if (ret == 0)
166819a81c14SSteve Longerbeam 		return -EINVAL;
166919a81c14SSteve Longerbeam 	sensor->prev_hts = ret;
167019a81c14SSteve Longerbeam 
167119a81c14SSteve Longerbeam 	/* read preview VTS */
167219a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
167319a81c14SSteve Longerbeam 	if (ret < 0)
167419a81c14SSteve Longerbeam 		return ret;
167519a81c14SSteve Longerbeam 	prev_vts = ret;
167619a81c14SSteve Longerbeam 
167719a81c14SSteve Longerbeam 	/* calculate banding filter */
167819a81c14SSteve Longerbeam 	/* 60Hz */
167919a81c14SSteve Longerbeam 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
168019a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
168119a81c14SSteve Longerbeam 	if (ret)
168219a81c14SSteve Longerbeam 		return ret;
168319a81c14SSteve Longerbeam 	if (!band_step60)
168419a81c14SSteve Longerbeam 		return -EINVAL;
168519a81c14SSteve Longerbeam 	max_band60 = (int)((prev_vts - 4) / band_step60);
168619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
168719a81c14SSteve Longerbeam 	if (ret)
168819a81c14SSteve Longerbeam 		return ret;
168919a81c14SSteve Longerbeam 
169019a81c14SSteve Longerbeam 	/* 50Hz */
169119a81c14SSteve Longerbeam 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
169219a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
169319a81c14SSteve Longerbeam 	if (ret)
169419a81c14SSteve Longerbeam 		return ret;
169519a81c14SSteve Longerbeam 	if (!band_step50)
169619a81c14SSteve Longerbeam 		return -EINVAL;
169719a81c14SSteve Longerbeam 	max_band50 = (int)((prev_vts - 4) / band_step50);
169819a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
169919a81c14SSteve Longerbeam }
170019a81c14SSteve Longerbeam 
170119a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
170219a81c14SSteve Longerbeam {
170319a81c14SSteve Longerbeam 	/* stable in high */
170419a81c14SSteve Longerbeam 	u32 fast_high, fast_low;
170519a81c14SSteve Longerbeam 	int ret;
170619a81c14SSteve Longerbeam 
170719a81c14SSteve Longerbeam 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
170819a81c14SSteve Longerbeam 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
170919a81c14SSteve Longerbeam 
171019a81c14SSteve Longerbeam 	fast_high = sensor->ae_high << 1;
171119a81c14SSteve Longerbeam 	if (fast_high > 255)
171219a81c14SSteve Longerbeam 		fast_high = 255;
171319a81c14SSteve Longerbeam 
171419a81c14SSteve Longerbeam 	fast_low = sensor->ae_low >> 1;
171519a81c14SSteve Longerbeam 
171619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
171719a81c14SSteve Longerbeam 	if (ret)
171819a81c14SSteve Longerbeam 		return ret;
171919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
172019a81c14SSteve Longerbeam 	if (ret)
172119a81c14SSteve Longerbeam 		return ret;
172219a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
172319a81c14SSteve Longerbeam 	if (ret)
172419a81c14SSteve Longerbeam 		return ret;
172519a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
172619a81c14SSteve Longerbeam 	if (ret)
172719a81c14SSteve Longerbeam 		return ret;
172819a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
172919a81c14SSteve Longerbeam 	if (ret)
173019a81c14SSteve Longerbeam 		return ret;
173119a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
173219a81c14SSteve Longerbeam }
173319a81c14SSteve Longerbeam 
1734c2c3f42dSHugues Fruchet static int ov5640_get_binning(struct ov5640_dev *sensor)
173519a81c14SSteve Longerbeam {
173619a81c14SSteve Longerbeam 	u8 temp;
173719a81c14SSteve Longerbeam 	int ret;
173819a81c14SSteve Longerbeam 
173919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
174019a81c14SSteve Longerbeam 	if (ret)
174119a81c14SSteve Longerbeam 		return ret;
1742c2c3f42dSHugues Fruchet 
1743c2c3f42dSHugues Fruchet 	return temp & BIT(0);
174419a81c14SSteve Longerbeam }
174519a81c14SSteve Longerbeam 
1746ce85705aSHugues Fruchet static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1747ce85705aSHugues Fruchet {
1748ce85705aSHugues Fruchet 	int ret;
1749ce85705aSHugues Fruchet 
1750ce85705aSHugues Fruchet 	/*
1751ce85705aSHugues Fruchet 	 * TIMING TC REG21:
1752ce85705aSHugues Fruchet 	 * - [0]:	Horizontal binning enable
1753ce85705aSHugues Fruchet 	 */
1754ce85705aSHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1755ce85705aSHugues Fruchet 			     BIT(0), enable ? BIT(0) : 0);
1756ce85705aSHugues Fruchet 	if (ret)
1757ce85705aSHugues Fruchet 		return ret;
1758ce85705aSHugues Fruchet 	/*
1759ce85705aSHugues Fruchet 	 * TIMING TC REG20:
1760ce85705aSHugues Fruchet 	 * - [0]:	Undocumented, but hardcoded init sequences
1761ce85705aSHugues Fruchet 	 *		are always setting REG21/REG20 bit 0 to same value...
1762ce85705aSHugues Fruchet 	 */
1763ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1764ce85705aSHugues Fruchet 			      BIT(0), enable ? BIT(0) : 0);
1765ce85705aSHugues Fruchet }
1766ce85705aSHugues Fruchet 
176719a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
176819a81c14SSteve Longerbeam {
17698670d70aSHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
177019a81c14SSteve Longerbeam 	u8 temp, channel = virtual_channel;
177119a81c14SSteve Longerbeam 	int ret;
177219a81c14SSteve Longerbeam 
17738670d70aSHugues Fruchet 	if (channel > 3) {
17748670d70aSHugues Fruchet 		dev_err(&client->dev,
17758670d70aSHugues Fruchet 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
17768670d70aSHugues Fruchet 			__func__, channel);
177719a81c14SSteve Longerbeam 		return -EINVAL;
17788670d70aSHugues Fruchet 	}
177919a81c14SSteve Longerbeam 
178019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
178119a81c14SSteve Longerbeam 	if (ret)
178219a81c14SSteve Longerbeam 		return ret;
178319a81c14SSteve Longerbeam 	temp &= ~(3 << 6);
178419a81c14SSteve Longerbeam 	temp |= (channel << 6);
178519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
178619a81c14SSteve Longerbeam }
178719a81c14SSteve Longerbeam 
178819a81c14SSteve Longerbeam static const struct ov5640_mode_info *
178919a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
179019a81c14SSteve Longerbeam 		 int width, int height, bool nearest)
179119a81c14SSteve Longerbeam {
17923c4a7372SHugues Fruchet 	const struct ov5640_mode_info *mode;
179319a81c14SSteve Longerbeam 
17943c4a7372SHugues Fruchet 	mode = v4l2_find_nearest_size(ov5640_mode_data[fr],
17953c4a7372SHugues Fruchet 				      ARRAY_SIZE(ov5640_mode_data[fr]),
17963c4a7372SHugues Fruchet 				      hact, vact,
17973c4a7372SHugues Fruchet 				      width, height);
179819a81c14SSteve Longerbeam 
17993c4a7372SHugues Fruchet 	if (!mode ||
18003c4a7372SHugues Fruchet 	    (!nearest && (mode->hact != width || mode->vact != height)))
18013c4a7372SHugues Fruchet 		return NULL;
180219a81c14SSteve Longerbeam 
180319a81c14SSteve Longerbeam 	return mode;
180419a81c14SSteve Longerbeam }
180519a81c14SSteve Longerbeam 
180619a81c14SSteve Longerbeam /*
180719a81c14SSteve Longerbeam  * sensor changes between scaling and subsampling, go through
180819a81c14SSteve Longerbeam  * exposure calculation
180919a81c14SSteve Longerbeam  */
181041d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
181141d8d7f5SHugues Fruchet 					 const struct ov5640_mode_info *mode)
181219a81c14SSteve Longerbeam {
181319a81c14SSteve Longerbeam 	u32 prev_shutter, prev_gain16;
181419a81c14SSteve Longerbeam 	u32 cap_shutter, cap_gain16;
181519a81c14SSteve Longerbeam 	u32 cap_sysclk, cap_hts, cap_vts;
181619a81c14SSteve Longerbeam 	u32 light_freq, cap_bandfilt, cap_maxband;
181719a81c14SSteve Longerbeam 	u32 cap_gain16_shutter;
181819a81c14SSteve Longerbeam 	u8 average;
181919a81c14SSteve Longerbeam 	int ret;
182019a81c14SSteve Longerbeam 
182141d8d7f5SHugues Fruchet 	if (!mode->reg_data)
182219a81c14SSteve Longerbeam 		return -EINVAL;
182319a81c14SSteve Longerbeam 
182419a81c14SSteve Longerbeam 	/* read preview shutter */
182519a81c14SSteve Longerbeam 	ret = ov5640_get_exposure(sensor);
182619a81c14SSteve Longerbeam 	if (ret < 0)
182719a81c14SSteve Longerbeam 		return ret;
182819a81c14SSteve Longerbeam 	prev_shutter = ret;
1829c2c3f42dSHugues Fruchet 	ret = ov5640_get_binning(sensor);
183019a81c14SSteve Longerbeam 	if (ret < 0)
183119a81c14SSteve Longerbeam 		return ret;
183219a81c14SSteve Longerbeam 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
183319a81c14SSteve Longerbeam 	    mode->id != OV5640_MODE_1080P_1920_1080)
183419a81c14SSteve Longerbeam 		prev_shutter *= 2;
183519a81c14SSteve Longerbeam 
183619a81c14SSteve Longerbeam 	/* read preview gain */
183719a81c14SSteve Longerbeam 	ret = ov5640_get_gain(sensor);
183819a81c14SSteve Longerbeam 	if (ret < 0)
183919a81c14SSteve Longerbeam 		return ret;
184019a81c14SSteve Longerbeam 	prev_gain16 = ret;
184119a81c14SSteve Longerbeam 
184219a81c14SSteve Longerbeam 	/* get average */
184319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
184419a81c14SSteve Longerbeam 	if (ret)
184519a81c14SSteve Longerbeam 		return ret;
184619a81c14SSteve Longerbeam 
184719a81c14SSteve Longerbeam 	/* turn off night mode for capture */
184819a81c14SSteve Longerbeam 	ret = ov5640_set_night_mode(sensor);
184919a81c14SSteve Longerbeam 	if (ret < 0)
185019a81c14SSteve Longerbeam 		return ret;
185119a81c14SSteve Longerbeam 
185219a81c14SSteve Longerbeam 	/* Write capture setting */
185319a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
185419a81c14SSteve Longerbeam 	if (ret < 0)
185519a81c14SSteve Longerbeam 		return ret;
185619a81c14SSteve Longerbeam 
185719a81c14SSteve Longerbeam 	/* read capture VTS */
185819a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
185919a81c14SSteve Longerbeam 	if (ret < 0)
186019a81c14SSteve Longerbeam 		return ret;
186119a81c14SSteve Longerbeam 	cap_vts = ret;
186219a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
186319a81c14SSteve Longerbeam 	if (ret < 0)
186419a81c14SSteve Longerbeam 		return ret;
186519a81c14SSteve Longerbeam 	if (ret == 0)
186619a81c14SSteve Longerbeam 		return -EINVAL;
186719a81c14SSteve Longerbeam 	cap_hts = ret;
186819a81c14SSteve Longerbeam 
186919a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
187019a81c14SSteve Longerbeam 	if (ret < 0)
187119a81c14SSteve Longerbeam 		return ret;
187219a81c14SSteve Longerbeam 	if (ret == 0)
187319a81c14SSteve Longerbeam 		return -EINVAL;
187419a81c14SSteve Longerbeam 	cap_sysclk = ret;
187519a81c14SSteve Longerbeam 
187619a81c14SSteve Longerbeam 	/* calculate capture banding filter */
187719a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
187819a81c14SSteve Longerbeam 	if (ret < 0)
187919a81c14SSteve Longerbeam 		return ret;
188019a81c14SSteve Longerbeam 	light_freq = ret;
188119a81c14SSteve Longerbeam 
188219a81c14SSteve Longerbeam 	if (light_freq == 60) {
188319a81c14SSteve Longerbeam 		/* 60Hz */
188419a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
188519a81c14SSteve Longerbeam 	} else {
188619a81c14SSteve Longerbeam 		/* 50Hz */
188719a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
188819a81c14SSteve Longerbeam 	}
188919a81c14SSteve Longerbeam 
189019a81c14SSteve Longerbeam 	if (!sensor->prev_sysclk) {
189119a81c14SSteve Longerbeam 		ret = ov5640_get_sysclk(sensor);
189219a81c14SSteve Longerbeam 		if (ret < 0)
189319a81c14SSteve Longerbeam 			return ret;
189419a81c14SSteve Longerbeam 		if (ret == 0)
189519a81c14SSteve Longerbeam 			return -EINVAL;
189619a81c14SSteve Longerbeam 		sensor->prev_sysclk = ret;
189719a81c14SSteve Longerbeam 	}
189819a81c14SSteve Longerbeam 
189919a81c14SSteve Longerbeam 	if (!cap_bandfilt)
190019a81c14SSteve Longerbeam 		return -EINVAL;
190119a81c14SSteve Longerbeam 
190219a81c14SSteve Longerbeam 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
190319a81c14SSteve Longerbeam 
190419a81c14SSteve Longerbeam 	/* calculate capture shutter/gain16 */
190519a81c14SSteve Longerbeam 	if (average > sensor->ae_low && average < sensor->ae_high) {
190619a81c14SSteve Longerbeam 		/* in stable range */
190719a81c14SSteve Longerbeam 		cap_gain16_shutter =
190819a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
190919a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
191019a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts *
191119a81c14SSteve Longerbeam 			sensor->ae_target / average;
191219a81c14SSteve Longerbeam 	} else {
191319a81c14SSteve Longerbeam 		cap_gain16_shutter =
191419a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
191519a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
191619a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts;
191719a81c14SSteve Longerbeam 	}
191819a81c14SSteve Longerbeam 
191919a81c14SSteve Longerbeam 	/* gain to shutter */
192019a81c14SSteve Longerbeam 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
192119a81c14SSteve Longerbeam 		/* shutter < 1/100 */
192219a81c14SSteve Longerbeam 		cap_shutter = cap_gain16_shutter / 16;
192319a81c14SSteve Longerbeam 		if (cap_shutter < 1)
192419a81c14SSteve Longerbeam 			cap_shutter = 1;
192519a81c14SSteve Longerbeam 
192619a81c14SSteve Longerbeam 		cap_gain16 = cap_gain16_shutter / cap_shutter;
192719a81c14SSteve Longerbeam 		if (cap_gain16 < 16)
192819a81c14SSteve Longerbeam 			cap_gain16 = 16;
192919a81c14SSteve Longerbeam 	} else {
193019a81c14SSteve Longerbeam 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
193119a81c14SSteve Longerbeam 			/* exposure reach max */
193219a81c14SSteve Longerbeam 			cap_shutter = cap_bandfilt * cap_maxband;
193319a81c14SSteve Longerbeam 			if (!cap_shutter)
193419a81c14SSteve Longerbeam 				return -EINVAL;
193519a81c14SSteve Longerbeam 
193619a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
193719a81c14SSteve Longerbeam 		} else {
193819a81c14SSteve Longerbeam 			/* 1/100 < (cap_shutter = n/100) =< max */
193919a81c14SSteve Longerbeam 			cap_shutter =
194019a81c14SSteve Longerbeam 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
194119a81c14SSteve Longerbeam 				* cap_bandfilt;
194219a81c14SSteve Longerbeam 			if (!cap_shutter)
194319a81c14SSteve Longerbeam 				return -EINVAL;
194419a81c14SSteve Longerbeam 
194519a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
194619a81c14SSteve Longerbeam 		}
194719a81c14SSteve Longerbeam 	}
194819a81c14SSteve Longerbeam 
194919a81c14SSteve Longerbeam 	/* set capture gain */
19503cca8ef5SHugues Fruchet 	ret = ov5640_set_gain(sensor, cap_gain16);
195119a81c14SSteve Longerbeam 	if (ret)
195219a81c14SSteve Longerbeam 		return ret;
195319a81c14SSteve Longerbeam 
195419a81c14SSteve Longerbeam 	/* write capture shutter */
195519a81c14SSteve Longerbeam 	if (cap_shutter > (cap_vts - 4)) {
195619a81c14SSteve Longerbeam 		cap_vts = cap_shutter + 4;
195719a81c14SSteve Longerbeam 		ret = ov5640_set_vts(sensor, cap_vts);
195819a81c14SSteve Longerbeam 		if (ret < 0)
195919a81c14SSteve Longerbeam 			return ret;
196019a81c14SSteve Longerbeam 	}
196119a81c14SSteve Longerbeam 
196219a81c14SSteve Longerbeam 	/* set exposure */
19633cca8ef5SHugues Fruchet 	return ov5640_set_exposure(sensor, cap_shutter);
196419a81c14SSteve Longerbeam }
196519a81c14SSteve Longerbeam 
196619a81c14SSteve Longerbeam /*
196719a81c14SSteve Longerbeam  * if sensor changes inside scaling or subsampling
196819a81c14SSteve Longerbeam  * change mode directly
196919a81c14SSteve Longerbeam  */
197019a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
19713cca8ef5SHugues Fruchet 				  const struct ov5640_mode_info *mode)
197219a81c14SSteve Longerbeam {
197341d8d7f5SHugues Fruchet 	if (!mode->reg_data)
197419a81c14SSteve Longerbeam 		return -EINVAL;
197519a81c14SSteve Longerbeam 
197619a81c14SSteve Longerbeam 	/* Write capture setting */
19773cca8ef5SHugues Fruchet 	return ov5640_load_regs(sensor, mode);
197819a81c14SSteve Longerbeam }
197919a81c14SSteve Longerbeam 
1980985cdcb0SHugues Fruchet static int ov5640_set_mode(struct ov5640_dev *sensor)
198119a81c14SSteve Longerbeam {
198219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = sensor->current_mode;
1983985cdcb0SHugues Fruchet 	const struct ov5640_mode_info *orig_mode = sensor->last_mode;
198419a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
19853cca8ef5SHugues Fruchet 	bool auto_gain = sensor->ctrls.auto_gain->val == 1;
1986dc29a1c1SHugues Fruchet 	bool auto_exp =  sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
1987aa288248SMaxime Ripard 	unsigned long rate;
198819a81c14SSteve Longerbeam 	int ret;
198919a81c14SSteve Longerbeam 
199019a81c14SSteve Longerbeam 	dn_mode = mode->dn_mode;
199119a81c14SSteve Longerbeam 	orig_dn_mode = orig_mode->dn_mode;
199219a81c14SSteve Longerbeam 
199319a81c14SSteve Longerbeam 	/* auto gain and exposure must be turned off when changing modes */
19943cca8ef5SHugues Fruchet 	if (auto_gain) {
19953cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, false);
199619a81c14SSteve Longerbeam 		if (ret)
199719a81c14SSteve Longerbeam 			return ret;
19983cca8ef5SHugues Fruchet 	}
1999bf4a4b51SMaxime Ripard 
20003cca8ef5SHugues Fruchet 	if (auto_exp) {
2001dc29a1c1SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, false);
200219a81c14SSteve Longerbeam 		if (ret)
20033cca8ef5SHugues Fruchet 			goto restore_auto_gain;
20043cca8ef5SHugues Fruchet 	}
200519a81c14SSteve Longerbeam 
2006aa288248SMaxime Ripard 	/*
2007aa288248SMaxime Ripard 	 * All the formats we support have 16 bits per pixel, seems to require
2008aa288248SMaxime Ripard 	 * the same rate than YUV, so we can just use 16 bpp all the time.
2009aa288248SMaxime Ripard 	 */
2010aa288248SMaxime Ripard 	rate = mode->pixel_clock * 16;
2011aa288248SMaxime Ripard 	if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
2012aa288248SMaxime Ripard 		rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
2013aa288248SMaxime Ripard 		ret = ov5640_set_mipi_pclk(sensor, rate);
2014aa288248SMaxime Ripard 	} else {
2015aa288248SMaxime Ripard 		rate = rate / sensor->ep.bus.parallel.bus_width;
2016aa288248SMaxime Ripard 		ret = ov5640_set_dvp_pclk(sensor, rate);
2017aa288248SMaxime Ripard 	}
2018aa288248SMaxime Ripard 
2019aa288248SMaxime Ripard 	if (ret < 0)
2020aa288248SMaxime Ripard 		return 0;
2021aa288248SMaxime Ripard 
202219a81c14SSteve Longerbeam 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
202319a81c14SSteve Longerbeam 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
202419a81c14SSteve Longerbeam 		/*
202519a81c14SSteve Longerbeam 		 * change between subsampling and scaling
20263cca8ef5SHugues Fruchet 		 * go through exposure calculation
202719a81c14SSteve Longerbeam 		 */
202819a81c14SSteve Longerbeam 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
202919a81c14SSteve Longerbeam 	} else {
203019a81c14SSteve Longerbeam 		/*
203119a81c14SSteve Longerbeam 		 * change inside subsampling or scaling
203219a81c14SSteve Longerbeam 		 * download firmware directly
203319a81c14SSteve Longerbeam 		 */
20343cca8ef5SHugues Fruchet 		ret = ov5640_set_mode_direct(sensor, mode);
203519a81c14SSteve Longerbeam 	}
203619a81c14SSteve Longerbeam 	if (ret < 0)
20373cca8ef5SHugues Fruchet 		goto restore_auto_exp_gain;
20383cca8ef5SHugues Fruchet 
20393cca8ef5SHugues Fruchet 	/* restore auto gain and exposure */
20403cca8ef5SHugues Fruchet 	if (auto_gain)
20413cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
20423cca8ef5SHugues Fruchet 	if (auto_exp)
20433cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
204419a81c14SSteve Longerbeam 
2045ce85705aSHugues Fruchet 	ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2046ce85705aSHugues Fruchet 	if (ret < 0)
2047ce85705aSHugues Fruchet 		return ret;
204819a81c14SSteve Longerbeam 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
204919a81c14SSteve Longerbeam 	if (ret < 0)
205019a81c14SSteve Longerbeam 		return ret;
205119a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
205219a81c14SSteve Longerbeam 	if (ret < 0)
205319a81c14SSteve Longerbeam 		return ret;
205419a81c14SSteve Longerbeam 	ret = ov5640_set_bandingfilter(sensor);
205519a81c14SSteve Longerbeam 	if (ret < 0)
205619a81c14SSteve Longerbeam 		return ret;
205719a81c14SSteve Longerbeam 	ret = ov5640_set_virtual_channel(sensor);
205819a81c14SSteve Longerbeam 	if (ret < 0)
205919a81c14SSteve Longerbeam 		return ret;
206019a81c14SSteve Longerbeam 
206119a81c14SSteve Longerbeam 	sensor->pending_mode_change = false;
2062985cdcb0SHugues Fruchet 	sensor->last_mode = mode;
206319a81c14SSteve Longerbeam 
206419a81c14SSteve Longerbeam 	return 0;
20653cca8ef5SHugues Fruchet 
20663cca8ef5SHugues Fruchet restore_auto_exp_gain:
20673cca8ef5SHugues Fruchet 	if (auto_exp)
20683cca8ef5SHugues Fruchet 		ov5640_set_autoexposure(sensor, true);
20693cca8ef5SHugues Fruchet restore_auto_gain:
20703cca8ef5SHugues Fruchet 	if (auto_gain)
20713cca8ef5SHugues Fruchet 		ov5640_set_autogain(sensor, true);
20723cca8ef5SHugues Fruchet 
20733cca8ef5SHugues Fruchet 	return ret;
207419a81c14SSteve Longerbeam }
207519a81c14SSteve Longerbeam 
207619ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor,
207719ad26f9SAkinobu Mita 			       struct v4l2_mbus_framefmt *format);
207819ad26f9SAkinobu Mita 
207919a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */
208019a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor)
208119a81c14SSteve Longerbeam {
208219a81c14SSteve Longerbeam 	int ret;
208319a81c14SSteve Longerbeam 
208419a81c14SSteve Longerbeam 	/* first load the initial register values */
208519a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
208619a81c14SSteve Longerbeam 	if (ret < 0)
208719a81c14SSteve Longerbeam 		return ret;
2088985cdcb0SHugues Fruchet 	sensor->last_mode = &ov5640_mode_init_data;
208919a81c14SSteve Longerbeam 
20908f57c2f8SMaxime Ripard 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
20918f57c2f8SMaxime Ripard 			     (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) |
20928f57c2f8SMaxime Ripard 			     ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT));
20938f57c2f8SMaxime Ripard 	if (ret)
20948f57c2f8SMaxime Ripard 		return ret;
20958f57c2f8SMaxime Ripard 
209619a81c14SSteve Longerbeam 	/* now restore the last capture mode */
2097985cdcb0SHugues Fruchet 	ret = ov5640_set_mode(sensor);
209819ad26f9SAkinobu Mita 	if (ret < 0)
209919ad26f9SAkinobu Mita 		return ret;
210019ad26f9SAkinobu Mita 
210119ad26f9SAkinobu Mita 	return ov5640_set_framefmt(sensor, &sensor->fmt);
210219a81c14SSteve Longerbeam }
210319a81c14SSteve Longerbeam 
210419a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable)
210519a81c14SSteve Longerbeam {
21061fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
210719a81c14SSteve Longerbeam }
210819a81c14SSteve Longerbeam 
210919a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor)
211019a81c14SSteve Longerbeam {
211119a81c14SSteve Longerbeam 	if (!sensor->reset_gpio)
211219a81c14SSteve Longerbeam 		return;
211319a81c14SSteve Longerbeam 
21141fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
211519a81c14SSteve Longerbeam 
211619a81c14SSteve Longerbeam 	/* camera power cycle */
211719a81c14SSteve Longerbeam 	ov5640_power(sensor, false);
211819a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
211919a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
212019a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
212119a81c14SSteve Longerbeam 
21221fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
212319a81c14SSteve Longerbeam 	usleep_range(1000, 2000);
212419a81c14SSteve Longerbeam 
21251fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
212619a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
212719a81c14SSteve Longerbeam }
212819a81c14SSteve Longerbeam 
21290f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor)
213019a81c14SSteve Longerbeam {
21310f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
21320f7acb52SHugues Fruchet 	int ret;
213319a81c14SSteve Longerbeam 
21340f7acb52SHugues Fruchet 	ret = clk_prepare_enable(sensor->xclk);
21350f7acb52SHugues Fruchet 	if (ret) {
21360f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable clock\n",
21370f7acb52SHugues Fruchet 			__func__);
21380f7acb52SHugues Fruchet 		return ret;
21390f7acb52SHugues Fruchet 	}
214019a81c14SSteve Longerbeam 
214119a81c14SSteve Longerbeam 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
214219a81c14SSteve Longerbeam 				    sensor->supplies);
21430f7acb52SHugues Fruchet 	if (ret) {
21440f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable regulators\n",
21450f7acb52SHugues Fruchet 			__func__);
214619a81c14SSteve Longerbeam 		goto xclk_off;
21470f7acb52SHugues Fruchet 	}
214819a81c14SSteve Longerbeam 
214919a81c14SSteve Longerbeam 	ov5640_reset(sensor);
215019a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
215119a81c14SSteve Longerbeam 
215219a81c14SSteve Longerbeam 	ret = ov5640_init_slave_id(sensor);
215319a81c14SSteve Longerbeam 	if (ret)
215419a81c14SSteve Longerbeam 		goto power_off;
215519a81c14SSteve Longerbeam 
21560f7acb52SHugues Fruchet 	return 0;
21570f7acb52SHugues Fruchet 
21580f7acb52SHugues Fruchet power_off:
21590f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
21600f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
21610f7acb52SHugues Fruchet xclk_off:
21620f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
21630f7acb52SHugues Fruchet 	return ret;
21640f7acb52SHugues Fruchet }
21650f7acb52SHugues Fruchet 
21660f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor)
21670f7acb52SHugues Fruchet {
21680f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
21690f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
21700f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
21710f7acb52SHugues Fruchet }
21720f7acb52SHugues Fruchet 
21730f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
21740f7acb52SHugues Fruchet {
21750f7acb52SHugues Fruchet 	int ret = 0;
21760f7acb52SHugues Fruchet 
21770f7acb52SHugues Fruchet 	if (on) {
21780f7acb52SHugues Fruchet 		ret = ov5640_set_power_on(sensor);
21790f7acb52SHugues Fruchet 		if (ret)
21800f7acb52SHugues Fruchet 			return ret;
21810f7acb52SHugues Fruchet 
218219a81c14SSteve Longerbeam 		ret = ov5640_restore_mode(sensor);
218319a81c14SSteve Longerbeam 		if (ret)
218419a81c14SSteve Longerbeam 			goto power_off;
218519a81c14SSteve Longerbeam 
2186aa4bb8b8SJacopo Mondi 		/* We're done here for DVP bus, while CSI-2 needs setup. */
21872d95e7edSSakari Ailus 		if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
2188aa4bb8b8SJacopo Mondi 			return 0;
2189aa4bb8b8SJacopo Mondi 
219019a81c14SSteve Longerbeam 		/*
2191aa4bb8b8SJacopo Mondi 		 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2192aa4bb8b8SJacopo Mondi 		 *
2193aa4bb8b8SJacopo Mondi 		 * 0x300e = 0x40
2194aa4bb8b8SJacopo Mondi 		 * [7:5] = 010	: 2 data lanes mode (see FIXME note in
2195aa4bb8b8SJacopo Mondi 		 *		  "ov5640_set_stream_mipi()")
2196aa4bb8b8SJacopo Mondi 		 * [4] = 0	: Power up MIPI HS Tx
2197aa4bb8b8SJacopo Mondi 		 * [3] = 0	: Power up MIPI LS Rx
2198aa4bb8b8SJacopo Mondi 		 * [2] = 0	: MIPI interface disabled
219919a81c14SSteve Longerbeam 		 */
2200aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
2201aa4bb8b8SJacopo Mondi 				       OV5640_REG_IO_MIPI_CTRL00, 0x40);
220219a81c14SSteve Longerbeam 		if (ret)
220319a81c14SSteve Longerbeam 			goto power_off;
2204aa4bb8b8SJacopo Mondi 
2205aa4bb8b8SJacopo Mondi 		/*
2206aa4bb8b8SJacopo Mondi 		 * Gate clock and set LP11 in 'no packets mode' (idle)
2207aa4bb8b8SJacopo Mondi 		 *
2208aa4bb8b8SJacopo Mondi 		 * 0x4800 = 0x24
2209aa4bb8b8SJacopo Mondi 		 * [5] = 1	: Gate clock when 'no packets'
2210aa4bb8b8SJacopo Mondi 		 * [2] = 1	: MIPI bus in LP11 when 'no packets'
2211aa4bb8b8SJacopo Mondi 		 */
2212aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
2213aa4bb8b8SJacopo Mondi 				       OV5640_REG_MIPI_CTRL00, 0x24);
221419a81c14SSteve Longerbeam 		if (ret)
221519a81c14SSteve Longerbeam 			goto power_off;
2216aa4bb8b8SJacopo Mondi 
2217aa4bb8b8SJacopo Mondi 		/*
2218aa4bb8b8SJacopo Mondi 		 * Set data lanes and clock in LP11 when 'sleeping'
2219aa4bb8b8SJacopo Mondi 		 *
2220aa4bb8b8SJacopo Mondi 		 * 0x3019 = 0x70
2221aa4bb8b8SJacopo Mondi 		 * [6] = 1	: MIPI data lane 2 in LP11 when 'sleeping'
2222aa4bb8b8SJacopo Mondi 		 * [5] = 1	: MIPI data lane 1 in LP11 when 'sleeping'
2223aa4bb8b8SJacopo Mondi 		 * [4] = 1	: MIPI clock lane in LP11 when 'sleeping'
2224aa4bb8b8SJacopo Mondi 		 */
2225aa4bb8b8SJacopo Mondi 		ret = ov5640_write_reg(sensor,
2226aa4bb8b8SJacopo Mondi 				       OV5640_REG_PAD_OUTPUT00, 0x70);
2227aa4bb8b8SJacopo Mondi 		if (ret)
2228aa4bb8b8SJacopo Mondi 			goto power_off;
2229aa4bb8b8SJacopo Mondi 
2230aa4bb8b8SJacopo Mondi 		/* Give lanes some time to coax into LP11 state. */
2231aa4bb8b8SJacopo Mondi 		usleep_range(500, 1000);
2232aa4bb8b8SJacopo Mondi 
2233aa4bb8b8SJacopo Mondi 	} else {
22342d95e7edSSakari Ailus 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
2235aa4bb8b8SJacopo Mondi 			/* Reset MIPI bus settings to their default values. */
2236aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
2237aa4bb8b8SJacopo Mondi 					 OV5640_REG_IO_MIPI_CTRL00, 0x58);
2238aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
2239aa4bb8b8SJacopo Mondi 					 OV5640_REG_MIPI_CTRL00, 0x04);
2240aa4bb8b8SJacopo Mondi 			ov5640_write_reg(sensor,
2241aa4bb8b8SJacopo Mondi 					 OV5640_REG_PAD_OUTPUT00, 0x00);
2242aa4bb8b8SJacopo Mondi 		}
2243aa4bb8b8SJacopo Mondi 
2244aa4bb8b8SJacopo Mondi 		ov5640_set_power_off(sensor);
2245f22996dbSHugues Fruchet 	}
224619a81c14SSteve Longerbeam 
224719a81c14SSteve Longerbeam 	return 0;
224819a81c14SSteve Longerbeam 
224919a81c14SSteve Longerbeam power_off:
22500f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
225119a81c14SSteve Longerbeam 	return ret;
225219a81c14SSteve Longerbeam }
225319a81c14SSteve Longerbeam 
225419a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */
225519a81c14SSteve Longerbeam 
225619a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on)
225719a81c14SSteve Longerbeam {
225819a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
225919a81c14SSteve Longerbeam 	int ret = 0;
226019a81c14SSteve Longerbeam 
226119a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
226219a81c14SSteve Longerbeam 
226319a81c14SSteve Longerbeam 	/*
226419a81c14SSteve Longerbeam 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
226519a81c14SSteve Longerbeam 	 * update the power state.
226619a81c14SSteve Longerbeam 	 */
226719a81c14SSteve Longerbeam 	if (sensor->power_count == !on) {
226819a81c14SSteve Longerbeam 		ret = ov5640_set_power(sensor, !!on);
226919a81c14SSteve Longerbeam 		if (ret)
227019a81c14SSteve Longerbeam 			goto out;
227119a81c14SSteve Longerbeam 	}
227219a81c14SSteve Longerbeam 
227319a81c14SSteve Longerbeam 	/* Update the power count. */
227419a81c14SSteve Longerbeam 	sensor->power_count += on ? 1 : -1;
227519a81c14SSteve Longerbeam 	WARN_ON(sensor->power_count < 0);
227619a81c14SSteve Longerbeam out:
227719a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
227819a81c14SSteve Longerbeam 
227919a81c14SSteve Longerbeam 	if (on && !ret && sensor->power_count == 1) {
228019a81c14SSteve Longerbeam 		/* restore controls */
228119a81c14SSteve Longerbeam 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
228219a81c14SSteve Longerbeam 	}
228319a81c14SSteve Longerbeam 
228419a81c14SSteve Longerbeam 	return ret;
228519a81c14SSteve Longerbeam }
228619a81c14SSteve Longerbeam 
228719a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
228819a81c14SSteve Longerbeam 				     struct v4l2_fract *fi,
228919a81c14SSteve Longerbeam 				     u32 width, u32 height)
229019a81c14SSteve Longerbeam {
229119a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
229219a81c14SSteve Longerbeam 	u32 minfps, maxfps, fps;
229319a81c14SSteve Longerbeam 	int ret;
229419a81c14SSteve Longerbeam 
229519a81c14SSteve Longerbeam 	minfps = ov5640_framerates[OV5640_15_FPS];
229619a81c14SSteve Longerbeam 	maxfps = ov5640_framerates[OV5640_30_FPS];
229719a81c14SSteve Longerbeam 
229819a81c14SSteve Longerbeam 	if (fi->numerator == 0) {
229919a81c14SSteve Longerbeam 		fi->denominator = maxfps;
230019a81c14SSteve Longerbeam 		fi->numerator = 1;
230119a81c14SSteve Longerbeam 		return OV5640_30_FPS;
230219a81c14SSteve Longerbeam 	}
230319a81c14SSteve Longerbeam 
230419a81c14SSteve Longerbeam 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
230519a81c14SSteve Longerbeam 
230619a81c14SSteve Longerbeam 	fi->numerator = 1;
230719a81c14SSteve Longerbeam 	if (fps > maxfps)
230819a81c14SSteve Longerbeam 		fi->denominator = maxfps;
230919a81c14SSteve Longerbeam 	else if (fps < minfps)
231019a81c14SSteve Longerbeam 		fi->denominator = minfps;
231119a81c14SSteve Longerbeam 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
231219a81c14SSteve Longerbeam 		fi->denominator = maxfps;
231319a81c14SSteve Longerbeam 	else
231419a81c14SSteve Longerbeam 		fi->denominator = minfps;
231519a81c14SSteve Longerbeam 
231619a81c14SSteve Longerbeam 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
231719a81c14SSteve Longerbeam 
231819a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, ret, width, height, false);
231919a81c14SSteve Longerbeam 	return mode ? ret : -EINVAL;
232019a81c14SSteve Longerbeam }
232119a81c14SSteve Longerbeam 
232219a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd,
232319a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
232419a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
232519a81c14SSteve Longerbeam {
232619a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
232719a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt *fmt;
232819a81c14SSteve Longerbeam 
232919a81c14SSteve Longerbeam 	if (format->pad != 0)
233019a81c14SSteve Longerbeam 		return -EINVAL;
233119a81c14SSteve Longerbeam 
233219a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
233319a81c14SSteve Longerbeam 
233419a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
233519a81c14SSteve Longerbeam 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
233619a81c14SSteve Longerbeam 						 format->pad);
233719a81c14SSteve Longerbeam 	else
233819a81c14SSteve Longerbeam 		fmt = &sensor->fmt;
233919a81c14SSteve Longerbeam 
234019a81c14SSteve Longerbeam 	format->format = *fmt;
234119a81c14SSteve Longerbeam 
234219a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
234319a81c14SSteve Longerbeam 
234419a81c14SSteve Longerbeam 	return 0;
234519a81c14SSteve Longerbeam }
234619a81c14SSteve Longerbeam 
234719a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
234819a81c14SSteve Longerbeam 				   struct v4l2_mbus_framefmt *fmt,
234919a81c14SSteve Longerbeam 				   enum ov5640_frame_rate fr,
235019a81c14SSteve Longerbeam 				   const struct ov5640_mode_info **new_mode)
235119a81c14SSteve Longerbeam {
235219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
235319a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
2354e3ee691dSHugues Fruchet 	int i;
235519a81c14SSteve Longerbeam 
235619a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
235719a81c14SSteve Longerbeam 	if (!mode)
235819a81c14SSteve Longerbeam 		return -EINVAL;
2359dba13a0bSMaxime Ripard 	fmt->width = mode->hact;
2360dba13a0bSMaxime Ripard 	fmt->height = mode->vact;
236119a81c14SSteve Longerbeam 
236219a81c14SSteve Longerbeam 	if (new_mode)
236319a81c14SSteve Longerbeam 		*new_mode = mode;
2364e3ee691dSHugues Fruchet 
2365e3ee691dSHugues Fruchet 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2366e3ee691dSHugues Fruchet 		if (ov5640_formats[i].code == fmt->code)
2367e3ee691dSHugues Fruchet 			break;
2368e3ee691dSHugues Fruchet 	if (i >= ARRAY_SIZE(ov5640_formats))
2369e6441fdeSHugues Fruchet 		i = 0;
2370e6441fdeSHugues Fruchet 
2371e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[i].code;
2372e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[i].colorspace;
2373e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2374e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2375e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2376e3ee691dSHugues Fruchet 
237719a81c14SSteve Longerbeam 	return 0;
237819a81c14SSteve Longerbeam }
237919a81c14SSteve Longerbeam 
238019a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd,
238119a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
238219a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
238319a81c14SSteve Longerbeam {
238419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
238519a81c14SSteve Longerbeam 	const struct ov5640_mode_info *new_mode;
2386e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
238707115449SJacopo Mondi 	struct v4l2_mbus_framefmt *fmt;
238819a81c14SSteve Longerbeam 	int ret;
238919a81c14SSteve Longerbeam 
239019a81c14SSteve Longerbeam 	if (format->pad != 0)
239119a81c14SSteve Longerbeam 		return -EINVAL;
239219a81c14SSteve Longerbeam 
239319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
239419a81c14SSteve Longerbeam 
239519a81c14SSteve Longerbeam 	if (sensor->streaming) {
239619a81c14SSteve Longerbeam 		ret = -EBUSY;
239719a81c14SSteve Longerbeam 		goto out;
239819a81c14SSteve Longerbeam 	}
239919a81c14SSteve Longerbeam 
2400e6441fdeSHugues Fruchet 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
240119a81c14SSteve Longerbeam 				      sensor->current_fr, &new_mode);
240219a81c14SSteve Longerbeam 	if (ret)
240319a81c14SSteve Longerbeam 		goto out;
240419a81c14SSteve Longerbeam 
240507115449SJacopo Mondi 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
240607115449SJacopo Mondi 		fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
240707115449SJacopo Mondi 	else
240807115449SJacopo Mondi 		fmt = &sensor->fmt;
240919a81c14SSteve Longerbeam 
2410e6441fdeSHugues Fruchet 	*fmt = *mbus_fmt;
241119a81c14SSteve Longerbeam 
24126949d864SHugues Fruchet 	if (new_mode != sensor->current_mode) {
241319a81c14SSteve Longerbeam 		sensor->current_mode = new_mode;
241419a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
24156949d864SHugues Fruchet 	}
241607115449SJacopo Mondi 	if (mbus_fmt->code != sensor->fmt.code)
2417fb98e29fSHugues Fruchet 		sensor->pending_fmt_change = true;
241807115449SJacopo Mondi 
241919a81c14SSteve Longerbeam out:
242019a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
242119a81c14SSteve Longerbeam 	return ret;
242219a81c14SSteve Longerbeam }
242319a81c14SSteve Longerbeam 
2424e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2425e3ee691dSHugues Fruchet 			       struct v4l2_mbus_framefmt *format)
2426e3ee691dSHugues Fruchet {
2427e3ee691dSHugues Fruchet 	int ret = 0;
2428e3ee691dSHugues Fruchet 	bool is_rgb = false;
2429d47c4126SHugues Fruchet 	bool is_jpeg = false;
2430e3ee691dSHugues Fruchet 	u8 val;
2431e3ee691dSHugues Fruchet 
2432e3ee691dSHugues Fruchet 	switch (format->code) {
2433e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_UYVY8_2X8:
2434e3ee691dSHugues Fruchet 		/* YUV422, UYVY */
2435e3ee691dSHugues Fruchet 		val = 0x3f;
2436e3ee691dSHugues Fruchet 		break;
2437e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_YUYV8_2X8:
2438e3ee691dSHugues Fruchet 		/* YUV422, YUYV */
2439e3ee691dSHugues Fruchet 		val = 0x30;
2440e3ee691dSHugues Fruchet 		break;
2441e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
2442e3ee691dSHugues Fruchet 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2443e3ee691dSHugues Fruchet 		val = 0x6F;
2444e3ee691dSHugues Fruchet 		is_rgb = true;
2445e3ee691dSHugues Fruchet 		break;
2446e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
2447e3ee691dSHugues Fruchet 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2448e3ee691dSHugues Fruchet 		val = 0x61;
2449e3ee691dSHugues Fruchet 		is_rgb = true;
2450e3ee691dSHugues Fruchet 		break;
2451d47c4126SHugues Fruchet 	case MEDIA_BUS_FMT_JPEG_1X8:
2452d47c4126SHugues Fruchet 		/* YUV422, YUYV */
2453d47c4126SHugues Fruchet 		val = 0x30;
2454d47c4126SHugues Fruchet 		is_jpeg = true;
2455d47c4126SHugues Fruchet 		break;
2456e3ee691dSHugues Fruchet 	default:
2457e3ee691dSHugues Fruchet 		return -EINVAL;
2458e3ee691dSHugues Fruchet 	}
2459e3ee691dSHugues Fruchet 
2460e3ee691dSHugues Fruchet 	/* FORMAT CONTROL00: YUV and RGB formatting */
2461e3ee691dSHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
2462e3ee691dSHugues Fruchet 	if (ret)
2463e3ee691dSHugues Fruchet 		return ret;
2464e3ee691dSHugues Fruchet 
2465e3ee691dSHugues Fruchet 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
2466d47c4126SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
2467e3ee691dSHugues Fruchet 			       is_rgb ? 0x01 : 0x00);
2468d47c4126SHugues Fruchet 	if (ret)
2469d47c4126SHugues Fruchet 		return ret;
2470d47c4126SHugues Fruchet 
2471d47c4126SHugues Fruchet 	/*
2472d47c4126SHugues Fruchet 	 * TIMING TC REG21:
2473d47c4126SHugues Fruchet 	 * - [5]:	JPEG enable
2474d47c4126SHugues Fruchet 	 */
2475d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2476d47c4126SHugues Fruchet 			     BIT(5), is_jpeg ? BIT(5) : 0);
2477d47c4126SHugues Fruchet 	if (ret)
2478d47c4126SHugues Fruchet 		return ret;
2479d47c4126SHugues Fruchet 
2480d47c4126SHugues Fruchet 	/*
2481d47c4126SHugues Fruchet 	 * SYSTEM RESET02:
2482d47c4126SHugues Fruchet 	 * - [4]:	Reset JFIFO
2483d47c4126SHugues Fruchet 	 * - [3]:	Reset SFIFO
2484d47c4126SHugues Fruchet 	 * - [2]:	Reset JPEG
2485d47c4126SHugues Fruchet 	 */
2486d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2487d47c4126SHugues Fruchet 			     BIT(4) | BIT(3) | BIT(2),
2488d47c4126SHugues Fruchet 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2489d47c4126SHugues Fruchet 	if (ret)
2490d47c4126SHugues Fruchet 		return ret;
2491d47c4126SHugues Fruchet 
2492d47c4126SHugues Fruchet 	/*
2493d47c4126SHugues Fruchet 	 * CLOCK ENABLE02:
2494d47c4126SHugues Fruchet 	 * - [5]:	Enable JPEG 2x clock
2495d47c4126SHugues Fruchet 	 * - [3]:	Enable JPEG clock
2496d47c4126SHugues Fruchet 	 */
2497d47c4126SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2498d47c4126SHugues Fruchet 			      BIT(5) | BIT(3),
2499d47c4126SHugues Fruchet 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
2500e3ee691dSHugues Fruchet }
250119a81c14SSteve Longerbeam 
250219a81c14SSteve Longerbeam /*
250319a81c14SSteve Longerbeam  * Sensor Controls.
250419a81c14SSteve Longerbeam  */
250519a81c14SSteve Longerbeam 
250619a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
250719a81c14SSteve Longerbeam {
250819a81c14SSteve Longerbeam 	int ret;
250919a81c14SSteve Longerbeam 
251019a81c14SSteve Longerbeam 	if (value) {
251119a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
251219a81c14SSteve Longerbeam 				     BIT(0), BIT(0));
251319a81c14SSteve Longerbeam 		if (ret)
251419a81c14SSteve Longerbeam 			return ret;
251519a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
251619a81c14SSteve Longerbeam 	} else {
251719a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
251819a81c14SSteve Longerbeam 	}
251919a81c14SSteve Longerbeam 
252019a81c14SSteve Longerbeam 	return ret;
252119a81c14SSteve Longerbeam }
252219a81c14SSteve Longerbeam 
252319a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
252419a81c14SSteve Longerbeam {
252519a81c14SSteve Longerbeam 	int ret;
252619a81c14SSteve Longerbeam 
252719a81c14SSteve Longerbeam 	if (value) {
252819a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
252919a81c14SSteve Longerbeam 				     BIT(2), BIT(2));
253019a81c14SSteve Longerbeam 		if (ret)
253119a81c14SSteve Longerbeam 			return ret;
253219a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
253319a81c14SSteve Longerbeam 				       value & 0xff);
253419a81c14SSteve Longerbeam 	} else {
253519a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
253619a81c14SSteve Longerbeam 	}
253719a81c14SSteve Longerbeam 
253819a81c14SSteve Longerbeam 	return ret;
253919a81c14SSteve Longerbeam }
254019a81c14SSteve Longerbeam 
254119a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
254219a81c14SSteve Longerbeam {
254319a81c14SSteve Longerbeam 	int ret;
254419a81c14SSteve Longerbeam 
254519a81c14SSteve Longerbeam 	if (value) {
254619a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
254719a81c14SSteve Longerbeam 				     BIT(1), BIT(1));
254819a81c14SSteve Longerbeam 		if (ret)
254919a81c14SSteve Longerbeam 			return ret;
255019a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
255119a81c14SSteve Longerbeam 				       value & 0xff);
255219a81c14SSteve Longerbeam 		if (ret)
255319a81c14SSteve Longerbeam 			return ret;
255419a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
255519a81c14SSteve Longerbeam 				       value & 0xff);
255619a81c14SSteve Longerbeam 	} else {
255719a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
255819a81c14SSteve Longerbeam 	}
255919a81c14SSteve Longerbeam 
256019a81c14SSteve Longerbeam 	return ret;
256119a81c14SSteve Longerbeam }
256219a81c14SSteve Longerbeam 
256319a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
256419a81c14SSteve Longerbeam {
256519a81c14SSteve Longerbeam 	int ret;
256619a81c14SSteve Longerbeam 
256719a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
256819a81c14SSteve Longerbeam 			     BIT(0), awb ? 0 : 1);
256919a81c14SSteve Longerbeam 	if (ret)
257019a81c14SSteve Longerbeam 		return ret;
257119a81c14SSteve Longerbeam 
257219a81c14SSteve Longerbeam 	if (!awb) {
257319a81c14SSteve Longerbeam 		u16 red = (u16)sensor->ctrls.red_balance->val;
257419a81c14SSteve Longerbeam 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
257519a81c14SSteve Longerbeam 
257619a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
257719a81c14SSteve Longerbeam 		if (ret)
257819a81c14SSteve Longerbeam 			return ret;
257919a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
258019a81c14SSteve Longerbeam 	}
258119a81c14SSteve Longerbeam 
258219a81c14SSteve Longerbeam 	return ret;
258319a81c14SSteve Longerbeam }
258419a81c14SSteve Longerbeam 
25853cca8ef5SHugues Fruchet static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
25863cca8ef5SHugues Fruchet 				    enum v4l2_exposure_auto_type auto_exposure)
258719a81c14SSteve Longerbeam {
258819a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
25893cca8ef5SHugues Fruchet 	bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
259019a81c14SSteve Longerbeam 	int ret = 0;
259119a81c14SSteve Longerbeam 
259219a81c14SSteve Longerbeam 	if (ctrls->auto_exp->is_new) {
25933cca8ef5SHugues Fruchet 		ret = ov5640_set_autoexposure(sensor, auto_exp);
259419a81c14SSteve Longerbeam 		if (ret)
259519a81c14SSteve Longerbeam 			return ret;
259619a81c14SSteve Longerbeam 	}
259719a81c14SSteve Longerbeam 
25983cca8ef5SHugues Fruchet 	if (!auto_exp && ctrls->exposure->is_new) {
259919a81c14SSteve Longerbeam 		u16 max_exp;
260019a81c14SSteve Longerbeam 
260119a81c14SSteve Longerbeam 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
260219a81c14SSteve Longerbeam 					&max_exp);
260319a81c14SSteve Longerbeam 		if (ret)
260419a81c14SSteve Longerbeam 			return ret;
260519a81c14SSteve Longerbeam 		ret = ov5640_get_vts(sensor);
260619a81c14SSteve Longerbeam 		if (ret < 0)
260719a81c14SSteve Longerbeam 			return ret;
260819a81c14SSteve Longerbeam 		max_exp += ret;
26096146fde3SHugues Fruchet 		ret = 0;
261019a81c14SSteve Longerbeam 
261119a81c14SSteve Longerbeam 		if (ctrls->exposure->val < max_exp)
261219a81c14SSteve Longerbeam 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
261319a81c14SSteve Longerbeam 	}
261419a81c14SSteve Longerbeam 
261519a81c14SSteve Longerbeam 	return ret;
261619a81c14SSteve Longerbeam }
261719a81c14SSteve Longerbeam 
26183cca8ef5SHugues Fruchet static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
261919a81c14SSteve Longerbeam {
262019a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
262119a81c14SSteve Longerbeam 	int ret = 0;
262219a81c14SSteve Longerbeam 
262319a81c14SSteve Longerbeam 	if (ctrls->auto_gain->is_new) {
26243cca8ef5SHugues Fruchet 		ret = ov5640_set_autogain(sensor, auto_gain);
262519a81c14SSteve Longerbeam 		if (ret)
262619a81c14SSteve Longerbeam 			return ret;
262719a81c14SSteve Longerbeam 	}
262819a81c14SSteve Longerbeam 
26293cca8ef5SHugues Fruchet 	if (!auto_gain && ctrls->gain->is_new)
26303cca8ef5SHugues Fruchet 		ret = ov5640_set_gain(sensor, ctrls->gain->val);
263119a81c14SSteve Longerbeam 
263219a81c14SSteve Longerbeam 	return ret;
263319a81c14SSteve Longerbeam }
263419a81c14SSteve Longerbeam 
263519a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
263619a81c14SSteve Longerbeam {
263719a81c14SSteve Longerbeam 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
263819a81c14SSteve Longerbeam 			      0xa4, value ? 0xa4 : 0);
263919a81c14SSteve Longerbeam }
264019a81c14SSteve Longerbeam 
26411068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
26421068fecaSMylène Josserand {
26431068fecaSMylène Josserand 	int ret;
26441068fecaSMylène Josserand 
26451068fecaSMylène Josserand 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
26461068fecaSMylène Josserand 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
26471068fecaSMylène Josserand 			     0 : BIT(7));
26481068fecaSMylène Josserand 	if (ret)
26491068fecaSMylène Josserand 		return ret;
26501068fecaSMylène Josserand 
26511068fecaSMylène Josserand 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
26521068fecaSMylène Josserand 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
26531068fecaSMylène Josserand 			      BIT(2) : 0);
26541068fecaSMylène Josserand }
26551068fecaSMylène Josserand 
2656ce85705aSHugues Fruchet static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2657ce85705aSHugues Fruchet {
2658ce85705aSHugues Fruchet 	/*
2659c3f3ba3eSHugues Fruchet 	 * If sensor is mounted upside down, mirror logic is inversed.
2660c3f3ba3eSHugues Fruchet 	 *
2661ce85705aSHugues Fruchet 	 * Sensor is a BSI (Back Side Illuminated) one,
2662ce85705aSHugues Fruchet 	 * so image captured is physically mirrored.
2663ce85705aSHugues Fruchet 	 * This is why mirror logic is inversed in
2664ce85705aSHugues Fruchet 	 * order to cancel this mirror effect.
2665ce85705aSHugues Fruchet 	 */
2666ce85705aSHugues Fruchet 
2667ce85705aSHugues Fruchet 	/*
2668ce85705aSHugues Fruchet 	 * TIMING TC REG21:
2669ce85705aSHugues Fruchet 	 * - [2]:	ISP mirror
2670ce85705aSHugues Fruchet 	 * - [1]:	Sensor mirror
2671ce85705aSHugues Fruchet 	 */
2672ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2673ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2674c3f3ba3eSHugues Fruchet 			      (!(value ^ sensor->upside_down)) ?
2675c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2676ce85705aSHugues Fruchet }
2677ce85705aSHugues Fruchet 
2678ce85705aSHugues Fruchet static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2679ce85705aSHugues Fruchet {
2680c3f3ba3eSHugues Fruchet 	/* If sensor is mounted upside down, flip logic is inversed */
2681c3f3ba3eSHugues Fruchet 
2682ce85705aSHugues Fruchet 	/*
2683ce85705aSHugues Fruchet 	 * TIMING TC REG20:
2684ce85705aSHugues Fruchet 	 * - [2]:	ISP vflip
2685ce85705aSHugues Fruchet 	 * - [1]:	Sensor vflip
2686ce85705aSHugues Fruchet 	 */
2687ce85705aSHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2688ce85705aSHugues Fruchet 			      BIT(2) | BIT(1),
2689c3f3ba3eSHugues Fruchet 			      (value ^ sensor->upside_down) ?
2690c3f3ba3eSHugues Fruchet 			      (BIT(2) | BIT(1)) : 0);
2691ce85705aSHugues Fruchet }
2692ce85705aSHugues Fruchet 
269319a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
269419a81c14SSteve Longerbeam {
269519a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
269619a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
269719a81c14SSteve Longerbeam 	int val;
269819a81c14SSteve Longerbeam 
269919a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
270019a81c14SSteve Longerbeam 
270119a81c14SSteve Longerbeam 	switch (ctrl->id) {
270219a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
270319a81c14SSteve Longerbeam 		val = ov5640_get_gain(sensor);
270419a81c14SSteve Longerbeam 		if (val < 0)
270519a81c14SSteve Longerbeam 			return val;
270619a81c14SSteve Longerbeam 		sensor->ctrls.gain->val = val;
270719a81c14SSteve Longerbeam 		break;
270819a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
270919a81c14SSteve Longerbeam 		val = ov5640_get_exposure(sensor);
271019a81c14SSteve Longerbeam 		if (val < 0)
271119a81c14SSteve Longerbeam 			return val;
271219a81c14SSteve Longerbeam 		sensor->ctrls.exposure->val = val;
271319a81c14SSteve Longerbeam 		break;
271419a81c14SSteve Longerbeam 	}
271519a81c14SSteve Longerbeam 
271619a81c14SSteve Longerbeam 	return 0;
271719a81c14SSteve Longerbeam }
271819a81c14SSteve Longerbeam 
271919a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
272019a81c14SSteve Longerbeam {
272119a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
272219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
272319a81c14SSteve Longerbeam 	int ret;
272419a81c14SSteve Longerbeam 
272519a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
272619a81c14SSteve Longerbeam 
272719a81c14SSteve Longerbeam 	/*
272819a81c14SSteve Longerbeam 	 * If the device is not powered up by the host driver do
272919a81c14SSteve Longerbeam 	 * not apply any controls to H/W at this time. Instead
273019a81c14SSteve Longerbeam 	 * the controls will be restored right after power-up.
273119a81c14SSteve Longerbeam 	 */
273219a81c14SSteve Longerbeam 	if (sensor->power_count == 0)
273319a81c14SSteve Longerbeam 		return 0;
273419a81c14SSteve Longerbeam 
273519a81c14SSteve Longerbeam 	switch (ctrl->id) {
273619a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
273719a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
273819a81c14SSteve Longerbeam 		break;
273919a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
274019a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
274119a81c14SSteve Longerbeam 		break;
274219a81c14SSteve Longerbeam 	case V4L2_CID_AUTO_WHITE_BALANCE:
274319a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
274419a81c14SSteve Longerbeam 		break;
274519a81c14SSteve Longerbeam 	case V4L2_CID_HUE:
274619a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
274719a81c14SSteve Longerbeam 		break;
274819a81c14SSteve Longerbeam 	case V4L2_CID_CONTRAST:
274919a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
275019a81c14SSteve Longerbeam 		break;
275119a81c14SSteve Longerbeam 	case V4L2_CID_SATURATION:
275219a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
275319a81c14SSteve Longerbeam 		break;
275419a81c14SSteve Longerbeam 	case V4L2_CID_TEST_PATTERN:
275519a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
275619a81c14SSteve Longerbeam 		break;
27571068fecaSMylène Josserand 	case V4L2_CID_POWER_LINE_FREQUENCY:
27581068fecaSMylène Josserand 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
27591068fecaSMylène Josserand 		break;
2760ce85705aSHugues Fruchet 	case V4L2_CID_HFLIP:
2761ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2762ce85705aSHugues Fruchet 		break;
2763ce85705aSHugues Fruchet 	case V4L2_CID_VFLIP:
2764ce85705aSHugues Fruchet 		ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2765ce85705aSHugues Fruchet 		break;
276619a81c14SSteve Longerbeam 	default:
276719a81c14SSteve Longerbeam 		ret = -EINVAL;
276819a81c14SSteve Longerbeam 		break;
276919a81c14SSteve Longerbeam 	}
277019a81c14SSteve Longerbeam 
277119a81c14SSteve Longerbeam 	return ret;
277219a81c14SSteve Longerbeam }
277319a81c14SSteve Longerbeam 
277419a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
277519a81c14SSteve Longerbeam 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
277619a81c14SSteve Longerbeam 	.s_ctrl = ov5640_s_ctrl,
277719a81c14SSteve Longerbeam };
277819a81c14SSteve Longerbeam 
277919a81c14SSteve Longerbeam static const char * const test_pattern_menu[] = {
278019a81c14SSteve Longerbeam 	"Disabled",
278119a81c14SSteve Longerbeam 	"Color bars",
278219a81c14SSteve Longerbeam };
278319a81c14SSteve Longerbeam 
278419a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor)
278519a81c14SSteve Longerbeam {
278619a81c14SSteve Longerbeam 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
278719a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
278819a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
278919a81c14SSteve Longerbeam 	int ret;
279019a81c14SSteve Longerbeam 
279119a81c14SSteve Longerbeam 	v4l2_ctrl_handler_init(hdl, 32);
279219a81c14SSteve Longerbeam 
279319a81c14SSteve Longerbeam 	/* we can use our own mutex for the ctrl lock */
279419a81c14SSteve Longerbeam 	hdl->lock = &sensor->lock;
279519a81c14SSteve Longerbeam 
279619a81c14SSteve Longerbeam 	/* Auto/manual white balance */
279719a81c14SSteve Longerbeam 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
279819a81c14SSteve Longerbeam 					   V4L2_CID_AUTO_WHITE_BALANCE,
279919a81c14SSteve Longerbeam 					   0, 1, 1, 1);
280019a81c14SSteve Longerbeam 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
280119a81c14SSteve Longerbeam 						0, 4095, 1, 0);
280219a81c14SSteve Longerbeam 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
280319a81c14SSteve Longerbeam 					       0, 4095, 1, 0);
280419a81c14SSteve Longerbeam 	/* Auto/manual exposure */
280519a81c14SSteve Longerbeam 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
280619a81c14SSteve Longerbeam 						 V4L2_CID_EXPOSURE_AUTO,
280719a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_MANUAL, 0,
280819a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_AUTO);
280919a81c14SSteve Longerbeam 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
281019a81c14SSteve Longerbeam 					    0, 65535, 1, 0);
281119a81c14SSteve Longerbeam 	/* Auto/manual gain */
281219a81c14SSteve Longerbeam 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
281319a81c14SSteve Longerbeam 					     0, 1, 1, 1);
281419a81c14SSteve Longerbeam 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
281519a81c14SSteve Longerbeam 					0, 1023, 1, 0);
281619a81c14SSteve Longerbeam 
281719a81c14SSteve Longerbeam 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
281819a81c14SSteve Longerbeam 					      0, 255, 1, 64);
281919a81c14SSteve Longerbeam 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
282019a81c14SSteve Longerbeam 				       0, 359, 1, 0);
282119a81c14SSteve Longerbeam 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
282219a81c14SSteve Longerbeam 					    0, 255, 1, 0);
282319a81c14SSteve Longerbeam 	ctrls->test_pattern =
282419a81c14SSteve Longerbeam 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
282519a81c14SSteve Longerbeam 					     ARRAY_SIZE(test_pattern_menu) - 1,
282619a81c14SSteve Longerbeam 					     0, 0, test_pattern_menu);
2827ce85705aSHugues Fruchet 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2828ce85705aSHugues Fruchet 					 0, 1, 1, 0);
2829ce85705aSHugues Fruchet 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2830ce85705aSHugues Fruchet 					 0, 1, 1, 0);
283119a81c14SSteve Longerbeam 
28321068fecaSMylène Josserand 	ctrls->light_freq =
28331068fecaSMylène Josserand 		v4l2_ctrl_new_std_menu(hdl, ops,
28341068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY,
28351068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
28361068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
28371068fecaSMylène Josserand 
283819a81c14SSteve Longerbeam 	if (hdl->error) {
283919a81c14SSteve Longerbeam 		ret = hdl->error;
284019a81c14SSteve Longerbeam 		goto free_ctrls;
284119a81c14SSteve Longerbeam 	}
284219a81c14SSteve Longerbeam 
284319a81c14SSteve Longerbeam 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
284419a81c14SSteve Longerbeam 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
284519a81c14SSteve Longerbeam 
284619a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
284719a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
284819a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
284919a81c14SSteve Longerbeam 
285019a81c14SSteve Longerbeam 	sensor->sd.ctrl_handler = hdl;
285119a81c14SSteve Longerbeam 	return 0;
285219a81c14SSteve Longerbeam 
285319a81c14SSteve Longerbeam free_ctrls:
285419a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(hdl);
285519a81c14SSteve Longerbeam 	return ret;
285619a81c14SSteve Longerbeam }
285719a81c14SSteve Longerbeam 
285819a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
285919a81c14SSteve Longerbeam 				  struct v4l2_subdev_pad_config *cfg,
286019a81c14SSteve Longerbeam 				  struct v4l2_subdev_frame_size_enum *fse)
286119a81c14SSteve Longerbeam {
286219a81c14SSteve Longerbeam 	if (fse->pad != 0)
286319a81c14SSteve Longerbeam 		return -EINVAL;
286419a81c14SSteve Longerbeam 	if (fse->index >= OV5640_NUM_MODES)
286519a81c14SSteve Longerbeam 		return -EINVAL;
286619a81c14SSteve Longerbeam 
286741d8d7f5SHugues Fruchet 	fse->min_width =
2868dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].hact;
286941d8d7f5SHugues Fruchet 	fse->max_width = fse->min_width;
287041d8d7f5SHugues Fruchet 	fse->min_height =
2871dba13a0bSMaxime Ripard 		ov5640_mode_data[0][fse->index].vact;
287241d8d7f5SHugues Fruchet 	fse->max_height = fse->min_height;
287319a81c14SSteve Longerbeam 
287419a81c14SSteve Longerbeam 	return 0;
287519a81c14SSteve Longerbeam }
287619a81c14SSteve Longerbeam 
287719a81c14SSteve Longerbeam static int ov5640_enum_frame_interval(
287819a81c14SSteve Longerbeam 	struct v4l2_subdev *sd,
287919a81c14SSteve Longerbeam 	struct v4l2_subdev_pad_config *cfg,
288019a81c14SSteve Longerbeam 	struct v4l2_subdev_frame_interval_enum *fie)
288119a81c14SSteve Longerbeam {
288219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
288319a81c14SSteve Longerbeam 	struct v4l2_fract tpf;
288419a81c14SSteve Longerbeam 	int ret;
288519a81c14SSteve Longerbeam 
288619a81c14SSteve Longerbeam 	if (fie->pad != 0)
288719a81c14SSteve Longerbeam 		return -EINVAL;
288819a81c14SSteve Longerbeam 	if (fie->index >= OV5640_NUM_FRAMERATES)
288919a81c14SSteve Longerbeam 		return -EINVAL;
289019a81c14SSteve Longerbeam 
289119a81c14SSteve Longerbeam 	tpf.numerator = 1;
289219a81c14SSteve Longerbeam 	tpf.denominator = ov5640_framerates[fie->index];
289319a81c14SSteve Longerbeam 
289419a81c14SSteve Longerbeam 	ret = ov5640_try_frame_interval(sensor, &tpf,
289519a81c14SSteve Longerbeam 					fie->width, fie->height);
289619a81c14SSteve Longerbeam 	if (ret < 0)
289719a81c14SSteve Longerbeam 		return -EINVAL;
289819a81c14SSteve Longerbeam 
289919a81c14SSteve Longerbeam 	fie->interval = tpf;
290019a81c14SSteve Longerbeam 	return 0;
290119a81c14SSteve Longerbeam }
290219a81c14SSteve Longerbeam 
290319a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
290419a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
290519a81c14SSteve Longerbeam {
290619a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
290719a81c14SSteve Longerbeam 
290819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
290919a81c14SSteve Longerbeam 	fi->interval = sensor->frame_interval;
291019a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
291119a81c14SSteve Longerbeam 
291219a81c14SSteve Longerbeam 	return 0;
291319a81c14SSteve Longerbeam }
291419a81c14SSteve Longerbeam 
291519a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
291619a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
291719a81c14SSteve Longerbeam {
291819a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
291919a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
292019a81c14SSteve Longerbeam 	int frame_rate, ret = 0;
292119a81c14SSteve Longerbeam 
292219a81c14SSteve Longerbeam 	if (fi->pad != 0)
292319a81c14SSteve Longerbeam 		return -EINVAL;
292419a81c14SSteve Longerbeam 
292519a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
292619a81c14SSteve Longerbeam 
292719a81c14SSteve Longerbeam 	if (sensor->streaming) {
292819a81c14SSteve Longerbeam 		ret = -EBUSY;
292919a81c14SSteve Longerbeam 		goto out;
293019a81c14SSteve Longerbeam 	}
293119a81c14SSteve Longerbeam 
293219a81c14SSteve Longerbeam 	mode = sensor->current_mode;
293319a81c14SSteve Longerbeam 
293419a81c14SSteve Longerbeam 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2935dba13a0bSMaxime Ripard 					       mode->hact, mode->vact);
293619a81c14SSteve Longerbeam 	if (frame_rate < 0)
293719a81c14SSteve Longerbeam 		frame_rate = OV5640_15_FPS;
293819a81c14SSteve Longerbeam 
29393c4a7372SHugues Fruchet 	mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2940dba13a0bSMaxime Ripard 				mode->vact, true);
29413c4a7372SHugues Fruchet 	if (!mode) {
29423c4a7372SHugues Fruchet 		ret = -EINVAL;
29433c4a7372SHugues Fruchet 		goto out;
29443c4a7372SHugues Fruchet 	}
29453c4a7372SHugues Fruchet 
29460929983eSHugues Fruchet 	if (mode != sensor->current_mode ||
29470929983eSHugues Fruchet 	    frame_rate != sensor->current_fr) {
29480929983eSHugues Fruchet 		sensor->current_fr = frame_rate;
29490929983eSHugues Fruchet 		sensor->frame_interval = fi->interval;
29503c4a7372SHugues Fruchet 		sensor->current_mode = mode;
295119a81c14SSteve Longerbeam 		sensor->pending_mode_change = true;
29526949d864SHugues Fruchet 	}
295319a81c14SSteve Longerbeam out:
295419a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
295519a81c14SSteve Longerbeam 	return ret;
295619a81c14SSteve Longerbeam }
295719a81c14SSteve Longerbeam 
295819a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
295919a81c14SSteve Longerbeam 				 struct v4l2_subdev_pad_config *cfg,
296019a81c14SSteve Longerbeam 				 struct v4l2_subdev_mbus_code_enum *code)
296119a81c14SSteve Longerbeam {
296219a81c14SSteve Longerbeam 	if (code->pad != 0)
296319a81c14SSteve Longerbeam 		return -EINVAL;
2964e3ee691dSHugues Fruchet 	if (code->index >= ARRAY_SIZE(ov5640_formats))
296519a81c14SSteve Longerbeam 		return -EINVAL;
296619a81c14SSteve Longerbeam 
2967e3ee691dSHugues Fruchet 	code->code = ov5640_formats[code->index].code;
296819a81c14SSteve Longerbeam 	return 0;
296919a81c14SSteve Longerbeam }
297019a81c14SSteve Longerbeam 
297119a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
297219a81c14SSteve Longerbeam {
297319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
297419a81c14SSteve Longerbeam 	int ret = 0;
297519a81c14SSteve Longerbeam 
297619a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
297719a81c14SSteve Longerbeam 
297819a81c14SSteve Longerbeam 	if (sensor->streaming == !enable) {
297919a81c14SSteve Longerbeam 		if (enable && sensor->pending_mode_change) {
2980985cdcb0SHugues Fruchet 			ret = ov5640_set_mode(sensor);
298119a81c14SSteve Longerbeam 			if (ret)
298219a81c14SSteve Longerbeam 				goto out;
2983fb98e29fSHugues Fruchet 		}
2984e3ee691dSHugues Fruchet 
2985fb98e29fSHugues Fruchet 		if (enable && sensor->pending_fmt_change) {
2986e3ee691dSHugues Fruchet 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2987e3ee691dSHugues Fruchet 			if (ret)
2988e3ee691dSHugues Fruchet 				goto out;
2989fb98e29fSHugues Fruchet 			sensor->pending_fmt_change = false;
299019a81c14SSteve Longerbeam 		}
299119a81c14SSteve Longerbeam 
29922d95e7edSSakari Ailus 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2993f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, enable);
2994f22996dbSHugues Fruchet 		else
2995f22996dbSHugues Fruchet 			ret = ov5640_set_stream_dvp(sensor, enable);
2996f22996dbSHugues Fruchet 
299719a81c14SSteve Longerbeam 		if (!ret)
299819a81c14SSteve Longerbeam 			sensor->streaming = enable;
299919a81c14SSteve Longerbeam 	}
300019a81c14SSteve Longerbeam out:
300119a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
300219a81c14SSteve Longerbeam 	return ret;
300319a81c14SSteve Longerbeam }
300419a81c14SSteve Longerbeam 
300519a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = {
300619a81c14SSteve Longerbeam 	.s_power = ov5640_s_power,
30072d18fbc5SAkinobu Mita 	.log_status = v4l2_ctrl_subdev_log_status,
30082d18fbc5SAkinobu Mita 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
30092d18fbc5SAkinobu Mita 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
301019a81c14SSteve Longerbeam };
301119a81c14SSteve Longerbeam 
301219a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = {
301319a81c14SSteve Longerbeam 	.g_frame_interval = ov5640_g_frame_interval,
301419a81c14SSteve Longerbeam 	.s_frame_interval = ov5640_s_frame_interval,
301519a81c14SSteve Longerbeam 	.s_stream = ov5640_s_stream,
301619a81c14SSteve Longerbeam };
301719a81c14SSteve Longerbeam 
301819a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
301919a81c14SSteve Longerbeam 	.enum_mbus_code = ov5640_enum_mbus_code,
302019a81c14SSteve Longerbeam 	.get_fmt = ov5640_get_fmt,
302119a81c14SSteve Longerbeam 	.set_fmt = ov5640_set_fmt,
302219a81c14SSteve Longerbeam 	.enum_frame_size = ov5640_enum_frame_size,
302319a81c14SSteve Longerbeam 	.enum_frame_interval = ov5640_enum_frame_interval,
302419a81c14SSteve Longerbeam };
302519a81c14SSteve Longerbeam 
302619a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = {
302719a81c14SSteve Longerbeam 	.core = &ov5640_core_ops,
302819a81c14SSteve Longerbeam 	.video = &ov5640_video_ops,
302919a81c14SSteve Longerbeam 	.pad = &ov5640_pad_ops,
303019a81c14SSteve Longerbeam };
303119a81c14SSteve Longerbeam 
303219a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor)
303319a81c14SSteve Longerbeam {
303419a81c14SSteve Longerbeam 	int i;
303519a81c14SSteve Longerbeam 
303619a81c14SSteve Longerbeam 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
303719a81c14SSteve Longerbeam 		sensor->supplies[i].supply = ov5640_supply_name[i];
303819a81c14SSteve Longerbeam 
303919a81c14SSteve Longerbeam 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
304019a81c14SSteve Longerbeam 				       OV5640_NUM_SUPPLIES,
304119a81c14SSteve Longerbeam 				       sensor->supplies);
304219a81c14SSteve Longerbeam }
304319a81c14SSteve Longerbeam 
30440f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor)
30450f7acb52SHugues Fruchet {
30460f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
30470f7acb52SHugues Fruchet 	int ret = 0;
30480f7acb52SHugues Fruchet 	u16 chip_id;
30490f7acb52SHugues Fruchet 
30500f7acb52SHugues Fruchet 	ret = ov5640_set_power_on(sensor);
30510f7acb52SHugues Fruchet 	if (ret)
30520f7acb52SHugues Fruchet 		return ret;
30530f7acb52SHugues Fruchet 
30540f7acb52SHugues Fruchet 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
30550f7acb52SHugues Fruchet 	if (ret) {
30560f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
30570f7acb52SHugues Fruchet 			__func__);
30580f7acb52SHugues Fruchet 		goto power_off;
30590f7acb52SHugues Fruchet 	}
30600f7acb52SHugues Fruchet 
30610f7acb52SHugues Fruchet 	if (chip_id != 0x5640) {
30620f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
30630f7acb52SHugues Fruchet 			__func__, chip_id);
30640f7acb52SHugues Fruchet 		ret = -ENXIO;
30650f7acb52SHugues Fruchet 	}
30660f7acb52SHugues Fruchet 
30670f7acb52SHugues Fruchet power_off:
30680f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
30690f7acb52SHugues Fruchet 	return ret;
30700f7acb52SHugues Fruchet }
30710f7acb52SHugues Fruchet 
307219a81c14SSteve Longerbeam static int ov5640_probe(struct i2c_client *client,
307319a81c14SSteve Longerbeam 			const struct i2c_device_id *id)
307419a81c14SSteve Longerbeam {
307519a81c14SSteve Longerbeam 	struct device *dev = &client->dev;
307619a81c14SSteve Longerbeam 	struct fwnode_handle *endpoint;
307719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor;
3078e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *fmt;
3079c3f3ba3eSHugues Fruchet 	u32 rotation;
308019a81c14SSteve Longerbeam 	int ret;
308119a81c14SSteve Longerbeam 
308219a81c14SSteve Longerbeam 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
308319a81c14SSteve Longerbeam 	if (!sensor)
308419a81c14SSteve Longerbeam 		return -ENOMEM;
308519a81c14SSteve Longerbeam 
308619a81c14SSteve Longerbeam 	sensor->i2c_client = client;
3087fb98e29fSHugues Fruchet 
3088fb98e29fSHugues Fruchet 	/*
3089fb98e29fSHugues Fruchet 	 * default init sequence initialize sensor to
3090fb98e29fSHugues Fruchet 	 * YUV422 UYVY VGA@30fps
3091fb98e29fSHugues Fruchet 	 */
3092e6441fdeSHugues Fruchet 	fmt = &sensor->fmt;
3093fb98e29fSHugues Fruchet 	fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
3094fb98e29fSHugues Fruchet 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
3095e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
3096e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
3097e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
3098e6441fdeSHugues Fruchet 	fmt->width = 640;
3099e6441fdeSHugues Fruchet 	fmt->height = 480;
3100e6441fdeSHugues Fruchet 	fmt->field = V4L2_FIELD_NONE;
310119a81c14SSteve Longerbeam 	sensor->frame_interval.numerator = 1;
310219a81c14SSteve Longerbeam 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
310319a81c14SSteve Longerbeam 	sensor->current_fr = OV5640_30_FPS;
310419a81c14SSteve Longerbeam 	sensor->current_mode =
310519a81c14SSteve Longerbeam 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
3106985cdcb0SHugues Fruchet 	sensor->last_mode = sensor->current_mode;
310719a81c14SSteve Longerbeam 
310819a81c14SSteve Longerbeam 	sensor->ae_target = 52;
310919a81c14SSteve Longerbeam 
3110c3f3ba3eSHugues Fruchet 	/* optional indication of physical rotation of sensor */
3111c3f3ba3eSHugues Fruchet 	ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3112c3f3ba3eSHugues Fruchet 				       &rotation);
3113c3f3ba3eSHugues Fruchet 	if (!ret) {
3114c3f3ba3eSHugues Fruchet 		switch (rotation) {
3115c3f3ba3eSHugues Fruchet 		case 180:
3116c3f3ba3eSHugues Fruchet 			sensor->upside_down = true;
3117c3f3ba3eSHugues Fruchet 			/* fall through */
3118c3f3ba3eSHugues Fruchet 		case 0:
3119c3f3ba3eSHugues Fruchet 			break;
3120c3f3ba3eSHugues Fruchet 		default:
3121c3f3ba3eSHugues Fruchet 			dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3122c3f3ba3eSHugues Fruchet 				 rotation);
3123c3f3ba3eSHugues Fruchet 		}
3124c3f3ba3eSHugues Fruchet 	}
3125c3f3ba3eSHugues Fruchet 
3126ce96bcf5SSakari Ailus 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3127ce96bcf5SSakari Ailus 						  NULL);
312819a81c14SSteve Longerbeam 	if (!endpoint) {
312919a81c14SSteve Longerbeam 		dev_err(dev, "endpoint node not found\n");
313019a81c14SSteve Longerbeam 		return -EINVAL;
313119a81c14SSteve Longerbeam 	}
313219a81c14SSteve Longerbeam 
313319a81c14SSteve Longerbeam 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
313419a81c14SSteve Longerbeam 	fwnode_handle_put(endpoint);
313519a81c14SSteve Longerbeam 	if (ret) {
313619a81c14SSteve Longerbeam 		dev_err(dev, "Could not parse endpoint\n");
313719a81c14SSteve Longerbeam 		return ret;
313819a81c14SSteve Longerbeam 	}
313919a81c14SSteve Longerbeam 
314019a81c14SSteve Longerbeam 	/* get system clock (xclk) */
314119a81c14SSteve Longerbeam 	sensor->xclk = devm_clk_get(dev, "xclk");
314219a81c14SSteve Longerbeam 	if (IS_ERR(sensor->xclk)) {
314319a81c14SSteve Longerbeam 		dev_err(dev, "failed to get xclk\n");
314419a81c14SSteve Longerbeam 		return PTR_ERR(sensor->xclk);
314519a81c14SSteve Longerbeam 	}
314619a81c14SSteve Longerbeam 
314719a81c14SSteve Longerbeam 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
314819a81c14SSteve Longerbeam 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
314919a81c14SSteve Longerbeam 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
315019a81c14SSteve Longerbeam 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
315119a81c14SSteve Longerbeam 			sensor->xclk_freq);
315219a81c14SSteve Longerbeam 		return -EINVAL;
315319a81c14SSteve Longerbeam 	}
315419a81c14SSteve Longerbeam 
315519a81c14SSteve Longerbeam 	/* request optional power down pin */
315619a81c14SSteve Longerbeam 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
315719a81c14SSteve Longerbeam 						    GPIOD_OUT_HIGH);
315819a81c14SSteve Longerbeam 	/* request optional reset pin */
315919a81c14SSteve Longerbeam 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
316019a81c14SSteve Longerbeam 						     GPIOD_OUT_HIGH);
316119a81c14SSteve Longerbeam 
316219a81c14SSteve Longerbeam 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
316319a81c14SSteve Longerbeam 
31642d18fbc5SAkinobu Mita 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
31652d18fbc5SAkinobu Mita 			    V4L2_SUBDEV_FL_HAS_EVENTS;
316619a81c14SSteve Longerbeam 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
316719a81c14SSteve Longerbeam 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
316819a81c14SSteve Longerbeam 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
316919a81c14SSteve Longerbeam 	if (ret)
317019a81c14SSteve Longerbeam 		return ret;
317119a81c14SSteve Longerbeam 
317219a81c14SSteve Longerbeam 	ret = ov5640_get_regulators(sensor);
317319a81c14SSteve Longerbeam 	if (ret)
317419a81c14SSteve Longerbeam 		return ret;
317519a81c14SSteve Longerbeam 
317619a81c14SSteve Longerbeam 	mutex_init(&sensor->lock);
317719a81c14SSteve Longerbeam 
31780f7acb52SHugues Fruchet 	ret = ov5640_check_chip_id(sensor);
31790f7acb52SHugues Fruchet 	if (ret)
31800f7acb52SHugues Fruchet 		goto entity_cleanup;
31810f7acb52SHugues Fruchet 
318219a81c14SSteve Longerbeam 	ret = ov5640_init_controls(sensor);
318319a81c14SSteve Longerbeam 	if (ret)
318419a81c14SSteve Longerbeam 		goto entity_cleanup;
318519a81c14SSteve Longerbeam 
318619a81c14SSteve Longerbeam 	ret = v4l2_async_register_subdev(&sensor->sd);
318719a81c14SSteve Longerbeam 	if (ret)
318819a81c14SSteve Longerbeam 		goto free_ctrls;
318919a81c14SSteve Longerbeam 
319019a81c14SSteve Longerbeam 	return 0;
319119a81c14SSteve Longerbeam 
319219a81c14SSteve Longerbeam free_ctrls:
319319a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
319419a81c14SSteve Longerbeam entity_cleanup:
319519a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
319619a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
319719a81c14SSteve Longerbeam 	return ret;
319819a81c14SSteve Longerbeam }
319919a81c14SSteve Longerbeam 
320019a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client)
320119a81c14SSteve Longerbeam {
320219a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
320319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
320419a81c14SSteve Longerbeam 
320519a81c14SSteve Longerbeam 	v4l2_async_unregister_subdev(&sensor->sd);
320619a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
320719a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
320819a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
320919a81c14SSteve Longerbeam 
321019a81c14SSteve Longerbeam 	return 0;
321119a81c14SSteve Longerbeam }
321219a81c14SSteve Longerbeam 
321319a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = {
321419a81c14SSteve Longerbeam 	{"ov5640", 0},
321519a81c14SSteve Longerbeam 	{},
321619a81c14SSteve Longerbeam };
321719a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id);
321819a81c14SSteve Longerbeam 
321919a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = {
322019a81c14SSteve Longerbeam 	{ .compatible = "ovti,ov5640" },
322119a81c14SSteve Longerbeam 	{ /* sentinel */ }
322219a81c14SSteve Longerbeam };
322319a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
322419a81c14SSteve Longerbeam 
322519a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = {
322619a81c14SSteve Longerbeam 	.driver = {
322719a81c14SSteve Longerbeam 		.name  = "ov5640",
322819a81c14SSteve Longerbeam 		.of_match_table	= ov5640_dt_ids,
322919a81c14SSteve Longerbeam 	},
323019a81c14SSteve Longerbeam 	.id_table = ov5640_id,
323119a81c14SSteve Longerbeam 	.probe    = ov5640_probe,
323219a81c14SSteve Longerbeam 	.remove   = ov5640_remove,
323319a81c14SSteve Longerbeam };
323419a81c14SSteve Longerbeam 
323519a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver);
323619a81c14SSteve Longerbeam 
323719a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
323819a81c14SSteve Longerbeam MODULE_LICENSE("GPL");
3239