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