xref: /openbmc/linux/drivers/media/i2c/ov5693.c (revision 927a8e38)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
4  *
5  * Adapted from the atomisp-ov5693 driver, with contributions from:
6  *
7  * Daniel Scally
8  * Jean-Michel Hautbois
9  * Fabian Wuthrich
10  * Tsuchiya Yuto
11  * Jordan Hand
12  * Jake Day
13  */
14 
15 #include <asm/unaligned.h>
16 #include <linux/acpi.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-fwnode.h>
29 
30 #define OV5693_REG_8BIT(n)			((1 << 16) | (n))
31 #define OV5693_REG_16BIT(n)			((2 << 16) | (n))
32 #define OV5693_REG_24BIT(n)			((3 << 16) | (n))
33 #define OV5693_REG_SIZE_SHIFT			16
34 #define OV5693_REG_ADDR_MASK			0xffff
35 
36 /* System Control */
37 #define OV5693_SW_RESET_REG			OV5693_REG_8BIT(0x0103)
38 #define OV5693_SW_STREAM_REG			OV5693_REG_8BIT(0x0100)
39 #define OV5693_START_STREAMING			0x01
40 #define OV5693_STOP_STREAMING			0x00
41 #define OV5693_SW_RESET				0x01
42 
43 #define OV5693_REG_CHIP_ID			OV5693_REG_16BIT(0x300a)
44 /* Yes, this is right. The datasheet for the OV5693 gives its ID as 0x5690 */
45 #define OV5693_CHIP_ID				0x5690
46 
47 /* Exposure */
48 #define OV5693_EXPOSURE_CTRL_REG		OV5693_REG_24BIT(0x3500)
49 #define OV5693_EXPOSURE_CTRL_MASK		GENMASK(19, 4)
50 #define OV5693_INTEGRATION_TIME_MARGIN		8
51 #define OV5693_EXPOSURE_MIN			1
52 #define OV5693_EXPOSURE_STEP			1
53 
54 /* Analogue Gain */
55 #define OV5693_GAIN_CTRL_REG			OV5693_REG_16BIT(0x350a)
56 #define OV5693_GAIN_CTRL_MASK			GENMASK(10, 4)
57 #define OV5693_GAIN_MIN				1
58 #define OV5693_GAIN_MAX				127
59 #define OV5693_GAIN_DEF				8
60 #define OV5693_GAIN_STEP			1
61 
62 /* Digital Gain */
63 #define OV5693_MWB_RED_GAIN_REG			OV5693_REG_16BIT(0x3400)
64 #define OV5693_MWB_GREEN_GAIN_REG		OV5693_REG_16BIT(0x3402)
65 #define OV5693_MWB_BLUE_GAIN_REG		OV5693_REG_16BIT(0x3404)
66 #define OV5693_MWB_GAIN_MASK			GENMASK(11, 0)
67 #define OV5693_MWB_GAIN_MAX			0x0fff
68 #define OV5693_DIGITAL_GAIN_MIN			1
69 #define OV5693_DIGITAL_GAIN_MAX			4095
70 #define OV5693_DIGITAL_GAIN_DEF			1024
71 #define OV5693_DIGITAL_GAIN_STEP		1
72 
73 /* Timing and Format */
74 #define OV5693_CROP_START_X_REG			OV5693_REG_16BIT(0x3800)
75 #define OV5693_CROP_START_Y_REG			OV5693_REG_16BIT(0x3802)
76 #define OV5693_CROP_END_X_REG			OV5693_REG_16BIT(0x3804)
77 #define OV5693_CROP_END_Y_REG			OV5693_REG_16BIT(0x3806)
78 #define OV5693_OUTPUT_SIZE_X_REG		OV5693_REG_16BIT(0x3808)
79 #define OV5693_OUTPUT_SIZE_Y_REG		OV5693_REG_16BIT(0x380a)
80 
81 #define OV5693_TIMING_HTS_REG			OV5693_REG_16BIT(0x380c)
82 #define OV5693_FIXED_PPL			2688U
83 #define OV5693_TIMING_VTS_REG			OV5693_REG_16BIT(0x380e)
84 #define OV5693_TIMING_MAX_VTS			0xffff
85 #define OV5693_TIMING_MIN_VTS			0x04
86 
87 #define OV5693_OFFSET_START_X_REG		OV5693_REG_16BIT(0x3810)
88 #define OV5693_OFFSET_START_Y_REG		OV5693_REG_16BIT(0x3812)
89 
90 #define OV5693_SUB_INC_X_REG			OV5693_REG_8BIT(0x3814)
91 #define OV5693_SUB_INC_Y_REG			OV5693_REG_8BIT(0x3815)
92 
93 #define OV5693_FORMAT1_REG			OV5693_REG_8BIT(0x3820)
94 #define OV5693_FORMAT1_FLIP_VERT_ISP_EN		BIT(6)
95 #define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN	BIT(1)
96 #define OV5693_FORMAT1_VBIN_EN			BIT(0)
97 #define OV5693_FORMAT2_REG			OV5693_REG_8BIT(0x3821)
98 #define OV5693_FORMAT2_HDR_EN			BIT(7)
99 #define OV5693_FORMAT2_FLIP_HORZ_ISP_EN		BIT(2)
100 #define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN	BIT(1)
101 #define OV5693_FORMAT2_HBIN_EN			BIT(0)
102 
103 #define OV5693_ISP_CTRL2_REG			OV5693_REG_8BIT(0x5002)
104 #define OV5693_ISP_SCALE_ENABLE			BIT(7)
105 
106 /* Pixel Array */
107 #define OV5693_NATIVE_WIDTH			2624
108 #define OV5693_NATIVE_HEIGHT			1956
109 #define OV5693_NATIVE_START_LEFT		0
110 #define OV5693_NATIVE_START_TOP			0
111 #define OV5693_ACTIVE_WIDTH			2592
112 #define OV5693_ACTIVE_HEIGHT			1944
113 #define OV5693_ACTIVE_START_LEFT		16
114 #define OV5693_ACTIVE_START_TOP			6
115 #define OV5693_MIN_CROP_WIDTH			2
116 #define OV5693_MIN_CROP_HEIGHT			2
117 
118 /* Test Pattern */
119 #define OV5693_TEST_PATTERN_REG			OV5693_REG_8BIT(0x5e00)
120 #define OV5693_TEST_PATTERN_ENABLE		BIT(7)
121 #define OV5693_TEST_PATTERN_ROLLING		BIT(6)
122 #define OV5693_TEST_PATTERN_RANDOM		0x01
123 #define OV5693_TEST_PATTERN_BARS		0x00
124 
125 /* System Frequencies */
126 #define OV5693_XVCLK_FREQ			19200000
127 #define OV5693_LINK_FREQ_419_2MHZ		419200000
128 #define OV5693_PIXEL_RATE			167680000
129 
130 #define to_ov5693_sensor(x) container_of(x, struct ov5693_device, sd)
131 
132 static const char * const ov5693_supply_names[] = {
133 	"avdd",		/* Analog power */
134 	"dovdd",	/* Digital I/O power */
135 	"dvdd",		/* Digital circuit power */
136 };
137 
138 #define OV5693_NUM_SUPPLIES	ARRAY_SIZE(ov5693_supply_names)
139 
140 struct ov5693_reg {
141 	u32 reg;
142 	u8 val;
143 };
144 
145 struct ov5693_reg_list {
146 	u32 num_regs;
147 	const struct ov5693_reg *regs;
148 };
149 
150 struct ov5693_device {
151 	struct i2c_client *client;
152 	struct device *dev;
153 
154 	/* Protect against concurrent changes to controls */
155 	struct mutex lock;
156 
157 	struct gpio_desc *reset;
158 	struct gpio_desc *powerdown;
159 	struct gpio_desc *privacy_led;
160 	struct regulator_bulk_data supplies[OV5693_NUM_SUPPLIES];
161 	struct clk *xvclk;
162 
163 	struct ov5693_mode {
164 		struct v4l2_rect crop;
165 		struct v4l2_mbus_framefmt format;
166 		bool binning_x;
167 		bool binning_y;
168 		unsigned int inc_x_odd;
169 		unsigned int inc_y_odd;
170 		unsigned int vts;
171 	} mode;
172 	bool streaming;
173 
174 	struct v4l2_subdev sd;
175 	struct media_pad pad;
176 
177 	struct ov5693_v4l2_ctrls {
178 		struct v4l2_ctrl_handler handler;
179 		struct v4l2_ctrl *link_freq;
180 		struct v4l2_ctrl *pixel_rate;
181 		struct v4l2_ctrl *exposure;
182 		struct v4l2_ctrl *analogue_gain;
183 		struct v4l2_ctrl *digital_gain;
184 		struct v4l2_ctrl *hflip;
185 		struct v4l2_ctrl *vflip;
186 		struct v4l2_ctrl *hblank;
187 		struct v4l2_ctrl *vblank;
188 		struct v4l2_ctrl *test_pattern;
189 	} ctrls;
190 };
191 
192 static const struct ov5693_reg ov5693_global_regs[] = {
193 	{OV5693_REG_8BIT(0x3016), 0xf0},
194 	{OV5693_REG_8BIT(0x3017), 0xf0},
195 	{OV5693_REG_8BIT(0x3018), 0xf0},
196 	{OV5693_REG_8BIT(0x3022), 0x01},
197 	{OV5693_REG_8BIT(0x3028), 0x44},
198 	{OV5693_REG_8BIT(0x3098), 0x02},
199 	{OV5693_REG_8BIT(0x3099), 0x19},
200 	{OV5693_REG_8BIT(0x309a), 0x02},
201 	{OV5693_REG_8BIT(0x309b), 0x01},
202 	{OV5693_REG_8BIT(0x309c), 0x00},
203 	{OV5693_REG_8BIT(0x30a0), 0xd2},
204 	{OV5693_REG_8BIT(0x30a2), 0x01},
205 	{OV5693_REG_8BIT(0x30b2), 0x00},
206 	{OV5693_REG_8BIT(0x30b3), 0x83},
207 	{OV5693_REG_8BIT(0x30b4), 0x03},
208 	{OV5693_REG_8BIT(0x30b5), 0x04},
209 	{OV5693_REG_8BIT(0x30b6), 0x01},
210 	{OV5693_REG_8BIT(0x3080), 0x01},
211 	{OV5693_REG_8BIT(0x3104), 0x21},
212 	{OV5693_REG_8BIT(0x3106), 0x00},
213 	{OV5693_REG_8BIT(0x3406), 0x01},
214 	{OV5693_REG_8BIT(0x3503), 0x07},
215 	{OV5693_REG_8BIT(0x350b), 0x40},
216 	{OV5693_REG_8BIT(0x3601), 0x0a},
217 	{OV5693_REG_8BIT(0x3602), 0x38},
218 	{OV5693_REG_8BIT(0x3612), 0x80},
219 	{OV5693_REG_8BIT(0x3620), 0x54},
220 	{OV5693_REG_8BIT(0x3621), 0xc7},
221 	{OV5693_REG_8BIT(0x3622), 0x0f},
222 	{OV5693_REG_8BIT(0x3625), 0x10},
223 	{OV5693_REG_8BIT(0x3630), 0x55},
224 	{OV5693_REG_8BIT(0x3631), 0xf4},
225 	{OV5693_REG_8BIT(0x3632), 0x00},
226 	{OV5693_REG_8BIT(0x3633), 0x34},
227 	{OV5693_REG_8BIT(0x3634), 0x02},
228 	{OV5693_REG_8BIT(0x364d), 0x0d},
229 	{OV5693_REG_8BIT(0x364f), 0xdd},
230 	{OV5693_REG_8BIT(0x3660), 0x04},
231 	{OV5693_REG_8BIT(0x3662), 0x10},
232 	{OV5693_REG_8BIT(0x3663), 0xf1},
233 	{OV5693_REG_8BIT(0x3665), 0x00},
234 	{OV5693_REG_8BIT(0x3666), 0x20},
235 	{OV5693_REG_8BIT(0x3667), 0x00},
236 	{OV5693_REG_8BIT(0x366a), 0x80},
237 	{OV5693_REG_8BIT(0x3680), 0xe0},
238 	{OV5693_REG_8BIT(0x3681), 0x00},
239 	{OV5693_REG_8BIT(0x3700), 0x42},
240 	{OV5693_REG_8BIT(0x3701), 0x14},
241 	{OV5693_REG_8BIT(0x3702), 0xa0},
242 	{OV5693_REG_8BIT(0x3703), 0xd8},
243 	{OV5693_REG_8BIT(0x3704), 0x78},
244 	{OV5693_REG_8BIT(0x3705), 0x02},
245 	{OV5693_REG_8BIT(0x370a), 0x00},
246 	{OV5693_REG_8BIT(0x370b), 0x20},
247 	{OV5693_REG_8BIT(0x370c), 0x0c},
248 	{OV5693_REG_8BIT(0x370d), 0x11},
249 	{OV5693_REG_8BIT(0x370e), 0x00},
250 	{OV5693_REG_8BIT(0x370f), 0x40},
251 	{OV5693_REG_8BIT(0x3710), 0x00},
252 	{OV5693_REG_8BIT(0x371a), 0x1c},
253 	{OV5693_REG_8BIT(0x371b), 0x05},
254 	{OV5693_REG_8BIT(0x371c), 0x01},
255 	{OV5693_REG_8BIT(0x371e), 0xa1},
256 	{OV5693_REG_8BIT(0x371f), 0x0c},
257 	{OV5693_REG_8BIT(0x3721), 0x00},
258 	{OV5693_REG_8BIT(0x3724), 0x10},
259 	{OV5693_REG_8BIT(0x3726), 0x00},
260 	{OV5693_REG_8BIT(0x372a), 0x01},
261 	{OV5693_REG_8BIT(0x3730), 0x10},
262 	{OV5693_REG_8BIT(0x3738), 0x22},
263 	{OV5693_REG_8BIT(0x3739), 0xe5},
264 	{OV5693_REG_8BIT(0x373a), 0x50},
265 	{OV5693_REG_8BIT(0x373b), 0x02},
266 	{OV5693_REG_8BIT(0x373c), 0x41},
267 	{OV5693_REG_8BIT(0x373f), 0x02},
268 	{OV5693_REG_8BIT(0x3740), 0x42},
269 	{OV5693_REG_8BIT(0x3741), 0x02},
270 	{OV5693_REG_8BIT(0x3742), 0x18},
271 	{OV5693_REG_8BIT(0x3743), 0x01},
272 	{OV5693_REG_8BIT(0x3744), 0x02},
273 	{OV5693_REG_8BIT(0x3747), 0x10},
274 	{OV5693_REG_8BIT(0x374c), 0x04},
275 	{OV5693_REG_8BIT(0x3751), 0xf0},
276 	{OV5693_REG_8BIT(0x3752), 0x00},
277 	{OV5693_REG_8BIT(0x3753), 0x00},
278 	{OV5693_REG_8BIT(0x3754), 0xc0},
279 	{OV5693_REG_8BIT(0x3755), 0x00},
280 	{OV5693_REG_8BIT(0x3756), 0x1a},
281 	{OV5693_REG_8BIT(0x3758), 0x00},
282 	{OV5693_REG_8BIT(0x3759), 0x0f},
283 	{OV5693_REG_8BIT(0x376b), 0x44},
284 	{OV5693_REG_8BIT(0x375c), 0x04},
285 	{OV5693_REG_8BIT(0x3774), 0x10},
286 	{OV5693_REG_8BIT(0x3776), 0x00},
287 	{OV5693_REG_8BIT(0x377f), 0x08},
288 	{OV5693_REG_8BIT(0x3780), 0x22},
289 	{OV5693_REG_8BIT(0x3781), 0x0c},
290 	{OV5693_REG_8BIT(0x3784), 0x2c},
291 	{OV5693_REG_8BIT(0x3785), 0x1e},
292 	{OV5693_REG_8BIT(0x378f), 0xf5},
293 	{OV5693_REG_8BIT(0x3791), 0xb0},
294 	{OV5693_REG_8BIT(0x3795), 0x00},
295 	{OV5693_REG_8BIT(0x3796), 0x64},
296 	{OV5693_REG_8BIT(0x3797), 0x11},
297 	{OV5693_REG_8BIT(0x3798), 0x30},
298 	{OV5693_REG_8BIT(0x3799), 0x41},
299 	{OV5693_REG_8BIT(0x379a), 0x07},
300 	{OV5693_REG_8BIT(0x379b), 0xb0},
301 	{OV5693_REG_8BIT(0x379c), 0x0c},
302 	{OV5693_REG_8BIT(0x3a04), 0x06},
303 	{OV5693_REG_8BIT(0x3a05), 0x14},
304 	{OV5693_REG_8BIT(0x3e07), 0x20},
305 	{OV5693_REG_8BIT(0x4000), 0x08},
306 	{OV5693_REG_8BIT(0x4001), 0x04},
307 	{OV5693_REG_8BIT(0x4004), 0x08},
308 	{OV5693_REG_8BIT(0x4006), 0x20},
309 	{OV5693_REG_8BIT(0x4008), 0x24},
310 	{OV5693_REG_8BIT(0x4009), 0x10},
311 	{OV5693_REG_8BIT(0x4058), 0x00},
312 	{OV5693_REG_8BIT(0x4101), 0xb2},
313 	{OV5693_REG_8BIT(0x4307), 0x31},
314 	{OV5693_REG_8BIT(0x4511), 0x05},
315 	{OV5693_REG_8BIT(0x4512), 0x01},
316 	{OV5693_REG_8BIT(0x481f), 0x30},
317 	{OV5693_REG_8BIT(0x4826), 0x2c},
318 	{OV5693_REG_8BIT(0x4d02), 0xfd},
319 	{OV5693_REG_8BIT(0x4d03), 0xf5},
320 	{OV5693_REG_8BIT(0x4d04), 0x0c},
321 	{OV5693_REG_8BIT(0x4d05), 0xcc},
322 	{OV5693_REG_8BIT(0x4837), 0x0a},
323 	{OV5693_REG_8BIT(0x5003), 0x20},
324 	{OV5693_REG_8BIT(0x5013), 0x00},
325 	{OV5693_REG_8BIT(0x5842), 0x01},
326 	{OV5693_REG_8BIT(0x5843), 0x2b},
327 	{OV5693_REG_8BIT(0x5844), 0x01},
328 	{OV5693_REG_8BIT(0x5845), 0x92},
329 	{OV5693_REG_8BIT(0x5846), 0x01},
330 	{OV5693_REG_8BIT(0x5847), 0x8f},
331 	{OV5693_REG_8BIT(0x5848), 0x01},
332 	{OV5693_REG_8BIT(0x5849), 0x0c},
333 	{OV5693_REG_8BIT(0x5e10), 0x0c},
334 	{OV5693_REG_8BIT(0x3820), 0x00},
335 	{OV5693_REG_8BIT(0x3821), 0x1e},
336 	{OV5693_REG_8BIT(0x5041), 0x14}
337 };
338 
339 static const struct ov5693_reg_list ov5693_global_setting = {
340 	.num_regs = ARRAY_SIZE(ov5693_global_regs),
341 	.regs = ov5693_global_regs,
342 };
343 
344 static const struct v4l2_rect ov5693_default_crop = {
345 	.left = OV5693_ACTIVE_START_LEFT,
346 	.top = OV5693_ACTIVE_START_TOP,
347 	.width = OV5693_ACTIVE_WIDTH,
348 	.height = OV5693_ACTIVE_HEIGHT,
349 };
350 
351 static const struct v4l2_mbus_framefmt ov5693_default_fmt = {
352 	.width = OV5693_ACTIVE_WIDTH,
353 	.height = OV5693_ACTIVE_HEIGHT,
354 	.code = MEDIA_BUS_FMT_SBGGR10_1X10,
355 };
356 
357 static const s64 link_freq_menu_items[] = {
358 	OV5693_LINK_FREQ_419_2MHZ
359 };
360 
361 static const char * const ov5693_test_pattern_menu[] = {
362 	"Disabled",
363 	"Random Data",
364 	"Colour Bars",
365 	"Colour Bars with Rolling Bar"
366 };
367 
368 static const u8 ov5693_test_pattern_bits[] = {
369 	0,
370 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_RANDOM,
371 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS,
372 	OV5693_TEST_PATTERN_ENABLE | OV5693_TEST_PATTERN_BARS |
373 	OV5693_TEST_PATTERN_ROLLING,
374 };
375 
376 /* I2C I/O Operations */
377 
378 static int ov5693_read_reg(struct ov5693_device *ov5693, u32 addr, u32 *value)
379 {
380 	struct i2c_client *client = ov5693->client;
381 	__be16 reg;
382 	u8 val[4];
383 	struct i2c_msg msg[] = {
384 		{
385 			.addr	= client->addr,
386 			.flags	= 0,
387 			.len	= 2,
388 			.buf	= (u8 *)&reg,
389 		},
390 		{
391 			.addr	= client->addr,
392 			.flags	= I2C_M_RD,
393 			.buf	= (u8 *)&val,
394 		},
395 	};
396 	unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
397 	unsigned int i;
398 	int ret;
399 
400 	reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);
401 
402 	msg[1].len = len;
403 
404 	ret = i2c_transfer(client->adapter, msg, 2);
405 	if (ret < 0)
406 		return dev_err_probe(&client->dev, ret,
407 				     "Failed to read register 0x%04x\n",
408 				     addr & OV5693_REG_ADDR_MASK);
409 
410 	*value = 0;
411 	for (i = 0; i < len; ++i) {
412 		*value <<= 8;
413 		*value |= val[i];
414 	}
415 
416 	return 0;
417 }
418 
419 static void ov5693_write_reg(struct ov5693_device *ov5693, u32 addr, u32 value,
420 			     int *error)
421 {
422 	struct i2c_client *client = ov5693->client;
423 	struct {
424 		__be16 reg;
425 		u8 val[4];
426 	} __packed buf;
427 	struct i2c_msg msg = {
428 		.addr	= client->addr,
429 		.buf	= (u8 *)&buf,
430 	};
431 	unsigned int len = ((addr >> OV5693_REG_SIZE_SHIFT) & 3);
432 	unsigned int i;
433 	int ret;
434 
435 	if (*error < 0)
436 		return;
437 
438 	buf.reg = cpu_to_be16(addr & OV5693_REG_ADDR_MASK);
439 	for (i = 0; i < len; ++i) {
440 		buf.val[len - i - 1] = value & 0xff;
441 		value >>= 8;
442 	}
443 
444 	msg.len	= len + 2;
445 
446 	ret = i2c_transfer(client->adapter, &msg, 1);
447 	if (ret < 0) {
448 		dev_err(&client->dev, "Failed to write register 0x%04x: %d\n",
449 			addr & OV5693_REG_ADDR_MASK, ret);
450 		*error = ret;
451 	}
452 }
453 
454 static int ov5693_write_reg_array(struct ov5693_device *ov5693,
455 				  const struct ov5693_reg_list *reglist)
456 {
457 	unsigned int i;
458 	int ret = 0;
459 
460 	for (i = 0; i < reglist->num_regs; i++)
461 		ov5693_write_reg(ov5693, reglist->regs[i].reg,
462 				 reglist->regs[i].val, &ret);
463 
464 	return ret;
465 }
466 
467 static int ov5693_update_bits(struct ov5693_device *ov5693, u32 address,
468 			      u32 mask, u32 bits)
469 {
470 	u32 value = 0;
471 	int ret;
472 
473 	ret = ov5693_read_reg(ov5693, address, &value);
474 	if (ret)
475 		return ret;
476 
477 	value &= ~mask;
478 	value |= bits;
479 
480 	ov5693_write_reg(ov5693, address, value, &ret);
481 
482 	return ret;
483 }
484 
485 /* V4L2 Controls Functions */
486 
487 static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
488 				      bool enable)
489 {
490 	u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
491 		  OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
492 	int ret;
493 
494 	ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG, bits,
495 				 enable ? bits : 0);
496 	if (ret)
497 		return ret;
498 
499 	return 0;
500 }
501 
502 static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
503 				      bool enable)
504 {
505 	u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
506 		  OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
507 	int ret;
508 
509 	ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG, bits,
510 				 enable ? bits : 0);
511 	if (ret)
512 		return ret;
513 
514 	return 0;
515 }
516 
517 static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
518 {
519 	u32 exposure;
520 	int ret;
521 
522 	ret = ov5693_read_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, &exposure);
523 	if (ret)
524 		return ret;
525 
526 	/* The lowest 4 bits are unsupported fractional bits */
527 	*value = exposure >> 4;
528 
529 	return 0;
530 }
531 
532 static int ov5693_exposure_configure(struct ov5693_device *ov5693,
533 				     u32 exposure)
534 {
535 	int ret = 0;
536 
537 	exposure = (exposure << 4) & OV5693_EXPOSURE_CTRL_MASK;
538 
539 	ov5693_write_reg(ov5693, OV5693_EXPOSURE_CTRL_REG, exposure, &ret);
540 
541 	return ret;
542 }
543 
544 static int ov5693_get_gain(struct ov5693_device *ov5693, u32 *gain)
545 {
546 	u32 value;
547 	int ret;
548 
549 	ret = ov5693_read_reg(ov5693, OV5693_GAIN_CTRL_REG, &value);
550 	if (ret)
551 		return ret;
552 
553 	/* As with exposure, the lowest 4 bits are fractional bits. */
554 	*gain = value >> 4;
555 
556 	return ret;
557 }
558 
559 static int ov5693_digital_gain_configure(struct ov5693_device *ov5693,
560 					 u32 gain)
561 {
562 	int ret = 0;
563 
564 	gain &= OV5693_MWB_GAIN_MASK;
565 
566 	ov5693_write_reg(ov5693, OV5693_MWB_RED_GAIN_REG, gain, &ret);
567 	ov5693_write_reg(ov5693, OV5693_MWB_GREEN_GAIN_REG, gain, &ret);
568 	ov5693_write_reg(ov5693, OV5693_MWB_BLUE_GAIN_REG, gain, &ret);
569 
570 	return ret;
571 }
572 
573 static int ov5693_analog_gain_configure(struct ov5693_device *ov5693, u32 gain)
574 {
575 	int ret = 0;
576 
577 	gain = (gain << 4) & OV5693_GAIN_CTRL_MASK;
578 
579 	ov5693_write_reg(ov5693, OV5693_GAIN_CTRL_REG, gain, &ret);
580 
581 	return ret;
582 }
583 
584 static int ov5693_vts_configure(struct ov5693_device *ov5693, u32 vblank)
585 {
586 	u16 vts = ov5693->mode.format.height + vblank;
587 	int ret = 0;
588 
589 	ov5693_write_reg(ov5693, OV5693_TIMING_VTS_REG, vts, &ret);
590 
591 	return ret;
592 }
593 
594 static int ov5693_test_pattern_configure(struct ov5693_device *ov5693, u32 idx)
595 {
596 	int ret = 0;
597 
598 	ov5693_write_reg(ov5693, OV5693_TEST_PATTERN_REG,
599 			 ov5693_test_pattern_bits[idx], &ret);
600 
601 	return ret;
602 }
603 
604 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
605 {
606 	struct ov5693_device *ov5693 =
607 	    container_of(ctrl->handler, struct ov5693_device, ctrls.handler);
608 	int ret = 0;
609 
610 	/* If VBLANK is altered we need to update exposure to compensate */
611 	if (ctrl->id == V4L2_CID_VBLANK) {
612 		int exposure_max;
613 
614 		exposure_max = ov5693->mode.format.height + ctrl->val -
615 			       OV5693_INTEGRATION_TIME_MARGIN;
616 		__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
617 					 ov5693->ctrls.exposure->minimum,
618 					 exposure_max,
619 					 ov5693->ctrls.exposure->step,
620 					 min(ov5693->ctrls.exposure->val,
621 					     exposure_max));
622 	}
623 
624 	/* Only apply changes to the controls if the device is powered up */
625 	if (!pm_runtime_get_if_in_use(ov5693->dev))
626 		return 0;
627 
628 	switch (ctrl->id) {
629 	case V4L2_CID_EXPOSURE:
630 		ret = ov5693_exposure_configure(ov5693, ctrl->val);
631 		break;
632 	case V4L2_CID_ANALOGUE_GAIN:
633 		ret = ov5693_analog_gain_configure(ov5693, ctrl->val);
634 		break;
635 	case V4L2_CID_DIGITAL_GAIN:
636 		ret = ov5693_digital_gain_configure(ov5693, ctrl->val);
637 		break;
638 	case V4L2_CID_HFLIP:
639 		ret = ov5693_flip_horz_configure(ov5693, !!ctrl->val);
640 		break;
641 	case V4L2_CID_VFLIP:
642 		ret = ov5693_flip_vert_configure(ov5693, !!ctrl->val);
643 		break;
644 	case V4L2_CID_VBLANK:
645 		ret = ov5693_vts_configure(ov5693, ctrl->val);
646 		break;
647 	case V4L2_CID_TEST_PATTERN:
648 		ret = ov5693_test_pattern_configure(ov5693, ctrl->val);
649 		break;
650 	default:
651 		ret = -EINVAL;
652 	}
653 
654 	pm_runtime_put(ov5693->dev);
655 
656 	return ret;
657 }
658 
659 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
660 {
661 	struct ov5693_device *ov5693 = container_of(ctrl->handler,
662 						    struct ov5693_device,
663 						    ctrls.handler);
664 
665 	switch (ctrl->id) {
666 	case V4L2_CID_EXPOSURE_ABSOLUTE:
667 		return ov5693_get_exposure(ov5693, &ctrl->val);
668 	case V4L2_CID_AUTOGAIN:
669 		return ov5693_get_gain(ov5693, &ctrl->val);
670 	default:
671 		return -EINVAL;
672 	}
673 }
674 
675 static const struct v4l2_ctrl_ops ov5693_ctrl_ops = {
676 	.s_ctrl = ov5693_s_ctrl,
677 	.g_volatile_ctrl = ov5693_g_volatile_ctrl
678 };
679 
680 /* System Control Functions */
681 
682 static int ov5693_mode_configure(struct ov5693_device *ov5693)
683 {
684 	const struct ov5693_mode *mode = &ov5693->mode;
685 	int ret = 0;
686 
687 	/* Crop Start X */
688 	ov5693_write_reg(ov5693, OV5693_CROP_START_X_REG, mode->crop.left,
689 			 &ret);
690 
691 	/* Offset X */
692 	ov5693_write_reg(ov5693, OV5693_OFFSET_START_X_REG, 0, &ret);
693 
694 	/* Output Size X */
695 	ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
696 			 &ret);
697 
698 	/* Crop End X */
699 	ov5693_write_reg(ov5693, OV5693_CROP_END_X_REG,
700 			 mode->crop.left + mode->crop.width, &ret);
701 
702 	/* Horizontal Total Size */
703 	ov5693_write_reg(ov5693, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
704 			 &ret);
705 
706 	/* Crop Start Y */
707 	ov5693_write_reg(ov5693, OV5693_CROP_START_Y_REG, mode->crop.top,
708 			 &ret);
709 
710 	/* Offset Y */
711 	ov5693_write_reg(ov5693, OV5693_OFFSET_START_Y_REG, 0, &ret);
712 
713 	/* Output Size Y */
714 	ov5693_write_reg(ov5693, OV5693_OUTPUT_SIZE_Y_REG, mode->format.height,
715 			 &ret);
716 
717 	/* Crop End Y */
718 	ov5693_write_reg(ov5693, OV5693_CROP_END_Y_REG,
719 			 mode->crop.top + mode->crop.height, &ret);
720 
721 	/* Subsample X increase */
722 	ov5693_write_reg(ov5693, OV5693_SUB_INC_X_REG,
723 			 ((mode->inc_x_odd << 4) & 0xf0) | 0x01, &ret);
724 	/* Subsample Y increase */
725 	ov5693_write_reg(ov5693, OV5693_SUB_INC_Y_REG,
726 			 ((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
727 
728 	if (ret)
729 		return ret;
730 
731 	/* Binning */
732 	ret = ov5693_update_bits(ov5693, OV5693_FORMAT1_REG,
733 				 OV5693_FORMAT1_VBIN_EN,
734 				 mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0);
735 	if (ret)
736 		return ret;
737 
738 	ret = ov5693_update_bits(ov5693, OV5693_FORMAT2_REG,
739 				 OV5693_FORMAT2_HBIN_EN,
740 				 mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0);
741 
742 	return ret;
743 }
744 
745 static int ov5693_enable_streaming(struct ov5693_device *ov5693, bool enable)
746 {
747 	int ret = 0;
748 
749 	ov5693_write_reg(ov5693, OV5693_SW_STREAM_REG,
750 			 enable ? OV5693_START_STREAMING :
751 				  OV5693_STOP_STREAMING, &ret);
752 
753 	return ret;
754 }
755 
756 static int ov5693_sw_reset(struct ov5693_device *ov5693)
757 {
758 	int ret = 0;
759 
760 	ov5693_write_reg(ov5693, OV5693_SW_RESET_REG, OV5693_SW_RESET, &ret);
761 
762 	return ret;
763 }
764 
765 static int ov5693_sensor_init(struct ov5693_device *ov5693)
766 {
767 	int ret;
768 
769 	ret = ov5693_sw_reset(ov5693);
770 	if (ret)
771 		return dev_err_probe(ov5693->dev, ret,
772 				     "software reset error\n");
773 
774 	ret = ov5693_write_reg_array(ov5693, &ov5693_global_setting);
775 	if (ret)
776 		return dev_err_probe(ov5693->dev, ret,
777 				     "global settings error\n");
778 
779 	ret = ov5693_mode_configure(ov5693);
780 	if (ret)
781 		return dev_err_probe(ov5693->dev, ret,
782 				     "mode configure error\n");
783 
784 	ret = ov5693_enable_streaming(ov5693, false);
785 	if (ret)
786 		dev_err(ov5693->dev, "stop streaming error\n");
787 
788 	return ret;
789 }
790 
791 static void ov5693_sensor_powerdown(struct ov5693_device *ov5693)
792 {
793 	gpiod_set_value_cansleep(ov5693->privacy_led, 0);
794 	gpiod_set_value_cansleep(ov5693->reset, 1);
795 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
796 
797 	regulator_bulk_disable(OV5693_NUM_SUPPLIES, ov5693->supplies);
798 
799 	clk_disable_unprepare(ov5693->xvclk);
800 }
801 
802 static int ov5693_sensor_powerup(struct ov5693_device *ov5693)
803 {
804 	int ret;
805 
806 	gpiod_set_value_cansleep(ov5693->reset, 1);
807 	gpiod_set_value_cansleep(ov5693->powerdown, 1);
808 
809 	ret = clk_prepare_enable(ov5693->xvclk);
810 	if (ret) {
811 		dev_err(ov5693->dev, "Failed to enable clk\n");
812 		goto fail_power;
813 	}
814 
815 	ret = regulator_bulk_enable(OV5693_NUM_SUPPLIES, ov5693->supplies);
816 	if (ret) {
817 		dev_err(ov5693->dev, "Failed to enable regulators\n");
818 		goto fail_power;
819 	}
820 
821 	gpiod_set_value_cansleep(ov5693->powerdown, 0);
822 	gpiod_set_value_cansleep(ov5693->reset, 0);
823 	gpiod_set_value_cansleep(ov5693->privacy_led, 1);
824 
825 	usleep_range(5000, 7500);
826 
827 	return 0;
828 
829 fail_power:
830 	ov5693_sensor_powerdown(ov5693);
831 	return ret;
832 }
833 
834 static int __maybe_unused ov5693_sensor_suspend(struct device *dev)
835 {
836 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
837 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
838 
839 	ov5693_sensor_powerdown(ov5693);
840 
841 	return 0;
842 }
843 
844 static int __maybe_unused ov5693_sensor_resume(struct device *dev)
845 {
846 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
847 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
848 	int ret;
849 
850 	mutex_lock(&ov5693->lock);
851 
852 	ret = ov5693_sensor_powerup(ov5693);
853 	if (ret)
854 		goto out_unlock;
855 
856 	ret = ov5693_sensor_init(ov5693);
857 	if (ret) {
858 		dev_err(dev, "ov5693 sensor init failure\n");
859 		goto err_power;
860 	}
861 
862 	goto out_unlock;
863 
864 err_power:
865 	ov5693_sensor_powerdown(ov5693);
866 out_unlock:
867 	mutex_unlock(&ov5693->lock);
868 	return ret;
869 }
870 
871 static int ov5693_detect(struct ov5693_device *ov5693)
872 {
873 	int ret;
874 	u32 id;
875 
876 	ret = ov5693_read_reg(ov5693, OV5693_REG_CHIP_ID, &id);
877 	if (ret)
878 		return ret;
879 
880 	if (id != OV5693_CHIP_ID)
881 		return dev_err_probe(ov5693->dev, -ENODEV,
882 				     "sensor ID mismatch. Found 0x%04x\n", id);
883 
884 	return 0;
885 }
886 
887 /* V4L2 Framework callbacks */
888 
889 static unsigned int __ov5693_calc_vts(u32 height)
890 {
891 	/*
892 	 * We need to set a sensible default VTS for whatever format height we
893 	 * happen to be given from set_fmt(). This function just targets
894 	 * an even multiple of 30fps.
895 	 */
896 
897 	unsigned int tgt_fps;
898 
899 	tgt_fps = rounddown(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / height, 30);
900 
901 	return ALIGN_DOWN(OV5693_PIXEL_RATE / OV5693_FIXED_PPL / tgt_fps, 2);
902 }
903 
904 static struct v4l2_mbus_framefmt *
905 __ov5693_get_pad_format(struct ov5693_device *ov5693,
906 			struct v4l2_subdev_state *state,
907 			unsigned int pad, enum v4l2_subdev_format_whence which)
908 {
909 	switch (which) {
910 	case V4L2_SUBDEV_FORMAT_TRY:
911 		return v4l2_subdev_get_try_format(&ov5693->sd, state, pad);
912 	case V4L2_SUBDEV_FORMAT_ACTIVE:
913 		return &ov5693->mode.format;
914 	default:
915 		return NULL;
916 	}
917 }
918 
919 static struct v4l2_rect *
920 __ov5693_get_pad_crop(struct ov5693_device *ov5693,
921 		      struct v4l2_subdev_state *state,
922 		      unsigned int pad, enum v4l2_subdev_format_whence which)
923 {
924 	switch (which) {
925 	case V4L2_SUBDEV_FORMAT_TRY:
926 		return v4l2_subdev_get_try_crop(&ov5693->sd, state, pad);
927 	case V4L2_SUBDEV_FORMAT_ACTIVE:
928 		return &ov5693->mode.crop;
929 	}
930 
931 	return NULL;
932 }
933 
934 static int ov5693_get_fmt(struct v4l2_subdev *sd,
935 			  struct v4l2_subdev_state *state,
936 			  struct v4l2_subdev_format *format)
937 {
938 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
939 
940 	format->format = ov5693->mode.format;
941 
942 	return 0;
943 }
944 
945 static int ov5693_set_fmt(struct v4l2_subdev *sd,
946 			  struct v4l2_subdev_state *state,
947 			  struct v4l2_subdev_format *format)
948 {
949 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
950 	const struct v4l2_rect *crop;
951 	struct v4l2_mbus_framefmt *fmt;
952 	unsigned int hratio, vratio;
953 	unsigned int width, height;
954 	unsigned int hblank;
955 	int exposure_max;
956 
957 	crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
958 
959 	/*
960 	 * Align to two to simplify the binning calculations below, and clamp
961 	 * the requested format at the crop rectangle
962 	 */
963 	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
964 			OV5693_MIN_CROP_WIDTH, crop->width);
965 	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
966 			 OV5693_MIN_CROP_HEIGHT, crop->height);
967 
968 	/*
969 	 * We can only support setting either the dimensions of the crop rect
970 	 * or those dimensions binned (separately) by a factor of two.
971 	 */
972 	hratio = clamp_t(unsigned int,
973 			 DIV_ROUND_CLOSEST(crop->width, width), 1, 2);
974 	vratio = clamp_t(unsigned int,
975 			 DIV_ROUND_CLOSEST(crop->height, height), 1, 2);
976 
977 	fmt = __ov5693_get_pad_format(ov5693, state, format->pad,
978 				      format->which);
979 
980 	fmt->width = crop->width / hratio;
981 	fmt->height = crop->height / vratio;
982 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
983 
984 	format->format = *fmt;
985 
986 	if (format->which == V4L2_SUBDEV_FORMAT_TRY)
987 		return 0;
988 
989 	mutex_lock(&ov5693->lock);
990 
991 	ov5693->mode.binning_x = hratio > 1;
992 	ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
993 	ov5693->mode.binning_y = vratio > 1;
994 	ov5693->mode.inc_y_odd = vratio > 1 ? 3 : 1;
995 
996 	ov5693->mode.vts = __ov5693_calc_vts(fmt->height);
997 
998 	__v4l2_ctrl_modify_range(ov5693->ctrls.vblank,
999 				 OV5693_TIMING_MIN_VTS,
1000 				 OV5693_TIMING_MAX_VTS - fmt->height,
1001 				 1, ov5693->mode.vts - fmt->height);
1002 	__v4l2_ctrl_s_ctrl(ov5693->ctrls.vblank,
1003 			   ov5693->mode.vts - fmt->height);
1004 
1005 	hblank = OV5693_FIXED_PPL - fmt->width;
1006 	__v4l2_ctrl_modify_range(ov5693->ctrls.hblank, hblank, hblank, 1,
1007 				 hblank);
1008 
1009 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
1010 	__v4l2_ctrl_modify_range(ov5693->ctrls.exposure,
1011 				 ov5693->ctrls.exposure->minimum, exposure_max,
1012 				 ov5693->ctrls.exposure->step,
1013 				 min(ov5693->ctrls.exposure->val,
1014 				     exposure_max));
1015 
1016 	mutex_unlock(&ov5693->lock);
1017 	return 0;
1018 }
1019 
1020 static int ov5693_get_selection(struct v4l2_subdev *sd,
1021 				struct v4l2_subdev_state *state,
1022 				struct v4l2_subdev_selection *sel)
1023 {
1024 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1025 
1026 	switch (sel->target) {
1027 	case V4L2_SEL_TGT_CROP:
1028 		mutex_lock(&ov5693->lock);
1029 		sel->r = *__ov5693_get_pad_crop(ov5693, state, sel->pad,
1030 						sel->which);
1031 		mutex_unlock(&ov5693->lock);
1032 		break;
1033 	case V4L2_SEL_TGT_NATIVE_SIZE:
1034 		sel->r.top = 0;
1035 		sel->r.left = 0;
1036 		sel->r.width = OV5693_NATIVE_WIDTH;
1037 		sel->r.height = OV5693_NATIVE_HEIGHT;
1038 		break;
1039 	case V4L2_SEL_TGT_CROP_BOUNDS:
1040 	case V4L2_SEL_TGT_CROP_DEFAULT:
1041 		sel->r.top = OV5693_ACTIVE_START_TOP;
1042 		sel->r.left = OV5693_ACTIVE_START_LEFT;
1043 		sel->r.width = OV5693_ACTIVE_WIDTH;
1044 		sel->r.height = OV5693_ACTIVE_HEIGHT;
1045 		break;
1046 	default:
1047 		return -EINVAL;
1048 	}
1049 
1050 	return 0;
1051 }
1052 
1053 static int ov5693_set_selection(struct v4l2_subdev *sd,
1054 				struct v4l2_subdev_state *state,
1055 				struct v4l2_subdev_selection *sel)
1056 {
1057 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1058 	struct v4l2_mbus_framefmt *format;
1059 	struct v4l2_rect *__crop;
1060 	struct v4l2_rect rect;
1061 
1062 	if (sel->target != V4L2_SEL_TGT_CROP)
1063 		return -EINVAL;
1064 
1065 	/*
1066 	 * Clamp the boundaries of the crop rectangle to the size of the sensor
1067 	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
1068 	 * disrupted.
1069 	 */
1070 	rect.left = clamp(ALIGN(sel->r.left, 2), OV5693_NATIVE_START_LEFT,
1071 			  OV5693_NATIVE_WIDTH);
1072 	rect.top = clamp(ALIGN(sel->r.top, 2), OV5693_NATIVE_START_TOP,
1073 			 OV5693_NATIVE_HEIGHT);
1074 	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
1075 			     OV5693_MIN_CROP_WIDTH, OV5693_NATIVE_WIDTH);
1076 	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
1077 			      OV5693_MIN_CROP_HEIGHT, OV5693_NATIVE_HEIGHT);
1078 
1079 	/* Make sure the crop rectangle isn't outside the bounds of the array */
1080 	rect.width = min_t(unsigned int, rect.width,
1081 			   OV5693_NATIVE_WIDTH - rect.left);
1082 	rect.height = min_t(unsigned int, rect.height,
1083 			    OV5693_NATIVE_HEIGHT - rect.top);
1084 
1085 	__crop = __ov5693_get_pad_crop(ov5693, state, sel->pad, sel->which);
1086 
1087 	if (rect.width != __crop->width || rect.height != __crop->height) {
1088 		/*
1089 		 * Reset the output image size if the crop rectangle size has
1090 		 * been modified.
1091 		 */
1092 		format = __ov5693_get_pad_format(ov5693, state, sel->pad,
1093 						 sel->which);
1094 		format->width = rect.width;
1095 		format->height = rect.height;
1096 	}
1097 
1098 	*__crop = rect;
1099 	sel->r = rect;
1100 
1101 	return 0;
1102 }
1103 
1104 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1105 {
1106 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1107 	int ret;
1108 
1109 	if (enable) {
1110 		ret = pm_runtime_get_sync(ov5693->dev);
1111 		if (ret < 0)
1112 			goto err_power_down;
1113 
1114 		mutex_lock(&ov5693->lock);
1115 		ret = __v4l2_ctrl_handler_setup(&ov5693->ctrls.handler);
1116 		if (ret) {
1117 			mutex_unlock(&ov5693->lock);
1118 			goto err_power_down;
1119 		}
1120 
1121 		ret = ov5693_enable_streaming(ov5693, true);
1122 		mutex_unlock(&ov5693->lock);
1123 	} else {
1124 		mutex_lock(&ov5693->lock);
1125 		ret = ov5693_enable_streaming(ov5693, false);
1126 		mutex_unlock(&ov5693->lock);
1127 	}
1128 	if (ret)
1129 		goto err_power_down;
1130 
1131 	ov5693->streaming = !!enable;
1132 
1133 	if (!enable)
1134 		pm_runtime_put(ov5693->dev);
1135 
1136 	return 0;
1137 err_power_down:
1138 	pm_runtime_put_noidle(ov5693->dev);
1139 	return ret;
1140 }
1141 
1142 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1143 				   struct v4l2_subdev_frame_interval *interval)
1144 {
1145 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1146 	unsigned int framesize = OV5693_FIXED_PPL * (ov5693->mode.format.height +
1147 				 ov5693->ctrls.vblank->val);
1148 	unsigned int fps = DIV_ROUND_CLOSEST(OV5693_PIXEL_RATE, framesize);
1149 
1150 	interval->interval.numerator = 1;
1151 	interval->interval.denominator = fps;
1152 
1153 	return 0;
1154 }
1155 
1156 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1157 				 struct v4l2_subdev_state *state,
1158 				 struct v4l2_subdev_mbus_code_enum *code)
1159 {
1160 	/* Only a single mbus format is supported */
1161 	if (code->index > 0)
1162 		return -EINVAL;
1163 
1164 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1165 	return 0;
1166 }
1167 
1168 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1169 				  struct v4l2_subdev_state *state,
1170 				  struct v4l2_subdev_frame_size_enum *fse)
1171 {
1172 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1173 	struct v4l2_rect *__crop;
1174 
1175 	if (fse->index > 1 || fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
1176 		return -EINVAL;
1177 
1178 	__crop = __ov5693_get_pad_crop(ov5693, state, fse->pad, fse->which);
1179 	if (!__crop)
1180 		return -EINVAL;
1181 
1182 	fse->min_width = __crop->width / (fse->index + 1);
1183 	fse->min_height = __crop->height / (fse->index + 1);
1184 	fse->max_width = fse->min_width;
1185 	fse->max_height = fse->min_height;
1186 
1187 	return 0;
1188 }
1189 
1190 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1191 	.s_stream = ov5693_s_stream,
1192 	.g_frame_interval = ov5693_g_frame_interval,
1193 };
1194 
1195 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1196 	.enum_mbus_code = ov5693_enum_mbus_code,
1197 	.enum_frame_size = ov5693_enum_frame_size,
1198 	.get_fmt = ov5693_get_fmt,
1199 	.set_fmt = ov5693_set_fmt,
1200 	.get_selection = ov5693_get_selection,
1201 	.set_selection = ov5693_set_selection,
1202 };
1203 
1204 static const struct v4l2_subdev_ops ov5693_ops = {
1205 	.video = &ov5693_video_ops,
1206 	.pad = &ov5693_pad_ops,
1207 };
1208 
1209 /* Sensor and Driver Configuration Functions */
1210 
1211 static int ov5693_init_controls(struct ov5693_device *ov5693)
1212 {
1213 	const struct v4l2_ctrl_ops *ops = &ov5693_ctrl_ops;
1214 	struct ov5693_v4l2_ctrls *ctrls = &ov5693->ctrls;
1215 	struct v4l2_fwnode_device_properties props;
1216 	int vblank_max, vblank_def;
1217 	int exposure_max;
1218 	int hblank;
1219 	int ret;
1220 
1221 	ret = v4l2_ctrl_handler_init(&ctrls->handler, 12);
1222 	if (ret)
1223 		return ret;
1224 
1225 	/* link freq */
1226 	ctrls->link_freq = v4l2_ctrl_new_int_menu(&ctrls->handler,
1227 						  NULL, V4L2_CID_LINK_FREQ,
1228 						  0, 0, link_freq_menu_items);
1229 	if (ctrls->link_freq)
1230 		ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1231 
1232 	/* pixel rate */
1233 	ctrls->pixel_rate = v4l2_ctrl_new_std(&ctrls->handler, NULL,
1234 					      V4L2_CID_PIXEL_RATE, 0,
1235 					      OV5693_PIXEL_RATE, 1,
1236 					      OV5693_PIXEL_RATE);
1237 
1238 	/* Exposure */
1239 	exposure_max = ov5693->mode.vts - OV5693_INTEGRATION_TIME_MARGIN;
1240 	ctrls->exposure = v4l2_ctrl_new_std(&ctrls->handler, ops,
1241 					    V4L2_CID_EXPOSURE,
1242 					    OV5693_EXPOSURE_MIN, exposure_max,
1243 					    OV5693_EXPOSURE_STEP, exposure_max);
1244 
1245 	/* Gain */
1246 	ctrls->analogue_gain = v4l2_ctrl_new_std(&ctrls->handler,
1247 						 ops, V4L2_CID_ANALOGUE_GAIN,
1248 						 OV5693_GAIN_MIN,
1249 						 OV5693_GAIN_MAX,
1250 						 OV5693_GAIN_STEP,
1251 						 OV5693_GAIN_DEF);
1252 
1253 	ctrls->digital_gain = v4l2_ctrl_new_std(&ctrls->handler, ops,
1254 						V4L2_CID_DIGITAL_GAIN,
1255 						OV5693_DIGITAL_GAIN_MIN,
1256 						OV5693_DIGITAL_GAIN_MAX,
1257 						OV5693_DIGITAL_GAIN_STEP,
1258 						OV5693_DIGITAL_GAIN_DEF);
1259 
1260 	/* Flip */
1261 	ctrls->hflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
1262 					 V4L2_CID_HFLIP, 0, 1, 1, 0);
1263 
1264 	ctrls->vflip = v4l2_ctrl_new_std(&ctrls->handler, ops,
1265 					 V4L2_CID_VFLIP, 0, 1, 1, 0);
1266 
1267 	hblank = OV5693_FIXED_PPL - ov5693->mode.format.width;
1268 	ctrls->hblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
1269 					  V4L2_CID_HBLANK, hblank,
1270 					  hblank, 1, hblank);
1271 
1272 	if (ctrls->hblank)
1273 		ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1274 
1275 	vblank_max = OV5693_TIMING_MAX_VTS - ov5693->mode.format.height;
1276 	vblank_def = ov5693->mode.vts - ov5693->mode.format.height;
1277 	ctrls->vblank = v4l2_ctrl_new_std(&ctrls->handler, ops,
1278 					  V4L2_CID_VBLANK,
1279 					  OV5693_TIMING_MIN_VTS,
1280 					  vblank_max, 1, vblank_def);
1281 
1282 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(
1283 					&ctrls->handler, ops,
1284 					V4L2_CID_TEST_PATTERN,
1285 					ARRAY_SIZE(ov5693_test_pattern_menu) - 1,
1286 					0, 0, ov5693_test_pattern_menu);
1287 
1288 	if (ctrls->handler.error) {
1289 		dev_err(ov5693->dev, "Error initialising v4l2 ctrls\n");
1290 		ret = ctrls->handler.error;
1291 		goto err_free_handler;
1292 	}
1293 
1294 	/* set properties from fwnode (e.g. rotation, orientation) */
1295 	ret = v4l2_fwnode_device_parse(ov5693->dev, &props);
1296 	if (ret)
1297 		goto err_free_handler;
1298 
1299 	ret = v4l2_ctrl_new_fwnode_properties(&ctrls->handler, ops,
1300 					      &props);
1301 	if (ret)
1302 		goto err_free_handler;
1303 
1304 	/* Use same lock for controls as for everything else. */
1305 	ctrls->handler.lock = &ov5693->lock;
1306 	ov5693->sd.ctrl_handler = &ctrls->handler;
1307 
1308 	return 0;
1309 
1310 err_free_handler:
1311 	v4l2_ctrl_handler_free(&ctrls->handler);
1312 	return ret;
1313 }
1314 
1315 static int ov5693_configure_gpios(struct ov5693_device *ov5693)
1316 {
1317 	ov5693->reset = devm_gpiod_get_optional(ov5693->dev, "reset",
1318 						GPIOD_OUT_HIGH);
1319 	if (IS_ERR(ov5693->reset)) {
1320 		dev_err(ov5693->dev, "Error fetching reset GPIO\n");
1321 		return PTR_ERR(ov5693->reset);
1322 	}
1323 
1324 	ov5693->powerdown = devm_gpiod_get_optional(ov5693->dev, "powerdown",
1325 						    GPIOD_OUT_HIGH);
1326 	if (IS_ERR(ov5693->powerdown)) {
1327 		dev_err(ov5693->dev, "Error fetching powerdown GPIO\n");
1328 		return PTR_ERR(ov5693->powerdown);
1329 	}
1330 
1331 	ov5693->privacy_led = devm_gpiod_get_optional(ov5693->dev, "privacy-led",
1332 						      GPIOD_OUT_LOW);
1333 	if (IS_ERR(ov5693->privacy_led)) {
1334 		dev_err(ov5693->dev, "Error fetching privacy-led GPIO\n");
1335 		return PTR_ERR(ov5693->privacy_led);
1336 	}
1337 
1338 	return 0;
1339 }
1340 
1341 static int ov5693_get_regulators(struct ov5693_device *ov5693)
1342 {
1343 	unsigned int i;
1344 
1345 	for (i = 0; i < OV5693_NUM_SUPPLIES; i++)
1346 		ov5693->supplies[i].supply = ov5693_supply_names[i];
1347 
1348 	return devm_regulator_bulk_get(ov5693->dev, OV5693_NUM_SUPPLIES,
1349 				       ov5693->supplies);
1350 }
1351 
1352 static int ov5693_check_hwcfg(struct ov5693_device *ov5693)
1353 {
1354 	struct fwnode_handle *fwnode = dev_fwnode(ov5693->dev);
1355 	struct v4l2_fwnode_endpoint bus_cfg = {
1356 		.bus_type = V4L2_MBUS_CSI2_DPHY,
1357 	};
1358 	struct fwnode_handle *endpoint;
1359 	unsigned int i;
1360 	int ret;
1361 
1362 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
1363 	if (!endpoint)
1364 		return -EPROBE_DEFER; /* Could be provided by cio2-bridge */
1365 
1366 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
1367 	fwnode_handle_put(endpoint);
1368 	if (ret)
1369 		return ret;
1370 
1371 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1372 		dev_err(ov5693->dev, "only a 2-lane CSI2 config is supported");
1373 		ret = -EINVAL;
1374 		goto out_free_bus_cfg;
1375 	}
1376 
1377 	if (!bus_cfg.nr_of_link_frequencies) {
1378 		dev_err(ov5693->dev, "no link frequencies defined\n");
1379 		ret = -EINVAL;
1380 		goto out_free_bus_cfg;
1381 	}
1382 
1383 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1384 		if (bus_cfg.link_frequencies[i] == OV5693_LINK_FREQ_419_2MHZ)
1385 			break;
1386 
1387 	if (i == bus_cfg.nr_of_link_frequencies) {
1388 		dev_err(ov5693->dev, "supported link freq %ull not found\n",
1389 			OV5693_LINK_FREQ_419_2MHZ);
1390 		ret = -EINVAL;
1391 		goto out_free_bus_cfg;
1392 	}
1393 
1394 out_free_bus_cfg:
1395 	v4l2_fwnode_endpoint_free(&bus_cfg);
1396 
1397 	return ret;
1398 }
1399 
1400 static int ov5693_probe(struct i2c_client *client)
1401 {
1402 	struct ov5693_device *ov5693;
1403 	u32 xvclk_rate;
1404 	int ret = 0;
1405 
1406 	ov5693 = devm_kzalloc(&client->dev, sizeof(*ov5693), GFP_KERNEL);
1407 	if (!ov5693)
1408 		return -ENOMEM;
1409 
1410 	ov5693->client = client;
1411 	ov5693->dev = &client->dev;
1412 
1413 	ret = ov5693_check_hwcfg(ov5693);
1414 	if (ret)
1415 		return ret;
1416 
1417 	mutex_init(&ov5693->lock);
1418 
1419 	v4l2_i2c_subdev_init(&ov5693->sd, client, &ov5693_ops);
1420 
1421 	ov5693->xvclk = devm_clk_get_optional(&client->dev, "xvclk");
1422 	if (IS_ERR(ov5693->xvclk))
1423 		return dev_err_probe(&client->dev, PTR_ERR(ov5693->xvclk),
1424 				     "failed to get xvclk: %ld\n",
1425 				     PTR_ERR(ov5693->xvclk));
1426 
1427 	if (ov5693->xvclk) {
1428 		xvclk_rate = clk_get_rate(ov5693->xvclk);
1429 	} else {
1430 		ret = fwnode_property_read_u32(dev_fwnode(&client->dev),
1431 				     "clock-frequency",
1432 				     &xvclk_rate);
1433 
1434 		if (ret) {
1435 			dev_err(&client->dev, "can't get clock frequency");
1436 			return ret;
1437 		}
1438 	}
1439 
1440 	if (xvclk_rate != OV5693_XVCLK_FREQ)
1441 		dev_warn(&client->dev, "Found clk freq %u, expected %u\n",
1442 			 xvclk_rate, OV5693_XVCLK_FREQ);
1443 
1444 	ret = ov5693_configure_gpios(ov5693);
1445 	if (ret)
1446 		return ret;
1447 
1448 	ret = ov5693_get_regulators(ov5693);
1449 	if (ret)
1450 		return dev_err_probe(&client->dev, ret,
1451 				     "Error fetching regulators\n");
1452 
1453 	ov5693->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1454 	ov5693->pad.flags = MEDIA_PAD_FL_SOURCE;
1455 	ov5693->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1456 
1457 	ov5693->mode.crop = ov5693_default_crop;
1458 	ov5693->mode.format = ov5693_default_fmt;
1459 	ov5693->mode.vts = __ov5693_calc_vts(ov5693->mode.format.height);
1460 
1461 	ret = ov5693_init_controls(ov5693);
1462 	if (ret)
1463 		return ret;
1464 
1465 	ret = media_entity_pads_init(&ov5693->sd.entity, 1, &ov5693->pad);
1466 	if (ret)
1467 		goto err_ctrl_handler_free;
1468 
1469 	/*
1470 	 * We need the driver to work in the event that pm runtime is disable in
1471 	 * the kernel, so power up and verify the chip now. In the event that
1472 	 * runtime pm is disabled this will leave the chip on, so that streaming
1473 	 * will work.
1474 	 */
1475 
1476 	ret = ov5693_sensor_powerup(ov5693);
1477 	if (ret)
1478 		goto err_media_entity_cleanup;
1479 
1480 	ret = ov5693_detect(ov5693);
1481 	if (ret)
1482 		goto err_powerdown;
1483 
1484 	pm_runtime_set_active(&client->dev);
1485 	pm_runtime_get_noresume(&client->dev);
1486 	pm_runtime_enable(&client->dev);
1487 
1488 	ret = v4l2_async_register_subdev_sensor(&ov5693->sd);
1489 	if (ret) {
1490 		dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1491 			ret);
1492 		goto err_pm_runtime;
1493 	}
1494 
1495 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1496 	pm_runtime_use_autosuspend(&client->dev);
1497 	pm_runtime_put_autosuspend(&client->dev);
1498 
1499 	return ret;
1500 
1501 err_pm_runtime:
1502 	pm_runtime_disable(&client->dev);
1503 	pm_runtime_put_noidle(&client->dev);
1504 err_powerdown:
1505 	ov5693_sensor_powerdown(ov5693);
1506 err_media_entity_cleanup:
1507 	media_entity_cleanup(&ov5693->sd.entity);
1508 err_ctrl_handler_free:
1509 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
1510 
1511 	return ret;
1512 }
1513 
1514 static void ov5693_remove(struct i2c_client *client)
1515 {
1516 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1517 	struct ov5693_device *ov5693 = to_ov5693_sensor(sd);
1518 
1519 	v4l2_async_unregister_subdev(sd);
1520 	media_entity_cleanup(&ov5693->sd.entity);
1521 	v4l2_ctrl_handler_free(&ov5693->ctrls.handler);
1522 	mutex_destroy(&ov5693->lock);
1523 
1524 	/*
1525 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1526 	 * make sure to turn power off manually.
1527 	 */
1528 	pm_runtime_disable(&client->dev);
1529 	if (!pm_runtime_status_suspended(&client->dev))
1530 		ov5693_sensor_powerdown(ov5693);
1531 	pm_runtime_set_suspended(&client->dev);
1532 }
1533 
1534 static const struct dev_pm_ops ov5693_pm_ops = {
1535 	SET_RUNTIME_PM_OPS(ov5693_sensor_suspend, ov5693_sensor_resume, NULL)
1536 };
1537 
1538 static const struct acpi_device_id ov5693_acpi_match[] = {
1539 	{"INT33BE"},
1540 	{},
1541 };
1542 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1543 
1544 static const struct of_device_id ov5693_of_match[] = {
1545 	{ .compatible = "ovti,ov5693", },
1546 	{ /* sentinel */ },
1547 };
1548 MODULE_DEVICE_TABLE(of, ov5693_of_match);
1549 
1550 static struct i2c_driver ov5693_driver = {
1551 	.driver = {
1552 		.name = "ov5693",
1553 		.acpi_match_table = ov5693_acpi_match,
1554 		.of_match_table = ov5693_of_match,
1555 		.pm = &ov5693_pm_ops,
1556 	},
1557 	.probe = ov5693_probe,
1558 	.remove = ov5693_remove,
1559 };
1560 module_i2c_driver(ov5693_driver);
1561 
1562 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1563 MODULE_LICENSE("GPL");
1564