1 From 9b5a7e22d42b137d7ced182ded00337c6f819795 Mon Sep 17 00:00:00 2001
2 From: Lee Jackson <info@arducam.com>
3 Date: Fri, 27 Aug 2021 14:36:55 +0800
4 Subject: [PATCH] media: i2c: imx519: Support for the Sony IMX519
7 dt-bindings: media: i2c: Add IMX519 CMOS sensor binding
9 Add YAML device tree binding for IMX519 CMOS image sensor, and
10 the relevant MAINTAINERS entries.
12 Signed-off-by: Lee Jackson <info@arducam.com>
14 media: i2c: Add driver for IMX519 sensor
16 Adds a driver for the 16MPix IMX519 CSI2 sensor.
17 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
18 currently only supports 2 lanes.
20 The following Bayer modes are currently available:
22 4656x3496 10-bit @ 10fps
23 3840x2160 10-bit (cropped) @ 21fps
24 2328x1748 10-bit (binned) @ 30fps
25 1920x1080 10-bit (cropped/binned) @ 60fps
26 1280x720 10-bit (cropped/binned) @ 120fps
28 Signed-off-by: Lee Jackson <info@arducam.com>
30 media: i2c: imx519: Advertise embedded data node on media pad 1
32 This commit updates the imx519 driver to adverise support for embedded
35 The imx519 sensor subdevice overloads the media pad to differentiate
36 between image stream (pad 0) and embedded data stream (pad 1) when
37 performing the v4l2_subdev_pad_ops functions.
39 Signed-off-by: Lee Jackson <info@arducam.com>
41 media: i2c: imx519: Sensor should report RAW color space
43 Tested on Raspberry Pi running libcamera.
45 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
47 media: i2c: Update imx519 Kconfig entry
49 Bring the IMX519 Kconfig declaration in line with the upstream entries.
51 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
53 .../devicetree/bindings/media/i2c/imx519.yaml | 113 +
55 drivers/media/i2c/Kconfig | 11 +
56 drivers/media/i2c/Makefile | 1 +
57 drivers/media/i2c/imx519.c | 2090 +++++++++++++++++
58 5 files changed, 2223 insertions(+)
59 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx519.yaml
60 create mode 100644 drivers/media/i2c/imx519.c
63 +++ b/Documentation/devicetree/bindings/media/i2c/imx519.yaml
65 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
68 +$id: http://devicetree.org/schemas/media/i2c/imx519.yaml#
69 +$schema: http://devicetree.org/meta-schemas/core.yaml#
71 +title: Sony 1/2.5-Inch 16Mpixel CMOS Digital Image Sensor
74 + - Lee Jackson <info@arducam.com>
77 + The Sony IMX519 is a 1/2.5-inch CMOS active pixel digital image sensor
78 + with an active array size of 4656H x 3496V. It is programmable through
79 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
80 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
88 + description: I2C device address
96 + Digital I/O voltage supply, 1.05 volts
100 + Analog voltage supply, 2.8 volts
104 + Digital core voltage supply, 1.8 volts
108 + Reference to the GPIO connected to the xclr pin, if any.
109 + Must be released (set high) after all supplies and INCK are applied.
111 + # See ../video-interfaces.txt for more details
120 + The sensor supports either two-lane, or four-lane operation.
121 + For two-lane operation the property must be set to <1 2>.
126 + clock-noncontinuous:
129 + MIPI CSI-2 clock is non-continuous if this property is present,
130 + otherwise it's continuous.
134 + - $ref: /schemas/types.yaml#/definitions/uint64-array
136 + Allowed data bus frequencies.
150 +additionalProperties: false
155 + #address-cells = <1>;
158 + imx519: sensor@1a {
159 + compatible = "sony,imx519";
161 + clocks = <&imx519_clk>;
162 + VANA-supply = <&imx519_vana>; /* 2.8v */
163 + VDIG-supply = <&imx519_vdig>; /* 1.05v */
164 + VDDL-supply = <&imx519_vddl>; /* 1.8v */
167 + imx519_0: endpoint {
168 + remote-endpoint = <&csi1_ep>;
169 + data-lanes = <1 2>;
170 + clock-noncontinuous;
171 + link-frequencies = /bits/ 64 <493500000>;
180 @@ -19287,6 +19287,14 @@ T: git git://linuxtv.org/media_tree.git
181 F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
182 F: drivers/media/i2c/imx477.c
184 +SONY IMX519 SENSOR DRIVER
185 +M: Arducam Kernel Maintenance <info@arducam.com>
186 +L: linux-media@vger.kernel.org
188 +T: git git://linuxtv.org/media_tree.git
189 +F: Documentation/devicetree/bindings/media/i2c/imx519.yaml
190 +F: drivers/media/i2c/imx519.c
192 SONY MEMORYSTICK SUBSYSTEM
193 M: Maxim Levitsky <maximlevitsky@gmail.com>
194 M: Alex Dubov <oakad@yahoo.com>
195 --- a/drivers/media/i2c/Kconfig
196 +++ b/drivers/media/i2c/Kconfig
197 @@ -235,6 +235,17 @@ config VIDEO_IMX412
198 To compile this driver as a module, choose M here: the
199 module will be called imx412.
202 + tristate "Arducam IMX519 sensor support"
203 + depends on I2C && VIDEO_DEV
204 + select VIDEO_V4L2_SUBDEV_API
206 + This is a Video4Linux2 sensor driver for the Arducam
209 + To compile this driver as a module, choose M here: the
210 + module will be called IMX519.
212 config VIDEO_MAX9271_LIB
215 --- a/drivers/media/i2c/Makefile
216 +++ b/drivers/media/i2c/Makefile
217 @@ -49,6 +49,7 @@ obj-$(CONFIG_VIDEO_IMX335) += imx335.o
218 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
219 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
220 obj-$(CONFIG_VIDEO_IMX477) += imx477.o
221 +obj-$(CONFIG_VIDEO_IMX519) += imx519.o
222 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
223 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
224 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
226 +++ b/drivers/media/i2c/imx519.c
228 +// SPDX-License-Identifier: GPL-2.0
230 + * A V4L2 driver for Sony IMX519 cameras.
231 + * Copyright (C) 2021 Arducam Technology co., Ltd.
233 + * Based on Sony IMX477 camera driver
234 + * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
236 +#include <asm/unaligned.h>
237 +#include <linux/clk.h>
238 +#include <linux/delay.h>
239 +#include <linux/gpio/consumer.h>
240 +#include <linux/i2c.h>
241 +#include <linux/module.h>
242 +#include <linux/of_device.h>
243 +#include <linux/pm_runtime.h>
244 +#include <linux/regulator/consumer.h>
245 +#include <media/v4l2-ctrls.h>
246 +#include <media/v4l2-device.h>
247 +#include <media/v4l2-event.h>
248 +#include <media/v4l2-fwnode.h>
249 +#include <media/v4l2-mediabus.h>
251 +#define IMX519_REG_VALUE_08BIT 1
252 +#define IMX519_REG_VALUE_16BIT 2
255 +#define IMX519_REG_CHIP_ID 0x0016
256 +#define IMX519_CHIP_ID 0x0519
258 +#define IMX519_REG_MODE_SELECT 0x0100
259 +#define IMX519_MODE_STANDBY 0x00
260 +#define IMX519_MODE_STREAMING 0x01
262 +#define IMX519_REG_ORIENTATION 0x101
264 +#define IMX519_XCLK_FREQ 24000000
266 +#define IMX519_DEFAULT_LINK_FREQ 493500000
268 +/* Pixel rate is fixed at 686MHz for all the modes */
269 +#define IMX519_PIXEL_RATE 686000000
271 +/* V_TIMING internal */
272 +#define IMX519_REG_FRAME_LENGTH 0x0340
273 +#define IMX519_FRAME_LENGTH_MAX 0xffdc
275 +/* Long exposure multiplier */
276 +#define IMX519_LONG_EXP_SHIFT_MAX 7
277 +#define IMX519_LONG_EXP_SHIFT_REG 0x3100
279 +/* Exposure control */
280 +#define IMX519_REG_EXPOSURE 0x0202
281 +#define IMX519_EXPOSURE_OFFSET 32
282 +#define IMX519_EXPOSURE_MIN 1
283 +#define IMX519_EXPOSURE_STEP 1
284 +#define IMX519_EXPOSURE_DEFAULT 0x3e8
285 +#define IMX519_EXPOSURE_MAX (IMX519_FRAME_LENGTH_MAX - \
286 + IMX519_EXPOSURE_OFFSET)
288 +/* Analog gain control */
289 +#define IMX519_REG_ANALOG_GAIN 0x0204
290 +#define IMX519_ANA_GAIN_MIN 0
291 +#define IMX519_ANA_GAIN_MAX 960
292 +#define IMX519_ANA_GAIN_STEP 1
293 +#define IMX519_ANA_GAIN_DEFAULT 0x0
295 +/* Digital gain control */
296 +#define IMX519_REG_DIGITAL_GAIN 0x020e
297 +#define IMX519_DGTL_GAIN_MIN 0x0100
298 +#define IMX519_DGTL_GAIN_MAX 0xffff
299 +#define IMX519_DGTL_GAIN_DEFAULT 0x0100
300 +#define IMX519_DGTL_GAIN_STEP 1
302 +/* Test Pattern Control */
303 +#define IMX519_REG_TEST_PATTERN 0x0600
304 +#define IMX519_TEST_PATTERN_DISABLE 0
305 +#define IMX519_TEST_PATTERN_SOLID_COLOR 1
306 +#define IMX519_TEST_PATTERN_COLOR_BARS 2
307 +#define IMX519_TEST_PATTERN_GREY_COLOR 3
308 +#define IMX519_TEST_PATTERN_PN9 4
310 +/* Test pattern colour components */
311 +#define IMX519_REG_TEST_PATTERN_R 0x0602
312 +#define IMX519_REG_TEST_PATTERN_GR 0x0604
313 +#define IMX519_REG_TEST_PATTERN_B 0x0606
314 +#define IMX519_REG_TEST_PATTERN_GB 0x0608
315 +#define IMX519_TEST_PATTERN_COLOUR_MIN 0
316 +#define IMX519_TEST_PATTERN_COLOUR_MAX 0x0fff
317 +#define IMX519_TEST_PATTERN_COLOUR_STEP 1
318 +#define IMX519_TEST_PATTERN_R_DEFAULT IMX519_TEST_PATTERN_COLOUR_MAX
319 +#define IMX519_TEST_PATTERN_GR_DEFAULT 0
320 +#define IMX519_TEST_PATTERN_B_DEFAULT 0
321 +#define IMX519_TEST_PATTERN_GB_DEFAULT 0
323 +/* Embedded metadata stream structure */
324 +#define IMX519_EMBEDDED_LINE_WIDTH 16384
325 +#define IMX519_NUM_EMBEDDED_LINES 1
333 +/* IMX519 native and active pixel array size. */
334 +#define IMX519_NATIVE_WIDTH 4672U
335 +#define IMX519_NATIVE_HEIGHT 3648U
336 +#define IMX519_PIXEL_ARRAY_LEFT 8U
337 +#define IMX519_PIXEL_ARRAY_TOP 48U
338 +#define IMX519_PIXEL_ARRAY_WIDTH 4656U
339 +#define IMX519_PIXEL_ARRAY_HEIGHT 3496U
346 +struct imx519_reg_list {
347 + unsigned int num_of_regs;
348 + const struct imx519_reg *regs;
351 +/* Mode : resolution and related config&values */
352 +struct imx519_mode {
354 + unsigned int width;
357 + unsigned int height;
359 + /* H-timing in pixels */
360 + unsigned int line_length_pix;
362 + /* Analog crop rectangle. */
363 + struct v4l2_rect crop;
365 + /* Highest possible framerate. */
366 + struct v4l2_fract timeperframe_min;
368 + /* Default framerate. */
369 + struct v4l2_fract timeperframe_default;
371 + /* Default register values */
372 + struct imx519_reg_list reg_list;
375 +static const struct imx519_reg mode_common_regs[] = {
651 +static const struct imx519_reg mode_4656x3496_regs[] = {
728 +static const struct imx519_reg mode_3840x2160_regs[] = {
804 +/* 2x2 binned 30fps mode */
805 +static const struct imx519_reg mode_2328x1748_regs[] = {
881 +/* 1080p 60fps mode */
882 +static const struct imx519_reg mode_1920x1080_regs[] = {
958 +/* 720p 120fps mode */
959 +static const struct imx519_reg mode_1280x720_regs[] = {
1036 +static const struct imx519_mode supported_modes_10bit[] = {
1040 + .line_length_pix = 0x4200,
1042 + .left = IMX519_PIXEL_ARRAY_LEFT,
1043 + .top = IMX519_PIXEL_ARRAY_TOP,
1047 + .timeperframe_min = {
1049 + .denominator = 1000
1051 + .timeperframe_default = {
1053 + .denominator = 1000
1056 + .num_of_regs = ARRAY_SIZE(mode_4656x3496_regs),
1057 + .regs = mode_4656x3496_regs,
1063 + .line_length_pix = 0x3870,
1065 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1066 + .top = IMX519_PIXEL_ARRAY_TOP + 672,
1070 + .timeperframe_min = {
1072 + .denominator = 2100
1074 + .timeperframe_default = {
1076 + .denominator = 2100
1079 + .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
1080 + .regs = mode_3840x2160_regs,
1086 + .line_length_pix = 0x2412,
1088 + .left = IMX519_PIXEL_ARRAY_LEFT,
1089 + .top = IMX519_PIXEL_ARRAY_TOP,
1093 + .timeperframe_min = {
1095 + .denominator = 3000
1097 + .timeperframe_default = {
1099 + .denominator = 3000
1102 + .num_of_regs = ARRAY_SIZE(mode_2328x1748_regs),
1103 + .regs = mode_2328x1748_regs,
1109 + .line_length_pix = 0x25D9,
1111 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1112 + .top = IMX519_PIXEL_ARRAY_TOP + 674,
1116 + .timeperframe_min = {
1118 + .denominator = 6000
1120 + .timeperframe_default = {
1122 + .denominator = 6000
1125 + .num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
1126 + .regs = mode_1920x1080_regs,
1132 + .line_length_pix = 0x1B3B,
1134 + .left = IMX519_PIXEL_ARRAY_LEFT + 1048,
1135 + .top = IMX519_PIXEL_ARRAY_TOP + 1042,
1139 + .timeperframe_min = {
1141 + .denominator = 12000
1143 + .timeperframe_default = {
1145 + .denominator = 12000
1148 + .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1149 + .regs = mode_1280x720_regs,
1155 + * The supported formats.
1156 + * This table MUST contain 4 entries per format, to cover the various flip
1157 + * combinations in the order
1163 +static const u32 codes[] = {
1164 + /* 10-bit modes. */
1165 + MEDIA_BUS_FMT_SRGGB10_1X10,
1166 + MEDIA_BUS_FMT_SGRBG10_1X10,
1167 + MEDIA_BUS_FMT_SGBRG10_1X10,
1168 + MEDIA_BUS_FMT_SBGGR10_1X10,
1171 +static const char * const imx519_test_pattern_menu[] = {
1175 + "Grey Color Bars",
1179 +static const int imx519_test_pattern_val[] = {
1180 + IMX519_TEST_PATTERN_DISABLE,
1181 + IMX519_TEST_PATTERN_COLOR_BARS,
1182 + IMX519_TEST_PATTERN_SOLID_COLOR,
1183 + IMX519_TEST_PATTERN_GREY_COLOR,
1184 + IMX519_TEST_PATTERN_PN9,
1187 +/* regulator supplies */
1188 +static const char * const imx519_supply_name[] = {
1189 + /* Supplies can be enabled in any order */
1190 + "VANA", /* Analog (2.8V) supply */
1191 + "VDIG", /* Digital Core (1.05V) supply */
1192 + "VDDL", /* IF (1.8V) supply */
1195 +#define IMX519_NUM_SUPPLIES ARRAY_SIZE(imx519_supply_name)
1198 + * Initialisation delay between XCLR low->high and the moment when the sensor
1199 + * can start capture (i.e. can leave software standby), given by T7 in the
1200 + * datasheet is 8ms. This does include I2C setup time as well.
1202 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1203 + * in the datasheet) is much smaller - 600us.
1205 +#define IMX519_XCLR_MIN_DELAY_US 8000
1206 +#define IMX519_XCLR_DELAY_RANGE_US 1000
1209 + struct v4l2_subdev sd;
1210 + struct media_pad pad[NUM_PADS];
1212 + unsigned int fmt_code;
1216 + struct gpio_desc *reset_gpio;
1217 + struct regulator_bulk_data supplies[IMX519_NUM_SUPPLIES];
1219 + struct v4l2_ctrl_handler ctrl_handler;
1220 + /* V4L2 Controls */
1221 + struct v4l2_ctrl *pixel_rate;
1222 + struct v4l2_ctrl *exposure;
1223 + struct v4l2_ctrl *vflip;
1224 + struct v4l2_ctrl *hflip;
1225 + struct v4l2_ctrl *vblank;
1226 + struct v4l2_ctrl *hblank;
1228 + /* Current mode */
1229 + const struct imx519_mode *mode;
1232 + * Mutex for serialized access:
1233 + * Protect sensor module set pad format and start/stop streaming safely.
1235 + struct mutex mutex;
1237 + /* Streaming on/off */
1240 + /* Rewrite common registers on stream on? */
1241 + bool common_regs_written;
1243 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1244 + unsigned int long_exp_shift;
1247 +static inline struct imx519 *to_imx519(struct v4l2_subdev *_sd)
1249 + return container_of(_sd, struct imx519, sd);
1252 +/* Read registers up to 2 at a time */
1253 +static int imx519_read_reg(struct imx519 *imx519, u16 reg, u32 len, u32 *val)
1255 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1256 + struct i2c_msg msgs[2];
1257 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1258 + u8 data_buf[4] = { 0, };
1264 + /* Write register address */
1265 + msgs[0].addr = client->addr;
1266 + msgs[0].flags = 0;
1267 + msgs[0].len = ARRAY_SIZE(addr_buf);
1268 + msgs[0].buf = addr_buf;
1270 + /* Read data from register */
1271 + msgs[1].addr = client->addr;
1272 + msgs[1].flags = I2C_M_RD;
1273 + msgs[1].len = len;
1274 + msgs[1].buf = &data_buf[4 - len];
1276 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1277 + if (ret != ARRAY_SIZE(msgs))
1280 + *val = get_unaligned_be32(data_buf);
1285 +/* Write registers up to 2 at a time */
1286 +static int imx519_write_reg(struct imx519 *imx519, u16 reg, u32 len, u32 val)
1288 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1294 + put_unaligned_be16(reg, buf);
1295 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1296 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1302 +/* Write a list of registers */
1303 +static int imx519_write_regs(struct imx519 *imx519,
1304 + const struct imx519_reg *regs, u32 len)
1306 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1310 + for (i = 0; i < len; i++) {
1311 + ret = imx519_write_reg(imx519, regs[i].address, 1, regs[i].val);
1313 + dev_err_ratelimited(&client->dev,
1314 + "Failed to write reg 0x%4.4x. error = %d\n",
1315 + regs[i].address, ret);
1324 +/* Get bayer order based on flip setting. */
1325 +static u32 imx519_get_format_code(struct imx519 *imx519)
1329 + lockdep_assert_held(&imx519->mutex);
1331 + i = (imx519->vflip->val ? 2 : 0) |
1332 + (imx519->hflip->val ? 1 : 0);
1337 +static int imx519_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1339 + struct imx519 *imx519 = to_imx519(sd);
1340 + struct v4l2_mbus_framefmt *try_fmt_img =
1341 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
1342 + struct v4l2_mbus_framefmt *try_fmt_meta =
1343 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
1344 + struct v4l2_rect *try_crop;
1346 + mutex_lock(&imx519->mutex);
1348 + /* Initialize try_fmt for the image pad */
1349 + try_fmt_img->width = supported_modes_10bit[0].width;
1350 + try_fmt_img->height = supported_modes_10bit[0].height;
1351 + try_fmt_img->code = imx519_get_format_code(imx519);
1352 + try_fmt_img->field = V4L2_FIELD_NONE;
1354 + /* Initialize try_fmt for the embedded metadata pad */
1355 + try_fmt_meta->width = IMX519_EMBEDDED_LINE_WIDTH;
1356 + try_fmt_meta->height = IMX519_NUM_EMBEDDED_LINES;
1357 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1358 + try_fmt_meta->field = V4L2_FIELD_NONE;
1360 + /* Initialize try_crop */
1361 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
1362 + try_crop->left = IMX519_PIXEL_ARRAY_LEFT;
1363 + try_crop->top = IMX519_PIXEL_ARRAY_TOP;
1364 + try_crop->width = IMX519_PIXEL_ARRAY_WIDTH;
1365 + try_crop->height = IMX519_PIXEL_ARRAY_HEIGHT;
1367 + mutex_unlock(&imx519->mutex);
1372 +static void imx519_adjust_exposure_range(struct imx519 *imx519)
1374 + int exposure_max, exposure_def;
1376 + /* Honour the VBLANK limits when setting exposure. */
1377 + exposure_max = imx519->mode->height + imx519->vblank->val -
1378 + IMX519_EXPOSURE_OFFSET;
1379 + exposure_def = min(exposure_max, imx519->exposure->val);
1380 + __v4l2_ctrl_modify_range(imx519->exposure, imx519->exposure->minimum,
1381 + exposure_max, imx519->exposure->step,
1385 +static int imx519_set_frame_length(struct imx519 *imx519, unsigned int val)
1389 + imx519->long_exp_shift = 0;
1391 + while (val > IMX519_FRAME_LENGTH_MAX) {
1392 + imx519->long_exp_shift++;
1396 + ret = imx519_write_reg(imx519, IMX519_REG_FRAME_LENGTH,
1397 + IMX519_REG_VALUE_16BIT, val);
1401 + return imx519_write_reg(imx519, IMX519_LONG_EXP_SHIFT_REG,
1402 + IMX519_REG_VALUE_08BIT, imx519->long_exp_shift);
1405 +static int imx519_set_ctrl(struct v4l2_ctrl *ctrl)
1407 + struct imx519 *imx519 =
1408 + container_of(ctrl->handler, struct imx519, ctrl_handler);
1409 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1413 + * The VBLANK control may change the limits of usable exposure, so check
1414 + * and adjust if necessary.
1416 + if (ctrl->id == V4L2_CID_VBLANK)
1417 + imx519_adjust_exposure_range(imx519);
1420 + * Applying V4L2 control value only happens
1421 + * when power is up for streaming
1423 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1426 + switch (ctrl->id) {
1427 + case V4L2_CID_ANALOGUE_GAIN:
1428 + ret = imx519_write_reg(imx519, IMX519_REG_ANALOG_GAIN,
1429 + IMX519_REG_VALUE_16BIT, ctrl->val);
1431 + case V4L2_CID_EXPOSURE:
1432 + ret = imx519_write_reg(imx519, IMX519_REG_EXPOSURE,
1433 + IMX519_REG_VALUE_16BIT, ctrl->val >>
1434 + imx519->long_exp_shift);
1436 + case V4L2_CID_DIGITAL_GAIN:
1437 + ret = imx519_write_reg(imx519, IMX519_REG_DIGITAL_GAIN,
1438 + IMX519_REG_VALUE_16BIT, ctrl->val);
1440 + case V4L2_CID_TEST_PATTERN:
1441 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN,
1442 + IMX519_REG_VALUE_16BIT,
1443 + imx519_test_pattern_val[ctrl->val]);
1445 + case V4L2_CID_TEST_PATTERN_RED:
1446 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_R,
1447 + IMX519_REG_VALUE_16BIT, ctrl->val);
1449 + case V4L2_CID_TEST_PATTERN_GREENR:
1450 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GR,
1451 + IMX519_REG_VALUE_16BIT, ctrl->val);
1453 + case V4L2_CID_TEST_PATTERN_BLUE:
1454 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_B,
1455 + IMX519_REG_VALUE_16BIT, ctrl->val);
1457 + case V4L2_CID_TEST_PATTERN_GREENB:
1458 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GB,
1459 + IMX519_REG_VALUE_16BIT, ctrl->val);
1461 + case V4L2_CID_HFLIP:
1462 + case V4L2_CID_VFLIP:
1463 + ret = imx519_write_reg(imx519, IMX519_REG_ORIENTATION, 1,
1464 + imx519->hflip->val |
1465 + imx519->vflip->val << 1);
1467 + case V4L2_CID_VBLANK:
1468 + ret = imx519_set_frame_length(imx519,
1469 + imx519->mode->height + ctrl->val);
1472 + dev_info(&client->dev,
1473 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1474 + ctrl->id, ctrl->val);
1479 + pm_runtime_put(&client->dev);
1484 +static const struct v4l2_ctrl_ops imx519_ctrl_ops = {
1485 + .s_ctrl = imx519_set_ctrl,
1488 +static int imx519_enum_mbus_code(struct v4l2_subdev *sd,
1489 + struct v4l2_subdev_state *sd_state,
1490 + struct v4l2_subdev_mbus_code_enum *code)
1492 + struct imx519 *imx519 = to_imx519(sd);
1494 + if (code->pad >= NUM_PADS)
1497 + if (code->pad == IMAGE_PAD) {
1498 + if (code->index > 0)
1501 + code->code = imx519_get_format_code(imx519);
1503 + if (code->index > 0)
1506 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1512 +static int imx519_enum_frame_size(struct v4l2_subdev *sd,
1513 + struct v4l2_subdev_state *sd_state,
1514 + struct v4l2_subdev_frame_size_enum *fse)
1516 + struct imx519 *imx519 = to_imx519(sd);
1518 + if (fse->pad >= NUM_PADS)
1521 + if (fse->pad == IMAGE_PAD) {
1522 + if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
1525 + if (fse->code != imx519_get_format_code(imx519))
1528 + fse->min_width = supported_modes_10bit[fse->index].width;
1529 + fse->max_width = fse->min_width;
1530 + fse->min_height = supported_modes_10bit[fse->index].height;
1531 + fse->max_height = fse->min_height;
1533 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1536 + fse->min_width = IMX519_EMBEDDED_LINE_WIDTH;
1537 + fse->max_width = fse->min_width;
1538 + fse->min_height = IMX519_NUM_EMBEDDED_LINES;
1539 + fse->max_height = fse->min_height;
1545 +static void imx519_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1547 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1548 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1549 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1552 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1555 +static void imx519_update_image_pad_format(struct imx519 *imx519,
1556 + const struct imx519_mode *mode,
1557 + struct v4l2_subdev_format *fmt)
1559 + fmt->format.width = mode->width;
1560 + fmt->format.height = mode->height;
1561 + fmt->format.field = V4L2_FIELD_NONE;
1562 + imx519_reset_colorspace(&fmt->format);
1565 +static void imx519_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1567 + fmt->format.width = IMX519_EMBEDDED_LINE_WIDTH;
1568 + fmt->format.height = IMX519_NUM_EMBEDDED_LINES;
1569 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1570 + fmt->format.field = V4L2_FIELD_NONE;
1573 +static int imx519_get_pad_format(struct v4l2_subdev *sd,
1574 + struct v4l2_subdev_state *sd_state,
1575 + struct v4l2_subdev_format *fmt)
1577 + struct imx519 *imx519 = to_imx519(sd);
1579 + if (fmt->pad >= NUM_PADS)
1582 + mutex_lock(&imx519->mutex);
1584 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1585 + struct v4l2_mbus_framefmt *try_fmt =
1586 + v4l2_subdev_get_try_format(&imx519->sd, sd_state,
1588 + /* update the code which could change due to vflip or hflip: */
1589 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1590 + imx519_get_format_code(imx519) :
1591 + MEDIA_BUS_FMT_SENSOR_DATA;
1592 + fmt->format = *try_fmt;
1594 + if (fmt->pad == IMAGE_PAD) {
1595 + imx519_update_image_pad_format(imx519, imx519->mode,
1597 + fmt->format.code =
1598 + imx519_get_format_code(imx519);
1600 + imx519_update_metadata_pad_format(fmt);
1604 + mutex_unlock(&imx519->mutex);
1609 +unsigned int imx519_get_frame_length(const struct imx519_mode *mode,
1610 + const struct v4l2_fract *timeperframe)
1614 + frame_length = (u64)timeperframe->numerator * IMX519_PIXEL_RATE;
1615 + do_div(frame_length,
1616 + (u64)timeperframe->denominator * mode->line_length_pix);
1618 + if (WARN_ON(frame_length > IMX519_FRAME_LENGTH_MAX))
1619 + frame_length = IMX519_FRAME_LENGTH_MAX;
1621 + return max_t(unsigned int, frame_length, mode->height);
1624 +static void imx519_set_framing_limits(struct imx519 *imx519)
1626 + unsigned int frm_length_min, frm_length_default, hblank;
1627 + const struct imx519_mode *mode = imx519->mode;
1629 + frm_length_min = imx519_get_frame_length(mode, &mode->timeperframe_min);
1630 + frm_length_default =
1631 + imx519_get_frame_length(mode, &mode->timeperframe_default);
1633 + /* Default to no long exposure multiplier. */
1634 + imx519->long_exp_shift = 0;
1636 + /* Update limits and set FPS to default */
1637 + __v4l2_ctrl_modify_range(imx519->vblank, frm_length_min - mode->height,
1638 + ((1 << IMX519_LONG_EXP_SHIFT_MAX) *
1639 + IMX519_FRAME_LENGTH_MAX) - mode->height,
1640 + 1, frm_length_default - mode->height);
1642 + /* Setting this will adjust the exposure limits as well. */
1643 + __v4l2_ctrl_s_ctrl(imx519->vblank, frm_length_default - mode->height);
1646 + * Currently PPL is fixed to the mode specified value, so hblank
1647 + * depends on mode->width only, and is not changeable in any
1648 + * way other than changing the mode.
1650 + hblank = mode->line_length_pix - mode->width;
1651 + __v4l2_ctrl_modify_range(imx519->hblank, hblank, hblank, 1, hblank);
1654 +static int imx519_set_pad_format(struct v4l2_subdev *sd,
1655 + struct v4l2_subdev_state *sd_state,
1656 + struct v4l2_subdev_format *fmt)
1658 + struct v4l2_mbus_framefmt *framefmt;
1659 + const struct imx519_mode *mode;
1660 + struct imx519 *imx519 = to_imx519(sd);
1662 + if (fmt->pad >= NUM_PADS)
1665 + mutex_lock(&imx519->mutex);
1667 + if (fmt->pad == IMAGE_PAD) {
1668 + /* Bayer order varies with flips */
1669 + fmt->format.code = imx519_get_format_code(imx519);
1671 + mode = v4l2_find_nearest_size(supported_modes_10bit,
1672 + ARRAY_SIZE(supported_modes_10bit),
1674 + fmt->format.width,
1675 + fmt->format.height);
1676 + imx519_update_image_pad_format(imx519, mode, fmt);
1677 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1678 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1680 + *framefmt = fmt->format;
1682 + imx519->mode = mode;
1683 + imx519->fmt_code = fmt->format.code;
1684 + imx519_set_framing_limits(imx519);
1687 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1688 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1690 + *framefmt = fmt->format;
1692 + /* Only one embedded data mode is supported */
1693 + imx519_update_metadata_pad_format(fmt);
1697 + mutex_unlock(&imx519->mutex);
1702 +static const struct v4l2_rect *
1703 +__imx519_get_pad_crop(struct imx519 *imx519, struct v4l2_subdev_state *sd_state,
1704 + unsigned int pad, enum v4l2_subdev_format_whence which)
1707 + case V4L2_SUBDEV_FORMAT_TRY:
1708 + return v4l2_subdev_get_try_crop(&imx519->sd, sd_state, pad);
1709 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1710 + return &imx519->mode->crop;
1716 +static int imx519_get_selection(struct v4l2_subdev *sd,
1717 + struct v4l2_subdev_state *sd_state,
1718 + struct v4l2_subdev_selection *sel)
1720 + switch (sel->target) {
1721 + case V4L2_SEL_TGT_CROP: {
1722 + struct imx519 *imx519 = to_imx519(sd);
1724 + mutex_lock(&imx519->mutex);
1725 + sel->r = *__imx519_get_pad_crop(imx519, sd_state, sel->pad,
1727 + mutex_unlock(&imx519->mutex);
1732 + case V4L2_SEL_TGT_NATIVE_SIZE:
1735 + sel->r.width = IMX519_NATIVE_WIDTH;
1736 + sel->r.height = IMX519_NATIVE_HEIGHT;
1740 + case V4L2_SEL_TGT_CROP_DEFAULT:
1741 + case V4L2_SEL_TGT_CROP_BOUNDS:
1742 + sel->r.left = IMX519_PIXEL_ARRAY_LEFT;
1743 + sel->r.top = IMX519_PIXEL_ARRAY_TOP;
1744 + sel->r.width = IMX519_PIXEL_ARRAY_WIDTH;
1745 + sel->r.height = IMX519_PIXEL_ARRAY_HEIGHT;
1753 +/* Start streaming */
1754 +static int imx519_start_streaming(struct imx519 *imx519)
1756 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1757 + const struct imx519_reg_list *reg_list;
1760 + if (!imx519->common_regs_written) {
1761 + ret = imx519_write_regs(imx519, mode_common_regs,
1762 + ARRAY_SIZE(mode_common_regs));
1765 + dev_err(&client->dev, "%s failed to set common settings\n",
1769 + imx519->common_regs_written = true;
1772 + /* Apply default values of current mode */
1773 + reg_list = &imx519->mode->reg_list;
1774 + ret = imx519_write_regs(imx519, reg_list->regs, reg_list->num_of_regs);
1776 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
1780 + /* Apply customized values from user */
1781 + ret = __v4l2_ctrl_handler_setup(imx519->sd.ctrl_handler);
1785 + /* set stream on register */
1786 + return imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1787 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STREAMING);
1790 +/* Stop streaming */
1791 +static void imx519_stop_streaming(struct imx519 *imx519)
1793 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1796 + /* set stream off register */
1797 + ret = imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1798 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STANDBY);
1800 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
1803 +static int imx519_set_stream(struct v4l2_subdev *sd, int enable)
1805 + struct imx519 *imx519 = to_imx519(sd);
1806 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1809 + mutex_lock(&imx519->mutex);
1810 + if (imx519->streaming == enable) {
1811 + mutex_unlock(&imx519->mutex);
1816 + ret = pm_runtime_get_sync(&client->dev);
1818 + pm_runtime_put_noidle(&client->dev);
1823 + * Apply default & customized values
1824 + * and then start streaming.
1826 + ret = imx519_start_streaming(imx519);
1830 + imx519_stop_streaming(imx519);
1831 + pm_runtime_put(&client->dev);
1834 + imx519->streaming = enable;
1836 + /* vflip and hflip cannot change during streaming */
1837 + __v4l2_ctrl_grab(imx519->vflip, enable);
1838 + __v4l2_ctrl_grab(imx519->hflip, enable);
1840 + mutex_unlock(&imx519->mutex);
1845 + pm_runtime_put(&client->dev);
1847 + mutex_unlock(&imx519->mutex);
1852 +/* Power/clock management functions */
1853 +static int imx519_power_on(struct device *dev)
1855 + struct i2c_client *client = to_i2c_client(dev);
1856 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1857 + struct imx519 *imx519 = to_imx519(sd);
1860 + ret = regulator_bulk_enable(IMX519_NUM_SUPPLIES,
1861 + imx519->supplies);
1863 + dev_err(&client->dev, "%s: failed to enable regulators\n",
1868 + ret = clk_prepare_enable(imx519->xclk);
1870 + dev_err(&client->dev, "%s: failed to enable clock\n",
1875 + gpiod_set_value_cansleep(imx519->reset_gpio, 1);
1876 + usleep_range(IMX519_XCLR_MIN_DELAY_US,
1877 + IMX519_XCLR_MIN_DELAY_US + IMX519_XCLR_DELAY_RANGE_US);
1882 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1886 +static int imx519_power_off(struct device *dev)
1888 + struct i2c_client *client = to_i2c_client(dev);
1889 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1890 + struct imx519 *imx519 = to_imx519(sd);
1892 + gpiod_set_value_cansleep(imx519->reset_gpio, 0);
1893 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1894 + clk_disable_unprepare(imx519->xclk);
1896 + /* Force reprogramming of the common registers when powered up again. */
1897 + imx519->common_regs_written = false;
1902 +static int __maybe_unused imx519_suspend(struct device *dev)
1904 + struct i2c_client *client = to_i2c_client(dev);
1905 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1906 + struct imx519 *imx519 = to_imx519(sd);
1908 + if (imx519->streaming)
1909 + imx519_stop_streaming(imx519);
1914 +static int __maybe_unused imx519_resume(struct device *dev)
1916 + struct i2c_client *client = to_i2c_client(dev);
1917 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1918 + struct imx519 *imx519 = to_imx519(sd);
1921 + if (imx519->streaming) {
1922 + ret = imx519_start_streaming(imx519);
1930 + imx519_stop_streaming(imx519);
1931 + imx519->streaming = 0;
1935 +static int imx519_get_regulators(struct imx519 *imx519)
1937 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1940 + for (i = 0; i < IMX519_NUM_SUPPLIES; i++)
1941 + imx519->supplies[i].supply = imx519_supply_name[i];
1943 + return devm_regulator_bulk_get(&client->dev,
1944 + IMX519_NUM_SUPPLIES,
1945 + imx519->supplies);
1948 +/* Verify chip ID */
1949 +static int imx519_identify_module(struct imx519 *imx519, u32 expected_id)
1951 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1955 + ret = imx519_read_reg(imx519, IMX519_REG_CHIP_ID,
1956 + IMX519_REG_VALUE_16BIT, &val);
1958 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1959 + expected_id, ret);
1963 + if (val != expected_id) {
1964 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1965 + expected_id, val);
1969 + dev_info(&client->dev, "Device found is imx%x\n", val);
1974 +static const struct v4l2_subdev_core_ops imx519_core_ops = {
1975 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1976 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1979 +static const struct v4l2_subdev_video_ops imx519_video_ops = {
1980 + .s_stream = imx519_set_stream,
1983 +static const struct v4l2_subdev_pad_ops imx519_pad_ops = {
1984 + .enum_mbus_code = imx519_enum_mbus_code,
1985 + .get_fmt = imx519_get_pad_format,
1986 + .set_fmt = imx519_set_pad_format,
1987 + .get_selection = imx519_get_selection,
1988 + .enum_frame_size = imx519_enum_frame_size,
1991 +static const struct v4l2_subdev_ops imx519_subdev_ops = {
1992 + .core = &imx519_core_ops,
1993 + .video = &imx519_video_ops,
1994 + .pad = &imx519_pad_ops,
1997 +static const struct v4l2_subdev_internal_ops imx519_internal_ops = {
1998 + .open = imx519_open,
2001 +/* Initialize control handlers */
2002 +static int imx519_init_controls(struct imx519 *imx519)
2004 + struct v4l2_ctrl_handler *ctrl_hdlr;
2005 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
2006 + struct v4l2_fwnode_device_properties props;
2010 + ctrl_hdlr = &imx519->ctrl_handler;
2011 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2015 + mutex_init(&imx519->mutex);
2016 + ctrl_hdlr->lock = &imx519->mutex;
2018 + /* By default, PIXEL_RATE is read only */
2019 + imx519->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2020 + V4L2_CID_PIXEL_RATE,
2021 + IMX519_PIXEL_RATE,
2022 + IMX519_PIXEL_RATE, 1,
2023 + IMX519_PIXEL_RATE);
2026 + * Create the controls here, but mode specific limits are setup
2027 + * in the imx519_set_framing_limits() call below.
2029 + imx519->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2030 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2031 + imx519->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2032 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2034 + /* HBLANK is read-only for now, but does change with mode. */
2035 + if (imx519->hblank)
2036 + imx519->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2038 + imx519->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2039 + V4L2_CID_EXPOSURE,
2040 + IMX519_EXPOSURE_MIN,
2041 + IMX519_EXPOSURE_MAX,
2042 + IMX519_EXPOSURE_STEP,
2043 + IMX519_EXPOSURE_DEFAULT);
2045 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2046 + IMX519_ANA_GAIN_MIN, IMX519_ANA_GAIN_MAX,
2047 + IMX519_ANA_GAIN_STEP, IMX519_ANA_GAIN_DEFAULT);
2049 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2050 + IMX519_DGTL_GAIN_MIN, IMX519_DGTL_GAIN_MAX,
2051 + IMX519_DGTL_GAIN_STEP, IMX519_DGTL_GAIN_DEFAULT);
2053 + imx519->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2054 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2055 + if (imx519->hflip)
2056 + imx519->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2058 + imx519->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2059 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2060 + if (imx519->vflip)
2061 + imx519->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2063 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx519_ctrl_ops,
2064 + V4L2_CID_TEST_PATTERN,
2065 + ARRAY_SIZE(imx519_test_pattern_menu) - 1,
2066 + 0, 0, imx519_test_pattern_menu);
2067 + for (i = 0; i < 4; i++) {
2069 + * The assumption is that
2070 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2071 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2072 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2074 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2075 + V4L2_CID_TEST_PATTERN_RED + i,
2076 + IMX519_TEST_PATTERN_COLOUR_MIN,
2077 + IMX519_TEST_PATTERN_COLOUR_MAX,
2078 + IMX519_TEST_PATTERN_COLOUR_STEP,
2079 + IMX519_TEST_PATTERN_COLOUR_MAX);
2080 + /* The "Solid color" pattern is white by default */
2083 + if (ctrl_hdlr->error) {
2084 + ret = ctrl_hdlr->error;
2085 + dev_err(&client->dev, "%s control init failed (%d)\n",
2090 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2094 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx519_ctrl_ops,
2099 + imx519->sd.ctrl_handler = ctrl_hdlr;
2101 + /* Setup exposure and frame/line length limits. */
2102 + imx519_set_framing_limits(imx519);
2107 + v4l2_ctrl_handler_free(ctrl_hdlr);
2108 + mutex_destroy(&imx519->mutex);
2113 +static void imx519_free_controls(struct imx519 *imx519)
2115 + v4l2_ctrl_handler_free(imx519->sd.ctrl_handler);
2116 + mutex_destroy(&imx519->mutex);
2119 +static int imx519_check_hwcfg(struct device *dev)
2121 + struct fwnode_handle *endpoint;
2122 + struct v4l2_fwnode_endpoint ep_cfg = {
2123 + .bus_type = V4L2_MBUS_CSI2_DPHY
2125 + int ret = -EINVAL;
2127 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2129 + dev_err(dev, "endpoint node not found\n");
2133 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2134 + dev_err(dev, "could not parse endpoint\n");
2138 + /* Check the number of MIPI CSI2 data lanes */
2139 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2140 + dev_err(dev, "only 2 data lanes are currently supported\n");
2144 + /* Check the link frequency set in device tree */
2145 + if (!ep_cfg.nr_of_link_frequencies) {
2146 + dev_err(dev, "link-frequency property not found in DT\n");
2150 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2151 + ep_cfg.link_frequencies[0] != IMX519_DEFAULT_LINK_FREQ) {
2152 + dev_err(dev, "Link frequency not supported: %lld\n",
2153 + ep_cfg.link_frequencies[0]);
2160 + v4l2_fwnode_endpoint_free(&ep_cfg);
2161 + fwnode_handle_put(endpoint);
2166 +static const struct of_device_id imx519_dt_ids[] = {
2167 + { .compatible = "sony,imx519"},
2168 + { /* sentinel */ }
2171 +static int imx519_probe(struct i2c_client *client)
2173 + struct device *dev = &client->dev;
2174 + struct imx519 *imx519;
2175 + const struct of_device_id *match;
2179 + imx519 = devm_kzalloc(&client->dev, sizeof(*imx519), GFP_KERNEL);
2183 + v4l2_i2c_subdev_init(&imx519->sd, client, &imx519_subdev_ops);
2185 + match = of_match_device(imx519_dt_ids, dev);
2189 + /* Check the hardware configuration in device tree */
2190 + if (imx519_check_hwcfg(dev))
2193 + /* Get system clock (xclk) */
2194 + imx519->xclk = devm_clk_get(dev, NULL);
2195 + if (IS_ERR(imx519->xclk)) {
2196 + dev_err(dev, "failed to get xclk\n");
2197 + return PTR_ERR(imx519->xclk);
2200 + xclk_freq = clk_get_rate(imx519->xclk);
2201 + if (xclk_freq != IMX519_XCLK_FREQ) {
2202 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2207 + ret = imx519_get_regulators(imx519);
2209 + dev_err(dev, "failed to get regulators\n");
2213 + /* Request optional enable pin */
2214 + imx519->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2218 + * The sensor must be powered for imx519_identify_module()
2219 + * to be able to read the CHIP_ID register
2221 + ret = imx519_power_on(dev);
2225 + ret = imx519_identify_module(imx519, IMX519_CHIP_ID);
2227 + goto error_power_off;
2229 + /* Set default mode to max resolution */
2230 + imx519->mode = &supported_modes_10bit[0];
2231 + imx519->fmt_code = MEDIA_BUS_FMT_SRGGB10_1X10;
2233 + /* Enable runtime PM and turn off the device */
2234 + pm_runtime_set_active(dev);
2235 + pm_runtime_enable(dev);
2236 + pm_runtime_idle(dev);
2238 + /* This needs the pm runtime to be registered. */
2239 + ret = imx519_init_controls(imx519);
2241 + goto error_power_off;
2243 + /* Initialize subdev */
2244 + imx519->sd.internal_ops = &imx519_internal_ops;
2245 + imx519->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2246 + V4L2_SUBDEV_FL_HAS_EVENTS;
2247 + imx519->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2249 + /* Initialize source pads */
2250 + imx519->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2251 + imx519->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2253 + ret = media_entity_pads_init(&imx519->sd.entity, NUM_PADS, imx519->pad);
2255 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2256 + goto error_handler_free;
2259 + ret = v4l2_async_register_subdev_sensor(&imx519->sd);
2261 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2262 + goto error_media_entity;
2267 +error_media_entity:
2268 + media_entity_cleanup(&imx519->sd.entity);
2270 +error_handler_free:
2271 + imx519_free_controls(imx519);
2274 + pm_runtime_disable(&client->dev);
2275 + pm_runtime_set_suspended(&client->dev);
2276 + imx519_power_off(&client->dev);
2281 +static void imx519_remove(struct i2c_client *client)
2283 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2284 + struct imx519 *imx519 = to_imx519(sd);
2286 + v4l2_async_unregister_subdev(sd);
2287 + media_entity_cleanup(&sd->entity);
2288 + imx519_free_controls(imx519);
2290 + pm_runtime_disable(&client->dev);
2291 + if (!pm_runtime_status_suspended(&client->dev))
2292 + imx519_power_off(&client->dev);
2293 + pm_runtime_set_suspended(&client->dev);
2296 +MODULE_DEVICE_TABLE(of, imx519_dt_ids);
2298 +static const struct dev_pm_ops imx519_pm_ops = {
2299 + SET_SYSTEM_SLEEP_PM_OPS(imx519_suspend, imx519_resume)
2300 + SET_RUNTIME_PM_OPS(imx519_power_off, imx519_power_on, NULL)
2303 +static struct i2c_driver imx519_i2c_driver = {
2306 + .of_match_table = imx519_dt_ids,
2307 + .pm = &imx519_pm_ops,
2309 + .probe_new = imx519_probe,
2310 + .remove = imx519_remove,
2313 +module_i2c_driver(imx519_i2c_driver);
2315 +MODULE_AUTHOR("Lee Jackson <info@arducam.com>");
2316 +MODULE_DESCRIPTION("Sony IMX519 sensor driver");
2317 +MODULE_LICENSE("GPL v2");