1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/module.h>
8 #include <linux/pm_runtime.h>
9 #include <media/v4l2-ctrls.h>
10 #include <media/v4l2-device.h>
11 #include <media/v4l2-event.h>
12 #include <media/v4l2-fwnode.h>
14 #define IMX355_REG_MODE_SELECT 0x0100
15 #define IMX355_MODE_STANDBY 0x00
16 #define IMX355_MODE_STREAMING 0x01
19 #define IMX355_REG_CHIP_ID 0x0016
20 #define IMX355_CHIP_ID 0x0355
22 /* V_TIMING internal */
23 #define IMX355_REG_FLL 0x0340
24 #define IMX355_FLL_MAX 0xffff
26 /* Exposure control */
27 #define IMX355_REG_EXPOSURE 0x0202
28 #define IMX355_EXPOSURE_MIN 1
29 #define IMX355_EXPOSURE_STEP 1
30 #define IMX355_EXPOSURE_DEFAULT 0x0282
32 /* Analog gain control */
33 #define IMX355_REG_ANALOG_GAIN 0x0204
34 #define IMX355_ANA_GAIN_MIN 0
35 #define IMX355_ANA_GAIN_MAX 960
36 #define IMX355_ANA_GAIN_STEP 1
37 #define IMX355_ANA_GAIN_DEFAULT 0
39 /* Digital gain control */
40 #define IMX355_REG_DPGA_USE_GLOBAL_GAIN 0x3070
41 #define IMX355_REG_DIG_GAIN_GLOBAL 0x020e
42 #define IMX355_DGTL_GAIN_MIN 256
43 #define IMX355_DGTL_GAIN_MAX 4095
44 #define IMX355_DGTL_GAIN_STEP 1
45 #define IMX355_DGTL_GAIN_DEFAULT 256
47 /* Test Pattern Control */
48 #define IMX355_REG_TEST_PATTERN 0x0600
49 #define IMX355_TEST_PATTERN_DISABLED 0
50 #define IMX355_TEST_PATTERN_SOLID_COLOR 1
51 #define IMX355_TEST_PATTERN_COLOR_BARS 2
52 #define IMX355_TEST_PATTERN_GRAY_COLOR_BARS 3
53 #define IMX355_TEST_PATTERN_PN9 4
56 #define IMX355_REG_ORIENTATION 0x0101
58 /* default link frequency and external clock */
59 #define IMX355_LINK_FREQ_DEFAULT 360000000
60 #define IMX355_EXT_CLK 19200000
61 #define IMX355_LINK_FREQ_INDEX 0
68 struct imx355_reg_list {
70 const struct imx355_reg *regs;
73 /* Mode : resolution and related config&values */
87 /* index of link frequency */
90 /* Default register values */
91 struct imx355_reg_list reg_list;
95 u32 ext_clk; /* sensor external clk */
96 s64 *link_freqs; /* CSI-2 link frequencies */
97 unsigned int nr_of_link_freqs;
101 struct v4l2_subdev sd;
102 struct media_pad pad;
104 struct v4l2_ctrl_handler ctrl_handler;
106 struct v4l2_ctrl *link_freq;
107 struct v4l2_ctrl *pixel_rate;
108 struct v4l2_ctrl *vblank;
109 struct v4l2_ctrl *hblank;
110 struct v4l2_ctrl *exposure;
111 struct v4l2_ctrl *vflip;
112 struct v4l2_ctrl *hflip;
115 const struct imx355_mode *cur_mode;
117 struct imx355_hwcfg *hwcfg;
118 s64 link_def_freq; /* CSI-2 link default frequency */
121 * Mutex for serialized access:
122 * Protect sensor set pad format and start/stop streaming safely.
123 * Protect access to sensor v4l2 controls.
128 static const struct imx355_reg imx355_global_regs[] = {
183 static const struct imx355_reg_list imx355_global_setting = {
184 .num_of_regs = ARRAY_SIZE(imx355_global_regs),
185 .regs = imx355_global_regs,
188 static const struct imx355_reg mode_3268x2448_regs[] = {
237 static const struct imx355_reg mode_3264x2448_regs[] = {
286 static const struct imx355_reg mode_3280x2464_regs[] = {
335 static const struct imx355_reg mode_1940x1096_regs[] = {
384 static const struct imx355_reg mode_1936x1096_regs[] = {
433 static const struct imx355_reg mode_1924x1080_regs[] = {
482 static const struct imx355_reg mode_1920x1080_regs[] = {
531 static const struct imx355_reg mode_1640x1232_regs[] = {
580 static const struct imx355_reg mode_1640x922_regs[] = {
629 static const struct imx355_reg mode_1300x736_regs[] = {
678 static const struct imx355_reg mode_1296x736_regs[] = {
727 static const struct imx355_reg mode_1284x720_regs[] = {
776 static const struct imx355_reg mode_1280x720_regs[] = {
825 static const struct imx355_reg mode_820x616_regs[] = {
874 static const char * const imx355_test_pattern_menu[] = {
877 "Eight Vertical Colour Bars",
878 "Colour Bars With Fade to Grey",
879 "Pseudorandom Sequence (PN9)",
882 /* supported link frequencies */
883 static const s64 link_freq_menu_items[] = {
884 IMX355_LINK_FREQ_DEFAULT,
888 static const struct imx355_mode supported_modes[] = {
895 .link_freq_index = IMX355_LINK_FREQ_INDEX,
897 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
898 .regs = mode_3280x2464_regs,
907 .link_freq_index = IMX355_LINK_FREQ_INDEX,
909 .num_of_regs = ARRAY_SIZE(mode_3268x2448_regs),
910 .regs = mode_3268x2448_regs,
919 .link_freq_index = IMX355_LINK_FREQ_INDEX,
921 .num_of_regs = ARRAY_SIZE(mode_3264x2448_regs),
922 .regs = mode_3264x2448_regs,
931 .link_freq_index = IMX355_LINK_FREQ_INDEX,
933 .num_of_regs = ARRAY_SIZE(mode_1940x1096_regs),
934 .regs = mode_1940x1096_regs,
943 .link_freq_index = IMX355_LINK_FREQ_INDEX,
945 .num_of_regs = ARRAY_SIZE(mode_1936x1096_regs),
946 .regs = mode_1936x1096_regs,
955 .link_freq_index = IMX355_LINK_FREQ_INDEX,
957 .num_of_regs = ARRAY_SIZE(mode_1924x1080_regs),
958 .regs = mode_1924x1080_regs,
967 .link_freq_index = IMX355_LINK_FREQ_INDEX,
969 .num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
970 .regs = mode_1920x1080_regs,
979 .link_freq_index = IMX355_LINK_FREQ_INDEX,
981 .num_of_regs = ARRAY_SIZE(mode_1640x1232_regs),
982 .regs = mode_1640x1232_regs,
991 .link_freq_index = IMX355_LINK_FREQ_INDEX,
993 .num_of_regs = ARRAY_SIZE(mode_1640x922_regs),
994 .regs = mode_1640x922_regs,
1003 .link_freq_index = IMX355_LINK_FREQ_INDEX,
1005 .num_of_regs = ARRAY_SIZE(mode_1300x736_regs),
1006 .regs = mode_1300x736_regs,
1015 .link_freq_index = IMX355_LINK_FREQ_INDEX,
1017 .num_of_regs = ARRAY_SIZE(mode_1296x736_regs),
1018 .regs = mode_1296x736_regs,
1027 .link_freq_index = IMX355_LINK_FREQ_INDEX,
1029 .num_of_regs = ARRAY_SIZE(mode_1284x720_regs),
1030 .regs = mode_1284x720_regs,
1039 .link_freq_index = IMX355_LINK_FREQ_INDEX,
1041 .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1042 .regs = mode_1280x720_regs,
1051 .link_freq_index = IMX355_LINK_FREQ_INDEX,
1053 .num_of_regs = ARRAY_SIZE(mode_820x616_regs),
1054 .regs = mode_820x616_regs,
1059 static inline struct imx355 *to_imx355(struct v4l2_subdev *_sd)
1061 return container_of(_sd, struct imx355, sd);
1064 /* Get bayer order based on flip setting. */
1065 static u32 imx355_get_format_code(struct imx355 *imx355)
1068 * Only one bayer order is supported.
1069 * It depends on the flip settings.
1072 static const u32 codes[2][2] = {
1073 { MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, },
1074 { MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, },
1077 lockdep_assert_held(&imx355->mutex);
1078 code = codes[imx355->vflip->val][imx355->hflip->val];
1083 /* Read registers up to 4 at a time */
1084 static int imx355_read_reg(struct imx355 *imx355, u16 reg, u32 len, u32 *val)
1086 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1087 struct i2c_msg msgs[2];
1089 u8 data_buf[4] = { 0 };
1095 put_unaligned_be16(reg, addr_buf);
1096 /* Write register address */
1097 msgs[0].addr = client->addr;
1099 msgs[0].len = ARRAY_SIZE(addr_buf);
1100 msgs[0].buf = addr_buf;
1102 /* Read data from register */
1103 msgs[1].addr = client->addr;
1104 msgs[1].flags = I2C_M_RD;
1106 msgs[1].buf = &data_buf[4 - len];
1108 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1109 if (ret != ARRAY_SIZE(msgs))
1112 *val = get_unaligned_be32(data_buf);
1117 /* Write registers up to 4 at a time */
1118 static int imx355_write_reg(struct imx355 *imx355, u16 reg, u32 len, u32 val)
1120 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1126 put_unaligned_be16(reg, buf);
1127 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1128 if (i2c_master_send(client, buf, len + 2) != len + 2)
1134 /* Write a list of registers */
1135 static int imx355_write_regs(struct imx355 *imx355,
1136 const struct imx355_reg *regs, u32 len)
1138 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1142 for (i = 0; i < len; i++) {
1143 ret = imx355_write_reg(imx355, regs[i].address, 1, regs[i].val);
1145 dev_err_ratelimited(&client->dev,
1146 "write reg 0x%4.4x return err %d",
1147 regs[i].address, ret);
1156 /* Open sub-device */
1157 static int imx355_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1159 struct imx355 *imx355 = to_imx355(sd);
1160 struct v4l2_mbus_framefmt *try_fmt =
1161 v4l2_subdev_state_get_format(fh->state, 0);
1163 mutex_lock(&imx355->mutex);
1165 /* Initialize try_fmt */
1166 try_fmt->width = imx355->cur_mode->width;
1167 try_fmt->height = imx355->cur_mode->height;
1168 try_fmt->code = imx355_get_format_code(imx355);
1169 try_fmt->field = V4L2_FIELD_NONE;
1171 mutex_unlock(&imx355->mutex);
1176 static int imx355_set_ctrl(struct v4l2_ctrl *ctrl)
1178 struct imx355 *imx355 = container_of(ctrl->handler,
1179 struct imx355, ctrl_handler);
1180 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1184 /* Propagate change of current control to all related controls */
1186 case V4L2_CID_VBLANK:
1187 /* Update max exposure while meeting expected vblanking */
1188 max = imx355->cur_mode->height + ctrl->val - 10;
1189 __v4l2_ctrl_modify_range(imx355->exposure,
1190 imx355->exposure->minimum,
1191 max, imx355->exposure->step, max);
1196 * Applying V4L2 control value only happens
1197 * when power is up for streaming
1199 if (!pm_runtime_get_if_in_use(&client->dev))
1203 case V4L2_CID_ANALOGUE_GAIN:
1204 /* Analog gain = 1024/(1024 - ctrl->val) times */
1205 ret = imx355_write_reg(imx355, IMX355_REG_ANALOG_GAIN, 2,
1208 case V4L2_CID_DIGITAL_GAIN:
1209 ret = imx355_write_reg(imx355, IMX355_REG_DIG_GAIN_GLOBAL, 2,
1212 case V4L2_CID_EXPOSURE:
1213 ret = imx355_write_reg(imx355, IMX355_REG_EXPOSURE, 2,
1216 case V4L2_CID_VBLANK:
1217 /* Update FLL that meets expected vertical blanking */
1218 ret = imx355_write_reg(imx355, IMX355_REG_FLL, 2,
1219 imx355->cur_mode->height + ctrl->val);
1221 case V4L2_CID_TEST_PATTERN:
1222 ret = imx355_write_reg(imx355, IMX355_REG_TEST_PATTERN,
1225 case V4L2_CID_HFLIP:
1226 case V4L2_CID_VFLIP:
1227 ret = imx355_write_reg(imx355, IMX355_REG_ORIENTATION, 1,
1228 imx355->hflip->val |
1229 imx355->vflip->val << 1);
1233 dev_info(&client->dev, "ctrl(id:0x%x,val:0x%x) is not handled",
1234 ctrl->id, ctrl->val);
1238 pm_runtime_put(&client->dev);
1243 static const struct v4l2_ctrl_ops imx355_ctrl_ops = {
1244 .s_ctrl = imx355_set_ctrl,
1247 static int imx355_enum_mbus_code(struct v4l2_subdev *sd,
1248 struct v4l2_subdev_state *sd_state,
1249 struct v4l2_subdev_mbus_code_enum *code)
1251 struct imx355 *imx355 = to_imx355(sd);
1253 if (code->index > 0)
1256 mutex_lock(&imx355->mutex);
1257 code->code = imx355_get_format_code(imx355);
1258 mutex_unlock(&imx355->mutex);
1263 static int imx355_enum_frame_size(struct v4l2_subdev *sd,
1264 struct v4l2_subdev_state *sd_state,
1265 struct v4l2_subdev_frame_size_enum *fse)
1267 struct imx355 *imx355 = to_imx355(sd);
1269 if (fse->index >= ARRAY_SIZE(supported_modes))
1272 mutex_lock(&imx355->mutex);
1273 if (fse->code != imx355_get_format_code(imx355)) {
1274 mutex_unlock(&imx355->mutex);
1277 mutex_unlock(&imx355->mutex);
1279 fse->min_width = supported_modes[fse->index].width;
1280 fse->max_width = fse->min_width;
1281 fse->min_height = supported_modes[fse->index].height;
1282 fse->max_height = fse->min_height;
1287 static void imx355_update_pad_format(struct imx355 *imx355,
1288 const struct imx355_mode *mode,
1289 struct v4l2_subdev_format *fmt)
1291 fmt->format.width = mode->width;
1292 fmt->format.height = mode->height;
1293 fmt->format.code = imx355_get_format_code(imx355);
1294 fmt->format.field = V4L2_FIELD_NONE;
1297 static int imx355_do_get_pad_format(struct imx355 *imx355,
1298 struct v4l2_subdev_state *sd_state,
1299 struct v4l2_subdev_format *fmt)
1301 struct v4l2_mbus_framefmt *framefmt;
1303 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1304 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
1305 fmt->format = *framefmt;
1307 imx355_update_pad_format(imx355, imx355->cur_mode, fmt);
1313 static int imx355_get_pad_format(struct v4l2_subdev *sd,
1314 struct v4l2_subdev_state *sd_state,
1315 struct v4l2_subdev_format *fmt)
1317 struct imx355 *imx355 = to_imx355(sd);
1320 mutex_lock(&imx355->mutex);
1321 ret = imx355_do_get_pad_format(imx355, sd_state, fmt);
1322 mutex_unlock(&imx355->mutex);
1328 imx355_set_pad_format(struct v4l2_subdev *sd,
1329 struct v4l2_subdev_state *sd_state,
1330 struct v4l2_subdev_format *fmt)
1332 struct imx355 *imx355 = to_imx355(sd);
1333 const struct imx355_mode *mode;
1334 struct v4l2_mbus_framefmt *framefmt;
1341 mutex_lock(&imx355->mutex);
1344 * Only one bayer order is supported.
1345 * It depends on the flip settings.
1347 fmt->format.code = imx355_get_format_code(imx355);
1349 mode = v4l2_find_nearest_size(supported_modes,
1350 ARRAY_SIZE(supported_modes),
1352 fmt->format.width, fmt->format.height);
1353 imx355_update_pad_format(imx355, mode, fmt);
1354 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1355 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
1356 *framefmt = fmt->format;
1358 imx355->cur_mode = mode;
1359 pixel_rate = imx355->link_def_freq * 2 * 4;
1360 do_div(pixel_rate, 10);
1361 __v4l2_ctrl_s_ctrl_int64(imx355->pixel_rate, pixel_rate);
1362 /* Update limits and set FPS to default */
1363 height = imx355->cur_mode->height;
1364 vblank_def = imx355->cur_mode->fll_def - height;
1365 vblank_min = imx355->cur_mode->fll_min - height;
1366 height = IMX355_FLL_MAX - height;
1367 __v4l2_ctrl_modify_range(imx355->vblank, vblank_min, height, 1,
1369 __v4l2_ctrl_s_ctrl(imx355->vblank, vblank_def);
1370 h_blank = mode->llp - imx355->cur_mode->width;
1372 * Currently hblank is not changeable.
1373 * So FPS control is done only by vblank.
1375 __v4l2_ctrl_modify_range(imx355->hblank, h_blank,
1376 h_blank, 1, h_blank);
1379 mutex_unlock(&imx355->mutex);
1384 /* Start streaming */
1385 static int imx355_start_streaming(struct imx355 *imx355)
1387 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1388 const struct imx355_reg_list *reg_list;
1391 /* Global Setting */
1392 reg_list = &imx355_global_setting;
1393 ret = imx355_write_regs(imx355, reg_list->regs, reg_list->num_of_regs);
1395 dev_err(&client->dev, "failed to set global settings");
1399 /* Apply default values of current mode */
1400 reg_list = &imx355->cur_mode->reg_list;
1401 ret = imx355_write_regs(imx355, reg_list->regs, reg_list->num_of_regs);
1403 dev_err(&client->dev, "failed to set mode");
1407 /* set digital gain control to all color mode */
1408 ret = imx355_write_reg(imx355, IMX355_REG_DPGA_USE_GLOBAL_GAIN, 1, 1);
1412 /* Apply customized values from user */
1413 ret = __v4l2_ctrl_handler_setup(imx355->sd.ctrl_handler);
1417 return imx355_write_reg(imx355, IMX355_REG_MODE_SELECT,
1418 1, IMX355_MODE_STREAMING);
1421 /* Stop streaming */
1422 static int imx355_stop_streaming(struct imx355 *imx355)
1424 return imx355_write_reg(imx355, IMX355_REG_MODE_SELECT,
1425 1, IMX355_MODE_STANDBY);
1428 static int imx355_set_stream(struct v4l2_subdev *sd, int enable)
1430 struct imx355 *imx355 = to_imx355(sd);
1431 struct i2c_client *client = v4l2_get_subdevdata(sd);
1434 mutex_lock(&imx355->mutex);
1437 ret = pm_runtime_resume_and_get(&client->dev);
1442 * Apply default & customized values
1443 * and then start streaming.
1445 ret = imx355_start_streaming(imx355);
1449 imx355_stop_streaming(imx355);
1450 pm_runtime_put(&client->dev);
1453 /* vflip and hflip cannot change during streaming */
1454 __v4l2_ctrl_grab(imx355->vflip, enable);
1455 __v4l2_ctrl_grab(imx355->hflip, enable);
1457 mutex_unlock(&imx355->mutex);
1462 pm_runtime_put(&client->dev);
1464 mutex_unlock(&imx355->mutex);
1469 /* Verify chip ID */
1470 static int imx355_identify_module(struct imx355 *imx355)
1472 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1476 ret = imx355_read_reg(imx355, IMX355_REG_CHIP_ID, 2, &val);
1480 if (val != IMX355_CHIP_ID) {
1481 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1482 IMX355_CHIP_ID, val);
1488 static const struct v4l2_subdev_core_ops imx355_subdev_core_ops = {
1489 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1490 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1493 static const struct v4l2_subdev_video_ops imx355_video_ops = {
1494 .s_stream = imx355_set_stream,
1497 static const struct v4l2_subdev_pad_ops imx355_pad_ops = {
1498 .enum_mbus_code = imx355_enum_mbus_code,
1499 .get_fmt = imx355_get_pad_format,
1500 .set_fmt = imx355_set_pad_format,
1501 .enum_frame_size = imx355_enum_frame_size,
1504 static const struct v4l2_subdev_ops imx355_subdev_ops = {
1505 .core = &imx355_subdev_core_ops,
1506 .video = &imx355_video_ops,
1507 .pad = &imx355_pad_ops,
1510 static const struct media_entity_operations imx355_subdev_entity_ops = {
1511 .link_validate = v4l2_subdev_link_validate,
1514 static const struct v4l2_subdev_internal_ops imx355_internal_ops = {
1515 .open = imx355_open,
1518 /* Initialize control handlers */
1519 static int imx355_init_controls(struct imx355 *imx355)
1521 struct i2c_client *client = v4l2_get_subdevdata(&imx355->sd);
1522 struct v4l2_ctrl_handler *ctrl_hdlr;
1528 const struct imx355_mode *mode;
1532 ctrl_hdlr = &imx355->ctrl_handler;
1533 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1537 ctrl_hdlr->lock = &imx355->mutex;
1538 max = ARRAY_SIZE(link_freq_menu_items) - 1;
1539 imx355->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx355_ctrl_ops,
1540 V4L2_CID_LINK_FREQ, max, 0,
1541 link_freq_menu_items);
1542 if (imx355->link_freq)
1543 imx355->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1545 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1546 pixel_rate = imx355->link_def_freq * 2 * 4;
1547 do_div(pixel_rate, 10);
1548 /* By default, PIXEL_RATE is read only */
1549 imx355->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1550 V4L2_CID_PIXEL_RATE, pixel_rate,
1551 pixel_rate, 1, pixel_rate);
1553 /* Initialize vblank/hblank/exposure parameters based on current mode */
1554 mode = imx355->cur_mode;
1555 vblank_def = mode->fll_def - mode->height;
1556 vblank_min = mode->fll_min - mode->height;
1557 imx355->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1558 V4L2_CID_VBLANK, vblank_min,
1559 IMX355_FLL_MAX - mode->height,
1562 hblank = mode->llp - mode->width;
1563 imx355->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1564 V4L2_CID_HBLANK, hblank, hblank,
1567 imx355->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1569 /* fll >= exposure time + adjust parameter (default value is 10) */
1570 exposure_max = mode->fll_def - 10;
1571 imx355->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1573 IMX355_EXPOSURE_MIN, exposure_max,
1574 IMX355_EXPOSURE_STEP,
1575 IMX355_EXPOSURE_DEFAULT);
1577 imx355->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1578 V4L2_CID_HFLIP, 0, 1, 1, 0);
1580 imx355->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1581 imx355->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops,
1582 V4L2_CID_VFLIP, 0, 1, 1, 0);
1584 imx355->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1586 v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1587 IMX355_ANA_GAIN_MIN, IMX355_ANA_GAIN_MAX,
1588 IMX355_ANA_GAIN_STEP, IMX355_ANA_GAIN_DEFAULT);
1591 v4l2_ctrl_new_std(ctrl_hdlr, &imx355_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1592 IMX355_DGTL_GAIN_MIN, IMX355_DGTL_GAIN_MAX,
1593 IMX355_DGTL_GAIN_STEP, IMX355_DGTL_GAIN_DEFAULT);
1595 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx355_ctrl_ops,
1596 V4L2_CID_TEST_PATTERN,
1597 ARRAY_SIZE(imx355_test_pattern_menu) - 1,
1598 0, 0, imx355_test_pattern_menu);
1599 if (ctrl_hdlr->error) {
1600 ret = ctrl_hdlr->error;
1601 dev_err(&client->dev, "control init failed: %d", ret);
1605 imx355->sd.ctrl_handler = ctrl_hdlr;
1610 v4l2_ctrl_handler_free(ctrl_hdlr);
1615 static struct imx355_hwcfg *imx355_get_hwcfg(struct device *dev)
1617 struct imx355_hwcfg *cfg;
1618 struct v4l2_fwnode_endpoint bus_cfg = {
1619 .bus_type = V4L2_MBUS_CSI2_DPHY
1621 struct fwnode_handle *ep;
1622 struct fwnode_handle *fwnode = dev_fwnode(dev);
1629 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1633 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1637 cfg = devm_kzalloc(dev, sizeof(*cfg), GFP_KERNEL);
1641 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1644 dev_err(dev, "can't get clock frequency");
1648 dev_dbg(dev, "ext clk: %d", cfg->ext_clk);
1649 if (cfg->ext_clk != IMX355_EXT_CLK) {
1650 dev_err(dev, "external clock %d is not supported",
1655 dev_dbg(dev, "num of link freqs: %d", bus_cfg.nr_of_link_frequencies);
1656 if (!bus_cfg.nr_of_link_frequencies) {
1657 dev_warn(dev, "no link frequencies defined");
1661 cfg->nr_of_link_freqs = bus_cfg.nr_of_link_frequencies;
1662 cfg->link_freqs = devm_kcalloc(dev,
1663 bus_cfg.nr_of_link_frequencies + 1,
1664 sizeof(*cfg->link_freqs), GFP_KERNEL);
1665 if (!cfg->link_freqs)
1668 for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++) {
1669 cfg->link_freqs[i] = bus_cfg.link_frequencies[i];
1670 dev_dbg(dev, "link_freq[%d] = %lld", i, cfg->link_freqs[i]);
1673 v4l2_fwnode_endpoint_free(&bus_cfg);
1674 fwnode_handle_put(ep);
1678 v4l2_fwnode_endpoint_free(&bus_cfg);
1679 fwnode_handle_put(ep);
1683 static int imx355_probe(struct i2c_client *client)
1685 struct imx355 *imx355;
1689 imx355 = devm_kzalloc(&client->dev, sizeof(*imx355), GFP_KERNEL);
1693 mutex_init(&imx355->mutex);
1695 /* Initialize subdev */
1696 v4l2_i2c_subdev_init(&imx355->sd, client, &imx355_subdev_ops);
1698 /* Check module identity */
1699 ret = imx355_identify_module(imx355);
1701 dev_err(&client->dev, "failed to find sensor: %d", ret);
1705 imx355->hwcfg = imx355_get_hwcfg(&client->dev);
1706 if (!imx355->hwcfg) {
1707 dev_err(&client->dev, "failed to get hwcfg");
1712 imx355->link_def_freq = link_freq_menu_items[IMX355_LINK_FREQ_INDEX];
1713 for (i = 0; i < imx355->hwcfg->nr_of_link_freqs; i++) {
1714 if (imx355->hwcfg->link_freqs[i] == imx355->link_def_freq) {
1715 dev_dbg(&client->dev, "link freq index %d matched", i);
1720 if (i == imx355->hwcfg->nr_of_link_freqs) {
1721 dev_err(&client->dev, "no link frequency supported");
1726 /* Set default mode to max resolution */
1727 imx355->cur_mode = &supported_modes[0];
1729 ret = imx355_init_controls(imx355);
1731 dev_err(&client->dev, "failed to init controls: %d", ret);
1735 /* Initialize subdev */
1736 imx355->sd.internal_ops = &imx355_internal_ops;
1737 imx355->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1738 V4L2_SUBDEV_FL_HAS_EVENTS;
1739 imx355->sd.entity.ops = &imx355_subdev_entity_ops;
1740 imx355->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1742 /* Initialize source pad */
1743 imx355->pad.flags = MEDIA_PAD_FL_SOURCE;
1744 ret = media_entity_pads_init(&imx355->sd.entity, 1, &imx355->pad);
1746 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1747 goto error_handler_free;
1750 ret = v4l2_async_register_subdev_sensor(&imx355->sd);
1752 goto error_media_entity;
1755 * Device is already turned on by i2c-core with ACPI domain PM.
1756 * Enable runtime PM and turn off the device.
1758 pm_runtime_set_active(&client->dev);
1759 pm_runtime_enable(&client->dev);
1760 pm_runtime_idle(&client->dev);
1765 media_entity_cleanup(&imx355->sd.entity);
1768 v4l2_ctrl_handler_free(imx355->sd.ctrl_handler);
1771 mutex_destroy(&imx355->mutex);
1776 static void imx355_remove(struct i2c_client *client)
1778 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1779 struct imx355 *imx355 = to_imx355(sd);
1781 v4l2_async_unregister_subdev(sd);
1782 media_entity_cleanup(&sd->entity);
1783 v4l2_ctrl_handler_free(sd->ctrl_handler);
1785 pm_runtime_disable(&client->dev);
1786 pm_runtime_set_suspended(&client->dev);
1788 mutex_destroy(&imx355->mutex);
1791 static const struct acpi_device_id imx355_acpi_ids[] __maybe_unused = {
1795 MODULE_DEVICE_TABLE(acpi, imx355_acpi_ids);
1797 static struct i2c_driver imx355_i2c_driver = {
1800 .acpi_match_table = ACPI_PTR(imx355_acpi_ids),
1802 .probe = imx355_probe,
1803 .remove = imx355_remove,
1805 module_i2c_driver(imx355_i2c_driver);
1807 MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>");
1808 MODULE_AUTHOR("Rapolu, Chiranjeevi");
1809 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
1810 MODULE_AUTHOR("Yang, Hyungwoo");
1811 MODULE_DESCRIPTION("Sony imx355 sensor driver");
1812 MODULE_LICENSE("GPL v2");