xref: /linux/drivers/media/i2c/ov4689.c (revision 518b21ba139cefa2ee7f9fcf516fdc6743e8db68)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ov4689 driver
4  *
5  * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
6  * Copyright (C) 2022, 2024 Mikhail Rudenko
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regulator/consumer.h>
16 #include <media/media-entity.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-cci.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-fwnode.h>
22 
23 #define OV4689_REG_CTRL_MODE		CCI_REG8(0x0100)
24 #define OV4689_MODE_SW_STANDBY		0x0
25 #define OV4689_MODE_STREAMING		BIT(0)
26 
27 #define OV4689_REG_CHIP_ID		CCI_REG16(0x300a)
28 #define CHIP_ID				0x004688
29 
30 #define OV4689_REG_EXPOSURE		CCI_REG24(0x3500)
31 #define OV4689_EXPOSURE_MIN		4
32 #define OV4689_EXPOSURE_STEP		1
33 
34 #define OV4689_REG_GAIN			CCI_REG16(0x3508)
35 #define OV4689_GAIN_STEP		1
36 #define OV4689_GAIN_DEFAULT		0x80
37 
38 #define OV4689_REG_DIG_GAIN		CCI_REG16(0x352a)
39 #define OV4689_DIG_GAIN_MIN		1
40 #define OV4689_DIG_GAIN_MAX		0x7fff
41 #define OV4689_DIG_GAIN_STEP		1
42 #define OV4689_DIG_GAIN_DEFAULT		0x800
43 
44 #define OV4689_REG_H_CROP_START		CCI_REG16(0x3800)
45 #define OV4689_REG_V_CROP_START		CCI_REG16(0x3802)
46 #define OV4689_REG_H_CROP_END		CCI_REG16(0x3804)
47 #define OV4689_REG_V_CROP_END		CCI_REG16(0x3806)
48 #define OV4689_REG_H_OUTPUT_SIZE	CCI_REG16(0x3808)
49 #define OV4689_REG_V_OUTPUT_SIZE	CCI_REG16(0x380a)
50 
51 #define OV4689_REG_HTS			CCI_REG16(0x380c)
52 #define OV4689_HTS_DIVIDER		4
53 #define OV4689_HTS_MAX			0x7fff
54 
55 #define OV4689_REG_VTS			CCI_REG16(0x380e)
56 #define OV4689_VTS_MAX			0x7fff
57 
58 #define OV4689_REG_H_WIN_OFF		CCI_REG16(0x3810)
59 #define OV4689_REG_V_WIN_OFF		CCI_REG16(0x3812)
60 
61 #define OV4689_REG_TIMING_FORMAT1	CCI_REG8(0x3820) /* Vertical */
62 #define OV4689_REG_TIMING_FORMAT2	CCI_REG8(0x3821) /* Horizontal */
63 #define OV4689_TIMING_FLIP_MASK		GENMASK(2, 1)
64 #define OV4689_TIMING_FLIP_ARRAY	BIT(1)
65 #define OV4689_TIMING_FLIP_DIGITAL	BIT(2)
66 #define OV4689_TIMING_FLIP_BOTH		(OV4689_TIMING_FLIP_ARRAY |\
67 					 OV4689_TIMING_FLIP_DIGITAL)
68 
69 #define OV4689_REG_ANCHOR_LEFT_START	CCI_REG16(0x4020)
70 #define OV4689_ANCHOR_LEFT_START_DEF	576
71 #define OV4689_REG_ANCHOR_LEFT_END	CCI_REG16(0x4022)
72 #define OV4689_ANCHOR_LEFT_END_DEF	831
73 #define OV4689_REG_ANCHOR_RIGHT_START	CCI_REG16(0x4024)
74 #define OV4689_ANCHOR_RIGHT_START_DEF	1984
75 #define OV4689_REG_ANCHOR_RIGHT_END	CCI_REG16(0x4026)
76 #define OV4689_ANCHOR_RIGHT_END_DEF	2239
77 
78 #define OV4689_REG_VFIFO_CTRL_01	CCI_REG8(0x4601)
79 
80 #define OV4689_REG_WB_GAIN_RED		CCI_REG16(0x500c)
81 #define OV4689_REG_WB_GAIN_BLUE		CCI_REG16(0x5010)
82 #define OV4689_WB_GAIN_MIN		1
83 #define OV4689_WB_GAIN_MAX		0xfff
84 #define OV4689_WB_GAIN_STEP		1
85 #define OV4689_WB_GAIN_DEFAULT		0x400
86 
87 #define OV4689_REG_TEST_PATTERN		CCI_REG8(0x5040)
88 #define OV4689_TEST_PATTERN_ENABLE	0x80
89 #define OV4689_TEST_PATTERN_DISABLE	0x0
90 
91 #define OV4689_LANES			4
92 #define OV4689_XVCLK_FREQ		24000000
93 
94 #define OV4689_PIXEL_ARRAY_WIDTH	2720
95 #define OV4689_PIXEL_ARRAY_HEIGHT	1536
96 #define OV4689_DUMMY_ROWS		8	/* 8 dummy rows on each side */
97 #define OV4689_DUMMY_COLUMNS		16	/* 16 dummy columns on each side */
98 
99 static const char *const ov4689_supply_names[] = {
100 	"avdd", /* Analog power */
101 	"dovdd", /* Digital I/O power */
102 	"dvdd", /* Digital core power */
103 };
104 
105 enum ov4689_mode_id {
106 	OV4689_MODE_2688_1520 = 0,
107 	OV4689_NUM_MODES,
108 };
109 
110 struct ov4689_mode {
111 	enum ov4689_mode_id id;
112 	u32 width;
113 	u32 height;
114 	u32 hts_def;
115 	u32 hts_min;
116 	u32 vts_def;
117 	u32 exp_def;
118 	u32 pixel_rate;
119 	const struct cci_reg_sequence *reg_list;
120 	unsigned int num_regs;
121 };
122 
123 struct ov4689 {
124 	struct device *dev;
125 	struct regmap *regmap;
126 	struct clk *xvclk;
127 	struct gpio_desc *reset_gpio;
128 	struct gpio_desc *pwdn_gpio;
129 	struct regulator_bulk_data supplies[ARRAY_SIZE(ov4689_supply_names)];
130 
131 	struct v4l2_subdev subdev;
132 	struct media_pad pad;
133 
134 	u32 clock_rate;
135 
136 	struct v4l2_ctrl_handler ctrl_handler;
137 	struct v4l2_ctrl *exposure;
138 
139 	const struct ov4689_mode *cur_mode;
140 };
141 
142 #define to_ov4689(sd) container_of(sd, struct ov4689, subdev)
143 
144 struct ov4689_gain_range {
145 	u32 logical_min;
146 	u32 logical_max;
147 	u32 offset;
148 	u32 divider;
149 	u32 physical_min;
150 	u32 physical_max;
151 };
152 
153 /*
154  * Xclk 24Mhz
155  * max_framerate 90fps
156  * mipi_datarate per lane 1008Mbps
157  */
158 static const struct cci_reg_sequence ov4689_2688x1520_regs[] = {
159 	/* System control*/
160 	{ CCI_REG8(0x0103), 0x01 }, /* SC_CTRL0103 software_reset = 1 */
161 	{ CCI_REG8(0x3000), 0x20 }, /* SC_CMMN_PAD_OEN0 FSIN_output_enable = 1 */
162 	{ CCI_REG8(0x3021), 0x03 }, /*
163 				     * SC_CMMN_MISC_CTRL fst_stby_ctr = 0,
164 				     * sleep_no_latch_enable = 0
165 				     */
166 
167 	/* AEC PK */
168 	{ CCI_REG8(0x3503), 0x04 }, /* AEC_MANUAL gain_input_as_sensor_gain_format = 1 */
169 
170 	/* ADC and analog control*/
171 	{ CCI_REG8(0x3603), 0x40 },
172 	{ CCI_REG8(0x3604), 0x02 },
173 	{ CCI_REG8(0x3609), 0x12 },
174 	{ CCI_REG8(0x360c), 0x08 },
175 	{ CCI_REG8(0x360f), 0xe5 },
176 	{ CCI_REG8(0x3608), 0x8f },
177 	{ CCI_REG8(0x3611), 0x00 },
178 	{ CCI_REG8(0x3613), 0xf7 },
179 	{ CCI_REG8(0x3616), 0x58 },
180 	{ CCI_REG8(0x3619), 0x99 },
181 	{ CCI_REG8(0x361b), 0x60 },
182 	{ CCI_REG8(0x361e), 0x79 },
183 	{ CCI_REG8(0x3634), 0x10 },
184 	{ CCI_REG8(0x3635), 0x10 },
185 	{ CCI_REG8(0x3636), 0x15 },
186 	{ CCI_REG8(0x3646), 0x86 },
187 	{ CCI_REG8(0x364a), 0x0b },
188 
189 	/* Sensor control */
190 	{ CCI_REG8(0x3700), 0x17 },
191 	{ CCI_REG8(0x3701), 0x22 },
192 	{ CCI_REG8(0x3703), 0x10 },
193 	{ CCI_REG8(0x370a), 0x37 },
194 	{ CCI_REG8(0x3706), 0x63 },
195 	{ CCI_REG8(0x3709), 0x3c },
196 	{ CCI_REG8(0x370c), 0x30 },
197 	{ CCI_REG8(0x3710), 0x24 },
198 	{ CCI_REG8(0x3720), 0x28 },
199 	{ CCI_REG8(0x3729), 0x7b },
200 	{ CCI_REG8(0x372b), 0xbd },
201 	{ CCI_REG8(0x372c), 0xbc },
202 	{ CCI_REG8(0x372e), 0x52 },
203 	{ CCI_REG8(0x373c), 0x0e },
204 	{ CCI_REG8(0x373e), 0x33 },
205 	{ CCI_REG8(0x3743), 0x10 },
206 	{ CCI_REG8(0x3744), 0x88 },
207 	{ CCI_REG8(0x3745), 0xc0 },
208 	{ CCI_REG8(0x374c), 0x00 },
209 	{ CCI_REG8(0x374e), 0x23 },
210 	{ CCI_REG8(0x3751), 0x7b },
211 	{ CCI_REG8(0x3753), 0xbd },
212 	{ CCI_REG8(0x3754), 0xbc },
213 	{ CCI_REG8(0x3756), 0x52 },
214 	{ CCI_REG8(0x376b), 0x20 },
215 	{ CCI_REG8(0x3774), 0x51 },
216 	{ CCI_REG8(0x3776), 0xbd },
217 	{ CCI_REG8(0x3777), 0xbd },
218 	{ CCI_REG8(0x3781), 0x18 },
219 	{ CCI_REG8(0x3783), 0x25 },
220 	{ CCI_REG8(0x3798), 0x1b },
221 
222 	/* Timing control */
223 	{ CCI_REG8(0x3819), 0x01 }, /* VSYNC_END_L vsync_end_point[7:0] = 0x01 */
224 
225 	/* OTP control */
226 	{ CCI_REG8(0x3d85), 0x36 }, /* OTP_REG85 OTP_power_up_load_setting_enable = 1,
227 				     * OTP_power_up_load_data_enable = 1,
228 				     * OTP_bist_select = 1 (compare with zero)
229 				     */
230 	{ CCI_REG8(0x3d8c), 0x71 }, /* OTP_SETTING_STT_ADDRESS_H */
231 	{ CCI_REG8(0x3d8d), 0xcb }, /* OTP_SETTING_STT_ADDRESS_L */
232 
233 	/* BLC registers*/
234 	{ CCI_REG8(0x4001), 0x40 }, /* DEBUG_MODE */
235 	{ CCI_REG8(0x401b), 0x00 }, /* DEBUG_MODE */
236 	{ CCI_REG8(0x401d), 0x00 }, /* DEBUG_MODE */
237 	{ CCI_REG8(0x401f), 0x00 }, /* DEBUG_MODE */
238 
239 	/* ADC sync control */
240 	{ CCI_REG8(0x4500), 0x6c }, /* ADC_SYNC_CTRL */
241 	{ CCI_REG8(0x4503), 0x01 }, /* ADC_SYNC_CTRL */
242 
243 	/* Temperature monitor */
244 	{ CCI_REG8(0x4d00), 0x04 }, /* TPM_CTRL_00 tmp_slope[15:8] = 0x04 */
245 	{ CCI_REG8(0x4d01), 0x42 }, /* TPM_CTRL_01 tmp_slope[7:0] = 0x42 */
246 	{ CCI_REG8(0x4d02), 0xd1 }, /* TPM_CTRL_02 tpm_offset[31:24] = 0xd1 */
247 	{ CCI_REG8(0x4d03), 0x93 }, /* TPM_CTRL_03 tpm_offset[23:16] = 0x93 */
248 	{ CCI_REG8(0x4d04), 0xf5 }, /* TPM_CTRL_04 tpm_offset[15:8]  = 0xf5 */
249 	{ CCI_REG8(0x4d05), 0xc1 }, /* TPM_CTRL_05 tpm_offset[7:0]   = 0xc1 */
250 
251 	/* pre-ISP control */
252 	{ CCI_REG8(0x5050), 0x0c }, /* DEBUG_MODE */
253 
254 	/* OTP-DPC control */
255 	{ CCI_REG8(0x5501), 0x10 }, /* OTP_DPC_START_L otp_start_address[7:0] = 0x10 */
256 	{ CCI_REG8(0x5503), 0x0f }, /* OTP_DPC_END_L otp_end_address[7:0] = 0x0f */
257 };
258 
259 static const struct ov4689_mode supported_modes[] = {
260 	{
261 		.id = OV4689_MODE_2688_1520,
262 		.width = 2688,
263 		.height = 1520,
264 		.exp_def = 1536,
265 		.hts_def = 10296,
266 		.hts_min = 3432,
267 		.vts_def = 1554,
268 		.pixel_rate = 480000000,
269 		.reg_list = ov4689_2688x1520_regs,
270 		.num_regs = ARRAY_SIZE(ov4689_2688x1520_regs),
271 	},
272 };
273 
274 static const u64 link_freq_menu_items[] = { 504000000 };
275 
276 static const char *const ov4689_test_pattern_menu[] = {
277 	"Disabled",
278 	"Vertical Color Bar Type 1",
279 	"Vertical Color Bar Type 2",
280 	"Vertical Color Bar Type 3",
281 	"Vertical Color Bar Type 4"
282 };
283 
284 /*
285  * These coefficients are based on those used in Rockchip's camera
286  * engine, with minor tweaks for continuity.
287  */
288 static const struct ov4689_gain_range ov4689_gain_ranges[] = {
289 	{
290 		.logical_min = 0,
291 		.logical_max = 255,
292 		.offset = 0,
293 		.divider = 1,
294 		.physical_min = 0,
295 		.physical_max = 255,
296 	},
297 	{
298 		.logical_min = 256,
299 		.logical_max = 511,
300 		.offset = 252,
301 		.divider = 2,
302 		.physical_min = 376,
303 		.physical_max = 504,
304 	},
305 	{
306 		.logical_min = 512,
307 		.logical_max = 1023,
308 		.offset = 758,
309 		.divider = 4,
310 		.physical_min = 884,
311 		.physical_max = 1012,
312 	},
313 	{
314 		.logical_min = 1024,
315 		.logical_max = 2047,
316 		.offset = 1788,
317 		.divider = 8,
318 		.physical_min = 1912,
319 		.physical_max = 2047,
320 	},
321 };
322 
ov4689_fill_fmt(const struct ov4689_mode * mode,struct v4l2_mbus_framefmt * fmt)323 static void ov4689_fill_fmt(const struct ov4689_mode *mode,
324 			    struct v4l2_mbus_framefmt *fmt)
325 {
326 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
327 	fmt->width = mode->width;
328 	fmt->height = mode->height;
329 	fmt->field = V4L2_FIELD_NONE;
330 }
331 
ov4689_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)332 static int ov4689_set_fmt(struct v4l2_subdev *sd,
333 			  struct v4l2_subdev_state *sd_state,
334 			  struct v4l2_subdev_format *fmt)
335 {
336 	struct v4l2_mbus_framefmt *mbus_fmt = &fmt->format;
337 	struct ov4689 *ov4689 = to_ov4689(sd);
338 
339 	/* only one mode supported for now */
340 	ov4689_fill_fmt(ov4689->cur_mode, mbus_fmt);
341 
342 	return 0;
343 }
344 
ov4689_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)345 static int ov4689_enum_mbus_code(struct v4l2_subdev *sd,
346 				 struct v4l2_subdev_state *sd_state,
347 				 struct v4l2_subdev_mbus_code_enum *code)
348 {
349 	if (code->index != 0)
350 		return -EINVAL;
351 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
352 
353 	return 0;
354 }
355 
ov4689_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)356 static int ov4689_enum_frame_sizes(struct v4l2_subdev *sd,
357 				   struct v4l2_subdev_state *sd_state,
358 				   struct v4l2_subdev_frame_size_enum *fse)
359 {
360 	if (fse->index >= ARRAY_SIZE(supported_modes))
361 		return -EINVAL;
362 
363 	if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
364 		return -EINVAL;
365 
366 	fse->min_width = supported_modes[fse->index].width;
367 	fse->max_width = supported_modes[fse->index].width;
368 	fse->max_height = supported_modes[fse->index].height;
369 	fse->min_height = supported_modes[fse->index].height;
370 
371 	return 0;
372 }
373 
ov4689_enable_test_pattern(struct ov4689 * ov4689,u32 pattern)374 static int ov4689_enable_test_pattern(struct ov4689 *ov4689, u32 pattern)
375 {
376 	u32 val;
377 
378 	if (pattern)
379 		val = (pattern - 1) | OV4689_TEST_PATTERN_ENABLE;
380 	else
381 		val = OV4689_TEST_PATTERN_DISABLE;
382 
383 	return cci_write(ov4689->regmap, OV4689_REG_TEST_PATTERN,
384 			 val, NULL);
385 }
386 
ov4689_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_selection * sel)387 static int ov4689_get_selection(struct v4l2_subdev *sd,
388 				struct v4l2_subdev_state *state,
389 				struct v4l2_subdev_selection *sel)
390 {
391 	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
392 		return -EINVAL;
393 
394 	switch (sel->target) {
395 	case V4L2_SEL_TGT_CROP_BOUNDS:
396 		sel->r.top = 0;
397 		sel->r.left = 0;
398 		sel->r.width = OV4689_PIXEL_ARRAY_WIDTH;
399 		sel->r.height = OV4689_PIXEL_ARRAY_HEIGHT;
400 		return 0;
401 	case V4L2_SEL_TGT_CROP:
402 	case V4L2_SEL_TGT_CROP_DEFAULT:
403 		sel->r.top = OV4689_DUMMY_ROWS;
404 		sel->r.left = OV4689_DUMMY_COLUMNS;
405 		sel->r.width =
406 			OV4689_PIXEL_ARRAY_WIDTH - 2 * OV4689_DUMMY_COLUMNS;
407 		sel->r.height =
408 			OV4689_PIXEL_ARRAY_HEIGHT - 2 * OV4689_DUMMY_ROWS;
409 		return 0;
410 	}
411 
412 	return -EINVAL;
413 }
414 
ov4689_setup_timings(struct ov4689 * ov4689)415 static int ov4689_setup_timings(struct ov4689 *ov4689)
416 {
417 	const struct ov4689_mode *mode = ov4689->cur_mode;
418 	struct regmap *rm = ov4689->regmap;
419 	int ret = 0;
420 
421 	cci_write(rm, OV4689_REG_H_CROP_START, 8, &ret);
422 	cci_write(rm, OV4689_REG_V_CROP_START, 8, &ret);
423 	cci_write(rm, OV4689_REG_H_CROP_END, 2711, &ret);
424 	cci_write(rm, OV4689_REG_V_CROP_END, 1531, &ret);
425 
426 	cci_write(rm, OV4689_REG_H_OUTPUT_SIZE, mode->width, &ret);
427 	cci_write(rm, OV4689_REG_V_OUTPUT_SIZE, mode->height, &ret);
428 
429 	cci_write(rm, OV4689_REG_H_WIN_OFF, 8, &ret);
430 	cci_write(rm, OV4689_REG_V_WIN_OFF, 4, &ret);
431 
432 	cci_write(rm, OV4689_REG_VFIFO_CTRL_01, 167, &ret);
433 
434 	return ret;
435 }
436 
ov4689_setup_blc_anchors(struct ov4689 * ov4689)437 static int ov4689_setup_blc_anchors(struct ov4689 *ov4689)
438 {
439 	struct regmap *rm = ov4689->regmap;
440 	int ret = 0;
441 
442 	cci_write(rm, OV4689_REG_ANCHOR_LEFT_START, 16, &ret);
443 	cci_write(rm, OV4689_REG_ANCHOR_LEFT_END, 1999, &ret);
444 	cci_write(rm, OV4689_REG_ANCHOR_RIGHT_START, 2400, &ret);
445 	cci_write(rm, OV4689_REG_ANCHOR_RIGHT_END, 2415, &ret);
446 
447 	return ret;
448 }
449 
ov4689_s_stream(struct v4l2_subdev * sd,int on)450 static int ov4689_s_stream(struct v4l2_subdev *sd, int on)
451 {
452 	struct ov4689 *ov4689 = to_ov4689(sd);
453 	struct v4l2_subdev_state *sd_state;
454 	struct device *dev = ov4689->dev;
455 	int ret = 0;
456 
457 	sd_state = v4l2_subdev_lock_and_get_active_state(&ov4689->subdev);
458 
459 	if (on) {
460 		ret = pm_runtime_resume_and_get(dev);
461 		if (ret < 0)
462 			goto unlock_and_return;
463 
464 		ret = cci_multi_reg_write(ov4689->regmap,
465 					  ov4689->cur_mode->reg_list,
466 					  ov4689->cur_mode->num_regs,
467 					  NULL);
468 		if (ret) {
469 			pm_runtime_put(dev);
470 			goto unlock_and_return;
471 		}
472 
473 		ret = ov4689_setup_timings(ov4689);
474 		if (ret) {
475 			pm_runtime_put(dev);
476 			goto unlock_and_return;
477 		}
478 
479 		ret = ov4689_setup_blc_anchors(ov4689);
480 		if (ret) {
481 			pm_runtime_put(dev);
482 			goto unlock_and_return;
483 		}
484 
485 		ret = __v4l2_ctrl_handler_setup(&ov4689->ctrl_handler);
486 		if (ret) {
487 			pm_runtime_put(dev);
488 			goto unlock_and_return;
489 		}
490 
491 		ret = cci_write(ov4689->regmap, OV4689_REG_CTRL_MODE,
492 				OV4689_MODE_STREAMING, NULL);
493 		if (ret) {
494 			pm_runtime_put(dev);
495 			goto unlock_and_return;
496 		}
497 	} else {
498 		cci_write(ov4689->regmap, OV4689_REG_CTRL_MODE,
499 			  OV4689_MODE_SW_STANDBY, NULL);
500 		pm_runtime_put_autosuspend(dev);
501 	}
502 
503 unlock_and_return:
504 	v4l2_subdev_unlock_state(sd_state);
505 
506 	return ret;
507 }
508 
509 /* Calculate the delay in us by clock rate and clock cycles */
ov4689_cal_delay(struct ov4689 * ov4689,u32 cycles)510 static inline u32 ov4689_cal_delay(struct ov4689 *ov4689, u32 cycles)
511 {
512 	return DIV_ROUND_UP(cycles * 1000,
513 			    DIV_ROUND_UP(ov4689->clock_rate, 1000));
514 }
515 
ov4689_power_on(struct device * dev)516 static int __maybe_unused ov4689_power_on(struct device *dev)
517 {
518 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
519 	struct ov4689 *ov4689 = to_ov4689(sd);
520 	u32 delay_us;
521 	int ret;
522 
523 	ret = clk_prepare_enable(ov4689->xvclk);
524 	if (ret < 0) {
525 		dev_err(dev, "Failed to enable xvclk\n");
526 		return ret;
527 	}
528 
529 	gpiod_set_value_cansleep(ov4689->reset_gpio, 1);
530 
531 	ret = regulator_bulk_enable(ARRAY_SIZE(ov4689_supply_names),
532 				    ov4689->supplies);
533 	if (ret < 0) {
534 		dev_err(dev, "Failed to enable regulators\n");
535 		goto disable_clk;
536 	}
537 
538 	gpiod_set_value_cansleep(ov4689->reset_gpio, 0);
539 	usleep_range(500, 1000);
540 	gpiod_set_value_cansleep(ov4689->pwdn_gpio, 0);
541 
542 	/* 8192 cycles prior to first SCCB transaction */
543 	delay_us = ov4689_cal_delay(ov4689, 8192);
544 	usleep_range(delay_us, delay_us * 2);
545 
546 	return 0;
547 
548 disable_clk:
549 	clk_disable_unprepare(ov4689->xvclk);
550 
551 	return ret;
552 }
553 
ov4689_power_off(struct device * dev)554 static int __maybe_unused ov4689_power_off(struct device *dev)
555 {
556 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
557 	struct ov4689 *ov4689 = to_ov4689(sd);
558 
559 	gpiod_set_value_cansleep(ov4689->pwdn_gpio, 1);
560 	clk_disable_unprepare(ov4689->xvclk);
561 	gpiod_set_value_cansleep(ov4689->reset_gpio, 1);
562 	regulator_bulk_disable(ARRAY_SIZE(ov4689_supply_names),
563 			       ov4689->supplies);
564 	return 0;
565 }
566 
ov4689_init_state(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)567 static int ov4689_init_state(struct v4l2_subdev *sd,
568 			     struct v4l2_subdev_state *sd_state)
569 {
570 	struct v4l2_mbus_framefmt *fmt =
571 		v4l2_subdev_state_get_format(sd_state, 0);
572 
573 	ov4689_fill_fmt(&supported_modes[OV4689_MODE_2688_1520], fmt);
574 
575 	return 0;
576 }
577 
578 static const struct dev_pm_ops ov4689_pm_ops = {
579 	SET_RUNTIME_PM_OPS(ov4689_power_off, ov4689_power_on, NULL)
580 };
581 
582 static const struct v4l2_subdev_video_ops ov4689_video_ops = {
583 	.s_stream = ov4689_s_stream,
584 };
585 
586 static const struct v4l2_subdev_pad_ops ov4689_pad_ops = {
587 	.enum_mbus_code = ov4689_enum_mbus_code,
588 	.enum_frame_size = ov4689_enum_frame_sizes,
589 	.get_fmt = v4l2_subdev_get_fmt,
590 	.set_fmt = ov4689_set_fmt,
591 	.get_selection = ov4689_get_selection,
592 };
593 
594 static const struct v4l2_subdev_internal_ops ov4689_internal_ops = {
595 	.init_state = ov4689_init_state,
596 };
597 
598 static const struct v4l2_subdev_ops ov4689_subdev_ops = {
599 	.video = &ov4689_video_ops,
600 	.pad = &ov4689_pad_ops,
601 };
602 
603 /*
604  * Map userspace (logical) gain to sensor (physical) gain using
605  * ov4689_gain_ranges table.
606  */
ov4689_map_gain(struct ov4689 * ov4689,int logical_gain,int * result)607 static int ov4689_map_gain(struct ov4689 *ov4689, int logical_gain, int *result)
608 {
609 	const struct ov4689_gain_range *range;
610 	unsigned int n;
611 
612 	for (n = 0; n < ARRAY_SIZE(ov4689_gain_ranges); n++) {
613 		if (logical_gain >= ov4689_gain_ranges[n].logical_min &&
614 		    logical_gain <= ov4689_gain_ranges[n].logical_max)
615 			break;
616 	}
617 
618 	if (n == ARRAY_SIZE(ov4689_gain_ranges)) {
619 		dev_warn_ratelimited(ov4689->dev,
620 				     "no mapping found for gain %d\n",
621 				     logical_gain);
622 		return -EINVAL;
623 	}
624 
625 	range = &ov4689_gain_ranges[n];
626 
627 	*result = clamp(range->offset + (logical_gain) / range->divider,
628 			range->physical_min, range->physical_max);
629 	return 0;
630 }
631 
ov4689_set_ctrl(struct v4l2_ctrl * ctrl)632 static int ov4689_set_ctrl(struct v4l2_ctrl *ctrl)
633 {
634 	struct ov4689 *ov4689 =
635 		container_of(ctrl->handler, struct ov4689, ctrl_handler);
636 	struct regmap *regmap = ov4689->regmap;
637 	struct device *dev = ov4689->dev;
638 	int sensor_gain = 0;
639 	s64 max_expo;
640 	int ret = 0;
641 
642 	/* Propagate change of current control to all related controls */
643 	switch (ctrl->id) {
644 	case V4L2_CID_VBLANK:
645 		/* Update max exposure while meeting expected vblanking */
646 		max_expo = ov4689->cur_mode->height + ctrl->val - 4;
647 		__v4l2_ctrl_modify_range(ov4689->exposure,
648 					 ov4689->exposure->minimum, max_expo,
649 					 ov4689->exposure->step,
650 					 ov4689->exposure->default_value);
651 		break;
652 	}
653 
654 	if (!pm_runtime_get_if_in_use(dev))
655 		return 0;
656 
657 	switch (ctrl->id) {
658 	case V4L2_CID_EXPOSURE:
659 		/* 4 least significant bits of exposure are fractional part */
660 		cci_write(regmap, OV4689_REG_EXPOSURE, ctrl->val << 4, &ret);
661 		break;
662 	case V4L2_CID_ANALOGUE_GAIN:
663 		ret = ov4689_map_gain(ov4689, ctrl->val, &sensor_gain);
664 		cci_write(regmap, OV4689_REG_GAIN, sensor_gain, &ret);
665 		break;
666 	case V4L2_CID_VBLANK:
667 		cci_write(regmap, OV4689_REG_VTS,
668 			  ctrl->val + ov4689->cur_mode->height, &ret);
669 		break;
670 	case V4L2_CID_TEST_PATTERN:
671 		ret = ov4689_enable_test_pattern(ov4689, ctrl->val);
672 		break;
673 	case V4L2_CID_HBLANK:
674 		cci_write(regmap, OV4689_REG_HTS,
675 			  (ctrl->val + ov4689->cur_mode->width) /
676 			  OV4689_HTS_DIVIDER, &ret);
677 		break;
678 	case V4L2_CID_VFLIP:
679 		cci_update_bits(regmap, OV4689_REG_TIMING_FORMAT1,
680 				OV4689_TIMING_FLIP_MASK,
681 				ctrl->val ? OV4689_TIMING_FLIP_BOTH : 0, &ret);
682 		break;
683 	case V4L2_CID_HFLIP:
684 		cci_update_bits(regmap, OV4689_REG_TIMING_FORMAT2,
685 				OV4689_TIMING_FLIP_MASK,
686 				ctrl->val ? 0 : OV4689_TIMING_FLIP_BOTH, &ret);
687 		break;
688 	case V4L2_CID_DIGITAL_GAIN:
689 		cci_write(regmap, OV4689_REG_DIG_GAIN, ctrl->val, &ret);
690 		break;
691 	case V4L2_CID_RED_BALANCE:
692 		cci_write(regmap, OV4689_REG_WB_GAIN_RED, ctrl->val, &ret);
693 		break;
694 	case V4L2_CID_BLUE_BALANCE:
695 		cci_write(regmap, OV4689_REG_WB_GAIN_BLUE, ctrl->val, &ret);
696 		break;
697 	default:
698 		dev_warn(dev, "%s Unhandled id:0x%x, val:0x%x\n",
699 			 __func__, ctrl->id, ctrl->val);
700 		ret = -EINVAL;
701 		break;
702 	}
703 
704 	pm_runtime_put_autosuspend(dev);
705 
706 	return ret;
707 }
708 
709 static const struct v4l2_ctrl_ops ov4689_ctrl_ops = {
710 	.s_ctrl = ov4689_set_ctrl,
711 };
712 
ov4689_initialize_controls(struct ov4689 * ov4689)713 static int ov4689_initialize_controls(struct ov4689 *ov4689)
714 {
715 	struct i2c_client *client = v4l2_get_subdevdata(&ov4689->subdev);
716 	struct v4l2_fwnode_device_properties props;
717 	struct v4l2_ctrl_handler *handler;
718 	const struct ov4689_mode *mode;
719 	s64 exposure_max, vblank_def;
720 	s64 hblank_def, hblank_min;
721 	struct v4l2_ctrl *ctrl;
722 	int ret;
723 
724 	handler = &ov4689->ctrl_handler;
725 	mode = ov4689->cur_mode;
726 	ret = v4l2_ctrl_handler_init(handler, 15);
727 	if (ret)
728 		return ret;
729 
730 	ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, 0, 0,
731 				      link_freq_menu_items);
732 	if (ctrl)
733 		ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
734 
735 	v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0,
736 			  mode->pixel_rate, 1, mode->pixel_rate);
737 
738 	hblank_def = mode->hts_def - mode->width;
739 	hblank_min = mode->hts_min - mode->width;
740 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_HBLANK,
741 			  hblank_min, OV4689_HTS_MAX - mode->width,
742 			  OV4689_HTS_DIVIDER, hblank_def);
743 
744 	vblank_def = mode->vts_def - mode->height;
745 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_VBLANK,
746 			  vblank_def, OV4689_VTS_MAX - mode->height, 1,
747 			  vblank_def);
748 
749 	exposure_max = mode->vts_def - 4;
750 	ov4689->exposure =
751 		v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_EXPOSURE,
752 				  OV4689_EXPOSURE_MIN, exposure_max,
753 				  OV4689_EXPOSURE_STEP, mode->exp_def);
754 
755 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
756 			  ov4689_gain_ranges[0].logical_min,
757 			  ov4689_gain_ranges[ARRAY_SIZE(ov4689_gain_ranges) - 1]
758 				  .logical_max,
759 			  OV4689_GAIN_STEP, OV4689_GAIN_DEFAULT);
760 
761 	v4l2_ctrl_new_std_menu_items(handler, &ov4689_ctrl_ops,
762 				     V4L2_CID_TEST_PATTERN,
763 				     ARRAY_SIZE(ov4689_test_pattern_menu) - 1,
764 				     0, 0, ov4689_test_pattern_menu);
765 
766 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
767 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
768 
769 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
770 			  OV4689_DIG_GAIN_MIN, OV4689_DIG_GAIN_MAX,
771 			  OV4689_DIG_GAIN_STEP, OV4689_DIG_GAIN_DEFAULT);
772 
773 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_RED_BALANCE,
774 			  OV4689_WB_GAIN_MIN, OV4689_WB_GAIN_MAX,
775 			  OV4689_WB_GAIN_STEP, OV4689_WB_GAIN_DEFAULT);
776 
777 	v4l2_ctrl_new_std(handler, &ov4689_ctrl_ops, V4L2_CID_BLUE_BALANCE,
778 			  OV4689_WB_GAIN_MIN, OV4689_WB_GAIN_MAX,
779 			  OV4689_WB_GAIN_STEP, OV4689_WB_GAIN_DEFAULT);
780 
781 	if (handler->error) {
782 		ret = handler->error;
783 		dev_err(ov4689->dev, "Failed to init controls(%d)\n", ret);
784 		goto err_free_handler;
785 	}
786 
787 	ret = v4l2_fwnode_device_parse(&client->dev, &props);
788 	if (ret)
789 		goto err_free_handler;
790 
791 	ret = v4l2_ctrl_new_fwnode_properties(handler, &ov4689_ctrl_ops,
792 					      &props);
793 	if (ret)
794 		goto err_free_handler;
795 
796 	ov4689->subdev.ctrl_handler = handler;
797 
798 	return 0;
799 
800 err_free_handler:
801 	v4l2_ctrl_handler_free(handler);
802 
803 	return ret;
804 }
805 
ov4689_check_sensor_id(struct ov4689 * ov4689,struct i2c_client * client)806 static int ov4689_check_sensor_id(struct ov4689 *ov4689,
807 				  struct i2c_client *client)
808 {
809 	struct device *dev = ov4689->dev;
810 	u64 id = 0;
811 	int ret;
812 
813 	ret = cci_read(ov4689->regmap, OV4689_REG_CHIP_ID, &id, NULL);
814 	if (ret) {
815 		dev_err(dev, "Cannot read sensor ID\n");
816 		return ret;
817 	}
818 
819 	if (id != CHIP_ID) {
820 		dev_err(dev, "Unexpected sensor ID %06llx, expected %06x\n",
821 			id, CHIP_ID);
822 		return -ENODEV;
823 	}
824 
825 	dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
826 
827 	return 0;
828 }
829 
ov4689_configure_regulators(struct ov4689 * ov4689)830 static int ov4689_configure_regulators(struct ov4689 *ov4689)
831 {
832 	unsigned int i;
833 
834 	for (i = 0; i < ARRAY_SIZE(ov4689_supply_names); i++)
835 		ov4689->supplies[i].supply = ov4689_supply_names[i];
836 
837 	return devm_regulator_bulk_get(ov4689->dev,
838 				       ARRAY_SIZE(ov4689_supply_names),
839 				       ov4689->supplies);
840 }
841 
ov4689_check_link_frequency(struct v4l2_fwnode_endpoint * ep)842 static u64 ov4689_check_link_frequency(struct v4l2_fwnode_endpoint *ep)
843 {
844 	const u64 *freqs = link_freq_menu_items;
845 	unsigned int i, j;
846 
847 	for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
848 		for (j = 0; j < ep->nr_of_link_frequencies; j++)
849 			if (freqs[i] == ep->link_frequencies[j])
850 				return freqs[i];
851 	}
852 
853 	return 0;
854 }
855 
ov4689_check_hwcfg(struct device * dev)856 static int ov4689_check_hwcfg(struct device *dev)
857 {
858 	struct fwnode_handle *fwnode = dev_fwnode(dev);
859 	struct v4l2_fwnode_endpoint bus_cfg = {
860 		.bus_type = V4L2_MBUS_CSI2_DPHY,
861 	};
862 	struct fwnode_handle *endpoint;
863 	int ret;
864 
865 	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
866 	if (!endpoint)
867 		return -EINVAL;
868 
869 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
870 	fwnode_handle_put(endpoint);
871 	if (ret)
872 		return ret;
873 
874 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV4689_LANES) {
875 		dev_err(dev, "Only a 4-lane CSI2 config is supported");
876 		ret = -EINVAL;
877 		goto out_free_bus_cfg;
878 	}
879 
880 	if (!ov4689_check_link_frequency(&bus_cfg)) {
881 		dev_err(dev, "No supported link frequency found\n");
882 		ret = -EINVAL;
883 	}
884 
885 out_free_bus_cfg:
886 	v4l2_fwnode_endpoint_free(&bus_cfg);
887 
888 	return ret;
889 }
890 
ov4689_probe(struct i2c_client * client)891 static int ov4689_probe(struct i2c_client *client)
892 {
893 	struct device *dev = &client->dev;
894 	struct v4l2_subdev *sd;
895 	struct ov4689 *ov4689;
896 	int ret;
897 
898 	ret = ov4689_check_hwcfg(dev);
899 	if (ret)
900 		return ret;
901 
902 	ov4689 = devm_kzalloc(dev, sizeof(*ov4689), GFP_KERNEL);
903 	if (!ov4689)
904 		return -ENOMEM;
905 
906 	ov4689->dev = dev;
907 
908 	ov4689->cur_mode = &supported_modes[OV4689_MODE_2688_1520];
909 
910 	ov4689->xvclk = devm_clk_get_optional(dev, NULL);
911 	if (IS_ERR(ov4689->xvclk))
912 		return dev_err_probe(dev, PTR_ERR(ov4689->xvclk),
913 				     "Failed to get external clock\n");
914 
915 	if (!ov4689->xvclk) {
916 		dev_dbg(dev,
917 			"No clock provided, using clock-frequency property\n");
918 		device_property_read_u32(dev, "clock-frequency",
919 					 &ov4689->clock_rate);
920 	} else {
921 		ov4689->clock_rate = clk_get_rate(ov4689->xvclk);
922 	}
923 
924 	if (ov4689->clock_rate != OV4689_XVCLK_FREQ) {
925 		dev_err(dev,
926 			"External clock rate mismatch: got %d Hz, expected %d Hz\n",
927 			ov4689->clock_rate, OV4689_XVCLK_FREQ);
928 		return -EINVAL;
929 	}
930 
931 	ov4689->regmap = devm_cci_regmap_init_i2c(client, 16);
932 	if (IS_ERR(ov4689->regmap)) {
933 		ret = PTR_ERR(ov4689->regmap);
934 		dev_err(dev, "failed to initialize CCI: %d\n", ret);
935 		return ret;
936 	}
937 
938 	ov4689->reset_gpio = devm_gpiod_get_optional(dev, "reset",
939 						     GPIOD_OUT_LOW);
940 	if (IS_ERR(ov4689->reset_gpio)) {
941 		dev_err(dev, "Failed to get reset-gpios\n");
942 		return PTR_ERR(ov4689->reset_gpio);
943 	}
944 
945 	ov4689->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_LOW);
946 	if (IS_ERR(ov4689->pwdn_gpio)) {
947 		dev_err(dev, "Failed to get pwdn-gpios\n");
948 		return PTR_ERR(ov4689->pwdn_gpio);
949 	}
950 
951 	ret = ov4689_configure_regulators(ov4689);
952 	if (ret)
953 		return dev_err_probe(dev, ret,
954 				     "Failed to get power regulators\n");
955 
956 	sd = &ov4689->subdev;
957 	v4l2_i2c_subdev_init(sd, client, &ov4689_subdev_ops);
958 	sd->internal_ops = &ov4689_internal_ops;
959 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
960 	ret = ov4689_initialize_controls(ov4689);
961 	if (ret) {
962 		dev_err(dev, "Failed to initialize controls\n");
963 		return ret;
964 	}
965 
966 	ret = ov4689_power_on(dev);
967 	if (ret)
968 		goto err_free_handler;
969 
970 	ret = ov4689_check_sensor_id(ov4689, client);
971 	if (ret)
972 		goto err_power_off;
973 
974 
975 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
976 	ov4689->pad.flags = MEDIA_PAD_FL_SOURCE;
977 	ret = media_entity_pads_init(&sd->entity, 1, &ov4689->pad);
978 	if (ret < 0)
979 		goto err_power_off;
980 
981 	sd->state_lock = ov4689->ctrl_handler.lock;
982 	ret = v4l2_subdev_init_finalize(sd);
983 	if (ret) {
984 		dev_err(dev, "Could not register v4l2 device\n");
985 		goto err_clean_entity;
986 	}
987 
988 	pm_runtime_set_active(dev);
989 	pm_runtime_get_noresume(dev);
990 	pm_runtime_enable(dev);
991 	pm_runtime_set_autosuspend_delay(dev, 1000);
992 	pm_runtime_use_autosuspend(dev);
993 
994 	ret = v4l2_async_register_subdev_sensor(sd);
995 	if (ret) {
996 		dev_err(dev, "v4l2 async register subdev failed\n");
997 		goto err_clean_subdev_pm;
998 	}
999 
1000 	pm_runtime_put_autosuspend(dev);
1001 
1002 	return 0;
1003 
1004 err_clean_subdev_pm:
1005 	pm_runtime_disable(dev);
1006 	pm_runtime_put_noidle(dev);
1007 	v4l2_subdev_cleanup(sd);
1008 err_clean_entity:
1009 	media_entity_cleanup(&sd->entity);
1010 err_power_off:
1011 	ov4689_power_off(dev);
1012 err_free_handler:
1013 	v4l2_ctrl_handler_free(&ov4689->ctrl_handler);
1014 
1015 	return ret;
1016 }
1017 
ov4689_remove(struct i2c_client * client)1018 static void ov4689_remove(struct i2c_client *client)
1019 {
1020 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1021 	struct ov4689 *ov4689 = to_ov4689(sd);
1022 
1023 	v4l2_async_unregister_subdev(sd);
1024 	media_entity_cleanup(&sd->entity);
1025 	v4l2_subdev_cleanup(sd);
1026 	v4l2_ctrl_handler_free(&ov4689->ctrl_handler);
1027 
1028 	pm_runtime_disable(&client->dev);
1029 	if (!pm_runtime_status_suspended(&client->dev))
1030 		ov4689_power_off(&client->dev);
1031 	pm_runtime_set_suspended(&client->dev);
1032 }
1033 
1034 static const struct of_device_id ov4689_of_match[] = {
1035 	{ .compatible = "ovti,ov4689" },
1036 	{},
1037 };
1038 MODULE_DEVICE_TABLE(of, ov4689_of_match);
1039 
1040 static struct i2c_driver ov4689_i2c_driver = {
1041 	.driver = {
1042 		.name = "ov4689",
1043 		.pm = &ov4689_pm_ops,
1044 		.of_match_table = ov4689_of_match,
1045 	},
1046 	.probe = ov4689_probe,
1047 	.remove	= ov4689_remove,
1048 };
1049 
1050 module_i2c_driver(ov4689_i2c_driver);
1051 
1052 MODULE_DESCRIPTION("OmniVision ov4689 sensor driver");
1053 MODULE_LICENSE("GPL");
1054