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 #define IMX258_REG_VTS 0x0340
35 /* HBLANK control - read only */
36 #define IMX258_PPL_DEFAULT 5352
38 /* Exposure control */
39 #define IMX258_REG_EXPOSURE 0x0202
40 #define IMX258_EXPOSURE_MIN 4
41 #define IMX258_EXPOSURE_STEP 1
42 #define IMX258_EXPOSURE_DEFAULT 0x640
43 #define IMX258_EXPOSURE_MAX 65535
45 /* Analog gain control */
46 #define IMX258_REG_ANALOG_GAIN 0x0204
47 #define IMX258_ANA_GAIN_MIN 0
48 #define IMX258_ANA_GAIN_MAX 480
49 #define IMX258_ANA_GAIN_STEP 1
50 #define IMX258_ANA_GAIN_DEFAULT 0x0
52 /* Digital gain control */
53 #define IMX258_REG_GR_DIGITAL_GAIN 0x020e
54 #define IMX258_REG_R_DIGITAL_GAIN 0x0210
55 #define IMX258_REG_B_DIGITAL_GAIN 0x0212
56 #define IMX258_REG_GB_DIGITAL_GAIN 0x0214
57 #define IMX258_DGTL_GAIN_MIN 0
58 #define IMX258_DGTL_GAIN_MAX 4096 /* Max = 0xFFF */
59 #define IMX258_DGTL_GAIN_DEFAULT 1024
60 #define IMX258_DGTL_GAIN_STEP 1
63 #define IMX258_REG_HDR 0x0220
64 #define IMX258_HDR_ON BIT(0)
65 #define IMX258_REG_HDR_RATIO 0x0222
66 #define IMX258_HDR_RATIO_MIN 0
67 #define IMX258_HDR_RATIO_MAX 5
68 #define IMX258_HDR_RATIO_STEP 1
69 #define IMX258_HDR_RATIO_DEFAULT 0x0
71 /* Test Pattern Control */
72 #define IMX258_REG_TEST_PATTERN 0x0600
75 #define REG_MIRROR_FLIP_CONTROL 0x0101
76 #define REG_CONFIG_MIRROR_FLIP 0x03
77 #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
84 struct imx258_reg_list {
86 const struct imx258_reg *regs;
89 /* Link frequency config */
90 struct imx258_link_freq_config {
94 /* PLL registers for this link frequency */
95 struct imx258_reg_list reg_list;
98 /* Mode : resolution and related config&values */
109 /* Index of Link frequency config to be used */
111 /* Default register values */
112 struct imx258_reg_list reg_list;
115 /* 4208x3120 needs 1267Mbps/lane, 4 lanes */
116 static const struct imx258_reg mipi_1267mbps_19_2mhz[] = {
136 static const struct imx258_reg mipi_1272mbps_24mhz[] = {
156 static const struct imx258_reg mipi_640mbps_19_2mhz[] = {
176 static const struct imx258_reg mipi_642mbps_24mhz[] = {
196 static const struct imx258_reg mode_4208x3120_regs[] = {
309 static const struct imx258_reg mode_2104_1560_regs[] = {
422 static const struct imx258_reg mode_1048_780_regs[] = {
535 static const char * const imx258_test_pattern_menu[] = {
538 "Eight Vertical Colour Bars",
539 "Colour Bars With Fade to Grey",
540 "Pseudorandom Sequence (PN9)",
543 /* regulator supplies */
544 static const char * const imx258_supply_name[] = {
545 /* Supplies can be enabled in any order */
546 "vana", /* Analog (2.8V) supply */
547 "vdig", /* Digital Core (1.05V) supply */
548 "vif", /* IF (1.8V) supply */
551 #define IMX258_NUM_SUPPLIES ARRAY_SIZE(imx258_supply_name)
554 IMX258_LINK_FREQ_1267MBPS,
555 IMX258_LINK_FREQ_640MBPS,
559 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
560 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
562 static u64 link_freq_to_pixel_rate(u64 f)
570 /* Menu items for LINK_FREQ V4L2 control */
571 /* Configurations for supported link frequencies */
572 #define IMX258_LINK_FREQ_634MHZ 633600000ULL
573 #define IMX258_LINK_FREQ_320MHZ 320000000ULL
575 static const s64 link_freq_menu_items_19_2[] = {
576 IMX258_LINK_FREQ_634MHZ,
577 IMX258_LINK_FREQ_320MHZ,
580 /* Configurations for supported link frequencies */
581 #define IMX258_LINK_FREQ_636MHZ 636000000ULL
582 #define IMX258_LINK_FREQ_321MHZ 321000000ULL
584 static const s64 link_freq_menu_items_24[] = {
585 IMX258_LINK_FREQ_636MHZ,
586 IMX258_LINK_FREQ_321MHZ,
589 /* Link frequency configs */
590 static const struct imx258_link_freq_config link_freq_configs_19_2[] = {
591 [IMX258_LINK_FREQ_1267MBPS] = {
592 .pixels_per_line = IMX258_PPL_DEFAULT,
594 .num_of_regs = ARRAY_SIZE(mipi_1267mbps_19_2mhz),
595 .regs = mipi_1267mbps_19_2mhz,
598 [IMX258_LINK_FREQ_640MBPS] = {
599 .pixels_per_line = IMX258_PPL_DEFAULT,
601 .num_of_regs = ARRAY_SIZE(mipi_640mbps_19_2mhz),
602 .regs = mipi_640mbps_19_2mhz,
607 static const struct imx258_link_freq_config link_freq_configs_24[] = {
608 [IMX258_LINK_FREQ_1267MBPS] = {
609 .pixels_per_line = IMX258_PPL_DEFAULT,
611 .num_of_regs = ARRAY_SIZE(mipi_1272mbps_24mhz),
612 .regs = mipi_1272mbps_24mhz,
615 [IMX258_LINK_FREQ_640MBPS] = {
616 .pixels_per_line = IMX258_PPL_DEFAULT,
618 .num_of_regs = ARRAY_SIZE(mipi_642mbps_24mhz),
619 .regs = mipi_642mbps_24mhz,
625 static const struct imx258_mode supported_modes[] = {
629 .vts_def = IMX258_VTS_30FPS,
630 .vts_min = IMX258_VTS_30FPS,
632 .num_of_regs = ARRAY_SIZE(mode_4208x3120_regs),
633 .regs = mode_4208x3120_regs,
635 .link_freq_index = IMX258_LINK_FREQ_1267MBPS,
640 .vts_def = IMX258_VTS_30FPS_2K,
641 .vts_min = IMX258_VTS_30FPS_2K,
643 .num_of_regs = ARRAY_SIZE(mode_2104_1560_regs),
644 .regs = mode_2104_1560_regs,
646 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
651 .vts_def = IMX258_VTS_30FPS_VGA,
652 .vts_min = IMX258_VTS_30FPS_VGA,
654 .num_of_regs = ARRAY_SIZE(mode_1048_780_regs),
655 .regs = mode_1048_780_regs,
657 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
662 struct v4l2_subdev sd;
663 struct media_pad pad;
665 struct v4l2_ctrl_handler ctrl_handler;
667 struct v4l2_ctrl *link_freq;
668 struct v4l2_ctrl *pixel_rate;
669 struct v4l2_ctrl *vblank;
670 struct v4l2_ctrl *hblank;
671 struct v4l2_ctrl *exposure;
674 const struct imx258_mode *cur_mode;
676 const struct imx258_link_freq_config *link_freq_configs;
677 const s64 *link_freq_menu_items;
680 * Mutex for serialized access:
681 * Protect sensor module set pad format and start/stop streaming safely.
685 /* Streaming on/off */
689 struct regulator_bulk_data supplies[IMX258_NUM_SUPPLIES];
692 static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
694 return container_of(_sd, struct imx258, sd);
697 /* Read registers up to 2 at a time */
698 static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
700 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
701 struct i2c_msg msgs[2];
702 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
703 u8 data_buf[4] = { 0, };
709 /* Write register address */
710 msgs[0].addr = client->addr;
712 msgs[0].len = ARRAY_SIZE(addr_buf);
713 msgs[0].buf = addr_buf;
715 /* Read data from register */
716 msgs[1].addr = client->addr;
717 msgs[1].flags = I2C_M_RD;
719 msgs[1].buf = &data_buf[4 - len];
721 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
722 if (ret != ARRAY_SIZE(msgs))
725 *val = get_unaligned_be32(data_buf);
730 /* Write registers up to 2 at a time */
731 static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
733 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
739 put_unaligned_be16(reg, buf);
740 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
741 if (i2c_master_send(client, buf, len + 2) != len + 2)
747 /* Write a list of registers */
748 static int imx258_write_regs(struct imx258 *imx258,
749 const struct imx258_reg *regs, u32 len)
751 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
755 for (i = 0; i < len; i++) {
756 ret = imx258_write_reg(imx258, regs[i].address, 1,
761 "Failed to write reg 0x%4.4x. error = %d\n",
762 regs[i].address, ret);
771 /* Open sub-device */
772 static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
774 struct v4l2_mbus_framefmt *try_fmt =
775 v4l2_subdev_get_try_format(sd, fh->state, 0);
777 /* Initialize try_fmt */
778 try_fmt->width = supported_modes[0].width;
779 try_fmt->height = supported_modes[0].height;
780 try_fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
781 try_fmt->field = V4L2_FIELD_NONE;
786 static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
790 ret = imx258_write_reg(imx258, IMX258_REG_GR_DIGITAL_GAIN,
791 IMX258_REG_VALUE_16BIT,
795 ret = imx258_write_reg(imx258, IMX258_REG_GB_DIGITAL_GAIN,
796 IMX258_REG_VALUE_16BIT,
800 ret = imx258_write_reg(imx258, IMX258_REG_R_DIGITAL_GAIN,
801 IMX258_REG_VALUE_16BIT,
805 ret = imx258_write_reg(imx258, IMX258_REG_B_DIGITAL_GAIN,
806 IMX258_REG_VALUE_16BIT,
813 static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
815 struct imx258 *imx258 =
816 container_of(ctrl->handler, struct imx258, ctrl_handler);
817 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
821 * Applying V4L2 control value only happens
822 * when power is up for streaming
824 if (pm_runtime_get_if_in_use(&client->dev) == 0)
828 case V4L2_CID_ANALOGUE_GAIN:
829 ret = imx258_write_reg(imx258, IMX258_REG_ANALOG_GAIN,
830 IMX258_REG_VALUE_16BIT,
833 case V4L2_CID_EXPOSURE:
834 ret = imx258_write_reg(imx258, IMX258_REG_EXPOSURE,
835 IMX258_REG_VALUE_16BIT,
838 case V4L2_CID_DIGITAL_GAIN:
839 ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
842 case V4L2_CID_TEST_PATTERN:
843 ret = imx258_write_reg(imx258, IMX258_REG_TEST_PATTERN,
844 IMX258_REG_VALUE_16BIT,
846 ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
847 IMX258_REG_VALUE_08BIT,
848 !ctrl->val ? REG_CONFIG_MIRROR_FLIP :
849 REG_CONFIG_FLIP_TEST_PATTERN);
851 case V4L2_CID_WIDE_DYNAMIC_RANGE:
853 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
854 IMX258_REG_VALUE_08BIT,
855 IMX258_HDR_RATIO_MIN);
857 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
858 IMX258_REG_VALUE_08BIT,
862 ret = imx258_write_reg(imx258, IMX258_REG_HDR_RATIO,
863 IMX258_REG_VALUE_08BIT,
864 BIT(IMX258_HDR_RATIO_MAX));
867 case V4L2_CID_VBLANK:
868 ret = imx258_write_reg(imx258, IMX258_REG_VTS,
869 IMX258_REG_VALUE_16BIT,
870 imx258->cur_mode->height + ctrl->val);
873 dev_info(&client->dev,
874 "ctrl(id:0x%x,val:0x%x) is not handled\n",
875 ctrl->id, ctrl->val);
880 pm_runtime_put(&client->dev);
885 static const struct v4l2_ctrl_ops imx258_ctrl_ops = {
886 .s_ctrl = imx258_set_ctrl,
889 static int imx258_enum_mbus_code(struct v4l2_subdev *sd,
890 struct v4l2_subdev_state *sd_state,
891 struct v4l2_subdev_mbus_code_enum *code)
893 /* Only one bayer order(GRBG) is supported */
897 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
902 static int imx258_enum_frame_size(struct v4l2_subdev *sd,
903 struct v4l2_subdev_state *sd_state,
904 struct v4l2_subdev_frame_size_enum *fse)
906 if (fse->index >= ARRAY_SIZE(supported_modes))
909 if (fse->code != MEDIA_BUS_FMT_SBGGR10_1X10)
912 fse->min_width = supported_modes[fse->index].width;
913 fse->max_width = fse->min_width;
914 fse->min_height = supported_modes[fse->index].height;
915 fse->max_height = fse->min_height;
920 static void imx258_update_pad_format(const struct imx258_mode *mode,
921 struct v4l2_subdev_format *fmt)
923 fmt->format.width = mode->width;
924 fmt->format.height = mode->height;
925 fmt->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
926 fmt->format.field = V4L2_FIELD_NONE;
929 static int __imx258_get_pad_format(struct imx258 *imx258,
930 struct v4l2_subdev_state *sd_state,
931 struct v4l2_subdev_format *fmt)
933 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
934 fmt->format = *v4l2_subdev_get_try_format(&imx258->sd,
938 imx258_update_pad_format(imx258->cur_mode, fmt);
943 static int imx258_get_pad_format(struct v4l2_subdev *sd,
944 struct v4l2_subdev_state *sd_state,
945 struct v4l2_subdev_format *fmt)
947 struct imx258 *imx258 = to_imx258(sd);
950 mutex_lock(&imx258->mutex);
951 ret = __imx258_get_pad_format(imx258, sd_state, fmt);
952 mutex_unlock(&imx258->mutex);
957 static int imx258_set_pad_format(struct v4l2_subdev *sd,
958 struct v4l2_subdev_state *sd_state,
959 struct v4l2_subdev_format *fmt)
961 struct imx258 *imx258 = to_imx258(sd);
962 const struct imx258_mode *mode;
963 struct v4l2_mbus_framefmt *framefmt;
970 mutex_lock(&imx258->mutex);
972 /* Only one raw bayer(GBRG) order is supported */
973 fmt->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
975 mode = v4l2_find_nearest_size(supported_modes,
976 ARRAY_SIZE(supported_modes), width, height,
977 fmt->format.width, fmt->format.height);
978 imx258_update_pad_format(mode, fmt);
979 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
980 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
981 *framefmt = fmt->format;
983 imx258->cur_mode = mode;
984 __v4l2_ctrl_s_ctrl(imx258->link_freq, mode->link_freq_index);
986 link_freq = imx258->link_freq_menu_items[mode->link_freq_index];
987 pixel_rate = link_freq_to_pixel_rate(link_freq);
988 __v4l2_ctrl_s_ctrl_int64(imx258->pixel_rate, pixel_rate);
989 /* Update limits and set FPS to default */
990 vblank_def = imx258->cur_mode->vts_def -
991 imx258->cur_mode->height;
992 vblank_min = imx258->cur_mode->vts_min -
993 imx258->cur_mode->height;
994 __v4l2_ctrl_modify_range(
995 imx258->vblank, vblank_min,
996 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
998 __v4l2_ctrl_s_ctrl(imx258->vblank, vblank_def);
1000 imx258->link_freq_configs[mode->link_freq_index].pixels_per_line
1001 - imx258->cur_mode->width;
1002 __v4l2_ctrl_modify_range(imx258->hblank, h_blank,
1003 h_blank, 1, h_blank);
1006 mutex_unlock(&imx258->mutex);
1011 /* Start streaming */
1012 static int imx258_start_streaming(struct imx258 *imx258)
1014 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1015 const struct imx258_reg_list *reg_list;
1016 int ret, link_freq_index;
1019 link_freq_index = imx258->cur_mode->link_freq_index;
1020 reg_list = &imx258->link_freq_configs[link_freq_index].reg_list;
1021 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
1023 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1027 /* Apply default values of current mode */
1028 reg_list = &imx258->cur_mode->reg_list;
1029 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
1031 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1035 /* Set Orientation be 180 degree */
1036 ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
1037 IMX258_REG_VALUE_08BIT, REG_CONFIG_MIRROR_FLIP);
1039 dev_err(&client->dev, "%s failed to set orientation\n",
1044 /* Apply customized values from user */
1045 ret = __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
1049 /* set stream on register */
1050 return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
1051 IMX258_REG_VALUE_08BIT,
1052 IMX258_MODE_STREAMING);
1055 /* Stop streaming */
1056 static int imx258_stop_streaming(struct imx258 *imx258)
1058 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1061 /* set stream off register */
1062 ret = imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
1063 IMX258_REG_VALUE_08BIT, IMX258_MODE_STANDBY);
1065 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1068 * Return success even if it was an error, as there is nothing the
1069 * caller can do about it.
1074 static int imx258_power_on(struct device *dev)
1076 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1077 struct imx258 *imx258 = to_imx258(sd);
1080 ret = regulator_bulk_enable(IMX258_NUM_SUPPLIES,
1083 dev_err(dev, "%s: failed to enable regulators\n",
1088 ret = clk_prepare_enable(imx258->clk);
1090 dev_err(dev, "failed to enable clock\n");
1091 regulator_bulk_disable(IMX258_NUM_SUPPLIES, imx258->supplies);
1097 static int imx258_power_off(struct device *dev)
1099 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1100 struct imx258 *imx258 = to_imx258(sd);
1102 clk_disable_unprepare(imx258->clk);
1103 regulator_bulk_disable(IMX258_NUM_SUPPLIES, imx258->supplies);
1108 static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
1110 struct imx258 *imx258 = to_imx258(sd);
1111 struct i2c_client *client = v4l2_get_subdevdata(sd);
1114 mutex_lock(&imx258->mutex);
1115 if (imx258->streaming == enable) {
1116 mutex_unlock(&imx258->mutex);
1121 ret = pm_runtime_resume_and_get(&client->dev);
1126 * Apply default & customized values
1127 * and then start streaming.
1129 ret = imx258_start_streaming(imx258);
1133 imx258_stop_streaming(imx258);
1134 pm_runtime_put(&client->dev);
1137 imx258->streaming = enable;
1138 mutex_unlock(&imx258->mutex);
1143 pm_runtime_put(&client->dev);
1145 mutex_unlock(&imx258->mutex);
1150 static int __maybe_unused imx258_suspend(struct device *dev)
1152 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1153 struct imx258 *imx258 = to_imx258(sd);
1155 if (imx258->streaming)
1156 imx258_stop_streaming(imx258);
1161 static int __maybe_unused imx258_resume(struct device *dev)
1163 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1164 struct imx258 *imx258 = to_imx258(sd);
1167 if (imx258->streaming) {
1168 ret = imx258_start_streaming(imx258);
1176 imx258_stop_streaming(imx258);
1177 imx258->streaming = 0;
1181 /* Verify chip ID */
1182 static int imx258_identify_module(struct imx258 *imx258)
1184 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1188 ret = imx258_read_reg(imx258, IMX258_REG_CHIP_ID,
1189 IMX258_REG_VALUE_16BIT, &val);
1191 dev_err(&client->dev, "failed to read chip id %x\n",
1196 if (val != IMX258_CHIP_ID) {
1197 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1198 IMX258_CHIP_ID, val);
1205 static const struct v4l2_subdev_video_ops imx258_video_ops = {
1206 .s_stream = imx258_set_stream,
1209 static const struct v4l2_subdev_pad_ops imx258_pad_ops = {
1210 .enum_mbus_code = imx258_enum_mbus_code,
1211 .get_fmt = imx258_get_pad_format,
1212 .set_fmt = imx258_set_pad_format,
1213 .enum_frame_size = imx258_enum_frame_size,
1216 static const struct v4l2_subdev_ops imx258_subdev_ops = {
1217 .video = &imx258_video_ops,
1218 .pad = &imx258_pad_ops,
1221 static const struct v4l2_subdev_internal_ops imx258_internal_ops = {
1222 .open = imx258_open,
1225 /* Initialize control handlers */
1226 static int imx258_init_controls(struct imx258 *imx258)
1228 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1229 struct v4l2_fwnode_device_properties props;
1230 struct v4l2_ctrl_handler *ctrl_hdlr;
1231 struct v4l2_ctrl *vflip, *hflip;
1238 ctrl_hdlr = &imx258->ctrl_handler;
1239 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 13);
1243 mutex_init(&imx258->mutex);
1244 ctrl_hdlr->lock = &imx258->mutex;
1245 imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1248 ARRAY_SIZE(link_freq_menu_items_19_2) - 1,
1250 imx258->link_freq_menu_items);
1252 if (imx258->link_freq)
1253 imx258->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1255 /* The driver only supports one bayer order and flips by default. */
1256 hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1257 V4L2_CID_HFLIP, 1, 1, 1, 1);
1259 hflip->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1261 vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1262 V4L2_CID_VFLIP, 1, 1, 1, 1);
1264 vflip->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1267 link_freq_to_pixel_rate(imx258->link_freq_menu_items[0]);
1269 link_freq_to_pixel_rate(imx258->link_freq_menu_items[1]);
1270 /* By default, PIXEL_RATE is read only */
1271 imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1272 V4L2_CID_PIXEL_RATE,
1273 pixel_rate_min, pixel_rate_max,
1277 vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
1278 vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
1279 imx258->vblank = v4l2_ctrl_new_std(
1280 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
1282 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
1285 imx258->hblank = v4l2_ctrl_new_std(
1286 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
1287 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1288 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1290 IMX258_PPL_DEFAULT - imx258->cur_mode->width);
1293 imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1295 imx258->exposure = v4l2_ctrl_new_std(
1296 ctrl_hdlr, &imx258_ctrl_ops,
1297 V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
1298 IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
1299 IMX258_EXPOSURE_DEFAULT);
1301 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1302 IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
1303 IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
1305 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1306 IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
1307 IMX258_DGTL_GAIN_STEP,
1308 IMX258_DGTL_GAIN_DEFAULT);
1310 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_WIDE_DYNAMIC_RANGE,
1311 0, 1, 1, IMX258_HDR_RATIO_DEFAULT);
1313 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
1314 V4L2_CID_TEST_PATTERN,
1315 ARRAY_SIZE(imx258_test_pattern_menu) - 1,
1316 0, 0, imx258_test_pattern_menu);
1318 if (ctrl_hdlr->error) {
1319 ret = ctrl_hdlr->error;
1320 dev_err(&client->dev, "%s control init failed (%d)\n",
1325 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1329 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx258_ctrl_ops,
1334 imx258->sd.ctrl_handler = ctrl_hdlr;
1339 v4l2_ctrl_handler_free(ctrl_hdlr);
1340 mutex_destroy(&imx258->mutex);
1345 static void imx258_free_controls(struct imx258 *imx258)
1347 v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
1348 mutex_destroy(&imx258->mutex);
1351 static int imx258_get_regulators(struct imx258 *imx258,
1352 struct i2c_client *client)
1356 for (i = 0; i < IMX258_NUM_SUPPLIES; i++)
1357 imx258->supplies[i].supply = imx258_supply_name[i];
1359 return devm_regulator_bulk_get(&client->dev,
1360 IMX258_NUM_SUPPLIES,
1364 static int imx258_probe(struct i2c_client *client)
1366 struct imx258 *imx258;
1370 imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
1374 ret = imx258_get_regulators(imx258, client);
1378 imx258->clk = devm_clk_get_optional(&client->dev, NULL);
1379 if (IS_ERR(imx258->clk))
1380 return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
1381 "error getting clock\n");
1383 dev_dbg(&client->dev,
1384 "no clock provided, using clock-frequency property\n");
1386 device_property_read_u32(&client->dev, "clock-frequency", &val);
1387 } else if (IS_ERR(imx258->clk)) {
1388 return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
1389 "error getting clock\n");
1391 val = clk_get_rate(imx258->clk);
1396 imx258->link_freq_configs = link_freq_configs_19_2;
1397 imx258->link_freq_menu_items = link_freq_menu_items_19_2;
1400 imx258->link_freq_configs = link_freq_configs_24;
1401 imx258->link_freq_menu_items = link_freq_menu_items_24;
1404 dev_err(&client->dev, "input clock frequency of %u not supported\n",
1409 /* Initialize subdev */
1410 v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
1412 /* Will be powered off via pm_runtime_idle */
1413 ret = imx258_power_on(&client->dev);
1417 /* Check module identity */
1418 ret = imx258_identify_module(imx258);
1420 goto error_identify;
1422 /* Set default mode to max resolution */
1423 imx258->cur_mode = &supported_modes[0];
1425 ret = imx258_init_controls(imx258);
1427 goto error_identify;
1429 /* Initialize subdev */
1430 imx258->sd.internal_ops = &imx258_internal_ops;
1431 imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1432 imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1434 /* Initialize source pad */
1435 imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
1437 ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
1439 goto error_handler_free;
1441 ret = v4l2_async_register_subdev_sensor(&imx258->sd);
1443 goto error_media_entity;
1445 pm_runtime_set_active(&client->dev);
1446 pm_runtime_enable(&client->dev);
1447 pm_runtime_idle(&client->dev);
1452 media_entity_cleanup(&imx258->sd.entity);
1455 imx258_free_controls(imx258);
1458 imx258_power_off(&client->dev);
1463 static void imx258_remove(struct i2c_client *client)
1465 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1466 struct imx258 *imx258 = to_imx258(sd);
1468 v4l2_async_unregister_subdev(sd);
1469 media_entity_cleanup(&sd->entity);
1470 imx258_free_controls(imx258);
1472 pm_runtime_disable(&client->dev);
1473 if (!pm_runtime_status_suspended(&client->dev))
1474 imx258_power_off(&client->dev);
1475 pm_runtime_set_suspended(&client->dev);
1478 static const struct dev_pm_ops imx258_pm_ops = {
1479 SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
1480 SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
1484 static const struct acpi_device_id imx258_acpi_ids[] = {
1489 MODULE_DEVICE_TABLE(acpi, imx258_acpi_ids);
1492 static const struct of_device_id imx258_dt_ids[] = {
1493 { .compatible = "sony,imx258" },
1496 MODULE_DEVICE_TABLE(of, imx258_dt_ids);
1498 static struct i2c_driver imx258_i2c_driver = {
1501 .pm = &imx258_pm_ops,
1502 .acpi_match_table = ACPI_PTR(imx258_acpi_ids),
1503 .of_match_table = imx258_dt_ids,
1505 .probe = imx258_probe,
1506 .remove = imx258_remove,
1509 module_i2c_driver(imx258_i2c_driver);
1511 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>");
1512 MODULE_AUTHOR("Chiang, Alan");
1513 MODULE_AUTHOR("Chen, Jason");
1514 MODULE_DESCRIPTION("Sony IMX258 sensor driver");
1515 MODULE_LICENSE("GPL v2");