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 MIPI_CTRL00_CLOCK_LANE_GATE BIT(5)
39 #define MIPI_CTRL00_BUS_IDLE BIT(2)
40 #define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0)
42 #define OV5647_SW_STANDBY 0x0100
43 #define OV5647_SW_RESET 0x0103
44 #define OV5647_REG_CHIPID_H 0x300A
45 #define OV5647_REG_CHIPID_L 0x300B
46 #define OV5640_REG_PAD_OUT 0x300D
47 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
48 #define OV5647_REG_MIPI_CTRL00 0x4800
49 #define OV5647_REG_MIPI_CTRL14 0x4814
51 #define REG_TERM 0xfffe
53 #define REG_DLY 0xffff
55 #define OV5647_ROW_START 0x01
56 #define OV5647_ROW_START_MIN 0
57 #define OV5647_ROW_START_MAX 2004
58 #define OV5647_ROW_START_DEF 54
60 #define OV5647_COLUMN_START 0x02
61 #define OV5647_COLUMN_START_MIN 0
62 #define OV5647_COLUMN_START_MAX 2750
63 #define OV5647_COLUMN_START_DEF 16
65 #define OV5647_WINDOW_HEIGHT 0x03
66 #define OV5647_WINDOW_HEIGHT_MIN 2
67 #define OV5647_WINDOW_HEIGHT_MAX 2006
68 #define OV5647_WINDOW_HEIGHT_DEF 1944
70 #define OV5647_WINDOW_WIDTH 0x04
71 #define OV5647_WINDOW_WIDTH_MIN 2
72 #define OV5647_WINDOW_WIDTH_MAX 2752
73 #define OV5647_WINDOW_WIDTH_DEF 2592
81 struct v4l2_subdev sd;
84 struct v4l2_mbus_framefmt format;
91 static inline struct ov5647 *to_state(struct v4l2_subdev *sd)
93 return container_of(sd, struct ov5647, sd);
96 static struct regval_list sensor_oe_disable_regs[] = {
102 static struct regval_list sensor_oe_enable_regs[] = {
108 static struct regval_list ov5647_640x480[] = {
199 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
202 unsigned char data[3] = { reg >> 8, reg & 0xff, val};
203 struct i2c_client *client = v4l2_get_subdevdata(sd);
205 ret = i2c_master_send(client, data, 3);
207 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
213 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
216 unsigned char data_w[2] = { reg >> 8, reg & 0xff };
217 struct i2c_client *client = v4l2_get_subdevdata(sd);
219 ret = i2c_master_send(client, data_w, 2);
221 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
226 ret = i2c_master_recv(client, val, 1);
228 dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
234 static int ov5647_write_array(struct v4l2_subdev *sd,
235 struct regval_list *regs, int array_size)
239 for (i = 0; i < array_size; i++) {
240 ret = ov5647_write(sd, regs[i].addr, regs[i].data);
248 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
253 ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
257 channel_id &= ~(3 << 6);
258 return ov5647_write(sd, OV5647_REG_MIPI_CTRL14, channel_id | (channel << 6));
261 static int ov5647_stream_on(struct v4l2_subdev *sd)
265 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, MIPI_CTRL00_BUS_IDLE);
269 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
273 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
276 static int ov5647_stream_off(struct v4l2_subdev *sd)
280 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, MIPI_CTRL00_CLOCK_LANE_GATE
281 | MIPI_CTRL00_BUS_IDLE | MIPI_CTRL00_CLOCK_LANE_DISABLE);
285 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
289 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
292 static int set_sw_standby(struct v4l2_subdev *sd, bool standby)
297 ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
306 return ov5647_write(sd, OV5647_SW_STANDBY, rdval);
309 static int __sensor_init(struct v4l2_subdev *sd)
313 struct i2c_client *client = v4l2_get_subdevdata(sd);
315 ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
319 ret = ov5647_write_array(sd, ov5647_640x480,
320 ARRAY_SIZE(ov5647_640x480));
322 dev_err(&client->dev, "write sensor default regs error\n");
326 ret = ov5647_set_virtual_channel(sd, 0);
330 ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
334 if (!(resetval & 0x01)) {
335 dev_err(&client->dev, "Device was in SW standby");
336 ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
342 * stream off to make the clock lane into LP-11 state.
344 return ov5647_stream_off(sd);
347 static int ov5647_sensor_power(struct v4l2_subdev *sd, int on)
350 struct ov5647 *ov5647 = to_state(sd);
351 struct i2c_client *client = v4l2_get_subdevdata(sd);
353 mutex_lock(&ov5647->lock);
355 if (on && !ov5647->power_count) {
356 dev_dbg(&client->dev, "OV5647 power on\n");
358 ret = clk_prepare_enable(ov5647->xclk);
360 dev_err(&client->dev, "clk prepare enable failed\n");
364 ret = ov5647_write_array(sd, sensor_oe_enable_regs,
365 ARRAY_SIZE(sensor_oe_enable_regs));
367 clk_disable_unprepare(ov5647->xclk);
368 dev_err(&client->dev,
369 "write sensor_oe_enable_regs error\n");
373 ret = __sensor_init(sd);
375 clk_disable_unprepare(ov5647->xclk);
376 dev_err(&client->dev,
377 "Camera not available, check Power\n");
380 } else if (!on && ov5647->power_count == 1) {
381 dev_dbg(&client->dev, "OV5647 power off\n");
383 ret = ov5647_write_array(sd, sensor_oe_disable_regs,
384 ARRAY_SIZE(sensor_oe_disable_regs));
387 dev_dbg(&client->dev, "disable oe failed\n");
389 ret = set_sw_standby(sd, true);
392 dev_dbg(&client->dev, "soft stby failed\n");
394 clk_disable_unprepare(ov5647->xclk);
397 /* Update the power count. */
398 ov5647->power_count += on ? 1 : -1;
399 WARN_ON(ov5647->power_count < 0);
402 mutex_unlock(&ov5647->lock);
407 #ifdef CONFIG_VIDEO_ADV_DEBUG
408 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
409 struct v4l2_dbg_register *reg)
414 ret = ov5647_read(sd, reg->reg & 0xff, &val);
424 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
425 const struct v4l2_dbg_register *reg)
427 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
432 * Subdev core operations registration
434 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
435 .s_power = ov5647_sensor_power,
436 #ifdef CONFIG_VIDEO_ADV_DEBUG
437 .g_register = ov5647_sensor_get_register,
438 .s_register = ov5647_sensor_set_register,
442 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
445 return ov5647_stream_on(sd);
447 return ov5647_stream_off(sd);
450 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
451 .s_stream = ov5647_s_stream,
454 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
455 struct v4l2_subdev_pad_config *cfg,
456 struct v4l2_subdev_mbus_code_enum *code)
461 code->code = MEDIA_BUS_FMT_SBGGR8_1X8;
466 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
467 .enum_mbus_code = ov5647_enum_mbus_code,
470 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
471 .core = &ov5647_subdev_core_ops,
472 .video = &ov5647_subdev_video_ops,
473 .pad = &ov5647_subdev_pad_ops,
476 static int ov5647_detect(struct v4l2_subdev *sd)
480 struct i2c_client *client = v4l2_get_subdevdata(sd);
482 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
486 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
491 dev_err(&client->dev, "ID High expected 0x56 got %x", read);
495 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
500 dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
504 return ov5647_write(sd, OV5647_SW_RESET, 0x00);
507 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
509 struct v4l2_mbus_framefmt *format =
510 v4l2_subdev_get_try_format(sd, fh->pad, 0);
511 struct v4l2_rect *crop =
512 v4l2_subdev_get_try_crop(sd, fh->pad, 0);
514 crop->left = OV5647_COLUMN_START_DEF;
515 crop->top = OV5647_ROW_START_DEF;
516 crop->width = OV5647_WINDOW_WIDTH_DEF;
517 crop->height = OV5647_WINDOW_HEIGHT_DEF;
519 format->code = MEDIA_BUS_FMT_SBGGR8_1X8;
521 format->width = OV5647_WINDOW_WIDTH_DEF;
522 format->height = OV5647_WINDOW_HEIGHT_DEF;
523 format->field = V4L2_FIELD_NONE;
524 format->colorspace = V4L2_COLORSPACE_SRGB;
529 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
533 static int ov5647_parse_dt(struct device_node *np)
535 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
536 struct device_node *ep;
540 ep = of_graph_get_next_endpoint(np, NULL);
544 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
550 static int ov5647_probe(struct i2c_client *client)
552 struct device *dev = &client->dev;
553 struct ov5647 *sensor;
555 struct v4l2_subdev *sd;
556 struct device_node *np = client->dev.of_node;
559 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
563 if (IS_ENABLED(CONFIG_OF) && np) {
564 ret = ov5647_parse_dt(np);
566 dev_err(dev, "DT parsing error: %d\n", ret);
571 /* get system clock (xclk) */
572 sensor->xclk = devm_clk_get(dev, NULL);
573 if (IS_ERR(sensor->xclk)) {
574 dev_err(dev, "could not get xclk");
575 return PTR_ERR(sensor->xclk);
578 xclk_freq = clk_get_rate(sensor->xclk);
579 if (xclk_freq != 25000000) {
580 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
584 mutex_init(&sensor->lock);
587 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
588 sensor->sd.internal_ops = &ov5647_subdev_internal_ops;
589 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
591 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
592 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
593 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
597 ret = ov5647_detect(sd);
601 ret = v4l2_async_register_subdev(sd);
605 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
608 media_entity_cleanup(&sd->entity);
610 mutex_destroy(&sensor->lock);
614 static int ov5647_remove(struct i2c_client *client)
616 struct v4l2_subdev *sd = i2c_get_clientdata(client);
617 struct ov5647 *ov5647 = to_state(sd);
619 v4l2_async_unregister_subdev(&ov5647->sd);
620 media_entity_cleanup(&ov5647->sd.entity);
621 v4l2_device_unregister_subdev(sd);
622 mutex_destroy(&ov5647->lock);
627 static const struct i2c_device_id ov5647_id[] = {
631 MODULE_DEVICE_TABLE(i2c, ov5647_id);
633 #if IS_ENABLED(CONFIG_OF)
634 static const struct of_device_id ov5647_of_match[] = {
635 { .compatible = "ovti,ov5647" },
638 MODULE_DEVICE_TABLE(of, ov5647_of_match);
641 static struct i2c_driver ov5647_driver = {
643 .of_match_table = of_match_ptr(ov5647_of_match),
646 .probe_new = ov5647_probe,
647 .remove = ov5647_remove,
648 .id_table = ov5647_id,
651 module_i2c_driver(ov5647_driver);
653 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
654 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
655 MODULE_LICENSE("GPL v2");