1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/acpi.h>
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regulator/consumer.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-fwnode.h>
14 #include <asm/unaligned.h>
16 #define IMX258_REG_VALUE_08BIT 1
17 #define IMX258_REG_VALUE_16BIT 2
19 #define IMX258_REG_MODE_SELECT 0x0100
20 #define IMX258_MODE_STANDBY 0x00
21 #define IMX258_MODE_STREAMING 0x01
24 #define IMX258_REG_CHIP_ID 0x0016
25 #define IMX258_CHIP_ID 0x0258
27 /* V_TIMING internal */
28 #define IMX258_VTS_30FPS 0x0c50
29 #define IMX258_VTS_30FPS_2K 0x0638
30 #define IMX258_VTS_30FPS_VGA 0x034c
31 #define IMX258_VTS_MAX 0xffff
33 /* HBLANK control - read only */
34 #define IMX258_PPL_DEFAULT 5352
36 /* Exposure control */
37 #define IMX258_REG_EXPOSURE 0x0202
38 #define IMX258_EXPOSURE_MIN 4
39 #define IMX258_EXPOSURE_STEP 1
40 #define IMX258_EXPOSURE_DEFAULT 0x640
41 #define IMX258_EXPOSURE_MAX 65535
43 /* Analog gain control */
44 #define IMX258_REG_ANALOG_GAIN 0x0204
45 #define IMX258_ANA_GAIN_MIN 0
46 #define IMX258_ANA_GAIN_MAX 480
47 #define IMX258_ANA_GAIN_STEP 1
48 #define IMX258_ANA_GAIN_DEFAULT 0x0
50 /* Digital gain control */
51 #define IMX258_REG_GR_DIGITAL_GAIN 0x020e
52 #define IMX258_REG_R_DIGITAL_GAIN 0x0210
53 #define IMX258_REG_B_DIGITAL_GAIN 0x0212
54 #define IMX258_REG_GB_DIGITAL_GAIN 0x0214
55 #define IMX258_DGTL_GAIN_MIN 0
56 #define IMX258_DGTL_GAIN_MAX 4096 /* Max = 0xFFF */
57 #define IMX258_DGTL_GAIN_DEFAULT 1024
58 #define IMX258_DGTL_GAIN_STEP 1
61 #define IMX258_REG_HDR 0x0220
62 #define IMX258_HDR_ON BIT(0)
63 #define IMX258_REG_HDR_RATIO 0x0222
64 #define IMX258_HDR_RATIO_MIN 0
65 #define IMX258_HDR_RATIO_MAX 5
66 #define IMX258_HDR_RATIO_STEP 1
67 #define IMX258_HDR_RATIO_DEFAULT 0x0
69 /* Test Pattern Control */
70 #define IMX258_REG_TEST_PATTERN 0x0600
73 #define REG_MIRROR_FLIP_CONTROL 0x0101
74 #define REG_CONFIG_MIRROR_HFLIP 0x01
75 #define REG_CONFIG_MIRROR_VFLIP 0x02
76 #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
78 /* Input clock frequency in Hz */
79 #define IMX258_INPUT_CLOCK_FREQ 19200000
86 struct imx258_reg_list {
88 const struct imx258_reg *regs;
91 /* Link frequency config */
92 struct imx258_link_freq_config {
95 /* PLL registers for this link frequency */
96 struct imx258_reg_list reg_list;
99 /* Mode : resolution and related config&values */
110 /* Index of Link frequency config to be used */
112 /* Default register values */
113 struct imx258_reg_list reg_list;
116 /* 4208x3120 needs 1267Mbps/lane, 4 lanes */
117 static const struct imx258_reg mipi_data_rate_1267mbps[] = {
135 static const struct imx258_reg mipi_data_rate_640mbps[] = {
153 static const struct imx258_reg mode_4208x3120_regs[] = {
270 static const struct imx258_reg mode_2104_1560_regs[] = {
387 static const struct imx258_reg mode_1048_780_regs[] = {
505 * The supported formats.
506 * This table MUST contain 4 entries per format, to cover the various flip
507 * combinations in the order
513 static const u32 codes[] = {
515 MEDIA_BUS_FMT_SRGGB10_1X10,
516 MEDIA_BUS_FMT_SGRBG10_1X10,
517 MEDIA_BUS_FMT_SGBRG10_1X10,
518 MEDIA_BUS_FMT_SBGGR10_1X10
520 static const char * const imx258_test_pattern_menu[] = {
523 "Eight Vertical Colour Bars",
524 "Colour Bars With Fade to Grey",
525 "Pseudorandom Sequence (PN9)",
528 /* regulator supplies */
529 static const char * const imx258_supply_name[] = {
530 /* Supplies can be enabled in any order */
531 "vana", /* Analog (2.8V) supply */
532 "vdig", /* Digital Core (1.05V) supply */
533 "vif", /* IF (1.8V) supply */
536 #define IMX258_NUM_SUPPLIES ARRAY_SIZE(imx258_supply_name)
538 /* Configurations for supported link frequencies */
539 #define IMX258_LINK_FREQ_634MHZ 633600000ULL
540 #define IMX258_LINK_FREQ_320MHZ 320000000ULL
543 IMX258_LINK_FREQ_1267MBPS,
544 IMX258_LINK_FREQ_640MBPS,
548 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
549 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
551 static u64 link_freq_to_pixel_rate(u64 f)
559 /* Menu items for LINK_FREQ V4L2 control */
560 static const s64 link_freq_menu_items[] = {
561 IMX258_LINK_FREQ_634MHZ,
562 IMX258_LINK_FREQ_320MHZ,
565 /* Link frequency configs */
566 static const struct imx258_link_freq_config link_freq_configs[] = {
567 [IMX258_LINK_FREQ_1267MBPS] = {
568 .pixels_per_line = IMX258_PPL_DEFAULT,
570 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1267mbps),
571 .regs = mipi_data_rate_1267mbps,
574 [IMX258_LINK_FREQ_640MBPS] = {
575 .pixels_per_line = IMX258_PPL_DEFAULT,
577 .num_of_regs = ARRAY_SIZE(mipi_data_rate_640mbps),
578 .regs = mipi_data_rate_640mbps,
584 static const struct imx258_mode supported_modes[] = {
588 .vts_def = IMX258_VTS_30FPS,
589 .vts_min = IMX258_VTS_30FPS,
591 .num_of_regs = ARRAY_SIZE(mode_4208x3120_regs),
592 .regs = mode_4208x3120_regs,
594 .link_freq_index = IMX258_LINK_FREQ_1267MBPS,
599 .vts_def = IMX258_VTS_30FPS_2K,
600 .vts_min = IMX258_VTS_30FPS_2K,
602 .num_of_regs = ARRAY_SIZE(mode_2104_1560_regs),
603 .regs = mode_2104_1560_regs,
605 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
610 .vts_def = IMX258_VTS_30FPS_VGA,
611 .vts_min = IMX258_VTS_30FPS_VGA,
613 .num_of_regs = ARRAY_SIZE(mode_1048_780_regs),
614 .regs = mode_1048_780_regs,
616 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
621 struct v4l2_subdev sd;
622 struct media_pad pad;
624 struct v4l2_ctrl_handler ctrl_handler;
626 struct v4l2_ctrl *link_freq;
627 struct v4l2_ctrl *pixel_rate;
628 struct v4l2_ctrl *vblank;
629 struct v4l2_ctrl *hblank;
630 struct v4l2_ctrl *exposure;
631 struct v4l2_ctrl *hflip;
632 struct v4l2_ctrl *vflip;
635 const struct imx258_mode *cur_mode;
638 * Mutex for serialized access:
639 * Protect sensor module set pad format and start/stop streaming safely.
643 /* Streaming on/off */
647 struct regulator_bulk_data supplies[IMX258_NUM_SUPPLIES];
650 static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
652 return container_of(_sd, struct imx258, sd);
655 /* Read registers up to 2 at a time */
656 static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
658 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
659 struct i2c_msg msgs[2];
660 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
661 u8 data_buf[4] = { 0, };
667 /* Write register address */
668 msgs[0].addr = client->addr;
670 msgs[0].len = ARRAY_SIZE(addr_buf);
671 msgs[0].buf = addr_buf;
673 /* Read data from register */
674 msgs[1].addr = client->addr;
675 msgs[1].flags = I2C_M_RD;
677 msgs[1].buf = &data_buf[4 - len];
679 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
680 if (ret != ARRAY_SIZE(msgs))
683 *val = get_unaligned_be32(data_buf);
688 /* Write registers up to 2 at a time */
689 static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
691 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
697 put_unaligned_be16(reg, buf);
698 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
699 if (i2c_master_send(client, buf, len + 2) != len + 2)
705 /* Write a list of registers */
706 static int imx258_write_regs(struct imx258 *imx258,
707 const struct imx258_reg *regs, u32 len)
709 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
713 for (i = 0; i < len; i++) {
714 ret = imx258_write_reg(imx258, regs[i].address, 1,
719 "Failed to write reg 0x%4.4x. error = %d\n",
720 regs[i].address, ret);
729 /* Get bayer order based on flip setting. */
730 static u32 imx258_get_format_code(struct imx258 *imx258)
734 lockdep_assert_held(&imx258->mutex);
736 i = (imx258->vflip->val ? 2 : 0) |
737 (imx258->hflip->val ? 1 : 0);
741 /* Open sub-device */
742 static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
744 struct imx258 *imx258 = to_imx258(sd);
745 struct v4l2_mbus_framefmt *try_fmt =
746 v4l2_subdev_get_try_format(sd, fh->state, 0);
748 /* Initialize try_fmt */
749 try_fmt->width = supported_modes[0].width;
750 try_fmt->height = supported_modes[0].height;
751 try_fmt->code = imx258_get_format_code(imx258);
752 try_fmt->field = V4L2_FIELD_NONE;
757 static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
761 ret = imx258_write_reg(imx258, IMX258_REG_GR_DIGITAL_GAIN,
762 IMX258_REG_VALUE_16BIT,
766 ret = imx258_write_reg(imx258, IMX258_REG_GB_DIGITAL_GAIN,
767 IMX258_REG_VALUE_16BIT,
771 ret = imx258_write_reg(imx258, IMX258_REG_R_DIGITAL_GAIN,
772 IMX258_REG_VALUE_16BIT,
776 ret = imx258_write_reg(imx258, IMX258_REG_B_DIGITAL_GAIN,
777 IMX258_REG_VALUE_16BIT,
784 static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
786 struct imx258 *imx258 =
787 container_of(ctrl->handler, struct imx258, ctrl_handler);
788 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
792 * Applying V4L2 control value only happens
793 * when power is up for streaming
795 if (pm_runtime_get_if_in_use(&client->dev) == 0)
799 case V4L2_CID_ANALOGUE_GAIN:
800 ret = imx258_write_reg(imx258, IMX258_REG_ANALOG_GAIN,
801 IMX258_REG_VALUE_16BIT,
804 case V4L2_CID_EXPOSURE:
805 ret = imx258_write_reg(imx258, IMX258_REG_EXPOSURE,
806 IMX258_REG_VALUE_16BIT,
809 case V4L2_CID_DIGITAL_GAIN:
810 ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
813 case V4L2_CID_TEST_PATTERN:
814 ret = imx258_write_reg(imx258, IMX258_REG_TEST_PATTERN,
815 IMX258_REG_VALUE_16BIT,
818 case V4L2_CID_WIDE_DYNAMIC_RANGE:
820 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
821 IMX258_REG_VALUE_08BIT,
822 IMX258_HDR_RATIO_MIN);
824 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
825 IMX258_REG_VALUE_08BIT,
829 ret = imx258_write_reg(imx258, IMX258_REG_HDR_RATIO,
830 IMX258_REG_VALUE_08BIT,
831 BIT(IMX258_HDR_RATIO_MAX));
836 ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
837 IMX258_REG_VALUE_08BIT,
838 (imx258->hflip->val ?
839 REG_CONFIG_MIRROR_HFLIP : 0) |
840 (imx258->vflip->val ?
841 REG_CONFIG_MIRROR_VFLIP : 0));
844 dev_info(&client->dev,
845 "ctrl(id:0x%x,val:0x%x) is not handled\n",
846 ctrl->id, ctrl->val);
851 pm_runtime_put(&client->dev);
856 static const struct v4l2_ctrl_ops imx258_ctrl_ops = {
857 .s_ctrl = imx258_set_ctrl,
860 static int imx258_enum_mbus_code(struct v4l2_subdev *sd,
861 struct v4l2_subdev_state *sd_state,
862 struct v4l2_subdev_mbus_code_enum *code)
864 struct imx258 *imx258 = to_imx258(sd);
866 /* Only one bayer format (10 bit) is supported */
870 code->code = imx258_get_format_code(imx258);
875 static int imx258_enum_frame_size(struct v4l2_subdev *sd,
876 struct v4l2_subdev_state *sd_state,
877 struct v4l2_subdev_frame_size_enum *fse)
879 struct imx258 *imx258 = to_imx258(sd);
880 if (fse->index >= ARRAY_SIZE(supported_modes))
883 if (fse->code != imx258_get_format_code(imx258))
886 fse->min_width = supported_modes[fse->index].width;
887 fse->max_width = fse->min_width;
888 fse->min_height = supported_modes[fse->index].height;
889 fse->max_height = fse->min_height;
894 static void imx258_update_pad_format(struct imx258 *imx258,
895 const struct imx258_mode *mode,
896 struct v4l2_subdev_format *fmt)
898 fmt->format.width = mode->width;
899 fmt->format.height = mode->height;
900 fmt->format.code = imx258_get_format_code(imx258);
901 fmt->format.field = V4L2_FIELD_NONE;
904 static int __imx258_get_pad_format(struct imx258 *imx258,
905 struct v4l2_subdev_state *sd_state,
906 struct v4l2_subdev_format *fmt)
908 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
909 fmt->format = *v4l2_subdev_get_try_format(&imx258->sd,
913 imx258_update_pad_format(imx258, imx258->cur_mode, fmt);
918 static int imx258_get_pad_format(struct v4l2_subdev *sd,
919 struct v4l2_subdev_state *sd_state,
920 struct v4l2_subdev_format *fmt)
922 struct imx258 *imx258 = to_imx258(sd);
925 mutex_lock(&imx258->mutex);
926 ret = __imx258_get_pad_format(imx258, sd_state, fmt);
927 mutex_unlock(&imx258->mutex);
932 static int imx258_set_pad_format(struct v4l2_subdev *sd,
933 struct v4l2_subdev_state *sd_state,
934 struct v4l2_subdev_format *fmt)
936 struct imx258 *imx258 = to_imx258(sd);
937 const struct imx258_mode *mode;
938 struct v4l2_mbus_framefmt *framefmt;
945 mutex_lock(&imx258->mutex);
947 fmt->format.code = imx258_get_format_code(imx258);
949 mode = v4l2_find_nearest_size(supported_modes,
950 ARRAY_SIZE(supported_modes), width, height,
951 fmt->format.width, fmt->format.height);
952 imx258_update_pad_format(imx258, mode, fmt);
953 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
954 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
955 *framefmt = fmt->format;
957 imx258->cur_mode = mode;
958 __v4l2_ctrl_s_ctrl(imx258->link_freq, mode->link_freq_index);
960 link_freq = link_freq_menu_items[mode->link_freq_index];
961 pixel_rate = link_freq_to_pixel_rate(link_freq);
962 __v4l2_ctrl_s_ctrl_int64(imx258->pixel_rate, pixel_rate);
963 /* Update limits and set FPS to default */
964 vblank_def = imx258->cur_mode->vts_def -
965 imx258->cur_mode->height;
966 vblank_min = imx258->cur_mode->vts_min -
967 imx258->cur_mode->height;
968 __v4l2_ctrl_modify_range(
969 imx258->vblank, vblank_min,
970 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
972 __v4l2_ctrl_s_ctrl(imx258->vblank, vblank_def);
974 link_freq_configs[mode->link_freq_index].pixels_per_line
975 - imx258->cur_mode->width;
976 __v4l2_ctrl_modify_range(imx258->hblank, h_blank,
977 h_blank, 1, h_blank);
980 mutex_unlock(&imx258->mutex);
985 /* Start streaming */
986 static int imx258_start_streaming(struct imx258 *imx258)
988 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
989 const struct imx258_reg_list *reg_list;
990 int ret, link_freq_index;
993 link_freq_index = imx258->cur_mode->link_freq_index;
994 reg_list = &link_freq_configs[link_freq_index].reg_list;
995 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
997 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1001 /* Apply default values of current mode */
1002 reg_list = &imx258->cur_mode->reg_list;
1003 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
1005 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1009 /* Apply customized values from user */
1010 ret = __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
1014 /* set stream on register */
1015 return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
1016 IMX258_REG_VALUE_08BIT,
1017 IMX258_MODE_STREAMING);
1020 /* Stop streaming */
1021 static int imx258_stop_streaming(struct imx258 *imx258)
1023 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1026 /* set stream off register */
1027 ret = imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
1028 IMX258_REG_VALUE_08BIT, IMX258_MODE_STANDBY);
1030 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1033 * Return success even if it was an error, as there is nothing the
1034 * caller can do about it.
1039 static int imx258_power_on(struct device *dev)
1041 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1042 struct imx258 *imx258 = to_imx258(sd);
1045 ret = regulator_bulk_enable(IMX258_NUM_SUPPLIES,
1048 dev_err(dev, "%s: failed to enable regulators\n",
1053 ret = clk_prepare_enable(imx258->clk);
1055 dev_err(dev, "failed to enable clock\n");
1056 regulator_bulk_disable(IMX258_NUM_SUPPLIES, imx258->supplies);
1062 static int imx258_power_off(struct device *dev)
1064 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1065 struct imx258 *imx258 = to_imx258(sd);
1067 clk_disable_unprepare(imx258->clk);
1068 regulator_bulk_disable(IMX258_NUM_SUPPLIES, imx258->supplies);
1073 static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
1075 struct imx258 *imx258 = to_imx258(sd);
1076 struct i2c_client *client = v4l2_get_subdevdata(sd);
1079 mutex_lock(&imx258->mutex);
1080 if (imx258->streaming == enable) {
1081 mutex_unlock(&imx258->mutex);
1086 ret = pm_runtime_resume_and_get(&client->dev);
1091 * Apply default & customized values
1092 * and then start streaming.
1094 ret = imx258_start_streaming(imx258);
1098 imx258_stop_streaming(imx258);
1099 pm_runtime_put(&client->dev);
1102 imx258->streaming = enable;
1103 mutex_unlock(&imx258->mutex);
1108 pm_runtime_put(&client->dev);
1110 mutex_unlock(&imx258->mutex);
1115 static int __maybe_unused imx258_suspend(struct device *dev)
1117 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1118 struct imx258 *imx258 = to_imx258(sd);
1120 if (imx258->streaming)
1121 imx258_stop_streaming(imx258);
1126 static int __maybe_unused imx258_resume(struct device *dev)
1128 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1129 struct imx258 *imx258 = to_imx258(sd);
1132 if (imx258->streaming) {
1133 ret = imx258_start_streaming(imx258);
1141 imx258_stop_streaming(imx258);
1142 imx258->streaming = 0;
1146 /* Verify chip ID */
1147 static int imx258_identify_module(struct imx258 *imx258)
1149 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1153 ret = imx258_read_reg(imx258, IMX258_REG_CHIP_ID,
1154 IMX258_REG_VALUE_16BIT, &val);
1156 dev_err(&client->dev, "failed to read chip id %x\n",
1161 if (val != IMX258_CHIP_ID) {
1162 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1163 IMX258_CHIP_ID, val);
1170 static const struct v4l2_subdev_video_ops imx258_video_ops = {
1171 .s_stream = imx258_set_stream,
1174 static const struct v4l2_subdev_pad_ops imx258_pad_ops = {
1175 .enum_mbus_code = imx258_enum_mbus_code,
1176 .get_fmt = imx258_get_pad_format,
1177 .set_fmt = imx258_set_pad_format,
1178 .enum_frame_size = imx258_enum_frame_size,
1181 static const struct v4l2_subdev_ops imx258_subdev_ops = {
1182 .video = &imx258_video_ops,
1183 .pad = &imx258_pad_ops,
1186 static const struct v4l2_subdev_internal_ops imx258_internal_ops = {
1187 .open = imx258_open,
1190 /* Initialize control handlers */
1191 static int imx258_init_controls(struct imx258 *imx258)
1193 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1194 struct v4l2_fwnode_device_properties props;
1195 struct v4l2_ctrl_handler *ctrl_hdlr;
1202 ctrl_hdlr = &imx258->ctrl_handler;
1203 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1207 mutex_init(&imx258->mutex);
1208 ctrl_hdlr->lock = &imx258->mutex;
1209 imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1212 ARRAY_SIZE(link_freq_menu_items) - 1,
1214 link_freq_menu_items);
1216 if (imx258->link_freq)
1217 imx258->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1219 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1220 pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
1221 /* By default, PIXEL_RATE is read only */
1222 imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1223 V4L2_CID_PIXEL_RATE,
1224 pixel_rate_min, pixel_rate_max,
1228 vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
1229 vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
1230 imx258->vblank = v4l2_ctrl_new_std(
1231 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
1233 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
1237 imx258->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1239 imx258->hblank = v4l2_ctrl_new_std(
1240 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
1241 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1242 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1244 IMX258_PPL_DEFAULT - imx258->cur_mode->width);
1247 imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1249 imx258->exposure = v4l2_ctrl_new_std(
1250 ctrl_hdlr, &imx258_ctrl_ops,
1251 V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
1252 IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
1253 IMX258_EXPOSURE_DEFAULT);
1255 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1256 IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
1257 IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
1259 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1260 IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
1261 IMX258_DGTL_GAIN_STEP,
1262 IMX258_DGTL_GAIN_DEFAULT);
1264 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_WIDE_DYNAMIC_RANGE,
1265 0, 1, 1, IMX258_HDR_RATIO_DEFAULT);
1267 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
1268 V4L2_CID_TEST_PATTERN,
1269 ARRAY_SIZE(imx258_test_pattern_menu) - 1,
1270 0, 0, imx258_test_pattern_menu);
1272 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1276 imx258->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1277 V4L2_CID_HFLIP, 0, 1, 1,
1278 props.rotation == 180 ? 1 : 0);
1280 imx258->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1282 imx258->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1283 V4L2_CID_VFLIP, 0, 1, 1,
1284 props.rotation == 180 ? 1 : 0);
1286 imx258->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1287 if (ctrl_hdlr->error) {
1288 ret = ctrl_hdlr->error;
1289 dev_err(&client->dev, "%s control init failed (%d)\n",
1294 imx258->sd.ctrl_handler = ctrl_hdlr;
1299 v4l2_ctrl_handler_free(ctrl_hdlr);
1300 mutex_destroy(&imx258->mutex);
1305 static void imx258_free_controls(struct imx258 *imx258)
1307 v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
1308 mutex_destroy(&imx258->mutex);
1311 static int imx258_get_regulators(struct imx258 *imx258,
1312 struct i2c_client *client)
1316 for (i = 0; i < IMX258_NUM_SUPPLIES; i++)
1317 imx258->supplies[i].supply = imx258_supply_name[i];
1319 return devm_regulator_bulk_get(&client->dev,
1320 IMX258_NUM_SUPPLIES,
1324 static int imx258_probe(struct i2c_client *client)
1326 struct imx258 *imx258;
1330 imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
1334 ret = imx258_get_regulators(imx258, client);
1338 imx258->clk = devm_clk_get_optional(&client->dev, NULL);
1339 if (IS_ERR(imx258->clk))
1340 return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
1341 "error getting clock\n");
1343 dev_dbg(&client->dev,
1344 "no clock provided, using clock-frequency property\n");
1346 device_property_read_u32(&client->dev, "clock-frequency", &val);
1348 val = clk_get_rate(imx258->clk);
1350 if (val != IMX258_INPUT_CLOCK_FREQ) {
1351 dev_err(&client->dev, "input clock frequency not supported\n");
1355 /* Initialize subdev */
1356 v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
1358 /* Will be powered off via pm_runtime_idle */
1359 ret = imx258_power_on(&client->dev);
1363 /* Check module identity */
1364 ret = imx258_identify_module(imx258);
1366 goto error_identify;
1368 /* Set default mode to max resolution */
1369 imx258->cur_mode = &supported_modes[0];
1371 ret = imx258_init_controls(imx258);
1373 goto error_identify;
1375 /* Initialize subdev */
1376 imx258->sd.internal_ops = &imx258_internal_ops;
1377 imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1378 imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1380 /* Initialize source pad */
1381 imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
1383 ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
1385 goto error_handler_free;
1387 ret = v4l2_async_register_subdev_sensor(&imx258->sd);
1389 goto error_media_entity;
1391 pm_runtime_set_active(&client->dev);
1392 pm_runtime_enable(&client->dev);
1393 pm_runtime_idle(&client->dev);
1398 media_entity_cleanup(&imx258->sd.entity);
1401 imx258_free_controls(imx258);
1404 imx258_power_off(&client->dev);
1409 static int imx258_remove(struct i2c_client *client)
1411 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1412 struct imx258 *imx258 = to_imx258(sd);
1414 v4l2_async_unregister_subdev(sd);
1415 media_entity_cleanup(&sd->entity);
1416 imx258_free_controls(imx258);
1418 pm_runtime_disable(&client->dev);
1419 if (!pm_runtime_status_suspended(&client->dev))
1420 imx258_power_off(&client->dev);
1421 pm_runtime_set_suspended(&client->dev);
1426 static const struct dev_pm_ops imx258_pm_ops = {
1427 SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
1428 SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
1432 static const struct acpi_device_id imx258_acpi_ids[] = {
1437 MODULE_DEVICE_TABLE(acpi, imx258_acpi_ids);
1440 static const struct of_device_id imx258_dt_ids[] = {
1441 { .compatible = "sony,imx258" },
1444 MODULE_DEVICE_TABLE(of, imx258_dt_ids);
1446 static struct i2c_driver imx258_i2c_driver = {
1449 .pm = &imx258_pm_ops,
1450 .acpi_match_table = ACPI_PTR(imx258_acpi_ids),
1451 .of_match_table = imx258_dt_ids,
1453 .probe_new = imx258_probe,
1454 .remove = imx258_remove,
1457 module_i2c_driver(imx258_i2c_driver);
1459 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>");
1460 MODULE_AUTHOR("Chiang, Alan");
1461 MODULE_AUTHOR("Chen, Jason");
1462 MODULE_DESCRIPTION("Sony IMX258 sensor driver");
1463 MODULE_LICENSE("GPL v2");