1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-fwnode.h>
14 #define HI556_REG_VALUE_08BIT 1
15 #define HI556_REG_VALUE_16BIT 2
16 #define HI556_REG_VALUE_24BIT 3
18 #define HI556_LINK_FREQ_437MHZ 437000000ULL
19 #define HI556_MCLK 19200000
20 #define HI556_DATA_LANES 2
21 #define HI556_RGB_DEPTH 10
23 #define HI556_REG_CHIP_ID 0x0f16
24 #define HI556_CHIP_ID 0x0556
26 #define HI556_REG_MODE_SELECT 0x0a00
27 #define HI556_MODE_STANDBY 0x0000
28 #define HI556_MODE_STREAMING 0x0100
30 /* vertical-timings from sensor */
31 #define HI556_REG_FLL 0x0006
32 #define HI556_FLL_30FPS 0x0814
33 #define HI556_FLL_30FPS_MIN 0x0814
34 #define HI556_FLL_MAX 0x7fff
36 /* horizontal-timings from sensor */
37 #define HI556_REG_LLP 0x0008
39 /* Exposure controls from sensor */
40 #define HI556_REG_EXPOSURE 0x0074
41 #define HI556_EXPOSURE_MIN 6
42 #define HI556_EXPOSURE_MAX_MARGIN 2
43 #define HI556_EXPOSURE_STEP 1
45 /* Analog gain controls from sensor */
46 #define HI556_REG_ANALOG_GAIN 0x0077
47 #define HI556_ANAL_GAIN_MIN 0
48 #define HI556_ANAL_GAIN_MAX 240
49 #define HI556_ANAL_GAIN_STEP 1
51 /* Digital gain controls from sensor */
52 #define HI556_REG_MWB_GR_GAIN 0x0078
53 #define HI556_REG_MWB_GB_GAIN 0x007a
54 #define HI556_REG_MWB_R_GAIN 0x007c
55 #define HI556_REG_MWB_B_GAIN 0x007e
56 #define HI556_DGTL_GAIN_MIN 0
57 #define HI556_DGTL_GAIN_MAX 2048
58 #define HI556_DGTL_GAIN_STEP 1
59 #define HI556_DGTL_GAIN_DEFAULT 256
61 /* Test Pattern Control */
62 #define HI556_REG_ISP 0X0a05
63 #define HI556_REG_ISP_TPG_EN 0x01
64 #define HI556_REG_TEST_PATTERN 0x0201
66 /* HI556 native and active pixel array size. */
67 #define HI556_NATIVE_WIDTH 2592U
68 #define HI556_NATIVE_HEIGHT 1944U
69 #define HI556_PIXEL_ARRAY_LEFT 0U
70 #define HI556_PIXEL_ARRAY_TOP 0U
71 #define HI556_PIXEL_ARRAY_WIDTH 2592U
72 #define HI556_PIXEL_ARRAY_HEIGHT 1944U
75 HI556_LINK_FREQ_437MHZ_INDEX,
83 struct hi556_reg_list {
85 const struct hi556_reg *regs;
88 struct hi556_link_freq_config {
89 const struct hi556_reg_list reg_list;
93 /* Frame width in pixels */
96 /* Frame height in pixels */
99 /* Analog crop rectangle. */
100 struct v4l2_rect crop;
102 /* Horizontal timining size */
105 /* Default vertical timining size */
108 /* Min vertical timining size */
111 /* Link frequency needed for this resolution */
114 /* Sensor register settings for this resolution */
115 const struct hi556_reg_list reg_list;
118 #define to_hi556(_sd) container_of(_sd, struct hi556, sd)
120 //SENSOR_INITIALIZATION
121 static const struct hi556_reg mipi_data_rate_874mbps[] = {
347 static const struct hi556_reg mode_2592x1944_regs[] = {
392 static const struct hi556_reg mode_2592x1444_regs[] = {
435 static const struct hi556_reg mode_1296x972_regs[] = {
480 static const struct hi556_reg mode_1296x722_regs[] = {
524 static const char * const hi556_test_pattern_menu[] = {
528 "Fade To Grey Colour Bars",
530 "Gradient Horizontal",
536 static const s64 link_freq_menu_items[] = {
537 HI556_LINK_FREQ_437MHZ,
540 static const struct hi556_link_freq_config link_freq_configs[] = {
541 [HI556_LINK_FREQ_437MHZ_INDEX] = {
543 .num_of_regs = ARRAY_SIZE(mipi_data_rate_874mbps),
544 .regs = mipi_data_rate_874mbps,
549 static const struct hi556_mode supported_modes[] = {
551 .width = HI556_PIXEL_ARRAY_WIDTH,
552 .height = HI556_PIXEL_ARRAY_HEIGHT,
554 .left = HI556_PIXEL_ARRAY_LEFT,
555 .top = HI556_PIXEL_ARRAY_TOP,
556 .width = HI556_PIXEL_ARRAY_WIDTH,
557 .height = HI556_PIXEL_ARRAY_HEIGHT
559 .fll_def = HI556_FLL_30FPS,
560 .fll_min = HI556_FLL_30FPS_MIN,
563 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
564 .regs = mode_2592x1944_regs,
566 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
569 .width = HI556_PIXEL_ARRAY_WIDTH,
572 .left = HI556_PIXEL_ARRAY_LEFT,
574 .width = HI556_PIXEL_ARRAY_WIDTH,
581 .num_of_regs = ARRAY_SIZE(mode_2592x1444_regs),
582 .regs = mode_2592x1444_regs,
584 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
590 .left = HI556_PIXEL_ARRAY_LEFT,
591 .top = HI556_PIXEL_ARRAY_TOP,
592 .width = HI556_PIXEL_ARRAY_WIDTH,
593 .height = HI556_PIXEL_ARRAY_HEIGHT
595 .fll_def = HI556_FLL_30FPS,
596 .fll_min = HI556_FLL_30FPS_MIN,
599 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
600 .regs = mode_1296x972_regs,
602 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
608 .left = HI556_PIXEL_ARRAY_LEFT,
610 .width = HI556_PIXEL_ARRAY_WIDTH,
613 .fll_def = HI556_FLL_30FPS,
614 .fll_min = HI556_FLL_30FPS_MIN,
617 .num_of_regs = ARRAY_SIZE(mode_1296x722_regs),
618 .regs = mode_1296x722_regs,
620 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
625 struct v4l2_subdev sd;
626 struct media_pad pad;
627 struct v4l2_ctrl_handler ctrl_handler;
630 struct v4l2_ctrl *link_freq;
631 struct v4l2_ctrl *pixel_rate;
632 struct v4l2_ctrl *vblank;
633 struct v4l2_ctrl *hblank;
634 struct v4l2_ctrl *exposure;
637 const struct hi556_mode *cur_mode;
639 /* To serialize asynchronus callbacks */
642 /* True if the device has been identified */
646 static u64 to_pixel_rate(u32 f_index)
648 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * HI556_DATA_LANES;
650 do_div(pixel_rate, HI556_RGB_DEPTH);
655 static int hi556_read_reg(struct hi556 *hi556, u16 reg, u16 len, u32 *val)
657 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
658 struct i2c_msg msgs[2];
660 u8 data_buf[4] = {0};
666 put_unaligned_be16(reg, addr_buf);
667 msgs[0].addr = client->addr;
669 msgs[0].len = sizeof(addr_buf);
670 msgs[0].buf = addr_buf;
671 msgs[1].addr = client->addr;
672 msgs[1].flags = I2C_M_RD;
674 msgs[1].buf = &data_buf[4 - len];
676 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
677 if (ret != ARRAY_SIZE(msgs))
680 *val = get_unaligned_be32(data_buf);
685 static int hi556_write_reg(struct hi556 *hi556, u16 reg, u16 len, u32 val)
687 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
693 put_unaligned_be16(reg, buf);
694 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
695 if (i2c_master_send(client, buf, len + 2) != len + 2)
701 static int hi556_write_reg_list(struct hi556 *hi556,
702 const struct hi556_reg_list *r_list)
704 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
708 for (i = 0; i < r_list->num_of_regs; i++) {
709 ret = hi556_write_reg(hi556, r_list->regs[i].address,
710 HI556_REG_VALUE_16BIT,
711 r_list->regs[i].val);
713 dev_err_ratelimited(&client->dev,
714 "failed to write reg 0x%4.4x. error = %d",
715 r_list->regs[i].address, ret);
723 static int hi556_update_digital_gain(struct hi556 *hi556, u32 d_gain)
727 ret = hi556_write_reg(hi556, HI556_REG_MWB_GR_GAIN,
728 HI556_REG_VALUE_16BIT, d_gain);
732 ret = hi556_write_reg(hi556, HI556_REG_MWB_GB_GAIN,
733 HI556_REG_VALUE_16BIT, d_gain);
737 ret = hi556_write_reg(hi556, HI556_REG_MWB_R_GAIN,
738 HI556_REG_VALUE_16BIT, d_gain);
742 return hi556_write_reg(hi556, HI556_REG_MWB_B_GAIN,
743 HI556_REG_VALUE_16BIT, d_gain);
746 static int hi556_test_pattern(struct hi556 *hi556, u32 pattern)
752 ret = hi556_read_reg(hi556, HI556_REG_ISP,
753 HI556_REG_VALUE_08BIT, &val);
757 ret = hi556_write_reg(hi556, HI556_REG_ISP,
758 HI556_REG_VALUE_08BIT,
759 val | HI556_REG_ISP_TPG_EN);
764 return hi556_write_reg(hi556, HI556_REG_TEST_PATTERN,
765 HI556_REG_VALUE_08BIT, pattern);
768 static int hi556_set_ctrl(struct v4l2_ctrl *ctrl)
770 struct hi556 *hi556 = container_of(ctrl->handler,
771 struct hi556, ctrl_handler);
772 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
776 /* Propagate change of current control to all related controls */
777 if (ctrl->id == V4L2_CID_VBLANK) {
778 /* Update max exposure while meeting expected vblanking */
779 exposure_max = hi556->cur_mode->height + ctrl->val -
780 HI556_EXPOSURE_MAX_MARGIN;
781 __v4l2_ctrl_modify_range(hi556->exposure,
782 hi556->exposure->minimum,
783 exposure_max, hi556->exposure->step,
787 /* V4L2 controls values will be applied only when power is already up */
788 if (!pm_runtime_get_if_in_use(&client->dev))
792 case V4L2_CID_ANALOGUE_GAIN:
793 ret = hi556_write_reg(hi556, HI556_REG_ANALOG_GAIN,
794 HI556_REG_VALUE_16BIT, ctrl->val);
797 case V4L2_CID_DIGITAL_GAIN:
798 ret = hi556_update_digital_gain(hi556, ctrl->val);
801 case V4L2_CID_EXPOSURE:
802 ret = hi556_write_reg(hi556, HI556_REG_EXPOSURE,
803 HI556_REG_VALUE_16BIT, ctrl->val);
806 case V4L2_CID_VBLANK:
807 /* Update FLL that meets expected vertical blanking */
808 ret = hi556_write_reg(hi556, HI556_REG_FLL,
809 HI556_REG_VALUE_16BIT,
810 hi556->cur_mode->height + ctrl->val);
813 case V4L2_CID_TEST_PATTERN:
814 ret = hi556_test_pattern(hi556, ctrl->val);
822 pm_runtime_put(&client->dev);
827 static const struct v4l2_ctrl_ops hi556_ctrl_ops = {
828 .s_ctrl = hi556_set_ctrl,
831 static int hi556_init_controls(struct hi556 *hi556)
833 struct v4l2_ctrl_handler *ctrl_hdlr;
834 s64 exposure_max, h_blank;
837 ctrl_hdlr = &hi556->ctrl_handler;
838 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
842 ctrl_hdlr->lock = &hi556->mutex;
843 hi556->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &hi556_ctrl_ops,
845 ARRAY_SIZE(link_freq_menu_items) - 1,
846 0, link_freq_menu_items);
847 if (hi556->link_freq)
848 hi556->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
850 hi556->pixel_rate = v4l2_ctrl_new_std
851 (ctrl_hdlr, &hi556_ctrl_ops,
852 V4L2_CID_PIXEL_RATE, 0,
853 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX),
855 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX));
856 hi556->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
858 hi556->cur_mode->fll_min -
859 hi556->cur_mode->height,
861 hi556->cur_mode->height, 1,
862 hi556->cur_mode->fll_def -
863 hi556->cur_mode->height);
865 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
867 hi556->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
868 V4L2_CID_HBLANK, h_blank, h_blank, 1,
871 hi556->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
873 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
874 HI556_ANAL_GAIN_MIN, HI556_ANAL_GAIN_MAX,
875 HI556_ANAL_GAIN_STEP, HI556_ANAL_GAIN_MIN);
876 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
877 HI556_DGTL_GAIN_MIN, HI556_DGTL_GAIN_MAX,
878 HI556_DGTL_GAIN_STEP, HI556_DGTL_GAIN_DEFAULT);
879 exposure_max = hi556->cur_mode->fll_def - HI556_EXPOSURE_MAX_MARGIN;
880 hi556->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
882 HI556_EXPOSURE_MIN, exposure_max,
885 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &hi556_ctrl_ops,
886 V4L2_CID_TEST_PATTERN,
887 ARRAY_SIZE(hi556_test_pattern_menu) - 1,
888 0, 0, hi556_test_pattern_menu);
889 if (ctrl_hdlr->error)
890 return ctrl_hdlr->error;
892 hi556->sd.ctrl_handler = ctrl_hdlr;
897 static void hi556_assign_pad_format(const struct hi556_mode *mode,
898 struct v4l2_mbus_framefmt *fmt)
900 fmt->width = mode->width;
901 fmt->height = mode->height;
902 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
903 fmt->field = V4L2_FIELD_NONE;
906 static int hi556_identify_module(struct hi556 *hi556)
908 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
912 if (hi556->identified)
915 ret = hi556_read_reg(hi556, HI556_REG_CHIP_ID,
916 HI556_REG_VALUE_16BIT, &val);
920 if (val != HI556_CHIP_ID) {
921 dev_err(&client->dev, "chip id mismatch: %x!=%x",
926 hi556->identified = true;
931 static const struct v4l2_rect *
932 __hi556_get_pad_crop(struct hi556 *hi556,
933 struct v4l2_subdev_state *sd_state,
934 unsigned int pad, enum v4l2_subdev_format_whence which)
937 case V4L2_SUBDEV_FORMAT_TRY:
938 return v4l2_subdev_get_try_crop(&hi556->sd, sd_state, pad);
939 case V4L2_SUBDEV_FORMAT_ACTIVE:
940 return &hi556->cur_mode->crop;
946 static int hi556_get_selection(struct v4l2_subdev *sd,
947 struct v4l2_subdev_state *sd_state,
948 struct v4l2_subdev_selection *sel)
950 switch (sel->target) {
951 case V4L2_SEL_TGT_CROP: {
952 struct hi556 *hi556 = to_hi556(sd);
954 mutex_lock(&hi556->mutex);
955 sel->r = *__hi556_get_pad_crop(hi556, sd_state, sel->pad,
957 mutex_unlock(&hi556->mutex);
962 case V4L2_SEL_TGT_NATIVE_SIZE:
965 sel->r.width = HI556_NATIVE_WIDTH;
966 sel->r.height = HI556_NATIVE_HEIGHT;
970 case V4L2_SEL_TGT_CROP_DEFAULT:
971 case V4L2_SEL_TGT_CROP_BOUNDS:
972 sel->r.top = HI556_PIXEL_ARRAY_TOP;
973 sel->r.left = HI556_PIXEL_ARRAY_LEFT;
974 sel->r.width = HI556_PIXEL_ARRAY_WIDTH;
975 sel->r.height = HI556_PIXEL_ARRAY_HEIGHT;
983 static int hi556_start_streaming(struct hi556 *hi556)
985 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
986 const struct hi556_reg_list *reg_list;
987 int link_freq_index, ret;
989 ret = hi556_identify_module(hi556);
993 link_freq_index = hi556->cur_mode->link_freq_index;
994 reg_list = &link_freq_configs[link_freq_index].reg_list;
995 ret = hi556_write_reg_list(hi556, reg_list);
997 dev_err(&client->dev, "failed to set plls");
1001 reg_list = &hi556->cur_mode->reg_list;
1002 ret = hi556_write_reg_list(hi556, reg_list);
1004 dev_err(&client->dev, "failed to set mode");
1008 ret = __v4l2_ctrl_handler_setup(hi556->sd.ctrl_handler);
1012 ret = hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
1013 HI556_REG_VALUE_16BIT, HI556_MODE_STREAMING);
1016 dev_err(&client->dev, "failed to set stream");
1023 static void hi556_stop_streaming(struct hi556 *hi556)
1025 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
1027 if (hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
1028 HI556_REG_VALUE_16BIT, HI556_MODE_STANDBY))
1029 dev_err(&client->dev, "failed to set stream");
1032 static int hi556_set_stream(struct v4l2_subdev *sd, int enable)
1034 struct hi556 *hi556 = to_hi556(sd);
1035 struct i2c_client *client = v4l2_get_subdevdata(sd);
1038 mutex_lock(&hi556->mutex);
1040 ret = pm_runtime_resume_and_get(&client->dev);
1042 mutex_unlock(&hi556->mutex);
1046 ret = hi556_start_streaming(hi556);
1049 hi556_stop_streaming(hi556);
1050 pm_runtime_put(&client->dev);
1053 hi556_stop_streaming(hi556);
1054 pm_runtime_put(&client->dev);
1057 mutex_unlock(&hi556->mutex);
1062 static int hi556_set_format(struct v4l2_subdev *sd,
1063 struct v4l2_subdev_state *sd_state,
1064 struct v4l2_subdev_format *fmt)
1066 struct hi556 *hi556 = to_hi556(sd);
1067 const struct hi556_mode *mode;
1068 s32 vblank_def, h_blank;
1070 mode = v4l2_find_nearest_size(supported_modes,
1071 ARRAY_SIZE(supported_modes), width,
1072 height, fmt->format.width,
1073 fmt->format.height);
1075 mutex_lock(&hi556->mutex);
1076 hi556_assign_pad_format(mode, &fmt->format);
1077 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1078 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
1080 hi556->cur_mode = mode;
1081 __v4l2_ctrl_s_ctrl(hi556->link_freq, mode->link_freq_index);
1082 __v4l2_ctrl_s_ctrl_int64(hi556->pixel_rate,
1083 to_pixel_rate(mode->link_freq_index));
1085 /* Update limits and set FPS to default */
1086 vblank_def = mode->fll_def - mode->height;
1087 __v4l2_ctrl_modify_range(hi556->vblank,
1088 mode->fll_min - mode->height,
1089 HI556_FLL_MAX - mode->height, 1,
1091 __v4l2_ctrl_s_ctrl(hi556->vblank, vblank_def);
1093 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
1095 __v4l2_ctrl_modify_range(hi556->hblank, h_blank, h_blank, 1,
1099 mutex_unlock(&hi556->mutex);
1104 static int hi556_get_format(struct v4l2_subdev *sd,
1105 struct v4l2_subdev_state *sd_state,
1106 struct v4l2_subdev_format *fmt)
1108 struct hi556 *hi556 = to_hi556(sd);
1110 mutex_lock(&hi556->mutex);
1111 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1112 fmt->format = *v4l2_subdev_get_try_format(&hi556->sd,
1116 hi556_assign_pad_format(hi556->cur_mode, &fmt->format);
1118 mutex_unlock(&hi556->mutex);
1123 static int hi556_enum_mbus_code(struct v4l2_subdev *sd,
1124 struct v4l2_subdev_state *sd_state,
1125 struct v4l2_subdev_mbus_code_enum *code)
1127 if (code->index > 0)
1130 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1135 static int hi556_enum_frame_size(struct v4l2_subdev *sd,
1136 struct v4l2_subdev_state *sd_state,
1137 struct v4l2_subdev_frame_size_enum *fse)
1139 if (fse->index >= ARRAY_SIZE(supported_modes))
1142 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1145 fse->min_width = supported_modes[fse->index].width;
1146 fse->max_width = fse->min_width;
1147 fse->min_height = supported_modes[fse->index].height;
1148 fse->max_height = fse->min_height;
1153 static int hi556_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1155 struct hi556 *hi556 = to_hi556(sd);
1156 struct v4l2_rect *try_crop;
1158 mutex_lock(&hi556->mutex);
1159 hi556_assign_pad_format(&supported_modes[0],
1160 v4l2_subdev_get_try_format(sd, fh->state, 0));
1162 /* Initialize try_crop rectangle. */
1163 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
1164 try_crop->top = HI556_PIXEL_ARRAY_TOP;
1165 try_crop->left = HI556_PIXEL_ARRAY_LEFT;
1166 try_crop->width = HI556_PIXEL_ARRAY_WIDTH;
1167 try_crop->height = HI556_PIXEL_ARRAY_HEIGHT;
1169 mutex_unlock(&hi556->mutex);
1174 static const struct v4l2_subdev_video_ops hi556_video_ops = {
1175 .s_stream = hi556_set_stream,
1178 static const struct v4l2_subdev_pad_ops hi556_pad_ops = {
1179 .set_fmt = hi556_set_format,
1180 .get_fmt = hi556_get_format,
1181 .get_selection = hi556_get_selection,
1182 .enum_mbus_code = hi556_enum_mbus_code,
1183 .enum_frame_size = hi556_enum_frame_size,
1186 static const struct v4l2_subdev_ops hi556_subdev_ops = {
1187 .video = &hi556_video_ops,
1188 .pad = &hi556_pad_ops,
1191 static const struct media_entity_operations hi556_subdev_entity_ops = {
1192 .link_validate = v4l2_subdev_link_validate,
1195 static const struct v4l2_subdev_internal_ops hi556_internal_ops = {
1199 static int hi556_check_hwcfg(struct device *dev)
1201 struct fwnode_handle *ep;
1202 struct fwnode_handle *fwnode = dev_fwnode(dev);
1203 struct v4l2_fwnode_endpoint bus_cfg = {
1204 .bus_type = V4L2_MBUS_CSI2_DPHY
1213 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1215 dev_err(dev, "can't get clock frequency");
1219 if (mclk != HI556_MCLK) {
1220 dev_err(dev, "external clock %d is not supported", mclk);
1224 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1228 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1229 fwnode_handle_put(ep);
1233 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1234 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1235 bus_cfg.bus.mipi_csi2.num_data_lanes);
1237 goto check_hwcfg_error;
1240 if (!bus_cfg.nr_of_link_frequencies) {
1241 dev_err(dev, "no link frequencies defined");
1243 goto check_hwcfg_error;
1246 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1247 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1248 if (link_freq_menu_items[i] ==
1249 bus_cfg.link_frequencies[j])
1253 if (j == bus_cfg.nr_of_link_frequencies) {
1254 dev_err(dev, "no link frequency %lld supported",
1255 link_freq_menu_items[i]);
1257 goto check_hwcfg_error;
1262 v4l2_fwnode_endpoint_free(&bus_cfg);
1267 static void hi556_remove(struct i2c_client *client)
1269 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1270 struct hi556 *hi556 = to_hi556(sd);
1272 v4l2_async_unregister_subdev(sd);
1273 media_entity_cleanup(&sd->entity);
1274 v4l2_ctrl_handler_free(sd->ctrl_handler);
1275 pm_runtime_disable(&client->dev);
1276 mutex_destroy(&hi556->mutex);
1279 static int hi556_probe(struct i2c_client *client)
1281 struct hi556 *hi556;
1285 ret = hi556_check_hwcfg(&client->dev);
1287 dev_err(&client->dev, "failed to check HW configuration: %d",
1292 hi556 = devm_kzalloc(&client->dev, sizeof(*hi556), GFP_KERNEL);
1296 v4l2_i2c_subdev_init(&hi556->sd, client, &hi556_subdev_ops);
1298 full_power = acpi_dev_state_d0(&client->dev);
1300 ret = hi556_identify_module(hi556);
1302 dev_err(&client->dev, "failed to find sensor: %d", ret);
1307 mutex_init(&hi556->mutex);
1308 hi556->cur_mode = &supported_modes[0];
1309 ret = hi556_init_controls(hi556);
1311 dev_err(&client->dev, "failed to init controls: %d", ret);
1312 goto probe_error_v4l2_ctrl_handler_free;
1315 hi556->sd.internal_ops = &hi556_internal_ops;
1316 hi556->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1317 hi556->sd.entity.ops = &hi556_subdev_entity_ops;
1318 hi556->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1319 hi556->pad.flags = MEDIA_PAD_FL_SOURCE;
1320 ret = media_entity_pads_init(&hi556->sd.entity, 1, &hi556->pad);
1322 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1323 goto probe_error_v4l2_ctrl_handler_free;
1326 ret = v4l2_async_register_subdev_sensor(&hi556->sd);
1328 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1330 goto probe_error_media_entity_cleanup;
1333 /* Set the device's state to active if it's in D0 state. */
1335 pm_runtime_set_active(&client->dev);
1336 pm_runtime_enable(&client->dev);
1337 pm_runtime_idle(&client->dev);
1341 probe_error_media_entity_cleanup:
1342 media_entity_cleanup(&hi556->sd.entity);
1344 probe_error_v4l2_ctrl_handler_free:
1345 v4l2_ctrl_handler_free(hi556->sd.ctrl_handler);
1346 mutex_destroy(&hi556->mutex);
1352 static const struct acpi_device_id hi556_acpi_ids[] = {
1357 MODULE_DEVICE_TABLE(acpi, hi556_acpi_ids);
1360 static struct i2c_driver hi556_i2c_driver = {
1363 .acpi_match_table = ACPI_PTR(hi556_acpi_ids),
1365 .probe = hi556_probe,
1366 .remove = hi556_remove,
1367 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1370 module_i2c_driver(hi556_i2c_driver);
1372 MODULE_AUTHOR("Shawn Tu");
1373 MODULE_DESCRIPTION("Hynix HI556 sensor driver");
1374 MODULE_LICENSE("GPL v2");