1 From 227eddefe9cb07dd1d160f0015b0d17d66cd3cf8 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Thu, 7 May 2020 15:50:54 +0100
4 Subject: [PATCH] media: i2c: imx477: Support for the Sony IMX477
7 dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
9 Add YAML device tree binding for IMX477 CMOS image sensor.
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
13 media: i2c: Add driver for Sony IMX477 sensor
15 Adds a driver for the 12MPix Sony IMX477 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 4056x3040 12-bit @ 10fps
22 2028x1520 12-bit (binned) @ 40fps
23 2028x1050 12-bit (cropped/binned) @ 50fps
24 1012x760 10-bit (scaled) @ 120 fps
26 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
28 media: i2c: imx477: Add support for adaptive frame control
30 Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
31 automatically adjust the sensor frame length based on exposure time,
32 allowing variable frame rates and longer exposures.
34 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
36 media: i2c: imx477: Return correct result on sensor id verification
38 The test should return -EIO if the register read id does not match
39 the expected sensor id.
41 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
43 media: i2c: imx477: Parse and register properties
45 Parse device properties and register controls for them using the V4L2
46 fwnode properties helpers.
48 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
50 media: i2c: imx477: Selection compliance fixes
52 To comply with the intended usage of the V4L2 selection target when
53 used to retrieve a sensor image properties, adjust the rectangles
54 returned by the imx477 driver.
56 The top/left crop coordinates of the TGT_CROP rectangle were set to
57 (0, 0) instead of (8, 16) which is the offset from the larger physical
58 pixel array rectangle. This was also a mismatch with the default values
59 crop rectangle value, so this is corrected. Found with v4l2-compliance.
61 While at it, add V4L2_SEL_TGT_CROP_BOUNDS support: CROP_DEFAULT and
62 CROP_BOUNDS have the same size as the non-active pixels are not readable
63 using the selection API. Found with v4l2-compliance.
65 This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
68 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
70 media: i2c: imx477: Remove auto frame length adjusting
72 The V4L2_CID_EXPOSURE_AUTO_PRIORITY was used to let the sensor control
73 frame length (effectively framerate) based on the requested exposure
74 time requested. Remove this feature as it is never used, and goes
75 against how V4L2 likes to handle exposure and vblank controls.
77 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
79 media: i2c: imx477: Add very long exposure control to the driver
81 Add support for very long exposures by using the exposure multiplier
82 register. Userland does not need to pass any additional controls to
83 enable long exposures, it simply requests a larger vblank to extend the
84 exposure control range appropriately.
86 Currently, since hblank is fixed, a maximum of approximately 124 seconds
87 of exposure time can be used. In a future change, hblank could also be
88 controlled in userland to give over 200 seconds of exposure time.
90 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
92 media: i2c: imx477: Fix crop height for 2028x1080 mode
94 The crop height for this mode was set at 2600 lines, it should be 2160
97 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
99 media: i2c: imx477: Replace existing 1012x760 mode
101 The existing 1012x760 120 fps mode has significant IQ problem using
102 the internal sensor scaler. Replace this mode with a 1332x990 120 fps
103 mode instead. This new mode has a smaller field of view, but does not
104 suffer from the bad IQ of the original mode.
106 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
108 media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
110 The only field in this struct that is used is the format code, so
111 replace the struct with this single field.
113 Save the format code in imx477_set_pad_format() when setting up a new
114 mode so that imx477_get_pad_format() performs the right lookup.
115 Otherwise, this caused a bug where the mode lookup occurred on the
116 12-bit table rather than the 10-bit table.
118 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
120 media: i2c: imx477: Remove unused function parameter
122 The struct imx477 *ctrl parameter is not used in the function
123 imx477_adjust_exposure_range(), so remove it.
125 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
127 media: i2c: imx477: Fix for long exposure limit calculations
129 Do not scale IMX477_EXPOSURE_OFFSET with the long exposure factor during
130 the limit calculations. This allows larger exposure times, and does seem to be
131 what the sensor is doing internally.
133 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
135 media: i2c: imx477: Extend driver to support imx378 sensor
137 The imx378 sensor is almost identical to the imx477 and can be
138 supported as a "compatible" sensor with just a few extra register
141 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
143 media: i2c: imx477: Fix framerates for 1332x990 mode
145 The imx477 driver's line length for this mode had not been updated to
146 the value supplied to us by the sensor manufacturer. With this
147 correction the sensor delivers the framerates that are expected.
149 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
151 media: i2c: imx477: Allow control of on-sensor DPC
153 A module parameter "dpc_enable" is added to allow the control of the
154 sensor's on-board DPC (Defective Pixel Correction) function.
156 This is a global setting to be configured before using the sensor;
157 there is no intention that this would ever be changed on-the-fly.
159 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
161 media: i2c: imx477: Sensor should report RAW color space
163 Tested on Raspberry Pi running libcamera.
165 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
167 media: i2c: imx477: Add vsync trigger_mode parameter
169 trigger_mode == 0 (default) => no effect / no registers written
170 trigger_mode == 1 => source
171 trigger_mode == 2 => sink
173 This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
175 Signed-off-by: Jonas Jacob <jonas.jacob@neocortexvision.com>
177 media: i2c: Update imx477 Kconfig entry
179 Bring the IMX477 Kconfig declaration in line with upstream entries.
181 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
183 media: i2c: imx477: Correct minimum exposure lines
185 The minimum number of exposure lines value (IMX477_EXPOSURE_MIN) was
186 previously 20 but this is not correct. The datasheet is not completely
187 explicit, however the new value of 4 has been tested with all the
188 sensor modes supported by this driver, and matches the lowest exposure
189 value of 114us that could be achieved wtih Raspberry Pi's legacy
192 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
194 media: i2c: imx477: Allow dynamic horizontal blanking control
196 Currently, the V4L2_CID_HBLANK control is marked as read-only. Remove this
197 restriction and allow userland to modify the control if needed.
199 Set the maximum limit of the line length to 0xfff0.
201 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
203 media: i2c: imx477: Reset hblank on mode switch
205 Reset the hblank control to the minimum value on every mode switch. This is to
206 account for userland instances that do not yet control hblank, otherwise it
207 gets set to a non-optimal value.
209 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
211 media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
213 On a mode change, only call imx477_set_framing_limits() to adjust the hblank
214 and vblank limits if the new mode is different from the existing mode. This
215 preserves any manual control values the user might have set.
217 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
219 .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
221 drivers/media/i2c/Kconfig | 11 +
222 drivers/media/i2c/Makefile | 1 +
223 drivers/media/i2c/imx477.c | 2308 +++++++++++++++++
224 5 files changed, 2441 insertions(+)
225 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx477.yaml
226 create mode 100644 drivers/media/i2c/imx477.c
229 +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
231 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
234 +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
235 +$schema: http://devicetree.org/meta-schemas/core.yaml#
237 +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
240 + - Naushir Patuck <naush@raspberypi.com>
243 + The Sony IMX477 is a 1/2.3-inch CMOS active pixel digital image sensor
244 + with an active array size of 4056H x 3040V. It is programmable through
245 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
246 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
254 + description: I2C device address
262 + Digital I/O voltage supply, 1.05 volts
266 + Analog voltage supply, 2.8 volts
270 + Digital core voltage supply, 1.8 volts
274 + Reference to the GPIO connected to the xclr pin, if any.
275 + Must be released (set high) after all all supplies and INCK are applied.
277 + # See ../video-interfaces.txt for more details
286 + The sensor supports either two-lane, or four-lane operation.
287 + For two-lane operation the property must be set to <1 2>.
292 + clock-noncontinuous:
295 + MIPI CSI-2 clock is non-continuous if this property is present,
296 + otherwise it's continuous.
300 + - $ref: /schemas/types.yaml#/definitions/uint64-array
302 + Allowed data bus frequencies.
316 +additionalProperties: false
321 + #address-cells = <1>;
324 + imx477: sensor@10 {
325 + compatible = "sony,imx477";
327 + clocks = <&imx477_clk>;
328 + VANA-supply = <&imx477_vana>; /* 2.8v */
329 + VDIG-supply = <&imx477_vdig>; /* 1.05v */
330 + VDDL-supply = <&imx477_vddl>; /* 1.8v */
333 + imx477_0: endpoint {
334 + remote-endpoint = <&csi1_ep>;
335 + data-lanes = <1 2>;
336 + clock-noncontinuous;
337 + link-frequencies = /bits/ 64 <450000000>;
346 @@ -19281,6 +19281,14 @@ T: git git://linuxtv.org/media_tree.git
347 F: Documentation/devicetree/bindings/media/i2c/sony,imx412.yaml
348 F: drivers/media/i2c/imx412.c
350 +SONY IMX477 SENSOR DRIVER
351 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
352 +L: linux-media@vger.kernel.org
354 +T: git git://linuxtv.org/media_tree.git
355 +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
356 +F: drivers/media/i2c/imx477.c
358 SONY MEMORYSTICK SUBSYSTEM
359 M: Maxim Levitsky <maximlevitsky@gmail.com>
360 M: Alex Dubov <oakad@yahoo.com>
361 --- a/drivers/media/i2c/Kconfig
362 +++ b/drivers/media/i2c/Kconfig
363 @@ -1030,6 +1030,17 @@ config VIDEO_UDA1342
364 To compile this driver as a module, choose M here: the
365 module will be called uda1342.
368 + tristate "Sony IMX477 sensor support"
369 + depends on I2C && VIDEO_DEV
370 + select VIDEO_V4L2_SUBDEV_API
372 + This is a Video4Linux2 sensor driver for the Sony
373 + IMX477 camera. Also supports the Sony IMX378.
375 + To compile this driver as a module, choose M here: the
376 + module will be called imx477.
378 config VIDEO_VP27SMPX
379 tristate "Panasonic VP27's internal MPX"
380 depends on VIDEO_DEV && I2C
381 --- a/drivers/media/i2c/Makefile
382 +++ b/drivers/media/i2c/Makefile
383 @@ -48,6 +48,7 @@ obj-$(CONFIG_VIDEO_IMX334) += imx334.o
384 obj-$(CONFIG_VIDEO_IMX335) += imx335.o
385 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
386 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
387 +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
388 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
389 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
390 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
392 +++ b/drivers/media/i2c/imx477.c
394 +// SPDX-License-Identifier: GPL-2.0
396 + * A V4L2 driver for Sony IMX477 cameras.
397 + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
399 + * Based on Sony imx219 camera driver
400 + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
402 +#include <asm/unaligned.h>
403 +#include <linux/clk.h>
404 +#include <linux/delay.h>
405 +#include <linux/gpio/consumer.h>
406 +#include <linux/i2c.h>
407 +#include <linux/module.h>
408 +#include <linux/of_device.h>
409 +#include <linux/pm_runtime.h>
410 +#include <linux/regulator/consumer.h>
411 +#include <media/v4l2-ctrls.h>
412 +#include <media/v4l2-device.h>
413 +#include <media/v4l2-event.h>
414 +#include <media/v4l2-fwnode.h>
415 +#include <media/v4l2-mediabus.h>
417 +static int dpc_enable = 1;
418 +module_param(dpc_enable, int, 0644);
419 +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
421 +static int trigger_mode;
422 +module_param(trigger_mode, int, 0644);
423 +MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
425 +#define IMX477_REG_VALUE_08BIT 1
426 +#define IMX477_REG_VALUE_16BIT 2
429 +#define IMX477_REG_CHIP_ID 0x0016
430 +#define IMX477_CHIP_ID 0x0477
431 +#define IMX378_CHIP_ID 0x0378
433 +#define IMX477_REG_MODE_SELECT 0x0100
434 +#define IMX477_MODE_STANDBY 0x00
435 +#define IMX477_MODE_STREAMING 0x01
437 +#define IMX477_REG_ORIENTATION 0x101
439 +#define IMX477_XCLK_FREQ 24000000
441 +#define IMX477_DEFAULT_LINK_FREQ 450000000
443 +/* Pixel rate is fixed at 840MHz for all the modes */
444 +#define IMX477_PIXEL_RATE 840000000
446 +/* V_TIMING internal */
447 +#define IMX477_REG_FRAME_LENGTH 0x0340
448 +#define IMX477_FRAME_LENGTH_MAX 0xffdc
450 +/* H_TIMING internal */
451 +#define IMX477_REG_LINE_LENGTH 0x0342
452 +#define IMX477_LINE_LENGTH_MAX 0xfff0
454 +/* Long exposure multiplier */
455 +#define IMX477_LONG_EXP_SHIFT_MAX 7
456 +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
458 +/* Exposure control */
459 +#define IMX477_REG_EXPOSURE 0x0202
460 +#define IMX477_EXPOSURE_OFFSET 22
461 +#define IMX477_EXPOSURE_MIN 4
462 +#define IMX477_EXPOSURE_STEP 1
463 +#define IMX477_EXPOSURE_DEFAULT 0x640
464 +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
465 + IMX477_EXPOSURE_OFFSET)
467 +/* Analog gain control */
468 +#define IMX477_REG_ANALOG_GAIN 0x0204
469 +#define IMX477_ANA_GAIN_MIN 0
470 +#define IMX477_ANA_GAIN_MAX 978
471 +#define IMX477_ANA_GAIN_STEP 1
472 +#define IMX477_ANA_GAIN_DEFAULT 0x0
474 +/* Digital gain control */
475 +#define IMX477_REG_DIGITAL_GAIN 0x020e
476 +#define IMX477_DGTL_GAIN_MIN 0x0100
477 +#define IMX477_DGTL_GAIN_MAX 0xffff
478 +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
479 +#define IMX477_DGTL_GAIN_STEP 1
481 +/* Test Pattern Control */
482 +#define IMX477_REG_TEST_PATTERN 0x0600
483 +#define IMX477_TEST_PATTERN_DISABLE 0
484 +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
485 +#define IMX477_TEST_PATTERN_COLOR_BARS 2
486 +#define IMX477_TEST_PATTERN_GREY_COLOR 3
487 +#define IMX477_TEST_PATTERN_PN9 4
489 +/* Test pattern colour components */
490 +#define IMX477_REG_TEST_PATTERN_R 0x0602
491 +#define IMX477_REG_TEST_PATTERN_GR 0x0604
492 +#define IMX477_REG_TEST_PATTERN_B 0x0606
493 +#define IMX477_REG_TEST_PATTERN_GB 0x0608
494 +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
495 +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
496 +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
497 +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
498 +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
499 +#define IMX477_TEST_PATTERN_B_DEFAULT 0
500 +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
503 +#define IMX477_REG_MC_MODE 0x3f0b
504 +#define IMX477_REG_MS_SEL 0x3041
505 +#define IMX477_REG_XVS_IO_CTRL 0x3040
506 +#define IMX477_REG_EXTOUT_EN 0x4b81
508 +/* Embedded metadata stream structure */
509 +#define IMX477_EMBEDDED_LINE_WIDTH 16384
510 +#define IMX477_NUM_EMBEDDED_LINES 1
518 +/* IMX477 native and active pixel array size. */
519 +#define IMX477_NATIVE_WIDTH 4072U
520 +#define IMX477_NATIVE_HEIGHT 3176U
521 +#define IMX477_PIXEL_ARRAY_LEFT 8U
522 +#define IMX477_PIXEL_ARRAY_TOP 16U
523 +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
524 +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
531 +struct imx477_reg_list {
532 + unsigned int num_of_regs;
533 + const struct imx477_reg *regs;
536 +/* Mode : resolution and related config&values */
537 +struct imx477_mode {
539 + unsigned int width;
542 + unsigned int height;
544 + /* H-timing in pixels */
545 + unsigned int line_length_pix;
547 + /* Analog crop rectangle. */
548 + struct v4l2_rect crop;
550 + /* Highest possible framerate. */
551 + struct v4l2_fract timeperframe_min;
553 + /* Default framerate. */
554 + struct v4l2_fract timeperframe_default;
556 + /* Default register values */
557 + struct imx477_reg_list reg_list;
560 +static const struct imx477_reg mode_common_regs[] = {
872 +static const struct imx477_reg mode_4056x3040_regs[] = {
983 +/* 2x2 binned. 40fps */
984 +static const struct imx477_reg mode_2028x1520_regs[] = {
1084 +/* 1080p cropped mode */
1085 +static const struct imx477_reg mode_2028x1080_regs[] = {
1185 +/* 4x4 binned. 120fps */
1186 +static const struct imx477_reg mode_1332x990_regs[] = {
1316 +static const struct imx477_mode supported_modes_12bit[] = {
1318 + /* 12MPix 10fps mode */
1321 + .line_length_pix = 0x5dc0,
1323 + .left = IMX477_PIXEL_ARRAY_LEFT,
1324 + .top = IMX477_PIXEL_ARRAY_TOP,
1328 + .timeperframe_min = {
1330 + .denominator = 1000
1332 + .timeperframe_default = {
1334 + .denominator = 1000
1337 + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
1338 + .regs = mode_4056x3040_regs,
1342 + /* 2x2 binned 40fps mode */
1345 + .line_length_pix = 0x31c4,
1347 + .left = IMX477_PIXEL_ARRAY_LEFT,
1348 + .top = IMX477_PIXEL_ARRAY_TOP,
1352 + .timeperframe_min = {
1354 + .denominator = 4000
1356 + .timeperframe_default = {
1358 + .denominator = 3000
1361 + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
1362 + .regs = mode_2028x1520_regs,
1366 + /* 1080p 50fps cropped mode */
1369 + .line_length_pix = 0x31c4,
1371 + .left = IMX477_PIXEL_ARRAY_LEFT,
1372 + .top = IMX477_PIXEL_ARRAY_TOP + 440,
1376 + .timeperframe_min = {
1378 + .denominator = 5000
1380 + .timeperframe_default = {
1382 + .denominator = 3000
1385 + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
1386 + .regs = mode_2028x1080_regs,
1391 +static const struct imx477_mode supported_modes_10bit[] = {
1393 + /* 120fps. 2x2 binned and cropped */
1396 + .line_length_pix = 6664,
1399 + * FIXME: the analog crop rectangle is actually
1400 + * programmed with a horizontal displacement of 0
1401 + * pixels, not 4. It gets shrunk after going through
1402 + * the scaler. Move this information to the compose
1403 + * rectangle once the driver is expanded to represent
1404 + * its processing blocks with multiple subdevs.
1406 + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1407 + .top = IMX477_PIXEL_ARRAY_TOP + 528,
1411 + .timeperframe_min = {
1413 + .denominator = 12000
1415 + .timeperframe_default = {
1417 + .denominator = 12000
1420 + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1421 + .regs = mode_1332x990_regs,
1427 + * The supported formats.
1428 + * This table MUST contain 4 entries per format, to cover the various flip
1429 + * combinations in the order
1435 +static const u32 codes[] = {
1436 + /* 12-bit modes. */
1437 + MEDIA_BUS_FMT_SRGGB12_1X12,
1438 + MEDIA_BUS_FMT_SGRBG12_1X12,
1439 + MEDIA_BUS_FMT_SGBRG12_1X12,
1440 + MEDIA_BUS_FMT_SBGGR12_1X12,
1441 + /* 10-bit modes. */
1442 + MEDIA_BUS_FMT_SRGGB10_1X10,
1443 + MEDIA_BUS_FMT_SGRBG10_1X10,
1444 + MEDIA_BUS_FMT_SGBRG10_1X10,
1445 + MEDIA_BUS_FMT_SBGGR10_1X10,
1448 +static const char * const imx477_test_pattern_menu[] = {
1452 + "Grey Color Bars",
1456 +static const int imx477_test_pattern_val[] = {
1457 + IMX477_TEST_PATTERN_DISABLE,
1458 + IMX477_TEST_PATTERN_COLOR_BARS,
1459 + IMX477_TEST_PATTERN_SOLID_COLOR,
1460 + IMX477_TEST_PATTERN_GREY_COLOR,
1461 + IMX477_TEST_PATTERN_PN9,
1464 +/* regulator supplies */
1465 +static const char * const imx477_supply_name[] = {
1466 + /* Supplies can be enabled in any order */
1467 + "VANA", /* Analog (2.8V) supply */
1468 + "VDIG", /* Digital Core (1.05V) supply */
1469 + "VDDL", /* IF (1.8V) supply */
1472 +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1475 + * Initialisation delay between XCLR low->high and the moment when the sensor
1476 + * can start capture (i.e. can leave software standby), given by T7 in the
1477 + * datasheet is 8ms. This does include I2C setup time as well.
1479 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1480 + * in the datasheet) is much smaller - 600us.
1482 +#define IMX477_XCLR_MIN_DELAY_US 8000
1483 +#define IMX477_XCLR_DELAY_RANGE_US 1000
1485 +struct imx477_compatible_data {
1486 + unsigned int chip_id;
1487 + struct imx477_reg_list extra_regs;
1491 + struct v4l2_subdev sd;
1492 + struct media_pad pad[NUM_PADS];
1494 + unsigned int fmt_code;
1499 + struct gpio_desc *reset_gpio;
1500 + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1502 + struct v4l2_ctrl_handler ctrl_handler;
1503 + /* V4L2 Controls */
1504 + struct v4l2_ctrl *pixel_rate;
1505 + struct v4l2_ctrl *exposure;
1506 + struct v4l2_ctrl *vflip;
1507 + struct v4l2_ctrl *hflip;
1508 + struct v4l2_ctrl *vblank;
1509 + struct v4l2_ctrl *hblank;
1511 + /* Current mode */
1512 + const struct imx477_mode *mode;
1515 + * Mutex for serialized access:
1516 + * Protect sensor module set pad format and start/stop streaming safely.
1518 + struct mutex mutex;
1520 + /* Streaming on/off */
1523 + /* Rewrite common registers on stream on? */
1524 + bool common_regs_written;
1526 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1527 + unsigned int long_exp_shift;
1529 + /* Any extra information related to different compatible sensors */
1530 + const struct imx477_compatible_data *compatible_data;
1533 +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1535 + return container_of(_sd, struct imx477, sd);
1538 +static inline void get_mode_table(unsigned int code,
1539 + const struct imx477_mode **mode_list,
1540 + unsigned int *num_modes)
1544 + case MEDIA_BUS_FMT_SRGGB12_1X12:
1545 + case MEDIA_BUS_FMT_SGRBG12_1X12:
1546 + case MEDIA_BUS_FMT_SGBRG12_1X12:
1547 + case MEDIA_BUS_FMT_SBGGR12_1X12:
1548 + *mode_list = supported_modes_12bit;
1549 + *num_modes = ARRAY_SIZE(supported_modes_12bit);
1552 + case MEDIA_BUS_FMT_SRGGB10_1X10:
1553 + case MEDIA_BUS_FMT_SGRBG10_1X10:
1554 + case MEDIA_BUS_FMT_SGBRG10_1X10:
1555 + case MEDIA_BUS_FMT_SBGGR10_1X10:
1556 + *mode_list = supported_modes_10bit;
1557 + *num_modes = ARRAY_SIZE(supported_modes_10bit);
1560 + *mode_list = NULL;
1565 +/* Read registers up to 2 at a time */
1566 +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1568 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1569 + struct i2c_msg msgs[2];
1570 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1571 + u8 data_buf[4] = { 0, };
1577 + /* Write register address */
1578 + msgs[0].addr = client->addr;
1579 + msgs[0].flags = 0;
1580 + msgs[0].len = ARRAY_SIZE(addr_buf);
1581 + msgs[0].buf = addr_buf;
1583 + /* Read data from register */
1584 + msgs[1].addr = client->addr;
1585 + msgs[1].flags = I2C_M_RD;
1586 + msgs[1].len = len;
1587 + msgs[1].buf = &data_buf[4 - len];
1589 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1590 + if (ret != ARRAY_SIZE(msgs))
1593 + *val = get_unaligned_be32(data_buf);
1598 +/* Write registers up to 2 at a time */
1599 +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1601 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1607 + put_unaligned_be16(reg, buf);
1608 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1609 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1615 +/* Write a list of registers */
1616 +static int imx477_write_regs(struct imx477 *imx477,
1617 + const struct imx477_reg *regs, u32 len)
1619 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1623 + for (i = 0; i < len; i++) {
1624 + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1626 + dev_err_ratelimited(&client->dev,
1627 + "Failed to write reg 0x%4.4x. error = %d\n",
1628 + regs[i].address, ret);
1637 +/* Get bayer order based on flip setting. */
1638 +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1642 + lockdep_assert_held(&imx477->mutex);
1644 + for (i = 0; i < ARRAY_SIZE(codes); i++)
1645 + if (codes[i] == code)
1648 + if (i >= ARRAY_SIZE(codes))
1651 + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1652 + (imx477->hflip->val ? 1 : 0);
1657 +static void imx477_set_default_format(struct imx477 *imx477)
1659 + /* Set default mode to max resolution */
1660 + imx477->mode = &supported_modes_12bit[0];
1661 + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1664 +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1666 + struct imx477 *imx477 = to_imx477(sd);
1667 + struct v4l2_mbus_framefmt *try_fmt_img =
1668 + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
1669 + struct v4l2_mbus_framefmt *try_fmt_meta =
1670 + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
1671 + struct v4l2_rect *try_crop;
1673 + mutex_lock(&imx477->mutex);
1675 + /* Initialize try_fmt for the image pad */
1676 + try_fmt_img->width = supported_modes_12bit[0].width;
1677 + try_fmt_img->height = supported_modes_12bit[0].height;
1678 + try_fmt_img->code = imx477_get_format_code(imx477,
1679 + MEDIA_BUS_FMT_SRGGB12_1X12);
1680 + try_fmt_img->field = V4L2_FIELD_NONE;
1682 + /* Initialize try_fmt for the embedded metadata pad */
1683 + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1684 + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1685 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1686 + try_fmt_meta->field = V4L2_FIELD_NONE;
1688 + /* Initialize try_crop */
1689 + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
1690 + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1691 + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1692 + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1693 + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1695 + mutex_unlock(&imx477->mutex);
1700 +static void imx477_adjust_exposure_range(struct imx477 *imx477)
1702 + int exposure_max, exposure_def;
1704 + /* Honour the VBLANK limits when setting exposure. */
1705 + exposure_max = imx477->mode->height + imx477->vblank->val -
1706 + IMX477_EXPOSURE_OFFSET;
1707 + exposure_def = min(exposure_max, imx477->exposure->val);
1708 + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1709 + exposure_max, imx477->exposure->step,
1713 +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1717 + imx477->long_exp_shift = 0;
1719 + while (val > IMX477_FRAME_LENGTH_MAX) {
1720 + imx477->long_exp_shift++;
1724 + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1725 + IMX477_REG_VALUE_16BIT, val);
1729 + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1730 + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1733 +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1735 + struct imx477 *imx477 =
1736 + container_of(ctrl->handler, struct imx477, ctrl_handler);
1737 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1741 + * The VBLANK control may change the limits of usable exposure, so check
1742 + * and adjust if necessary.
1744 + if (ctrl->id == V4L2_CID_VBLANK)
1745 + imx477_adjust_exposure_range(imx477);
1748 + * Applying V4L2 control value only happens
1749 + * when power is up for streaming
1751 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1754 + switch (ctrl->id) {
1755 + case V4L2_CID_ANALOGUE_GAIN:
1756 + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1757 + IMX477_REG_VALUE_16BIT, ctrl->val);
1759 + case V4L2_CID_EXPOSURE:
1760 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1761 + IMX477_REG_VALUE_16BIT, ctrl->val >>
1762 + imx477->long_exp_shift);
1764 + case V4L2_CID_DIGITAL_GAIN:
1765 + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1766 + IMX477_REG_VALUE_16BIT, ctrl->val);
1768 + case V4L2_CID_TEST_PATTERN:
1769 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1770 + IMX477_REG_VALUE_16BIT,
1771 + imx477_test_pattern_val[ctrl->val]);
1773 + case V4L2_CID_TEST_PATTERN_RED:
1774 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1775 + IMX477_REG_VALUE_16BIT, ctrl->val);
1777 + case V4L2_CID_TEST_PATTERN_GREENR:
1778 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1779 + IMX477_REG_VALUE_16BIT, ctrl->val);
1781 + case V4L2_CID_TEST_PATTERN_BLUE:
1782 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1783 + IMX477_REG_VALUE_16BIT, ctrl->val);
1785 + case V4L2_CID_TEST_PATTERN_GREENB:
1786 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1787 + IMX477_REG_VALUE_16BIT, ctrl->val);
1789 + case V4L2_CID_HFLIP:
1790 + case V4L2_CID_VFLIP:
1791 + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1792 + imx477->hflip->val |
1793 + imx477->vflip->val << 1);
1795 + case V4L2_CID_VBLANK:
1796 + ret = imx477_set_frame_length(imx477,
1797 + imx477->mode->height + ctrl->val);
1799 + case V4L2_CID_HBLANK:
1800 + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
1801 + imx477->mode->width + ctrl->val);
1804 + dev_info(&client->dev,
1805 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1806 + ctrl->id, ctrl->val);
1811 + pm_runtime_put(&client->dev);
1816 +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1817 + .s_ctrl = imx477_set_ctrl,
1820 +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1821 + struct v4l2_subdev_pad_config *cfg,
1822 + struct v4l2_subdev_mbus_code_enum *code)
1824 + struct imx477 *imx477 = to_imx477(sd);
1826 + if (code->pad >= NUM_PADS)
1829 + if (code->pad == IMAGE_PAD) {
1830 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1833 + code->code = imx477_get_format_code(imx477,
1834 + codes[code->index * 4]);
1836 + if (code->index > 0)
1839 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1845 +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1846 + struct v4l2_subdev_pad_config *cfg,
1847 + struct v4l2_subdev_frame_size_enum *fse)
1849 + struct imx477 *imx477 = to_imx477(sd);
1851 + if (fse->pad >= NUM_PADS)
1854 + if (fse->pad == IMAGE_PAD) {
1855 + const struct imx477_mode *mode_list;
1856 + unsigned int num_modes;
1858 + get_mode_table(fse->code, &mode_list, &num_modes);
1860 + if (fse->index >= num_modes)
1863 + if (fse->code != imx477_get_format_code(imx477, fse->code))
1866 + fse->min_width = mode_list[fse->index].width;
1867 + fse->max_width = fse->min_width;
1868 + fse->min_height = mode_list[fse->index].height;
1869 + fse->max_height = fse->min_height;
1871 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1874 + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1875 + fse->max_width = fse->min_width;
1876 + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1877 + fse->max_height = fse->min_height;
1883 +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1885 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1886 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1887 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1890 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1893 +static void imx477_update_image_pad_format(struct imx477 *imx477,
1894 + const struct imx477_mode *mode,
1895 + struct v4l2_subdev_format *fmt)
1897 + fmt->format.width = mode->width;
1898 + fmt->format.height = mode->height;
1899 + fmt->format.field = V4L2_FIELD_NONE;
1900 + imx477_reset_colorspace(&fmt->format);
1903 +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1905 + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1906 + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1907 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1908 + fmt->format.field = V4L2_FIELD_NONE;
1911 +static int imx477_get_pad_format(struct v4l2_subdev *sd,
1912 + struct v4l2_subdev_pad_config *cfg,
1913 + struct v4l2_subdev_format *fmt)
1915 + struct imx477 *imx477 = to_imx477(sd);
1917 + if (fmt->pad >= NUM_PADS)
1920 + mutex_lock(&imx477->mutex);
1922 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1923 + struct v4l2_mbus_framefmt *try_fmt =
1924 + v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
1925 + /* update the code which could change due to vflip or hflip: */
1926 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1927 + imx477_get_format_code(imx477, try_fmt->code) :
1928 + MEDIA_BUS_FMT_SENSOR_DATA;
1929 + fmt->format = *try_fmt;
1931 + if (fmt->pad == IMAGE_PAD) {
1932 + imx477_update_image_pad_format(imx477, imx477->mode,
1934 + fmt->format.code =
1935 + imx477_get_format_code(imx477, imx477->fmt_code);
1937 + imx477_update_metadata_pad_format(fmt);
1941 + mutex_unlock(&imx477->mutex);
1946 +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
1947 + const struct v4l2_fract *timeperframe)
1951 + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
1952 + do_div(frame_length,
1953 + (u64)timeperframe->denominator * mode->line_length_pix);
1955 + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
1956 + frame_length = IMX477_FRAME_LENGTH_MAX;
1958 + return max_t(unsigned int, frame_length, mode->height);
1961 +static void imx477_set_framing_limits(struct imx477 *imx477)
1963 + unsigned int frm_length_min, frm_length_default, hblank_min;
1964 + const struct imx477_mode *mode = imx477->mode;
1966 + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
1967 + frm_length_default =
1968 + imx477_get_frame_length(mode, &mode->timeperframe_default);
1970 + /* Default to no long exposure multiplier. */
1971 + imx477->long_exp_shift = 0;
1973 + /* Update limits and set FPS to default */
1974 + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
1975 + ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
1976 + IMX477_FRAME_LENGTH_MAX) - mode->height,
1977 + 1, frm_length_default - mode->height);
1979 + /* Setting this will adjust the exposure limits as well. */
1980 + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
1982 + hblank_min = mode->line_length_pix - mode->width;
1983 + __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
1984 + IMX477_LINE_LENGTH_MAX, 1, hblank_min);
1985 + __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
1988 +static int imx477_set_pad_format(struct v4l2_subdev *sd,
1989 + struct v4l2_subdev_pad_config *cfg,
1990 + struct v4l2_subdev_format *fmt)
1992 + struct v4l2_mbus_framefmt *framefmt;
1993 + const struct imx477_mode *mode;
1994 + struct imx477 *imx477 = to_imx477(sd);
1996 + if (fmt->pad >= NUM_PADS)
1999 + mutex_lock(&imx477->mutex);
2001 + if (fmt->pad == IMAGE_PAD) {
2002 + const struct imx477_mode *mode_list;
2003 + unsigned int num_modes;
2005 + /* Bayer order varies with flips */
2006 + fmt->format.code = imx477_get_format_code(imx477,
2007 + fmt->format.code);
2009 + get_mode_table(fmt->format.code, &mode_list, &num_modes);
2011 + mode = v4l2_find_nearest_size(mode_list,
2014 + fmt->format.width,
2015 + fmt->format.height);
2016 + imx477_update_image_pad_format(imx477, mode, fmt);
2017 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2018 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
2020 + *framefmt = fmt->format;
2021 + } else if (imx477->mode != mode) {
2022 + imx477->mode = mode;
2023 + imx477->fmt_code = fmt->format.code;
2024 + imx477_set_framing_limits(imx477);
2027 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2028 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
2030 + *framefmt = fmt->format;
2032 + /* Only one embedded data mode is supported */
2033 + imx477_update_metadata_pad_format(fmt);
2037 + mutex_unlock(&imx477->mutex);
2042 +static const struct v4l2_rect *
2043 +__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
2044 + unsigned int pad, enum v4l2_subdev_format_whence which)
2047 + case V4L2_SUBDEV_FORMAT_TRY:
2048 + return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
2049 + case V4L2_SUBDEV_FORMAT_ACTIVE:
2050 + return &imx477->mode->crop;
2056 +static int imx477_get_selection(struct v4l2_subdev *sd,
2057 + struct v4l2_subdev_pad_config *cfg,
2058 + struct v4l2_subdev_selection *sel)
2060 + switch (sel->target) {
2061 + case V4L2_SEL_TGT_CROP: {
2062 + struct imx477 *imx477 = to_imx477(sd);
2064 + mutex_lock(&imx477->mutex);
2065 + sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
2067 + mutex_unlock(&imx477->mutex);
2072 + case V4L2_SEL_TGT_NATIVE_SIZE:
2075 + sel->r.width = IMX477_NATIVE_WIDTH;
2076 + sel->r.height = IMX477_NATIVE_HEIGHT;
2080 + case V4L2_SEL_TGT_CROP_DEFAULT:
2081 + case V4L2_SEL_TGT_CROP_BOUNDS:
2082 + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
2083 + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
2084 + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
2085 + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
2093 +/* Start streaming */
2094 +static int imx477_start_streaming(struct imx477 *imx477)
2096 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2097 + const struct imx477_reg_list *reg_list;
2098 + const struct imx477_reg_list *extra_regs;
2101 + if (!imx477->common_regs_written) {
2102 + ret = imx477_write_regs(imx477, mode_common_regs,
2103 + ARRAY_SIZE(mode_common_regs));
2105 + extra_regs = &imx477->compatible_data->extra_regs;
2106 + ret = imx477_write_regs(imx477, extra_regs->regs,
2107 + extra_regs->num_of_regs);
2111 + dev_err(&client->dev, "%s failed to set common settings\n",
2115 + imx477->common_regs_written = true;
2118 + /* Apply default values of current mode */
2119 + reg_list = &imx477->mode->reg_list;
2120 + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
2122 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
2126 + /* Set on-sensor DPC. */
2127 + imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2128 + imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2130 + /* Set vsync trigger mode */
2131 + if (trigger_mode != 0) {
2132 + /* trigger_mode == 1 for source, 2 for sink */
2133 + const u32 val = (trigger_mode == 1) ? 1 : 0;
2135 + imx477_write_reg(imx477, IMX477_REG_MC_MODE,
2136 + IMX477_REG_VALUE_08BIT, 1);
2137 + imx477_write_reg(imx477, IMX477_REG_MS_SEL,
2138 + IMX477_REG_VALUE_08BIT, val);
2139 + imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
2140 + IMX477_REG_VALUE_08BIT, val);
2141 + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
2142 + IMX477_REG_VALUE_08BIT, val);
2145 + /* Apply customized values from user */
2146 + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
2150 + /* set stream on register */
2151 + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2152 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
2155 +/* Stop streaming */
2156 +static void imx477_stop_streaming(struct imx477 *imx477)
2158 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2161 + /* set stream off register */
2162 + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2163 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
2165 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
2168 +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
2170 + struct imx477 *imx477 = to_imx477(sd);
2171 + struct i2c_client *client = v4l2_get_subdevdata(sd);
2174 + mutex_lock(&imx477->mutex);
2175 + if (imx477->streaming == enable) {
2176 + mutex_unlock(&imx477->mutex);
2181 + ret = pm_runtime_get_sync(&client->dev);
2183 + pm_runtime_put_noidle(&client->dev);
2188 + * Apply default & customized values
2189 + * and then start streaming.
2191 + ret = imx477_start_streaming(imx477);
2195 + imx477_stop_streaming(imx477);
2196 + pm_runtime_put(&client->dev);
2199 + imx477->streaming = enable;
2201 + /* vflip and hflip cannot change during streaming */
2202 + __v4l2_ctrl_grab(imx477->vflip, enable);
2203 + __v4l2_ctrl_grab(imx477->hflip, enable);
2205 + mutex_unlock(&imx477->mutex);
2210 + pm_runtime_put(&client->dev);
2212 + mutex_unlock(&imx477->mutex);
2217 +/* Power/clock management functions */
2218 +static int imx477_power_on(struct device *dev)
2220 + struct i2c_client *client = to_i2c_client(dev);
2221 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2222 + struct imx477 *imx477 = to_imx477(sd);
2225 + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
2226 + imx477->supplies);
2228 + dev_err(&client->dev, "%s: failed to enable regulators\n",
2233 + ret = clk_prepare_enable(imx477->xclk);
2235 + dev_err(&client->dev, "%s: failed to enable clock\n",
2240 + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
2241 + usleep_range(IMX477_XCLR_MIN_DELAY_US,
2242 + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
2247 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2251 +static int imx477_power_off(struct device *dev)
2253 + struct i2c_client *client = to_i2c_client(dev);
2254 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2255 + struct imx477 *imx477 = to_imx477(sd);
2257 + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
2258 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2259 + clk_disable_unprepare(imx477->xclk);
2261 + /* Force reprogramming of the common registers when powered up again. */
2262 + imx477->common_regs_written = false;
2267 +static int __maybe_unused imx477_suspend(struct device *dev)
2269 + struct i2c_client *client = to_i2c_client(dev);
2270 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2271 + struct imx477 *imx477 = to_imx477(sd);
2273 + if (imx477->streaming)
2274 + imx477_stop_streaming(imx477);
2279 +static int __maybe_unused imx477_resume(struct device *dev)
2281 + struct i2c_client *client = to_i2c_client(dev);
2282 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2283 + struct imx477 *imx477 = to_imx477(sd);
2286 + if (imx477->streaming) {
2287 + ret = imx477_start_streaming(imx477);
2295 + imx477_stop_streaming(imx477);
2296 + imx477->streaming = 0;
2300 +static int imx477_get_regulators(struct imx477 *imx477)
2302 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2305 + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
2306 + imx477->supplies[i].supply = imx477_supply_name[i];
2308 + return devm_regulator_bulk_get(&client->dev,
2309 + IMX477_NUM_SUPPLIES,
2310 + imx477->supplies);
2313 +/* Verify chip ID */
2314 +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
2316 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2320 + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
2321 + IMX477_REG_VALUE_16BIT, &val);
2323 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
2324 + expected_id, ret);
2328 + if (val != expected_id) {
2329 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2330 + expected_id, val);
2334 + dev_info(&client->dev, "Device found is imx%x\n", val);
2339 +static const struct v4l2_subdev_core_ops imx477_core_ops = {
2340 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2341 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2344 +static const struct v4l2_subdev_video_ops imx477_video_ops = {
2345 + .s_stream = imx477_set_stream,
2348 +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
2349 + .enum_mbus_code = imx477_enum_mbus_code,
2350 + .get_fmt = imx477_get_pad_format,
2351 + .set_fmt = imx477_set_pad_format,
2352 + .get_selection = imx477_get_selection,
2353 + .enum_frame_size = imx477_enum_frame_size,
2356 +static const struct v4l2_subdev_ops imx477_subdev_ops = {
2357 + .core = &imx477_core_ops,
2358 + .video = &imx477_video_ops,
2359 + .pad = &imx477_pad_ops,
2362 +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
2363 + .open = imx477_open,
2366 +/* Initialize control handlers */
2367 +static int imx477_init_controls(struct imx477 *imx477)
2369 + struct v4l2_ctrl_handler *ctrl_hdlr;
2370 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2371 + struct v4l2_fwnode_device_properties props;
2375 + ctrl_hdlr = &imx477->ctrl_handler;
2376 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2380 + mutex_init(&imx477->mutex);
2381 + ctrl_hdlr->lock = &imx477->mutex;
2383 + /* By default, PIXEL_RATE is read only */
2384 + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2385 + V4L2_CID_PIXEL_RATE,
2386 + IMX477_PIXEL_RATE,
2387 + IMX477_PIXEL_RATE, 1,
2388 + IMX477_PIXEL_RATE);
2391 + * Create the controls here, but mode specific limits are setup
2392 + * in the imx477_set_framing_limits() call below.
2394 + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2395 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2396 + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2397 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2399 + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2400 + V4L2_CID_EXPOSURE,
2401 + IMX477_EXPOSURE_MIN,
2402 + IMX477_EXPOSURE_MAX,
2403 + IMX477_EXPOSURE_STEP,
2404 + IMX477_EXPOSURE_DEFAULT);
2406 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2407 + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2408 + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2410 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2411 + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2412 + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2414 + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2415 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2416 + if (imx477->hflip)
2417 + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2419 + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2420 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2421 + if (imx477->vflip)
2422 + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2424 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2425 + V4L2_CID_TEST_PATTERN,
2426 + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2427 + 0, 0, imx477_test_pattern_menu);
2428 + for (i = 0; i < 4; i++) {
2430 + * The assumption is that
2431 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2432 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2433 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2435 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2436 + V4L2_CID_TEST_PATTERN_RED + i,
2437 + IMX477_TEST_PATTERN_COLOUR_MIN,
2438 + IMX477_TEST_PATTERN_COLOUR_MAX,
2439 + IMX477_TEST_PATTERN_COLOUR_STEP,
2440 + IMX477_TEST_PATTERN_COLOUR_MAX);
2441 + /* The "Solid color" pattern is white by default */
2444 + if (ctrl_hdlr->error) {
2445 + ret = ctrl_hdlr->error;
2446 + dev_err(&client->dev, "%s control init failed (%d)\n",
2451 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2455 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2460 + imx477->sd.ctrl_handler = ctrl_hdlr;
2462 + /* Setup exposure and frame/line length limits. */
2463 + imx477_set_framing_limits(imx477);
2468 + v4l2_ctrl_handler_free(ctrl_hdlr);
2469 + mutex_destroy(&imx477->mutex);
2474 +static void imx477_free_controls(struct imx477 *imx477)
2476 + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2477 + mutex_destroy(&imx477->mutex);
2480 +static int imx477_check_hwcfg(struct device *dev)
2482 + struct fwnode_handle *endpoint;
2483 + struct v4l2_fwnode_endpoint ep_cfg = {
2484 + .bus_type = V4L2_MBUS_CSI2_DPHY
2486 + int ret = -EINVAL;
2488 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2490 + dev_err(dev, "endpoint node not found\n");
2494 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2495 + dev_err(dev, "could not parse endpoint\n");
2499 + /* Check the number of MIPI CSI2 data lanes */
2500 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2501 + dev_err(dev, "only 2 data lanes are currently supported\n");
2505 + /* Check the link frequency set in device tree */
2506 + if (!ep_cfg.nr_of_link_frequencies) {
2507 + dev_err(dev, "link-frequency property not found in DT\n");
2511 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2512 + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2513 + dev_err(dev, "Link frequency not supported: %lld\n",
2514 + ep_cfg.link_frequencies[0]);
2521 + v4l2_fwnode_endpoint_free(&ep_cfg);
2522 + fwnode_handle_put(endpoint);
2527 +static const struct imx477_compatible_data imx477_compatible = {
2528 + .chip_id = IMX477_CHIP_ID,
2535 +static const struct imx477_reg imx378_regs[] = {
2541 +static const struct imx477_compatible_data imx378_compatible = {
2542 + .chip_id = IMX378_CHIP_ID,
2544 + .num_of_regs = ARRAY_SIZE(imx378_regs),
2545 + .regs = imx378_regs
2549 +static const struct of_device_id imx477_dt_ids[] = {
2550 + { .compatible = "sony,imx477", .data = &imx477_compatible },
2551 + { .compatible = "sony,imx378", .data = &imx378_compatible },
2552 + { /* sentinel */ }
2555 +static int imx477_probe(struct i2c_client *client)
2557 + struct device *dev = &client->dev;
2558 + struct imx477 *imx477;
2559 + const struct of_device_id *match;
2562 + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2566 + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2568 + match = of_match_device(imx477_dt_ids, dev);
2571 + imx477->compatible_data =
2572 + (const struct imx477_compatible_data *)match->data;
2574 + /* Check the hardware configuration in device tree */
2575 + if (imx477_check_hwcfg(dev))
2578 + /* Get system clock (xclk) */
2579 + imx477->xclk = devm_clk_get(dev, NULL);
2580 + if (IS_ERR(imx477->xclk)) {
2581 + dev_err(dev, "failed to get xclk\n");
2582 + return PTR_ERR(imx477->xclk);
2585 + imx477->xclk_freq = clk_get_rate(imx477->xclk);
2586 + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2587 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2588 + imx477->xclk_freq);
2592 + ret = imx477_get_regulators(imx477);
2594 + dev_err(dev, "failed to get regulators\n");
2598 + /* Request optional enable pin */
2599 + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2603 + * The sensor must be powered for imx477_identify_module()
2604 + * to be able to read the CHIP_ID register
2606 + ret = imx477_power_on(dev);
2610 + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2612 + goto error_power_off;
2614 + /* Initialize default format */
2615 + imx477_set_default_format(imx477);
2617 + /* Enable runtime PM and turn off the device */
2618 + pm_runtime_set_active(dev);
2619 + pm_runtime_enable(dev);
2620 + pm_runtime_idle(dev);
2622 + /* This needs the pm runtime to be registered. */
2623 + ret = imx477_init_controls(imx477);
2625 + goto error_power_off;
2627 + /* Initialize subdev */
2628 + imx477->sd.internal_ops = &imx477_internal_ops;
2629 + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2630 + V4L2_SUBDEV_FL_HAS_EVENTS;
2631 + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2633 + /* Initialize source pads */
2634 + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2635 + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2637 + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2639 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2640 + goto error_handler_free;
2643 + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2645 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2646 + goto error_media_entity;
2651 +error_media_entity:
2652 + media_entity_cleanup(&imx477->sd.entity);
2654 +error_handler_free:
2655 + imx477_free_controls(imx477);
2658 + pm_runtime_disable(&client->dev);
2659 + pm_runtime_set_suspended(&client->dev);
2660 + imx477_power_off(&client->dev);
2665 +static void imx477_remove(struct i2c_client *client)
2667 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2668 + struct imx477 *imx477 = to_imx477(sd);
2670 + v4l2_async_unregister_subdev(sd);
2671 + media_entity_cleanup(&sd->entity);
2672 + imx477_free_controls(imx477);
2674 + pm_runtime_disable(&client->dev);
2675 + if (!pm_runtime_status_suspended(&client->dev))
2676 + imx477_power_off(&client->dev);
2677 + pm_runtime_set_suspended(&client->dev);
2680 +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2682 +static const struct dev_pm_ops imx477_pm_ops = {
2683 + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2684 + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2687 +static struct i2c_driver imx477_i2c_driver = {
2690 + .of_match_table = imx477_dt_ids,
2691 + .pm = &imx477_pm_ops,
2693 + .probe_new = imx477_probe,
2694 + .remove = imx477_remove,
2697 +module_i2c_driver(imx477_i2c_driver);
2699 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2700 +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2701 +MODULE_LICENSE("GPL v2");