1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/regulator/consumer.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-fwnode.h>
18 #define OV5675_REG_VALUE_08BIT 1
19 #define OV5675_REG_VALUE_16BIT 2
20 #define OV5675_REG_VALUE_24BIT 3
22 #define OV5675_LINK_FREQ_450MHZ 450000000ULL
23 #define OV5675_SCLK 90000000LL
24 #define OV5675_XVCLK_19_2 19200000
25 #define OV5675_DATA_LANES 2
26 #define OV5675_RGB_DEPTH 10
28 #define OV5675_REG_CHIP_ID 0x300a
29 #define OV5675_CHIP_ID 0x5675
31 #define OV5675_REG_MODE_SELECT 0x0100
32 #define OV5675_MODE_STANDBY 0x00
33 #define OV5675_MODE_STREAMING 0x01
35 /* vertical-timings from sensor */
36 #define OV5675_REG_VTS 0x380e
37 #define OV5675_VTS_30FPS 0x07e4
38 #define OV5675_VTS_30FPS_MIN 0x07e4
39 #define OV5675_VTS_MAX 0x7fff
41 /* horizontal-timings from sensor */
42 #define OV5675_REG_HTS 0x380c
44 /* Exposure controls from sensor */
45 #define OV5675_REG_EXPOSURE 0x3500
46 #define OV5675_EXPOSURE_MIN 4
47 #define OV5675_EXPOSURE_MAX_MARGIN 4
48 #define OV5675_EXPOSURE_STEP 1
50 /* Analog gain controls from sensor */
51 #define OV5675_REG_ANALOG_GAIN 0x3508
52 #define OV5675_ANAL_GAIN_MIN 128
53 #define OV5675_ANAL_GAIN_MAX 2047
54 #define OV5675_ANAL_GAIN_STEP 1
56 /* Digital gain controls from sensor */
57 #define OV5675_REG_DIGITAL_GAIN 0x350a
58 #define OV5675_REG_MWB_R_GAIN 0x5019
59 #define OV5675_REG_MWB_G_GAIN 0x501b
60 #define OV5675_REG_MWB_B_GAIN 0x501d
61 #define OV5675_DGTL_GAIN_MIN 1024
62 #define OV5675_DGTL_GAIN_MAX 4095
63 #define OV5675_DGTL_GAIN_STEP 1
64 #define OV5675_DGTL_GAIN_DEFAULT 1024
67 #define OV5675_REG_GROUP_ACCESS 0x3208
68 #define OV5675_GROUP_HOLD_START 0x0
69 #define OV5675_GROUP_HOLD_END 0x10
70 #define OV5675_GROUP_HOLD_LAUNCH 0xa0
72 /* Test Pattern Control */
73 #define OV5675_REG_TEST_PATTERN 0x4503
74 #define OV5675_TEST_PATTERN_ENABLE BIT(7)
75 #define OV5675_TEST_PATTERN_BAR_SHIFT 2
77 /* Flip Mirror Controls from sensor */
78 #define OV5675_REG_FORMAT1 0x3820
79 #define OV5675_REG_FORMAT2 0x373d
81 #define to_ov5675(_sd) container_of(_sd, struct ov5675, sd)
83 static const char * const ov5675_supply_names[] = {
84 "avdd", /* Analog power */
85 "dovdd", /* Digital I/O power */
86 "dvdd", /* Digital core power */
89 #define OV5675_NUM_SUPPLIES ARRAY_SIZE(ov5675_supply_names)
92 OV5675_LINK_FREQ_900MBPS,
100 struct ov5675_reg_list {
102 const struct ov5675_reg *regs;
105 struct ov5675_link_freq_config {
106 const struct ov5675_reg_list reg_list;
110 /* Frame width in pixels */
113 /* Frame height in pixels */
116 /* Horizontal timining size */
119 /* Default vertical timining size */
122 /* Min vertical timining size */
125 /* Link frequency needed for this resolution */
128 /* Sensor register settings for this resolution */
129 const struct ov5675_reg_list reg_list;
132 static const struct ov5675_reg mipi_data_rate_900mbps[] = {
141 static const struct ov5675_reg mode_2592x1944_regs[] = {
294 static const struct ov5675_reg mode_1296x972_regs[] = {
447 static const char * const ov5675_test_pattern_menu[] = {
449 "Standard Color Bar",
450 "Top-Bottom Darker Color Bar",
451 "Right-Left Darker Color Bar",
452 "Bottom-Top Darker Color Bar"
455 static const s64 link_freq_menu_items[] = {
456 OV5675_LINK_FREQ_450MHZ,
459 static const struct ov5675_link_freq_config link_freq_configs[] = {
460 [OV5675_LINK_FREQ_900MBPS] = {
462 .num_of_regs = ARRAY_SIZE(mipi_data_rate_900mbps),
463 .regs = mipi_data_rate_900mbps,
468 static const struct ov5675_mode supported_modes[] = {
473 .vts_def = OV5675_VTS_30FPS,
474 .vts_min = OV5675_VTS_30FPS_MIN,
476 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
477 .regs = mode_2592x1944_regs,
479 .link_freq_index = OV5675_LINK_FREQ_900MBPS,
485 .vts_def = OV5675_VTS_30FPS,
486 .vts_min = OV5675_VTS_30FPS_MIN,
488 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
489 .regs = mode_1296x972_regs,
491 .link_freq_index = OV5675_LINK_FREQ_900MBPS,
496 struct v4l2_subdev sd;
497 struct media_pad pad;
498 struct v4l2_ctrl_handler ctrl_handler;
500 struct gpio_desc *reset_gpio;
501 struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES];
504 struct v4l2_ctrl *link_freq;
505 struct v4l2_ctrl *pixel_rate;
506 struct v4l2_ctrl *vblank;
507 struct v4l2_ctrl *hblank;
508 struct v4l2_ctrl *exposure;
511 const struct ov5675_mode *cur_mode;
513 /* To serialize asynchronus callbacks */
516 /* Streaming on/off */
519 /* True if the device has been identified */
523 static u64 to_pixel_rate(u32 f_index)
525 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV5675_DATA_LANES;
527 do_div(pixel_rate, OV5675_RGB_DEPTH);
532 static u64 to_pixels_per_line(u32 hts, u32 f_index)
534 u64 ppl = hts * to_pixel_rate(f_index);
536 do_div(ppl, OV5675_SCLK);
541 static int ov5675_read_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 *val)
543 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
544 struct i2c_msg msgs[2];
546 u8 data_buf[4] = {0};
552 put_unaligned_be16(reg, addr_buf);
553 msgs[0].addr = client->addr;
555 msgs[0].len = sizeof(addr_buf);
556 msgs[0].buf = addr_buf;
557 msgs[1].addr = client->addr;
558 msgs[1].flags = I2C_M_RD;
560 msgs[1].buf = &data_buf[4 - len];
562 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
563 if (ret != ARRAY_SIZE(msgs))
566 *val = get_unaligned_be32(data_buf);
571 static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)
573 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
579 put_unaligned_be16(reg, buf);
580 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
581 if (i2c_master_send(client, buf, len + 2) != len + 2)
587 static int ov5675_write_reg_list(struct ov5675 *ov5675,
588 const struct ov5675_reg_list *r_list)
590 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
594 for (i = 0; i < r_list->num_of_regs; i++) {
595 ret = ov5675_write_reg(ov5675, r_list->regs[i].address, 1,
596 r_list->regs[i].val);
598 dev_err_ratelimited(&client->dev,
599 "failed to write reg 0x%4.4x. error = %d",
600 r_list->regs[i].address, ret);
608 static int ov5675_update_digital_gain(struct ov5675 *ov5675, u32 d_gain)
612 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
613 OV5675_REG_VALUE_08BIT,
614 OV5675_GROUP_HOLD_START);
618 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_R_GAIN,
619 OV5675_REG_VALUE_16BIT, d_gain);
623 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_G_GAIN,
624 OV5675_REG_VALUE_16BIT, d_gain);
628 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_B_GAIN,
629 OV5675_REG_VALUE_16BIT, d_gain);
633 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
634 OV5675_REG_VALUE_08BIT,
635 OV5675_GROUP_HOLD_END);
639 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
640 OV5675_REG_VALUE_08BIT,
641 OV5675_GROUP_HOLD_LAUNCH);
645 static int ov5675_test_pattern(struct ov5675 *ov5675, u32 pattern)
648 pattern = (pattern - 1) << OV5675_TEST_PATTERN_BAR_SHIFT |
649 OV5675_TEST_PATTERN_ENABLE;
651 return ov5675_write_reg(ov5675, OV5675_REG_TEST_PATTERN,
652 OV5675_REG_VALUE_08BIT, pattern);
656 * OV5675 supports keeping the pixel order by mirror and flip function
657 * The Bayer order isn't affected by the flip controls
659 static int ov5675_set_ctrl_hflip(struct ov5675 *ov5675, u32 ctrl_val)
664 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
665 OV5675_REG_VALUE_08BIT, &val);
669 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
670 OV5675_REG_VALUE_08BIT,
671 ctrl_val ? val & ~BIT(3) : val | BIT(3));
674 static int ov5675_set_ctrl_vflip(struct ov5675 *ov5675, u8 ctrl_val)
679 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
680 OV5675_REG_VALUE_08BIT, &val);
684 ret = ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
685 OV5675_REG_VALUE_08BIT,
686 ctrl_val ? val | BIT(4) | BIT(5) : val & ~BIT(4) & ~BIT(5));
691 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT2,
692 OV5675_REG_VALUE_08BIT, &val);
697 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT2,
698 OV5675_REG_VALUE_08BIT,
699 ctrl_val ? val | BIT(1) : val & ~BIT(1));
702 static int ov5675_set_ctrl(struct v4l2_ctrl *ctrl)
704 struct ov5675 *ov5675 = container_of(ctrl->handler,
705 struct ov5675, ctrl_handler);
706 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
710 /* Propagate change of current control to all related controls */
711 if (ctrl->id == V4L2_CID_VBLANK) {
712 /* Update max exposure while meeting expected vblanking */
713 exposure_max = ov5675->cur_mode->height + ctrl->val -
714 OV5675_EXPOSURE_MAX_MARGIN;
715 __v4l2_ctrl_modify_range(ov5675->exposure,
716 ov5675->exposure->minimum,
717 exposure_max, ov5675->exposure->step,
721 /* V4L2 controls values will be applied only when power is already up */
722 if (!pm_runtime_get_if_in_use(&client->dev))
726 case V4L2_CID_ANALOGUE_GAIN:
727 ret = ov5675_write_reg(ov5675, OV5675_REG_ANALOG_GAIN,
728 OV5675_REG_VALUE_16BIT, ctrl->val);
731 case V4L2_CID_DIGITAL_GAIN:
732 ret = ov5675_update_digital_gain(ov5675, ctrl->val);
735 case V4L2_CID_EXPOSURE:
736 /* 4 least significant bits of expsoure are fractional part
738 * for ov5675, the unit of exposure is differnt from other
739 * OmniVision sensors, its exposure value is twice of the
740 * register value, the exposure should be divided by 2 before
741 * set register, e.g. val << 3.
743 ret = ov5675_write_reg(ov5675, OV5675_REG_EXPOSURE,
744 OV5675_REG_VALUE_24BIT, ctrl->val << 3);
747 case V4L2_CID_VBLANK:
748 ret = ov5675_write_reg(ov5675, OV5675_REG_VTS,
749 OV5675_REG_VALUE_16BIT,
750 ov5675->cur_mode->height + ctrl->val +
754 case V4L2_CID_TEST_PATTERN:
755 ret = ov5675_test_pattern(ov5675, ctrl->val);
759 ov5675_set_ctrl_hflip(ov5675, ctrl->val);
763 ov5675_set_ctrl_vflip(ov5675, ctrl->val);
771 pm_runtime_put(&client->dev);
776 static const struct v4l2_ctrl_ops ov5675_ctrl_ops = {
777 .s_ctrl = ov5675_set_ctrl,
780 static int ov5675_init_controls(struct ov5675 *ov5675)
782 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
783 struct v4l2_fwnode_device_properties props;
784 struct v4l2_ctrl_handler *ctrl_hdlr;
785 s64 exposure_max, h_blank;
788 ctrl_hdlr = &ov5675->ctrl_handler;
789 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
793 ctrl_hdlr->lock = &ov5675->mutex;
794 ov5675->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov5675_ctrl_ops,
796 ARRAY_SIZE(link_freq_menu_items) - 1,
797 0, link_freq_menu_items);
798 if (ov5675->link_freq)
799 ov5675->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
801 ov5675->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
802 V4L2_CID_PIXEL_RATE, 0,
803 to_pixel_rate(OV5675_LINK_FREQ_900MBPS),
805 to_pixel_rate(OV5675_LINK_FREQ_900MBPS));
806 ov5675->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
808 ov5675->cur_mode->vts_min - ov5675->cur_mode->height,
809 OV5675_VTS_MAX - ov5675->cur_mode->height, 1,
810 ov5675->cur_mode->vts_def - ov5675->cur_mode->height);
811 h_blank = to_pixels_per_line(ov5675->cur_mode->hts,
812 ov5675->cur_mode->link_freq_index) - ov5675->cur_mode->width;
813 ov5675->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
814 V4L2_CID_HBLANK, h_blank, h_blank, 1,
817 ov5675->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
819 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
820 OV5675_ANAL_GAIN_MIN, OV5675_ANAL_GAIN_MAX,
821 OV5675_ANAL_GAIN_STEP, OV5675_ANAL_GAIN_MIN);
822 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
823 OV5675_DGTL_GAIN_MIN, OV5675_DGTL_GAIN_MAX,
824 OV5675_DGTL_GAIN_STEP, OV5675_DGTL_GAIN_DEFAULT);
825 exposure_max = (ov5675->cur_mode->vts_def - OV5675_EXPOSURE_MAX_MARGIN);
826 ov5675->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
828 OV5675_EXPOSURE_MIN, exposure_max,
829 OV5675_EXPOSURE_STEP,
831 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5675_ctrl_ops,
832 V4L2_CID_TEST_PATTERN,
833 ARRAY_SIZE(ov5675_test_pattern_menu) - 1,
834 0, 0, ov5675_test_pattern_menu);
835 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
836 V4L2_CID_HFLIP, 0, 1, 1, 0);
837 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
838 V4L2_CID_VFLIP, 0, 1, 1, 0);
840 if (ctrl_hdlr->error) {
841 v4l2_ctrl_handler_free(ctrl_hdlr);
842 return ctrl_hdlr->error;
845 ret = v4l2_fwnode_device_parse(&client->dev, &props);
849 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5675_ctrl_ops,
854 ov5675->sd.ctrl_handler = ctrl_hdlr;
859 v4l2_ctrl_handler_free(ctrl_hdlr);
864 static void ov5675_update_pad_format(const struct ov5675_mode *mode,
865 struct v4l2_mbus_framefmt *fmt)
867 fmt->width = mode->width;
868 fmt->height = mode->height;
869 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
870 fmt->field = V4L2_FIELD_NONE;
873 static int ov5675_identify_module(struct ov5675 *ov5675)
875 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
879 if (ov5675->identified)
882 ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID,
883 OV5675_REG_VALUE_24BIT, &val);
887 if (val != OV5675_CHIP_ID) {
888 dev_err(&client->dev, "chip id mismatch: %x!=%x",
889 OV5675_CHIP_ID, val);
893 ov5675->identified = true;
898 static int ov5675_start_streaming(struct ov5675 *ov5675)
900 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
901 const struct ov5675_reg_list *reg_list;
902 int link_freq_index, ret;
904 ret = ov5675_identify_module(ov5675);
908 link_freq_index = ov5675->cur_mode->link_freq_index;
909 reg_list = &link_freq_configs[link_freq_index].reg_list;
910 ret = ov5675_write_reg_list(ov5675, reg_list);
912 dev_err(&client->dev, "failed to set plls");
916 reg_list = &ov5675->cur_mode->reg_list;
917 ret = ov5675_write_reg_list(ov5675, reg_list);
919 dev_err(&client->dev, "failed to set mode");
923 ret = __v4l2_ctrl_handler_setup(ov5675->sd.ctrl_handler);
927 ret = ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
928 OV5675_REG_VALUE_08BIT, OV5675_MODE_STREAMING);
930 dev_err(&client->dev, "failed to set stream");
937 static void ov5675_stop_streaming(struct ov5675 *ov5675)
939 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
941 if (ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
942 OV5675_REG_VALUE_08BIT, OV5675_MODE_STANDBY))
943 dev_err(&client->dev, "failed to set stream");
946 static int ov5675_set_stream(struct v4l2_subdev *sd, int enable)
948 struct ov5675 *ov5675 = to_ov5675(sd);
949 struct i2c_client *client = v4l2_get_subdevdata(sd);
952 if (ov5675->streaming == enable)
955 mutex_lock(&ov5675->mutex);
957 ret = pm_runtime_resume_and_get(&client->dev);
959 mutex_unlock(&ov5675->mutex);
963 ret = ov5675_start_streaming(ov5675);
966 ov5675_stop_streaming(ov5675);
967 pm_runtime_put(&client->dev);
970 ov5675_stop_streaming(ov5675);
971 pm_runtime_put(&client->dev);
974 ov5675->streaming = enable;
975 mutex_unlock(&ov5675->mutex);
980 static int ov5675_power_off(struct device *dev)
982 /* 512 xvclk cycles after the last SCCB transation or MIPI frame end */
983 u32 delay_us = DIV_ROUND_UP(512, OV5675_XVCLK_19_2 / 1000 / 1000);
984 struct v4l2_subdev *sd = dev_get_drvdata(dev);
985 struct ov5675 *ov5675 = to_ov5675(sd);
987 usleep_range(delay_us, delay_us * 2);
989 clk_disable_unprepare(ov5675->xvclk);
990 gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
991 regulator_bulk_disable(OV5675_NUM_SUPPLIES, ov5675->supplies);
996 static int ov5675_power_on(struct device *dev)
998 u32 delay_us = DIV_ROUND_UP(8192, OV5675_XVCLK_19_2 / 1000 / 1000);
999 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1000 struct ov5675 *ov5675 = to_ov5675(sd);
1003 ret = clk_prepare_enable(ov5675->xvclk);
1005 dev_err(dev, "failed to enable xvclk: %d\n", ret);
1009 gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
1011 ret = regulator_bulk_enable(OV5675_NUM_SUPPLIES, ov5675->supplies);
1013 clk_disable_unprepare(ov5675->xvclk);
1017 /* Reset pulse should be at least 2ms and reset gpio released only once
1018 * regulators are stable.
1020 usleep_range(2000, 2200);
1022 gpiod_set_value_cansleep(ov5675->reset_gpio, 0);
1024 /* 8192 xvclk cycles prior to the first SCCB transation */
1025 usleep_range(delay_us, delay_us * 2);
1030 static int __maybe_unused ov5675_suspend(struct device *dev)
1032 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1033 struct ov5675 *ov5675 = to_ov5675(sd);
1035 mutex_lock(&ov5675->mutex);
1036 if (ov5675->streaming)
1037 ov5675_stop_streaming(ov5675);
1039 mutex_unlock(&ov5675->mutex);
1044 static int __maybe_unused ov5675_resume(struct device *dev)
1046 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1047 struct ov5675 *ov5675 = to_ov5675(sd);
1050 mutex_lock(&ov5675->mutex);
1051 if (ov5675->streaming) {
1052 ret = ov5675_start_streaming(ov5675);
1054 ov5675->streaming = false;
1055 ov5675_stop_streaming(ov5675);
1056 mutex_unlock(&ov5675->mutex);
1061 mutex_unlock(&ov5675->mutex);
1066 static int ov5675_set_format(struct v4l2_subdev *sd,
1067 struct v4l2_subdev_state *sd_state,
1068 struct v4l2_subdev_format *fmt)
1070 struct ov5675 *ov5675 = to_ov5675(sd);
1071 const struct ov5675_mode *mode;
1072 s32 vblank_def, h_blank;
1074 mode = v4l2_find_nearest_size(supported_modes,
1075 ARRAY_SIZE(supported_modes), width,
1076 height, fmt->format.width,
1077 fmt->format.height);
1079 mutex_lock(&ov5675->mutex);
1080 ov5675_update_pad_format(mode, &fmt->format);
1081 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1082 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
1084 ov5675->cur_mode = mode;
1085 __v4l2_ctrl_s_ctrl(ov5675->link_freq, mode->link_freq_index);
1086 __v4l2_ctrl_s_ctrl_int64(ov5675->pixel_rate,
1087 to_pixel_rate(mode->link_freq_index));
1089 /* Update limits and set FPS to default */
1090 vblank_def = mode->vts_def - mode->height;
1091 __v4l2_ctrl_modify_range(ov5675->vblank,
1092 mode->vts_min - mode->height,
1093 OV5675_VTS_MAX - mode->height, 1,
1095 __v4l2_ctrl_s_ctrl(ov5675->vblank, vblank_def);
1096 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1098 __v4l2_ctrl_modify_range(ov5675->hblank, h_blank, h_blank, 1,
1102 mutex_unlock(&ov5675->mutex);
1107 static int ov5675_get_format(struct v4l2_subdev *sd,
1108 struct v4l2_subdev_state *sd_state,
1109 struct v4l2_subdev_format *fmt)
1111 struct ov5675 *ov5675 = to_ov5675(sd);
1113 mutex_lock(&ov5675->mutex);
1114 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1115 fmt->format = *v4l2_subdev_get_try_format(&ov5675->sd,
1119 ov5675_update_pad_format(ov5675->cur_mode, &fmt->format);
1121 mutex_unlock(&ov5675->mutex);
1126 static int ov5675_get_selection(struct v4l2_subdev *sd,
1127 struct v4l2_subdev_state *state,
1128 struct v4l2_subdev_selection *sel)
1130 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1133 switch (sel->target) {
1134 case V4L2_SEL_TGT_CROP_BOUNDS:
1137 sel->r.width = 2624;
1138 sel->r.height = 2000;
1140 case V4L2_SEL_TGT_CROP:
1141 case V4L2_SEL_TGT_CROP_DEFAULT:
1144 sel->r.width = 2592;
1145 sel->r.height = 1944;
1151 static int ov5675_enum_mbus_code(struct v4l2_subdev *sd,
1152 struct v4l2_subdev_state *sd_state,
1153 struct v4l2_subdev_mbus_code_enum *code)
1155 if (code->index > 0)
1158 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1163 static int ov5675_enum_frame_size(struct v4l2_subdev *sd,
1164 struct v4l2_subdev_state *sd_state,
1165 struct v4l2_subdev_frame_size_enum *fse)
1167 if (fse->index >= ARRAY_SIZE(supported_modes))
1170 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1173 fse->min_width = supported_modes[fse->index].width;
1174 fse->max_width = fse->min_width;
1175 fse->min_height = supported_modes[fse->index].height;
1176 fse->max_height = fse->min_height;
1181 static int ov5675_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1183 struct ov5675 *ov5675 = to_ov5675(sd);
1185 mutex_lock(&ov5675->mutex);
1186 ov5675_update_pad_format(&supported_modes[0],
1187 v4l2_subdev_get_try_format(sd, fh->state, 0));
1188 mutex_unlock(&ov5675->mutex);
1193 static const struct v4l2_subdev_video_ops ov5675_video_ops = {
1194 .s_stream = ov5675_set_stream,
1197 static const struct v4l2_subdev_pad_ops ov5675_pad_ops = {
1198 .set_fmt = ov5675_set_format,
1199 .get_fmt = ov5675_get_format,
1200 .get_selection = ov5675_get_selection,
1201 .enum_mbus_code = ov5675_enum_mbus_code,
1202 .enum_frame_size = ov5675_enum_frame_size,
1205 static const struct v4l2_subdev_ops ov5675_subdev_ops = {
1206 .video = &ov5675_video_ops,
1207 .pad = &ov5675_pad_ops,
1210 static const struct media_entity_operations ov5675_subdev_entity_ops = {
1211 .link_validate = v4l2_subdev_link_validate,
1214 static const struct v4l2_subdev_internal_ops ov5675_internal_ops = {
1215 .open = ov5675_open,
1218 static int ov5675_get_hwcfg(struct ov5675 *ov5675, struct device *dev)
1220 struct fwnode_handle *ep;
1221 struct fwnode_handle *fwnode = dev_fwnode(dev);
1222 struct v4l2_fwnode_endpoint bus_cfg = {
1223 .bus_type = V4L2_MBUS_CSI2_DPHY
1232 ov5675->xvclk = devm_clk_get_optional(dev, NULL);
1233 if (IS_ERR(ov5675->xvclk))
1234 return dev_err_probe(dev, PTR_ERR(ov5675->xvclk),
1235 "failed to get xvclk: %ld\n",
1236 PTR_ERR(ov5675->xvclk));
1238 if (ov5675->xvclk) {
1239 xvclk_rate = clk_get_rate(ov5675->xvclk);
1241 ret = fwnode_property_read_u32(fwnode, "clock-frequency",
1245 dev_err(dev, "can't get clock frequency");
1250 if (xvclk_rate != OV5675_XVCLK_19_2) {
1251 dev_err(dev, "external clock rate %u is unsupported",
1256 ov5675->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1258 if (IS_ERR(ov5675->reset_gpio)) {
1259 ret = PTR_ERR(ov5675->reset_gpio);
1260 dev_err(dev, "failed to get reset-gpios: %d\n", ret);
1264 for (i = 0; i < OV5675_NUM_SUPPLIES; i++)
1265 ov5675->supplies[i].supply = ov5675_supply_names[i];
1267 ret = devm_regulator_bulk_get(dev, OV5675_NUM_SUPPLIES,
1272 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1276 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1277 fwnode_handle_put(ep);
1281 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV5675_DATA_LANES) {
1282 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1283 bus_cfg.bus.mipi_csi2.num_data_lanes);
1285 goto check_hwcfg_error;
1288 if (!bus_cfg.nr_of_link_frequencies) {
1289 dev_err(dev, "no link frequencies defined");
1291 goto check_hwcfg_error;
1294 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1295 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1296 if (link_freq_menu_items[i] ==
1297 bus_cfg.link_frequencies[j])
1301 if (j == bus_cfg.nr_of_link_frequencies) {
1302 dev_err(dev, "no link frequency %lld supported",
1303 link_freq_menu_items[i]);
1305 goto check_hwcfg_error;
1310 v4l2_fwnode_endpoint_free(&bus_cfg);
1315 static void ov5675_remove(struct i2c_client *client)
1317 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1318 struct ov5675 *ov5675 = to_ov5675(sd);
1320 v4l2_async_unregister_subdev(sd);
1321 media_entity_cleanup(&sd->entity);
1322 v4l2_ctrl_handler_free(sd->ctrl_handler);
1323 pm_runtime_disable(&client->dev);
1324 mutex_destroy(&ov5675->mutex);
1326 if (!pm_runtime_status_suspended(&client->dev))
1327 ov5675_power_off(&client->dev);
1328 pm_runtime_set_suspended(&client->dev);
1331 static int ov5675_probe(struct i2c_client *client)
1333 struct ov5675 *ov5675;
1337 ov5675 = devm_kzalloc(&client->dev, sizeof(*ov5675), GFP_KERNEL);
1341 ret = ov5675_get_hwcfg(ov5675, &client->dev);
1343 dev_err(&client->dev, "failed to get HW configuration: %d",
1348 v4l2_i2c_subdev_init(&ov5675->sd, client, &ov5675_subdev_ops);
1350 ret = ov5675_power_on(&client->dev);
1352 dev_err(&client->dev, "failed to power on: %d\n", ret);
1356 full_power = acpi_dev_state_d0(&client->dev);
1358 ret = ov5675_identify_module(ov5675);
1360 dev_err(&client->dev, "failed to find sensor: %d", ret);
1361 goto probe_power_off;
1365 mutex_init(&ov5675->mutex);
1366 ov5675->cur_mode = &supported_modes[0];
1367 ret = ov5675_init_controls(ov5675);
1369 dev_err(&client->dev, "failed to init controls: %d", ret);
1370 goto probe_error_v4l2_ctrl_handler_free;
1373 ov5675->sd.internal_ops = &ov5675_internal_ops;
1374 ov5675->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1375 ov5675->sd.entity.ops = &ov5675_subdev_entity_ops;
1376 ov5675->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1377 ov5675->pad.flags = MEDIA_PAD_FL_SOURCE;
1378 ret = media_entity_pads_init(&ov5675->sd.entity, 1, &ov5675->pad);
1380 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1381 goto probe_error_v4l2_ctrl_handler_free;
1384 ret = v4l2_async_register_subdev_sensor(&ov5675->sd);
1386 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1388 goto probe_error_media_entity_cleanup;
1391 /* Set the device's state to active if it's in D0 state. */
1393 pm_runtime_set_active(&client->dev);
1394 pm_runtime_enable(&client->dev);
1395 pm_runtime_idle(&client->dev);
1399 probe_error_media_entity_cleanup:
1400 media_entity_cleanup(&ov5675->sd.entity);
1402 probe_error_v4l2_ctrl_handler_free:
1403 v4l2_ctrl_handler_free(ov5675->sd.ctrl_handler);
1404 mutex_destroy(&ov5675->mutex);
1406 ov5675_power_off(&client->dev);
1411 static const struct dev_pm_ops ov5675_pm_ops = {
1412 SET_SYSTEM_SLEEP_PM_OPS(ov5675_suspend, ov5675_resume)
1413 SET_RUNTIME_PM_OPS(ov5675_power_off, ov5675_power_on, NULL)
1417 static const struct acpi_device_id ov5675_acpi_ids[] = {
1422 MODULE_DEVICE_TABLE(acpi, ov5675_acpi_ids);
1425 static const struct of_device_id ov5675_of_match[] = {
1426 { .compatible = "ovti,ov5675", },
1429 MODULE_DEVICE_TABLE(of, ov5675_of_match);
1431 static struct i2c_driver ov5675_i2c_driver = {
1434 .pm = &ov5675_pm_ops,
1435 .acpi_match_table = ACPI_PTR(ov5675_acpi_ids),
1436 .of_match_table = ov5675_of_match,
1438 .probe_new = ov5675_probe,
1439 .remove = ov5675_remove,
1440 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1443 module_i2c_driver(ov5675_i2c_driver);
1446 MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
1447 MODULE_LICENSE("GPL v2");