xref: /openbmc/linux/drivers/media/i2c/ov5640.c (revision bf4a4b51)
119a81c14SSteve Longerbeam /*
219a81c14SSteve Longerbeam  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
319a81c14SSteve Longerbeam  * Copyright (C) 2014-2017 Mentor Graphics Inc.
419a81c14SSteve Longerbeam  *
519a81c14SSteve Longerbeam  * This program is free software; you can redistribute it and/or modify
619a81c14SSteve Longerbeam  * it under the terms of the GNU General Public License as published by
719a81c14SSteve Longerbeam  * the Free Software Foundation; either version 2 of the License, or
819a81c14SSteve Longerbeam  * (at your option) any later version.
919a81c14SSteve Longerbeam  */
1019a81c14SSteve Longerbeam 
1119a81c14SSteve Longerbeam #include <linux/clk.h>
1219a81c14SSteve Longerbeam #include <linux/clk-provider.h>
1319a81c14SSteve Longerbeam #include <linux/clkdev.h>
1419a81c14SSteve Longerbeam #include <linux/ctype.h>
1519a81c14SSteve Longerbeam #include <linux/delay.h>
1619a81c14SSteve Longerbeam #include <linux/device.h>
1741d8d7f5SHugues Fruchet #include <linux/gpio/consumer.h>
1819a81c14SSteve Longerbeam #include <linux/i2c.h>
1919a81c14SSteve Longerbeam #include <linux/init.h>
2019a81c14SSteve Longerbeam #include <linux/module.h>
2119a81c14SSteve Longerbeam #include <linux/of_device.h>
2241d8d7f5SHugues Fruchet #include <linux/regulator/consumer.h>
2319a81c14SSteve Longerbeam #include <linux/slab.h>
2419a81c14SSteve Longerbeam #include <linux/types.h>
2519a81c14SSteve Longerbeam #include <media/v4l2-async.h>
2619a81c14SSteve Longerbeam #include <media/v4l2-ctrls.h>
2719a81c14SSteve Longerbeam #include <media/v4l2-device.h>
2819a81c14SSteve Longerbeam #include <media/v4l2-fwnode.h>
2919a81c14SSteve Longerbeam #include <media/v4l2-subdev.h>
3019a81c14SSteve Longerbeam 
3119a81c14SSteve Longerbeam /* min/typical/max system clock (xclk) frequencies */
3219a81c14SSteve Longerbeam #define OV5640_XCLK_MIN  6000000
3319a81c14SSteve Longerbeam #define OV5640_XCLK_MAX 24000000
3419a81c14SSteve Longerbeam 
3519a81c14SSteve Longerbeam #define OV5640_DEFAULT_SLAVE_ID 0x3c
3619a81c14SSteve Longerbeam 
37d47c4126SHugues Fruchet #define OV5640_REG_SYS_RESET02		0x3002
38d47c4126SHugues Fruchet #define OV5640_REG_SYS_CLOCK_ENABLE02	0x3006
39f22996dbSHugues Fruchet #define OV5640_REG_SYS_CTRL0		0x3008
4019a81c14SSteve Longerbeam #define OV5640_REG_CHIP_ID		0x300a
41f22996dbSHugues Fruchet #define OV5640_REG_IO_MIPI_CTRL00	0x300e
42f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE01	0x3017
43f22996dbSHugues Fruchet #define OV5640_REG_PAD_OUTPUT_ENABLE02	0x3018
4419a81c14SSteve Longerbeam #define OV5640_REG_PAD_OUTPUT00		0x3019
45f22996dbSHugues Fruchet #define OV5640_REG_SYSTEM_CONTROL1	0x302e
4619a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL0		0x3034
4719a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL1		0x3035
4819a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL2		0x3036
4919a81c14SSteve Longerbeam #define OV5640_REG_SC_PLL_CTRL3		0x3037
5019a81c14SSteve Longerbeam #define OV5640_REG_SLAVE_ID		0x3100
51f22996dbSHugues Fruchet #define OV5640_REG_SCCB_SYS_CTRL1	0x3103
5219a81c14SSteve Longerbeam #define OV5640_REG_SYS_ROOT_DIVIDER	0x3108
5319a81c14SSteve Longerbeam #define OV5640_REG_AWB_R_GAIN		0x3400
5419a81c14SSteve Longerbeam #define OV5640_REG_AWB_G_GAIN		0x3402
5519a81c14SSteve Longerbeam #define OV5640_REG_AWB_B_GAIN		0x3404
5619a81c14SSteve Longerbeam #define OV5640_REG_AWB_MANUAL_CTRL	0x3406
5719a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_HI	0x3500
5819a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_MED	0x3501
5919a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_EXPOSURE_LO	0x3502
6019a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_MANUAL	0x3503
6119a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_REAL_GAIN	0x350a
6219a81c14SSteve Longerbeam #define OV5640_REG_AEC_PK_VTS		0x350c
6319a81c14SSteve Longerbeam #define OV5640_REG_TIMING_HTS		0x380c
6419a81c14SSteve Longerbeam #define OV5640_REG_TIMING_VTS		0x380e
6519a81c14SSteve Longerbeam #define OV5640_REG_TIMING_TC_REG21	0x3821
6619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL00		0x3a00
6719a81c14SSteve Longerbeam #define OV5640_REG_AEC_B50_STEP		0x3a08
6819a81c14SSteve Longerbeam #define OV5640_REG_AEC_B60_STEP		0x3a0a
6919a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0D		0x3a0d
7019a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0E		0x3a0e
7119a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL0F		0x3a0f
7219a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL10		0x3a10
7319a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL11		0x3a11
7419a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1B		0x3a1b
7519a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1E		0x3a1e
7619a81c14SSteve Longerbeam #define OV5640_REG_AEC_CTRL1F		0x3a1f
7719a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL00	0x3c00
7819a81c14SSteve Longerbeam #define OV5640_REG_HZ5060_CTRL01	0x3c01
7919a81c14SSteve Longerbeam #define OV5640_REG_SIGMADELTA_CTRL0C	0x3c0c
8019a81c14SSteve Longerbeam #define OV5640_REG_FRAME_CTRL01		0x4202
81e3ee691dSHugues Fruchet #define OV5640_REG_FORMAT_CONTROL00	0x4300
82f22996dbSHugues Fruchet #define OV5640_REG_POLARITY_CTRL00	0x4740
8319a81c14SSteve Longerbeam #define OV5640_REG_MIPI_CTRL00		0x4800
8419a81c14SSteve Longerbeam #define OV5640_REG_DEBUG_MODE		0x4814
85e3ee691dSHugues Fruchet #define OV5640_REG_ISP_FORMAT_MUX_CTRL	0x501f
8619a81c14SSteve Longerbeam #define OV5640_REG_PRE_ISP_TEST_SET1	0x503d
8719a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL0		0x5580
8819a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL1		0x5581
8919a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL3		0x5583
9019a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL4		0x5584
9119a81c14SSteve Longerbeam #define OV5640_REG_SDE_CTRL5		0x5585
9219a81c14SSteve Longerbeam #define OV5640_REG_AVG_READOUT		0x56a1
9319a81c14SSteve Longerbeam 
9419a81c14SSteve Longerbeam enum ov5640_mode_id {
9519a81c14SSteve Longerbeam 	OV5640_MODE_QCIF_176_144 = 0,
9619a81c14SSteve Longerbeam 	OV5640_MODE_QVGA_320_240,
9719a81c14SSteve Longerbeam 	OV5640_MODE_VGA_640_480,
9819a81c14SSteve Longerbeam 	OV5640_MODE_NTSC_720_480,
9919a81c14SSteve Longerbeam 	OV5640_MODE_PAL_720_576,
10019a81c14SSteve Longerbeam 	OV5640_MODE_XGA_1024_768,
10119a81c14SSteve Longerbeam 	OV5640_MODE_720P_1280_720,
10219a81c14SSteve Longerbeam 	OV5640_MODE_1080P_1920_1080,
10319a81c14SSteve Longerbeam 	OV5640_MODE_QSXGA_2592_1944,
10419a81c14SSteve Longerbeam 	OV5640_NUM_MODES,
10519a81c14SSteve Longerbeam };
10619a81c14SSteve Longerbeam 
10719a81c14SSteve Longerbeam enum ov5640_frame_rate {
10819a81c14SSteve Longerbeam 	OV5640_15_FPS = 0,
10919a81c14SSteve Longerbeam 	OV5640_30_FPS,
11019a81c14SSteve Longerbeam 	OV5640_NUM_FRAMERATES,
11119a81c14SSteve Longerbeam };
11219a81c14SSteve Longerbeam 
113e3ee691dSHugues Fruchet struct ov5640_pixfmt {
114e3ee691dSHugues Fruchet 	u32 code;
115e3ee691dSHugues Fruchet 	u32 colorspace;
116e3ee691dSHugues Fruchet };
117e3ee691dSHugues Fruchet 
118e3ee691dSHugues Fruchet static const struct ov5640_pixfmt ov5640_formats[] = {
119d47c4126SHugues Fruchet 	{ MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
120e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
121e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
122e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
123e3ee691dSHugues Fruchet 	{ MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
124e3ee691dSHugues Fruchet };
125e3ee691dSHugues Fruchet 
12619a81c14SSteve Longerbeam /*
12719a81c14SSteve Longerbeam  * FIXME: remove this when a subdev API becomes available
12819a81c14SSteve Longerbeam  * to set the MIPI CSI-2 virtual channel.
12919a81c14SSteve Longerbeam  */
13019a81c14SSteve Longerbeam static unsigned int virtual_channel;
1318670d70aSHugues Fruchet module_param(virtual_channel, uint, 0444);
13219a81c14SSteve Longerbeam MODULE_PARM_DESC(virtual_channel,
13319a81c14SSteve Longerbeam 		 "MIPI CSI-2 virtual channel (0..3), default 0");
13419a81c14SSteve Longerbeam 
13519a81c14SSteve Longerbeam static const int ov5640_framerates[] = {
13619a81c14SSteve Longerbeam 	[OV5640_15_FPS] = 15,
13719a81c14SSteve Longerbeam 	[OV5640_30_FPS] = 30,
13819a81c14SSteve Longerbeam };
13919a81c14SSteve Longerbeam 
14019a81c14SSteve Longerbeam /* regulator supplies */
14119a81c14SSteve Longerbeam static const char * const ov5640_supply_name[] = {
14241d8d7f5SHugues Fruchet 	"DOVDD", /* Digital I/O (1.8V) supply */
14319a81c14SSteve Longerbeam 	"DVDD",  /* Digital Core (1.5V) supply */
14419a81c14SSteve Longerbeam 	"AVDD",  /* Analog (2.8V) supply */
14519a81c14SSteve Longerbeam };
14619a81c14SSteve Longerbeam 
14719a81c14SSteve Longerbeam #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
14819a81c14SSteve Longerbeam 
14919a81c14SSteve Longerbeam /*
15019a81c14SSteve Longerbeam  * Image size under 1280 * 960 are SUBSAMPLING
15119a81c14SSteve Longerbeam  * Image size upper 1280 * 960 are SCALING
15219a81c14SSteve Longerbeam  */
15319a81c14SSteve Longerbeam enum ov5640_downsize_mode {
15419a81c14SSteve Longerbeam 	SUBSAMPLING,
15519a81c14SSteve Longerbeam 	SCALING,
15619a81c14SSteve Longerbeam };
15719a81c14SSteve Longerbeam 
15819a81c14SSteve Longerbeam struct reg_value {
15919a81c14SSteve Longerbeam 	u16 reg_addr;
16019a81c14SSteve Longerbeam 	u8 val;
16119a81c14SSteve Longerbeam 	u8 mask;
16219a81c14SSteve Longerbeam 	u32 delay_ms;
16319a81c14SSteve Longerbeam };
16419a81c14SSteve Longerbeam 
16519a81c14SSteve Longerbeam struct ov5640_mode_info {
16619a81c14SSteve Longerbeam 	enum ov5640_mode_id id;
16719a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode;
16819a81c14SSteve Longerbeam 	u32 width;
16919a81c14SSteve Longerbeam 	u32 height;
17019a81c14SSteve Longerbeam 	const struct reg_value *reg_data;
17119a81c14SSteve Longerbeam 	u32 reg_data_size;
17219a81c14SSteve Longerbeam };
17319a81c14SSteve Longerbeam 
17419a81c14SSteve Longerbeam struct ov5640_ctrls {
17519a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler handler;
17619a81c14SSteve Longerbeam 	struct {
17719a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_exp;
17819a81c14SSteve Longerbeam 		struct v4l2_ctrl *exposure;
17919a81c14SSteve Longerbeam 	};
18019a81c14SSteve Longerbeam 	struct {
18119a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_wb;
18219a81c14SSteve Longerbeam 		struct v4l2_ctrl *blue_balance;
18319a81c14SSteve Longerbeam 		struct v4l2_ctrl *red_balance;
18419a81c14SSteve Longerbeam 	};
18519a81c14SSteve Longerbeam 	struct {
18619a81c14SSteve Longerbeam 		struct v4l2_ctrl *auto_gain;
18719a81c14SSteve Longerbeam 		struct v4l2_ctrl *gain;
18819a81c14SSteve Longerbeam 	};
18919a81c14SSteve Longerbeam 	struct v4l2_ctrl *brightness;
1901068fecaSMylène Josserand 	struct v4l2_ctrl *light_freq;
19119a81c14SSteve Longerbeam 	struct v4l2_ctrl *saturation;
19219a81c14SSteve Longerbeam 	struct v4l2_ctrl *contrast;
19319a81c14SSteve Longerbeam 	struct v4l2_ctrl *hue;
19419a81c14SSteve Longerbeam 	struct v4l2_ctrl *test_pattern;
19519a81c14SSteve Longerbeam };
19619a81c14SSteve Longerbeam 
19719a81c14SSteve Longerbeam struct ov5640_dev {
19819a81c14SSteve Longerbeam 	struct i2c_client *i2c_client;
19919a81c14SSteve Longerbeam 	struct v4l2_subdev sd;
20019a81c14SSteve Longerbeam 	struct media_pad pad;
20119a81c14SSteve Longerbeam 	struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
20219a81c14SSteve Longerbeam 	struct clk *xclk; /* system clock to OV5640 */
20319a81c14SSteve Longerbeam 	u32 xclk_freq;
20419a81c14SSteve Longerbeam 
20519a81c14SSteve Longerbeam 	struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
20619a81c14SSteve Longerbeam 	struct gpio_desc *reset_gpio;
20719a81c14SSteve Longerbeam 	struct gpio_desc *pwdn_gpio;
20819a81c14SSteve Longerbeam 
20919a81c14SSteve Longerbeam 	/* lock to protect all members below */
21019a81c14SSteve Longerbeam 	struct mutex lock;
21119a81c14SSteve Longerbeam 
21219a81c14SSteve Longerbeam 	int power_count;
21319a81c14SSteve Longerbeam 
21419a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt fmt;
21519a81c14SSteve Longerbeam 
21619a81c14SSteve Longerbeam 	const struct ov5640_mode_info *current_mode;
21719a81c14SSteve Longerbeam 	enum ov5640_frame_rate current_fr;
21819a81c14SSteve Longerbeam 	struct v4l2_fract frame_interval;
21919a81c14SSteve Longerbeam 
22019a81c14SSteve Longerbeam 	struct ov5640_ctrls ctrls;
22119a81c14SSteve Longerbeam 
22219a81c14SSteve Longerbeam 	u32 prev_sysclk, prev_hts;
22319a81c14SSteve Longerbeam 	u32 ae_low, ae_high, ae_target;
22419a81c14SSteve Longerbeam 
22519a81c14SSteve Longerbeam 	bool pending_mode_change;
22619a81c14SSteve Longerbeam 	bool streaming;
22719a81c14SSteve Longerbeam };
22819a81c14SSteve Longerbeam 
22919a81c14SSteve Longerbeam static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
23019a81c14SSteve Longerbeam {
23119a81c14SSteve Longerbeam 	return container_of(sd, struct ov5640_dev, sd);
23219a81c14SSteve Longerbeam }
23319a81c14SSteve Longerbeam 
23419a81c14SSteve Longerbeam static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
23519a81c14SSteve Longerbeam {
23619a81c14SSteve Longerbeam 	return &container_of(ctrl->handler, struct ov5640_dev,
23719a81c14SSteve Longerbeam 			     ctrls.handler)->sd;
23819a81c14SSteve Longerbeam }
23919a81c14SSteve Longerbeam 
24019a81c14SSteve Longerbeam /*
24119a81c14SSteve Longerbeam  * FIXME: all of these register tables are likely filled with
24219a81c14SSteve Longerbeam  * entries that set the register to their power-on default values,
24319a81c14SSteve Longerbeam  * and which are otherwise not touched by this driver. Those entries
24419a81c14SSteve Longerbeam  * should be identified and removed to speed register load time
24519a81c14SSteve Longerbeam  * over i2c.
24619a81c14SSteve Longerbeam  */
24719a81c14SSteve Longerbeam 
24819a81c14SSteve Longerbeam static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
24919a81c14SSteve Longerbeam 	{0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
25019a81c14SSteve Longerbeam 	{0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
25119a81c14SSteve Longerbeam 	{0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
25219a81c14SSteve Longerbeam 	{0x3037, 0x13, 0, 0}, {0x3108, 0x01, 0, 0}, {0x3630, 0x36, 0, 0},
25319a81c14SSteve Longerbeam 	{0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
25419a81c14SSteve Longerbeam 	{0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
25519a81c14SSteve Longerbeam 	{0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
25619a81c14SSteve Longerbeam 	{0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
25719a81c14SSteve Longerbeam 	{0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
25819a81c14SSteve Longerbeam 	{0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
25919a81c14SSteve Longerbeam 	{0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
26019a81c14SSteve Longerbeam 	{0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
26119a81c14SSteve Longerbeam 	{0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
26219a81c14SSteve Longerbeam 	{0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
26319a81c14SSteve Longerbeam 	{0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
26419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
26519a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
26619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
26719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
26819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
26919a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
27019a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
27119a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
27219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
27319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
27419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
27519a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
27619a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
27719a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
27819a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
27919a81c14SSteve Longerbeam 	{0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
28019a81c14SSteve Longerbeam 	{0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
28119a81c14SSteve Longerbeam 	{0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
28219a81c14SSteve Longerbeam 	{0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
28319a81c14SSteve Longerbeam 	{0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
28419a81c14SSteve Longerbeam 	{0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
28519a81c14SSteve Longerbeam 	{0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
28619a81c14SSteve Longerbeam 	{0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
28719a81c14SSteve Longerbeam 	{0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
28819a81c14SSteve Longerbeam 	{0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
28919a81c14SSteve Longerbeam 	{0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
29019a81c14SSteve Longerbeam 	{0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
29119a81c14SSteve Longerbeam 	{0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
29219a81c14SSteve Longerbeam 	{0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
29319a81c14SSteve Longerbeam 	{0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
29419a81c14SSteve Longerbeam 	{0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
29519a81c14SSteve Longerbeam 	{0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
29619a81c14SSteve Longerbeam 	{0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
29719a81c14SSteve Longerbeam 	{0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
29819a81c14SSteve Longerbeam 	{0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
29919a81c14SSteve Longerbeam 	{0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
30019a81c14SSteve Longerbeam 	{0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
30119a81c14SSteve Longerbeam 	{0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
30219a81c14SSteve Longerbeam 	{0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
30319a81c14SSteve Longerbeam 	{0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
30419a81c14SSteve Longerbeam 	{0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
30519a81c14SSteve Longerbeam 	{0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
30619a81c14SSteve Longerbeam 	{0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
30719a81c14SSteve Longerbeam 	{0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
30819a81c14SSteve Longerbeam 	{0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
30919a81c14SSteve Longerbeam 	{0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
31019a81c14SSteve Longerbeam 	{0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
31119a81c14SSteve Longerbeam 	{0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
31219a81c14SSteve Longerbeam 	{0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
31319a81c14SSteve Longerbeam 	{0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
31419a81c14SSteve Longerbeam 	{0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
31519a81c14SSteve Longerbeam 	{0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
31619a81c14SSteve Longerbeam 	{0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
31719a81c14SSteve Longerbeam 	{0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
31819a81c14SSteve Longerbeam 	{0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
31919a81c14SSteve Longerbeam 	{0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
32019a81c14SSteve Longerbeam 	{0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
32119a81c14SSteve Longerbeam 	{0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
32219a81c14SSteve Longerbeam 	{0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
32319a81c14SSteve Longerbeam 	{0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
32419a81c14SSteve Longerbeam 	{0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
32519a81c14SSteve Longerbeam 	{0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
32619a81c14SSteve Longerbeam 	{0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
32719a81c14SSteve Longerbeam 	{0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
32819a81c14SSteve Longerbeam 	{0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
32919a81c14SSteve Longerbeam 	{0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
33019a81c14SSteve Longerbeam 	{0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
33119a81c14SSteve Longerbeam 	{0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
33219a81c14SSteve Longerbeam 	{0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
33319a81c14SSteve Longerbeam 	{0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
33419a81c14SSteve Longerbeam };
33519a81c14SSteve Longerbeam 
33619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
33719a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
33819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
33919a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
34019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
34119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
34219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
34319a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
34419a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
34519a81c14SSteve Longerbeam 	{0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
34619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
34719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
34819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
34919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
35019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
35119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
35219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
35319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
35419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
35519a81c14SSteve Longerbeam };
35619a81c14SSteve Longerbeam 
35719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
35819a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
35919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
36019a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
36119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
36219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
36319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
36419a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
36519a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
36619a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
36719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
36819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
36919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
37019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
37119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
37219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
37319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
37419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
37519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
37619a81c14SSteve Longerbeam };
37719a81c14SSteve Longerbeam 
37819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
37919a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
38019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
38119a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
38219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
38319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
38419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
38519a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
38619a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
38719a81c14SSteve Longerbeam 	{0x380e, 0x04, 0, 0}, {0x380f, 0x38, 0, 0}, {0x3810, 0x00, 0, 0},
38819a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
38919a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
39019a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
39119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
39219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
39319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
39419a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
39519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
39619a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
39719a81c14SSteve Longerbeam 	{0x3808, 0x04, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0},
39819a81c14SSteve Longerbeam 	{0x380b, 0x00, 0, 0}, {0x3035, 0x12, 0, 0},
39919a81c14SSteve Longerbeam };
40019a81c14SSteve Longerbeam 
40119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
40219a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
40319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
40419a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
40519a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
40619a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
40719a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
40819a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0x80, 0, 0}, {0x380a, 0x01, 0, 0},
40919a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
41019a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
41119a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
41219a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
41319a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
41419a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
41519a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
41619a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
41719a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
41819a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
41919a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3808, 0x04, 0, 0},
42019a81c14SSteve Longerbeam 	{0x3809, 0x00, 0, 0}, {0x380a, 0x03, 0, 0}, {0x380b, 0x00, 0, 0},
42119a81c14SSteve Longerbeam };
42219a81c14SSteve Longerbeam 
42319a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
42419a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
42519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
42619a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
42719a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
42819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
42919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
43019a81c14SSteve Longerbeam 	{0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
43119a81c14SSteve Longerbeam 	{0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
43219a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
43319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
43419a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
43519a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
43619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
43719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
43819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
43919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
44019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
44119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
44219a81c14SSteve Longerbeam };
44319a81c14SSteve Longerbeam 
44419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
44519a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
44619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
44719a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
44819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
44919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
45019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
45119a81c14SSteve Longerbeam 	{0x3808, 0x01, 0, 0}, {0x3809, 0x40, 0, 0}, {0x380a, 0x00, 0, 0},
45219a81c14SSteve Longerbeam 	{0x380b, 0xf0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
45319a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
45419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
45519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
45619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
45719a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
45819a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
45919a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
46019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
46119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
46219a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
46319a81c14SSteve Longerbeam };
46419a81c14SSteve Longerbeam 
46519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
46619a81c14SSteve Longerbeam 	{0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
46719a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
46819a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
46919a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
47019a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
47119a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
47219a81c14SSteve Longerbeam 	{0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
47319a81c14SSteve Longerbeam 	{0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
47419a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
47519a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
47619a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
47719a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
47819a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
47919a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
48019a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
48119a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
48219a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
48319a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
48419a81c14SSteve Longerbeam };
48541d8d7f5SHugues Fruchet 
48619a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
48719a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
48819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
48919a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
49019a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
49119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
49219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
49319a81c14SSteve Longerbeam 	{0x3808, 0x00, 0, 0}, {0x3809, 0xb0, 0, 0}, {0x380a, 0x00, 0, 0},
49419a81c14SSteve Longerbeam 	{0x380b, 0x90, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
49519a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
49619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
49719a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
49819a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
49919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
50019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
50119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
50219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
50319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
50419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
50519a81c14SSteve Longerbeam };
50619a81c14SSteve Longerbeam 
50719a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
50819a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
50919a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
51019a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
51119a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
51219a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
51319a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
51419a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
51519a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
51619a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
51719a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
51819a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
51919a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
52019a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
52119a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
52219a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
52319a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
52419a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
52519a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
52619a81c14SSteve Longerbeam };
52719a81c14SSteve Longerbeam 
52819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
52919a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
53019a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
53119a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
53219a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
53319a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
53419a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
53519a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x01, 0, 0},
53619a81c14SSteve Longerbeam 	{0x380b, 0xe0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
53719a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
53819a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
53919a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
54019a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
54119a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
54219a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
54319a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
54419a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
54519a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
54619a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
54719a81c14SSteve Longerbeam };
54819a81c14SSteve Longerbeam 
54919a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
55019a81c14SSteve Longerbeam 	{0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
55119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
55219a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
55319a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
55419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
55519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
55619a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
55719a81c14SSteve Longerbeam 	{0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
55819a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
55919a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
56019a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
56119a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
56219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
56319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
56419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
56519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
56619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
56719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
56819a81c14SSteve Longerbeam };
56919a81c14SSteve Longerbeam 
57019a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
57119a81c14SSteve Longerbeam 	{0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
57219a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
57319a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
57419a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
57519a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
57619a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
57719a81c14SSteve Longerbeam 	{0x3808, 0x02, 0, 0}, {0x3809, 0xd0, 0, 0}, {0x380a, 0x02, 0, 0},
57819a81c14SSteve Longerbeam 	{0x380b, 0x40, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x68, 0, 0},
57919a81c14SSteve Longerbeam 	{0x380e, 0x03, 0, 0}, {0x380f, 0xd8, 0, 0}, {0x3810, 0x00, 0, 0},
58019a81c14SSteve Longerbeam 	{0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
58119a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
58219a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
58319a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
58419a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
58519a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
58619a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
58719a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
58819a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
58919a81c14SSteve Longerbeam };
59019a81c14SSteve Longerbeam 
59119a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
59219a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
59319a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
59419a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
59519a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
59619a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
59719a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
59819a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
59919a81c14SSteve Longerbeam 	{0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
60019a81c14SSteve Longerbeam 	{0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
60119a81c14SSteve Longerbeam 	{0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
60219a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
60319a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
60419a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
60519a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
60619a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
60719a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
60819a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
60919a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
61019a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
61119a81c14SSteve Longerbeam 	{0x3008, 0x02, 0, 0}, {0x3503, 0,    0, 0},
61219a81c14SSteve Longerbeam };
61319a81c14SSteve Longerbeam 
61419a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
61519a81c14SSteve Longerbeam 	{0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
61619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
61719a81c14SSteve Longerbeam 	{0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
61819a81c14SSteve Longerbeam 	{0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
61919a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
62019a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
62119a81c14SSteve Longerbeam 	{0x3808, 0x05, 0, 0}, {0x3809, 0x00, 0, 0}, {0x380a, 0x02, 0, 0},
62219a81c14SSteve Longerbeam 	{0x380b, 0xd0, 0, 0}, {0x380c, 0x07, 0, 0}, {0x380d, 0x64, 0, 0},
62319a81c14SSteve Longerbeam 	{0x380e, 0x02, 0, 0}, {0x380f, 0xe4, 0, 0}, {0x3810, 0x00, 0, 0},
62419a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
62519a81c14SSteve Longerbeam 	{0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
62619a81c14SSteve Longerbeam 	{0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
62719a81c14SSteve Longerbeam 	{0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
62819a81c14SSteve Longerbeam 	{0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
62919a81c14SSteve Longerbeam 	{0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
63019a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
63119a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
63219a81c14SSteve Longerbeam 	{0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
63319a81c14SSteve Longerbeam };
63419a81c14SSteve Longerbeam 
63519a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
63619a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
63719a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
63819a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
63919a81c14SSteve Longerbeam 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
64019a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
64119a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
64219a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
64319a81c14SSteve Longerbeam 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
64419a81c14SSteve Longerbeam 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
64519a81c14SSteve Longerbeam 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
64619a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
64719a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
64819a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
64919a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
65019a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
65119a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
65219a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
65319a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
65419a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
65519a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
65619a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
65719a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
65819a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
65919a81c14SSteve Longerbeam 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
66019a81c14SSteve Longerbeam 	{0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
66119a81c14SSteve Longerbeam 	{0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
66219a81c14SSteve Longerbeam 	{0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
66319a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
66419a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
66519a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
66619a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
66719a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
66819a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
66919a81c14SSteve Longerbeam 	{0x3503, 0, 0, 0},
67019a81c14SSteve Longerbeam };
67119a81c14SSteve Longerbeam 
67219a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
67319a81c14SSteve Longerbeam 	{0x3008, 0x42, 0, 0},
67419a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
67519a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
67619a81c14SSteve Longerbeam 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
67719a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
67819a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
67919a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
68019a81c14SSteve Longerbeam 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
68119a81c14SSteve Longerbeam 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
68219a81c14SSteve Longerbeam 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
68319a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
68419a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
68519a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
68619a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
68719a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
68819a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
68919a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
69019a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
69119a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
69219a81c14SSteve Longerbeam 	{0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
69319a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
69419a81c14SSteve Longerbeam 	{0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
69519a81c14SSteve Longerbeam 	{0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
69619a81c14SSteve Longerbeam 	{0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, {0x3808, 0x07, 0, 0},
69719a81c14SSteve Longerbeam 	{0x3809, 0x80, 0, 0}, {0x380a, 0x04, 0, 0}, {0x380b, 0x38, 0, 0},
69819a81c14SSteve Longerbeam 	{0x380c, 0x09, 0, 0}, {0x380d, 0xc4, 0, 0}, {0x380e, 0x04, 0, 0},
69919a81c14SSteve Longerbeam 	{0x380f, 0x60, 0, 0}, {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
70019a81c14SSteve Longerbeam 	{0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
70119a81c14SSteve Longerbeam 	{0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
70219a81c14SSteve Longerbeam 	{0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
70319a81c14SSteve Longerbeam 	{0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
70419a81c14SSteve Longerbeam 	{0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
70519a81c14SSteve Longerbeam 	{0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
70619a81c14SSteve Longerbeam };
70719a81c14SSteve Longerbeam 
70819a81c14SSteve Longerbeam static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
70919a81c14SSteve Longerbeam 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0},
71019a81c14SSteve Longerbeam 	{0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
71119a81c14SSteve Longerbeam 	{0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
71219a81c14SSteve Longerbeam 	{0x3820, 0x40, 0, 0}, {0x3821, 0x06, 0, 0}, {0x3814, 0x11, 0, 0},
71319a81c14SSteve Longerbeam 	{0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
71419a81c14SSteve Longerbeam 	{0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
71519a81c14SSteve Longerbeam 	{0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
71619a81c14SSteve Longerbeam 	{0x3808, 0x0a, 0, 0}, {0x3809, 0x20, 0, 0}, {0x380a, 0x07, 0, 0},
71719a81c14SSteve Longerbeam 	{0x380b, 0x98, 0, 0}, {0x380c, 0x0b, 0, 0}, {0x380d, 0x1c, 0, 0},
71819a81c14SSteve Longerbeam 	{0x380e, 0x07, 0, 0}, {0x380f, 0xb0, 0, 0}, {0x3810, 0x00, 0, 0},
71919a81c14SSteve Longerbeam 	{0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
72019a81c14SSteve Longerbeam 	{0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
72119a81c14SSteve Longerbeam 	{0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
72219a81c14SSteve Longerbeam 	{0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
72319a81c14SSteve Longerbeam 	{0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
72419a81c14SSteve Longerbeam 	{0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
72519a81c14SSteve Longerbeam 	{0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
72619a81c14SSteve Longerbeam 	{0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
72719a81c14SSteve Longerbeam 	{0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
72819a81c14SSteve Longerbeam };
72919a81c14SSteve Longerbeam 
73019a81c14SSteve Longerbeam /* power-on sensor init reg table */
73119a81c14SSteve Longerbeam static const struct ov5640_mode_info ov5640_mode_init_data = {
73219a81c14SSteve Longerbeam 	0, SUBSAMPLING, 640, 480, ov5640_init_setting_30fps_VGA,
73319a81c14SSteve Longerbeam 	ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
73419a81c14SSteve Longerbeam };
73519a81c14SSteve Longerbeam 
73619a81c14SSteve Longerbeam static const struct ov5640_mode_info
73719a81c14SSteve Longerbeam ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
73819a81c14SSteve Longerbeam 	{
73919a81c14SSteve Longerbeam 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
74019a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QCIF_176_144,
74119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
74219a81c14SSteve Longerbeam 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
74319a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QVGA_320_240,
74419a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
74519a81c14SSteve Longerbeam 		{OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
74619a81c14SSteve Longerbeam 		 ov5640_setting_15fps_VGA_640_480,
74719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
74819a81c14SSteve Longerbeam 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
74919a81c14SSteve Longerbeam 		 ov5640_setting_15fps_NTSC_720_480,
75019a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
75119a81c14SSteve Longerbeam 		{OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
75219a81c14SSteve Longerbeam 		 ov5640_setting_15fps_PAL_720_576,
75319a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
75419a81c14SSteve Longerbeam 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
75519a81c14SSteve Longerbeam 		 ov5640_setting_15fps_XGA_1024_768,
75619a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
75719a81c14SSteve Longerbeam 		{OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
75819a81c14SSteve Longerbeam 		 ov5640_setting_15fps_720P_1280_720,
75919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
76019a81c14SSteve Longerbeam 		{OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
76119a81c14SSteve Longerbeam 		 ov5640_setting_15fps_1080P_1920_1080,
76219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
76319a81c14SSteve Longerbeam 		{OV5640_MODE_QSXGA_2592_1944, SCALING, 2592, 1944,
76419a81c14SSteve Longerbeam 		 ov5640_setting_15fps_QSXGA_2592_1944,
76519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
76619a81c14SSteve Longerbeam 	}, {
76719a81c14SSteve Longerbeam 		{OV5640_MODE_QCIF_176_144, SUBSAMPLING, 176, 144,
76819a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QCIF_176_144,
76919a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
77019a81c14SSteve Longerbeam 		{OV5640_MODE_QVGA_320_240, SUBSAMPLING, 320,  240,
77119a81c14SSteve Longerbeam 		 ov5640_setting_30fps_QVGA_320_240,
77219a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
77319a81c14SSteve Longerbeam 		{OV5640_MODE_VGA_640_480, SUBSAMPLING, 640,  480,
77419a81c14SSteve Longerbeam 		 ov5640_setting_30fps_VGA_640_480,
77519a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
77619a81c14SSteve Longerbeam 		{OV5640_MODE_NTSC_720_480, SUBSAMPLING, 720, 480,
77719a81c14SSteve Longerbeam 		 ov5640_setting_30fps_NTSC_720_480,
77819a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
77919a81c14SSteve Longerbeam 		{OV5640_MODE_PAL_720_576, SUBSAMPLING, 720, 576,
78019a81c14SSteve Longerbeam 		 ov5640_setting_30fps_PAL_720_576,
78119a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
78219a81c14SSteve Longerbeam 		{OV5640_MODE_XGA_1024_768, SUBSAMPLING, 1024, 768,
78319a81c14SSteve Longerbeam 		 ov5640_setting_30fps_XGA_1024_768,
78419a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
78519a81c14SSteve Longerbeam 		{OV5640_MODE_720P_1280_720, SUBSAMPLING, 1280, 720,
78619a81c14SSteve Longerbeam 		 ov5640_setting_30fps_720P_1280_720,
78719a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
78819a81c14SSteve Longerbeam 		{OV5640_MODE_1080P_1920_1080, SCALING, 1920, 1080,
78919a81c14SSteve Longerbeam 		 ov5640_setting_30fps_1080P_1920_1080,
79019a81c14SSteve Longerbeam 		 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
79119a81c14SSteve Longerbeam 		{OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, NULL, 0},
79219a81c14SSteve Longerbeam 	},
79319a81c14SSteve Longerbeam };
79419a81c14SSteve Longerbeam 
79519a81c14SSteve Longerbeam static int ov5640_init_slave_id(struct ov5640_dev *sensor)
79619a81c14SSteve Longerbeam {
79719a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
79819a81c14SSteve Longerbeam 	struct i2c_msg msg;
79919a81c14SSteve Longerbeam 	u8 buf[3];
80019a81c14SSteve Longerbeam 	int ret;
80119a81c14SSteve Longerbeam 
80219a81c14SSteve Longerbeam 	if (client->addr == OV5640_DEFAULT_SLAVE_ID)
80319a81c14SSteve Longerbeam 		return 0;
80419a81c14SSteve Longerbeam 
80519a81c14SSteve Longerbeam 	buf[0] = OV5640_REG_SLAVE_ID >> 8;
80619a81c14SSteve Longerbeam 	buf[1] = OV5640_REG_SLAVE_ID & 0xff;
80719a81c14SSteve Longerbeam 	buf[2] = client->addr << 1;
80819a81c14SSteve Longerbeam 
80919a81c14SSteve Longerbeam 	msg.addr = OV5640_DEFAULT_SLAVE_ID;
81019a81c14SSteve Longerbeam 	msg.flags = 0;
81119a81c14SSteve Longerbeam 	msg.buf = buf;
81219a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
81319a81c14SSteve Longerbeam 
81419a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
81519a81c14SSteve Longerbeam 	if (ret < 0) {
81619a81c14SSteve Longerbeam 		dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
81719a81c14SSteve Longerbeam 		return ret;
81819a81c14SSteve Longerbeam 	}
81919a81c14SSteve Longerbeam 
82019a81c14SSteve Longerbeam 	return 0;
82119a81c14SSteve Longerbeam }
82219a81c14SSteve Longerbeam 
82319a81c14SSteve Longerbeam static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
82419a81c14SSteve Longerbeam {
82519a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
82619a81c14SSteve Longerbeam 	struct i2c_msg msg;
82719a81c14SSteve Longerbeam 	u8 buf[3];
82819a81c14SSteve Longerbeam 	int ret;
82919a81c14SSteve Longerbeam 
83019a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
83119a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
83219a81c14SSteve Longerbeam 	buf[2] = val;
83319a81c14SSteve Longerbeam 
83419a81c14SSteve Longerbeam 	msg.addr = client->addr;
83519a81c14SSteve Longerbeam 	msg.flags = client->flags;
83619a81c14SSteve Longerbeam 	msg.buf = buf;
83719a81c14SSteve Longerbeam 	msg.len = sizeof(buf);
83819a81c14SSteve Longerbeam 
83919a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, &msg, 1);
84019a81c14SSteve Longerbeam 	if (ret < 0) {
8413924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
84219a81c14SSteve Longerbeam 			__func__, reg, val);
84319a81c14SSteve Longerbeam 		return ret;
84419a81c14SSteve Longerbeam 	}
84519a81c14SSteve Longerbeam 
84619a81c14SSteve Longerbeam 	return 0;
84719a81c14SSteve Longerbeam }
84819a81c14SSteve Longerbeam 
84919a81c14SSteve Longerbeam static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
85019a81c14SSteve Longerbeam {
85119a81c14SSteve Longerbeam 	struct i2c_client *client = sensor->i2c_client;
85219a81c14SSteve Longerbeam 	struct i2c_msg msg[2];
85319a81c14SSteve Longerbeam 	u8 buf[2];
85419a81c14SSteve Longerbeam 	int ret;
85519a81c14SSteve Longerbeam 
85619a81c14SSteve Longerbeam 	buf[0] = reg >> 8;
85719a81c14SSteve Longerbeam 	buf[1] = reg & 0xff;
85819a81c14SSteve Longerbeam 
85919a81c14SSteve Longerbeam 	msg[0].addr = client->addr;
86019a81c14SSteve Longerbeam 	msg[0].flags = client->flags;
86119a81c14SSteve Longerbeam 	msg[0].buf = buf;
86219a81c14SSteve Longerbeam 	msg[0].len = sizeof(buf);
86319a81c14SSteve Longerbeam 
86419a81c14SSteve Longerbeam 	msg[1].addr = client->addr;
86519a81c14SSteve Longerbeam 	msg[1].flags = client->flags | I2C_M_RD;
86619a81c14SSteve Longerbeam 	msg[1].buf = buf;
86719a81c14SSteve Longerbeam 	msg[1].len = 1;
86819a81c14SSteve Longerbeam 
86919a81c14SSteve Longerbeam 	ret = i2c_transfer(client->adapter, msg, 2);
8703924c623SHugues Fruchet 	if (ret < 0) {
8713924c623SHugues Fruchet 		dev_err(&client->dev, "%s: error: reg=%x\n",
8723924c623SHugues Fruchet 			__func__, reg);
87319a81c14SSteve Longerbeam 		return ret;
8743924c623SHugues Fruchet 	}
87519a81c14SSteve Longerbeam 
87619a81c14SSteve Longerbeam 	*val = buf[0];
87719a81c14SSteve Longerbeam 	return 0;
87819a81c14SSteve Longerbeam }
87919a81c14SSteve Longerbeam 
88019a81c14SSteve Longerbeam static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
88119a81c14SSteve Longerbeam {
88219a81c14SSteve Longerbeam 	u8 hi, lo;
88319a81c14SSteve Longerbeam 	int ret;
88419a81c14SSteve Longerbeam 
88519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &hi);
88619a81c14SSteve Longerbeam 	if (ret)
88719a81c14SSteve Longerbeam 		return ret;
88819a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg + 1, &lo);
88919a81c14SSteve Longerbeam 	if (ret)
89019a81c14SSteve Longerbeam 		return ret;
89119a81c14SSteve Longerbeam 
89219a81c14SSteve Longerbeam 	*val = ((u16)hi << 8) | (u16)lo;
89319a81c14SSteve Longerbeam 	return 0;
89419a81c14SSteve Longerbeam }
89519a81c14SSteve Longerbeam 
89619a81c14SSteve Longerbeam static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
89719a81c14SSteve Longerbeam {
89819a81c14SSteve Longerbeam 	int ret;
89919a81c14SSteve Longerbeam 
90019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, reg, val >> 8);
90119a81c14SSteve Longerbeam 	if (ret)
90219a81c14SSteve Longerbeam 		return ret;
90319a81c14SSteve Longerbeam 
90419a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg + 1, val & 0xff);
90519a81c14SSteve Longerbeam }
90619a81c14SSteve Longerbeam 
90719a81c14SSteve Longerbeam static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
90819a81c14SSteve Longerbeam 			  u8 mask, u8 val)
90919a81c14SSteve Longerbeam {
91019a81c14SSteve Longerbeam 	u8 readval;
91119a81c14SSteve Longerbeam 	int ret;
91219a81c14SSteve Longerbeam 
91319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, reg, &readval);
91419a81c14SSteve Longerbeam 	if (ret)
91519a81c14SSteve Longerbeam 		return ret;
91619a81c14SSteve Longerbeam 
91719a81c14SSteve Longerbeam 	readval &= ~mask;
91819a81c14SSteve Longerbeam 	val &= mask;
91919a81c14SSteve Longerbeam 	val |= readval;
92019a81c14SSteve Longerbeam 
92119a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, reg, val);
92219a81c14SSteve Longerbeam }
92319a81c14SSteve Longerbeam 
92419a81c14SSteve Longerbeam /* download ov5640 settings to sensor through i2c */
92519a81c14SSteve Longerbeam static int ov5640_load_regs(struct ov5640_dev *sensor,
92619a81c14SSteve Longerbeam 			    const struct ov5640_mode_info *mode)
92719a81c14SSteve Longerbeam {
92819a81c14SSteve Longerbeam 	const struct reg_value *regs = mode->reg_data;
92919a81c14SSteve Longerbeam 	unsigned int i;
93019a81c14SSteve Longerbeam 	u32 delay_ms;
93119a81c14SSteve Longerbeam 	u16 reg_addr;
93219a81c14SSteve Longerbeam 	u8 mask, val;
93319a81c14SSteve Longerbeam 	int ret = 0;
93419a81c14SSteve Longerbeam 
93519a81c14SSteve Longerbeam 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
93619a81c14SSteve Longerbeam 		delay_ms = regs->delay_ms;
93719a81c14SSteve Longerbeam 		reg_addr = regs->reg_addr;
93819a81c14SSteve Longerbeam 		val = regs->val;
93919a81c14SSteve Longerbeam 		mask = regs->mask;
94019a81c14SSteve Longerbeam 
94119a81c14SSteve Longerbeam 		if (mask)
94219a81c14SSteve Longerbeam 			ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
94319a81c14SSteve Longerbeam 		else
94419a81c14SSteve Longerbeam 			ret = ov5640_write_reg(sensor, reg_addr, val);
94519a81c14SSteve Longerbeam 		if (ret)
94619a81c14SSteve Longerbeam 			break;
94719a81c14SSteve Longerbeam 
94819a81c14SSteve Longerbeam 		if (delay_ms)
94919a81c14SSteve Longerbeam 			usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
95019a81c14SSteve Longerbeam 	}
95119a81c14SSteve Longerbeam 
95219a81c14SSteve Longerbeam 	return ret;
95319a81c14SSteve Longerbeam }
95419a81c14SSteve Longerbeam 
95519a81c14SSteve Longerbeam /* read exposure, in number of line periods */
95619a81c14SSteve Longerbeam static int ov5640_get_exposure(struct ov5640_dev *sensor)
95719a81c14SSteve Longerbeam {
95819a81c14SSteve Longerbeam 	int exp, ret;
95919a81c14SSteve Longerbeam 	u8 temp;
96019a81c14SSteve Longerbeam 
96119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
96219a81c14SSteve Longerbeam 	if (ret)
96319a81c14SSteve Longerbeam 		return ret;
96419a81c14SSteve Longerbeam 	exp = ((int)temp & 0x0f) << 16;
96519a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
96619a81c14SSteve Longerbeam 	if (ret)
96719a81c14SSteve Longerbeam 		return ret;
96819a81c14SSteve Longerbeam 	exp |= ((int)temp << 8);
96919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
97019a81c14SSteve Longerbeam 	if (ret)
97119a81c14SSteve Longerbeam 		return ret;
97219a81c14SSteve Longerbeam 	exp |= (int)temp;
97319a81c14SSteve Longerbeam 
97419a81c14SSteve Longerbeam 	return exp >> 4;
97519a81c14SSteve Longerbeam }
97619a81c14SSteve Longerbeam 
97719a81c14SSteve Longerbeam /* write exposure, given number of line periods */
97819a81c14SSteve Longerbeam static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
97919a81c14SSteve Longerbeam {
98019a81c14SSteve Longerbeam 	int ret;
98119a81c14SSteve Longerbeam 
98219a81c14SSteve Longerbeam 	exposure <<= 4;
98319a81c14SSteve Longerbeam 
98419a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
98519a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_LO,
98619a81c14SSteve Longerbeam 			       exposure & 0xff);
98719a81c14SSteve Longerbeam 	if (ret)
98819a81c14SSteve Longerbeam 		return ret;
98919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor,
99019a81c14SSteve Longerbeam 			       OV5640_REG_AEC_PK_EXPOSURE_MED,
99119a81c14SSteve Longerbeam 			       (exposure >> 8) & 0xff);
99219a81c14SSteve Longerbeam 	if (ret)
99319a81c14SSteve Longerbeam 		return ret;
99419a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor,
99519a81c14SSteve Longerbeam 				OV5640_REG_AEC_PK_EXPOSURE_HI,
99619a81c14SSteve Longerbeam 				(exposure >> 16) & 0x0f);
99719a81c14SSteve Longerbeam }
99819a81c14SSteve Longerbeam 
99919a81c14SSteve Longerbeam static int ov5640_get_gain(struct ov5640_dev *sensor)
100019a81c14SSteve Longerbeam {
100119a81c14SSteve Longerbeam 	u16 gain;
100219a81c14SSteve Longerbeam 	int ret;
100319a81c14SSteve Longerbeam 
100419a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
100519a81c14SSteve Longerbeam 	if (ret)
100619a81c14SSteve Longerbeam 		return ret;
100719a81c14SSteve Longerbeam 
100819a81c14SSteve Longerbeam 	return gain & 0x3ff;
100919a81c14SSteve Longerbeam }
101019a81c14SSteve Longerbeam 
1011f22996dbSHugues Fruchet static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1012f22996dbSHugues Fruchet {
1013f22996dbSHugues Fruchet 	int ret;
1014f22996dbSHugues Fruchet 	unsigned int flags = sensor->ep.bus.parallel.flags;
1015f22996dbSHugues Fruchet 	u8 pclk_pol = 0;
1016f22996dbSHugues Fruchet 	u8 hsync_pol = 0;
1017f22996dbSHugues Fruchet 	u8 vsync_pol = 0;
1018f22996dbSHugues Fruchet 
1019f22996dbSHugues Fruchet 	/*
1020f22996dbSHugues Fruchet 	 * Note about parallel port configuration.
1021f22996dbSHugues Fruchet 	 *
1022f22996dbSHugues Fruchet 	 * When configured in parallel mode, the OV5640 will
1023f22996dbSHugues Fruchet 	 * output 10 bits data on DVP data lines [9:0].
1024f22996dbSHugues Fruchet 	 * If only 8 bits data are wanted, the 8 bits data lines
1025f22996dbSHugues Fruchet 	 * of the camera interface must be physically connected
1026f22996dbSHugues Fruchet 	 * on the DVP data lines [9:2].
1027f22996dbSHugues Fruchet 	 *
1028f22996dbSHugues Fruchet 	 * Control lines polarity can be configured through
1029f22996dbSHugues Fruchet 	 * devicetree endpoint control lines properties.
1030f22996dbSHugues Fruchet 	 * If no endpoint control lines properties are set,
1031f22996dbSHugues Fruchet 	 * polarity will be as below:
1032f22996dbSHugues Fruchet 	 * - VSYNC:	active high
1033f22996dbSHugues Fruchet 	 * - HREF:	active low
1034f22996dbSHugues Fruchet 	 * - PCLK:	active low
1035f22996dbSHugues Fruchet 	 */
1036f22996dbSHugues Fruchet 
1037f22996dbSHugues Fruchet 	if (on) {
1038f22996dbSHugues Fruchet 		/*
1039f22996dbSHugues Fruchet 		 * reset MIPI PCLK/SERCLK divider
1040f22996dbSHugues Fruchet 		 *
1041f22996dbSHugues Fruchet 		 * SC PLL CONTRL1 0
1042f22996dbSHugues Fruchet 		 * - [3..0]:	MIPI PCLK/SERCLK divider
1043f22996dbSHugues Fruchet 		 */
1044f22996dbSHugues Fruchet 		ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1045f22996dbSHugues Fruchet 		if (ret)
1046f22996dbSHugues Fruchet 			return ret;
1047f22996dbSHugues Fruchet 
1048f22996dbSHugues Fruchet 		/*
1049f22996dbSHugues Fruchet 		 * configure parallel port control lines polarity
1050f22996dbSHugues Fruchet 		 *
1051f22996dbSHugues Fruchet 		 * POLARITY CTRL0
1052f22996dbSHugues Fruchet 		 * - [5]:	PCLK polarity (0: active low, 1: active high)
1053f22996dbSHugues Fruchet 		 * - [1]:	HREF polarity (0: active low, 1: active high)
1054f22996dbSHugues Fruchet 		 * - [0]:	VSYNC polarity (mismatch here between
1055f22996dbSHugues Fruchet 		 *		datasheet and hardware, 0 is active high
1056f22996dbSHugues Fruchet 		 *		and 1 is active low...)
1057f22996dbSHugues Fruchet 		 */
1058f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1059f22996dbSHugues Fruchet 			pclk_pol = 1;
1060f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1061f22996dbSHugues Fruchet 			hsync_pol = 1;
1062f22996dbSHugues Fruchet 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1063f22996dbSHugues Fruchet 			vsync_pol = 1;
1064f22996dbSHugues Fruchet 
1065f22996dbSHugues Fruchet 		ret = ov5640_write_reg(sensor,
1066f22996dbSHugues Fruchet 				       OV5640_REG_POLARITY_CTRL00,
1067f22996dbSHugues Fruchet 				       (pclk_pol << 5) |
1068f22996dbSHugues Fruchet 				       (hsync_pol << 1) |
1069f22996dbSHugues Fruchet 				       vsync_pol);
1070f22996dbSHugues Fruchet 
1071f22996dbSHugues Fruchet 		if (ret)
1072f22996dbSHugues Fruchet 			return ret;
1073f22996dbSHugues Fruchet 	}
1074f22996dbSHugues Fruchet 
1075f22996dbSHugues Fruchet 	/*
1076f22996dbSHugues Fruchet 	 * powerdown MIPI TX/RX PHY & disable MIPI
1077f22996dbSHugues Fruchet 	 *
1078f22996dbSHugues Fruchet 	 * MIPI CONTROL 00
1079f22996dbSHugues Fruchet 	 * 4:	 PWDN PHY TX
1080f22996dbSHugues Fruchet 	 * 3:	 PWDN PHY RX
1081f22996dbSHugues Fruchet 	 * 2:	 MIPI enable
1082f22996dbSHugues Fruchet 	 */
1083f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1084f22996dbSHugues Fruchet 			       OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1085f22996dbSHugues Fruchet 	if (ret)
1086f22996dbSHugues Fruchet 		return ret;
1087f22996dbSHugues Fruchet 
1088f22996dbSHugues Fruchet 	/*
1089f22996dbSHugues Fruchet 	 * enable VSYNC/HREF/PCLK DVP control lines
1090f22996dbSHugues Fruchet 	 * & D[9:6] DVP data lines
1091f22996dbSHugues Fruchet 	 *
1092f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 01
1093f22996dbSHugues Fruchet 	 * - 6:		VSYNC output enable
1094f22996dbSHugues Fruchet 	 * - 5:		HREF output enable
1095f22996dbSHugues Fruchet 	 * - 4:		PCLK output enable
1096f22996dbSHugues Fruchet 	 * - [3:0]:	D[9:6] output enable
1097f22996dbSHugues Fruchet 	 */
1098f22996dbSHugues Fruchet 	ret = ov5640_write_reg(sensor,
1099f22996dbSHugues Fruchet 			       OV5640_REG_PAD_OUTPUT_ENABLE01,
1100f22996dbSHugues Fruchet 			       on ? 0x7f : 0);
1101f22996dbSHugues Fruchet 	if (ret)
1102f22996dbSHugues Fruchet 		return ret;
1103f22996dbSHugues Fruchet 
1104f22996dbSHugues Fruchet 	/*
1105f22996dbSHugues Fruchet 	 * enable D[5:0] DVP data lines
1106f22996dbSHugues Fruchet 	 *
1107f22996dbSHugues Fruchet 	 * PAD OUTPUT ENABLE 02
1108f22996dbSHugues Fruchet 	 * - [7:2]:	D[5:0] output enable
1109f22996dbSHugues Fruchet 	 */
1110f22996dbSHugues Fruchet 	return ov5640_write_reg(sensor,
1111f22996dbSHugues Fruchet 				OV5640_REG_PAD_OUTPUT_ENABLE02,
1112f22996dbSHugues Fruchet 				on ? 0xfc : 0);
1113f22996dbSHugues Fruchet }
1114f22996dbSHugues Fruchet 
1115f22996dbSHugues Fruchet static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
111619a81c14SSteve Longerbeam {
111719a81c14SSteve Longerbeam 	int ret;
111819a81c14SSteve Longerbeam 
111919a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
112019a81c14SSteve Longerbeam 			     on ? 0 : BIT(5));
112119a81c14SSteve Longerbeam 	if (ret)
112219a81c14SSteve Longerbeam 		return ret;
112319a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
112419a81c14SSteve Longerbeam 			       on ? 0x00 : 0x70);
112519a81c14SSteve Longerbeam 	if (ret)
112619a81c14SSteve Longerbeam 		return ret;
112719a81c14SSteve Longerbeam 
112819a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
112919a81c14SSteve Longerbeam 				on ? 0x00 : 0x0f);
113019a81c14SSteve Longerbeam }
113119a81c14SSteve Longerbeam 
113219a81c14SSteve Longerbeam static int ov5640_get_sysclk(struct ov5640_dev *sensor)
113319a81c14SSteve Longerbeam {
113419a81c14SSteve Longerbeam 	 /* calculate sysclk */
113519a81c14SSteve Longerbeam 	u32 xvclk = sensor->xclk_freq / 10000;
113619a81c14SSteve Longerbeam 	u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
113719a81c14SSteve Longerbeam 	u32 sclk_rdiv_map[] = {1, 2, 4, 8};
113819a81c14SSteve Longerbeam 	u32 bit_div2x = 1, sclk_rdiv, sysclk;
113919a81c14SSteve Longerbeam 	u8 temp1, temp2;
114019a81c14SSteve Longerbeam 	int ret;
114119a81c14SSteve Longerbeam 
114219a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
114319a81c14SSteve Longerbeam 	if (ret)
114419a81c14SSteve Longerbeam 		return ret;
114519a81c14SSteve Longerbeam 	temp2 = temp1 & 0x0f;
114619a81c14SSteve Longerbeam 	if (temp2 == 8 || temp2 == 10)
114719a81c14SSteve Longerbeam 		bit_div2x = temp2 / 2;
114819a81c14SSteve Longerbeam 
114919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
115019a81c14SSteve Longerbeam 	if (ret)
115119a81c14SSteve Longerbeam 		return ret;
115219a81c14SSteve Longerbeam 	sysdiv = temp1 >> 4;
115319a81c14SSteve Longerbeam 	if (sysdiv == 0)
115419a81c14SSteve Longerbeam 		sysdiv = 16;
115519a81c14SSteve Longerbeam 
115619a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
115719a81c14SSteve Longerbeam 	if (ret)
115819a81c14SSteve Longerbeam 		return ret;
115919a81c14SSteve Longerbeam 	multiplier = temp1;
116019a81c14SSteve Longerbeam 
116119a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
116219a81c14SSteve Longerbeam 	if (ret)
116319a81c14SSteve Longerbeam 		return ret;
116419a81c14SSteve Longerbeam 	prediv = temp1 & 0x0f;
116519a81c14SSteve Longerbeam 	pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
116619a81c14SSteve Longerbeam 
116719a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
116819a81c14SSteve Longerbeam 	if (ret)
116919a81c14SSteve Longerbeam 		return ret;
117019a81c14SSteve Longerbeam 	temp2 = temp1 & 0x03;
117119a81c14SSteve Longerbeam 	sclk_rdiv = sclk_rdiv_map[temp2];
117219a81c14SSteve Longerbeam 
117319a81c14SSteve Longerbeam 	if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
117419a81c14SSteve Longerbeam 		return -EINVAL;
117519a81c14SSteve Longerbeam 
117619a81c14SSteve Longerbeam 	VCO = xvclk * multiplier / prediv;
117719a81c14SSteve Longerbeam 
117819a81c14SSteve Longerbeam 	sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
117919a81c14SSteve Longerbeam 
118019a81c14SSteve Longerbeam 	return sysclk;
118119a81c14SSteve Longerbeam }
118219a81c14SSteve Longerbeam 
118319a81c14SSteve Longerbeam static int ov5640_set_night_mode(struct ov5640_dev *sensor)
118419a81c14SSteve Longerbeam {
118519a81c14SSteve Longerbeam 	 /* read HTS from register settings */
118619a81c14SSteve Longerbeam 	u8 mode;
118719a81c14SSteve Longerbeam 	int ret;
118819a81c14SSteve Longerbeam 
118919a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
119019a81c14SSteve Longerbeam 	if (ret)
119119a81c14SSteve Longerbeam 		return ret;
119219a81c14SSteve Longerbeam 	mode &= 0xfb;
119319a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
119419a81c14SSteve Longerbeam }
119519a81c14SSteve Longerbeam 
119619a81c14SSteve Longerbeam static int ov5640_get_hts(struct ov5640_dev *sensor)
119719a81c14SSteve Longerbeam {
119819a81c14SSteve Longerbeam 	/* read HTS from register settings */
119919a81c14SSteve Longerbeam 	u16 hts;
120019a81c14SSteve Longerbeam 	int ret;
120119a81c14SSteve Longerbeam 
120219a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
120319a81c14SSteve Longerbeam 	if (ret)
120419a81c14SSteve Longerbeam 		return ret;
120519a81c14SSteve Longerbeam 	return hts;
120619a81c14SSteve Longerbeam }
120719a81c14SSteve Longerbeam 
120819a81c14SSteve Longerbeam static int ov5640_get_vts(struct ov5640_dev *sensor)
120919a81c14SSteve Longerbeam {
121019a81c14SSteve Longerbeam 	u16 vts;
121119a81c14SSteve Longerbeam 	int ret;
121219a81c14SSteve Longerbeam 
121319a81c14SSteve Longerbeam 	ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
121419a81c14SSteve Longerbeam 	if (ret)
121519a81c14SSteve Longerbeam 		return ret;
121619a81c14SSteve Longerbeam 	return vts;
121719a81c14SSteve Longerbeam }
121819a81c14SSteve Longerbeam 
121919a81c14SSteve Longerbeam static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
122019a81c14SSteve Longerbeam {
122119a81c14SSteve Longerbeam 	return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
122219a81c14SSteve Longerbeam }
122319a81c14SSteve Longerbeam 
122419a81c14SSteve Longerbeam static int ov5640_get_light_freq(struct ov5640_dev *sensor)
122519a81c14SSteve Longerbeam {
122619a81c14SSteve Longerbeam 	/* get banding filter value */
122719a81c14SSteve Longerbeam 	int ret, light_freq = 0;
122819a81c14SSteve Longerbeam 	u8 temp, temp1;
122919a81c14SSteve Longerbeam 
123019a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
123119a81c14SSteve Longerbeam 	if (ret)
123219a81c14SSteve Longerbeam 		return ret;
123319a81c14SSteve Longerbeam 
123419a81c14SSteve Longerbeam 	if (temp & 0x80) {
123519a81c14SSteve Longerbeam 		/* manual */
123619a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
123719a81c14SSteve Longerbeam 				      &temp1);
123819a81c14SSteve Longerbeam 		if (ret)
123919a81c14SSteve Longerbeam 			return ret;
124019a81c14SSteve Longerbeam 		if (temp1 & 0x04) {
124119a81c14SSteve Longerbeam 			/* 50Hz */
124219a81c14SSteve Longerbeam 			light_freq = 50;
124319a81c14SSteve Longerbeam 		} else {
124419a81c14SSteve Longerbeam 			/* 60Hz */
124519a81c14SSteve Longerbeam 			light_freq = 60;
124619a81c14SSteve Longerbeam 		}
124719a81c14SSteve Longerbeam 	} else {
124819a81c14SSteve Longerbeam 		/* auto */
124919a81c14SSteve Longerbeam 		ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
125019a81c14SSteve Longerbeam 				      &temp1);
125119a81c14SSteve Longerbeam 		if (ret)
125219a81c14SSteve Longerbeam 			return ret;
125319a81c14SSteve Longerbeam 
125419a81c14SSteve Longerbeam 		if (temp1 & 0x01) {
125519a81c14SSteve Longerbeam 			/* 50Hz */
125619a81c14SSteve Longerbeam 			light_freq = 50;
125719a81c14SSteve Longerbeam 		} else {
125819a81c14SSteve Longerbeam 			/* 60Hz */
125919a81c14SSteve Longerbeam 		}
126019a81c14SSteve Longerbeam 	}
126119a81c14SSteve Longerbeam 
126219a81c14SSteve Longerbeam 	return light_freq;
126319a81c14SSteve Longerbeam }
126419a81c14SSteve Longerbeam 
126519a81c14SSteve Longerbeam static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
126619a81c14SSteve Longerbeam {
126719a81c14SSteve Longerbeam 	u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
126819a81c14SSteve Longerbeam 	int ret;
126919a81c14SSteve Longerbeam 
127019a81c14SSteve Longerbeam 	/* read preview PCLK */
127119a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
127219a81c14SSteve Longerbeam 	if (ret < 0)
127319a81c14SSteve Longerbeam 		return ret;
127419a81c14SSteve Longerbeam 	if (ret == 0)
127519a81c14SSteve Longerbeam 		return -EINVAL;
127619a81c14SSteve Longerbeam 	sensor->prev_sysclk = ret;
127719a81c14SSteve Longerbeam 	/* read preview HTS */
127819a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
127919a81c14SSteve Longerbeam 	if (ret < 0)
128019a81c14SSteve Longerbeam 		return ret;
128119a81c14SSteve Longerbeam 	if (ret == 0)
128219a81c14SSteve Longerbeam 		return -EINVAL;
128319a81c14SSteve Longerbeam 	sensor->prev_hts = ret;
128419a81c14SSteve Longerbeam 
128519a81c14SSteve Longerbeam 	/* read preview VTS */
128619a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
128719a81c14SSteve Longerbeam 	if (ret < 0)
128819a81c14SSteve Longerbeam 		return ret;
128919a81c14SSteve Longerbeam 	prev_vts = ret;
129019a81c14SSteve Longerbeam 
129119a81c14SSteve Longerbeam 	/* calculate banding filter */
129219a81c14SSteve Longerbeam 	/* 60Hz */
129319a81c14SSteve Longerbeam 	band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
129419a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
129519a81c14SSteve Longerbeam 	if (ret)
129619a81c14SSteve Longerbeam 		return ret;
129719a81c14SSteve Longerbeam 	if (!band_step60)
129819a81c14SSteve Longerbeam 		return -EINVAL;
129919a81c14SSteve Longerbeam 	max_band60 = (int)((prev_vts - 4) / band_step60);
130019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
130119a81c14SSteve Longerbeam 	if (ret)
130219a81c14SSteve Longerbeam 		return ret;
130319a81c14SSteve Longerbeam 
130419a81c14SSteve Longerbeam 	/* 50Hz */
130519a81c14SSteve Longerbeam 	band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
130619a81c14SSteve Longerbeam 	ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
130719a81c14SSteve Longerbeam 	if (ret)
130819a81c14SSteve Longerbeam 		return ret;
130919a81c14SSteve Longerbeam 	if (!band_step50)
131019a81c14SSteve Longerbeam 		return -EINVAL;
131119a81c14SSteve Longerbeam 	max_band50 = (int)((prev_vts - 4) / band_step50);
131219a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
131319a81c14SSteve Longerbeam }
131419a81c14SSteve Longerbeam 
131519a81c14SSteve Longerbeam static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
131619a81c14SSteve Longerbeam {
131719a81c14SSteve Longerbeam 	/* stable in high */
131819a81c14SSteve Longerbeam 	u32 fast_high, fast_low;
131919a81c14SSteve Longerbeam 	int ret;
132019a81c14SSteve Longerbeam 
132119a81c14SSteve Longerbeam 	sensor->ae_low = target * 23 / 25;	/* 0.92 */
132219a81c14SSteve Longerbeam 	sensor->ae_high = target * 27 / 25;	/* 1.08 */
132319a81c14SSteve Longerbeam 
132419a81c14SSteve Longerbeam 	fast_high = sensor->ae_high << 1;
132519a81c14SSteve Longerbeam 	if (fast_high > 255)
132619a81c14SSteve Longerbeam 		fast_high = 255;
132719a81c14SSteve Longerbeam 
132819a81c14SSteve Longerbeam 	fast_low = sensor->ae_low >> 1;
132919a81c14SSteve Longerbeam 
133019a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
133119a81c14SSteve Longerbeam 	if (ret)
133219a81c14SSteve Longerbeam 		return ret;
133319a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
133419a81c14SSteve Longerbeam 	if (ret)
133519a81c14SSteve Longerbeam 		return ret;
133619a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
133719a81c14SSteve Longerbeam 	if (ret)
133819a81c14SSteve Longerbeam 		return ret;
133919a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
134019a81c14SSteve Longerbeam 	if (ret)
134119a81c14SSteve Longerbeam 		return ret;
134219a81c14SSteve Longerbeam 	ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
134319a81c14SSteve Longerbeam 	if (ret)
134419a81c14SSteve Longerbeam 		return ret;
134519a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
134619a81c14SSteve Longerbeam }
134719a81c14SSteve Longerbeam 
134819a81c14SSteve Longerbeam static int ov5640_binning_on(struct ov5640_dev *sensor)
134919a81c14SSteve Longerbeam {
135019a81c14SSteve Longerbeam 	u8 temp;
135119a81c14SSteve Longerbeam 	int ret;
135219a81c14SSteve Longerbeam 
135319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
135419a81c14SSteve Longerbeam 	if (ret)
135519a81c14SSteve Longerbeam 		return ret;
135619a81c14SSteve Longerbeam 	temp &= 0xfe;
135719a81c14SSteve Longerbeam 	return temp ? 1 : 0;
135819a81c14SSteve Longerbeam }
135919a81c14SSteve Longerbeam 
136019a81c14SSteve Longerbeam static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
136119a81c14SSteve Longerbeam {
13628670d70aSHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
136319a81c14SSteve Longerbeam 	u8 temp, channel = virtual_channel;
136419a81c14SSteve Longerbeam 	int ret;
136519a81c14SSteve Longerbeam 
13668670d70aSHugues Fruchet 	if (channel > 3) {
13678670d70aSHugues Fruchet 		dev_err(&client->dev,
13688670d70aSHugues Fruchet 			"%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
13698670d70aSHugues Fruchet 			__func__, channel);
137019a81c14SSteve Longerbeam 		return -EINVAL;
13718670d70aSHugues Fruchet 	}
137219a81c14SSteve Longerbeam 
137319a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
137419a81c14SSteve Longerbeam 	if (ret)
137519a81c14SSteve Longerbeam 		return ret;
137619a81c14SSteve Longerbeam 	temp &= ~(3 << 6);
137719a81c14SSteve Longerbeam 	temp |= (channel << 6);
137819a81c14SSteve Longerbeam 	return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
137919a81c14SSteve Longerbeam }
138019a81c14SSteve Longerbeam 
138119a81c14SSteve Longerbeam static const struct ov5640_mode_info *
138219a81c14SSteve Longerbeam ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
138319a81c14SSteve Longerbeam 		 int width, int height, bool nearest)
138419a81c14SSteve Longerbeam {
138519a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = NULL;
138619a81c14SSteve Longerbeam 	int i;
138719a81c14SSteve Longerbeam 
138819a81c14SSteve Longerbeam 	for (i = OV5640_NUM_MODES - 1; i >= 0; i--) {
138919a81c14SSteve Longerbeam 		mode = &ov5640_mode_data[fr][i];
139019a81c14SSteve Longerbeam 
139119a81c14SSteve Longerbeam 		if (!mode->reg_data)
139219a81c14SSteve Longerbeam 			continue;
139319a81c14SSteve Longerbeam 
139419a81c14SSteve Longerbeam 		if ((nearest && mode->width <= width &&
139519a81c14SSteve Longerbeam 		     mode->height <= height) ||
139619a81c14SSteve Longerbeam 		    (!nearest && mode->width == width &&
139719a81c14SSteve Longerbeam 		     mode->height == height))
139819a81c14SSteve Longerbeam 			break;
139919a81c14SSteve Longerbeam 	}
140019a81c14SSteve Longerbeam 
140119a81c14SSteve Longerbeam 	if (nearest && i < 0)
140219a81c14SSteve Longerbeam 		mode = &ov5640_mode_data[fr][0];
140319a81c14SSteve Longerbeam 
140419a81c14SSteve Longerbeam 	return mode;
140519a81c14SSteve Longerbeam }
140619a81c14SSteve Longerbeam 
140719a81c14SSteve Longerbeam /*
140819a81c14SSteve Longerbeam  * sensor changes between scaling and subsampling, go through
140919a81c14SSteve Longerbeam  * exposure calculation
141019a81c14SSteve Longerbeam  */
141141d8d7f5SHugues Fruchet static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
141241d8d7f5SHugues Fruchet 					 const struct ov5640_mode_info *mode)
141319a81c14SSteve Longerbeam {
141419a81c14SSteve Longerbeam 	u32 prev_shutter, prev_gain16;
141519a81c14SSteve Longerbeam 	u32 cap_shutter, cap_gain16;
141619a81c14SSteve Longerbeam 	u32 cap_sysclk, cap_hts, cap_vts;
141719a81c14SSteve Longerbeam 	u32 light_freq, cap_bandfilt, cap_maxband;
141819a81c14SSteve Longerbeam 	u32 cap_gain16_shutter;
141919a81c14SSteve Longerbeam 	u8 average;
142019a81c14SSteve Longerbeam 	int ret;
142119a81c14SSteve Longerbeam 
142241d8d7f5SHugues Fruchet 	if (!mode->reg_data)
142319a81c14SSteve Longerbeam 		return -EINVAL;
142419a81c14SSteve Longerbeam 
142519a81c14SSteve Longerbeam 	/* read preview shutter */
142619a81c14SSteve Longerbeam 	ret = ov5640_get_exposure(sensor);
142719a81c14SSteve Longerbeam 	if (ret < 0)
142819a81c14SSteve Longerbeam 		return ret;
142919a81c14SSteve Longerbeam 	prev_shutter = ret;
143019a81c14SSteve Longerbeam 	ret = ov5640_binning_on(sensor);
143119a81c14SSteve Longerbeam 	if (ret < 0)
143219a81c14SSteve Longerbeam 		return ret;
143319a81c14SSteve Longerbeam 	if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
143419a81c14SSteve Longerbeam 	    mode->id != OV5640_MODE_1080P_1920_1080)
143519a81c14SSteve Longerbeam 		prev_shutter *= 2;
143619a81c14SSteve Longerbeam 
143719a81c14SSteve Longerbeam 	/* read preview gain */
143819a81c14SSteve Longerbeam 	ret = ov5640_get_gain(sensor);
143919a81c14SSteve Longerbeam 	if (ret < 0)
144019a81c14SSteve Longerbeam 		return ret;
144119a81c14SSteve Longerbeam 	prev_gain16 = ret;
144219a81c14SSteve Longerbeam 
144319a81c14SSteve Longerbeam 	/* get average */
144419a81c14SSteve Longerbeam 	ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
144519a81c14SSteve Longerbeam 	if (ret)
144619a81c14SSteve Longerbeam 		return ret;
144719a81c14SSteve Longerbeam 
144819a81c14SSteve Longerbeam 	/* turn off night mode for capture */
144919a81c14SSteve Longerbeam 	ret = ov5640_set_night_mode(sensor);
145019a81c14SSteve Longerbeam 	if (ret < 0)
145119a81c14SSteve Longerbeam 		return ret;
145219a81c14SSteve Longerbeam 
145319a81c14SSteve Longerbeam 	/* Write capture setting */
145419a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
145519a81c14SSteve Longerbeam 	if (ret < 0)
145619a81c14SSteve Longerbeam 		return ret;
145719a81c14SSteve Longerbeam 
145819a81c14SSteve Longerbeam 	/* read capture VTS */
145919a81c14SSteve Longerbeam 	ret = ov5640_get_vts(sensor);
146019a81c14SSteve Longerbeam 	if (ret < 0)
146119a81c14SSteve Longerbeam 		return ret;
146219a81c14SSteve Longerbeam 	cap_vts = ret;
146319a81c14SSteve Longerbeam 	ret = ov5640_get_hts(sensor);
146419a81c14SSteve Longerbeam 	if (ret < 0)
146519a81c14SSteve Longerbeam 		return ret;
146619a81c14SSteve Longerbeam 	if (ret == 0)
146719a81c14SSteve Longerbeam 		return -EINVAL;
146819a81c14SSteve Longerbeam 	cap_hts = ret;
146919a81c14SSteve Longerbeam 
147019a81c14SSteve Longerbeam 	ret = ov5640_get_sysclk(sensor);
147119a81c14SSteve Longerbeam 	if (ret < 0)
147219a81c14SSteve Longerbeam 		return ret;
147319a81c14SSteve Longerbeam 	if (ret == 0)
147419a81c14SSteve Longerbeam 		return -EINVAL;
147519a81c14SSteve Longerbeam 	cap_sysclk = ret;
147619a81c14SSteve Longerbeam 
147719a81c14SSteve Longerbeam 	/* calculate capture banding filter */
147819a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
147919a81c14SSteve Longerbeam 	if (ret < 0)
148019a81c14SSteve Longerbeam 		return ret;
148119a81c14SSteve Longerbeam 	light_freq = ret;
148219a81c14SSteve Longerbeam 
148319a81c14SSteve Longerbeam 	if (light_freq == 60) {
148419a81c14SSteve Longerbeam 		/* 60Hz */
148519a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
148619a81c14SSteve Longerbeam 	} else {
148719a81c14SSteve Longerbeam 		/* 50Hz */
148819a81c14SSteve Longerbeam 		cap_bandfilt = cap_sysclk * 100 / cap_hts;
148919a81c14SSteve Longerbeam 	}
149019a81c14SSteve Longerbeam 
149119a81c14SSteve Longerbeam 	if (!sensor->prev_sysclk) {
149219a81c14SSteve Longerbeam 		ret = ov5640_get_sysclk(sensor);
149319a81c14SSteve Longerbeam 		if (ret < 0)
149419a81c14SSteve Longerbeam 			return ret;
149519a81c14SSteve Longerbeam 		if (ret == 0)
149619a81c14SSteve Longerbeam 			return -EINVAL;
149719a81c14SSteve Longerbeam 		sensor->prev_sysclk = ret;
149819a81c14SSteve Longerbeam 	}
149919a81c14SSteve Longerbeam 
150019a81c14SSteve Longerbeam 	if (!cap_bandfilt)
150119a81c14SSteve Longerbeam 		return -EINVAL;
150219a81c14SSteve Longerbeam 
150319a81c14SSteve Longerbeam 	cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
150419a81c14SSteve Longerbeam 
150519a81c14SSteve Longerbeam 	/* calculate capture shutter/gain16 */
150619a81c14SSteve Longerbeam 	if (average > sensor->ae_low && average < sensor->ae_high) {
150719a81c14SSteve Longerbeam 		/* in stable range */
150819a81c14SSteve Longerbeam 		cap_gain16_shutter =
150919a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
151019a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
151119a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts *
151219a81c14SSteve Longerbeam 			sensor->ae_target / average;
151319a81c14SSteve Longerbeam 	} else {
151419a81c14SSteve Longerbeam 		cap_gain16_shutter =
151519a81c14SSteve Longerbeam 			prev_gain16 * prev_shutter *
151619a81c14SSteve Longerbeam 			cap_sysclk / sensor->prev_sysclk *
151719a81c14SSteve Longerbeam 			sensor->prev_hts / cap_hts;
151819a81c14SSteve Longerbeam 	}
151919a81c14SSteve Longerbeam 
152019a81c14SSteve Longerbeam 	/* gain to shutter */
152119a81c14SSteve Longerbeam 	if (cap_gain16_shutter < (cap_bandfilt * 16)) {
152219a81c14SSteve Longerbeam 		/* shutter < 1/100 */
152319a81c14SSteve Longerbeam 		cap_shutter = cap_gain16_shutter / 16;
152419a81c14SSteve Longerbeam 		if (cap_shutter < 1)
152519a81c14SSteve Longerbeam 			cap_shutter = 1;
152619a81c14SSteve Longerbeam 
152719a81c14SSteve Longerbeam 		cap_gain16 = cap_gain16_shutter / cap_shutter;
152819a81c14SSteve Longerbeam 		if (cap_gain16 < 16)
152919a81c14SSteve Longerbeam 			cap_gain16 = 16;
153019a81c14SSteve Longerbeam 	} else {
153119a81c14SSteve Longerbeam 		if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
153219a81c14SSteve Longerbeam 			/* exposure reach max */
153319a81c14SSteve Longerbeam 			cap_shutter = cap_bandfilt * cap_maxband;
153419a81c14SSteve Longerbeam 			if (!cap_shutter)
153519a81c14SSteve Longerbeam 				return -EINVAL;
153619a81c14SSteve Longerbeam 
153719a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
153819a81c14SSteve Longerbeam 		} else {
153919a81c14SSteve Longerbeam 			/* 1/100 < (cap_shutter = n/100) =< max */
154019a81c14SSteve Longerbeam 			cap_shutter =
154119a81c14SSteve Longerbeam 				((int)(cap_gain16_shutter / 16 / cap_bandfilt))
154219a81c14SSteve Longerbeam 				* cap_bandfilt;
154319a81c14SSteve Longerbeam 			if (!cap_shutter)
154419a81c14SSteve Longerbeam 				return -EINVAL;
154519a81c14SSteve Longerbeam 
154619a81c14SSteve Longerbeam 			cap_gain16 = cap_gain16_shutter / cap_shutter;
154719a81c14SSteve Longerbeam 		}
154819a81c14SSteve Longerbeam 	}
154919a81c14SSteve Longerbeam 
155019a81c14SSteve Longerbeam 	/* set capture gain */
155119a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
155219a81c14SSteve Longerbeam 	if (ret)
155319a81c14SSteve Longerbeam 		return ret;
155419a81c14SSteve Longerbeam 
155519a81c14SSteve Longerbeam 	/* write capture shutter */
155619a81c14SSteve Longerbeam 	if (cap_shutter > (cap_vts - 4)) {
155719a81c14SSteve Longerbeam 		cap_vts = cap_shutter + 4;
155819a81c14SSteve Longerbeam 		ret = ov5640_set_vts(sensor, cap_vts);
155919a81c14SSteve Longerbeam 		if (ret < 0)
156019a81c14SSteve Longerbeam 			return ret;
156119a81c14SSteve Longerbeam 	}
156219a81c14SSteve Longerbeam 
156319a81c14SSteve Longerbeam 	/* set exposure */
156419a81c14SSteve Longerbeam 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
156519a81c14SSteve Longerbeam }
156619a81c14SSteve Longerbeam 
156719a81c14SSteve Longerbeam /*
156819a81c14SSteve Longerbeam  * if sensor changes inside scaling or subsampling
156919a81c14SSteve Longerbeam  * change mode directly
157019a81c14SSteve Longerbeam  */
157119a81c14SSteve Longerbeam static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1572bf4a4b51SMaxime Ripard 				  const struct ov5640_mode_info *mode,
1573bf4a4b51SMaxime Ripard 				  s32 exposure)
157419a81c14SSteve Longerbeam {
157519a81c14SSteve Longerbeam 	int ret;
157619a81c14SSteve Longerbeam 
157741d8d7f5SHugues Fruchet 	if (!mode->reg_data)
157819a81c14SSteve Longerbeam 		return -EINVAL;
157919a81c14SSteve Longerbeam 
158019a81c14SSteve Longerbeam 	/* Write capture setting */
158119a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, mode);
158219a81c14SSteve Longerbeam 	if (ret < 0)
158319a81c14SSteve Longerbeam 		return ret;
158419a81c14SSteve Longerbeam 
158519a81c14SSteve Longerbeam 	/* turn auto gain/exposure back on for direct mode */
158619a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
158719a81c14SSteve Longerbeam 	if (ret)
158819a81c14SSteve Longerbeam 		return ret;
1589bf4a4b51SMaxime Ripard 
1590bf4a4b51SMaxime Ripard 	return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, exposure);
159119a81c14SSteve Longerbeam }
159219a81c14SSteve Longerbeam 
159319a81c14SSteve Longerbeam static int ov5640_set_mode(struct ov5640_dev *sensor,
159419a81c14SSteve Longerbeam 			   const struct ov5640_mode_info *orig_mode)
159519a81c14SSteve Longerbeam {
159619a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode = sensor->current_mode;
159719a81c14SSteve Longerbeam 	enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1598bf4a4b51SMaxime Ripard 	s32 exposure;
159919a81c14SSteve Longerbeam 	int ret;
160019a81c14SSteve Longerbeam 
160119a81c14SSteve Longerbeam 	dn_mode = mode->dn_mode;
160219a81c14SSteve Longerbeam 	orig_dn_mode = orig_mode->dn_mode;
160319a81c14SSteve Longerbeam 
160419a81c14SSteve Longerbeam 	/* auto gain and exposure must be turned off when changing modes */
160519a81c14SSteve Longerbeam 	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
160619a81c14SSteve Longerbeam 	if (ret)
160719a81c14SSteve Longerbeam 		return ret;
1608bf4a4b51SMaxime Ripard 
1609bf4a4b51SMaxime Ripard 	exposure = sensor->ctrls.auto_exp->val;
1610bf4a4b51SMaxime Ripard 	ret = ov5640_set_exposure(sensor, V4L2_EXPOSURE_MANUAL);
161119a81c14SSteve Longerbeam 	if (ret)
161219a81c14SSteve Longerbeam 		return ret;
161319a81c14SSteve Longerbeam 
161419a81c14SSteve Longerbeam 	if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
161519a81c14SSteve Longerbeam 	    (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
161619a81c14SSteve Longerbeam 		/*
161719a81c14SSteve Longerbeam 		 * change between subsampling and scaling
161819a81c14SSteve Longerbeam 		 * go through exposure calucation
161919a81c14SSteve Longerbeam 		 */
162019a81c14SSteve Longerbeam 		ret = ov5640_set_mode_exposure_calc(sensor, mode);
162119a81c14SSteve Longerbeam 	} else {
162219a81c14SSteve Longerbeam 		/*
162319a81c14SSteve Longerbeam 		 * change inside subsampling or scaling
162419a81c14SSteve Longerbeam 		 * download firmware directly
162519a81c14SSteve Longerbeam 		 */
1626bf4a4b51SMaxime Ripard 		ret = ov5640_set_mode_direct(sensor, mode, exposure);
162719a81c14SSteve Longerbeam 	}
162819a81c14SSteve Longerbeam 
162919a81c14SSteve Longerbeam 	if (ret < 0)
163019a81c14SSteve Longerbeam 		return ret;
163119a81c14SSteve Longerbeam 
163219a81c14SSteve Longerbeam 	ret = ov5640_set_ae_target(sensor, sensor->ae_target);
163319a81c14SSteve Longerbeam 	if (ret < 0)
163419a81c14SSteve Longerbeam 		return ret;
163519a81c14SSteve Longerbeam 	ret = ov5640_get_light_freq(sensor);
163619a81c14SSteve Longerbeam 	if (ret < 0)
163719a81c14SSteve Longerbeam 		return ret;
163819a81c14SSteve Longerbeam 	ret = ov5640_set_bandingfilter(sensor);
163919a81c14SSteve Longerbeam 	if (ret < 0)
164019a81c14SSteve Longerbeam 		return ret;
164119a81c14SSteve Longerbeam 	ret = ov5640_set_virtual_channel(sensor);
164219a81c14SSteve Longerbeam 	if (ret < 0)
164319a81c14SSteve Longerbeam 		return ret;
164419a81c14SSteve Longerbeam 
164519a81c14SSteve Longerbeam 	sensor->pending_mode_change = false;
164619a81c14SSteve Longerbeam 
164719a81c14SSteve Longerbeam 	return 0;
164819a81c14SSteve Longerbeam }
164919a81c14SSteve Longerbeam 
165019ad26f9SAkinobu Mita static int ov5640_set_framefmt(struct ov5640_dev *sensor,
165119ad26f9SAkinobu Mita 			       struct v4l2_mbus_framefmt *format);
165219ad26f9SAkinobu Mita 
165319a81c14SSteve Longerbeam /* restore the last set video mode after chip power-on */
165419a81c14SSteve Longerbeam static int ov5640_restore_mode(struct ov5640_dev *sensor)
165519a81c14SSteve Longerbeam {
165619a81c14SSteve Longerbeam 	int ret;
165719a81c14SSteve Longerbeam 
165819a81c14SSteve Longerbeam 	/* first load the initial register values */
165919a81c14SSteve Longerbeam 	ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
166019a81c14SSteve Longerbeam 	if (ret < 0)
166119a81c14SSteve Longerbeam 		return ret;
166219a81c14SSteve Longerbeam 
166319a81c14SSteve Longerbeam 	/* now restore the last capture mode */
166419ad26f9SAkinobu Mita 	ret = ov5640_set_mode(sensor, &ov5640_mode_init_data);
166519ad26f9SAkinobu Mita 	if (ret < 0)
166619ad26f9SAkinobu Mita 		return ret;
166719ad26f9SAkinobu Mita 
166819ad26f9SAkinobu Mita 	return ov5640_set_framefmt(sensor, &sensor->fmt);
166919a81c14SSteve Longerbeam }
167019a81c14SSteve Longerbeam 
167119a81c14SSteve Longerbeam static void ov5640_power(struct ov5640_dev *sensor, bool enable)
167219a81c14SSteve Longerbeam {
16731fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
167419a81c14SSteve Longerbeam }
167519a81c14SSteve Longerbeam 
167619a81c14SSteve Longerbeam static void ov5640_reset(struct ov5640_dev *sensor)
167719a81c14SSteve Longerbeam {
167819a81c14SSteve Longerbeam 	if (!sensor->reset_gpio)
167919a81c14SSteve Longerbeam 		return;
168019a81c14SSteve Longerbeam 
16811fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
168219a81c14SSteve Longerbeam 
168319a81c14SSteve Longerbeam 	/* camera power cycle */
168419a81c14SSteve Longerbeam 	ov5640_power(sensor, false);
168519a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
168619a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
168719a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
168819a81c14SSteve Longerbeam 
16891fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 1);
169019a81c14SSteve Longerbeam 	usleep_range(1000, 2000);
169119a81c14SSteve Longerbeam 
16921fddc5daSHugues Fruchet 	gpiod_set_value_cansleep(sensor->reset_gpio, 0);
169319a81c14SSteve Longerbeam 	usleep_range(5000, 10000);
169419a81c14SSteve Longerbeam }
169519a81c14SSteve Longerbeam 
16960f7acb52SHugues Fruchet static int ov5640_set_power_on(struct ov5640_dev *sensor)
169719a81c14SSteve Longerbeam {
16980f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
16990f7acb52SHugues Fruchet 	int ret;
170019a81c14SSteve Longerbeam 
17010f7acb52SHugues Fruchet 	ret = clk_prepare_enable(sensor->xclk);
17020f7acb52SHugues Fruchet 	if (ret) {
17030f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable clock\n",
17040f7acb52SHugues Fruchet 			__func__);
17050f7acb52SHugues Fruchet 		return ret;
17060f7acb52SHugues Fruchet 	}
170719a81c14SSteve Longerbeam 
170819a81c14SSteve Longerbeam 	ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
170919a81c14SSteve Longerbeam 				    sensor->supplies);
17100f7acb52SHugues Fruchet 	if (ret) {
17110f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to enable regulators\n",
17120f7acb52SHugues Fruchet 			__func__);
171319a81c14SSteve Longerbeam 		goto xclk_off;
17140f7acb52SHugues Fruchet 	}
171519a81c14SSteve Longerbeam 
171619a81c14SSteve Longerbeam 	ov5640_reset(sensor);
171719a81c14SSteve Longerbeam 	ov5640_power(sensor, true);
171819a81c14SSteve Longerbeam 
171919a81c14SSteve Longerbeam 	ret = ov5640_init_slave_id(sensor);
172019a81c14SSteve Longerbeam 	if (ret)
172119a81c14SSteve Longerbeam 		goto power_off;
172219a81c14SSteve Longerbeam 
17230f7acb52SHugues Fruchet 	return 0;
17240f7acb52SHugues Fruchet 
17250f7acb52SHugues Fruchet power_off:
17260f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
17270f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
17280f7acb52SHugues Fruchet xclk_off:
17290f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
17300f7acb52SHugues Fruchet 	return ret;
17310f7acb52SHugues Fruchet }
17320f7acb52SHugues Fruchet 
17330f7acb52SHugues Fruchet static void ov5640_set_power_off(struct ov5640_dev *sensor)
17340f7acb52SHugues Fruchet {
17350f7acb52SHugues Fruchet 	ov5640_power(sensor, false);
17360f7acb52SHugues Fruchet 	regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
17370f7acb52SHugues Fruchet 	clk_disable_unprepare(sensor->xclk);
17380f7acb52SHugues Fruchet }
17390f7acb52SHugues Fruchet 
17400f7acb52SHugues Fruchet static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
17410f7acb52SHugues Fruchet {
17420f7acb52SHugues Fruchet 	int ret = 0;
17430f7acb52SHugues Fruchet 
17440f7acb52SHugues Fruchet 	if (on) {
17450f7acb52SHugues Fruchet 		ret = ov5640_set_power_on(sensor);
17460f7acb52SHugues Fruchet 		if (ret)
17470f7acb52SHugues Fruchet 			return ret;
17480f7acb52SHugues Fruchet 
174919a81c14SSteve Longerbeam 		ret = ov5640_restore_mode(sensor);
175019a81c14SSteve Longerbeam 		if (ret)
175119a81c14SSteve Longerbeam 			goto power_off;
175219a81c14SSteve Longerbeam 
1753f22996dbSHugues Fruchet 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2) {
175419a81c14SSteve Longerbeam 			/*
175519a81c14SSteve Longerbeam 			 * start streaming briefly followed by stream off in
175619a81c14SSteve Longerbeam 			 * order to coax the clock lane into LP-11 state.
175719a81c14SSteve Longerbeam 			 */
1758f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, true);
175919a81c14SSteve Longerbeam 			if (ret)
176019a81c14SSteve Longerbeam 				goto power_off;
176119a81c14SSteve Longerbeam 			usleep_range(1000, 2000);
1762f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, false);
176319a81c14SSteve Longerbeam 			if (ret)
176419a81c14SSteve Longerbeam 				goto power_off;
1765f22996dbSHugues Fruchet 		}
176619a81c14SSteve Longerbeam 
176719a81c14SSteve Longerbeam 		return 0;
176819a81c14SSteve Longerbeam 	}
176919a81c14SSteve Longerbeam 
177019a81c14SSteve Longerbeam power_off:
17710f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
177219a81c14SSteve Longerbeam 	return ret;
177319a81c14SSteve Longerbeam }
177419a81c14SSteve Longerbeam 
177519a81c14SSteve Longerbeam /* --------------- Subdev Operations --------------- */
177619a81c14SSteve Longerbeam 
177719a81c14SSteve Longerbeam static int ov5640_s_power(struct v4l2_subdev *sd, int on)
177819a81c14SSteve Longerbeam {
177919a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
178019a81c14SSteve Longerbeam 	int ret = 0;
178119a81c14SSteve Longerbeam 
178219a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
178319a81c14SSteve Longerbeam 
178419a81c14SSteve Longerbeam 	/*
178519a81c14SSteve Longerbeam 	 * If the power count is modified from 0 to != 0 or from != 0 to 0,
178619a81c14SSteve Longerbeam 	 * update the power state.
178719a81c14SSteve Longerbeam 	 */
178819a81c14SSteve Longerbeam 	if (sensor->power_count == !on) {
178919a81c14SSteve Longerbeam 		ret = ov5640_set_power(sensor, !!on);
179019a81c14SSteve Longerbeam 		if (ret)
179119a81c14SSteve Longerbeam 			goto out;
179219a81c14SSteve Longerbeam 	}
179319a81c14SSteve Longerbeam 
179419a81c14SSteve Longerbeam 	/* Update the power count. */
179519a81c14SSteve Longerbeam 	sensor->power_count += on ? 1 : -1;
179619a81c14SSteve Longerbeam 	WARN_ON(sensor->power_count < 0);
179719a81c14SSteve Longerbeam out:
179819a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
179919a81c14SSteve Longerbeam 
180019a81c14SSteve Longerbeam 	if (on && !ret && sensor->power_count == 1) {
180119a81c14SSteve Longerbeam 		/* restore controls */
180219a81c14SSteve Longerbeam 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
180319a81c14SSteve Longerbeam 	}
180419a81c14SSteve Longerbeam 
180519a81c14SSteve Longerbeam 	return ret;
180619a81c14SSteve Longerbeam }
180719a81c14SSteve Longerbeam 
180819a81c14SSteve Longerbeam static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
180919a81c14SSteve Longerbeam 				     struct v4l2_fract *fi,
181019a81c14SSteve Longerbeam 				     u32 width, u32 height)
181119a81c14SSteve Longerbeam {
181219a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
181319a81c14SSteve Longerbeam 	u32 minfps, maxfps, fps;
181419a81c14SSteve Longerbeam 	int ret;
181519a81c14SSteve Longerbeam 
181619a81c14SSteve Longerbeam 	minfps = ov5640_framerates[OV5640_15_FPS];
181719a81c14SSteve Longerbeam 	maxfps = ov5640_framerates[OV5640_30_FPS];
181819a81c14SSteve Longerbeam 
181919a81c14SSteve Longerbeam 	if (fi->numerator == 0) {
182019a81c14SSteve Longerbeam 		fi->denominator = maxfps;
182119a81c14SSteve Longerbeam 		fi->numerator = 1;
182219a81c14SSteve Longerbeam 		return OV5640_30_FPS;
182319a81c14SSteve Longerbeam 	}
182419a81c14SSteve Longerbeam 
182519a81c14SSteve Longerbeam 	fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
182619a81c14SSteve Longerbeam 
182719a81c14SSteve Longerbeam 	fi->numerator = 1;
182819a81c14SSteve Longerbeam 	if (fps > maxfps)
182919a81c14SSteve Longerbeam 		fi->denominator = maxfps;
183019a81c14SSteve Longerbeam 	else if (fps < minfps)
183119a81c14SSteve Longerbeam 		fi->denominator = minfps;
183219a81c14SSteve Longerbeam 	else if (2 * fps >= 2 * minfps + (maxfps - minfps))
183319a81c14SSteve Longerbeam 		fi->denominator = maxfps;
183419a81c14SSteve Longerbeam 	else
183519a81c14SSteve Longerbeam 		fi->denominator = minfps;
183619a81c14SSteve Longerbeam 
183719a81c14SSteve Longerbeam 	ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
183819a81c14SSteve Longerbeam 
183919a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, ret, width, height, false);
184019a81c14SSteve Longerbeam 	return mode ? ret : -EINVAL;
184119a81c14SSteve Longerbeam }
184219a81c14SSteve Longerbeam 
184319a81c14SSteve Longerbeam static int ov5640_get_fmt(struct v4l2_subdev *sd,
184419a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
184519a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
184619a81c14SSteve Longerbeam {
184719a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
184819a81c14SSteve Longerbeam 	struct v4l2_mbus_framefmt *fmt;
184919a81c14SSteve Longerbeam 
185019a81c14SSteve Longerbeam 	if (format->pad != 0)
185119a81c14SSteve Longerbeam 		return -EINVAL;
185219a81c14SSteve Longerbeam 
185319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
185419a81c14SSteve Longerbeam 
185519a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
185619a81c14SSteve Longerbeam 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
185719a81c14SSteve Longerbeam 						 format->pad);
185819a81c14SSteve Longerbeam 	else
185919a81c14SSteve Longerbeam 		fmt = &sensor->fmt;
186019a81c14SSteve Longerbeam 
186119a81c14SSteve Longerbeam 	format->format = *fmt;
186219a81c14SSteve Longerbeam 
186319a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
186419a81c14SSteve Longerbeam 
186519a81c14SSteve Longerbeam 	return 0;
186619a81c14SSteve Longerbeam }
186719a81c14SSteve Longerbeam 
186819a81c14SSteve Longerbeam static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
186919a81c14SSteve Longerbeam 				   struct v4l2_mbus_framefmt *fmt,
187019a81c14SSteve Longerbeam 				   enum ov5640_frame_rate fr,
187119a81c14SSteve Longerbeam 				   const struct ov5640_mode_info **new_mode)
187219a81c14SSteve Longerbeam {
187319a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
187419a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
1875e3ee691dSHugues Fruchet 	int i;
187619a81c14SSteve Longerbeam 
187719a81c14SSteve Longerbeam 	mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
187819a81c14SSteve Longerbeam 	if (!mode)
187919a81c14SSteve Longerbeam 		return -EINVAL;
188019a81c14SSteve Longerbeam 	fmt->width = mode->width;
188119a81c14SSteve Longerbeam 	fmt->height = mode->height;
188219a81c14SSteve Longerbeam 
188319a81c14SSteve Longerbeam 	if (new_mode)
188419a81c14SSteve Longerbeam 		*new_mode = mode;
1885e3ee691dSHugues Fruchet 
1886e3ee691dSHugues Fruchet 	for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
1887e3ee691dSHugues Fruchet 		if (ov5640_formats[i].code == fmt->code)
1888e3ee691dSHugues Fruchet 			break;
1889e3ee691dSHugues Fruchet 	if (i >= ARRAY_SIZE(ov5640_formats))
1890e6441fdeSHugues Fruchet 		i = 0;
1891e6441fdeSHugues Fruchet 
1892e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[i].code;
1893e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[i].colorspace;
1894e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1895e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1896e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1897e3ee691dSHugues Fruchet 
189819a81c14SSteve Longerbeam 	return 0;
189919a81c14SSteve Longerbeam }
190019a81c14SSteve Longerbeam 
190119a81c14SSteve Longerbeam static int ov5640_set_fmt(struct v4l2_subdev *sd,
190219a81c14SSteve Longerbeam 			  struct v4l2_subdev_pad_config *cfg,
190319a81c14SSteve Longerbeam 			  struct v4l2_subdev_format *format)
190419a81c14SSteve Longerbeam {
190519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
190619a81c14SSteve Longerbeam 	const struct ov5640_mode_info *new_mode;
1907e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
190819a81c14SSteve Longerbeam 	int ret;
190919a81c14SSteve Longerbeam 
191019a81c14SSteve Longerbeam 	if (format->pad != 0)
191119a81c14SSteve Longerbeam 		return -EINVAL;
191219a81c14SSteve Longerbeam 
191319a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
191419a81c14SSteve Longerbeam 
191519a81c14SSteve Longerbeam 	if (sensor->streaming) {
191619a81c14SSteve Longerbeam 		ret = -EBUSY;
191719a81c14SSteve Longerbeam 		goto out;
191819a81c14SSteve Longerbeam 	}
191919a81c14SSteve Longerbeam 
1920e6441fdeSHugues Fruchet 	ret = ov5640_try_fmt_internal(sd, mbus_fmt,
192119a81c14SSteve Longerbeam 				      sensor->current_fr, &new_mode);
192219a81c14SSteve Longerbeam 	if (ret)
192319a81c14SSteve Longerbeam 		goto out;
192419a81c14SSteve Longerbeam 
192519a81c14SSteve Longerbeam 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
192619a81c14SSteve Longerbeam 		struct v4l2_mbus_framefmt *fmt =
192719a81c14SSteve Longerbeam 			v4l2_subdev_get_try_format(sd, cfg, 0);
192819a81c14SSteve Longerbeam 
1929e6441fdeSHugues Fruchet 		*fmt = *mbus_fmt;
193019a81c14SSteve Longerbeam 		goto out;
193119a81c14SSteve Longerbeam 	}
193219a81c14SSteve Longerbeam 
193319a81c14SSteve Longerbeam 	sensor->current_mode = new_mode;
1934e6441fdeSHugues Fruchet 	sensor->fmt = *mbus_fmt;
193519a81c14SSteve Longerbeam 	sensor->pending_mode_change = true;
193619a81c14SSteve Longerbeam out:
193719a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
193819a81c14SSteve Longerbeam 	return ret;
193919a81c14SSteve Longerbeam }
194019a81c14SSteve Longerbeam 
1941e3ee691dSHugues Fruchet static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1942e3ee691dSHugues Fruchet 			       struct v4l2_mbus_framefmt *format)
1943e3ee691dSHugues Fruchet {
1944e3ee691dSHugues Fruchet 	int ret = 0;
1945e3ee691dSHugues Fruchet 	bool is_rgb = false;
1946d47c4126SHugues Fruchet 	bool is_jpeg = false;
1947e3ee691dSHugues Fruchet 	u8 val;
1948e3ee691dSHugues Fruchet 
1949e3ee691dSHugues Fruchet 	switch (format->code) {
1950e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_UYVY8_2X8:
1951e3ee691dSHugues Fruchet 		/* YUV422, UYVY */
1952e3ee691dSHugues Fruchet 		val = 0x3f;
1953e3ee691dSHugues Fruchet 		break;
1954e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_YUYV8_2X8:
1955e3ee691dSHugues Fruchet 		/* YUV422, YUYV */
1956e3ee691dSHugues Fruchet 		val = 0x30;
1957e3ee691dSHugues Fruchet 		break;
1958e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_LE:
1959e3ee691dSHugues Fruchet 		/* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
1960e3ee691dSHugues Fruchet 		val = 0x6F;
1961e3ee691dSHugues Fruchet 		is_rgb = true;
1962e3ee691dSHugues Fruchet 		break;
1963e3ee691dSHugues Fruchet 	case MEDIA_BUS_FMT_RGB565_2X8_BE:
1964e3ee691dSHugues Fruchet 		/* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
1965e3ee691dSHugues Fruchet 		val = 0x61;
1966e3ee691dSHugues Fruchet 		is_rgb = true;
1967e3ee691dSHugues Fruchet 		break;
1968d47c4126SHugues Fruchet 	case MEDIA_BUS_FMT_JPEG_1X8:
1969d47c4126SHugues Fruchet 		/* YUV422, YUYV */
1970d47c4126SHugues Fruchet 		val = 0x30;
1971d47c4126SHugues Fruchet 		is_jpeg = true;
1972d47c4126SHugues Fruchet 		break;
1973e3ee691dSHugues Fruchet 	default:
1974e3ee691dSHugues Fruchet 		return -EINVAL;
1975e3ee691dSHugues Fruchet 	}
1976e3ee691dSHugues Fruchet 
1977e3ee691dSHugues Fruchet 	/* FORMAT CONTROL00: YUV and RGB formatting */
1978e3ee691dSHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
1979e3ee691dSHugues Fruchet 	if (ret)
1980e3ee691dSHugues Fruchet 		return ret;
1981e3ee691dSHugues Fruchet 
1982e3ee691dSHugues Fruchet 	/* FORMAT MUX CONTROL: ISP YUV or RGB */
1983d47c4126SHugues Fruchet 	ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
1984e3ee691dSHugues Fruchet 			       is_rgb ? 0x01 : 0x00);
1985d47c4126SHugues Fruchet 	if (ret)
1986d47c4126SHugues Fruchet 		return ret;
1987d47c4126SHugues Fruchet 
1988d47c4126SHugues Fruchet 	/*
1989d47c4126SHugues Fruchet 	 * TIMING TC REG21:
1990d47c4126SHugues Fruchet 	 * - [5]:	JPEG enable
1991d47c4126SHugues Fruchet 	 */
1992d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1993d47c4126SHugues Fruchet 			     BIT(5), is_jpeg ? BIT(5) : 0);
1994d47c4126SHugues Fruchet 	if (ret)
1995d47c4126SHugues Fruchet 		return ret;
1996d47c4126SHugues Fruchet 
1997d47c4126SHugues Fruchet 	/*
1998d47c4126SHugues Fruchet 	 * SYSTEM RESET02:
1999d47c4126SHugues Fruchet 	 * - [4]:	Reset JFIFO
2000d47c4126SHugues Fruchet 	 * - [3]:	Reset SFIFO
2001d47c4126SHugues Fruchet 	 * - [2]:	Reset JPEG
2002d47c4126SHugues Fruchet 	 */
2003d47c4126SHugues Fruchet 	ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2004d47c4126SHugues Fruchet 			     BIT(4) | BIT(3) | BIT(2),
2005d47c4126SHugues Fruchet 			     is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2006d47c4126SHugues Fruchet 	if (ret)
2007d47c4126SHugues Fruchet 		return ret;
2008d47c4126SHugues Fruchet 
2009d47c4126SHugues Fruchet 	/*
2010d47c4126SHugues Fruchet 	 * CLOCK ENABLE02:
2011d47c4126SHugues Fruchet 	 * - [5]:	Enable JPEG 2x clock
2012d47c4126SHugues Fruchet 	 * - [3]:	Enable JPEG clock
2013d47c4126SHugues Fruchet 	 */
2014d47c4126SHugues Fruchet 	return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2015d47c4126SHugues Fruchet 			      BIT(5) | BIT(3),
2016d47c4126SHugues Fruchet 			      is_jpeg ? (BIT(5) | BIT(3)) : 0);
2017e3ee691dSHugues Fruchet }
201819a81c14SSteve Longerbeam 
201919a81c14SSteve Longerbeam /*
202019a81c14SSteve Longerbeam  * Sensor Controls.
202119a81c14SSteve Longerbeam  */
202219a81c14SSteve Longerbeam 
202319a81c14SSteve Longerbeam static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
202419a81c14SSteve Longerbeam {
202519a81c14SSteve Longerbeam 	int ret;
202619a81c14SSteve Longerbeam 
202719a81c14SSteve Longerbeam 	if (value) {
202819a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
202919a81c14SSteve Longerbeam 				     BIT(0), BIT(0));
203019a81c14SSteve Longerbeam 		if (ret)
203119a81c14SSteve Longerbeam 			return ret;
203219a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
203319a81c14SSteve Longerbeam 	} else {
203419a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
203519a81c14SSteve Longerbeam 	}
203619a81c14SSteve Longerbeam 
203719a81c14SSteve Longerbeam 	return ret;
203819a81c14SSteve Longerbeam }
203919a81c14SSteve Longerbeam 
204019a81c14SSteve Longerbeam static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
204119a81c14SSteve Longerbeam {
204219a81c14SSteve Longerbeam 	int ret;
204319a81c14SSteve Longerbeam 
204419a81c14SSteve Longerbeam 	if (value) {
204519a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
204619a81c14SSteve Longerbeam 				     BIT(2), BIT(2));
204719a81c14SSteve Longerbeam 		if (ret)
204819a81c14SSteve Longerbeam 			return ret;
204919a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
205019a81c14SSteve Longerbeam 				       value & 0xff);
205119a81c14SSteve Longerbeam 	} else {
205219a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
205319a81c14SSteve Longerbeam 	}
205419a81c14SSteve Longerbeam 
205519a81c14SSteve Longerbeam 	return ret;
205619a81c14SSteve Longerbeam }
205719a81c14SSteve Longerbeam 
205819a81c14SSteve Longerbeam static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
205919a81c14SSteve Longerbeam {
206019a81c14SSteve Longerbeam 	int ret;
206119a81c14SSteve Longerbeam 
206219a81c14SSteve Longerbeam 	if (value) {
206319a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
206419a81c14SSteve Longerbeam 				     BIT(1), BIT(1));
206519a81c14SSteve Longerbeam 		if (ret)
206619a81c14SSteve Longerbeam 			return ret;
206719a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
206819a81c14SSteve Longerbeam 				       value & 0xff);
206919a81c14SSteve Longerbeam 		if (ret)
207019a81c14SSteve Longerbeam 			return ret;
207119a81c14SSteve Longerbeam 		ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
207219a81c14SSteve Longerbeam 				       value & 0xff);
207319a81c14SSteve Longerbeam 	} else {
207419a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
207519a81c14SSteve Longerbeam 	}
207619a81c14SSteve Longerbeam 
207719a81c14SSteve Longerbeam 	return ret;
207819a81c14SSteve Longerbeam }
207919a81c14SSteve Longerbeam 
208019a81c14SSteve Longerbeam static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
208119a81c14SSteve Longerbeam {
208219a81c14SSteve Longerbeam 	int ret;
208319a81c14SSteve Longerbeam 
208419a81c14SSteve Longerbeam 	ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
208519a81c14SSteve Longerbeam 			     BIT(0), awb ? 0 : 1);
208619a81c14SSteve Longerbeam 	if (ret)
208719a81c14SSteve Longerbeam 		return ret;
208819a81c14SSteve Longerbeam 
208919a81c14SSteve Longerbeam 	if (!awb) {
209019a81c14SSteve Longerbeam 		u16 red = (u16)sensor->ctrls.red_balance->val;
209119a81c14SSteve Longerbeam 		u16 blue = (u16)sensor->ctrls.blue_balance->val;
209219a81c14SSteve Longerbeam 
209319a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
209419a81c14SSteve Longerbeam 		if (ret)
209519a81c14SSteve Longerbeam 			return ret;
209619a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
209719a81c14SSteve Longerbeam 	}
209819a81c14SSteve Longerbeam 
209919a81c14SSteve Longerbeam 	return ret;
210019a81c14SSteve Longerbeam }
210119a81c14SSteve Longerbeam 
210219a81c14SSteve Longerbeam static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
210319a81c14SSteve Longerbeam {
210419a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
210519a81c14SSteve Longerbeam 	bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
210619a81c14SSteve Longerbeam 	int ret = 0;
210719a81c14SSteve Longerbeam 
210819a81c14SSteve Longerbeam 	if (ctrls->auto_exp->is_new) {
210919a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
211019a81c14SSteve Longerbeam 				     BIT(0), auto_exposure ? 0 : BIT(0));
211119a81c14SSteve Longerbeam 		if (ret)
211219a81c14SSteve Longerbeam 			return ret;
211319a81c14SSteve Longerbeam 	}
211419a81c14SSteve Longerbeam 
211519a81c14SSteve Longerbeam 	if (!auto_exposure && ctrls->exposure->is_new) {
211619a81c14SSteve Longerbeam 		u16 max_exp;
211719a81c14SSteve Longerbeam 
211819a81c14SSteve Longerbeam 		ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
211919a81c14SSteve Longerbeam 					&max_exp);
212019a81c14SSteve Longerbeam 		if (ret)
212119a81c14SSteve Longerbeam 			return ret;
212219a81c14SSteve Longerbeam 		ret = ov5640_get_vts(sensor);
212319a81c14SSteve Longerbeam 		if (ret < 0)
212419a81c14SSteve Longerbeam 			return ret;
212519a81c14SSteve Longerbeam 		max_exp += ret;
21266146fde3SHugues Fruchet 		ret = 0;
212719a81c14SSteve Longerbeam 
212819a81c14SSteve Longerbeam 		if (ctrls->exposure->val < max_exp)
212919a81c14SSteve Longerbeam 			ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
213019a81c14SSteve Longerbeam 	}
213119a81c14SSteve Longerbeam 
213219a81c14SSteve Longerbeam 	return ret;
213319a81c14SSteve Longerbeam }
213419a81c14SSteve Longerbeam 
213519a81c14SSteve Longerbeam static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
213619a81c14SSteve Longerbeam {
213719a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
213819a81c14SSteve Longerbeam 	int ret = 0;
213919a81c14SSteve Longerbeam 
214019a81c14SSteve Longerbeam 	if (ctrls->auto_gain->is_new) {
214119a81c14SSteve Longerbeam 		ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
214241d8d7f5SHugues Fruchet 				     BIT(1),
214341d8d7f5SHugues Fruchet 				     ctrls->auto_gain->val ? 0 : BIT(1));
214419a81c14SSteve Longerbeam 		if (ret)
214519a81c14SSteve Longerbeam 			return ret;
214619a81c14SSteve Longerbeam 	}
214719a81c14SSteve Longerbeam 
214819a81c14SSteve Longerbeam 	if (!auto_gain && ctrls->gain->is_new) {
214919a81c14SSteve Longerbeam 		u16 gain = (u16)ctrls->gain->val;
215019a81c14SSteve Longerbeam 
215119a81c14SSteve Longerbeam 		ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
215219a81c14SSteve Longerbeam 					 gain & 0x3ff);
215319a81c14SSteve Longerbeam 	}
215419a81c14SSteve Longerbeam 
215519a81c14SSteve Longerbeam 	return ret;
215619a81c14SSteve Longerbeam }
215719a81c14SSteve Longerbeam 
215819a81c14SSteve Longerbeam static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
215919a81c14SSteve Longerbeam {
216019a81c14SSteve Longerbeam 	return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
216119a81c14SSteve Longerbeam 			      0xa4, value ? 0xa4 : 0);
216219a81c14SSteve Longerbeam }
216319a81c14SSteve Longerbeam 
21641068fecaSMylène Josserand static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
21651068fecaSMylène Josserand {
21661068fecaSMylène Josserand 	int ret;
21671068fecaSMylène Josserand 
21681068fecaSMylène Josserand 	ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
21691068fecaSMylène Josserand 			     (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
21701068fecaSMylène Josserand 			     0 : BIT(7));
21711068fecaSMylène Josserand 	if (ret)
21721068fecaSMylène Josserand 		return ret;
21731068fecaSMylène Josserand 
21741068fecaSMylène Josserand 	return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
21751068fecaSMylène Josserand 			      (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
21761068fecaSMylène Josserand 			      BIT(2) : 0);
21771068fecaSMylène Josserand }
21781068fecaSMylène Josserand 
217919a81c14SSteve Longerbeam static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
218019a81c14SSteve Longerbeam {
218119a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
218219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
218319a81c14SSteve Longerbeam 	int val;
218419a81c14SSteve Longerbeam 
218519a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
218619a81c14SSteve Longerbeam 
218719a81c14SSteve Longerbeam 	switch (ctrl->id) {
218819a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
218919a81c14SSteve Longerbeam 		if (!ctrl->val)
219019a81c14SSteve Longerbeam 			return 0;
219119a81c14SSteve Longerbeam 		val = ov5640_get_gain(sensor);
219219a81c14SSteve Longerbeam 		if (val < 0)
219319a81c14SSteve Longerbeam 			return val;
219419a81c14SSteve Longerbeam 		sensor->ctrls.gain->val = val;
219519a81c14SSteve Longerbeam 		break;
219619a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
219719a81c14SSteve Longerbeam 		if (ctrl->val == V4L2_EXPOSURE_MANUAL)
219819a81c14SSteve Longerbeam 			return 0;
219919a81c14SSteve Longerbeam 		val = ov5640_get_exposure(sensor);
220019a81c14SSteve Longerbeam 		if (val < 0)
220119a81c14SSteve Longerbeam 			return val;
220219a81c14SSteve Longerbeam 		sensor->ctrls.exposure->val = val;
220319a81c14SSteve Longerbeam 		break;
220419a81c14SSteve Longerbeam 	}
220519a81c14SSteve Longerbeam 
220619a81c14SSteve Longerbeam 	return 0;
220719a81c14SSteve Longerbeam }
220819a81c14SSteve Longerbeam 
220919a81c14SSteve Longerbeam static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
221019a81c14SSteve Longerbeam {
221119a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
221219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
221319a81c14SSteve Longerbeam 	int ret;
221419a81c14SSteve Longerbeam 
221519a81c14SSteve Longerbeam 	/* v4l2_ctrl_lock() locks our own mutex */
221619a81c14SSteve Longerbeam 
221719a81c14SSteve Longerbeam 	/*
221819a81c14SSteve Longerbeam 	 * If the device is not powered up by the host driver do
221919a81c14SSteve Longerbeam 	 * not apply any controls to H/W at this time. Instead
222019a81c14SSteve Longerbeam 	 * the controls will be restored right after power-up.
222119a81c14SSteve Longerbeam 	 */
222219a81c14SSteve Longerbeam 	if (sensor->power_count == 0)
222319a81c14SSteve Longerbeam 		return 0;
222419a81c14SSteve Longerbeam 
222519a81c14SSteve Longerbeam 	switch (ctrl->id) {
222619a81c14SSteve Longerbeam 	case V4L2_CID_AUTOGAIN:
222719a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
222819a81c14SSteve Longerbeam 		break;
222919a81c14SSteve Longerbeam 	case V4L2_CID_EXPOSURE_AUTO:
223019a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
223119a81c14SSteve Longerbeam 		break;
223219a81c14SSteve Longerbeam 	case V4L2_CID_AUTO_WHITE_BALANCE:
223319a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
223419a81c14SSteve Longerbeam 		break;
223519a81c14SSteve Longerbeam 	case V4L2_CID_HUE:
223619a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
223719a81c14SSteve Longerbeam 		break;
223819a81c14SSteve Longerbeam 	case V4L2_CID_CONTRAST:
223919a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
224019a81c14SSteve Longerbeam 		break;
224119a81c14SSteve Longerbeam 	case V4L2_CID_SATURATION:
224219a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
224319a81c14SSteve Longerbeam 		break;
224419a81c14SSteve Longerbeam 	case V4L2_CID_TEST_PATTERN:
224519a81c14SSteve Longerbeam 		ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
224619a81c14SSteve Longerbeam 		break;
22471068fecaSMylène Josserand 	case V4L2_CID_POWER_LINE_FREQUENCY:
22481068fecaSMylène Josserand 		ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
22491068fecaSMylène Josserand 		break;
225019a81c14SSteve Longerbeam 	default:
225119a81c14SSteve Longerbeam 		ret = -EINVAL;
225219a81c14SSteve Longerbeam 		break;
225319a81c14SSteve Longerbeam 	}
225419a81c14SSteve Longerbeam 
225519a81c14SSteve Longerbeam 	return ret;
225619a81c14SSteve Longerbeam }
225719a81c14SSteve Longerbeam 
225819a81c14SSteve Longerbeam static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
225919a81c14SSteve Longerbeam 	.g_volatile_ctrl = ov5640_g_volatile_ctrl,
226019a81c14SSteve Longerbeam 	.s_ctrl = ov5640_s_ctrl,
226119a81c14SSteve Longerbeam };
226219a81c14SSteve Longerbeam 
226319a81c14SSteve Longerbeam static const char * const test_pattern_menu[] = {
226419a81c14SSteve Longerbeam 	"Disabled",
226519a81c14SSteve Longerbeam 	"Color bars",
226619a81c14SSteve Longerbeam };
226719a81c14SSteve Longerbeam 
226819a81c14SSteve Longerbeam static int ov5640_init_controls(struct ov5640_dev *sensor)
226919a81c14SSteve Longerbeam {
227019a81c14SSteve Longerbeam 	const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
227119a81c14SSteve Longerbeam 	struct ov5640_ctrls *ctrls = &sensor->ctrls;
227219a81c14SSteve Longerbeam 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
227319a81c14SSteve Longerbeam 	int ret;
227419a81c14SSteve Longerbeam 
227519a81c14SSteve Longerbeam 	v4l2_ctrl_handler_init(hdl, 32);
227619a81c14SSteve Longerbeam 
227719a81c14SSteve Longerbeam 	/* we can use our own mutex for the ctrl lock */
227819a81c14SSteve Longerbeam 	hdl->lock = &sensor->lock;
227919a81c14SSteve Longerbeam 
228019a81c14SSteve Longerbeam 	/* Auto/manual white balance */
228119a81c14SSteve Longerbeam 	ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
228219a81c14SSteve Longerbeam 					   V4L2_CID_AUTO_WHITE_BALANCE,
228319a81c14SSteve Longerbeam 					   0, 1, 1, 1);
228419a81c14SSteve Longerbeam 	ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
228519a81c14SSteve Longerbeam 						0, 4095, 1, 0);
228619a81c14SSteve Longerbeam 	ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
228719a81c14SSteve Longerbeam 					       0, 4095, 1, 0);
228819a81c14SSteve Longerbeam 	/* Auto/manual exposure */
228919a81c14SSteve Longerbeam 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
229019a81c14SSteve Longerbeam 						 V4L2_CID_EXPOSURE_AUTO,
229119a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_MANUAL, 0,
229219a81c14SSteve Longerbeam 						 V4L2_EXPOSURE_AUTO);
229319a81c14SSteve Longerbeam 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
229419a81c14SSteve Longerbeam 					    0, 65535, 1, 0);
229519a81c14SSteve Longerbeam 	/* Auto/manual gain */
229619a81c14SSteve Longerbeam 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
229719a81c14SSteve Longerbeam 					     0, 1, 1, 1);
229819a81c14SSteve Longerbeam 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
229919a81c14SSteve Longerbeam 					0, 1023, 1, 0);
230019a81c14SSteve Longerbeam 
230119a81c14SSteve Longerbeam 	ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
230219a81c14SSteve Longerbeam 					      0, 255, 1, 64);
230319a81c14SSteve Longerbeam 	ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
230419a81c14SSteve Longerbeam 				       0, 359, 1, 0);
230519a81c14SSteve Longerbeam 	ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
230619a81c14SSteve Longerbeam 					    0, 255, 1, 0);
230719a81c14SSteve Longerbeam 	ctrls->test_pattern =
230819a81c14SSteve Longerbeam 		v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
230919a81c14SSteve Longerbeam 					     ARRAY_SIZE(test_pattern_menu) - 1,
231019a81c14SSteve Longerbeam 					     0, 0, test_pattern_menu);
231119a81c14SSteve Longerbeam 
23121068fecaSMylène Josserand 	ctrls->light_freq =
23131068fecaSMylène Josserand 		v4l2_ctrl_new_std_menu(hdl, ops,
23141068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY,
23151068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
23161068fecaSMylène Josserand 				       V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
23171068fecaSMylène Josserand 
231819a81c14SSteve Longerbeam 	if (hdl->error) {
231919a81c14SSteve Longerbeam 		ret = hdl->error;
232019a81c14SSteve Longerbeam 		goto free_ctrls;
232119a81c14SSteve Longerbeam 	}
232219a81c14SSteve Longerbeam 
232319a81c14SSteve Longerbeam 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
232419a81c14SSteve Longerbeam 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
232519a81c14SSteve Longerbeam 
232619a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
232719a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
232819a81c14SSteve Longerbeam 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
232919a81c14SSteve Longerbeam 
233019a81c14SSteve Longerbeam 	sensor->sd.ctrl_handler = hdl;
233119a81c14SSteve Longerbeam 	return 0;
233219a81c14SSteve Longerbeam 
233319a81c14SSteve Longerbeam free_ctrls:
233419a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(hdl);
233519a81c14SSteve Longerbeam 	return ret;
233619a81c14SSteve Longerbeam }
233719a81c14SSteve Longerbeam 
233819a81c14SSteve Longerbeam static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
233919a81c14SSteve Longerbeam 				  struct v4l2_subdev_pad_config *cfg,
234019a81c14SSteve Longerbeam 				  struct v4l2_subdev_frame_size_enum *fse)
234119a81c14SSteve Longerbeam {
234219a81c14SSteve Longerbeam 	if (fse->pad != 0)
234319a81c14SSteve Longerbeam 		return -EINVAL;
234419a81c14SSteve Longerbeam 	if (fse->index >= OV5640_NUM_MODES)
234519a81c14SSteve Longerbeam 		return -EINVAL;
234619a81c14SSteve Longerbeam 
234741d8d7f5SHugues Fruchet 	fse->min_width =
234819a81c14SSteve Longerbeam 		ov5640_mode_data[0][fse->index].width;
234941d8d7f5SHugues Fruchet 	fse->max_width = fse->min_width;
235041d8d7f5SHugues Fruchet 	fse->min_height =
235119a81c14SSteve Longerbeam 		ov5640_mode_data[0][fse->index].height;
235241d8d7f5SHugues Fruchet 	fse->max_height = fse->min_height;
235319a81c14SSteve Longerbeam 
235419a81c14SSteve Longerbeam 	return 0;
235519a81c14SSteve Longerbeam }
235619a81c14SSteve Longerbeam 
235719a81c14SSteve Longerbeam static int ov5640_enum_frame_interval(
235819a81c14SSteve Longerbeam 	struct v4l2_subdev *sd,
235919a81c14SSteve Longerbeam 	struct v4l2_subdev_pad_config *cfg,
236019a81c14SSteve Longerbeam 	struct v4l2_subdev_frame_interval_enum *fie)
236119a81c14SSteve Longerbeam {
236219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
236319a81c14SSteve Longerbeam 	struct v4l2_fract tpf;
236419a81c14SSteve Longerbeam 	int ret;
236519a81c14SSteve Longerbeam 
236619a81c14SSteve Longerbeam 	if (fie->pad != 0)
236719a81c14SSteve Longerbeam 		return -EINVAL;
236819a81c14SSteve Longerbeam 	if (fie->index >= OV5640_NUM_FRAMERATES)
236919a81c14SSteve Longerbeam 		return -EINVAL;
237019a81c14SSteve Longerbeam 
237119a81c14SSteve Longerbeam 	tpf.numerator = 1;
237219a81c14SSteve Longerbeam 	tpf.denominator = ov5640_framerates[fie->index];
237319a81c14SSteve Longerbeam 
237419a81c14SSteve Longerbeam 	ret = ov5640_try_frame_interval(sensor, &tpf,
237519a81c14SSteve Longerbeam 					fie->width, fie->height);
237619a81c14SSteve Longerbeam 	if (ret < 0)
237719a81c14SSteve Longerbeam 		return -EINVAL;
237819a81c14SSteve Longerbeam 
237919a81c14SSteve Longerbeam 	fie->interval = tpf;
238019a81c14SSteve Longerbeam 	return 0;
238119a81c14SSteve Longerbeam }
238219a81c14SSteve Longerbeam 
238319a81c14SSteve Longerbeam static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
238419a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
238519a81c14SSteve Longerbeam {
238619a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
238719a81c14SSteve Longerbeam 
238819a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
238919a81c14SSteve Longerbeam 	fi->interval = sensor->frame_interval;
239019a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
239119a81c14SSteve Longerbeam 
239219a81c14SSteve Longerbeam 	return 0;
239319a81c14SSteve Longerbeam }
239419a81c14SSteve Longerbeam 
239519a81c14SSteve Longerbeam static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
239619a81c14SSteve Longerbeam 				   struct v4l2_subdev_frame_interval *fi)
239719a81c14SSteve Longerbeam {
239819a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
239919a81c14SSteve Longerbeam 	const struct ov5640_mode_info *mode;
240019a81c14SSteve Longerbeam 	int frame_rate, ret = 0;
240119a81c14SSteve Longerbeam 
240219a81c14SSteve Longerbeam 	if (fi->pad != 0)
240319a81c14SSteve Longerbeam 		return -EINVAL;
240419a81c14SSteve Longerbeam 
240519a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
240619a81c14SSteve Longerbeam 
240719a81c14SSteve Longerbeam 	if (sensor->streaming) {
240819a81c14SSteve Longerbeam 		ret = -EBUSY;
240919a81c14SSteve Longerbeam 		goto out;
241019a81c14SSteve Longerbeam 	}
241119a81c14SSteve Longerbeam 
241219a81c14SSteve Longerbeam 	mode = sensor->current_mode;
241319a81c14SSteve Longerbeam 
241419a81c14SSteve Longerbeam 	frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
241519a81c14SSteve Longerbeam 					       mode->width, mode->height);
241619a81c14SSteve Longerbeam 	if (frame_rate < 0)
241719a81c14SSteve Longerbeam 		frame_rate = OV5640_15_FPS;
241819a81c14SSteve Longerbeam 
241919a81c14SSteve Longerbeam 	sensor->current_fr = frame_rate;
242019a81c14SSteve Longerbeam 	sensor->frame_interval = fi->interval;
24213b498424SHugues Fruchet 	sensor->current_mode = ov5640_find_mode(sensor, frame_rate, mode->width,
24223b498424SHugues Fruchet 						mode->height, true);
242319a81c14SSteve Longerbeam 	sensor->pending_mode_change = true;
242419a81c14SSteve Longerbeam out:
242519a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
242619a81c14SSteve Longerbeam 	return ret;
242719a81c14SSteve Longerbeam }
242819a81c14SSteve Longerbeam 
242919a81c14SSteve Longerbeam static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
243019a81c14SSteve Longerbeam 				 struct v4l2_subdev_pad_config *cfg,
243119a81c14SSteve Longerbeam 				 struct v4l2_subdev_mbus_code_enum *code)
243219a81c14SSteve Longerbeam {
243319a81c14SSteve Longerbeam 	if (code->pad != 0)
243419a81c14SSteve Longerbeam 		return -EINVAL;
2435e3ee691dSHugues Fruchet 	if (code->index >= ARRAY_SIZE(ov5640_formats))
243619a81c14SSteve Longerbeam 		return -EINVAL;
243719a81c14SSteve Longerbeam 
2438e3ee691dSHugues Fruchet 	code->code = ov5640_formats[code->index].code;
243919a81c14SSteve Longerbeam 	return 0;
244019a81c14SSteve Longerbeam }
244119a81c14SSteve Longerbeam 
244219a81c14SSteve Longerbeam static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
244319a81c14SSteve Longerbeam {
244419a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
244519a81c14SSteve Longerbeam 	int ret = 0;
244619a81c14SSteve Longerbeam 
244719a81c14SSteve Longerbeam 	mutex_lock(&sensor->lock);
244819a81c14SSteve Longerbeam 
244919a81c14SSteve Longerbeam 	if (sensor->streaming == !enable) {
245019a81c14SSteve Longerbeam 		if (enable && sensor->pending_mode_change) {
245119a81c14SSteve Longerbeam 			ret = ov5640_set_mode(sensor, sensor->current_mode);
245219a81c14SSteve Longerbeam 			if (ret)
245319a81c14SSteve Longerbeam 				goto out;
2454e3ee691dSHugues Fruchet 
2455e3ee691dSHugues Fruchet 			ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2456e3ee691dSHugues Fruchet 			if (ret)
2457e3ee691dSHugues Fruchet 				goto out;
245819a81c14SSteve Longerbeam 		}
245919a81c14SSteve Longerbeam 
2460f22996dbSHugues Fruchet 		if (sensor->ep.bus_type == V4L2_MBUS_CSI2)
2461f22996dbSHugues Fruchet 			ret = ov5640_set_stream_mipi(sensor, enable);
2462f22996dbSHugues Fruchet 		else
2463f22996dbSHugues Fruchet 			ret = ov5640_set_stream_dvp(sensor, enable);
2464f22996dbSHugues Fruchet 
246519a81c14SSteve Longerbeam 		if (!ret)
246619a81c14SSteve Longerbeam 			sensor->streaming = enable;
246719a81c14SSteve Longerbeam 	}
246819a81c14SSteve Longerbeam out:
246919a81c14SSteve Longerbeam 	mutex_unlock(&sensor->lock);
247019a81c14SSteve Longerbeam 	return ret;
247119a81c14SSteve Longerbeam }
247219a81c14SSteve Longerbeam 
247319a81c14SSteve Longerbeam static const struct v4l2_subdev_core_ops ov5640_core_ops = {
247419a81c14SSteve Longerbeam 	.s_power = ov5640_s_power,
247519a81c14SSteve Longerbeam };
247619a81c14SSteve Longerbeam 
247719a81c14SSteve Longerbeam static const struct v4l2_subdev_video_ops ov5640_video_ops = {
247819a81c14SSteve Longerbeam 	.g_frame_interval = ov5640_g_frame_interval,
247919a81c14SSteve Longerbeam 	.s_frame_interval = ov5640_s_frame_interval,
248019a81c14SSteve Longerbeam 	.s_stream = ov5640_s_stream,
248119a81c14SSteve Longerbeam };
248219a81c14SSteve Longerbeam 
248319a81c14SSteve Longerbeam static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
248419a81c14SSteve Longerbeam 	.enum_mbus_code = ov5640_enum_mbus_code,
248519a81c14SSteve Longerbeam 	.get_fmt = ov5640_get_fmt,
248619a81c14SSteve Longerbeam 	.set_fmt = ov5640_set_fmt,
248719a81c14SSteve Longerbeam 	.enum_frame_size = ov5640_enum_frame_size,
248819a81c14SSteve Longerbeam 	.enum_frame_interval = ov5640_enum_frame_interval,
248919a81c14SSteve Longerbeam };
249019a81c14SSteve Longerbeam 
249119a81c14SSteve Longerbeam static const struct v4l2_subdev_ops ov5640_subdev_ops = {
249219a81c14SSteve Longerbeam 	.core = &ov5640_core_ops,
249319a81c14SSteve Longerbeam 	.video = &ov5640_video_ops,
249419a81c14SSteve Longerbeam 	.pad = &ov5640_pad_ops,
249519a81c14SSteve Longerbeam };
249619a81c14SSteve Longerbeam 
249719a81c14SSteve Longerbeam static int ov5640_get_regulators(struct ov5640_dev *sensor)
249819a81c14SSteve Longerbeam {
249919a81c14SSteve Longerbeam 	int i;
250019a81c14SSteve Longerbeam 
250119a81c14SSteve Longerbeam 	for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
250219a81c14SSteve Longerbeam 		sensor->supplies[i].supply = ov5640_supply_name[i];
250319a81c14SSteve Longerbeam 
250419a81c14SSteve Longerbeam 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
250519a81c14SSteve Longerbeam 				       OV5640_NUM_SUPPLIES,
250619a81c14SSteve Longerbeam 				       sensor->supplies);
250719a81c14SSteve Longerbeam }
250819a81c14SSteve Longerbeam 
25090f7acb52SHugues Fruchet static int ov5640_check_chip_id(struct ov5640_dev *sensor)
25100f7acb52SHugues Fruchet {
25110f7acb52SHugues Fruchet 	struct i2c_client *client = sensor->i2c_client;
25120f7acb52SHugues Fruchet 	int ret = 0;
25130f7acb52SHugues Fruchet 	u16 chip_id;
25140f7acb52SHugues Fruchet 
25150f7acb52SHugues Fruchet 	ret = ov5640_set_power_on(sensor);
25160f7acb52SHugues Fruchet 	if (ret)
25170f7acb52SHugues Fruchet 		return ret;
25180f7acb52SHugues Fruchet 
25190f7acb52SHugues Fruchet 	ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
25200f7acb52SHugues Fruchet 	if (ret) {
25210f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: failed to read chip identifier\n",
25220f7acb52SHugues Fruchet 			__func__);
25230f7acb52SHugues Fruchet 		goto power_off;
25240f7acb52SHugues Fruchet 	}
25250f7acb52SHugues Fruchet 
25260f7acb52SHugues Fruchet 	if (chip_id != 0x5640) {
25270f7acb52SHugues Fruchet 		dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
25280f7acb52SHugues Fruchet 			__func__, chip_id);
25290f7acb52SHugues Fruchet 		ret = -ENXIO;
25300f7acb52SHugues Fruchet 	}
25310f7acb52SHugues Fruchet 
25320f7acb52SHugues Fruchet power_off:
25330f7acb52SHugues Fruchet 	ov5640_set_power_off(sensor);
25340f7acb52SHugues Fruchet 	return ret;
25350f7acb52SHugues Fruchet }
25360f7acb52SHugues Fruchet 
253719a81c14SSteve Longerbeam static int ov5640_probe(struct i2c_client *client,
253819a81c14SSteve Longerbeam 			const struct i2c_device_id *id)
253919a81c14SSteve Longerbeam {
254019a81c14SSteve Longerbeam 	struct device *dev = &client->dev;
254119a81c14SSteve Longerbeam 	struct fwnode_handle *endpoint;
254219a81c14SSteve Longerbeam 	struct ov5640_dev *sensor;
2543e6441fdeSHugues Fruchet 	struct v4l2_mbus_framefmt *fmt;
254419a81c14SSteve Longerbeam 	int ret;
254519a81c14SSteve Longerbeam 
254619a81c14SSteve Longerbeam 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
254719a81c14SSteve Longerbeam 	if (!sensor)
254819a81c14SSteve Longerbeam 		return -ENOMEM;
254919a81c14SSteve Longerbeam 
255019a81c14SSteve Longerbeam 	sensor->i2c_client = client;
2551e6441fdeSHugues Fruchet 	fmt = &sensor->fmt;
2552e6441fdeSHugues Fruchet 	fmt->code = ov5640_formats[0].code;
2553e6441fdeSHugues Fruchet 	fmt->colorspace = ov5640_formats[0].colorspace;
2554e6441fdeSHugues Fruchet 	fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2555e6441fdeSHugues Fruchet 	fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2556e6441fdeSHugues Fruchet 	fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2557e6441fdeSHugues Fruchet 	fmt->width = 640;
2558e6441fdeSHugues Fruchet 	fmt->height = 480;
2559e6441fdeSHugues Fruchet 	fmt->field = V4L2_FIELD_NONE;
256019a81c14SSteve Longerbeam 	sensor->frame_interval.numerator = 1;
256119a81c14SSteve Longerbeam 	sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
256219a81c14SSteve Longerbeam 	sensor->current_fr = OV5640_30_FPS;
256319a81c14SSteve Longerbeam 	sensor->current_mode =
256419a81c14SSteve Longerbeam 		&ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
256519a81c14SSteve Longerbeam 	sensor->pending_mode_change = true;
256619a81c14SSteve Longerbeam 
256719a81c14SSteve Longerbeam 	sensor->ae_target = 52;
256819a81c14SSteve Longerbeam 
2569ce96bcf5SSakari Ailus 	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2570ce96bcf5SSakari Ailus 						  NULL);
257119a81c14SSteve Longerbeam 	if (!endpoint) {
257219a81c14SSteve Longerbeam 		dev_err(dev, "endpoint node not found\n");
257319a81c14SSteve Longerbeam 		return -EINVAL;
257419a81c14SSteve Longerbeam 	}
257519a81c14SSteve Longerbeam 
257619a81c14SSteve Longerbeam 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
257719a81c14SSteve Longerbeam 	fwnode_handle_put(endpoint);
257819a81c14SSteve Longerbeam 	if (ret) {
257919a81c14SSteve Longerbeam 		dev_err(dev, "Could not parse endpoint\n");
258019a81c14SSteve Longerbeam 		return ret;
258119a81c14SSteve Longerbeam 	}
258219a81c14SSteve Longerbeam 
258319a81c14SSteve Longerbeam 	/* get system clock (xclk) */
258419a81c14SSteve Longerbeam 	sensor->xclk = devm_clk_get(dev, "xclk");
258519a81c14SSteve Longerbeam 	if (IS_ERR(sensor->xclk)) {
258619a81c14SSteve Longerbeam 		dev_err(dev, "failed to get xclk\n");
258719a81c14SSteve Longerbeam 		return PTR_ERR(sensor->xclk);
258819a81c14SSteve Longerbeam 	}
258919a81c14SSteve Longerbeam 
259019a81c14SSteve Longerbeam 	sensor->xclk_freq = clk_get_rate(sensor->xclk);
259119a81c14SSteve Longerbeam 	if (sensor->xclk_freq < OV5640_XCLK_MIN ||
259219a81c14SSteve Longerbeam 	    sensor->xclk_freq > OV5640_XCLK_MAX) {
259319a81c14SSteve Longerbeam 		dev_err(dev, "xclk frequency out of range: %d Hz\n",
259419a81c14SSteve Longerbeam 			sensor->xclk_freq);
259519a81c14SSteve Longerbeam 		return -EINVAL;
259619a81c14SSteve Longerbeam 	}
259719a81c14SSteve Longerbeam 
259819a81c14SSteve Longerbeam 	/* request optional power down pin */
259919a81c14SSteve Longerbeam 	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
260019a81c14SSteve Longerbeam 						    GPIOD_OUT_HIGH);
260119a81c14SSteve Longerbeam 	/* request optional reset pin */
260219a81c14SSteve Longerbeam 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
260319a81c14SSteve Longerbeam 						     GPIOD_OUT_HIGH);
260419a81c14SSteve Longerbeam 
260519a81c14SSteve Longerbeam 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
260619a81c14SSteve Longerbeam 
2607dae82d9dSAkinobu Mita 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
260819a81c14SSteve Longerbeam 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
260919a81c14SSteve Longerbeam 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
261019a81c14SSteve Longerbeam 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
261119a81c14SSteve Longerbeam 	if (ret)
261219a81c14SSteve Longerbeam 		return ret;
261319a81c14SSteve Longerbeam 
261419a81c14SSteve Longerbeam 	ret = ov5640_get_regulators(sensor);
261519a81c14SSteve Longerbeam 	if (ret)
261619a81c14SSteve Longerbeam 		return ret;
261719a81c14SSteve Longerbeam 
261819a81c14SSteve Longerbeam 	mutex_init(&sensor->lock);
261919a81c14SSteve Longerbeam 
26200f7acb52SHugues Fruchet 	ret = ov5640_check_chip_id(sensor);
26210f7acb52SHugues Fruchet 	if (ret)
26220f7acb52SHugues Fruchet 		goto entity_cleanup;
26230f7acb52SHugues Fruchet 
262419a81c14SSteve Longerbeam 	ret = ov5640_init_controls(sensor);
262519a81c14SSteve Longerbeam 	if (ret)
262619a81c14SSteve Longerbeam 		goto entity_cleanup;
262719a81c14SSteve Longerbeam 
262819a81c14SSteve Longerbeam 	ret = v4l2_async_register_subdev(&sensor->sd);
262919a81c14SSteve Longerbeam 	if (ret)
263019a81c14SSteve Longerbeam 		goto free_ctrls;
263119a81c14SSteve Longerbeam 
263219a81c14SSteve Longerbeam 	return 0;
263319a81c14SSteve Longerbeam 
263419a81c14SSteve Longerbeam free_ctrls:
263519a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
263619a81c14SSteve Longerbeam entity_cleanup:
263719a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
263819a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
263919a81c14SSteve Longerbeam 	return ret;
264019a81c14SSteve Longerbeam }
264119a81c14SSteve Longerbeam 
264219a81c14SSteve Longerbeam static int ov5640_remove(struct i2c_client *client)
264319a81c14SSteve Longerbeam {
264419a81c14SSteve Longerbeam 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
264519a81c14SSteve Longerbeam 	struct ov5640_dev *sensor = to_ov5640_dev(sd);
264619a81c14SSteve Longerbeam 
264719a81c14SSteve Longerbeam 	v4l2_async_unregister_subdev(&sensor->sd);
264819a81c14SSteve Longerbeam 	mutex_destroy(&sensor->lock);
264919a81c14SSteve Longerbeam 	media_entity_cleanup(&sensor->sd.entity);
265019a81c14SSteve Longerbeam 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
265119a81c14SSteve Longerbeam 
265219a81c14SSteve Longerbeam 	return 0;
265319a81c14SSteve Longerbeam }
265419a81c14SSteve Longerbeam 
265519a81c14SSteve Longerbeam static const struct i2c_device_id ov5640_id[] = {
265619a81c14SSteve Longerbeam 	{"ov5640", 0},
265719a81c14SSteve Longerbeam 	{},
265819a81c14SSteve Longerbeam };
265919a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(i2c, ov5640_id);
266019a81c14SSteve Longerbeam 
266119a81c14SSteve Longerbeam static const struct of_device_id ov5640_dt_ids[] = {
266219a81c14SSteve Longerbeam 	{ .compatible = "ovti,ov5640" },
266319a81c14SSteve Longerbeam 	{ /* sentinel */ }
266419a81c14SSteve Longerbeam };
266519a81c14SSteve Longerbeam MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
266619a81c14SSteve Longerbeam 
266719a81c14SSteve Longerbeam static struct i2c_driver ov5640_i2c_driver = {
266819a81c14SSteve Longerbeam 	.driver = {
266919a81c14SSteve Longerbeam 		.name  = "ov5640",
267019a81c14SSteve Longerbeam 		.of_match_table	= ov5640_dt_ids,
267119a81c14SSteve Longerbeam 	},
267219a81c14SSteve Longerbeam 	.id_table = ov5640_id,
267319a81c14SSteve Longerbeam 	.probe    = ov5640_probe,
267419a81c14SSteve Longerbeam 	.remove   = ov5640_remove,
267519a81c14SSteve Longerbeam };
267619a81c14SSteve Longerbeam 
267719a81c14SSteve Longerbeam module_i2c_driver(ov5640_i2c_driver);
267819a81c14SSteve Longerbeam 
267919a81c14SSteve Longerbeam MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
268019a81c14SSteve Longerbeam MODULE_LICENSE("GPL");
2681