1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Omnivision OV2680 CMOS Image Sensor driver
4  *
5  * Copyright (C) 2018 Linaro Ltd
6  *
7  * Based on OV5640 Sensor Driver
8  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
9  * Copyright (C) 2014-2017 Mentor Graphics Inc.
10  *
11  */
12 
13 #include <asm/unaligned.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/regulator/consumer.h>
23 
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-subdev.h>
27 
28 #define OV2680_XVCLK_VALUE	24000000
29 
30 #define OV2680_CHIP_ID		0x2680
31 
32 #define OV2680_REG_STREAM_CTRL		0x0100
33 #define OV2680_REG_SOFT_RESET		0x0103
34 
35 #define OV2680_REG_CHIP_ID_HIGH		0x300a
36 #define OV2680_REG_CHIP_ID_LOW		0x300b
37 
38 #define OV2680_REG_R_MANUAL		0x3503
39 #define OV2680_REG_GAIN_PK		0x350a
40 #define OV2680_REG_EXPOSURE_PK_HIGH	0x3500
41 #define OV2680_REG_TIMING_HTS		0x380c
42 #define OV2680_REG_TIMING_VTS		0x380e
43 #define OV2680_REG_FORMAT1		0x3820
44 #define OV2680_REG_FORMAT2		0x3821
45 
46 #define OV2680_REG_ISP_CTRL00		0x5080
47 
48 #define OV2680_FRAME_RATE		30
49 
50 #define OV2680_REG_VALUE_8BIT		1
51 #define OV2680_REG_VALUE_16BIT		2
52 #define OV2680_REG_VALUE_24BIT		3
53 
54 #define OV2680_WIDTH_MAX		1600
55 #define OV2680_HEIGHT_MAX		1200
56 
57 enum ov2680_mode_id {
58 	OV2680_MODE_QUXGA_800_600,
59 	OV2680_MODE_720P_1280_720,
60 	OV2680_MODE_UXGA_1600_1200,
61 	OV2680_MODE_MAX,
62 };
63 
64 struct reg_value {
65 	u16 reg_addr;
66 	u8 val;
67 };
68 
69 static const char * const ov2680_supply_name[] = {
70 	"DOVDD",
71 	"DVDD",
72 	"AVDD",
73 };
74 
75 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
76 
77 struct ov2680_mode_info {
78 	const char *name;
79 	enum ov2680_mode_id id;
80 	u32 width;
81 	u32 height;
82 	const struct reg_value *reg_data;
83 	u32 reg_data_size;
84 };
85 
86 struct ov2680_ctrls {
87 	struct v4l2_ctrl_handler handler;
88 	struct {
89 		struct v4l2_ctrl *auto_exp;
90 		struct v4l2_ctrl *exposure;
91 	};
92 	struct {
93 		struct v4l2_ctrl *auto_gain;
94 		struct v4l2_ctrl *gain;
95 	};
96 
97 	struct v4l2_ctrl *hflip;
98 	struct v4l2_ctrl *vflip;
99 	struct v4l2_ctrl *test_pattern;
100 };
101 
102 struct ov2680_dev {
103 	struct i2c_client		*i2c_client;
104 	struct v4l2_subdev		sd;
105 
106 	struct media_pad		pad;
107 	struct clk			*xvclk;
108 	u32				xvclk_freq;
109 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
110 
111 	struct gpio_desc		*reset_gpio;
112 	struct mutex			lock; /* protect members */
113 
114 	bool				mode_pending_changes;
115 	bool				is_enabled;
116 	bool				is_streaming;
117 
118 	struct ov2680_ctrls		ctrls;
119 	struct v4l2_mbus_framefmt	fmt;
120 	struct v4l2_fract		frame_interval;
121 
122 	const struct ov2680_mode_info	*current_mode;
123 };
124 
125 static const char * const test_pattern_menu[] = {
126 	"Disabled",
127 	"Color Bars",
128 	"Random Data",
129 	"Square",
130 	"Black Image",
131 };
132 
133 static const int ov2680_hv_flip_bayer_order[] = {
134 	MEDIA_BUS_FMT_SBGGR10_1X10,
135 	MEDIA_BUS_FMT_SGRBG10_1X10,
136 	MEDIA_BUS_FMT_SGBRG10_1X10,
137 	MEDIA_BUS_FMT_SRGGB10_1X10,
138 };
139 
140 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
141 	{0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
142 	{0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
143 	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
144 	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
145 	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
146 };
147 
148 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
149 	{0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
150 	{0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
151 	{0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
152 	{0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
153 };
154 
155 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
156 	{0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
157 	{0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
158 	{0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
159 	{0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
160 	{0x4008, 0x00}, {0x4837, 0x18}
161 };
162 
163 static const struct ov2680_mode_info ov2680_mode_init_data = {
164 	"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
165 	ov2680_setting_30fps_QUXGA_800_600,
166 	ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
167 };
168 
169 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
170 	{"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
171 	 800, 600, ov2680_setting_30fps_QUXGA_800_600,
172 	 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
173 	{"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
174 	 1280, 720, ov2680_setting_30fps_720P_1280_720,
175 	 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
176 	{"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
177 	 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
178 	 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
179 };
180 
to_ov2680_dev(struct v4l2_subdev * sd)181 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
182 {
183 	return container_of(sd, struct ov2680_dev, sd);
184 }
185 
ov2680_to_dev(struct ov2680_dev * sensor)186 static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
187 {
188 	return &sensor->i2c_client->dev;
189 }
190 
ctrl_to_sd(struct v4l2_ctrl * ctrl)191 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
192 {
193 	return &container_of(ctrl->handler, struct ov2680_dev,
194 			     ctrls.handler)->sd;
195 }
196 
__ov2680_write_reg(struct ov2680_dev * sensor,u16 reg,unsigned int len,u32 val)197 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
198 			      unsigned int len, u32 val)
199 {
200 	struct i2c_client *client = sensor->i2c_client;
201 	u8 buf[6];
202 	int ret;
203 
204 	if (len > 4)
205 		return -EINVAL;
206 
207 	put_unaligned_be16(reg, buf);
208 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
209 	ret = i2c_master_send(client, buf, len + 2);
210 	if (ret != len + 2) {
211 		dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
212 		return -EIO;
213 	}
214 
215 	return 0;
216 }
217 
218 #define ov2680_write_reg(s, r, v) \
219 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
220 
221 #define ov2680_write_reg16(s, r, v) \
222 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
223 
224 #define ov2680_write_reg24(s, r, v) \
225 	__ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
226 
__ov2680_read_reg(struct ov2680_dev * sensor,u16 reg,unsigned int len,u32 * val)227 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
228 			     unsigned int len, u32 *val)
229 {
230 	struct i2c_client *client = sensor->i2c_client;
231 	struct i2c_msg msgs[2];
232 	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
233 	u8 data_buf[4] = { 0, };
234 	int ret;
235 
236 	if (len > 4)
237 		return -EINVAL;
238 
239 	msgs[0].addr = client->addr;
240 	msgs[0].flags = 0;
241 	msgs[0].len = ARRAY_SIZE(addr_buf);
242 	msgs[0].buf = addr_buf;
243 
244 	msgs[1].addr = client->addr;
245 	msgs[1].flags = I2C_M_RD;
246 	msgs[1].len = len;
247 	msgs[1].buf = &data_buf[4 - len];
248 
249 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
250 	if (ret != ARRAY_SIZE(msgs)) {
251 		dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
252 		return -EIO;
253 	}
254 
255 	*val = get_unaligned_be32(data_buf);
256 
257 	return 0;
258 }
259 
260 #define ov2680_read_reg(s, r, v) \
261 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
262 
263 #define ov2680_read_reg16(s, r, v) \
264 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
265 
266 #define ov2680_read_reg24(s, r, v) \
267 	__ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
268 
ov2680_mod_reg(struct ov2680_dev * sensor,u16 reg,u8 mask,u8 val)269 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
270 {
271 	u32 readval;
272 	int ret;
273 
274 	ret = ov2680_read_reg(sensor, reg, &readval);
275 	if (ret < 0)
276 		return ret;
277 
278 	readval &= ~mask;
279 	val &= mask;
280 	val |= readval;
281 
282 	return ov2680_write_reg(sensor, reg, val);
283 }
284 
ov2680_load_regs(struct ov2680_dev * sensor,const struct ov2680_mode_info * mode)285 static int ov2680_load_regs(struct ov2680_dev *sensor,
286 			    const struct ov2680_mode_info *mode)
287 {
288 	const struct reg_value *regs = mode->reg_data;
289 	unsigned int i;
290 	int ret = 0;
291 	u16 reg_addr;
292 	u8 val;
293 
294 	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
295 		reg_addr = regs->reg_addr;
296 		val = regs->val;
297 
298 		ret = ov2680_write_reg(sensor, reg_addr, val);
299 		if (ret)
300 			break;
301 	}
302 
303 	return ret;
304 }
305 
ov2680_power_up(struct ov2680_dev * sensor)306 static void ov2680_power_up(struct ov2680_dev *sensor)
307 {
308 	if (!sensor->reset_gpio)
309 		return;
310 
311 	gpiod_set_value(sensor->reset_gpio, 0);
312 	usleep_range(5000, 10000);
313 }
314 
ov2680_power_down(struct ov2680_dev * sensor)315 static void ov2680_power_down(struct ov2680_dev *sensor)
316 {
317 	if (!sensor->reset_gpio)
318 		return;
319 
320 	gpiod_set_value(sensor->reset_gpio, 1);
321 	usleep_range(5000, 10000);
322 }
323 
ov2680_bayer_order(struct ov2680_dev * sensor)324 static int ov2680_bayer_order(struct ov2680_dev *sensor)
325 {
326 	u32 format1;
327 	u32 format2;
328 	u32 hv_flip;
329 	int ret;
330 
331 	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1);
332 	if (ret < 0)
333 		return ret;
334 
335 	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2);
336 	if (ret < 0)
337 		return ret;
338 
339 	hv_flip = (format2 & BIT(2)  << 1) | (format1 & BIT(2));
340 
341 	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
342 
343 	return 0;
344 }
345 
ov2680_vflip_enable(struct ov2680_dev * sensor)346 static int ov2680_vflip_enable(struct ov2680_dev *sensor)
347 {
348 	int ret;
349 
350 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
351 	if (ret < 0)
352 		return ret;
353 
354 	return ov2680_bayer_order(sensor);
355 }
356 
ov2680_vflip_disable(struct ov2680_dev * sensor)357 static int ov2680_vflip_disable(struct ov2680_dev *sensor)
358 {
359 	int ret;
360 
361 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
362 	if (ret < 0)
363 		return ret;
364 
365 	return ov2680_bayer_order(sensor);
366 }
367 
ov2680_hflip_enable(struct ov2680_dev * sensor)368 static int ov2680_hflip_enable(struct ov2680_dev *sensor)
369 {
370 	int ret;
371 
372 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
373 	if (ret < 0)
374 		return ret;
375 
376 	return ov2680_bayer_order(sensor);
377 }
378 
ov2680_hflip_disable(struct ov2680_dev * sensor)379 static int ov2680_hflip_disable(struct ov2680_dev *sensor)
380 {
381 	int ret;
382 
383 	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
384 	if (ret < 0)
385 		return ret;
386 
387 	return ov2680_bayer_order(sensor);
388 }
389 
ov2680_test_pattern_set(struct ov2680_dev * sensor,int value)390 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
391 {
392 	int ret;
393 
394 	if (!value)
395 		return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
396 
397 	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
398 	if (ret < 0)
399 		return ret;
400 
401 	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
402 	if (ret < 0)
403 		return ret;
404 
405 	return 0;
406 }
407 
ov2680_gain_set(struct ov2680_dev * sensor,bool auto_gain)408 static int ov2680_gain_set(struct ov2680_dev *sensor, bool auto_gain)
409 {
410 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
411 	u32 gain;
412 	int ret;
413 
414 	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(1),
415 			     auto_gain ? 0 : BIT(1));
416 	if (ret < 0)
417 		return ret;
418 
419 	if (auto_gain || !ctrls->gain->is_new)
420 		return 0;
421 
422 	gain = ctrls->gain->val;
423 
424 	ret = ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
425 
426 	return 0;
427 }
428 
ov2680_gain_get(struct ov2680_dev * sensor)429 static int ov2680_gain_get(struct ov2680_dev *sensor)
430 {
431 	u32 gain;
432 	int ret;
433 
434 	ret = ov2680_read_reg16(sensor, OV2680_REG_GAIN_PK, &gain);
435 	if (ret)
436 		return ret;
437 
438 	return gain;
439 }
440 
ov2680_exposure_set(struct ov2680_dev * sensor,bool auto_exp)441 static int ov2680_exposure_set(struct ov2680_dev *sensor, bool auto_exp)
442 {
443 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
444 	u32 exp;
445 	int ret;
446 
447 	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(0),
448 			     auto_exp ? 0 : BIT(0));
449 	if (ret < 0)
450 		return ret;
451 
452 	if (auto_exp || !ctrls->exposure->is_new)
453 		return 0;
454 
455 	exp = (u32)ctrls->exposure->val;
456 	exp <<= 4;
457 
458 	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp);
459 }
460 
ov2680_exposure_get(struct ov2680_dev * sensor)461 static int ov2680_exposure_get(struct ov2680_dev *sensor)
462 {
463 	int ret;
464 	u32 exp;
465 
466 	ret = ov2680_read_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, &exp);
467 	if (ret)
468 		return ret;
469 
470 	return exp >> 4;
471 }
472 
ov2680_stream_enable(struct ov2680_dev * sensor)473 static int ov2680_stream_enable(struct ov2680_dev *sensor)
474 {
475 	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
476 }
477 
ov2680_stream_disable(struct ov2680_dev * sensor)478 static int ov2680_stream_disable(struct ov2680_dev *sensor)
479 {
480 	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
481 }
482 
ov2680_mode_set(struct ov2680_dev * sensor)483 static int ov2680_mode_set(struct ov2680_dev *sensor)
484 {
485 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
486 	int ret;
487 
488 	ret = ov2680_gain_set(sensor, false);
489 	if (ret < 0)
490 		return ret;
491 
492 	ret = ov2680_exposure_set(sensor, false);
493 	if (ret < 0)
494 		return ret;
495 
496 	ret = ov2680_load_regs(sensor, sensor->current_mode);
497 	if (ret < 0)
498 		return ret;
499 
500 	if (ctrls->auto_gain->val) {
501 		ret = ov2680_gain_set(sensor, true);
502 		if (ret < 0)
503 			return ret;
504 	}
505 
506 	if (ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO) {
507 		ret = ov2680_exposure_set(sensor, true);
508 		if (ret < 0)
509 			return ret;
510 	}
511 
512 	sensor->mode_pending_changes = false;
513 
514 	return 0;
515 }
516 
ov2680_mode_restore(struct ov2680_dev * sensor)517 static int ov2680_mode_restore(struct ov2680_dev *sensor)
518 {
519 	int ret;
520 
521 	ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
522 	if (ret < 0)
523 		return ret;
524 
525 	return ov2680_mode_set(sensor);
526 }
527 
ov2680_power_off(struct ov2680_dev * sensor)528 static int ov2680_power_off(struct ov2680_dev *sensor)
529 {
530 	if (!sensor->is_enabled)
531 		return 0;
532 
533 	clk_disable_unprepare(sensor->xvclk);
534 	ov2680_power_down(sensor);
535 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
536 	sensor->is_enabled = false;
537 
538 	return 0;
539 }
540 
ov2680_power_on(struct ov2680_dev * sensor)541 static int ov2680_power_on(struct ov2680_dev *sensor)
542 {
543 	struct device *dev = ov2680_to_dev(sensor);
544 	int ret;
545 
546 	if (sensor->is_enabled)
547 		return 0;
548 
549 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
550 	if (ret < 0) {
551 		dev_err(dev, "failed to enable regulators: %d\n", ret);
552 		return ret;
553 	}
554 
555 	if (!sensor->reset_gpio) {
556 		ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
557 		if (ret != 0) {
558 			dev_err(dev, "sensor soft reset failed\n");
559 			return ret;
560 		}
561 		usleep_range(1000, 2000);
562 	} else {
563 		ov2680_power_down(sensor);
564 		ov2680_power_up(sensor);
565 	}
566 
567 	ret = clk_prepare_enable(sensor->xvclk);
568 	if (ret < 0)
569 		return ret;
570 
571 	ret = ov2680_mode_restore(sensor);
572 	if (ret < 0)
573 		goto disable;
574 
575 	sensor->is_enabled = true;
576 
577 	/* Set clock lane into LP-11 state */
578 	ov2680_stream_enable(sensor);
579 	usleep_range(1000, 2000);
580 	ov2680_stream_disable(sensor);
581 
582 	return 0;
583 
584 disable:
585 	dev_err(dev, "failed to enable sensor: %d\n", ret);
586 	ov2680_power_off(sensor);
587 
588 	return ret;
589 }
590 
ov2680_s_power(struct v4l2_subdev * sd,int on)591 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
592 {
593 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
594 	int ret = 0;
595 
596 	mutex_lock(&sensor->lock);
597 
598 	if (on)
599 		ret = ov2680_power_on(sensor);
600 	else
601 		ret = ov2680_power_off(sensor);
602 
603 	mutex_unlock(&sensor->lock);
604 
605 	if (on && ret == 0) {
606 		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
607 		if (ret < 0)
608 			return ret;
609 	}
610 
611 	return ret;
612 }
613 
ov2680_s_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)614 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
615 				     struct v4l2_subdev_frame_interval *fi)
616 {
617 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
618 
619 	mutex_lock(&sensor->lock);
620 	fi->interval = sensor->frame_interval;
621 	mutex_unlock(&sensor->lock);
622 
623 	return 0;
624 }
625 
ov2680_s_stream(struct v4l2_subdev * sd,int enable)626 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
627 {
628 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
629 	int ret = 0;
630 
631 	mutex_lock(&sensor->lock);
632 
633 	if (sensor->is_streaming == !!enable)
634 		goto unlock;
635 
636 	if (enable && sensor->mode_pending_changes) {
637 		ret = ov2680_mode_set(sensor);
638 		if (ret < 0)
639 			goto unlock;
640 	}
641 
642 	if (enable)
643 		ret = ov2680_stream_enable(sensor);
644 	else
645 		ret = ov2680_stream_disable(sensor);
646 
647 	sensor->is_streaming = !!enable;
648 
649 unlock:
650 	mutex_unlock(&sensor->lock);
651 
652 	return ret;
653 }
654 
ov2680_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)655 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
656 				 struct v4l2_subdev_pad_config *cfg,
657 				 struct v4l2_subdev_mbus_code_enum *code)
658 {
659 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
660 
661 	if (code->pad != 0 || code->index != 0)
662 		return -EINVAL;
663 
664 	code->code = sensor->fmt.code;
665 
666 	return 0;
667 }
668 
ov2680_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)669 static int ov2680_get_fmt(struct v4l2_subdev *sd,
670 			  struct v4l2_subdev_pad_config *cfg,
671 			  struct v4l2_subdev_format *format)
672 {
673 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
674 	struct v4l2_mbus_framefmt *fmt = NULL;
675 	int ret = 0;
676 
677 	if (format->pad != 0)
678 		return -EINVAL;
679 
680 	mutex_lock(&sensor->lock);
681 
682 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
683 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
684 		fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, format->pad);
685 #else
686 		ret = -ENOTTY;
687 #endif
688 	} else {
689 		fmt = &sensor->fmt;
690 	}
691 
692 	if (fmt)
693 		format->format = *fmt;
694 
695 	mutex_unlock(&sensor->lock);
696 
697 	return ret;
698 }
699 
ov2680_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)700 static int ov2680_set_fmt(struct v4l2_subdev *sd,
701 			  struct v4l2_subdev_pad_config *cfg,
702 			  struct v4l2_subdev_format *format)
703 {
704 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
705 	struct v4l2_mbus_framefmt *fmt = &format->format;
706 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
707 	struct v4l2_mbus_framefmt *try_fmt;
708 #endif
709 	const struct ov2680_mode_info *mode;
710 	int ret = 0;
711 
712 	if (format->pad != 0)
713 		return -EINVAL;
714 
715 	mutex_lock(&sensor->lock);
716 
717 	if (sensor->is_streaming) {
718 		ret = -EBUSY;
719 		goto unlock;
720 	}
721 
722 	mode = v4l2_find_nearest_size(ov2680_mode_data,
723 				      ARRAY_SIZE(ov2680_mode_data), width,
724 				      height, fmt->width, fmt->height);
725 	if (!mode) {
726 		ret = -EINVAL;
727 		goto unlock;
728 	}
729 
730 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
731 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
732 		try_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
733 		format->format = *try_fmt;
734 #else
735 		ret = -ENOTTY;
736 #endif
737 
738 		goto unlock;
739 	}
740 
741 	fmt->width = mode->width;
742 	fmt->height = mode->height;
743 	fmt->code = sensor->fmt.code;
744 	fmt->colorspace = sensor->fmt.colorspace;
745 
746 	sensor->current_mode = mode;
747 	sensor->fmt = format->format;
748 	sensor->mode_pending_changes = true;
749 
750 unlock:
751 	mutex_unlock(&sensor->lock);
752 
753 	return ret;
754 }
755 
ov2680_init_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg)756 static int ov2680_init_cfg(struct v4l2_subdev *sd,
757 			   struct v4l2_subdev_pad_config *cfg)
758 {
759 	struct v4l2_subdev_format fmt = {
760 		.which = cfg ? V4L2_SUBDEV_FORMAT_TRY
761 				: V4L2_SUBDEV_FORMAT_ACTIVE,
762 		.format = {
763 			.width = 800,
764 			.height = 600,
765 		}
766 	};
767 
768 	return ov2680_set_fmt(sd, cfg, &fmt);
769 }
770 
ov2680_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)771 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
772 				  struct v4l2_subdev_pad_config *cfg,
773 				  struct v4l2_subdev_frame_size_enum *fse)
774 {
775 	int index = fse->index;
776 
777 	if (index >= OV2680_MODE_MAX || index < 0)
778 		return -EINVAL;
779 
780 	fse->min_width = ov2680_mode_data[index].width;
781 	fse->min_height = ov2680_mode_data[index].height;
782 	fse->max_width = ov2680_mode_data[index].width;
783 	fse->max_height = ov2680_mode_data[index].height;
784 
785 	return 0;
786 }
787 
ov2680_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)788 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
789 			      struct v4l2_subdev_pad_config *cfg,
790 			      struct v4l2_subdev_frame_interval_enum *fie)
791 {
792 	struct v4l2_fract tpf;
793 
794 	if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
795 	    fie->height > OV2680_HEIGHT_MAX ||
796 	    fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
797 		return -EINVAL;
798 
799 	tpf.denominator = OV2680_FRAME_RATE;
800 	tpf.numerator = 1;
801 
802 	fie->interval = tpf;
803 
804 	return 0;
805 }
806 
ov2680_g_volatile_ctrl(struct v4l2_ctrl * ctrl)807 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
808 {
809 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
810 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
811 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
812 	int val;
813 
814 	if (!sensor->is_enabled)
815 		return 0;
816 
817 	switch (ctrl->id) {
818 	case V4L2_CID_GAIN:
819 		val = ov2680_gain_get(sensor);
820 		if (val < 0)
821 			return val;
822 		ctrls->gain->val = val;
823 		break;
824 	case V4L2_CID_EXPOSURE:
825 		val = ov2680_exposure_get(sensor);
826 		if (val < 0)
827 			return val;
828 		ctrls->exposure->val = val;
829 		break;
830 	}
831 
832 	return 0;
833 }
834 
ov2680_s_ctrl(struct v4l2_ctrl * ctrl)835 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
836 {
837 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
838 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
839 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
840 
841 	if (!sensor->is_enabled)
842 		return 0;
843 
844 	switch (ctrl->id) {
845 	case V4L2_CID_AUTOGAIN:
846 		return ov2680_gain_set(sensor, !!ctrl->val);
847 	case V4L2_CID_GAIN:
848 		return ov2680_gain_set(sensor, !!ctrls->auto_gain->val);
849 	case V4L2_CID_EXPOSURE_AUTO:
850 		return ov2680_exposure_set(sensor, !!ctrl->val);
851 	case V4L2_CID_EXPOSURE:
852 		return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val);
853 	case V4L2_CID_VFLIP:
854 		if (sensor->is_streaming)
855 			return -EBUSY;
856 		if (ctrl->val)
857 			return ov2680_vflip_enable(sensor);
858 		else
859 			return ov2680_vflip_disable(sensor);
860 	case V4L2_CID_HFLIP:
861 		if (sensor->is_streaming)
862 			return -EBUSY;
863 		if (ctrl->val)
864 			return ov2680_hflip_enable(sensor);
865 		else
866 			return ov2680_hflip_disable(sensor);
867 	case V4L2_CID_TEST_PATTERN:
868 		return ov2680_test_pattern_set(sensor, ctrl->val);
869 	default:
870 		break;
871 	}
872 
873 	return -EINVAL;
874 }
875 
876 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
877 	.g_volatile_ctrl = ov2680_g_volatile_ctrl,
878 	.s_ctrl = ov2680_s_ctrl,
879 };
880 
881 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
882 	.s_power = ov2680_s_power,
883 };
884 
885 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
886 	.g_frame_interval	= ov2680_s_g_frame_interval,
887 	.s_frame_interval	= ov2680_s_g_frame_interval,
888 	.s_stream		= ov2680_s_stream,
889 };
890 
891 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
892 	.init_cfg		= ov2680_init_cfg,
893 	.enum_mbus_code		= ov2680_enum_mbus_code,
894 	.get_fmt		= ov2680_get_fmt,
895 	.set_fmt		= ov2680_set_fmt,
896 	.enum_frame_size	= ov2680_enum_frame_size,
897 	.enum_frame_interval	= ov2680_enum_frame_interval,
898 };
899 
900 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
901 	.core	= &ov2680_core_ops,
902 	.video	= &ov2680_video_ops,
903 	.pad	= &ov2680_pad_ops,
904 };
905 
ov2680_mode_init(struct ov2680_dev * sensor)906 static int ov2680_mode_init(struct ov2680_dev *sensor)
907 {
908 	const struct ov2680_mode_info *init_mode;
909 
910 	/* set initial mode */
911 	sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
912 	sensor->fmt.width = 800;
913 	sensor->fmt.height = 600;
914 	sensor->fmt.field = V4L2_FIELD_NONE;
915 	sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
916 
917 	sensor->frame_interval.denominator = OV2680_FRAME_RATE;
918 	sensor->frame_interval.numerator = 1;
919 
920 	init_mode = &ov2680_mode_init_data;
921 
922 	sensor->current_mode = init_mode;
923 
924 	sensor->mode_pending_changes = true;
925 
926 	return 0;
927 }
928 
ov2680_v4l2_init(struct ov2680_dev * sensor)929 static int ov2680_v4l2_init(struct ov2680_dev *sensor)
930 {
931 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
932 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
933 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
934 	int ret = 0;
935 
936 	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
937 			     &ov2680_subdev_ops);
938 
939 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
940 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
941 #endif
942 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
943 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
944 
945 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
946 	if (ret < 0)
947 		return ret;
948 
949 	v4l2_ctrl_handler_init(hdl, 7);
950 
951 	hdl->lock = &sensor->lock;
952 
953 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
954 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
955 
956 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
957 					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
958 					ARRAY_SIZE(test_pattern_menu) - 1,
959 					0, 0, test_pattern_menu);
960 
961 	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
962 						 V4L2_CID_EXPOSURE_AUTO,
963 						 V4L2_EXPOSURE_MANUAL, 0,
964 						 V4L2_EXPOSURE_AUTO);
965 
966 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
967 					    0, 32767, 1, 0);
968 
969 	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
970 					     0, 1, 1, 1);
971 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
972 
973 	if (hdl->error) {
974 		ret = hdl->error;
975 		goto cleanup_entity;
976 	}
977 
978 	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
979 	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
980 
981 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
982 	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
983 
984 	sensor->sd.ctrl_handler = hdl;
985 
986 	ret = v4l2_async_register_subdev(&sensor->sd);
987 	if (ret < 0)
988 		goto cleanup_entity;
989 
990 	return 0;
991 
992 cleanup_entity:
993 	media_entity_cleanup(&sensor->sd.entity);
994 	v4l2_ctrl_handler_free(hdl);
995 
996 	return ret;
997 }
998 
ov2680_get_regulators(struct ov2680_dev * sensor)999 static int ov2680_get_regulators(struct ov2680_dev *sensor)
1000 {
1001 	int i;
1002 
1003 	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
1004 		sensor->supplies[i].supply = ov2680_supply_name[i];
1005 
1006 	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
1007 				       OV2680_NUM_SUPPLIES,
1008 				       sensor->supplies);
1009 }
1010 
ov2680_check_id(struct ov2680_dev * sensor)1011 static int ov2680_check_id(struct ov2680_dev *sensor)
1012 {
1013 	struct device *dev = ov2680_to_dev(sensor);
1014 	u32 chip_id;
1015 	int ret;
1016 
1017 	ov2680_power_on(sensor);
1018 
1019 	ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
1020 	if (ret < 0) {
1021 		dev_err(dev, "failed to read chip id high\n");
1022 		return -ENODEV;
1023 	}
1024 
1025 	if (chip_id != OV2680_CHIP_ID) {
1026 		dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
1027 			chip_id, OV2680_CHIP_ID);
1028 		return -ENODEV;
1029 	}
1030 
1031 	return 0;
1032 }
1033 
ov2860_parse_dt(struct ov2680_dev * sensor)1034 static int ov2860_parse_dt(struct ov2680_dev *sensor)
1035 {
1036 	struct device *dev = ov2680_to_dev(sensor);
1037 	int ret;
1038 
1039 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1040 						     GPIOD_OUT_HIGH);
1041 	ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
1042 	if (ret < 0) {
1043 		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
1044 		return ret;
1045 	}
1046 
1047 	sensor->xvclk = devm_clk_get(dev, "xvclk");
1048 	if (IS_ERR(sensor->xvclk)) {
1049 		dev_err(dev, "xvclk clock missing or invalid\n");
1050 		return PTR_ERR(sensor->xvclk);
1051 	}
1052 
1053 	sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
1054 	if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
1055 		dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
1056 			sensor->xvclk_freq, OV2680_XVCLK_VALUE);
1057 		return -EINVAL;
1058 	}
1059 
1060 	return 0;
1061 }
1062 
ov2680_probe(struct i2c_client * client)1063 static int ov2680_probe(struct i2c_client *client)
1064 {
1065 	struct device *dev = &client->dev;
1066 	struct ov2680_dev *sensor;
1067 	int ret;
1068 
1069 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1070 	if (!sensor)
1071 		return -ENOMEM;
1072 
1073 	sensor->i2c_client = client;
1074 
1075 	ret = ov2860_parse_dt(sensor);
1076 	if (ret < 0)
1077 		return -EINVAL;
1078 
1079 	ret = ov2680_mode_init(sensor);
1080 	if (ret < 0)
1081 		return ret;
1082 
1083 	ret = ov2680_get_regulators(sensor);
1084 	if (ret < 0) {
1085 		dev_err(dev, "failed to get regulators\n");
1086 		return ret;
1087 	}
1088 
1089 	mutex_init(&sensor->lock);
1090 
1091 	ret = ov2680_v4l2_init(sensor);
1092 	if (ret < 0)
1093 		goto lock_destroy;
1094 
1095 	ret = ov2680_check_id(sensor);
1096 	if (ret < 0)
1097 		goto error_cleanup;
1098 
1099 	dev_info(dev, "ov2680 init correctly\n");
1100 
1101 	return 0;
1102 
1103 error_cleanup:
1104 	dev_err(dev, "ov2680 init fail: %d\n", ret);
1105 
1106 	media_entity_cleanup(&sensor->sd.entity);
1107 	v4l2_async_unregister_subdev(&sensor->sd);
1108 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1109 
1110 lock_destroy:
1111 	mutex_destroy(&sensor->lock);
1112 
1113 	return ret;
1114 }
1115 
ov2680_remove(struct i2c_client * client)1116 static int ov2680_remove(struct i2c_client *client)
1117 {
1118 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1119 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1120 
1121 	v4l2_async_unregister_subdev(&sensor->sd);
1122 	mutex_destroy(&sensor->lock);
1123 	media_entity_cleanup(&sensor->sd.entity);
1124 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1125 
1126 	return 0;
1127 }
1128 
ov2680_suspend(struct device * dev)1129 static int __maybe_unused ov2680_suspend(struct device *dev)
1130 {
1131 	struct i2c_client *client = to_i2c_client(dev);
1132 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1133 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1134 
1135 	if (sensor->is_streaming)
1136 		ov2680_stream_disable(sensor);
1137 
1138 	return 0;
1139 }
1140 
ov2680_resume(struct device * dev)1141 static int __maybe_unused ov2680_resume(struct device *dev)
1142 {
1143 	struct i2c_client *client = to_i2c_client(dev);
1144 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1145 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1146 	int ret;
1147 
1148 	if (sensor->is_streaming) {
1149 		ret = ov2680_stream_enable(sensor);
1150 		if (ret < 0)
1151 			goto stream_disable;
1152 	}
1153 
1154 	return 0;
1155 
1156 stream_disable:
1157 	ov2680_stream_disable(sensor);
1158 	sensor->is_streaming = false;
1159 
1160 	return ret;
1161 }
1162 
1163 static const struct dev_pm_ops ov2680_pm_ops = {
1164 	SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
1165 };
1166 
1167 static const struct of_device_id ov2680_dt_ids[] = {
1168 	{ .compatible = "ovti,ov2680" },
1169 	{ /* sentinel */ },
1170 };
1171 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1172 
1173 static struct i2c_driver ov2680_i2c_driver = {
1174 	.driver = {
1175 		.name  = "ov2680",
1176 		.pm = &ov2680_pm_ops,
1177 		.of_match_table	= of_match_ptr(ov2680_dt_ids),
1178 	},
1179 	.probe_new	= ov2680_probe,
1180 	.remove		= ov2680_remove,
1181 };
1182 module_i2c_driver(ov2680_i2c_driver);
1183 
1184 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1185 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1186 MODULE_LICENSE("GPL v2");
1187