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