xref: /openbmc/linux/drivers/media/i2c/ov5693.c (revision f3a5e2cc)
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>
25*f3a5e2ccSHans de Goede 
26*f3a5e2ccSHans 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 */
32*f3a5e2ccSHans de Goede #define OV5693_SW_RESET_REG			CCI_REG8(0x0103)
33*f3a5e2ccSHans 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 
38*f3a5e2ccSHans 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 */
43*f3a5e2ccSHans 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 */
50*f3a5e2ccSHans 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 */
58*f3a5e2ccSHans de Goede #define OV5693_MWB_RED_GAIN_REG			CCI_REG16(0x3400)
59*f3a5e2ccSHans de Goede #define OV5693_MWB_GREEN_GAIN_REG		CCI_REG16(0x3402)
60*f3a5e2ccSHans 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 */
69*f3a5e2ccSHans de Goede #define OV5693_CROP_START_X_REG			CCI_REG16(0x3800)
70*f3a5e2ccSHans de Goede #define OV5693_CROP_START_Y_REG			CCI_REG16(0x3802)
71*f3a5e2ccSHans de Goede #define OV5693_CROP_END_X_REG			CCI_REG16(0x3804)
72*f3a5e2ccSHans de Goede #define OV5693_CROP_END_Y_REG			CCI_REG16(0x3806)
73*f3a5e2ccSHans de Goede #define OV5693_OUTPUT_SIZE_X_REG		CCI_REG16(0x3808)
74*f3a5e2ccSHans de Goede #define OV5693_OUTPUT_SIZE_Y_REG		CCI_REG16(0x380a)
7589aef879SDaniel Scally 
76*f3a5e2ccSHans de Goede #define OV5693_TIMING_HTS_REG			CCI_REG16(0x380c)
7789aef879SDaniel Scally #define OV5693_FIXED_PPL			2688U
78*f3a5e2ccSHans 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 
82*f3a5e2ccSHans de Goede #define OV5693_OFFSET_START_X_REG		CCI_REG16(0x3810)
83*f3a5e2ccSHans de Goede #define OV5693_OFFSET_START_Y_REG		CCI_REG16(0x3812)
8489aef879SDaniel Scally 
85*f3a5e2ccSHans de Goede #define OV5693_SUB_INC_X_REG			CCI_REG8(0x3814)
86*f3a5e2ccSHans de Goede #define OV5693_SUB_INC_Y_REG			CCI_REG8(0x3815)
8789aef879SDaniel Scally 
88*f3a5e2ccSHans 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)
92*f3a5e2ccSHans 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 
98*f3a5e2ccSHans 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 */
114*f3a5e2ccSHans 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;
137*f3a5e2ccSHans 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;
1442fa8d1d7SHans de Goede 	struct gpio_desc *privacy_led;
14589aef879SDaniel Scally 	struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
1468a47d09eSTommaso Merciai 	struct clk *xvclk;
14789aef879SDaniel Scally 
14889aef879SDaniel Scally 	struct ov5693_mode {
14989aef879SDaniel Scally 		struct v4l2_rect crop;
15089aef879SDaniel Scally 		struct v4l2_mbus_framefmt format;
15189aef879SDaniel Scally 		bool binning_x;
15289aef879SDaniel Scally 		bool binning_y;
15389aef879SDaniel Scally 		unsigned int inc_x_odd;
15489aef879SDaniel Scally 		unsigned int inc_y_odd;
15589aef879SDaniel Scally 		unsigned int vts;
15689aef879SDaniel Scally 	} mode;
15789aef879SDaniel Scally 	bool streaming;
15889aef879SDaniel Scally 
15989aef879SDaniel Scally 	struct v4l2_subdev sd;
16089aef879SDaniel Scally 	struct media_pad pad;
16189aef879SDaniel Scally 
16289aef879SDaniel Scally 	struct ov5693_v4l2_ctrls {
16389aef879SDaniel Scally 		struct v4l2_ctrl_handler handler;
16489aef879SDaniel Scally 		struct v4l2_ctrl *link_freq;
16589aef879SDaniel Scally 		struct v4l2_ctrl *pixel_rate;
16689aef879SDaniel Scally 		struct v4l2_ctrl *exposure;
16789aef879SDaniel Scally 		struct v4l2_ctrl *analogue_gain;
16889aef879SDaniel Scally 		struct v4l2_ctrl *digital_gain;
16989aef879SDaniel Scally 		struct v4l2_ctrl *hflip;
17089aef879SDaniel Scally 		struct v4l2_ctrl *vflip;
17189aef879SDaniel Scally 		struct v4l2_ctrl *hblank;
17289aef879SDaniel Scally 		struct v4l2_ctrl *vblank;
17389aef879SDaniel Scally 		struct v4l2_ctrl *test_pattern;
17489aef879SDaniel Scally 	} ctrls;
17589aef879SDaniel Scally };
17689aef879SDaniel Scally 
177*f3a5e2ccSHans de Goede static const struct cci_reg_sequence ov5693_global_regs[] = {
178*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3016), 0xf0},
179*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3017), 0xf0},
180*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3018), 0xf0},
181*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3022), 0x01},
182*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3028), 0x44},
183*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3098), 0x02},
184*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3099), 0x19},
185*f3a5e2ccSHans de Goede 	{CCI_REG8(0x309a), 0x02},
186*f3a5e2ccSHans de Goede 	{CCI_REG8(0x309b), 0x01},
187*f3a5e2ccSHans de Goede 	{CCI_REG8(0x309c), 0x00},
188*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30a0), 0xd2},
189*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30a2), 0x01},
190*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b2), 0x00},
191*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b3), 0x83},
192*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b4), 0x03},
193*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b5), 0x04},
194*f3a5e2ccSHans de Goede 	{CCI_REG8(0x30b6), 0x01},
195*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3080), 0x01},
196*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3104), 0x21},
197*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3106), 0x00},
198*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3406), 0x01},
199*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3503), 0x07},
200*f3a5e2ccSHans de Goede 	{CCI_REG8(0x350b), 0x40},
201*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3601), 0x0a},
202*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3602), 0x38},
203*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3612), 0x80},
204*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3620), 0x54},
205*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3621), 0xc7},
206*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3622), 0x0f},
207*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3625), 0x10},
208*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3630), 0x55},
209*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3631), 0xf4},
210*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3632), 0x00},
211*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3633), 0x34},
212*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3634), 0x02},
213*f3a5e2ccSHans de Goede 	{CCI_REG8(0x364d), 0x0d},
214*f3a5e2ccSHans de Goede 	{CCI_REG8(0x364f), 0xdd},
215*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3660), 0x04},
216*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3662), 0x10},
217*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3663), 0xf1},
218*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3665), 0x00},
219*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3666), 0x20},
220*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3667), 0x00},
221*f3a5e2ccSHans de Goede 	{CCI_REG8(0x366a), 0x80},
222*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3680), 0xe0},
223*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3681), 0x00},
224*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3700), 0x42},
225*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3701), 0x14},
226*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3702), 0xa0},
227*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3703), 0xd8},
228*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3704), 0x78},
229*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3705), 0x02},
230*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370a), 0x00},
231*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370b), 0x20},
232*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370c), 0x0c},
233*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370d), 0x11},
234*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370e), 0x00},
235*f3a5e2ccSHans de Goede 	{CCI_REG8(0x370f), 0x40},
236*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3710), 0x00},
237*f3a5e2ccSHans de Goede 	{CCI_REG8(0x371a), 0x1c},
238*f3a5e2ccSHans de Goede 	{CCI_REG8(0x371b), 0x05},
239*f3a5e2ccSHans de Goede 	{CCI_REG8(0x371c), 0x01},
240*f3a5e2ccSHans de Goede 	{CCI_REG8(0x371e), 0xa1},
241*f3a5e2ccSHans de Goede 	{CCI_REG8(0x371f), 0x0c},
242*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3721), 0x00},
243*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3724), 0x10},
244*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3726), 0x00},
245*f3a5e2ccSHans de Goede 	{CCI_REG8(0x372a), 0x01},
246*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3730), 0x10},
247*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3738), 0x22},
248*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3739), 0xe5},
249*f3a5e2ccSHans de Goede 	{CCI_REG8(0x373a), 0x50},
250*f3a5e2ccSHans de Goede 	{CCI_REG8(0x373b), 0x02},
251*f3a5e2ccSHans de Goede 	{CCI_REG8(0x373c), 0x41},
252*f3a5e2ccSHans de Goede 	{CCI_REG8(0x373f), 0x02},
253*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3740), 0x42},
254*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3741), 0x02},
255*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3742), 0x18},
256*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3743), 0x01},
257*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3744), 0x02},
258*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3747), 0x10},
259*f3a5e2ccSHans de Goede 	{CCI_REG8(0x374c), 0x04},
260*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3751), 0xf0},
261*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3752), 0x00},
262*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3753), 0x00},
263*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3754), 0xc0},
264*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3755), 0x00},
265*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3756), 0x1a},
266*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3758), 0x00},
267*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3759), 0x0f},
268*f3a5e2ccSHans de Goede 	{CCI_REG8(0x376b), 0x44},
269*f3a5e2ccSHans de Goede 	{CCI_REG8(0x375c), 0x04},
270*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3774), 0x10},
271*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3776), 0x00},
272*f3a5e2ccSHans de Goede 	{CCI_REG8(0x377f), 0x08},
273*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3780), 0x22},
274*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3781), 0x0c},
275*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3784), 0x2c},
276*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3785), 0x1e},
277*f3a5e2ccSHans de Goede 	{CCI_REG8(0x378f), 0xf5},
278*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3791), 0xb0},
279*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3795), 0x00},
280*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3796), 0x64},
281*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3797), 0x11},
282*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3798), 0x30},
283*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3799), 0x41},
284*f3a5e2ccSHans de Goede 	{CCI_REG8(0x379a), 0x07},
285*f3a5e2ccSHans de Goede 	{CCI_REG8(0x379b), 0xb0},
286*f3a5e2ccSHans de Goede 	{CCI_REG8(0x379c), 0x0c},
287*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3a04), 0x06},
288*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3a05), 0x14},
289*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3e07), 0x20},
290*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4000), 0x08},
291*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4001), 0x04},
292*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4004), 0x08},
293*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4006), 0x20},
294*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4008), 0x24},
295*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4009), 0x10},
296*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4058), 0x00},
297*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4101), 0xb2},
298*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4307), 0x31},
299*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4511), 0x05},
300*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4512), 0x01},
301*f3a5e2ccSHans de Goede 	{CCI_REG8(0x481f), 0x30},
302*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4826), 0x2c},
303*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d02), 0xfd},
304*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d03), 0xf5},
305*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d04), 0x0c},
306*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4d05), 0xcc},
307*f3a5e2ccSHans de Goede 	{CCI_REG8(0x4837), 0x0a},
308*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5003), 0x20},
309*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5013), 0x00},
310*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5842), 0x01},
311*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5843), 0x2b},
312*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5844), 0x01},
313*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5845), 0x92},
314*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5846), 0x01},
315*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5847), 0x8f},
316*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5848), 0x01},
317*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5849), 0x0c},
318*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5e10), 0x0c},
319*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3820), 0x00},
320*f3a5e2ccSHans de Goede 	{CCI_REG8(0x3821), 0x1e},
321*f3a5e2ccSHans de Goede 	{CCI_REG8(0x5041), 0x14}
32289aef879SDaniel Scally };
32389aef879SDaniel Scally 
32489aef879SDaniel Scally static const struct v4l2_rect ov5693_default_crop = {
32589aef879SDaniel Scally 	.left = OV5693_ACTIVE_START_LEFT,
32689aef879SDaniel Scally 	.top = OV5693_ACTIVE_START_TOP,
32789aef879SDaniel Scally 	.width = OV5693_ACTIVE_WIDTH,
32889aef879SDaniel Scally 	.height = OV5693_ACTIVE_HEIGHT,
32989aef879SDaniel Scally };
33089aef879SDaniel Scally 
33189aef879SDaniel Scally static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
33289aef879SDaniel Scally 	.width = OV5693_ACTIVE_WIDTH,
33389aef879SDaniel Scally 	.height = OV5693_ACTIVE_HEIGHT,
33489aef879SDaniel Scally 	.code = MEDIA_BUS_FMT_SBGGR10_1X10,
33589aef879SDaniel Scally };
33689aef879SDaniel Scally 
33789aef879SDaniel Scally static const s64 link_freq_menu_items[] = {
33889aef879SDaniel Scally 	OV5693_LINK_FREQ_419_2MHZ
33989aef879SDaniel Scally };
34089aef879SDaniel Scally 
34189aef879SDaniel Scally static const char * const ov5693_test_pattern_menu[] = {
34289aef879SDaniel Scally 	"Disabled",
34389aef879SDaniel Scally 	"Random Data",
34489aef879SDaniel Scally 	"Colour Bars",
34589aef879SDaniel Scally 	"Colour Bars with Rolling Bar"
34689aef879SDaniel Scally };
34789aef879SDaniel Scally 
34889aef879SDaniel Scally static const u8 ov5693_test_pattern_bits[] = {
34989aef879SDaniel Scally 	0,
35089aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
35189aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
35289aef879SDaniel Scally 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
35389aef879SDaniel Scally 	OV5693_TEST_PATTERN_ROLLING,
35489aef879SDaniel Scally };
35589aef879SDaniel Scally 
35689aef879SDaniel Scally /* V4L2 Controls Functions */
35789aef879SDaniel Scally 
ov5693_flip_vert_configure(struct ov5693_device * ov5693,bool enable)35889aef879SDaniel Scally static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
35989aef879SDaniel Scally 				      bool enable)
36089aef879SDaniel Scally {
36189aef879SDaniel Scally 	u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
36289aef879SDaniel Scally 		  OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
36389aef879SDaniel Scally 	int ret;
36489aef879SDaniel Scally 
365*f3a5e2ccSHans de Goede 	ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG, bits,
366*f3a5e2ccSHans de Goede 			      enable ? bits : 0, NULL);
36789aef879SDaniel Scally 	if (ret)
36889aef879SDaniel Scally 		return ret;
36989aef879SDaniel Scally 
37089aef879SDaniel Scally 	return 0;
37189aef879SDaniel Scally }
37289aef879SDaniel Scally 
ov5693_flip_horz_configure(struct ov5693_device * ov5693,bool enable)37389aef879SDaniel Scally static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
37489aef879SDaniel Scally 				      bool enable)
37589aef879SDaniel Scally {
37689aef879SDaniel Scally 	u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
37789aef879SDaniel Scally 		  OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
37889aef879SDaniel Scally 	int ret;
37989aef879SDaniel Scally 
380*f3a5e2ccSHans de Goede 	ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG, bits,
381*f3a5e2ccSHans de Goede 			      enable ? bits : 0, NULL);
38289aef879SDaniel Scally 	if (ret)
38389aef879SDaniel Scally 		return ret;
38489aef879SDaniel Scally 
38589aef879SDaniel Scally 	return 0;
38689aef879SDaniel Scally }
38789aef879SDaniel Scally 
ov5693_get_exposure(struct ov5693_device * ov5693,s32 * value)38889aef879SDaniel Scally static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
38989aef879SDaniel Scally {
390*f3a5e2ccSHans de Goede 	u64 exposure;
39189aef879SDaniel Scally 	int ret;
39289aef879SDaniel Scally 
393*f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, &exposure,
394*f3a5e2ccSHans de Goede 		       NULL);
39589aef879SDaniel Scally 	if (ret)
39689aef879SDaniel Scally 		return ret;
39789aef879SDaniel Scally 
39889aef879SDaniel Scally 	/* The lowest 4 bits are unsupported fractional bits */
39989aef879SDaniel Scally 	*value = exposure >> 4;
40089aef879SDaniel Scally 
40189aef879SDaniel Scally 	return 0;
40289aef879SDaniel Scally }
40389aef879SDaniel Scally 
ov5693_exposure_configure(struct ov5693_device * ov5693,u32 exposure)40489aef879SDaniel Scally static int ov5693_exposure_configure(struct ov5693_device *ov5693,
40589aef879SDaniel Scally 				     u32 exposure)
40689aef879SDaniel Scally {
40789aef879SDaniel Scally 	int ret = 0;
40889aef879SDaniel Scally 
40989aef879SDaniel Scally 	exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;
41089aef879SDaniel Scally 
411*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);
41289aef879SDaniel Scally 
41389aef879SDaniel Scally 	return ret;
41489aef879SDaniel Scally }
41589aef879SDaniel Scally 
ov5693_get_gain(struct ov5693_device * ov5693,u32 * gain)41689aef879SDaniel Scally static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
41789aef879SDaniel Scally {
418*f3a5e2ccSHans de Goede 	u64 value;
41989aef879SDaniel Scally 	int ret;
42089aef879SDaniel Scally 
421*f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_GAIN_CTRL_REG, &value, NULL);
42289aef879SDaniel Scally 	if (ret)
42389aef879SDaniel Scally 		return ret;
42489aef879SDaniel Scally 
42589aef879SDaniel Scally 	/* As with exposure, the lowest 4 bits are fractional bits. */
42689aef879SDaniel Scally 	*gain = value >> 4;
42789aef879SDaniel Scally 
42889aef879SDaniel Scally 	return ret;
42989aef879SDaniel Scally }
43089aef879SDaniel Scally 
ov5693_digital_gain_configure(struct ov5693_device * ov5693,u32 gain)43189aef879SDaniel Scally static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
43289aef879SDaniel Scally 					 u32 gain)
43389aef879SDaniel Scally {
43489aef879SDaniel Scally 	int ret = 0;
43589aef879SDaniel Scally 
43689aef879SDaniel Scally 	gain &= OV5693_MWB_GAIN_MASK;
43789aef879SDaniel Scally 
438*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_RED_GAIN_REG, gain, &ret);
439*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
440*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);
44189aef879SDaniel Scally 
44289aef879SDaniel Scally 	return ret;
44389aef879SDaniel Scally }
44489aef879SDaniel Scally 
ov5693_analog_gain_configure(struct ov5693_device * ov5693,u32 gain)44589aef879SDaniel Scally static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
44689aef879SDaniel Scally {
44789aef879SDaniel Scally 	int ret = 0;
44889aef879SDaniel Scally 
44989aef879SDaniel Scally 	gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;
45089aef879SDaniel Scally 
451*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_GAIN_CTRL_REG, gain, &ret);
45289aef879SDaniel Scally 
45389aef879SDaniel Scally 	return ret;
45489aef879SDaniel Scally }
45589aef879SDaniel Scally 
ov5693_vts_configure(struct ov5693_device * ov5693,u32 vblank)45689aef879SDaniel Scally static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
45789aef879SDaniel Scally {
45889aef879SDaniel Scally 	u16 vts = ov5693->mode.format.height + vblank;
45989aef879SDaniel Scally 	int ret = 0;
46089aef879SDaniel Scally 
461*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TIMING_VTS_REG, vts, &ret);
46289aef879SDaniel Scally 
46389aef879SDaniel Scally 	return ret;
46489aef879SDaniel Scally }
46589aef879SDaniel Scally 
ov5693_test_pattern_configure(struct ov5693_device * ov5693,u32 idx)46689aef879SDaniel Scally static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
46789aef879SDaniel Scally {
46889aef879SDaniel Scally 	int ret = 0;
46989aef879SDaniel Scally 
470*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TEST_PATTERN_REG,
47189aef879SDaniel Scally 		  ov5693_test_pattern_bits[idx], &ret);
47289aef879SDaniel Scally 
47389aef879SDaniel Scally 	return ret;
47489aef879SDaniel Scally }
47589aef879SDaniel Scally 
ov5693_s_ctrl(struct v4l2_ctrl * ctrl)47689aef879SDaniel Scally static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
47789aef879SDaniel Scally {
47889aef879SDaniel Scally 	struct ov5693_device *ov5693 =
47989aef879SDaniel Scally 	    container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
48089aef879SDaniel Scally 	int ret = 0;
48189aef879SDaniel Scally 
48289aef879SDaniel Scally 	/* If VBLANK is altered we need to update exposure to compensate */
48389aef879SDaniel Scally 	if (ctrl->id == V4L2_CID_VBLANK) {
48489aef879SDaniel Scally 		int exposure_max;
48589aef879SDaniel Scally 
48689aef879SDaniel Scally 		exposure_max = ov5693->mode.format.height + ctrl->val -
48789aef879SDaniel Scally 			       OV5693_INTEGRATION_TIME_MARGIN;
48889aef879SDaniel Scally 		__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
48989aef879SDaniel Scally 					 ov5693->ctrls.exposure->minimum,
49089aef879SDaniel Scally 					 exposure_max,
49189aef879SDaniel Scally 					 ov5693->ctrls.exposure->step,
49289aef879SDaniel Scally 					 min(ov5693->ctrls.exposure->val,
49389aef879SDaniel Scally 					     exposure_max));
49489aef879SDaniel Scally 	}
49589aef879SDaniel Scally 
49689aef879SDaniel Scally 	/* Only apply changes to the controls if the device is powered up */
49789aef879SDaniel Scally 	if (!pm_runtime_get_if_in_use(ov5693->dev))
49889aef879SDaniel Scally 		return 0;
49989aef879SDaniel Scally 
50089aef879SDaniel Scally 	switch (ctrl->id) {
50189aef879SDaniel Scally 	case V4L2_CID_EXPOSURE:
50289aef879SDaniel Scally 		ret = ov5693_exposure_configure(ov5693, ctrl->val);
50389aef879SDaniel Scally 		break;
50489aef879SDaniel Scally 	case V4L2_CID_ANALOGUE_GAIN:
50589aef879SDaniel Scally 		ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
50689aef879SDaniel Scally 		break;
50789aef879SDaniel Scally 	case V4L2_CID_DIGITAL_GAIN:
50889aef879SDaniel Scally 		ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
50989aef879SDaniel Scally 		break;
51089aef879SDaniel Scally 	case V4L2_CID_HFLIP:
51189aef879SDaniel Scally 		ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
51289aef879SDaniel Scally 		break;
51389aef879SDaniel Scally 	case V4L2_CID_VFLIP:
51489aef879SDaniel Scally 		ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
51589aef879SDaniel Scally 		break;
51689aef879SDaniel Scally 	case V4L2_CID_VBLANK:
51789aef879SDaniel Scally 		ret = ov5693_vts_configure(ov5693, ctrl->val);
51889aef879SDaniel Scally 		break;
51989aef879SDaniel Scally 	case V4L2_CID_TEST_PATTERN:
52089aef879SDaniel Scally 		ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
52189aef879SDaniel Scally 		break;
52289aef879SDaniel Scally 	default:
52389aef879SDaniel Scally 		ret = -EINVAL;
52489aef879SDaniel Scally 	}
52589aef879SDaniel Scally 
52689aef879SDaniel Scally 	pm_runtime_put(ov5693->dev);
52789aef879SDaniel Scally 
52889aef879SDaniel Scally 	return ret;
52989aef879SDaniel Scally }
53089aef879SDaniel Scally 
ov5693_g_volatile_ctrl(struct v4l2_ctrl * ctrl)53189aef879SDaniel Scally static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
53289aef879SDaniel Scally {
53389aef879SDaniel Scally 	struct ov5693_device *ov5693 = container_of(ctrl->handler,
53489aef879SDaniel Scally 						    struct ov5693_device,
53589aef879SDaniel Scally 						    ctrls.handler);
53689aef879SDaniel Scally 
53789aef879SDaniel Scally 	switch (ctrl->id) {
53889aef879SDaniel Scally 	case V4L2_CID_EXPOSURE_ABSOLUTE:
53989aef879SDaniel Scally 		return ov5693_get_exposure(ov5693, &ctrl->val);
54089aef879SDaniel Scally 	case V4L2_CID_AUTOGAIN:
54189aef879SDaniel Scally 		return ov5693_get_gain(ov5693, &ctrl->val);
54289aef879SDaniel Scally 	default:
54389aef879SDaniel Scally 		return -EINVAL;
54489aef879SDaniel Scally 	}
54589aef879SDaniel Scally }
54689aef879SDaniel Scally 
54789aef879SDaniel Scally static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
54889aef879SDaniel Scally 	.s_ctrl = ov5693_s_ctrl,
54989aef879SDaniel Scally 	.g_volatile_ctrl = ov5693_g_volatile_ctrl
55089aef879SDaniel Scally };
55189aef879SDaniel Scally 
55289aef879SDaniel Scally /* System Control Functions */
55389aef879SDaniel Scally 
ov5693_mode_configure(struct ov5693_device * ov5693)55489aef879SDaniel Scally static int ov5693_mode_configure(struct ov5693_device *ov5693)
55589aef879SDaniel Scally {
55689aef879SDaniel Scally 	const struct ov5693_mode *mode = &ov5693->mode;
55789aef879SDaniel Scally 	int ret = 0;
55889aef879SDaniel Scally 
55989aef879SDaniel Scally 	/* Crop Start X */
560*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_START_X_REG, mode->crop.left,
56189aef879SDaniel Scally 		  &ret);
56289aef879SDaniel Scally 
56389aef879SDaniel Scally 	/* Offset X */
564*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OFFSET_START_X_REG, 0, &ret);
56589aef879SDaniel Scally 
56689aef879SDaniel Scally 	/* Output Size X */
567*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
56889aef879SDaniel Scally 		  &ret);
56989aef879SDaniel Scally 
57089aef879SDaniel Scally 	/* Crop End X */
571*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_END_X_REG,
57289aef879SDaniel Scally 		  mode->crop.left + mode->crop.width, &ret);
57389aef879SDaniel Scally 
57489aef879SDaniel Scally 	/* Horizontal Total Size */
575*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
57689aef879SDaniel Scally 		  &ret);
57789aef879SDaniel Scally 
57889aef879SDaniel Scally 	/* Crop Start Y */
579*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_START_Y_REG, mode->crop.top,
58089aef879SDaniel Scally 		  &ret);
58189aef879SDaniel Scally 
58289aef879SDaniel Scally 	/* Offset Y */
583*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OFFSET_START_Y_REG, 0, &ret);
58489aef879SDaniel Scally 
58589aef879SDaniel Scally 	/* Output Size Y */
586*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
58789aef879SDaniel Scally 		  &ret);
58889aef879SDaniel Scally 
58989aef879SDaniel Scally 	/* Crop End Y */
590*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_CROP_END_Y_REG,
59189aef879SDaniel Scally 		  mode->crop.top + mode->crop.height, &ret);
59289aef879SDaniel Scally 
59389aef879SDaniel Scally 	/* Subsample X increase */
594*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SUB_INC_X_REG,
59589aef879SDaniel Scally 		  ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
59689aef879SDaniel Scally 	/* Subsample Y increase */
597*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SUB_INC_Y_REG,
59889aef879SDaniel Scally 		  ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
59989aef879SDaniel Scally 
60089aef879SDaniel Scally 	/* Binning */
601*f3a5e2ccSHans de Goede 	cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG,
60289aef879SDaniel Scally 			OV5693_FORMAT1_VBIN_EN,
603*f3a5e2ccSHans de Goede 			mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0, &ret);
60489aef879SDaniel Scally 
605*f3a5e2ccSHans de Goede 	cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG,
60689aef879SDaniel Scally 			OV5693_FORMAT2_HBIN_EN,
607*f3a5e2ccSHans de Goede 			mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0, &ret);
60889aef879SDaniel Scally 
60989aef879SDaniel Scally 	return ret;
61089aef879SDaniel Scally }
61189aef879SDaniel Scally 
ov5693_enable_streaming(struct ov5693_device * ov5693,bool enable)61289aef879SDaniel Scally static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
61389aef879SDaniel Scally {
61489aef879SDaniel Scally 	int ret = 0;
61589aef879SDaniel Scally 
616*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SW_STREAM_REG,
617*f3a5e2ccSHans de Goede 		  enable ? OV5693_START_STREAMING : OV5693_STOP_STREAMING,
618*f3a5e2ccSHans de Goede 		  &ret);
61989aef879SDaniel Scally 
62089aef879SDaniel Scally 	return ret;
62189aef879SDaniel Scally }
62289aef879SDaniel Scally 
ov5693_sw_reset(struct ov5693_device * ov5693)62389aef879SDaniel Scally static int ov5693_sw_reset(struct ov5693_device *ov5693)
62489aef879SDaniel Scally {
62589aef879SDaniel Scally 	int ret = 0;
62689aef879SDaniel Scally 
627*f3a5e2ccSHans de Goede 	cci_write(ov5693->regmap, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);
62889aef879SDaniel Scally 
62989aef879SDaniel Scally 	return ret;
63089aef879SDaniel Scally }
63189aef879SDaniel Scally 
ov5693_sensor_init(struct ov5693_device * ov5693)63289aef879SDaniel Scally static int ov5693_sensor_init(struct ov5693_device *ov5693)
63389aef879SDaniel Scally {
63489aef879SDaniel Scally 	int ret;
63589aef879SDaniel Scally 
63689aef879SDaniel Scally 	ret = ov5693_sw_reset(ov5693);
63789aef879SDaniel Scally 	if (ret)
63889aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
63989aef879SDaniel Scally 				     "software reset error\n");
64089aef879SDaniel Scally 
641*f3a5e2ccSHans de Goede 	ret = cci_multi_reg_write(ov5693->regmap, ov5693_global_regs,
642*f3a5e2ccSHans de Goede 				  ARRAY_SIZE(ov5693_global_regs), NULL);
64389aef879SDaniel Scally 	if (ret)
64489aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
64589aef879SDaniel Scally 				     "global settings error\n");
64689aef879SDaniel Scally 
64789aef879SDaniel Scally 	ret = ov5693_mode_configure(ov5693);
64889aef879SDaniel Scally 	if (ret)
64989aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, ret,
65089aef879SDaniel Scally 				     "mode configure error\n");
65189aef879SDaniel Scally 
65289aef879SDaniel Scally 	ret = ov5693_enable_streaming(ov5693, false);
65389aef879SDaniel Scally 	if (ret)
65489aef879SDaniel Scally 		dev_err(ov5693->dev, "stop streaming error\n");
65589aef879SDaniel Scally 
65689aef879SDaniel Scally 	return ret;
65789aef879SDaniel Scally }
65889aef879SDaniel Scally 
ov5693_sensor_powerdown(struct ov5693_device * ov5693)65989aef879SDaniel Scally static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
66089aef879SDaniel Scally {
6612fa8d1d7SHans de Goede 	gpiod_set_value_cansleep(ov5693->privacy_led, 0);
66289aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 1);
66389aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
66489aef879SDaniel Scally 
66589aef879SDaniel Scally 	regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);
66689aef879SDaniel Scally 
6678a47d09eSTommaso Merciai 	clk_disable_unprepare(ov5693->xvclk);
66889aef879SDaniel Scally }
66989aef879SDaniel Scally 
ov5693_sensor_powerup(struct ov5693_device * ov5693)67089aef879SDaniel Scally static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
67189aef879SDaniel Scally {
67289aef879SDaniel Scally 	int ret;
67389aef879SDaniel Scally 
67489aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 1);
67589aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
67689aef879SDaniel Scally 
6778a47d09eSTommaso Merciai 	ret = clk_prepare_enable(ov5693->xvclk);
67889aef879SDaniel Scally 	if (ret) {
67989aef879SDaniel Scally 		dev_err(ov5693->dev, "Failed to enable clk\n");
68089aef879SDaniel Scally 		goto fail_power;
68189aef879SDaniel Scally 	}
68289aef879SDaniel Scally 
68389aef879SDaniel Scally 	ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
68489aef879SDaniel Scally 	if (ret) {
68589aef879SDaniel Scally 		dev_err(ov5693->dev, "Failed to enable regulators\n");
68689aef879SDaniel Scally 		goto fail_power;
68789aef879SDaniel Scally 	}
68889aef879SDaniel Scally 
68989aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->powerdown, 0);
69089aef879SDaniel Scally 	gpiod_set_value_cansleep(ov5693->reset, 0);
6912fa8d1d7SHans de Goede 	gpiod_set_value_cansleep(ov5693->privacy_led, 1);
69289aef879SDaniel Scally 
69389aef879SDaniel Scally 	usleep_range(5000, 7500);
69489aef879SDaniel Scally 
69589aef879SDaniel Scally 	return 0;
69689aef879SDaniel Scally 
69789aef879SDaniel Scally fail_power:
69889aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
69989aef879SDaniel Scally 	return ret;
70089aef879SDaniel Scally }
70189aef879SDaniel Scally 
ov5693_sensor_suspend(struct device * dev)70289aef879SDaniel Scally static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
70389aef879SDaniel Scally {
70489aef879SDaniel Scally 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
70589aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
70689aef879SDaniel Scally 
70789aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
70889aef879SDaniel Scally 
70989aef879SDaniel Scally 	return 0;
71089aef879SDaniel Scally }
71189aef879SDaniel Scally 
ov5693_sensor_resume(struct device * dev)71289aef879SDaniel Scally static int __maybe_unused ov5693_sensor_resume(struct device *dev)
71389aef879SDaniel Scally {
71489aef879SDaniel Scally 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
71589aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
71689aef879SDaniel Scally 	int ret;
71789aef879SDaniel Scally 
71889aef879SDaniel Scally 	mutex_lock(&ov5693->lock);
71989aef879SDaniel Scally 
72089aef879SDaniel Scally 	ret = ov5693_sensor_powerup(ov5693);
72189aef879SDaniel Scally 	if (ret)
72289aef879SDaniel Scally 		goto out_unlock;
72389aef879SDaniel Scally 
72489aef879SDaniel Scally 	ret = ov5693_sensor_init(ov5693);
72589aef879SDaniel Scally 	if (ret) {
72689aef879SDaniel Scally 		dev_err(dev, "ov5693 sensor init failure\n");
72789aef879SDaniel Scally 		goto err_power;
72889aef879SDaniel Scally 	}
72989aef879SDaniel Scally 
73089aef879SDaniel Scally 	goto out_unlock;
73189aef879SDaniel Scally 
73289aef879SDaniel Scally err_power:
73389aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
73489aef879SDaniel Scally out_unlock:
73589aef879SDaniel Scally 	mutex_unlock(&ov5693->lock);
73689aef879SDaniel Scally 	return ret;
73789aef879SDaniel Scally }
73889aef879SDaniel Scally 
ov5693_detect(struct ov5693_device * ov5693)73989aef879SDaniel Scally static int ov5693_detect(struct ov5693_device *ov5693)
74089aef879SDaniel Scally {
74189aef879SDaniel Scally 	int ret;
742*f3a5e2ccSHans de Goede 	u64 id;
74389aef879SDaniel Scally 
744*f3a5e2ccSHans de Goede 	ret = cci_read(ov5693->regmap, OV5693_REG_CHIP_ID, &id, NULL);
74589aef879SDaniel Scally 	if (ret)
74689aef879SDaniel Scally 		return ret;
74789aef879SDaniel Scally 
74889aef879SDaniel Scally 	if (id != OV5693_CHIP_ID)
74989aef879SDaniel Scally 		return dev_err_probe(ov5693->dev, -ENODEV,
750*f3a5e2ccSHans de Goede 				     "sensor ID mismatch. Got 0x%04llx\n", id);
75189aef879SDaniel Scally 
75289aef879SDaniel Scally 	return 0;
75389aef879SDaniel Scally }
75489aef879SDaniel Scally 
75589aef879SDaniel Scally /* V4L2 Framework callbacks */
75689aef879SDaniel Scally 
__ov5693_calc_vts(u32 height)75789aef879SDaniel Scally static unsigned int __ov5693_calc_vts(u32 height)
75889aef879SDaniel Scally {
75989aef879SDaniel Scally 	/*
76089aef879SDaniel Scally 	 * We need to set a sensible default VTS for whatever format height we
76189aef879SDaniel Scally 	 * happen to be given from set_fmt(). This function just targets
76289aef879SDaniel Scally 	 * an even multiple of 30fps.
76389aef879SDaniel Scally 	 */
76489aef879SDaniel Scally 
76589aef879SDaniel Scally 	unsigned int tgt_fps;
76689aef879SDaniel Scally 
76789aef879SDaniel Scally 	tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);
76889aef879SDaniel Scally 
76989aef879SDaniel Scally 	return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
77089aef879SDaniel Scally }
77189aef879SDaniel Scally 
77289aef879SDaniel 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)77389aef879SDaniel Scally __ov5693_get_pad_format(struct ov5693_device *ov5693,
77489aef879SDaniel Scally 			struct v4l2_subdev_state *state,
77589aef879SDaniel Scally 			unsigned int pad, enum v4l2_subdev_format_whence which)
77689aef879SDaniel Scally {
77789aef879SDaniel Scally 	switch (which) {
77889aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_TRY:
77989aef879SDaniel Scally 		return v4l2_subdev_get_try_format(&ov5693->sd, state, pad);
78089aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_ACTIVE:
78189aef879SDaniel Scally 		return &ov5693->mode.format;
78289aef879SDaniel Scally 	default:
78389aef879SDaniel Scally 		return NULL;
78489aef879SDaniel Scally 	}
78589aef879SDaniel Scally }
78689aef879SDaniel Scally 
78789aef879SDaniel 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)78889aef879SDaniel Scally __ov5693_get_pad_crop(struct ov5693_device *ov5693,
78989aef879SDaniel Scally 		      struct v4l2_subdev_state *state,
79089aef879SDaniel Scally 		      unsigned int pad, enum v4l2_subdev_format_whence which)
79189aef879SDaniel Scally {
79289aef879SDaniel Scally 	switch (which) {
79389aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_TRY:
79489aef879SDaniel Scally 		return v4l2_subdev_get_try_crop(&ov5693->sd, state, pad);
79589aef879SDaniel Scally 	case V4L2_SUBDEV_FORMAT_ACTIVE:
79689aef879SDaniel Scally 		return &ov5693->mode.crop;
79789aef879SDaniel Scally 	}
79889aef879SDaniel Scally 
79989aef879SDaniel Scally 	return NULL;
80089aef879SDaniel Scally }
80189aef879SDaniel Scally 
ov5693_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)80289aef879SDaniel Scally static int ov5693_get_fmt(struct v4l2_subdev *sd,
80389aef879SDaniel Scally 			  struct v4l2_subdev_state *state,
80489aef879SDaniel Scally 			  struct v4l2_subdev_format *format)
80589aef879SDaniel Scally {
80689aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
80789aef879SDaniel Scally 
80889aef879SDaniel Scally 	format->format = ov5693->mode.format;
80989aef879SDaniel Scally 
81089aef879SDaniel Scally 	return 0;
81189aef879SDaniel Scally }
81289aef879SDaniel Scally 
ov5693_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)81389aef879SDaniel Scally static int ov5693_set_fmt(struct v4l2_subdev *sd,
81489aef879SDaniel Scally 			  struct v4l2_subdev_state *state,
81589aef879SDaniel Scally 			  struct v4l2_subdev_format *format)
81689aef879SDaniel Scally {
81789aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
81889aef879SDaniel Scally 	const struct v4l2_rect *crop;
81989aef879SDaniel Scally 	struct v4l2_mbus_framefmt *fmt;
82089aef879SDaniel Scally 	unsigned int hratio, vratio;
82189aef879SDaniel Scally 	unsigned int width, height;
82289aef879SDaniel Scally 	unsigned int hblank;
82389aef879SDaniel Scally 	int exposure_max;
82489aef879SDaniel Scally 
82589aef879SDaniel Scally 	crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
82689aef879SDaniel Scally 
82789aef879SDaniel Scally 	/*
82889aef879SDaniel Scally 	 * Align to two to simplify the binning calculations below, and clamp
82989aef879SDaniel Scally 	 * the requested format at the crop rectangle
83089aef879SDaniel Scally 	 */
83189aef879SDaniel Scally 	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
83289aef879SDaniel Scally 			OV5693_MIN_CROP_WIDTH, crop->width);
83389aef879SDaniel Scally 	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
83489aef879SDaniel Scally 			 OV5693_MIN_CROP_HEIGHT, crop->height);
83589aef879SDaniel Scally 
83689aef879SDaniel Scally 	/*
83789aef879SDaniel Scally 	 * We can only support setting either the dimensions of the crop rect
83889aef879SDaniel Scally 	 * or those dimensions binned (separately) by a factor of two.
83989aef879SDaniel Scally 	 */
84089aef879SDaniel Scally 	hratio = clamp_t(unsigned int,
84189aef879SDaniel Scally 			 DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
84289aef879SDaniel Scally 	vratio = clamp_t(unsigned int,
84389aef879SDaniel Scally 			 DIV_ROUND_CLOSEST(crop->height, height), 1, 2);
84489aef879SDaniel Scally 
84589aef879SDaniel Scally 	fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
84689aef879SDaniel Scally 				      format->which);
84789aef879SDaniel Scally 
84889aef879SDaniel Scally 	fmt->width = crop->width / hratio;
84989aef879SDaniel Scally 	fmt->height = crop->height / vratio;
85089aef879SDaniel Scally 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
85189aef879SDaniel Scally 
85289aef879SDaniel Scally 	format->format = *fmt;
85389aef879SDaniel Scally 
85489aef879SDaniel Scally 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
8551949c01eSkernel test robot 		return 0;
85689aef879SDaniel Scally 
85789aef879SDaniel Scally 	mutex_lock(&ov5693->lock);
85889aef879SDaniel Scally 
8592a7f8142Skernel test robot 	ov5693->mode.binning_x = hratio > 1;
86089aef879SDaniel Scally 	ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
8612a7f8142Skernel test robot 	ov5693->mode.binning_y = vratio > 1;
86289aef879SDaniel Scally 	ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;
86389aef879SDaniel Scally 
86489aef879SDaniel Scally 	ov5693->mode.vts = __ov5693_calc_vts(fmt->height);
86589aef879SDaniel Scally 
86689aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
86789aef879SDaniel Scally 				 OV5693_TIMING_MIN_VTS,
86889aef879SDaniel Scally 				 OV5693_TIMING_MAX_VTS - fmt->height,
86989aef879SDaniel Scally 				 1, ov5693->mode.vts - fmt->height);
87089aef879SDaniel Scally 	__v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
87189aef879SDaniel Scally 			   ov5693->mode.vts - fmt->height);
87289aef879SDaniel Scally 
87389aef879SDaniel Scally 	hblank = OV5693_FIXED_PPL - fmt->width;
87489aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
87589aef879SDaniel Scally 				 hblank);
87689aef879SDaniel Scally 
87789aef879SDaniel Scally 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
87889aef879SDaniel Scally 	__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
87989aef879SDaniel Scally 				 ov5693->ctrls.exposure->minimum, exposure_max,
88089aef879SDaniel Scally 				 ov5693->ctrls.exposure->step,
88189aef879SDaniel Scally 				 min(ov5693->ctrls.exposure->val,
88289aef879SDaniel Scally 				     exposure_max));
88389aef879SDaniel Scally 
88489aef879SDaniel Scally 	mutex_unlock(&ov5693->lock);
8851949c01eSkernel test robot 	return 0;
88689aef879SDaniel Scally }
88789aef879SDaniel Scally 
ov5693_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)88889aef879SDaniel Scally static int ov5693_get_selection(struct v4l2_subdev *sd,
88989aef879SDaniel Scally 				struct v4l2_subdev_state *state,
89089aef879SDaniel Scally 				struct v4l2_subdev_selection *sel)
89189aef879SDaniel Scally {
89289aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
89389aef879SDaniel Scally 
89489aef879SDaniel Scally 	switch (sel->target) {
89589aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP:
89689aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
89789aef879SDaniel Scally 		sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
89889aef879SDaniel Scally 						sel->which);
89989aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
90089aef879SDaniel Scally 		break;
90189aef879SDaniel Scally 	case V4L2_SEL_TGT_NATIVE_SIZE:
90289aef879SDaniel Scally 		sel->r.top = 0;
90389aef879SDaniel Scally 		sel->r.left = 0;
90489aef879SDaniel Scally 		sel->r.width = OV5693_NATIVE_WIDTH;
90589aef879SDaniel Scally 		sel->r.height = OV5693_NATIVE_HEIGHT;
90689aef879SDaniel Scally 		break;
90789aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP_BOUNDS:
90889aef879SDaniel Scally 	case V4L2_SEL_TGT_CROP_DEFAULT:
90989aef879SDaniel Scally 		sel->r.top = OV5693_ACTIVE_START_TOP;
91089aef879SDaniel Scally 		sel->r.left = OV5693_ACTIVE_START_LEFT;
91189aef879SDaniel Scally 		sel->r.width = OV5693_ACTIVE_WIDTH;
91289aef879SDaniel Scally 		sel->r.height = OV5693_ACTIVE_HEIGHT;
91389aef879SDaniel Scally 		break;
91489aef879SDaniel Scally 	default:
91589aef879SDaniel Scally 		return -EINVAL;
91689aef879SDaniel Scally 	}
91789aef879SDaniel Scally 
91889aef879SDaniel Scally 	return 0;
91989aef879SDaniel Scally }
92089aef879SDaniel Scally 
ov5693_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)92189aef879SDaniel Scally static int ov5693_set_selection(struct v4l2_subdev *sd,
92289aef879SDaniel Scally 				struct v4l2_subdev_state *state,
92389aef879SDaniel Scally 				struct v4l2_subdev_selection *sel)
92489aef879SDaniel Scally {
92589aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
92689aef879SDaniel Scally 	struct v4l2_mbus_framefmt *format;
92789aef879SDaniel Scally 	struct v4l2_rect *__crop;
92889aef879SDaniel Scally 	struct v4l2_rect rect;
92989aef879SDaniel Scally 
93089aef879SDaniel Scally 	if (sel->target != V4L2_SEL_TGT_CROP)
93189aef879SDaniel Scally 		return -EINVAL;
93289aef879SDaniel Scally 
93389aef879SDaniel Scally 	/*
93489aef879SDaniel Scally 	 * Clamp the boundaries of the crop rectangle to the size of the sensor
93589aef879SDaniel Scally 	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
93689aef879SDaniel Scally 	 * disrupted.
93789aef879SDaniel Scally 	 */
93889aef879SDaniel Scally 	rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
93989aef879SDaniel Scally 			  OV5693_NATIVE_WIDTH);
94089aef879SDaniel Scally 	rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
94189aef879SDaniel Scally 			 OV5693_NATIVE_HEIGHT);
94289aef879SDaniel Scally 	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
94389aef879SDaniel Scally 			     OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
94489aef879SDaniel Scally 	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
94589aef879SDaniel Scally 			      OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);
94689aef879SDaniel Scally 
94789aef879SDaniel Scally 	/* Make sure the crop rectangle isn't outside the bounds of the array */
94889aef879SDaniel Scally 	rect.width = min_t(unsigned int, rect.width,
94989aef879SDaniel Scally 			   OV5693_NATIVE_WIDTH - rect.left);
95089aef879SDaniel Scally 	rect.height = min_t(unsigned int, rect.height,
95189aef879SDaniel Scally 			    OV5693_NATIVE_HEIGHT - rect.top);
95289aef879SDaniel Scally 
95389aef879SDaniel Scally 	__crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);
95489aef879SDaniel Scally 
95589aef879SDaniel Scally 	if (rect.width != __crop->width || rect.height != __crop->height) {
95689aef879SDaniel Scally 		/*
95789aef879SDaniel Scally 		 * Reset the output image size if the crop rectangle size has
95889aef879SDaniel Scally 		 * been modified.
95989aef879SDaniel Scally 		 */
96089aef879SDaniel Scally 		format = __ov5693_get_pad_format(ov5693, state, sel->pad,
96189aef879SDaniel Scally 						 sel->which);
96289aef879SDaniel Scally 		format->width = rect.width;
96389aef879SDaniel Scally 		format->height = rect.height;
96489aef879SDaniel Scally 	}
96589aef879SDaniel Scally 
96689aef879SDaniel Scally 	*__crop = rect;
96789aef879SDaniel Scally 	sel->r = rect;
96889aef879SDaniel Scally 
96989aef879SDaniel Scally 	return 0;
97089aef879SDaniel Scally }
97189aef879SDaniel Scally 
ov5693_s_stream(struct v4l2_subdev * sd,int enable)97289aef879SDaniel Scally static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
97389aef879SDaniel Scally {
97489aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
97589aef879SDaniel Scally 	int ret;
97689aef879SDaniel Scally 
97789aef879SDaniel Scally 	if (enable) {
97889aef879SDaniel Scally 		ret = pm_runtime_get_sync(ov5693->dev);
97989aef879SDaniel Scally 		if (ret < 0)
98089aef879SDaniel Scally 			goto err_power_down;
98189aef879SDaniel Scally 
98289aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
98389aef879SDaniel Scally 		ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
98489aef879SDaniel Scally 		if (ret) {
98589aef879SDaniel Scally 			mutex_unlock(&ov5693->lock);
98689aef879SDaniel Scally 			goto err_power_down;
98789aef879SDaniel Scally 		}
98889aef879SDaniel Scally 
98989aef879SDaniel Scally 		ret = ov5693_enable_streaming(ov5693, true);
99089aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
99189aef879SDaniel Scally 	} else {
99289aef879SDaniel Scally 		mutex_lock(&ov5693->lock);
99389aef879SDaniel Scally 		ret = ov5693_enable_streaming(ov5693, false);
99489aef879SDaniel Scally 		mutex_unlock(&ov5693->lock);
99589aef879SDaniel Scally 	}
99689aef879SDaniel Scally 	if (ret)
99789aef879SDaniel Scally 		goto err_power_down;
99889aef879SDaniel Scally 
99989aef879SDaniel Scally 	ov5693->streaming = !!enable;
100089aef879SDaniel Scally 
100189aef879SDaniel Scally 	if (!enable)
100289aef879SDaniel Scally 		pm_runtime_put(ov5693->dev);
100389aef879SDaniel Scally 
100489aef879SDaniel Scally 	return 0;
100589aef879SDaniel Scally err_power_down:
100689aef879SDaniel Scally 	pm_runtime_put_noidle(ov5693->dev);
100789aef879SDaniel Scally 	return ret;
100889aef879SDaniel Scally }
100989aef879SDaniel Scally 
ov5693_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)101089aef879SDaniel Scally static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
101189aef879SDaniel Scally 				   struct v4l2_subdev_frame_interval *interval)
101289aef879SDaniel Scally {
101389aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
101489aef879SDaniel Scally 	unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
101589aef879SDaniel Scally 				 ov5693->ctrls.vblank->val);
101689aef879SDaniel Scally 	unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);
101789aef879SDaniel Scally 
101889aef879SDaniel Scally 	interval->interval.numerator = 1;
101989aef879SDaniel Scally 	interval->interval.denominator = fps;
102089aef879SDaniel Scally 
102189aef879SDaniel Scally 	return 0;
102289aef879SDaniel Scally }
102389aef879SDaniel Scally 
ov5693_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_mbus_code_enum * code)102489aef879SDaniel Scally static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
102589aef879SDaniel Scally 				 struct v4l2_subdev_state *state,
102689aef879SDaniel Scally 				 struct v4l2_subdev_mbus_code_enum *code)
102789aef879SDaniel Scally {
102889aef879SDaniel Scally 	/* Only a single mbus format is supported */
102989aef879SDaniel Scally 	if (code->index > 0)
103089aef879SDaniel Scally 		return -EINVAL;
103189aef879SDaniel Scally 
103289aef879SDaniel Scally 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
103389aef879SDaniel Scally 	return 0;
103489aef879SDaniel Scally }
103589aef879SDaniel Scally 
ov5693_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_frame_size_enum * fse)103689aef879SDaniel Scally static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
103789aef879SDaniel Scally 				  struct v4l2_subdev_state *state,
103889aef879SDaniel Scally 				  struct v4l2_subdev_frame_size_enum *fse)
103989aef879SDaniel Scally {
104089aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
104189aef879SDaniel Scally 	struct v4l2_rect *__crop;
104289aef879SDaniel Scally 
104389aef879SDaniel Scally 	if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
104489aef879SDaniel Scally 		return -EINVAL;
104589aef879SDaniel Scally 
104689aef879SDaniel Scally 	__crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
104789aef879SDaniel Scally 	if (!__crop)
104889aef879SDaniel Scally 		return -EINVAL;
104989aef879SDaniel Scally 
105089aef879SDaniel Scally 	fse->min_width = __crop->width / (fse->index + 1);
105189aef879SDaniel Scally 	fse->min_height = __crop->height / (fse->index + 1);
105289aef879SDaniel Scally 	fse->max_width = fse->min_width;
105389aef879SDaniel Scally 	fse->max_height = fse->min_height;
105489aef879SDaniel Scally 
105589aef879SDaniel Scally 	return 0;
105689aef879SDaniel Scally }
105789aef879SDaniel Scally 
105889aef879SDaniel Scally static const struct v4l2_subdev_video_ops ov5693_video_ops = {
105989aef879SDaniel Scally 	.s_stream = ov5693_s_stream,
106089aef879SDaniel Scally 	.g_frame_interval = ov5693_g_frame_interval,
106189aef879SDaniel Scally };
106289aef879SDaniel Scally 
106389aef879SDaniel Scally static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
106489aef879SDaniel Scally 	.enum_mbus_code = ov5693_enum_mbus_code,
106589aef879SDaniel Scally 	.enum_frame_size = ov5693_enum_frame_size,
106689aef879SDaniel Scally 	.get_fmt = ov5693_get_fmt,
106789aef879SDaniel Scally 	.set_fmt = ov5693_set_fmt,
106889aef879SDaniel Scally 	.get_selection = ov5693_get_selection,
106989aef879SDaniel Scally 	.set_selection = ov5693_set_selection,
107089aef879SDaniel Scally };
107189aef879SDaniel Scally 
107289aef879SDaniel Scally static const struct v4l2_subdev_ops ov5693_ops = {
107389aef879SDaniel Scally 	.video = &ov5693_video_ops,
107489aef879SDaniel Scally 	.pad = &ov5693_pad_ops,
107589aef879SDaniel Scally };
107689aef879SDaniel Scally 
107789aef879SDaniel Scally /* Sensor and Driver Configuration Functions */
107889aef879SDaniel Scally 
ov5693_init_controls(struct ov5693_device * ov5693)107989aef879SDaniel Scally static int ov5693_init_controls(struct ov5693_device *ov5693)
108089aef879SDaniel Scally {
108189aef879SDaniel Scally 	const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
108289aef879SDaniel Scally 	struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
108389aef879SDaniel Scally 	struct v4l2_fwnode_device_properties props;
108489aef879SDaniel Scally 	int vblank_max, vblank_def;
108589aef879SDaniel Scally 	int exposure_max;
108689aef879SDaniel Scally 	int hblank;
108789aef879SDaniel Scally 	int ret;
108889aef879SDaniel Scally 
108989aef879SDaniel Scally 	ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
109089aef879SDaniel Scally 	if (ret)
109189aef879SDaniel Scally 		return ret;
109289aef879SDaniel Scally 
109389aef879SDaniel Scally 	/* link freq */
109489aef879SDaniel Scally 	ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
109589aef879SDaniel Scally 						  NULL, V4L2_CID_LINK_FREQ,
109689aef879SDaniel Scally 						  0, 0, link_freq_menu_items);
109789aef879SDaniel Scally 	if (ctrls->link_freq)
109889aef879SDaniel Scally 		ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
109989aef879SDaniel Scally 
110089aef879SDaniel Scally 	/* pixel rate */
110189aef879SDaniel Scally 	ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
110289aef879SDaniel Scally 					      V4L2_CID_PIXEL_RATE, 0,
110389aef879SDaniel Scally 					      OV5693_PIXEL_RATE, 1,
110489aef879SDaniel Scally 					      OV5693_PIXEL_RATE);
110589aef879SDaniel Scally 
110689aef879SDaniel Scally 	/* Exposure */
110789aef879SDaniel Scally 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
110889aef879SDaniel Scally 	ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
110989aef879SDaniel Scally 					    V4L2_CID_EXPOSURE,
111089aef879SDaniel Scally 					    OV5693_EXPOSURE_MIN, exposure_max,
111189aef879SDaniel Scally 					    OV5693_EXPOSURE_STEP, exposure_max);
111289aef879SDaniel Scally 
111389aef879SDaniel Scally 	/* Gain */
111489aef879SDaniel Scally 	ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
111589aef879SDaniel Scally 						 ops, V4L2_CID_ANALOGUE_GAIN,
111689aef879SDaniel Scally 						 OV5693_GAIN_MIN,
111789aef879SDaniel Scally 						 OV5693_GAIN_MAX,
111889aef879SDaniel Scally 						 OV5693_GAIN_STEP,
111989aef879SDaniel Scally 						 OV5693_GAIN_DEF);
112089aef879SDaniel Scally 
112189aef879SDaniel Scally 	ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
112289aef879SDaniel Scally 						V4L2_CID_DIGITAL_GAIN,
112389aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_MIN,
112489aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_MAX,
112589aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_STEP,
112689aef879SDaniel Scally 						OV5693_DIGITAL_GAIN_DEF);
112789aef879SDaniel Scally 
112889aef879SDaniel Scally 	/* Flip */
112989aef879SDaniel Scally 	ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
113089aef879SDaniel Scally 					 V4L2_CID_HFLIP, 0, 1, 1, 0);
113189aef879SDaniel Scally 
113289aef879SDaniel Scally 	ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
113389aef879SDaniel Scally 					 V4L2_CID_VFLIP, 0, 1, 1, 0);
113489aef879SDaniel Scally 
113589aef879SDaniel Scally 	hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
113689aef879SDaniel Scally 	ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
113789aef879SDaniel Scally 					  V4L2_CID_HBLANK, hblank,
113889aef879SDaniel Scally 					  hblank, 1, hblank);
113989aef879SDaniel Scally 
114089aef879SDaniel Scally 	if (ctrls->hblank)
114189aef879SDaniel Scally 		ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
114289aef879SDaniel Scally 
114389aef879SDaniel Scally 	vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
114489aef879SDaniel Scally 	vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
114589aef879SDaniel Scally 	ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
114689aef879SDaniel Scally 					  V4L2_CID_VBLANK,
114789aef879SDaniel Scally 					  OV5693_TIMING_MIN_VTS,
114889aef879SDaniel Scally 					  vblank_max, 1, vblank_def);
114989aef879SDaniel Scally 
115089aef879SDaniel Scally 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
115189aef879SDaniel Scally 					&ctrls->handler, ops,
115289aef879SDaniel Scally 					V4L2_CID_TEST_PATTERN,
115389aef879SDaniel Scally 					ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
115489aef879SDaniel Scally 					0, 0, ov5693_test_pattern_menu);
115589aef879SDaniel Scally 
115689aef879SDaniel Scally 	if (ctrls->handler.error) {
115789aef879SDaniel Scally 		dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
115889aef879SDaniel Scally 		ret = ctrls->handler.error;
115989aef879SDaniel Scally 		goto err_free_handler;
116089aef879SDaniel Scally 	}
116189aef879SDaniel Scally 
116289aef879SDaniel Scally 	/* set properties from fwnode (e.g. rotation, orientation) */
116389aef879SDaniel Scally 	ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
116489aef879SDaniel Scally 	if (ret)
116589aef879SDaniel Scally 		goto err_free_handler;
116689aef879SDaniel Scally 
116789aef879SDaniel Scally 	ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
116889aef879SDaniel Scally 					      &props);
116989aef879SDaniel Scally 	if (ret)
117089aef879SDaniel Scally 		goto err_free_handler;
117189aef879SDaniel Scally 
117289aef879SDaniel Scally 	/* Use same lock for controls as for everything else. */
117389aef879SDaniel Scally 	ctrls->handler.lock = &ov5693->lock;
117489aef879SDaniel Scally 	ov5693->sd.ctrl_handler = &ctrls->handler;
117589aef879SDaniel Scally 
117689aef879SDaniel Scally 	return 0;
117789aef879SDaniel Scally 
117889aef879SDaniel Scally err_free_handler:
117989aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ctrls->handler);
118089aef879SDaniel Scally 	return ret;
118189aef879SDaniel Scally }
118289aef879SDaniel Scally 
ov5693_configure_gpios(struct ov5693_device * ov5693)118389aef879SDaniel Scally static int ov5693_configure_gpios(struct ov5693_device *ov5693)
118489aef879SDaniel Scally {
118589aef879SDaniel Scally 	ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
118689aef879SDaniel Scally 						GPIOD_OUT_HIGH);
118789aef879SDaniel Scally 	if (IS_ERR(ov5693->reset)) {
118889aef879SDaniel Scally 		dev_err(ov5693->dev, "Error fetching reset GPIO\n");
118989aef879SDaniel Scally 		return PTR_ERR(ov5693->reset);
119089aef879SDaniel Scally 	}
119189aef879SDaniel Scally 
119289aef879SDaniel Scally 	ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
119389aef879SDaniel Scally 						    GPIOD_OUT_HIGH);
119489aef879SDaniel Scally 	if (IS_ERR(ov5693->powerdown)) {
119589aef879SDaniel Scally 		dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
119689aef879SDaniel Scally 		return PTR_ERR(ov5693->powerdown);
119789aef879SDaniel Scally 	}
119889aef879SDaniel Scally 
11992fa8d1d7SHans de Goede 	ov5693->privacy_led = devm_gpiod_get_optional(ov5693->dev, "privacy-led",
12002fa8d1d7SHans de Goede 						      GPIOD_OUT_LOW);
12012fa8d1d7SHans de Goede 	if (IS_ERR(ov5693->privacy_led)) {
12022fa8d1d7SHans de Goede 		dev_err(ov5693->dev, "Error fetching privacy-led GPIO\n");
12032fa8d1d7SHans de Goede 		return PTR_ERR(ov5693->privacy_led);
12042fa8d1d7SHans de Goede 	}
12052fa8d1d7SHans de Goede 
120689aef879SDaniel Scally 	return 0;
120789aef879SDaniel Scally }
120889aef879SDaniel Scally 
ov5693_get_regulators(struct ov5693_device * ov5693)120989aef879SDaniel Scally static int ov5693_get_regulators(struct ov5693_device *ov5693)
121089aef879SDaniel Scally {
121189aef879SDaniel Scally 	unsigned int i;
121289aef879SDaniel Scally 
121389aef879SDaniel Scally 	for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
121489aef879SDaniel Scally 		ov5693->supplies[i].supply = ov5693_supply_names[i];
121589aef879SDaniel Scally 
121689aef879SDaniel Scally 	return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
121789aef879SDaniel Scally 				       ov5693->supplies);
121889aef879SDaniel Scally }
121989aef879SDaniel Scally 
ov5693_check_hwcfg(struct ov5693_device * ov5693)122089aef879SDaniel Scally static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
122189aef879SDaniel Scally {
122289aef879SDaniel Scally 	struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
122389aef879SDaniel Scally 	struct v4l2_fwnode_endpoint bus_cfg = {
122489aef879SDaniel Scally 		.bus_type = V4L2_MBUS_CSI2_DPHY,
122589aef879SDaniel Scally 	};
122689aef879SDaniel Scally 	struct fwnode_handle *endpoint;
122789aef879SDaniel Scally 	unsigned int i;
122889aef879SDaniel Scally 	int ret;
122989aef879SDaniel Scally 
123089aef879SDaniel Scally 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
123189aef879SDaniel Scally 	if (!endpoint)
123289aef879SDaniel Scally 		return -EPROBE_DEFER; /* Could be provided by cio2-bridge */
123389aef879SDaniel Scally 
123489aef879SDaniel Scally 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
123589aef879SDaniel Scally 	fwnode_handle_put(endpoint);
123689aef879SDaniel Scally 	if (ret)
123789aef879SDaniel Scally 		return ret;
123889aef879SDaniel Scally 
123989aef879SDaniel Scally 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
124089aef879SDaniel Scally 		dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
124189aef879SDaniel Scally 		ret = -EINVAL;
124289aef879SDaniel Scally 		goto out_free_bus_cfg;
124389aef879SDaniel Scally 	}
124489aef879SDaniel Scally 
124589aef879SDaniel Scally 	if (!bus_cfg.nr_of_link_frequencies) {
124689aef879SDaniel Scally 		dev_err(ov5693->dev, "no link frequencies defined\n");
124789aef879SDaniel Scally 		ret = -EINVAL;
124889aef879SDaniel Scally 		goto out_free_bus_cfg;
124989aef879SDaniel Scally 	}
125089aef879SDaniel Scally 
125189aef879SDaniel Scally 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
125289aef879SDaniel Scally 		if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
125389aef879SDaniel Scally 			break;
125489aef879SDaniel Scally 
125589aef879SDaniel Scally 	if (i == bus_cfg.nr_of_link_frequencies) {
125689aef879SDaniel Scally 		dev_err(ov5693->dev, "supported link freq %ull not found\n",
125789aef879SDaniel Scally 			OV5693_LINK_FREQ_419_2MHZ);
125889aef879SDaniel Scally 		ret = -EINVAL;
125989aef879SDaniel Scally 		goto out_free_bus_cfg;
126089aef879SDaniel Scally 	}
126189aef879SDaniel Scally 
126289aef879SDaniel Scally out_free_bus_cfg:
126389aef879SDaniel Scally 	v4l2_fwnode_endpoint_free(&bus_cfg);
126489aef879SDaniel Scally 
126589aef879SDaniel Scally 	return ret;
126689aef879SDaniel Scally }
126789aef879SDaniel Scally 
ov5693_probe(struct i2c_client * client)126889aef879SDaniel Scally static int ov5693_probe(struct i2c_client *client)
126989aef879SDaniel Scally {
127089aef879SDaniel Scally 	struct ov5693_device *ov5693;
12718a47d09eSTommaso Merciai 	u32 xvclk_rate;
127289aef879SDaniel Scally 	int ret = 0;
127389aef879SDaniel Scally 
127489aef879SDaniel Scally 	ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
127589aef879SDaniel Scally 	if (!ov5693)
127689aef879SDaniel Scally 		return -ENOMEM;
127789aef879SDaniel Scally 
127889aef879SDaniel Scally 	ov5693->dev = &client->dev;
127989aef879SDaniel Scally 
1280*f3a5e2ccSHans de Goede 	ov5693->regmap = devm_cci_regmap_init_i2c(client, 16);
1281*f3a5e2ccSHans de Goede 	if (IS_ERR(ov5693->regmap))
1282*f3a5e2ccSHans de Goede 		return PTR_ERR(ov5693->regmap);
1283*f3a5e2ccSHans de Goede 
128489aef879SDaniel Scally 	ret = ov5693_check_hwcfg(ov5693);
128589aef879SDaniel Scally 	if (ret)
128689aef879SDaniel Scally 		return ret;
128789aef879SDaniel Scally 
128889aef879SDaniel Scally 	mutex_init(&ov5693->lock);
128989aef879SDaniel Scally 
129089aef879SDaniel Scally 	v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);
129189aef879SDaniel Scally 
129288b0c212STommaso Merciai 	ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
129388b0c212STommaso Merciai 	if (IS_ERR(ov5693->xvclk))
129488b0c212STommaso Merciai 		return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
129588b0c212STommaso Merciai 				     "failed to get xvclk: %ld\n",
129688b0c212STommaso Merciai 				     PTR_ERR(ov5693->xvclk));
129788b0c212STommaso Merciai 
129888b0c212STommaso Merciai 	if (ov5693->xvclk) {
129988b0c212STommaso Merciai 		xvclk_rate = clk_get_rate(ov5693->xvclk);
130088b0c212STommaso Merciai 	} else {
130188b0c212STommaso Merciai 		ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
130288b0c212STommaso Merciai 				     "clock-frequency",
130388b0c212STommaso Merciai 				     &xvclk_rate);
130488b0c212STommaso Merciai 
130588b0c212STommaso Merciai 		if (ret) {
130688b0c212STommaso Merciai 			dev_err(&client->dev, "can't get clock frequency");
130788b0c212STommaso Merciai 			return ret;
130888b0c212STommaso Merciai 		}
130989aef879SDaniel Scally 	}
131089aef879SDaniel Scally 
13118a47d09eSTommaso Merciai 	if (xvclk_rate != OV5693_XVCLK_FREQ)
131289aef879SDaniel Scally 		dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
13138a47d09eSTommaso Merciai 			 xvclk_rate, OV5693_XVCLK_FREQ);
131489aef879SDaniel Scally 
131589aef879SDaniel Scally 	ret = ov5693_configure_gpios(ov5693);
131689aef879SDaniel Scally 	if (ret)
131789aef879SDaniel Scally 		return ret;
131889aef879SDaniel Scally 
131989aef879SDaniel Scally 	ret = ov5693_get_regulators(ov5693);
132089aef879SDaniel Scally 	if (ret)
132189aef879SDaniel Scally 		return dev_err_probe(&client->dev, ret,
132289aef879SDaniel Scally 				     "Error fetching regulators\n");
132389aef879SDaniel Scally 
132489aef879SDaniel Scally 	ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
132589aef879SDaniel Scally 	ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
132689aef879SDaniel Scally 	ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
132789aef879SDaniel Scally 
132889aef879SDaniel Scally 	ov5693->mode.crop = ov5693_default_crop;
132989aef879SDaniel Scally 	ov5693->mode.format = ov5693_default_fmt;
133089aef879SDaniel Scally 	ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);
133189aef879SDaniel Scally 
133289aef879SDaniel Scally 	ret = ov5693_init_controls(ov5693);
133389aef879SDaniel Scally 	if (ret)
133489aef879SDaniel Scally 		return ret;
133589aef879SDaniel Scally 
133689aef879SDaniel Scally 	ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
133789aef879SDaniel Scally 	if (ret)
133889aef879SDaniel Scally 		goto err_ctrl_handler_free;
133989aef879SDaniel Scally 
134089aef879SDaniel Scally 	/*
134189aef879SDaniel Scally 	 * We need the driver to work in the event that pm runtime is disable in
134289aef879SDaniel Scally 	 * the kernel, so power up and verify the chip now. In the event that
134389aef879SDaniel Scally 	 * runtime pm is disabled this will leave the chip on, so that streaming
134489aef879SDaniel Scally 	 * will work.
134589aef879SDaniel Scally 	 */
134689aef879SDaniel Scally 
134789aef879SDaniel Scally 	ret = ov5693_sensor_powerup(ov5693);
134889aef879SDaniel Scally 	if (ret)
134989aef879SDaniel Scally 		goto err_media_entity_cleanup;
135089aef879SDaniel Scally 
135189aef879SDaniel Scally 	ret = ov5693_detect(ov5693);
135289aef879SDaniel Scally 	if (ret)
135389aef879SDaniel Scally 		goto err_powerdown;
135489aef879SDaniel Scally 
135589aef879SDaniel Scally 	pm_runtime_set_active(&client->dev);
135689aef879SDaniel Scally 	pm_runtime_get_noresume(&client->dev);
135789aef879SDaniel Scally 	pm_runtime_enable(&client->dev);
135889aef879SDaniel Scally 
135989aef879SDaniel Scally 	ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
136089aef879SDaniel Scally 	if (ret) {
136189aef879SDaniel Scally 		dev_err(&client->dev, "failed to register V4L2 subdev: %d",
136289aef879SDaniel Scally 			ret);
136389aef879SDaniel Scally 		goto err_pm_runtime;
136489aef879SDaniel Scally 	}
136589aef879SDaniel Scally 
136689aef879SDaniel Scally 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
136789aef879SDaniel Scally 	pm_runtime_use_autosuspend(&client->dev);
136889aef879SDaniel Scally 	pm_runtime_put_autosuspend(&client->dev);
136989aef879SDaniel Scally 
137089aef879SDaniel Scally 	return ret;
137189aef879SDaniel Scally 
137289aef879SDaniel Scally err_pm_runtime:
137389aef879SDaniel Scally 	pm_runtime_disable(&client->dev);
137489aef879SDaniel Scally 	pm_runtime_put_noidle(&client->dev);
137589aef879SDaniel Scally err_powerdown:
137689aef879SDaniel Scally 	ov5693_sensor_powerdown(ov5693);
137789aef879SDaniel Scally err_media_entity_cleanup:
137889aef879SDaniel Scally 	media_entity_cleanup(&ov5693->sd.entity);
137989aef879SDaniel Scally err_ctrl_handler_free:
138089aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
138189aef879SDaniel Scally 
138289aef879SDaniel Scally 	return ret;
138389aef879SDaniel Scally }
138489aef879SDaniel Scally 
ov5693_remove(struct i2c_client * client)1385ed5c2f5fSUwe Kleine-König static void ov5693_remove(struct i2c_client *client)
138689aef879SDaniel Scally {
138789aef879SDaniel Scally 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
138889aef879SDaniel Scally 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
138989aef879SDaniel Scally 
139089aef879SDaniel Scally 	v4l2_async_unregister_subdev(sd);
139189aef879SDaniel Scally 	media_entity_cleanup(&ov5693->sd.entity);
139289aef879SDaniel Scally 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
139389aef879SDaniel Scally 	mutex_destroy(&ov5693->lock);
139489aef879SDaniel Scally 
139589aef879SDaniel Scally 	/*
139689aef879SDaniel Scally 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
139789aef879SDaniel Scally 	 * make sure to turn power off manually.
139889aef879SDaniel Scally 	 */
139989aef879SDaniel Scally 	pm_runtime_disable(&client->dev);
140089aef879SDaniel Scally 	if (!pm_runtime_status_suspended(&client->dev))
140189aef879SDaniel Scally 		ov5693_sensor_powerdown(ov5693);
140289aef879SDaniel Scally 	pm_runtime_set_suspended(&client->dev);
140389aef879SDaniel Scally }
140489aef879SDaniel Scally 
140589aef879SDaniel Scally static const struct dev_pm_ops ov5693_pm_ops = {
140689aef879SDaniel Scally 	SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
140789aef879SDaniel Scally };
140889aef879SDaniel Scally 
140989aef879SDaniel Scally static const struct acpi_device_id ov5693_acpi_match[] = {
141089aef879SDaniel Scally 	{"INT33BE"},
141189aef879SDaniel Scally 	{},
141289aef879SDaniel Scally };
141389aef879SDaniel Scally MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
141489aef879SDaniel Scally 
1415109670dfSTommaso Merciai static const struct of_device_id ov5693_of_match[] = {
1416109670dfSTommaso Merciai 	{ .compatible = "ovti,ov5693", },
1417109670dfSTommaso Merciai 	{ /* sentinel */ },
1418109670dfSTommaso Merciai };
1419109670dfSTommaso Merciai MODULE_DEVICE_TABLE(of, ov5693_of_match);
1420109670dfSTommaso Merciai 
142189aef879SDaniel Scally static struct i2c_driver ov5693_driver = {
142289aef879SDaniel Scally 	.driver = {
142389aef879SDaniel Scally 		.name = "ov5693",
142489aef879SDaniel Scally 		.acpi_match_table = ov5693_acpi_match,
1425109670dfSTommaso Merciai 		.of_match_table = ov5693_of_match,
142689aef879SDaniel Scally 		.pm = &ov5693_pm_ops,
142789aef879SDaniel Scally 	},
1428aaeb31c0SUwe Kleine-König 	.probe = ov5693_probe,
142989aef879SDaniel Scally 	.remove = ov5693_remove,
143089aef879SDaniel Scally };
143189aef879SDaniel Scally module_i2c_driver(ov5693_driver);
143289aef879SDaniel Scally 
143389aef879SDaniel Scally MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
143489aef879SDaniel Scally MODULE_LICENSE("GPL");
1435