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 char * const hi556_test_pattern_menu[] = {
484 "Fade To Grey Colour Bars",
486 "Gradient Horizontal",
492 static const s64 link_freq_menu_items[] = {
493 HI556_LINK_FREQ_437MHZ,
496 static const struct hi556_link_freq_config link_freq_configs[] = {
497 [HI556_LINK_FREQ_437MHZ_INDEX] = {
499 .num_of_regs = ARRAY_SIZE(mipi_data_rate_874mbps),
500 .regs = mipi_data_rate_874mbps,
505 static const struct hi556_mode supported_modes[] = {
507 .width = HI556_PIXEL_ARRAY_WIDTH,
508 .height = HI556_PIXEL_ARRAY_HEIGHT,
510 .left = HI556_PIXEL_ARRAY_LEFT,
511 .top = HI556_PIXEL_ARRAY_TOP,
512 .width = HI556_PIXEL_ARRAY_WIDTH,
513 .height = HI556_PIXEL_ARRAY_HEIGHT
515 .fll_def = HI556_FLL_30FPS,
516 .fll_min = HI556_FLL_30FPS_MIN,
519 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
520 .regs = mode_2592x1944_regs,
522 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
525 .width = HI556_PIXEL_ARRAY_WIDTH,
528 .left = HI556_PIXEL_ARRAY_LEFT,
530 .width = HI556_PIXEL_ARRAY_WIDTH,
537 .num_of_regs = ARRAY_SIZE(mode_2592x1444_regs),
538 .regs = mode_2592x1444_regs,
540 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
546 .left = HI556_PIXEL_ARRAY_LEFT,
547 .top = HI556_PIXEL_ARRAY_TOP,
548 .width = HI556_PIXEL_ARRAY_WIDTH,
549 .height = HI556_PIXEL_ARRAY_HEIGHT
551 .fll_def = HI556_FLL_30FPS,
552 .fll_min = HI556_FLL_30FPS_MIN,
555 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
556 .regs = mode_1296x972_regs,
558 .link_freq_index = HI556_LINK_FREQ_437MHZ_INDEX,
563 struct v4l2_subdev sd;
564 struct media_pad pad;
565 struct v4l2_ctrl_handler ctrl_handler;
568 struct v4l2_ctrl *link_freq;
569 struct v4l2_ctrl *pixel_rate;
570 struct v4l2_ctrl *vblank;
571 struct v4l2_ctrl *hblank;
572 struct v4l2_ctrl *exposure;
575 const struct hi556_mode *cur_mode;
577 /* To serialize asynchronus callbacks */
580 /* Streaming on/off */
583 /* True if the device has been identified */
587 static u64 to_pixel_rate(u32 f_index)
589 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * HI556_DATA_LANES;
591 do_div(pixel_rate, HI556_RGB_DEPTH);
596 static int hi556_read_reg(struct hi556 *hi556, u16 reg, u16 len, u32 *val)
598 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
599 struct i2c_msg msgs[2];
601 u8 data_buf[4] = {0};
607 put_unaligned_be16(reg, addr_buf);
608 msgs[0].addr = client->addr;
610 msgs[0].len = sizeof(addr_buf);
611 msgs[0].buf = addr_buf;
612 msgs[1].addr = client->addr;
613 msgs[1].flags = I2C_M_RD;
615 msgs[1].buf = &data_buf[4 - len];
617 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
618 if (ret != ARRAY_SIZE(msgs))
621 *val = get_unaligned_be32(data_buf);
626 static int hi556_write_reg(struct hi556 *hi556, u16 reg, u16 len, u32 val)
628 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
634 put_unaligned_be16(reg, buf);
635 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
636 if (i2c_master_send(client, buf, len + 2) != len + 2)
642 static int hi556_write_reg_list(struct hi556 *hi556,
643 const struct hi556_reg_list *r_list)
645 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
649 for (i = 0; i < r_list->num_of_regs; i++) {
650 ret = hi556_write_reg(hi556, r_list->regs[i].address,
651 HI556_REG_VALUE_16BIT,
652 r_list->regs[i].val);
654 dev_err_ratelimited(&client->dev,
655 "failed to write reg 0x%4.4x. error = %d",
656 r_list->regs[i].address, ret);
664 static int hi556_update_digital_gain(struct hi556 *hi556, u32 d_gain)
668 ret = hi556_write_reg(hi556, HI556_REG_MWB_GR_GAIN,
669 HI556_REG_VALUE_16BIT, d_gain);
673 ret = hi556_write_reg(hi556, HI556_REG_MWB_GB_GAIN,
674 HI556_REG_VALUE_16BIT, d_gain);
678 ret = hi556_write_reg(hi556, HI556_REG_MWB_R_GAIN,
679 HI556_REG_VALUE_16BIT, d_gain);
683 return hi556_write_reg(hi556, HI556_REG_MWB_B_GAIN,
684 HI556_REG_VALUE_16BIT, d_gain);
687 static int hi556_test_pattern(struct hi556 *hi556, u32 pattern)
693 ret = hi556_read_reg(hi556, HI556_REG_ISP,
694 HI556_REG_VALUE_08BIT, &val);
698 ret = hi556_write_reg(hi556, HI556_REG_ISP,
699 HI556_REG_VALUE_08BIT,
700 val | HI556_REG_ISP_TPG_EN);
705 return hi556_write_reg(hi556, HI556_REG_TEST_PATTERN,
706 HI556_REG_VALUE_08BIT, pattern);
709 static int hi556_set_ctrl(struct v4l2_ctrl *ctrl)
711 struct hi556 *hi556 = container_of(ctrl->handler,
712 struct hi556, ctrl_handler);
713 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
717 /* Propagate change of current control to all related controls */
718 if (ctrl->id == V4L2_CID_VBLANK) {
719 /* Update max exposure while meeting expected vblanking */
720 exposure_max = hi556->cur_mode->height + ctrl->val -
721 HI556_EXPOSURE_MAX_MARGIN;
722 __v4l2_ctrl_modify_range(hi556->exposure,
723 hi556->exposure->minimum,
724 exposure_max, hi556->exposure->step,
728 /* V4L2 controls values will be applied only when power is already up */
729 if (!pm_runtime_get_if_in_use(&client->dev))
733 case V4L2_CID_ANALOGUE_GAIN:
734 ret = hi556_write_reg(hi556, HI556_REG_ANALOG_GAIN,
735 HI556_REG_VALUE_16BIT, ctrl->val);
738 case V4L2_CID_DIGITAL_GAIN:
739 ret = hi556_update_digital_gain(hi556, ctrl->val);
742 case V4L2_CID_EXPOSURE:
743 ret = hi556_write_reg(hi556, HI556_REG_EXPOSURE,
744 HI556_REG_VALUE_16BIT, ctrl->val);
747 case V4L2_CID_VBLANK:
748 /* Update FLL that meets expected vertical blanking */
749 ret = hi556_write_reg(hi556, HI556_REG_FLL,
750 HI556_REG_VALUE_16BIT,
751 hi556->cur_mode->height + ctrl->val);
754 case V4L2_CID_TEST_PATTERN:
755 ret = hi556_test_pattern(hi556, ctrl->val);
763 pm_runtime_put(&client->dev);
768 static const struct v4l2_ctrl_ops hi556_ctrl_ops = {
769 .s_ctrl = hi556_set_ctrl,
772 static int hi556_init_controls(struct hi556 *hi556)
774 struct v4l2_ctrl_handler *ctrl_hdlr;
775 s64 exposure_max, h_blank;
778 ctrl_hdlr = &hi556->ctrl_handler;
779 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
783 ctrl_hdlr->lock = &hi556->mutex;
784 hi556->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &hi556_ctrl_ops,
786 ARRAY_SIZE(link_freq_menu_items) - 1,
787 0, link_freq_menu_items);
788 if (hi556->link_freq)
789 hi556->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
791 hi556->pixel_rate = v4l2_ctrl_new_std
792 (ctrl_hdlr, &hi556_ctrl_ops,
793 V4L2_CID_PIXEL_RATE, 0,
794 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX),
796 to_pixel_rate(HI556_LINK_FREQ_437MHZ_INDEX));
797 hi556->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
799 hi556->cur_mode->fll_min -
800 hi556->cur_mode->height,
802 hi556->cur_mode->height, 1,
803 hi556->cur_mode->fll_def -
804 hi556->cur_mode->height);
806 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
808 hi556->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
809 V4L2_CID_HBLANK, h_blank, h_blank, 1,
812 hi556->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
814 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
815 HI556_ANAL_GAIN_MIN, HI556_ANAL_GAIN_MAX,
816 HI556_ANAL_GAIN_STEP, HI556_ANAL_GAIN_MIN);
817 v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
818 HI556_DGTL_GAIN_MIN, HI556_DGTL_GAIN_MAX,
819 HI556_DGTL_GAIN_STEP, HI556_DGTL_GAIN_DEFAULT);
820 exposure_max = hi556->cur_mode->fll_def - HI556_EXPOSURE_MAX_MARGIN;
821 hi556->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &hi556_ctrl_ops,
823 HI556_EXPOSURE_MIN, exposure_max,
826 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &hi556_ctrl_ops,
827 V4L2_CID_TEST_PATTERN,
828 ARRAY_SIZE(hi556_test_pattern_menu) - 1,
829 0, 0, hi556_test_pattern_menu);
830 if (ctrl_hdlr->error)
831 return ctrl_hdlr->error;
833 hi556->sd.ctrl_handler = ctrl_hdlr;
838 static void hi556_assign_pad_format(const struct hi556_mode *mode,
839 struct v4l2_mbus_framefmt *fmt)
841 fmt->width = mode->width;
842 fmt->height = mode->height;
843 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
844 fmt->field = V4L2_FIELD_NONE;
847 static int hi556_identify_module(struct hi556 *hi556)
849 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
853 if (hi556->identified)
856 ret = hi556_read_reg(hi556, HI556_REG_CHIP_ID,
857 HI556_REG_VALUE_16BIT, &val);
861 if (val != HI556_CHIP_ID) {
862 dev_err(&client->dev, "chip id mismatch: %x!=%x",
867 hi556->identified = true;
872 static const struct v4l2_rect *
873 __hi556_get_pad_crop(struct hi556 *hi556,
874 struct v4l2_subdev_state *sd_state,
875 unsigned int pad, enum v4l2_subdev_format_whence which)
878 case V4L2_SUBDEV_FORMAT_TRY:
879 return v4l2_subdev_get_try_crop(&hi556->sd, sd_state, pad);
880 case V4L2_SUBDEV_FORMAT_ACTIVE:
881 return &hi556->cur_mode->crop;
887 static int hi556_get_selection(struct v4l2_subdev *sd,
888 struct v4l2_subdev_state *sd_state,
889 struct v4l2_subdev_selection *sel)
891 switch (sel->target) {
892 case V4L2_SEL_TGT_CROP: {
893 struct hi556 *hi556 = to_hi556(sd);
895 mutex_lock(&hi556->mutex);
896 sel->r = *__hi556_get_pad_crop(hi556, sd_state, sel->pad,
898 mutex_unlock(&hi556->mutex);
903 case V4L2_SEL_TGT_NATIVE_SIZE:
906 sel->r.width = HI556_NATIVE_WIDTH;
907 sel->r.height = HI556_NATIVE_HEIGHT;
911 case V4L2_SEL_TGT_CROP_DEFAULT:
912 case V4L2_SEL_TGT_CROP_BOUNDS:
913 sel->r.top = HI556_PIXEL_ARRAY_TOP;
914 sel->r.left = HI556_PIXEL_ARRAY_LEFT;
915 sel->r.width = HI556_PIXEL_ARRAY_WIDTH;
916 sel->r.height = HI556_PIXEL_ARRAY_HEIGHT;
924 static int hi556_start_streaming(struct hi556 *hi556)
926 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
927 const struct hi556_reg_list *reg_list;
928 int link_freq_index, ret;
930 ret = hi556_identify_module(hi556);
934 link_freq_index = hi556->cur_mode->link_freq_index;
935 reg_list = &link_freq_configs[link_freq_index].reg_list;
936 ret = hi556_write_reg_list(hi556, reg_list);
938 dev_err(&client->dev, "failed to set plls");
942 reg_list = &hi556->cur_mode->reg_list;
943 ret = hi556_write_reg_list(hi556, reg_list);
945 dev_err(&client->dev, "failed to set mode");
949 ret = __v4l2_ctrl_handler_setup(hi556->sd.ctrl_handler);
953 ret = hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
954 HI556_REG_VALUE_16BIT, HI556_MODE_STREAMING);
957 dev_err(&client->dev, "failed to set stream");
964 static void hi556_stop_streaming(struct hi556 *hi556)
966 struct i2c_client *client = v4l2_get_subdevdata(&hi556->sd);
968 if (hi556_write_reg(hi556, HI556_REG_MODE_SELECT,
969 HI556_REG_VALUE_16BIT, HI556_MODE_STANDBY))
970 dev_err(&client->dev, "failed to set stream");
973 static int hi556_set_stream(struct v4l2_subdev *sd, int enable)
975 struct hi556 *hi556 = to_hi556(sd);
976 struct i2c_client *client = v4l2_get_subdevdata(sd);
979 if (hi556->streaming == enable)
982 mutex_lock(&hi556->mutex);
984 ret = pm_runtime_resume_and_get(&client->dev);
986 mutex_unlock(&hi556->mutex);
990 ret = hi556_start_streaming(hi556);
993 hi556_stop_streaming(hi556);
994 pm_runtime_put(&client->dev);
997 hi556_stop_streaming(hi556);
998 pm_runtime_put(&client->dev);
1001 hi556->streaming = enable;
1002 mutex_unlock(&hi556->mutex);
1007 static int __maybe_unused hi556_suspend(struct device *dev)
1009 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1010 struct hi556 *hi556 = to_hi556(sd);
1012 mutex_lock(&hi556->mutex);
1013 if (hi556->streaming)
1014 hi556_stop_streaming(hi556);
1016 mutex_unlock(&hi556->mutex);
1021 static int __maybe_unused hi556_resume(struct device *dev)
1023 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1024 struct hi556 *hi556 = to_hi556(sd);
1027 mutex_lock(&hi556->mutex);
1028 if (hi556->streaming) {
1029 ret = hi556_start_streaming(hi556);
1034 mutex_unlock(&hi556->mutex);
1039 hi556_stop_streaming(hi556);
1040 hi556->streaming = 0;
1041 mutex_unlock(&hi556->mutex);
1045 static int hi556_set_format(struct v4l2_subdev *sd,
1046 struct v4l2_subdev_state *sd_state,
1047 struct v4l2_subdev_format *fmt)
1049 struct hi556 *hi556 = to_hi556(sd);
1050 const struct hi556_mode *mode;
1051 s32 vblank_def, h_blank;
1053 mode = v4l2_find_nearest_size(supported_modes,
1054 ARRAY_SIZE(supported_modes), width,
1055 height, fmt->format.width,
1056 fmt->format.height);
1058 mutex_lock(&hi556->mutex);
1059 hi556_assign_pad_format(mode, &fmt->format);
1060 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1061 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
1063 hi556->cur_mode = mode;
1064 __v4l2_ctrl_s_ctrl(hi556->link_freq, mode->link_freq_index);
1065 __v4l2_ctrl_s_ctrl_int64(hi556->pixel_rate,
1066 to_pixel_rate(mode->link_freq_index));
1068 /* Update limits and set FPS to default */
1069 vblank_def = mode->fll_def - mode->height;
1070 __v4l2_ctrl_modify_range(hi556->vblank,
1071 mode->fll_min - mode->height,
1072 HI556_FLL_MAX - mode->height, 1,
1074 __v4l2_ctrl_s_ctrl(hi556->vblank, vblank_def);
1076 h_blank = hi556->cur_mode->llp - hi556->cur_mode->width;
1078 __v4l2_ctrl_modify_range(hi556->hblank, h_blank, h_blank, 1,
1082 mutex_unlock(&hi556->mutex);
1087 static int hi556_get_format(struct v4l2_subdev *sd,
1088 struct v4l2_subdev_state *sd_state,
1089 struct v4l2_subdev_format *fmt)
1091 struct hi556 *hi556 = to_hi556(sd);
1093 mutex_lock(&hi556->mutex);
1094 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1095 fmt->format = *v4l2_subdev_get_try_format(&hi556->sd,
1099 hi556_assign_pad_format(hi556->cur_mode, &fmt->format);
1101 mutex_unlock(&hi556->mutex);
1106 static int hi556_enum_mbus_code(struct v4l2_subdev *sd,
1107 struct v4l2_subdev_state *sd_state,
1108 struct v4l2_subdev_mbus_code_enum *code)
1110 if (code->index > 0)
1113 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1118 static int hi556_enum_frame_size(struct v4l2_subdev *sd,
1119 struct v4l2_subdev_state *sd_state,
1120 struct v4l2_subdev_frame_size_enum *fse)
1122 if (fse->index >= ARRAY_SIZE(supported_modes))
1125 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1128 fse->min_width = supported_modes[fse->index].width;
1129 fse->max_width = fse->min_width;
1130 fse->min_height = supported_modes[fse->index].height;
1131 fse->max_height = fse->min_height;
1136 static int hi556_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1138 struct hi556 *hi556 = to_hi556(sd);
1139 struct v4l2_rect *try_crop;
1141 mutex_lock(&hi556->mutex);
1142 hi556_assign_pad_format(&supported_modes[0],
1143 v4l2_subdev_get_try_format(sd, fh->state, 0));
1145 /* Initialize try_crop rectangle. */
1146 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
1147 try_crop->top = HI556_PIXEL_ARRAY_TOP;
1148 try_crop->left = HI556_PIXEL_ARRAY_LEFT;
1149 try_crop->width = HI556_PIXEL_ARRAY_WIDTH;
1150 try_crop->height = HI556_PIXEL_ARRAY_HEIGHT;
1152 mutex_unlock(&hi556->mutex);
1157 static const struct v4l2_subdev_video_ops hi556_video_ops = {
1158 .s_stream = hi556_set_stream,
1161 static const struct v4l2_subdev_pad_ops hi556_pad_ops = {
1162 .set_fmt = hi556_set_format,
1163 .get_fmt = hi556_get_format,
1164 .get_selection = hi556_get_selection,
1165 .enum_mbus_code = hi556_enum_mbus_code,
1166 .enum_frame_size = hi556_enum_frame_size,
1169 static const struct v4l2_subdev_ops hi556_subdev_ops = {
1170 .video = &hi556_video_ops,
1171 .pad = &hi556_pad_ops,
1174 static const struct media_entity_operations hi556_subdev_entity_ops = {
1175 .link_validate = v4l2_subdev_link_validate,
1178 static const struct v4l2_subdev_internal_ops hi556_internal_ops = {
1182 static int hi556_check_hwcfg(struct device *dev)
1184 struct fwnode_handle *ep;
1185 struct fwnode_handle *fwnode = dev_fwnode(dev);
1186 struct v4l2_fwnode_endpoint bus_cfg = {
1187 .bus_type = V4L2_MBUS_CSI2_DPHY
1196 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1198 dev_err(dev, "can't get clock frequency");
1202 if (mclk != HI556_MCLK) {
1203 dev_err(dev, "external clock %d is not supported", mclk);
1207 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1211 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1212 fwnode_handle_put(ep);
1216 if (bus_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1217 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1218 bus_cfg.bus.mipi_csi2.num_data_lanes);
1220 goto check_hwcfg_error;
1223 if (!bus_cfg.nr_of_link_frequencies) {
1224 dev_err(dev, "no link frequencies defined");
1226 goto check_hwcfg_error;
1229 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1230 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1231 if (link_freq_menu_items[i] ==
1232 bus_cfg.link_frequencies[j])
1236 if (j == bus_cfg.nr_of_link_frequencies) {
1237 dev_err(dev, "no link frequency %lld supported",
1238 link_freq_menu_items[i]);
1240 goto check_hwcfg_error;
1245 v4l2_fwnode_endpoint_free(&bus_cfg);
1250 static void hi556_remove(struct i2c_client *client)
1252 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1253 struct hi556 *hi556 = to_hi556(sd);
1255 v4l2_async_unregister_subdev(sd);
1256 media_entity_cleanup(&sd->entity);
1257 v4l2_ctrl_handler_free(sd->ctrl_handler);
1258 pm_runtime_disable(&client->dev);
1259 mutex_destroy(&hi556->mutex);
1262 static int hi556_probe(struct i2c_client *client)
1264 struct hi556 *hi556;
1268 ret = hi556_check_hwcfg(&client->dev);
1270 dev_err(&client->dev, "failed to check HW configuration: %d",
1275 hi556 = devm_kzalloc(&client->dev, sizeof(*hi556), GFP_KERNEL);
1279 v4l2_i2c_subdev_init(&hi556->sd, client, &hi556_subdev_ops);
1281 full_power = acpi_dev_state_d0(&client->dev);
1283 ret = hi556_identify_module(hi556);
1285 dev_err(&client->dev, "failed to find sensor: %d", ret);
1290 mutex_init(&hi556->mutex);
1291 hi556->cur_mode = &supported_modes[0];
1292 ret = hi556_init_controls(hi556);
1294 dev_err(&client->dev, "failed to init controls: %d", ret);
1295 goto probe_error_v4l2_ctrl_handler_free;
1298 hi556->sd.internal_ops = &hi556_internal_ops;
1299 hi556->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1300 hi556->sd.entity.ops = &hi556_subdev_entity_ops;
1301 hi556->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1302 hi556->pad.flags = MEDIA_PAD_FL_SOURCE;
1303 ret = media_entity_pads_init(&hi556->sd.entity, 1, &hi556->pad);
1305 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1306 goto probe_error_v4l2_ctrl_handler_free;
1309 ret = v4l2_async_register_subdev_sensor(&hi556->sd);
1311 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1313 goto probe_error_media_entity_cleanup;
1316 /* Set the device's state to active if it's in D0 state. */
1318 pm_runtime_set_active(&client->dev);
1319 pm_runtime_enable(&client->dev);
1320 pm_runtime_idle(&client->dev);
1324 probe_error_media_entity_cleanup:
1325 media_entity_cleanup(&hi556->sd.entity);
1327 probe_error_v4l2_ctrl_handler_free:
1328 v4l2_ctrl_handler_free(hi556->sd.ctrl_handler);
1329 mutex_destroy(&hi556->mutex);
1334 static const struct dev_pm_ops hi556_pm_ops = {
1335 SET_SYSTEM_SLEEP_PM_OPS(hi556_suspend, hi556_resume)
1339 static const struct acpi_device_id hi556_acpi_ids[] = {
1344 MODULE_DEVICE_TABLE(acpi, hi556_acpi_ids);
1347 static struct i2c_driver hi556_i2c_driver = {
1350 .pm = &hi556_pm_ops,
1351 .acpi_match_table = ACPI_PTR(hi556_acpi_ids),
1353 .probe_new = hi556_probe,
1354 .remove = hi556_remove,
1355 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1358 module_i2c_driver(hi556_i2c_driver);
1361 MODULE_DESCRIPTION("Hynix HI556 sensor driver");
1362 MODULE_LICENSE("GPL v2");