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