1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/module.h>
7 #include <linux/pm_runtime.h>
8 #include <media/v4l2-ctrls.h>
9 #include <media/v4l2-device.h>
10 #include <media/v4l2-fwnode.h>
12 #define OV13B10_REG_VALUE_08BIT 1
13 #define OV13B10_REG_VALUE_16BIT 2
14 #define OV13B10_REG_VALUE_24BIT 3
16 #define OV13B10_REG_MODE_SELECT 0x0100
17 #define OV13B10_MODE_STANDBY 0x00
18 #define OV13B10_MODE_STREAMING 0x01
20 #define OV13B10_REG_SOFTWARE_RST 0x0103
21 #define OV13B10_SOFTWARE_RST 0x01
24 #define OV13B10_REG_CHIP_ID 0x300a
25 #define OV13B10_CHIP_ID 0x560d42
27 /* V_TIMING internal */
28 #define OV13B10_REG_VTS 0x380e
29 #define OV13B10_VTS_30FPS 0x0c7c
30 #define OV13B10_VTS_60FPS 0x063e
31 #define OV13B10_VTS_MAX 0x7fff
33 /* HBLANK control - read only */
34 #define OV13B10_PPL_560MHZ 4704
36 /* Exposure control */
37 #define OV13B10_REG_EXPOSURE 0x3500
38 #define OV13B10_EXPOSURE_MIN 4
39 #define OV13B10_EXPOSURE_STEP 1
40 #define OV13B10_EXPOSURE_DEFAULT 0x40
42 /* Analog gain control */
43 #define OV13B10_REG_ANALOG_GAIN 0x3508
44 #define OV13B10_ANA_GAIN_MIN 0x80
45 #define OV13B10_ANA_GAIN_MAX 0x07c0
46 #define OV13B10_ANA_GAIN_STEP 1
47 #define OV13B10_ANA_GAIN_DEFAULT 0x80
49 /* Digital gain control */
50 #define OV13B10_REG_DGTL_GAIN_H 0x350a
51 #define OV13B10_REG_DGTL_GAIN_M 0x350b
52 #define OV13B10_REG_DGTL_GAIN_L 0x350c
54 #define OV13B10_DGTL_GAIN_MIN 1024 /* Min = 1 X */
55 #define OV13B10_DGTL_GAIN_MAX (4096 - 1) /* Max = 4 X */
56 #define OV13B10_DGTL_GAIN_DEFAULT 2560 /* Default gain = 2.5 X */
57 #define OV13B10_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
59 #define OV13B10_DGTL_GAIN_L_SHIFT 6
60 #define OV13B10_DGTL_GAIN_L_MASK 0x3
61 #define OV13B10_DGTL_GAIN_M_SHIFT 2
62 #define OV13B10_DGTL_GAIN_M_MASK 0xff
63 #define OV13B10_DGTL_GAIN_H_SHIFT 10
64 #define OV13B10_DGTL_GAIN_H_MASK 0x3
66 /* Test Pattern Control */
67 #define OV13B10_REG_TEST_PATTERN 0x5080
68 #define OV13B10_TEST_PATTERN_ENABLE BIT(7)
69 #define OV13B10_TEST_PATTERN_MASK 0xf3
70 #define OV13B10_TEST_PATTERN_BAR_SHIFT 2
73 #define OV13B10_REG_FORMAT1 0x3820
74 #define OV13B10_REG_FORMAT2 0x3821
76 /* Horizontal Window Offset */
77 #define OV13B10_REG_H_WIN_OFFSET 0x3811
79 /* Vertical Window Offset */
80 #define OV13B10_REG_V_WIN_OFFSET 0x3813
87 struct ov13b10_reg_list {
89 const struct ov13b10_reg *regs;
92 /* Link frequency config */
93 struct ov13b10_link_freq_config {
96 /* registers for this link frequency */
97 struct ov13b10_reg_list reg_list;
100 /* Mode : resolution and related config&values */
101 struct ov13b10_mode {
111 /* Index of Link frequency config to be used */
113 /* Default register values */
114 struct ov13b10_reg_list reg_list;
117 /* 4208x3120 needs 1120Mbps/lane, 4 lanes */
118 static const struct ov13b10_reg mipi_data_rate_1120mbps[] = {
249 static const struct ov13b10_reg mode_4208x3120_regs[] = {
293 static const struct ov13b10_reg mode_4160x3120_regs[] = {
337 static const struct ov13b10_reg mode_4160x2340_regs[] = {
381 static const struct ov13b10_reg mode_2104x1560_regs[] = {
425 static const struct ov13b10_reg mode_2080x1170_regs[] = {
469 static const char * const ov13b10_test_pattern_menu[] = {
471 "Vertical Color Bar Type 1",
472 "Vertical Color Bar Type 2",
473 "Vertical Color Bar Type 3",
474 "Vertical Color Bar Type 4"
477 /* Configurations for supported link frequencies */
478 #define OV13B10_LINK_FREQ_560MHZ 560000000ULL
479 #define OV13B10_LINK_FREQ_INDEX_0 0
481 #define OV13B10_EXT_CLK 19200000
482 #define OV13B10_DATA_LANES 4
485 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
486 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
488 static u64 link_freq_to_pixel_rate(u64 f)
490 f *= 2 * OV13B10_DATA_LANES;
496 /* Menu items for LINK_FREQ V4L2 control */
497 static const s64 link_freq_menu_items[] = {
498 OV13B10_LINK_FREQ_560MHZ
501 /* Link frequency configs */
502 static const struct ov13b10_link_freq_config
503 link_freq_configs[] = {
505 .pixels_per_line = OV13B10_PPL_560MHZ,
507 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1120mbps),
508 .regs = mipi_data_rate_1120mbps,
514 static const struct ov13b10_mode supported_modes[] = {
518 .vts_def = OV13B10_VTS_30FPS,
519 .vts_min = OV13B10_VTS_30FPS,
521 .num_of_regs = ARRAY_SIZE(mode_4208x3120_regs),
522 .regs = mode_4208x3120_regs,
524 .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
529 .vts_def = OV13B10_VTS_30FPS,
530 .vts_min = OV13B10_VTS_30FPS,
532 .num_of_regs = ARRAY_SIZE(mode_4160x3120_regs),
533 .regs = mode_4160x3120_regs,
535 .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
540 .vts_def = OV13B10_VTS_30FPS,
541 .vts_min = OV13B10_VTS_30FPS,
543 .num_of_regs = ARRAY_SIZE(mode_4160x2340_regs),
544 .regs = mode_4160x2340_regs,
546 .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
551 .vts_def = OV13B10_VTS_60FPS,
552 .vts_min = OV13B10_VTS_60FPS,
554 .num_of_regs = ARRAY_SIZE(mode_2104x1560_regs),
555 .regs = mode_2104x1560_regs,
557 .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
562 .vts_def = OV13B10_VTS_60FPS,
563 .vts_min = OV13B10_VTS_60FPS,
565 .num_of_regs = ARRAY_SIZE(mode_2080x1170_regs),
566 .regs = mode_2080x1170_regs,
568 .link_freq_index = OV13B10_LINK_FREQ_INDEX_0,
573 struct v4l2_subdev sd;
574 struct media_pad pad;
576 struct v4l2_ctrl_handler ctrl_handler;
578 struct v4l2_ctrl *link_freq;
579 struct v4l2_ctrl *pixel_rate;
580 struct v4l2_ctrl *vblank;
581 struct v4l2_ctrl *hblank;
582 struct v4l2_ctrl *exposure;
585 const struct ov13b10_mode *cur_mode;
587 /* Mutex for serialized access */
590 /* Streaming on/off */
593 /* True if the device has been identified */
597 #define to_ov13b10(_sd) container_of(_sd, struct ov13b10, sd)
599 /* Read registers up to 4 at a time */
600 static int ov13b10_read_reg(struct ov13b10 *ov13b,
601 u16 reg, u32 len, u32 *val)
603 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
604 struct i2c_msg msgs[2];
608 __be16 reg_addr_be = cpu_to_be16(reg);
613 data_be_p = (u8 *)&data_be;
614 /* Write register address */
615 msgs[0].addr = client->addr;
618 msgs[0].buf = (u8 *)®_addr_be;
620 /* Read data from register */
621 msgs[1].addr = client->addr;
622 msgs[1].flags = I2C_M_RD;
624 msgs[1].buf = &data_be_p[4 - len];
626 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
627 if (ret != ARRAY_SIZE(msgs))
630 *val = be32_to_cpu(data_be);
635 /* Write registers up to 4 at a time */
636 static int ov13b10_write_reg(struct ov13b10 *ov13b,
637 u16 reg, u32 len, u32 __val)
639 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
650 val = cpu_to_be32(__val);
656 buf[buf_i++] = val_p[val_i++];
658 if (i2c_master_send(client, buf, len + 2) != len + 2)
664 /* Write a list of registers */
665 static int ov13b10_write_regs(struct ov13b10 *ov13b,
666 const struct ov13b10_reg *regs, u32 len)
668 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
672 for (i = 0; i < len; i++) {
673 ret = ov13b10_write_reg(ov13b, regs[i].address, 1,
676 dev_err_ratelimited(&client->dev,
677 "Failed to write reg 0x%4.4x. error = %d\n",
678 regs[i].address, ret);
687 static int ov13b10_write_reg_list(struct ov13b10 *ov13b,
688 const struct ov13b10_reg_list *r_list)
690 return ov13b10_write_regs(ov13b, r_list->regs, r_list->num_of_regs);
693 /* Open sub-device */
694 static int ov13b10_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
696 const struct ov13b10_mode *default_mode = &supported_modes[0];
697 struct ov13b10 *ov13b = to_ov13b10(sd);
698 struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_get_try_format(sd,
702 mutex_lock(&ov13b->mutex);
704 /* Initialize try_fmt */
705 try_fmt->width = default_mode->width;
706 try_fmt->height = default_mode->height;
707 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
708 try_fmt->field = V4L2_FIELD_NONE;
710 /* No crop or compose */
711 mutex_unlock(&ov13b->mutex);
716 static int ov13b10_update_digital_gain(struct ov13b10 *ov13b, u32 d_gain)
722 * 0x350C[7:6], 0x350B[7:0], 0x350A[1:0]
725 val = (d_gain & OV13B10_DGTL_GAIN_L_MASK) << OV13B10_DGTL_GAIN_L_SHIFT;
726 ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_L,
727 OV13B10_REG_VALUE_08BIT, val);
731 val = (d_gain >> OV13B10_DGTL_GAIN_M_SHIFT) & OV13B10_DGTL_GAIN_M_MASK;
732 ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_M,
733 OV13B10_REG_VALUE_08BIT, val);
737 val = (d_gain >> OV13B10_DGTL_GAIN_H_SHIFT) & OV13B10_DGTL_GAIN_H_MASK;
738 ret = ov13b10_write_reg(ov13b, OV13B10_REG_DGTL_GAIN_H,
739 OV13B10_REG_VALUE_08BIT, val);
744 static int ov13b10_enable_test_pattern(struct ov13b10 *ov13b, u32 pattern)
749 ret = ov13b10_read_reg(ov13b, OV13B10_REG_TEST_PATTERN,
750 OV13B10_REG_VALUE_08BIT, &val);
755 val &= OV13B10_TEST_PATTERN_MASK;
756 val |= ((pattern - 1) << OV13B10_TEST_PATTERN_BAR_SHIFT) |
757 OV13B10_TEST_PATTERN_ENABLE;
759 val &= ~OV13B10_TEST_PATTERN_ENABLE;
762 return ov13b10_write_reg(ov13b, OV13B10_REG_TEST_PATTERN,
763 OV13B10_REG_VALUE_08BIT, val);
766 static int ov13b10_set_ctrl_hflip(struct ov13b10 *ov13b, u32 ctrl_val)
771 ret = ov13b10_read_reg(ov13b, OV13B10_REG_FORMAT1,
772 OV13B10_REG_VALUE_08BIT, &val);
776 ret = ov13b10_write_reg(ov13b, OV13B10_REG_FORMAT1,
777 OV13B10_REG_VALUE_08BIT,
778 ctrl_val ? val & ~BIT(3) : val);
783 ret = ov13b10_read_reg(ov13b, OV13B10_REG_H_WIN_OFFSET,
784 OV13B10_REG_VALUE_08BIT, &val);
789 * Applying cropping offset to reverse the change of Bayer order
790 * after mirroring image
792 return ov13b10_write_reg(ov13b, OV13B10_REG_H_WIN_OFFSET,
793 OV13B10_REG_VALUE_08BIT,
794 ctrl_val ? ++val : val);
797 static int ov13b10_set_ctrl_vflip(struct ov13b10 *ov13b, u32 ctrl_val)
802 ret = ov13b10_read_reg(ov13b, OV13B10_REG_FORMAT1,
803 OV13B10_REG_VALUE_08BIT, &val);
807 ret = ov13b10_write_reg(ov13b, OV13B10_REG_FORMAT1,
808 OV13B10_REG_VALUE_08BIT,
809 ctrl_val ? val | BIT(4) | BIT(5) : val);
814 ret = ov13b10_read_reg(ov13b, OV13B10_REG_V_WIN_OFFSET,
815 OV13B10_REG_VALUE_08BIT, &val);
820 * Applying cropping offset to reverse the change of Bayer order
821 * after flipping image
823 return ov13b10_write_reg(ov13b, OV13B10_REG_V_WIN_OFFSET,
824 OV13B10_REG_VALUE_08BIT,
825 ctrl_val ? --val : val);
828 static int ov13b10_set_ctrl(struct v4l2_ctrl *ctrl)
830 struct ov13b10 *ov13b = container_of(ctrl->handler,
831 struct ov13b10, ctrl_handler);
832 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
836 /* Propagate change of current control to all related controls */
838 case V4L2_CID_VBLANK:
839 /* Update max exposure while meeting expected vblanking */
840 max = ov13b->cur_mode->height + ctrl->val - 8;
841 __v4l2_ctrl_modify_range(ov13b->exposure,
842 ov13b->exposure->minimum,
843 max, ov13b->exposure->step, max);
848 * Applying V4L2 control value only happens
849 * when power is up for streaming
851 if (!pm_runtime_get_if_in_use(&client->dev))
856 case V4L2_CID_ANALOGUE_GAIN:
857 ret = ov13b10_write_reg(ov13b, OV13B10_REG_ANALOG_GAIN,
858 OV13B10_REG_VALUE_16BIT,
861 case V4L2_CID_DIGITAL_GAIN:
862 ret = ov13b10_update_digital_gain(ov13b, ctrl->val);
864 case V4L2_CID_EXPOSURE:
865 ret = ov13b10_write_reg(ov13b, OV13B10_REG_EXPOSURE,
866 OV13B10_REG_VALUE_24BIT,
869 case V4L2_CID_VBLANK:
870 ret = ov13b10_write_reg(ov13b, OV13B10_REG_VTS,
871 OV13B10_REG_VALUE_16BIT,
872 ov13b->cur_mode->height
875 case V4L2_CID_TEST_PATTERN:
876 ret = ov13b10_enable_test_pattern(ov13b, ctrl->val);
879 ov13b10_set_ctrl_hflip(ov13b, ctrl->val);
882 ov13b10_set_ctrl_vflip(ov13b, ctrl->val);
885 dev_info(&client->dev,
886 "ctrl(id:0x%x,val:0x%x) is not handled\n",
887 ctrl->id, ctrl->val);
891 pm_runtime_put(&client->dev);
896 static const struct v4l2_ctrl_ops ov13b10_ctrl_ops = {
897 .s_ctrl = ov13b10_set_ctrl,
900 static int ov13b10_enum_mbus_code(struct v4l2_subdev *sd,
901 struct v4l2_subdev_state *sd_state,
902 struct v4l2_subdev_mbus_code_enum *code)
904 /* Only one bayer order(GRBG) is supported */
908 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
913 static int ov13b10_enum_frame_size(struct v4l2_subdev *sd,
914 struct v4l2_subdev_state *sd_state,
915 struct v4l2_subdev_frame_size_enum *fse)
917 if (fse->index >= ARRAY_SIZE(supported_modes))
920 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
923 fse->min_width = supported_modes[fse->index].width;
924 fse->max_width = fse->min_width;
925 fse->min_height = supported_modes[fse->index].height;
926 fse->max_height = fse->min_height;
931 static void ov13b10_update_pad_format(const struct ov13b10_mode *mode,
932 struct v4l2_subdev_format *fmt)
934 fmt->format.width = mode->width;
935 fmt->format.height = mode->height;
936 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
937 fmt->format.field = V4L2_FIELD_NONE;
940 static int ov13b10_do_get_pad_format(struct ov13b10 *ov13b,
941 struct v4l2_subdev_state *sd_state,
942 struct v4l2_subdev_format *fmt)
944 struct v4l2_mbus_framefmt *framefmt;
945 struct v4l2_subdev *sd = &ov13b->sd;
947 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
948 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
949 fmt->format = *framefmt;
951 ov13b10_update_pad_format(ov13b->cur_mode, fmt);
957 static int ov13b10_get_pad_format(struct v4l2_subdev *sd,
958 struct v4l2_subdev_state *sd_state,
959 struct v4l2_subdev_format *fmt)
961 struct ov13b10 *ov13b = to_ov13b10(sd);
964 mutex_lock(&ov13b->mutex);
965 ret = ov13b10_do_get_pad_format(ov13b, sd_state, fmt);
966 mutex_unlock(&ov13b->mutex);
972 ov13b10_set_pad_format(struct v4l2_subdev *sd,
973 struct v4l2_subdev_state *sd_state,
974 struct v4l2_subdev_format *fmt)
976 struct ov13b10 *ov13b = to_ov13b10(sd);
977 const struct ov13b10_mode *mode;
978 struct v4l2_mbus_framefmt *framefmt;
985 mutex_lock(&ov13b->mutex);
987 /* Only one raw bayer(GRBG) order is supported */
988 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
989 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
991 mode = v4l2_find_nearest_size(supported_modes,
992 ARRAY_SIZE(supported_modes),
994 fmt->format.width, fmt->format.height);
995 ov13b10_update_pad_format(mode, fmt);
996 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
997 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
998 *framefmt = fmt->format;
1000 ov13b->cur_mode = mode;
1001 __v4l2_ctrl_s_ctrl(ov13b->link_freq, mode->link_freq_index);
1002 link_freq = link_freq_menu_items[mode->link_freq_index];
1003 pixel_rate = link_freq_to_pixel_rate(link_freq);
1004 __v4l2_ctrl_s_ctrl_int64(ov13b->pixel_rate, pixel_rate);
1006 /* Update limits and set FPS to default */
1007 vblank_def = ov13b->cur_mode->vts_def -
1008 ov13b->cur_mode->height;
1009 vblank_min = ov13b->cur_mode->vts_min -
1010 ov13b->cur_mode->height;
1011 __v4l2_ctrl_modify_range(ov13b->vblank, vblank_min,
1013 - ov13b->cur_mode->height,
1016 __v4l2_ctrl_s_ctrl(ov13b->vblank, vblank_def);
1018 link_freq_configs[mode->link_freq_index].pixels_per_line
1019 - ov13b->cur_mode->width;
1020 __v4l2_ctrl_modify_range(ov13b->hblank, h_blank,
1021 h_blank, 1, h_blank);
1024 mutex_unlock(&ov13b->mutex);
1029 /* Verify chip ID */
1030 static int ov13b10_identify_module(struct ov13b10 *ov13b)
1032 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
1036 if (ov13b->identified)
1039 ret = ov13b10_read_reg(ov13b, OV13B10_REG_CHIP_ID,
1040 OV13B10_REG_VALUE_24BIT, &val);
1044 if (val != OV13B10_CHIP_ID) {
1045 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1046 OV13B10_CHIP_ID, val);
1050 ov13b->identified = true;
1055 static int ov13b10_start_streaming(struct ov13b10 *ov13b)
1057 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
1058 const struct ov13b10_reg_list *reg_list;
1059 int ret, link_freq_index;
1061 ret = ov13b10_identify_module(ov13b);
1065 /* Get out of from software reset */
1066 ret = ov13b10_write_reg(ov13b, OV13B10_REG_SOFTWARE_RST,
1067 OV13B10_REG_VALUE_08BIT, OV13B10_SOFTWARE_RST);
1069 dev_err(&client->dev, "%s failed to set powerup registers\n",
1074 link_freq_index = ov13b->cur_mode->link_freq_index;
1075 reg_list = &link_freq_configs[link_freq_index].reg_list;
1076 ret = ov13b10_write_reg_list(ov13b, reg_list);
1078 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1082 /* Apply default values of current mode */
1083 reg_list = &ov13b->cur_mode->reg_list;
1084 ret = ov13b10_write_reg_list(ov13b, reg_list);
1086 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1090 /* Apply customized values from user */
1091 ret = __v4l2_ctrl_handler_setup(ov13b->sd.ctrl_handler);
1095 return ov13b10_write_reg(ov13b, OV13B10_REG_MODE_SELECT,
1096 OV13B10_REG_VALUE_08BIT,
1097 OV13B10_MODE_STREAMING);
1100 /* Stop streaming */
1101 static int ov13b10_stop_streaming(struct ov13b10 *ov13b)
1103 return ov13b10_write_reg(ov13b, OV13B10_REG_MODE_SELECT,
1104 OV13B10_REG_VALUE_08BIT, OV13B10_MODE_STANDBY);
1107 static int ov13b10_set_stream(struct v4l2_subdev *sd, int enable)
1109 struct ov13b10 *ov13b = to_ov13b10(sd);
1110 struct i2c_client *client = v4l2_get_subdevdata(sd);
1113 mutex_lock(&ov13b->mutex);
1114 if (ov13b->streaming == enable) {
1115 mutex_unlock(&ov13b->mutex);
1120 ret = pm_runtime_resume_and_get(&client->dev);
1125 * Apply default & customized values
1126 * and then start streaming.
1128 ret = ov13b10_start_streaming(ov13b);
1132 ov13b10_stop_streaming(ov13b);
1133 pm_runtime_put(&client->dev);
1136 ov13b->streaming = enable;
1137 mutex_unlock(&ov13b->mutex);
1142 pm_runtime_put(&client->dev);
1144 mutex_unlock(&ov13b->mutex);
1149 static int __maybe_unused ov13b10_suspend(struct device *dev)
1151 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1152 struct ov13b10 *ov13b = to_ov13b10(sd);
1154 if (ov13b->streaming)
1155 ov13b10_stop_streaming(ov13b);
1160 static int __maybe_unused ov13b10_resume(struct device *dev)
1162 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1163 struct ov13b10 *ov13b = to_ov13b10(sd);
1166 if (ov13b->streaming) {
1167 ret = ov13b10_start_streaming(ov13b);
1175 ov13b10_stop_streaming(ov13b);
1176 ov13b->streaming = false;
1180 static const struct v4l2_subdev_video_ops ov13b10_video_ops = {
1181 .s_stream = ov13b10_set_stream,
1184 static const struct v4l2_subdev_pad_ops ov13b10_pad_ops = {
1185 .enum_mbus_code = ov13b10_enum_mbus_code,
1186 .get_fmt = ov13b10_get_pad_format,
1187 .set_fmt = ov13b10_set_pad_format,
1188 .enum_frame_size = ov13b10_enum_frame_size,
1191 static const struct v4l2_subdev_ops ov13b10_subdev_ops = {
1192 .video = &ov13b10_video_ops,
1193 .pad = &ov13b10_pad_ops,
1196 static const struct media_entity_operations ov13b10_subdev_entity_ops = {
1197 .link_validate = v4l2_subdev_link_validate,
1200 static const struct v4l2_subdev_internal_ops ov13b10_internal_ops = {
1201 .open = ov13b10_open,
1204 /* Initialize control handlers */
1205 static int ov13b10_init_controls(struct ov13b10 *ov13b)
1207 struct i2c_client *client = v4l2_get_subdevdata(&ov13b->sd);
1208 struct v4l2_fwnode_device_properties props;
1209 struct v4l2_ctrl_handler *ctrl_hdlr;
1216 const struct ov13b10_mode *mode;
1220 ctrl_hdlr = &ov13b->ctrl_handler;
1221 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1225 mutex_init(&ov13b->mutex);
1226 ctrl_hdlr->lock = &ov13b->mutex;
1227 max = ARRAY_SIZE(link_freq_menu_items) - 1;
1228 ov13b->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1233 link_freq_menu_items);
1234 if (ov13b->link_freq)
1235 ov13b->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1237 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1239 /* By default, PIXEL_RATE is read only */
1240 ov13b->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1241 V4L2_CID_PIXEL_RATE,
1242 pixel_rate_min, pixel_rate_max,
1245 mode = ov13b->cur_mode;
1246 vblank_def = mode->vts_def - mode->height;
1247 vblank_min = mode->vts_min - mode->height;
1248 ov13b->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1251 OV13B10_VTS_MAX - mode->height, 1,
1254 hblank = link_freq_configs[mode->link_freq_index].pixels_per_line -
1256 ov13b->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1258 hblank, hblank, 1, hblank);
1260 ov13b->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1262 exposure_max = mode->vts_def - 8;
1263 ov13b->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1265 OV13B10_EXPOSURE_MIN,
1266 exposure_max, OV13B10_EXPOSURE_STEP,
1269 v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1270 OV13B10_ANA_GAIN_MIN, OV13B10_ANA_GAIN_MAX,
1271 OV13B10_ANA_GAIN_STEP, OV13B10_ANA_GAIN_DEFAULT);
1274 v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1275 OV13B10_DGTL_GAIN_MIN, OV13B10_DGTL_GAIN_MAX,
1276 OV13B10_DGTL_GAIN_STEP, OV13B10_DGTL_GAIN_DEFAULT);
1278 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov13b10_ctrl_ops,
1279 V4L2_CID_TEST_PATTERN,
1280 ARRAY_SIZE(ov13b10_test_pattern_menu) - 1,
1281 0, 0, ov13b10_test_pattern_menu);
1283 v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1284 V4L2_CID_HFLIP, 0, 1, 1, 0);
1285 v4l2_ctrl_new_std(ctrl_hdlr, &ov13b10_ctrl_ops,
1286 V4L2_CID_VFLIP, 0, 1, 1, 0);
1288 if (ctrl_hdlr->error) {
1289 ret = ctrl_hdlr->error;
1290 dev_err(&client->dev, "%s control init failed (%d)\n",
1295 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1299 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov13b10_ctrl_ops,
1304 ov13b->sd.ctrl_handler = ctrl_hdlr;
1309 v4l2_ctrl_handler_free(ctrl_hdlr);
1310 mutex_destroy(&ov13b->mutex);
1315 static void ov13b10_free_controls(struct ov13b10 *ov13b)
1317 v4l2_ctrl_handler_free(ov13b->sd.ctrl_handler);
1318 mutex_destroy(&ov13b->mutex);
1321 static int ov13b10_check_hwcfg(struct device *dev)
1323 struct v4l2_fwnode_endpoint bus_cfg = {
1324 .bus_type = V4L2_MBUS_CSI2_DPHY
1326 struct fwnode_handle *ep;
1327 struct fwnode_handle *fwnode = dev_fwnode(dev);
1335 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1338 dev_err(dev, "can't get clock frequency");
1342 if (ext_clk != OV13B10_EXT_CLK) {
1343 dev_err(dev, "external clock %d is not supported",
1348 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1352 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1353 fwnode_handle_put(ep);
1357 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV13B10_DATA_LANES) {
1358 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1359 bus_cfg.bus.mipi_csi2.num_data_lanes);
1364 if (!bus_cfg.nr_of_link_frequencies) {
1365 dev_err(dev, "no link frequencies defined");
1370 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1371 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1372 if (link_freq_menu_items[i] ==
1373 bus_cfg.link_frequencies[j])
1377 if (j == bus_cfg.nr_of_link_frequencies) {
1378 dev_err(dev, "no link frequency %lld supported",
1379 link_freq_menu_items[i]);
1386 v4l2_fwnode_endpoint_free(&bus_cfg);
1391 static int ov13b10_probe(struct i2c_client *client)
1393 struct ov13b10 *ov13b;
1397 /* Check HW config */
1398 ret = ov13b10_check_hwcfg(&client->dev);
1400 dev_err(&client->dev, "failed to check hwcfg: %d", ret);
1404 ov13b = devm_kzalloc(&client->dev, sizeof(*ov13b), GFP_KERNEL);
1408 /* Initialize subdev */
1409 v4l2_i2c_subdev_init(&ov13b->sd, client, &ov13b10_subdev_ops);
1411 full_power = acpi_dev_state_d0(&client->dev);
1413 /* Check module identity */
1414 ret = ov13b10_identify_module(ov13b);
1416 dev_err(&client->dev, "failed to find sensor: %d\n", ret);
1421 /* Set default mode to max resolution */
1422 ov13b->cur_mode = &supported_modes[0];
1424 ret = ov13b10_init_controls(ov13b);
1428 /* Initialize subdev */
1429 ov13b->sd.internal_ops = &ov13b10_internal_ops;
1430 ov13b->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1431 ov13b->sd.entity.ops = &ov13b10_subdev_entity_ops;
1432 ov13b->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1434 /* Initialize source pad */
1435 ov13b->pad.flags = MEDIA_PAD_FL_SOURCE;
1436 ret = media_entity_pads_init(&ov13b->sd.entity, 1, &ov13b->pad);
1438 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1439 goto error_handler_free;
1444 * Device is already turned on by i2c-core with ACPI domain PM.
1445 * Enable runtime PM and turn off the device.
1447 /* Set the device's state to active if it's in D0 state. */
1449 pm_runtime_set_active(&client->dev);
1450 pm_runtime_enable(&client->dev);
1451 pm_runtime_idle(&client->dev);
1453 ret = v4l2_async_register_subdev_sensor(&ov13b->sd);
1455 goto error_media_entity_runtime_pm;
1459 error_media_entity_runtime_pm:
1460 pm_runtime_disable(&client->dev);
1462 pm_runtime_set_suspended(&client->dev);
1463 media_entity_cleanup(&ov13b->sd.entity);
1466 ov13b10_free_controls(ov13b);
1467 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1472 static void ov13b10_remove(struct i2c_client *client)
1474 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1475 struct ov13b10 *ov13b = to_ov13b10(sd);
1477 v4l2_async_unregister_subdev(sd);
1478 media_entity_cleanup(&sd->entity);
1479 ov13b10_free_controls(ov13b);
1481 pm_runtime_disable(&client->dev);
1482 pm_runtime_set_suspended(&client->dev);
1485 static const struct dev_pm_ops ov13b10_pm_ops = {
1486 SET_SYSTEM_SLEEP_PM_OPS(ov13b10_suspend, ov13b10_resume)
1490 static const struct acpi_device_id ov13b10_acpi_ids[] = {
1495 MODULE_DEVICE_TABLE(acpi, ov13b10_acpi_ids);
1498 static struct i2c_driver ov13b10_i2c_driver = {
1501 .pm = &ov13b10_pm_ops,
1502 .acpi_match_table = ACPI_PTR(ov13b10_acpi_ids),
1504 .probe_new = ov13b10_probe,
1505 .remove = ov13b10_remove,
1506 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1509 module_i2c_driver(ov13b10_i2c_driver);
1511 MODULE_AUTHOR("Kao, Arec <arec.kao@intel.com>");
1512 MODULE_DESCRIPTION("Omnivision ov13b10 sensor driver");
1513 MODULE_LICENSE("GPL v2");