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 <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-fwnode.h>
13 #include <asm/unaligned.h>
15 #define IMX258_REG_VALUE_08BIT 1
16 #define IMX258_REG_VALUE_16BIT 2
18 #define IMX258_REG_MODE_SELECT 0x0100
19 #define IMX258_MODE_STANDBY 0x00
20 #define IMX258_MODE_STREAMING 0x01
23 #define IMX258_REG_CHIP_ID 0x0016
24 #define IMX258_CHIP_ID 0x0258
26 /* V_TIMING internal */
27 #define IMX258_VTS_30FPS 0x0c50
28 #define IMX258_VTS_30FPS_2K 0x0638
29 #define IMX258_VTS_30FPS_VGA 0x034c
30 #define IMX258_VTS_MAX 0xffff
33 #define IMX258_FLL_MIN 0x08a6
34 #define IMX258_FLL_MAX 0xffff
35 #define IMX258_FLL_STEP 1
36 #define IMX258_FLL_DEFAULT 0x0c98
38 /* HBLANK control - read only */
39 #define IMX258_PPL_DEFAULT 5352
41 /* Exposure control */
42 #define IMX258_REG_EXPOSURE 0x0202
43 #define IMX258_EXPOSURE_MIN 4
44 #define IMX258_EXPOSURE_STEP 1
45 #define IMX258_EXPOSURE_DEFAULT 0x640
46 #define IMX258_EXPOSURE_MAX 65535
48 /* Analog gain control */
49 #define IMX258_REG_ANALOG_GAIN 0x0204
50 #define IMX258_ANA_GAIN_MIN 0
51 #define IMX258_ANA_GAIN_MAX 480
52 #define IMX258_ANA_GAIN_STEP 1
53 #define IMX258_ANA_GAIN_DEFAULT 0x0
55 /* Digital gain control */
56 #define IMX258_REG_GR_DIGITAL_GAIN 0x020e
57 #define IMX258_REG_R_DIGITAL_GAIN 0x0210
58 #define IMX258_REG_B_DIGITAL_GAIN 0x0212
59 #define IMX258_REG_GB_DIGITAL_GAIN 0x0214
60 #define IMX258_DGTL_GAIN_MIN 0
61 #define IMX258_DGTL_GAIN_MAX 4096 /* Max = 0xFFF */
62 #define IMX258_DGTL_GAIN_DEFAULT 1024
63 #define IMX258_DGTL_GAIN_STEP 1
66 #define IMX258_REG_HDR 0x0220
67 #define IMX258_HDR_ON BIT(0)
68 #define IMX258_REG_HDR_RATIO 0x0222
69 #define IMX258_HDR_RATIO_MIN 0
70 #define IMX258_HDR_RATIO_MAX 5
71 #define IMX258_HDR_RATIO_STEP 1
72 #define IMX258_HDR_RATIO_DEFAULT 0x0
74 /* Test Pattern Control */
75 #define IMX258_REG_TEST_PATTERN 0x0600
78 #define REG_MIRROR_FLIP_CONTROL 0x0101
79 #define REG_CONFIG_MIRROR_FLIP 0x03
80 #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
82 /* Input clock frequency in Hz */
83 #define IMX258_INPUT_CLOCK_FREQ 19200000
90 struct imx258_reg_list {
92 const struct imx258_reg *regs;
95 /* Link frequency config */
96 struct imx258_link_freq_config {
99 /* PLL registers for this link frequency */
100 struct imx258_reg_list reg_list;
103 /* Mode : resolution and related config&values */
114 /* Index of Link frequency config to be used */
116 /* Default register values */
117 struct imx258_reg_list reg_list;
120 /* 4208x3118 needs 1267Mbps/lane, 4 lanes */
121 static const struct imx258_reg mipi_data_rate_1267mbps[] = {
139 static const struct imx258_reg mipi_data_rate_640mbps[] = {
157 static const struct imx258_reg mode_4208x3118_regs[] = {
276 static const struct imx258_reg mode_2104_1560_regs[] = {
395 static const struct imx258_reg mode_1048_780_regs[] = {
514 static const char * const imx258_test_pattern_menu[] = {
517 "Eight Vertical Colour Bars",
518 "Colour Bars With Fade to Grey",
519 "Pseudorandom Sequence (PN9)",
522 /* Configurations for supported link frequencies */
523 #define IMX258_LINK_FREQ_634MHZ 633600000ULL
524 #define IMX258_LINK_FREQ_320MHZ 320000000ULL
527 IMX258_LINK_FREQ_1267MBPS,
528 IMX258_LINK_FREQ_640MBPS,
532 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
533 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
535 static u64 link_freq_to_pixel_rate(u64 f)
543 /* Menu items for LINK_FREQ V4L2 control */
544 static const s64 link_freq_menu_items[] = {
545 IMX258_LINK_FREQ_634MHZ,
546 IMX258_LINK_FREQ_320MHZ,
549 /* Link frequency configs */
550 static const struct imx258_link_freq_config link_freq_configs[] = {
551 [IMX258_LINK_FREQ_1267MBPS] = {
552 .pixels_per_line = IMX258_PPL_DEFAULT,
554 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1267mbps),
555 .regs = mipi_data_rate_1267mbps,
558 [IMX258_LINK_FREQ_640MBPS] = {
559 .pixels_per_line = IMX258_PPL_DEFAULT,
561 .num_of_regs = ARRAY_SIZE(mipi_data_rate_640mbps),
562 .regs = mipi_data_rate_640mbps,
568 static const struct imx258_mode supported_modes[] = {
572 .vts_def = IMX258_VTS_30FPS,
573 .vts_min = IMX258_VTS_30FPS,
575 .num_of_regs = ARRAY_SIZE(mode_4208x3118_regs),
576 .regs = mode_4208x3118_regs,
578 .link_freq_index = IMX258_LINK_FREQ_1267MBPS,
583 .vts_def = IMX258_VTS_30FPS_2K,
584 .vts_min = IMX258_VTS_30FPS_2K,
586 .num_of_regs = ARRAY_SIZE(mode_2104_1560_regs),
587 .regs = mode_2104_1560_regs,
589 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
594 .vts_def = IMX258_VTS_30FPS_VGA,
595 .vts_min = IMX258_VTS_30FPS_VGA,
597 .num_of_regs = ARRAY_SIZE(mode_1048_780_regs),
598 .regs = mode_1048_780_regs,
600 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
605 struct v4l2_subdev sd;
606 struct media_pad pad;
608 struct v4l2_ctrl_handler ctrl_handler;
610 struct v4l2_ctrl *link_freq;
611 struct v4l2_ctrl *pixel_rate;
612 struct v4l2_ctrl *vblank;
613 struct v4l2_ctrl *hblank;
614 struct v4l2_ctrl *exposure;
617 const struct imx258_mode *cur_mode;
620 * Mutex for serialized access:
621 * Protect sensor module set pad format and start/stop streaming safely.
625 /* Streaming on/off */
631 static inline struct imx258 *to_imx258(struct v4l2_subdev *_sd)
633 return container_of(_sd, struct imx258, sd);
636 /* Read registers up to 2 at a time */
637 static int imx258_read_reg(struct imx258 *imx258, u16 reg, u32 len, u32 *val)
639 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
640 struct i2c_msg msgs[2];
641 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
642 u8 data_buf[4] = { 0, };
648 /* Write register address */
649 msgs[0].addr = client->addr;
651 msgs[0].len = ARRAY_SIZE(addr_buf);
652 msgs[0].buf = addr_buf;
654 /* Read data from register */
655 msgs[1].addr = client->addr;
656 msgs[1].flags = I2C_M_RD;
658 msgs[1].buf = &data_buf[4 - len];
660 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
661 if (ret != ARRAY_SIZE(msgs))
664 *val = get_unaligned_be32(data_buf);
669 /* Write registers up to 2 at a time */
670 static int imx258_write_reg(struct imx258 *imx258, u16 reg, u32 len, u32 val)
672 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
678 put_unaligned_be16(reg, buf);
679 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
680 if (i2c_master_send(client, buf, len + 2) != len + 2)
686 /* Write a list of registers */
687 static int imx258_write_regs(struct imx258 *imx258,
688 const struct imx258_reg *regs, u32 len)
690 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
694 for (i = 0; i < len; i++) {
695 ret = imx258_write_reg(imx258, regs[i].address, 1,
700 "Failed to write reg 0x%4.4x. error = %d\n",
701 regs[i].address, ret);
710 /* Open sub-device */
711 static int imx258_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
713 struct v4l2_mbus_framefmt *try_fmt =
714 v4l2_subdev_get_try_format(sd, fh->state, 0);
716 /* Initialize try_fmt */
717 try_fmt->width = supported_modes[0].width;
718 try_fmt->height = supported_modes[0].height;
719 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
720 try_fmt->field = V4L2_FIELD_NONE;
725 static int imx258_update_digital_gain(struct imx258 *imx258, u32 len, u32 val)
729 ret = imx258_write_reg(imx258, IMX258_REG_GR_DIGITAL_GAIN,
730 IMX258_REG_VALUE_16BIT,
734 ret = imx258_write_reg(imx258, IMX258_REG_GB_DIGITAL_GAIN,
735 IMX258_REG_VALUE_16BIT,
739 ret = imx258_write_reg(imx258, IMX258_REG_R_DIGITAL_GAIN,
740 IMX258_REG_VALUE_16BIT,
744 ret = imx258_write_reg(imx258, IMX258_REG_B_DIGITAL_GAIN,
745 IMX258_REG_VALUE_16BIT,
752 static int imx258_set_ctrl(struct v4l2_ctrl *ctrl)
754 struct imx258 *imx258 =
755 container_of(ctrl->handler, struct imx258, ctrl_handler);
756 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
760 * Applying V4L2 control value only happens
761 * when power is up for streaming
763 if (pm_runtime_get_if_in_use(&client->dev) == 0)
767 case V4L2_CID_ANALOGUE_GAIN:
768 ret = imx258_write_reg(imx258, IMX258_REG_ANALOG_GAIN,
769 IMX258_REG_VALUE_16BIT,
772 case V4L2_CID_EXPOSURE:
773 ret = imx258_write_reg(imx258, IMX258_REG_EXPOSURE,
774 IMX258_REG_VALUE_16BIT,
777 case V4L2_CID_DIGITAL_GAIN:
778 ret = imx258_update_digital_gain(imx258, IMX258_REG_VALUE_16BIT,
781 case V4L2_CID_TEST_PATTERN:
782 ret = imx258_write_reg(imx258, IMX258_REG_TEST_PATTERN,
783 IMX258_REG_VALUE_16BIT,
785 ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
786 IMX258_REG_VALUE_08BIT,
787 !ctrl->val ? REG_CONFIG_MIRROR_FLIP :
788 REG_CONFIG_FLIP_TEST_PATTERN);
790 case V4L2_CID_WIDE_DYNAMIC_RANGE:
792 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
793 IMX258_REG_VALUE_08BIT,
794 IMX258_HDR_RATIO_MIN);
796 ret = imx258_write_reg(imx258, IMX258_REG_HDR,
797 IMX258_REG_VALUE_08BIT,
801 ret = imx258_write_reg(imx258, IMX258_REG_HDR_RATIO,
802 IMX258_REG_VALUE_08BIT,
803 BIT(IMX258_HDR_RATIO_MAX));
807 dev_info(&client->dev,
808 "ctrl(id:0x%x,val:0x%x) is not handled\n",
809 ctrl->id, ctrl->val);
814 pm_runtime_put(&client->dev);
819 static const struct v4l2_ctrl_ops imx258_ctrl_ops = {
820 .s_ctrl = imx258_set_ctrl,
823 static int imx258_enum_mbus_code(struct v4l2_subdev *sd,
824 struct v4l2_subdev_state *sd_state,
825 struct v4l2_subdev_mbus_code_enum *code)
827 /* Only one bayer order(GRBG) is supported */
831 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
836 static int imx258_enum_frame_size(struct v4l2_subdev *sd,
837 struct v4l2_subdev_state *sd_state,
838 struct v4l2_subdev_frame_size_enum *fse)
840 if (fse->index >= ARRAY_SIZE(supported_modes))
843 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
846 fse->min_width = supported_modes[fse->index].width;
847 fse->max_width = fse->min_width;
848 fse->min_height = supported_modes[fse->index].height;
849 fse->max_height = fse->min_height;
854 static void imx258_update_pad_format(const struct imx258_mode *mode,
855 struct v4l2_subdev_format *fmt)
857 fmt->format.width = mode->width;
858 fmt->format.height = mode->height;
859 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
860 fmt->format.field = V4L2_FIELD_NONE;
863 static int __imx258_get_pad_format(struct imx258 *imx258,
864 struct v4l2_subdev_state *sd_state,
865 struct v4l2_subdev_format *fmt)
867 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
868 fmt->format = *v4l2_subdev_get_try_format(&imx258->sd,
872 imx258_update_pad_format(imx258->cur_mode, fmt);
877 static int imx258_get_pad_format(struct v4l2_subdev *sd,
878 struct v4l2_subdev_state *sd_state,
879 struct v4l2_subdev_format *fmt)
881 struct imx258 *imx258 = to_imx258(sd);
884 mutex_lock(&imx258->mutex);
885 ret = __imx258_get_pad_format(imx258, sd_state, fmt);
886 mutex_unlock(&imx258->mutex);
891 static int imx258_set_pad_format(struct v4l2_subdev *sd,
892 struct v4l2_subdev_state *sd_state,
893 struct v4l2_subdev_format *fmt)
895 struct imx258 *imx258 = to_imx258(sd);
896 const struct imx258_mode *mode;
897 struct v4l2_mbus_framefmt *framefmt;
904 mutex_lock(&imx258->mutex);
906 /* Only one raw bayer(GBRG) order is supported */
907 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
909 mode = v4l2_find_nearest_size(supported_modes,
910 ARRAY_SIZE(supported_modes), width, height,
911 fmt->format.width, fmt->format.height);
912 imx258_update_pad_format(mode, fmt);
913 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
914 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
915 *framefmt = fmt->format;
917 imx258->cur_mode = mode;
918 __v4l2_ctrl_s_ctrl(imx258->link_freq, mode->link_freq_index);
920 link_freq = link_freq_menu_items[mode->link_freq_index];
921 pixel_rate = link_freq_to_pixel_rate(link_freq);
922 __v4l2_ctrl_s_ctrl_int64(imx258->pixel_rate, pixel_rate);
923 /* Update limits and set FPS to default */
924 vblank_def = imx258->cur_mode->vts_def -
925 imx258->cur_mode->height;
926 vblank_min = imx258->cur_mode->vts_min -
927 imx258->cur_mode->height;
928 __v4l2_ctrl_modify_range(
929 imx258->vblank, vblank_min,
930 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
932 __v4l2_ctrl_s_ctrl(imx258->vblank, vblank_def);
934 link_freq_configs[mode->link_freq_index].pixels_per_line
935 - imx258->cur_mode->width;
936 __v4l2_ctrl_modify_range(imx258->hblank, h_blank,
937 h_blank, 1, h_blank);
940 mutex_unlock(&imx258->mutex);
945 /* Start streaming */
946 static int imx258_start_streaming(struct imx258 *imx258)
948 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
949 const struct imx258_reg_list *reg_list;
950 int ret, link_freq_index;
953 link_freq_index = imx258->cur_mode->link_freq_index;
954 reg_list = &link_freq_configs[link_freq_index].reg_list;
955 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
957 dev_err(&client->dev, "%s failed to set plls\n", __func__);
961 /* Apply default values of current mode */
962 reg_list = &imx258->cur_mode->reg_list;
963 ret = imx258_write_regs(imx258, reg_list->regs, reg_list->num_of_regs);
965 dev_err(&client->dev, "%s failed to set mode\n", __func__);
969 /* Set Orientation be 180 degree */
970 ret = imx258_write_reg(imx258, REG_MIRROR_FLIP_CONTROL,
971 IMX258_REG_VALUE_08BIT, REG_CONFIG_MIRROR_FLIP);
973 dev_err(&client->dev, "%s failed to set orientation\n",
978 /* Apply customized values from user */
979 ret = __v4l2_ctrl_handler_setup(imx258->sd.ctrl_handler);
983 /* set stream on register */
984 return imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
985 IMX258_REG_VALUE_08BIT,
986 IMX258_MODE_STREAMING);
990 static int imx258_stop_streaming(struct imx258 *imx258)
992 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
995 /* set stream off register */
996 ret = imx258_write_reg(imx258, IMX258_REG_MODE_SELECT,
997 IMX258_REG_VALUE_08BIT, IMX258_MODE_STANDBY);
999 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1002 * Return success even if it was an error, as there is nothing the
1003 * caller can do about it.
1008 static int imx258_power_on(struct device *dev)
1010 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1011 struct imx258 *imx258 = to_imx258(sd);
1014 ret = clk_prepare_enable(imx258->clk);
1016 dev_err(dev, "failed to enable clock\n");
1021 static int imx258_power_off(struct device *dev)
1023 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1024 struct imx258 *imx258 = to_imx258(sd);
1026 clk_disable_unprepare(imx258->clk);
1031 static int imx258_set_stream(struct v4l2_subdev *sd, int enable)
1033 struct imx258 *imx258 = to_imx258(sd);
1034 struct i2c_client *client = v4l2_get_subdevdata(sd);
1037 mutex_lock(&imx258->mutex);
1038 if (imx258->streaming == enable) {
1039 mutex_unlock(&imx258->mutex);
1044 ret = pm_runtime_resume_and_get(&client->dev);
1049 * Apply default & customized values
1050 * and then start streaming.
1052 ret = imx258_start_streaming(imx258);
1056 imx258_stop_streaming(imx258);
1057 pm_runtime_put(&client->dev);
1060 imx258->streaming = enable;
1061 mutex_unlock(&imx258->mutex);
1066 pm_runtime_put(&client->dev);
1068 mutex_unlock(&imx258->mutex);
1073 static int __maybe_unused imx258_suspend(struct device *dev)
1075 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1076 struct imx258 *imx258 = to_imx258(sd);
1078 if (imx258->streaming)
1079 imx258_stop_streaming(imx258);
1084 static int __maybe_unused imx258_resume(struct device *dev)
1086 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1087 struct imx258 *imx258 = to_imx258(sd);
1090 if (imx258->streaming) {
1091 ret = imx258_start_streaming(imx258);
1099 imx258_stop_streaming(imx258);
1100 imx258->streaming = 0;
1104 /* Verify chip ID */
1105 static int imx258_identify_module(struct imx258 *imx258)
1107 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1111 ret = imx258_read_reg(imx258, IMX258_REG_CHIP_ID,
1112 IMX258_REG_VALUE_16BIT, &val);
1114 dev_err(&client->dev, "failed to read chip id %x\n",
1119 if (val != IMX258_CHIP_ID) {
1120 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1121 IMX258_CHIP_ID, val);
1128 static const struct v4l2_subdev_video_ops imx258_video_ops = {
1129 .s_stream = imx258_set_stream,
1132 static const struct v4l2_subdev_pad_ops imx258_pad_ops = {
1133 .enum_mbus_code = imx258_enum_mbus_code,
1134 .get_fmt = imx258_get_pad_format,
1135 .set_fmt = imx258_set_pad_format,
1136 .enum_frame_size = imx258_enum_frame_size,
1139 static const struct v4l2_subdev_ops imx258_subdev_ops = {
1140 .video = &imx258_video_ops,
1141 .pad = &imx258_pad_ops,
1144 static const struct v4l2_subdev_internal_ops imx258_internal_ops = {
1145 .open = imx258_open,
1148 /* Initialize control handlers */
1149 static int imx258_init_controls(struct imx258 *imx258)
1151 struct i2c_client *client = v4l2_get_subdevdata(&imx258->sd);
1152 struct v4l2_fwnode_device_properties props;
1153 struct v4l2_ctrl_handler *ctrl_hdlr;
1154 struct v4l2_ctrl *vflip, *hflip;
1161 ctrl_hdlr = &imx258->ctrl_handler;
1162 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 13);
1166 mutex_init(&imx258->mutex);
1167 ctrl_hdlr->lock = &imx258->mutex;
1168 imx258->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1171 ARRAY_SIZE(link_freq_menu_items) - 1,
1173 link_freq_menu_items);
1175 if (imx258->link_freq)
1176 imx258->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1178 /* The driver only supports one bayer order and flips by default. */
1179 hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1180 V4L2_CID_HFLIP, 1, 1, 1, 1);
1182 hflip->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1184 vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1185 V4L2_CID_VFLIP, 1, 1, 1, 1);
1187 vflip->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1189 pixel_rate_max = link_freq_to_pixel_rate(link_freq_menu_items[0]);
1190 pixel_rate_min = link_freq_to_pixel_rate(link_freq_menu_items[1]);
1191 /* By default, PIXEL_RATE is read only */
1192 imx258->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops,
1193 V4L2_CID_PIXEL_RATE,
1194 pixel_rate_min, pixel_rate_max,
1198 vblank_def = imx258->cur_mode->vts_def - imx258->cur_mode->height;
1199 vblank_min = imx258->cur_mode->vts_min - imx258->cur_mode->height;
1200 imx258->vblank = v4l2_ctrl_new_std(
1201 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_VBLANK,
1203 IMX258_VTS_MAX - imx258->cur_mode->height, 1,
1207 imx258->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1209 imx258->hblank = v4l2_ctrl_new_std(
1210 ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_HBLANK,
1211 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1212 IMX258_PPL_DEFAULT - imx258->cur_mode->width,
1214 IMX258_PPL_DEFAULT - imx258->cur_mode->width);
1217 imx258->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1219 imx258->exposure = v4l2_ctrl_new_std(
1220 ctrl_hdlr, &imx258_ctrl_ops,
1221 V4L2_CID_EXPOSURE, IMX258_EXPOSURE_MIN,
1222 IMX258_EXPOSURE_MAX, IMX258_EXPOSURE_STEP,
1223 IMX258_EXPOSURE_DEFAULT);
1225 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1226 IMX258_ANA_GAIN_MIN, IMX258_ANA_GAIN_MAX,
1227 IMX258_ANA_GAIN_STEP, IMX258_ANA_GAIN_DEFAULT);
1229 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1230 IMX258_DGTL_GAIN_MIN, IMX258_DGTL_GAIN_MAX,
1231 IMX258_DGTL_GAIN_STEP,
1232 IMX258_DGTL_GAIN_DEFAULT);
1234 v4l2_ctrl_new_std(ctrl_hdlr, &imx258_ctrl_ops, V4L2_CID_WIDE_DYNAMIC_RANGE,
1235 0, 1, 1, IMX258_HDR_RATIO_DEFAULT);
1237 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx258_ctrl_ops,
1238 V4L2_CID_TEST_PATTERN,
1239 ARRAY_SIZE(imx258_test_pattern_menu) - 1,
1240 0, 0, imx258_test_pattern_menu);
1242 if (ctrl_hdlr->error) {
1243 ret = ctrl_hdlr->error;
1244 dev_err(&client->dev, "%s control init failed (%d)\n",
1249 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1253 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx258_ctrl_ops,
1258 imx258->sd.ctrl_handler = ctrl_hdlr;
1263 v4l2_ctrl_handler_free(ctrl_hdlr);
1264 mutex_destroy(&imx258->mutex);
1269 static void imx258_free_controls(struct imx258 *imx258)
1271 v4l2_ctrl_handler_free(imx258->sd.ctrl_handler);
1272 mutex_destroy(&imx258->mutex);
1275 static int imx258_probe(struct i2c_client *client)
1277 struct imx258 *imx258;
1281 imx258 = devm_kzalloc(&client->dev, sizeof(*imx258), GFP_KERNEL);
1285 imx258->clk = devm_clk_get_optional(&client->dev, NULL);
1286 if (IS_ERR(imx258->clk))
1287 return dev_err_probe(&client->dev, PTR_ERR(imx258->clk),
1288 "error getting clock\n");
1290 dev_dbg(&client->dev,
1291 "no clock provided, using clock-frequency property\n");
1293 device_property_read_u32(&client->dev, "clock-frequency", &val);
1295 val = clk_get_rate(imx258->clk);
1297 if (val != IMX258_INPUT_CLOCK_FREQ) {
1298 dev_err(&client->dev, "input clock frequency not supported\n");
1302 /* Initialize subdev */
1303 v4l2_i2c_subdev_init(&imx258->sd, client, &imx258_subdev_ops);
1305 /* Will be powered off via pm_runtime_idle */
1306 ret = imx258_power_on(&client->dev);
1310 /* Check module identity */
1311 ret = imx258_identify_module(imx258);
1313 goto error_identify;
1315 /* Set default mode to max resolution */
1316 imx258->cur_mode = &supported_modes[0];
1318 ret = imx258_init_controls(imx258);
1320 goto error_identify;
1322 /* Initialize subdev */
1323 imx258->sd.internal_ops = &imx258_internal_ops;
1324 imx258->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1325 imx258->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1327 /* Initialize source pad */
1328 imx258->pad.flags = MEDIA_PAD_FL_SOURCE;
1330 ret = media_entity_pads_init(&imx258->sd.entity, 1, &imx258->pad);
1332 goto error_handler_free;
1334 ret = v4l2_async_register_subdev_sensor(&imx258->sd);
1336 goto error_media_entity;
1338 pm_runtime_set_active(&client->dev);
1339 pm_runtime_enable(&client->dev);
1340 pm_runtime_idle(&client->dev);
1345 media_entity_cleanup(&imx258->sd.entity);
1348 imx258_free_controls(imx258);
1351 imx258_power_off(&client->dev);
1356 static void imx258_remove(struct i2c_client *client)
1358 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1359 struct imx258 *imx258 = to_imx258(sd);
1361 v4l2_async_unregister_subdev(sd);
1362 media_entity_cleanup(&sd->entity);
1363 imx258_free_controls(imx258);
1365 pm_runtime_disable(&client->dev);
1366 if (!pm_runtime_status_suspended(&client->dev))
1367 imx258_power_off(&client->dev);
1368 pm_runtime_set_suspended(&client->dev);
1371 static const struct dev_pm_ops imx258_pm_ops = {
1372 SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend, imx258_resume)
1373 SET_RUNTIME_PM_OPS(imx258_power_off, imx258_power_on, NULL)
1377 static const struct acpi_device_id imx258_acpi_ids[] = {
1382 MODULE_DEVICE_TABLE(acpi, imx258_acpi_ids);
1385 static const struct of_device_id imx258_dt_ids[] = {
1386 { .compatible = "sony,imx258" },
1389 MODULE_DEVICE_TABLE(of, imx258_dt_ids);
1391 static struct i2c_driver imx258_i2c_driver = {
1394 .pm = &imx258_pm_ops,
1395 .acpi_match_table = ACPI_PTR(imx258_acpi_ids),
1396 .of_match_table = imx258_dt_ids,
1398 .probe = imx258_probe,
1399 .remove = imx258_remove,
1402 module_i2c_driver(imx258_i2c_driver);
1404 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>");
1405 MODULE_AUTHOR("Chiang, Alan");
1406 MODULE_AUTHOR("Chen, Jason");
1407 MODULE_DESCRIPTION("Sony IMX258 sensor driver");
1408 MODULE_LICENSE("GPL v2");