1 From c35ffd0ab66cb799a1f54f34212de9ab4b9cfb14 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 sensor
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
11 Add YAML device tree binding for IMX477 CMOS image sensor.
13 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
15 media: i2c: Add driver for Sony IMX477 sensor
17 Adds a driver for the 12MPix Sony IMX477 CSI2 sensor.
18 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
19 currently only supports 2 lanes.
21 The following Bayer modes are currently available:
23 4056x3040 12-bit @ 10fps
24 2028x1520 12-bit (binned) @ 40fps
25 2028x1050 12-bit (cropped/binned) @ 50fps
26 1012x760 10-bit (scaled) @ 120 fps
28 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
30 media: i2c: imx477: Add support for adaptive frame control
32 Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
33 automatically adjust the sensor frame length based on exposure time,
34 allowing variable frame rates and longer exposures.
36 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
38 media: i2c: imx477: Return correct result on sensor id verification
40 The test should return -EIO if the register read id does not match
41 the expected sensor id.
43 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
45 media: i2c: imx477: Parse and register properties
47 Parse device properties and register controls for them using the V4L2
48 fwnode properties helpers.
50 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
52 media: i2c: imx477: Selection compliance fixes
54 To comply with the intended usage of the V4L2 selection target when
55 used to retrieve a sensor image properties, adjust the rectangles
56 returned by the imx477 driver.
58 The top/left crop coordinates of the TGT_CROP rectangle were set to
59 (0, 0) instead of (8, 16) which is the offset from the larger physical
60 pixel array rectangle. This was also a mismatch with the default values
61 crop rectangle value, so this is corrected. Found with v4l2-compliance.
63 While at it, add V4L2_SEL_TGT_CROP_BOUNDS support: CROP_DEFAULT and
64 CROP_BOUNDS have the same size as the non-active pixels are not readable
65 using the selection API. Found with v4l2-compliance.
67 This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
70 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
72 media: i2c: imx477: Remove auto frame length adjusting
74 The V4L2_CID_EXPOSURE_AUTO_PRIORITY was used to let the sensor control
75 frame length (effectively framerate) based on the requested exposure
76 time requested. Remove this feature as it is never used, and goes
77 against how V4L2 likes to handle exposure and vblank controls.
79 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
81 media: i2c: imx477: Add very long exposure control to the driver
83 Add support for very long exposures by using the exposure multiplier
84 register. Userland does not need to pass any additional controls to
85 enable long exposures, it simply requests a larger vblank to extend the
86 exposure control range appropriately.
88 Currently, since hblank is fixed, a maximum of approximately 124 seconds
89 of exposure time can be used. In a future change, hblank could also be
90 controlled in userland to give over 200 seconds of exposure time.
92 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
94 media: i2c: imx477: Fix crop height for 2028x1080 mode
96 The crop height for this mode was set at 2600 lines, it should be 2160
99 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
101 media: i2c: imx477: Replace existing 1012x760 mode
103 The existing 1012x760 120 fps mode has significant IQ problem using
104 the internal sensor scaler. Replace this mode with a 1332x990 120 fps
105 mode instead. This new mode has a smaller field of view, but does not
106 suffer from the bad IQ of the original mode.
108 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
110 media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
112 The only field in this struct that is used is the format code, so
113 replace the struct with this single field.
115 Save the format code in imx477_set_pad_format() when setting up a new
116 mode so that imx477_get_pad_format() performs the right lookup.
117 Otherwise, this caused a bug where the mode lookup occurred on the
118 12-bit table rather than the 10-bit table.
120 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
122 media: i2c: imx477: Remove unused function parameter
124 The struct imx477 *ctrl parameter is not used in the function
125 imx477_adjust_exposure_range(), so remove it.
127 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
129 media: i2c: imx477: Fix for long exposure limit calculations
131 Do not scale IMX477_EXPOSURE_OFFSET with the long exposure factor during
132 the limit calculations. This allows larger exposure times, and does seem to be
133 what the sensor is doing internally.
135 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
137 media: i2c: imx477: Extend driver to support imx378 sensor
139 The imx378 sensor is almost identical to the imx477 and can be
140 supported as a "compatible" sensor with just a few extra register
143 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
145 media: i2c: imx477: Fix framerates for 1332x990 mode
147 The imx477 driver's line length for this mode had not been updated to
148 the value supplied to us by the sensor manufacturer. With this
149 correction the sensor delivers the framerates that are expected.
151 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
153 media: i2c: imx477: Allow control of on-sensor DPC
155 A module parameter "dpc_enable" is added to allow the control of the
156 sensor's on-board DPC (Defective Pixel Correction) function.
158 This is a global setting to be configured before using the sensor;
159 there is no intention that this would ever be changed on-the-fly.
161 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
163 media: i2c: imx477: Sensor should report RAW color space
165 Tested on Raspberry Pi running libcamera.
167 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
169 media: i2c: imx477: Add vsync trigger_mode parameter
171 trigger_mode == 0 (default) => no effect / no registers written
172 trigger_mode == 1 => source
173 trigger_mode == 2 => sink
175 This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
177 Signed-off-by: Jonas Jacob <jonas.jacob@neocortexvision.com>
179 media: i2c: Update imx477 Kconfig entry
181 Bring the IMX477 Kconfig declaration in line with upstream entries.
183 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
185 media: i2c: imx477: Correct minimum exposure lines
187 The minimum number of exposure lines value (IMX477_EXPOSURE_MIN) was
188 previously 20 but this is not correct. The datasheet is not completely
189 explicit, however the new value of 4 has been tested with all the
190 sensor modes supported by this driver, and matches the lowest exposure
191 value of 114us that could be achieved wtih Raspberry Pi's legacy
194 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
196 media: i2c: imx477: Allow dynamic horizontal blanking control
198 Currently, the V4L2_CID_HBLANK control is marked as read-only. Remove this
199 restriction and allow userland to modify the control if needed.
201 Set the maximum limit of the line length to 0xfff0.
203 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
205 media: i2c: imx477: Reset hblank on mode switch
207 Reset the hblank control to the minimum value on every mode switch. This is to
208 account for userland instances that do not yet control hblank, otherwise it
209 gets set to a non-optimal value.
211 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
213 media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
215 On a mode change, only call imx477_set_framing_limits() to adjust the hblank
216 and vblank limits if the new mode is different from the existing mode. This
217 preserves any manual control values the user might have set.
219 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
221 driver: media: i2c: imx477: Re-enable temperature sensor
223 The temperature sensor enable register write got lost at some point.
226 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
228 media: i2c: imx477: Fix locking in imx477_init_controls()
230 The driver does not lock the imx477 mutex when calling
231 imx477_set_framing_limits(), leading to:
233 WARNING: CPU: 3 PID: 426 at drivers/media/v4l2-core/v4l2-ctrls-api.c:934 __v4l2_ctrl_modify_range+0x1a0/0x210 [
236 Fix this by taking the lock.
238 Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
240 drivers: media: imx477: Disable the scaler
242 The horizontal scaler was enabled for the 2028x1520 and 2028x1080 modes,
243 with a scale factor of 1. It caused a single column of bad pixels on the
244 right edge of the image. Since scaling is not needed for these modes,
247 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
249 drivers: media: imx477: Set horizontal binning when disabling the scaler
251 The horizontal scaler has been disabled but actually the sensor is not
252 binning horizontally, resulting in images that are stretched 2x
253 horizontally (missing the right half of the field of view completely).
255 Therefore we must additionally set the horizontal binning mode. There
256 is only marginal change in output quality and noise levels.
258 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
259 Fixes: f075893e9b0e ("drivers: media: imx477: Disable the scaler")
261 drivers: media: imx477: Add V4L2_CID_LINK_FREQ control
263 Add V4L2_CID_LINK_FREQ as a read-only control with a value of 450 Mhz.
264 This will be used by the CFE driver to corretly setup the DPHY timing
265 parameters in the CSI-2 block.
267 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
269 drivers: media: imx477: Correctly set IMX477_PIXEL_RATE as a r/o control
271 This control is meant to be read-only, mark it as such.
273 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
275 drivers: media: i2c: imx296,imx477: Configure tigger_mode every time
277 Don't assume the camera has been reset each time we start streaming,
278 but always write registers relating to trigger_mode, even in mode 0.
280 IMX477: Stop driving XVS on stop streaming, to avoid spurious pulses.
282 Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
284 media: i2c: imx477: Squash fixes
286 imx477: make trigger-mode more configurable
288 Allow trigger-mode to be overridden using device tree so that it can be
289 set per camera. Previously the mode could only be changed using a module
290 parameter, which would then affect all cameras.
292 Signed-off-by: Erik Botö <erik.boto@gmail.com>
294 drivers: media: imx477: Add V4L2_CID_LINK_FREQ control
296 Add V4L2_CID_LINK_FREQ as a read-only control with a value of 450 Mhz.
297 This will be used by the CFE driver to corretly setup the DPHY timing
298 parameters in the CSI-2 block.
300 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
302 .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
304 drivers/media/i2c/Kconfig | 11 +
305 drivers/media/i2c/Makefile | 1 +
306 drivers/media/i2c/imx477.c | 2339 +++++++++++++++++
307 5 files changed, 2472 insertions(+)
308 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx477.yaml
309 create mode 100644 drivers/media/i2c/imx477.c
312 +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
314 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
317 +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
318 +$schema: http://devicetree.org/meta-schemas/core.yaml#
320 +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
323 + - Naushir Patuck <naush@raspberypi.com>
326 + The Sony IMX477 is a 1/2.3-inch CMOS active pixel digital image sensor
327 + with an active array size of 4056H x 3040V. It is programmable through
328 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
329 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
337 + description: I2C device address
345 + Digital I/O voltage supply, 1.05 volts
349 + Analog voltage supply, 2.8 volts
353 + Digital core voltage supply, 1.8 volts
357 + Reference to the GPIO connected to the xclr pin, if any.
358 + Must be released (set high) after all all supplies and INCK are applied.
360 + # See ../video-interfaces.txt for more details
369 + The sensor supports either two-lane, or four-lane operation.
370 + For two-lane operation the property must be set to <1 2>.
375 + clock-noncontinuous:
378 + MIPI CSI-2 clock is non-continuous if this property is present,
379 + otherwise it's continuous.
383 + - $ref: /schemas/types.yaml#/definitions/uint64-array
385 + Allowed data bus frequencies.
399 +additionalProperties: false
404 + #address-cells = <1>;
407 + imx477: sensor@10 {
408 + compatible = "sony,imx477";
410 + clocks = <&imx477_clk>;
411 + VANA-supply = <&imx477_vana>; /* 2.8v */
412 + VDIG-supply = <&imx477_vdig>; /* 1.05v */
413 + VDDL-supply = <&imx477_vddl>; /* 1.8v */
416 + imx477_0: endpoint {
417 + remote-endpoint = <&csi1_ep>;
418 + data-lanes = <1 2>;
419 + clock-noncontinuous;
420 + link-frequencies = /bits/ 64 <450000000>;
429 @@ -21548,6 +21548,14 @@ T: git git://linuxtv.org/media.git
430 F: Documentation/devicetree/bindings/media/i2c/sony,imx415.yaml
431 F: drivers/media/i2c/imx415.c
433 +SONY IMX477 SENSOR DRIVER
434 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
435 +L: linux-media@vger.kernel.org
437 +T: git git://linuxtv.org/media_tree.git
438 +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
439 +F: drivers/media/i2c/imx477.c
441 SONY MEMORYSTICK SUBSYSTEM
442 M: Maxim Levitsky <maximlevitsky@gmail.com>
443 M: Alex Dubov <oakad@yahoo.com>
444 --- a/drivers/media/i2c/Kconfig
445 +++ b/drivers/media/i2c/Kconfig
446 @@ -972,6 +972,17 @@ config VIDEO_UDA1342
447 To compile this driver as a module, choose M here: the
448 module will be called uda1342.
451 + tristate "Sony IMX477 sensor support"
452 + depends on I2C && VIDEO_DEV
453 + select VIDEO_V4L2_SUBDEV_API
455 + This is a Video4Linux2 sensor driver for the Sony
456 + IMX477 camera. Also supports the Sony IMX378.
458 + To compile this driver as a module, choose M here: the
459 + module will be called imx477.
461 config VIDEO_VP27SMPX
462 tristate "Panasonic VP27's internal MPX"
463 depends on VIDEO_DEV && I2C
464 --- a/drivers/media/i2c/Makefile
465 +++ b/drivers/media/i2c/Makefile
466 @@ -59,6 +59,7 @@ obj-$(CONFIG_VIDEO_IMX335) += imx335.o
467 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
468 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
469 obj-$(CONFIG_VIDEO_IMX415) += imx415.o
470 +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
471 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
472 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
473 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
475 +++ b/drivers/media/i2c/imx477.c
477 +// SPDX-License-Identifier: GPL-2.0
479 + * A V4L2 driver for Sony IMX477 cameras.
480 + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
482 + * Based on Sony imx219 camera driver
483 + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
485 +#include <linux/unaligned.h>
486 +#include <linux/clk.h>
487 +#include <linux/delay.h>
488 +#include <linux/gpio/consumer.h>
489 +#include <linux/i2c.h>
490 +#include <linux/module.h>
491 +#include <linux/of_device.h>
492 +#include <linux/pm_runtime.h>
493 +#include <linux/regulator/consumer.h>
494 +#include <media/v4l2-ctrls.h>
495 +#include <media/v4l2-device.h>
496 +#include <media/v4l2-event.h>
497 +#include <media/v4l2-fwnode.h>
498 +#include <media/v4l2-mediabus.h>
500 +static int dpc_enable = 1;
501 +module_param(dpc_enable, int, 0644);
502 +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
504 +static int trigger_mode;
505 +module_param(trigger_mode, int, 0644);
506 +MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
508 +#define IMX477_REG_VALUE_08BIT 1
509 +#define IMX477_REG_VALUE_16BIT 2
512 +#define IMX477_REG_CHIP_ID 0x0016
513 +#define IMX477_CHIP_ID 0x0477
514 +#define IMX378_CHIP_ID 0x0378
516 +#define IMX477_REG_MODE_SELECT 0x0100
517 +#define IMX477_MODE_STANDBY 0x00
518 +#define IMX477_MODE_STREAMING 0x01
520 +#define IMX477_REG_ORIENTATION 0x101
522 +#define IMX477_XCLK_FREQ 24000000
524 +#define IMX477_DEFAULT_LINK_FREQ 450000000
526 +/* Pixel rate is fixed at 840MHz for all the modes */
527 +#define IMX477_PIXEL_RATE 840000000
529 +/* V_TIMING internal */
530 +#define IMX477_REG_FRAME_LENGTH 0x0340
531 +#define IMX477_FRAME_LENGTH_MAX 0xffdc
533 +/* H_TIMING internal */
534 +#define IMX477_REG_LINE_LENGTH 0x0342
535 +#define IMX477_LINE_LENGTH_MAX 0xfff0
537 +/* Long exposure multiplier */
538 +#define IMX477_LONG_EXP_SHIFT_MAX 7
539 +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
541 +/* Exposure control */
542 +#define IMX477_REG_EXPOSURE 0x0202
543 +#define IMX477_EXPOSURE_OFFSET 22
544 +#define IMX477_EXPOSURE_MIN 4
545 +#define IMX477_EXPOSURE_STEP 1
546 +#define IMX477_EXPOSURE_DEFAULT 0x640
547 +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
548 + IMX477_EXPOSURE_OFFSET)
550 +/* Analog gain control */
551 +#define IMX477_REG_ANALOG_GAIN 0x0204
552 +#define IMX477_ANA_GAIN_MIN 0
553 +#define IMX477_ANA_GAIN_MAX 978
554 +#define IMX477_ANA_GAIN_STEP 1
555 +#define IMX477_ANA_GAIN_DEFAULT 0x0
557 +/* Digital gain control */
558 +#define IMX477_REG_DIGITAL_GAIN 0x020e
559 +#define IMX477_DGTL_GAIN_MIN 0x0100
560 +#define IMX477_DGTL_GAIN_MAX 0xffff
561 +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
562 +#define IMX477_DGTL_GAIN_STEP 1
564 +/* Test Pattern Control */
565 +#define IMX477_REG_TEST_PATTERN 0x0600
566 +#define IMX477_TEST_PATTERN_DISABLE 0
567 +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
568 +#define IMX477_TEST_PATTERN_COLOR_BARS 2
569 +#define IMX477_TEST_PATTERN_GREY_COLOR 3
570 +#define IMX477_TEST_PATTERN_PN9 4
572 +/* Test pattern colour components */
573 +#define IMX477_REG_TEST_PATTERN_R 0x0602
574 +#define IMX477_REG_TEST_PATTERN_GR 0x0604
575 +#define IMX477_REG_TEST_PATTERN_B 0x0606
576 +#define IMX477_REG_TEST_PATTERN_GB 0x0608
577 +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
578 +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
579 +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
580 +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
581 +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
582 +#define IMX477_TEST_PATTERN_B_DEFAULT 0
583 +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
586 +#define IMX477_REG_MC_MODE 0x3f0b
587 +#define IMX477_REG_MS_SEL 0x3041
588 +#define IMX477_REG_XVS_IO_CTRL 0x3040
589 +#define IMX477_REG_EXTOUT_EN 0x4b81
591 +/* Embedded metadata stream structure */
592 +#define IMX477_EMBEDDED_LINE_WIDTH 16384
593 +#define IMX477_NUM_EMBEDDED_LINES 1
601 +/* IMX477 native and active pixel array size. */
602 +#define IMX477_NATIVE_WIDTH 4072U
603 +#define IMX477_NATIVE_HEIGHT 3176U
604 +#define IMX477_PIXEL_ARRAY_LEFT 8U
605 +#define IMX477_PIXEL_ARRAY_TOP 16U
606 +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
607 +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
614 +struct imx477_reg_list {
615 + unsigned int num_of_regs;
616 + const struct imx477_reg *regs;
619 +/* Mode : resolution and related config&values */
620 +struct imx477_mode {
622 + unsigned int width;
625 + unsigned int height;
627 + /* H-timing in pixels */
628 + unsigned int line_length_pix;
630 + /* Analog crop rectangle. */
631 + struct v4l2_rect crop;
633 + /* Highest possible framerate. */
634 + struct v4l2_fract timeperframe_min;
636 + /* Default framerate. */
637 + struct v4l2_fract timeperframe_default;
639 + /* Default register values */
640 + struct imx477_reg_list reg_list;
643 +static const s64 imx477_link_freq_menu[] = {
644 + IMX477_DEFAULT_LINK_FREQ,
647 +static const struct imx477_reg mode_common_regs[] = {
960 +static const struct imx477_reg mode_4056x3040_regs[] = {
1071 +/* 2x2 binned. 40fps */
1072 +static const struct imx477_reg mode_2028x1520_regs[] = {
1172 +/* 1080p cropped mode */
1173 +static const struct imx477_reg mode_2028x1080_regs[] = {
1273 +/* 4x4 binned. 120fps */
1274 +static const struct imx477_reg mode_1332x990_regs[] = {
1404 +static const struct imx477_mode supported_modes_12bit[] = {
1406 + /* 12MPix 10fps mode */
1409 + .line_length_pix = 0x5dc0,
1411 + .left = IMX477_PIXEL_ARRAY_LEFT,
1412 + .top = IMX477_PIXEL_ARRAY_TOP,
1416 + .timeperframe_min = {
1418 + .denominator = 1000
1420 + .timeperframe_default = {
1422 + .denominator = 1000
1425 + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
1426 + .regs = mode_4056x3040_regs,
1430 + /* 2x2 binned 40fps mode */
1433 + .line_length_pix = 0x31c4,
1435 + .left = IMX477_PIXEL_ARRAY_LEFT,
1436 + .top = IMX477_PIXEL_ARRAY_TOP,
1440 + .timeperframe_min = {
1442 + .denominator = 4000
1444 + .timeperframe_default = {
1446 + .denominator = 3000
1449 + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
1450 + .regs = mode_2028x1520_regs,
1454 + /* 1080p 50fps cropped mode */
1457 + .line_length_pix = 0x31c4,
1459 + .left = IMX477_PIXEL_ARRAY_LEFT,
1460 + .top = IMX477_PIXEL_ARRAY_TOP + 440,
1464 + .timeperframe_min = {
1466 + .denominator = 5000
1468 + .timeperframe_default = {
1470 + .denominator = 3000
1473 + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
1474 + .regs = mode_2028x1080_regs,
1479 +static const struct imx477_mode supported_modes_10bit[] = {
1481 + /* 120fps. 2x2 binned and cropped */
1484 + .line_length_pix = 6664,
1487 + * FIXME: the analog crop rectangle is actually
1488 + * programmed with a horizontal displacement of 0
1489 + * pixels, not 4. It gets shrunk after going through
1490 + * the scaler. Move this information to the compose
1491 + * rectangle once the driver is expanded to represent
1492 + * its processing blocks with multiple subdevs.
1494 + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1495 + .top = IMX477_PIXEL_ARRAY_TOP + 528,
1499 + .timeperframe_min = {
1501 + .denominator = 12000
1503 + .timeperframe_default = {
1505 + .denominator = 12000
1508 + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1509 + .regs = mode_1332x990_regs,
1515 + * The supported formats.
1516 + * This table MUST contain 4 entries per format, to cover the various flip
1517 + * combinations in the order
1523 +static const u32 codes[] = {
1524 + /* 12-bit modes. */
1525 + MEDIA_BUS_FMT_SRGGB12_1X12,
1526 + MEDIA_BUS_FMT_SGRBG12_1X12,
1527 + MEDIA_BUS_FMT_SGBRG12_1X12,
1528 + MEDIA_BUS_FMT_SBGGR12_1X12,
1529 + /* 10-bit modes. */
1530 + MEDIA_BUS_FMT_SRGGB10_1X10,
1531 + MEDIA_BUS_FMT_SGRBG10_1X10,
1532 + MEDIA_BUS_FMT_SGBRG10_1X10,
1533 + MEDIA_BUS_FMT_SBGGR10_1X10,
1536 +static const char * const imx477_test_pattern_menu[] = {
1540 + "Grey Color Bars",
1544 +static const int imx477_test_pattern_val[] = {
1545 + IMX477_TEST_PATTERN_DISABLE,
1546 + IMX477_TEST_PATTERN_COLOR_BARS,
1547 + IMX477_TEST_PATTERN_SOLID_COLOR,
1548 + IMX477_TEST_PATTERN_GREY_COLOR,
1549 + IMX477_TEST_PATTERN_PN9,
1552 +/* regulator supplies */
1553 +static const char * const imx477_supply_name[] = {
1554 + /* Supplies can be enabled in any order */
1555 + "VANA", /* Analog (2.8V) supply */
1556 + "VDIG", /* Digital Core (1.05V) supply */
1557 + "VDDL", /* IF (1.8V) supply */
1560 +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1563 + * Initialisation delay between XCLR low->high and the moment when the sensor
1564 + * can start capture (i.e. can leave software standby), given by T7 in the
1565 + * datasheet is 8ms. This does include I2C setup time as well.
1567 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1568 + * in the datasheet) is much smaller - 600us.
1570 +#define IMX477_XCLR_MIN_DELAY_US 8000
1571 +#define IMX477_XCLR_DELAY_RANGE_US 1000
1573 +struct imx477_compatible_data {
1574 + unsigned int chip_id;
1575 + struct imx477_reg_list extra_regs;
1579 + struct v4l2_subdev sd;
1580 + struct media_pad pad[NUM_PADS];
1582 + unsigned int fmt_code;
1587 + struct gpio_desc *reset_gpio;
1588 + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1590 + struct v4l2_ctrl_handler ctrl_handler;
1591 + /* V4L2 Controls */
1592 + struct v4l2_ctrl *pixel_rate;
1593 + struct v4l2_ctrl *link_freq;
1594 + struct v4l2_ctrl *exposure;
1595 + struct v4l2_ctrl *vflip;
1596 + struct v4l2_ctrl *hflip;
1597 + struct v4l2_ctrl *vblank;
1598 + struct v4l2_ctrl *hblank;
1600 + /* Current mode */
1601 + const struct imx477_mode *mode;
1603 + /* Trigger mode */
1604 + int trigger_mode_of;
1607 + * Mutex for serialized access:
1608 + * Protect sensor module set pad format and start/stop streaming safely.
1610 + struct mutex mutex;
1612 + /* Streaming on/off */
1615 + /* Rewrite common registers on stream on? */
1616 + bool common_regs_written;
1618 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1619 + unsigned int long_exp_shift;
1621 + /* Any extra information related to different compatible sensors */
1622 + const struct imx477_compatible_data *compatible_data;
1625 +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1627 + return container_of(_sd, struct imx477, sd);
1630 +static inline void get_mode_table(unsigned int code,
1631 + const struct imx477_mode **mode_list,
1632 + unsigned int *num_modes)
1636 + case MEDIA_BUS_FMT_SRGGB12_1X12:
1637 + case MEDIA_BUS_FMT_SGRBG12_1X12:
1638 + case MEDIA_BUS_FMT_SGBRG12_1X12:
1639 + case MEDIA_BUS_FMT_SBGGR12_1X12:
1640 + *mode_list = supported_modes_12bit;
1641 + *num_modes = ARRAY_SIZE(supported_modes_12bit);
1644 + case MEDIA_BUS_FMT_SRGGB10_1X10:
1645 + case MEDIA_BUS_FMT_SGRBG10_1X10:
1646 + case MEDIA_BUS_FMT_SGBRG10_1X10:
1647 + case MEDIA_BUS_FMT_SBGGR10_1X10:
1648 + *mode_list = supported_modes_10bit;
1649 + *num_modes = ARRAY_SIZE(supported_modes_10bit);
1652 + *mode_list = NULL;
1657 +/* Read registers up to 2 at a time */
1658 +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1660 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1661 + struct i2c_msg msgs[2];
1662 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1663 + u8 data_buf[4] = { 0, };
1669 + /* Write register address */
1670 + msgs[0].addr = client->addr;
1671 + msgs[0].flags = 0;
1672 + msgs[0].len = ARRAY_SIZE(addr_buf);
1673 + msgs[0].buf = addr_buf;
1675 + /* Read data from register */
1676 + msgs[1].addr = client->addr;
1677 + msgs[1].flags = I2C_M_RD;
1678 + msgs[1].len = len;
1679 + msgs[1].buf = &data_buf[4 - len];
1681 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1682 + if (ret != ARRAY_SIZE(msgs))
1685 + *val = get_unaligned_be32(data_buf);
1690 +/* Write registers up to 2 at a time */
1691 +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1693 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1699 + put_unaligned_be16(reg, buf);
1700 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1701 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1707 +/* Write a list of registers */
1708 +static int imx477_write_regs(struct imx477 *imx477,
1709 + const struct imx477_reg *regs, u32 len)
1711 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1715 + for (i = 0; i < len; i++) {
1716 + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1718 + dev_err_ratelimited(&client->dev,
1719 + "Failed to write reg 0x%4.4x. error = %d\n",
1720 + regs[i].address, ret);
1729 +/* Get bayer order based on flip setting. */
1730 +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1734 + lockdep_assert_held(&imx477->mutex);
1736 + for (i = 0; i < ARRAY_SIZE(codes); i++)
1737 + if (codes[i] == code)
1740 + if (i >= ARRAY_SIZE(codes))
1743 + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1744 + (imx477->hflip->val ? 1 : 0);
1749 +static void imx477_set_default_format(struct imx477 *imx477)
1751 + /* Set default mode to max resolution */
1752 + imx477->mode = &supported_modes_12bit[0];
1753 + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1756 +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1758 + struct imx477 *imx477 = to_imx477(sd);
1759 + struct v4l2_mbus_framefmt *try_fmt_img =
1760 + v4l2_subdev_state_get_format(fh->state, IMAGE_PAD);
1761 + struct v4l2_mbus_framefmt *try_fmt_meta =
1762 + v4l2_subdev_state_get_format(fh->state, METADATA_PAD);
1763 + struct v4l2_rect *try_crop;
1765 + mutex_lock(&imx477->mutex);
1767 + /* Initialize try_fmt for the image pad */
1768 + try_fmt_img->width = supported_modes_12bit[0].width;
1769 + try_fmt_img->height = supported_modes_12bit[0].height;
1770 + try_fmt_img->code = imx477_get_format_code(imx477,
1771 + MEDIA_BUS_FMT_SRGGB12_1X12);
1772 + try_fmt_img->field = V4L2_FIELD_NONE;
1774 + /* Initialize try_fmt for the embedded metadata pad */
1775 + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1776 + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1777 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1778 + try_fmt_meta->field = V4L2_FIELD_NONE;
1780 + /* Initialize try_crop */
1781 + try_crop = v4l2_subdev_state_get_crop(fh->state, IMAGE_PAD);
1782 + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1783 + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1784 + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1785 + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1787 + mutex_unlock(&imx477->mutex);
1792 +static void imx477_adjust_exposure_range(struct imx477 *imx477)
1794 + int exposure_max, exposure_def;
1796 + /* Honour the VBLANK limits when setting exposure. */
1797 + exposure_max = imx477->mode->height + imx477->vblank->val -
1798 + IMX477_EXPOSURE_OFFSET;
1799 + exposure_def = min(exposure_max, imx477->exposure->val);
1800 + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1801 + exposure_max, imx477->exposure->step,
1805 +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1809 + imx477->long_exp_shift = 0;
1811 + while (val > IMX477_FRAME_LENGTH_MAX) {
1812 + imx477->long_exp_shift++;
1816 + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1817 + IMX477_REG_VALUE_16BIT, val);
1821 + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1822 + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1825 +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1827 + struct imx477 *imx477 =
1828 + container_of(ctrl->handler, struct imx477, ctrl_handler);
1829 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1833 + * The VBLANK control may change the limits of usable exposure, so check
1834 + * and adjust if necessary.
1836 + if (ctrl->id == V4L2_CID_VBLANK)
1837 + imx477_adjust_exposure_range(imx477);
1840 + * Applying V4L2 control value only happens
1841 + * when power is up for streaming
1843 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1846 + switch (ctrl->id) {
1847 + case V4L2_CID_ANALOGUE_GAIN:
1848 + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1849 + IMX477_REG_VALUE_16BIT, ctrl->val);
1851 + case V4L2_CID_EXPOSURE:
1852 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1853 + IMX477_REG_VALUE_16BIT, ctrl->val >>
1854 + imx477->long_exp_shift);
1856 + case V4L2_CID_DIGITAL_GAIN:
1857 + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1858 + IMX477_REG_VALUE_16BIT, ctrl->val);
1860 + case V4L2_CID_TEST_PATTERN:
1861 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1862 + IMX477_REG_VALUE_16BIT,
1863 + imx477_test_pattern_val[ctrl->val]);
1865 + case V4L2_CID_TEST_PATTERN_RED:
1866 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1867 + IMX477_REG_VALUE_16BIT, ctrl->val);
1869 + case V4L2_CID_TEST_PATTERN_GREENR:
1870 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1871 + IMX477_REG_VALUE_16BIT, ctrl->val);
1873 + case V4L2_CID_TEST_PATTERN_BLUE:
1874 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1875 + IMX477_REG_VALUE_16BIT, ctrl->val);
1877 + case V4L2_CID_TEST_PATTERN_GREENB:
1878 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1879 + IMX477_REG_VALUE_16BIT, ctrl->val);
1881 + case V4L2_CID_HFLIP:
1882 + case V4L2_CID_VFLIP:
1883 + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1884 + imx477->hflip->val |
1885 + imx477->vflip->val << 1);
1887 + case V4L2_CID_VBLANK:
1888 + ret = imx477_set_frame_length(imx477,
1889 + imx477->mode->height + ctrl->val);
1891 + case V4L2_CID_HBLANK:
1892 + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
1893 + imx477->mode->width + ctrl->val);
1896 + dev_info(&client->dev,
1897 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1898 + ctrl->id, ctrl->val);
1903 + pm_runtime_put(&client->dev);
1908 +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1909 + .s_ctrl = imx477_set_ctrl,
1912 +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1913 + struct v4l2_subdev_state *sd_state,
1914 + struct v4l2_subdev_mbus_code_enum *code)
1916 + struct imx477 *imx477 = to_imx477(sd);
1918 + if (code->pad >= NUM_PADS)
1921 + if (code->pad == IMAGE_PAD) {
1922 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1925 + code->code = imx477_get_format_code(imx477,
1926 + codes[code->index * 4]);
1928 + if (code->index > 0)
1931 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1937 +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1938 + struct v4l2_subdev_state *sd_state,
1939 + struct v4l2_subdev_frame_size_enum *fse)
1941 + struct imx477 *imx477 = to_imx477(sd);
1943 + if (fse->pad >= NUM_PADS)
1946 + if (fse->pad == IMAGE_PAD) {
1947 + const struct imx477_mode *mode_list;
1948 + unsigned int num_modes;
1950 + get_mode_table(fse->code, &mode_list, &num_modes);
1952 + if (fse->index >= num_modes)
1955 + if (fse->code != imx477_get_format_code(imx477, fse->code))
1958 + fse->min_width = mode_list[fse->index].width;
1959 + fse->max_width = fse->min_width;
1960 + fse->min_height = mode_list[fse->index].height;
1961 + fse->max_height = fse->min_height;
1963 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1966 + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1967 + fse->max_width = fse->min_width;
1968 + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1969 + fse->max_height = fse->min_height;
1975 +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1977 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1978 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1979 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1982 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1985 +static void imx477_update_image_pad_format(struct imx477 *imx477,
1986 + const struct imx477_mode *mode,
1987 + struct v4l2_subdev_format *fmt)
1989 + fmt->format.width = mode->width;
1990 + fmt->format.height = mode->height;
1991 + fmt->format.field = V4L2_FIELD_NONE;
1992 + imx477_reset_colorspace(&fmt->format);
1995 +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1997 + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1998 + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1999 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
2000 + fmt->format.field = V4L2_FIELD_NONE;
2003 +static int imx477_get_pad_format(struct v4l2_subdev *sd,
2004 + struct v4l2_subdev_state *sd_state,
2005 + struct v4l2_subdev_format *fmt)
2007 + struct imx477 *imx477 = to_imx477(sd);
2009 + if (fmt->pad >= NUM_PADS)
2012 + mutex_lock(&imx477->mutex);
2014 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2015 + struct v4l2_mbus_framefmt *try_fmt =
2016 + v4l2_subdev_state_get_format(sd_state,
2018 + /* update the code which could change due to vflip or hflip: */
2019 + try_fmt->code = fmt->pad == IMAGE_PAD ?
2020 + imx477_get_format_code(imx477, try_fmt->code) :
2021 + MEDIA_BUS_FMT_SENSOR_DATA;
2022 + fmt->format = *try_fmt;
2024 + if (fmt->pad == IMAGE_PAD) {
2025 + imx477_update_image_pad_format(imx477, imx477->mode,
2027 + fmt->format.code =
2028 + imx477_get_format_code(imx477, imx477->fmt_code);
2030 + imx477_update_metadata_pad_format(fmt);
2034 + mutex_unlock(&imx477->mutex);
2039 +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
2040 + const struct v4l2_fract *timeperframe)
2044 + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
2045 + do_div(frame_length,
2046 + (u64)timeperframe->denominator * mode->line_length_pix);
2048 + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
2049 + frame_length = IMX477_FRAME_LENGTH_MAX;
2051 + return max_t(unsigned int, frame_length, mode->height);
2054 +static void imx477_set_framing_limits(struct imx477 *imx477)
2056 + unsigned int frm_length_min, frm_length_default, hblank_min;
2057 + const struct imx477_mode *mode = imx477->mode;
2059 + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
2060 + frm_length_default =
2061 + imx477_get_frame_length(mode, &mode->timeperframe_default);
2063 + /* Default to no long exposure multiplier. */
2064 + imx477->long_exp_shift = 0;
2066 + /* Update limits and set FPS to default */
2067 + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
2068 + ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
2069 + IMX477_FRAME_LENGTH_MAX) - mode->height,
2070 + 1, frm_length_default - mode->height);
2072 + /* Setting this will adjust the exposure limits as well. */
2073 + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
2075 + hblank_min = mode->line_length_pix - mode->width;
2076 + __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
2077 + IMX477_LINE_LENGTH_MAX, 1, hblank_min);
2078 + __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
2081 +static int imx477_set_pad_format(struct v4l2_subdev *sd,
2082 + struct v4l2_subdev_state *sd_state,
2083 + struct v4l2_subdev_format *fmt)
2085 + struct v4l2_mbus_framefmt *framefmt;
2086 + const struct imx477_mode *mode;
2087 + struct imx477 *imx477 = to_imx477(sd);
2089 + if (fmt->pad >= NUM_PADS)
2092 + mutex_lock(&imx477->mutex);
2094 + if (fmt->pad == IMAGE_PAD) {
2095 + const struct imx477_mode *mode_list;
2096 + unsigned int num_modes;
2098 + /* Bayer order varies with flips */
2099 + fmt->format.code = imx477_get_format_code(imx477,
2100 + fmt->format.code);
2102 + get_mode_table(fmt->format.code, &mode_list, &num_modes);
2104 + mode = v4l2_find_nearest_size(mode_list,
2107 + fmt->format.width,
2108 + fmt->format.height);
2109 + imx477_update_image_pad_format(imx477, mode, fmt);
2110 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2111 + framefmt = v4l2_subdev_state_get_format(sd_state,
2113 + *framefmt = fmt->format;
2114 + } else if (imx477->mode != mode) {
2115 + imx477->mode = mode;
2116 + imx477->fmt_code = fmt->format.code;
2117 + imx477_set_framing_limits(imx477);
2120 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2121 + framefmt = v4l2_subdev_state_get_format(sd_state,
2123 + *framefmt = fmt->format;
2125 + /* Only one embedded data mode is supported */
2126 + imx477_update_metadata_pad_format(fmt);
2130 + mutex_unlock(&imx477->mutex);
2135 +static const struct v4l2_rect *
2136 +__imx477_get_pad_crop(struct imx477 *imx477,
2137 + struct v4l2_subdev_state *sd_state,
2138 + unsigned int pad, enum v4l2_subdev_format_whence which)
2141 + case V4L2_SUBDEV_FORMAT_TRY:
2142 + return v4l2_subdev_state_get_crop(sd_state, pad);
2143 + case V4L2_SUBDEV_FORMAT_ACTIVE:
2144 + return &imx477->mode->crop;
2150 +static int imx477_get_selection(struct v4l2_subdev *sd,
2151 + struct v4l2_subdev_state *sd_state,
2152 + struct v4l2_subdev_selection *sel)
2154 + switch (sel->target) {
2155 + case V4L2_SEL_TGT_CROP: {
2156 + struct imx477 *imx477 = to_imx477(sd);
2158 + mutex_lock(&imx477->mutex);
2159 + sel->r = *__imx477_get_pad_crop(imx477, sd_state, sel->pad,
2161 + mutex_unlock(&imx477->mutex);
2166 + case V4L2_SEL_TGT_NATIVE_SIZE:
2169 + sel->r.width = IMX477_NATIVE_WIDTH;
2170 + sel->r.height = IMX477_NATIVE_HEIGHT;
2174 + case V4L2_SEL_TGT_CROP_DEFAULT:
2175 + case V4L2_SEL_TGT_CROP_BOUNDS:
2176 + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
2177 + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
2178 + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
2179 + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
2187 +/* Start streaming */
2188 +static int imx477_start_streaming(struct imx477 *imx477)
2190 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2191 + const struct imx477_reg_list *reg_list;
2192 + const struct imx477_reg_list *extra_regs;
2195 + if (!imx477->common_regs_written) {
2196 + ret = imx477_write_regs(imx477, mode_common_regs,
2197 + ARRAY_SIZE(mode_common_regs));
2199 + extra_regs = &imx477->compatible_data->extra_regs;
2200 + ret = imx477_write_regs(imx477, extra_regs->regs,
2201 + extra_regs->num_of_regs);
2205 + dev_err(&client->dev, "%s failed to set common settings\n",
2209 + imx477->common_regs_written = true;
2212 + /* Apply default values of current mode */
2213 + reg_list = &imx477->mode->reg_list;
2214 + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
2216 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
2220 + /* Set on-sensor DPC. */
2221 + imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2222 + imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2224 + /* Apply customized values from user */
2225 + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
2229 + /* Set vsync trigger mode: 0=standalone, 1=source, 2=sink */
2230 + tm = (imx477->trigger_mode_of >= 0) ? imx477->trigger_mode_of : trigger_mode;
2231 + imx477_write_reg(imx477, IMX477_REG_MC_MODE,
2232 + IMX477_REG_VALUE_08BIT, (tm > 0) ? 1 : 0);
2233 + imx477_write_reg(imx477, IMX477_REG_MS_SEL,
2234 + IMX477_REG_VALUE_08BIT, (tm <= 1) ? 1 : 0);
2235 + imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
2236 + IMX477_REG_VALUE_08BIT, (tm == 1) ? 1 : 0);
2237 + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
2238 + IMX477_REG_VALUE_08BIT, (tm == 1) ? 1 : 0);
2240 + /* set stream on register */
2241 + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2242 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
2245 +/* Stop streaming */
2246 +static void imx477_stop_streaming(struct imx477 *imx477)
2248 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2251 + /* set stream off register */
2252 + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2253 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
2255 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
2257 + /* Stop driving XVS out (there is still a weak pull-up) */
2258 + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
2259 + IMX477_REG_VALUE_08BIT, 0);
2262 +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
2264 + struct imx477 *imx477 = to_imx477(sd);
2265 + struct i2c_client *client = v4l2_get_subdevdata(sd);
2268 + mutex_lock(&imx477->mutex);
2269 + if (imx477->streaming == enable) {
2270 + mutex_unlock(&imx477->mutex);
2275 + ret = pm_runtime_get_sync(&client->dev);
2277 + pm_runtime_put_noidle(&client->dev);
2282 + * Apply default & customized values
2283 + * and then start streaming.
2285 + ret = imx477_start_streaming(imx477);
2289 + imx477_stop_streaming(imx477);
2290 + pm_runtime_put(&client->dev);
2293 + imx477->streaming = enable;
2295 + /* vflip and hflip cannot change during streaming */
2296 + __v4l2_ctrl_grab(imx477->vflip, enable);
2297 + __v4l2_ctrl_grab(imx477->hflip, enable);
2299 + mutex_unlock(&imx477->mutex);
2304 + pm_runtime_put(&client->dev);
2306 + mutex_unlock(&imx477->mutex);
2311 +/* Power/clock management functions */
2312 +static int imx477_power_on(struct device *dev)
2314 + struct i2c_client *client = to_i2c_client(dev);
2315 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2316 + struct imx477 *imx477 = to_imx477(sd);
2319 + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
2320 + imx477->supplies);
2322 + dev_err(&client->dev, "%s: failed to enable regulators\n",
2327 + ret = clk_prepare_enable(imx477->xclk);
2329 + dev_err(&client->dev, "%s: failed to enable clock\n",
2334 + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
2335 + usleep_range(IMX477_XCLR_MIN_DELAY_US,
2336 + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
2341 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2345 +static int imx477_power_off(struct device *dev)
2347 + struct i2c_client *client = to_i2c_client(dev);
2348 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2349 + struct imx477 *imx477 = to_imx477(sd);
2351 + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
2352 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2353 + clk_disable_unprepare(imx477->xclk);
2355 + /* Force reprogramming of the common registers when powered up again. */
2356 + imx477->common_regs_written = false;
2361 +static int __maybe_unused imx477_suspend(struct device *dev)
2363 + struct i2c_client *client = to_i2c_client(dev);
2364 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2365 + struct imx477 *imx477 = to_imx477(sd);
2367 + if (imx477->streaming)
2368 + imx477_stop_streaming(imx477);
2373 +static int __maybe_unused imx477_resume(struct device *dev)
2375 + struct i2c_client *client = to_i2c_client(dev);
2376 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2377 + struct imx477 *imx477 = to_imx477(sd);
2380 + if (imx477->streaming) {
2381 + ret = imx477_start_streaming(imx477);
2389 + imx477_stop_streaming(imx477);
2390 + imx477->streaming = 0;
2394 +static int imx477_get_regulators(struct imx477 *imx477)
2396 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2399 + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
2400 + imx477->supplies[i].supply = imx477_supply_name[i];
2402 + return devm_regulator_bulk_get(&client->dev,
2403 + IMX477_NUM_SUPPLIES,
2404 + imx477->supplies);
2407 +/* Verify chip ID */
2408 +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
2410 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2414 + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
2415 + IMX477_REG_VALUE_16BIT, &val);
2417 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
2418 + expected_id, ret);
2422 + if (val != expected_id) {
2423 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2424 + expected_id, val);
2428 + dev_info(&client->dev, "Device found is imx%x\n", val);
2433 +static const struct v4l2_subdev_core_ops imx477_core_ops = {
2434 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2435 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2438 +static const struct v4l2_subdev_video_ops imx477_video_ops = {
2439 + .s_stream = imx477_set_stream,
2442 +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
2443 + .enum_mbus_code = imx477_enum_mbus_code,
2444 + .get_fmt = imx477_get_pad_format,
2445 + .set_fmt = imx477_set_pad_format,
2446 + .get_selection = imx477_get_selection,
2447 + .enum_frame_size = imx477_enum_frame_size,
2450 +static const struct v4l2_subdev_ops imx477_subdev_ops = {
2451 + .core = &imx477_core_ops,
2452 + .video = &imx477_video_ops,
2453 + .pad = &imx477_pad_ops,
2456 +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
2457 + .open = imx477_open,
2460 +/* Initialize control handlers */
2461 +static int imx477_init_controls(struct imx477 *imx477)
2463 + struct v4l2_ctrl_handler *ctrl_hdlr;
2464 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2465 + struct v4l2_fwnode_device_properties props;
2469 + ctrl_hdlr = &imx477->ctrl_handler;
2470 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2474 + mutex_init(&imx477->mutex);
2475 + ctrl_hdlr->lock = &imx477->mutex;
2477 + /* By default, PIXEL_RATE is read only */
2478 + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2479 + V4L2_CID_PIXEL_RATE,
2480 + IMX477_PIXEL_RATE,
2481 + IMX477_PIXEL_RATE, 1,
2482 + IMX477_PIXEL_RATE);
2483 + if (imx477->pixel_rate)
2484 + imx477->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2486 + /* LINK_FREQ is also read only */
2487 + imx477->link_freq =
2488 + v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx477_ctrl_ops,
2489 + V4L2_CID_LINK_FREQ,
2490 + ARRAY_SIZE(imx477_link_freq_menu) - 1, 0,
2491 + imx477_link_freq_menu);
2492 + if (imx477->link_freq)
2493 + imx477->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2496 + * Create the controls here, but mode specific limits are setup
2497 + * in the imx477_set_framing_limits() call below.
2499 + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2500 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2501 + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2502 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2504 + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2505 + V4L2_CID_EXPOSURE,
2506 + IMX477_EXPOSURE_MIN,
2507 + IMX477_EXPOSURE_MAX,
2508 + IMX477_EXPOSURE_STEP,
2509 + IMX477_EXPOSURE_DEFAULT);
2511 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2512 + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2513 + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2515 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2516 + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2517 + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2519 + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2520 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2521 + if (imx477->hflip)
2522 + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2524 + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2525 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2526 + if (imx477->vflip)
2527 + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2529 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2530 + V4L2_CID_TEST_PATTERN,
2531 + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2532 + 0, 0, imx477_test_pattern_menu);
2533 + for (i = 0; i < 4; i++) {
2535 + * The assumption is that
2536 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2537 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2538 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2540 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2541 + V4L2_CID_TEST_PATTERN_RED + i,
2542 + IMX477_TEST_PATTERN_COLOUR_MIN,
2543 + IMX477_TEST_PATTERN_COLOUR_MAX,
2544 + IMX477_TEST_PATTERN_COLOUR_STEP,
2545 + IMX477_TEST_PATTERN_COLOUR_MAX);
2546 + /* The "Solid color" pattern is white by default */
2549 + if (ctrl_hdlr->error) {
2550 + ret = ctrl_hdlr->error;
2551 + dev_err(&client->dev, "%s control init failed (%d)\n",
2556 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2560 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2565 + imx477->sd.ctrl_handler = ctrl_hdlr;
2567 + mutex_lock(&imx477->mutex);
2569 + /* Setup exposure and frame/line length limits. */
2570 + imx477_set_framing_limits(imx477);
2572 + mutex_unlock(&imx477->mutex);
2577 + v4l2_ctrl_handler_free(ctrl_hdlr);
2578 + mutex_destroy(&imx477->mutex);
2583 +static void imx477_free_controls(struct imx477 *imx477)
2585 + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2586 + mutex_destroy(&imx477->mutex);
2589 +static int imx477_check_hwcfg(struct device *dev)
2591 + struct fwnode_handle *endpoint;
2592 + struct v4l2_fwnode_endpoint ep_cfg = {
2593 + .bus_type = V4L2_MBUS_CSI2_DPHY
2595 + int ret = -EINVAL;
2597 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2599 + dev_err(dev, "endpoint node not found\n");
2603 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2604 + dev_err(dev, "could not parse endpoint\n");
2608 + /* Check the number of MIPI CSI2 data lanes */
2609 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2610 + dev_err(dev, "only 2 data lanes are currently supported\n");
2614 + /* Check the link frequency set in device tree */
2615 + if (!ep_cfg.nr_of_link_frequencies) {
2616 + dev_err(dev, "link-frequency property not found in DT\n");
2620 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2621 + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2622 + dev_err(dev, "Link frequency not supported: %lld\n",
2623 + ep_cfg.link_frequencies[0]);
2630 + v4l2_fwnode_endpoint_free(&ep_cfg);
2631 + fwnode_handle_put(endpoint);
2636 +static const struct imx477_compatible_data imx477_compatible = {
2637 + .chip_id = IMX477_CHIP_ID,
2644 +static const struct imx477_reg imx378_regs[] = {
2650 +static const struct imx477_compatible_data imx378_compatible = {
2651 + .chip_id = IMX378_CHIP_ID,
2653 + .num_of_regs = ARRAY_SIZE(imx378_regs),
2654 + .regs = imx378_regs
2658 +static const struct of_device_id imx477_dt_ids[] = {
2659 + { .compatible = "sony,imx477", .data = &imx477_compatible },
2660 + { .compatible = "sony,imx378", .data = &imx378_compatible },
2661 + { /* sentinel */ }
2664 +static int imx477_probe(struct i2c_client *client)
2666 + struct device *dev = &client->dev;
2667 + struct imx477 *imx477;
2668 + const struct of_device_id *match;
2672 + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2676 + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2678 + match = of_match_device(imx477_dt_ids, dev);
2681 + imx477->compatible_data =
2682 + (const struct imx477_compatible_data *)match->data;
2684 + /* Check the hardware configuration in device tree */
2685 + if (imx477_check_hwcfg(dev))
2688 + /* Default the trigger mode from OF to -1, which means invalid */
2689 + ret = of_property_read_u32(dev->of_node, "trigger-mode", &tm_of);
2690 + imx477->trigger_mode_of = (ret == 0) ? tm_of : -1;
2692 + /* Get system clock (xclk) */
2693 + imx477->xclk = devm_clk_get(dev, NULL);
2694 + if (IS_ERR(imx477->xclk)) {
2695 + dev_err(dev, "failed to get xclk\n");
2696 + return PTR_ERR(imx477->xclk);
2699 + imx477->xclk_freq = clk_get_rate(imx477->xclk);
2700 + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2701 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2702 + imx477->xclk_freq);
2706 + ret = imx477_get_regulators(imx477);
2708 + dev_err(dev, "failed to get regulators\n");
2712 + /* Request optional enable pin */
2713 + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2717 + * The sensor must be powered for imx477_identify_module()
2718 + * to be able to read the CHIP_ID register
2720 + ret = imx477_power_on(dev);
2724 + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2726 + goto error_power_off;
2728 + /* Initialize default format */
2729 + imx477_set_default_format(imx477);
2731 + /* Enable runtime PM and turn off the device */
2732 + pm_runtime_set_active(dev);
2733 + pm_runtime_enable(dev);
2734 + pm_runtime_idle(dev);
2736 + /* This needs the pm runtime to be registered. */
2737 + ret = imx477_init_controls(imx477);
2739 + goto error_power_off;
2741 + /* Initialize subdev */
2742 + imx477->sd.internal_ops = &imx477_internal_ops;
2743 + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2744 + V4L2_SUBDEV_FL_HAS_EVENTS;
2745 + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2747 + /* Initialize source pads */
2748 + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2749 + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2751 + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2753 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2754 + goto error_handler_free;
2757 + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2759 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2760 + goto error_media_entity;
2765 +error_media_entity:
2766 + media_entity_cleanup(&imx477->sd.entity);
2768 +error_handler_free:
2769 + imx477_free_controls(imx477);
2772 + pm_runtime_disable(&client->dev);
2773 + pm_runtime_set_suspended(&client->dev);
2774 + imx477_power_off(&client->dev);
2779 +static void imx477_remove(struct i2c_client *client)
2781 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2782 + struct imx477 *imx477 = to_imx477(sd);
2784 + v4l2_async_unregister_subdev(sd);
2785 + media_entity_cleanup(&sd->entity);
2786 + imx477_free_controls(imx477);
2788 + pm_runtime_disable(&client->dev);
2789 + if (!pm_runtime_status_suspended(&client->dev))
2790 + imx477_power_off(&client->dev);
2791 + pm_runtime_set_suspended(&client->dev);
2794 +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2796 +static const struct dev_pm_ops imx477_pm_ops = {
2797 + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2798 + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2801 +static struct i2c_driver imx477_i2c_driver = {
2804 + .of_match_table = imx477_dt_ids,
2805 + .pm = &imx477_pm_ops,
2807 + .probe = imx477_probe,
2808 + .remove = imx477_remove,
2811 +module_i2c_driver(imx477_i2c_driver);
2813 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2814 +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2815 +MODULE_LICENSE("GPL v2");