1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the OV7251 camera sensor.
5 * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
6 * Copyright (c) 2017-2018, Linaro Ltd.
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/types.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-subdev.h>
26 #define OV7251_SC_MODE_SELECT 0x0100
27 #define OV7251_SC_MODE_SELECT_SW_STANDBY 0x0
28 #define OV7251_SC_MODE_SELECT_STREAMING 0x1
30 #define OV7251_CHIP_ID_HIGH 0x300a
31 #define OV7251_CHIP_ID_HIGH_BYTE 0x77
32 #define OV7251_CHIP_ID_LOW 0x300b
33 #define OV7251_CHIP_ID_LOW_BYTE 0x50
34 #define OV7251_SC_GP_IO_IN1 0x3029
35 #define OV7251_AEC_EXPO_0 0x3500
36 #define OV7251_AEC_EXPO_1 0x3501
37 #define OV7251_AEC_EXPO_2 0x3502
38 #define OV7251_AEC_AGC_ADJ_0 0x350a
39 #define OV7251_AEC_AGC_ADJ_1 0x350b
40 /* Exposure must be at least 20 lines shorter than VTS */
41 #define OV7251_EXPOSURE_OFFSET 20
42 /* HTS is registers 0x380c and 0x380d */
43 #define OV7251_HTS 0x3a0
44 #define OV7251_VTS_HIGH 0x380e
45 #define OV7251_VTS_LOW 0x380f
46 #define OV7251_VTS_MIN_OFFSET 92
47 #define OV7251_VTS_MAX 0x7fff
48 #define OV7251_TIMING_FORMAT1 0x3820
49 #define OV7251_TIMING_FORMAT1_VFLIP BIT(2)
50 #define OV7251_TIMING_FORMAT2 0x3821
51 #define OV7251_TIMING_FORMAT2_MIRROR BIT(2)
52 #define OV7251_PRE_ISP_00 0x5e00
53 #define OV7251_PRE_ISP_00_TEST_PATTERN BIT(7)
54 #define OV7251_PLL1_PRE_DIV_REG 0x30b4
55 #define OV7251_PLL1_MULT_REG 0x30b3
56 #define OV7251_PLL1_DIVIDER_REG 0x30b1
57 #define OV7251_PLL1_PIX_DIV_REG 0x30b0
58 #define OV7251_PLL1_MIPI_DIV_REG 0x30b5
59 #define OV7251_PLL2_PRE_DIV_REG 0x3098
60 #define OV7251_PLL2_MULT_REG 0x3099
61 #define OV7251_PLL2_DIVIDER_REG 0x309d
62 #define OV7251_PLL2_SYS_DIV_REG 0x309a
63 #define OV7251_PLL2_ADC_DIV_REG 0x309b
66 * OV7251 native and active pixel array size.
67 * Datasheet not available to confirm these values, so assume there are no
70 #define OV7251_NATIVE_WIDTH 640U
71 #define OV7251_NATIVE_HEIGHT 480U
72 #define OV7251_PIXEL_ARRAY_LEFT 0U
73 #define OV7251_PIXEL_ARRAY_TOP 0U
74 #define OV7251_PIXEL_ARRAY_WIDTH 640U
75 #define OV7251_PIXEL_ARRAY_HEIGHT 480U
77 #define OV7251_PIXEL_CLOCK 48000000
84 struct ov7251_frame_ival_info {
86 struct v4l2_fract timeperframe;
89 struct ov7251_mode_info {
92 const struct reg_value *data;
97 struct ov7251_pll1_config {
101 unsigned int pix_div;
102 unsigned int mipi_div;
105 struct ov7251_pll2_config {
106 unsigned int pre_div;
109 unsigned int sys_div;
110 unsigned int adc_div;
113 struct ov7251_pll_configs {
114 const struct ov7251_pll1_config *pll1;
115 const struct ov7251_pll2_config *pll2;
119 struct i2c_client *i2c_client;
121 struct v4l2_subdev sd;
122 struct media_pad pad;
123 struct v4l2_fwnode_endpoint ep;
124 struct v4l2_mbus_framefmt fmt;
125 struct v4l2_rect crop;
129 const struct ov7251_pll_configs *pll_configs;
131 struct regulator *io_regulator;
132 struct regulator *core_regulator;
133 struct regulator *analog_regulator;
135 const struct ov7251_mode_info *current_mode;
136 const struct ov7251_frame_ival_info *current_ival;
138 struct v4l2_ctrl_handler ctrls;
139 struct v4l2_ctrl *exposure;
140 struct v4l2_ctrl *hblank;
141 struct v4l2_ctrl *vblank;
143 /* Cached register values */
149 struct mutex lock; /* lock to protect power state, ctrls and mode */
152 struct gpio_desc *enable_gpio;
155 static inline struct ov7251 *to_ov7251(struct v4l2_subdev *sd)
157 return container_of(sd, struct ov7251, sd);
163 OV7251_NUM_SUPPORTED_RATES
166 static const struct ov7251_pll1_config ov7251_pll1_config_19_2_mhz = {
174 static const struct ov7251_pll1_config ov7251_pll1_config_24_mhz = {
182 static const struct ov7251_pll2_config ov7251_pll2_config_19_2_mhz = {
190 static const struct ov7251_pll2_config ov7251_pll2_config_24_mhz = {
198 static const struct ov7251_pll_configs ov7251_pll_configs_19_2_mhz = {
199 .pll1 = &ov7251_pll1_config_19_2_mhz,
200 .pll2 = &ov7251_pll2_config_19_2_mhz
203 static const struct ov7251_pll_configs ov7251_pll_configs_24_mhz = {
204 .pll1 = &ov7251_pll1_config_24_mhz,
205 .pll2 = &ov7251_pll2_config_24_mhz
208 static const struct ov7251_pll_configs *ov7251_pll_configs[] = {
209 [OV7251_19_2_MHZ] = &ov7251_pll_configs_19_2_mhz,
210 [OV7251_24_MHZ] = &ov7251_pll_configs_24_mhz
213 static const struct reg_value ov7251_global_init_setting[] = {
218 static const struct reg_value ov7251_setting_vga[] = {
270 { 0x3808, 0x02 }, /* width high */
271 { 0x3809, 0x80 }, /* width low */
272 { 0x380a, 0x01 }, /* height high */
273 { 0x380b, 0xe0 }, /* height low */
274 { 0x380c, 0x03 }, /* total horiz timing high */
275 { 0x380d, 0xa0 }, /* total horiz timing low */
344 static const unsigned long supported_xclk_rates[] = {
345 [OV7251_19_2_MHZ] = 19200000,
346 [OV7251_24_MHZ] = 24000000,
349 static const s64 link_freq[] = {
353 static const struct ov7251_frame_ival_info ov7251_frame_ival_info_data[] = {
377 static const struct ov7251_mode_info ov7251_mode_info_data[] = {
381 .data = ov7251_setting_vga,
382 .data_size = ARRAY_SIZE(ov7251_setting_vga),
387 static int ov7251_regulators_enable(struct ov7251 *ov7251)
391 /* OV7251 power up sequence requires core regulator
392 * to be enabled not earlier than io regulator
395 ret = regulator_enable(ov7251->io_regulator);
397 dev_err(ov7251->dev, "set io voltage failed\n");
401 ret = regulator_enable(ov7251->analog_regulator);
403 dev_err(ov7251->dev, "set analog voltage failed\n");
407 ret = regulator_enable(ov7251->core_regulator);
409 dev_err(ov7251->dev, "set core voltage failed\n");
410 goto err_disable_analog;
416 regulator_disable(ov7251->analog_regulator);
419 regulator_disable(ov7251->io_regulator);
424 static void ov7251_regulators_disable(struct ov7251 *ov7251)
428 ret = regulator_disable(ov7251->core_regulator);
430 dev_err(ov7251->dev, "core regulator disable failed\n");
432 ret = regulator_disable(ov7251->analog_regulator);
434 dev_err(ov7251->dev, "analog regulator disable failed\n");
436 ret = regulator_disable(ov7251->io_regulator);
438 dev_err(ov7251->dev, "io regulator disable failed\n");
441 static int ov7251_write_reg(struct ov7251 *ov7251, u16 reg, u8 val)
446 regbuf[0] = reg >> 8;
447 regbuf[1] = reg & 0xff;
450 ret = i2c_master_send(ov7251->i2c_client, regbuf, 3);
452 dev_err(ov7251->dev, "%s: write reg error %d: reg=%x, val=%x\n",
453 __func__, ret, reg, val);
460 static int ov7251_write_seq_regs(struct ov7251 *ov7251, u16 reg, u8 *val,
464 u8 nregbuf = sizeof(reg) + num * sizeof(*val);
467 if (nregbuf > sizeof(regbuf))
470 regbuf[0] = reg >> 8;
471 regbuf[1] = reg & 0xff;
473 memcpy(regbuf + 2, val, num);
475 ret = i2c_master_send(ov7251->i2c_client, regbuf, nregbuf);
478 "%s: write seq regs error %d: first reg=%x\n",
486 static int ov7251_read_reg(struct ov7251 *ov7251, u16 reg, u8 *val)
491 regbuf[0] = reg >> 8;
492 regbuf[1] = reg & 0xff;
494 ret = i2c_master_send(ov7251->i2c_client, regbuf, 2);
496 dev_err(ov7251->dev, "%s: write reg error %d: reg=%x\n",
501 ret = i2c_master_recv(ov7251->i2c_client, val, 1);
503 dev_err(ov7251->dev, "%s: read reg error %d: reg=%x\n",
511 static int ov7251_pll_configure(struct ov7251 *ov7251)
513 const struct ov7251_pll_configs *configs;
516 configs = ov7251->pll_configs;
518 ret = ov7251_write_reg(ov7251, OV7251_PLL1_PRE_DIV_REG,
519 configs->pll1->pre_div);
523 ret = ov7251_write_reg(ov7251, OV7251_PLL1_MULT_REG,
524 configs->pll1->mult);
527 ret = ov7251_write_reg(ov7251, OV7251_PLL1_DIVIDER_REG,
532 ret = ov7251_write_reg(ov7251, OV7251_PLL1_PIX_DIV_REG,
533 configs->pll1->pix_div);
537 ret = ov7251_write_reg(ov7251, OV7251_PLL1_MIPI_DIV_REG,
538 configs->pll1->mipi_div);
542 ret = ov7251_write_reg(ov7251, OV7251_PLL2_PRE_DIV_REG,
543 configs->pll2->pre_div);
547 ret = ov7251_write_reg(ov7251, OV7251_PLL2_MULT_REG,
548 configs->pll2->mult);
552 ret = ov7251_write_reg(ov7251, OV7251_PLL2_DIVIDER_REG,
557 ret = ov7251_write_reg(ov7251, OV7251_PLL2_SYS_DIV_REG,
558 configs->pll2->sys_div);
562 ret = ov7251_write_reg(ov7251, OV7251_PLL2_ADC_DIV_REG,
563 configs->pll2->adc_div);
568 static int ov7251_set_exposure(struct ov7251 *ov7251, s32 exposure)
573 reg = OV7251_AEC_EXPO_0;
574 val[0] = (exposure & 0xf000) >> 12; /* goes to OV7251_AEC_EXPO_0 */
575 val[1] = (exposure & 0x0ff0) >> 4; /* goes to OV7251_AEC_EXPO_1 */
576 val[2] = (exposure & 0x000f) << 4; /* goes to OV7251_AEC_EXPO_2 */
578 return ov7251_write_seq_regs(ov7251, reg, val, 3);
581 static int ov7251_set_gain(struct ov7251 *ov7251, s32 gain)
586 reg = OV7251_AEC_AGC_ADJ_0;
587 val[0] = (gain & 0x0300) >> 8; /* goes to OV7251_AEC_AGC_ADJ_0 */
588 val[1] = gain & 0xff; /* goes to OV7251_AEC_AGC_ADJ_1 */
590 return ov7251_write_seq_regs(ov7251, reg, val, 2);
593 static int ov7251_set_register_array(struct ov7251 *ov7251,
594 const struct reg_value *settings,
595 unsigned int num_settings)
600 for (i = 0; i < num_settings; ++i, ++settings) {
601 ret = ov7251_write_reg(ov7251, settings->reg, settings->val);
609 static int ov7251_set_power_on(struct ov7251 *ov7251)
614 ret = ov7251_regulators_enable(ov7251);
618 ret = clk_prepare_enable(ov7251->xclk);
620 dev_err(ov7251->dev, "clk prepare enable failed\n");
621 ov7251_regulators_disable(ov7251);
625 gpiod_set_value_cansleep(ov7251->enable_gpio, 1);
627 /* wait at least 65536 external clock cycles */
628 wait_us = DIV_ROUND_UP(65536 * 1000,
629 DIV_ROUND_UP(ov7251->xclk_freq, 1000));
630 usleep_range(wait_us, wait_us + 1000);
635 static void ov7251_set_power_off(struct ov7251 *ov7251)
637 clk_disable_unprepare(ov7251->xclk);
638 gpiod_set_value_cansleep(ov7251->enable_gpio, 0);
639 ov7251_regulators_disable(ov7251);
642 static int __maybe_unused ov7251_sensor_suspend(struct device *dev)
644 struct v4l2_subdev *sd = dev_get_drvdata(dev);
645 struct ov7251 *ov7251 = to_ov7251(sd);
647 ov7251_set_power_off(ov7251);
652 static int __maybe_unused ov7251_sensor_resume(struct device *dev)
654 struct v4l2_subdev *sd = dev_get_drvdata(dev);
655 struct ov7251 *ov7251 = to_ov7251(sd);
657 return ov7251_set_power_on(ov7251);
660 static int ov7251_set_hflip(struct ov7251 *ov7251, s32 value)
662 u8 val = ov7251->timing_format2;
666 val |= OV7251_TIMING_FORMAT2_MIRROR;
668 val &= ~OV7251_TIMING_FORMAT2_MIRROR;
670 ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT2, val);
672 ov7251->timing_format2 = val;
677 static int ov7251_set_vflip(struct ov7251 *ov7251, s32 value)
679 u8 val = ov7251->timing_format1;
683 val |= OV7251_TIMING_FORMAT1_VFLIP;
685 val &= ~OV7251_TIMING_FORMAT1_VFLIP;
687 ret = ov7251_write_reg(ov7251, OV7251_TIMING_FORMAT1, val);
689 ov7251->timing_format1 = val;
694 static int ov7251_set_vblank(struct ov7251 *ov7251, s32 value)
699 reg = OV7251_VTS_HIGH;
700 value += ov7251->current_mode->height;
701 val[0] = (value & 0xff00) >> 8; /* goes to OV7251_VTS_HIGH */
702 val[1] = value & 0xff; /* goes to OV7251_VTS_LOW */
704 return ov7251_write_seq_regs(ov7251, reg, val, 2);
707 static int ov7251_set_test_pattern(struct ov7251 *ov7251, s32 value)
709 u8 val = ov7251->pre_isp_00;
713 val |= OV7251_PRE_ISP_00_TEST_PATTERN;
715 val &= ~OV7251_PRE_ISP_00_TEST_PATTERN;
717 ret = ov7251_write_reg(ov7251, OV7251_PRE_ISP_00, val);
719 ov7251->pre_isp_00 = val;
724 static const char * const ov7251_test_pattern_menu[] = {
726 "Vertical Pattern Bars",
729 static int ov7251_s_ctrl(struct v4l2_ctrl *ctrl)
731 struct ov7251 *ov7251 = container_of(ctrl->handler,
732 struct ov7251, ctrls);
736 /* v4l2_ctrl_lock() locks our mutex */
738 case V4L2_CID_VBLANK:
739 /* Update max exposure while meeting expected vblanking */
740 max = ov7251->current_mode->height + ctrl->val - OV7251_EXPOSURE_OFFSET;
741 __v4l2_ctrl_modify_range(ov7251->exposure,
742 ov7251->exposure->minimum, max,
743 ov7251->exposure->step,
744 ov7251->exposure->default_value);
748 if (!pm_runtime_get_if_in_use(ov7251->dev))
752 case V4L2_CID_EXPOSURE:
753 ret = ov7251_set_exposure(ov7251, ctrl->val);
755 case V4L2_CID_ANALOGUE_GAIN:
756 ret = ov7251_set_gain(ov7251, ctrl->val);
758 case V4L2_CID_TEST_PATTERN:
759 ret = ov7251_set_test_pattern(ov7251, ctrl->val);
762 ret = ov7251_set_hflip(ov7251, ctrl->val);
765 ret = ov7251_set_vflip(ov7251, ctrl->val);
767 case V4L2_CID_VBLANK:
768 ret = ov7251_set_vblank(ov7251, ctrl->val);
775 pm_runtime_put(ov7251->dev);
780 static const struct v4l2_ctrl_ops ov7251_ctrl_ops = {
781 .s_ctrl = ov7251_s_ctrl,
784 static int ov7251_enum_mbus_code(struct v4l2_subdev *sd,
785 struct v4l2_subdev_state *sd_state,
786 struct v4l2_subdev_mbus_code_enum *code)
791 code->code = MEDIA_BUS_FMT_Y10_1X10;
796 static int ov7251_enum_frame_size(struct v4l2_subdev *subdev,
797 struct v4l2_subdev_state *sd_state,
798 struct v4l2_subdev_frame_size_enum *fse)
800 if (fse->code != MEDIA_BUS_FMT_Y10_1X10)
803 if (fse->index >= ARRAY_SIZE(ov7251_mode_info_data))
806 fse->min_width = ov7251_mode_info_data[fse->index].width;
807 fse->max_width = ov7251_mode_info_data[fse->index].width;
808 fse->min_height = ov7251_mode_info_data[fse->index].height;
809 fse->max_height = ov7251_mode_info_data[fse->index].height;
814 static int ov7251_enum_frame_ival(struct v4l2_subdev *subdev,
815 struct v4l2_subdev_state *sd_state,
816 struct v4l2_subdev_frame_interval_enum *fie)
818 unsigned int index = fie->index;
821 for (i = 0; i < ARRAY_SIZE(ov7251_frame_ival_info_data); i++) {
822 if (fie->width != ov7251_mode_info_data[0].width ||
823 fie->height != ov7251_mode_info_data[0].height)
827 fie->interval = ov7251_frame_ival_info_data[i].timeperframe;
835 static struct v4l2_mbus_framefmt *
836 __ov7251_get_pad_format(struct ov7251 *ov7251,
837 struct v4l2_subdev_state *sd_state,
839 enum v4l2_subdev_format_whence which)
842 case V4L2_SUBDEV_FORMAT_TRY:
843 return v4l2_subdev_get_try_format(&ov7251->sd, sd_state, pad);
844 case V4L2_SUBDEV_FORMAT_ACTIVE:
851 static int ov7251_get_format(struct v4l2_subdev *sd,
852 struct v4l2_subdev_state *sd_state,
853 struct v4l2_subdev_format *format)
855 struct ov7251 *ov7251 = to_ov7251(sd);
857 mutex_lock(&ov7251->lock);
858 format->format = *__ov7251_get_pad_format(ov7251, sd_state,
861 mutex_unlock(&ov7251->lock);
866 static struct v4l2_rect *
867 __ov7251_get_pad_crop(struct ov7251 *ov7251,
868 struct v4l2_subdev_state *sd_state,
869 unsigned int pad, enum v4l2_subdev_format_whence which)
872 case V4L2_SUBDEV_FORMAT_TRY:
873 return v4l2_subdev_get_try_crop(&ov7251->sd, sd_state, pad);
874 case V4L2_SUBDEV_FORMAT_ACTIVE:
875 return &ov7251->crop;
881 static inline u32 avg_fps(const struct v4l2_fract *t)
883 return (t->denominator + (t->numerator >> 1)) / t->numerator;
886 static const struct ov7251_frame_ival_info *
887 ov7251_find_frame_ival_by_ival(struct ov7251 *ov7251, struct v4l2_fract *timeperframe)
889 unsigned int fps_req = avg_fps(timeperframe);
890 unsigned int max_dist_match = (unsigned int) -1;
891 unsigned int i, n = 0;
893 for (i = 0; i < ARRAY_SIZE(ov7251_frame_ival_info_data); i++) {
895 unsigned int fps_tmp;
897 fps_tmp = avg_fps(&ov7251_frame_ival_info_data[i].timeperframe);
899 dist = abs(fps_req - fps_tmp);
901 if (dist < max_dist_match) {
903 max_dist_match = dist;
907 return &ov7251_frame_ival_info_data[n];
910 static int ov7251_set_format(struct v4l2_subdev *sd,
911 struct v4l2_subdev_state *sd_state,
912 struct v4l2_subdev_format *format)
914 struct ov7251 *ov7251 = to_ov7251(sd);
915 struct v4l2_mbus_framefmt *__format;
916 struct v4l2_rect *__crop;
917 const struct ov7251_mode_info *new_mode;
921 mutex_lock(&ov7251->lock);
923 __crop = __ov7251_get_pad_crop(ov7251, sd_state, format->pad,
926 new_mode = v4l2_find_nearest_size(ov7251_mode_info_data,
927 ARRAY_SIZE(ov7251_mode_info_data),
929 format->format.width, format->format.height);
931 __crop->width = new_mode->width;
932 __crop->height = new_mode->height;
934 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
935 h_blank = OV7251_HTS - new_mode->width;
936 __v4l2_ctrl_modify_range(ov7251->hblank, h_blank,
937 h_blank, 1, h_blank);
938 __v4l2_ctrl_s_ctrl(ov7251->hblank, h_blank);
940 ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
941 ov7251->current_ival->vts -
942 OV7251_EXPOSURE_OFFSET,
943 1, new_mode->exposure_def);
947 ret = __v4l2_ctrl_s_ctrl(ov7251->exposure,
948 new_mode->exposure_def);
952 ov7251->current_mode = new_mode;
955 __format = __ov7251_get_pad_format(ov7251, sd_state, format->pad,
957 __format->width = __crop->width;
958 __format->height = __crop->height;
959 __format->code = MEDIA_BUS_FMT_Y10_1X10;
960 __format->field = V4L2_FIELD_NONE;
961 __format->colorspace = V4L2_COLORSPACE_SRGB;
962 __format->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(__format->colorspace);
963 __format->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
964 __format->colorspace, __format->ycbcr_enc);
965 __format->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(__format->colorspace);
967 format->format = *__format;
970 mutex_unlock(&ov7251->lock);
975 static int ov7251_entity_init_cfg(struct v4l2_subdev *subdev,
976 struct v4l2_subdev_state *sd_state)
978 struct v4l2_subdev_format fmt = {
979 .which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
980 : V4L2_SUBDEV_FORMAT_ACTIVE,
987 ov7251_set_format(subdev, sd_state, &fmt);
992 static int ov7251_get_selection(struct v4l2_subdev *sd,
993 struct v4l2_subdev_state *sd_state,
994 struct v4l2_subdev_selection *sel)
996 struct ov7251 *ov7251 = to_ov7251(sd);
998 switch (sel->target) {
999 case V4L2_SEL_TGT_CROP:
1000 mutex_lock(&ov7251->lock);
1001 sel->r = *__ov7251_get_pad_crop(ov7251, sd_state, sel->pad,
1003 mutex_unlock(&ov7251->lock);
1007 case V4L2_SEL_TGT_NATIVE_SIZE:
1010 sel->r.width = OV7251_NATIVE_WIDTH;
1011 sel->r.height = OV7251_NATIVE_HEIGHT;
1015 case V4L2_SEL_TGT_CROP_DEFAULT:
1016 case V4L2_SEL_TGT_CROP_BOUNDS:
1017 sel->r.top = OV7251_PIXEL_ARRAY_TOP;
1018 sel->r.left = OV7251_PIXEL_ARRAY_LEFT;
1019 sel->r.width = OV7251_PIXEL_ARRAY_WIDTH;
1020 sel->r.height = OV7251_PIXEL_ARRAY_HEIGHT;
1028 static int ov7251_s_stream(struct v4l2_subdev *subdev, int enable)
1030 struct ov7251 *ov7251 = to_ov7251(subdev);
1033 mutex_lock(&ov7251->lock);
1036 ret = pm_runtime_get_sync(ov7251->dev);
1040 ret = ov7251_set_register_array(ov7251,
1041 ov7251_global_init_setting,
1042 ARRAY_SIZE(ov7251_global_init_setting));
1044 dev_err(ov7251->dev, "could not set global_init_setting\n");
1045 goto err_power_down;
1048 ret = ov7251_pll_configure(ov7251);
1050 dev_err(ov7251->dev, "error configuring PLLs\n");
1051 goto err_power_down;
1054 ret = ov7251_set_register_array(ov7251,
1055 ov7251->current_mode->data,
1056 ov7251->current_mode->data_size);
1058 dev_err(ov7251->dev, "could not set mode %dx%d\n",
1059 ov7251->current_mode->width,
1060 ov7251->current_mode->height);
1061 goto err_power_down;
1063 ret = __v4l2_ctrl_handler_setup(&ov7251->ctrls);
1065 dev_err(ov7251->dev, "could not sync v4l2 controls\n");
1066 goto err_power_down;
1068 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1069 OV7251_SC_MODE_SELECT_STREAMING);
1071 goto err_power_down;
1073 ret = ov7251_write_reg(ov7251, OV7251_SC_MODE_SELECT,
1074 OV7251_SC_MODE_SELECT_SW_STANDBY);
1075 pm_runtime_put(ov7251->dev);
1078 mutex_unlock(&ov7251->lock);
1082 pm_runtime_put_noidle(ov7251->dev);
1083 mutex_unlock(&ov7251->lock);
1087 static int ov7251_get_frame_interval(struct v4l2_subdev *subdev,
1088 struct v4l2_subdev_frame_interval *fi)
1090 struct ov7251 *ov7251 = to_ov7251(subdev);
1092 mutex_lock(&ov7251->lock);
1093 fi->interval = ov7251->current_ival->timeperframe;
1094 mutex_unlock(&ov7251->lock);
1099 static int ov7251_set_frame_interval(struct v4l2_subdev *subdev,
1100 struct v4l2_subdev_frame_interval *fi)
1102 struct ov7251 *ov7251 = to_ov7251(subdev);
1103 const struct ov7251_frame_ival_info *new_ival;
1106 mutex_lock(&ov7251->lock);
1107 new_ival = ov7251_find_frame_ival_by_ival(ov7251, &fi->interval);
1109 if (new_ival != ov7251->current_ival) {
1110 ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
1112 OV7251_EXPOSURE_OFFSET,
1113 1, ov7251->exposure->val);
1117 ret = __v4l2_ctrl_s_ctrl(ov7251->vblank,
1119 ov7251->current_mode->height);
1123 ov7251->current_ival = new_ival;
1126 fi->interval = ov7251->current_ival->timeperframe;
1129 mutex_unlock(&ov7251->lock);
1134 static const struct v4l2_subdev_video_ops ov7251_video_ops = {
1135 .s_stream = ov7251_s_stream,
1136 .g_frame_interval = ov7251_get_frame_interval,
1137 .s_frame_interval = ov7251_set_frame_interval,
1140 static const struct v4l2_subdev_pad_ops ov7251_subdev_pad_ops = {
1141 .init_cfg = ov7251_entity_init_cfg,
1142 .enum_mbus_code = ov7251_enum_mbus_code,
1143 .enum_frame_size = ov7251_enum_frame_size,
1144 .enum_frame_interval = ov7251_enum_frame_ival,
1145 .get_fmt = ov7251_get_format,
1146 .set_fmt = ov7251_set_format,
1147 .get_selection = ov7251_get_selection,
1150 static const struct v4l2_subdev_ops ov7251_subdev_ops = {
1151 .video = &ov7251_video_ops,
1152 .pad = &ov7251_subdev_pad_ops,
1155 static int ov7251_check_hwcfg(struct ov7251 *ov7251)
1157 struct fwnode_handle *fwnode = dev_fwnode(ov7251->dev);
1158 struct v4l2_fwnode_endpoint bus_cfg = {
1159 .bus_type = V4L2_MBUS_CSI2_DPHY,
1161 struct fwnode_handle *endpoint;
1166 endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
1168 return -EPROBE_DEFER; /* could be provided by cio2-bridge */
1170 ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &bus_cfg);
1171 fwnode_handle_put(endpoint);
1173 return dev_err_probe(ov7251->dev, ret,
1174 "parsing endpoint node failed\n");
1176 if (bus_cfg.bus_type != V4L2_MBUS_CSI2_DPHY) {
1178 dev_err(ov7251->dev, "invalid bus type (%u), must be (%u)\n",
1179 bus_cfg.bus_type, V4L2_MBUS_CSI2_DPHY);
1180 goto out_free_bus_cfg;
1183 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 1) {
1184 dev_err(ov7251->dev, "only a 1-lane CSI2 config is supported");
1186 goto out_free_bus_cfg;
1189 if (!bus_cfg.nr_of_link_frequencies) {
1190 dev_err(ov7251->dev, "no link frequencies defined\n");
1192 goto out_free_bus_cfg;
1196 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
1197 for (j = 0; j < ARRAY_SIZE(link_freq); j++)
1198 if (bus_cfg.link_frequencies[i] == link_freq[j]) {
1207 if (i == bus_cfg.nr_of_link_frequencies) {
1208 dev_err(ov7251->dev, "no supported link freq found\n");
1210 goto out_free_bus_cfg;
1214 v4l2_fwnode_endpoint_free(&bus_cfg);
1219 static int ov7251_detect_chip(struct ov7251 *ov7251)
1221 u8 chip_id_high, chip_id_low, chip_rev;
1224 ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_HIGH, &chip_id_high);
1225 if (ret < 0 || chip_id_high != OV7251_CHIP_ID_HIGH_BYTE)
1226 return dev_err_probe(ov7251->dev, -ENODEV,
1227 "could not read ID high\n");
1229 ret = ov7251_read_reg(ov7251, OV7251_CHIP_ID_LOW, &chip_id_low);
1230 if (ret < 0 || chip_id_low != OV7251_CHIP_ID_LOW_BYTE)
1231 return dev_err_probe(ov7251->dev, -ENODEV,
1232 "could not read ID low\n");
1234 ret = ov7251_read_reg(ov7251, OV7251_SC_GP_IO_IN1, &chip_rev);
1236 return dev_err_probe(ov7251->dev, -ENODEV,
1237 "could not read revision\n");
1240 dev_info(ov7251->dev,
1241 "OV7251 revision %x (%s) detected at address 0x%02x\n",
1243 chip_rev == 0x4 ? "1A / 1B" :
1244 chip_rev == 0x5 ? "1C / 1D" :
1245 chip_rev == 0x6 ? "1E" :
1246 chip_rev == 0x7 ? "1F" : "unknown",
1247 ov7251->i2c_client->addr);
1252 static int ov7251_probe(struct i2c_client *client)
1254 struct v4l2_fwnode_device_properties props;
1255 struct device *dev = &client->dev;
1256 struct v4l2_ctrl *ctrl;
1257 struct ov7251 *ov7251;
1258 unsigned int rate = 0;
1259 u32 h_blank, v_blank, v_blank_max;
1263 ov7251 = devm_kzalloc(dev, sizeof(struct ov7251), GFP_KERNEL);
1267 ov7251->i2c_client = client;
1270 ret = ov7251_check_hwcfg(ov7251);
1274 /* get system clock (xclk) */
1275 ov7251->xclk = devm_clk_get(dev, NULL);
1276 if (IS_ERR(ov7251->xclk)) {
1277 dev_err(dev, "could not get xclk");
1278 return PTR_ERR(ov7251->xclk);
1282 * We could have either a 24MHz or 19.2MHz clock rate from either dt or
1283 * ACPI. We also need to support the IPU3 case which will have both an
1284 * external clock AND a clock-frequency property.
1286 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1288 if (!ret && ov7251->xclk) {
1289 ret = clk_set_rate(ov7251->xclk, rate);
1291 return dev_err_probe(dev, ret,
1292 "failed to set clock rate\n");
1293 } else if (ret && !ov7251->xclk) {
1294 return dev_err_probe(dev, ret, "invalid clock config\n");
1297 ov7251->xclk_freq = rate ? rate : clk_get_rate(ov7251->xclk);
1299 for (i = 0; i < ARRAY_SIZE(supported_xclk_rates); i++)
1300 if (ov7251->xclk_freq == supported_xclk_rates[i])
1303 if (i == ARRAY_SIZE(supported_xclk_rates))
1304 return dev_err_probe(dev, -EINVAL,
1305 "clock rate %u Hz is unsupported\n",
1308 ov7251->pll_configs = ov7251_pll_configs[i];
1310 ov7251->io_regulator = devm_regulator_get(dev, "vdddo");
1311 if (IS_ERR(ov7251->io_regulator)) {
1312 dev_err(dev, "cannot get io regulator\n");
1313 return PTR_ERR(ov7251->io_regulator);
1316 ov7251->core_regulator = devm_regulator_get(dev, "vddd");
1317 if (IS_ERR(ov7251->core_regulator)) {
1318 dev_err(dev, "cannot get core regulator\n");
1319 return PTR_ERR(ov7251->core_regulator);
1322 ov7251->analog_regulator = devm_regulator_get(dev, "vdda");
1323 if (IS_ERR(ov7251->analog_regulator)) {
1324 dev_err(dev, "cannot get analog regulator\n");
1325 return PTR_ERR(ov7251->analog_regulator);
1328 ov7251->enable_gpio = devm_gpiod_get_optional(dev, "enable",
1330 if (IS_ERR(ov7251->enable_gpio)) {
1331 dev_err(dev, "cannot get enable gpio\n");
1332 return PTR_ERR(ov7251->enable_gpio);
1335 mutex_init(&ov7251->lock);
1337 ov7251->current_mode = &ov7251_mode_info_data[0];
1338 ov7251->current_ival = &ov7251_frame_ival_info_data[0];
1340 v4l2_ctrl_handler_init(&ov7251->ctrls, 11);
1341 ov7251->ctrls.lock = &ov7251->lock;
1343 v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1344 V4L2_CID_HFLIP, 0, 1, 1, 0);
1345 v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1346 V4L2_CID_VFLIP, 0, 1, 1, 0);
1347 ov7251->exposure = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1348 V4L2_CID_EXPOSURE, 1, 32, 1, 32);
1349 v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1350 V4L2_CID_ANALOGUE_GAIN, 16, 1023, 1, 16);
1351 v4l2_ctrl_new_std_menu_items(&ov7251->ctrls, &ov7251_ctrl_ops,
1352 V4L2_CID_TEST_PATTERN,
1353 ARRAY_SIZE(ov7251_test_pattern_menu) - 1,
1354 0, 0, ov7251_test_pattern_menu);
1355 v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1356 V4L2_CID_PIXEL_RATE, OV7251_PIXEL_CLOCK,
1357 OV7251_PIXEL_CLOCK, 1, OV7251_PIXEL_CLOCK);
1358 ctrl = v4l2_ctrl_new_int_menu(&ov7251->ctrls, &ov7251_ctrl_ops,
1360 ARRAY_SIZE(link_freq) - 1,
1363 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1364 h_blank = OV7251_HTS - ov7251->current_mode->width;
1365 ov7251->hblank = v4l2_ctrl_new_std(&ov7251->ctrls, NULL,
1366 V4L2_CID_HBLANK, h_blank,
1367 h_blank, 1, h_blank);
1369 ov7251->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1371 v_blank = ov7251->current_ival->vts - ov7251->current_mode->height;
1372 v_blank_max = OV7251_VTS_MAX - ov7251->current_mode->width;
1373 ov7251->vblank = v4l2_ctrl_new_std(&ov7251->ctrls, &ov7251_ctrl_ops,
1375 OV7251_VTS_MIN_OFFSET,
1376 v_blank_max, 1, v_blank);
1378 ov7251->sd.ctrl_handler = &ov7251->ctrls;
1380 if (ov7251->ctrls.error) {
1381 dev_err(dev, "%s: control initialization error %d\n",
1382 __func__, ov7251->ctrls.error);
1383 ret = ov7251->ctrls.error;
1387 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1391 ret = v4l2_ctrl_new_fwnode_properties(&ov7251->ctrls, &ov7251_ctrl_ops,
1396 v4l2_i2c_subdev_init(&ov7251->sd, client, &ov7251_subdev_ops);
1397 ov7251->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1398 ov7251->pad.flags = MEDIA_PAD_FL_SOURCE;
1399 ov7251->sd.dev = &client->dev;
1400 ov7251->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1402 ret = media_entity_pads_init(&ov7251->sd.entity, 1, &ov7251->pad);
1404 dev_err(dev, "could not register media entity\n");
1408 ret = ov7251_set_power_on(ov7251);
1412 ret = ov7251_detect_chip(ov7251);
1416 pm_runtime_set_active(&client->dev);
1417 pm_runtime_get_noresume(&client->dev);
1418 pm_runtime_enable(&client->dev);
1420 ret = ov7251_read_reg(ov7251, OV7251_PRE_ISP_00,
1421 &ov7251->pre_isp_00);
1423 dev_err(dev, "could not read test pattern value\n");
1425 goto err_pm_runtime;
1428 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT1,
1429 &ov7251->timing_format1);
1431 dev_err(dev, "could not read vflip value\n");
1433 goto err_pm_runtime;
1436 ret = ov7251_read_reg(ov7251, OV7251_TIMING_FORMAT2,
1437 &ov7251->timing_format2);
1439 dev_err(dev, "could not read hflip value\n");
1441 goto err_pm_runtime;
1444 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1445 pm_runtime_use_autosuspend(&client->dev);
1446 pm_runtime_put_autosuspend(&client->dev);
1448 ret = v4l2_async_register_subdev(&ov7251->sd);
1450 dev_err(dev, "could not register v4l2 device\n");
1454 ov7251_entity_init_cfg(&ov7251->sd, NULL);
1459 pm_runtime_disable(ov7251->dev);
1460 pm_runtime_put_noidle(ov7251->dev);
1462 ov7251_set_power_off(ov7251);
1464 media_entity_cleanup(&ov7251->sd.entity);
1466 v4l2_ctrl_handler_free(&ov7251->ctrls);
1467 mutex_destroy(&ov7251->lock);
1472 static int ov7251_remove(struct i2c_client *client)
1474 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1475 struct ov7251 *ov7251 = to_ov7251(sd);
1477 v4l2_async_unregister_subdev(&ov7251->sd);
1478 media_entity_cleanup(&ov7251->sd.entity);
1479 v4l2_ctrl_handler_free(&ov7251->ctrls);
1480 mutex_destroy(&ov7251->lock);
1482 pm_runtime_disable(ov7251->dev);
1483 if (!pm_runtime_status_suspended(ov7251->dev))
1484 ov7251_set_power_off(ov7251);
1485 pm_runtime_set_suspended(ov7251->dev);
1490 static const struct dev_pm_ops ov7251_pm_ops = {
1491 SET_RUNTIME_PM_OPS(ov7251_sensor_suspend, ov7251_sensor_resume, NULL)
1494 static const struct of_device_id ov7251_of_match[] = {
1495 { .compatible = "ovti,ov7251" },
1498 MODULE_DEVICE_TABLE(of, ov7251_of_match);
1500 static const struct acpi_device_id ov7251_acpi_match[] = {
1504 MODULE_DEVICE_TABLE(acpi, ov7251_acpi_match);
1506 static struct i2c_driver ov7251_i2c_driver = {
1508 .of_match_table = ov7251_of_match,
1509 .acpi_match_table = ov7251_acpi_match,
1511 .pm = &ov7251_pm_ops,
1513 .probe_new = ov7251_probe,
1514 .remove = ov7251_remove,
1517 module_i2c_driver(ov7251_i2c_driver);
1519 MODULE_DESCRIPTION("Omnivision OV7251 Camera Driver");
1520 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1521 MODULE_LICENSE("GPL v2");