1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/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 360000000LL
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 unsigned long link_freq_bitmap
;
100 struct v4l2_subdev sd
;
101 struct media_pad pad
;
103 struct v4l2_ctrl_handler ctrl_handler
;
105 struct v4l2_ctrl
*link_freq
;
106 struct v4l2_ctrl
*pixel_rate
;
107 struct v4l2_ctrl
*vblank
;
108 struct v4l2_ctrl
*hblank
;
109 struct v4l2_ctrl
*exposure
;
110 struct v4l2_ctrl
*vflip
;
111 struct v4l2_ctrl
*hflip
;
114 const struct imx355_mode
*cur_mode
;
116 struct imx355_hwcfg
*hwcfg
;
119 * Mutex for serialized access:
120 * Protect sensor set pad format and start/stop streaming safely.
121 * Protect access to sensor v4l2 controls.
126 static const struct imx355_reg imx355_global_regs
[] = {
181 static const struct imx355_reg_list imx355_global_setting
= {
182 .num_of_regs
= ARRAY_SIZE(imx355_global_regs
),
183 .regs
= imx355_global_regs
,
186 static const struct imx355_reg mode_3268x2448_regs
[] = {
235 static const struct imx355_reg mode_3264x2448_regs
[] = {
284 static const struct imx355_reg mode_3280x2464_regs
[] = {
333 static const struct imx355_reg mode_1940x1096_regs
[] = {
382 static const struct imx355_reg mode_1936x1096_regs
[] = {
431 static const struct imx355_reg mode_1924x1080_regs
[] = {
480 static const struct imx355_reg mode_1920x1080_regs
[] = {
529 static const struct imx355_reg mode_1640x1232_regs
[] = {
578 static const struct imx355_reg mode_1640x922_regs
[] = {
627 static const struct imx355_reg mode_1300x736_regs
[] = {
676 static const struct imx355_reg mode_1296x736_regs
[] = {
725 static const struct imx355_reg mode_1284x720_regs
[] = {
774 static const struct imx355_reg mode_1280x720_regs
[] = {
823 static const struct imx355_reg mode_820x616_regs
[] = {
872 static const char * const imx355_test_pattern_menu
[] = {
875 "Eight Vertical Colour Bars",
876 "Colour Bars With Fade to Grey",
877 "Pseudorandom Sequence (PN9)",
881 * When adding more than the one below, make sure the disallowed ones will
882 * actually be disabled in the LINK_FREQ control.
884 static const s64 link_freq_menu_items
[] = {
885 IMX355_LINK_FREQ_DEFAULT
,
889 static const struct imx355_mode supported_modes
[] = {
896 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
898 .num_of_regs
= ARRAY_SIZE(mode_3280x2464_regs
),
899 .regs
= mode_3280x2464_regs
,
908 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
910 .num_of_regs
= ARRAY_SIZE(mode_3268x2448_regs
),
911 .regs
= mode_3268x2448_regs
,
920 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
922 .num_of_regs
= ARRAY_SIZE(mode_3264x2448_regs
),
923 .regs
= mode_3264x2448_regs
,
932 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
934 .num_of_regs
= ARRAY_SIZE(mode_1940x1096_regs
),
935 .regs
= mode_1940x1096_regs
,
944 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
946 .num_of_regs
= ARRAY_SIZE(mode_1936x1096_regs
),
947 .regs
= mode_1936x1096_regs
,
956 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
958 .num_of_regs
= ARRAY_SIZE(mode_1924x1080_regs
),
959 .regs
= mode_1924x1080_regs
,
968 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
970 .num_of_regs
= ARRAY_SIZE(mode_1920x1080_regs
),
971 .regs
= mode_1920x1080_regs
,
980 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
982 .num_of_regs
= ARRAY_SIZE(mode_1640x1232_regs
),
983 .regs
= mode_1640x1232_regs
,
992 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
994 .num_of_regs
= ARRAY_SIZE(mode_1640x922_regs
),
995 .regs
= mode_1640x922_regs
,
1004 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1006 .num_of_regs
= ARRAY_SIZE(mode_1300x736_regs
),
1007 .regs
= mode_1300x736_regs
,
1016 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1018 .num_of_regs
= ARRAY_SIZE(mode_1296x736_regs
),
1019 .regs
= mode_1296x736_regs
,
1028 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1030 .num_of_regs
= ARRAY_SIZE(mode_1284x720_regs
),
1031 .regs
= mode_1284x720_regs
,
1040 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1042 .num_of_regs
= ARRAY_SIZE(mode_1280x720_regs
),
1043 .regs
= mode_1280x720_regs
,
1052 .link_freq_index
= IMX355_LINK_FREQ_INDEX
,
1054 .num_of_regs
= ARRAY_SIZE(mode_820x616_regs
),
1055 .regs
= mode_820x616_regs
,
1060 static inline struct imx355
*to_imx355(struct v4l2_subdev
*_sd
)
1062 return container_of(_sd
, struct imx355
, sd
);
1065 /* Get bayer order based on flip setting. */
1066 static u32
imx355_get_format_code(struct imx355
*imx355
)
1069 * Only one bayer order is supported.
1070 * It depends on the flip settings.
1073 static const u32 codes
[2][2] = {
1074 { MEDIA_BUS_FMT_SRGGB10_1X10
, MEDIA_BUS_FMT_SGRBG10_1X10
, },
1075 { MEDIA_BUS_FMT_SGBRG10_1X10
, MEDIA_BUS_FMT_SBGGR10_1X10
, },
1078 lockdep_assert_held(&imx355
->mutex
);
1079 code
= codes
[imx355
->vflip
->val
][imx355
->hflip
->val
];
1084 /* Read registers up to 4 at a time */
1085 static int imx355_read_reg(struct imx355
*imx355
, u16 reg
, u32 len
, u32
*val
)
1087 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1088 struct i2c_msg msgs
[2];
1090 u8 data_buf
[4] = { 0 };
1096 put_unaligned_be16(reg
, addr_buf
);
1097 /* Write register address */
1098 msgs
[0].addr
= client
->addr
;
1100 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
1101 msgs
[0].buf
= addr_buf
;
1103 /* Read data from register */
1104 msgs
[1].addr
= client
->addr
;
1105 msgs
[1].flags
= I2C_M_RD
;
1107 msgs
[1].buf
= &data_buf
[4 - len
];
1109 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
1110 if (ret
!= ARRAY_SIZE(msgs
))
1113 *val
= get_unaligned_be32(data_buf
);
1118 /* Write registers up to 4 at a time */
1119 static int imx355_write_reg(struct imx355
*imx355
, u16 reg
, u32 len
, u32 val
)
1121 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1127 put_unaligned_be16(reg
, buf
);
1128 put_unaligned_be32(val
<< (8 * (4 - len
)), buf
+ 2);
1129 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
1135 /* Write a list of registers */
1136 static int imx355_write_regs(struct imx355
*imx355
,
1137 const struct imx355_reg
*regs
, u32 len
)
1139 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1143 for (i
= 0; i
< len
; i
++) {
1144 ret
= imx355_write_reg(imx355
, regs
[i
].address
, 1, regs
[i
].val
);
1146 dev_err_ratelimited(&client
->dev
,
1147 "write reg 0x%4.4x return err %d",
1148 regs
[i
].address
, ret
);
1157 /* Open sub-device */
1158 static int imx355_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1160 struct imx355
*imx355
= to_imx355(sd
);
1161 struct v4l2_mbus_framefmt
*try_fmt
=
1162 v4l2_subdev_state_get_format(fh
->state
, 0);
1164 mutex_lock(&imx355
->mutex
);
1166 /* Initialize try_fmt */
1167 try_fmt
->width
= imx355
->cur_mode
->width
;
1168 try_fmt
->height
= imx355
->cur_mode
->height
;
1169 try_fmt
->code
= imx355_get_format_code(imx355
);
1170 try_fmt
->field
= V4L2_FIELD_NONE
;
1172 mutex_unlock(&imx355
->mutex
);
1177 static int imx355_set_ctrl(struct v4l2_ctrl
*ctrl
)
1179 struct imx355
*imx355
= container_of(ctrl
->handler
,
1180 struct imx355
, ctrl_handler
);
1181 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1185 /* Propagate change of current control to all related controls */
1187 case V4L2_CID_VBLANK
:
1188 /* Update max exposure while meeting expected vblanking */
1189 max
= imx355
->cur_mode
->height
+ ctrl
->val
- 10;
1190 __v4l2_ctrl_modify_range(imx355
->exposure
,
1191 imx355
->exposure
->minimum
,
1192 max
, imx355
->exposure
->step
, max
);
1197 * Applying V4L2 control value only happens
1198 * when power is up for streaming
1200 if (!pm_runtime_get_if_in_use(&client
->dev
))
1204 case V4L2_CID_ANALOGUE_GAIN
:
1205 /* Analog gain = 1024/(1024 - ctrl->val) times */
1206 ret
= imx355_write_reg(imx355
, IMX355_REG_ANALOG_GAIN
, 2,
1209 case V4L2_CID_DIGITAL_GAIN
:
1210 ret
= imx355_write_reg(imx355
, IMX355_REG_DIG_GAIN_GLOBAL
, 2,
1213 case V4L2_CID_EXPOSURE
:
1214 ret
= imx355_write_reg(imx355
, IMX355_REG_EXPOSURE
, 2,
1217 case V4L2_CID_VBLANK
:
1218 /* Update FLL that meets expected vertical blanking */
1219 ret
= imx355_write_reg(imx355
, IMX355_REG_FLL
, 2,
1220 imx355
->cur_mode
->height
+ ctrl
->val
);
1222 case V4L2_CID_TEST_PATTERN
:
1223 ret
= imx355_write_reg(imx355
, IMX355_REG_TEST_PATTERN
,
1226 case V4L2_CID_HFLIP
:
1227 case V4L2_CID_VFLIP
:
1228 ret
= imx355_write_reg(imx355
, IMX355_REG_ORIENTATION
, 1,
1229 imx355
->hflip
->val
|
1230 imx355
->vflip
->val
<< 1);
1234 dev_info(&client
->dev
, "ctrl(id:0x%x,val:0x%x) is not handled",
1235 ctrl
->id
, ctrl
->val
);
1239 pm_runtime_put(&client
->dev
);
1244 static const struct v4l2_ctrl_ops imx355_ctrl_ops
= {
1245 .s_ctrl
= imx355_set_ctrl
,
1248 static int imx355_enum_mbus_code(struct v4l2_subdev
*sd
,
1249 struct v4l2_subdev_state
*sd_state
,
1250 struct v4l2_subdev_mbus_code_enum
*code
)
1252 struct imx355
*imx355
= to_imx355(sd
);
1254 if (code
->index
> 0)
1257 mutex_lock(&imx355
->mutex
);
1258 code
->code
= imx355_get_format_code(imx355
);
1259 mutex_unlock(&imx355
->mutex
);
1264 static int imx355_enum_frame_size(struct v4l2_subdev
*sd
,
1265 struct v4l2_subdev_state
*sd_state
,
1266 struct v4l2_subdev_frame_size_enum
*fse
)
1268 struct imx355
*imx355
= to_imx355(sd
);
1270 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1273 mutex_lock(&imx355
->mutex
);
1274 if (fse
->code
!= imx355_get_format_code(imx355
)) {
1275 mutex_unlock(&imx355
->mutex
);
1278 mutex_unlock(&imx355
->mutex
);
1280 fse
->min_width
= supported_modes
[fse
->index
].width
;
1281 fse
->max_width
= fse
->min_width
;
1282 fse
->min_height
= supported_modes
[fse
->index
].height
;
1283 fse
->max_height
= fse
->min_height
;
1288 static void imx355_update_pad_format(struct imx355
*imx355
,
1289 const struct imx355_mode
*mode
,
1290 struct v4l2_subdev_format
*fmt
)
1292 fmt
->format
.width
= mode
->width
;
1293 fmt
->format
.height
= mode
->height
;
1294 fmt
->format
.code
= imx355_get_format_code(imx355
);
1295 fmt
->format
.field
= V4L2_FIELD_NONE
;
1298 static int imx355_do_get_pad_format(struct imx355
*imx355
,
1299 struct v4l2_subdev_state
*sd_state
,
1300 struct v4l2_subdev_format
*fmt
)
1302 struct v4l2_mbus_framefmt
*framefmt
;
1304 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1305 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
1306 fmt
->format
= *framefmt
;
1308 imx355_update_pad_format(imx355
, imx355
->cur_mode
, fmt
);
1314 static int imx355_get_pad_format(struct v4l2_subdev
*sd
,
1315 struct v4l2_subdev_state
*sd_state
,
1316 struct v4l2_subdev_format
*fmt
)
1318 struct imx355
*imx355
= to_imx355(sd
);
1321 mutex_lock(&imx355
->mutex
);
1322 ret
= imx355_do_get_pad_format(imx355
, sd_state
, fmt
);
1323 mutex_unlock(&imx355
->mutex
);
1329 imx355_set_pad_format(struct v4l2_subdev
*sd
,
1330 struct v4l2_subdev_state
*sd_state
,
1331 struct v4l2_subdev_format
*fmt
)
1333 struct imx355
*imx355
= to_imx355(sd
);
1334 const struct imx355_mode
*mode
;
1335 struct v4l2_mbus_framefmt
*framefmt
;
1342 mutex_lock(&imx355
->mutex
);
1345 * Only one bayer order is supported.
1346 * It depends on the flip settings.
1348 fmt
->format
.code
= imx355_get_format_code(imx355
);
1350 mode
= v4l2_find_nearest_size(supported_modes
,
1351 ARRAY_SIZE(supported_modes
),
1353 fmt
->format
.width
, fmt
->format
.height
);
1354 imx355_update_pad_format(imx355
, mode
, fmt
);
1355 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1356 framefmt
= v4l2_subdev_state_get_format(sd_state
, fmt
->pad
);
1357 *framefmt
= fmt
->format
;
1359 imx355
->cur_mode
= mode
;
1360 pixel_rate
= IMX355_LINK_FREQ_DEFAULT
* 2 * 4;
1361 do_div(pixel_rate
, 10);
1362 __v4l2_ctrl_s_ctrl_int64(imx355
->pixel_rate
, pixel_rate
);
1363 /* Update limits and set FPS to default */
1364 height
= imx355
->cur_mode
->height
;
1365 vblank_def
= imx355
->cur_mode
->fll_def
- height
;
1366 vblank_min
= imx355
->cur_mode
->fll_min
- height
;
1367 height
= IMX355_FLL_MAX
- height
;
1368 __v4l2_ctrl_modify_range(imx355
->vblank
, vblank_min
, height
, 1,
1370 __v4l2_ctrl_s_ctrl(imx355
->vblank
, vblank_def
);
1371 h_blank
= mode
->llp
- imx355
->cur_mode
->width
;
1373 * Currently hblank is not changeable.
1374 * So FPS control is done only by vblank.
1376 __v4l2_ctrl_modify_range(imx355
->hblank
, h_blank
,
1377 h_blank
, 1, h_blank
);
1380 mutex_unlock(&imx355
->mutex
);
1385 /* Start streaming */
1386 static int imx355_start_streaming(struct imx355
*imx355
)
1388 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1389 const struct imx355_reg_list
*reg_list
;
1392 /* Global Setting */
1393 reg_list
= &imx355_global_setting
;
1394 ret
= imx355_write_regs(imx355
, reg_list
->regs
, reg_list
->num_of_regs
);
1396 dev_err(&client
->dev
, "failed to set global settings");
1400 /* Apply default values of current mode */
1401 reg_list
= &imx355
->cur_mode
->reg_list
;
1402 ret
= imx355_write_regs(imx355
, reg_list
->regs
, reg_list
->num_of_regs
);
1404 dev_err(&client
->dev
, "failed to set mode");
1408 /* set digital gain control to all color mode */
1409 ret
= imx355_write_reg(imx355
, IMX355_REG_DPGA_USE_GLOBAL_GAIN
, 1, 1);
1413 /* Apply customized values from user */
1414 ret
= __v4l2_ctrl_handler_setup(imx355
->sd
.ctrl_handler
);
1418 return imx355_write_reg(imx355
, IMX355_REG_MODE_SELECT
,
1419 1, IMX355_MODE_STREAMING
);
1422 /* Stop streaming */
1423 static int imx355_stop_streaming(struct imx355
*imx355
)
1425 return imx355_write_reg(imx355
, IMX355_REG_MODE_SELECT
,
1426 1, IMX355_MODE_STANDBY
);
1429 static int imx355_set_stream(struct v4l2_subdev
*sd
, int enable
)
1431 struct imx355
*imx355
= to_imx355(sd
);
1432 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1435 mutex_lock(&imx355
->mutex
);
1438 ret
= pm_runtime_resume_and_get(&client
->dev
);
1443 * Apply default & customized values
1444 * and then start streaming.
1446 ret
= imx355_start_streaming(imx355
);
1450 imx355_stop_streaming(imx355
);
1451 pm_runtime_put(&client
->dev
);
1454 /* vflip and hflip cannot change during streaming */
1455 __v4l2_ctrl_grab(imx355
->vflip
, enable
);
1456 __v4l2_ctrl_grab(imx355
->hflip
, enable
);
1458 mutex_unlock(&imx355
->mutex
);
1463 pm_runtime_put(&client
->dev
);
1465 mutex_unlock(&imx355
->mutex
);
1470 /* Verify chip ID */
1471 static int imx355_identify_module(struct imx355
*imx355
)
1473 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1477 ret
= imx355_read_reg(imx355
, IMX355_REG_CHIP_ID
, 2, &val
);
1481 if (val
!= IMX355_CHIP_ID
) {
1482 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1483 IMX355_CHIP_ID
, val
);
1489 static const struct v4l2_subdev_core_ops imx355_subdev_core_ops
= {
1490 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
1491 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
1494 static const struct v4l2_subdev_video_ops imx355_video_ops
= {
1495 .s_stream
= imx355_set_stream
,
1498 static const struct v4l2_subdev_pad_ops imx355_pad_ops
= {
1499 .enum_mbus_code
= imx355_enum_mbus_code
,
1500 .get_fmt
= imx355_get_pad_format
,
1501 .set_fmt
= imx355_set_pad_format
,
1502 .enum_frame_size
= imx355_enum_frame_size
,
1505 static const struct v4l2_subdev_ops imx355_subdev_ops
= {
1506 .core
= &imx355_subdev_core_ops
,
1507 .video
= &imx355_video_ops
,
1508 .pad
= &imx355_pad_ops
,
1511 static const struct media_entity_operations imx355_subdev_entity_ops
= {
1512 .link_validate
= v4l2_subdev_link_validate
,
1515 static const struct v4l2_subdev_internal_ops imx355_internal_ops
= {
1516 .open
= imx355_open
,
1519 /* Initialize control handlers */
1520 static int imx355_init_controls(struct imx355
*imx355
)
1522 struct i2c_client
*client
= v4l2_get_subdevdata(&imx355
->sd
);
1523 struct v4l2_fwnode_device_properties props
;
1524 struct v4l2_ctrl_handler
*ctrl_hdlr
;
1530 const struct imx355_mode
*mode
;
1534 ctrl_hdlr
= &imx355
->ctrl_handler
;
1535 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 12);
1539 ctrl_hdlr
->lock
= &imx355
->mutex
;
1540 max
= ARRAY_SIZE(link_freq_menu_items
) - 1;
1541 imx355
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &imx355_ctrl_ops
,
1542 V4L2_CID_LINK_FREQ
, max
, 0,
1543 link_freq_menu_items
);
1544 if (imx355
->link_freq
)
1545 imx355
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1547 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
1548 pixel_rate
= IMX355_LINK_FREQ_DEFAULT
* 2 * 4;
1549 do_div(pixel_rate
, 10);
1550 /* By default, PIXEL_RATE is read only */
1551 imx355
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1552 V4L2_CID_PIXEL_RATE
, pixel_rate
,
1553 pixel_rate
, 1, pixel_rate
);
1555 /* Initialize vblank/hblank/exposure parameters based on current mode */
1556 mode
= imx355
->cur_mode
;
1557 vblank_def
= mode
->fll_def
- mode
->height
;
1558 vblank_min
= mode
->fll_min
- mode
->height
;
1559 imx355
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1560 V4L2_CID_VBLANK
, vblank_min
,
1561 IMX355_FLL_MAX
- mode
->height
,
1564 hblank
= mode
->llp
- mode
->width
;
1565 imx355
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1566 V4L2_CID_HBLANK
, hblank
, hblank
,
1569 imx355
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1571 /* fll >= exposure time + adjust parameter (default value is 10) */
1572 exposure_max
= mode
->fll_def
- 10;
1573 imx355
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1575 IMX355_EXPOSURE_MIN
, exposure_max
,
1576 IMX355_EXPOSURE_STEP
,
1577 IMX355_EXPOSURE_DEFAULT
);
1579 imx355
->hflip
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1580 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1582 imx355
->hflip
->flags
|= V4L2_CTRL_FLAG_MODIFY_LAYOUT
;
1583 imx355
->vflip
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
,
1584 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1586 imx355
->vflip
->flags
|= V4L2_CTRL_FLAG_MODIFY_LAYOUT
;
1588 v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
1589 IMX355_ANA_GAIN_MIN
, IMX355_ANA_GAIN_MAX
,
1590 IMX355_ANA_GAIN_STEP
, IMX355_ANA_GAIN_DEFAULT
);
1593 v4l2_ctrl_new_std(ctrl_hdlr
, &imx355_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
1594 IMX355_DGTL_GAIN_MIN
, IMX355_DGTL_GAIN_MAX
,
1595 IMX355_DGTL_GAIN_STEP
, IMX355_DGTL_GAIN_DEFAULT
);
1597 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &imx355_ctrl_ops
,
1598 V4L2_CID_TEST_PATTERN
,
1599 ARRAY_SIZE(imx355_test_pattern_menu
) - 1,
1600 0, 0, imx355_test_pattern_menu
);
1601 if (ctrl_hdlr
->error
) {
1602 ret
= ctrl_hdlr
->error
;
1603 dev_err(&client
->dev
, "control init failed: %d", ret
);
1607 ret
= v4l2_fwnode_device_parse(&client
->dev
, &props
);
1611 ret
= v4l2_ctrl_new_fwnode_properties(ctrl_hdlr
, &imx355_ctrl_ops
,
1616 imx355
->sd
.ctrl_handler
= ctrl_hdlr
;
1621 v4l2_ctrl_handler_free(ctrl_hdlr
);
1626 static struct imx355_hwcfg
*imx355_get_hwcfg(struct device
*dev
)
1628 struct imx355_hwcfg
*cfg
;
1629 struct v4l2_fwnode_endpoint bus_cfg
= {
1630 .bus_type
= V4L2_MBUS_CSI2_DPHY
1632 struct fwnode_handle
*ep
;
1633 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1639 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1643 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1647 cfg
= devm_kzalloc(dev
, sizeof(*cfg
), GFP_KERNEL
);
1651 ret
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
1654 dev_err(dev
, "can't get clock frequency");
1658 dev_dbg(dev
, "ext clk: %d", cfg
->ext_clk
);
1659 if (cfg
->ext_clk
!= IMX355_EXT_CLK
) {
1660 dev_err(dev
, "external clock %d is not supported",
1665 ret
= v4l2_link_freq_to_bitmap(dev
, bus_cfg
.link_frequencies
,
1666 bus_cfg
.nr_of_link_frequencies
,
1667 link_freq_menu_items
,
1668 ARRAY_SIZE(link_freq_menu_items
),
1669 &cfg
->link_freq_bitmap
);
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
;
1688 imx355
= devm_kzalloc(&client
->dev
, sizeof(*imx355
), GFP_KERNEL
);
1692 mutex_init(&imx355
->mutex
);
1694 /* Initialize subdev */
1695 v4l2_i2c_subdev_init(&imx355
->sd
, client
, &imx355_subdev_ops
);
1697 /* Check module identity */
1698 ret
= imx355_identify_module(imx355
);
1700 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1704 imx355
->hwcfg
= imx355_get_hwcfg(&client
->dev
);
1705 if (!imx355
->hwcfg
) {
1706 dev_err(&client
->dev
, "failed to get hwcfg");
1711 /* Set default mode to max resolution */
1712 imx355
->cur_mode
= &supported_modes
[0];
1714 ret
= imx355_init_controls(imx355
);
1716 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1720 /* Initialize subdev */
1721 imx355
->sd
.internal_ops
= &imx355_internal_ops
;
1722 imx355
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
|
1723 V4L2_SUBDEV_FL_HAS_EVENTS
;
1724 imx355
->sd
.entity
.ops
= &imx355_subdev_entity_ops
;
1725 imx355
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1727 /* Initialize source pad */
1728 imx355
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1729 ret
= media_entity_pads_init(&imx355
->sd
.entity
, 1, &imx355
->pad
);
1731 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1732 goto error_handler_free
;
1736 * Device is already turned on by i2c-core with ACPI domain PM.
1737 * Enable runtime PM and turn off the device.
1739 pm_runtime_set_active(&client
->dev
);
1740 pm_runtime_enable(&client
->dev
);
1741 pm_runtime_idle(&client
->dev
);
1743 ret
= v4l2_async_register_subdev_sensor(&imx355
->sd
);
1745 goto error_media_entity_runtime_pm
;
1749 error_media_entity_runtime_pm
:
1750 pm_runtime_disable(&client
->dev
);
1751 pm_runtime_set_suspended(&client
->dev
);
1752 media_entity_cleanup(&imx355
->sd
.entity
);
1755 v4l2_ctrl_handler_free(imx355
->sd
.ctrl_handler
);
1758 mutex_destroy(&imx355
->mutex
);
1763 static void imx355_remove(struct i2c_client
*client
)
1765 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1766 struct imx355
*imx355
= to_imx355(sd
);
1768 v4l2_async_unregister_subdev(sd
);
1769 media_entity_cleanup(&sd
->entity
);
1770 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1772 pm_runtime_disable(&client
->dev
);
1773 pm_runtime_set_suspended(&client
->dev
);
1775 mutex_destroy(&imx355
->mutex
);
1778 static const struct acpi_device_id imx355_acpi_ids
[] __maybe_unused
= {
1782 MODULE_DEVICE_TABLE(acpi
, imx355_acpi_ids
);
1784 static struct i2c_driver imx355_i2c_driver
= {
1787 .acpi_match_table
= ACPI_PTR(imx355_acpi_ids
),
1789 .probe
= imx355_probe
,
1790 .remove
= imx355_remove
,
1792 module_i2c_driver(imx355_i2c_driver
);
1794 MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>");
1795 MODULE_AUTHOR("Rapolu, Chiranjeevi");
1796 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
1797 MODULE_AUTHOR("Yang, Hyungwoo");
1798 MODULE_DESCRIPTION("Sony imx355 sensor driver");
1799 MODULE_LICENSE("GPL v2");