]> git.ipfire.org Git - thirdparty/openwrt.git/blob
28ca11aeb6a8a59c0008ab8421ce2749eb2fa5cf
[thirdparty/openwrt.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
10
11 Add YAML device tree binding for IMX477 CMOS image sensor.
12
13 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
14
15 media: i2c: Add driver for Sony IMX477 sensor
16
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.
20
21 The following Bayer modes are currently available:
22
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
27
28 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
29
30 media: i2c: imx477: Add support for adaptive frame control
31
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.
35
36 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
37
38 media: i2c: imx477: Return correct result on sensor id verification
39
40 The test should return -EIO if the register read id does not match
41 the expected sensor id.
42
43 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
44
45 media: i2c: imx477: Parse and register properties
46
47 Parse device properties and register controls for them using the V4L2
48 fwnode properties helpers.
49
50 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
51
52 media: i2c: imx477: Selection compliance fixes
53
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.
57
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.
62
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.
66
67 This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
68 the imx219 sensor.
69
70 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
71
72 media: i2c: imx477: Remove auto frame length adjusting
73
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.
78
79 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
80
81 media: i2c: imx477: Add very long exposure control to the driver
82
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.
87
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.
91
92 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
93
94 media: i2c: imx477: Fix crop height for 2028x1080 mode
95
96 The crop height for this mode was set at 2600 lines, it should be 2160
97 lines instead.
98
99 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
100
101 media: i2c: imx477: Replace existing 1012x760 mode
102
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.
107
108 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
109
110 media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
111
112 The only field in this struct that is used is the format code, so
113 replace the struct with this single field.
114
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.
119
120 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
121
122 media: i2c: imx477: Remove unused function parameter
123
124 The struct imx477 *ctrl parameter is not used in the function
125 imx477_adjust_exposure_range(), so remove it.
126
127 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
128
129 media: i2c: imx477: Fix for long exposure limit calculations
130
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.
134
135 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
136
137 media: i2c: imx477: Extend driver to support imx378 sensor
138
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
141 writes.
142
143 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
144
145 media: i2c: imx477: Fix framerates for 1332x990 mode
146
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.
150
151 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
152
153 media: i2c: imx477: Allow control of on-sensor DPC
154
155 A module parameter "dpc_enable" is added to allow the control of the
156 sensor's on-board DPC (Defective Pixel Correction) function.
157
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.
160
161 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
162
163 media: i2c: imx477: Sensor should report RAW color space
164
165 Tested on Raspberry Pi running libcamera.
166
167 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
168
169 media: i2c: imx477: Add vsync trigger_mode parameter
170
171 trigger_mode == 0 (default) => no effect / no registers written
172 trigger_mode == 1 => source
173 trigger_mode == 2 => sink
174
175 This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
176
177 Signed-off-by: Jonas Jacob <jonas.jacob@neocortexvision.com>
178
179 media: i2c: Update imx477 Kconfig entry
180
181 Bring the IMX477 Kconfig declaration in line with upstream entries.
182
183 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
184
185 media: i2c: imx477: Correct minimum exposure lines
186
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
192 firmware driver.
193
194 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
195
196 media: i2c: imx477: Allow dynamic horizontal blanking control
197
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.
200
201 Set the maximum limit of the line length to 0xfff0.
202
203 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
204
205 media: i2c: imx477: Reset hblank on mode switch
206
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.
210
211 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
212
213 media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
214
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.
218
219 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
220
221 driver: media: i2c: imx477: Re-enable temperature sensor
222
223 The temperature sensor enable register write got lost at some point.
224 Re-enable it.
225
226 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
227
228 media: i2c: imx477: Fix locking in imx477_init_controls()
229
230 The driver does not lock the imx477 mutex when calling
231 imx477_set_framing_limits(), leading to:
232
233 WARNING: CPU: 3 PID: 426 at drivers/media/v4l2-core/v4l2-ctrls-api.c:934 __v4l2_ctrl_modify_range+0x1a0/0x210 [
234 videodev]
235
236 Fix this by taking the lock.
237
238 Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
239
240 drivers: media: imx477: Disable the scaler
241
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,
245 disable it entirely.
246
247 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
248
249 drivers: media: imx477: Set horizontal binning when disabling the scaler
250
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).
254
255 Therefore we must additionally set the horizontal binning mode. There
256 is only marginal change in output quality and noise levels.
257
258 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
259 Fixes: f075893e9b0e ("drivers: media: imx477: Disable the scaler")
260
261 drivers: media: imx477: Add V4L2_CID_LINK_FREQ control
262
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.
266
267 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
268
269 drivers: media: imx477: Correctly set IMX477_PIXEL_RATE as a r/o control
270
271 This control is meant to be read-only, mark it as such.
272
273 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
274
275 drivers: media: i2c: imx296,imx477: Configure tigger_mode every time
276
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.
279
280 IMX477: Stop driving XVS on stop streaming, to avoid spurious pulses.
281
282 Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
283
284 media: i2c: imx477: Squash fixes
285
286 imx477: make trigger-mode more configurable
287
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.
291
292 Signed-off-by: Erik Botö <erik.boto@gmail.com>
293
294 drivers: media: imx477: Add V4L2_CID_LINK_FREQ control
295
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.
299
300 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
301 ---
302 .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
303 MAINTAINERS | 8 +
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
310
311 --- /dev/null
312 +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
313 @@ -0,0 +1,113 @@
314 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
315 +%YAML 1.2
316 +---
317 +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
318 +$schema: http://devicetree.org/meta-schemas/core.yaml#
319 +
320 +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
321 +
322 +maintainers:
323 + - Naushir Patuck <naush@raspberypi.com>
324 +
325 +description: |-
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
330 + 4 data lanes.
331 +
332 +properties:
333 + compatible:
334 + const: sony,imx477
335 +
336 + reg:
337 + description: I2C device address
338 + maxItems: 1
339 +
340 + clocks:
341 + maxItems: 1
342 +
343 + VDIG-supply:
344 + description:
345 + Digital I/O voltage supply, 1.05 volts
346 +
347 + VANA-supply:
348 + description:
349 + Analog voltage supply, 2.8 volts
350 +
351 + VDDL-supply:
352 + description:
353 + Digital core voltage supply, 1.8 volts
354 +
355 + reset-gpios:
356 + description: |-
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.
359 +
360 + # See ../video-interfaces.txt for more details
361 + port:
362 + type: object
363 + properties:
364 + endpoint:
365 + type: object
366 + properties:
367 + data-lanes:
368 + description: |-
369 + The sensor supports either two-lane, or four-lane operation.
370 + For two-lane operation the property must be set to <1 2>.
371 + items:
372 + - const: 1
373 + - const: 2
374 +
375 + clock-noncontinuous:
376 + type: boolean
377 + description: |-
378 + MIPI CSI-2 clock is non-continuous if this property is present,
379 + otherwise it's continuous.
380 +
381 + link-frequencies:
382 + allOf:
383 + - $ref: /schemas/types.yaml#/definitions/uint64-array
384 + description:
385 + Allowed data bus frequencies.
386 +
387 + required:
388 + - link-frequencies
389 +
390 +required:
391 + - compatible
392 + - reg
393 + - clocks
394 + - VANA-supply
395 + - VDIG-supply
396 + - VDDL-supply
397 + - port
398 +
399 +additionalProperties: false
400 +
401 +examples:
402 + - |
403 + i2c0 {
404 + #address-cells = <1>;
405 + #size-cells = <0>;
406 +
407 + imx477: sensor@10 {
408 + compatible = "sony,imx477";
409 + reg = <0x1a>;
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 */
414 +
415 + port {
416 + imx477_0: endpoint {
417 + remote-endpoint = <&csi1_ep>;
418 + data-lanes = <1 2>;
419 + clock-noncontinuous;
420 + link-frequencies = /bits/ 64 <450000000>;
421 + };
422 + };
423 + };
424 + };
425 +
426 +...
427 --- a/MAINTAINERS
428 +++ b/MAINTAINERS
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
432
433 +SONY IMX477 SENSOR DRIVER
434 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
435 +L: linux-media@vger.kernel.org
436 +S: Maintained
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
440 +
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.
449
450 +config VIDEO_IMX477
451 + tristate "Sony IMX477 sensor support"
452 + depends on I2C && VIDEO_DEV
453 + select VIDEO_V4L2_SUBDEV_API
454 + help
455 + This is a Video4Linux2 sensor driver for the Sony
456 + IMX477 camera. Also supports the Sony IMX378.
457 +
458 + To compile this driver as a module, choose M here: the
459 + module will be called imx477.
460 +
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
474 --- /dev/null
475 +++ b/drivers/media/i2c/imx477.c
476 @@ -0,0 +1,2339 @@
477 +// SPDX-License-Identifier: GPL-2.0
478 +/*
479 + * A V4L2 driver for Sony IMX477 cameras.
480 + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
481 + *
482 + * Based on Sony imx219 camera driver
483 + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
484 + */
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>
499 +
500 +static int dpc_enable = 1;
501 +module_param(dpc_enable, int, 0644);
502 +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
503 +
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");
507 +
508 +#define IMX477_REG_VALUE_08BIT 1
509 +#define IMX477_REG_VALUE_16BIT 2
510 +
511 +/* Chip ID */
512 +#define IMX477_REG_CHIP_ID 0x0016
513 +#define IMX477_CHIP_ID 0x0477
514 +#define IMX378_CHIP_ID 0x0378
515 +
516 +#define IMX477_REG_MODE_SELECT 0x0100
517 +#define IMX477_MODE_STANDBY 0x00
518 +#define IMX477_MODE_STREAMING 0x01
519 +
520 +#define IMX477_REG_ORIENTATION 0x101
521 +
522 +#define IMX477_XCLK_FREQ 24000000
523 +
524 +#define IMX477_DEFAULT_LINK_FREQ 450000000
525 +
526 +/* Pixel rate is fixed at 840MHz for all the modes */
527 +#define IMX477_PIXEL_RATE 840000000
528 +
529 +/* V_TIMING internal */
530 +#define IMX477_REG_FRAME_LENGTH 0x0340
531 +#define IMX477_FRAME_LENGTH_MAX 0xffdc
532 +
533 +/* H_TIMING internal */
534 +#define IMX477_REG_LINE_LENGTH 0x0342
535 +#define IMX477_LINE_LENGTH_MAX 0xfff0
536 +
537 +/* Long exposure multiplier */
538 +#define IMX477_LONG_EXP_SHIFT_MAX 7
539 +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
540 +
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)
549 +
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
556 +
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
563 +
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
571 +
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
584 +
585 +/* Trigger mode */
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
590 +
591 +/* Embedded metadata stream structure */
592 +#define IMX477_EMBEDDED_LINE_WIDTH 16384
593 +#define IMX477_NUM_EMBEDDED_LINES 1
594 +
595 +enum pad_types {
596 + IMAGE_PAD,
597 + METADATA_PAD,
598 + NUM_PADS
599 +};
600 +
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
608 +
609 +struct imx477_reg {
610 + u16 address;
611 + u8 val;
612 +};
613 +
614 +struct imx477_reg_list {
615 + unsigned int num_of_regs;
616 + const struct imx477_reg *regs;
617 +};
618 +
619 +/* Mode : resolution and related config&values */
620 +struct imx477_mode {
621 + /* Frame width */
622 + unsigned int width;
623 +
624 + /* Frame height */
625 + unsigned int height;
626 +
627 + /* H-timing in pixels */
628 + unsigned int line_length_pix;
629 +
630 + /* Analog crop rectangle. */
631 + struct v4l2_rect crop;
632 +
633 + /* Highest possible framerate. */
634 + struct v4l2_fract timeperframe_min;
635 +
636 + /* Default framerate. */
637 + struct v4l2_fract timeperframe_default;
638 +
639 + /* Default register values */
640 + struct imx477_reg_list reg_list;
641 +};
642 +
643 +static const s64 imx477_link_freq_menu[] = {
644 + IMX477_DEFAULT_LINK_FREQ,
645 +};
646 +
647 +static const struct imx477_reg mode_common_regs[] = {
648 + {0x0136, 0x18},
649 + {0x0137, 0x00},
650 + {0x0138, 0x01},
651 + {0xe000, 0x00},
652 + {0xe07a, 0x01},
653 + {0x0808, 0x02},
654 + {0x4ae9, 0x18},
655 + {0x4aea, 0x08},
656 + {0xf61c, 0x04},
657 + {0xf61e, 0x04},
658 + {0x4ae9, 0x21},
659 + {0x4aea, 0x80},
660 + {0x38a8, 0x1f},
661 + {0x38a9, 0xff},
662 + {0x38aa, 0x1f},
663 + {0x38ab, 0xff},
664 + {0x55d4, 0x00},
665 + {0x55d5, 0x00},
666 + {0x55d6, 0x07},
667 + {0x55d7, 0xff},
668 + {0x55e8, 0x07},
669 + {0x55e9, 0xff},
670 + {0x55ea, 0x00},
671 + {0x55eb, 0x00},
672 + {0x574c, 0x07},
673 + {0x574d, 0xff},
674 + {0x574e, 0x00},
675 + {0x574f, 0x00},
676 + {0x5754, 0x00},
677 + {0x5755, 0x00},
678 + {0x5756, 0x07},
679 + {0x5757, 0xff},
680 + {0x5973, 0x04},
681 + {0x5974, 0x01},
682 + {0x5d13, 0xc3},
683 + {0x5d14, 0x58},
684 + {0x5d15, 0xa3},
685 + {0x5d16, 0x1d},
686 + {0x5d17, 0x65},
687 + {0x5d18, 0x8c},
688 + {0x5d1a, 0x06},
689 + {0x5d1b, 0xa9},
690 + {0x5d1c, 0x45},
691 + {0x5d1d, 0x3a},
692 + {0x5d1e, 0xab},
693 + {0x5d1f, 0x15},
694 + {0x5d21, 0x0e},
695 + {0x5d22, 0x52},
696 + {0x5d23, 0xaa},
697 + {0x5d24, 0x7d},
698 + {0x5d25, 0x57},
699 + {0x5d26, 0xa8},
700 + {0x5d37, 0x5a},
701 + {0x5d38, 0x5a},
702 + {0x5d77, 0x7f},
703 + {0x7b75, 0x0e},
704 + {0x7b76, 0x0b},
705 + {0x7b77, 0x08},
706 + {0x7b78, 0x0a},
707 + {0x7b79, 0x47},
708 + {0x7b7c, 0x00},
709 + {0x7b7d, 0x00},
710 + {0x8d1f, 0x00},
711 + {0x8d27, 0x00},
712 + {0x9004, 0x03},
713 + {0x9200, 0x50},
714 + {0x9201, 0x6c},
715 + {0x9202, 0x71},
716 + {0x9203, 0x00},
717 + {0x9204, 0x71},
718 + {0x9205, 0x01},
719 + {0x9371, 0x6a},
720 + {0x9373, 0x6a},
721 + {0x9375, 0x64},
722 + {0x991a, 0x00},
723 + {0x996b, 0x8c},
724 + {0x996c, 0x64},
725 + {0x996d, 0x50},
726 + {0x9a4c, 0x0d},
727 + {0x9a4d, 0x0d},
728 + {0xa001, 0x0a},
729 + {0xa003, 0x0a},
730 + {0xa005, 0x0a},
731 + {0xa006, 0x01},
732 + {0xa007, 0xc0},
733 + {0xa009, 0xc0},
734 + {0x3d8a, 0x01},
735 + {0x4421, 0x04},
736 + {0x7b3b, 0x01},
737 + {0x7b4c, 0x00},
738 + {0x9905, 0x00},
739 + {0x9907, 0x00},
740 + {0x9909, 0x00},
741 + {0x990b, 0x00},
742 + {0x9944, 0x3c},
743 + {0x9947, 0x3c},
744 + {0x994a, 0x8c},
745 + {0x994b, 0x50},
746 + {0x994c, 0x1b},
747 + {0x994d, 0x8c},
748 + {0x994e, 0x50},
749 + {0x994f, 0x1b},
750 + {0x9950, 0x8c},
751 + {0x9951, 0x1b},
752 + {0x9952, 0x0a},
753 + {0x9953, 0x8c},
754 + {0x9954, 0x1b},
755 + {0x9955, 0x0a},
756 + {0x9a13, 0x04},
757 + {0x9a14, 0x04},
758 + {0x9a19, 0x00},
759 + {0x9a1c, 0x04},
760 + {0x9a1d, 0x04},
761 + {0x9a26, 0x05},
762 + {0x9a27, 0x05},
763 + {0x9a2c, 0x01},
764 + {0x9a2d, 0x03},
765 + {0x9a2f, 0x05},
766 + {0x9a30, 0x05},
767 + {0x9a41, 0x00},
768 + {0x9a46, 0x00},
769 + {0x9a47, 0x00},
770 + {0x9c17, 0x35},
771 + {0x9c1d, 0x31},
772 + {0x9c29, 0x50},
773 + {0x9c3b, 0x2f},
774 + {0x9c41, 0x6b},
775 + {0x9c47, 0x2d},
776 + {0x9c4d, 0x40},
777 + {0x9c6b, 0x00},
778 + {0x9c71, 0xc8},
779 + {0x9c73, 0x32},
780 + {0x9c75, 0x04},
781 + {0x9c7d, 0x2d},
782 + {0x9c83, 0x40},
783 + {0x9c94, 0x3f},
784 + {0x9c95, 0x3f},
785 + {0x9c96, 0x3f},
786 + {0x9c97, 0x00},
787 + {0x9c98, 0x00},
788 + {0x9c99, 0x00},
789 + {0x9c9a, 0x3f},
790 + {0x9c9b, 0x3f},
791 + {0x9c9c, 0x3f},
792 + {0x9ca0, 0x0f},
793 + {0x9ca1, 0x0f},
794 + {0x9ca2, 0x0f},
795 + {0x9ca3, 0x00},
796 + {0x9ca4, 0x00},
797 + {0x9ca5, 0x00},
798 + {0x9ca6, 0x1e},
799 + {0x9ca7, 0x1e},
800 + {0x9ca8, 0x1e},
801 + {0x9ca9, 0x00},
802 + {0x9caa, 0x00},
803 + {0x9cab, 0x00},
804 + {0x9cac, 0x09},
805 + {0x9cad, 0x09},
806 + {0x9cae, 0x09},
807 + {0x9cbd, 0x50},
808 + {0x9cbf, 0x50},
809 + {0x9cc1, 0x50},
810 + {0x9cc3, 0x40},
811 + {0x9cc5, 0x40},
812 + {0x9cc7, 0x40},
813 + {0x9cc9, 0x0a},
814 + {0x9ccb, 0x0a},
815 + {0x9ccd, 0x0a},
816 + {0x9d17, 0x35},
817 + {0x9d1d, 0x31},
818 + {0x9d29, 0x50},
819 + {0x9d3b, 0x2f},
820 + {0x9d41, 0x6b},
821 + {0x9d47, 0x42},
822 + {0x9d4d, 0x5a},
823 + {0x9d6b, 0x00},
824 + {0x9d71, 0xc8},
825 + {0x9d73, 0x32},
826 + {0x9d75, 0x04},
827 + {0x9d7d, 0x42},
828 + {0x9d83, 0x5a},
829 + {0x9d94, 0x3f},
830 + {0x9d95, 0x3f},
831 + {0x9d96, 0x3f},
832 + {0x9d97, 0x00},
833 + {0x9d98, 0x00},
834 + {0x9d99, 0x00},
835 + {0x9d9a, 0x3f},
836 + {0x9d9b, 0x3f},
837 + {0x9d9c, 0x3f},
838 + {0x9d9d, 0x1f},
839 + {0x9d9e, 0x1f},
840 + {0x9d9f, 0x1f},
841 + {0x9da0, 0x0f},
842 + {0x9da1, 0x0f},
843 + {0x9da2, 0x0f},
844 + {0x9da3, 0x00},
845 + {0x9da4, 0x00},
846 + {0x9da5, 0x00},
847 + {0x9da6, 0x1e},
848 + {0x9da7, 0x1e},
849 + {0x9da8, 0x1e},
850 + {0x9da9, 0x00},
851 + {0x9daa, 0x00},
852 + {0x9dab, 0x00},
853 + {0x9dac, 0x09},
854 + {0x9dad, 0x09},
855 + {0x9dae, 0x09},
856 + {0x9dc9, 0x0a},
857 + {0x9dcb, 0x0a},
858 + {0x9dcd, 0x0a},
859 + {0x9e17, 0x35},
860 + {0x9e1d, 0x31},
861 + {0x9e29, 0x50},
862 + {0x9e3b, 0x2f},
863 + {0x9e41, 0x6b},
864 + {0x9e47, 0x2d},
865 + {0x9e4d, 0x40},
866 + {0x9e6b, 0x00},
867 + {0x9e71, 0xc8},
868 + {0x9e73, 0x32},
869 + {0x9e75, 0x04},
870 + {0x9e94, 0x0f},
871 + {0x9e95, 0x0f},
872 + {0x9e96, 0x0f},
873 + {0x9e97, 0x00},
874 + {0x9e98, 0x00},
875 + {0x9e99, 0x00},
876 + {0x9ea0, 0x0f},
877 + {0x9ea1, 0x0f},
878 + {0x9ea2, 0x0f},
879 + {0x9ea3, 0x00},
880 + {0x9ea4, 0x00},
881 + {0x9ea5, 0x00},
882 + {0x9ea6, 0x3f},
883 + {0x9ea7, 0x3f},
884 + {0x9ea8, 0x3f},
885 + {0x9ea9, 0x00},
886 + {0x9eaa, 0x00},
887 + {0x9eab, 0x00},
888 + {0x9eac, 0x09},
889 + {0x9ead, 0x09},
890 + {0x9eae, 0x09},
891 + {0x9ec9, 0x0a},
892 + {0x9ecb, 0x0a},
893 + {0x9ecd, 0x0a},
894 + {0x9f17, 0x35},
895 + {0x9f1d, 0x31},
896 + {0x9f29, 0x50},
897 + {0x9f3b, 0x2f},
898 + {0x9f41, 0x6b},
899 + {0x9f47, 0x42},
900 + {0x9f4d, 0x5a},
901 + {0x9f6b, 0x00},
902 + {0x9f71, 0xc8},
903 + {0x9f73, 0x32},
904 + {0x9f75, 0x04},
905 + {0x9f94, 0x0f},
906 + {0x9f95, 0x0f},
907 + {0x9f96, 0x0f},
908 + {0x9f97, 0x00},
909 + {0x9f98, 0x00},
910 + {0x9f99, 0x00},
911 + {0x9f9a, 0x2f},
912 + {0x9f9b, 0x2f},
913 + {0x9f9c, 0x2f},
914 + {0x9f9d, 0x00},
915 + {0x9f9e, 0x00},
916 + {0x9f9f, 0x00},
917 + {0x9fa0, 0x0f},
918 + {0x9fa1, 0x0f},
919 + {0x9fa2, 0x0f},
920 + {0x9fa3, 0x00},
921 + {0x9fa4, 0x00},
922 + {0x9fa5, 0x00},
923 + {0x9fa6, 0x1e},
924 + {0x9fa7, 0x1e},
925 + {0x9fa8, 0x1e},
926 + {0x9fa9, 0x00},
927 + {0x9faa, 0x00},
928 + {0x9fab, 0x00},
929 + {0x9fac, 0x09},
930 + {0x9fad, 0x09},
931 + {0x9fae, 0x09},
932 + {0x9fc9, 0x0a},
933 + {0x9fcb, 0x0a},
934 + {0x9fcd, 0x0a},
935 + {0xa14b, 0xff},
936 + {0xa151, 0x0c},
937 + {0xa153, 0x50},
938 + {0xa155, 0x02},
939 + {0xa157, 0x00},
940 + {0xa1ad, 0xff},
941 + {0xa1b3, 0x0c},
942 + {0xa1b5, 0x50},
943 + {0xa1b9, 0x00},
944 + {0xa24b, 0xff},
945 + {0xa257, 0x00},
946 + {0xa2ad, 0xff},
947 + {0xa2b9, 0x00},
948 + {0xb21f, 0x04},
949 + {0xb35c, 0x00},
950 + {0xb35e, 0x08},
951 + {0x0112, 0x0c},
952 + {0x0113, 0x0c},
953 + {0x0114, 0x01},
954 + {0x0350, 0x00},
955 + {0xbcf1, 0x02},
956 + {0x3ff9, 0x01},
957 +};
958 +
959 +/* 12 mpix 10fps */
960 +static const struct imx477_reg mode_4056x3040_regs[] = {
961 + {0x0342, 0x5d},
962 + {0x0343, 0xc0},
963 + {0x0344, 0x00},
964 + {0x0345, 0x00},
965 + {0x0346, 0x00},
966 + {0x0347, 0x00},
967 + {0x0348, 0x0f},
968 + {0x0349, 0xd7},
969 + {0x034a, 0x0b},
970 + {0x034b, 0xdf},
971 + {0x00e3, 0x00},
972 + {0x00e4, 0x00},
973 + {0x00fc, 0x0a},
974 + {0x00fd, 0x0a},
975 + {0x00fe, 0x0a},
976 + {0x00ff, 0x0a},
977 + {0x0220, 0x00},
978 + {0x0221, 0x11},
979 + {0x0381, 0x01},
980 + {0x0383, 0x01},
981 + {0x0385, 0x01},
982 + {0x0387, 0x01},
983 + {0x0900, 0x00},
984 + {0x0901, 0x11},
985 + {0x0902, 0x02},
986 + {0x3140, 0x02},
987 + {0x3c00, 0x00},
988 + {0x3c01, 0x03},
989 + {0x3c02, 0xa2},
990 + {0x3f0d, 0x01},
991 + {0x5748, 0x07},
992 + {0x5749, 0xff},
993 + {0x574a, 0x00},
994 + {0x574b, 0x00},
995 + {0x7b75, 0x0a},
996 + {0x7b76, 0x0c},
997 + {0x7b77, 0x07},
998 + {0x7b78, 0x06},
999 + {0x7b79, 0x3c},
1000 + {0x7b53, 0x01},
1001 + {0x9369, 0x5a},
1002 + {0x936b, 0x55},
1003 + {0x936d, 0x28},
1004 + {0x9304, 0x00},
1005 + {0x9305, 0x00},
1006 + {0x9e9a, 0x2f},
1007 + {0x9e9b, 0x2f},
1008 + {0x9e9c, 0x2f},
1009 + {0x9e9d, 0x00},
1010 + {0x9e9e, 0x00},
1011 + {0x9e9f, 0x00},
1012 + {0xa2a9, 0x60},
1013 + {0xa2b7, 0x00},
1014 + {0x0401, 0x00},
1015 + {0x0404, 0x00},
1016 + {0x0405, 0x10},
1017 + {0x0408, 0x00},
1018 + {0x0409, 0x00},
1019 + {0x040a, 0x00},
1020 + {0x040b, 0x00},
1021 + {0x040c, 0x0f},
1022 + {0x040d, 0xd8},
1023 + {0x040e, 0x0b},
1024 + {0x040f, 0xe0},
1025 + {0x034c, 0x0f},
1026 + {0x034d, 0xd8},
1027 + {0x034e, 0x0b},
1028 + {0x034f, 0xe0},
1029 + {0x0301, 0x05},
1030 + {0x0303, 0x02},
1031 + {0x0305, 0x04},
1032 + {0x0306, 0x01},
1033 + {0x0307, 0x5e},
1034 + {0x0309, 0x0c},
1035 + {0x030b, 0x02},
1036 + {0x030d, 0x02},
1037 + {0x030e, 0x00},
1038 + {0x030f, 0x96},
1039 + {0x0310, 0x01},
1040 + {0x0820, 0x07},
1041 + {0x0821, 0x08},
1042 + {0x0822, 0x00},
1043 + {0x0823, 0x00},
1044 + {0x080a, 0x00},
1045 + {0x080b, 0x7f},
1046 + {0x080c, 0x00},
1047 + {0x080d, 0x4f},
1048 + {0x080e, 0x00},
1049 + {0x080f, 0x77},
1050 + {0x0810, 0x00},
1051 + {0x0811, 0x5f},
1052 + {0x0812, 0x00},
1053 + {0x0813, 0x57},
1054 + {0x0814, 0x00},
1055 + {0x0815, 0x4f},
1056 + {0x0816, 0x01},
1057 + {0x0817, 0x27},
1058 + {0x0818, 0x00},
1059 + {0x0819, 0x3f},
1060 + {0xe04c, 0x00},
1061 + {0xe04d, 0x7f},
1062 + {0xe04e, 0x00},
1063 + {0xe04f, 0x1f},
1064 + {0x3e20, 0x01},
1065 + {0x3e37, 0x00},
1066 + {0x3f50, 0x00},
1067 + {0x3f56, 0x02},
1068 + {0x3f57, 0xae},
1069 +};
1070 +
1071 +/* 2x2 binned. 40fps */
1072 +static const struct imx477_reg mode_2028x1520_regs[] = {
1073 + {0x0342, 0x31},
1074 + {0x0343, 0xc4},
1075 + {0x0344, 0x00},
1076 + {0x0345, 0x00},
1077 + {0x0346, 0x00},
1078 + {0x0347, 0x00},
1079 + {0x0348, 0x0f},
1080 + {0x0349, 0xd7},
1081 + {0x034a, 0x0b},
1082 + {0x034b, 0xdf},
1083 + {0x0220, 0x00},
1084 + {0x0221, 0x11},
1085 + {0x0381, 0x01},
1086 + {0x0383, 0x01},
1087 + {0x0385, 0x01},
1088 + {0x0387, 0x01},
1089 + {0x0900, 0x01},
1090 + {0x0901, 0x22},
1091 + {0x0902, 0x02},
1092 + {0x3140, 0x02},
1093 + {0x3c00, 0x00},
1094 + {0x3c01, 0x03},
1095 + {0x3c02, 0xa2},
1096 + {0x3f0d, 0x01},
1097 + {0x5748, 0x07},
1098 + {0x5749, 0xff},
1099 + {0x574a, 0x00},
1100 + {0x574b, 0x00},
1101 + {0x7b53, 0x01},
1102 + {0x9369, 0x73},
1103 + {0x936b, 0x64},
1104 + {0x936d, 0x5f},
1105 + {0x9304, 0x00},
1106 + {0x9305, 0x00},
1107 + {0x9e9a, 0x2f},
1108 + {0x9e9b, 0x2f},
1109 + {0x9e9c, 0x2f},
1110 + {0x9e9d, 0x00},
1111 + {0x9e9e, 0x00},
1112 + {0x9e9f, 0x00},
1113 + {0xa2a9, 0x60},
1114 + {0xa2b7, 0x00},
1115 + {0x0401, 0x00},
1116 + {0x0404, 0x00},
1117 + {0x0405, 0x20},
1118 + {0x0408, 0x00},
1119 + {0x0409, 0x00},
1120 + {0x040a, 0x00},
1121 + {0x040b, 0x00},
1122 + {0x040c, 0x0f},
1123 + {0x040d, 0xd8},
1124 + {0x040e, 0x0b},
1125 + {0x040f, 0xe0},
1126 + {0x034c, 0x07},
1127 + {0x034d, 0xec},
1128 + {0x034e, 0x05},
1129 + {0x034f, 0xf0},
1130 + {0x0301, 0x05},
1131 + {0x0303, 0x02},
1132 + {0x0305, 0x04},
1133 + {0x0306, 0x01},
1134 + {0x0307, 0x5e},
1135 + {0x0309, 0x0c},
1136 + {0x030b, 0x02},
1137 + {0x030d, 0x02},
1138 + {0x030e, 0x00},
1139 + {0x030f, 0x96},
1140 + {0x0310, 0x01},
1141 + {0x0820, 0x07},
1142 + {0x0821, 0x08},
1143 + {0x0822, 0x00},
1144 + {0x0823, 0x00},
1145 + {0x080a, 0x00},
1146 + {0x080b, 0x7f},
1147 + {0x080c, 0x00},
1148 + {0x080d, 0x4f},
1149 + {0x080e, 0x00},
1150 + {0x080f, 0x77},
1151 + {0x0810, 0x00},
1152 + {0x0811, 0x5f},
1153 + {0x0812, 0x00},
1154 + {0x0813, 0x57},
1155 + {0x0814, 0x00},
1156 + {0x0815, 0x4f},
1157 + {0x0816, 0x01},
1158 + {0x0817, 0x27},
1159 + {0x0818, 0x00},
1160 + {0x0819, 0x3f},
1161 + {0xe04c, 0x00},
1162 + {0xe04d, 0x7f},
1163 + {0xe04e, 0x00},
1164 + {0xe04f, 0x1f},
1165 + {0x3e20, 0x01},
1166 + {0x3e37, 0x00},
1167 + {0x3f50, 0x00},
1168 + {0x3f56, 0x01},
1169 + {0x3f57, 0x6c},
1170 +};
1171 +
1172 +/* 1080p cropped mode */
1173 +static const struct imx477_reg mode_2028x1080_regs[] = {
1174 + {0x0342, 0x31},
1175 + {0x0343, 0xc4},
1176 + {0x0344, 0x00},
1177 + {0x0345, 0x00},
1178 + {0x0346, 0x01},
1179 + {0x0347, 0xb8},
1180 + {0x0348, 0x0f},
1181 + {0x0349, 0xd7},
1182 + {0x034a, 0x0a},
1183 + {0x034b, 0x27},
1184 + {0x0220, 0x00},
1185 + {0x0221, 0x11},
1186 + {0x0381, 0x01},
1187 + {0x0383, 0x01},
1188 + {0x0385, 0x01},
1189 + {0x0387, 0x01},
1190 + {0x0900, 0x01},
1191 + {0x0901, 0x22},
1192 + {0x0902, 0x02},
1193 + {0x3140, 0x02},
1194 + {0x3c00, 0x00},
1195 + {0x3c01, 0x03},
1196 + {0x3c02, 0xa2},
1197 + {0x3f0d, 0x01},
1198 + {0x5748, 0x07},
1199 + {0x5749, 0xff},
1200 + {0x574a, 0x00},
1201 + {0x574b, 0x00},
1202 + {0x7b53, 0x01},
1203 + {0x9369, 0x73},
1204 + {0x936b, 0x64},
1205 + {0x936d, 0x5f},
1206 + {0x9304, 0x00},
1207 + {0x9305, 0x00},
1208 + {0x9e9a, 0x2f},
1209 + {0x9e9b, 0x2f},
1210 + {0x9e9c, 0x2f},
1211 + {0x9e9d, 0x00},
1212 + {0x9e9e, 0x00},
1213 + {0x9e9f, 0x00},
1214 + {0xa2a9, 0x60},
1215 + {0xa2b7, 0x00},
1216 + {0x0401, 0x00},
1217 + {0x0404, 0x00},
1218 + {0x0405, 0x20},
1219 + {0x0408, 0x00},
1220 + {0x0409, 0x00},
1221 + {0x040a, 0x00},
1222 + {0x040b, 0x00},
1223 + {0x040c, 0x0f},
1224 + {0x040d, 0xd8},
1225 + {0x040e, 0x04},
1226 + {0x040f, 0x38},
1227 + {0x034c, 0x07},
1228 + {0x034d, 0xec},
1229 + {0x034e, 0x04},
1230 + {0x034f, 0x38},
1231 + {0x0301, 0x05},
1232 + {0x0303, 0x02},
1233 + {0x0305, 0x04},
1234 + {0x0306, 0x01},
1235 + {0x0307, 0x5e},
1236 + {0x0309, 0x0c},
1237 + {0x030b, 0x02},
1238 + {0x030d, 0x02},
1239 + {0x030e, 0x00},
1240 + {0x030f, 0x96},
1241 + {0x0310, 0x01},
1242 + {0x0820, 0x07},
1243 + {0x0821, 0x08},
1244 + {0x0822, 0x00},
1245 + {0x0823, 0x00},
1246 + {0x080a, 0x00},
1247 + {0x080b, 0x7f},
1248 + {0x080c, 0x00},
1249 + {0x080d, 0x4f},
1250 + {0x080e, 0x00},
1251 + {0x080f, 0x77},
1252 + {0x0810, 0x00},
1253 + {0x0811, 0x5f},
1254 + {0x0812, 0x00},
1255 + {0x0813, 0x57},
1256 + {0x0814, 0x00},
1257 + {0x0815, 0x4f},
1258 + {0x0816, 0x01},
1259 + {0x0817, 0x27},
1260 + {0x0818, 0x00},
1261 + {0x0819, 0x3f},
1262 + {0xe04c, 0x00},
1263 + {0xe04d, 0x7f},
1264 + {0xe04e, 0x00},
1265 + {0xe04f, 0x1f},
1266 + {0x3e20, 0x01},
1267 + {0x3e37, 0x00},
1268 + {0x3f50, 0x00},
1269 + {0x3f56, 0x01},
1270 + {0x3f57, 0x6c},
1271 +};
1272 +
1273 +/* 4x4 binned. 120fps */
1274 +static const struct imx477_reg mode_1332x990_regs[] = {
1275 + {0x420b, 0x01},
1276 + {0x990c, 0x00},
1277 + {0x990d, 0x08},
1278 + {0x9956, 0x8c},
1279 + {0x9957, 0x64},
1280 + {0x9958, 0x50},
1281 + {0x9a48, 0x06},
1282 + {0x9a49, 0x06},
1283 + {0x9a4a, 0x06},
1284 + {0x9a4b, 0x06},
1285 + {0x9a4c, 0x06},
1286 + {0x9a4d, 0x06},
1287 + {0x0112, 0x0a},
1288 + {0x0113, 0x0a},
1289 + {0x0114, 0x01},
1290 + {0x0342, 0x1a},
1291 + {0x0343, 0x08},
1292 + {0x0340, 0x04},
1293 + {0x0341, 0x1a},
1294 + {0x0344, 0x00},
1295 + {0x0345, 0x00},
1296 + {0x0346, 0x02},
1297 + {0x0347, 0x10},
1298 + {0x0348, 0x0f},
1299 + {0x0349, 0xd7},
1300 + {0x034a, 0x09},
1301 + {0x034b, 0xcf},
1302 + {0x00e3, 0x00},
1303 + {0x00e4, 0x00},
1304 + {0x00fc, 0x0a},
1305 + {0x00fd, 0x0a},
1306 + {0x00fe, 0x0a},
1307 + {0x00ff, 0x0a},
1308 + {0xe013, 0x00},
1309 + {0x0220, 0x00},
1310 + {0x0221, 0x11},
1311 + {0x0381, 0x01},
1312 + {0x0383, 0x01},
1313 + {0x0385, 0x01},
1314 + {0x0387, 0x01},
1315 + {0x0900, 0x01},
1316 + {0x0901, 0x22},
1317 + {0x0902, 0x02},
1318 + {0x3140, 0x02},
1319 + {0x3c00, 0x00},
1320 + {0x3c01, 0x01},
1321 + {0x3c02, 0x9c},
1322 + {0x3f0d, 0x00},
1323 + {0x5748, 0x00},
1324 + {0x5749, 0x00},
1325 + {0x574a, 0x00},
1326 + {0x574b, 0xa4},
1327 + {0x7b75, 0x0e},
1328 + {0x7b76, 0x09},
1329 + {0x7b77, 0x08},
1330 + {0x7b78, 0x06},
1331 + {0x7b79, 0x34},
1332 + {0x7b53, 0x00},
1333 + {0x9369, 0x73},
1334 + {0x936b, 0x64},
1335 + {0x936d, 0x5f},
1336 + {0x9304, 0x03},
1337 + {0x9305, 0x80},
1338 + {0x9e9a, 0x2f},
1339 + {0x9e9b, 0x2f},
1340 + {0x9e9c, 0x2f},
1341 + {0x9e9d, 0x00},
1342 + {0x9e9e, 0x00},
1343 + {0x9e9f, 0x00},
1344 + {0xa2a9, 0x27},
1345 + {0xa2b7, 0x03},
1346 + {0x0401, 0x00},
1347 + {0x0404, 0x00},
1348 + {0x0405, 0x10},
1349 + {0x0408, 0x01},
1350 + {0x0409, 0x5c},
1351 + {0x040a, 0x00},
1352 + {0x040b, 0x00},
1353 + {0x040c, 0x05},
1354 + {0x040d, 0x34},
1355 + {0x040e, 0x03},
1356 + {0x040f, 0xde},
1357 + {0x034c, 0x05},
1358 + {0x034d, 0x34},
1359 + {0x034e, 0x03},
1360 + {0x034f, 0xde},
1361 + {0x0301, 0x05},
1362 + {0x0303, 0x02},
1363 + {0x0305, 0x02},
1364 + {0x0306, 0x00},
1365 + {0x0307, 0xaf},
1366 + {0x0309, 0x0a},
1367 + {0x030b, 0x02},
1368 + {0x030d, 0x02},
1369 + {0x030e, 0x00},
1370 + {0x030f, 0x96},
1371 + {0x0310, 0x01},
1372 + {0x0820, 0x07},
1373 + {0x0821, 0x08},
1374 + {0x0822, 0x00},
1375 + {0x0823, 0x00},
1376 + {0x080a, 0x00},
1377 + {0x080b, 0x7f},
1378 + {0x080c, 0x00},
1379 + {0x080d, 0x4f},
1380 + {0x080e, 0x00},
1381 + {0x080f, 0x77},
1382 + {0x0810, 0x00},
1383 + {0x0811, 0x5f},
1384 + {0x0812, 0x00},
1385 + {0x0813, 0x57},
1386 + {0x0814, 0x00},
1387 + {0x0815, 0x4f},
1388 + {0x0816, 0x01},
1389 + {0x0817, 0x27},
1390 + {0x0818, 0x00},
1391 + {0x0819, 0x3f},
1392 + {0xe04c, 0x00},
1393 + {0xe04d, 0x5f},
1394 + {0xe04e, 0x00},
1395 + {0xe04f, 0x1f},
1396 + {0x3e20, 0x01},
1397 + {0x3e37, 0x00},
1398 + {0x3f50, 0x00},
1399 + {0x3f56, 0x00},
1400 + {0x3f57, 0xbf},
1401 +};
1402 +
1403 +/* Mode configs */
1404 +static const struct imx477_mode supported_modes_12bit[] = {
1405 + {
1406 + /* 12MPix 10fps mode */
1407 + .width = 4056,
1408 + .height = 3040,
1409 + .line_length_pix = 0x5dc0,
1410 + .crop = {
1411 + .left = IMX477_PIXEL_ARRAY_LEFT,
1412 + .top = IMX477_PIXEL_ARRAY_TOP,
1413 + .width = 4056,
1414 + .height = 3040,
1415 + },
1416 + .timeperframe_min = {
1417 + .numerator = 100,
1418 + .denominator = 1000
1419 + },
1420 + .timeperframe_default = {
1421 + .numerator = 100,
1422 + .denominator = 1000
1423 + },
1424 + .reg_list = {
1425 + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
1426 + .regs = mode_4056x3040_regs,
1427 + },
1428 + },
1429 + {
1430 + /* 2x2 binned 40fps mode */
1431 + .width = 2028,
1432 + .height = 1520,
1433 + .line_length_pix = 0x31c4,
1434 + .crop = {
1435 + .left = IMX477_PIXEL_ARRAY_LEFT,
1436 + .top = IMX477_PIXEL_ARRAY_TOP,
1437 + .width = 4056,
1438 + .height = 3040,
1439 + },
1440 + .timeperframe_min = {
1441 + .numerator = 100,
1442 + .denominator = 4000
1443 + },
1444 + .timeperframe_default = {
1445 + .numerator = 100,
1446 + .denominator = 3000
1447 + },
1448 + .reg_list = {
1449 + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
1450 + .regs = mode_2028x1520_regs,
1451 + },
1452 + },
1453 + {
1454 + /* 1080p 50fps cropped mode */
1455 + .width = 2028,
1456 + .height = 1080,
1457 + .line_length_pix = 0x31c4,
1458 + .crop = {
1459 + .left = IMX477_PIXEL_ARRAY_LEFT,
1460 + .top = IMX477_PIXEL_ARRAY_TOP + 440,
1461 + .width = 4056,
1462 + .height = 2160,
1463 + },
1464 + .timeperframe_min = {
1465 + .numerator = 100,
1466 + .denominator = 5000
1467 + },
1468 + .timeperframe_default = {
1469 + .numerator = 100,
1470 + .denominator = 3000
1471 + },
1472 + .reg_list = {
1473 + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
1474 + .regs = mode_2028x1080_regs,
1475 + },
1476 + }
1477 +};
1478 +
1479 +static const struct imx477_mode supported_modes_10bit[] = {
1480 + {
1481 + /* 120fps. 2x2 binned and cropped */
1482 + .width = 1332,
1483 + .height = 990,
1484 + .line_length_pix = 6664,
1485 + .crop = {
1486 + /*
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.
1493 + */
1494 + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1495 + .top = IMX477_PIXEL_ARRAY_TOP + 528,
1496 + .width = 2664,
1497 + .height = 1980,
1498 + },
1499 + .timeperframe_min = {
1500 + .numerator = 100,
1501 + .denominator = 12000
1502 + },
1503 + .timeperframe_default = {
1504 + .numerator = 100,
1505 + .denominator = 12000
1506 + },
1507 + .reg_list = {
1508 + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1509 + .regs = mode_1332x990_regs,
1510 + }
1511 + }
1512 +};
1513 +
1514 +/*
1515 + * The supported formats.
1516 + * This table MUST contain 4 entries per format, to cover the various flip
1517 + * combinations in the order
1518 + * - no flip
1519 + * - h flip
1520 + * - v flip
1521 + * - h&v flips
1522 + */
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,
1534 +};
1535 +
1536 +static const char * const imx477_test_pattern_menu[] = {
1537 + "Disabled",
1538 + "Color Bars",
1539 + "Solid Color",
1540 + "Grey Color Bars",
1541 + "PN9"
1542 +};
1543 +
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,
1550 +};
1551 +
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 */
1558 +};
1559 +
1560 +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1561 +
1562 +/*
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.
1566 + *
1567 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1568 + * in the datasheet) is much smaller - 600us.
1569 + */
1570 +#define IMX477_XCLR_MIN_DELAY_US 8000
1571 +#define IMX477_XCLR_DELAY_RANGE_US 1000
1572 +
1573 +struct imx477_compatible_data {
1574 + unsigned int chip_id;
1575 + struct imx477_reg_list extra_regs;
1576 +};
1577 +
1578 +struct imx477 {
1579 + struct v4l2_subdev sd;
1580 + struct media_pad pad[NUM_PADS];
1581 +
1582 + unsigned int fmt_code;
1583 +
1584 + struct clk *xclk;
1585 + u32 xclk_freq;
1586 +
1587 + struct gpio_desc *reset_gpio;
1588 + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1589 +
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;
1599 +
1600 + /* Current mode */
1601 + const struct imx477_mode *mode;
1602 +
1603 + /* Trigger mode */
1604 + int trigger_mode_of;
1605 +
1606 + /*
1607 + * Mutex for serialized access:
1608 + * Protect sensor module set pad format and start/stop streaming safely.
1609 + */
1610 + struct mutex mutex;
1611 +
1612 + /* Streaming on/off */
1613 + bool streaming;
1614 +
1615 + /* Rewrite common registers on stream on? */
1616 + bool common_regs_written;
1617 +
1618 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1619 + unsigned int long_exp_shift;
1620 +
1621 + /* Any extra information related to different compatible sensors */
1622 + const struct imx477_compatible_data *compatible_data;
1623 +};
1624 +
1625 +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1626 +{
1627 + return container_of(_sd, struct imx477, sd);
1628 +}
1629 +
1630 +static inline void get_mode_table(unsigned int code,
1631 + const struct imx477_mode **mode_list,
1632 + unsigned int *num_modes)
1633 +{
1634 + switch (code) {
1635 + /* 12-bit */
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);
1642 + break;
1643 + /* 10-bit */
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);
1650 + break;
1651 + default:
1652 + *mode_list = NULL;
1653 + *num_modes = 0;
1654 + }
1655 +}
1656 +
1657 +/* Read registers up to 2 at a time */
1658 +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1659 +{
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, };
1664 + int ret;
1665 +
1666 + if (len > 4)
1667 + return -EINVAL;
1668 +
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;
1674 +
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];
1680 +
1681 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1682 + if (ret != ARRAY_SIZE(msgs))
1683 + return -EIO;
1684 +
1685 + *val = get_unaligned_be32(data_buf);
1686 +
1687 + return 0;
1688 +}
1689 +
1690 +/* Write registers up to 2 at a time */
1691 +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1692 +{
1693 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1694 + u8 buf[6];
1695 +
1696 + if (len > 4)
1697 + return -EINVAL;
1698 +
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)
1702 + return -EIO;
1703 +
1704 + return 0;
1705 +}
1706 +
1707 +/* Write a list of registers */
1708 +static int imx477_write_regs(struct imx477 *imx477,
1709 + const struct imx477_reg *regs, u32 len)
1710 +{
1711 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1712 + unsigned int i;
1713 + int ret;
1714 +
1715 + for (i = 0; i < len; i++) {
1716 + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1717 + if (ret) {
1718 + dev_err_ratelimited(&client->dev,
1719 + "Failed to write reg 0x%4.4x. error = %d\n",
1720 + regs[i].address, ret);
1721 +
1722 + return ret;
1723 + }
1724 + }
1725 +
1726 + return 0;
1727 +}
1728 +
1729 +/* Get bayer order based on flip setting. */
1730 +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1731 +{
1732 + unsigned int i;
1733 +
1734 + lockdep_assert_held(&imx477->mutex);
1735 +
1736 + for (i = 0; i < ARRAY_SIZE(codes); i++)
1737 + if (codes[i] == code)
1738 + break;
1739 +
1740 + if (i >= ARRAY_SIZE(codes))
1741 + i = 0;
1742 +
1743 + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1744 + (imx477->hflip->val ? 1 : 0);
1745 +
1746 + return codes[i];
1747 +}
1748 +
1749 +static void imx477_set_default_format(struct imx477 *imx477)
1750 +{
1751 + /* Set default mode to max resolution */
1752 + imx477->mode = &supported_modes_12bit[0];
1753 + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1754 +}
1755 +
1756 +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1757 +{
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;
1764 +
1765 + mutex_lock(&imx477->mutex);
1766 +
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;
1773 +
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;
1779 +
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;
1786 +
1787 + mutex_unlock(&imx477->mutex);
1788 +
1789 + return 0;
1790 +}
1791 +
1792 +static void imx477_adjust_exposure_range(struct imx477 *imx477)
1793 +{
1794 + int exposure_max, exposure_def;
1795 +
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,
1802 + exposure_def);
1803 +}
1804 +
1805 +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1806 +{
1807 + int ret = 0;
1808 +
1809 + imx477->long_exp_shift = 0;
1810 +
1811 + while (val > IMX477_FRAME_LENGTH_MAX) {
1812 + imx477->long_exp_shift++;
1813 + val >>= 1;
1814 + }
1815 +
1816 + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1817 + IMX477_REG_VALUE_16BIT, val);
1818 + if (ret)
1819 + return ret;
1820 +
1821 + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1822 + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1823 +}
1824 +
1825 +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1826 +{
1827 + struct imx477 *imx477 =
1828 + container_of(ctrl->handler, struct imx477, ctrl_handler);
1829 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1830 + int ret = 0;
1831 +
1832 + /*
1833 + * The VBLANK control may change the limits of usable exposure, so check
1834 + * and adjust if necessary.
1835 + */
1836 + if (ctrl->id == V4L2_CID_VBLANK)
1837 + imx477_adjust_exposure_range(imx477);
1838 +
1839 + /*
1840 + * Applying V4L2 control value only happens
1841 + * when power is up for streaming
1842 + */
1843 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1844 + return 0;
1845 +
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);
1850 + break;
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);
1855 + break;
1856 + case V4L2_CID_DIGITAL_GAIN:
1857 + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1858 + IMX477_REG_VALUE_16BIT, ctrl->val);
1859 + break;
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]);
1864 + break;
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);
1868 + break;
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);
1872 + break;
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);
1876 + break;
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);
1880 + break;
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);
1886 + break;
1887 + case V4L2_CID_VBLANK:
1888 + ret = imx477_set_frame_length(imx477,
1889 + imx477->mode->height + ctrl->val);
1890 + break;
1891 + case V4L2_CID_HBLANK:
1892 + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
1893 + imx477->mode->width + ctrl->val);
1894 + break;
1895 + default:
1896 + dev_info(&client->dev,
1897 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1898 + ctrl->id, ctrl->val);
1899 + ret = -EINVAL;
1900 + break;
1901 + }
1902 +
1903 + pm_runtime_put(&client->dev);
1904 +
1905 + return ret;
1906 +}
1907 +
1908 +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1909 + .s_ctrl = imx477_set_ctrl,
1910 +};
1911 +
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)
1915 +{
1916 + struct imx477 *imx477 = to_imx477(sd);
1917 +
1918 + if (code->pad >= NUM_PADS)
1919 + return -EINVAL;
1920 +
1921 + if (code->pad == IMAGE_PAD) {
1922 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1923 + return -EINVAL;
1924 +
1925 + code->code = imx477_get_format_code(imx477,
1926 + codes[code->index * 4]);
1927 + } else {
1928 + if (code->index > 0)
1929 + return -EINVAL;
1930 +
1931 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1932 + }
1933 +
1934 + return 0;
1935 +}
1936 +
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)
1940 +{
1941 + struct imx477 *imx477 = to_imx477(sd);
1942 +
1943 + if (fse->pad >= NUM_PADS)
1944 + return -EINVAL;
1945 +
1946 + if (fse->pad == IMAGE_PAD) {
1947 + const struct imx477_mode *mode_list;
1948 + unsigned int num_modes;
1949 +
1950 + get_mode_table(fse->code, &mode_list, &num_modes);
1951 +
1952 + if (fse->index >= num_modes)
1953 + return -EINVAL;
1954 +
1955 + if (fse->code != imx477_get_format_code(imx477, fse->code))
1956 + return -EINVAL;
1957 +
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;
1962 + } else {
1963 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1964 + return -EINVAL;
1965 +
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;
1970 + }
1971 +
1972 + return 0;
1973 +}
1974 +
1975 +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1976 +{
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,
1980 + fmt->colorspace,
1981 + fmt->ycbcr_enc);
1982 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1983 +}
1984 +
1985 +static void imx477_update_image_pad_format(struct imx477 *imx477,
1986 + const struct imx477_mode *mode,
1987 + struct v4l2_subdev_format *fmt)
1988 +{
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);
1993 +}
1994 +
1995 +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1996 +{
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;
2001 +}
2002 +
2003 +static int imx477_get_pad_format(struct v4l2_subdev *sd,
2004 + struct v4l2_subdev_state *sd_state,
2005 + struct v4l2_subdev_format *fmt)
2006 +{
2007 + struct imx477 *imx477 = to_imx477(sd);
2008 +
2009 + if (fmt->pad >= NUM_PADS)
2010 + return -EINVAL;
2011 +
2012 + mutex_lock(&imx477->mutex);
2013 +
2014 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2015 + struct v4l2_mbus_framefmt *try_fmt =
2016 + v4l2_subdev_state_get_format(sd_state,
2017 + fmt->pad);
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;
2023 + } else {
2024 + if (fmt->pad == IMAGE_PAD) {
2025 + imx477_update_image_pad_format(imx477, imx477->mode,
2026 + fmt);
2027 + fmt->format.code =
2028 + imx477_get_format_code(imx477, imx477->fmt_code);
2029 + } else {
2030 + imx477_update_metadata_pad_format(fmt);
2031 + }
2032 + }
2033 +
2034 + mutex_unlock(&imx477->mutex);
2035 + return 0;
2036 +}
2037 +
2038 +static
2039 +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
2040 + const struct v4l2_fract *timeperframe)
2041 +{
2042 + u64 frame_length;
2043 +
2044 + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
2045 + do_div(frame_length,
2046 + (u64)timeperframe->denominator * mode->line_length_pix);
2047 +
2048 + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
2049 + frame_length = IMX477_FRAME_LENGTH_MAX;
2050 +
2051 + return max_t(unsigned int, frame_length, mode->height);
2052 +}
2053 +
2054 +static void imx477_set_framing_limits(struct imx477 *imx477)
2055 +{
2056 + unsigned int frm_length_min, frm_length_default, hblank_min;
2057 + const struct imx477_mode *mode = imx477->mode;
2058 +
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);
2062 +
2063 + /* Default to no long exposure multiplier. */
2064 + imx477->long_exp_shift = 0;
2065 +
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);
2071 +
2072 + /* Setting this will adjust the exposure limits as well. */
2073 + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
2074 +
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);
2079 +}
2080 +
2081 +static int imx477_set_pad_format(struct v4l2_subdev *sd,
2082 + struct v4l2_subdev_state *sd_state,
2083 + struct v4l2_subdev_format *fmt)
2084 +{
2085 + struct v4l2_mbus_framefmt *framefmt;
2086 + const struct imx477_mode *mode;
2087 + struct imx477 *imx477 = to_imx477(sd);
2088 +
2089 + if (fmt->pad >= NUM_PADS)
2090 + return -EINVAL;
2091 +
2092 + mutex_lock(&imx477->mutex);
2093 +
2094 + if (fmt->pad == IMAGE_PAD) {
2095 + const struct imx477_mode *mode_list;
2096 + unsigned int num_modes;
2097 +
2098 + /* Bayer order varies with flips */
2099 + fmt->format.code = imx477_get_format_code(imx477,
2100 + fmt->format.code);
2101 +
2102 + get_mode_table(fmt->format.code, &mode_list, &num_modes);
2103 +
2104 + mode = v4l2_find_nearest_size(mode_list,
2105 + num_modes,
2106 + width, height,
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,
2112 + fmt->pad);
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);
2118 + }
2119 + } else {
2120 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2121 + framefmt = v4l2_subdev_state_get_format(sd_state,
2122 + fmt->pad);
2123 + *framefmt = fmt->format;
2124 + } else {
2125 + /* Only one embedded data mode is supported */
2126 + imx477_update_metadata_pad_format(fmt);
2127 + }
2128 + }
2129 +
2130 + mutex_unlock(&imx477->mutex);
2131 +
2132 + return 0;
2133 +}
2134 +
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)
2139 +{
2140 + switch (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;
2145 + }
2146 +
2147 + return NULL;
2148 +}
2149 +
2150 +static int imx477_get_selection(struct v4l2_subdev *sd,
2151 + struct v4l2_subdev_state *sd_state,
2152 + struct v4l2_subdev_selection *sel)
2153 +{
2154 + switch (sel->target) {
2155 + case V4L2_SEL_TGT_CROP: {
2156 + struct imx477 *imx477 = to_imx477(sd);
2157 +
2158 + mutex_lock(&imx477->mutex);
2159 + sel->r = *__imx477_get_pad_crop(imx477, sd_state, sel->pad,
2160 + sel->which);
2161 + mutex_unlock(&imx477->mutex);
2162 +
2163 + return 0;
2164 + }
2165 +
2166 + case V4L2_SEL_TGT_NATIVE_SIZE:
2167 + sel->r.left = 0;
2168 + sel->r.top = 0;
2169 + sel->r.width = IMX477_NATIVE_WIDTH;
2170 + sel->r.height = IMX477_NATIVE_HEIGHT;
2171 +
2172 + return 0;
2173 +
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;
2180 +
2181 + return 0;
2182 + }
2183 +
2184 + return -EINVAL;
2185 +}
2186 +
2187 +/* Start streaming */
2188 +static int imx477_start_streaming(struct imx477 *imx477)
2189 +{
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;
2193 + int ret, tm;
2194 +
2195 + if (!imx477->common_regs_written) {
2196 + ret = imx477_write_regs(imx477, mode_common_regs,
2197 + ARRAY_SIZE(mode_common_regs));
2198 + if (!ret) {
2199 + extra_regs = &imx477->compatible_data->extra_regs;
2200 + ret = imx477_write_regs(imx477, extra_regs->regs,
2201 + extra_regs->num_of_regs);
2202 + }
2203 +
2204 + if (ret) {
2205 + dev_err(&client->dev, "%s failed to set common settings\n",
2206 + __func__);
2207 + return ret;
2208 + }
2209 + imx477->common_regs_written = true;
2210 + }
2211 +
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);
2215 + if (ret) {
2216 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
2217 + return ret;
2218 + }
2219 +
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);
2223 +
2224 + /* Apply customized values from user */
2225 + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
2226 + if (ret)
2227 + return ret;
2228 +
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);
2239 +
2240 + /* set stream on register */
2241 + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2242 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
2243 +}
2244 +
2245 +/* Stop streaming */
2246 +static void imx477_stop_streaming(struct imx477 *imx477)
2247 +{
2248 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2249 + int ret;
2250 +
2251 + /* set stream off register */
2252 + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2253 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
2254 + if (ret)
2255 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
2256 +
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);
2260 +}
2261 +
2262 +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
2263 +{
2264 + struct imx477 *imx477 = to_imx477(sd);
2265 + struct i2c_client *client = v4l2_get_subdevdata(sd);
2266 + int ret = 0;
2267 +
2268 + mutex_lock(&imx477->mutex);
2269 + if (imx477->streaming == enable) {
2270 + mutex_unlock(&imx477->mutex);
2271 + return 0;
2272 + }
2273 +
2274 + if (enable) {
2275 + ret = pm_runtime_get_sync(&client->dev);
2276 + if (ret < 0) {
2277 + pm_runtime_put_noidle(&client->dev);
2278 + goto err_unlock;
2279 + }
2280 +
2281 + /*
2282 + * Apply default & customized values
2283 + * and then start streaming.
2284 + */
2285 + ret = imx477_start_streaming(imx477);
2286 + if (ret)
2287 + goto err_rpm_put;
2288 + } else {
2289 + imx477_stop_streaming(imx477);
2290 + pm_runtime_put(&client->dev);
2291 + }
2292 +
2293 + imx477->streaming = enable;
2294 +
2295 + /* vflip and hflip cannot change during streaming */
2296 + __v4l2_ctrl_grab(imx477->vflip, enable);
2297 + __v4l2_ctrl_grab(imx477->hflip, enable);
2298 +
2299 + mutex_unlock(&imx477->mutex);
2300 +
2301 + return ret;
2302 +
2303 +err_rpm_put:
2304 + pm_runtime_put(&client->dev);
2305 +err_unlock:
2306 + mutex_unlock(&imx477->mutex);
2307 +
2308 + return ret;
2309 +}
2310 +
2311 +/* Power/clock management functions */
2312 +static int imx477_power_on(struct device *dev)
2313 +{
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);
2317 + int ret;
2318 +
2319 + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
2320 + imx477->supplies);
2321 + if (ret) {
2322 + dev_err(&client->dev, "%s: failed to enable regulators\n",
2323 + __func__);
2324 + return ret;
2325 + }
2326 +
2327 + ret = clk_prepare_enable(imx477->xclk);
2328 + if (ret) {
2329 + dev_err(&client->dev, "%s: failed to enable clock\n",
2330 + __func__);
2331 + goto reg_off;
2332 + }
2333 +
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);
2337 +
2338 + return 0;
2339 +
2340 +reg_off:
2341 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2342 + return ret;
2343 +}
2344 +
2345 +static int imx477_power_off(struct device *dev)
2346 +{
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);
2350 +
2351 + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
2352 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2353 + clk_disable_unprepare(imx477->xclk);
2354 +
2355 + /* Force reprogramming of the common registers when powered up again. */
2356 + imx477->common_regs_written = false;
2357 +
2358 + return 0;
2359 +}
2360 +
2361 +static int __maybe_unused imx477_suspend(struct device *dev)
2362 +{
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);
2366 +
2367 + if (imx477->streaming)
2368 + imx477_stop_streaming(imx477);
2369 +
2370 + return 0;
2371 +}
2372 +
2373 +static int __maybe_unused imx477_resume(struct device *dev)
2374 +{
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);
2378 + int ret;
2379 +
2380 + if (imx477->streaming) {
2381 + ret = imx477_start_streaming(imx477);
2382 + if (ret)
2383 + goto error;
2384 + }
2385 +
2386 + return 0;
2387 +
2388 +error:
2389 + imx477_stop_streaming(imx477);
2390 + imx477->streaming = 0;
2391 + return ret;
2392 +}
2393 +
2394 +static int imx477_get_regulators(struct imx477 *imx477)
2395 +{
2396 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2397 + unsigned int i;
2398 +
2399 + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
2400 + imx477->supplies[i].supply = imx477_supply_name[i];
2401 +
2402 + return devm_regulator_bulk_get(&client->dev,
2403 + IMX477_NUM_SUPPLIES,
2404 + imx477->supplies);
2405 +}
2406 +
2407 +/* Verify chip ID */
2408 +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
2409 +{
2410 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2411 + int ret;
2412 + u32 val;
2413 +
2414 + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
2415 + IMX477_REG_VALUE_16BIT, &val);
2416 + if (ret) {
2417 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
2418 + expected_id, ret);
2419 + return ret;
2420 + }
2421 +
2422 + if (val != expected_id) {
2423 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2424 + expected_id, val);
2425 + return -EIO;
2426 + }
2427 +
2428 + dev_info(&client->dev, "Device found is imx%x\n", val);
2429 +
2430 + return 0;
2431 +}
2432 +
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,
2436 +};
2437 +
2438 +static const struct v4l2_subdev_video_ops imx477_video_ops = {
2439 + .s_stream = imx477_set_stream,
2440 +};
2441 +
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,
2448 +};
2449 +
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,
2454 +};
2455 +
2456 +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
2457 + .open = imx477_open,
2458 +};
2459 +
2460 +/* Initialize control handlers */
2461 +static int imx477_init_controls(struct imx477 *imx477)
2462 +{
2463 + struct v4l2_ctrl_handler *ctrl_hdlr;
2464 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2465 + struct v4l2_fwnode_device_properties props;
2466 + unsigned int i;
2467 + int ret;
2468 +
2469 + ctrl_hdlr = &imx477->ctrl_handler;
2470 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2471 + if (ret)
2472 + return ret;
2473 +
2474 + mutex_init(&imx477->mutex);
2475 + ctrl_hdlr->lock = &imx477->mutex;
2476 +
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;
2485 +
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;
2494 +
2495 + /*
2496 + * Create the controls here, but mode specific limits are setup
2497 + * in the imx477_set_framing_limits() call below.
2498 + */
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);
2503 +
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);
2510 +
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);
2514 +
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);
2518 +
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;
2523 +
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;
2528 +
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++) {
2534 + /*
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
2539 + */
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 */
2547 + }
2548 +
2549 + if (ctrl_hdlr->error) {
2550 + ret = ctrl_hdlr->error;
2551 + dev_err(&client->dev, "%s control init failed (%d)\n",
2552 + __func__, ret);
2553 + goto error;
2554 + }
2555 +
2556 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2557 + if (ret)
2558 + goto error;
2559 +
2560 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2561 + &props);
2562 + if (ret)
2563 + goto error;
2564 +
2565 + imx477->sd.ctrl_handler = ctrl_hdlr;
2566 +
2567 + mutex_lock(&imx477->mutex);
2568 +
2569 + /* Setup exposure and frame/line length limits. */
2570 + imx477_set_framing_limits(imx477);
2571 +
2572 + mutex_unlock(&imx477->mutex);
2573 +
2574 + return 0;
2575 +
2576 +error:
2577 + v4l2_ctrl_handler_free(ctrl_hdlr);
2578 + mutex_destroy(&imx477->mutex);
2579 +
2580 + return ret;
2581 +}
2582 +
2583 +static void imx477_free_controls(struct imx477 *imx477)
2584 +{
2585 + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2586 + mutex_destroy(&imx477->mutex);
2587 +}
2588 +
2589 +static int imx477_check_hwcfg(struct device *dev)
2590 +{
2591 + struct fwnode_handle *endpoint;
2592 + struct v4l2_fwnode_endpoint ep_cfg = {
2593 + .bus_type = V4L2_MBUS_CSI2_DPHY
2594 + };
2595 + int ret = -EINVAL;
2596 +
2597 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2598 + if (!endpoint) {
2599 + dev_err(dev, "endpoint node not found\n");
2600 + return -EINVAL;
2601 + }
2602 +
2603 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2604 + dev_err(dev, "could not parse endpoint\n");
2605 + goto error_out;
2606 + }
2607 +
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");
2611 + goto error_out;
2612 + }
2613 +
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");
2617 + goto error_out;
2618 + }
2619 +
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]);
2624 + goto error_out;
2625 + }
2626 +
2627 + ret = 0;
2628 +
2629 +error_out:
2630 + v4l2_fwnode_endpoint_free(&ep_cfg);
2631 + fwnode_handle_put(endpoint);
2632 +
2633 + return ret;
2634 +}
2635 +
2636 +static const struct imx477_compatible_data imx477_compatible = {
2637 + .chip_id = IMX477_CHIP_ID,
2638 + .extra_regs = {
2639 + .num_of_regs = 0,
2640 + .regs = NULL
2641 + }
2642 +};
2643 +
2644 +static const struct imx477_reg imx378_regs[] = {
2645 + {0x3e35, 0x01},
2646 + {0x4421, 0x08},
2647 + {0x3ff9, 0x00},
2648 +};
2649 +
2650 +static const struct imx477_compatible_data imx378_compatible = {
2651 + .chip_id = IMX378_CHIP_ID,
2652 + .extra_regs = {
2653 + .num_of_regs = ARRAY_SIZE(imx378_regs),
2654 + .regs = imx378_regs
2655 + }
2656 +};
2657 +
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 */ }
2662 +};
2663 +
2664 +static int imx477_probe(struct i2c_client *client)
2665 +{
2666 + struct device *dev = &client->dev;
2667 + struct imx477 *imx477;
2668 + const struct of_device_id *match;
2669 + int ret;
2670 + u32 tm_of;
2671 +
2672 + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2673 + if (!imx477)
2674 + return -ENOMEM;
2675 +
2676 + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2677 +
2678 + match = of_match_device(imx477_dt_ids, dev);
2679 + if (!match)
2680 + return -ENODEV;
2681 + imx477->compatible_data =
2682 + (const struct imx477_compatible_data *)match->data;
2683 +
2684 + /* Check the hardware configuration in device tree */
2685 + if (imx477_check_hwcfg(dev))
2686 + return -EINVAL;
2687 +
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;
2691 +
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);
2697 + }
2698 +
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);
2703 + return -EINVAL;
2704 + }
2705 +
2706 + ret = imx477_get_regulators(imx477);
2707 + if (ret) {
2708 + dev_err(dev, "failed to get regulators\n");
2709 + return ret;
2710 + }
2711 +
2712 + /* Request optional enable pin */
2713 + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2714 + GPIOD_OUT_HIGH);
2715 +
2716 + /*
2717 + * The sensor must be powered for imx477_identify_module()
2718 + * to be able to read the CHIP_ID register
2719 + */
2720 + ret = imx477_power_on(dev);
2721 + if (ret)
2722 + return ret;
2723 +
2724 + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2725 + if (ret)
2726 + goto error_power_off;
2727 +
2728 + /* Initialize default format */
2729 + imx477_set_default_format(imx477);
2730 +
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);
2735 +
2736 + /* This needs the pm runtime to be registered. */
2737 + ret = imx477_init_controls(imx477);
2738 + if (ret)
2739 + goto error_power_off;
2740 +
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;
2746 +
2747 + /* Initialize source pads */
2748 + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2749 + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2750 +
2751 + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2752 + if (ret) {
2753 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2754 + goto error_handler_free;
2755 + }
2756 +
2757 + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2758 + if (ret < 0) {
2759 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2760 + goto error_media_entity;
2761 + }
2762 +
2763 + return 0;
2764 +
2765 +error_media_entity:
2766 + media_entity_cleanup(&imx477->sd.entity);
2767 +
2768 +error_handler_free:
2769 + imx477_free_controls(imx477);
2770 +
2771 +error_power_off:
2772 + pm_runtime_disable(&client->dev);
2773 + pm_runtime_set_suspended(&client->dev);
2774 + imx477_power_off(&client->dev);
2775 +
2776 + return ret;
2777 +}
2778 +
2779 +static void imx477_remove(struct i2c_client *client)
2780 +{
2781 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2782 + struct imx477 *imx477 = to_imx477(sd);
2783 +
2784 + v4l2_async_unregister_subdev(sd);
2785 + media_entity_cleanup(&sd->entity);
2786 + imx477_free_controls(imx477);
2787 +
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);
2792 +}
2793 +
2794 +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2795 +
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)
2799 +};
2800 +
2801 +static struct i2c_driver imx477_i2c_driver = {
2802 + .driver = {
2803 + .name = "imx477",
2804 + .of_match_table = imx477_dt_ids,
2805 + .pm = &imx477_pm_ops,
2806 + },
2807 + .probe = imx477_probe,
2808 + .remove = imx477_remove,
2809 +};
2810 +
2811 +module_i2c_driver(imx477_i2c_driver);
2812 +
2813 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2814 +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2815 +MODULE_LICENSE("GPL v2");