1 // SPDX-License-Identifier: GPL-2.0
3 * A V4L2 driver for Sony IMX477 cameras.
4 * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
6 * Based on Sony imx219 camera driver
7 * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
9 #include <asm/unaligned.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-fwnode.h>
22 #include <media/v4l2-mediabus.h>
24 static int dpc_enable = 1;
25 module_param(dpc_enable, int, 0644);
26 MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
28 static int trigger_mode;
29 module_param(trigger_mode, int, 0644);
30 MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
32 #define IMX477_REG_VALUE_08BIT 1
33 #define IMX477_REG_VALUE_16BIT 2
36 #define IMX477_REG_CHIP_ID 0x0016
37 #define IMX477_CHIP_ID 0x0477
38 #define IMX378_CHIP_ID 0x0378
40 #define IMX477_REG_MODE_SELECT 0x0100
41 #define IMX477_MODE_STANDBY 0x00
42 #define IMX477_MODE_STREAMING 0x01
44 #define IMX477_REG_ORIENTATION 0x101
46 #define IMX477_XCLK_FREQ 24000000
48 #define IMX477_DEFAULT_LINK_FREQ 450000000
50 /* Pixel rate is fixed at 840MHz for all the modes */
51 #define IMX477_PIXEL_RATE 840000000
53 /* V_TIMING internal */
54 #define IMX477_REG_FRAME_LENGTH 0x0340
55 #define IMX477_FRAME_LENGTH_MAX 0xffdc
57 /* H_TIMING internal */
58 #define IMX477_REG_LINE_LENGTH 0x0342
59 #define IMX477_LINE_LENGTH_MAX 0xfff0
61 /* Long exposure multiplier */
62 #define IMX477_LONG_EXP_SHIFT_MAX 7
63 #define IMX477_LONG_EXP_SHIFT_REG 0x3100
65 /* Exposure control */
66 #define IMX477_REG_EXPOSURE 0x0202
67 #define IMX477_EXPOSURE_OFFSET 22
68 #define IMX477_EXPOSURE_MIN 4
69 #define IMX477_EXPOSURE_STEP 1
70 #define IMX477_EXPOSURE_DEFAULT 0x640
71 #define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
72 IMX477_EXPOSURE_OFFSET)
74 /* Analog gain control */
75 #define IMX477_REG_ANALOG_GAIN 0x0204
76 #define IMX477_ANA_GAIN_MIN 0
77 #define IMX477_ANA_GAIN_MAX 978
78 #define IMX477_ANA_GAIN_STEP 1
79 #define IMX477_ANA_GAIN_DEFAULT 0x0
81 /* Digital gain control */
82 #define IMX477_REG_DIGITAL_GAIN 0x020e
83 #define IMX477_DGTL_GAIN_MIN 0x0100
84 #define IMX477_DGTL_GAIN_MAX 0xffff
85 #define IMX477_DGTL_GAIN_DEFAULT 0x0100
86 #define IMX477_DGTL_GAIN_STEP 1
88 /* Test Pattern Control */
89 #define IMX477_REG_TEST_PATTERN 0x0600
90 #define IMX477_TEST_PATTERN_DISABLE 0
91 #define IMX477_TEST_PATTERN_SOLID_COLOR 1
92 #define IMX477_TEST_PATTERN_COLOR_BARS 2
93 #define IMX477_TEST_PATTERN_GREY_COLOR 3
94 #define IMX477_TEST_PATTERN_PN9 4
96 /* Test pattern colour components */
97 #define IMX477_REG_TEST_PATTERN_R 0x0602
98 #define IMX477_REG_TEST_PATTERN_GR 0x0604
99 #define IMX477_REG_TEST_PATTERN_B 0x0606
100 #define IMX477_REG_TEST_PATTERN_GB 0x0608
101 #define IMX477_TEST_PATTERN_COLOUR_MIN 0
102 #define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
103 #define IMX477_TEST_PATTERN_COLOUR_STEP 1
104 #define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
105 #define IMX477_TEST_PATTERN_GR_DEFAULT 0
106 #define IMX477_TEST_PATTERN_B_DEFAULT 0
107 #define IMX477_TEST_PATTERN_GB_DEFAULT 0
110 #define IMX477_REG_MC_MODE 0x3f0b
111 #define IMX477_REG_MS_SEL 0x3041
112 #define IMX477_REG_XVS_IO_CTRL 0x3040
113 #define IMX477_REG_EXTOUT_EN 0x4b81
115 /* Embedded metadata stream structure */
116 #define IMX477_EMBEDDED_LINE_WIDTH 16384
117 #define IMX477_NUM_EMBEDDED_LINES 1
125 /* IMX477 native and active pixel array size. */
126 #define IMX477_NATIVE_WIDTH 4072U
127 #define IMX477_NATIVE_HEIGHT 3176U
128 #define IMX477_PIXEL_ARRAY_LEFT 8U
129 #define IMX477_PIXEL_ARRAY_TOP 16U
130 #define IMX477_PIXEL_ARRAY_WIDTH 4056U
131 #define IMX477_PIXEL_ARRAY_HEIGHT 3040U
138 struct imx477_reg_list {
139 unsigned int num_of_regs;
140 const struct imx477_reg *regs;
143 /* Mode : resolution and related config&values */
151 /* H-timing in pixels */
152 unsigned int line_length_pix;
154 /* Analog crop rectangle. */
155 struct v4l2_rect crop;
157 /* Highest possible framerate. */
158 struct v4l2_fract timeperframe_min;
160 /* Default framerate. */
161 struct v4l2_fract timeperframe_default;
163 /* Default register values */
164 struct imx477_reg_list reg_list;
167 static const struct imx477_reg mode_common_regs[] = {
480 static const struct imx477_reg mode_4056x3040_regs[] = {
591 /* 2x2 binned. 40fps */
592 static const struct imx477_reg mode_2028x1520_regs[] = {
692 /* 1080p cropped mode */
693 static const struct imx477_reg mode_2028x1080_regs[] = {
793 /* 4x4 binned. 120fps */
794 static const struct imx477_reg mode_1332x990_regs[] = {
924 static const struct imx477_mode supported_modes_12bit[] = {
926 /* 12MPix 10fps mode */
929 .line_length_pix = 0x5dc0,
931 .left = IMX477_PIXEL_ARRAY_LEFT,
932 .top = IMX477_PIXEL_ARRAY_TOP,
936 .timeperframe_min = {
940 .timeperframe_default = {
945 .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
946 .regs = mode_4056x3040_regs,
950 /* 2x2 binned 40fps mode */
953 .line_length_pix = 0x31c4,
955 .left = IMX477_PIXEL_ARRAY_LEFT,
956 .top = IMX477_PIXEL_ARRAY_TOP,
960 .timeperframe_min = {
964 .timeperframe_default = {
969 .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
970 .regs = mode_2028x1520_regs,
974 /* 1080p 50fps cropped mode */
977 .line_length_pix = 0x31c4,
979 .left = IMX477_PIXEL_ARRAY_LEFT,
980 .top = IMX477_PIXEL_ARRAY_TOP + 440,
984 .timeperframe_min = {
988 .timeperframe_default = {
993 .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
994 .regs = mode_2028x1080_regs,
999 static const struct imx477_mode supported_modes_10bit[] = {
1001 /* 120fps. 2x2 binned and cropped */
1004 .line_length_pix = 6664,
1007 * FIXME: the analog crop rectangle is actually
1008 * programmed with a horizontal displacement of 0
1009 * pixels, not 4. It gets shrunk after going through
1010 * the scaler. Move this information to the compose
1011 * rectangle once the driver is expanded to represent
1012 * its processing blocks with multiple subdevs.
1014 .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1015 .top = IMX477_PIXEL_ARRAY_TOP + 528,
1019 .timeperframe_min = {
1021 .denominator = 12000
1023 .timeperframe_default = {
1025 .denominator = 12000
1028 .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1029 .regs = mode_1332x990_regs,
1035 * The supported formats.
1036 * This table MUST contain 4 entries per format, to cover the various flip
1037 * combinations in the order
1043 static const u32 codes[] = {
1045 MEDIA_BUS_FMT_SRGGB12_1X12,
1046 MEDIA_BUS_FMT_SGRBG12_1X12,
1047 MEDIA_BUS_FMT_SGBRG12_1X12,
1048 MEDIA_BUS_FMT_SBGGR12_1X12,
1050 MEDIA_BUS_FMT_SRGGB10_1X10,
1051 MEDIA_BUS_FMT_SGRBG10_1X10,
1052 MEDIA_BUS_FMT_SGBRG10_1X10,
1053 MEDIA_BUS_FMT_SBGGR10_1X10,
1056 static const char * const imx477_test_pattern_menu[] = {
1064 static const int imx477_test_pattern_val[] = {
1065 IMX477_TEST_PATTERN_DISABLE,
1066 IMX477_TEST_PATTERN_COLOR_BARS,
1067 IMX477_TEST_PATTERN_SOLID_COLOR,
1068 IMX477_TEST_PATTERN_GREY_COLOR,
1069 IMX477_TEST_PATTERN_PN9,
1072 /* regulator supplies */
1073 static const char * const imx477_supply_name[] = {
1074 /* Supplies can be enabled in any order */
1075 "VANA", /* Analog (2.8V) supply */
1076 "VDIG", /* Digital Core (1.05V) supply */
1077 "VDDL", /* IF (1.8V) supply */
1080 #define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1083 * Initialisation delay between XCLR low->high and the moment when the sensor
1084 * can start capture (i.e. can leave software standby), given by T7 in the
1085 * datasheet is 8ms. This does include I2C setup time as well.
1087 * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1088 * in the datasheet) is much smaller - 600us.
1090 #define IMX477_XCLR_MIN_DELAY_US 8000
1091 #define IMX477_XCLR_DELAY_RANGE_US 1000
1093 struct imx477_compatible_data {
1094 unsigned int chip_id;
1095 struct imx477_reg_list extra_regs;
1099 struct v4l2_subdev sd;
1100 struct media_pad pad[NUM_PADS];
1102 unsigned int fmt_code;
1107 struct gpio_desc *reset_gpio;
1108 struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1110 struct v4l2_ctrl_handler ctrl_handler;
1112 struct v4l2_ctrl *pixel_rate;
1113 struct v4l2_ctrl *exposure;
1114 struct v4l2_ctrl *vflip;
1115 struct v4l2_ctrl *hflip;
1116 struct v4l2_ctrl *vblank;
1117 struct v4l2_ctrl *hblank;
1120 const struct imx477_mode *mode;
1123 * Mutex for serialized access:
1124 * Protect sensor module set pad format and start/stop streaming safely.
1128 /* Streaming on/off */
1131 /* Rewrite common registers on stream on? */
1132 bool common_regs_written;
1134 /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1135 unsigned int long_exp_shift;
1137 /* Any extra information related to different compatible sensors */
1138 const struct imx477_compatible_data *compatible_data;
1141 static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1143 return container_of(_sd, struct imx477, sd);
1146 static inline void get_mode_table(unsigned int code,
1147 const struct imx477_mode **mode_list,
1148 unsigned int *num_modes)
1152 case MEDIA_BUS_FMT_SRGGB12_1X12:
1153 case MEDIA_BUS_FMT_SGRBG12_1X12:
1154 case MEDIA_BUS_FMT_SGBRG12_1X12:
1155 case MEDIA_BUS_FMT_SBGGR12_1X12:
1156 *mode_list = supported_modes_12bit;
1157 *num_modes = ARRAY_SIZE(supported_modes_12bit);
1160 case MEDIA_BUS_FMT_SRGGB10_1X10:
1161 case MEDIA_BUS_FMT_SGRBG10_1X10:
1162 case MEDIA_BUS_FMT_SGBRG10_1X10:
1163 case MEDIA_BUS_FMT_SBGGR10_1X10:
1164 *mode_list = supported_modes_10bit;
1165 *num_modes = ARRAY_SIZE(supported_modes_10bit);
1173 /* Read registers up to 2 at a time */
1174 static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1176 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1177 struct i2c_msg msgs[2];
1178 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1179 u8 data_buf[4] = { 0, };
1185 /* Write register address */
1186 msgs[0].addr = client->addr;
1188 msgs[0].len = ARRAY_SIZE(addr_buf);
1189 msgs[0].buf = addr_buf;
1191 /* Read data from register */
1192 msgs[1].addr = client->addr;
1193 msgs[1].flags = I2C_M_RD;
1195 msgs[1].buf = &data_buf[4 - len];
1197 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1198 if (ret != ARRAY_SIZE(msgs))
1201 *val = get_unaligned_be32(data_buf);
1206 /* Write registers up to 2 at a time */
1207 static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1209 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1215 put_unaligned_be16(reg, buf);
1216 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1217 if (i2c_master_send(client, buf, len + 2) != len + 2)
1223 /* Write a list of registers */
1224 static int imx477_write_regs(struct imx477 *imx477,
1225 const struct imx477_reg *regs, u32 len)
1227 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1231 for (i = 0; i < len; i++) {
1232 ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1234 dev_err_ratelimited(&client->dev,
1235 "Failed to write reg 0x%4.4x. error = %d\n",
1236 regs[i].address, ret);
1245 /* Get bayer order based on flip setting. */
1246 static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1250 lockdep_assert_held(&imx477->mutex);
1252 for (i = 0; i < ARRAY_SIZE(codes); i++)
1253 if (codes[i] == code)
1256 if (i >= ARRAY_SIZE(codes))
1259 i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1260 (imx477->hflip->val ? 1 : 0);
1265 static void imx477_set_default_format(struct imx477 *imx477)
1267 /* Set default mode to max resolution */
1268 imx477->mode = &supported_modes_12bit[0];
1269 imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1272 static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1274 struct imx477 *imx477 = to_imx477(sd);
1275 struct v4l2_mbus_framefmt *try_fmt_img =
1276 v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
1277 struct v4l2_mbus_framefmt *try_fmt_meta =
1278 v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
1279 struct v4l2_rect *try_crop;
1281 mutex_lock(&imx477->mutex);
1283 /* Initialize try_fmt for the image pad */
1284 try_fmt_img->width = supported_modes_12bit[0].width;
1285 try_fmt_img->height = supported_modes_12bit[0].height;
1286 try_fmt_img->code = imx477_get_format_code(imx477,
1287 MEDIA_BUS_FMT_SRGGB12_1X12);
1288 try_fmt_img->field = V4L2_FIELD_NONE;
1290 /* Initialize try_fmt for the embedded metadata pad */
1291 try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1292 try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1293 try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1294 try_fmt_meta->field = V4L2_FIELD_NONE;
1296 /* Initialize try_crop */
1297 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
1298 try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1299 try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1300 try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1301 try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1303 mutex_unlock(&imx477->mutex);
1308 static void imx477_adjust_exposure_range(struct imx477 *imx477)
1310 int exposure_max, exposure_def;
1312 /* Honour the VBLANK limits when setting exposure. */
1313 exposure_max = imx477->mode->height + imx477->vblank->val -
1314 IMX477_EXPOSURE_OFFSET;
1315 exposure_def = min(exposure_max, imx477->exposure->val);
1316 __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1317 exposure_max, imx477->exposure->step,
1321 static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1325 imx477->long_exp_shift = 0;
1327 while (val > IMX477_FRAME_LENGTH_MAX) {
1328 imx477->long_exp_shift++;
1332 ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1333 IMX477_REG_VALUE_16BIT, val);
1337 return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1338 IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1341 static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1343 struct imx477 *imx477 =
1344 container_of(ctrl->handler, struct imx477, ctrl_handler);
1345 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1349 * The VBLANK control may change the limits of usable exposure, so check
1350 * and adjust if necessary.
1352 if (ctrl->id == V4L2_CID_VBLANK)
1353 imx477_adjust_exposure_range(imx477);
1356 * Applying V4L2 control value only happens
1357 * when power is up for streaming
1359 if (pm_runtime_get_if_in_use(&client->dev) == 0)
1363 case V4L2_CID_ANALOGUE_GAIN:
1364 ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1365 IMX477_REG_VALUE_16BIT, ctrl->val);
1367 case V4L2_CID_EXPOSURE:
1368 ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1369 IMX477_REG_VALUE_16BIT, ctrl->val >>
1370 imx477->long_exp_shift);
1372 case V4L2_CID_DIGITAL_GAIN:
1373 ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1374 IMX477_REG_VALUE_16BIT, ctrl->val);
1376 case V4L2_CID_TEST_PATTERN:
1377 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1378 IMX477_REG_VALUE_16BIT,
1379 imx477_test_pattern_val[ctrl->val]);
1381 case V4L2_CID_TEST_PATTERN_RED:
1382 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1383 IMX477_REG_VALUE_16BIT, ctrl->val);
1385 case V4L2_CID_TEST_PATTERN_GREENR:
1386 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1387 IMX477_REG_VALUE_16BIT, ctrl->val);
1389 case V4L2_CID_TEST_PATTERN_BLUE:
1390 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1391 IMX477_REG_VALUE_16BIT, ctrl->val);
1393 case V4L2_CID_TEST_PATTERN_GREENB:
1394 ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1395 IMX477_REG_VALUE_16BIT, ctrl->val);
1397 case V4L2_CID_HFLIP:
1398 case V4L2_CID_VFLIP:
1399 ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1400 imx477->hflip->val |
1401 imx477->vflip->val << 1);
1403 case V4L2_CID_VBLANK:
1404 ret = imx477_set_frame_length(imx477,
1405 imx477->mode->height + ctrl->val);
1407 case V4L2_CID_HBLANK:
1408 ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
1409 imx477->mode->width + ctrl->val);
1412 dev_info(&client->dev,
1413 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1414 ctrl->id, ctrl->val);
1419 pm_runtime_put(&client->dev);
1424 static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1425 .s_ctrl = imx477_set_ctrl,
1428 static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1429 struct v4l2_subdev_state *sd_state,
1430 struct v4l2_subdev_mbus_code_enum *code)
1432 struct imx477 *imx477 = to_imx477(sd);
1434 if (code->pad >= NUM_PADS)
1437 if (code->pad == IMAGE_PAD) {
1438 if (code->index >= (ARRAY_SIZE(codes) / 4))
1441 code->code = imx477_get_format_code(imx477,
1442 codes[code->index * 4]);
1444 if (code->index > 0)
1447 code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1453 static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1454 struct v4l2_subdev_state *sd_state,
1455 struct v4l2_subdev_frame_size_enum *fse)
1457 struct imx477 *imx477 = to_imx477(sd);
1459 if (fse->pad >= NUM_PADS)
1462 if (fse->pad == IMAGE_PAD) {
1463 const struct imx477_mode *mode_list;
1464 unsigned int num_modes;
1466 get_mode_table(fse->code, &mode_list, &num_modes);
1468 if (fse->index >= num_modes)
1471 if (fse->code != imx477_get_format_code(imx477, fse->code))
1474 fse->min_width = mode_list[fse->index].width;
1475 fse->max_width = fse->min_width;
1476 fse->min_height = mode_list[fse->index].height;
1477 fse->max_height = fse->min_height;
1479 if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1482 fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1483 fse->max_width = fse->min_width;
1484 fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1485 fse->max_height = fse->min_height;
1491 static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1493 fmt->colorspace = V4L2_COLORSPACE_RAW;
1494 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1495 fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1498 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1501 static void imx477_update_image_pad_format(struct imx477 *imx477,
1502 const struct imx477_mode *mode,
1503 struct v4l2_subdev_format *fmt)
1505 fmt->format.width = mode->width;
1506 fmt->format.height = mode->height;
1507 fmt->format.field = V4L2_FIELD_NONE;
1508 imx477_reset_colorspace(&fmt->format);
1511 static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1513 fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1514 fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1515 fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1516 fmt->format.field = V4L2_FIELD_NONE;
1519 static int imx477_get_pad_format(struct v4l2_subdev *sd,
1520 struct v4l2_subdev_state *sd_state,
1521 struct v4l2_subdev_format *fmt)
1523 struct imx477 *imx477 = to_imx477(sd);
1525 if (fmt->pad >= NUM_PADS)
1528 mutex_lock(&imx477->mutex);
1530 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1531 struct v4l2_mbus_framefmt *try_fmt =
1532 v4l2_subdev_get_try_format(&imx477->sd, sd_state,
1534 /* update the code which could change due to vflip or hflip: */
1535 try_fmt->code = fmt->pad == IMAGE_PAD ?
1536 imx477_get_format_code(imx477, try_fmt->code) :
1537 MEDIA_BUS_FMT_SENSOR_DATA;
1538 fmt->format = *try_fmt;
1540 if (fmt->pad == IMAGE_PAD) {
1541 imx477_update_image_pad_format(imx477, imx477->mode,
1544 imx477_get_format_code(imx477, imx477->fmt_code);
1546 imx477_update_metadata_pad_format(fmt);
1550 mutex_unlock(&imx477->mutex);
1555 unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
1556 const struct v4l2_fract *timeperframe)
1560 frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
1561 do_div(frame_length,
1562 (u64)timeperframe->denominator * mode->line_length_pix);
1564 if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
1565 frame_length = IMX477_FRAME_LENGTH_MAX;
1567 return max_t(unsigned int, frame_length, mode->height);
1570 static void imx477_set_framing_limits(struct imx477 *imx477)
1572 unsigned int frm_length_min, frm_length_default, hblank_min;
1573 const struct imx477_mode *mode = imx477->mode;
1575 frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
1576 frm_length_default =
1577 imx477_get_frame_length(mode, &mode->timeperframe_default);
1579 /* Default to no long exposure multiplier. */
1580 imx477->long_exp_shift = 0;
1582 /* Update limits and set FPS to default */
1583 __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
1584 ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
1585 IMX477_FRAME_LENGTH_MAX) - mode->height,
1586 1, frm_length_default - mode->height);
1588 /* Setting this will adjust the exposure limits as well. */
1589 __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
1591 hblank_min = mode->line_length_pix - mode->width;
1592 __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
1593 IMX477_LINE_LENGTH_MAX, 1, hblank_min);
1594 __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
1597 static int imx477_set_pad_format(struct v4l2_subdev *sd,
1598 struct v4l2_subdev_state *sd_state,
1599 struct v4l2_subdev_format *fmt)
1601 struct v4l2_mbus_framefmt *framefmt;
1602 const struct imx477_mode *mode;
1603 struct imx477 *imx477 = to_imx477(sd);
1605 if (fmt->pad >= NUM_PADS)
1608 mutex_lock(&imx477->mutex);
1610 if (fmt->pad == IMAGE_PAD) {
1611 const struct imx477_mode *mode_list;
1612 unsigned int num_modes;
1614 /* Bayer order varies with flips */
1615 fmt->format.code = imx477_get_format_code(imx477,
1618 get_mode_table(fmt->format.code, &mode_list, &num_modes);
1620 mode = v4l2_find_nearest_size(mode_list,
1624 fmt->format.height);
1625 imx477_update_image_pad_format(imx477, mode, fmt);
1626 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1627 framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1629 *framefmt = fmt->format;
1630 } else if (imx477->mode != mode) {
1631 imx477->mode = mode;
1632 imx477->fmt_code = fmt->format.code;
1633 imx477_set_framing_limits(imx477);
1636 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1637 framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1639 *framefmt = fmt->format;
1641 /* Only one embedded data mode is supported */
1642 imx477_update_metadata_pad_format(fmt);
1646 mutex_unlock(&imx477->mutex);
1651 static const struct v4l2_rect *
1652 __imx477_get_pad_crop(struct imx477 *imx477,
1653 struct v4l2_subdev_state *sd_state,
1654 unsigned int pad, enum v4l2_subdev_format_whence which)
1657 case V4L2_SUBDEV_FORMAT_TRY:
1658 return v4l2_subdev_get_try_crop(&imx477->sd, sd_state, pad);
1659 case V4L2_SUBDEV_FORMAT_ACTIVE:
1660 return &imx477->mode->crop;
1666 static int imx477_get_selection(struct v4l2_subdev *sd,
1667 struct v4l2_subdev_state *sd_state,
1668 struct v4l2_subdev_selection *sel)
1670 switch (sel->target) {
1671 case V4L2_SEL_TGT_CROP: {
1672 struct imx477 *imx477 = to_imx477(sd);
1674 mutex_lock(&imx477->mutex);
1675 sel->r = *__imx477_get_pad_crop(imx477, sd_state, sel->pad,
1677 mutex_unlock(&imx477->mutex);
1682 case V4L2_SEL_TGT_NATIVE_SIZE:
1685 sel->r.width = IMX477_NATIVE_WIDTH;
1686 sel->r.height = IMX477_NATIVE_HEIGHT;
1690 case V4L2_SEL_TGT_CROP_DEFAULT:
1691 case V4L2_SEL_TGT_CROP_BOUNDS:
1692 sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
1693 sel->r.top = IMX477_PIXEL_ARRAY_TOP;
1694 sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
1695 sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
1703 /* Start streaming */
1704 static int imx477_start_streaming(struct imx477 *imx477)
1706 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1707 const struct imx477_reg_list *reg_list;
1708 const struct imx477_reg_list *extra_regs;
1711 if (!imx477->common_regs_written) {
1712 ret = imx477_write_regs(imx477, mode_common_regs,
1713 ARRAY_SIZE(mode_common_regs));
1715 extra_regs = &imx477->compatible_data->extra_regs;
1716 ret = imx477_write_regs(imx477, extra_regs->regs,
1717 extra_regs->num_of_regs);
1721 dev_err(&client->dev, "%s failed to set common settings\n",
1725 imx477->common_regs_written = true;
1728 /* Apply default values of current mode */
1729 reg_list = &imx477->mode->reg_list;
1730 ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
1732 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1736 /* Set on-sensor DPC. */
1737 imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
1738 imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
1740 /* Set vsync trigger mode */
1741 if (trigger_mode != 0) {
1742 /* trigger_mode == 1 for source, 2 for sink */
1743 const u32 val = (trigger_mode == 1) ? 1 : 0;
1745 imx477_write_reg(imx477, IMX477_REG_MC_MODE,
1746 IMX477_REG_VALUE_08BIT, 1);
1747 imx477_write_reg(imx477, IMX477_REG_MS_SEL,
1748 IMX477_REG_VALUE_08BIT, val);
1749 imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
1750 IMX477_REG_VALUE_08BIT, val);
1751 imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
1752 IMX477_REG_VALUE_08BIT, val);
1755 /* Apply customized values from user */
1756 ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
1760 /* set stream on register */
1761 return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1762 IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
1765 /* Stop streaming */
1766 static void imx477_stop_streaming(struct imx477 *imx477)
1768 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1771 /* set stream off register */
1772 ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1773 IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
1775 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1778 static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
1780 struct imx477 *imx477 = to_imx477(sd);
1781 struct i2c_client *client = v4l2_get_subdevdata(sd);
1784 mutex_lock(&imx477->mutex);
1785 if (imx477->streaming == enable) {
1786 mutex_unlock(&imx477->mutex);
1791 ret = pm_runtime_get_sync(&client->dev);
1793 pm_runtime_put_noidle(&client->dev);
1798 * Apply default & customized values
1799 * and then start streaming.
1801 ret = imx477_start_streaming(imx477);
1805 imx477_stop_streaming(imx477);
1806 pm_runtime_put(&client->dev);
1809 imx477->streaming = enable;
1811 /* vflip and hflip cannot change during streaming */
1812 __v4l2_ctrl_grab(imx477->vflip, enable);
1813 __v4l2_ctrl_grab(imx477->hflip, enable);
1815 mutex_unlock(&imx477->mutex);
1820 pm_runtime_put(&client->dev);
1822 mutex_unlock(&imx477->mutex);
1827 /* Power/clock management functions */
1828 static int imx477_power_on(struct device *dev)
1830 struct i2c_client *client = to_i2c_client(dev);
1831 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1832 struct imx477 *imx477 = to_imx477(sd);
1835 ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
1838 dev_err(&client->dev, "%s: failed to enable regulators\n",
1843 ret = clk_prepare_enable(imx477->xclk);
1845 dev_err(&client->dev, "%s: failed to enable clock\n",
1850 gpiod_set_value_cansleep(imx477->reset_gpio, 1);
1851 usleep_range(IMX477_XCLR_MIN_DELAY_US,
1852 IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
1857 regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1861 static int imx477_power_off(struct device *dev)
1863 struct i2c_client *client = to_i2c_client(dev);
1864 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1865 struct imx477 *imx477 = to_imx477(sd);
1867 gpiod_set_value_cansleep(imx477->reset_gpio, 0);
1868 regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1869 clk_disable_unprepare(imx477->xclk);
1871 /* Force reprogramming of the common registers when powered up again. */
1872 imx477->common_regs_written = false;
1877 static int __maybe_unused imx477_suspend(struct device *dev)
1879 struct i2c_client *client = to_i2c_client(dev);
1880 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1881 struct imx477 *imx477 = to_imx477(sd);
1883 if (imx477->streaming)
1884 imx477_stop_streaming(imx477);
1889 static int __maybe_unused imx477_resume(struct device *dev)
1891 struct i2c_client *client = to_i2c_client(dev);
1892 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1893 struct imx477 *imx477 = to_imx477(sd);
1896 if (imx477->streaming) {
1897 ret = imx477_start_streaming(imx477);
1905 imx477_stop_streaming(imx477);
1906 imx477->streaming = 0;
1910 static int imx477_get_regulators(struct imx477 *imx477)
1912 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1915 for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
1916 imx477->supplies[i].supply = imx477_supply_name[i];
1918 return devm_regulator_bulk_get(&client->dev,
1919 IMX477_NUM_SUPPLIES,
1923 /* Verify chip ID */
1924 static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
1926 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1930 ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
1931 IMX477_REG_VALUE_16BIT, &val);
1933 dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1938 if (val != expected_id) {
1939 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1944 dev_info(&client->dev, "Device found is imx%x\n", val);
1949 static const struct v4l2_subdev_core_ops imx477_core_ops = {
1950 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1951 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1954 static const struct v4l2_subdev_video_ops imx477_video_ops = {
1955 .s_stream = imx477_set_stream,
1958 static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
1959 .enum_mbus_code = imx477_enum_mbus_code,
1960 .get_fmt = imx477_get_pad_format,
1961 .set_fmt = imx477_set_pad_format,
1962 .get_selection = imx477_get_selection,
1963 .enum_frame_size = imx477_enum_frame_size,
1966 static const struct v4l2_subdev_ops imx477_subdev_ops = {
1967 .core = &imx477_core_ops,
1968 .video = &imx477_video_ops,
1969 .pad = &imx477_pad_ops,
1972 static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
1973 .open = imx477_open,
1976 /* Initialize control handlers */
1977 static int imx477_init_controls(struct imx477 *imx477)
1979 struct v4l2_ctrl_handler *ctrl_hdlr;
1980 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1981 struct v4l2_fwnode_device_properties props;
1985 ctrl_hdlr = &imx477->ctrl_handler;
1986 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
1990 mutex_init(&imx477->mutex);
1991 ctrl_hdlr->lock = &imx477->mutex;
1993 /* By default, PIXEL_RATE is read only */
1994 imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
1995 V4L2_CID_PIXEL_RATE,
1997 IMX477_PIXEL_RATE, 1,
2001 * Create the controls here, but mode specific limits are setup
2002 * in the imx477_set_framing_limits() call below.
2004 imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2005 V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2006 imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2007 V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2009 imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2011 IMX477_EXPOSURE_MIN,
2012 IMX477_EXPOSURE_MAX,
2013 IMX477_EXPOSURE_STEP,
2014 IMX477_EXPOSURE_DEFAULT);
2016 v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2017 IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2018 IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2020 v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2021 IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2022 IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2024 imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2025 V4L2_CID_HFLIP, 0, 1, 1, 0);
2027 imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2029 imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2030 V4L2_CID_VFLIP, 0, 1, 1, 0);
2032 imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2034 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2035 V4L2_CID_TEST_PATTERN,
2036 ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2037 0, 0, imx477_test_pattern_menu);
2038 for (i = 0; i < 4; i++) {
2040 * The assumption is that
2041 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2042 * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2043 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2045 v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2046 V4L2_CID_TEST_PATTERN_RED + i,
2047 IMX477_TEST_PATTERN_COLOUR_MIN,
2048 IMX477_TEST_PATTERN_COLOUR_MAX,
2049 IMX477_TEST_PATTERN_COLOUR_STEP,
2050 IMX477_TEST_PATTERN_COLOUR_MAX);
2051 /* The "Solid color" pattern is white by default */
2054 if (ctrl_hdlr->error) {
2055 ret = ctrl_hdlr->error;
2056 dev_err(&client->dev, "%s control init failed (%d)\n",
2061 ret = v4l2_fwnode_device_parse(&client->dev, &props);
2065 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2070 imx477->sd.ctrl_handler = ctrl_hdlr;
2072 mutex_lock(&imx477->mutex);
2074 /* Setup exposure and frame/line length limits. */
2075 imx477_set_framing_limits(imx477);
2077 mutex_unlock(&imx477->mutex);
2082 v4l2_ctrl_handler_free(ctrl_hdlr);
2083 mutex_destroy(&imx477->mutex);
2088 static void imx477_free_controls(struct imx477 *imx477)
2090 v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2091 mutex_destroy(&imx477->mutex);
2094 static int imx477_check_hwcfg(struct device *dev)
2096 struct fwnode_handle *endpoint;
2097 struct v4l2_fwnode_endpoint ep_cfg = {
2098 .bus_type = V4L2_MBUS_CSI2_DPHY
2102 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2104 dev_err(dev, "endpoint node not found\n");
2108 if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2109 dev_err(dev, "could not parse endpoint\n");
2113 /* Check the number of MIPI CSI2 data lanes */
2114 if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2115 dev_err(dev, "only 2 data lanes are currently supported\n");
2119 /* Check the link frequency set in device tree */
2120 if (!ep_cfg.nr_of_link_frequencies) {
2121 dev_err(dev, "link-frequency property not found in DT\n");
2125 if (ep_cfg.nr_of_link_frequencies != 1 ||
2126 ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2127 dev_err(dev, "Link frequency not supported: %lld\n",
2128 ep_cfg.link_frequencies[0]);
2135 v4l2_fwnode_endpoint_free(&ep_cfg);
2136 fwnode_handle_put(endpoint);
2141 static const struct imx477_compatible_data imx477_compatible = {
2142 .chip_id = IMX477_CHIP_ID,
2149 static const struct imx477_reg imx378_regs[] = {
2155 static const struct imx477_compatible_data imx378_compatible = {
2156 .chip_id = IMX378_CHIP_ID,
2158 .num_of_regs = ARRAY_SIZE(imx378_regs),
2163 static const struct of_device_id imx477_dt_ids[] = {
2164 { .compatible = "sony,imx477", .data = &imx477_compatible },
2165 { .compatible = "sony,imx378", .data = &imx378_compatible },
2169 static int imx477_probe(struct i2c_client *client)
2171 struct device *dev = &client->dev;
2172 struct imx477 *imx477;
2173 const struct of_device_id *match;
2176 imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2180 v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2182 match = of_match_device(imx477_dt_ids, dev);
2185 imx477->compatible_data =
2186 (const struct imx477_compatible_data *)match->data;
2188 /* Check the hardware configuration in device tree */
2189 if (imx477_check_hwcfg(dev))
2192 /* Get system clock (xclk) */
2193 imx477->xclk = devm_clk_get(dev, NULL);
2194 if (IS_ERR(imx477->xclk)) {
2195 dev_err(dev, "failed to get xclk\n");
2196 return PTR_ERR(imx477->xclk);
2199 imx477->xclk_freq = clk_get_rate(imx477->xclk);
2200 if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2201 dev_err(dev, "xclk frequency not supported: %d Hz\n",
2206 ret = imx477_get_regulators(imx477);
2208 dev_err(dev, "failed to get regulators\n");
2212 /* Request optional enable pin */
2213 imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2217 * The sensor must be powered for imx477_identify_module()
2218 * to be able to read the CHIP_ID register
2220 ret = imx477_power_on(dev);
2224 ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2226 goto error_power_off;
2228 /* Initialize default format */
2229 imx477_set_default_format(imx477);
2231 /* Enable runtime PM and turn off the device */
2232 pm_runtime_set_active(dev);
2233 pm_runtime_enable(dev);
2234 pm_runtime_idle(dev);
2236 /* This needs the pm runtime to be registered. */
2237 ret = imx477_init_controls(imx477);
2239 goto error_power_off;
2241 /* Initialize subdev */
2242 imx477->sd.internal_ops = &imx477_internal_ops;
2243 imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2244 V4L2_SUBDEV_FL_HAS_EVENTS;
2245 imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2247 /* Initialize source pads */
2248 imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2249 imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2251 ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2253 dev_err(dev, "failed to init entity pads: %d\n", ret);
2254 goto error_handler_free;
2257 ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2259 dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2260 goto error_media_entity;
2266 media_entity_cleanup(&imx477->sd.entity);
2269 imx477_free_controls(imx477);
2272 pm_runtime_disable(&client->dev);
2273 pm_runtime_set_suspended(&client->dev);
2274 imx477_power_off(&client->dev);
2279 static void imx477_remove(struct i2c_client *client)
2281 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2282 struct imx477 *imx477 = to_imx477(sd);
2284 v4l2_async_unregister_subdev(sd);
2285 media_entity_cleanup(&sd->entity);
2286 imx477_free_controls(imx477);
2288 pm_runtime_disable(&client->dev);
2289 if (!pm_runtime_status_suspended(&client->dev))
2290 imx477_power_off(&client->dev);
2291 pm_runtime_set_suspended(&client->dev);
2294 MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2296 static const struct dev_pm_ops imx477_pm_ops = {
2297 SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2298 SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2301 static struct i2c_driver imx477_i2c_driver = {
2304 .of_match_table = imx477_dt_ids,
2305 .pm = &imx477_pm_ops,
2307 .probe = imx477_probe,
2308 .remove = imx477_remove,
2311 module_i2c_driver(imx477_i2c_driver);
2313 MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2314 MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2315 MODULE_LICENSE("GPL v2");