1 From 93582ca03e35b453e6da5c50a80339f5d00c3f05 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Fri, 8 May 2020 10:00:12 +0100
4 Subject: [PATCH] media: i2c: Add driver for Sony IMX477 sensor
6 Adds a driver for the 12MPix Sony IMX477 CSI2 sensor.
7 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
8 currently only supports 2 lanes.
10 The following Bayer modes are currently available:
12 4056x3040 12-bit @ 10fps
13 2028x1520 12-bit (binned) @ 40fps
14 2028x1050 12-bit (cropped/binned) @ 50fps
15 1012x760 10-bit (scaled) @ 120 fps
17 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
20 drivers/media/i2c/Kconfig | 11 +
21 drivers/media/i2c/Makefile | 1 +
22 drivers/media/i2c/imx477.c | 2191 ++++++++++++++++++++++++++++++++++++
23 4 files changed, 2211 insertions(+)
24 create mode 100644 drivers/media/i2c/imx477.c
28 @@ -16347,6 +16347,14 @@ S: Maintained
29 T: git git://linuxtv.org/media_tree.git
30 F: drivers/media/i2c/imx355.c
32 +SONY IMX477 SENSOR DRIVER
33 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
34 +L: linux-media@vger.kernel.org
36 +T: git git://linuxtv.org/media_tree.git
37 +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
38 +F: drivers/media/i2c/imx477.c
40 SONY MEMORYSTICK SUBSYSTEM
41 M: Maxim Levitsky <maximlevitsky@gmail.com>
42 M: Alex Dubov <oakad@yahoo.com>
43 --- a/drivers/media/i2c/Kconfig
44 +++ b/drivers/media/i2c/Kconfig
45 @@ -801,6 +801,17 @@ config VIDEO_IMX290
46 To compile this driver as a module, choose M here: the
47 module will be called imx290.
50 + tristate "Sony IMX477 sensor support"
51 + depends on I2C && VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API
52 + depends on MEDIA_CAMERA_SUPPORT
54 + This is a Video4Linux2 sensor driver for the Sony
57 + To compile this driver as a module, choose M here: the
58 + module will be called imx477.
61 tristate "Sony IMX319 sensor support"
62 depends on I2C && VIDEO_V4L2
63 --- a/drivers/media/i2c/Makefile
64 +++ b/drivers/media/i2c/Makefile
65 @@ -119,6 +119,7 @@ obj-$(CONFIG_VIDEO_IMX219) += imx219.o
66 obj-$(CONFIG_VIDEO_IMX258) += imx258.o
67 obj-$(CONFIG_VIDEO_IMX274) += imx274.o
68 obj-$(CONFIG_VIDEO_IMX290) += imx290.o
69 +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
70 obj-$(CONFIG_VIDEO_IMX319) += imx319.o
71 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
72 obj-$(CONFIG_VIDEO_MAX9286) += max9286.o
74 +++ b/drivers/media/i2c/imx477.c
76 +// SPDX-License-Identifier: GPL-2.0
78 + * A V4L2 driver for Sony IMX477 cameras.
79 + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
81 + * Based on Sony imx219 camera driver
82 + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
84 +#include <asm/unaligned.h>
85 +#include <linux/clk.h>
86 +#include <linux/delay.h>
87 +#include <linux/gpio/consumer.h>
88 +#include <linux/i2c.h>
89 +#include <linux/module.h>
90 +#include <linux/pm_runtime.h>
91 +#include <linux/regulator/consumer.h>
92 +#include <media/v4l2-ctrls.h>
93 +#include <media/v4l2-device.h>
94 +#include <media/v4l2-event.h>
95 +#include <media/v4l2-fwnode.h>
96 +#include <media/v4l2-mediabus.h>
98 +#define IMX477_REG_VALUE_08BIT 1
99 +#define IMX477_REG_VALUE_16BIT 2
102 +#define IMX477_REG_CHIP_ID 0x0016
103 +#define IMX477_CHIP_ID 0x0477
105 +#define IMX477_REG_MODE_SELECT 0x0100
106 +#define IMX477_MODE_STANDBY 0x00
107 +#define IMX477_MODE_STREAMING 0x01
109 +#define IMX477_REG_ORIENTATION 0x101
111 +#define IMX477_XCLK_FREQ 24000000
113 +#define IMX477_DEFAULT_LINK_FREQ 450000000
115 +/* Pixel rate is fixed at 840MHz for all the modes */
116 +#define IMX477_PIXEL_RATE 840000000
118 +/* V_TIMING internal */
119 +#define IMX477_REG_FRAME_LENGTH 0x0340
120 +#define IMX477_FRAME_LENGTH_MAX 0xffdc
122 +/* Exposure control */
123 +#define IMX477_REG_EXPOSURE 0x0202
124 +#define IMX477_EXPOSURE_OFFSET 22
125 +#define IMX477_EXPOSURE_MIN 20
126 +#define IMX477_EXPOSURE_STEP 1
127 +#define IMX477_EXPOSURE_DEFAULT 0x640
128 +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
129 + IMX477_EXPOSURE_OFFSET)
131 +/* Analog gain control */
132 +#define IMX477_REG_ANALOG_GAIN 0x0204
133 +#define IMX477_ANA_GAIN_MIN 0
134 +#define IMX477_ANA_GAIN_MAX 978
135 +#define IMX477_ANA_GAIN_STEP 1
136 +#define IMX477_ANA_GAIN_DEFAULT 0x0
138 +/* Digital gain control */
139 +#define IMX477_REG_DIGITAL_GAIN 0x020e
140 +#define IMX477_DGTL_GAIN_MIN 0x0100
141 +#define IMX477_DGTL_GAIN_MAX 0xffff
142 +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
143 +#define IMX477_DGTL_GAIN_STEP 1
145 +/* Test Pattern Control */
146 +#define IMX477_REG_TEST_PATTERN 0x0600
147 +#define IMX477_TEST_PATTERN_DISABLE 0
148 +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
149 +#define IMX477_TEST_PATTERN_COLOR_BARS 2
150 +#define IMX477_TEST_PATTERN_GREY_COLOR 3
151 +#define IMX477_TEST_PATTERN_PN9 4
153 +/* Test pattern colour components */
154 +#define IMX477_REG_TEST_PATTERN_R 0x0602
155 +#define IMX477_REG_TEST_PATTERN_GR 0x0604
156 +#define IMX477_REG_TEST_PATTERN_B 0x0606
157 +#define IMX477_REG_TEST_PATTERN_GB 0x0608
158 +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
159 +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
160 +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
161 +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
162 +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
163 +#define IMX477_TEST_PATTERN_B_DEFAULT 0
164 +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
166 +/* Embedded metadata stream structure */
167 +#define IMX477_EMBEDDED_LINE_WIDTH 16384
168 +#define IMX477_NUM_EMBEDDED_LINES 1
176 +/* IMX477 native and active pixel array size. */
177 +#define IMX477_NATIVE_WIDTH 4072U
178 +#define IMX477_NATIVE_HEIGHT 3176U
179 +#define IMX477_PIXEL_ARRAY_LEFT 8U
180 +#define IMX477_PIXEL_ARRAY_TOP 16U
181 +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
182 +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
189 +struct imx477_reg_list {
190 + unsigned int num_of_regs;
191 + const struct imx477_reg *regs;
194 +/* Mode : resolution and related config&values */
195 +struct imx477_mode {
197 + unsigned int width;
200 + unsigned int height;
202 + /* H-timing in pixels */
203 + unsigned int line_length_pix;
205 + /* Analog crop rectangle. */
206 + struct v4l2_rect crop;
208 + /* Highest possible framerate. */
209 + struct v4l2_fract timeperframe_min;
211 + /* Default framerate. */
212 + struct v4l2_fract timeperframe_default;
214 + /* Default register values */
215 + struct imx477_reg_list reg_list;
218 +static const struct imx477_reg mode_common_regs[] = {
530 +static const struct imx477_reg mode_4056x3040_regs[] = {
641 +/* 2x2 binned. 40fps */
642 +static const struct imx477_reg mode_2028x1520_regs[] = {
742 +/* 1080p cropped mode */
743 +static const struct imx477_reg mode_2028x1080_regs[] = {
843 +/* 4x4 binned. 120fps */
844 +static const struct imx477_reg mode_1012x760_regs[] = {
971 +static const struct imx477_mode supported_modes_12bit[] = {
973 + /* 12MPix 10fps mode */
976 + .line_length_pix = 0x5dc0,
983 + .timeperframe_min = {
985 + .denominator = 1000
987 + .timeperframe_default = {
989 + .denominator = 1000
992 + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
993 + .regs = mode_4056x3040_regs,
997 + /* 2x2 binned 40fps mode */
1000 + .line_length_pix = 0x31c4,
1007 + .timeperframe_min = {
1009 + .denominator = 4000
1011 + .timeperframe_default = {
1013 + .denominator = 3000
1016 + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
1017 + .regs = mode_2028x1520_regs,
1021 + /* 1080p 50fps cropped mode */
1024 + .line_length_pix = 0x31c4,
1031 + .timeperframe_min = {
1033 + .denominator = 5000
1035 + .timeperframe_default = {
1037 + .denominator = 3000
1040 + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
1041 + .regs = mode_2028x1080_regs,
1046 +static const struct imx477_mode supported_modes_10bit[] = {
1048 + /* 720P 120fps. 4x4 binned */
1051 + .line_length_pix = 0x1460,
1054 + * FIXME: the analog crop rectangle is actually
1055 + * programmed with a horizontal displacement of 0
1056 + * pixels, not 4. It gets shrunk after going through
1057 + * the scaler. Move this information to the compose
1058 + * rectangle once the driver is expanded to represent
1059 + * its processing blocks with multiple subdevs.
1066 + .timeperframe_min = {
1068 + .denominator = 12000
1070 + .timeperframe_default = {
1072 + .denominator = 60000
1075 + .num_of_regs = ARRAY_SIZE(mode_1012x760_regs),
1076 + .regs = mode_1012x760_regs,
1082 + * The supported formats.
1083 + * This table MUST contain 4 entries per format, to cover the various flip
1084 + * combinations in the order
1090 +static const u32 codes[] = {
1091 + /* 12-bit modes. */
1092 + MEDIA_BUS_FMT_SRGGB12_1X12,
1093 + MEDIA_BUS_FMT_SGRBG12_1X12,
1094 + MEDIA_BUS_FMT_SGBRG12_1X12,
1095 + MEDIA_BUS_FMT_SBGGR12_1X12,
1096 + /* 10-bit modes. */
1097 + MEDIA_BUS_FMT_SRGGB10_1X10,
1098 + MEDIA_BUS_FMT_SGRBG10_1X10,
1099 + MEDIA_BUS_FMT_SGBRG10_1X10,
1100 + MEDIA_BUS_FMT_SBGGR10_1X10,
1103 +static const char * const imx477_test_pattern_menu[] = {
1107 + "Grey Color Bars",
1111 +static const int imx477_test_pattern_val[] = {
1112 + IMX477_TEST_PATTERN_DISABLE,
1113 + IMX477_TEST_PATTERN_COLOR_BARS,
1114 + IMX477_TEST_PATTERN_SOLID_COLOR,
1115 + IMX477_TEST_PATTERN_GREY_COLOR,
1116 + IMX477_TEST_PATTERN_PN9,
1119 +/* regulator supplies */
1120 +static const char * const imx477_supply_name[] = {
1121 + /* Supplies can be enabled in any order */
1122 + "VANA", /* Analog (2.8V) supply */
1123 + "VDIG", /* Digital Core (1.05V) supply */
1124 + "VDDL", /* IF (1.8V) supply */
1127 +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1130 + * Initialisation delay between XCLR low->high and the moment when the sensor
1131 + * can start capture (i.e. can leave software standby), given by T7 in the
1132 + * datasheet is 8ms. This does include I2C setup time as well.
1134 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1135 + * in the datasheet) is much smaller - 600us.
1137 +#define IMX477_XCLR_MIN_DELAY_US 8000
1138 +#define IMX477_XCLR_DELAY_RANGE_US 1000
1141 + struct v4l2_subdev sd;
1142 + struct media_pad pad[NUM_PADS];
1144 + struct v4l2_mbus_framefmt fmt;
1149 + struct gpio_desc *reset_gpio;
1150 + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1152 + struct v4l2_ctrl_handler ctrl_handler;
1153 + /* V4L2 Controls */
1154 + struct v4l2_ctrl *pixel_rate;
1155 + struct v4l2_ctrl *exposure;
1156 + struct v4l2_ctrl *vflip;
1157 + struct v4l2_ctrl *hflip;
1158 + struct v4l2_ctrl *vblank;
1159 + struct v4l2_ctrl *hblank;
1161 + /* Current mode */
1162 + const struct imx477_mode *mode;
1165 + * Mutex for serialized access:
1166 + * Protect sensor module set pad format and start/stop streaming safely.
1168 + struct mutex mutex;
1170 + /* Streaming on/off */
1173 + /* Rewrite common registers on stream on? */
1174 + bool common_regs_written;
1177 +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1179 + return container_of(_sd, struct imx477, sd);
1182 +static inline void get_mode_table(unsigned int code,
1183 + const struct imx477_mode **mode_list,
1184 + unsigned int *num_modes)
1188 + case MEDIA_BUS_FMT_SRGGB12_1X12:
1189 + case MEDIA_BUS_FMT_SGRBG12_1X12:
1190 + case MEDIA_BUS_FMT_SGBRG12_1X12:
1191 + case MEDIA_BUS_FMT_SBGGR12_1X12:
1192 + *mode_list = supported_modes_12bit;
1193 + *num_modes = ARRAY_SIZE(supported_modes_12bit);
1196 + case MEDIA_BUS_FMT_SRGGB10_1X10:
1197 + case MEDIA_BUS_FMT_SGRBG10_1X10:
1198 + case MEDIA_BUS_FMT_SGBRG10_1X10:
1199 + case MEDIA_BUS_FMT_SBGGR10_1X10:
1200 + *mode_list = supported_modes_10bit;
1201 + *num_modes = ARRAY_SIZE(supported_modes_10bit);
1204 + *mode_list = NULL;
1209 +/* Read registers up to 2 at a time */
1210 +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1212 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1213 + struct i2c_msg msgs[2];
1214 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1215 + u8 data_buf[4] = { 0, };
1221 + /* Write register address */
1222 + msgs[0].addr = client->addr;
1223 + msgs[0].flags = 0;
1224 + msgs[0].len = ARRAY_SIZE(addr_buf);
1225 + msgs[0].buf = addr_buf;
1227 + /* Read data from register */
1228 + msgs[1].addr = client->addr;
1229 + msgs[1].flags = I2C_M_RD;
1230 + msgs[1].len = len;
1231 + msgs[1].buf = &data_buf[4 - len];
1233 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1234 + if (ret != ARRAY_SIZE(msgs))
1237 + *val = get_unaligned_be32(data_buf);
1242 +/* Write registers up to 2 at a time */
1243 +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1245 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1251 + put_unaligned_be16(reg, buf);
1252 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1253 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1259 +/* Write a list of registers */
1260 +static int imx477_write_regs(struct imx477 *imx477,
1261 + const struct imx477_reg *regs, u32 len)
1263 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1267 + for (i = 0; i < len; i++) {
1268 + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1270 + dev_err_ratelimited(&client->dev,
1271 + "Failed to write reg 0x%4.4x. error = %d\n",
1272 + regs[i].address, ret);
1281 +/* Get bayer order based on flip setting. */
1282 +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1286 + lockdep_assert_held(&imx477->mutex);
1288 + for (i = 0; i < ARRAY_SIZE(codes); i++)
1289 + if (codes[i] == code)
1292 + if (i >= ARRAY_SIZE(codes))
1295 + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1296 + (imx477->hflip->val ? 1 : 0);
1301 +static void imx477_set_default_format(struct imx477 *imx477)
1303 + struct v4l2_mbus_framefmt *fmt = &imx477->fmt;
1305 + /* Set default mode to max resolution */
1306 + imx477->mode = &supported_modes_12bit[0];
1308 + fmt->code = MEDIA_BUS_FMT_SRGGB12_1X12;
1309 + fmt->colorspace = V4L2_COLORSPACE_SRGB;
1310 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1311 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1314 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1315 + fmt->width = imx477->mode->width;
1316 + fmt->height = imx477->mode->height;
1317 + fmt->field = V4L2_FIELD_NONE;
1320 +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1322 + struct imx477 *imx477 = to_imx477(sd);
1323 + struct v4l2_mbus_framefmt *try_fmt_img =
1324 + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
1325 + struct v4l2_mbus_framefmt *try_fmt_meta =
1326 + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
1327 + struct v4l2_rect *try_crop;
1329 + mutex_lock(&imx477->mutex);
1331 + /* Initialize try_fmt for the image pad */
1332 + try_fmt_img->width = supported_modes_12bit[0].width;
1333 + try_fmt_img->height = supported_modes_12bit[0].height;
1334 + try_fmt_img->code = imx477_get_format_code(imx477,
1335 + MEDIA_BUS_FMT_SRGGB12_1X12);
1336 + try_fmt_img->field = V4L2_FIELD_NONE;
1338 + /* Initialize try_fmt for the embedded metadata pad */
1339 + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1340 + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1341 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1342 + try_fmt_meta->field = V4L2_FIELD_NONE;
1344 + /* Initialize try_crop */
1345 + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
1346 + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1347 + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1348 + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1349 + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1351 + mutex_unlock(&imx477->mutex);
1356 +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1358 + struct imx477 *imx477 =
1359 + container_of(ctrl->handler, struct imx477, ctrl_handler);
1360 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1363 + if (ctrl->id == V4L2_CID_VBLANK) {
1364 + int exposure_max, exposure_def;
1366 + /* Update max exposure while meeting expected vblanking */
1367 + exposure_max = imx477->mode->height + ctrl->val -
1368 + IMX477_EXPOSURE_OFFSET;
1369 + exposure_def = min(exposure_max, imx477->exposure->val);
1370 + __v4l2_ctrl_modify_range(imx477->exposure,
1371 + imx477->exposure->minimum,
1372 + exposure_max, imx477->exposure->step,
1377 + * Applying V4L2 control value only happens
1378 + * when power is up for streaming
1380 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1383 + switch (ctrl->id) {
1384 + case V4L2_CID_ANALOGUE_GAIN:
1385 + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1386 + IMX477_REG_VALUE_16BIT, ctrl->val);
1388 + case V4L2_CID_EXPOSURE:
1389 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1390 + IMX477_REG_VALUE_16BIT, ctrl->val);
1392 + case V4L2_CID_DIGITAL_GAIN:
1393 + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1394 + IMX477_REG_VALUE_16BIT, ctrl->val);
1396 + case V4L2_CID_TEST_PATTERN:
1397 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1398 + IMX477_REG_VALUE_16BIT,
1399 + imx477_test_pattern_val[ctrl->val]);
1401 + case V4L2_CID_TEST_PATTERN_RED:
1402 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1403 + IMX477_REG_VALUE_16BIT, ctrl->val);
1405 + case V4L2_CID_TEST_PATTERN_GREENR:
1406 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1407 + IMX477_REG_VALUE_16BIT, ctrl->val);
1409 + case V4L2_CID_TEST_PATTERN_BLUE:
1410 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1411 + IMX477_REG_VALUE_16BIT, ctrl->val);
1413 + case V4L2_CID_TEST_PATTERN_GREENB:
1414 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1415 + IMX477_REG_VALUE_16BIT, ctrl->val);
1417 + case V4L2_CID_HFLIP:
1418 + case V4L2_CID_VFLIP:
1419 + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1420 + imx477->hflip->val |
1421 + imx477->vflip->val << 1);
1423 + case V4L2_CID_VBLANK:
1424 + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1425 + IMX477_REG_VALUE_16BIT,
1426 + imx477->mode->height + ctrl->val);
1429 + dev_info(&client->dev,
1430 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1431 + ctrl->id, ctrl->val);
1436 + pm_runtime_put(&client->dev);
1441 +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1442 + .s_ctrl = imx477_set_ctrl,
1445 +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1446 + struct v4l2_subdev_pad_config *cfg,
1447 + struct v4l2_subdev_mbus_code_enum *code)
1449 + struct imx477 *imx477 = to_imx477(sd);
1451 + if (code->pad >= NUM_PADS)
1454 + if (code->pad == IMAGE_PAD) {
1455 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1458 + code->code = imx477_get_format_code(imx477,
1459 + codes[code->index * 4]);
1461 + if (code->index > 0)
1464 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1470 +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1471 + struct v4l2_subdev_pad_config *cfg,
1472 + struct v4l2_subdev_frame_size_enum *fse)
1474 + struct imx477 *imx477 = to_imx477(sd);
1476 + if (fse->pad >= NUM_PADS)
1479 + if (fse->pad == IMAGE_PAD) {
1480 + const struct imx477_mode *mode_list;
1481 + unsigned int num_modes;
1483 + get_mode_table(fse->code, &mode_list, &num_modes);
1485 + if (fse->index >= num_modes)
1488 + if (fse->code != imx477_get_format_code(imx477, fse->code))
1491 + fse->min_width = mode_list[fse->index].width;
1492 + fse->max_width = fse->min_width;
1493 + fse->min_height = mode_list[fse->index].height;
1494 + fse->max_height = fse->min_height;
1496 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1499 + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1500 + fse->max_width = fse->min_width;
1501 + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1502 + fse->max_height = fse->min_height;
1508 +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1510 + fmt->colorspace = V4L2_COLORSPACE_SRGB;
1511 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1512 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1515 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1518 +static void imx477_update_image_pad_format(struct imx477 *imx477,
1519 + const struct imx477_mode *mode,
1520 + struct v4l2_subdev_format *fmt)
1522 + fmt->format.width = mode->width;
1523 + fmt->format.height = mode->height;
1524 + fmt->format.field = V4L2_FIELD_NONE;
1525 + imx477_reset_colorspace(&fmt->format);
1528 +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1530 + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1531 + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1532 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1533 + fmt->format.field = V4L2_FIELD_NONE;
1536 +static int imx477_get_pad_format(struct v4l2_subdev *sd,
1537 + struct v4l2_subdev_pad_config *cfg,
1538 + struct v4l2_subdev_format *fmt)
1540 + struct imx477 *imx477 = to_imx477(sd);
1542 + if (fmt->pad >= NUM_PADS)
1545 + mutex_lock(&imx477->mutex);
1547 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1548 + struct v4l2_mbus_framefmt *try_fmt =
1549 + v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
1550 + /* update the code which could change due to vflip or hflip: */
1551 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1552 + imx477_get_format_code(imx477, try_fmt->code) :
1553 + MEDIA_BUS_FMT_SENSOR_DATA;
1554 + fmt->format = *try_fmt;
1556 + if (fmt->pad == IMAGE_PAD) {
1557 + imx477_update_image_pad_format(imx477, imx477->mode,
1559 + fmt->format.code =
1560 + imx477_get_format_code(imx477, imx477->fmt.code);
1562 + imx477_update_metadata_pad_format(fmt);
1566 + mutex_unlock(&imx477->mutex);
1571 +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
1572 + const struct v4l2_fract *timeperframe)
1576 + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
1577 + do_div(frame_length,
1578 + (u64)timeperframe->denominator * mode->line_length_pix);
1580 + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
1581 + frame_length = IMX477_FRAME_LENGTH_MAX;
1583 + return max_t(unsigned int, frame_length, mode->height);
1586 +static void imx477_set_framing_limits(struct imx477 *imx477)
1588 + const struct imx477_mode *mode = imx477->mode;
1589 + unsigned int frm_length_min, frm_length_default;
1590 + unsigned int exposure_max, exposure_def, hblank;
1592 + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
1593 + frm_length_default =
1594 + imx477_get_frame_length(mode, &mode->timeperframe_default);
1596 + /* Update limits and set FPS to default */
1597 + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
1598 + IMX477_FRAME_LENGTH_MAX - mode->height,
1599 + 1, frm_length_default - mode->height);
1600 + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
1602 + /* Update max exposure while meeting expected vblanking */
1603 + exposure_max = IMX477_FRAME_LENGTH_MAX - IMX477_EXPOSURE_OFFSET;
1604 + exposure_def = frm_length_default - mode->height -
1605 + IMX477_EXPOSURE_OFFSET;
1606 + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1607 + exposure_max, imx477->exposure->step,
1610 + * Currently PPL is fixed to the mode specified value, so hblank
1611 + * depends on mode->width only, and is not changeable in any
1612 + * way other than changing the mode.
1614 + hblank = mode->line_length_pix - mode->width;
1615 + __v4l2_ctrl_modify_range(imx477->hblank, hblank, hblank, 1, hblank);
1618 +static int imx477_set_pad_format(struct v4l2_subdev *sd,
1619 + struct v4l2_subdev_pad_config *cfg,
1620 + struct v4l2_subdev_format *fmt)
1622 + struct v4l2_mbus_framefmt *framefmt;
1623 + const struct imx477_mode *mode;
1624 + struct imx477 *imx477 = to_imx477(sd);
1626 + if (fmt->pad >= NUM_PADS)
1629 + mutex_lock(&imx477->mutex);
1631 + if (fmt->pad == IMAGE_PAD) {
1632 + const struct imx477_mode *mode_list;
1633 + unsigned int num_modes;
1635 + /* Bayer order varies with flips */
1636 + fmt->format.code = imx477_get_format_code(imx477,
1637 + fmt->format.code);
1639 + get_mode_table(fmt->format.code, &mode_list, &num_modes);
1641 + mode = v4l2_find_nearest_size(mode_list,
1644 + fmt->format.width,
1645 + fmt->format.height);
1646 + imx477_update_image_pad_format(imx477, mode, fmt);
1647 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1648 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
1650 + *framefmt = fmt->format;
1652 + imx477->mode = mode;
1653 + imx477_set_framing_limits(imx477);
1656 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1657 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
1659 + *framefmt = fmt->format;
1661 + /* Only one embedded data mode is supported */
1662 + imx477_update_metadata_pad_format(fmt);
1666 + mutex_unlock(&imx477->mutex);
1671 +static const struct v4l2_rect *
1672 +__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
1673 + unsigned int pad, enum v4l2_subdev_format_whence which)
1676 + case V4L2_SUBDEV_FORMAT_TRY:
1677 + return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
1678 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1679 + return &imx477->mode->crop;
1685 +static int imx477_get_selection(struct v4l2_subdev *sd,
1686 + struct v4l2_subdev_pad_config *cfg,
1687 + struct v4l2_subdev_selection *sel)
1689 + switch (sel->target) {
1690 + case V4L2_SEL_TGT_CROP: {
1691 + struct imx477 *imx477 = to_imx477(sd);
1693 + mutex_lock(&imx477->mutex);
1694 + sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
1696 + mutex_unlock(&imx477->mutex);
1701 + case V4L2_SEL_TGT_NATIVE_SIZE:
1704 + sel->r.width = IMX477_NATIVE_WIDTH;
1705 + sel->r.height = IMX477_NATIVE_HEIGHT;
1709 + case V4L2_SEL_TGT_CROP_DEFAULT:
1710 + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
1711 + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
1712 + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
1713 + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
1721 +/* Start streaming */
1722 +static int imx477_start_streaming(struct imx477 *imx477)
1724 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1725 + const struct imx477_reg_list *reg_list;
1728 + if (!imx477->common_regs_written) {
1729 + ret = imx477_write_regs(imx477, mode_common_regs,
1730 + ARRAY_SIZE(mode_common_regs));
1732 + dev_err(&client->dev, "%s failed to set common settings\n",
1736 + imx477->common_regs_written = true;
1739 + /* Apply default values of current mode */
1740 + reg_list = &imx477->mode->reg_list;
1741 + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
1743 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
1747 + /* Apply customized values from user */
1748 + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
1752 + /* set stream on register */
1753 + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1754 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
1757 +/* Stop streaming */
1758 +static void imx477_stop_streaming(struct imx477 *imx477)
1760 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1763 + /* set stream off register */
1764 + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
1765 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
1767 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
1770 +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
1772 + struct imx477 *imx477 = to_imx477(sd);
1773 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1776 + mutex_lock(&imx477->mutex);
1777 + if (imx477->streaming == enable) {
1778 + mutex_unlock(&imx477->mutex);
1783 + ret = pm_runtime_get_sync(&client->dev);
1785 + pm_runtime_put_noidle(&client->dev);
1790 + * Apply default & customized values
1791 + * and then start streaming.
1793 + ret = imx477_start_streaming(imx477);
1797 + imx477_stop_streaming(imx477);
1798 + pm_runtime_put(&client->dev);
1801 + imx477->streaming = enable;
1803 + /* vflip and hflip cannot change during streaming */
1804 + __v4l2_ctrl_grab(imx477->vflip, enable);
1805 + __v4l2_ctrl_grab(imx477->hflip, enable);
1807 + mutex_unlock(&imx477->mutex);
1812 + pm_runtime_put(&client->dev);
1814 + mutex_unlock(&imx477->mutex);
1819 +/* Power/clock management functions */
1820 +static int imx477_power_on(struct device *dev)
1822 + struct i2c_client *client = to_i2c_client(dev);
1823 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1824 + struct imx477 *imx477 = to_imx477(sd);
1827 + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
1828 + imx477->supplies);
1830 + dev_err(&client->dev, "%s: failed to enable regulators\n",
1835 + ret = clk_prepare_enable(imx477->xclk);
1837 + dev_err(&client->dev, "%s: failed to enable clock\n",
1842 + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
1843 + usleep_range(IMX477_XCLR_MIN_DELAY_US,
1844 + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
1849 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1853 +static int imx477_power_off(struct device *dev)
1855 + struct i2c_client *client = to_i2c_client(dev);
1856 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1857 + struct imx477 *imx477 = to_imx477(sd);
1859 + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
1860 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
1861 + clk_disable_unprepare(imx477->xclk);
1863 + /* Force reprogramming of the common registers when powered up again. */
1864 + imx477->common_regs_written = false;
1869 +static int __maybe_unused imx477_suspend(struct device *dev)
1871 + struct i2c_client *client = to_i2c_client(dev);
1872 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1873 + struct imx477 *imx477 = to_imx477(sd);
1875 + if (imx477->streaming)
1876 + imx477_stop_streaming(imx477);
1881 +static int __maybe_unused imx477_resume(struct device *dev)
1883 + struct i2c_client *client = to_i2c_client(dev);
1884 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1885 + struct imx477 *imx477 = to_imx477(sd);
1888 + if (imx477->streaming) {
1889 + ret = imx477_start_streaming(imx477);
1897 + imx477_stop_streaming(imx477);
1898 + imx477->streaming = 0;
1902 +static int imx477_get_regulators(struct imx477 *imx477)
1904 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1907 + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
1908 + imx477->supplies[i].supply = imx477_supply_name[i];
1910 + return devm_regulator_bulk_get(&client->dev,
1911 + IMX477_NUM_SUPPLIES,
1912 + imx477->supplies);
1915 +/* Verify chip ID */
1916 +static int imx477_identify_module(struct imx477 *imx477)
1918 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1922 + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
1923 + IMX477_REG_VALUE_16BIT, &val);
1925 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1926 + IMX477_CHIP_ID, ret);
1930 + if (val != IMX477_CHIP_ID) {
1931 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1932 + IMX477_CHIP_ID, val);
1939 +static const struct v4l2_subdev_core_ops imx477_core_ops = {
1940 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1941 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1944 +static const struct v4l2_subdev_video_ops imx477_video_ops = {
1945 + .s_stream = imx477_set_stream,
1948 +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
1949 + .enum_mbus_code = imx477_enum_mbus_code,
1950 + .get_fmt = imx477_get_pad_format,
1951 + .set_fmt = imx477_set_pad_format,
1952 + .get_selection = imx477_get_selection,
1953 + .enum_frame_size = imx477_enum_frame_size,
1956 +static const struct v4l2_subdev_ops imx477_subdev_ops = {
1957 + .core = &imx477_core_ops,
1958 + .video = &imx477_video_ops,
1959 + .pad = &imx477_pad_ops,
1962 +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
1963 + .open = imx477_open,
1966 +/* Initialize control handlers */
1967 +static int imx477_init_controls(struct imx477 *imx477)
1969 + struct v4l2_ctrl_handler *ctrl_hdlr;
1970 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1974 + ctrl_hdlr = &imx477->ctrl_handler;
1975 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 14);
1979 + mutex_init(&imx477->mutex);
1980 + ctrl_hdlr->lock = &imx477->mutex;
1982 + /* By default, PIXEL_RATE is read only */
1983 + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
1984 + V4L2_CID_PIXEL_RATE,
1985 + IMX477_PIXEL_RATE,
1986 + IMX477_PIXEL_RATE, 1,
1987 + IMX477_PIXEL_RATE);
1990 + * Create the controls here, but mode specific limits are setup
1991 + * in the imx477_set_framing_limits() call below.
1993 + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
1994 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
1995 + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
1996 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
1998 + /* HBLANK is read-only for now, but does change with mode. */
1999 + if (imx477->hblank)
2000 + imx477->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2002 + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2003 + V4L2_CID_EXPOSURE,
2004 + IMX477_EXPOSURE_MIN,
2005 + IMX477_EXPOSURE_MAX,
2006 + IMX477_EXPOSURE_STEP,
2007 + IMX477_EXPOSURE_DEFAULT);
2009 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2010 + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2011 + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2013 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2014 + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2015 + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2017 + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2018 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2019 + if (imx477->hflip)
2020 + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2022 + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2023 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2024 + if (imx477->vflip)
2025 + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2027 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2028 + V4L2_CID_TEST_PATTERN,
2029 + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2030 + 0, 0, imx477_test_pattern_menu);
2031 + for (i = 0; i < 4; i++) {
2033 + * The assumption is that
2034 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2035 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2036 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2038 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2039 + V4L2_CID_TEST_PATTERN_RED + i,
2040 + IMX477_TEST_PATTERN_COLOUR_MIN,
2041 + IMX477_TEST_PATTERN_COLOUR_MAX,
2042 + IMX477_TEST_PATTERN_COLOUR_STEP,
2043 + IMX477_TEST_PATTERN_COLOUR_MAX);
2044 + /* The "Solid color" pattern is white by default */
2047 + if (ctrl_hdlr->error) {
2048 + ret = ctrl_hdlr->error;
2049 + dev_err(&client->dev, "%s control init failed (%d)\n",
2054 + imx477->sd.ctrl_handler = ctrl_hdlr;
2056 + /* Setup exposure and frame/line length limits. */
2057 + imx477_set_framing_limits(imx477);
2062 + v4l2_ctrl_handler_free(ctrl_hdlr);
2063 + mutex_destroy(&imx477->mutex);
2068 +static void imx477_free_controls(struct imx477 *imx477)
2070 + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2071 + mutex_destroy(&imx477->mutex);
2074 +static int imx477_check_hwcfg(struct device *dev)
2076 + struct fwnode_handle *endpoint;
2077 + struct v4l2_fwnode_endpoint ep_cfg = {
2078 + .bus_type = V4L2_MBUS_CSI2_DPHY
2080 + int ret = -EINVAL;
2082 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2084 + dev_err(dev, "endpoint node not found\n");
2088 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2089 + dev_err(dev, "could not parse endpoint\n");
2093 + /* Check the number of MIPI CSI2 data lanes */
2094 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2095 + dev_err(dev, "only 2 data lanes are currently supported\n");
2099 + /* Check the link frequency set in device tree */
2100 + if (!ep_cfg.nr_of_link_frequencies) {
2101 + dev_err(dev, "link-frequency property not found in DT\n");
2105 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2106 + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2107 + dev_err(dev, "Link frequency not supported: %lld\n",
2108 + ep_cfg.link_frequencies[0]);
2115 + v4l2_fwnode_endpoint_free(&ep_cfg);
2116 + fwnode_handle_put(endpoint);
2121 +static int imx477_probe(struct i2c_client *client)
2123 + struct device *dev = &client->dev;
2124 + struct imx477 *imx477;
2127 + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2131 + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2133 + /* Check the hardware configuration in device tree */
2134 + if (imx477_check_hwcfg(dev))
2137 + /* Get system clock (xclk) */
2138 + imx477->xclk = devm_clk_get(dev, NULL);
2139 + if (IS_ERR(imx477->xclk)) {
2140 + dev_err(dev, "failed to get xclk\n");
2141 + return PTR_ERR(imx477->xclk);
2144 + imx477->xclk_freq = clk_get_rate(imx477->xclk);
2145 + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2146 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2147 + imx477->xclk_freq);
2151 + ret = imx477_get_regulators(imx477);
2153 + dev_err(dev, "failed to get regulators\n");
2157 + /* Request optional enable pin */
2158 + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2162 + * The sensor must be powered for imx477_identify_module()
2163 + * to be able to read the CHIP_ID register
2165 + ret = imx477_power_on(dev);
2169 + ret = imx477_identify_module(imx477);
2171 + goto error_power_off;
2173 + /* Initialize default format */
2174 + imx477_set_default_format(imx477);
2176 + /* Enable runtime PM and turn off the device */
2177 + pm_runtime_set_active(dev);
2178 + pm_runtime_enable(dev);
2179 + pm_runtime_idle(dev);
2181 + /* This needs the pm runtime to be registered. */
2182 + ret = imx477_init_controls(imx477);
2184 + goto error_power_off;
2186 + /* Initialize subdev */
2187 + imx477->sd.internal_ops = &imx477_internal_ops;
2188 + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2189 + V4L2_SUBDEV_FL_HAS_EVENTS;
2190 + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2192 + /* Initialize source pads */
2193 + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2194 + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2196 + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2198 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2199 + goto error_handler_free;
2202 + ret = v4l2_async_register_subdev_sensor_common(&imx477->sd);
2204 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2205 + goto error_media_entity;
2210 +error_media_entity:
2211 + media_entity_cleanup(&imx477->sd.entity);
2213 +error_handler_free:
2214 + imx477_free_controls(imx477);
2217 + pm_runtime_disable(&client->dev);
2218 + pm_runtime_set_suspended(&client->dev);
2219 + imx477_power_off(&client->dev);
2224 +static int imx477_remove(struct i2c_client *client)
2226 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2227 + struct imx477 *imx477 = to_imx477(sd);
2229 + v4l2_async_unregister_subdev(sd);
2230 + media_entity_cleanup(&sd->entity);
2231 + imx477_free_controls(imx477);
2233 + pm_runtime_disable(&client->dev);
2234 + if (!pm_runtime_status_suspended(&client->dev))
2235 + imx477_power_off(&client->dev);
2236 + pm_runtime_set_suspended(&client->dev);
2241 +static const struct of_device_id imx477_dt_ids[] = {
2242 + { .compatible = "sony,imx477" },
2243 + { /* sentinel */ }
2245 +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2247 +static const struct dev_pm_ops imx477_pm_ops = {
2248 + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2249 + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2252 +static struct i2c_driver imx477_i2c_driver = {
2255 + .of_match_table = imx477_dt_ids,
2256 + .pm = &imx477_pm_ops,
2258 + .probe_new = imx477_probe,
2259 + .remove = imx477_remove,
2262 +module_i2c_driver(imx477_i2c_driver);
2264 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2265 +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2266 +MODULE_LICENSE("GPL v2");