xref: /linux/drivers/media/i2c/ov5647.c (revision d060296cc0300ae8ed08004ebd3994bf325fa257)
1 /*
2  * A V4L2 driver for OmniVision OV5647 cameras.
3  *
4  * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
5  * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
6  *
7  * Based on Omnivision OV7670 Camera Driver
8  * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
9  *
10  * Copyright (C) 2016, Synopsys, Inc.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation version 2.
15  *
16  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
17  * kind, whether express or implied; without even the implied warranty
18  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  */
21 
22 #include <linux/clk.h>
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/io.h>
27 #include <linux/module.h>
28 #include <linux/of_graph.h>
29 #include <linux/slab.h>
30 #include <linux/videodev2.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-fwnode.h>
33 #include <media/v4l2-image-sizes.h>
34 #include <media/v4l2-mediabus.h>
35 
36 #define SENSOR_NAME "ov5647"
37 
38 #define OV5647_SW_RESET		0x0103
39 #define OV5647_REG_CHIPID_H	0x300A
40 #define OV5647_REG_CHIPID_L	0x300B
41 
42 #define REG_TERM 0xfffe
43 #define VAL_TERM 0xfe
44 #define REG_DLY  0xffff
45 
46 #define OV5647_ROW_START		0x01
47 #define OV5647_ROW_START_MIN		0
48 #define OV5647_ROW_START_MAX		2004
49 #define OV5647_ROW_START_DEF		54
50 
51 #define OV5647_COLUMN_START		0x02
52 #define OV5647_COLUMN_START_MIN		0
53 #define OV5647_COLUMN_START_MAX		2750
54 #define OV5647_COLUMN_START_DEF		16
55 
56 #define OV5647_WINDOW_HEIGHT		0x03
57 #define OV5647_WINDOW_HEIGHT_MIN	2
58 #define OV5647_WINDOW_HEIGHT_MAX	2006
59 #define OV5647_WINDOW_HEIGHT_DEF	1944
60 
61 #define OV5647_WINDOW_WIDTH		0x04
62 #define OV5647_WINDOW_WIDTH_MIN		2
63 #define OV5647_WINDOW_WIDTH_MAX		2752
64 #define OV5647_WINDOW_WIDTH_DEF		2592
65 
66 struct regval_list {
67 	u16 addr;
68 	u8 data;
69 };
70 
71 struct ov5647 {
72 	struct v4l2_subdev		sd;
73 	struct media_pad		pad;
74 	struct mutex			lock;
75 	struct v4l2_mbus_framefmt	format;
76 	unsigned int			width;
77 	unsigned int			height;
78 	int				power_count;
79 	struct clk			*xclk;
80 };
81 
82 static inline struct ov5647 *to_state(struct v4l2_subdev *sd)
83 {
84 	return container_of(sd, struct ov5647, sd);
85 }
86 
87 static struct regval_list sensor_oe_disable_regs[] = {
88 	{0x3000, 0x00},
89 	{0x3001, 0x00},
90 	{0x3002, 0x00},
91 };
92 
93 static struct regval_list sensor_oe_enable_regs[] = {
94 	{0x3000, 0x0f},
95 	{0x3001, 0xff},
96 	{0x3002, 0xe4},
97 };
98 
99 static struct regval_list ov5647_640x480[] = {
100 	{0x0100, 0x00},
101 	{0x0103, 0x01},
102 	{0x3034, 0x08},
103 	{0x3035, 0x21},
104 	{0x3036, 0x46},
105 	{0x303c, 0x11},
106 	{0x3106, 0xf5},
107 	{0x3821, 0x07},
108 	{0x3820, 0x41},
109 	{0x3827, 0xec},
110 	{0x370c, 0x0f},
111 	{0x3612, 0x59},
112 	{0x3618, 0x00},
113 	{0x5000, 0x06},
114 	{0x5001, 0x01},
115 	{0x5002, 0x41},
116 	{0x5003, 0x08},
117 	{0x5a00, 0x08},
118 	{0x3000, 0x00},
119 	{0x3001, 0x00},
120 	{0x3002, 0x00},
121 	{0x3016, 0x08},
122 	{0x3017, 0xe0},
123 	{0x3018, 0x44},
124 	{0x301c, 0xf8},
125 	{0x301d, 0xf0},
126 	{0x3a18, 0x00},
127 	{0x3a19, 0xf8},
128 	{0x3c01, 0x80},
129 	{0x3b07, 0x0c},
130 	{0x380c, 0x07},
131 	{0x380d, 0x68},
132 	{0x380e, 0x03},
133 	{0x380f, 0xd8},
134 	{0x3814, 0x31},
135 	{0x3815, 0x31},
136 	{0x3708, 0x64},
137 	{0x3709, 0x52},
138 	{0x3808, 0x02},
139 	{0x3809, 0x80},
140 	{0x380a, 0x01},
141 	{0x380b, 0xE0},
142 	{0x3801, 0x00},
143 	{0x3802, 0x00},
144 	{0x3803, 0x00},
145 	{0x3804, 0x0a},
146 	{0x3805, 0x3f},
147 	{0x3806, 0x07},
148 	{0x3807, 0xa1},
149 	{0x3811, 0x08},
150 	{0x3813, 0x02},
151 	{0x3630, 0x2e},
152 	{0x3632, 0xe2},
153 	{0x3633, 0x23},
154 	{0x3634, 0x44},
155 	{0x3636, 0x06},
156 	{0x3620, 0x64},
157 	{0x3621, 0xe0},
158 	{0x3600, 0x37},
159 	{0x3704, 0xa0},
160 	{0x3703, 0x5a},
161 	{0x3715, 0x78},
162 	{0x3717, 0x01},
163 	{0x3731, 0x02},
164 	{0x370b, 0x60},
165 	{0x3705, 0x1a},
166 	{0x3f05, 0x02},
167 	{0x3f06, 0x10},
168 	{0x3f01, 0x0a},
169 	{0x3a08, 0x01},
170 	{0x3a09, 0x27},
171 	{0x3a0a, 0x00},
172 	{0x3a0b, 0xf6},
173 	{0x3a0d, 0x04},
174 	{0x3a0e, 0x03},
175 	{0x3a0f, 0x58},
176 	{0x3a10, 0x50},
177 	{0x3a1b, 0x58},
178 	{0x3a1e, 0x50},
179 	{0x3a11, 0x60},
180 	{0x3a1f, 0x28},
181 	{0x4001, 0x02},
182 	{0x4004, 0x02},
183 	{0x4000, 0x09},
184 	{0x4837, 0x24},
185 	{0x4050, 0x6e},
186 	{0x4051, 0x8f},
187 	{0x0100, 0x01},
188 };
189 
190 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
191 {
192 	int ret;
193 	unsigned char data[3] = { reg >> 8, reg & 0xff, val};
194 	struct i2c_client *client = v4l2_get_subdevdata(sd);
195 
196 	ret = i2c_master_send(client, data, 3);
197 	if (ret < 0)
198 		dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
199 				__func__, reg);
200 
201 	return ret;
202 }
203 
204 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
205 {
206 	int ret;
207 	unsigned char data_w[2] = { reg >> 8, reg & 0xff };
208 	struct i2c_client *client = v4l2_get_subdevdata(sd);
209 
210 	ret = i2c_master_send(client, data_w, 2);
211 	if (ret < 0) {
212 		dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
213 			__func__, reg);
214 		return ret;
215 	}
216 
217 	ret = i2c_master_recv(client, val, 1);
218 	if (ret < 0)
219 		dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
220 				__func__, reg);
221 
222 	return ret;
223 }
224 
225 static int ov5647_write_array(struct v4l2_subdev *sd,
226 				struct regval_list *regs, int array_size)
227 {
228 	int i, ret;
229 
230 	for (i = 0; i < array_size; i++) {
231 		ret = ov5647_write(sd, regs[i].addr, regs[i].data);
232 		if (ret < 0)
233 			return ret;
234 	}
235 
236 	return 0;
237 }
238 
239 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
240 {
241 	u8 channel_id;
242 	int ret;
243 
244 	ret = ov5647_read(sd, 0x4814, &channel_id);
245 	if (ret < 0)
246 		return ret;
247 
248 	channel_id &= ~(3 << 6);
249 	return ov5647_write(sd, 0x4814, channel_id | (channel << 6));
250 }
251 
252 static int ov5647_stream_on(struct v4l2_subdev *sd)
253 {
254 	int ret;
255 
256 	ret = ov5647_write(sd, 0x4202, 0x00);
257 	if (ret < 0)
258 		return ret;
259 
260 	return ov5647_write(sd, 0x300D, 0x00);
261 }
262 
263 static int ov5647_stream_off(struct v4l2_subdev *sd)
264 {
265 	int ret;
266 
267 	ret = ov5647_write(sd, 0x4202, 0x0f);
268 	if (ret < 0)
269 		return ret;
270 
271 	return ov5647_write(sd, 0x300D, 0x01);
272 }
273 
274 static int set_sw_standby(struct v4l2_subdev *sd, bool standby)
275 {
276 	int ret;
277 	u8 rdval;
278 
279 	ret = ov5647_read(sd, 0x0100, &rdval);
280 	if (ret < 0)
281 		return ret;
282 
283 	if (standby)
284 		rdval &= ~0x01;
285 	else
286 		rdval |= 0x01;
287 
288 	return ov5647_write(sd, 0x0100, rdval);
289 }
290 
291 static int __sensor_init(struct v4l2_subdev *sd)
292 {
293 	int ret;
294 	u8 resetval, rdval;
295 	struct i2c_client *client = v4l2_get_subdevdata(sd);
296 
297 	ret = ov5647_read(sd, 0x0100, &rdval);
298 	if (ret < 0)
299 		return ret;
300 
301 	ret = ov5647_write_array(sd, ov5647_640x480,
302 					ARRAY_SIZE(ov5647_640x480));
303 	if (ret < 0) {
304 		dev_err(&client->dev, "write sensor default regs error\n");
305 		return ret;
306 	}
307 
308 	ret = ov5647_set_virtual_channel(sd, 0);
309 	if (ret < 0)
310 		return ret;
311 
312 	ret = ov5647_read(sd, 0x0100, &resetval);
313 	if (ret < 0)
314 		return ret;
315 
316 	if (!(resetval & 0x01)) {
317 		dev_err(&client->dev, "Device was in SW standby");
318 		ret = ov5647_write(sd, 0x0100, 0x01);
319 		if (ret < 0)
320 			return ret;
321 	}
322 
323 	return ov5647_write(sd, 0x4800, 0x04);
324 }
325 
326 static int ov5647_sensor_power(struct v4l2_subdev *sd, int on)
327 {
328 	int ret = 0;
329 	struct ov5647 *ov5647 = to_state(sd);
330 	struct i2c_client *client = v4l2_get_subdevdata(sd);
331 
332 	mutex_lock(&ov5647->lock);
333 
334 	if (on && !ov5647->power_count)	{
335 		dev_dbg(&client->dev, "OV5647 power on\n");
336 
337 		ret = clk_prepare_enable(ov5647->xclk);
338 		if (ret < 0) {
339 			dev_err(&client->dev, "clk prepare enable failed\n");
340 			goto out;
341 		}
342 
343 		ret = ov5647_write_array(sd, sensor_oe_enable_regs,
344 				ARRAY_SIZE(sensor_oe_enable_regs));
345 		if (ret < 0) {
346 			clk_disable_unprepare(ov5647->xclk);
347 			dev_err(&client->dev,
348 				"write sensor_oe_enable_regs error\n");
349 			goto out;
350 		}
351 
352 		ret = __sensor_init(sd);
353 		if (ret < 0) {
354 			clk_disable_unprepare(ov5647->xclk);
355 			dev_err(&client->dev,
356 				"Camera not available, check Power\n");
357 			goto out;
358 		}
359 	} else if (!on && ov5647->power_count == 1) {
360 		dev_dbg(&client->dev, "OV5647 power off\n");
361 
362 		ret = ov5647_write_array(sd, sensor_oe_disable_regs,
363 				ARRAY_SIZE(sensor_oe_disable_regs));
364 
365 		if (ret < 0)
366 			dev_dbg(&client->dev, "disable oe failed\n");
367 
368 		ret = set_sw_standby(sd, true);
369 
370 		if (ret < 0)
371 			dev_dbg(&client->dev, "soft stby failed\n");
372 
373 		clk_disable_unprepare(ov5647->xclk);
374 	}
375 
376 	/* Update the power count. */
377 	ov5647->power_count += on ? 1 : -1;
378 	WARN_ON(ov5647->power_count < 0);
379 
380 out:
381 	mutex_unlock(&ov5647->lock);
382 
383 	return ret;
384 }
385 
386 #ifdef CONFIG_VIDEO_ADV_DEBUG
387 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
388 				struct v4l2_dbg_register *reg)
389 {
390 	u8 val;
391 	int ret;
392 
393 	ret = ov5647_read(sd, reg->reg & 0xff, &val);
394 	if (ret < 0)
395 		return ret;
396 
397 	reg->val = val;
398 	reg->size = 1;
399 
400 	return 0;
401 }
402 
403 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
404 				const struct v4l2_dbg_register *reg)
405 {
406 	return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
407 }
408 #endif
409 
410 /**
411  * @short Subdev core operations registration
412  */
413 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
414 	.s_power		= ov5647_sensor_power,
415 #ifdef CONFIG_VIDEO_ADV_DEBUG
416 	.g_register		= ov5647_sensor_get_register,
417 	.s_register		= ov5647_sensor_set_register,
418 #endif
419 };
420 
421 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
422 {
423 	if (enable)
424 		return ov5647_stream_on(sd);
425 	else
426 		return ov5647_stream_off(sd);
427 }
428 
429 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
430 	.s_stream =		ov5647_s_stream,
431 };
432 
433 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
434 				struct v4l2_subdev_pad_config *cfg,
435 				struct v4l2_subdev_mbus_code_enum *code)
436 {
437 	if (code->index > 0)
438 		return -EINVAL;
439 
440 	code->code = MEDIA_BUS_FMT_SBGGR8_1X8;
441 
442 	return 0;
443 }
444 
445 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
446 	.enum_mbus_code = ov5647_enum_mbus_code,
447 };
448 
449 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
450 	.core		= &ov5647_subdev_core_ops,
451 	.video		= &ov5647_subdev_video_ops,
452 	.pad		= &ov5647_subdev_pad_ops,
453 };
454 
455 static int ov5647_detect(struct v4l2_subdev *sd)
456 {
457 	u8 read;
458 	int ret;
459 	struct i2c_client *client = v4l2_get_subdevdata(sd);
460 
461 	ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
462 	if (ret < 0)
463 		return ret;
464 
465 	ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
466 	if (ret < 0)
467 		return ret;
468 
469 	if (read != 0x56) {
470 		dev_err(&client->dev, "ID High expected 0x56 got %x", read);
471 		return -ENODEV;
472 	}
473 
474 	ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
475 	if (ret < 0)
476 		return ret;
477 
478 	if (read != 0x47) {
479 		dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
480 		return -ENODEV;
481 	}
482 
483 	return ov5647_write(sd, OV5647_SW_RESET, 0x00);
484 }
485 
486 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
487 {
488 	struct v4l2_mbus_framefmt *format =
489 				v4l2_subdev_get_try_format(sd, fh->pad, 0);
490 	struct v4l2_rect *crop =
491 				v4l2_subdev_get_try_crop(sd, fh->pad, 0);
492 
493 	crop->left = OV5647_COLUMN_START_DEF;
494 	crop->top = OV5647_ROW_START_DEF;
495 	crop->width = OV5647_WINDOW_WIDTH_DEF;
496 	crop->height = OV5647_WINDOW_HEIGHT_DEF;
497 
498 	format->code = MEDIA_BUS_FMT_SBGGR8_1X8;
499 
500 	format->width = OV5647_WINDOW_WIDTH_DEF;
501 	format->height = OV5647_WINDOW_HEIGHT_DEF;
502 	format->field = V4L2_FIELD_NONE;
503 	format->colorspace = V4L2_COLORSPACE_SRGB;
504 
505 	return 0;
506 }
507 
508 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
509 	.open = ov5647_open,
510 };
511 
512 static int ov5647_parse_dt(struct device_node *np)
513 {
514 	struct v4l2_fwnode_endpoint bus_cfg;
515 	struct device_node *ep;
516 
517 	int ret;
518 
519 	ep = of_graph_get_next_endpoint(np, NULL);
520 	if (!ep)
521 		return -EINVAL;
522 
523 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
524 
525 	of_node_put(ep);
526 	return ret;
527 }
528 
529 static int ov5647_probe(struct i2c_client *client,
530 			const struct i2c_device_id *id)
531 {
532 	struct device *dev = &client->dev;
533 	struct ov5647 *sensor;
534 	int ret;
535 	struct v4l2_subdev *sd;
536 	struct device_node *np = client->dev.of_node;
537 	u32 xclk_freq;
538 
539 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
540 	if (!sensor)
541 		return -ENOMEM;
542 
543 	if (IS_ENABLED(CONFIG_OF) && np) {
544 		ret = ov5647_parse_dt(np);
545 		if (ret) {
546 			dev_err(dev, "DT parsing error: %d\n", ret);
547 			return ret;
548 		}
549 	}
550 
551 	/* get system clock (xclk) */
552 	sensor->xclk = devm_clk_get(dev, NULL);
553 	if (IS_ERR(sensor->xclk)) {
554 		dev_err(dev, "could not get xclk");
555 		return PTR_ERR(sensor->xclk);
556 	}
557 
558 	xclk_freq = clk_get_rate(sensor->xclk);
559 	if (xclk_freq != 25000000) {
560 		dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
561 		return -EINVAL;
562 	}
563 
564 	mutex_init(&sensor->lock);
565 
566 	sd = &sensor->sd;
567 	v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
568 	sensor->sd.internal_ops = &ov5647_subdev_internal_ops;
569 	sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
570 
571 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
572 	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
573 	ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
574 	if (ret < 0)
575 		goto mutex_remove;
576 
577 	ret = ov5647_detect(sd);
578 	if (ret < 0)
579 		goto error;
580 
581 	ret = v4l2_async_register_subdev(sd);
582 	if (ret < 0)
583 		goto error;
584 
585 	dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
586 	return 0;
587 error:
588 	media_entity_cleanup(&sd->entity);
589 mutex_remove:
590 	mutex_destroy(&sensor->lock);
591 	return ret;
592 }
593 
594 static int ov5647_remove(struct i2c_client *client)
595 {
596 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
597 	struct ov5647 *ov5647 = to_state(sd);
598 
599 	v4l2_async_unregister_subdev(&ov5647->sd);
600 	media_entity_cleanup(&ov5647->sd.entity);
601 	v4l2_device_unregister_subdev(sd);
602 	mutex_destroy(&ov5647->lock);
603 
604 	return 0;
605 }
606 
607 static const struct i2c_device_id ov5647_id[] = {
608 	{ "ov5647", 0 },
609 	{ }
610 };
611 MODULE_DEVICE_TABLE(i2c, ov5647_id);
612 
613 #if IS_ENABLED(CONFIG_OF)
614 static const struct of_device_id ov5647_of_match[] = {
615 	{ .compatible = "ovti,ov5647" },
616 	{ /* sentinel */ },
617 };
618 MODULE_DEVICE_TABLE(of, ov5647_of_match);
619 #endif
620 
621 static struct i2c_driver ov5647_driver = {
622 	.driver = {
623 		.of_match_table = of_match_ptr(ov5647_of_match),
624 		.name	= SENSOR_NAME,
625 	},
626 	.probe		= ov5647_probe,
627 	.remove		= ov5647_remove,
628 	.id_table	= ov5647_id,
629 };
630 
631 module_i2c_driver(ov5647_driver);
632 
633 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
634 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
635 MODULE_LICENSE("GPL v2");
636