1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <linux/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 asynchronous callbacks */
516 /* True if the device has been identified */
520 static u64 to_pixel_rate(u32 f_index)
522 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV5675_DATA_LANES;
524 do_div(pixel_rate, OV5675_RGB_DEPTH);
529 static u64 to_pixels_per_line(u32 hts, u32 f_index)
531 u64 ppl = hts * to_pixel_rate(f_index);
533 do_div(ppl, OV5675_SCLK);
538 static int ov5675_read_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 *val)
540 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
541 struct i2c_msg msgs[2];
543 u8 data_buf[4] = {0};
549 put_unaligned_be16(reg, addr_buf);
550 msgs[0].addr = client->addr;
552 msgs[0].len = sizeof(addr_buf);
553 msgs[0].buf = addr_buf;
554 msgs[1].addr = client->addr;
555 msgs[1].flags = I2C_M_RD;
557 msgs[1].buf = &data_buf[4 - len];
559 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
560 if (ret != ARRAY_SIZE(msgs))
563 *val = get_unaligned_be32(data_buf);
568 static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)
570 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
576 put_unaligned_be16(reg, buf);
577 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
578 if (i2c_master_send(client, buf, len + 2) != len + 2)
584 static int ov5675_write_reg_list(struct ov5675 *ov5675,
585 const struct ov5675_reg_list *r_list)
587 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
591 for (i = 0; i < r_list->num_of_regs; i++) {
592 ret = ov5675_write_reg(ov5675, r_list->regs[i].address, 1,
593 r_list->regs[i].val);
595 dev_err_ratelimited(&client->dev,
596 "failed to write reg 0x%4.4x. error = %d",
597 r_list->regs[i].address, ret);
605 static int ov5675_update_digital_gain(struct ov5675 *ov5675, u32 d_gain)
609 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
610 OV5675_REG_VALUE_08BIT,
611 OV5675_GROUP_HOLD_START);
615 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_R_GAIN,
616 OV5675_REG_VALUE_16BIT, d_gain);
620 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_G_GAIN,
621 OV5675_REG_VALUE_16BIT, d_gain);
625 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_B_GAIN,
626 OV5675_REG_VALUE_16BIT, d_gain);
630 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
631 OV5675_REG_VALUE_08BIT,
632 OV5675_GROUP_HOLD_END);
636 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
637 OV5675_REG_VALUE_08BIT,
638 OV5675_GROUP_HOLD_LAUNCH);
642 static int ov5675_test_pattern(struct ov5675 *ov5675, u32 pattern)
645 pattern = (pattern - 1) << OV5675_TEST_PATTERN_BAR_SHIFT |
646 OV5675_TEST_PATTERN_ENABLE;
648 return ov5675_write_reg(ov5675, OV5675_REG_TEST_PATTERN,
649 OV5675_REG_VALUE_08BIT, pattern);
653 * OV5675 supports keeping the pixel order by mirror and flip function
654 * The Bayer order isn't affected by the flip controls
656 static int ov5675_set_ctrl_hflip(struct ov5675 *ov5675, u32 ctrl_val)
661 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
662 OV5675_REG_VALUE_08BIT, &val);
666 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
667 OV5675_REG_VALUE_08BIT,
668 ctrl_val ? val & ~BIT(3) : val | BIT(3));
671 static int ov5675_set_ctrl_vflip(struct ov5675 *ov5675, u8 ctrl_val)
676 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
677 OV5675_REG_VALUE_08BIT, &val);
681 ret = ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
682 OV5675_REG_VALUE_08BIT,
683 ctrl_val ? val | BIT(4) | BIT(5) : val & ~BIT(4) & ~BIT(5));
688 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT2,
689 OV5675_REG_VALUE_08BIT, &val);
694 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT2,
695 OV5675_REG_VALUE_08BIT,
696 ctrl_val ? val | BIT(1) : val & ~BIT(1));
699 static int ov5675_set_ctrl(struct v4l2_ctrl *ctrl)
701 struct ov5675 *ov5675 = container_of(ctrl->handler,
702 struct ov5675, ctrl_handler);
703 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
707 /* Propagate change of current control to all related controls */
708 if (ctrl->id == V4L2_CID_VBLANK) {
709 /* Update max exposure while meeting expected vblanking */
710 exposure_max = ov5675->cur_mode->height + ctrl->val -
711 OV5675_EXPOSURE_MAX_MARGIN;
712 __v4l2_ctrl_modify_range(ov5675->exposure,
713 ov5675->exposure->minimum,
714 exposure_max, ov5675->exposure->step,
718 /* V4L2 controls values will be applied only when power is already up */
719 if (!pm_runtime_get_if_in_use(&client->dev))
723 case V4L2_CID_ANALOGUE_GAIN:
724 ret = ov5675_write_reg(ov5675, OV5675_REG_ANALOG_GAIN,
725 OV5675_REG_VALUE_16BIT, ctrl->val);
728 case V4L2_CID_DIGITAL_GAIN:
729 ret = ov5675_update_digital_gain(ov5675, ctrl->val);
732 case V4L2_CID_EXPOSURE:
733 /* 4 least significant bits of expsoure are fractional part
735 * for ov5675, the unit of exposure is different from other
736 * OmniVision sensors, its exposure value is twice of the
737 * register value, the exposure should be divided by 2 before
738 * set register, e.g. val << 3.
740 ret = ov5675_write_reg(ov5675, OV5675_REG_EXPOSURE,
741 OV5675_REG_VALUE_24BIT, ctrl->val << 3);
744 case V4L2_CID_VBLANK:
745 ret = ov5675_write_reg(ov5675, OV5675_REG_VTS,
746 OV5675_REG_VALUE_16BIT,
747 ov5675->cur_mode->height + ctrl->val +
751 case V4L2_CID_TEST_PATTERN:
752 ret = ov5675_test_pattern(ov5675, ctrl->val);
756 ov5675_set_ctrl_hflip(ov5675, ctrl->val);
760 ov5675_set_ctrl_vflip(ov5675, ctrl->val);
768 pm_runtime_put(&client->dev);
773 static const struct v4l2_ctrl_ops ov5675_ctrl_ops = {
774 .s_ctrl = ov5675_set_ctrl,
777 static int ov5675_init_controls(struct ov5675 *ov5675)
779 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
780 struct v4l2_fwnode_device_properties props;
781 struct v4l2_ctrl_handler *ctrl_hdlr;
782 s64 exposure_max, h_blank;
785 ctrl_hdlr = &ov5675->ctrl_handler;
786 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
790 ctrl_hdlr->lock = &ov5675->mutex;
791 ov5675->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov5675_ctrl_ops,
793 ARRAY_SIZE(link_freq_menu_items) - 1,
794 0, link_freq_menu_items);
795 if (ov5675->link_freq)
796 ov5675->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
798 ov5675->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
799 V4L2_CID_PIXEL_RATE, 0,
800 to_pixel_rate(OV5675_LINK_FREQ_900MBPS),
802 to_pixel_rate(OV5675_LINK_FREQ_900MBPS));
803 ov5675->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
805 ov5675->cur_mode->vts_min - ov5675->cur_mode->height,
806 OV5675_VTS_MAX - ov5675->cur_mode->height, 1,
807 ov5675->cur_mode->vts_def - ov5675->cur_mode->height);
808 h_blank = to_pixels_per_line(ov5675->cur_mode->hts,
809 ov5675->cur_mode->link_freq_index) - ov5675->cur_mode->width;
810 ov5675->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
811 V4L2_CID_HBLANK, h_blank, h_blank, 1,
814 ov5675->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
816 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
817 OV5675_ANAL_GAIN_MIN, OV5675_ANAL_GAIN_MAX,
818 OV5675_ANAL_GAIN_STEP, OV5675_ANAL_GAIN_MIN);
819 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
820 OV5675_DGTL_GAIN_MIN, OV5675_DGTL_GAIN_MAX,
821 OV5675_DGTL_GAIN_STEP, OV5675_DGTL_GAIN_DEFAULT);
822 exposure_max = (ov5675->cur_mode->vts_def - OV5675_EXPOSURE_MAX_MARGIN);
823 ov5675->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
825 OV5675_EXPOSURE_MIN, exposure_max,
826 OV5675_EXPOSURE_STEP,
828 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5675_ctrl_ops,
829 V4L2_CID_TEST_PATTERN,
830 ARRAY_SIZE(ov5675_test_pattern_menu) - 1,
831 0, 0, ov5675_test_pattern_menu);
832 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
833 V4L2_CID_HFLIP, 0, 1, 1, 0);
834 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
835 V4L2_CID_VFLIP, 0, 1, 1, 0);
837 if (ctrl_hdlr->error) {
838 v4l2_ctrl_handler_free(ctrl_hdlr);
839 return ctrl_hdlr->error;
842 ret = v4l2_fwnode_device_parse(&client->dev, &props);
846 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov5675_ctrl_ops,
851 ov5675->sd.ctrl_handler = ctrl_hdlr;
856 v4l2_ctrl_handler_free(ctrl_hdlr);
861 static void ov5675_update_pad_format(const struct ov5675_mode *mode,
862 struct v4l2_mbus_framefmt *fmt)
864 fmt->width = mode->width;
865 fmt->height = mode->height;
866 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
867 fmt->field = V4L2_FIELD_NONE;
870 static int ov5675_identify_module(struct ov5675 *ov5675)
872 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
876 if (ov5675->identified)
879 ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID,
880 OV5675_REG_VALUE_24BIT, &val);
884 if (val != OV5675_CHIP_ID) {
885 dev_err(&client->dev, "chip id mismatch: %x!=%x",
886 OV5675_CHIP_ID, val);
890 ov5675->identified = true;
895 static int ov5675_start_streaming(struct ov5675 *ov5675)
897 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
898 const struct ov5675_reg_list *reg_list;
899 int link_freq_index, ret;
901 ret = ov5675_identify_module(ov5675);
905 link_freq_index = ov5675->cur_mode->link_freq_index;
906 reg_list = &link_freq_configs[link_freq_index].reg_list;
907 ret = ov5675_write_reg_list(ov5675, reg_list);
909 dev_err(&client->dev, "failed to set plls");
913 reg_list = &ov5675->cur_mode->reg_list;
914 ret = ov5675_write_reg_list(ov5675, reg_list);
916 dev_err(&client->dev, "failed to set mode");
920 ret = __v4l2_ctrl_handler_setup(ov5675->sd.ctrl_handler);
924 ret = ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
925 OV5675_REG_VALUE_08BIT, OV5675_MODE_STREAMING);
927 dev_err(&client->dev, "failed to set stream");
934 static void ov5675_stop_streaming(struct ov5675 *ov5675)
936 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
938 if (ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
939 OV5675_REG_VALUE_08BIT, OV5675_MODE_STANDBY))
940 dev_err(&client->dev, "failed to set stream");
943 static int ov5675_set_stream(struct v4l2_subdev *sd, int enable)
945 struct ov5675 *ov5675 = to_ov5675(sd);
946 struct i2c_client *client = v4l2_get_subdevdata(sd);
949 mutex_lock(&ov5675->mutex);
951 ret = pm_runtime_resume_and_get(&client->dev);
953 mutex_unlock(&ov5675->mutex);
957 ret = ov5675_start_streaming(ov5675);
960 ov5675_stop_streaming(ov5675);
961 pm_runtime_put(&client->dev);
964 ov5675_stop_streaming(ov5675);
965 pm_runtime_put(&client->dev);
968 mutex_unlock(&ov5675->mutex);
973 static int ov5675_power_off(struct device *dev)
975 struct v4l2_subdev *sd = dev_get_drvdata(dev);
976 struct ov5675 *ov5675 = to_ov5675(sd);
978 usleep_range(90, 100);
980 clk_disable_unprepare(ov5675->xvclk);
981 gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
982 regulator_bulk_disable(OV5675_NUM_SUPPLIES, ov5675->supplies);
987 static int ov5675_power_on(struct device *dev)
989 struct v4l2_subdev *sd = dev_get_drvdata(dev);
990 struct ov5675 *ov5675 = to_ov5675(sd);
993 ret = clk_prepare_enable(ov5675->xvclk);
995 dev_err(dev, "failed to enable xvclk: %d\n", ret);
999 gpiod_set_value_cansleep(ov5675->reset_gpio, 1);
1001 ret = regulator_bulk_enable(OV5675_NUM_SUPPLIES, ov5675->supplies);
1003 clk_disable_unprepare(ov5675->xvclk);
1007 /* Reset pulse should be at least 2ms and reset gpio released only once
1008 * regulators are stable.
1010 usleep_range(2000, 2200);
1012 gpiod_set_value_cansleep(ov5675->reset_gpio, 0);
1014 /* Worst case quiesence gap is 1.365 milliseconds @ 6MHz XVCLK
1015 * Add an additional threshold grace period to ensure reset
1016 * completion before initiating our first I2C transaction.
1018 usleep_range(1500, 1600);
1023 static int ov5675_set_format(struct v4l2_subdev *sd,
1024 struct v4l2_subdev_state *sd_state,
1025 struct v4l2_subdev_format *fmt)
1027 struct ov5675 *ov5675 = to_ov5675(sd);
1028 const struct ov5675_mode *mode;
1029 s32 vblank_def, h_blank;
1031 mode = v4l2_find_nearest_size(supported_modes,
1032 ARRAY_SIZE(supported_modes), width,
1033 height, fmt->format.width,
1034 fmt->format.height);
1036 mutex_lock(&ov5675->mutex);
1037 ov5675_update_pad_format(mode, &fmt->format);
1038 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1039 *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
1041 ov5675->cur_mode = mode;
1042 __v4l2_ctrl_s_ctrl(ov5675->link_freq, mode->link_freq_index);
1043 __v4l2_ctrl_s_ctrl_int64(ov5675->pixel_rate,
1044 to_pixel_rate(mode->link_freq_index));
1046 /* Update limits and set FPS to default */
1047 vblank_def = mode->vts_def - mode->height;
1048 __v4l2_ctrl_modify_range(ov5675->vblank,
1049 mode->vts_min - mode->height,
1050 OV5675_VTS_MAX - mode->height, 1,
1052 __v4l2_ctrl_s_ctrl(ov5675->vblank, vblank_def);
1053 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1055 __v4l2_ctrl_modify_range(ov5675->hblank, h_blank, h_blank, 1,
1059 mutex_unlock(&ov5675->mutex);
1064 static int ov5675_get_format(struct v4l2_subdev *sd,
1065 struct v4l2_subdev_state *sd_state,
1066 struct v4l2_subdev_format *fmt)
1068 struct ov5675 *ov5675 = to_ov5675(sd);
1070 mutex_lock(&ov5675->mutex);
1071 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1072 fmt->format = *v4l2_subdev_state_get_format(sd_state,
1075 ov5675_update_pad_format(ov5675->cur_mode, &fmt->format);
1077 mutex_unlock(&ov5675->mutex);
1082 static int ov5675_get_selection(struct v4l2_subdev *sd,
1083 struct v4l2_subdev_state *state,
1084 struct v4l2_subdev_selection *sel)
1086 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1089 switch (sel->target) {
1090 case V4L2_SEL_TGT_CROP_BOUNDS:
1093 sel->r.width = 2624;
1094 sel->r.height = 2000;
1096 case V4L2_SEL_TGT_CROP:
1097 case V4L2_SEL_TGT_CROP_DEFAULT:
1100 sel->r.width = 2592;
1101 sel->r.height = 1944;
1107 static int ov5675_enum_mbus_code(struct v4l2_subdev *sd,
1108 struct v4l2_subdev_state *sd_state,
1109 struct v4l2_subdev_mbus_code_enum *code)
1111 if (code->index > 0)
1114 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1119 static int ov5675_enum_frame_size(struct v4l2_subdev *sd,
1120 struct v4l2_subdev_state *sd_state,
1121 struct v4l2_subdev_frame_size_enum *fse)
1123 if (fse->index >= ARRAY_SIZE(supported_modes))
1126 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1129 fse->min_width = supported_modes[fse->index].width;
1130 fse->max_width = fse->min_width;
1131 fse->min_height = supported_modes[fse->index].height;
1132 fse->max_height = fse->min_height;
1137 static int ov5675_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1139 struct ov5675 *ov5675 = to_ov5675(sd);
1141 mutex_lock(&ov5675->mutex);
1142 ov5675_update_pad_format(&supported_modes[0],
1143 v4l2_subdev_state_get_format(fh->state, 0));
1144 mutex_unlock(&ov5675->mutex);
1149 static const struct v4l2_subdev_video_ops ov5675_video_ops = {
1150 .s_stream = ov5675_set_stream,
1153 static const struct v4l2_subdev_pad_ops ov5675_pad_ops = {
1154 .set_fmt = ov5675_set_format,
1155 .get_fmt = ov5675_get_format,
1156 .get_selection = ov5675_get_selection,
1157 .enum_mbus_code = ov5675_enum_mbus_code,
1158 .enum_frame_size = ov5675_enum_frame_size,
1161 static const struct v4l2_subdev_ops ov5675_subdev_ops = {
1162 .video = &ov5675_video_ops,
1163 .pad = &ov5675_pad_ops,
1166 static const struct media_entity_operations ov5675_subdev_entity_ops = {
1167 .link_validate = v4l2_subdev_link_validate,
1170 static const struct v4l2_subdev_internal_ops ov5675_internal_ops = {
1171 .open = ov5675_open,
1174 static int ov5675_get_hwcfg(struct ov5675 *ov5675, struct device *dev)
1176 struct fwnode_handle *ep;
1177 struct fwnode_handle *fwnode = dev_fwnode(dev);
1178 struct v4l2_fwnode_endpoint bus_cfg = {
1179 .bus_type = V4L2_MBUS_CSI2_DPHY
1188 ov5675->xvclk = devm_clk_get_optional(dev, NULL);
1189 if (IS_ERR(ov5675->xvclk))
1190 return dev_err_probe(dev, PTR_ERR(ov5675->xvclk),
1191 "failed to get xvclk: %ld\n",
1192 PTR_ERR(ov5675->xvclk));
1194 if (ov5675->xvclk) {
1195 xvclk_rate = clk_get_rate(ov5675->xvclk);
1197 ret = fwnode_property_read_u32(fwnode, "clock-frequency",
1201 dev_err(dev, "can't get clock frequency");
1206 if (xvclk_rate != OV5675_XVCLK_19_2) {
1207 dev_err(dev, "external clock rate %u is unsupported",
1212 ov5675->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1214 if (IS_ERR(ov5675->reset_gpio)) {
1215 ret = PTR_ERR(ov5675->reset_gpio);
1216 dev_err(dev, "failed to get reset-gpios: %d\n", ret);
1220 for (i = 0; i < OV5675_NUM_SUPPLIES; i++)
1221 ov5675->supplies[i].supply = ov5675_supply_names[i];
1223 ret = devm_regulator_bulk_get(dev, OV5675_NUM_SUPPLIES,
1228 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1232 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1233 fwnode_handle_put(ep);
1237 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV5675_DATA_LANES) {
1238 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1239 bus_cfg.bus.mipi_csi2.num_data_lanes);
1241 goto check_hwcfg_error;
1244 if (!bus_cfg.nr_of_link_frequencies) {
1245 dev_err(dev, "no link frequencies defined");
1247 goto check_hwcfg_error;
1250 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1251 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1252 if (link_freq_menu_items[i] ==
1253 bus_cfg.link_frequencies[j])
1257 if (j == bus_cfg.nr_of_link_frequencies) {
1258 dev_err(dev, "no link frequency %lld supported",
1259 link_freq_menu_items[i]);
1261 goto check_hwcfg_error;
1266 v4l2_fwnode_endpoint_free(&bus_cfg);
1271 static void ov5675_remove(struct i2c_client *client)
1273 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1274 struct ov5675 *ov5675 = to_ov5675(sd);
1276 v4l2_async_unregister_subdev(sd);
1277 media_entity_cleanup(&sd->entity);
1278 v4l2_ctrl_handler_free(sd->ctrl_handler);
1279 pm_runtime_disable(&client->dev);
1280 mutex_destroy(&ov5675->mutex);
1282 if (!pm_runtime_status_suspended(&client->dev))
1283 ov5675_power_off(&client->dev);
1284 pm_runtime_set_suspended(&client->dev);
1287 static int ov5675_probe(struct i2c_client *client)
1289 struct ov5675 *ov5675;
1293 ov5675 = devm_kzalloc(&client->dev, sizeof(*ov5675), GFP_KERNEL);
1297 ret = ov5675_get_hwcfg(ov5675, &client->dev);
1299 dev_err(&client->dev, "failed to get HW configuration: %d",
1304 v4l2_i2c_subdev_init(&ov5675->sd, client, &ov5675_subdev_ops);
1306 ret = ov5675_power_on(&client->dev);
1308 dev_err(&client->dev, "failed to power on: %d\n", ret);
1312 full_power = acpi_dev_state_d0(&client->dev);
1314 ret = ov5675_identify_module(ov5675);
1316 dev_err(&client->dev, "failed to find sensor: %d", ret);
1317 goto probe_power_off;
1321 mutex_init(&ov5675->mutex);
1322 ov5675->cur_mode = &supported_modes[0];
1323 ret = ov5675_init_controls(ov5675);
1325 dev_err(&client->dev, "failed to init controls: %d", ret);
1326 goto probe_error_v4l2_ctrl_handler_free;
1329 ov5675->sd.internal_ops = &ov5675_internal_ops;
1330 ov5675->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1331 ov5675->sd.entity.ops = &ov5675_subdev_entity_ops;
1332 ov5675->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1333 ov5675->pad.flags = MEDIA_PAD_FL_SOURCE;
1334 ret = media_entity_pads_init(&ov5675->sd.entity, 1, &ov5675->pad);
1336 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1337 goto probe_error_v4l2_ctrl_handler_free;
1340 ret = v4l2_async_register_subdev_sensor(&ov5675->sd);
1342 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1344 goto probe_error_media_entity_cleanup;
1347 /* Set the device's state to active if it's in D0 state. */
1349 pm_runtime_set_active(&client->dev);
1350 pm_runtime_enable(&client->dev);
1351 pm_runtime_idle(&client->dev);
1355 probe_error_media_entity_cleanup:
1356 media_entity_cleanup(&ov5675->sd.entity);
1358 probe_error_v4l2_ctrl_handler_free:
1359 v4l2_ctrl_handler_free(ov5675->sd.ctrl_handler);
1360 mutex_destroy(&ov5675->mutex);
1362 ov5675_power_off(&client->dev);
1367 static const struct dev_pm_ops ov5675_pm_ops = {
1368 SET_RUNTIME_PM_OPS(ov5675_power_off, ov5675_power_on, NULL)
1372 static const struct acpi_device_id ov5675_acpi_ids[] = {
1377 MODULE_DEVICE_TABLE(acpi, ov5675_acpi_ids);
1380 static const struct of_device_id ov5675_of_match[] = {
1381 { .compatible = "ovti,ov5675", },
1384 MODULE_DEVICE_TABLE(of, ov5675_of_match);
1386 static struct i2c_driver ov5675_i2c_driver = {
1389 .pm = &ov5675_pm_ops,
1390 .acpi_match_table = ACPI_PTR(ov5675_acpi_ids),
1391 .of_match_table = ov5675_of_match,
1393 .probe = ov5675_probe,
1394 .remove = ov5675_remove,
1395 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1398 module_i2c_driver(ov5675_i2c_driver);
1400 MODULE_AUTHOR("Shawn Tu");
1401 MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
1402 MODULE_LICENSE("GPL v2");