1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
17 #define HI556_REG_VALUE_08BIT 1
18 #define HI556_REG_VALUE_16BIT 2
19 #define HI556_REG_VALUE_24BIT 3
21 #define HI556_LINK_FREQ_437MHZ 437000000ULL
22 #define HI556_MCLK 19200000
23 #define HI556_DATA_LANES 2
24 #define HI556_RGB_DEPTH 10
26 #define HI556_REG_CHIP_ID 0x0f16
27 #define HI556_CHIP_ID 0x0556
29 #define HI556_REG_MODE_SELECT 0x0a00
30 #define HI556_MODE_STANDBY 0x0000
31 #define HI556_MODE_STREAMING 0x0100
33 /* vertical-timings from sensor */
34 #define HI556_REG_FLL 0x0006
35 #define HI556_FLL_30FPS 0x0814
36 #define HI556_FLL_30FPS_MIN 0x0814
37 #define HI556_FLL_MAX 0x7fff
39 /* horizontal-timings from sensor */
40 #define HI556_REG_LLP 0x0008
42 /* Exposure controls from sensor */
43 #define HI556_REG_EXPOSURE 0x0074
44 #define HI556_EXPOSURE_MIN 6
45 #define HI556_EXPOSURE_MAX_MARGIN 2
46 #define HI556_EXPOSURE_STEP 1
48 /* Analog gain controls from sensor */
49 #define HI556_REG_ANALOG_GAIN 0x0077
50 #define HI556_ANAL_GAIN_MIN 0
51 #define HI556_ANAL_GAIN_MAX 240
52 #define HI556_ANAL_GAIN_STEP 1
54 /* Digital gain controls from sensor */
55 #define HI556_REG_MWB_GR_GAIN 0x0078
56 #define HI556_REG_MWB_GB_GAIN 0x007a
57 #define HI556_REG_MWB_R_GAIN 0x007c
58 #define HI556_REG_MWB_B_GAIN 0x007e
59 #define HI556_DGTL_GAIN_MIN 0
60 #define HI556_DGTL_GAIN_MAX 2048
61 #define HI556_DGTL_GAIN_STEP 1
62 #define HI556_DGTL_GAIN_DEFAULT 256
64 /* Test Pattern Control */
65 #define HI556_REG_ISP 0X0a05
66 #define HI556_REG_ISP_TPG_EN 0x01
67 #define HI556_REG_TEST_PATTERN 0x0201
69 /* HI556 native and active pixel array size. */
70 #define HI556_NATIVE_WIDTH 2592U
71 #define HI556_NATIVE_HEIGHT 1944U
72 #define HI556_PIXEL_ARRAY_LEFT 0U
73 #define HI556_PIXEL_ARRAY_TOP 0U
74 #define HI556_PIXEL_ARRAY_WIDTH 2592U
75 #define HI556_PIXEL_ARRAY_HEIGHT 1944U
78 HI556_LINK_FREQ_437MHZ_INDEX,
86 struct hi556_reg_list {
88 const struct hi556_reg *regs;
91 struct hi556_link_freq_config {
92 const struct hi556_reg_list reg_list;
96 /* Frame width in pixels */
99 /* Frame height in pixels */
102 /* Analog crop rectangle. */
103 struct v4l2_rect crop;
105 /* Horizontal timining size */
108 /* Default vertical timining size */
111 /* Min vertical timining size */
114 /* Link frequency needed for this resolution */
117 /* Sensor register settings for this resolution */
118 const struct hi556_reg_list reg_list;
121 #define to_hi556(_sd) container_of(_sd, struct hi556, sd)
123 //SENSOR_INITIALIZATION
124 static const struct hi556_reg mipi_data_rate_874mbps[] = {
350 static const struct hi556_reg mode_2592x1944_regs[] = {
395 static const struct hi556_reg mode_2592x1444_regs[] = {
438 static const struct hi556_reg mode_1296x972_regs[] = {
483 static const struct hi556_reg mode_1296x722_regs[] = {
527 static const char * const hi556_test_pattern_menu[] = {
531 "Fade To Grey Colour Bars",
533 "Gradient Horizontal",
539 static const s64 link_freq_menu_items[] = {
540 HI556_LINK_FREQ_437MHZ,
543 static const struct hi556_link_freq_config link_freq_configs[] = {
544 [HI556_LINK_FREQ_437MHZ_INDEX] = {
546 .num_of_regs = ARRAY_SIZE(mipi_data_rate_874mbps),
547 .regs = mipi_data_rate_874mbps,
552 static const struct hi556_mode supported_modes[] = {
554 .width = HI556_PIXEL_ARRAY_WIDTH,
555 .height = HI556_PIXEL_ARRAY_HEIGHT,
557 .left = HI556_PIXEL_ARRAY_LEFT,
558 .top = HI556_PIXEL_ARRAY_TOP,
559 .width = HI556_PIXEL_ARRAY_WIDTH,
560 .height = HI556_PIXEL_ARRAY_HEIGHT
562 .fll_def = HI556_FLL_30FPS,
563 .fll_min = HI556_FLL_30FPS_MIN,
566 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
567 .regs = mode_2592x1944_regs,
569 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
572 .width = HI556_PIXEL_ARRAY_WIDTH,
575 .left = HI556_PIXEL_ARRAY_LEFT,
577 .width = HI556_PIXEL_ARRAY_WIDTH,
584 .num_of_regs = ARRAY_SIZE(mode_2592x1444_regs),
585 .regs = mode_2592x1444_regs,
587 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
593 .left = HI556_PIXEL_ARRAY_LEFT,
594 .top = HI556_PIXEL_ARRAY_TOP,
595 .width = HI556_PIXEL_ARRAY_WIDTH,
596 .height = HI556_PIXEL_ARRAY_HEIGHT
598 .fll_def = HI556_FLL_30FPS,
599 .fll_min = HI556_FLL_30FPS_MIN,
602 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
603 .regs = mode_1296x972_regs,
605 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
611 .left = HI556_PIXEL_ARRAY_LEFT,
613 .width = HI556_PIXEL_ARRAY_WIDTH,
616 .fll_def = HI556_FLL_30FPS,
617 .fll_min = HI556_FLL_30FPS_MIN,
620 .num_of_regs = ARRAY_SIZE(mode_1296x722_regs),
621 .regs = mode_1296x722_regs,
623 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
628 struct v4l2_subdev sd;
629 struct media_pad pad;
630 struct v4l2_ctrl_handler ctrl_handler;
633 struct v4l2_ctrl *link_freq;
634 struct v4l2_ctrl *pixel_rate;
635 struct v4l2_ctrl *vblank;
636 struct v4l2_ctrl *hblank;
637 struct v4l2_ctrl *exposure;
639 /* GPIOs, clocks, etc. */
640 struct gpio_desc *reset_gpio;
642 struct regulator *avdd;
645 const struct hi556_mode *cur_mode;
647 /* To serialize asynchronus callbacks */
650 /* True if the device has been identified */
654 static u64 to_pixel_rate(u32 f_index)
656 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * HI556_DATA_LANES;
658 do_div(pixel_rate, HI556_RGB_DEPTH);
663 static int hi556_read_reg(struct hi556 *hi556, u16 reg, u16 len, u32 *val)
665 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
666 struct i2c_msg msgs[2];
668 u8 data_buf[4] = {0};
674 put_unaligned_be16(reg, addr_buf);
675 msgs[0].addr = client->addr;
677 msgs[0].len = sizeof(addr_buf);
678 msgs[0].buf = addr_buf;
679 msgs[1].addr = client->addr;
680 msgs[1].flags = I2C_M_RD;
682 msgs[1].buf = &data_buf[4 - len];
684 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
685 if (ret != ARRAY_SIZE(msgs))
688 *val = get_unaligned_be32(data_buf);
693 static int hi556_write_reg(struct hi556 *hi556, u16 reg, u16 len, u32 val)
695 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
701 put_unaligned_be16(reg, buf);
702 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
703 if (i2c_master_send(client, buf, len + 2) != len + 2)
709 static int hi556_write_reg_list(struct hi556 *hi556,
710 const struct hi556_reg_list *r_list)
712 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
716 for (i = 0; i < r_list->num_of_regs; i++) {
717 ret = hi556_write_reg(hi556, r_list->regs[i].address,
718 HI556_REG_VALUE_16BIT,
719 r_list->regs[i].val);
721 dev_err_ratelimited(&client->dev,
722 "failed to write reg 0x%4.4x. error = %d",
723 r_list->regs[i].address, ret);
731 static int hi556_update_digital_gain(struct hi556 *hi556, u32 d_gain)
735 ret = hi556_write_reg(hi556, HI556_REG_MWB_GR_GAIN,
736 HI556_REG_VALUE_16BIT, d_gain);
740 ret = hi556_write_reg(hi556, HI556_REG_MWB_GB_GAIN,
741 HI556_REG_VALUE_16BIT, d_gain);
745 ret = hi556_write_reg(hi556, HI556_REG_MWB_R_GAIN,
746 HI556_REG_VALUE_16BIT, d_gain);
750 return hi556_write_reg(hi556, HI556_REG_MWB_B_GAIN,
751 HI556_REG_VALUE_16BIT, d_gain);
754 static int hi556_test_pattern(struct hi556 *hi556, u32 pattern)
760 ret = hi556_read_reg(hi556, HI556_REG_ISP,
761 HI556_REG_VALUE_08BIT, &val);
765 ret = hi556_write_reg(hi556, HI556_REG_ISP,
766 HI556_REG_VALUE_08BIT,
767 val | HI556_REG_ISP_TPG_EN);
772 return hi556_write_reg(hi556, HI556_REG_TEST_PATTERN,
773 HI556_REG_VALUE_08BIT, pattern);
776 static int hi556_set_ctrl(struct v4l2_ctrl *ctrl)
778 struct hi556 *hi556 = container_of(ctrl->handler,
779 struct hi556, ctrl_handler);
780 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
784 /* Propagate change of current control to all related controls */
785 if (ctrl->id == V4L2_CID_VBLANK) {
786 /* Update max exposure while meeting expected vblanking */
787 exposure_max = hi556->cur_mode->height + ctrl->val -
788 HI556_EXPOSURE_MAX_MARGIN;
789 __v4l2_ctrl_modify_range(hi556->exposure,
790 hi556->exposure->minimum,
791 exposure_max, hi556->exposure->step,
795 /* V4L2 controls values will be applied only when power is already up */
796 if (!pm_runtime_get_if_in_use(&client->dev))
800 case V4L2_CID_ANALOGUE_GAIN:
801 ret = hi556_write_reg(hi556, HI556_REG_ANALOG_GAIN,
802 HI556_REG_VALUE_16BIT, ctrl->val);
805 case V4L2_CID_DIGITAL_GAIN:
806 ret = hi556_update_digital_gain(hi556, ctrl->val);
809 case V4L2_CID_EXPOSURE:
810 ret = hi556_write_reg(hi556, HI556_REG_EXPOSURE,
811 HI556_REG_VALUE_16BIT, ctrl->val);
814 case V4L2_CID_VBLANK:
815 /* Update FLL that meets expected vertical blanking */
816 ret = hi556_write_reg(hi556, HI556_REG_FLL,
817 HI556_REG_VALUE_16BIT,
818 hi556->cur_mode->height + ctrl->val);
821 case V4L2_CID_TEST_PATTERN:
822 ret = hi556_test_pattern(hi556, ctrl->val);
830 pm_runtime_put(&client->dev);
835 static const struct v4l2_ctrl_ops hi556_ctrl_ops = {
836 .s_ctrl = hi556_set_ctrl,
839 static int hi556_init_controls(struct hi556 *hi556)
841 struct v4l2_ctrl_handler *ctrl_hdlr;
842 s64 exposure_max, h_blank;
845 ctrl_hdlr = &hi556->ctrl_handler;
846 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
850 ctrl_hdlr->lock = &hi556->mutex;
851 hi556->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &hi556_ctrl_ops,
853 ARRAY_SIZE(link_freq_menu_items) - 1,
854 0, link_freq_menu_items);
855 if (hi556->link_freq)
856 hi556->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
858 hi556->pixel_rate = v4l2_ctrl_new_std
859 (ctrl_hdlr, &hi556_ctrl_ops,
860 V4L2_CID_PIXEL_RATE, 0,
861 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX),
863 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX));
864 hi556->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
866 hi556->cur_mode->fll_min -
867 hi556->cur_mode->height,
869 hi556->cur_mode->height, 1,
870 hi556->cur_mode->fll_def -
871 hi556->cur_mode->height);
873 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
875 hi556->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
876 V4L2_CID_HBLANK, h_blank, h_blank, 1,
879 hi556->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
881 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
882 HI556_ANAL_GAIN_MIN, HI556_ANAL_GAIN_MAX,
883 HI556_ANAL_GAIN_STEP, HI556_ANAL_GAIN_MIN);
884 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
885 HI556_DGTL_GAIN_MIN, HI556_DGTL_GAIN_MAX,
886 HI556_DGTL_GAIN_STEP, HI556_DGTL_GAIN_DEFAULT);
887 exposure_max = hi556->cur_mode->fll_def - HI556_EXPOSURE_MAX_MARGIN;
888 hi556->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
890 HI556_EXPOSURE_MIN, exposure_max,
893 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &hi556_ctrl_ops,
894 V4L2_CID_TEST_PATTERN,
895 ARRAY_SIZE(hi556_test_pattern_menu) - 1,
896 0, 0, hi556_test_pattern_menu);
897 if (ctrl_hdlr->error)
898 return ctrl_hdlr->error;
900 hi556->sd.ctrl_handler = ctrl_hdlr;
905 static void hi556_assign_pad_format(const struct hi556_mode *mode,
906 struct v4l2_mbus_framefmt *fmt)
908 fmt->width = mode->width;
909 fmt->height = mode->height;
910 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
911 fmt->field = V4L2_FIELD_NONE;
914 static int hi556_identify_module(struct hi556 *hi556)
916 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
920 if (hi556->identified)
923 ret = hi556_read_reg(hi556, HI556_REG_CHIP_ID,
924 HI556_REG_VALUE_16BIT, &val);
928 if (val != HI556_CHIP_ID) {
929 dev_err(&client->dev, "chip id mismatch: %x!=%x",
934 hi556->identified = true;
939 static const struct v4l2_rect *
940 __hi556_get_pad_crop(struct hi556 *hi556,
941 struct v4l2_subdev_state *sd_state,
942 unsigned int pad, enum v4l2_subdev_format_whence which)
945 case V4L2_SUBDEV_FORMAT_TRY:
946 return v4l2_subdev_state_get_crop(sd_state, pad);
947 case V4L2_SUBDEV_FORMAT_ACTIVE:
948 return &hi556->cur_mode->crop;
954 static int hi556_get_selection(struct v4l2_subdev *sd,
955 struct v4l2_subdev_state *sd_state,
956 struct v4l2_subdev_selection *sel)
958 switch (sel->target) {
959 case V4L2_SEL_TGT_CROP: {
960 struct hi556 *hi556 = to_hi556(sd);
962 mutex_lock(&hi556->mutex);
963 sel->r = *__hi556_get_pad_crop(hi556, sd_state, sel->pad,
965 mutex_unlock(&hi556->mutex);
970 case V4L2_SEL_TGT_NATIVE_SIZE:
973 sel->r.width = HI556_NATIVE_WIDTH;
974 sel->r.height = HI556_NATIVE_HEIGHT;
978 case V4L2_SEL_TGT_CROP_DEFAULT:
979 case V4L2_SEL_TGT_CROP_BOUNDS:
980 sel->r.top = HI556_PIXEL_ARRAY_TOP;
981 sel->r.left = HI556_PIXEL_ARRAY_LEFT;
982 sel->r.width = HI556_PIXEL_ARRAY_WIDTH;
983 sel->r.height = HI556_PIXEL_ARRAY_HEIGHT;
991 static int hi556_start_streaming(struct hi556 *hi556)
993 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
994 const struct hi556_reg_list *reg_list;
995 int link_freq_index, ret;
997 ret = hi556_identify_module(hi556);
1001 link_freq_index = hi556->cur_mode->link_freq_index;
1002 reg_list = &link_freq_configs[link_freq_index].reg_list;
1003 ret = hi556_write_reg_list(hi556, reg_list);
1005 dev_err(&client->dev, "failed to set plls");
1009 reg_list = &hi556->cur_mode->reg_list;
1010 ret = hi556_write_reg_list(hi556, reg_list);
1012 dev_err(&client->dev, "failed to set mode");
1016 ret = __v4l2_ctrl_handler_setup(hi556->sd.ctrl_handler);
1020 ret = hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
1021 HI556_REG_VALUE_16BIT, HI556_MODE_STREAMING);
1024 dev_err(&client->dev, "failed to set stream");
1031 static void hi556_stop_streaming(struct hi556 *hi556)
1033 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
1035 if (hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
1036 HI556_REG_VALUE_16BIT, HI556_MODE_STANDBY))
1037 dev_err(&client->dev, "failed to set stream");
1040 static int hi556_set_stream(struct v4l2_subdev *sd, int enable)
1042 struct hi556 *hi556 = to_hi556(sd);
1043 struct i2c_client *client = v4l2_get_subdevdata(sd);
1046 mutex_lock(&hi556->mutex);
1048 ret = pm_runtime_resume_and_get(&client->dev);
1050 mutex_unlock(&hi556->mutex);
1054 ret = hi556_start_streaming(hi556);
1057 hi556_stop_streaming(hi556);
1058 pm_runtime_put(&client->dev);
1061 hi556_stop_streaming(hi556);
1062 pm_runtime_put(&client->dev);
1065 mutex_unlock(&hi556->mutex);
1070 static int hi556_set_format(struct v4l2_subdev *sd,
1071 struct v4l2_subdev_state *sd_state,
1072 struct v4l2_subdev_format *fmt)
1074 struct hi556 *hi556 = to_hi556(sd);
1075 const struct hi556_mode *mode;
1076 s32 vblank_def, h_blank;
1078 mode = v4l2_find_nearest_size(supported_modes,
1079 ARRAY_SIZE(supported_modes), width,
1080 height, fmt->format.width,
1081 fmt->format.height);
1083 mutex_lock(&hi556->mutex);
1084 hi556_assign_pad_format(mode, &fmt->format);
1085 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1086 *v4l2_subdev_state_get_format(sd_state, fmt->pad) = fmt->format;
1088 hi556->cur_mode = mode;
1089 __v4l2_ctrl_s_ctrl(hi556->link_freq, mode->link_freq_index);
1090 __v4l2_ctrl_s_ctrl_int64(hi556->pixel_rate,
1091 to_pixel_rate(mode->link_freq_index));
1093 /* Update limits and set FPS to default */
1094 vblank_def = mode->fll_def - mode->height;
1095 __v4l2_ctrl_modify_range(hi556->vblank,
1096 mode->fll_min - mode->height,
1097 HI556_FLL_MAX - mode->height, 1,
1099 __v4l2_ctrl_s_ctrl(hi556->vblank, vblank_def);
1101 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
1103 __v4l2_ctrl_modify_range(hi556->hblank, h_blank, h_blank, 1,
1107 mutex_unlock(&hi556->mutex);
1112 static int hi556_get_format(struct v4l2_subdev *sd,
1113 struct v4l2_subdev_state *sd_state,
1114 struct v4l2_subdev_format *fmt)
1116 struct hi556 *hi556 = to_hi556(sd);
1118 mutex_lock(&hi556->mutex);
1119 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1120 fmt->format = *v4l2_subdev_state_get_format(sd_state,
1123 hi556_assign_pad_format(hi556->cur_mode, &fmt->format);
1125 mutex_unlock(&hi556->mutex);
1130 static int hi556_enum_mbus_code(struct v4l2_subdev *sd,
1131 struct v4l2_subdev_state *sd_state,
1132 struct v4l2_subdev_mbus_code_enum *code)
1134 if (code->index > 0)
1137 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1142 static int hi556_enum_frame_size(struct v4l2_subdev *sd,
1143 struct v4l2_subdev_state *sd_state,
1144 struct v4l2_subdev_frame_size_enum *fse)
1146 if (fse->index >= ARRAY_SIZE(supported_modes))
1149 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1152 fse->min_width = supported_modes[fse->index].width;
1153 fse->max_width = fse->min_width;
1154 fse->min_height = supported_modes[fse->index].height;
1155 fse->max_height = fse->min_height;
1160 static int hi556_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1162 struct hi556 *hi556 = to_hi556(sd);
1163 struct v4l2_rect *try_crop;
1165 mutex_lock(&hi556->mutex);
1166 hi556_assign_pad_format(&supported_modes[0],
1167 v4l2_subdev_state_get_format(fh->state, 0));
1169 /* Initialize try_crop rectangle. */
1170 try_crop = v4l2_subdev_state_get_crop(fh->state, 0);
1171 try_crop->top = HI556_PIXEL_ARRAY_TOP;
1172 try_crop->left = HI556_PIXEL_ARRAY_LEFT;
1173 try_crop->width = HI556_PIXEL_ARRAY_WIDTH;
1174 try_crop->height = HI556_PIXEL_ARRAY_HEIGHT;
1176 mutex_unlock(&hi556->mutex);
1181 static const struct v4l2_subdev_video_ops hi556_video_ops = {
1182 .s_stream = hi556_set_stream,
1185 static const struct v4l2_subdev_pad_ops hi556_pad_ops = {
1186 .set_fmt = hi556_set_format,
1187 .get_fmt = hi556_get_format,
1188 .get_selection = hi556_get_selection,
1189 .enum_mbus_code = hi556_enum_mbus_code,
1190 .enum_frame_size = hi556_enum_frame_size,
1193 static const struct v4l2_subdev_ops hi556_subdev_ops = {
1194 .video = &hi556_video_ops,
1195 .pad = &hi556_pad_ops,
1198 static const struct media_entity_operations hi556_subdev_entity_ops = {
1199 .link_validate = v4l2_subdev_link_validate,
1202 static const struct v4l2_subdev_internal_ops hi556_internal_ops = {
1206 static int hi556_check_hwcfg(struct device *dev)
1208 struct fwnode_handle *ep;
1209 struct fwnode_handle *fwnode = dev_fwnode(dev);
1210 struct v4l2_fwnode_endpoint bus_cfg = {
1211 .bus_type = V4L2_MBUS_CSI2_DPHY
1218 * Sometimes the fwnode graph is initialized by the bridge driver,
1221 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1223 return -EPROBE_DEFER;
1225 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1226 fwnode_handle_put(ep);
1230 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1232 dev_err(dev, "can't get clock frequency");
1236 if (mclk != HI556_MCLK) {
1237 dev_err(dev, "external clock %d is not supported", mclk);
1241 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1242 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1243 bus_cfg.bus.mipi_csi2.num_data_lanes);
1245 goto check_hwcfg_error;
1248 if (!bus_cfg.nr_of_link_frequencies) {
1249 dev_err(dev, "no link frequencies defined");
1251 goto check_hwcfg_error;
1254 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1255 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1256 if (link_freq_menu_items[i] ==
1257 bus_cfg.link_frequencies[j])
1261 if (j == bus_cfg.nr_of_link_frequencies) {
1262 dev_err(dev, "no link frequency %lld supported",
1263 link_freq_menu_items[i]);
1265 goto check_hwcfg_error;
1270 v4l2_fwnode_endpoint_free(&bus_cfg);
1275 static void hi556_remove(struct i2c_client *client)
1277 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1278 struct hi556 *hi556 = to_hi556(sd);
1280 v4l2_async_unregister_subdev(sd);
1281 media_entity_cleanup(&sd->entity);
1282 v4l2_ctrl_handler_free(sd->ctrl_handler);
1283 pm_runtime_disable(&client->dev);
1284 mutex_destroy(&hi556->mutex);
1287 static int hi556_suspend(struct device *dev)
1289 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1290 struct hi556 *hi556 = to_hi556(sd);
1293 gpiod_set_value_cansleep(hi556->reset_gpio, 1);
1295 ret = regulator_disable(hi556->avdd);
1297 dev_err(dev, "failed to disable avdd: %d\n", ret);
1298 gpiod_set_value_cansleep(hi556->reset_gpio, 0);
1302 clk_disable_unprepare(hi556->clk);
1306 static int hi556_resume(struct device *dev)
1308 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1309 struct hi556 *hi556 = to_hi556(sd);
1312 ret = clk_prepare_enable(hi556->clk);
1316 ret = regulator_enable(hi556->avdd);
1318 dev_err(dev, "failed to enable avdd: %d\n", ret);
1319 clk_disable_unprepare(hi556->clk);
1323 gpiod_set_value_cansleep(hi556->reset_gpio, 0);
1324 usleep_range(5000, 5500);
1328 static int hi556_probe(struct i2c_client *client)
1330 struct hi556 *hi556;
1334 ret = hi556_check_hwcfg(&client->dev);
1336 dev_err(&client->dev, "failed to check HW configuration: %d",
1341 hi556 = devm_kzalloc(&client->dev, sizeof(*hi556), GFP_KERNEL);
1345 v4l2_i2c_subdev_init(&hi556->sd, client, &hi556_subdev_ops);
1347 hi556->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1349 if (IS_ERR(hi556->reset_gpio))
1350 return dev_err_probe(&client->dev, PTR_ERR(hi556->reset_gpio),
1351 "failed to get reset GPIO\n");
1353 hi556->clk = devm_clk_get_optional(&client->dev, "clk");
1354 if (IS_ERR(hi556->clk))
1355 return dev_err_probe(&client->dev, PTR_ERR(hi556->clk),
1356 "failed to get clock\n");
1358 /* The regulator core will provide a "dummy" regulator if necessary */
1359 hi556->avdd = devm_regulator_get(&client->dev, "avdd");
1360 if (IS_ERR(hi556->avdd))
1361 return dev_err_probe(&client->dev, PTR_ERR(hi556->avdd),
1362 "failed to get avdd regulator\n");
1364 full_power = acpi_dev_state_d0(&client->dev);
1366 /* Ensure non ACPI managed resources are enabled */
1367 ret = hi556_resume(&client->dev);
1369 return dev_err_probe(&client->dev, ret,
1370 "failed to power on sensor\n");
1372 ret = hi556_identify_module(hi556);
1374 dev_err(&client->dev, "failed to find sensor: %d", ret);
1375 goto probe_error_power_off;
1379 mutex_init(&hi556->mutex);
1380 hi556->cur_mode = &supported_modes[0];
1381 ret = hi556_init_controls(hi556);
1383 dev_err(&client->dev, "failed to init controls: %d", ret);
1384 goto probe_error_v4l2_ctrl_handler_free;
1387 hi556->sd.internal_ops = &hi556_internal_ops;
1388 hi556->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1389 hi556->sd.entity.ops = &hi556_subdev_entity_ops;
1390 hi556->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1391 hi556->pad.flags = MEDIA_PAD_FL_SOURCE;
1392 ret = media_entity_pads_init(&hi556->sd.entity, 1, &hi556->pad);
1394 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1395 goto probe_error_v4l2_ctrl_handler_free;
1398 ret = v4l2_async_register_subdev_sensor(&hi556->sd);
1400 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1402 goto probe_error_media_entity_cleanup;
1405 /* Set the device's state to active if it's in D0 state. */
1407 pm_runtime_set_active(&client->dev);
1408 pm_runtime_enable(&client->dev);
1409 pm_runtime_idle(&client->dev);
1413 probe_error_media_entity_cleanup:
1414 media_entity_cleanup(&hi556->sd.entity);
1416 probe_error_v4l2_ctrl_handler_free:
1417 v4l2_ctrl_handler_free(hi556->sd.ctrl_handler);
1418 mutex_destroy(&hi556->mutex);
1420 probe_error_power_off:
1422 hi556_suspend(&client->dev);
1427 static DEFINE_RUNTIME_DEV_PM_OPS(hi556_pm_ops, hi556_suspend, hi556_resume,
1431 static const struct acpi_device_id hi556_acpi_ids[] = {
1436 MODULE_DEVICE_TABLE(acpi, hi556_acpi_ids);
1439 static struct i2c_driver hi556_i2c_driver = {
1442 .acpi_match_table = ACPI_PTR(hi556_acpi_ids),
1443 .pm = pm_sleep_ptr(&hi556_pm_ops),
1445 .probe = hi556_probe,
1446 .remove = hi556_remove,
1447 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1450 module_i2c_driver(hi556_i2c_driver);
1452 MODULE_AUTHOR("Shawn Tu");
1453 MODULE_DESCRIPTION("Hynix HI556 sensor driver");
1454 MODULE_LICENSE("GPL v2");