1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regulator/consumer.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-fwnode.h>
15 #define OV08D10_SCLK 144000000ULL
16 #define OV08D10_XVCLK_19_2 19200000
17 #define OV08D10_ROWCLK 36000
18 #define OV08D10_DATA_LANES 2
19 #define OV08D10_RGB_DEPTH 10
21 #define OV08D10_REG_PAGE 0xfd
22 #define OV08D10_REG_GLOBAL_EFFECTIVE 0x01
23 #define OV08D10_REG_CHIP_ID_0 0x00
24 #define OV08D10_REG_CHIP_ID_1 0x01
25 #define OV08D10_ID_MASK GENMASK(15, 0)
26 #define OV08D10_CHIP_ID 0x5608
28 #define OV08D10_REG_MODE_SELECT 0xa0
29 #define OV08D10_MODE_STANDBY 0x00
30 #define OV08D10_MODE_STREAMING 0x01
32 /* vertical-timings from sensor */
33 #define OV08D10_REG_VTS_H 0x05
34 #define OV08D10_REG_VTS_L 0x06
35 #define OV08D10_VTS_MAX 0x7fff
37 /* Exposure controls from sensor */
38 #define OV08D10_REG_EXPOSURE_H 0x02
39 #define OV08D10_REG_EXPOSURE_M 0x03
40 #define OV08D10_REG_EXPOSURE_L 0x04
41 #define OV08D10_EXPOSURE_MIN 6
42 #define OV08D10_EXPOSURE_MAX_MARGIN 6
43 #define OV08D10_EXPOSURE_STEP 1
45 /* Analog gain controls from sensor */
46 #define OV08D10_REG_ANALOG_GAIN 0x24
47 #define OV08D10_ANAL_GAIN_MIN 128
48 #define OV08D10_ANAL_GAIN_MAX 2047
49 #define OV08D10_ANAL_GAIN_STEP 1
51 /* Digital gain controls from sensor */
52 #define OV08D10_REG_MWB_DGAIN_C 0x21
53 #define OV08D10_REG_MWB_DGAIN_F 0x22
54 #define OV08D10_DGTL_GAIN_MIN 0
55 #define OV08D10_DGTL_GAIN_MAX 4095
56 #define OV08D10_DGTL_GAIN_STEP 1
57 #define OV08D10_DGTL_GAIN_DEFAULT 1024
59 /* Test Pattern Control */
60 #define OV08D10_REG_TEST_PATTERN 0x12
61 #define OV08D10_TEST_PATTERN_ENABLE 0x01
62 #define OV08D10_TEST_PATTERN_DISABLE 0x00
64 /* Flip Mirror Controls from sensor */
65 #define OV08D10_REG_FLIP_OPT 0x32
66 #define OV08D10_REG_FLIP_MASK 0x3
68 #define to_ov08d10(_sd) container_of(_sd, struct ov08d10, sd)
75 struct ov08d10_reg_list {
77 const struct ov08d10_reg *regs;
80 struct ov08d10_link_freq_config {
81 const struct ov08d10_reg_list reg_list;
85 /* Frame width in pixels */
88 /* Frame height in pixels */
91 /* Horizontal timining size */
94 /* Default vertical timining size */
97 /* Min vertical timining size */
100 /* Link frequency needed for this resolution */
103 /* Sensor register settings for this resolution */
104 const struct ov08d10_reg_list reg_list;
106 /* Number of data lanes */
110 /* 3280x2460, 3264x2448 need 720Mbps/lane, 2 lanes */
111 static const struct ov08d10_reg mipi_data_rate_720mbps[] = {
121 /* 1632x1224 needs 360Mbps/lane, 2 lanes */
122 static const struct ov08d10_reg mipi_data_rate_360mbps[] = {
134 static const struct ov08d10_reg lane_2_mode_3280x2460[] = {
135 /* 3280x2460 resolution */
252 static const struct ov08d10_reg lane_2_mode_3264x2448[] = {
253 /* 3264x2448 resolution */
370 static const struct ov08d10_reg lane_2_mode_1632x1224[] = {
371 /* 1640x1232 resolution */
512 static const char * const ov08d10_test_pattern_menu[] = {
514 "Standard Color Bar",
518 struct v4l2_subdev sd;
519 struct media_pad pad;
520 struct v4l2_ctrl_handler ctrl_handler;
525 struct v4l2_ctrl *link_freq;
526 struct v4l2_ctrl *pixel_rate;
527 struct v4l2_ctrl *vblank;
528 struct v4l2_ctrl *hblank;
529 struct v4l2_ctrl *vflip;
530 struct v4l2_ctrl *hflip;
531 struct v4l2_ctrl *exposure;
534 const struct ov08d10_mode *cur_mode;
536 /* To serialize asynchronus callbacks */
539 /* Streaming on/off */
545 const struct ov08d10_lane_cfg *priv_lane;
549 struct ov08d10_lane_cfg {
550 const s64 link_freq_menu[2];
551 const struct ov08d10_link_freq_config link_freq_configs[2];
552 const struct ov08d10_mode sp_modes[3];
555 static const struct ov08d10_lane_cfg lane_cfg_2 = {
563 ARRAY_SIZE(mipi_data_rate_720mbps),
564 .regs = mipi_data_rate_720mbps,
570 ARRAY_SIZE(mipi_data_rate_360mbps),
571 .regs = mipi_data_rate_360mbps,
581 .num_of_regs = ARRAY_SIZE(lane_2_mode_3280x2460),
582 .regs = lane_2_mode_3280x2460,
584 .link_freq_index = 0,
594 .num_of_regs = ARRAY_SIZE(lane_2_mode_3264x2448),
595 .regs = lane_2_mode_3264x2448,
597 .link_freq_index = 0,
607 .num_of_regs = ARRAY_SIZE(lane_2_mode_1632x1224),
608 .regs = lane_2_mode_1632x1224,
610 .link_freq_index = 1,
615 static u32 ov08d10_get_format_code(struct ov08d10 *ov08d10)
617 static const u32 codes[2][2] = {
618 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10},
619 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10},
622 return codes[ov08d10->vflip->val][ov08d10->hflip->val];
625 static unsigned int ov08d10_modes_num(const struct ov08d10 *ov08d10)
627 unsigned int i, count = 0;
629 for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->sp_modes); i++) {
630 if (ov08d10->priv_lane->sp_modes[i].width == 0)
638 static u64 to_rate(const s64 *link_freq_menu,
639 u32 f_index, u8 nlanes)
641 u64 pixel_rate = link_freq_menu[f_index] * 2 * nlanes;
643 do_div(pixel_rate, OV08D10_RGB_DEPTH);
648 static u64 to_pixels_per_line(const s64 *link_freq_menu, u32 hts,
649 u32 f_index, u8 nlanes)
651 u64 ppl = hts * to_rate(link_freq_menu, f_index, nlanes);
653 do_div(ppl, OV08D10_SCLK);
658 static int ov08d10_write_reg_list(struct ov08d10 *ov08d10,
659 const struct ov08d10_reg_list *r_list)
661 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
665 for (i = 0; i < r_list->num_of_regs; i++) {
666 ret = i2c_smbus_write_byte_data(client, r_list->regs[i].address,
667 r_list->regs[i].val);
669 dev_err_ratelimited(&client->dev,
670 "failed to write reg 0x%2.2x. error = %d",
671 r_list->regs[i].address, ret);
679 static int ov08d10_update_analog_gain(struct ov08d10 *ov08d10, u32 a_gain)
681 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
685 val = ((a_gain >> 3) & 0xFF);
686 /* CIS control registers */
687 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
692 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_ANALOG_GAIN, val);
696 return i2c_smbus_write_byte_data(client,
697 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
700 static int ov08d10_update_digital_gain(struct ov08d10 *ov08d10, u32 d_gain)
702 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
706 d_gain = (d_gain >> 1);
707 /* CIS control registers */
708 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
712 val = ((d_gain >> 8) & 0x3F);
714 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_C, val);
719 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_F, val);
723 return i2c_smbus_write_byte_data(client,
724 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
727 static int ov08d10_set_exposure(struct ov08d10 *ov08d10, u32 exposure)
729 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
732 u32 hts, cur_vts, exp_cal;
735 cur_vts = ov08d10->cur_mode->vts_def;
736 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
740 hts_h = i2c_smbus_read_byte_data(client, 0x37);
741 hts_l = i2c_smbus_read_byte_data(client, 0x38);
742 hts = ((hts_h << 8) | (hts_l));
743 exp_cal = 66 * OV08D10_ROWCLK / hts;
744 exposure = exposure * exp_cal / (cur_vts - OV08D10_EXPOSURE_MAX_MARGIN);
745 /* CIS control registers */
746 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
750 /* update exposure */
751 val = ((exposure >> 16) & 0xFF);
752 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_H, val);
756 val = ((exposure >> 8) & 0xFF);
757 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_M, val);
761 val = exposure & 0xFF;
762 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_L, val);
766 return i2c_smbus_write_byte_data(client,
767 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
770 static int ov08d10_set_vblank(struct ov08d10 *ov08d10, u32 vblank)
772 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
776 /* CIS control registers */
777 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
781 val = ((vblank >> 8) & 0xFF);
783 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_H, val);
788 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_L, val);
792 return i2c_smbus_write_byte_data(client,
793 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
796 static int ov08d10_test_pattern(struct ov08d10 *ov08d10, u32 pattern)
798 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
803 val = OV08D10_TEST_PATTERN_ENABLE;
805 val = OV08D10_TEST_PATTERN_DISABLE;
807 /* CIS control registers */
808 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
812 ret = i2c_smbus_write_byte_data(client,
813 OV08D10_REG_TEST_PATTERN, val);
817 return i2c_smbus_write_byte_data(client,
818 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
821 static int ov08d10_set_ctrl_flip(struct ov08d10 *ov08d10, u32 ctrl_val)
823 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
827 /* System control registers */
828 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
832 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_FLIP_OPT);
836 val = ret | (ctrl_val & OV08D10_REG_FLIP_MASK);
838 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
842 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_FLIP_OPT, val);
847 return i2c_smbus_write_byte_data(client,
848 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
851 static int ov08d10_set_ctrl(struct v4l2_ctrl *ctrl)
853 struct ov08d10 *ov08d10 = container_of(ctrl->handler,
854 struct ov08d10, ctrl_handler);
855 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
859 /* Propagate change of current control to all related controls */
860 if (ctrl->id == V4L2_CID_VBLANK) {
861 /* Update max exposure while meeting expected vblanking */
862 exposure_max = ov08d10->cur_mode->height + ctrl->val -
863 OV08D10_EXPOSURE_MAX_MARGIN;
864 __v4l2_ctrl_modify_range(ov08d10->exposure,
865 ov08d10->exposure->minimum,
866 exposure_max, ov08d10->exposure->step,
870 /* V4L2 controls values will be applied only when power is already up */
871 if (!pm_runtime_get_if_in_use(&client->dev))
875 case V4L2_CID_ANALOGUE_GAIN:
876 ret = ov08d10_update_analog_gain(ov08d10, ctrl->val);
879 case V4L2_CID_DIGITAL_GAIN:
880 ret = ov08d10_update_digital_gain(ov08d10, ctrl->val);
883 case V4L2_CID_EXPOSURE:
884 ret = ov08d10_set_exposure(ov08d10, ctrl->val);
887 case V4L2_CID_VBLANK:
888 ret = ov08d10_set_vblank(ov08d10, ctrl->val);
891 case V4L2_CID_TEST_PATTERN:
892 ret = ov08d10_test_pattern(ov08d10, ctrl->val);
897 ret = ov08d10_set_ctrl_flip(ov08d10,
898 ov08d10->hflip->val |
899 ov08d10->vflip->val << 1);
907 pm_runtime_put(&client->dev);
912 static const struct v4l2_ctrl_ops ov08d10_ctrl_ops = {
913 .s_ctrl = ov08d10_set_ctrl,
916 static int ov08d10_init_controls(struct ov08d10 *ov08d10)
918 struct v4l2_ctrl_handler *ctrl_hdlr;
925 const struct ov08d10_mode *mode;
928 ctrl_hdlr = &ov08d10->ctrl_handler;
929 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
933 ctrl_hdlr->lock = &ov08d10->mutex;
934 link_freq_size = ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu);
936 v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov08d10_ctrl_ops,
940 ov08d10->priv_lane->link_freq_menu);
941 if (ov08d10->link_freq)
942 ov08d10->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
944 pixel_rate_max = to_rate(ov08d10->priv_lane->link_freq_menu, 0,
945 ov08d10->cur_mode->data_lanes);
946 ov08d10->pixel_rate =
947 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
948 V4L2_CID_PIXEL_RATE, 0, pixel_rate_max, 1,
951 mode = ov08d10->cur_mode;
952 vblank_def = mode->vts_def - mode->height;
953 vblank_min = mode->vts_min - mode->height;
955 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
956 V4L2_CID_VBLANK, vblank_min,
957 OV08D10_VTS_MAX - mode->height, 1,
960 h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
961 mode->hts, mode->link_freq_index,
964 ov08d10->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
965 V4L2_CID_HBLANK, h_blank, h_blank,
968 ov08d10->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
970 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
971 OV08D10_ANAL_GAIN_MIN, OV08D10_ANAL_GAIN_MAX,
972 OV08D10_ANAL_GAIN_STEP, OV08D10_ANAL_GAIN_MIN);
974 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
975 OV08D10_DGTL_GAIN_MIN, OV08D10_DGTL_GAIN_MAX,
976 OV08D10_DGTL_GAIN_STEP, OV08D10_DGTL_GAIN_DEFAULT);
978 exposure_max = mode->vts_def - OV08D10_EXPOSURE_MAX_MARGIN;
979 ov08d10->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
981 OV08D10_EXPOSURE_MIN,
983 OV08D10_EXPOSURE_STEP,
986 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov08d10_ctrl_ops,
987 V4L2_CID_TEST_PATTERN,
988 ARRAY_SIZE(ov08d10_test_pattern_menu) - 1,
989 0, 0, ov08d10_test_pattern_menu);
991 ov08d10->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
992 V4L2_CID_HFLIP, 0, 1, 1, 0);
994 ov08d10->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
995 ov08d10->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
996 V4L2_CID_VFLIP, 0, 1, 1, 0);
998 ov08d10->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1000 if (ctrl_hdlr->error)
1001 return ctrl_hdlr->error;
1003 ov08d10->sd.ctrl_handler = ctrl_hdlr;
1008 static void ov08d10_update_pad_format(struct ov08d10 *ov08d10,
1009 const struct ov08d10_mode *mode,
1010 struct v4l2_mbus_framefmt *fmt)
1012 fmt->width = mode->width;
1013 fmt->height = mode->height;
1014 fmt->code = ov08d10_get_format_code(ov08d10);
1015 fmt->field = V4L2_FIELD_NONE;
1018 static int ov08d10_start_streaming(struct ov08d10 *ov08d10)
1020 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1021 const struct ov08d10_reg_list *reg_list;
1022 int link_freq_index, ret;
1024 link_freq_index = ov08d10->cur_mode->link_freq_index;
1026 &ov08d10->priv_lane->link_freq_configs[link_freq_index].reg_list;
1029 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1031 dev_err(&client->dev, "failed to reset sensor");
1034 ret = i2c_smbus_write_byte_data(client, 0x20, 0x0e);
1036 dev_err(&client->dev, "failed to reset sensor");
1039 usleep_range(3000, 4000);
1040 ret = i2c_smbus_write_byte_data(client, 0x20, 0x0b);
1042 dev_err(&client->dev, "failed to reset sensor");
1046 /* update sensor setting */
1047 ret = ov08d10_write_reg_list(ov08d10, reg_list);
1049 dev_err(&client->dev, "failed to set plls");
1053 reg_list = &ov08d10->cur_mode->reg_list;
1054 ret = ov08d10_write_reg_list(ov08d10, reg_list);
1056 dev_err(&client->dev, "failed to set mode");
1060 ret = __v4l2_ctrl_handler_setup(ov08d10->sd.ctrl_handler);
1064 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1068 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
1069 OV08D10_MODE_STREAMING);
1073 return i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
1076 static void ov08d10_stop_streaming(struct ov08d10 *ov08d10)
1078 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1081 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1083 dev_err(&client->dev, "failed to stop streaming");
1086 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
1087 OV08D10_MODE_STANDBY);
1089 dev_err(&client->dev, "failed to stop streaming");
1093 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
1095 dev_err(&client->dev, "failed to stop streaming");
1100 static int ov08d10_set_stream(struct v4l2_subdev *sd, int enable)
1102 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1103 struct i2c_client *client = v4l2_get_subdevdata(sd);
1106 if (ov08d10->streaming == enable)
1109 mutex_lock(&ov08d10->mutex);
1111 ret = pm_runtime_resume_and_get(&client->dev);
1113 mutex_unlock(&ov08d10->mutex);
1117 ret = ov08d10_start_streaming(ov08d10);
1120 ov08d10_stop_streaming(ov08d10);
1121 pm_runtime_put(&client->dev);
1124 ov08d10_stop_streaming(ov08d10);
1125 pm_runtime_put(&client->dev);
1128 ov08d10->streaming = enable;
1130 /* vflip and hflip cannot change during streaming */
1131 __v4l2_ctrl_grab(ov08d10->vflip, enable);
1132 __v4l2_ctrl_grab(ov08d10->hflip, enable);
1134 mutex_unlock(&ov08d10->mutex);
1139 static int __maybe_unused ov08d10_suspend(struct device *dev)
1141 struct i2c_client *client = to_i2c_client(dev);
1142 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1143 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1145 mutex_lock(&ov08d10->mutex);
1146 if (ov08d10->streaming)
1147 ov08d10_stop_streaming(ov08d10);
1149 mutex_unlock(&ov08d10->mutex);
1154 static int __maybe_unused ov08d10_resume(struct device *dev)
1156 struct i2c_client *client = to_i2c_client(dev);
1157 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1158 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1161 mutex_lock(&ov08d10->mutex);
1163 if (ov08d10->streaming) {
1164 ret = ov08d10_start_streaming(ov08d10);
1166 ov08d10->streaming = false;
1167 ov08d10_stop_streaming(ov08d10);
1168 mutex_unlock(&ov08d10->mutex);
1173 mutex_unlock(&ov08d10->mutex);
1178 static int ov08d10_set_format(struct v4l2_subdev *sd,
1179 struct v4l2_subdev_state *sd_state,
1180 struct v4l2_subdev_format *fmt)
1182 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1183 const struct ov08d10_mode *mode;
1184 s32 vblank_def, h_blank;
1187 mode = v4l2_find_nearest_size(ov08d10->priv_lane->sp_modes,
1188 ov08d10->modes_size,
1189 width, height, fmt->format.width,
1190 fmt->format.height);
1192 mutex_lock(&ov08d10->mutex);
1193 ov08d10_update_pad_format(ov08d10, mode, &fmt->format);
1194 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1195 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) =
1198 ov08d10->cur_mode = mode;
1199 __v4l2_ctrl_s_ctrl(ov08d10->link_freq, mode->link_freq_index);
1200 pixel_rate = to_rate(ov08d10->priv_lane->link_freq_menu,
1201 mode->link_freq_index,
1202 ov08d10->cur_mode->data_lanes);
1203 __v4l2_ctrl_s_ctrl_int64(ov08d10->pixel_rate, pixel_rate);
1205 /* Update limits and set FPS to default */
1206 vblank_def = mode->vts_def - mode->height;
1207 __v4l2_ctrl_modify_range(ov08d10->vblank,
1208 mode->vts_min - mode->height,
1209 OV08D10_VTS_MAX - mode->height, 1,
1211 __v4l2_ctrl_s_ctrl(ov08d10->vblank, vblank_def);
1212 h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
1214 mode->link_freq_index,
1215 ov08d10->cur_mode->data_lanes)
1217 __v4l2_ctrl_modify_range(ov08d10->hblank, h_blank, h_blank, 1,
1221 mutex_unlock(&ov08d10->mutex);
1226 static int ov08d10_get_format(struct v4l2_subdev *sd,
1227 struct v4l2_subdev_state *sd_state,
1228 struct v4l2_subdev_format *fmt)
1230 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1232 mutex_lock(&ov08d10->mutex);
1233 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1234 fmt->format = *v4l2_subdev_get_try_format(&ov08d10->sd,
1238 ov08d10_update_pad_format(ov08d10, ov08d10->cur_mode,
1241 mutex_unlock(&ov08d10->mutex);
1246 static int ov08d10_enum_mbus_code(struct v4l2_subdev *sd,
1247 struct v4l2_subdev_state *sd_state,
1248 struct v4l2_subdev_mbus_code_enum *code)
1250 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1252 if (code->index > 0)
1255 mutex_lock(&ov08d10->mutex);
1256 code->code = ov08d10_get_format_code(ov08d10);
1257 mutex_unlock(&ov08d10->mutex);
1262 static int ov08d10_enum_frame_size(struct v4l2_subdev *sd,
1263 struct v4l2_subdev_state *sd_state,
1264 struct v4l2_subdev_frame_size_enum *fse)
1266 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1268 if (fse->index >= ov08d10->modes_size)
1271 mutex_lock(&ov08d10->mutex);
1272 if (fse->code != ov08d10_get_format_code(ov08d10)) {
1273 mutex_unlock(&ov08d10->mutex);
1276 mutex_unlock(&ov08d10->mutex);
1278 fse->min_width = ov08d10->priv_lane->sp_modes[fse->index].width;
1279 fse->max_width = fse->min_width;
1280 fse->min_height = ov08d10->priv_lane->sp_modes[fse->index].height;
1281 fse->max_height = fse->min_height;
1286 static int ov08d10_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1288 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1290 mutex_lock(&ov08d10->mutex);
1291 ov08d10_update_pad_format(ov08d10, &ov08d10->priv_lane->sp_modes[0],
1292 v4l2_subdev_get_try_format(sd, fh->state, 0));
1293 mutex_unlock(&ov08d10->mutex);
1298 static const struct v4l2_subdev_video_ops ov08d10_video_ops = {
1299 .s_stream = ov08d10_set_stream,
1302 static const struct v4l2_subdev_pad_ops ov08d10_pad_ops = {
1303 .set_fmt = ov08d10_set_format,
1304 .get_fmt = ov08d10_get_format,
1305 .enum_mbus_code = ov08d10_enum_mbus_code,
1306 .enum_frame_size = ov08d10_enum_frame_size,
1309 static const struct v4l2_subdev_ops ov08d10_subdev_ops = {
1310 .video = &ov08d10_video_ops,
1311 .pad = &ov08d10_pad_ops,
1314 static const struct v4l2_subdev_internal_ops ov08d10_internal_ops = {
1315 .open = ov08d10_open,
1318 static int ov08d10_identify_module(struct ov08d10 *ov08d10)
1320 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1325 /* System control registers */
1326 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1330 /* Validate the chip ID */
1331 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_0);
1337 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_1);
1341 chip_id = val | ret;
1343 if ((chip_id & OV08D10_ID_MASK) != OV08D10_CHIP_ID) {
1344 dev_err(&client->dev, "unexpected sensor id(0x%04x)\n",
1352 static int ov08d10_get_hwcfg(struct ov08d10 *ov08d10, struct device *dev)
1354 struct fwnode_handle *ep;
1355 struct fwnode_handle *fwnode = dev_fwnode(dev);
1356 struct v4l2_fwnode_endpoint bus_cfg = {
1357 .bus_type = V4L2_MBUS_CSI2_DPHY
1366 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
1370 if (xvclk_rate != OV08D10_XVCLK_19_2)
1371 dev_warn(dev, "external clock rate %u is unsupported",
1374 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1378 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1379 fwnode_handle_put(ep);
1383 /* Get number of data lanes */
1384 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1385 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1386 bus_cfg.bus.mipi_csi2.num_data_lanes);
1388 goto check_hwcfg_error;
1391 dev_dbg(dev, "Using %u data lanes\n", ov08d10->cur_mode->data_lanes);
1393 ov08d10->priv_lane = &lane_cfg_2;
1394 ov08d10->modes_size = ov08d10_modes_num(ov08d10);
1396 if (!bus_cfg.nr_of_link_frequencies) {
1397 dev_err(dev, "no link frequencies defined");
1399 goto check_hwcfg_error;
1402 for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu); i++) {
1403 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1404 if (ov08d10->priv_lane->link_freq_menu[i] ==
1405 bus_cfg.link_frequencies[j])
1409 if (j == bus_cfg.nr_of_link_frequencies) {
1410 dev_err(dev, "no link frequency %lld supported",
1411 ov08d10->priv_lane->link_freq_menu[i]);
1413 goto check_hwcfg_error;
1418 v4l2_fwnode_endpoint_free(&bus_cfg);
1423 static void ov08d10_remove(struct i2c_client *client)
1425 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1426 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1428 v4l2_async_unregister_subdev(sd);
1429 media_entity_cleanup(&sd->entity);
1430 v4l2_ctrl_handler_free(sd->ctrl_handler);
1431 pm_runtime_disable(&client->dev);
1432 mutex_destroy(&ov08d10->mutex);
1435 static int ov08d10_probe(struct i2c_client *client)
1437 struct ov08d10 *ov08d10;
1440 ov08d10 = devm_kzalloc(&client->dev, sizeof(*ov08d10), GFP_KERNEL);
1444 ret = ov08d10_get_hwcfg(ov08d10, &client->dev);
1446 dev_err(&client->dev, "failed to get HW configuration: %d",
1451 v4l2_i2c_subdev_init(&ov08d10->sd, client, &ov08d10_subdev_ops);
1453 ret = ov08d10_identify_module(ov08d10);
1455 dev_err(&client->dev, "failed to find sensor: %d", ret);
1459 mutex_init(&ov08d10->mutex);
1460 ov08d10->cur_mode = &ov08d10->priv_lane->sp_modes[0];
1461 ret = ov08d10_init_controls(ov08d10);
1463 dev_err(&client->dev, "failed to init controls: %d", ret);
1464 goto probe_error_v4l2_ctrl_handler_free;
1467 ov08d10->sd.internal_ops = &ov08d10_internal_ops;
1468 ov08d10->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1469 ov08d10->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1470 ov08d10->pad.flags = MEDIA_PAD_FL_SOURCE;
1471 ret = media_entity_pads_init(&ov08d10->sd.entity, 1, &ov08d10->pad);
1473 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1474 goto probe_error_v4l2_ctrl_handler_free;
1477 ret = v4l2_async_register_subdev_sensor(&ov08d10->sd);
1479 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1481 goto probe_error_media_entity_cleanup;
1485 * Device is already turned on by i2c-core with ACPI domain PM.
1486 * Enable runtime PM and turn off the device.
1488 pm_runtime_set_active(&client->dev);
1489 pm_runtime_enable(&client->dev);
1490 pm_runtime_idle(&client->dev);
1494 probe_error_media_entity_cleanup:
1495 media_entity_cleanup(&ov08d10->sd.entity);
1497 probe_error_v4l2_ctrl_handler_free:
1498 v4l2_ctrl_handler_free(ov08d10->sd.ctrl_handler);
1499 mutex_destroy(&ov08d10->mutex);
1504 static const struct dev_pm_ops ov08d10_pm_ops = {
1505 SET_SYSTEM_SLEEP_PM_OPS(ov08d10_suspend, ov08d10_resume)
1509 static const struct acpi_device_id ov08d10_acpi_ids[] = {
1514 MODULE_DEVICE_TABLE(acpi, ov08d10_acpi_ids);
1517 static struct i2c_driver ov08d10_i2c_driver = {
1520 .pm = &ov08d10_pm_ops,
1521 .acpi_match_table = ACPI_PTR(ov08d10_acpi_ids),
1523 .probe = ov08d10_probe,
1524 .remove = ov08d10_remove,
1527 module_i2c_driver(ov08d10_i2c_driver);
1530 MODULE_DESCRIPTION("OmniVision ov08d10 sensor driver");
1531 MODULE_LICENSE("GPL v2");