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 */
542 const struct ov08d10_lane_cfg *priv_lane;
546 struct ov08d10_lane_cfg {
547 const s64 link_freq_menu[2];
548 const struct ov08d10_link_freq_config link_freq_configs[2];
549 const struct ov08d10_mode sp_modes[3];
552 static const struct ov08d10_lane_cfg lane_cfg_2 = {
560 ARRAY_SIZE(mipi_data_rate_720mbps),
561 .regs = mipi_data_rate_720mbps,
567 ARRAY_SIZE(mipi_data_rate_360mbps),
568 .regs = mipi_data_rate_360mbps,
578 .num_of_regs = ARRAY_SIZE(lane_2_mode_3280x2460),
579 .regs = lane_2_mode_3280x2460,
581 .link_freq_index = 0,
591 .num_of_regs = ARRAY_SIZE(lane_2_mode_3264x2448),
592 .regs = lane_2_mode_3264x2448,
594 .link_freq_index = 0,
604 .num_of_regs = ARRAY_SIZE(lane_2_mode_1632x1224),
605 .regs = lane_2_mode_1632x1224,
607 .link_freq_index = 1,
612 static u32 ov08d10_get_format_code(struct ov08d10 *ov08d10)
614 static const u32 codes[2][2] = {
615 { MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10},
616 { MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10},
619 return codes[ov08d10->vflip->val][ov08d10->hflip->val];
622 static unsigned int ov08d10_modes_num(const struct ov08d10 *ov08d10)
624 unsigned int i, count = 0;
626 for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->sp_modes); i++) {
627 if (ov08d10->priv_lane->sp_modes[i].width == 0)
635 static u64 to_rate(const s64 *link_freq_menu,
636 u32 f_index, u8 nlanes)
638 u64 pixel_rate = link_freq_menu[f_index] * 2 * nlanes;
640 do_div(pixel_rate, OV08D10_RGB_DEPTH);
645 static u64 to_pixels_per_line(const s64 *link_freq_menu, u32 hts,
646 u32 f_index, u8 nlanes)
648 u64 ppl = hts * to_rate(link_freq_menu, f_index, nlanes);
650 do_div(ppl, OV08D10_SCLK);
655 static int ov08d10_write_reg_list(struct ov08d10 *ov08d10,
656 const struct ov08d10_reg_list *r_list)
658 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
662 for (i = 0; i < r_list->num_of_regs; i++) {
663 ret = i2c_smbus_write_byte_data(client, r_list->regs[i].address,
664 r_list->regs[i].val);
666 dev_err_ratelimited(&client->dev,
667 "failed to write reg 0x%2.2x. error = %d",
668 r_list->regs[i].address, ret);
676 static int ov08d10_update_analog_gain(struct ov08d10 *ov08d10, u32 a_gain)
678 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
682 val = ((a_gain >> 3) & 0xFF);
683 /* CIS control registers */
684 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
689 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_ANALOG_GAIN, val);
693 return i2c_smbus_write_byte_data(client,
694 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
697 static int ov08d10_update_digital_gain(struct ov08d10 *ov08d10, u32 d_gain)
699 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
703 d_gain = (d_gain >> 1);
704 /* CIS control registers */
705 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
709 val = ((d_gain >> 8) & 0x3F);
711 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_C, val);
716 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MWB_DGAIN_F, val);
720 return i2c_smbus_write_byte_data(client,
721 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
724 static int ov08d10_set_exposure(struct ov08d10 *ov08d10, u32 exposure)
726 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
729 u32 hts, cur_vts, exp_cal;
732 cur_vts = ov08d10->cur_mode->vts_def;
733 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
737 hts_h = i2c_smbus_read_byte_data(client, 0x37);
738 hts_l = i2c_smbus_read_byte_data(client, 0x38);
739 hts = ((hts_h << 8) | (hts_l));
740 exp_cal = 66 * OV08D10_ROWCLK / hts;
741 exposure = exposure * exp_cal / (cur_vts - OV08D10_EXPOSURE_MAX_MARGIN);
742 /* CIS control registers */
743 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
747 /* update exposure */
748 val = ((exposure >> 16) & 0xFF);
749 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_H, val);
753 val = ((exposure >> 8) & 0xFF);
754 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_M, val);
758 val = exposure & 0xFF;
759 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_EXPOSURE_L, val);
763 return i2c_smbus_write_byte_data(client,
764 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
767 static int ov08d10_set_vblank(struct ov08d10 *ov08d10, u32 vblank)
769 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
773 /* CIS control registers */
774 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
778 val = ((vblank >> 8) & 0xFF);
780 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_H, val);
785 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_VTS_L, val);
789 return i2c_smbus_write_byte_data(client,
790 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
793 static int ov08d10_test_pattern(struct ov08d10 *ov08d10, u32 pattern)
795 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
800 val = OV08D10_TEST_PATTERN_ENABLE;
802 val = OV08D10_TEST_PATTERN_DISABLE;
804 /* CIS control registers */
805 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
809 ret = i2c_smbus_write_byte_data(client,
810 OV08D10_REG_TEST_PATTERN, val);
814 return i2c_smbus_write_byte_data(client,
815 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
818 static int ov08d10_set_ctrl_flip(struct ov08d10 *ov08d10, u32 ctrl_val)
820 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
824 /* System control registers */
825 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
829 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_FLIP_OPT);
833 val = ret | (ctrl_val & OV08D10_REG_FLIP_MASK);
835 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
839 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_FLIP_OPT, val);
844 return i2c_smbus_write_byte_data(client,
845 OV08D10_REG_GLOBAL_EFFECTIVE, 0x01);
848 static int ov08d10_set_ctrl(struct v4l2_ctrl *ctrl)
850 struct ov08d10 *ov08d10 = container_of(ctrl->handler,
851 struct ov08d10, ctrl_handler);
852 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
856 /* Propagate change of current control to all related controls */
857 if (ctrl->id == V4L2_CID_VBLANK) {
858 /* Update max exposure while meeting expected vblanking */
859 exposure_max = ov08d10->cur_mode->height + ctrl->val -
860 OV08D10_EXPOSURE_MAX_MARGIN;
861 __v4l2_ctrl_modify_range(ov08d10->exposure,
862 ov08d10->exposure->minimum,
863 exposure_max, ov08d10->exposure->step,
867 /* V4L2 controls values will be applied only when power is already up */
868 if (!pm_runtime_get_if_in_use(&client->dev))
872 case V4L2_CID_ANALOGUE_GAIN:
873 ret = ov08d10_update_analog_gain(ov08d10, ctrl->val);
876 case V4L2_CID_DIGITAL_GAIN:
877 ret = ov08d10_update_digital_gain(ov08d10, ctrl->val);
880 case V4L2_CID_EXPOSURE:
881 ret = ov08d10_set_exposure(ov08d10, ctrl->val);
884 case V4L2_CID_VBLANK:
885 ret = ov08d10_set_vblank(ov08d10, ctrl->val);
888 case V4L2_CID_TEST_PATTERN:
889 ret = ov08d10_test_pattern(ov08d10, ctrl->val);
894 ret = ov08d10_set_ctrl_flip(ov08d10,
895 ov08d10->hflip->val |
896 ov08d10->vflip->val << 1);
904 pm_runtime_put(&client->dev);
909 static const struct v4l2_ctrl_ops ov08d10_ctrl_ops = {
910 .s_ctrl = ov08d10_set_ctrl,
913 static int ov08d10_init_controls(struct ov08d10 *ov08d10)
915 struct v4l2_ctrl_handler *ctrl_hdlr;
922 const struct ov08d10_mode *mode;
925 ctrl_hdlr = &ov08d10->ctrl_handler;
926 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
930 ctrl_hdlr->lock = &ov08d10->mutex;
931 link_freq_size = ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu);
933 v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov08d10_ctrl_ops,
937 ov08d10->priv_lane->link_freq_menu);
938 if (ov08d10->link_freq)
939 ov08d10->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
941 pixel_rate_max = to_rate(ov08d10->priv_lane->link_freq_menu, 0,
942 ov08d10->cur_mode->data_lanes);
943 ov08d10->pixel_rate =
944 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
945 V4L2_CID_PIXEL_RATE, 0, pixel_rate_max, 1,
948 mode = ov08d10->cur_mode;
949 vblank_def = mode->vts_def - mode->height;
950 vblank_min = mode->vts_min - mode->height;
952 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
953 V4L2_CID_VBLANK, vblank_min,
954 OV08D10_VTS_MAX - mode->height, 1,
957 h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
958 mode->hts, mode->link_freq_index,
961 ov08d10->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
962 V4L2_CID_HBLANK, h_blank, h_blank,
965 ov08d10->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
967 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
968 OV08D10_ANAL_GAIN_MIN, OV08D10_ANAL_GAIN_MAX,
969 OV08D10_ANAL_GAIN_STEP, OV08D10_ANAL_GAIN_MIN);
971 v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
972 OV08D10_DGTL_GAIN_MIN, OV08D10_DGTL_GAIN_MAX,
973 OV08D10_DGTL_GAIN_STEP, OV08D10_DGTL_GAIN_DEFAULT);
975 exposure_max = mode->vts_def - OV08D10_EXPOSURE_MAX_MARGIN;
976 ov08d10->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
978 OV08D10_EXPOSURE_MIN,
980 OV08D10_EXPOSURE_STEP,
983 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov08d10_ctrl_ops,
984 V4L2_CID_TEST_PATTERN,
985 ARRAY_SIZE(ov08d10_test_pattern_menu) - 1,
986 0, 0, ov08d10_test_pattern_menu);
988 ov08d10->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
989 V4L2_CID_HFLIP, 0, 1, 1, 0);
991 ov08d10->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
992 ov08d10->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov08d10_ctrl_ops,
993 V4L2_CID_VFLIP, 0, 1, 1, 0);
995 ov08d10->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
997 if (ctrl_hdlr->error)
998 return ctrl_hdlr->error;
1000 ov08d10->sd.ctrl_handler = ctrl_hdlr;
1005 static void ov08d10_update_pad_format(struct ov08d10 *ov08d10,
1006 const struct ov08d10_mode *mode,
1007 struct v4l2_mbus_framefmt *fmt)
1009 fmt->width = mode->width;
1010 fmt->height = mode->height;
1011 fmt->code = ov08d10_get_format_code(ov08d10);
1012 fmt->field = V4L2_FIELD_NONE;
1015 static int ov08d10_start_streaming(struct ov08d10 *ov08d10)
1017 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1018 const struct ov08d10_reg_list *reg_list;
1019 int link_freq_index, ret;
1021 link_freq_index = ov08d10->cur_mode->link_freq_index;
1023 &ov08d10->priv_lane->link_freq_configs[link_freq_index].reg_list;
1026 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1028 dev_err(&client->dev, "failed to reset sensor");
1031 ret = i2c_smbus_write_byte_data(client, 0x20, 0x0e);
1033 dev_err(&client->dev, "failed to reset sensor");
1036 usleep_range(3000, 4000);
1037 ret = i2c_smbus_write_byte_data(client, 0x20, 0x0b);
1039 dev_err(&client->dev, "failed to reset sensor");
1043 /* update sensor setting */
1044 ret = ov08d10_write_reg_list(ov08d10, reg_list);
1046 dev_err(&client->dev, "failed to set plls");
1050 reg_list = &ov08d10->cur_mode->reg_list;
1051 ret = ov08d10_write_reg_list(ov08d10, reg_list);
1053 dev_err(&client->dev, "failed to set mode");
1057 ret = __v4l2_ctrl_handler_setup(ov08d10->sd.ctrl_handler);
1061 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1065 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
1066 OV08D10_MODE_STREAMING);
1070 return i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
1073 static void ov08d10_stop_streaming(struct ov08d10 *ov08d10)
1075 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1078 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1080 dev_err(&client->dev, "failed to stop streaming");
1083 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_MODE_SELECT,
1084 OV08D10_MODE_STANDBY);
1086 dev_err(&client->dev, "failed to stop streaming");
1090 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x01);
1092 dev_err(&client->dev, "failed to stop streaming");
1097 static int ov08d10_set_stream(struct v4l2_subdev *sd, int enable)
1099 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1100 struct i2c_client *client = v4l2_get_subdevdata(sd);
1103 mutex_lock(&ov08d10->mutex);
1105 ret = pm_runtime_resume_and_get(&client->dev);
1107 mutex_unlock(&ov08d10->mutex);
1111 ret = ov08d10_start_streaming(ov08d10);
1114 ov08d10_stop_streaming(ov08d10);
1115 pm_runtime_put(&client->dev);
1118 ov08d10_stop_streaming(ov08d10);
1119 pm_runtime_put(&client->dev);
1122 /* vflip and hflip cannot change during streaming */
1123 __v4l2_ctrl_grab(ov08d10->vflip, enable);
1124 __v4l2_ctrl_grab(ov08d10->hflip, enable);
1126 mutex_unlock(&ov08d10->mutex);
1131 static int ov08d10_set_format(struct v4l2_subdev *sd,
1132 struct v4l2_subdev_state *sd_state,
1133 struct v4l2_subdev_format *fmt)
1135 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1136 const struct ov08d10_mode *mode;
1137 s32 vblank_def, h_blank;
1140 mode = v4l2_find_nearest_size(ov08d10->priv_lane->sp_modes,
1141 ov08d10->modes_size,
1142 width, height, fmt->format.width,
1143 fmt->format.height);
1145 mutex_lock(&ov08d10->mutex);
1146 ov08d10_update_pad_format(ov08d10, mode, &fmt->format);
1147 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1148 *v4l2_subdev_state_get_format(sd_state, fmt->pad) =
1151 ov08d10->cur_mode = mode;
1152 __v4l2_ctrl_s_ctrl(ov08d10->link_freq, mode->link_freq_index);
1153 pixel_rate = to_rate(ov08d10->priv_lane->link_freq_menu,
1154 mode->link_freq_index,
1155 ov08d10->cur_mode->data_lanes);
1156 __v4l2_ctrl_s_ctrl_int64(ov08d10->pixel_rate, pixel_rate);
1158 /* Update limits and set FPS to default */
1159 vblank_def = mode->vts_def - mode->height;
1160 __v4l2_ctrl_modify_range(ov08d10->vblank,
1161 mode->vts_min - mode->height,
1162 OV08D10_VTS_MAX - mode->height, 1,
1164 __v4l2_ctrl_s_ctrl(ov08d10->vblank, vblank_def);
1165 h_blank = to_pixels_per_line(ov08d10->priv_lane->link_freq_menu,
1167 mode->link_freq_index,
1168 ov08d10->cur_mode->data_lanes)
1170 __v4l2_ctrl_modify_range(ov08d10->hblank, h_blank, h_blank, 1,
1174 mutex_unlock(&ov08d10->mutex);
1179 static int ov08d10_get_format(struct v4l2_subdev *sd,
1180 struct v4l2_subdev_state *sd_state,
1181 struct v4l2_subdev_format *fmt)
1183 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1185 mutex_lock(&ov08d10->mutex);
1186 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1187 fmt->format = *v4l2_subdev_state_get_format(sd_state,
1190 ov08d10_update_pad_format(ov08d10, ov08d10->cur_mode,
1193 mutex_unlock(&ov08d10->mutex);
1198 static int ov08d10_enum_mbus_code(struct v4l2_subdev *sd,
1199 struct v4l2_subdev_state *sd_state,
1200 struct v4l2_subdev_mbus_code_enum *code)
1202 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1204 if (code->index > 0)
1207 mutex_lock(&ov08d10->mutex);
1208 code->code = ov08d10_get_format_code(ov08d10);
1209 mutex_unlock(&ov08d10->mutex);
1214 static int ov08d10_enum_frame_size(struct v4l2_subdev *sd,
1215 struct v4l2_subdev_state *sd_state,
1216 struct v4l2_subdev_frame_size_enum *fse)
1218 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1220 if (fse->index >= ov08d10->modes_size)
1223 mutex_lock(&ov08d10->mutex);
1224 if (fse->code != ov08d10_get_format_code(ov08d10)) {
1225 mutex_unlock(&ov08d10->mutex);
1228 mutex_unlock(&ov08d10->mutex);
1230 fse->min_width = ov08d10->priv_lane->sp_modes[fse->index].width;
1231 fse->max_width = fse->min_width;
1232 fse->min_height = ov08d10->priv_lane->sp_modes[fse->index].height;
1233 fse->max_height = fse->min_height;
1238 static int ov08d10_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1240 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1242 mutex_lock(&ov08d10->mutex);
1243 ov08d10_update_pad_format(ov08d10, &ov08d10->priv_lane->sp_modes[0],
1244 v4l2_subdev_state_get_format(fh->state, 0));
1245 mutex_unlock(&ov08d10->mutex);
1250 static const struct v4l2_subdev_video_ops ov08d10_video_ops = {
1251 .s_stream = ov08d10_set_stream,
1254 static const struct v4l2_subdev_pad_ops ov08d10_pad_ops = {
1255 .set_fmt = ov08d10_set_format,
1256 .get_fmt = ov08d10_get_format,
1257 .enum_mbus_code = ov08d10_enum_mbus_code,
1258 .enum_frame_size = ov08d10_enum_frame_size,
1261 static const struct v4l2_subdev_ops ov08d10_subdev_ops = {
1262 .video = &ov08d10_video_ops,
1263 .pad = &ov08d10_pad_ops,
1266 static const struct v4l2_subdev_internal_ops ov08d10_internal_ops = {
1267 .open = ov08d10_open,
1270 static int ov08d10_identify_module(struct ov08d10 *ov08d10)
1272 struct i2c_client *client = v4l2_get_subdevdata(&ov08d10->sd);
1277 /* System control registers */
1278 ret = i2c_smbus_write_byte_data(client, OV08D10_REG_PAGE, 0x00);
1282 /* Validate the chip ID */
1283 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_0);
1289 ret = i2c_smbus_read_byte_data(client, OV08D10_REG_CHIP_ID_1);
1293 chip_id = val | ret;
1295 if ((chip_id & OV08D10_ID_MASK) != OV08D10_CHIP_ID) {
1296 dev_err(&client->dev, "unexpected sensor id(0x%04x)\n",
1304 static int ov08d10_get_hwcfg(struct ov08d10 *ov08d10, struct device *dev)
1306 struct fwnode_handle *ep;
1307 struct fwnode_handle *fwnode = dev_fwnode(dev);
1308 struct v4l2_fwnode_endpoint bus_cfg = {
1309 .bus_type = V4L2_MBUS_CSI2_DPHY
1318 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
1322 if (xvclk_rate != OV08D10_XVCLK_19_2)
1323 dev_warn(dev, "external clock rate %u is unsupported",
1326 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1330 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1331 fwnode_handle_put(ep);
1335 /* Get number of data lanes */
1336 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1337 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1338 bus_cfg.bus.mipi_csi2.num_data_lanes);
1340 goto check_hwcfg_error;
1343 dev_dbg(dev, "Using %u data lanes\n", ov08d10->cur_mode->data_lanes);
1345 ov08d10->priv_lane = &lane_cfg_2;
1346 ov08d10->modes_size = ov08d10_modes_num(ov08d10);
1348 if (!bus_cfg.nr_of_link_frequencies) {
1349 dev_err(dev, "no link frequencies defined");
1351 goto check_hwcfg_error;
1354 for (i = 0; i < ARRAY_SIZE(ov08d10->priv_lane->link_freq_menu); i++) {
1355 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1356 if (ov08d10->priv_lane->link_freq_menu[i] ==
1357 bus_cfg.link_frequencies[j])
1361 if (j == bus_cfg.nr_of_link_frequencies) {
1362 dev_err(dev, "no link frequency %lld supported",
1363 ov08d10->priv_lane->link_freq_menu[i]);
1365 goto check_hwcfg_error;
1370 v4l2_fwnode_endpoint_free(&bus_cfg);
1375 static void ov08d10_remove(struct i2c_client *client)
1377 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1378 struct ov08d10 *ov08d10 = to_ov08d10(sd);
1380 v4l2_async_unregister_subdev(sd);
1381 media_entity_cleanup(&sd->entity);
1382 v4l2_ctrl_handler_free(sd->ctrl_handler);
1383 pm_runtime_disable(&client->dev);
1384 mutex_destroy(&ov08d10->mutex);
1387 static int ov08d10_probe(struct i2c_client *client)
1389 struct ov08d10 *ov08d10;
1392 ov08d10 = devm_kzalloc(&client->dev, sizeof(*ov08d10), GFP_KERNEL);
1396 ret = ov08d10_get_hwcfg(ov08d10, &client->dev);
1398 dev_err(&client->dev, "failed to get HW configuration: %d",
1403 v4l2_i2c_subdev_init(&ov08d10->sd, client, &ov08d10_subdev_ops);
1405 ret = ov08d10_identify_module(ov08d10);
1407 dev_err(&client->dev, "failed to find sensor: %d", ret);
1411 mutex_init(&ov08d10->mutex);
1412 ov08d10->cur_mode = &ov08d10->priv_lane->sp_modes[0];
1413 ret = ov08d10_init_controls(ov08d10);
1415 dev_err(&client->dev, "failed to init controls: %d", ret);
1416 goto probe_error_v4l2_ctrl_handler_free;
1419 ov08d10->sd.internal_ops = &ov08d10_internal_ops;
1420 ov08d10->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1421 ov08d10->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1422 ov08d10->pad.flags = MEDIA_PAD_FL_SOURCE;
1423 ret = media_entity_pads_init(&ov08d10->sd.entity, 1, &ov08d10->pad);
1425 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1426 goto probe_error_v4l2_ctrl_handler_free;
1429 ret = v4l2_async_register_subdev_sensor(&ov08d10->sd);
1431 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1433 goto probe_error_media_entity_cleanup;
1437 * Device is already turned on by i2c-core with ACPI domain PM.
1438 * Enable runtime PM and turn off the device.
1440 pm_runtime_set_active(&client->dev);
1441 pm_runtime_enable(&client->dev);
1442 pm_runtime_idle(&client->dev);
1446 probe_error_media_entity_cleanup:
1447 media_entity_cleanup(&ov08d10->sd.entity);
1449 probe_error_v4l2_ctrl_handler_free:
1450 v4l2_ctrl_handler_free(ov08d10->sd.ctrl_handler);
1451 mutex_destroy(&ov08d10->mutex);
1457 static const struct acpi_device_id ov08d10_acpi_ids[] = {
1462 MODULE_DEVICE_TABLE(acpi, ov08d10_acpi_ids);
1465 static struct i2c_driver ov08d10_i2c_driver = {
1468 .acpi_match_table = ACPI_PTR(ov08d10_acpi_ids),
1470 .probe = ov08d10_probe,
1471 .remove = ov08d10_remove,
1474 module_i2c_driver(ov08d10_i2c_driver);
1477 MODULE_DESCRIPTION("OmniVision ov08d10 sensor driver");
1478 MODULE_LICENSE("GPL v2");