1 From 4f5baf76ca33c76b97a4d310d5ca13395ba2110c 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 sensor
6 dt-bindings: media: i2c: Add IMX519 CMOS sensor binding
8 Add YAML device tree binding for IMX519 CMOS image sensor, and
9 the relevant MAINTAINERS entries.
11 Signed-off-by: Lee Jackson <info@arducam.com>
13 media: i2c: Add driver for IMX519 sensor
15 Adds a driver for the 16MPix IMX519 CSI2 sensor.
16 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
17 currently only supports 2 lanes.
19 The following Bayer modes are currently available:
21 4656x3496 10-bit @ 10fps
22 3840x2160 10-bit (cropped) @ 21fps
23 2328x1748 10-bit (binned) @ 30fps
24 1920x1080 10-bit (cropped/binned) @ 60fps
25 1280x720 10-bit (cropped/binned) @ 120fps
27 Signed-off-by: Lee Jackson <info@arducam.com>
29 media: i2c: imx519: Advertise embedded data node on media pad 1
31 This commit updates the imx519 driver to adverise support for embedded
34 The imx519 sensor subdevice overloads the media pad to differentiate
35 between image stream (pad 0) and embedded data stream (pad 1) when
36 performing the v4l2_subdev_pad_ops functions.
38 Signed-off-by: Lee Jackson <info@arducam.com>
40 media: i2c: imx519: Sensor should report RAW color space
42 Tested on Raspberry Pi running libcamera.
44 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
46 media: i2c: Update imx519 Kconfig entry
48 Bring the IMX519 Kconfig declaration in line with the upstream entries.
50 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
52 media: i2c: Add PDAF support for IMX519
54 Add PDAF support for IMX519, and reduce the pixel rate to 426666667,
55 link freq to 408000000.
57 Signed-off-by: Lee Jackson <lee.jackson@arducam.com>
59 drivers: media: imx519: Add V4L2_CID_LINK_FREQ control
61 Add V4L2_CID_LINK_FREQ as a read-only control with a value of 408 Mhz.
62 This will be used by the CFE driver to corretly setup the DPHY timing
63 parameters in the CSI-2 block.
65 Signed-off-by: Lee Jackson <lee.jackson@arducam.com>
67 media: i2c: imx519: Squash fixes
69 .../devicetree/bindings/media/i2c/imx519.yaml | 113 +
71 drivers/media/i2c/Kconfig | 11 +
72 drivers/media/i2c/Makefile | 1 +
73 drivers/media/i2c/imx519.c | 2146 +++++++++++++++++
74 5 files changed, 2279 insertions(+)
75 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx519.yaml
76 create mode 100644 drivers/media/i2c/imx519.c
79 +++ b/Documentation/devicetree/bindings/media/i2c/imx519.yaml
81 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
84 +$id: http://devicetree.org/schemas/media/i2c/imx519.yaml#
85 +$schema: http://devicetree.org/meta-schemas/core.yaml#
87 +title: Sony 1/2.5-Inch 16Mpixel CMOS Digital Image Sensor
90 + - Lee Jackson <info@arducam.com>
93 + The Sony IMX519 is a 1/2.5-inch CMOS active pixel digital image sensor
94 + with an active array size of 4656H x 3496V. It is programmable through
95 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
96 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
104 + description: I2C device address
112 + Digital I/O voltage supply, 1.05 volts
116 + Analog voltage supply, 2.8 volts
120 + Digital core voltage supply, 1.8 volts
124 + Reference to the GPIO connected to the xclr pin, if any.
125 + Must be released (set high) after all supplies and INCK are applied.
127 + # See ../video-interfaces.txt for more details
136 + The sensor supports either two-lane, or four-lane operation.
137 + For two-lane operation the property must be set to <1 2>.
142 + clock-noncontinuous:
145 + MIPI CSI-2 clock is non-continuous if this property is present,
146 + otherwise it's continuous.
150 + - $ref: /schemas/types.yaml#/definitions/uint64-array
152 + Allowed data bus frequencies.
166 +additionalProperties: false
171 + #address-cells = <1>;
174 + imx519: sensor@1a {
175 + compatible = "sony,imx519";
177 + clocks = <&imx519_clk>;
178 + VANA-supply = <&imx519_vana>; /* 2.8v */
179 + VDIG-supply = <&imx519_vdig>; /* 1.05v */
180 + VDDL-supply = <&imx519_vddl>; /* 1.8v */
183 + imx519_0: endpoint {
184 + remote-endpoint = <&csi1_ep>;
185 + data-lanes = <1 2>;
186 + clock-noncontinuous;
187 + link-frequencies = /bits/ 64 <493500000>;
196 @@ -21556,6 +21556,14 @@ T: git git://linuxtv.org/media_tree.git
197 F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
198 F: drivers/media/i2c/imx477.c
200 +SONY IMX519 SENSOR DRIVER
201 +M: Arducam Kernel Maintenance <info@arducam.com>
202 +L: linux-media@vger.kernel.org
204 +T: git git://linuxtv.org/media_tree.git
205 +F: Documentation/devicetree/bindings/media/i2c/imx519.yaml
206 +F: drivers/media/i2c/imx519.c
208 SONY MEMORYSTICK SUBSYSTEM
209 M: Maxim Levitsky <maximlevitsky@gmail.com>
210 M: Alex Dubov <oakad@yahoo.com>
211 --- a/drivers/media/i2c/Kconfig
212 +++ b/drivers/media/i2c/Kconfig
213 @@ -265,6 +265,17 @@ config VIDEO_IMX415
214 To compile this driver as a module, choose M here: the
215 module will be called imx415.
218 + tristate "Arducam IMX519 sensor support"
219 + depends on I2C && VIDEO_DEV
220 + select VIDEO_V4L2_SUBDEV_API
222 + This is a Video4Linux2 sensor driver for the Arducam
225 + To compile this driver as a module, choose M here: the
226 + module will be called IMX519.
228 config VIDEO_MAX9271_LIB
231 --- a/drivers/media/i2c/Makefile
232 +++ b/drivers/media/i2c/Makefile
233 @@ -60,6 +60,7 @@ obj-$(CONFIG_VIDEO_IMX355) += imx355.o
234 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
235 obj-$(CONFIG_VIDEO_IMX415) += imx415.o
236 obj-$(CONFIG_VIDEO_IMX477) += imx477.o
237 +obj-$(CONFIG_VIDEO_IMX519) += imx519.o
238 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
239 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
240 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
242 +++ b/drivers/media/i2c/imx519.c
244 +// SPDX-License-Identifier: GPL-2.0
246 + * A V4L2 driver for Sony IMX519 cameras.
247 + * Copyright (C) 2021 Arducam Technology co., Ltd.
249 + * Based on Sony IMX477 camera driver
250 + * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
252 +#include <linux/unaligned.h>
253 +#include <linux/clk.h>
254 +#include <linux/delay.h>
255 +#include <linux/gpio/consumer.h>
256 +#include <linux/i2c.h>
257 +#include <linux/module.h>
258 +#include <linux/of_device.h>
259 +#include <linux/pm_runtime.h>
260 +#include <linux/regulator/consumer.h>
261 +#include <media/v4l2-ctrls.h>
262 +#include <media/v4l2-device.h>
263 +#include <media/v4l2-event.h>
264 +#include <media/v4l2-fwnode.h>
265 +#include <media/v4l2-mediabus.h>
267 +#define IMX519_REG_VALUE_08BIT 1
268 +#define IMX519_REG_VALUE_16BIT 2
271 +#define IMX519_REG_CHIP_ID 0x0016
272 +#define IMX519_CHIP_ID 0x0519
274 +#define IMX519_REG_MODE_SELECT 0x0100
275 +#define IMX519_MODE_STANDBY 0x00
276 +#define IMX519_MODE_STREAMING 0x01
278 +#define IMX519_REG_ORIENTATION 0x101
280 +#define IMX519_XCLK_FREQ 24000000
282 +#define IMX519_DEFAULT_LINK_FREQ 408000000
284 +/* Pixel rate is fixed at 426MHz for all the modes */
285 +#define IMX519_PIXEL_RATE 426666667
287 +/* V_TIMING internal */
288 +#define IMX519_REG_FRAME_LENGTH 0x0340
289 +#define IMX519_FRAME_LENGTH_MAX 0xffdc
291 +/* Long exposure multiplier */
292 +#define IMX519_LONG_EXP_SHIFT_MAX 7
293 +#define IMX519_LONG_EXP_SHIFT_REG 0x3100
295 +/* Exposure control */
296 +#define IMX519_REG_EXPOSURE 0x0202
297 +#define IMX519_EXPOSURE_OFFSET 32
298 +#define IMX519_EXPOSURE_MIN 20
299 +#define IMX519_EXPOSURE_STEP 1
300 +#define IMX519_EXPOSURE_DEFAULT 0x3e8
301 +#define IMX519_EXPOSURE_MAX (IMX519_FRAME_LENGTH_MAX - \
302 + IMX519_EXPOSURE_OFFSET)
304 +/* Analog gain control */
305 +#define IMX519_REG_ANALOG_GAIN 0x0204
306 +#define IMX519_ANA_GAIN_MIN 0
307 +#define IMX519_ANA_GAIN_MAX 960
308 +#define IMX519_ANA_GAIN_STEP 1
309 +#define IMX519_ANA_GAIN_DEFAULT 0x0
311 +/* Digital gain control */
312 +#define IMX519_REG_DIGITAL_GAIN 0x020e
313 +#define IMX519_DGTL_GAIN_MIN 0x0100
314 +#define IMX519_DGTL_GAIN_MAX 0xffff
315 +#define IMX519_DGTL_GAIN_DEFAULT 0x0100
316 +#define IMX519_DGTL_GAIN_STEP 1
318 +/* Test Pattern Control */
319 +#define IMX519_REG_TEST_PATTERN 0x0600
320 +#define IMX519_TEST_PATTERN_DISABLE 0
321 +#define IMX519_TEST_PATTERN_SOLID_COLOR 1
322 +#define IMX519_TEST_PATTERN_COLOR_BARS 2
323 +#define IMX519_TEST_PATTERN_GREY_COLOR 3
324 +#define IMX519_TEST_PATTERN_PN9 4
326 +/* Test pattern colour components */
327 +#define IMX519_REG_TEST_PATTERN_R 0x0602
328 +#define IMX519_REG_TEST_PATTERN_GR 0x0604
329 +#define IMX519_REG_TEST_PATTERN_B 0x0606
330 +#define IMX519_REG_TEST_PATTERN_GB 0x0608
331 +#define IMX519_TEST_PATTERN_COLOUR_MIN 0
332 +#define IMX519_TEST_PATTERN_COLOUR_MAX 0x0fff
333 +#define IMX519_TEST_PATTERN_COLOUR_STEP 1
334 +#define IMX519_TEST_PATTERN_R_DEFAULT IMX519_TEST_PATTERN_COLOUR_MAX
335 +#define IMX519_TEST_PATTERN_GR_DEFAULT 0
336 +#define IMX519_TEST_PATTERN_B_DEFAULT 0
337 +#define IMX519_TEST_PATTERN_GB_DEFAULT 0
339 +/* Embedded metadata stream structure */
340 +#define IMX519_EMBEDDED_LINE_WIDTH (5820 * 3)
341 +#define IMX519_NUM_EMBEDDED_LINES 1
349 +/* IMX519 native and active pixel array size. */
350 +#define IMX519_NATIVE_WIDTH 4672U
351 +#define IMX519_NATIVE_HEIGHT 3648U
352 +#define IMX519_PIXEL_ARRAY_LEFT 8U
353 +#define IMX519_PIXEL_ARRAY_TOP 48U
354 +#define IMX519_PIXEL_ARRAY_WIDTH 4656U
355 +#define IMX519_PIXEL_ARRAY_HEIGHT 3496U
362 +struct imx519_reg_list {
363 + unsigned int num_of_regs;
364 + const struct imx519_reg *regs;
367 +/* Mode : resolution and related config&values */
368 +struct imx519_mode {
370 + unsigned int width;
373 + unsigned int height;
375 + /* H-timing in pixels */
376 + unsigned int line_length_pix;
378 + /* Analog crop rectangle. */
379 + struct v4l2_rect crop;
381 + /* Highest possible framerate. */
382 + struct v4l2_fract timeperframe_min;
384 + /* Default framerate. */
385 + struct v4l2_fract timeperframe_default;
387 + /* Default register values */
388 + struct imx519_reg_list reg_list;
391 +static const s64 imx519_link_freq_menu[] = {
392 + IMX519_DEFAULT_LINK_FREQ,
395 +static const struct imx519_reg mode_common_regs[] = {
673 +static const struct imx519_reg mode_4656x3496_regs[] = {
758 +static const struct imx519_reg mode_3840x2160_regs[] = {
842 +/* 2x2 binned 30fps mode */
843 +static const struct imx519_reg mode_2328x1748_regs[] = {
927 +/* 1080p 60fps mode */
928 +static const struct imx519_reg mode_1920x1080_regs[] = {
1012 +/* 720p 120fps mode */
1013 +static const struct imx519_reg mode_1280x720_regs[] = {
1098 +static const struct imx519_mode supported_modes_10bit[] = {
1102 + .line_length_pix = 0x316a,
1104 + .left = IMX519_PIXEL_ARRAY_LEFT,
1105 + .top = IMX519_PIXEL_ARRAY_TOP,
1109 + .timeperframe_min = {
1111 + .denominator = 900
1113 + .timeperframe_default = {
1115 + .denominator = 900
1118 + .num_of_regs = ARRAY_SIZE(mode_4656x3496_regs),
1119 + .regs = mode_4656x3496_regs,
1125 + .line_length_pix = 0x28f6,
1127 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1128 + .top = IMX519_PIXEL_ARRAY_TOP + 672,
1132 + .timeperframe_min = {
1134 + .denominator = 1800
1136 + .timeperframe_default = {
1138 + .denominator = 1800
1141 + .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
1142 + .regs = mode_3840x2160_regs,
1148 + .line_length_pix = 0x1970,
1150 + .left = IMX519_PIXEL_ARRAY_LEFT,
1151 + .top = IMX519_PIXEL_ARRAY_TOP,
1155 + .timeperframe_min = {
1157 + .denominator = 3000
1159 + .timeperframe_default = {
1161 + .denominator = 3000
1164 + .num_of_regs = ARRAY_SIZE(mode_2328x1748_regs),
1165 + .regs = mode_2328x1748_regs,
1171 + .line_length_pix = 0x178b,
1173 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1174 + .top = IMX519_PIXEL_ARRAY_TOP + 674,
1178 + .timeperframe_min = {
1180 + .denominator = 6000
1182 + .timeperframe_default = {
1184 + .denominator = 6000
1187 + .num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
1188 + .regs = mode_1920x1080_regs,
1194 + .line_length_pix = 0x1800,
1196 + .left = IMX519_PIXEL_ARRAY_LEFT + 1048,
1197 + .top = IMX519_PIXEL_ARRAY_TOP + 1042,
1201 + .timeperframe_min = {
1203 + .denominator = 8000
1205 + .timeperframe_default = {
1207 + .denominator = 8000
1210 + .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1211 + .regs = mode_1280x720_regs,
1217 + * The supported formats.
1218 + * This table MUST contain 4 entries per format, to cover the various flip
1219 + * combinations in the order
1225 +static const u32 codes[] = {
1226 + /* 10-bit modes. */
1227 + MEDIA_BUS_FMT_SRGGB10_1X10,
1228 + MEDIA_BUS_FMT_SGRBG10_1X10,
1229 + MEDIA_BUS_FMT_SGBRG10_1X10,
1230 + MEDIA_BUS_FMT_SBGGR10_1X10,
1233 +static const char * const imx519_test_pattern_menu[] = {
1237 + "Grey Color Bars",
1241 +static const int imx519_test_pattern_val[] = {
1242 + IMX519_TEST_PATTERN_DISABLE,
1243 + IMX519_TEST_PATTERN_COLOR_BARS,
1244 + IMX519_TEST_PATTERN_SOLID_COLOR,
1245 + IMX519_TEST_PATTERN_GREY_COLOR,
1246 + IMX519_TEST_PATTERN_PN9,
1249 +/* regulator supplies */
1250 +static const char * const imx519_supply_name[] = {
1251 + /* Supplies can be enabled in any order */
1252 + "VANA", /* Analog (2.8V) supply */
1253 + "VDIG", /* Digital Core (1.05V) supply */
1254 + "VDDL", /* IF (1.8V) supply */
1257 +#define IMX519_NUM_SUPPLIES ARRAY_SIZE(imx519_supply_name)
1260 + * Initialisation delay between XCLR low->high and the moment when the sensor
1261 + * can start capture (i.e. can leave software standby), given by T7 in the
1262 + * datasheet is 8ms. This does include I2C setup time as well.
1264 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1265 + * in the datasheet) is much smaller - 600us.
1267 +#define IMX519_XCLR_MIN_DELAY_US 8000
1268 +#define IMX519_XCLR_DELAY_RANGE_US 1000
1271 + struct v4l2_subdev sd;
1272 + struct media_pad pad[NUM_PADS];
1274 + unsigned int fmt_code;
1278 + struct gpio_desc *reset_gpio;
1279 + struct regulator_bulk_data supplies[IMX519_NUM_SUPPLIES];
1281 + struct v4l2_ctrl_handler ctrl_handler;
1282 + /* V4L2 Controls */
1283 + struct v4l2_ctrl *pixel_rate;
1284 + struct v4l2_ctrl *exposure;
1285 + struct v4l2_ctrl *vflip;
1286 + struct v4l2_ctrl *hflip;
1287 + struct v4l2_ctrl *vblank;
1288 + struct v4l2_ctrl *hblank;
1290 + /* Current mode */
1291 + const struct imx519_mode *mode;
1294 + * Mutex for serialized access:
1295 + * Protect sensor module set pad format and start/stop streaming safely.
1297 + struct mutex mutex;
1299 + /* Streaming on/off */
1302 + /* Rewrite common registers on stream on? */
1303 + bool common_regs_written;
1305 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1306 + unsigned int long_exp_shift;
1309 +static inline struct imx519 *to_imx519(struct v4l2_subdev *_sd)
1311 + return container_of(_sd, struct imx519, sd);
1314 +/* Read registers up to 2 at a time */
1315 +static int imx519_read_reg(struct imx519 *imx519, u16 reg, u32 len, u32 *val)
1317 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1318 + struct i2c_msg msgs[2];
1319 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1320 + u8 data_buf[4] = { 0, };
1326 + /* Write register address */
1327 + msgs[0].addr = client->addr;
1328 + msgs[0].flags = 0;
1329 + msgs[0].len = ARRAY_SIZE(addr_buf);
1330 + msgs[0].buf = addr_buf;
1332 + /* Read data from register */
1333 + msgs[1].addr = client->addr;
1334 + msgs[1].flags = I2C_M_RD;
1335 + msgs[1].len = len;
1336 + msgs[1].buf = &data_buf[4 - len];
1338 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1339 + if (ret != ARRAY_SIZE(msgs))
1342 + *val = get_unaligned_be32(data_buf);
1347 +/* Write registers up to 2 at a time */
1348 +static int imx519_write_reg(struct imx519 *imx519, u16 reg, u32 len, u32 val)
1350 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1356 + put_unaligned_be16(reg, buf);
1357 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1358 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1364 +/* Write a list of registers */
1365 +static int imx519_write_regs(struct imx519 *imx519,
1366 + const struct imx519_reg *regs, u32 len)
1368 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1372 + for (i = 0; i < len; i++) {
1373 + ret = imx519_write_reg(imx519, regs[i].address, 1, regs[i].val);
1375 + dev_err_ratelimited(&client->dev,
1376 + "Failed to write reg 0x%4.4x. error = %d\n",
1377 + regs[i].address, ret);
1386 +/* Get bayer order based on flip setting. */
1387 +static u32 imx519_get_format_code(struct imx519 *imx519)
1391 + lockdep_assert_held(&imx519->mutex);
1393 + i = (imx519->vflip->val ? 2 : 0) |
1394 + (imx519->hflip->val ? 1 : 0);
1399 +static int imx519_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1401 + struct imx519 *imx519 = to_imx519(sd);
1402 + struct v4l2_mbus_framefmt *try_fmt_img =
1403 + v4l2_subdev_state_get_format(fh->state, IMAGE_PAD);
1404 + struct v4l2_mbus_framefmt *try_fmt_meta =
1405 + v4l2_subdev_state_get_format(fh->state, METADATA_PAD);
1406 + struct v4l2_rect *try_crop;
1408 + mutex_lock(&imx519->mutex);
1410 + /* Initialize try_fmt for the image pad */
1411 + try_fmt_img->width = supported_modes_10bit[0].width;
1412 + try_fmt_img->height = supported_modes_10bit[0].height;
1413 + try_fmt_img->code = imx519_get_format_code(imx519);
1414 + try_fmt_img->field = V4L2_FIELD_NONE;
1416 + /* Initialize try_fmt for the embedded metadata pad */
1417 + try_fmt_meta->width = IMX519_EMBEDDED_LINE_WIDTH;
1418 + try_fmt_meta->height = IMX519_NUM_EMBEDDED_LINES;
1419 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1420 + try_fmt_meta->field = V4L2_FIELD_NONE;
1422 + /* Initialize try_crop */
1423 + try_crop = v4l2_subdev_state_get_crop(fh->state, IMAGE_PAD);
1424 + try_crop->left = IMX519_PIXEL_ARRAY_LEFT;
1425 + try_crop->top = IMX519_PIXEL_ARRAY_TOP;
1426 + try_crop->width = IMX519_PIXEL_ARRAY_WIDTH;
1427 + try_crop->height = IMX519_PIXEL_ARRAY_HEIGHT;
1429 + mutex_unlock(&imx519->mutex);
1434 +static void imx519_adjust_exposure_range(struct imx519 *imx519)
1436 + int exposure_max, exposure_def;
1438 + /* Honour the VBLANK limits when setting exposure. */
1439 + exposure_max = imx519->mode->height + imx519->vblank->val -
1440 + IMX519_EXPOSURE_OFFSET;
1441 + exposure_def = min(exposure_max, imx519->exposure->val);
1442 + __v4l2_ctrl_modify_range(imx519->exposure, imx519->exposure->minimum,
1443 + exposure_max, imx519->exposure->step,
1447 +static int imx519_set_frame_length(struct imx519 *imx519, unsigned int val)
1451 + imx519->long_exp_shift = 0;
1453 + while (val > IMX519_FRAME_LENGTH_MAX) {
1454 + imx519->long_exp_shift++;
1458 + ret = imx519_write_reg(imx519, IMX519_REG_FRAME_LENGTH,
1459 + IMX519_REG_VALUE_16BIT, val);
1463 + return imx519_write_reg(imx519, IMX519_LONG_EXP_SHIFT_REG,
1464 + IMX519_REG_VALUE_08BIT, imx519->long_exp_shift);
1467 +static int imx519_set_ctrl(struct v4l2_ctrl *ctrl)
1469 + struct imx519 *imx519 =
1470 + container_of(ctrl->handler, struct imx519, ctrl_handler);
1471 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1475 + * The VBLANK control may change the limits of usable exposure, so check
1476 + * and adjust if necessary.
1478 + if (ctrl->id == V4L2_CID_VBLANK)
1479 + imx519_adjust_exposure_range(imx519);
1482 + * Applying V4L2 control value only happens
1483 + * when power is up for streaming
1485 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1488 + switch (ctrl->id) {
1489 + case V4L2_CID_ANALOGUE_GAIN:
1490 + ret = imx519_write_reg(imx519, IMX519_REG_ANALOG_GAIN,
1491 + IMX519_REG_VALUE_16BIT, ctrl->val);
1493 + case V4L2_CID_EXPOSURE:
1494 + ret = imx519_write_reg(imx519, IMX519_REG_EXPOSURE,
1495 + IMX519_REG_VALUE_16BIT, ctrl->val >>
1496 + imx519->long_exp_shift);
1498 + case V4L2_CID_DIGITAL_GAIN:
1499 + ret = imx519_write_reg(imx519, IMX519_REG_DIGITAL_GAIN,
1500 + IMX519_REG_VALUE_16BIT, ctrl->val);
1502 + case V4L2_CID_TEST_PATTERN:
1503 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN,
1504 + IMX519_REG_VALUE_16BIT,
1505 + imx519_test_pattern_val[ctrl->val]);
1507 + case V4L2_CID_TEST_PATTERN_RED:
1508 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_R,
1509 + IMX519_REG_VALUE_16BIT, ctrl->val);
1511 + case V4L2_CID_TEST_PATTERN_GREENR:
1512 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GR,
1513 + IMX519_REG_VALUE_16BIT, ctrl->val);
1515 + case V4L2_CID_TEST_PATTERN_BLUE:
1516 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_B,
1517 + IMX519_REG_VALUE_16BIT, ctrl->val);
1519 + case V4L2_CID_TEST_PATTERN_GREENB:
1520 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GB,
1521 + IMX519_REG_VALUE_16BIT, ctrl->val);
1523 + case V4L2_CID_HFLIP:
1524 + case V4L2_CID_VFLIP:
1525 + ret = imx519_write_reg(imx519, IMX519_REG_ORIENTATION, 1,
1526 + imx519->hflip->val |
1527 + imx519->vflip->val << 1);
1529 + case V4L2_CID_VBLANK:
1530 + ret = imx519_set_frame_length(imx519,
1531 + imx519->mode->height + ctrl->val);
1534 + dev_info(&client->dev,
1535 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1536 + ctrl->id, ctrl->val);
1541 + pm_runtime_put(&client->dev);
1546 +static const struct v4l2_ctrl_ops imx519_ctrl_ops = {
1547 + .s_ctrl = imx519_set_ctrl,
1550 +static int imx519_enum_mbus_code(struct v4l2_subdev *sd,
1551 + struct v4l2_subdev_state *sd_state,
1552 + struct v4l2_subdev_mbus_code_enum *code)
1554 + struct imx519 *imx519 = to_imx519(sd);
1556 + if (code->pad >= NUM_PADS)
1559 + if (code->pad == IMAGE_PAD) {
1560 + if (code->index > 0)
1563 + code->code = imx519_get_format_code(imx519);
1565 + if (code->index > 0)
1568 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1574 +static int imx519_enum_frame_size(struct v4l2_subdev *sd,
1575 + struct v4l2_subdev_state *sd_state,
1576 + struct v4l2_subdev_frame_size_enum *fse)
1578 + struct imx519 *imx519 = to_imx519(sd);
1580 + if (fse->pad >= NUM_PADS)
1583 + if (fse->pad == IMAGE_PAD) {
1584 + if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
1587 + if (fse->code != imx519_get_format_code(imx519))
1590 + fse->min_width = supported_modes_10bit[fse->index].width;
1591 + fse->max_width = fse->min_width;
1592 + fse->min_height = supported_modes_10bit[fse->index].height;
1593 + fse->max_height = fse->min_height;
1595 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1598 + fse->min_width = IMX519_EMBEDDED_LINE_WIDTH;
1599 + fse->max_width = fse->min_width;
1600 + fse->min_height = IMX519_NUM_EMBEDDED_LINES;
1601 + fse->max_height = fse->min_height;
1607 +static void imx519_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1609 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1610 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1611 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1614 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1617 +static void imx519_update_image_pad_format(struct imx519 *imx519,
1618 + const struct imx519_mode *mode,
1619 + struct v4l2_subdev_format *fmt)
1621 + fmt->format.width = mode->width;
1622 + fmt->format.height = mode->height;
1623 + fmt->format.field = V4L2_FIELD_NONE;
1624 + imx519_reset_colorspace(&fmt->format);
1627 +static void imx519_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1629 + fmt->format.width = IMX519_EMBEDDED_LINE_WIDTH;
1630 + fmt->format.height = IMX519_NUM_EMBEDDED_LINES;
1631 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1632 + fmt->format.field = V4L2_FIELD_NONE;
1635 +static int imx519_get_pad_format(struct v4l2_subdev *sd,
1636 + struct v4l2_subdev_state *sd_state,
1637 + struct v4l2_subdev_format *fmt)
1639 + struct imx519 *imx519 = to_imx519(sd);
1641 + if (fmt->pad >= NUM_PADS)
1644 + mutex_lock(&imx519->mutex);
1646 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1647 + struct v4l2_mbus_framefmt *try_fmt =
1648 + v4l2_subdev_state_get_format(sd_state,
1650 + /* update the code which could change due to vflip or hflip: */
1651 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1652 + imx519_get_format_code(imx519) :
1653 + MEDIA_BUS_FMT_SENSOR_DATA;
1654 + fmt->format = *try_fmt;
1656 + if (fmt->pad == IMAGE_PAD) {
1657 + imx519_update_image_pad_format(imx519, imx519->mode,
1659 + fmt->format.code =
1660 + imx519_get_format_code(imx519);
1662 + imx519_update_metadata_pad_format(fmt);
1666 + mutex_unlock(&imx519->mutex);
1671 +unsigned int imx519_get_frame_length(const struct imx519_mode *mode,
1672 + const struct v4l2_fract *timeperframe)
1676 + frame_length = (u64)timeperframe->numerator * IMX519_PIXEL_RATE;
1677 + do_div(frame_length,
1678 + (u64)timeperframe->denominator * mode->line_length_pix);
1680 + if (WARN_ON(frame_length > IMX519_FRAME_LENGTH_MAX))
1681 + frame_length = IMX519_FRAME_LENGTH_MAX;
1683 + return max_t(unsigned int, frame_length, mode->height);
1686 +static void imx519_set_framing_limits(struct imx519 *imx519)
1688 + unsigned int frm_length_min, frm_length_default, hblank;
1689 + const struct imx519_mode *mode = imx519->mode;
1691 + frm_length_min = imx519_get_frame_length(mode, &mode->timeperframe_min);
1692 + frm_length_default =
1693 + imx519_get_frame_length(mode, &mode->timeperframe_default);
1695 + /* Default to no long exposure multiplier. */
1696 + imx519->long_exp_shift = 0;
1698 + /* Update limits and set FPS to default */
1699 + __v4l2_ctrl_modify_range(imx519->vblank, frm_length_min - mode->height,
1700 + ((1 << IMX519_LONG_EXP_SHIFT_MAX) *
1701 + IMX519_FRAME_LENGTH_MAX) - mode->height,
1702 + 1, frm_length_default - mode->height);
1704 + /* Setting this will adjust the exposure limits as well. */
1705 + __v4l2_ctrl_s_ctrl(imx519->vblank, frm_length_default - mode->height);
1708 + * Currently PPL is fixed to the mode specified value, so hblank
1709 + * depends on mode->width only, and is not changeable in any
1710 + * way other than changing the mode.
1712 + hblank = mode->line_length_pix - mode->width;
1713 + __v4l2_ctrl_modify_range(imx519->hblank, hblank, hblank, 1, hblank);
1716 +static int imx519_set_pad_format(struct v4l2_subdev *sd,
1717 + struct v4l2_subdev_state *sd_state,
1718 + struct v4l2_subdev_format *fmt)
1720 + struct v4l2_mbus_framefmt *framefmt;
1721 + const struct imx519_mode *mode;
1722 + struct imx519 *imx519 = to_imx519(sd);
1724 + if (fmt->pad >= NUM_PADS)
1727 + mutex_lock(&imx519->mutex);
1729 + if (fmt->pad == IMAGE_PAD) {
1730 + /* Bayer order varies with flips */
1731 + fmt->format.code = imx519_get_format_code(imx519);
1733 + mode = v4l2_find_nearest_size(supported_modes_10bit,
1734 + ARRAY_SIZE(supported_modes_10bit),
1736 + fmt->format.width,
1737 + fmt->format.height);
1738 + imx519_update_image_pad_format(imx519, mode, fmt);
1739 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1740 + framefmt = v4l2_subdev_state_get_format(sd_state,
1742 + *framefmt = fmt->format;
1744 + imx519->mode = mode;
1745 + imx519->fmt_code = fmt->format.code;
1746 + imx519_set_framing_limits(imx519);
1749 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1750 + framefmt = v4l2_subdev_state_get_format(sd_state,
1752 + *framefmt = fmt->format;
1754 + /* Only one embedded data mode is supported */
1755 + imx519_update_metadata_pad_format(fmt);
1759 + mutex_unlock(&imx519->mutex);
1764 +static const struct v4l2_rect *
1765 +__imx519_get_pad_crop(struct imx519 *imx519, struct v4l2_subdev_state *sd_state,
1766 + unsigned int pad, enum v4l2_subdev_format_whence which)
1769 + case V4L2_SUBDEV_FORMAT_TRY:
1770 + return v4l2_subdev_state_get_crop(sd_state, pad);
1771 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1772 + return &imx519->mode->crop;
1778 +static int imx519_get_selection(struct v4l2_subdev *sd,
1779 + struct v4l2_subdev_state *sd_state,
1780 + struct v4l2_subdev_selection *sel)
1782 + switch (sel->target) {
1783 + case V4L2_SEL_TGT_CROP: {
1784 + struct imx519 *imx519 = to_imx519(sd);
1786 + mutex_lock(&imx519->mutex);
1787 + sel->r = *__imx519_get_pad_crop(imx519, sd_state, sel->pad,
1789 + mutex_unlock(&imx519->mutex);
1794 + case V4L2_SEL_TGT_NATIVE_SIZE:
1797 + sel->r.width = IMX519_NATIVE_WIDTH;
1798 + sel->r.height = IMX519_NATIVE_HEIGHT;
1802 + case V4L2_SEL_TGT_CROP_DEFAULT:
1803 + case V4L2_SEL_TGT_CROP_BOUNDS:
1804 + sel->r.left = IMX519_PIXEL_ARRAY_LEFT;
1805 + sel->r.top = IMX519_PIXEL_ARRAY_TOP;
1806 + sel->r.width = IMX519_PIXEL_ARRAY_WIDTH;
1807 + sel->r.height = IMX519_PIXEL_ARRAY_HEIGHT;
1815 +/* Start streaming */
1816 +static int imx519_start_streaming(struct imx519 *imx519)
1818 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1819 + const struct imx519_reg_list *reg_list;
1822 + if (!imx519->common_regs_written) {
1823 + ret = imx519_write_regs(imx519, mode_common_regs,
1824 + ARRAY_SIZE(mode_common_regs));
1827 + dev_err(&client->dev, "%s failed to set common settings\n",
1831 + imx519->common_regs_written = true;
1834 + /* Apply default values of current mode */
1835 + reg_list = &imx519->mode->reg_list;
1836 + ret = imx519_write_regs(imx519, reg_list->regs, reg_list->num_of_regs);
1838 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
1842 + /* Apply customized values from user */
1843 + ret = __v4l2_ctrl_handler_setup(imx519->sd.ctrl_handler);
1847 + /* set stream on register */
1848 + return imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1849 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STREAMING);
1852 +/* Stop streaming */
1853 +static void imx519_stop_streaming(struct imx519 *imx519)
1855 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1858 + /* set stream off register */
1859 + ret = imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1860 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STANDBY);
1862 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
1865 +static int imx519_set_stream(struct v4l2_subdev *sd, int enable)
1867 + struct imx519 *imx519 = to_imx519(sd);
1868 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1871 + mutex_lock(&imx519->mutex);
1872 + if (imx519->streaming == enable) {
1873 + mutex_unlock(&imx519->mutex);
1878 + ret = pm_runtime_get_sync(&client->dev);
1880 + pm_runtime_put_noidle(&client->dev);
1885 + * Apply default & customized values
1886 + * and then start streaming.
1888 + ret = imx519_start_streaming(imx519);
1892 + imx519_stop_streaming(imx519);
1893 + pm_runtime_put(&client->dev);
1896 + imx519->streaming = enable;
1898 + /* vflip and hflip cannot change during streaming */
1899 + __v4l2_ctrl_grab(imx519->vflip, enable);
1900 + __v4l2_ctrl_grab(imx519->hflip, enable);
1902 + mutex_unlock(&imx519->mutex);
1907 + pm_runtime_put(&client->dev);
1909 + mutex_unlock(&imx519->mutex);
1914 +/* Power/clock management functions */
1915 +static int imx519_power_on(struct device *dev)
1917 + struct i2c_client *client = to_i2c_client(dev);
1918 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1919 + struct imx519 *imx519 = to_imx519(sd);
1922 + ret = regulator_bulk_enable(IMX519_NUM_SUPPLIES,
1923 + imx519->supplies);
1925 + dev_err(&client->dev, "%s: failed to enable regulators\n",
1930 + ret = clk_prepare_enable(imx519->xclk);
1932 + dev_err(&client->dev, "%s: failed to enable clock\n",
1937 + gpiod_set_value_cansleep(imx519->reset_gpio, 1);
1938 + usleep_range(IMX519_XCLR_MIN_DELAY_US,
1939 + IMX519_XCLR_MIN_DELAY_US + IMX519_XCLR_DELAY_RANGE_US);
1944 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1948 +static int imx519_power_off(struct device *dev)
1950 + struct i2c_client *client = to_i2c_client(dev);
1951 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1952 + struct imx519 *imx519 = to_imx519(sd);
1954 + gpiod_set_value_cansleep(imx519->reset_gpio, 0);
1955 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1956 + clk_disable_unprepare(imx519->xclk);
1958 + /* Force reprogramming of the common registers when powered up again. */
1959 + imx519->common_regs_written = false;
1964 +static int __maybe_unused imx519_suspend(struct device *dev)
1966 + struct i2c_client *client = to_i2c_client(dev);
1967 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1968 + struct imx519 *imx519 = to_imx519(sd);
1970 + if (imx519->streaming)
1971 + imx519_stop_streaming(imx519);
1976 +static int __maybe_unused imx519_resume(struct device *dev)
1978 + struct i2c_client *client = to_i2c_client(dev);
1979 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1980 + struct imx519 *imx519 = to_imx519(sd);
1983 + if (imx519->streaming) {
1984 + ret = imx519_start_streaming(imx519);
1992 + imx519_stop_streaming(imx519);
1993 + imx519->streaming = 0;
1997 +static int imx519_get_regulators(struct imx519 *imx519)
1999 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
2002 + for (i = 0; i < IMX519_NUM_SUPPLIES; i++)
2003 + imx519->supplies[i].supply = imx519_supply_name[i];
2005 + return devm_regulator_bulk_get(&client->dev,
2006 + IMX519_NUM_SUPPLIES,
2007 + imx519->supplies);
2010 +/* Verify chip ID */
2011 +static int imx519_identify_module(struct imx519 *imx519, u32 expected_id)
2013 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
2017 + ret = imx519_read_reg(imx519, IMX519_REG_CHIP_ID,
2018 + IMX519_REG_VALUE_16BIT, &val);
2020 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
2021 + expected_id, ret);
2025 + if (val != expected_id) {
2026 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2027 + expected_id, val);
2031 + dev_info(&client->dev, "Device found is imx%x\n", val);
2036 +static const struct v4l2_subdev_core_ops imx519_core_ops = {
2037 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2038 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2041 +static const struct v4l2_subdev_video_ops imx519_video_ops = {
2042 + .s_stream = imx519_set_stream,
2045 +static const struct v4l2_subdev_pad_ops imx519_pad_ops = {
2046 + .enum_mbus_code = imx519_enum_mbus_code,
2047 + .get_fmt = imx519_get_pad_format,
2048 + .set_fmt = imx519_set_pad_format,
2049 + .get_selection = imx519_get_selection,
2050 + .enum_frame_size = imx519_enum_frame_size,
2053 +static const struct v4l2_subdev_ops imx519_subdev_ops = {
2054 + .core = &imx519_core_ops,
2055 + .video = &imx519_video_ops,
2056 + .pad = &imx519_pad_ops,
2059 +static const struct v4l2_subdev_internal_ops imx519_internal_ops = {
2060 + .open = imx519_open,
2063 +/* Initialize control handlers */
2064 +static int imx519_init_controls(struct imx519 *imx519)
2066 + struct v4l2_ctrl_handler *ctrl_hdlr;
2067 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
2068 + struct v4l2_fwnode_device_properties props;
2069 + struct v4l2_ctrl *link_freq;
2073 + ctrl_hdlr = &imx519->ctrl_handler;
2074 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2078 + mutex_init(&imx519->mutex);
2079 + ctrl_hdlr->lock = &imx519->mutex;
2081 + /* By default, PIXEL_RATE is read only */
2082 + imx519->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2083 + V4L2_CID_PIXEL_RATE,
2084 + IMX519_PIXEL_RATE,
2085 + IMX519_PIXEL_RATE, 1,
2086 + IMX519_PIXEL_RATE);
2088 + /* LINK_FREQ is also read only */
2090 + v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx519_ctrl_ops,
2091 + V4L2_CID_LINK_FREQ,
2092 + ARRAY_SIZE(imx519_link_freq_menu) - 1, 0,
2093 + imx519_link_freq_menu);
2095 + link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2098 + * Create the controls here, but mode specific limits are setup
2099 + * in the imx519_set_framing_limits() call below.
2101 + imx519->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2102 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2103 + imx519->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2104 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2106 + /* HBLANK is read-only for now, but does change with mode. */
2107 + if (imx519->hblank)
2108 + imx519->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2110 + imx519->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2111 + V4L2_CID_EXPOSURE,
2112 + IMX519_EXPOSURE_MIN,
2113 + IMX519_EXPOSURE_MAX,
2114 + IMX519_EXPOSURE_STEP,
2115 + IMX519_EXPOSURE_DEFAULT);
2117 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2118 + IMX519_ANA_GAIN_MIN, IMX519_ANA_GAIN_MAX,
2119 + IMX519_ANA_GAIN_STEP, IMX519_ANA_GAIN_DEFAULT);
2121 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2122 + IMX519_DGTL_GAIN_MIN, IMX519_DGTL_GAIN_MAX,
2123 + IMX519_DGTL_GAIN_STEP, IMX519_DGTL_GAIN_DEFAULT);
2125 + imx519->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2126 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2127 + if (imx519->hflip)
2128 + imx519->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2130 + imx519->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2131 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2132 + if (imx519->vflip)
2133 + imx519->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2135 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx519_ctrl_ops,
2136 + V4L2_CID_TEST_PATTERN,
2137 + ARRAY_SIZE(imx519_test_pattern_menu) - 1,
2138 + 0, 0, imx519_test_pattern_menu);
2139 + for (i = 0; i < 4; i++) {
2141 + * The assumption is that
2142 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2143 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2144 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2146 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2147 + V4L2_CID_TEST_PATTERN_RED + i,
2148 + IMX519_TEST_PATTERN_COLOUR_MIN,
2149 + IMX519_TEST_PATTERN_COLOUR_MAX,
2150 + IMX519_TEST_PATTERN_COLOUR_STEP,
2151 + IMX519_TEST_PATTERN_COLOUR_MAX);
2152 + /* The "Solid color" pattern is white by default */
2155 + if (ctrl_hdlr->error) {
2156 + ret = ctrl_hdlr->error;
2157 + dev_err(&client->dev, "%s control init failed (%d)\n",
2162 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2166 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx519_ctrl_ops,
2171 + imx519->sd.ctrl_handler = ctrl_hdlr;
2173 + /* Setup exposure and frame/line length limits. */
2174 + imx519_set_framing_limits(imx519);
2179 + v4l2_ctrl_handler_free(ctrl_hdlr);
2180 + mutex_destroy(&imx519->mutex);
2185 +static void imx519_free_controls(struct imx519 *imx519)
2187 + v4l2_ctrl_handler_free(imx519->sd.ctrl_handler);
2188 + mutex_destroy(&imx519->mutex);
2191 +static int imx519_check_hwcfg(struct device *dev)
2193 + struct fwnode_handle *endpoint;
2194 + struct v4l2_fwnode_endpoint ep_cfg = {
2195 + .bus_type = V4L2_MBUS_CSI2_DPHY
2197 + int ret = -EINVAL;
2199 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2201 + dev_err(dev, "endpoint node not found\n");
2205 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2206 + dev_err(dev, "could not parse endpoint\n");
2210 + /* Check the number of MIPI CSI2 data lanes */
2211 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2212 + dev_err(dev, "only 2 data lanes are currently supported\n");
2216 + /* Check the link frequency set in device tree */
2217 + if (!ep_cfg.nr_of_link_frequencies) {
2218 + dev_err(dev, "link-frequency property not found in DT\n");
2222 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2223 + ep_cfg.link_frequencies[0] != IMX519_DEFAULT_LINK_FREQ) {
2224 + dev_err(dev, "Link frequency not supported: %lld\n",
2225 + ep_cfg.link_frequencies[0]);
2232 + v4l2_fwnode_endpoint_free(&ep_cfg);
2233 + fwnode_handle_put(endpoint);
2238 +static const struct of_device_id imx519_dt_ids[] = {
2239 + { .compatible = "sony,imx519"},
2240 + { /* sentinel */ }
2243 +static int imx519_probe(struct i2c_client *client)
2245 + struct device *dev = &client->dev;
2246 + struct imx519 *imx519;
2247 + const struct of_device_id *match;
2251 + imx519 = devm_kzalloc(&client->dev, sizeof(*imx519), GFP_KERNEL);
2255 + v4l2_i2c_subdev_init(&imx519->sd, client, &imx519_subdev_ops);
2257 + match = of_match_device(imx519_dt_ids, dev);
2261 + /* Check the hardware configuration in device tree */
2262 + if (imx519_check_hwcfg(dev))
2265 + /* Get system clock (xclk) */
2266 + imx519->xclk = devm_clk_get(dev, NULL);
2267 + if (IS_ERR(imx519->xclk)) {
2268 + dev_err(dev, "failed to get xclk\n");
2269 + return PTR_ERR(imx519->xclk);
2272 + xclk_freq = clk_get_rate(imx519->xclk);
2273 + if (xclk_freq != IMX519_XCLK_FREQ) {
2274 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2279 + ret = imx519_get_regulators(imx519);
2281 + dev_err(dev, "failed to get regulators\n");
2285 + /* Request optional enable pin */
2286 + imx519->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2290 + * The sensor must be powered for imx519_identify_module()
2291 + * to be able to read the CHIP_ID register
2293 + ret = imx519_power_on(dev);
2297 + ret = imx519_identify_module(imx519, IMX519_CHIP_ID);
2299 + goto error_power_off;
2301 + /* Set default mode to max resolution */
2302 + imx519->mode = &supported_modes_10bit[0];
2303 + imx519->fmt_code = MEDIA_BUS_FMT_SRGGB10_1X10;
2305 + /* Enable runtime PM and turn off the device */
2306 + pm_runtime_set_active(dev);
2307 + pm_runtime_enable(dev);
2308 + pm_runtime_idle(dev);
2310 + /* This needs the pm runtime to be registered. */
2311 + ret = imx519_init_controls(imx519);
2313 + goto error_power_off;
2315 + /* Initialize subdev */
2316 + imx519->sd.internal_ops = &imx519_internal_ops;
2317 + imx519->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2318 + V4L2_SUBDEV_FL_HAS_EVENTS;
2319 + imx519->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2321 + /* Initialize source pads */
2322 + imx519->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2323 + imx519->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2325 + ret = media_entity_pads_init(&imx519->sd.entity, NUM_PADS, imx519->pad);
2327 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2328 + goto error_handler_free;
2331 + ret = v4l2_async_register_subdev_sensor(&imx519->sd);
2333 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2334 + goto error_media_entity;
2339 +error_media_entity:
2340 + media_entity_cleanup(&imx519->sd.entity);
2342 +error_handler_free:
2343 + imx519_free_controls(imx519);
2346 + pm_runtime_disable(&client->dev);
2347 + pm_runtime_set_suspended(&client->dev);
2348 + imx519_power_off(&client->dev);
2353 +static void imx519_remove(struct i2c_client *client)
2355 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2356 + struct imx519 *imx519 = to_imx519(sd);
2358 + v4l2_async_unregister_subdev(sd);
2359 + media_entity_cleanup(&sd->entity);
2360 + imx519_free_controls(imx519);
2362 + pm_runtime_disable(&client->dev);
2363 + if (!pm_runtime_status_suspended(&client->dev))
2364 + imx519_power_off(&client->dev);
2365 + pm_runtime_set_suspended(&client->dev);
2368 +MODULE_DEVICE_TABLE(of, imx519_dt_ids);
2370 +static const struct dev_pm_ops imx519_pm_ops = {
2371 + SET_SYSTEM_SLEEP_PM_OPS(imx519_suspend, imx519_resume)
2372 + SET_RUNTIME_PM_OPS(imx519_power_off, imx519_power_on, NULL)
2375 +static struct i2c_driver imx519_i2c_driver = {
2378 + .of_match_table = imx519_dt_ids,
2379 + .pm = &imx519_pm_ops,
2381 + .probe = imx519_probe,
2382 + .remove = imx519_remove,
2385 +module_i2c_driver(imx519_i2c_driver);
2387 +MODULE_AUTHOR("Lee Jackson <info@arducam.com>");
2388 +MODULE_DESCRIPTION("Sony IMX519 sensor driver");
2389 +MODULE_LICENSE("GPL v2");