1 // SPDX-License-Identifier: GPL-2.0
3 * A V4L2 driver for OmniVision OV5647 cameras.
5 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
8 * Based on Omnivision OV7670 Camera Driver
11 * Copyright (C) 2016, Synopsys, Inc.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/of_graph.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-image-sizes.h>
30 #include <media/v4l2-mediabus.h>
33 * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
34 * high if reset is inserted after PWDN goes high, host can access sensor's
35 * SCCB to initialize sensor."
37 #define PWDN_ACTIVE_DELAY_MS 20
39 #define MIPI_CTRL00_CLOCK_LANE_GATE BIT(5)
40 #define MIPI_CTRL00_LINE_SYNC_ENABLE BIT(4)
41 #define MIPI_CTRL00_BUS_IDLE BIT(2)
42 #define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0)
44 #define OV5647_SW_STANDBY 0x0100
45 #define OV5647_SW_RESET 0x0103
46 #define OV5647_REG_CHIPID_H 0x300a
47 #define OV5647_REG_CHIPID_L 0x300b
48 #define OV5640_REG_PAD_OUT 0x300d
49 #define OV5647_REG_EXP_HI 0x3500
50 #define OV5647_REG_EXP_MID 0x3501
51 #define OV5647_REG_EXP_LO 0x3502
52 #define OV5647_REG_AEC_AGC 0x3503
53 #define OV5647_REG_GAIN_HI 0x350a
54 #define OV5647_REG_GAIN_LO 0x350b
55 #define OV5647_REG_VTS_HI 0x380e
56 #define OV5647_REG_VTS_LO 0x380f
57 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
58 #define OV5647_REG_MIPI_CTRL00 0x4800
59 #define OV5647_REG_MIPI_CTRL14 0x4814
60 #define OV5647_REG_AWB 0x5001
61 #define OV5647_REG_ISPCTRL3D 0x503d
63 #define REG_TERM 0xfffe
65 #define REG_DLY 0xffff
67 /* OV5647 native and active pixel array size */
68 #define OV5647_NATIVE_WIDTH 2624U
69 #define OV5647_NATIVE_HEIGHT 1956U
71 #define OV5647_PIXEL_ARRAY_LEFT 16U
72 #define OV5647_PIXEL_ARRAY_TOP 16U
73 #define OV5647_PIXEL_ARRAY_WIDTH 2592U
74 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
76 #define OV5647_VBLANK_MIN 4
77 #define OV5647_VTS_MAX 32767
79 #define OV5647_EXPOSURE_MIN 4
80 #define OV5647_EXPOSURE_STEP 1
81 #define OV5647_EXPOSURE_DEFAULT 1000
82 #define OV5647_EXPOSURE_MAX 65535
90 struct v4l2_mbus_framefmt format;
91 struct v4l2_rect crop;
95 const struct regval_list *reg_list;
96 unsigned int num_regs;
100 struct v4l2_subdev sd;
101 struct media_pad pad;
104 struct gpio_desc *pwdn;
106 struct v4l2_ctrl_handler ctrls;
107 const struct ov5647_mode *mode;
108 struct v4l2_ctrl *pixel_rate;
109 struct v4l2_ctrl *hblank;
110 struct v4l2_ctrl *vblank;
111 struct v4l2_ctrl *exposure;
115 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
117 return container_of(sd, struct ov5647, sd);
120 static const char * const ov5647_test_pattern_menu[] = {
127 static const u8 ov5647_test_pattern_val[] = {
129 0x80, /* Color Bars */
130 0x82, /* Color Squares */
131 0x81, /* Random Data */
134 static const struct regval_list sensor_oe_disable_regs[] = {
140 static const struct regval_list sensor_oe_enable_regs[] = {
146 static struct regval_list ov5647_2592x1944_10bpp[] = {
235 static struct regval_list ov5647_1080p30_10bpp[] = {
324 static struct regval_list ov5647_2x2binned_10bpp[] = {
417 static struct regval_list ov5647_640x480_10bpp[] = {
507 static const struct ov5647_mode ov5647_modes[] = {
508 /* 2592x1944 full resolution full FOV 10-bit mode. */
511 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
512 .colorspace = V4L2_COLORSPACE_SRGB,
513 .field = V4L2_FIELD_NONE,
518 .left = OV5647_PIXEL_ARRAY_LEFT,
519 .top = OV5647_PIXEL_ARRAY_TOP,
523 .pixel_rate = 87500000,
526 .reg_list = ov5647_2592x1944_10bpp,
527 .num_regs = ARRAY_SIZE(ov5647_2592x1944_10bpp)
529 /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
532 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
533 .colorspace = V4L2_COLORSPACE_SRGB,
534 .field = V4L2_FIELD_NONE,
539 .left = 348 + OV5647_PIXEL_ARRAY_LEFT,
540 .top = 434 + OV5647_PIXEL_ARRAY_TOP,
544 .pixel_rate = 81666700,
547 .reg_list = ov5647_1080p30_10bpp,
548 .num_regs = ARRAY_SIZE(ov5647_1080p30_10bpp)
550 /* 2x2 binned full FOV 10-bit mode. */
553 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
554 .colorspace = V4L2_COLORSPACE_SRGB,
555 .field = V4L2_FIELD_NONE,
560 .left = OV5647_PIXEL_ARRAY_LEFT,
561 .top = OV5647_PIXEL_ARRAY_TOP,
565 .pixel_rate = 81666700,
568 .reg_list = ov5647_2x2binned_10bpp,
569 .num_regs = ARRAY_SIZE(ov5647_2x2binned_10bpp)
571 /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
574 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
575 .colorspace = V4L2_COLORSPACE_SRGB,
576 .field = V4L2_FIELD_NONE,
581 .left = 16 + OV5647_PIXEL_ARRAY_LEFT,
582 .top = OV5647_PIXEL_ARRAY_TOP,
586 .pixel_rate = 55000000,
589 .reg_list = ov5647_640x480_10bpp,
590 .num_regs = ARRAY_SIZE(ov5647_640x480_10bpp)
594 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
595 #define OV5647_DEFAULT_MODE (&ov5647_modes[3])
596 #define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format)
598 static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
600 unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
601 struct i2c_client *client = v4l2_get_subdevdata(sd);
604 ret = i2c_master_send(client, data, 4);
606 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
614 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
616 unsigned char data[3] = { reg >> 8, reg & 0xff, val};
617 struct i2c_client *client = v4l2_get_subdevdata(sd);
620 ret = i2c_master_send(client, data, 3);
622 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
630 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
632 struct i2c_client *client = v4l2_get_subdevdata(sd);
633 u8 buf[2] = { reg >> 8, reg & 0xff };
634 struct i2c_msg msg[2];
637 msg[0].addr = client->addr;
638 msg[0].flags = client->flags;
640 msg[0].len = sizeof(buf);
642 msg[1].addr = client->addr;
643 msg[1].flags = client->flags | I2C_M_RD;
647 ret = i2c_transfer(client->adapter, msg, 2);
649 dev_err(&client->dev, "%s: i2c read error, reg: %x = %d\n",
651 return ret >= 0 ? -EINVAL : ret;
659 static int ov5647_write_array(struct v4l2_subdev *sd,
660 const struct regval_list *regs, int array_size)
664 for (i = 0; i < array_size; i++) {
665 ret = ov5647_write(sd, regs[i].addr, regs[i].data);
673 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
678 ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
682 channel_id &= ~(3 << 6);
684 return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
685 channel_id | (channel << 6));
688 static int ov5647_set_mode(struct v4l2_subdev *sd)
690 struct i2c_client *client = v4l2_get_subdevdata(sd);
691 struct ov5647 *sensor = to_sensor(sd);
695 ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
699 ret = ov5647_write_array(sd, sensor->mode->reg_list,
700 sensor->mode->num_regs);
702 dev_err(&client->dev, "write sensor default regs error\n");
706 ret = ov5647_set_virtual_channel(sd, 0);
710 ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
714 if (!(resetval & 0x01)) {
715 dev_err(&client->dev, "Device was in SW standby");
716 ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
724 static int ov5647_stream_on(struct v4l2_subdev *sd)
726 struct i2c_client *client = v4l2_get_subdevdata(sd);
727 struct ov5647 *sensor = to_sensor(sd);
728 u8 val = MIPI_CTRL00_BUS_IDLE;
731 ret = ov5647_set_mode(sd);
733 dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
737 /* Apply customized values from user when stream starts. */
738 ret = __v4l2_ctrl_handler_setup(sd->ctrl_handler);
742 if (sensor->clock_ncont)
743 val |= MIPI_CTRL00_CLOCK_LANE_GATE |
744 MIPI_CTRL00_LINE_SYNC_ENABLE;
746 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
750 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
754 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
757 static int ov5647_stream_off(struct v4l2_subdev *sd)
761 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
762 MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
763 MIPI_CTRL00_CLOCK_LANE_DISABLE);
767 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
771 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
774 static int ov5647_power_on(struct device *dev)
776 struct ov5647 *sensor = dev_get_drvdata(dev);
779 dev_dbg(dev, "OV5647 power on\n");
782 gpiod_set_value_cansleep(sensor->pwdn, 0);
783 msleep(PWDN_ACTIVE_DELAY_MS);
786 ret = clk_prepare_enable(sensor->xclk);
788 dev_err(dev, "clk prepare enable failed\n");
792 ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
793 ARRAY_SIZE(sensor_oe_enable_regs));
795 dev_err(dev, "write sensor_oe_enable_regs error\n");
796 goto error_clk_disable;
799 /* Stream off to coax lanes into LP-11 state. */
800 ret = ov5647_stream_off(&sensor->sd);
802 dev_err(dev, "camera not available, check power\n");
803 goto error_clk_disable;
809 clk_disable_unprepare(sensor->xclk);
811 gpiod_set_value_cansleep(sensor->pwdn, 1);
816 static int ov5647_power_off(struct device *dev)
818 struct ov5647 *sensor = dev_get_drvdata(dev);
822 dev_dbg(dev, "OV5647 power off\n");
824 ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
825 ARRAY_SIZE(sensor_oe_disable_regs));
827 dev_dbg(dev, "disable oe failed\n");
829 /* Enter software standby */
830 ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
832 dev_dbg(dev, "software standby failed\n");
835 ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
837 dev_dbg(dev, "software standby failed\n");
839 clk_disable_unprepare(sensor->xclk);
840 gpiod_set_value_cansleep(sensor->pwdn, 1);
845 #ifdef CONFIG_VIDEO_ADV_DEBUG
846 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
847 struct v4l2_dbg_register *reg)
852 ret = ov5647_read(sd, reg->reg & 0xff, &val);
862 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
863 const struct v4l2_dbg_register *reg)
865 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
869 /* Subdev core operations registration */
870 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
871 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
872 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
873 #ifdef CONFIG_VIDEO_ADV_DEBUG
874 .g_register = ov5647_sensor_get_register,
875 .s_register = ov5647_sensor_set_register,
879 static const struct v4l2_rect *
880 __ov5647_get_pad_crop(struct ov5647 *ov5647,
881 struct v4l2_subdev_state *sd_state,
882 unsigned int pad, enum v4l2_subdev_format_whence which)
885 case V4L2_SUBDEV_FORMAT_TRY:
886 return v4l2_subdev_get_try_crop(&ov5647->sd, sd_state, pad);
887 case V4L2_SUBDEV_FORMAT_ACTIVE:
888 return &ov5647->mode->crop;
894 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
896 struct i2c_client *client = v4l2_get_subdevdata(sd);
897 struct ov5647 *sensor = to_sensor(sd);
900 mutex_lock(&sensor->lock);
901 if (sensor->streaming == enable) {
902 mutex_unlock(&sensor->lock);
907 ret = pm_runtime_resume_and_get(&client->dev);
911 ret = ov5647_stream_on(sd);
913 dev_err(&client->dev, "stream start failed: %d\n", ret);
917 ret = ov5647_stream_off(sd);
919 dev_err(&client->dev, "stream stop failed: %d\n", ret);
922 pm_runtime_put(&client->dev);
925 sensor->streaming = enable;
926 mutex_unlock(&sensor->lock);
931 pm_runtime_put(&client->dev);
933 mutex_unlock(&sensor->lock);
938 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
939 .s_stream = ov5647_s_stream,
942 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
943 struct v4l2_subdev_state *sd_state,
944 struct v4l2_subdev_mbus_code_enum *code)
949 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
954 static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
955 struct v4l2_subdev_state *sd_state,
956 struct v4l2_subdev_frame_size_enum *fse)
958 const struct v4l2_mbus_framefmt *fmt;
960 if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
961 fse->index >= ARRAY_SIZE(ov5647_modes))
964 fmt = &ov5647_modes[fse->index].format;
965 fse->min_width = fmt->width;
966 fse->max_width = fmt->width;
967 fse->min_height = fmt->height;
968 fse->max_height = fmt->height;
973 static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
974 struct v4l2_subdev_state *sd_state,
975 struct v4l2_subdev_format *format)
977 struct v4l2_mbus_framefmt *fmt = &format->format;
978 const struct v4l2_mbus_framefmt *sensor_format;
979 struct ov5647 *sensor = to_sensor(sd);
981 mutex_lock(&sensor->lock);
982 switch (format->which) {
983 case V4L2_SUBDEV_FORMAT_TRY:
984 sensor_format = v4l2_subdev_get_try_format(sd, sd_state,
988 sensor_format = &sensor->mode->format;
992 *fmt = *sensor_format;
993 mutex_unlock(&sensor->lock);
998 static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
999 struct v4l2_subdev_state *sd_state,
1000 struct v4l2_subdev_format *format)
1002 struct v4l2_mbus_framefmt *fmt = &format->format;
1003 struct ov5647 *sensor = to_sensor(sd);
1004 const struct ov5647_mode *mode;
1006 mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
1007 format.width, format.height,
1008 fmt->width, fmt->height);
1010 /* Update the sensor mode and apply at it at streamon time. */
1011 mutex_lock(&sensor->lock);
1012 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1013 *v4l2_subdev_get_try_format(sd, sd_state, format->pad) = mode->format;
1015 int exposure_max, exposure_def;
1018 sensor->mode = mode;
1019 __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
1020 mode->pixel_rate, 1, mode->pixel_rate);
1022 hblank = mode->hts - mode->format.width;
1023 __v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
1026 vblank = mode->vts - mode->format.height;
1027 __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
1028 OV5647_VTS_MAX - mode->format.height,
1030 __v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
1032 exposure_max = mode->vts - 4;
1033 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1034 __v4l2_ctrl_modify_range(sensor->exposure,
1035 sensor->exposure->minimum,
1036 exposure_max, sensor->exposure->step,
1039 *fmt = mode->format;
1040 mutex_unlock(&sensor->lock);
1045 static int ov5647_get_selection(struct v4l2_subdev *sd,
1046 struct v4l2_subdev_state *sd_state,
1047 struct v4l2_subdev_selection *sel)
1049 switch (sel->target) {
1050 case V4L2_SEL_TGT_CROP: {
1051 struct ov5647 *sensor = to_sensor(sd);
1053 mutex_lock(&sensor->lock);
1054 sel->r = *__ov5647_get_pad_crop(sensor, sd_state, sel->pad,
1056 mutex_unlock(&sensor->lock);
1061 case V4L2_SEL_TGT_NATIVE_SIZE:
1064 sel->r.width = OV5647_NATIVE_WIDTH;
1065 sel->r.height = OV5647_NATIVE_HEIGHT;
1069 case V4L2_SEL_TGT_CROP_DEFAULT:
1070 case V4L2_SEL_TGT_CROP_BOUNDS:
1071 sel->r.top = OV5647_PIXEL_ARRAY_TOP;
1072 sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
1073 sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
1074 sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
1082 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
1083 .enum_mbus_code = ov5647_enum_mbus_code,
1084 .enum_frame_size = ov5647_enum_frame_size,
1085 .set_fmt = ov5647_set_pad_fmt,
1086 .get_fmt = ov5647_get_pad_fmt,
1087 .get_selection = ov5647_get_selection,
1090 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
1091 .core = &ov5647_subdev_core_ops,
1092 .video = &ov5647_subdev_video_ops,
1093 .pad = &ov5647_subdev_pad_ops,
1096 static int ov5647_detect(struct v4l2_subdev *sd)
1098 struct i2c_client *client = v4l2_get_subdevdata(sd);
1102 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
1106 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
1111 dev_err(&client->dev, "ID High expected 0x56 got %x", read);
1115 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
1120 dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
1124 return ov5647_write(sd, OV5647_SW_RESET, 0x00);
1127 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1129 struct v4l2_mbus_framefmt *format =
1130 v4l2_subdev_get_try_format(sd, fh->state, 0);
1131 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
1133 crop->left = OV5647_PIXEL_ARRAY_LEFT;
1134 crop->top = OV5647_PIXEL_ARRAY_TOP;
1135 crop->width = OV5647_PIXEL_ARRAY_WIDTH;
1136 crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
1138 *format = OV5647_DEFAULT_FORMAT;
1143 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
1144 .open = ov5647_open,
1147 static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
1149 return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
1152 static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
1157 /* Non-zero turns on AGC by clearing bit 1.*/
1158 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, ®);
1162 return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
1166 static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
1172 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1175 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, ®);
1179 return ov5647_write(sd, OV5647_REG_AEC_AGC,
1180 val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
1184 static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
1188 /* 10 bits of gain, 2 in the high register. */
1189 ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
1193 return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
1196 static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
1201 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1202 * which we leave as zero (and don't receive in "val").
1204 ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
1208 ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
1212 return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
1215 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
1217 struct ov5647 *sensor = container_of(ctrl->handler,
1218 struct ov5647, ctrls);
1219 struct v4l2_subdev *sd = &sensor->sd;
1220 struct i2c_client *client = v4l2_get_subdevdata(sd);
1224 /* v4l2_ctrl_lock() locks our own mutex */
1226 if (ctrl->id == V4L2_CID_VBLANK) {
1227 int exposure_max, exposure_def;
1229 /* Update max exposure while meeting expected vblanking */
1230 exposure_max = sensor->mode->format.height + ctrl->val - 4;
1231 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1232 __v4l2_ctrl_modify_range(sensor->exposure,
1233 sensor->exposure->minimum,
1234 exposure_max, sensor->exposure->step,
1239 * If the device is not powered up do not apply any controls
1240 * to H/W at this time. Instead the controls will be restored
1241 * at s_stream(1) time.
1243 if (pm_runtime_get_if_in_use(&client->dev) == 0)
1247 case V4L2_CID_AUTO_WHITE_BALANCE:
1248 ret = ov5647_s_auto_white_balance(sd, ctrl->val);
1250 case V4L2_CID_AUTOGAIN:
1251 ret = ov5647_s_autogain(sd, ctrl->val);
1253 case V4L2_CID_EXPOSURE_AUTO:
1254 ret = ov5647_s_exposure_auto(sd, ctrl->val);
1256 case V4L2_CID_ANALOGUE_GAIN:
1257 ret = ov5647_s_analogue_gain(sd, ctrl->val);
1259 case V4L2_CID_EXPOSURE:
1260 ret = ov5647_s_exposure(sd, ctrl->val);
1262 case V4L2_CID_VBLANK:
1263 ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
1264 sensor->mode->format.height + ctrl->val);
1266 case V4L2_CID_TEST_PATTERN:
1267 ret = ov5647_write(sd, OV5647_REG_ISPCTRL3D,
1268 ov5647_test_pattern_val[ctrl->val]);
1271 /* Read-only, but we adjust it based on mode. */
1272 case V4L2_CID_PIXEL_RATE:
1273 case V4L2_CID_HBLANK:
1274 /* Read-only, but we adjust it based on mode. */
1278 dev_info(&client->dev,
1279 "Control (id:0x%x, val:0x%x) not supported\n",
1280 ctrl->id, ctrl->val);
1284 pm_runtime_put(&client->dev);
1289 static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
1290 .s_ctrl = ov5647_s_ctrl,
1293 static int ov5647_init_controls(struct ov5647 *sensor)
1295 struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
1296 int hblank, exposure_max, exposure_def;
1298 v4l2_ctrl_handler_init(&sensor->ctrls, 9);
1300 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1301 V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
1303 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1304 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
1306 v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
1307 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1308 0, V4L2_EXPOSURE_MANUAL);
1310 exposure_max = sensor->mode->vts - 4;
1311 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1312 sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1314 OV5647_EXPOSURE_MIN,
1315 exposure_max, OV5647_EXPOSURE_STEP,
1318 /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1319 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1320 V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
1322 /* By default, PIXEL_RATE is read only, but it does change per mode */
1323 sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1324 V4L2_CID_PIXEL_RATE,
1325 sensor->mode->pixel_rate,
1326 sensor->mode->pixel_rate, 1,
1327 sensor->mode->pixel_rate);
1329 /* By default, HBLANK is read only, but it does change per mode. */
1330 hblank = sensor->mode->hts - sensor->mode->format.width;
1331 sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1332 V4L2_CID_HBLANK, hblank, hblank, 1,
1335 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1336 V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
1338 sensor->mode->format.height, 1,
1340 sensor->mode->format.height);
1342 v4l2_ctrl_new_std_menu_items(&sensor->ctrls, &ov5647_ctrl_ops,
1343 V4L2_CID_TEST_PATTERN,
1344 ARRAY_SIZE(ov5647_test_pattern_menu) - 1,
1345 0, 0, ov5647_test_pattern_menu);
1347 if (sensor->ctrls.error)
1350 sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1351 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1352 sensor->sd.ctrl_handler = &sensor->ctrls;
1357 dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
1358 __func__, sensor->ctrls.error);
1359 v4l2_ctrl_handler_free(&sensor->ctrls);
1361 return sensor->ctrls.error;
1364 static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
1366 struct v4l2_fwnode_endpoint bus_cfg = {
1367 .bus_type = V4L2_MBUS_CSI2_DPHY,
1369 struct device_node *ep;
1372 ep = of_graph_get_next_endpoint(np, NULL);
1376 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
1380 sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
1381 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1389 static int ov5647_probe(struct i2c_client *client)
1391 struct device_node *np = client->dev.of_node;
1392 struct device *dev = &client->dev;
1393 struct ov5647 *sensor;
1394 struct v4l2_subdev *sd;
1398 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1402 if (IS_ENABLED(CONFIG_OF) && np) {
1403 ret = ov5647_parse_dt(sensor, np);
1405 dev_err(dev, "DT parsing error: %d\n", ret);
1410 sensor->xclk = devm_clk_get(dev, NULL);
1411 if (IS_ERR(sensor->xclk)) {
1412 dev_err(dev, "could not get xclk");
1413 return PTR_ERR(sensor->xclk);
1416 xclk_freq = clk_get_rate(sensor->xclk);
1417 if (xclk_freq != 25000000) {
1418 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
1422 /* Request the power down GPIO asserted. */
1423 sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
1424 if (IS_ERR(sensor->pwdn)) {
1425 dev_err(dev, "Failed to get 'pwdn' gpio\n");
1429 mutex_init(&sensor->lock);
1431 sensor->mode = OV5647_DEFAULT_MODE;
1433 ret = ov5647_init_controls(sensor);
1438 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
1439 sd->internal_ops = &ov5647_subdev_internal_ops;
1440 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1442 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1443 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1444 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
1446 goto ctrl_handler_free;
1448 ret = ov5647_power_on(dev);
1450 goto entity_cleanup;
1452 ret = ov5647_detect(sd);
1456 ret = v4l2_async_register_subdev(sd);
1460 /* Enable runtime PM and turn off the device */
1461 pm_runtime_set_active(dev);
1462 pm_runtime_enable(dev);
1463 pm_runtime_idle(dev);
1465 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
1470 ov5647_power_off(dev);
1472 media_entity_cleanup(&sd->entity);
1474 v4l2_ctrl_handler_free(&sensor->ctrls);
1476 mutex_destroy(&sensor->lock);
1481 static void ov5647_remove(struct i2c_client *client)
1483 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1484 struct ov5647 *sensor = to_sensor(sd);
1486 v4l2_async_unregister_subdev(&sensor->sd);
1487 media_entity_cleanup(&sensor->sd.entity);
1488 v4l2_ctrl_handler_free(&sensor->ctrls);
1489 v4l2_device_unregister_subdev(sd);
1490 pm_runtime_disable(&client->dev);
1491 mutex_destroy(&sensor->lock);
1494 static const struct dev_pm_ops ov5647_pm_ops = {
1495 SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
1498 static const struct i2c_device_id ov5647_id[] = {
1502 MODULE_DEVICE_TABLE(i2c, ov5647_id);
1504 #if IS_ENABLED(CONFIG_OF)
1505 static const struct of_device_id ov5647_of_match[] = {
1506 { .compatible = "ovti,ov5647" },
1509 MODULE_DEVICE_TABLE(of, ov5647_of_match);
1512 static struct i2c_driver ov5647_driver = {
1514 .of_match_table = of_match_ptr(ov5647_of_match),
1516 .pm = &ov5647_pm_ops,
1518 .probe_new = ov5647_probe,
1519 .remove = ov5647_remove,
1520 .id_table = ov5647_id,
1523 module_i2c_driver(ov5647_driver);
1526 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1527 MODULE_LICENSE("GPL v2");