2 * A V4L2 driver for OmniVision OV5647 cameras.
4 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
5 * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * Based on Omnivision OV7670 Camera Driver
8 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
10 * Copyright (C) 2016, Synopsys, Inc.
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.
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.
22 #include <linux/clk.h>
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/init.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>
36 #define SENSOR_NAME "ov5647"
38 #define OV5647_SW_RESET 0x0103
39 #define OV5647_REG_CHIPID_H 0x300A
40 #define OV5647_REG_CHIPID_L 0x300B
42 #define REG_TERM 0xfffe
44 #define REG_DLY 0xffff
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
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
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
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
72 struct v4l2_subdev sd;
75 struct v4l2_mbus_framefmt format;
82 static inline struct ov5647 *to_state(struct v4l2_subdev *sd)
84 return container_of(sd, struct ov5647, sd);
87 static struct regval_list sensor_oe_disable_regs[] = {
93 static struct regval_list sensor_oe_enable_regs[] = {
99 static struct regval_list ov5647_640x480[] = {
190 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
193 unsigned char data[3] = { reg >> 8, reg & 0xff, val};
194 struct i2c_client *client = v4l2_get_subdevdata(sd);
196 ret = i2c_master_send(client, data, 3);
198 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
204 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
207 unsigned char data_w[2] = { reg >> 8, reg & 0xff };
208 struct i2c_client *client = v4l2_get_subdevdata(sd);
210 ret = i2c_master_send(client, data_w, 2);
212 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
217 ret = i2c_master_recv(client, val, 1);
219 dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
225 static int ov5647_write_array(struct v4l2_subdev *sd,
226 struct regval_list *regs, int array_size)
230 for (i = 0; i < array_size; i++) {
231 ret = ov5647_write(sd, regs[i].addr, regs[i].data);
239 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
244 ret = ov5647_read(sd, 0x4814, &channel_id);
248 channel_id &= ~(3 << 6);
249 return ov5647_write(sd, 0x4814, channel_id | (channel << 6));
252 static int ov5647_stream_on(struct v4l2_subdev *sd)
256 ret = ov5647_write(sd, 0x4202, 0x00);
260 return ov5647_write(sd, 0x300D, 0x00);
263 static int ov5647_stream_off(struct v4l2_subdev *sd)
267 ret = ov5647_write(sd, 0x4202, 0x0f);
271 return ov5647_write(sd, 0x300D, 0x01);
274 static int set_sw_standby(struct v4l2_subdev *sd, bool standby)
279 ret = ov5647_read(sd, 0x0100, &rdval);
288 return ov5647_write(sd, 0x0100, rdval);
291 static int __sensor_init(struct v4l2_subdev *sd)
295 struct i2c_client *client = v4l2_get_subdevdata(sd);
297 ret = ov5647_read(sd, 0x0100, &rdval);
301 ret = ov5647_write_array(sd, ov5647_640x480,
302 ARRAY_SIZE(ov5647_640x480));
304 dev_err(&client->dev, "write sensor default regs error\n");
308 ret = ov5647_set_virtual_channel(sd, 0);
312 ret = ov5647_read(sd, 0x0100, &resetval);
316 if (!(resetval & 0x01)) {
317 dev_err(&client->dev, "Device was in SW standby");
318 ret = ov5647_write(sd, 0x0100, 0x01);
323 return ov5647_write(sd, 0x4800, 0x04);
326 static int ov5647_sensor_power(struct v4l2_subdev *sd, int on)
329 struct ov5647 *ov5647 = to_state(sd);
330 struct i2c_client *client = v4l2_get_subdevdata(sd);
332 mutex_lock(&ov5647->lock);
334 if (on && !ov5647->power_count) {
335 dev_dbg(&client->dev, "OV5647 power on\n");
337 ret = clk_prepare_enable(ov5647->xclk);
339 dev_err(&client->dev, "clk prepare enable failed\n");
343 ret = ov5647_write_array(sd, sensor_oe_enable_regs,
344 ARRAY_SIZE(sensor_oe_enable_regs));
346 clk_disable_unprepare(ov5647->xclk);
347 dev_err(&client->dev,
348 "write sensor_oe_enable_regs error\n");
352 ret = __sensor_init(sd);
354 clk_disable_unprepare(ov5647->xclk);
355 dev_err(&client->dev,
356 "Camera not available, check Power\n");
359 } else if (!on && ov5647->power_count == 1) {
360 dev_dbg(&client->dev, "OV5647 power off\n");
362 ret = ov5647_write_array(sd, sensor_oe_disable_regs,
363 ARRAY_SIZE(sensor_oe_disable_regs));
366 dev_dbg(&client->dev, "disable oe failed\n");
368 ret = set_sw_standby(sd, true);
371 dev_dbg(&client->dev, "soft stby failed\n");
373 clk_disable_unprepare(ov5647->xclk);
376 /* Update the power count. */
377 ov5647->power_count += on ? 1 : -1;
378 WARN_ON(ov5647->power_count < 0);
381 mutex_unlock(&ov5647->lock);
386 #ifdef CONFIG_VIDEO_ADV_DEBUG
387 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
388 struct v4l2_dbg_register *reg)
393 ret = ov5647_read(sd, reg->reg & 0xff, &val);
403 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
404 const struct v4l2_dbg_register *reg)
406 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
411 * @short Subdev core operations registration
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,
421 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
424 return ov5647_stream_on(sd);
426 return ov5647_stream_off(sd);
429 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
430 .s_stream = ov5647_s_stream,
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)
440 code->code = MEDIA_BUS_FMT_SBGGR8_1X8;
445 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
446 .enum_mbus_code = ov5647_enum_mbus_code,
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,
455 static int ov5647_detect(struct v4l2_subdev *sd)
459 struct i2c_client *client = v4l2_get_subdevdata(sd);
461 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
465 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
470 dev_err(&client->dev, "ID High expected 0x56 got %x", read);
474 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
479 dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
483 return ov5647_write(sd, OV5647_SW_RESET, 0x00);
486 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
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);
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;
498 format->code = MEDIA_BUS_FMT_SBGGR8_1X8;
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;
508 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
512 static int ov5647_parse_dt(struct device_node *np)
514 struct v4l2_fwnode_endpoint bus_cfg;
515 struct device_node *ep;
519 ep = of_graph_get_next_endpoint(np, NULL);
523 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
529 static int ov5647_probe(struct i2c_client *client,
530 const struct i2c_device_id *id)
532 struct device *dev = &client->dev;
533 struct ov5647 *sensor;
535 struct v4l2_subdev *sd;
536 struct device_node *np = client->dev.of_node;
539 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
543 if (IS_ENABLED(CONFIG_OF) && np) {
544 ret = ov5647_parse_dt(np);
546 dev_err(dev, "DT parsing error: %d\n", ret);
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);
558 xclk_freq = clk_get_rate(sensor->xclk);
559 if (xclk_freq != 25000000) {
560 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
564 mutex_init(&sensor->lock);
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;
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);
577 ret = ov5647_detect(sd);
581 ret = v4l2_async_register_subdev(sd);
585 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
588 media_entity_cleanup(&sd->entity);
590 mutex_destroy(&sensor->lock);
594 static int ov5647_remove(struct i2c_client *client)
596 struct v4l2_subdev *sd = i2c_get_clientdata(client);
597 struct ov5647 *ov5647 = to_state(sd);
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);
607 static const struct i2c_device_id ov5647_id[] = {
611 MODULE_DEVICE_TABLE(i2c, ov5647_id);
613 #if IS_ENABLED(CONFIG_OF)
614 static const struct of_device_id ov5647_of_match[] = {
615 { .compatible = "ovti,ov5647" },
618 MODULE_DEVICE_TABLE(of, ov5647_of_match);
621 static struct i2c_driver ov5647_driver = {
623 .of_match_table = of_match_ptr(ov5647_of_match),
626 .probe = ov5647_probe,
627 .remove = ov5647_remove,
628 .id_table = ov5647_id,
631 module_i2c_driver(ov5647_driver);
633 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
634 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
635 MODULE_LICENSE("GPL v2");