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
62 #define REG_TERM 0xfffe
64 #define REG_DLY 0xffff
66 /* OV5647 native and active pixel array size */
67 #define OV5647_NATIVE_WIDTH 2624U
68 #define OV5647_NATIVE_HEIGHT 1956U
70 #define OV5647_PIXEL_ARRAY_LEFT 16U
71 #define OV5647_PIXEL_ARRAY_TOP 16U
72 #define OV5647_PIXEL_ARRAY_WIDTH 2592U
73 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
75 #define OV5647_VBLANK_MIN 4
76 #define OV5647_VTS_MAX 32767
78 #define OV5647_EXPOSURE_MIN 4
79 #define OV5647_EXPOSURE_STEP 1
80 #define OV5647_EXPOSURE_DEFAULT 1000
81 #define OV5647_EXPOSURE_MAX 65535
89 struct v4l2_mbus_framefmt format;
90 struct v4l2_rect crop;
94 const struct regval_list *reg_list;
95 unsigned int num_regs;
99 struct v4l2_subdev sd;
100 struct media_pad pad;
103 struct gpio_desc *pwdn;
105 struct v4l2_ctrl_handler ctrls;
106 const struct ov5647_mode *mode;
107 struct v4l2_ctrl *pixel_rate;
108 struct v4l2_ctrl *hblank;
109 struct v4l2_ctrl *vblank;
110 struct v4l2_ctrl *exposure;
114 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
116 return container_of(sd, struct ov5647, sd);
119 static const struct regval_list sensor_oe_disable_regs[] = {
125 static const struct regval_list sensor_oe_enable_regs[] = {
131 static struct regval_list ov5647_2592x1944_10bpp[] = {
220 static struct regval_list ov5647_1080p30_10bpp[] = {
309 static struct regval_list ov5647_2x2binned_10bpp[] = {
402 static struct regval_list ov5647_640x480_10bpp[] = {
492 static const struct ov5647_mode ov5647_modes[] = {
493 /* 2592x1944 full resolution full FOV 10-bit mode. */
496 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
497 .colorspace = V4L2_COLORSPACE_SRGB,
498 .field = V4L2_FIELD_NONE,
503 .left = OV5647_PIXEL_ARRAY_LEFT,
504 .top = OV5647_PIXEL_ARRAY_TOP,
508 .pixel_rate = 87500000,
511 .reg_list = ov5647_2592x1944_10bpp,
512 .num_regs = ARRAY_SIZE(ov5647_2592x1944_10bpp)
514 /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
517 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
518 .colorspace = V4L2_COLORSPACE_SRGB,
519 .field = V4L2_FIELD_NONE,
524 .left = 348 + OV5647_PIXEL_ARRAY_LEFT,
525 .top = 434 + OV5647_PIXEL_ARRAY_TOP,
529 .pixel_rate = 81666700,
532 .reg_list = ov5647_1080p30_10bpp,
533 .num_regs = ARRAY_SIZE(ov5647_1080p30_10bpp)
535 /* 2x2 binned full FOV 10-bit mode. */
538 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
539 .colorspace = V4L2_COLORSPACE_SRGB,
540 .field = V4L2_FIELD_NONE,
545 .left = OV5647_PIXEL_ARRAY_LEFT,
546 .top = OV5647_PIXEL_ARRAY_TOP,
550 .pixel_rate = 81666700,
553 .reg_list = ov5647_2x2binned_10bpp,
554 .num_regs = ARRAY_SIZE(ov5647_2x2binned_10bpp)
556 /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
559 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
560 .colorspace = V4L2_COLORSPACE_SRGB,
561 .field = V4L2_FIELD_NONE,
566 .left = 16 + OV5647_PIXEL_ARRAY_LEFT,
567 .top = OV5647_PIXEL_ARRAY_TOP,
571 .pixel_rate = 55000000,
574 .reg_list = ov5647_640x480_10bpp,
575 .num_regs = ARRAY_SIZE(ov5647_640x480_10bpp)
579 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
580 #define OV5647_DEFAULT_MODE (&ov5647_modes[3])
581 #define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format)
583 static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
585 unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
586 struct i2c_client *client = v4l2_get_subdevdata(sd);
589 ret = i2c_master_send(client, data, 4);
591 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
599 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
601 unsigned char data[3] = { reg >> 8, reg & 0xff, val};
602 struct i2c_client *client = v4l2_get_subdevdata(sd);
605 ret = i2c_master_send(client, data, 3);
607 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
615 static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
617 unsigned char data_w[2] = { reg >> 8, reg & 0xff };
618 struct i2c_client *client = v4l2_get_subdevdata(sd);
621 ret = i2c_master_send(client, data_w, 2);
623 dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
628 ret = i2c_master_recv(client, val, 1);
630 dev_dbg(&client->dev, "%s: i2c read error, reg: %x\n",
638 static int ov5647_write_array(struct v4l2_subdev *sd,
639 const struct regval_list *regs, int array_size)
643 for (i = 0; i < array_size; i++) {
644 ret = ov5647_write(sd, regs[i].addr, regs[i].data);
652 static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
657 ret = ov5647_read(sd, OV5647_REG_MIPI_CTRL14, &channel_id);
661 channel_id &= ~(3 << 6);
663 return ov5647_write(sd, OV5647_REG_MIPI_CTRL14,
664 channel_id | (channel << 6));
667 static int ov5647_set_mode(struct v4l2_subdev *sd)
669 struct i2c_client *client = v4l2_get_subdevdata(sd);
670 struct ov5647 *sensor = to_sensor(sd);
674 ret = ov5647_read(sd, OV5647_SW_STANDBY, &rdval);
678 ret = ov5647_write_array(sd, sensor->mode->reg_list,
679 sensor->mode->num_regs);
681 dev_err(&client->dev, "write sensor default regs error\n");
685 ret = ov5647_set_virtual_channel(sd, 0);
689 ret = ov5647_read(sd, OV5647_SW_STANDBY, &resetval);
693 if (!(resetval & 0x01)) {
694 dev_err(&client->dev, "Device was in SW standby");
695 ret = ov5647_write(sd, OV5647_SW_STANDBY, 0x01);
703 static int ov5647_stream_on(struct v4l2_subdev *sd)
705 struct i2c_client *client = v4l2_get_subdevdata(sd);
706 struct ov5647 *sensor = to_sensor(sd);
707 u8 val = MIPI_CTRL00_BUS_IDLE;
710 ret = ov5647_set_mode(sd);
712 dev_err(&client->dev, "Failed to program sensor mode: %d\n", ret);
716 /* Apply customized values from user when stream starts. */
717 ret = __v4l2_ctrl_handler_setup(sd->ctrl_handler);
721 if (sensor->clock_ncont)
722 val |= MIPI_CTRL00_CLOCK_LANE_GATE |
723 MIPI_CTRL00_LINE_SYNC_ENABLE;
725 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00, val);
729 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x00);
733 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x00);
736 static int ov5647_stream_off(struct v4l2_subdev *sd)
740 ret = ov5647_write(sd, OV5647_REG_MIPI_CTRL00,
741 MIPI_CTRL00_CLOCK_LANE_GATE | MIPI_CTRL00_BUS_IDLE |
742 MIPI_CTRL00_CLOCK_LANE_DISABLE);
746 ret = ov5647_write(sd, OV5647_REG_FRAME_OFF_NUMBER, 0x0f);
750 return ov5647_write(sd, OV5640_REG_PAD_OUT, 0x01);
753 static int ov5647_power_on(struct device *dev)
755 struct ov5647 *sensor = dev_get_drvdata(dev);
758 dev_dbg(dev, "OV5647 power on\n");
761 gpiod_set_value_cansleep(sensor->pwdn, 0);
762 msleep(PWDN_ACTIVE_DELAY_MS);
765 ret = clk_prepare_enable(sensor->xclk);
767 dev_err(dev, "clk prepare enable failed\n");
771 ret = ov5647_write_array(&sensor->sd, sensor_oe_enable_regs,
772 ARRAY_SIZE(sensor_oe_enable_regs));
774 dev_err(dev, "write sensor_oe_enable_regs error\n");
775 goto error_clk_disable;
778 /* Stream off to coax lanes into LP-11 state. */
779 ret = ov5647_stream_off(&sensor->sd);
781 dev_err(dev, "camera not available, check power\n");
782 goto error_clk_disable;
788 clk_disable_unprepare(sensor->xclk);
790 gpiod_set_value_cansleep(sensor->pwdn, 1);
795 static int ov5647_power_off(struct device *dev)
797 struct ov5647 *sensor = dev_get_drvdata(dev);
801 dev_dbg(dev, "OV5647 power off\n");
803 ret = ov5647_write_array(&sensor->sd, sensor_oe_disable_regs,
804 ARRAY_SIZE(sensor_oe_disable_regs));
806 dev_dbg(dev, "disable oe failed\n");
808 /* Enter software standby */
809 ret = ov5647_read(&sensor->sd, OV5647_SW_STANDBY, &rdval);
811 dev_dbg(dev, "software standby failed\n");
814 ret = ov5647_write(&sensor->sd, OV5647_SW_STANDBY, rdval);
816 dev_dbg(dev, "software standby failed\n");
818 clk_disable_unprepare(sensor->xclk);
819 gpiod_set_value_cansleep(sensor->pwdn, 1);
824 #ifdef CONFIG_VIDEO_ADV_DEBUG
825 static int ov5647_sensor_get_register(struct v4l2_subdev *sd,
826 struct v4l2_dbg_register *reg)
831 ret = ov5647_read(sd, reg->reg & 0xff, &val);
841 static int ov5647_sensor_set_register(struct v4l2_subdev *sd,
842 const struct v4l2_dbg_register *reg)
844 return ov5647_write(sd, reg->reg & 0xff, reg->val & 0xff);
848 /* Subdev core operations registration */
849 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops = {
850 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
851 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
852 #ifdef CONFIG_VIDEO_ADV_DEBUG
853 .g_register = ov5647_sensor_get_register,
854 .s_register = ov5647_sensor_set_register,
858 static const struct v4l2_rect *
859 __ov5647_get_pad_crop(struct ov5647 *ov5647, struct v4l2_subdev_pad_config *cfg,
860 unsigned int pad, enum v4l2_subdev_format_whence which)
863 case V4L2_SUBDEV_FORMAT_TRY:
864 return v4l2_subdev_get_try_crop(&ov5647->sd, cfg, pad);
865 case V4L2_SUBDEV_FORMAT_ACTIVE:
866 return &ov5647->mode->crop;
872 static int ov5647_s_stream(struct v4l2_subdev *sd, int enable)
874 struct i2c_client *client = v4l2_get_subdevdata(sd);
875 struct ov5647 *sensor = to_sensor(sd);
878 mutex_lock(&sensor->lock);
879 if (sensor->streaming == enable) {
880 mutex_unlock(&sensor->lock);
885 ret = pm_runtime_get_sync(&client->dev);
889 ret = ov5647_stream_on(sd);
891 dev_err(&client->dev, "stream start failed: %d\n", ret);
895 ret = ov5647_stream_off(sd);
897 dev_err(&client->dev, "stream stop failed: %d\n", ret);
900 pm_runtime_put(&client->dev);
903 sensor->streaming = enable;
904 mutex_unlock(&sensor->lock);
909 pm_runtime_put(&client->dev);
910 mutex_unlock(&sensor->lock);
915 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops = {
916 .s_stream = ov5647_s_stream,
919 static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
920 struct v4l2_subdev_pad_config *cfg,
921 struct v4l2_subdev_mbus_code_enum *code)
926 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
931 static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
932 struct v4l2_subdev_pad_config *cfg,
933 struct v4l2_subdev_frame_size_enum *fse)
935 const struct v4l2_mbus_framefmt *fmt;
937 if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10 ||
938 fse->index >= ARRAY_SIZE(ov5647_modes))
941 fmt = &ov5647_modes[fse->index].format;
942 fse->min_width = fmt->width;
943 fse->max_width = fmt->width;
944 fse->min_height = fmt->height;
945 fse->max_height = fmt->height;
950 static int ov5647_get_pad_fmt(struct v4l2_subdev *sd,
951 struct v4l2_subdev_pad_config *cfg,
952 struct v4l2_subdev_format *format)
954 struct v4l2_mbus_framefmt *fmt = &format->format;
955 const struct v4l2_mbus_framefmt *sensor_format;
956 struct ov5647 *sensor = to_sensor(sd);
958 mutex_lock(&sensor->lock);
959 switch (format->which) {
960 case V4L2_SUBDEV_FORMAT_TRY:
961 sensor_format = v4l2_subdev_get_try_format(sd, cfg, format->pad);
964 sensor_format = &sensor->mode->format;
968 *fmt = *sensor_format;
969 mutex_unlock(&sensor->lock);
974 static int ov5647_set_pad_fmt(struct v4l2_subdev *sd,
975 struct v4l2_subdev_pad_config *cfg,
976 struct v4l2_subdev_format *format)
978 struct v4l2_mbus_framefmt *fmt = &format->format;
979 struct ov5647 *sensor = to_sensor(sd);
980 const struct ov5647_mode *mode;
982 mode = v4l2_find_nearest_size(ov5647_modes, ARRAY_SIZE(ov5647_modes),
983 format.width, format.height,
984 fmt->width, fmt->height);
986 /* Update the sensor mode and apply at it at streamon time. */
987 mutex_lock(&sensor->lock);
988 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
989 *v4l2_subdev_get_try_format(sd, cfg, format->pad) = mode->format;
991 int exposure_max, exposure_def;
995 __v4l2_ctrl_modify_range(sensor->pixel_rate, mode->pixel_rate,
996 mode->pixel_rate, 1, mode->pixel_rate);
998 hblank = mode->hts - mode->format.width;
999 __v4l2_ctrl_modify_range(sensor->hblank, hblank, hblank, 1,
1002 vblank = mode->vts - mode->format.height;
1003 __v4l2_ctrl_modify_range(sensor->vblank, OV5647_VBLANK_MIN,
1004 OV5647_VTS_MAX - mode->format.height,
1006 __v4l2_ctrl_s_ctrl(sensor->vblank, vblank);
1008 exposure_max = mode->vts - 4;
1009 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1010 __v4l2_ctrl_modify_range(sensor->exposure,
1011 sensor->exposure->minimum,
1012 exposure_max, sensor->exposure->step,
1015 *fmt = mode->format;
1016 mutex_unlock(&sensor->lock);
1021 static int ov5647_get_selection(struct v4l2_subdev *sd,
1022 struct v4l2_subdev_pad_config *cfg,
1023 struct v4l2_subdev_selection *sel)
1025 switch (sel->target) {
1026 case V4L2_SEL_TGT_CROP: {
1027 struct ov5647 *sensor = to_sensor(sd);
1029 mutex_lock(&sensor->lock);
1030 sel->r = *__ov5647_get_pad_crop(sensor, cfg, sel->pad,
1032 mutex_unlock(&sensor->lock);
1037 case V4L2_SEL_TGT_NATIVE_SIZE:
1040 sel->r.width = OV5647_NATIVE_WIDTH;
1041 sel->r.height = OV5647_NATIVE_HEIGHT;
1045 case V4L2_SEL_TGT_CROP_DEFAULT:
1046 case V4L2_SEL_TGT_CROP_BOUNDS:
1047 sel->r.top = OV5647_PIXEL_ARRAY_TOP;
1048 sel->r.left = OV5647_PIXEL_ARRAY_LEFT;
1049 sel->r.width = OV5647_PIXEL_ARRAY_WIDTH;
1050 sel->r.height = OV5647_PIXEL_ARRAY_HEIGHT;
1058 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops = {
1059 .enum_mbus_code = ov5647_enum_mbus_code,
1060 .enum_frame_size = ov5647_enum_frame_size,
1061 .set_fmt = ov5647_set_pad_fmt,
1062 .get_fmt = ov5647_get_pad_fmt,
1063 .get_selection = ov5647_get_selection,
1066 static const struct v4l2_subdev_ops ov5647_subdev_ops = {
1067 .core = &ov5647_subdev_core_ops,
1068 .video = &ov5647_subdev_video_ops,
1069 .pad = &ov5647_subdev_pad_ops,
1072 static int ov5647_detect(struct v4l2_subdev *sd)
1074 struct i2c_client *client = v4l2_get_subdevdata(sd);
1078 ret = ov5647_write(sd, OV5647_SW_RESET, 0x01);
1082 ret = ov5647_read(sd, OV5647_REG_CHIPID_H, &read);
1087 dev_err(&client->dev, "ID High expected 0x56 got %x", read);
1091 ret = ov5647_read(sd, OV5647_REG_CHIPID_L, &read);
1096 dev_err(&client->dev, "ID Low expected 0x47 got %x", read);
1100 return ov5647_write(sd, OV5647_SW_RESET, 0x00);
1103 static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1105 struct v4l2_mbus_framefmt *format =
1106 v4l2_subdev_get_try_format(sd, fh->pad, 0);
1107 struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
1109 crop->left = OV5647_PIXEL_ARRAY_LEFT;
1110 crop->top = OV5647_PIXEL_ARRAY_TOP;
1111 crop->width = OV5647_PIXEL_ARRAY_WIDTH;
1112 crop->height = OV5647_PIXEL_ARRAY_HEIGHT;
1114 *format = OV5647_DEFAULT_FORMAT;
1119 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops = {
1120 .open = ov5647_open,
1123 static int ov5647_s_auto_white_balance(struct v4l2_subdev *sd, u32 val)
1125 return ov5647_write(sd, OV5647_REG_AWB, val ? 1 : 0);
1128 static int ov5647_s_autogain(struct v4l2_subdev *sd, u32 val)
1133 /* Non-zero turns on AGC by clearing bit 1.*/
1134 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, ®);
1138 return ov5647_write(sd, OV5647_REG_AEC_AGC, val ? reg & ~BIT(1)
1142 static int ov5647_s_exposure_auto(struct v4l2_subdev *sd, u32 val)
1148 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1151 ret = ov5647_read(sd, OV5647_REG_AEC_AGC, ®);
1155 return ov5647_write(sd, OV5647_REG_AEC_AGC,
1156 val == V4L2_EXPOSURE_MANUAL ? reg | BIT(0)
1160 static int ov5647_s_analogue_gain(struct v4l2_subdev *sd, u32 val)
1164 /* 10 bits of gain, 2 in the high register. */
1165 ret = ov5647_write(sd, OV5647_REG_GAIN_HI, (val >> 8) & 3);
1169 return ov5647_write(sd, OV5647_REG_GAIN_LO, val & 0xff);
1172 static int ov5647_s_exposure(struct v4l2_subdev *sd, u32 val)
1177 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1178 * which we leave as zero (and don't receive in "val").
1180 ret = ov5647_write(sd, OV5647_REG_EXP_HI, (val >> 12) & 0xf);
1184 ret = ov5647_write(sd, OV5647_REG_EXP_MID, (val >> 4) & 0xff);
1188 return ov5647_write(sd, OV5647_REG_EXP_LO, (val & 0xf) << 4);
1191 static int ov5647_s_ctrl(struct v4l2_ctrl *ctrl)
1193 struct ov5647 *sensor = container_of(ctrl->handler,
1194 struct ov5647, ctrls);
1195 struct v4l2_subdev *sd = &sensor->sd;
1196 struct i2c_client *client = v4l2_get_subdevdata(sd);
1200 /* v4l2_ctrl_lock() locks our own mutex */
1202 if (ctrl->id == V4L2_CID_VBLANK) {
1203 int exposure_max, exposure_def;
1205 /* Update max exposure while meeting expected vblanking */
1206 exposure_max = sensor->mode->format.height + ctrl->val - 4;
1207 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1208 __v4l2_ctrl_modify_range(sensor->exposure,
1209 sensor->exposure->minimum,
1210 exposure_max, sensor->exposure->step,
1215 * If the device is not powered up do not apply any controls
1216 * to H/W at this time. Instead the controls will be restored
1217 * at s_stream(1) time.
1219 if (pm_runtime_get_if_in_use(&client->dev) == 0)
1223 case V4L2_CID_AUTO_WHITE_BALANCE:
1224 ret = ov5647_s_auto_white_balance(sd, ctrl->val);
1226 case V4L2_CID_AUTOGAIN:
1227 ret = ov5647_s_autogain(sd, ctrl->val);
1229 case V4L2_CID_EXPOSURE_AUTO:
1230 ret = ov5647_s_exposure_auto(sd, ctrl->val);
1232 case V4L2_CID_ANALOGUE_GAIN:
1233 ret = ov5647_s_analogue_gain(sd, ctrl->val);
1235 case V4L2_CID_EXPOSURE:
1236 ret = ov5647_s_exposure(sd, ctrl->val);
1238 case V4L2_CID_VBLANK:
1239 ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
1240 sensor->mode->format.height + ctrl->val);
1243 /* Read-only, but we adjust it based on mode. */
1244 case V4L2_CID_PIXEL_RATE:
1245 case V4L2_CID_HBLANK:
1246 /* Read-only, but we adjust it based on mode. */
1250 dev_info(&client->dev,
1251 "Control (id:0x%x, val:0x%x) not supported\n",
1252 ctrl->id, ctrl->val);
1256 pm_runtime_put(&client->dev);
1261 static const struct v4l2_ctrl_ops ov5647_ctrl_ops = {
1262 .s_ctrl = ov5647_s_ctrl,
1265 static int ov5647_init_controls(struct ov5647 *sensor)
1267 struct i2c_client *client = v4l2_get_subdevdata(&sensor->sd);
1268 int hblank, exposure_max, exposure_def;
1270 v4l2_ctrl_handler_init(&sensor->ctrls, 8);
1272 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1273 V4L2_CID_AUTOGAIN, 0, 1, 1, 0);
1275 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1276 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 0);
1278 v4l2_ctrl_new_std_menu(&sensor->ctrls, &ov5647_ctrl_ops,
1279 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1280 0, V4L2_EXPOSURE_MANUAL);
1282 exposure_max = sensor->mode->vts - 4;
1283 exposure_def = min(exposure_max, OV5647_EXPOSURE_DEFAULT);
1284 sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1286 OV5647_EXPOSURE_MIN,
1287 exposure_max, OV5647_EXPOSURE_STEP,
1290 /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1291 v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1292 V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 32);
1294 /* By default, PIXEL_RATE is read only, but it does change per mode */
1295 sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1296 V4L2_CID_PIXEL_RATE,
1297 sensor->mode->pixel_rate,
1298 sensor->mode->pixel_rate, 1,
1299 sensor->mode->pixel_rate);
1301 /* By default, HBLANK is read only, but it does change per mode. */
1302 hblank = sensor->mode->hts - sensor->mode->format.width;
1303 sensor->hblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1304 V4L2_CID_HBLANK, hblank, hblank, 1,
1307 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
1308 V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
1310 sensor->mode->format.height, 1,
1312 sensor->mode->format.height);
1314 if (sensor->ctrls.error)
1317 sensor->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1318 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1319 sensor->sd.ctrl_handler = &sensor->ctrls;
1324 dev_err(&client->dev, "%s Controls initialization failed (%d)\n",
1325 __func__, sensor->ctrls.error);
1326 v4l2_ctrl_handler_free(&sensor->ctrls);
1328 return sensor->ctrls.error;
1331 static int ov5647_parse_dt(struct ov5647 *sensor, struct device_node *np)
1333 struct v4l2_fwnode_endpoint bus_cfg = {
1334 .bus_type = V4L2_MBUS_CSI2_DPHY,
1336 struct device_node *ep;
1339 ep = of_graph_get_next_endpoint(np, NULL);
1343 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
1347 sensor->clock_ncont = bus_cfg.bus.mipi_csi2.flags &
1348 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1356 static int ov5647_probe(struct i2c_client *client)
1358 struct device_node *np = client->dev.of_node;
1359 struct device *dev = &client->dev;
1360 struct ov5647 *sensor;
1361 struct v4l2_subdev *sd;
1365 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1369 if (IS_ENABLED(CONFIG_OF) && np) {
1370 ret = ov5647_parse_dt(sensor, np);
1372 dev_err(dev, "DT parsing error: %d\n", ret);
1377 sensor->xclk = devm_clk_get(dev, NULL);
1378 if (IS_ERR(sensor->xclk)) {
1379 dev_err(dev, "could not get xclk");
1380 return PTR_ERR(sensor->xclk);
1383 xclk_freq = clk_get_rate(sensor->xclk);
1384 if (xclk_freq != 25000000) {
1385 dev_err(dev, "Unsupported clock frequency: %u\n", xclk_freq);
1389 /* Request the power down GPIO asserted. */
1390 sensor->pwdn = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_HIGH);
1391 if (IS_ERR(sensor->pwdn)) {
1392 dev_err(dev, "Failed to get 'pwdn' gpio\n");
1396 mutex_init(&sensor->lock);
1398 sensor->mode = OV5647_DEFAULT_MODE;
1400 ret = ov5647_init_controls(sensor);
1405 v4l2_i2c_subdev_init(sd, client, &ov5647_subdev_ops);
1406 sd->internal_ops = &ov5647_subdev_internal_ops;
1407 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1409 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1410 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1411 ret = media_entity_pads_init(&sd->entity, 1, &sensor->pad);
1413 goto ctrl_handler_free;
1415 ret = ov5647_power_on(dev);
1417 goto entity_cleanup;
1419 ret = ov5647_detect(sd);
1423 ret = v4l2_async_register_subdev(sd);
1427 /* Enable runtime PM and turn off the device */
1428 pm_runtime_set_active(dev);
1429 pm_runtime_enable(dev);
1430 pm_runtime_idle(dev);
1432 dev_dbg(dev, "OmniVision OV5647 camera driver probed\n");
1437 ov5647_power_off(dev);
1439 media_entity_cleanup(&sd->entity);
1441 v4l2_ctrl_handler_free(&sensor->ctrls);
1443 mutex_destroy(&sensor->lock);
1448 static int ov5647_remove(struct i2c_client *client)
1450 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1451 struct ov5647 *sensor = to_sensor(sd);
1453 v4l2_async_unregister_subdev(&sensor->sd);
1454 media_entity_cleanup(&sensor->sd.entity);
1455 v4l2_ctrl_handler_free(&sensor->ctrls);
1456 v4l2_device_unregister_subdev(sd);
1457 pm_runtime_disable(&client->dev);
1458 mutex_destroy(&sensor->lock);
1463 static const struct dev_pm_ops ov5647_pm_ops = {
1464 SET_RUNTIME_PM_OPS(ov5647_power_off, ov5647_power_on, NULL)
1467 static const struct i2c_device_id ov5647_id[] = {
1471 MODULE_DEVICE_TABLE(i2c, ov5647_id);
1473 #if IS_ENABLED(CONFIG_OF)
1474 static const struct of_device_id ov5647_of_match[] = {
1475 { .compatible = "ovti,ov5647" },
1478 MODULE_DEVICE_TABLE(of, ov5647_of_match);
1481 static struct i2c_driver ov5647_driver = {
1483 .of_match_table = of_match_ptr(ov5647_of_match),
1485 .pm = &ov5647_pm_ops,
1487 .probe_new = ov5647_probe,
1488 .remove = ov5647_remove,
1489 .id_table = ov5647_id,
1492 module_i2c_driver(ov5647_driver);
1495 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1496 MODULE_LICENSE("GPL v2");