xref: /linux/drivers/media/i2c/ov5693.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
189aef879SDaniel Scally // SPDX-License-Identifier: GPL-2.0
289aef879SDaniel Scally /*
389aef879SDaniel Scally  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
489aef879SDaniel Scally  *
589aef879SDaniel Scally  * Adapted from the atomisp-ov5693 driver, with contributions from:
689aef879SDaniel Scally  *
789aef879SDaniel Scally  * Daniel Scally
889aef879SDaniel Scally  * Jean-Michel Hautbois
989aef879SDaniel Scally  * Fabian Wuthrich
1089aef879SDaniel Scally  * Tsuchiya Yuto
1189aef879SDaniel Scally  * Jordan Hand
1289aef879SDaniel Scally  * Jake Day
1389aef879SDaniel Scally  */
1489aef879SDaniel Scally 
1589aef879SDaniel Scally #include <linux/acpi.h>
1689aef879SDaniel Scally #include <linux/clk.h>
1789aef879SDaniel Scally #include <linux/delay.h>
1889aef879SDaniel Scally #include <linux/device.h>
1989aef879SDaniel Scally #include <linux/i2c.h>
2089aef879SDaniel Scally #include <linux/module.h>
2189aef879SDaniel Scally #include <linux/pm_runtime.h>
2289aef879SDaniel Scally #include <linux/regulator/consumer.h>
2389aef879SDaniel Scally #include <linux/slab.h>
2489aef879SDaniel Scally #include <linux/types.h>
25f3a5e2ccSHans de Goede 
26f3a5e2ccSHans de Goede #include <media/v4l2-cci.h>
2789aef879SDaniel Scally #include <media/v4l2-ctrls.h>
2889aef879SDaniel Scally #include <media/v4l2-device.h>
2989aef879SDaniel Scally #include <media/v4l2-fwnode.h>
3089aef879SDaniel Scally 
3189aef879SDaniel Scally /* System Control */
32f3a5e2ccSHans de Goede #define OV5693_SW_RESET_REG			CCI_REG8(0x0103)
33f3a5e2ccSHans de Goede #define OV5693_SW_STREAM_REG			CCI_REG8(0x0100)
3489aef879SDaniel Scally #define OV5693_START_STREAMING			0x01
3589aef879SDaniel Scally #define OV5693_STOP_STREAMING			0x00
3689aef879SDaniel Scally #define OV5693_SW_RESET				0x01
3789aef879SDaniel Scally 
38f3a5e2ccSHans de Goede #define OV5693_REG_CHIP_ID			CCI_REG16(0x300a)
3989aef879SDaniel Scally /* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
4089aef879SDaniel Scally #define OV5693_CHIP_ID				0x5690
4189aef879SDaniel Scally 
4289aef879SDaniel Scally /* Exposure */
43f3a5e2ccSHans de Goede #define OV5693_EXPOSURE_CTRL_REG		CCI_REG24(0x3500)
4489aef879SDaniel Scally #define OV5693_EXPOSURE_CTRL_MASK		GENMASK(19, 4)
4589aef879SDaniel Scally #define OV5693_INTEGRATION_TIME_MARGIN		8
4689aef879SDaniel Scally #define OV5693_EXPOSURE_MIN			1
4789aef879SDaniel Scally #define OV5693_EXPOSURE_STEP			1
4889aef879SDaniel Scally 
4989aef879SDaniel Scally /* Analogue Gain */
50f3a5e2ccSHans de Goede #define OV5693_GAIN_CTRL_REG			CCI_REG16(0x350a)
5189aef879SDaniel Scally #define OV5693_GAIN_CTRL_MASK			GENMASK(10, 4)
5289aef879SDaniel Scally #define OV5693_GAIN_MIN				1
5389aef879SDaniel Scally #define OV5693_GAIN_MAX				127
5489aef879SDaniel Scally #define OV5693_GAIN_DEF				8
5589aef879SDaniel Scally #define OV5693_GAIN_STEP			1
5689aef879SDaniel Scally 
5789aef879SDaniel Scally /* Digital Gain */
58f3a5e2ccSHans de Goede #define OV5693_MWB_RED_GAIN_REG			CCI_REG16(0x3400)
59f3a5e2ccSHans de Goede #define OV5693_MWB_GREEN_GAIN_REG		CCI_REG16(0x3402)
60f3a5e2ccSHans de Goede #define OV5693_MWB_BLUE_GAIN_REG		CCI_REG16(0x3404)
6189aef879SDaniel Scally #define OV5693_MWB_GAIN_MASK			GENMASK(11, 0)
6289aef879SDaniel Scally #define OV5693_MWB_GAIN_MAX			0x0fff
6389aef879SDaniel Scally #define OV5693_DIGITAL_GAIN_MIN			1
6489aef879SDaniel Scally #define OV5693_DIGITAL_GAIN_MAX			4095
6589aef879SDaniel Scally #define OV5693_DIGITAL_GAIN_DEF			1024
6689aef879SDaniel Scally #define OV5693_DIGITAL_GAIN_STEP		1
6789aef879SDaniel Scally 
6889aef879SDaniel Scally /* Timing and Format */
69f3a5e2ccSHans de Goede #define OV5693_CROP_START_X_REG			CCI_REG16(0x3800)
70f3a5e2ccSHans de Goede #define OV5693_CROP_START_Y_REG			CCI_REG16(0x3802)
71f3a5e2ccSHans de Goede #define OV5693_CROP_END_X_REG			CCI_REG16(0x3804)
72f3a5e2ccSHans de Goede #define OV5693_CROP_END_Y_REG			CCI_REG16(0x3806)
73f3a5e2ccSHans de Goede #define OV5693_OUTPUT_SIZE_X_REG		CCI_REG16(0x3808)
74f3a5e2ccSHans de Goede #define OV5693_OUTPUT_SIZE_Y_REG		CCI_REG16(0x380a)
7589aef879SDaniel Scally 
76f3a5e2ccSHans de Goede #define OV5693_TIMING_HTS_REG			CCI_REG16(0x380c)
7789aef879SDaniel Scally #define OV5693_FIXED_PPL			2688U
78f3a5e2ccSHans de Goede #define OV5693_TIMING_VTS_REG			CCI_REG16(0x380e)
7989aef879SDaniel Scally #define OV5693_TIMING_MAX_VTS			0xffff
8089aef879SDaniel Scally #define OV5693_TIMING_MIN_VTS			0x04
8189aef879SDaniel Scally 
82f3a5e2ccSHans de Goede #define OV5693_OFFSET_START_X_REG		CCI_REG16(0x3810)
83f3a5e2ccSHans de Goede #define OV5693_OFFSET_START_Y_REG		CCI_REG16(0x3812)
8489aef879SDaniel Scally 
85f3a5e2ccSHans de Goede #define OV5693_SUB_INC_X_REG			CCI_REG8(0x3814)
86f3a5e2ccSHans de Goede #define OV5693_SUB_INC_Y_REG			CCI_REG8(0x3815)
8789aef879SDaniel Scally 
88f3a5e2ccSHans de Goede #define OV5693_FORMAT1_REG			CCI_REG8(0x3820)
8989aef879SDaniel Scally #define OV5693_FORMAT1_FLIP_VERT_ISP_EN		BIT(6)
9089aef879SDaniel Scally #define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN	BIT(1)
9189aef879SDaniel Scally #define OV5693_FORMAT1_VBIN_EN			BIT(0)
92f3a5e2ccSHans de Goede #define OV5693_FORMAT2_REG			CCI_REG8(0x3821)
9389aef879SDaniel Scally #define OV5693_FORMAT2_HDR_EN			BIT(7)
9489aef879SDaniel Scally #define OV5693_FORMAT2_FLIP_HORZ_ISP_EN		BIT(2)
9589aef879SDaniel Scally #define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN	BIT(1)
9689aef879SDaniel Scally #define OV5693_FORMAT2_HBIN_EN			BIT(0)
9789aef879SDaniel Scally 
98f3a5e2ccSHans de Goede #define OV5693_ISP_CTRL2_REG			CCI_REG8(0x5002)
9989aef879SDaniel Scally #define OV5693_ISP_SCALE_ENABLE			BIT(7)
10089aef879SDaniel Scally 
10189aef879SDaniel Scally /* Pixel Array */
10289aef879SDaniel Scally #define OV5693_NATIVE_WIDTH			2624
10389aef879SDaniel Scally #define OV5693_NATIVE_HEIGHT			1956
10489aef879SDaniel Scally #define OV5693_NATIVE_START_LEFT		0
10589aef879SDaniel Scally #define OV5693_NATIVE_START_TOP			0
10689aef879SDaniel Scally #define OV5693_ACTIVE_WIDTH			2592
10789aef879SDaniel Scally #define OV5693_ACTIVE_HEIGHT			1944
10889aef879SDaniel Scally #define OV5693_ACTIVE_START_LEFT		16
10989aef879SDaniel Scally #define OV5693_ACTIVE_START_TOP			6
11089aef879SDaniel Scally #define OV5693_MIN_CROP_WIDTH			2
11189aef879SDaniel Scally #define OV5693_MIN_CROP_HEIGHT			2
11289aef879SDaniel Scally 
11389aef879SDaniel Scally /* Test Pattern */
114f3a5e2ccSHans de Goede #define OV5693_TEST_PATTERN_REG			CCI_REG8(0x5e00)
11589aef879SDaniel Scally #define OV5693_TEST_PATTERN_ENABLE		BIT(7)
11689aef879SDaniel Scally #define OV5693_TEST_PATTERN_ROLLING		BIT(6)
11789aef879SDaniel Scally #define OV5693_TEST_PATTERN_RANDOM		0x01
11889aef879SDaniel Scally #define OV5693_TEST_PATTERN_BARS		0x00
11989aef879SDaniel Scally 
12089aef879SDaniel Scally /* System Frequencies */
12189aef879SDaniel Scally #define OV5693_XVCLK_FREQ			19200000
12289aef879SDaniel Scally #define OV5693_LINK_FREQ_419_2MHZ		419200000
12389aef879SDaniel Scally #define OV5693_PIXEL_RATE			167680000
12489aef879SDaniel Scally 
12589aef879SDaniel Scally #define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)
12689aef879SDaniel Scally 
127cfdb1954STommaso Merciai static const char * const ov5693_supply_names[] = {
128cfdb1954STommaso Merciai 	"avdd",		/* Analog power */
129cfdb1954STommaso Merciai 	"dovdd",	/* Digital I/O power */
1306ae8701fSTommaso Merciai 	"dvdd",		/* Digital circuit power */
131cfdb1954STommaso Merciai };
132cfdb1954STommaso Merciai 
133cfdb1954STommaso Merciai #define OV5693_NUM_SUPPLIES	ARRAY_SIZE(ov5693_supply_names)
134cfdb1954STommaso Merciai 
13589aef879SDaniel Scally struct ov5693_device {
13689aef879SDaniel Scally 	struct device *dev;
137f3a5e2ccSHans de Goede 	struct regmap *regmap;
13889aef879SDaniel Scally 
13989aef879SDaniel Scally 	/* Protect against concurrent changes to controls */
14089aef879SDaniel Scally 	struct mutex lock;
14189aef879SDaniel Scally 
14289aef879SDaniel Scally 	struct gpio_desc *reset;
14389aef879SDaniel Scally 	struct gpio_desc *powerdown;
14489aef879SDaniel Scally 	struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
1458a47d09eSTommaso Merciai 	struct clk *xvclk;
14689aef879SDaniel Scally 
14789aef879SDaniel Scally 	struct ov5693_mode {
14889aef879SDaniel Scally 		struct v4l2_rect crop;
14989aef879SDaniel Scally 		struct v4l2_mbus_framefmt format;
15089aef879SDaniel Scally 		bool binning_x;
15189aef879SDaniel Scally 		bool binning_y;
15289aef879SDaniel Scally 		unsigned int inc_x_odd;
15389aef879SDaniel Scally 		unsigned int inc_y_odd;
15489aef879SDaniel Scally 		unsigned int vts;
15589aef879SDaniel Scally 	} mode;
15689aef879SDaniel Scally 
15789aef879SDaniel Scally 	struct v4l2_subdev sd;
15889aef879SDaniel Scally 	struct media_pad pad;
15989aef879SDaniel Scally 
16089aef879SDaniel Scally 	struct ov5693_v4l2_ctrls {
16189aef879SDaniel Scally 		struct v4l2_ctrl_handler handler;
16289aef879SDaniel Scally 		struct v4l2_ctrl *link_freq;
16389aef879SDaniel Scally 		struct v4l2_ctrl *pixel_rate;
16489aef879SDaniel Scally 		struct v4l2_ctrl *exposure;
16589aef879SDaniel Scally 		struct v4l2_ctrl *analogue_gain;
16689aef879SDaniel Scally 		struct v4l2_ctrl *digital_gain;
16789aef879SDaniel Scally 		struct v4l2_ctrl *hflip;
16889aef879SDaniel Scally 		struct v4l2_ctrl *vflip;
16989aef879SDaniel Scally 		struct v4l2_ctrl *hblank;
17089aef879SDaniel Scally 		struct v4l2_ctrl *vblank;
17189aef879SDaniel Scally 		struct v4l2_ctrl *test_pattern;
17289aef879SDaniel Scally 	} ctrls;
17389aef879SDaniel Scally };
17489aef879SDaniel Scally 
175f3a5e2ccSHans de Goede static const struct cci_reg_sequence ov5693_global_regs[] = {
176f3a5e2ccSHans de Goede 	{CCI_REG8(0x3016), 0xf0},
177f3a5e2ccSHans de Goede 	{CCI_REG8(0x3017), 0xf0},
178f3a5e2ccSHans de Goede 	{CCI_REG8(0x3018), 0xf0},
179f3a5e2ccSHans de Goede 	{CCI_REG8(0x3022), 0x01},
180f3a5e2ccSHans de Goede 	{CCI_REG8(0x3028), 0x44},
181f3a5e2ccSHans de Goede 	{CCI_REG8(0x3098), 0x02},
182f3a5e2ccSHans de Goede 	{CCI_REG8(0x3099), 0x19},
183f3a5e2ccSHans de Goede 	{CCI_REG8(0x309a), 0x02},
184f3a5e2ccSHans de Goede 	{CCI_REG8(0x309b), 0x01},
185f3a5e2ccSHans de Goede 	{CCI_REG8(0x309c), 0x00},
186f3a5e2ccSHans de Goede 	{CCI_REG8(0x30a0), 0xd2},
187f3a5e2ccSHans de Goede 	{CCI_REG8(0x30a2), 0x01},
188f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b2), 0x00},
189f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b3), 0x83},
190f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b4), 0x03},
191f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b5), 0x04},
192f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b6), 0x01},
193f3a5e2ccSHans de Goede 	{CCI_REG8(0x3080), 0x01},
194f3a5e2ccSHans de Goede 	{CCI_REG8(0x3104), 0x21},
195f3a5e2ccSHans de Goede 	{CCI_REG8(0x3106), 0x00},
196f3a5e2ccSHans de Goede 	{CCI_REG8(0x3406), 0x01},
197f3a5e2ccSHans de Goede 	{CCI_REG8(0x3503), 0x07},
198f3a5e2ccSHans de Goede 	{CCI_REG8(0x350b), 0x40},
199f3a5e2ccSHans de Goede 	{CCI_REG8(0x3601), 0x0a},
200f3a5e2ccSHans de Goede 	{CCI_REG8(0x3602), 0x38},
201f3a5e2ccSHans de Goede 	{CCI_REG8(0x3612), 0x80},
202f3a5e2ccSHans de Goede 	{CCI_REG8(0x3620), 0x54},
203f3a5e2ccSHans de Goede 	{CCI_REG8(0x3621), 0xc7},
204f3a5e2ccSHans de Goede 	{CCI_REG8(0x3622), 0x0f},
205f3a5e2ccSHans de Goede 	{CCI_REG8(0x3625), 0x10},
206f3a5e2ccSHans de Goede 	{CCI_REG8(0x3630), 0x55},
207f3a5e2ccSHans de Goede 	{CCI_REG8(0x3631), 0xf4},
208f3a5e2ccSHans de Goede 	{CCI_REG8(0x3632), 0x00},
209f3a5e2ccSHans de Goede 	{CCI_REG8(0x3633), 0x34},
210f3a5e2ccSHans de Goede 	{CCI_REG8(0x3634), 0x02},
211f3a5e2ccSHans de Goede 	{CCI_REG8(0x364d), 0x0d},
212f3a5e2ccSHans de Goede 	{CCI_REG8(0x364f), 0xdd},
213f3a5e2ccSHans de Goede 	{CCI_REG8(0x3660), 0x04},
214f3a5e2ccSHans de Goede 	{CCI_REG8(0x3662), 0x10},
215f3a5e2ccSHans de Goede 	{CCI_REG8(0x3663), 0xf1},
216f3a5e2ccSHans de Goede 	{CCI_REG8(0x3665), 0x00},
217f3a5e2ccSHans de Goede 	{CCI_REG8(0x3666), 0x20},
218f3a5e2ccSHans de Goede 	{CCI_REG8(0x3667), 0x00},
219f3a5e2ccSHans de Goede 	{CCI_REG8(0x366a), 0x80},
220f3a5e2ccSHans de Goede 	{CCI_REG8(0x3680), 0xe0},
221f3a5e2ccSHans de Goede 	{CCI_REG8(0x3681), 0x00},
222f3a5e2ccSHans de Goede 	{CCI_REG8(0x3700), 0x42},
223f3a5e2ccSHans de Goede 	{CCI_REG8(0x3701), 0x14},
224f3a5e2ccSHans de Goede 	{CCI_REG8(0x3702), 0xa0},
225f3a5e2ccSHans de Goede 	{CCI_REG8(0x3703), 0xd8},
226f3a5e2ccSHans de Goede 	{CCI_REG8(0x3704), 0x78},
227f3a5e2ccSHans de Goede 	{CCI_REG8(0x3705), 0x02},
228f3a5e2ccSHans de Goede 	{CCI_REG8(0x370a), 0x00},
229f3a5e2ccSHans de Goede 	{CCI_REG8(0x370b), 0x20},
230f3a5e2ccSHans de Goede 	{CCI_REG8(0x370c), 0x0c},
231f3a5e2ccSHans de Goede 	{CCI_REG8(0x370d), 0x11},
232f3a5e2ccSHans de Goede 	{CCI_REG8(0x370e), 0x00},
233f3a5e2ccSHans de Goede 	{CCI_REG8(0x370f), 0x40},
234f3a5e2ccSHans de Goede 	{CCI_REG8(0x3710), 0x00},
235f3a5e2ccSHans de Goede 	{CCI_REG8(0x371a), 0x1c},
236f3a5e2ccSHans de Goede 	{CCI_REG8(0x371b), 0x05},
237f3a5e2ccSHans de Goede 	{CCI_REG8(0x371c), 0x01},
238f3a5e2ccSHans de Goede 	{CCI_REG8(0x371e), 0xa1},
239f3a5e2ccSHans de Goede 	{CCI_REG8(0x371f), 0x0c},
240f3a5e2ccSHans de Goede 	{CCI_REG8(0x3721), 0x00},
241f3a5e2ccSHans de Goede 	{CCI_REG8(0x3724), 0x10},
242f3a5e2ccSHans de Goede 	{CCI_REG8(0x3726), 0x00},
243f3a5e2ccSHans de Goede 	{CCI_REG8(0x372a), 0x01},
244f3a5e2ccSHans de Goede 	{CCI_REG8(0x3730), 0x10},
245f3a5e2ccSHans de Goede 	{CCI_REG8(0x3738), 0x22},
246f3a5e2ccSHans de Goede 	{CCI_REG8(0x3739), 0xe5},
247f3a5e2ccSHans de Goede 	{CCI_REG8(0x373a), 0x50},
248f3a5e2ccSHans de Goede 	{CCI_REG8(0x373b), 0x02},
249f3a5e2ccSHans de Goede 	{CCI_REG8(0x373c), 0x41},
250f3a5e2ccSHans de Goede 	{CCI_REG8(0x373f), 0x02},
251f3a5e2ccSHans de Goede 	{CCI_REG8(0x3740), 0x42},
252f3a5e2ccSHans de Goede 	{CCI_REG8(0x3741), 0x02},
253f3a5e2ccSHans de Goede 	{CCI_REG8(0x3742), 0x18},
254f3a5e2ccSHans de Goede 	{CCI_REG8(0x3743), 0x01},
255f3a5e2ccSHans de Goede 	{CCI_REG8(0x3744), 0x02},
256f3a5e2ccSHans de Goede 	{CCI_REG8(0x3747), 0x10},
257f3a5e2ccSHans de Goede 	{CCI_REG8(0x374c), 0x04},
258f3a5e2ccSHans de Goede 	{CCI_REG8(0x3751), 0xf0},
259f3a5e2ccSHans de Goede 	{CCI_REG8(0x3752), 0x00},
260f3a5e2ccSHans de Goede 	{CCI_REG8(0x3753), 0x00},
261f3a5e2ccSHans de Goede 	{CCI_REG8(0x3754), 0xc0},
262f3a5e2ccSHans de Goede 	{CCI_REG8(0x3755), 0x00},
263f3a5e2ccSHans de Goede 	{CCI_REG8(0x3756), 0x1a},
264f3a5e2ccSHans de Goede 	{CCI_REG8(0x3758), 0x00},
265f3a5e2ccSHans de Goede 	{CCI_REG8(0x3759), 0x0f},
266f3a5e2ccSHans de Goede 	{CCI_REG8(0x376b), 0x44},
267f3a5e2ccSHans de Goede 	{CCI_REG8(0x375c), 0x04},
268f3a5e2ccSHans de Goede 	{CCI_REG8(0x3774), 0x10},
269f3a5e2ccSHans de Goede 	{CCI_REG8(0x3776), 0x00},
270f3a5e2ccSHans de Goede 	{CCI_REG8(0x377f), 0x08},
271f3a5e2ccSHans de Goede 	{CCI_REG8(0x3780), 0x22},
272f3a5e2ccSHans de Goede 	{CCI_REG8(0x3781), 0x0c},
273f3a5e2ccSHans de Goede 	{CCI_REG8(0x3784), 0x2c},
274f3a5e2ccSHans de Goede 	{CCI_REG8(0x3785), 0x1e},
275f3a5e2ccSHans de Goede 	{CCI_REG8(0x378f), 0xf5},
276f3a5e2ccSHans de Goede 	{CCI_REG8(0x3791), 0xb0},
277f3a5e2ccSHans de Goede 	{CCI_REG8(0x3795), 0x00},
278f3a5e2ccSHans de Goede 	{CCI_REG8(0x3796), 0x64},
279f3a5e2ccSHans de Goede 	{CCI_REG8(0x3797), 0x11},
280f3a5e2ccSHans de Goede 	{CCI_REG8(0x3798), 0x30},
281f3a5e2ccSHans de Goede 	{CCI_REG8(0x3799), 0x41},
282f3a5e2ccSHans de Goede 	{CCI_REG8(0x379a), 0x07},
283f3a5e2ccSHans de Goede 	{CCI_REG8(0x379b), 0xb0},
284f3a5e2ccSHans de Goede 	{CCI_REG8(0x379c), 0x0c},
285f3a5e2ccSHans de Goede 	{CCI_REG8(0x3a04), 0x06},
286f3a5e2ccSHans de Goede 	{CCI_REG8(0x3a05), 0x14},
287f3a5e2ccSHans de Goede 	{CCI_REG8(0x3e07), 0x20},
288f3a5e2ccSHans de Goede 	{CCI_REG8(0x4000), 0x08},
289f3a5e2ccSHans de Goede 	{CCI_REG8(0x4001), 0x04},
290f3a5e2ccSHans de Goede 	{CCI_REG8(0x4004), 0x08},
291f3a5e2ccSHans de Goede 	{CCI_REG8(0x4006), 0x20},
292f3a5e2ccSHans de Goede 	{CCI_REG8(0x4008), 0x24},
293f3a5e2ccSHans de Goede 	{CCI_REG8(0x4009), 0x10},
294f3a5e2ccSHans de Goede 	{CCI_REG8(0x4058), 0x00},
295f3a5e2ccSHans de Goede 	{CCI_REG8(0x4101), 0xb2},
296f3a5e2ccSHans de Goede 	{CCI_REG8(0x4307), 0x31},
297f3a5e2ccSHans de Goede 	{CCI_REG8(0x4511), 0x05},
298f3a5e2ccSHans de Goede 	{CCI_REG8(0x4512), 0x01},
299f3a5e2ccSHans de Goede 	{CCI_REG8(0x481f), 0x30},
300f3a5e2ccSHans de Goede 	{CCI_REG8(0x4826), 0x2c},
301f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d02), 0xfd},
302f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d03), 0xf5},
303f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d04), 0x0c},
304f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d05), 0xcc},
305f3a5e2ccSHans de Goede 	{CCI_REG8(0x4837), 0x0a},
306f3a5e2ccSHans de Goede 	{CCI_REG8(0x5003), 0x20},
307f3a5e2ccSHans de Goede 	{CCI_REG8(0x5013), 0x00},
308f3a5e2ccSHans de Goede 	{CCI_REG8(0x5842), 0x01},
309f3a5e2ccSHans de Goede 	{CCI_REG8(0x5843), 0x2b},
310f3a5e2ccSHans de Goede 	{CCI_REG8(0x5844), 0x01},
311f3a5e2ccSHans de Goede 	{CCI_REG8(0x5845), 0x92},
312f3a5e2ccSHans de Goede 	{CCI_REG8(0x5846), 0x01},
313f3a5e2ccSHans de Goede 	{CCI_REG8(0x5847), 0x8f},
314f3a5e2ccSHans de Goede 	{CCI_REG8(0x5848), 0x01},
315f3a5e2ccSHans de Goede 	{CCI_REG8(0x5849), 0x0c},
316f3a5e2ccSHans de Goede 	{CCI_REG8(0x5e10), 0x0c},
317f3a5e2ccSHans de Goede 	{CCI_REG8(0x3820), 0x00},
318f3a5e2ccSHans de Goede 	{CCI_REG8(0x3821), 0x1e},
319f3a5e2ccSHans de Goede 	{CCI_REG8(0x5041), 0x14}
32089aef879SDaniel Scally };
32189aef879SDaniel Scally 
32289aef879SDaniel Scally static const struct v4l2_rect ov5693_default_crop = {
32389aef879SDaniel Scally 	.left = OV5693_ACTIVE_START_LEFT,
32489aef879SDaniel Scally 	.top = OV5693_ACTIVE_START_TOP,
32589aef879SDaniel Scally 	.width = OV5693_ACTIVE_WIDTH,
32689aef879SDaniel Scally 	.height = OV5693_ACTIVE_HEIGHT,
32789aef879SDaniel Scally };
32889aef879SDaniel Scally 
32989aef879SDaniel Scally static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
33089aef879SDaniel Scally 	.width = OV5693_ACTIVE_WIDTH,
33189aef879SDaniel Scally 	.height = OV5693_ACTIVE_HEIGHT,
33289aef879SDaniel Scally 	.code = MEDIA_BUS_FMT_SBGGR10_1X10,
33389aef879SDaniel Scally };
33489aef879SDaniel Scally 
33589aef879SDaniel Scally static const s64 link_freq_menu_items[] = {
33689aef879SDaniel Scally 	OV5693_LINK_FREQ_419_2MHZ
33789aef879SDaniel Scally };
33889aef879SDaniel Scally 
33989aef879SDaniel Scally static const char * const ov5693_test_pattern_menu[] = {
34089aef879SDaniel Scally 	"Disabled",
34189aef879SDaniel Scally 	"Random Data",
34289aef879SDaniel Scally 	"Colour Bars",
34389aef879SDaniel Scally 	"Colour Bars with Rolling Bar"
34489aef879SDaniel Scally };
34589aef879SDaniel Scally 
34689aef879SDaniel Scally static const u8 ov5693_test_pattern_bits[] = {
34789aef879SDaniel Scally 	0,
34889aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
34989aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
35089aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
35189aef879SDaniel Scally 	OV5693_TEST_PATTERN_ROLLING,
35289aef879SDaniel Scally };
35389aef879SDaniel Scally 
35489aef879SDaniel Scally /* V4L2 Controls Functions */
35589aef879SDaniel Scally 
ov5693_flip_vert_configure(struct ov5693_device * ov5693,bool enable)35689aef879SDaniel Scally static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
35789aef879SDaniel Scally 				      bool enable)
35889aef879SDaniel Scally {
35989aef879SDaniel Scally 	u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
36089aef879SDaniel Scally 		  OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
36189aef879SDaniel Scally 	int ret;
36289aef879SDaniel Scally 
363f3a5e2ccSHans de Goede 	ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG, bits,
364f3a5e2ccSHans de Goede 			      enable ? bits : 0, NULL);
36589aef879SDaniel Scally 	if (ret)
36689aef879SDaniel Scally 		return ret;
36789aef879SDaniel Scally 
36889aef879SDaniel Scally 	return 0;
36989aef879SDaniel Scally }
37089aef879SDaniel Scally 
ov5693_flip_horz_configure(struct ov5693_device * ov5693,bool enable)37189aef879SDaniel Scally static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
37289aef879SDaniel Scally 				      bool enable)
37389aef879SDaniel Scally {
37489aef879SDaniel Scally 	u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
37589aef879SDaniel Scally 		  OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
37689aef879SDaniel Scally 	int ret;
37789aef879SDaniel Scally 
378f3a5e2ccSHans de Goede 	ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG, bits,
379f3a5e2ccSHans de Goede 			      enable ? bits : 0, NULL);
38089aef879SDaniel Scally 	if (ret)
38189aef879SDaniel Scally 		return ret;
38289aef879SDaniel Scally 
38389aef879SDaniel Scally 	return 0;
38489aef879SDaniel Scally }
38589aef879SDaniel Scally 
ov5693_get_exposure(struct ov5693_device * ov5693,s32 * value)38689aef879SDaniel Scally static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
38789aef879SDaniel Scally {
388f3a5e2ccSHans de Goede 	u64 exposure;
38989aef879SDaniel Scally 	int ret;
39089aef879SDaniel Scally 
391f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, &exposure,
392f3a5e2ccSHans de Goede 		       NULL);
39389aef879SDaniel Scally 	if (ret)
39489aef879SDaniel Scally 		return ret;
39589aef879SDaniel Scally 
39689aef879SDaniel Scally 	/* The lowest 4 bits are unsupported fractional bits */
39789aef879SDaniel Scally 	*value = exposure >> 4;
39889aef879SDaniel Scally 
39989aef879SDaniel Scally 	return 0;
40089aef879SDaniel Scally }
40189aef879SDaniel Scally 
ov5693_exposure_configure(struct ov5693_device * ov5693,u32 exposure)40289aef879SDaniel Scally static int ov5693_exposure_configure(struct ov5693_device *ov5693,
40389aef879SDaniel Scally 				     u32 exposure)
40489aef879SDaniel Scally {
40589aef879SDaniel Scally 	int ret = 0;
40689aef879SDaniel Scally 
40789aef879SDaniel Scally 	exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;
40889aef879SDaniel Scally 
409f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);
41089aef879SDaniel Scally 
41189aef879SDaniel Scally 	return ret;
41289aef879SDaniel Scally }
41389aef879SDaniel Scally 
ov5693_get_gain(struct ov5693_device * ov5693,u32 * gain)41489aef879SDaniel Scally static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
41589aef879SDaniel Scally {
416f3a5e2ccSHans de Goede 	u64 value;
41789aef879SDaniel Scally 	int ret;
41889aef879SDaniel Scally 
419f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_GAIN_CTRL_REG, &value, NULL);
42089aef879SDaniel Scally 	if (ret)
42189aef879SDaniel Scally 		return ret;
42289aef879SDaniel Scally 
42389aef879SDaniel Scally 	/* As with exposure, the lowest 4 bits are fractional bits. */
42489aef879SDaniel Scally 	*gain = value >> 4;
42589aef879SDaniel Scally 
42689aef879SDaniel Scally 	return ret;
42789aef879SDaniel Scally }
42889aef879SDaniel Scally 
ov5693_digital_gain_configure(struct ov5693_device * ov5693,u32 gain)42989aef879SDaniel Scally static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
43089aef879SDaniel Scally 					 u32 gain)
43189aef879SDaniel Scally {
43289aef879SDaniel Scally 	int ret = 0;
43389aef879SDaniel Scally 
43489aef879SDaniel Scally 	gain &= OV5693_MWB_GAIN_MASK;
43589aef879SDaniel Scally 
436f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_RED_GAIN_REG, gain, &ret);
437f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
438f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);
43989aef879SDaniel Scally 
44089aef879SDaniel Scally 	return ret;
44189aef879SDaniel Scally }
44289aef879SDaniel Scally 
ov5693_analog_gain_configure(struct ov5693_device * ov5693,u32 gain)44389aef879SDaniel Scally static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
44489aef879SDaniel Scally {
44589aef879SDaniel Scally 	int ret = 0;
44689aef879SDaniel Scally 
44789aef879SDaniel Scally 	gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;
44889aef879SDaniel Scally 
449f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_GAIN_CTRL_REG, gain, &ret);
45089aef879SDaniel Scally 
45189aef879SDaniel Scally 	return ret;
45289aef879SDaniel Scally }
45389aef879SDaniel Scally 
ov5693_vts_configure(struct ov5693_device * ov5693,u32 vblank)45489aef879SDaniel Scally static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
45589aef879SDaniel Scally {
45689aef879SDaniel Scally 	u16 vts = ov5693->mode.format.height + vblank;
45789aef879SDaniel Scally 	int ret = 0;
45889aef879SDaniel Scally 
459f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TIMING_VTS_REG, vts, &ret);
46089aef879SDaniel Scally 
46189aef879SDaniel Scally 	return ret;
46289aef879SDaniel Scally }
46389aef879SDaniel Scally 
ov5693_test_pattern_configure(struct ov5693_device * ov5693,u32 idx)46489aef879SDaniel Scally static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
46589aef879SDaniel Scally {
46689aef879SDaniel Scally 	int ret = 0;
46789aef879SDaniel Scally 
468f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TEST_PATTERN_REG,
46989aef879SDaniel Scally 		  ov5693_test_pattern_bits[idx], &ret);
47089aef879SDaniel Scally 
47189aef879SDaniel Scally 	return ret;
47289aef879SDaniel Scally }
47389aef879SDaniel Scally 
ov5693_s_ctrl(struct v4l2_ctrl * ctrl)47489aef879SDaniel Scally static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
47589aef879SDaniel Scally {
47689aef879SDaniel Scally 	struct ov5693_device *ov5693 =
47789aef879SDaniel Scally 	    container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
47889aef879SDaniel Scally 	int ret = 0;
47989aef879SDaniel Scally 
48089aef879SDaniel Scally 	/* If VBLANK is altered we need to update exposure to compensate */
48189aef879SDaniel Scally 	if (ctrl->id == V4L2_CID_VBLANK) {
48289aef879SDaniel Scally 		int exposure_max;
48389aef879SDaniel Scally 
48489aef879SDaniel Scally 		exposure_max = ov5693->mode.format.height + ctrl->val -
48589aef879SDaniel Scally 			       OV5693_INTEGRATION_TIME_MARGIN;
48689aef879SDaniel Scally 		__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
48789aef879SDaniel Scally 					 ov5693->ctrls.exposure->minimum,
48889aef879SDaniel Scally 					 exposure_max,
48989aef879SDaniel Scally 					 ov5693->ctrls.exposure->step,
49089aef879SDaniel Scally 					 min(ov5693->ctrls.exposure->val,
49189aef879SDaniel Scally 					     exposure_max));
49289aef879SDaniel Scally 	}
49389aef879SDaniel Scally 
49489aef879SDaniel Scally 	/* Only apply changes to the controls if the device is powered up */
49589aef879SDaniel Scally 	if (!pm_runtime_get_if_in_use(ov5693->dev))
49689aef879SDaniel Scally 		return 0;
49789aef879SDaniel Scally 
49889aef879SDaniel Scally 	switch (ctrl->id) {
49989aef879SDaniel Scally 	case V4L2_CID_EXPOSURE:
50089aef879SDaniel Scally 		ret = ov5693_exposure_configure(ov5693, ctrl->val);
50189aef879SDaniel Scally 		break;
50289aef879SDaniel Scally 	case V4L2_CID_ANALOGUE_GAIN:
50389aef879SDaniel Scally 		ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
50489aef879SDaniel Scally 		break;
50589aef879SDaniel Scally 	case V4L2_CID_DIGITAL_GAIN:
50689aef879SDaniel Scally 		ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
50789aef879SDaniel Scally 		break;
50889aef879SDaniel Scally 	case V4L2_CID_HFLIP:
50989aef879SDaniel Scally 		ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
51089aef879SDaniel Scally 		break;
51189aef879SDaniel Scally 	case V4L2_CID_VFLIP:
51289aef879SDaniel Scally 		ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
51389aef879SDaniel Scally 		break;
51489aef879SDaniel Scally 	case V4L2_CID_VBLANK:
51589aef879SDaniel Scally 		ret = ov5693_vts_configure(ov5693, ctrl->val);
51689aef879SDaniel Scally 		break;
51789aef879SDaniel Scally 	case V4L2_CID_TEST_PATTERN:
51889aef879SDaniel Scally 		ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
51989aef879SDaniel Scally 		break;
52089aef879SDaniel Scally 	default:
52189aef879SDaniel Scally 		ret = -EINVAL;
52289aef879SDaniel Scally 	}
52389aef879SDaniel Scally 
52489aef879SDaniel Scally 	pm_runtime_put(ov5693->dev);
52589aef879SDaniel Scally 
52689aef879SDaniel Scally 	return ret;
52789aef879SDaniel Scally }
52889aef879SDaniel Scally 
ov5693_g_volatile_ctrl(struct v4l2_ctrl * ctrl)52989aef879SDaniel Scally static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
53089aef879SDaniel Scally {
53189aef879SDaniel Scally 	struct ov5693_device *ov5693 = container_of(ctrl->handler,
53289aef879SDaniel Scally 						    struct ov5693_device,
53389aef879SDaniel Scally 						    ctrls.handler);
53489aef879SDaniel Scally 
53589aef879SDaniel Scally 	switch (ctrl->id) {
53689aef879SDaniel Scally 	case V4L2_CID_EXPOSURE_ABSOLUTE:
53789aef879SDaniel Scally 		return ov5693_get_exposure(ov5693, &ctrl->val);
53889aef879SDaniel Scally 	case V4L2_CID_AUTOGAIN:
53989aef879SDaniel Scally 		return ov5693_get_gain(ov5693, &ctrl->val);
54089aef879SDaniel Scally 	default:
54189aef879SDaniel Scally 		return -EINVAL;
54289aef879SDaniel Scally 	}
54389aef879SDaniel Scally }
54489aef879SDaniel Scally 
54589aef879SDaniel Scally static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
54689aef879SDaniel Scally 	.s_ctrl = ov5693_s_ctrl,
54789aef879SDaniel Scally 	.g_volatile_ctrl = ov5693_g_volatile_ctrl
54889aef879SDaniel Scally };
54989aef879SDaniel Scally 
55089aef879SDaniel Scally /* System Control Functions */
55189aef879SDaniel Scally 
ov5693_mode_configure(struct ov5693_device * ov5693)55289aef879SDaniel Scally static int ov5693_mode_configure(struct ov5693_device *ov5693)
55389aef879SDaniel Scally {
55489aef879SDaniel Scally 	const struct ov5693_mode *mode = &ov5693->mode;
55589aef879SDaniel Scally 	int ret = 0;
55689aef879SDaniel Scally 
55789aef879SDaniel Scally 	/* Crop Start X */
558f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_START_X_REG, mode->crop.left,
55989aef879SDaniel Scally 		  &ret);
56089aef879SDaniel Scally 
56189aef879SDaniel Scally 	/* Offset X */
562f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OFFSET_START_X_REG, 0, &ret);
56389aef879SDaniel Scally 
56489aef879SDaniel Scally 	/* Output Size X */
565f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
56689aef879SDaniel Scally 		  &ret);
56789aef879SDaniel Scally 
56889aef879SDaniel Scally 	/* Crop End X */
569f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_END_X_REG,
57089aef879SDaniel Scally 		  mode->crop.left + mode->crop.width, &ret);
57189aef879SDaniel Scally 
57289aef879SDaniel Scally 	/* Horizontal Total Size */
573f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
57489aef879SDaniel Scally 		  &ret);
57589aef879SDaniel Scally 
57689aef879SDaniel Scally 	/* Crop Start Y */
577f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_START_Y_REG, mode->crop.top,
57889aef879SDaniel Scally 		  &ret);
57989aef879SDaniel Scally 
58089aef879SDaniel Scally 	/* Offset Y */
581f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OFFSET_START_Y_REG, 0, &ret);
58289aef879SDaniel Scally 
58389aef879SDaniel Scally 	/* Output Size Y */
584f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
58589aef879SDaniel Scally 		  &ret);
58689aef879SDaniel Scally 
58789aef879SDaniel Scally 	/* Crop End Y */
588f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_END_Y_REG,
58989aef879SDaniel Scally 		  mode->crop.top + mode->crop.height, &ret);
59089aef879SDaniel Scally 
59189aef879SDaniel Scally 	/* Subsample X increase */
592f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SUB_INC_X_REG,
59389aef879SDaniel Scally 		  ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
59489aef879SDaniel Scally 	/* Subsample Y increase */
595f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SUB_INC_Y_REG,
59689aef879SDaniel Scally 		  ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
59789aef879SDaniel Scally 
59889aef879SDaniel Scally 	/* Binning */
599f3a5e2ccSHans de Goede 	cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG,
60089aef879SDaniel Scally 			OV5693_FORMAT1_VBIN_EN,
601f3a5e2ccSHans de Goede 			mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0, &ret);
60289aef879SDaniel Scally 
603f3a5e2ccSHans de Goede 	cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG,
60489aef879SDaniel Scally 			OV5693_FORMAT2_HBIN_EN,
605f3a5e2ccSHans de Goede 			mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0, &ret);
60689aef879SDaniel Scally 
60789aef879SDaniel Scally 	return ret;
60889aef879SDaniel Scally }
60989aef879SDaniel Scally 
ov5693_enable_streaming(struct ov5693_device * ov5693,bool enable)61089aef879SDaniel Scally static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
61189aef879SDaniel Scally {
61289aef879SDaniel Scally 	int ret = 0;
61389aef879SDaniel Scally 
614f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SW_STREAM_REG,
615f3a5e2ccSHans de Goede 		  enable ? OV5693_START_STREAMING : OV5693_STOP_STREAMING,
616f3a5e2ccSHans de Goede 		  &ret);
61789aef879SDaniel Scally 
61889aef879SDaniel Scally 	return ret;
61989aef879SDaniel Scally }
62089aef879SDaniel Scally 
ov5693_sw_reset(struct ov5693_device * ov5693)62189aef879SDaniel Scally static int ov5693_sw_reset(struct ov5693_device *ov5693)
62289aef879SDaniel Scally {
62389aef879SDaniel Scally 	int ret = 0;
62489aef879SDaniel Scally 
625f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);
62689aef879SDaniel Scally 
62789aef879SDaniel Scally 	return ret;
62889aef879SDaniel Scally }
62989aef879SDaniel Scally 
ov5693_sensor_init(struct ov5693_device * ov5693)63089aef879SDaniel Scally static int ov5693_sensor_init(struct ov5693_device *ov5693)
63189aef879SDaniel Scally {
63289aef879SDaniel Scally 	int ret;
63389aef879SDaniel Scally 
63489aef879SDaniel Scally 	ret = ov5693_sw_reset(ov5693);
63589aef879SDaniel Scally 	if (ret)
63689aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
63789aef879SDaniel Scally 				     "software reset error\n");
63889aef879SDaniel Scally 
639f3a5e2ccSHans de Goede 	ret = cci_multi_reg_write(ov5693->regmap, ov5693_global_regs,
640f3a5e2ccSHans de Goede 				  ARRAY_SIZE(ov5693_global_regs), NULL);
64189aef879SDaniel Scally 	if (ret)
64289aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
64389aef879SDaniel Scally 				     "global settings error\n");
64489aef879SDaniel Scally 
64589aef879SDaniel Scally 	ret = ov5693_mode_configure(ov5693);
64689aef879SDaniel Scally 	if (ret)
64789aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
64889aef879SDaniel Scally 				     "mode configure error\n");
64989aef879SDaniel Scally 
65089aef879SDaniel Scally 	ret = ov5693_enable_streaming(ov5693, false);
65189aef879SDaniel Scally 	if (ret)
65289aef879SDaniel Scally 		dev_err(ov5693->dev, "stop streaming error\n");
65389aef879SDaniel Scally 
65489aef879SDaniel Scally 	return ret;
65589aef879SDaniel Scally }
65689aef879SDaniel Scally 
ov5693_sensor_powerdown(struct ov5693_device * ov5693)65789aef879SDaniel Scally static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
65889aef879SDaniel Scally {
65989aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 1);
66089aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
66189aef879SDaniel Scally 
66289aef879SDaniel Scally 	regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);
66389aef879SDaniel Scally 
6648a47d09eSTommaso Merciai 	clk_disable_unprepare(ov5693->xvclk);
66589aef879SDaniel Scally }
66689aef879SDaniel Scally 
ov5693_sensor_powerup(struct ov5693_device * ov5693)66789aef879SDaniel Scally static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
66889aef879SDaniel Scally {
66989aef879SDaniel Scally 	int ret;
67089aef879SDaniel Scally 
67189aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 1);
67289aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
67389aef879SDaniel Scally 
6748a47d09eSTommaso Merciai 	ret = clk_prepare_enable(ov5693->xvclk);
67589aef879SDaniel Scally 	if (ret) {
67689aef879SDaniel Scally 		dev_err(ov5693->dev, "Failed to enable clk\n");
67789aef879SDaniel Scally 		goto fail_power;
67889aef879SDaniel Scally 	}
67989aef879SDaniel Scally 
68089aef879SDaniel Scally 	ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
68189aef879SDaniel Scally 	if (ret) {
68289aef879SDaniel Scally 		dev_err(ov5693->dev, "Failed to enable regulators\n");
68389aef879SDaniel Scally 		goto fail_power;
68489aef879SDaniel Scally 	}
68589aef879SDaniel Scally 
68689aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 0);
68789aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 0);
68889aef879SDaniel Scally 
68989aef879SDaniel Scally 	usleep_range(5000, 7500);
69089aef879SDaniel Scally 
69189aef879SDaniel Scally 	return 0;
69289aef879SDaniel Scally 
69389aef879SDaniel Scally fail_power:
69489aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
69589aef879SDaniel Scally 	return ret;
69689aef879SDaniel Scally }
69789aef879SDaniel Scally 
ov5693_sensor_suspend(struct device * dev)69889aef879SDaniel Scally static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
69989aef879SDaniel Scally {
70089aef879SDaniel Scally 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
70189aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
70289aef879SDaniel Scally 
70389aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
70489aef879SDaniel Scally 
70589aef879SDaniel Scally 	return 0;
70689aef879SDaniel Scally }
70789aef879SDaniel Scally 
ov5693_sensor_resume(struct device * dev)70889aef879SDaniel Scally static int __maybe_unused ov5693_sensor_resume(struct device *dev)
70989aef879SDaniel Scally {
71089aef879SDaniel Scally 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
71189aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
71289aef879SDaniel Scally 	int ret;
71389aef879SDaniel Scally 
71489aef879SDaniel Scally 	mutex_lock(&ov5693->lock);
71589aef879SDaniel Scally 
71689aef879SDaniel Scally 	ret = ov5693_sensor_powerup(ov5693);
71789aef879SDaniel Scally 	if (ret)
71889aef879SDaniel Scally 		goto out_unlock;
71989aef879SDaniel Scally 
72089aef879SDaniel Scally 	ret = ov5693_sensor_init(ov5693);
72189aef879SDaniel Scally 	if (ret) {
72289aef879SDaniel Scally 		dev_err(dev, "ov5693 sensor init failure\n");
72389aef879SDaniel Scally 		goto err_power;
72489aef879SDaniel Scally 	}
72589aef879SDaniel Scally 
72689aef879SDaniel Scally 	goto out_unlock;
72789aef879SDaniel Scally 
72889aef879SDaniel Scally err_power:
72989aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
73089aef879SDaniel Scally out_unlock:
73189aef879SDaniel Scally 	mutex_unlock(&ov5693->lock);
73289aef879SDaniel Scally 	return ret;
73389aef879SDaniel Scally }
73489aef879SDaniel Scally 
ov5693_detect(struct ov5693_device * ov5693)73589aef879SDaniel Scally static int ov5693_detect(struct ov5693_device *ov5693)
73689aef879SDaniel Scally {
73789aef879SDaniel Scally 	int ret;
738f3a5e2ccSHans de Goede 	u64 id;
73989aef879SDaniel Scally 
740f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_REG_CHIP_ID, &id, NULL);
74189aef879SDaniel Scally 	if (ret)
74289aef879SDaniel Scally 		return ret;
74389aef879SDaniel Scally 
74489aef879SDaniel Scally 	if (id != OV5693_CHIP_ID)
74589aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, -ENODEV,
746f3a5e2ccSHans de Goede 				     "sensor ID mismatch. Got 0x%04llx\n", id);
74789aef879SDaniel Scally 
74889aef879SDaniel Scally 	return 0;
74989aef879SDaniel Scally }
75089aef879SDaniel Scally 
75189aef879SDaniel Scally /* V4L2 Framework callbacks */
75289aef879SDaniel Scally 
__ov5693_calc_vts(u32 height)75389aef879SDaniel Scally static unsigned int __ov5693_calc_vts(u32 height)
75489aef879SDaniel Scally {
75589aef879SDaniel Scally 	/*
75689aef879SDaniel Scally 	 * We need to set a sensible default VTS for whatever format height we
75789aef879SDaniel Scally 	 * happen to be given from set_fmt(). This function just targets
75889aef879SDaniel Scally 	 * an even multiple of 30fps.
75989aef879SDaniel Scally 	 */
76089aef879SDaniel Scally 
76189aef879SDaniel Scally 	unsigned int tgt_fps;
76289aef879SDaniel Scally 
76389aef879SDaniel Scally 	tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);
76489aef879SDaniel Scally 
76589aef879SDaniel Scally 	return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
76689aef879SDaniel Scally }
76789aef879SDaniel Scally 
76889aef879SDaniel Scally static struct v4l2_mbus_framefmt *
__ov5693_get_pad_format(struct ov5693_device * ov5693,struct v4l2_subdev_state * state,unsigned int pad,enum v4l2_subdev_format_whence which)76989aef879SDaniel Scally __ov5693_get_pad_format(struct ov5693_device *ov5693,
77089aef879SDaniel Scally 			struct v4l2_subdev_state *state,
77189aef879SDaniel Scally 			unsigned int pad, enum v4l2_subdev_format_whence which)
77289aef879SDaniel Scally {
77389aef879SDaniel Scally 	switch (which) {
77489aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_TRY:
775bc0e8d91SSakari Ailus 		return v4l2_subdev_state_get_format(state, pad);
77689aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_ACTIVE:
77789aef879SDaniel Scally 		return &ov5693->mode.format;
77889aef879SDaniel Scally 	default:
77989aef879SDaniel Scally 		return NULL;
78089aef879SDaniel Scally 	}
78189aef879SDaniel Scally }
78289aef879SDaniel Scally 
78389aef879SDaniel Scally static struct v4l2_rect *
__ov5693_get_pad_crop(struct ov5693_device * ov5693,struct v4l2_subdev_state * state,unsigned int pad,enum v4l2_subdev_format_whence which)78489aef879SDaniel Scally __ov5693_get_pad_crop(struct ov5693_device *ov5693,
78589aef879SDaniel Scally 		      struct v4l2_subdev_state *state,
78689aef879SDaniel Scally 		      unsigned int pad, enum v4l2_subdev_format_whence which)
78789aef879SDaniel Scally {
78889aef879SDaniel Scally 	switch (which) {
78989aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_TRY:
790bc0e8d91SSakari Ailus 		return v4l2_subdev_state_get_crop(state, pad);
79189aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_ACTIVE:
79289aef879SDaniel Scally 		return &ov5693->mode.crop;
79389aef879SDaniel Scally 	}
79489aef879SDaniel Scally 
79589aef879SDaniel Scally 	return NULL;
79689aef879SDaniel Scally }
79789aef879SDaniel Scally 
ov5693_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)79889aef879SDaniel Scally static int ov5693_get_fmt(struct v4l2_subdev *sd,
79989aef879SDaniel Scally 			  struct v4l2_subdev_state *state,
80089aef879SDaniel Scally 			  struct v4l2_subdev_format *format)
80189aef879SDaniel Scally {
80289aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
80389aef879SDaniel Scally 
80489aef879SDaniel Scally 	format->format = ov5693->mode.format;
80589aef879SDaniel Scally 
80689aef879SDaniel Scally 	return 0;
80789aef879SDaniel Scally }
80889aef879SDaniel Scally 
ov5693_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)80989aef879SDaniel Scally static int ov5693_set_fmt(struct v4l2_subdev *sd,
81089aef879SDaniel Scally 			  struct v4l2_subdev_state *state,
81189aef879SDaniel Scally 			  struct v4l2_subdev_format *format)
81289aef879SDaniel Scally {
81389aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
81489aef879SDaniel Scally 	const struct v4l2_rect *crop;
81589aef879SDaniel Scally 	struct v4l2_mbus_framefmt *fmt;
81689aef879SDaniel Scally 	unsigned int hratio, vratio;
81789aef879SDaniel Scally 	unsigned int width, height;
81889aef879SDaniel Scally 	unsigned int hblank;
81989aef879SDaniel Scally 	int exposure_max;
82089aef879SDaniel Scally 
82189aef879SDaniel Scally 	crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
82289aef879SDaniel Scally 
82389aef879SDaniel Scally 	/*
82489aef879SDaniel Scally 	 * Align to two to simplify the binning calculations below, and clamp
82589aef879SDaniel Scally 	 * the requested format at the crop rectangle
82689aef879SDaniel Scally 	 */
82789aef879SDaniel Scally 	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
82889aef879SDaniel Scally 			OV5693_MIN_CROP_WIDTH, crop->width);
82989aef879SDaniel Scally 	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
83089aef879SDaniel Scally 			 OV5693_MIN_CROP_HEIGHT, crop->height);
83189aef879SDaniel Scally 
83289aef879SDaniel Scally 	/*
83389aef879SDaniel Scally 	 * We can only support setting either the dimensions of the crop rect
83489aef879SDaniel Scally 	 * or those dimensions binned (separately) by a factor of two.
83589aef879SDaniel Scally 	 */
83689aef879SDaniel Scally 	hratio = clamp_t(unsigned int,
83789aef879SDaniel Scally 			 DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
83889aef879SDaniel Scally 	vratio = clamp_t(unsigned int,
83989aef879SDaniel Scally 			 DIV_ROUND_CLOSEST(crop->height, height), 1, 2);
84089aef879SDaniel Scally 
84189aef879SDaniel Scally 	fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
84289aef879SDaniel Scally 				      format->which);
84389aef879SDaniel Scally 
84489aef879SDaniel Scally 	fmt->width = crop->width / hratio;
84589aef879SDaniel Scally 	fmt->height = crop->height / vratio;
84689aef879SDaniel Scally 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
84789aef879SDaniel Scally 
84889aef879SDaniel Scally 	format->format = *fmt;
84989aef879SDaniel Scally 
85089aef879SDaniel Scally 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
8511949c01eSkernel test robot 		return 0;
85289aef879SDaniel Scally 
85389aef879SDaniel Scally 	mutex_lock(&ov5693->lock);
85489aef879SDaniel Scally 
8552a7f8142Skernel test robot 	ov5693->mode.binning_x = hratio > 1;
85689aef879SDaniel Scally 	ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
8572a7f8142Skernel test robot 	ov5693->mode.binning_y = vratio > 1;
85889aef879SDaniel Scally 	ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;
85989aef879SDaniel Scally 
86089aef879SDaniel Scally 	ov5693->mode.vts = __ov5693_calc_vts(fmt->height);
86189aef879SDaniel Scally 
86289aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
86389aef879SDaniel Scally 				 OV5693_TIMING_MIN_VTS,
86489aef879SDaniel Scally 				 OV5693_TIMING_MAX_VTS - fmt->height,
86589aef879SDaniel Scally 				 1, ov5693->mode.vts - fmt->height);
86689aef879SDaniel Scally 	__v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
86789aef879SDaniel Scally 			   ov5693->mode.vts - fmt->height);
86889aef879SDaniel Scally 
86989aef879SDaniel Scally 	hblank = OV5693_FIXED_PPL - fmt->width;
87089aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
87189aef879SDaniel Scally 				 hblank);
87289aef879SDaniel Scally 
87389aef879SDaniel Scally 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
87489aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
87589aef879SDaniel Scally 				 ov5693->ctrls.exposure->minimum, exposure_max,
87689aef879SDaniel Scally 				 ov5693->ctrls.exposure->step,
87789aef879SDaniel Scally 				 min(ov5693->ctrls.exposure->val,
87889aef879SDaniel Scally 				     exposure_max));
87989aef879SDaniel Scally 
88089aef879SDaniel Scally 	mutex_unlock(&ov5693->lock);
8811949c01eSkernel test robot 	return 0;
88289aef879SDaniel Scally }
88389aef879SDaniel Scally 
ov5693_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)88489aef879SDaniel Scally static int ov5693_get_selection(struct v4l2_subdev *sd,
88589aef879SDaniel Scally 				struct v4l2_subdev_state *state,
88689aef879SDaniel Scally 				struct v4l2_subdev_selection *sel)
88789aef879SDaniel Scally {
88889aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
88989aef879SDaniel Scally 
89089aef879SDaniel Scally 	switch (sel->target) {
89189aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP:
89289aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
89389aef879SDaniel Scally 		sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
89489aef879SDaniel Scally 						sel->which);
89589aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
89689aef879SDaniel Scally 		break;
89789aef879SDaniel Scally 	case V4L2_SEL_TGT_NATIVE_SIZE:
89889aef879SDaniel Scally 		sel->r.top = 0;
89989aef879SDaniel Scally 		sel->r.left = 0;
90089aef879SDaniel Scally 		sel->r.width = OV5693_NATIVE_WIDTH;
90189aef879SDaniel Scally 		sel->r.height = OV5693_NATIVE_HEIGHT;
90289aef879SDaniel Scally 		break;
90389aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP_BOUNDS:
90489aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP_DEFAULT:
90589aef879SDaniel Scally 		sel->r.top = OV5693_ACTIVE_START_TOP;
90689aef879SDaniel Scally 		sel->r.left = OV5693_ACTIVE_START_LEFT;
90789aef879SDaniel Scally 		sel->r.width = OV5693_ACTIVE_WIDTH;
90889aef879SDaniel Scally 		sel->r.height = OV5693_ACTIVE_HEIGHT;
90989aef879SDaniel Scally 		break;
91089aef879SDaniel Scally 	default:
91189aef879SDaniel Scally 		return -EINVAL;
91289aef879SDaniel Scally 	}
91389aef879SDaniel Scally 
91489aef879SDaniel Scally 	return 0;
91589aef879SDaniel Scally }
91689aef879SDaniel Scally 
ov5693_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)91789aef879SDaniel Scally static int ov5693_set_selection(struct v4l2_subdev *sd,
91889aef879SDaniel Scally 				struct v4l2_subdev_state *state,
91989aef879SDaniel Scally 				struct v4l2_subdev_selection *sel)
92089aef879SDaniel Scally {
92189aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
92289aef879SDaniel Scally 	struct v4l2_mbus_framefmt *format;
92389aef879SDaniel Scally 	struct v4l2_rect *__crop;
92489aef879SDaniel Scally 	struct v4l2_rect rect;
92589aef879SDaniel Scally 
92689aef879SDaniel Scally 	if (sel->target != V4L2_SEL_TGT_CROP)
92789aef879SDaniel Scally 		return -EINVAL;
92889aef879SDaniel Scally 
92989aef879SDaniel Scally 	/*
93089aef879SDaniel Scally 	 * Clamp the boundaries of the crop rectangle to the size of the sensor
93189aef879SDaniel Scally 	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
93289aef879SDaniel Scally 	 * disrupted.
93389aef879SDaniel Scally 	 */
93489aef879SDaniel Scally 	rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
93589aef879SDaniel Scally 			  OV5693_NATIVE_WIDTH);
93689aef879SDaniel Scally 	rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
93789aef879SDaniel Scally 			 OV5693_NATIVE_HEIGHT);
93889aef879SDaniel Scally 	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
93989aef879SDaniel Scally 			     OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
94089aef879SDaniel Scally 	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
94189aef879SDaniel Scally 			      OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);
94289aef879SDaniel Scally 
94389aef879SDaniel Scally 	/* Make sure the crop rectangle isn't outside the bounds of the array */
94489aef879SDaniel Scally 	rect.width = min_t(unsigned int, rect.width,
94589aef879SDaniel Scally 			   OV5693_NATIVE_WIDTH - rect.left);
94689aef879SDaniel Scally 	rect.height = min_t(unsigned int, rect.height,
94789aef879SDaniel Scally 			    OV5693_NATIVE_HEIGHT - rect.top);
94889aef879SDaniel Scally 
94989aef879SDaniel Scally 	__crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);
95089aef879SDaniel Scally 
95189aef879SDaniel Scally 	if (rect.width != __crop->width || rect.height != __crop->height) {
95289aef879SDaniel Scally 		/*
95389aef879SDaniel Scally 		 * Reset the output image size if the crop rectangle size has
95489aef879SDaniel Scally 		 * been modified.
95589aef879SDaniel Scally 		 */
95689aef879SDaniel Scally 		format = __ov5693_get_pad_format(ov5693, state, sel->pad,
95789aef879SDaniel Scally 						 sel->which);
95889aef879SDaniel Scally 		format->width = rect.width;
95989aef879SDaniel Scally 		format->height = rect.height;
96089aef879SDaniel Scally 	}
96189aef879SDaniel Scally 
96289aef879SDaniel Scally 	*__crop = rect;
96389aef879SDaniel Scally 	sel->r = rect;
96489aef879SDaniel Scally 
96589aef879SDaniel Scally 	return 0;
96689aef879SDaniel Scally }
96789aef879SDaniel Scally 
ov5693_s_stream(struct v4l2_subdev * sd,int enable)96889aef879SDaniel Scally static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
96989aef879SDaniel Scally {
97089aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
97189aef879SDaniel Scally 	int ret;
97289aef879SDaniel Scally 
97389aef879SDaniel Scally 	if (enable) {
974b92f8f35SLaurent Pinchart 		ret = pm_runtime_resume_and_get(ov5693->dev);
975b92f8f35SLaurent Pinchart 		if (ret)
976b92f8f35SLaurent Pinchart 			return ret;
97789aef879SDaniel Scally 
97889aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
97989aef879SDaniel Scally 		ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
98089aef879SDaniel Scally 		if (ret) {
98189aef879SDaniel Scally 			mutex_unlock(&ov5693->lock);
98289aef879SDaniel Scally 			goto err_power_down;
98389aef879SDaniel Scally 		}
98489aef879SDaniel Scally 
98589aef879SDaniel Scally 		ret = ov5693_enable_streaming(ov5693, true);
98689aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
98789aef879SDaniel Scally 	} else {
98889aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
98989aef879SDaniel Scally 		ret = ov5693_enable_streaming(ov5693, false);
99089aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
99189aef879SDaniel Scally 	}
99289aef879SDaniel Scally 	if (ret)
99389aef879SDaniel Scally 		goto err_power_down;
99489aef879SDaniel Scally 
99589aef879SDaniel Scally 	if (!enable)
99689aef879SDaniel Scally 		pm_runtime_put(ov5693->dev);
99789aef879SDaniel Scally 
99889aef879SDaniel Scally 	return 0;
99989aef879SDaniel Scally err_power_down:
100089aef879SDaniel Scally 	pm_runtime_put_noidle(ov5693->dev);
100189aef879SDaniel Scally 	return ret;
100289aef879SDaniel Scally }
100389aef879SDaniel Scally 
ov5693_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval * interval)1004287fe160SLaurent Pinchart static int ov5693_get_frame_interval(struct v4l2_subdev *sd,
1005287fe160SLaurent Pinchart 				     struct v4l2_subdev_state *sd_state,
100689aef879SDaniel Scally 				     struct v4l2_subdev_frame_interval *interval)
100789aef879SDaniel Scally {
100889aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
100989aef879SDaniel Scally 	unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
101089aef879SDaniel Scally 				 ov5693->ctrls.vblank->val);
101189aef879SDaniel Scally 	unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);
101289aef879SDaniel Scally 
1013*805d4311SLaurent Pinchart 	/*
1014*805d4311SLaurent Pinchart 	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1015*805d4311SLaurent Pinchart 	 * subdev active state API.
1016*805d4311SLaurent Pinchart 	 */
1017*805d4311SLaurent Pinchart 	if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1018*805d4311SLaurent Pinchart 		return -EINVAL;
1019*805d4311SLaurent Pinchart 
102089aef879SDaniel Scally 	interval->interval.numerator = 1;
102189aef879SDaniel Scally 	interval->interval.denominator = fps;
102289aef879SDaniel Scally 
102389aef879SDaniel Scally 	return 0;
102489aef879SDaniel Scally }
102589aef879SDaniel Scally 
ov5693_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_mbus_code_enum * code)102689aef879SDaniel Scally static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
102789aef879SDaniel Scally 				 struct v4l2_subdev_state *state,
102889aef879SDaniel Scally 				 struct v4l2_subdev_mbus_code_enum *code)
102989aef879SDaniel Scally {
103089aef879SDaniel Scally 	/* Only a single mbus format is supported */
103189aef879SDaniel Scally 	if (code->index > 0)
103289aef879SDaniel Scally 		return -EINVAL;
103389aef879SDaniel Scally 
103489aef879SDaniel Scally 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
103589aef879SDaniel Scally 	return 0;
103689aef879SDaniel Scally }
103789aef879SDaniel Scally 
ov5693_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_size_enum * fse)103889aef879SDaniel Scally static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
103989aef879SDaniel Scally 				  struct v4l2_subdev_state *state,
104089aef879SDaniel Scally 				  struct v4l2_subdev_frame_size_enum *fse)
104189aef879SDaniel Scally {
104289aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
104389aef879SDaniel Scally 	struct v4l2_rect *__crop;
104489aef879SDaniel Scally 
104589aef879SDaniel Scally 	if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
104689aef879SDaniel Scally 		return -EINVAL;
104789aef879SDaniel Scally 
104889aef879SDaniel Scally 	__crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
104989aef879SDaniel Scally 	if (!__crop)
105089aef879SDaniel Scally 		return -EINVAL;
105189aef879SDaniel Scally 
105289aef879SDaniel Scally 	fse->min_width = __crop->width / (fse->index + 1);
105389aef879SDaniel Scally 	fse->min_height = __crop->height / (fse->index + 1);
105489aef879SDaniel Scally 	fse->max_width = fse->min_width;
105589aef879SDaniel Scally 	fse->max_height = fse->min_height;
105689aef879SDaniel Scally 
105789aef879SDaniel Scally 	return 0;
105889aef879SDaniel Scally }
105989aef879SDaniel Scally 
106089aef879SDaniel Scally static const struct v4l2_subdev_video_ops ov5693_video_ops = {
106189aef879SDaniel Scally 	.s_stream = ov5693_s_stream,
106289aef879SDaniel Scally };
106389aef879SDaniel Scally 
106489aef879SDaniel Scally static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
106589aef879SDaniel Scally 	.enum_mbus_code = ov5693_enum_mbus_code,
106689aef879SDaniel Scally 	.enum_frame_size = ov5693_enum_frame_size,
106789aef879SDaniel Scally 	.get_fmt = ov5693_get_fmt,
106889aef879SDaniel Scally 	.set_fmt = ov5693_set_fmt,
106989aef879SDaniel Scally 	.get_selection = ov5693_get_selection,
107089aef879SDaniel Scally 	.set_selection = ov5693_set_selection,
1071287fe160SLaurent Pinchart 	.get_frame_interval = ov5693_get_frame_interval,
107289aef879SDaniel Scally };
107389aef879SDaniel Scally 
107489aef879SDaniel Scally static const struct v4l2_subdev_ops ov5693_ops = {
107589aef879SDaniel Scally 	.video = &ov5693_video_ops,
107689aef879SDaniel Scally 	.pad = &ov5693_pad_ops,
107789aef879SDaniel Scally };
107889aef879SDaniel Scally 
107989aef879SDaniel Scally /* Sensor and Driver Configuration Functions */
108089aef879SDaniel Scally 
ov5693_init_controls(struct ov5693_device * ov5693)108189aef879SDaniel Scally static int ov5693_init_controls(struct ov5693_device *ov5693)
108289aef879SDaniel Scally {
108389aef879SDaniel Scally 	const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
108489aef879SDaniel Scally 	struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
108589aef879SDaniel Scally 	struct v4l2_fwnode_device_properties props;
108689aef879SDaniel Scally 	int vblank_max, vblank_def;
108789aef879SDaniel Scally 	int exposure_max;
108889aef879SDaniel Scally 	int hblank;
108989aef879SDaniel Scally 	int ret;
109089aef879SDaniel Scally 
109189aef879SDaniel Scally 	ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
109289aef879SDaniel Scally 	if (ret)
109389aef879SDaniel Scally 		return ret;
109489aef879SDaniel Scally 
109589aef879SDaniel Scally 	/* link freq */
109689aef879SDaniel Scally 	ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
109789aef879SDaniel Scally 						  NULL, V4L2_CID_LINK_FREQ,
109889aef879SDaniel Scally 						  0, 0, link_freq_menu_items);
109989aef879SDaniel Scally 	if (ctrls->link_freq)
110089aef879SDaniel Scally 		ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
110189aef879SDaniel Scally 
110289aef879SDaniel Scally 	/* pixel rate */
110389aef879SDaniel Scally 	ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
110489aef879SDaniel Scally 					      V4L2_CID_PIXEL_RATE, 0,
110589aef879SDaniel Scally 					      OV5693_PIXEL_RATE, 1,
110689aef879SDaniel Scally 					      OV5693_PIXEL_RATE);
110789aef879SDaniel Scally 
110889aef879SDaniel Scally 	/* Exposure */
110989aef879SDaniel Scally 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
111089aef879SDaniel Scally 	ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
111189aef879SDaniel Scally 					    V4L2_CID_EXPOSURE,
111289aef879SDaniel Scally 					    OV5693_EXPOSURE_MIN, exposure_max,
111389aef879SDaniel Scally 					    OV5693_EXPOSURE_STEP, exposure_max);
111489aef879SDaniel Scally 
111589aef879SDaniel Scally 	/* Gain */
111689aef879SDaniel Scally 	ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
111789aef879SDaniel Scally 						 ops, V4L2_CID_ANALOGUE_GAIN,
111889aef879SDaniel Scally 						 OV5693_GAIN_MIN,
111989aef879SDaniel Scally 						 OV5693_GAIN_MAX,
112089aef879SDaniel Scally 						 OV5693_GAIN_STEP,
112189aef879SDaniel Scally 						 OV5693_GAIN_DEF);
112289aef879SDaniel Scally 
112389aef879SDaniel Scally 	ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
112489aef879SDaniel Scally 						V4L2_CID_DIGITAL_GAIN,
112589aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_MIN,
112689aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_MAX,
112789aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_STEP,
112889aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_DEF);
112989aef879SDaniel Scally 
113089aef879SDaniel Scally 	/* Flip */
113189aef879SDaniel Scally 	ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
113289aef879SDaniel Scally 					 V4L2_CID_HFLIP, 0, 1, 1, 0);
113389aef879SDaniel Scally 
113489aef879SDaniel Scally 	ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
113589aef879SDaniel Scally 					 V4L2_CID_VFLIP, 0, 1, 1, 0);
113689aef879SDaniel Scally 
113789aef879SDaniel Scally 	hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
113889aef879SDaniel Scally 	ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
113989aef879SDaniel Scally 					  V4L2_CID_HBLANK, hblank,
114089aef879SDaniel Scally 					  hblank, 1, hblank);
114189aef879SDaniel Scally 
114289aef879SDaniel Scally 	if (ctrls->hblank)
114389aef879SDaniel Scally 		ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
114489aef879SDaniel Scally 
114589aef879SDaniel Scally 	vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
114689aef879SDaniel Scally 	vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
114789aef879SDaniel Scally 	ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
114889aef879SDaniel Scally 					  V4L2_CID_VBLANK,
114989aef879SDaniel Scally 					  OV5693_TIMING_MIN_VTS,
115089aef879SDaniel Scally 					  vblank_max, 1, vblank_def);
115189aef879SDaniel Scally 
115289aef879SDaniel Scally 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
115389aef879SDaniel Scally 					&ctrls->handler, ops,
115489aef879SDaniel Scally 					V4L2_CID_TEST_PATTERN,
115589aef879SDaniel Scally 					ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
115689aef879SDaniel Scally 					0, 0, ov5693_test_pattern_menu);
115789aef879SDaniel Scally 
115889aef879SDaniel Scally 	if (ctrls->handler.error) {
115989aef879SDaniel Scally 		dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
116089aef879SDaniel Scally 		ret = ctrls->handler.error;
116189aef879SDaniel Scally 		goto err_free_handler;
116289aef879SDaniel Scally 	}
116389aef879SDaniel Scally 
116489aef879SDaniel Scally 	/* set properties from fwnode (e.g. rotation, orientation) */
116589aef879SDaniel Scally 	ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
116689aef879SDaniel Scally 	if (ret)
116789aef879SDaniel Scally 		goto err_free_handler;
116889aef879SDaniel Scally 
116989aef879SDaniel Scally 	ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
117089aef879SDaniel Scally 					      &props);
117189aef879SDaniel Scally 	if (ret)
117289aef879SDaniel Scally 		goto err_free_handler;
117389aef879SDaniel Scally 
117489aef879SDaniel Scally 	/* Use same lock for controls as for everything else. */
117589aef879SDaniel Scally 	ctrls->handler.lock = &ov5693->lock;
117689aef879SDaniel Scally 	ov5693->sd.ctrl_handler = &ctrls->handler;
117789aef879SDaniel Scally 
117889aef879SDaniel Scally 	return 0;
117989aef879SDaniel Scally 
118089aef879SDaniel Scally err_free_handler:
118189aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ctrls->handler);
118289aef879SDaniel Scally 	return ret;
118389aef879SDaniel Scally }
118489aef879SDaniel Scally 
ov5693_configure_gpios(struct ov5693_device * ov5693)118589aef879SDaniel Scally static int ov5693_configure_gpios(struct ov5693_device *ov5693)
118689aef879SDaniel Scally {
118789aef879SDaniel Scally 	ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
118889aef879SDaniel Scally 						GPIOD_OUT_HIGH);
118989aef879SDaniel Scally 	if (IS_ERR(ov5693->reset)) {
119089aef879SDaniel Scally 		dev_err(ov5693->dev, "Error fetching reset GPIO\n");
119189aef879SDaniel Scally 		return PTR_ERR(ov5693->reset);
119289aef879SDaniel Scally 	}
119389aef879SDaniel Scally 
119489aef879SDaniel Scally 	ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
119589aef879SDaniel Scally 						    GPIOD_OUT_HIGH);
119689aef879SDaniel Scally 	if (IS_ERR(ov5693->powerdown)) {
119789aef879SDaniel Scally 		dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
119889aef879SDaniel Scally 		return PTR_ERR(ov5693->powerdown);
119989aef879SDaniel Scally 	}
120089aef879SDaniel Scally 
120189aef879SDaniel Scally 	return 0;
120289aef879SDaniel Scally }
120389aef879SDaniel Scally 
ov5693_get_regulators(struct ov5693_device * ov5693)120489aef879SDaniel Scally static int ov5693_get_regulators(struct ov5693_device *ov5693)
120589aef879SDaniel Scally {
120689aef879SDaniel Scally 	unsigned int i;
120789aef879SDaniel Scally 
120889aef879SDaniel Scally 	for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
120989aef879SDaniel Scally 		ov5693->supplies[i].supply = ov5693_supply_names[i];
121089aef879SDaniel Scally 
121189aef879SDaniel Scally 	return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
121289aef879SDaniel Scally 				       ov5693->supplies);
121389aef879SDaniel Scally }
121489aef879SDaniel Scally 
ov5693_check_hwcfg(struct ov5693_device * ov5693)121589aef879SDaniel Scally static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
121689aef879SDaniel Scally {
121789aef879SDaniel Scally 	struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
121889aef879SDaniel Scally 	struct v4l2_fwnode_endpoint bus_cfg = {
121989aef879SDaniel Scally 		.bus_type = V4L2_MBUS_CSI2_DPHY,
122089aef879SDaniel Scally 	};
122189aef879SDaniel Scally 	struct fwnode_handle *endpoint;
122289aef879SDaniel Scally 	unsigned int i;
122389aef879SDaniel Scally 	int ret;
122489aef879SDaniel Scally 
122589aef879SDaniel Scally 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
122689aef879SDaniel Scally 	if (!endpoint)
122789aef879SDaniel Scally 		return -EPROBE_DEFER; /* Could be provided by cio2-bridge */
122889aef879SDaniel Scally 
122989aef879SDaniel Scally 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
123089aef879SDaniel Scally 	fwnode_handle_put(endpoint);
123189aef879SDaniel Scally 	if (ret)
123289aef879SDaniel Scally 		return ret;
123389aef879SDaniel Scally 
123489aef879SDaniel Scally 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
123589aef879SDaniel Scally 		dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
123689aef879SDaniel Scally 		ret = -EINVAL;
123789aef879SDaniel Scally 		goto out_free_bus_cfg;
123889aef879SDaniel Scally 	}
123989aef879SDaniel Scally 
124089aef879SDaniel Scally 	if (!bus_cfg.nr_of_link_frequencies) {
124189aef879SDaniel Scally 		dev_err(ov5693->dev, "no link frequencies defined\n");
124289aef879SDaniel Scally 		ret = -EINVAL;
124389aef879SDaniel Scally 		goto out_free_bus_cfg;
124489aef879SDaniel Scally 	}
124589aef879SDaniel Scally 
124689aef879SDaniel Scally 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
124789aef879SDaniel Scally 		if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
124889aef879SDaniel Scally 			break;
124989aef879SDaniel Scally 
125089aef879SDaniel Scally 	if (i == bus_cfg.nr_of_link_frequencies) {
125189aef879SDaniel Scally 		dev_err(ov5693->dev, "supported link freq %ull not found\n",
125289aef879SDaniel Scally 			OV5693_LINK_FREQ_419_2MHZ);
125389aef879SDaniel Scally 		ret = -EINVAL;
125489aef879SDaniel Scally 		goto out_free_bus_cfg;
125589aef879SDaniel Scally 	}
125689aef879SDaniel Scally 
125789aef879SDaniel Scally out_free_bus_cfg:
125889aef879SDaniel Scally 	v4l2_fwnode_endpoint_free(&bus_cfg);
125989aef879SDaniel Scally 
126089aef879SDaniel Scally 	return ret;
126189aef879SDaniel Scally }
126289aef879SDaniel Scally 
ov5693_probe(struct i2c_client * client)126389aef879SDaniel Scally static int ov5693_probe(struct i2c_client *client)
126489aef879SDaniel Scally {
126589aef879SDaniel Scally 	struct ov5693_device *ov5693;
12668a47d09eSTommaso Merciai 	u32 xvclk_rate;
126789aef879SDaniel Scally 	int ret = 0;
126889aef879SDaniel Scally 
126989aef879SDaniel Scally 	ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
127089aef879SDaniel Scally 	if (!ov5693)
127189aef879SDaniel Scally 		return -ENOMEM;
127289aef879SDaniel Scally 
127389aef879SDaniel Scally 	ov5693->dev = &client->dev;
127489aef879SDaniel Scally 
1275f3a5e2ccSHans de Goede 	ov5693->regmap = devm_cci_regmap_init_i2c(client, 16);
1276f3a5e2ccSHans de Goede 	if (IS_ERR(ov5693->regmap))
1277f3a5e2ccSHans de Goede 		return PTR_ERR(ov5693->regmap);
1278f3a5e2ccSHans de Goede 
127989aef879SDaniel Scally 	ret = ov5693_check_hwcfg(ov5693);
128089aef879SDaniel Scally 	if (ret)
128189aef879SDaniel Scally 		return ret;
128289aef879SDaniel Scally 
128389aef879SDaniel Scally 	mutex_init(&ov5693->lock);
128489aef879SDaniel Scally 
128589aef879SDaniel Scally 	v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);
128689aef879SDaniel Scally 
128788b0c212STommaso Merciai 	ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
128888b0c212STommaso Merciai 	if (IS_ERR(ov5693->xvclk))
128988b0c212STommaso Merciai 		return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
129088b0c212STommaso Merciai 				     "failed to get xvclk: %ld\n",
129188b0c212STommaso Merciai 				     PTR_ERR(ov5693->xvclk));
129288b0c212STommaso Merciai 
129388b0c212STommaso Merciai 	if (ov5693->xvclk) {
129488b0c212STommaso Merciai 		xvclk_rate = clk_get_rate(ov5693->xvclk);
129588b0c212STommaso Merciai 	} else {
129688b0c212STommaso Merciai 		ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
129788b0c212STommaso Merciai 				     "clock-frequency",
129888b0c212STommaso Merciai 				     &xvclk_rate);
129988b0c212STommaso Merciai 
130088b0c212STommaso Merciai 		if (ret) {
130188b0c212STommaso Merciai 			dev_err(&client->dev, "can't get clock frequency");
130288b0c212STommaso Merciai 			return ret;
130388b0c212STommaso Merciai 		}
130489aef879SDaniel Scally 	}
130589aef879SDaniel Scally 
13068a47d09eSTommaso Merciai 	if (xvclk_rate != OV5693_XVCLK_FREQ)
130789aef879SDaniel Scally 		dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
13088a47d09eSTommaso Merciai 			 xvclk_rate, OV5693_XVCLK_FREQ);
130989aef879SDaniel Scally 
131089aef879SDaniel Scally 	ret = ov5693_configure_gpios(ov5693);
131189aef879SDaniel Scally 	if (ret)
131289aef879SDaniel Scally 		return ret;
131389aef879SDaniel Scally 
131489aef879SDaniel Scally 	ret = ov5693_get_regulators(ov5693);
131589aef879SDaniel Scally 	if (ret)
131689aef879SDaniel Scally 		return dev_err_probe(&client->dev, ret,
131789aef879SDaniel Scally 				     "Error fetching regulators\n");
131889aef879SDaniel Scally 
131989aef879SDaniel Scally 	ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
132089aef879SDaniel Scally 	ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
132189aef879SDaniel Scally 	ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
132289aef879SDaniel Scally 
132389aef879SDaniel Scally 	ov5693->mode.crop = ov5693_default_crop;
132489aef879SDaniel Scally 	ov5693->mode.format = ov5693_default_fmt;
132589aef879SDaniel Scally 	ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);
132689aef879SDaniel Scally 
132789aef879SDaniel Scally 	ret = ov5693_init_controls(ov5693);
132889aef879SDaniel Scally 	if (ret)
132989aef879SDaniel Scally 		return ret;
133089aef879SDaniel Scally 
133189aef879SDaniel Scally 	ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
133289aef879SDaniel Scally 	if (ret)
133389aef879SDaniel Scally 		goto err_ctrl_handler_free;
133489aef879SDaniel Scally 
133589aef879SDaniel Scally 	/*
133689aef879SDaniel Scally 	 * We need the driver to work in the event that pm runtime is disable in
133789aef879SDaniel Scally 	 * the kernel, so power up and verify the chip now. In the event that
133889aef879SDaniel Scally 	 * runtime pm is disabled this will leave the chip on, so that streaming
133989aef879SDaniel Scally 	 * will work.
134089aef879SDaniel Scally 	 */
134189aef879SDaniel Scally 
134289aef879SDaniel Scally 	ret = ov5693_sensor_powerup(ov5693);
134389aef879SDaniel Scally 	if (ret)
134489aef879SDaniel Scally 		goto err_media_entity_cleanup;
134589aef879SDaniel Scally 
134689aef879SDaniel Scally 	ret = ov5693_detect(ov5693);
134789aef879SDaniel Scally 	if (ret)
134889aef879SDaniel Scally 		goto err_powerdown;
134989aef879SDaniel Scally 
135089aef879SDaniel Scally 	pm_runtime_set_active(&client->dev);
135189aef879SDaniel Scally 	pm_runtime_get_noresume(&client->dev);
135289aef879SDaniel Scally 	pm_runtime_enable(&client->dev);
135389aef879SDaniel Scally 
135489aef879SDaniel Scally 	ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
135589aef879SDaniel Scally 	if (ret) {
135689aef879SDaniel Scally 		dev_err(&client->dev, "failed to register V4L2 subdev: %d",
135789aef879SDaniel Scally 			ret);
135889aef879SDaniel Scally 		goto err_pm_runtime;
135989aef879SDaniel Scally 	}
136089aef879SDaniel Scally 
136189aef879SDaniel Scally 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
136289aef879SDaniel Scally 	pm_runtime_use_autosuspend(&client->dev);
136389aef879SDaniel Scally 	pm_runtime_put_autosuspend(&client->dev);
136489aef879SDaniel Scally 
136589aef879SDaniel Scally 	return ret;
136689aef879SDaniel Scally 
136789aef879SDaniel Scally err_pm_runtime:
136889aef879SDaniel Scally 	pm_runtime_disable(&client->dev);
136989aef879SDaniel Scally 	pm_runtime_put_noidle(&client->dev);
137089aef879SDaniel Scally err_powerdown:
137189aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
137289aef879SDaniel Scally err_media_entity_cleanup:
137389aef879SDaniel Scally 	media_entity_cleanup(&ov5693->sd.entity);
137489aef879SDaniel Scally err_ctrl_handler_free:
137589aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
137689aef879SDaniel Scally 
137789aef879SDaniel Scally 	return ret;
137889aef879SDaniel Scally }
137989aef879SDaniel Scally 
ov5693_remove(struct i2c_client * client)1380ed5c2f5fSUwe Kleine-König static void ov5693_remove(struct i2c_client *client)
138189aef879SDaniel Scally {
138289aef879SDaniel Scally 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
138389aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
138489aef879SDaniel Scally 
138589aef879SDaniel Scally 	v4l2_async_unregister_subdev(sd);
138689aef879SDaniel Scally 	media_entity_cleanup(&ov5693->sd.entity);
138789aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
138889aef879SDaniel Scally 	mutex_destroy(&ov5693->lock);
138989aef879SDaniel Scally 
139089aef879SDaniel Scally 	/*
139189aef879SDaniel Scally 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
139289aef879SDaniel Scally 	 * make sure to turn power off manually.
139389aef879SDaniel Scally 	 */
139489aef879SDaniel Scally 	pm_runtime_disable(&client->dev);
139589aef879SDaniel Scally 	if (!pm_runtime_status_suspended(&client->dev))
139689aef879SDaniel Scally 		ov5693_sensor_powerdown(ov5693);
139789aef879SDaniel Scally 	pm_runtime_set_suspended(&client->dev);
139889aef879SDaniel Scally }
139989aef879SDaniel Scally 
140089aef879SDaniel Scally static const struct dev_pm_ops ov5693_pm_ops = {
140189aef879SDaniel Scally 	SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
140289aef879SDaniel Scally };
140389aef879SDaniel Scally 
140489aef879SDaniel Scally static const struct acpi_device_id ov5693_acpi_match[] = {
140589aef879SDaniel Scally 	{"INT33BE"},
140689aef879SDaniel Scally 	{},
140789aef879SDaniel Scally };
140889aef879SDaniel Scally MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
140989aef879SDaniel Scally 
1410109670dfSTommaso Merciai static const struct of_device_id ov5693_of_match[] = {
1411109670dfSTommaso Merciai 	{ .compatible = "ovti,ov5693", },
1412109670dfSTommaso Merciai 	{ /* sentinel */ },
1413109670dfSTommaso Merciai };
1414109670dfSTommaso Merciai MODULE_DEVICE_TABLE(of, ov5693_of_match);
1415109670dfSTommaso Merciai 
141689aef879SDaniel Scally static struct i2c_driver ov5693_driver = {
141789aef879SDaniel Scally 	.driver = {
141889aef879SDaniel Scally 		.name = "ov5693",
141989aef879SDaniel Scally 		.acpi_match_table = ov5693_acpi_match,
1420109670dfSTommaso Merciai 		.of_match_table = ov5693_of_match,
142189aef879SDaniel Scally 		.pm = &ov5693_pm_ops,
142289aef879SDaniel Scally 	},
1423aaeb31c0SUwe Kleine-König 	.probe = ov5693_probe,
142489aef879SDaniel Scally 	.remove = ov5693_remove,
142589aef879SDaniel Scally };
142689aef879SDaniel Scally module_i2c_driver(ov5693_driver);
142789aef879SDaniel Scally 
142889aef879SDaniel Scally MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
142989aef879SDaniel Scally MODULE_LICENSE("GPL");
1430