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.
127 /* Streaming on/off */
131 static const struct imx355_reg imx355_global_regs
[] = {
186 static const struct imx355_reg_list imx355_global_setting
= {
187 .num_of_regs
= ARRAY_SIZE(imx355_global_regs
),
188 .regs
= imx355_global_regs
,
191 static const struct imx355_reg mode_3268x2448_regs
[] = {
240 static const struct imx355_reg mode_3264x2448_regs
[] = {
289 static const struct imx355_reg mode_3280x2464_regs
[] = {
338 static const struct imx355_reg mode_1940x1096_regs
[] = {
387 static const struct imx355_reg mode_1936x1096_regs
[] = {
436 static const struct imx355_reg mode_1924x1080_regs
[] = {
485 static const struct imx355_reg mode_1920x1080_regs
[] = {
534 static const struct imx355_reg mode_1640x1232_regs
[] = {
583 static const struct imx355_reg mode_1640x922_regs
[] = {
632 static const struct imx355_reg mode_1300x736_regs
[] = {
681 static const struct imx355_reg mode_1296x736_regs
[] = {
730 static const struct imx355_reg mode_1284x720_regs
[] = {
779 static const struct imx355_reg mode_1280x720_regs
[] = {
828 static const struct imx355_reg mode_820x616_regs
[] = {
877 static const char * const imx355_test_pattern_menu
[] = {
880 "Eight Vertical Colour Bars",
881 "Colour Bars With Fade to Grey",
882 "Pseudorandom Sequence (PN9)",
885 /* supported link frequencies */
886 static const s64 link_freq_menu_items
[] = {
887 IMX355_LINK_FREQ_DEFAULT
,
891 static const struct imx355_mode supported_modes
[] = {
898 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
900 .num_of_regs
= ARRAY_SIZE(mode_3280x2464_regs
),
901 .regs
= mode_3280x2464_regs
,
910 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
912 .num_of_regs
= ARRAY_SIZE(mode_3268x2448_regs
),
913 .regs
= mode_3268x2448_regs
,
922 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
924 .num_of_regs
= ARRAY_SIZE(mode_3264x2448_regs
),
925 .regs
= mode_3264x2448_regs
,
934 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
936 .num_of_regs
= ARRAY_SIZE(mode_1940x1096_regs
),
937 .regs
= mode_1940x1096_regs
,
946 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
948 .num_of_regs
= ARRAY_SIZE(mode_1936x1096_regs
),
949 .regs
= mode_1936x1096_regs
,
958 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
960 .num_of_regs
= ARRAY_SIZE(mode_1924x1080_regs
),
961 .regs
= mode_1924x1080_regs
,
970 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
972 .num_of_regs
= ARRAY_SIZE(mode_1920x1080_regs
),
973 .regs
= mode_1920x1080_regs
,
982 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
984 .num_of_regs
= ARRAY_SIZE(mode_1640x1232_regs
),
985 .regs
= mode_1640x1232_regs
,
994 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
996 .num_of_regs
= ARRAY_SIZE(mode_1640x922_regs
),
997 .regs
= mode_1640x922_regs
,
1006 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1008 .num_of_regs
= ARRAY_SIZE(mode_1300x736_regs
),
1009 .regs
= mode_1300x736_regs
,
1018 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1020 .num_of_regs
= ARRAY_SIZE(mode_1296x736_regs
),
1021 .regs
= mode_1296x736_regs
,
1030 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1032 .num_of_regs
= ARRAY_SIZE(mode_1284x720_regs
),
1033 .regs
= mode_1284x720_regs
,
1042 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1044 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1045 .regs
= mode_1280x720_regs
,
1054 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1056 .num_of_regs
= ARRAY_SIZE(mode_820x616_regs
),
1057 .regs
= mode_820x616_regs
,
1062 static inline struct imx355
*to_imx355(struct v4l2_subdev
*_sd
)
1064 return container_of(_sd
, struct imx355
, sd
);
1067 /* Get bayer order based on flip setting. */
1068 static u32
imx355_get_format_code(struct imx355
*imx355
)
1071 * Only one bayer order is supported.
1072 * It depends on the flip settings.
1075 static const u32 codes
[2][2] = {
1076 { MEDIA_BUS_FMT_SRGGB10_1X10
, MEDIA_BUS_FMT_SGRBG10_1X10
, },
1077 { MEDIA_BUS_FMT_SGBRG10_1X10
, MEDIA_BUS_FMT_SBGGR10_1X10
, },
1080 lockdep_assert_held(&imx355
->mutex
);
1081 code
= codes
[imx355
->vflip
->val
][imx355
->hflip
->val
];
1086 /* Read registers up to 4 at a time */
1087 static int imx355_read_reg(struct imx355
*imx355
, u16 reg
, u32 len
, u32
*val
)
1089 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1090 struct i2c_msg msgs
[2];
1092 u8 data_buf
[4] = { 0 };
1098 put_unaligned_be16(reg
, addr_buf
);
1099 /* Write register address */
1100 msgs
[0].addr
= client
->addr
;
1102 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
1103 msgs
[0].buf
= addr_buf
;
1105 /* Read data from register */
1106 msgs
[1].addr
= client
->addr
;
1107 msgs
[1].flags
= I2C_M_RD
;
1109 msgs
[1].buf
= &data_buf
[4 - len
];
1111 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1112 if (ret
!= ARRAY_SIZE(msgs
))
1115 *val
= get_unaligned_be32(data_buf
);
1120 /* Write registers up to 4 at a time */
1121 static int imx355_write_reg(struct imx355
*imx355
, u16 reg
, u32 len
, u32 val
)
1123 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1129 put_unaligned_be16(reg
, buf
);
1130 put_unaligned_be32(val
<< (8 * (4 - len
)), buf
+ 2);
1131 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1137 /* Write a list of registers */
1138 static int imx355_write_regs(struct imx355
*imx355
,
1139 const struct imx355_reg
*regs
, u32 len
)
1141 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1145 for (i
= 0; i
< len
; i
++) {
1146 ret
= imx355_write_reg(imx355
, regs
[i
].address
, 1, regs
[i
].val
);
1148 dev_err_ratelimited(&client
->dev
,
1149 "write reg 0x%4.4x return err %d",
1150 regs
[i
].address
, ret
);
1159 /* Open sub-device */
1160 static int imx355_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1162 struct imx355
*imx355
= to_imx355(sd
);
1163 struct v4l2_mbus_framefmt
*try_fmt
=
1164 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
1166 mutex_lock(&imx355
->mutex
);
1168 /* Initialize try_fmt */
1169 try_fmt
->width
= imx355
->cur_mode
->width
;
1170 try_fmt
->height
= imx355
->cur_mode
->height
;
1171 try_fmt
->code
= imx355_get_format_code(imx355
);
1172 try_fmt
->field
= V4L2_FIELD_NONE
;
1174 mutex_unlock(&imx355
->mutex
);
1179 static int imx355_set_ctrl(struct v4l2_ctrl
*ctrl
)
1181 struct imx355
*imx355
= container_of(ctrl
->handler
,
1182 struct imx355
, ctrl_handler
);
1183 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1187 /* Propagate change of current control to all related controls */
1189 case V4L2_CID_VBLANK
:
1190 /* Update max exposure while meeting expected vblanking */
1191 max
= imx355
->cur_mode
->height
+ ctrl
->val
- 10;
1192 __v4l2_ctrl_modify_range(imx355
->exposure
,
1193 imx355
->exposure
->minimum
,
1194 max
, imx355
->exposure
->step
, max
);
1199 * Applying V4L2 control value only happens
1200 * when power is up for streaming
1202 if (!pm_runtime_get_if_in_use(&client
->dev
))
1206 case V4L2_CID_ANALOGUE_GAIN
:
1207 /* Analog gain = 1024/(1024 - ctrl->val) times */
1208 ret
= imx355_write_reg(imx355
, IMX355_REG_ANALOG_GAIN
, 2,
1211 case V4L2_CID_DIGITAL_GAIN
:
1212 ret
= imx355_write_reg(imx355
, IMX355_REG_DIG_GAIN_GLOBAL
, 2,
1215 case V4L2_CID_EXPOSURE
:
1216 ret
= imx355_write_reg(imx355
, IMX355_REG_EXPOSURE
, 2,
1219 case V4L2_CID_VBLANK
:
1220 /* Update FLL that meets expected vertical blanking */
1221 ret
= imx355_write_reg(imx355
, IMX355_REG_FLL
, 2,
1222 imx355
->cur_mode
->height
+ ctrl
->val
);
1224 case V4L2_CID_TEST_PATTERN
:
1225 ret
= imx355_write_reg(imx355
, IMX355_REG_TEST_PATTERN
,
1228 case V4L2_CID_HFLIP
:
1229 case V4L2_CID_VFLIP
:
1230 ret
= imx355_write_reg(imx355
, IMX355_REG_ORIENTATION
, 1,
1231 imx355
->hflip
->val
|
1232 imx355
->vflip
->val
<< 1);
1236 dev_info(&client
->dev
, "ctrl(id:0x%x,val:0x%x) is not handled",
1237 ctrl
->id
, ctrl
->val
);
1241 pm_runtime_put(&client
->dev
);
1246 static const struct v4l2_ctrl_ops imx355_ctrl_ops
= {
1247 .s_ctrl
= imx355_set_ctrl
,
1250 static int imx355_enum_mbus_code(struct v4l2_subdev
*sd
,
1251 struct v4l2_subdev_pad_config
*cfg
,
1252 struct v4l2_subdev_mbus_code_enum
*code
)
1254 struct imx355
*imx355
= to_imx355(sd
);
1256 if (code
->index
> 0)
1259 mutex_lock(&imx355
->mutex
);
1260 code
->code
= imx355_get_format_code(imx355
);
1261 mutex_unlock(&imx355
->mutex
);
1266 static int imx355_enum_frame_size(struct v4l2_subdev
*sd
,
1267 struct v4l2_subdev_pad_config
*cfg
,
1268 struct v4l2_subdev_frame_size_enum
*fse
)
1270 struct imx355
*imx355
= to_imx355(sd
);
1272 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1275 mutex_lock(&imx355
->mutex
);
1276 if (fse
->code
!= imx355_get_format_code(imx355
)) {
1277 mutex_unlock(&imx355
->mutex
);
1280 mutex_unlock(&imx355
->mutex
);
1282 fse
->min_width
= supported_modes
[fse
->index
].width
;
1283 fse
->max_width
= fse
->min_width
;
1284 fse
->min_height
= supported_modes
[fse
->index
].height
;
1285 fse
->max_height
= fse
->min_height
;
1290 static void imx355_update_pad_format(struct imx355
*imx355
,
1291 const struct imx355_mode
*mode
,
1292 struct v4l2_subdev_format
*fmt
)
1294 fmt
->format
.width
= mode
->width
;
1295 fmt
->format
.height
= mode
->height
;
1296 fmt
->format
.code
= imx355_get_format_code(imx355
);
1297 fmt
->format
.field
= V4L2_FIELD_NONE
;
1300 static int imx355_do_get_pad_format(struct imx355
*imx355
,
1301 struct v4l2_subdev_pad_config
*cfg
,
1302 struct v4l2_subdev_format
*fmt
)
1304 struct v4l2_mbus_framefmt
*framefmt
;
1305 struct v4l2_subdev
*sd
= &imx355
->sd
;
1307 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1308 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1309 fmt
->format
= *framefmt
;
1311 imx355_update_pad_format(imx355
, imx355
->cur_mode
, fmt
);
1317 static int imx355_get_pad_format(struct v4l2_subdev
*sd
,
1318 struct v4l2_subdev_pad_config
*cfg
,
1319 struct v4l2_subdev_format
*fmt
)
1321 struct imx355
*imx355
= to_imx355(sd
);
1324 mutex_lock(&imx355
->mutex
);
1325 ret
= imx355_do_get_pad_format(imx355
, cfg
, fmt
);
1326 mutex_unlock(&imx355
->mutex
);
1332 imx355_set_pad_format(struct v4l2_subdev
*sd
,
1333 struct v4l2_subdev_pad_config
*cfg
,
1334 struct v4l2_subdev_format
*fmt
)
1336 struct imx355
*imx355
= to_imx355(sd
);
1337 const struct imx355_mode
*mode
;
1338 struct v4l2_mbus_framefmt
*framefmt
;
1345 mutex_lock(&imx355
->mutex
);
1348 * Only one bayer order is supported.
1349 * It depends on the flip settings.
1351 fmt
->format
.code
= imx355_get_format_code(imx355
);
1353 mode
= v4l2_find_nearest_size(supported_modes
,
1354 ARRAY_SIZE(supported_modes
),
1356 fmt
->format
.width
, fmt
->format
.height
);
1357 imx355_update_pad_format(imx355
, mode
, fmt
);
1358 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1359 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
1360 *framefmt
= fmt
->format
;
1362 imx355
->cur_mode
= mode
;
1363 pixel_rate
= imx355
->link_def_freq
* 2 * 4;
1364 do_div(pixel_rate
, 10);
1365 __v4l2_ctrl_s_ctrl_int64(imx355
->pixel_rate
, pixel_rate
);
1366 /* Update limits and set FPS to default */
1367 height
= imx355
->cur_mode
->height
;
1368 vblank_def
= imx355
->cur_mode
->fll_def
- height
;
1369 vblank_min
= imx355
->cur_mode
->fll_min
- height
;
1370 height
= IMX355_FLL_MAX
- height
;
1371 __v4l2_ctrl_modify_range(imx355
->vblank
, vblank_min
, height
, 1,
1373 __v4l2_ctrl_s_ctrl(imx355
->vblank
, vblank_def
);
1374 h_blank
= mode
->llp
- imx355
->cur_mode
->width
;
1376 * Currently hblank is not changeable.
1377 * So FPS control is done only by vblank.
1379 __v4l2_ctrl_modify_range(imx355
->hblank
, h_blank
,
1380 h_blank
, 1, h_blank
);
1383 mutex_unlock(&imx355
->mutex
);
1388 /* Start streaming */
1389 static int imx355_start_streaming(struct imx355
*imx355
)
1391 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1392 const struct imx355_reg_list
*reg_list
;
1395 /* Global Setting */
1396 reg_list
= &imx355_global_setting
;
1397 ret
= imx355_write_regs(imx355
, reg_list
->regs
, reg_list
->num_of_regs
);
1399 dev_err(&client
->dev
, "failed to set global settings");
1403 /* Apply default values of current mode */
1404 reg_list
= &imx355
->cur_mode
->reg_list
;
1405 ret
= imx355_write_regs(imx355
, reg_list
->regs
, reg_list
->num_of_regs
);
1407 dev_err(&client
->dev
, "failed to set mode");
1411 /* set digital gain control to all color mode */
1412 ret
= imx355_write_reg(imx355
, IMX355_REG_DPGA_USE_GLOBAL_GAIN
, 1, 1);
1416 /* Apply customized values from user */
1417 ret
= __v4l2_ctrl_handler_setup(imx355
->sd
.ctrl_handler
);
1421 return imx355_write_reg(imx355
, IMX355_REG_MODE_SELECT
,
1422 1, IMX355_MODE_STREAMING
);
1425 /* Stop streaming */
1426 static int imx355_stop_streaming(struct imx355
*imx355
)
1428 return imx355_write_reg(imx355
, IMX355_REG_MODE_SELECT
,
1429 1, IMX355_MODE_STANDBY
);
1432 static int imx355_set_stream(struct v4l2_subdev
*sd
, int enable
)
1434 struct imx355
*imx355
= to_imx355(sd
);
1435 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1438 mutex_lock(&imx355
->mutex
);
1439 if (imx355
->streaming
== enable
) {
1440 mutex_unlock(&imx355
->mutex
);
1445 ret
= pm_runtime_get_sync(&client
->dev
);
1447 pm_runtime_put_noidle(&client
->dev
);
1452 * Apply default & customized values
1453 * and then start streaming.
1455 ret
= imx355_start_streaming(imx355
);
1459 imx355_stop_streaming(imx355
);
1460 pm_runtime_put(&client
->dev
);
1463 imx355
->streaming
= enable
;
1465 /* vflip and hflip cannot change during streaming */
1466 __v4l2_ctrl_grab(imx355
->vflip
, enable
);
1467 __v4l2_ctrl_grab(imx355
->hflip
, enable
);
1469 mutex_unlock(&imx355
->mutex
);
1474 pm_runtime_put(&client
->dev
);
1476 mutex_unlock(&imx355
->mutex
);
1481 static int __maybe_unused
imx355_suspend(struct device
*dev
)
1483 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1484 struct imx355
*imx355
= to_imx355(sd
);
1486 if (imx355
->streaming
)
1487 imx355_stop_streaming(imx355
);
1492 static int __maybe_unused
imx355_resume(struct device
*dev
)
1494 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1495 struct imx355
*imx355
= to_imx355(sd
);
1498 if (imx355
->streaming
) {
1499 ret
= imx355_start_streaming(imx355
);
1507 imx355_stop_streaming(imx355
);
1508 imx355
->streaming
= 0;
1512 /* Verify chip ID */
1513 static int imx355_identify_module(struct imx355
*imx355
)
1515 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1519 ret
= imx355_read_reg(imx355
, IMX355_REG_CHIP_ID
, 2, &val
);
1523 if (val
!= IMX355_CHIP_ID
) {
1524 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1525 IMX355_CHIP_ID
, val
);
1531 static const struct v4l2_subdev_core_ops imx355_subdev_core_ops
= {
1532 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
1533 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
1536 static const struct v4l2_subdev_video_ops imx355_video_ops
= {
1537 .s_stream
= imx355_set_stream
,
1540 static const struct v4l2_subdev_pad_ops imx355_pad_ops
= {
1541 .enum_mbus_code
= imx355_enum_mbus_code
,
1542 .get_fmt
= imx355_get_pad_format
,
1543 .set_fmt
= imx355_set_pad_format
,
1544 .enum_frame_size
= imx355_enum_frame_size
,
1547 static const struct v4l2_subdev_ops imx355_subdev_ops
= {
1548 .core
= &imx355_subdev_core_ops
,
1549 .video
= &imx355_video_ops
,
1550 .pad
= &imx355_pad_ops
,
1553 static const struct media_entity_operations imx355_subdev_entity_ops
= {
1554 .link_validate
= v4l2_subdev_link_validate
,
1557 static const struct v4l2_subdev_internal_ops imx355_internal_ops
= {
1558 .open
= imx355_open
,
1561 /* Initialize control handlers */
1562 static int imx355_init_controls(struct imx355
*imx355
)
1564 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1565 struct v4l2_ctrl_handler
*ctrl_hdlr
;
1571 const struct imx355_mode
*mode
;
1575 ctrl_hdlr
= &imx355
->ctrl_handler
;
1576 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 10);
1580 ctrl_hdlr
->lock
= &imx355
->mutex
;
1581 max
= ARRAY_SIZE(link_freq_menu_items
) - 1;
1582 imx355
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &imx355_ctrl_ops
,
1583 V4L2_CID_LINK_FREQ
, max
, 0,
1584 link_freq_menu_items
);
1585 if (imx355
->link_freq
)
1586 imx355
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1588 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1589 pixel_rate
= imx355
->link_def_freq
* 2 * 4;
1590 do_div(pixel_rate
, 10);
1591 /* By default, PIXEL_RATE is read only */
1592 imx355
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1593 V4L2_CID_PIXEL_RATE
, pixel_rate
,
1594 pixel_rate
, 1, pixel_rate
);
1596 /* Initialize vblank/hblank/exposure parameters based on current mode */
1597 mode
= imx355
->cur_mode
;
1598 vblank_def
= mode
->fll_def
- mode
->height
;
1599 vblank_min
= mode
->fll_min
- mode
->height
;
1600 imx355
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1601 V4L2_CID_VBLANK
, vblank_min
,
1602 IMX355_FLL_MAX
- mode
->height
,
1605 hblank
= mode
->llp
- mode
->width
;
1606 imx355
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1607 V4L2_CID_HBLANK
, hblank
, hblank
,
1610 imx355
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1612 /* fll >= exposure time + adjust parameter (default value is 10) */
1613 exposure_max
= mode
->fll_def
- 10;
1614 imx355
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1616 IMX355_EXPOSURE_MIN
, exposure_max
,
1617 IMX355_EXPOSURE_STEP
,
1618 IMX355_EXPOSURE_DEFAULT
);
1620 imx355
->hflip
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1621 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1622 imx355
->vflip
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1623 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1625 v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
1626 IMX355_ANA_GAIN_MIN
, IMX355_ANA_GAIN_MAX
,
1627 IMX355_ANA_GAIN_STEP
, IMX355_ANA_GAIN_DEFAULT
);
1630 v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
1631 IMX355_DGTL_GAIN_MIN
, IMX355_DGTL_GAIN_MAX
,
1632 IMX355_DGTL_GAIN_STEP
, IMX355_DGTL_GAIN_DEFAULT
);
1634 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &imx355_ctrl_ops
,
1635 V4L2_CID_TEST_PATTERN
,
1636 ARRAY_SIZE(imx355_test_pattern_menu
) - 1,
1637 0, 0, imx355_test_pattern_menu
);
1638 if (ctrl_hdlr
->error
) {
1639 ret
= ctrl_hdlr
->error
;
1640 dev_err(&client
->dev
, "control init failed: %d", ret
);
1644 imx355
->sd
.ctrl_handler
= ctrl_hdlr
;
1649 v4l2_ctrl_handler_free(ctrl_hdlr
);
1654 static struct imx355_hwcfg
*imx355_get_hwcfg(struct device
*dev
)
1656 struct imx355_hwcfg
*cfg
;
1657 struct v4l2_fwnode_endpoint bus_cfg
= {
1658 .bus_type
= V4L2_MBUS_CSI2_DPHY
1660 struct fwnode_handle
*ep
;
1661 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1668 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1672 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1676 cfg
= devm_kzalloc(dev
, sizeof(*cfg
), GFP_KERNEL
);
1680 ret
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
1683 dev_err(dev
, "can't get clock frequency");
1687 dev_dbg(dev
, "ext clk: %d", cfg
->ext_clk
);
1688 if (cfg
->ext_clk
!= IMX355_EXT_CLK
) {
1689 dev_err(dev
, "external clock %d is not supported",
1694 dev_dbg(dev
, "num of link freqs: %d", bus_cfg
.nr_of_link_frequencies
);
1695 if (!bus_cfg
.nr_of_link_frequencies
) {
1696 dev_warn(dev
, "no link frequencies defined");
1700 cfg
->nr_of_link_freqs
= bus_cfg
.nr_of_link_frequencies
;
1701 cfg
->link_freqs
= devm_kcalloc(dev
,
1702 bus_cfg
.nr_of_link_frequencies
+ 1,
1703 sizeof(*cfg
->link_freqs
), GFP_KERNEL
);
1704 if (!cfg
->link_freqs
)
1707 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++) {
1708 cfg
->link_freqs
[i
] = bus_cfg
.link_frequencies
[i
];
1709 dev_dbg(dev
, "link_freq[%d] = %lld", i
, cfg
->link_freqs
[i
]);
1712 v4l2_fwnode_endpoint_free(&bus_cfg
);
1713 fwnode_handle_put(ep
);
1717 v4l2_fwnode_endpoint_free(&bus_cfg
);
1718 fwnode_handle_put(ep
);
1722 static int imx355_probe(struct i2c_client
*client
)
1724 struct imx355
*imx355
;
1728 imx355
= devm_kzalloc(&client
->dev
, sizeof(*imx355
), GFP_KERNEL
);
1732 mutex_init(&imx355
->mutex
);
1734 /* Initialize subdev */
1735 v4l2_i2c_subdev_init(&imx355
->sd
, client
, &imx355_subdev_ops
);
1737 /* Check module identity */
1738 ret
= imx355_identify_module(imx355
);
1740 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1744 imx355
->hwcfg
= imx355_get_hwcfg(&client
->dev
);
1745 if (!imx355
->hwcfg
) {
1746 dev_err(&client
->dev
, "failed to get hwcfg");
1751 imx355
->link_def_freq
= link_freq_menu_items
[IMX355_LINK_FREQ_INDEX
];
1752 for (i
= 0; i
< imx355
->hwcfg
->nr_of_link_freqs
; i
++) {
1753 if (imx355
->hwcfg
->link_freqs
[i
] == imx355
->link_def_freq
) {
1754 dev_dbg(&client
->dev
, "link freq index %d matched", i
);
1759 if (i
== imx355
->hwcfg
->nr_of_link_freqs
) {
1760 dev_err(&client
->dev
, "no link frequency supported");
1765 /* Set default mode to max resolution */
1766 imx355
->cur_mode
= &supported_modes
[0];
1768 ret
= imx355_init_controls(imx355
);
1770 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1774 /* Initialize subdev */
1775 imx355
->sd
.internal_ops
= &imx355_internal_ops
;
1776 imx355
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
|
1777 V4L2_SUBDEV_FL_HAS_EVENTS
;
1778 imx355
->sd
.entity
.ops
= &imx355_subdev_entity_ops
;
1779 imx355
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1781 /* Initialize source pad */
1782 imx355
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1783 ret
= media_entity_pads_init(&imx355
->sd
.entity
, 1, &imx355
->pad
);
1785 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1786 goto error_handler_free
;
1789 ret
= v4l2_async_register_subdev_sensor_common(&imx355
->sd
);
1791 goto error_media_entity
;
1794 * Device is already turned on by i2c-core with ACPI domain PM.
1795 * Enable runtime PM and turn off the device.
1797 pm_runtime_set_active(&client
->dev
);
1798 pm_runtime_enable(&client
->dev
);
1799 pm_runtime_idle(&client
->dev
);
1804 media_entity_cleanup(&imx355
->sd
.entity
);
1807 v4l2_ctrl_handler_free(imx355
->sd
.ctrl_handler
);
1810 mutex_destroy(&imx355
->mutex
);
1815 static int imx355_remove(struct i2c_client
*client
)
1817 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1818 struct imx355
*imx355
= to_imx355(sd
);
1820 v4l2_async_unregister_subdev(sd
);
1821 media_entity_cleanup(&sd
->entity
);
1822 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1824 pm_runtime_disable(&client
->dev
);
1825 pm_runtime_set_suspended(&client
->dev
);
1827 mutex_destroy(&imx355
->mutex
);
1832 static const struct dev_pm_ops imx355_pm_ops
= {
1833 SET_SYSTEM_SLEEP_PM_OPS(imx355_suspend
, imx355_resume
)
1836 static const struct acpi_device_id imx355_acpi_ids
[] __maybe_unused
= {
1840 MODULE_DEVICE_TABLE(acpi
, imx355_acpi_ids
);
1842 static struct i2c_driver imx355_i2c_driver
= {
1845 .pm
= &imx355_pm_ops
,
1846 .acpi_match_table
= ACPI_PTR(imx355_acpi_ids
),
1848 .probe_new
= imx355_probe
,
1849 .remove
= imx355_remove
,
1851 module_i2c_driver(imx355_i2c_driver
);
1853 MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>");
1854 MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
1855 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
1856 MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
1857 MODULE_DESCRIPTION("Sony imx355 sensor driver");
1858 MODULE_LICENSE("GPL v2");