kernel: bump 6.1 to 6.1.81
[openwrt/staging/981213.git] / target / linux / bcm27xx / patches-6.1 / 950-0195-media-i2c-imx477-Support-for-the-Sony-IMX477-sensor.patch
1 From 227eddefe9cb07dd1d160f0015b0d17d66cd3cf8 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Thu, 7 May 2020 15:50:54 +0100
4 Subject: [PATCH] media: i2c: imx477: Support for the Sony IMX477
5 sensor
6
7 dt-bindings: media: i2c: Add IMX477 CMOS sensor binding
8
9 Add YAML device tree binding for IMX477 CMOS image sensor.
10
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
12
13 media: i2c: Add driver for Sony IMX477 sensor
14
15 Adds a driver for the 12MPix Sony IMX477 CSI2 sensor.
16 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
17 currently only supports 2 lanes.
18
19 The following Bayer modes are currently available:
20
21 4056x3040 12-bit @ 10fps
22 2028x1520 12-bit (binned) @ 40fps
23 2028x1050 12-bit (cropped/binned) @ 50fps
24 1012x760 10-bit (scaled) @ 120 fps
25
26 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
27
28 media: i2c: imx477: Add support for adaptive frame control
29
30 Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
31 automatically adjust the sensor frame length based on exposure time,
32 allowing variable frame rates and longer exposures.
33
34 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
35
36 media: i2c: imx477: Return correct result on sensor id verification
37
38 The test should return -EIO if the register read id does not match
39 the expected sensor id.
40
41 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
42
43 media: i2c: imx477: Parse and register properties
44
45 Parse device properties and register controls for them using the V4L2
46 fwnode properties helpers.
47
48 Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
49
50 media: i2c: imx477: Selection compliance fixes
51
52 To comply with the intended usage of the V4L2 selection target when
53 used to retrieve a sensor image properties, adjust the rectangles
54 returned by the imx477 driver.
55
56 The top/left crop coordinates of the TGT_CROP rectangle were set to
57 (0, 0) instead of (8, 16) which is the offset from the larger physical
58 pixel array rectangle. This was also a mismatch with the default values
59 crop rectangle value, so this is corrected. Found with v4l2-compliance.
60
61 While at it, add V4L2_SEL_TGT_CROP_BOUNDS support: CROP_DEFAULT and
62 CROP_BOUNDS have the same size as the non-active pixels are not readable
63 using the selection API. Found with v4l2-compliance.
64
65 This commit mirrors 543790f777ba1b3264c168c653db6d415e7c983f done for
66 the imx219 sensor.
67
68 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
69
70 media: i2c: imx477: Remove auto frame length adjusting
71
72 The V4L2_CID_EXPOSURE_AUTO_PRIORITY was used to let the sensor control
73 frame length (effectively framerate) based on the requested exposure
74 time requested. Remove this feature as it is never used, and goes
75 against how V4L2 likes to handle exposure and vblank controls.
76
77 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
78
79 media: i2c: imx477: Add very long exposure control to the driver
80
81 Add support for very long exposures by using the exposure multiplier
82 register. Userland does not need to pass any additional controls to
83 enable long exposures, it simply requests a larger vblank to extend the
84 exposure control range appropriately.
85
86 Currently, since hblank is fixed, a maximum of approximately 124 seconds
87 of exposure time can be used. In a future change, hblank could also be
88 controlled in userland to give over 200 seconds of exposure time.
89
90 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
91
92 media: i2c: imx477: Fix crop height for 2028x1080 mode
93
94 The crop height for this mode was set at 2600 lines, it should be 2160
95 lines instead.
96
97 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
98
99 media: i2c: imx477: Replace existing 1012x760 mode
100
101 The existing 1012x760 120 fps mode has significant IQ problem using
102 the internal sensor scaler. Replace this mode with a 1332x990 120 fps
103 mode instead. This new mode has a smaller field of view, but does not
104 suffer from the bad IQ of the original mode.
105
106 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
107
108 media: i2c: imx477: Remove internal v4l2_mbus_framefmt from the state
109
110 The only field in this struct that is used is the format code, so
111 replace the struct with this single field.
112
113 Save the format code in imx477_set_pad_format() when setting up a new
114 mode so that imx477_get_pad_format() performs the right lookup.
115 Otherwise, this caused a bug where the mode lookup occurred on the
116 12-bit table rather than the 10-bit table.
117
118 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
119
120 media: i2c: imx477: Remove unused function parameter
121
122 The struct imx477 *ctrl parameter is not used in the function
123 imx477_adjust_exposure_range(), so remove it.
124
125 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
126
127 media: i2c: imx477: Fix for long exposure limit calculations
128
129 Do not scale IMX477_EXPOSURE_OFFSET with the long exposure factor during
130 the limit calculations. This allows larger exposure times, and does seem to be
131 what the sensor is doing internally.
132
133 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
134
135 media: i2c: imx477: Extend driver to support imx378 sensor
136
137 The imx378 sensor is almost identical to the imx477 and can be
138 supported as a "compatible" sensor with just a few extra register
139 writes.
140
141 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
142
143 media: i2c: imx477: Fix framerates for 1332x990 mode
144
145 The imx477 driver's line length for this mode had not been updated to
146 the value supplied to us by the sensor manufacturer. With this
147 correction the sensor delivers the framerates that are expected.
148
149 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
150
151 media: i2c: imx477: Allow control of on-sensor DPC
152
153 A module parameter "dpc_enable" is added to allow the control of the
154 sensor's on-board DPC (Defective Pixel Correction) function.
155
156 This is a global setting to be configured before using the sensor;
157 there is no intention that this would ever be changed on-the-fly.
158
159 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
160
161 media: i2c: imx477: Sensor should report RAW color space
162
163 Tested on Raspberry Pi running libcamera.
164
165 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
166
167 media: i2c: imx477: Add vsync trigger_mode parameter
168
169 trigger_mode == 0 (default) => no effect / no registers written
170 trigger_mode == 1 => source
171 trigger_mode == 2 => sink
172
173 This can be set e.g. in /boot/cmdline.txt as imx477.trigger_mode=N
174
175 Signed-off-by: Jonas Jacob <jonas.jacob@neocortexvision.com>
176
177 media: i2c: Update imx477 Kconfig entry
178
179 Bring the IMX477 Kconfig declaration in line with upstream entries.
180
181 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
182
183 media: i2c: imx477: Correct minimum exposure lines
184
185 The minimum number of exposure lines value (IMX477_EXPOSURE_MIN) was
186 previously 20 but this is not correct. The datasheet is not completely
187 explicit, however the new value of 4 has been tested with all the
188 sensor modes supported by this driver, and matches the lowest exposure
189 value of 114us that could be achieved wtih Raspberry Pi's legacy
190 firmware driver.
191
192 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
193
194 media: i2c: imx477: Allow dynamic horizontal blanking control
195
196 Currently, the V4L2_CID_HBLANK control is marked as read-only. Remove this
197 restriction and allow userland to modify the control if needed.
198
199 Set the maximum limit of the line length to 0xfff0.
200
201 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
202
203 media: i2c: imx477: Reset hblank on mode switch
204
205 Reset the hblank control to the minimum value on every mode switch. This is to
206 account for userland instances that do not yet control hblank, otherwise it
207 gets set to a non-optimal value.
208
209 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
210
211 media: i2c: imx477: Do not unconditionally adjust hblank and vblank limits
212
213 On a mode change, only call imx477_set_framing_limits() to adjust the hblank
214 and vblank limits if the new mode is different from the existing mode. This
215 preserves any manual control values the user might have set.
216
217 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
218 ---
219 .../devicetree/bindings/media/i2c/imx477.yaml | 113 +
220 MAINTAINERS | 8 +
221 drivers/media/i2c/Kconfig | 11 +
222 drivers/media/i2c/Makefile | 1 +
223 drivers/media/i2c/imx477.c | 2308 +++++++++++++++++
224 5 files changed, 2441 insertions(+)
225 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx477.yaml
226 create mode 100644 drivers/media/i2c/imx477.c
227
228 --- /dev/null
229 +++ b/Documentation/devicetree/bindings/media/i2c/imx477.yaml
230 @@ -0,0 +1,113 @@
231 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
232 +%YAML 1.2
233 +---
234 +$id: http://devicetree.org/schemas/media/i2c/imx477.yaml#
235 +$schema: http://devicetree.org/meta-schemas/core.yaml#
236 +
237 +title: Sony 1/2.3-Inch 12Mpixel CMOS Digital Image Sensor
238 +
239 +maintainers:
240 + - Naushir Patuck <naush@raspberypi.com>
241 +
242 +description: |-
243 + The Sony IMX477 is a 1/2.3-inch CMOS active pixel digital image sensor
244 + with an active array size of 4056H x 3040V. It is programmable through
245 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
246 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
247 + 4 data lanes.
248 +
249 +properties:
250 + compatible:
251 + const: sony,imx477
252 +
253 + reg:
254 + description: I2C device address
255 + maxItems: 1
256 +
257 + clocks:
258 + maxItems: 1
259 +
260 + VDIG-supply:
261 + description:
262 + Digital I/O voltage supply, 1.05 volts
263 +
264 + VANA-supply:
265 + description:
266 + Analog voltage supply, 2.8 volts
267 +
268 + VDDL-supply:
269 + description:
270 + Digital core voltage supply, 1.8 volts
271 +
272 + reset-gpios:
273 + description: |-
274 + Reference to the GPIO connected to the xclr pin, if any.
275 + Must be released (set high) after all all supplies and INCK are applied.
276 +
277 + # See ../video-interfaces.txt for more details
278 + port:
279 + type: object
280 + properties:
281 + endpoint:
282 + type: object
283 + properties:
284 + data-lanes:
285 + description: |-
286 + The sensor supports either two-lane, or four-lane operation.
287 + For two-lane operation the property must be set to <1 2>.
288 + items:
289 + - const: 1
290 + - const: 2
291 +
292 + clock-noncontinuous:
293 + type: boolean
294 + description: |-
295 + MIPI CSI-2 clock is non-continuous if this property is present,
296 + otherwise it's continuous.
297 +
298 + link-frequencies:
299 + allOf:
300 + - $ref: /schemas/types.yaml#/definitions/uint64-array
301 + description:
302 + Allowed data bus frequencies.
303 +
304 + required:
305 + - link-frequencies
306 +
307 +required:
308 + - compatible
309 + - reg
310 + - clocks
311 + - VANA-supply
312 + - VDIG-supply
313 + - VDDL-supply
314 + - port
315 +
316 +additionalProperties: false
317 +
318 +examples:
319 + - |
320 + i2c0 {
321 + #address-cells = <1>;
322 + #size-cells = <0>;
323 +
324 + imx477: sensor@10 {
325 + compatible = "sony,imx477";
326 + reg = <0x1a>;
327 + clocks = <&imx477_clk>;
328 + VANA-supply = <&imx477_vana>; /* 2.8v */
329 + VDIG-supply = <&imx477_vdig>; /* 1.05v */
330 + VDDL-supply = <&imx477_vddl>; /* 1.8v */
331 +
332 + port {
333 + imx477_0: endpoint {
334 + remote-endpoint = <&csi1_ep>;
335 + data-lanes = <1 2>;
336 + clock-noncontinuous;
337 + link-frequencies = /bits/ 64 <450000000>;
338 + };
339 + };
340 + };
341 + };
342 +
343 +...
344 --- a/MAINTAINERS
345 +++ b/MAINTAINERS
346 @@ -19290,6 +19290,14 @@ T: git git://linuxtv.org/media_tree.git
347 F: Documentation/devicetree/bindings/media/i2c/sony,imx412.yaml
348 F: drivers/media/i2c/imx412.c
349
350 +SONY IMX477 SENSOR DRIVER
351 +M: Raspberry Pi Kernel Maintenance <kernel-list@raspberrypi.com>
352 +L: linux-media@vger.kernel.org
353 +S: Maintained
354 +T: git git://linuxtv.org/media_tree.git
355 +F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
356 +F: drivers/media/i2c/imx477.c
357 +
358 SONY MEMORYSTICK SUBSYSTEM
359 M: Maxim Levitsky <maximlevitsky@gmail.com>
360 M: Alex Dubov <oakad@yahoo.com>
361 --- a/drivers/media/i2c/Kconfig
362 +++ b/drivers/media/i2c/Kconfig
363 @@ -1030,6 +1030,17 @@ config VIDEO_UDA1342
364 To compile this driver as a module, choose M here: the
365 module will be called uda1342.
366
367 +config VIDEO_IMX477
368 + tristate "Sony IMX477 sensor support"
369 + depends on I2C && VIDEO_DEV
370 + select VIDEO_V4L2_SUBDEV_API
371 + help
372 + This is a Video4Linux2 sensor driver for the Sony
373 + IMX477 camera. Also supports the Sony IMX378.
374 +
375 + To compile this driver as a module, choose M here: the
376 + module will be called imx477.
377 +
378 config VIDEO_VP27SMPX
379 tristate "Panasonic VP27's internal MPX"
380 depends on VIDEO_DEV && I2C
381 --- a/drivers/media/i2c/Makefile
382 +++ b/drivers/media/i2c/Makefile
383 @@ -48,6 +48,7 @@ obj-$(CONFIG_VIDEO_IMX334) += imx334.o
384 obj-$(CONFIG_VIDEO_IMX335) += imx335.o
385 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
386 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
387 +obj-$(CONFIG_VIDEO_IMX477) += imx477.o
388 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
389 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
390 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
391 --- /dev/null
392 +++ b/drivers/media/i2c/imx477.c
393 @@ -0,0 +1,2308 @@
394 +// SPDX-License-Identifier: GPL-2.0
395 +/*
396 + * A V4L2 driver for Sony IMX477 cameras.
397 + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd
398 + *
399 + * Based on Sony imx219 camera driver
400 + * Copyright (C) 2019-2020 Raspberry Pi (Trading) Ltd
401 + */
402 +#include <asm/unaligned.h>
403 +#include <linux/clk.h>
404 +#include <linux/delay.h>
405 +#include <linux/gpio/consumer.h>
406 +#include <linux/i2c.h>
407 +#include <linux/module.h>
408 +#include <linux/of_device.h>
409 +#include <linux/pm_runtime.h>
410 +#include <linux/regulator/consumer.h>
411 +#include <media/v4l2-ctrls.h>
412 +#include <media/v4l2-device.h>
413 +#include <media/v4l2-event.h>
414 +#include <media/v4l2-fwnode.h>
415 +#include <media/v4l2-mediabus.h>
416 +
417 +static int dpc_enable = 1;
418 +module_param(dpc_enable, int, 0644);
419 +MODULE_PARM_DESC(dpc_enable, "Enable on-sensor DPC");
420 +
421 +static int trigger_mode;
422 +module_param(trigger_mode, int, 0644);
423 +MODULE_PARM_DESC(trigger_mode, "Set vsync trigger mode: 1=source, 2=sink");
424 +
425 +#define IMX477_REG_VALUE_08BIT 1
426 +#define IMX477_REG_VALUE_16BIT 2
427 +
428 +/* Chip ID */
429 +#define IMX477_REG_CHIP_ID 0x0016
430 +#define IMX477_CHIP_ID 0x0477
431 +#define IMX378_CHIP_ID 0x0378
432 +
433 +#define IMX477_REG_MODE_SELECT 0x0100
434 +#define IMX477_MODE_STANDBY 0x00
435 +#define IMX477_MODE_STREAMING 0x01
436 +
437 +#define IMX477_REG_ORIENTATION 0x101
438 +
439 +#define IMX477_XCLK_FREQ 24000000
440 +
441 +#define IMX477_DEFAULT_LINK_FREQ 450000000
442 +
443 +/* Pixel rate is fixed at 840MHz for all the modes */
444 +#define IMX477_PIXEL_RATE 840000000
445 +
446 +/* V_TIMING internal */
447 +#define IMX477_REG_FRAME_LENGTH 0x0340
448 +#define IMX477_FRAME_LENGTH_MAX 0xffdc
449 +
450 +/* H_TIMING internal */
451 +#define IMX477_REG_LINE_LENGTH 0x0342
452 +#define IMX477_LINE_LENGTH_MAX 0xfff0
453 +
454 +/* Long exposure multiplier */
455 +#define IMX477_LONG_EXP_SHIFT_MAX 7
456 +#define IMX477_LONG_EXP_SHIFT_REG 0x3100
457 +
458 +/* Exposure control */
459 +#define IMX477_REG_EXPOSURE 0x0202
460 +#define IMX477_EXPOSURE_OFFSET 22
461 +#define IMX477_EXPOSURE_MIN 4
462 +#define IMX477_EXPOSURE_STEP 1
463 +#define IMX477_EXPOSURE_DEFAULT 0x640
464 +#define IMX477_EXPOSURE_MAX (IMX477_FRAME_LENGTH_MAX - \
465 + IMX477_EXPOSURE_OFFSET)
466 +
467 +/* Analog gain control */
468 +#define IMX477_REG_ANALOG_GAIN 0x0204
469 +#define IMX477_ANA_GAIN_MIN 0
470 +#define IMX477_ANA_GAIN_MAX 978
471 +#define IMX477_ANA_GAIN_STEP 1
472 +#define IMX477_ANA_GAIN_DEFAULT 0x0
473 +
474 +/* Digital gain control */
475 +#define IMX477_REG_DIGITAL_GAIN 0x020e
476 +#define IMX477_DGTL_GAIN_MIN 0x0100
477 +#define IMX477_DGTL_GAIN_MAX 0xffff
478 +#define IMX477_DGTL_GAIN_DEFAULT 0x0100
479 +#define IMX477_DGTL_GAIN_STEP 1
480 +
481 +/* Test Pattern Control */
482 +#define IMX477_REG_TEST_PATTERN 0x0600
483 +#define IMX477_TEST_PATTERN_DISABLE 0
484 +#define IMX477_TEST_PATTERN_SOLID_COLOR 1
485 +#define IMX477_TEST_PATTERN_COLOR_BARS 2
486 +#define IMX477_TEST_PATTERN_GREY_COLOR 3
487 +#define IMX477_TEST_PATTERN_PN9 4
488 +
489 +/* Test pattern colour components */
490 +#define IMX477_REG_TEST_PATTERN_R 0x0602
491 +#define IMX477_REG_TEST_PATTERN_GR 0x0604
492 +#define IMX477_REG_TEST_PATTERN_B 0x0606
493 +#define IMX477_REG_TEST_PATTERN_GB 0x0608
494 +#define IMX477_TEST_PATTERN_COLOUR_MIN 0
495 +#define IMX477_TEST_PATTERN_COLOUR_MAX 0x0fff
496 +#define IMX477_TEST_PATTERN_COLOUR_STEP 1
497 +#define IMX477_TEST_PATTERN_R_DEFAULT IMX477_TEST_PATTERN_COLOUR_MAX
498 +#define IMX477_TEST_PATTERN_GR_DEFAULT 0
499 +#define IMX477_TEST_PATTERN_B_DEFAULT 0
500 +#define IMX477_TEST_PATTERN_GB_DEFAULT 0
501 +
502 +/* Trigger mode */
503 +#define IMX477_REG_MC_MODE 0x3f0b
504 +#define IMX477_REG_MS_SEL 0x3041
505 +#define IMX477_REG_XVS_IO_CTRL 0x3040
506 +#define IMX477_REG_EXTOUT_EN 0x4b81
507 +
508 +/* Embedded metadata stream structure */
509 +#define IMX477_EMBEDDED_LINE_WIDTH 16384
510 +#define IMX477_NUM_EMBEDDED_LINES 1
511 +
512 +enum pad_types {
513 + IMAGE_PAD,
514 + METADATA_PAD,
515 + NUM_PADS
516 +};
517 +
518 +/* IMX477 native and active pixel array size. */
519 +#define IMX477_NATIVE_WIDTH 4072U
520 +#define IMX477_NATIVE_HEIGHT 3176U
521 +#define IMX477_PIXEL_ARRAY_LEFT 8U
522 +#define IMX477_PIXEL_ARRAY_TOP 16U
523 +#define IMX477_PIXEL_ARRAY_WIDTH 4056U
524 +#define IMX477_PIXEL_ARRAY_HEIGHT 3040U
525 +
526 +struct imx477_reg {
527 + u16 address;
528 + u8 val;
529 +};
530 +
531 +struct imx477_reg_list {
532 + unsigned int num_of_regs;
533 + const struct imx477_reg *regs;
534 +};
535 +
536 +/* Mode : resolution and related config&values */
537 +struct imx477_mode {
538 + /* Frame width */
539 + unsigned int width;
540 +
541 + /* Frame height */
542 + unsigned int height;
543 +
544 + /* H-timing in pixels */
545 + unsigned int line_length_pix;
546 +
547 + /* Analog crop rectangle. */
548 + struct v4l2_rect crop;
549 +
550 + /* Highest possible framerate. */
551 + struct v4l2_fract timeperframe_min;
552 +
553 + /* Default framerate. */
554 + struct v4l2_fract timeperframe_default;
555 +
556 + /* Default register values */
557 + struct imx477_reg_list reg_list;
558 +};
559 +
560 +static const struct imx477_reg mode_common_regs[] = {
561 + {0x0136, 0x18},
562 + {0x0137, 0x00},
563 + {0xe000, 0x00},
564 + {0xe07a, 0x01},
565 + {0x0808, 0x02},
566 + {0x4ae9, 0x18},
567 + {0x4aea, 0x08},
568 + {0xf61c, 0x04},
569 + {0xf61e, 0x04},
570 + {0x4ae9, 0x21},
571 + {0x4aea, 0x80},
572 + {0x38a8, 0x1f},
573 + {0x38a9, 0xff},
574 + {0x38aa, 0x1f},
575 + {0x38ab, 0xff},
576 + {0x55d4, 0x00},
577 + {0x55d5, 0x00},
578 + {0x55d6, 0x07},
579 + {0x55d7, 0xff},
580 + {0x55e8, 0x07},
581 + {0x55e9, 0xff},
582 + {0x55ea, 0x00},
583 + {0x55eb, 0x00},
584 + {0x574c, 0x07},
585 + {0x574d, 0xff},
586 + {0x574e, 0x00},
587 + {0x574f, 0x00},
588 + {0x5754, 0x00},
589 + {0x5755, 0x00},
590 + {0x5756, 0x07},
591 + {0x5757, 0xff},
592 + {0x5973, 0x04},
593 + {0x5974, 0x01},
594 + {0x5d13, 0xc3},
595 + {0x5d14, 0x58},
596 + {0x5d15, 0xa3},
597 + {0x5d16, 0x1d},
598 + {0x5d17, 0x65},
599 + {0x5d18, 0x8c},
600 + {0x5d1a, 0x06},
601 + {0x5d1b, 0xa9},
602 + {0x5d1c, 0x45},
603 + {0x5d1d, 0x3a},
604 + {0x5d1e, 0xab},
605 + {0x5d1f, 0x15},
606 + {0x5d21, 0x0e},
607 + {0x5d22, 0x52},
608 + {0x5d23, 0xaa},
609 + {0x5d24, 0x7d},
610 + {0x5d25, 0x57},
611 + {0x5d26, 0xa8},
612 + {0x5d37, 0x5a},
613 + {0x5d38, 0x5a},
614 + {0x5d77, 0x7f},
615 + {0x7b75, 0x0e},
616 + {0x7b76, 0x0b},
617 + {0x7b77, 0x08},
618 + {0x7b78, 0x0a},
619 + {0x7b79, 0x47},
620 + {0x7b7c, 0x00},
621 + {0x7b7d, 0x00},
622 + {0x8d1f, 0x00},
623 + {0x8d27, 0x00},
624 + {0x9004, 0x03},
625 + {0x9200, 0x50},
626 + {0x9201, 0x6c},
627 + {0x9202, 0x71},
628 + {0x9203, 0x00},
629 + {0x9204, 0x71},
630 + {0x9205, 0x01},
631 + {0x9371, 0x6a},
632 + {0x9373, 0x6a},
633 + {0x9375, 0x64},
634 + {0x991a, 0x00},
635 + {0x996b, 0x8c},
636 + {0x996c, 0x64},
637 + {0x996d, 0x50},
638 + {0x9a4c, 0x0d},
639 + {0x9a4d, 0x0d},
640 + {0xa001, 0x0a},
641 + {0xa003, 0x0a},
642 + {0xa005, 0x0a},
643 + {0xa006, 0x01},
644 + {0xa007, 0xc0},
645 + {0xa009, 0xc0},
646 + {0x3d8a, 0x01},
647 + {0x4421, 0x04},
648 + {0x7b3b, 0x01},
649 + {0x7b4c, 0x00},
650 + {0x9905, 0x00},
651 + {0x9907, 0x00},
652 + {0x9909, 0x00},
653 + {0x990b, 0x00},
654 + {0x9944, 0x3c},
655 + {0x9947, 0x3c},
656 + {0x994a, 0x8c},
657 + {0x994b, 0x50},
658 + {0x994c, 0x1b},
659 + {0x994d, 0x8c},
660 + {0x994e, 0x50},
661 + {0x994f, 0x1b},
662 + {0x9950, 0x8c},
663 + {0x9951, 0x1b},
664 + {0x9952, 0x0a},
665 + {0x9953, 0x8c},
666 + {0x9954, 0x1b},
667 + {0x9955, 0x0a},
668 + {0x9a13, 0x04},
669 + {0x9a14, 0x04},
670 + {0x9a19, 0x00},
671 + {0x9a1c, 0x04},
672 + {0x9a1d, 0x04},
673 + {0x9a26, 0x05},
674 + {0x9a27, 0x05},
675 + {0x9a2c, 0x01},
676 + {0x9a2d, 0x03},
677 + {0x9a2f, 0x05},
678 + {0x9a30, 0x05},
679 + {0x9a41, 0x00},
680 + {0x9a46, 0x00},
681 + {0x9a47, 0x00},
682 + {0x9c17, 0x35},
683 + {0x9c1d, 0x31},
684 + {0x9c29, 0x50},
685 + {0x9c3b, 0x2f},
686 + {0x9c41, 0x6b},
687 + {0x9c47, 0x2d},
688 + {0x9c4d, 0x40},
689 + {0x9c6b, 0x00},
690 + {0x9c71, 0xc8},
691 + {0x9c73, 0x32},
692 + {0x9c75, 0x04},
693 + {0x9c7d, 0x2d},
694 + {0x9c83, 0x40},
695 + {0x9c94, 0x3f},
696 + {0x9c95, 0x3f},
697 + {0x9c96, 0x3f},
698 + {0x9c97, 0x00},
699 + {0x9c98, 0x00},
700 + {0x9c99, 0x00},
701 + {0x9c9a, 0x3f},
702 + {0x9c9b, 0x3f},
703 + {0x9c9c, 0x3f},
704 + {0x9ca0, 0x0f},
705 + {0x9ca1, 0x0f},
706 + {0x9ca2, 0x0f},
707 + {0x9ca3, 0x00},
708 + {0x9ca4, 0x00},
709 + {0x9ca5, 0x00},
710 + {0x9ca6, 0x1e},
711 + {0x9ca7, 0x1e},
712 + {0x9ca8, 0x1e},
713 + {0x9ca9, 0x00},
714 + {0x9caa, 0x00},
715 + {0x9cab, 0x00},
716 + {0x9cac, 0x09},
717 + {0x9cad, 0x09},
718 + {0x9cae, 0x09},
719 + {0x9cbd, 0x50},
720 + {0x9cbf, 0x50},
721 + {0x9cc1, 0x50},
722 + {0x9cc3, 0x40},
723 + {0x9cc5, 0x40},
724 + {0x9cc7, 0x40},
725 + {0x9cc9, 0x0a},
726 + {0x9ccb, 0x0a},
727 + {0x9ccd, 0x0a},
728 + {0x9d17, 0x35},
729 + {0x9d1d, 0x31},
730 + {0x9d29, 0x50},
731 + {0x9d3b, 0x2f},
732 + {0x9d41, 0x6b},
733 + {0x9d47, 0x42},
734 + {0x9d4d, 0x5a},
735 + {0x9d6b, 0x00},
736 + {0x9d71, 0xc8},
737 + {0x9d73, 0x32},
738 + {0x9d75, 0x04},
739 + {0x9d7d, 0x42},
740 + {0x9d83, 0x5a},
741 + {0x9d94, 0x3f},
742 + {0x9d95, 0x3f},
743 + {0x9d96, 0x3f},
744 + {0x9d97, 0x00},
745 + {0x9d98, 0x00},
746 + {0x9d99, 0x00},
747 + {0x9d9a, 0x3f},
748 + {0x9d9b, 0x3f},
749 + {0x9d9c, 0x3f},
750 + {0x9d9d, 0x1f},
751 + {0x9d9e, 0x1f},
752 + {0x9d9f, 0x1f},
753 + {0x9da0, 0x0f},
754 + {0x9da1, 0x0f},
755 + {0x9da2, 0x0f},
756 + {0x9da3, 0x00},
757 + {0x9da4, 0x00},
758 + {0x9da5, 0x00},
759 + {0x9da6, 0x1e},
760 + {0x9da7, 0x1e},
761 + {0x9da8, 0x1e},
762 + {0x9da9, 0x00},
763 + {0x9daa, 0x00},
764 + {0x9dab, 0x00},
765 + {0x9dac, 0x09},
766 + {0x9dad, 0x09},
767 + {0x9dae, 0x09},
768 + {0x9dc9, 0x0a},
769 + {0x9dcb, 0x0a},
770 + {0x9dcd, 0x0a},
771 + {0x9e17, 0x35},
772 + {0x9e1d, 0x31},
773 + {0x9e29, 0x50},
774 + {0x9e3b, 0x2f},
775 + {0x9e41, 0x6b},
776 + {0x9e47, 0x2d},
777 + {0x9e4d, 0x40},
778 + {0x9e6b, 0x00},
779 + {0x9e71, 0xc8},
780 + {0x9e73, 0x32},
781 + {0x9e75, 0x04},
782 + {0x9e94, 0x0f},
783 + {0x9e95, 0x0f},
784 + {0x9e96, 0x0f},
785 + {0x9e97, 0x00},
786 + {0x9e98, 0x00},
787 + {0x9e99, 0x00},
788 + {0x9ea0, 0x0f},
789 + {0x9ea1, 0x0f},
790 + {0x9ea2, 0x0f},
791 + {0x9ea3, 0x00},
792 + {0x9ea4, 0x00},
793 + {0x9ea5, 0x00},
794 + {0x9ea6, 0x3f},
795 + {0x9ea7, 0x3f},
796 + {0x9ea8, 0x3f},
797 + {0x9ea9, 0x00},
798 + {0x9eaa, 0x00},
799 + {0x9eab, 0x00},
800 + {0x9eac, 0x09},
801 + {0x9ead, 0x09},
802 + {0x9eae, 0x09},
803 + {0x9ec9, 0x0a},
804 + {0x9ecb, 0x0a},
805 + {0x9ecd, 0x0a},
806 + {0x9f17, 0x35},
807 + {0x9f1d, 0x31},
808 + {0x9f29, 0x50},
809 + {0x9f3b, 0x2f},
810 + {0x9f41, 0x6b},
811 + {0x9f47, 0x42},
812 + {0x9f4d, 0x5a},
813 + {0x9f6b, 0x00},
814 + {0x9f71, 0xc8},
815 + {0x9f73, 0x32},
816 + {0x9f75, 0x04},
817 + {0x9f94, 0x0f},
818 + {0x9f95, 0x0f},
819 + {0x9f96, 0x0f},
820 + {0x9f97, 0x00},
821 + {0x9f98, 0x00},
822 + {0x9f99, 0x00},
823 + {0x9f9a, 0x2f},
824 + {0x9f9b, 0x2f},
825 + {0x9f9c, 0x2f},
826 + {0x9f9d, 0x00},
827 + {0x9f9e, 0x00},
828 + {0x9f9f, 0x00},
829 + {0x9fa0, 0x0f},
830 + {0x9fa1, 0x0f},
831 + {0x9fa2, 0x0f},
832 + {0x9fa3, 0x00},
833 + {0x9fa4, 0x00},
834 + {0x9fa5, 0x00},
835 + {0x9fa6, 0x1e},
836 + {0x9fa7, 0x1e},
837 + {0x9fa8, 0x1e},
838 + {0x9fa9, 0x00},
839 + {0x9faa, 0x00},
840 + {0x9fab, 0x00},
841 + {0x9fac, 0x09},
842 + {0x9fad, 0x09},
843 + {0x9fae, 0x09},
844 + {0x9fc9, 0x0a},
845 + {0x9fcb, 0x0a},
846 + {0x9fcd, 0x0a},
847 + {0xa14b, 0xff},
848 + {0xa151, 0x0c},
849 + {0xa153, 0x50},
850 + {0xa155, 0x02},
851 + {0xa157, 0x00},
852 + {0xa1ad, 0xff},
853 + {0xa1b3, 0x0c},
854 + {0xa1b5, 0x50},
855 + {0xa1b9, 0x00},
856 + {0xa24b, 0xff},
857 + {0xa257, 0x00},
858 + {0xa2ad, 0xff},
859 + {0xa2b9, 0x00},
860 + {0xb21f, 0x04},
861 + {0xb35c, 0x00},
862 + {0xb35e, 0x08},
863 + {0x0112, 0x0c},
864 + {0x0113, 0x0c},
865 + {0x0114, 0x01},
866 + {0x0350, 0x00},
867 + {0xbcf1, 0x02},
868 + {0x3ff9, 0x01},
869 +};
870 +
871 +/* 12 mpix 10fps */
872 +static const struct imx477_reg mode_4056x3040_regs[] = {
873 + {0x0342, 0x5d},
874 + {0x0343, 0xc0},
875 + {0x0344, 0x00},
876 + {0x0345, 0x00},
877 + {0x0346, 0x00},
878 + {0x0347, 0x00},
879 + {0x0348, 0x0f},
880 + {0x0349, 0xd7},
881 + {0x034a, 0x0b},
882 + {0x034b, 0xdf},
883 + {0x00e3, 0x00},
884 + {0x00e4, 0x00},
885 + {0x00fc, 0x0a},
886 + {0x00fd, 0x0a},
887 + {0x00fe, 0x0a},
888 + {0x00ff, 0x0a},
889 + {0x0220, 0x00},
890 + {0x0221, 0x11},
891 + {0x0381, 0x01},
892 + {0x0383, 0x01},
893 + {0x0385, 0x01},
894 + {0x0387, 0x01},
895 + {0x0900, 0x00},
896 + {0x0901, 0x11},
897 + {0x0902, 0x02},
898 + {0x3140, 0x02},
899 + {0x3c00, 0x00},
900 + {0x3c01, 0x03},
901 + {0x3c02, 0xa2},
902 + {0x3f0d, 0x01},
903 + {0x5748, 0x07},
904 + {0x5749, 0xff},
905 + {0x574a, 0x00},
906 + {0x574b, 0x00},
907 + {0x7b75, 0x0a},
908 + {0x7b76, 0x0c},
909 + {0x7b77, 0x07},
910 + {0x7b78, 0x06},
911 + {0x7b79, 0x3c},
912 + {0x7b53, 0x01},
913 + {0x9369, 0x5a},
914 + {0x936b, 0x55},
915 + {0x936d, 0x28},
916 + {0x9304, 0x00},
917 + {0x9305, 0x00},
918 + {0x9e9a, 0x2f},
919 + {0x9e9b, 0x2f},
920 + {0x9e9c, 0x2f},
921 + {0x9e9d, 0x00},
922 + {0x9e9e, 0x00},
923 + {0x9e9f, 0x00},
924 + {0xa2a9, 0x60},
925 + {0xa2b7, 0x00},
926 + {0x0401, 0x00},
927 + {0x0404, 0x00},
928 + {0x0405, 0x10},
929 + {0x0408, 0x00},
930 + {0x0409, 0x00},
931 + {0x040a, 0x00},
932 + {0x040b, 0x00},
933 + {0x040c, 0x0f},
934 + {0x040d, 0xd8},
935 + {0x040e, 0x0b},
936 + {0x040f, 0xe0},
937 + {0x034c, 0x0f},
938 + {0x034d, 0xd8},
939 + {0x034e, 0x0b},
940 + {0x034f, 0xe0},
941 + {0x0301, 0x05},
942 + {0x0303, 0x02},
943 + {0x0305, 0x04},
944 + {0x0306, 0x01},
945 + {0x0307, 0x5e},
946 + {0x0309, 0x0c},
947 + {0x030b, 0x02},
948 + {0x030d, 0x02},
949 + {0x030e, 0x00},
950 + {0x030f, 0x96},
951 + {0x0310, 0x01},
952 + {0x0820, 0x07},
953 + {0x0821, 0x08},
954 + {0x0822, 0x00},
955 + {0x0823, 0x00},
956 + {0x080a, 0x00},
957 + {0x080b, 0x7f},
958 + {0x080c, 0x00},
959 + {0x080d, 0x4f},
960 + {0x080e, 0x00},
961 + {0x080f, 0x77},
962 + {0x0810, 0x00},
963 + {0x0811, 0x5f},
964 + {0x0812, 0x00},
965 + {0x0813, 0x57},
966 + {0x0814, 0x00},
967 + {0x0815, 0x4f},
968 + {0x0816, 0x01},
969 + {0x0817, 0x27},
970 + {0x0818, 0x00},
971 + {0x0819, 0x3f},
972 + {0xe04c, 0x00},
973 + {0xe04d, 0x7f},
974 + {0xe04e, 0x00},
975 + {0xe04f, 0x1f},
976 + {0x3e20, 0x01},
977 + {0x3e37, 0x00},
978 + {0x3f50, 0x00},
979 + {0x3f56, 0x02},
980 + {0x3f57, 0xae},
981 +};
982 +
983 +/* 2x2 binned. 40fps */
984 +static const struct imx477_reg mode_2028x1520_regs[] = {
985 + {0x0342, 0x31},
986 + {0x0343, 0xc4},
987 + {0x0344, 0x00},
988 + {0x0345, 0x00},
989 + {0x0346, 0x00},
990 + {0x0347, 0x00},
991 + {0x0348, 0x0f},
992 + {0x0349, 0xd7},
993 + {0x034a, 0x0b},
994 + {0x034b, 0xdf},
995 + {0x0220, 0x00},
996 + {0x0221, 0x11},
997 + {0x0381, 0x01},
998 + {0x0383, 0x01},
999 + {0x0385, 0x01},
1000 + {0x0387, 0x01},
1001 + {0x0900, 0x01},
1002 + {0x0901, 0x12},
1003 + {0x0902, 0x02},
1004 + {0x3140, 0x02},
1005 + {0x3c00, 0x00},
1006 + {0x3c01, 0x03},
1007 + {0x3c02, 0xa2},
1008 + {0x3f0d, 0x01},
1009 + {0x5748, 0x07},
1010 + {0x5749, 0xff},
1011 + {0x574a, 0x00},
1012 + {0x574b, 0x00},
1013 + {0x7b53, 0x01},
1014 + {0x9369, 0x73},
1015 + {0x936b, 0x64},
1016 + {0x936d, 0x5f},
1017 + {0x9304, 0x00},
1018 + {0x9305, 0x00},
1019 + {0x9e9a, 0x2f},
1020 + {0x9e9b, 0x2f},
1021 + {0x9e9c, 0x2f},
1022 + {0x9e9d, 0x00},
1023 + {0x9e9e, 0x00},
1024 + {0x9e9f, 0x00},
1025 + {0xa2a9, 0x60},
1026 + {0xa2b7, 0x00},
1027 + {0x0401, 0x01},
1028 + {0x0404, 0x00},
1029 + {0x0405, 0x20},
1030 + {0x0408, 0x00},
1031 + {0x0409, 0x00},
1032 + {0x040a, 0x00},
1033 + {0x040b, 0x00},
1034 + {0x040c, 0x0f},
1035 + {0x040d, 0xd8},
1036 + {0x040e, 0x0b},
1037 + {0x040f, 0xe0},
1038 + {0x034c, 0x07},
1039 + {0x034d, 0xec},
1040 + {0x034e, 0x05},
1041 + {0x034f, 0xf0},
1042 + {0x0301, 0x05},
1043 + {0x0303, 0x02},
1044 + {0x0305, 0x04},
1045 + {0x0306, 0x01},
1046 + {0x0307, 0x5e},
1047 + {0x0309, 0x0c},
1048 + {0x030b, 0x02},
1049 + {0x030d, 0x02},
1050 + {0x030e, 0x00},
1051 + {0x030f, 0x96},
1052 + {0x0310, 0x01},
1053 + {0x0820, 0x07},
1054 + {0x0821, 0x08},
1055 + {0x0822, 0x00},
1056 + {0x0823, 0x00},
1057 + {0x080a, 0x00},
1058 + {0x080b, 0x7f},
1059 + {0x080c, 0x00},
1060 + {0x080d, 0x4f},
1061 + {0x080e, 0x00},
1062 + {0x080f, 0x77},
1063 + {0x0810, 0x00},
1064 + {0x0811, 0x5f},
1065 + {0x0812, 0x00},
1066 + {0x0813, 0x57},
1067 + {0x0814, 0x00},
1068 + {0x0815, 0x4f},
1069 + {0x0816, 0x01},
1070 + {0x0817, 0x27},
1071 + {0x0818, 0x00},
1072 + {0x0819, 0x3f},
1073 + {0xe04c, 0x00},
1074 + {0xe04d, 0x7f},
1075 + {0xe04e, 0x00},
1076 + {0xe04f, 0x1f},
1077 + {0x3e20, 0x01},
1078 + {0x3e37, 0x00},
1079 + {0x3f50, 0x00},
1080 + {0x3f56, 0x01},
1081 + {0x3f57, 0x6c},
1082 +};
1083 +
1084 +/* 1080p cropped mode */
1085 +static const struct imx477_reg mode_2028x1080_regs[] = {
1086 + {0x0342, 0x31},
1087 + {0x0343, 0xc4},
1088 + {0x0344, 0x00},
1089 + {0x0345, 0x00},
1090 + {0x0346, 0x01},
1091 + {0x0347, 0xb8},
1092 + {0x0348, 0x0f},
1093 + {0x0349, 0xd7},
1094 + {0x034a, 0x0a},
1095 + {0x034b, 0x27},
1096 + {0x0220, 0x00},
1097 + {0x0221, 0x11},
1098 + {0x0381, 0x01},
1099 + {0x0383, 0x01},
1100 + {0x0385, 0x01},
1101 + {0x0387, 0x01},
1102 + {0x0900, 0x01},
1103 + {0x0901, 0x12},
1104 + {0x0902, 0x02},
1105 + {0x3140, 0x02},
1106 + {0x3c00, 0x00},
1107 + {0x3c01, 0x03},
1108 + {0x3c02, 0xa2},
1109 + {0x3f0d, 0x01},
1110 + {0x5748, 0x07},
1111 + {0x5749, 0xff},
1112 + {0x574a, 0x00},
1113 + {0x574b, 0x00},
1114 + {0x7b53, 0x01},
1115 + {0x9369, 0x73},
1116 + {0x936b, 0x64},
1117 + {0x936d, 0x5f},
1118 + {0x9304, 0x00},
1119 + {0x9305, 0x00},
1120 + {0x9e9a, 0x2f},
1121 + {0x9e9b, 0x2f},
1122 + {0x9e9c, 0x2f},
1123 + {0x9e9d, 0x00},
1124 + {0x9e9e, 0x00},
1125 + {0x9e9f, 0x00},
1126 + {0xa2a9, 0x60},
1127 + {0xa2b7, 0x00},
1128 + {0x0401, 0x01},
1129 + {0x0404, 0x00},
1130 + {0x0405, 0x20},
1131 + {0x0408, 0x00},
1132 + {0x0409, 0x00},
1133 + {0x040a, 0x00},
1134 + {0x040b, 0x00},
1135 + {0x040c, 0x0f},
1136 + {0x040d, 0xd8},
1137 + {0x040e, 0x04},
1138 + {0x040f, 0x38},
1139 + {0x034c, 0x07},
1140 + {0x034d, 0xec},
1141 + {0x034e, 0x04},
1142 + {0x034f, 0x38},
1143 + {0x0301, 0x05},
1144 + {0x0303, 0x02},
1145 + {0x0305, 0x04},
1146 + {0x0306, 0x01},
1147 + {0x0307, 0x5e},
1148 + {0x0309, 0x0c},
1149 + {0x030b, 0x02},
1150 + {0x030d, 0x02},
1151 + {0x030e, 0x00},
1152 + {0x030f, 0x96},
1153 + {0x0310, 0x01},
1154 + {0x0820, 0x07},
1155 + {0x0821, 0x08},
1156 + {0x0822, 0x00},
1157 + {0x0823, 0x00},
1158 + {0x080a, 0x00},
1159 + {0x080b, 0x7f},
1160 + {0x080c, 0x00},
1161 + {0x080d, 0x4f},
1162 + {0x080e, 0x00},
1163 + {0x080f, 0x77},
1164 + {0x0810, 0x00},
1165 + {0x0811, 0x5f},
1166 + {0x0812, 0x00},
1167 + {0x0813, 0x57},
1168 + {0x0814, 0x00},
1169 + {0x0815, 0x4f},
1170 + {0x0816, 0x01},
1171 + {0x0817, 0x27},
1172 + {0x0818, 0x00},
1173 + {0x0819, 0x3f},
1174 + {0xe04c, 0x00},
1175 + {0xe04d, 0x7f},
1176 + {0xe04e, 0x00},
1177 + {0xe04f, 0x1f},
1178 + {0x3e20, 0x01},
1179 + {0x3e37, 0x00},
1180 + {0x3f50, 0x00},
1181 + {0x3f56, 0x01},
1182 + {0x3f57, 0x6c},
1183 +};
1184 +
1185 +/* 4x4 binned. 120fps */
1186 +static const struct imx477_reg mode_1332x990_regs[] = {
1187 + {0x420b, 0x01},
1188 + {0x990c, 0x00},
1189 + {0x990d, 0x08},
1190 + {0x9956, 0x8c},
1191 + {0x9957, 0x64},
1192 + {0x9958, 0x50},
1193 + {0x9a48, 0x06},
1194 + {0x9a49, 0x06},
1195 + {0x9a4a, 0x06},
1196 + {0x9a4b, 0x06},
1197 + {0x9a4c, 0x06},
1198 + {0x9a4d, 0x06},
1199 + {0x0112, 0x0a},
1200 + {0x0113, 0x0a},
1201 + {0x0114, 0x01},
1202 + {0x0342, 0x1a},
1203 + {0x0343, 0x08},
1204 + {0x0340, 0x04},
1205 + {0x0341, 0x1a},
1206 + {0x0344, 0x00},
1207 + {0x0345, 0x00},
1208 + {0x0346, 0x02},
1209 + {0x0347, 0x10},
1210 + {0x0348, 0x0f},
1211 + {0x0349, 0xd7},
1212 + {0x034a, 0x09},
1213 + {0x034b, 0xcf},
1214 + {0x00e3, 0x00},
1215 + {0x00e4, 0x00},
1216 + {0x00fc, 0x0a},
1217 + {0x00fd, 0x0a},
1218 + {0x00fe, 0x0a},
1219 + {0x00ff, 0x0a},
1220 + {0xe013, 0x00},
1221 + {0x0220, 0x00},
1222 + {0x0221, 0x11},
1223 + {0x0381, 0x01},
1224 + {0x0383, 0x01},
1225 + {0x0385, 0x01},
1226 + {0x0387, 0x01},
1227 + {0x0900, 0x01},
1228 + {0x0901, 0x22},
1229 + {0x0902, 0x02},
1230 + {0x3140, 0x02},
1231 + {0x3c00, 0x00},
1232 + {0x3c01, 0x01},
1233 + {0x3c02, 0x9c},
1234 + {0x3f0d, 0x00},
1235 + {0x5748, 0x00},
1236 + {0x5749, 0x00},
1237 + {0x574a, 0x00},
1238 + {0x574b, 0xa4},
1239 + {0x7b75, 0x0e},
1240 + {0x7b76, 0x09},
1241 + {0x7b77, 0x08},
1242 + {0x7b78, 0x06},
1243 + {0x7b79, 0x34},
1244 + {0x7b53, 0x00},
1245 + {0x9369, 0x73},
1246 + {0x936b, 0x64},
1247 + {0x936d, 0x5f},
1248 + {0x9304, 0x03},
1249 + {0x9305, 0x80},
1250 + {0x9e9a, 0x2f},
1251 + {0x9e9b, 0x2f},
1252 + {0x9e9c, 0x2f},
1253 + {0x9e9d, 0x00},
1254 + {0x9e9e, 0x00},
1255 + {0x9e9f, 0x00},
1256 + {0xa2a9, 0x27},
1257 + {0xa2b7, 0x03},
1258 + {0x0401, 0x00},
1259 + {0x0404, 0x00},
1260 + {0x0405, 0x10},
1261 + {0x0408, 0x01},
1262 + {0x0409, 0x5c},
1263 + {0x040a, 0x00},
1264 + {0x040b, 0x00},
1265 + {0x040c, 0x05},
1266 + {0x040d, 0x34},
1267 + {0x040e, 0x03},
1268 + {0x040f, 0xde},
1269 + {0x034c, 0x05},
1270 + {0x034d, 0x34},
1271 + {0x034e, 0x03},
1272 + {0x034f, 0xde},
1273 + {0x0301, 0x05},
1274 + {0x0303, 0x02},
1275 + {0x0305, 0x02},
1276 + {0x0306, 0x00},
1277 + {0x0307, 0xaf},
1278 + {0x0309, 0x0a},
1279 + {0x030b, 0x02},
1280 + {0x030d, 0x02},
1281 + {0x030e, 0x00},
1282 + {0x030f, 0x96},
1283 + {0x0310, 0x01},
1284 + {0x0820, 0x07},
1285 + {0x0821, 0x08},
1286 + {0x0822, 0x00},
1287 + {0x0823, 0x00},
1288 + {0x080a, 0x00},
1289 + {0x080b, 0x7f},
1290 + {0x080c, 0x00},
1291 + {0x080d, 0x4f},
1292 + {0x080e, 0x00},
1293 + {0x080f, 0x77},
1294 + {0x0810, 0x00},
1295 + {0x0811, 0x5f},
1296 + {0x0812, 0x00},
1297 + {0x0813, 0x57},
1298 + {0x0814, 0x00},
1299 + {0x0815, 0x4f},
1300 + {0x0816, 0x01},
1301 + {0x0817, 0x27},
1302 + {0x0818, 0x00},
1303 + {0x0819, 0x3f},
1304 + {0xe04c, 0x00},
1305 + {0xe04d, 0x5f},
1306 + {0xe04e, 0x00},
1307 + {0xe04f, 0x1f},
1308 + {0x3e20, 0x01},
1309 + {0x3e37, 0x00},
1310 + {0x3f50, 0x00},
1311 + {0x3f56, 0x00},
1312 + {0x3f57, 0xbf},
1313 +};
1314 +
1315 +/* Mode configs */
1316 +static const struct imx477_mode supported_modes_12bit[] = {
1317 + {
1318 + /* 12MPix 10fps mode */
1319 + .width = 4056,
1320 + .height = 3040,
1321 + .line_length_pix = 0x5dc0,
1322 + .crop = {
1323 + .left = IMX477_PIXEL_ARRAY_LEFT,
1324 + .top = IMX477_PIXEL_ARRAY_TOP,
1325 + .width = 4056,
1326 + .height = 3040,
1327 + },
1328 + .timeperframe_min = {
1329 + .numerator = 100,
1330 + .denominator = 1000
1331 + },
1332 + .timeperframe_default = {
1333 + .numerator = 100,
1334 + .denominator = 1000
1335 + },
1336 + .reg_list = {
1337 + .num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
1338 + .regs = mode_4056x3040_regs,
1339 + },
1340 + },
1341 + {
1342 + /* 2x2 binned 40fps mode */
1343 + .width = 2028,
1344 + .height = 1520,
1345 + .line_length_pix = 0x31c4,
1346 + .crop = {
1347 + .left = IMX477_PIXEL_ARRAY_LEFT,
1348 + .top = IMX477_PIXEL_ARRAY_TOP,
1349 + .width = 4056,
1350 + .height = 3040,
1351 + },
1352 + .timeperframe_min = {
1353 + .numerator = 100,
1354 + .denominator = 4000
1355 + },
1356 + .timeperframe_default = {
1357 + .numerator = 100,
1358 + .denominator = 3000
1359 + },
1360 + .reg_list = {
1361 + .num_of_regs = ARRAY_SIZE(mode_2028x1520_regs),
1362 + .regs = mode_2028x1520_regs,
1363 + },
1364 + },
1365 + {
1366 + /* 1080p 50fps cropped mode */
1367 + .width = 2028,
1368 + .height = 1080,
1369 + .line_length_pix = 0x31c4,
1370 + .crop = {
1371 + .left = IMX477_PIXEL_ARRAY_LEFT,
1372 + .top = IMX477_PIXEL_ARRAY_TOP + 440,
1373 + .width = 4056,
1374 + .height = 2160,
1375 + },
1376 + .timeperframe_min = {
1377 + .numerator = 100,
1378 + .denominator = 5000
1379 + },
1380 + .timeperframe_default = {
1381 + .numerator = 100,
1382 + .denominator = 3000
1383 + },
1384 + .reg_list = {
1385 + .num_of_regs = ARRAY_SIZE(mode_2028x1080_regs),
1386 + .regs = mode_2028x1080_regs,
1387 + },
1388 + }
1389 +};
1390 +
1391 +static const struct imx477_mode supported_modes_10bit[] = {
1392 + {
1393 + /* 120fps. 2x2 binned and cropped */
1394 + .width = 1332,
1395 + .height = 990,
1396 + .line_length_pix = 6664,
1397 + .crop = {
1398 + /*
1399 + * FIXME: the analog crop rectangle is actually
1400 + * programmed with a horizontal displacement of 0
1401 + * pixels, not 4. It gets shrunk after going through
1402 + * the scaler. Move this information to the compose
1403 + * rectangle once the driver is expanded to represent
1404 + * its processing blocks with multiple subdevs.
1405 + */
1406 + .left = IMX477_PIXEL_ARRAY_LEFT + 696,
1407 + .top = IMX477_PIXEL_ARRAY_TOP + 528,
1408 + .width = 2664,
1409 + .height = 1980,
1410 + },
1411 + .timeperframe_min = {
1412 + .numerator = 100,
1413 + .denominator = 12000
1414 + },
1415 + .timeperframe_default = {
1416 + .numerator = 100,
1417 + .denominator = 12000
1418 + },
1419 + .reg_list = {
1420 + .num_of_regs = ARRAY_SIZE(mode_1332x990_regs),
1421 + .regs = mode_1332x990_regs,
1422 + }
1423 + }
1424 +};
1425 +
1426 +/*
1427 + * The supported formats.
1428 + * This table MUST contain 4 entries per format, to cover the various flip
1429 + * combinations in the order
1430 + * - no flip
1431 + * - h flip
1432 + * - v flip
1433 + * - h&v flips
1434 + */
1435 +static const u32 codes[] = {
1436 + /* 12-bit modes. */
1437 + MEDIA_BUS_FMT_SRGGB12_1X12,
1438 + MEDIA_BUS_FMT_SGRBG12_1X12,
1439 + MEDIA_BUS_FMT_SGBRG12_1X12,
1440 + MEDIA_BUS_FMT_SBGGR12_1X12,
1441 + /* 10-bit modes. */
1442 + MEDIA_BUS_FMT_SRGGB10_1X10,
1443 + MEDIA_BUS_FMT_SGRBG10_1X10,
1444 + MEDIA_BUS_FMT_SGBRG10_1X10,
1445 + MEDIA_BUS_FMT_SBGGR10_1X10,
1446 +};
1447 +
1448 +static const char * const imx477_test_pattern_menu[] = {
1449 + "Disabled",
1450 + "Color Bars",
1451 + "Solid Color",
1452 + "Grey Color Bars",
1453 + "PN9"
1454 +};
1455 +
1456 +static const int imx477_test_pattern_val[] = {
1457 + IMX477_TEST_PATTERN_DISABLE,
1458 + IMX477_TEST_PATTERN_COLOR_BARS,
1459 + IMX477_TEST_PATTERN_SOLID_COLOR,
1460 + IMX477_TEST_PATTERN_GREY_COLOR,
1461 + IMX477_TEST_PATTERN_PN9,
1462 +};
1463 +
1464 +/* regulator supplies */
1465 +static const char * const imx477_supply_name[] = {
1466 + /* Supplies can be enabled in any order */
1467 + "VANA", /* Analog (2.8V) supply */
1468 + "VDIG", /* Digital Core (1.05V) supply */
1469 + "VDDL", /* IF (1.8V) supply */
1470 +};
1471 +
1472 +#define IMX477_NUM_SUPPLIES ARRAY_SIZE(imx477_supply_name)
1473 +
1474 +/*
1475 + * Initialisation delay between XCLR low->high and the moment when the sensor
1476 + * can start capture (i.e. can leave software standby), given by T7 in the
1477 + * datasheet is 8ms. This does include I2C setup time as well.
1478 + *
1479 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1480 + * in the datasheet) is much smaller - 600us.
1481 + */
1482 +#define IMX477_XCLR_MIN_DELAY_US 8000
1483 +#define IMX477_XCLR_DELAY_RANGE_US 1000
1484 +
1485 +struct imx477_compatible_data {
1486 + unsigned int chip_id;
1487 + struct imx477_reg_list extra_regs;
1488 +};
1489 +
1490 +struct imx477 {
1491 + struct v4l2_subdev sd;
1492 + struct media_pad pad[NUM_PADS];
1493 +
1494 + unsigned int fmt_code;
1495 +
1496 + struct clk *xclk;
1497 + u32 xclk_freq;
1498 +
1499 + struct gpio_desc *reset_gpio;
1500 + struct regulator_bulk_data supplies[IMX477_NUM_SUPPLIES];
1501 +
1502 + struct v4l2_ctrl_handler ctrl_handler;
1503 + /* V4L2 Controls */
1504 + struct v4l2_ctrl *pixel_rate;
1505 + struct v4l2_ctrl *exposure;
1506 + struct v4l2_ctrl *vflip;
1507 + struct v4l2_ctrl *hflip;
1508 + struct v4l2_ctrl *vblank;
1509 + struct v4l2_ctrl *hblank;
1510 +
1511 + /* Current mode */
1512 + const struct imx477_mode *mode;
1513 +
1514 + /*
1515 + * Mutex for serialized access:
1516 + * Protect sensor module set pad format and start/stop streaming safely.
1517 + */
1518 + struct mutex mutex;
1519 +
1520 + /* Streaming on/off */
1521 + bool streaming;
1522 +
1523 + /* Rewrite common registers on stream on? */
1524 + bool common_regs_written;
1525 +
1526 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1527 + unsigned int long_exp_shift;
1528 +
1529 + /* Any extra information related to different compatible sensors */
1530 + const struct imx477_compatible_data *compatible_data;
1531 +};
1532 +
1533 +static inline struct imx477 *to_imx477(struct v4l2_subdev *_sd)
1534 +{
1535 + return container_of(_sd, struct imx477, sd);
1536 +}
1537 +
1538 +static inline void get_mode_table(unsigned int code,
1539 + const struct imx477_mode **mode_list,
1540 + unsigned int *num_modes)
1541 +{
1542 + switch (code) {
1543 + /* 12-bit */
1544 + case MEDIA_BUS_FMT_SRGGB12_1X12:
1545 + case MEDIA_BUS_FMT_SGRBG12_1X12:
1546 + case MEDIA_BUS_FMT_SGBRG12_1X12:
1547 + case MEDIA_BUS_FMT_SBGGR12_1X12:
1548 + *mode_list = supported_modes_12bit;
1549 + *num_modes = ARRAY_SIZE(supported_modes_12bit);
1550 + break;
1551 + /* 10-bit */
1552 + case MEDIA_BUS_FMT_SRGGB10_1X10:
1553 + case MEDIA_BUS_FMT_SGRBG10_1X10:
1554 + case MEDIA_BUS_FMT_SGBRG10_1X10:
1555 + case MEDIA_BUS_FMT_SBGGR10_1X10:
1556 + *mode_list = supported_modes_10bit;
1557 + *num_modes = ARRAY_SIZE(supported_modes_10bit);
1558 + break;
1559 + default:
1560 + *mode_list = NULL;
1561 + *num_modes = 0;
1562 + }
1563 +}
1564 +
1565 +/* Read registers up to 2 at a time */
1566 +static int imx477_read_reg(struct imx477 *imx477, u16 reg, u32 len, u32 *val)
1567 +{
1568 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1569 + struct i2c_msg msgs[2];
1570 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1571 + u8 data_buf[4] = { 0, };
1572 + int ret;
1573 +
1574 + if (len > 4)
1575 + return -EINVAL;
1576 +
1577 + /* Write register address */
1578 + msgs[0].addr = client->addr;
1579 + msgs[0].flags = 0;
1580 + msgs[0].len = ARRAY_SIZE(addr_buf);
1581 + msgs[0].buf = addr_buf;
1582 +
1583 + /* Read data from register */
1584 + msgs[1].addr = client->addr;
1585 + msgs[1].flags = I2C_M_RD;
1586 + msgs[1].len = len;
1587 + msgs[1].buf = &data_buf[4 - len];
1588 +
1589 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1590 + if (ret != ARRAY_SIZE(msgs))
1591 + return -EIO;
1592 +
1593 + *val = get_unaligned_be32(data_buf);
1594 +
1595 + return 0;
1596 +}
1597 +
1598 +/* Write registers up to 2 at a time */
1599 +static int imx477_write_reg(struct imx477 *imx477, u16 reg, u32 len, u32 val)
1600 +{
1601 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1602 + u8 buf[6];
1603 +
1604 + if (len > 4)
1605 + return -EINVAL;
1606 +
1607 + put_unaligned_be16(reg, buf);
1608 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1609 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1610 + return -EIO;
1611 +
1612 + return 0;
1613 +}
1614 +
1615 +/* Write a list of registers */
1616 +static int imx477_write_regs(struct imx477 *imx477,
1617 + const struct imx477_reg *regs, u32 len)
1618 +{
1619 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1620 + unsigned int i;
1621 + int ret;
1622 +
1623 + for (i = 0; i < len; i++) {
1624 + ret = imx477_write_reg(imx477, regs[i].address, 1, regs[i].val);
1625 + if (ret) {
1626 + dev_err_ratelimited(&client->dev,
1627 + "Failed to write reg 0x%4.4x. error = %d\n",
1628 + regs[i].address, ret);
1629 +
1630 + return ret;
1631 + }
1632 + }
1633 +
1634 + return 0;
1635 +}
1636 +
1637 +/* Get bayer order based on flip setting. */
1638 +static u32 imx477_get_format_code(struct imx477 *imx477, u32 code)
1639 +{
1640 + unsigned int i;
1641 +
1642 + lockdep_assert_held(&imx477->mutex);
1643 +
1644 + for (i = 0; i < ARRAY_SIZE(codes); i++)
1645 + if (codes[i] == code)
1646 + break;
1647 +
1648 + if (i >= ARRAY_SIZE(codes))
1649 + i = 0;
1650 +
1651 + i = (i & ~3) | (imx477->vflip->val ? 2 : 0) |
1652 + (imx477->hflip->val ? 1 : 0);
1653 +
1654 + return codes[i];
1655 +}
1656 +
1657 +static void imx477_set_default_format(struct imx477 *imx477)
1658 +{
1659 + /* Set default mode to max resolution */
1660 + imx477->mode = &supported_modes_12bit[0];
1661 + imx477->fmt_code = MEDIA_BUS_FMT_SRGGB12_1X12;
1662 +}
1663 +
1664 +static int imx477_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1665 +{
1666 + struct imx477 *imx477 = to_imx477(sd);
1667 + struct v4l2_mbus_framefmt *try_fmt_img =
1668 + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
1669 + struct v4l2_mbus_framefmt *try_fmt_meta =
1670 + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
1671 + struct v4l2_rect *try_crop;
1672 +
1673 + mutex_lock(&imx477->mutex);
1674 +
1675 + /* Initialize try_fmt for the image pad */
1676 + try_fmt_img->width = supported_modes_12bit[0].width;
1677 + try_fmt_img->height = supported_modes_12bit[0].height;
1678 + try_fmt_img->code = imx477_get_format_code(imx477,
1679 + MEDIA_BUS_FMT_SRGGB12_1X12);
1680 + try_fmt_img->field = V4L2_FIELD_NONE;
1681 +
1682 + /* Initialize try_fmt for the embedded metadata pad */
1683 + try_fmt_meta->width = IMX477_EMBEDDED_LINE_WIDTH;
1684 + try_fmt_meta->height = IMX477_NUM_EMBEDDED_LINES;
1685 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1686 + try_fmt_meta->field = V4L2_FIELD_NONE;
1687 +
1688 + /* Initialize try_crop */
1689 + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, IMAGE_PAD);
1690 + try_crop->left = IMX477_PIXEL_ARRAY_LEFT;
1691 + try_crop->top = IMX477_PIXEL_ARRAY_TOP;
1692 + try_crop->width = IMX477_PIXEL_ARRAY_WIDTH;
1693 + try_crop->height = IMX477_PIXEL_ARRAY_HEIGHT;
1694 +
1695 + mutex_unlock(&imx477->mutex);
1696 +
1697 + return 0;
1698 +}
1699 +
1700 +static void imx477_adjust_exposure_range(struct imx477 *imx477)
1701 +{
1702 + int exposure_max, exposure_def;
1703 +
1704 + /* Honour the VBLANK limits when setting exposure. */
1705 + exposure_max = imx477->mode->height + imx477->vblank->val -
1706 + IMX477_EXPOSURE_OFFSET;
1707 + exposure_def = min(exposure_max, imx477->exposure->val);
1708 + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
1709 + exposure_max, imx477->exposure->step,
1710 + exposure_def);
1711 +}
1712 +
1713 +static int imx477_set_frame_length(struct imx477 *imx477, unsigned int val)
1714 +{
1715 + int ret = 0;
1716 +
1717 + imx477->long_exp_shift = 0;
1718 +
1719 + while (val > IMX477_FRAME_LENGTH_MAX) {
1720 + imx477->long_exp_shift++;
1721 + val >>= 1;
1722 + }
1723 +
1724 + ret = imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
1725 + IMX477_REG_VALUE_16BIT, val);
1726 + if (ret)
1727 + return ret;
1728 +
1729 + return imx477_write_reg(imx477, IMX477_LONG_EXP_SHIFT_REG,
1730 + IMX477_REG_VALUE_08BIT, imx477->long_exp_shift);
1731 +}
1732 +
1733 +static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
1734 +{
1735 + struct imx477 *imx477 =
1736 + container_of(ctrl->handler, struct imx477, ctrl_handler);
1737 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
1738 + int ret = 0;
1739 +
1740 + /*
1741 + * The VBLANK control may change the limits of usable exposure, so check
1742 + * and adjust if necessary.
1743 + */
1744 + if (ctrl->id == V4L2_CID_VBLANK)
1745 + imx477_adjust_exposure_range(imx477);
1746 +
1747 + /*
1748 + * Applying V4L2 control value only happens
1749 + * when power is up for streaming
1750 + */
1751 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1752 + return 0;
1753 +
1754 + switch (ctrl->id) {
1755 + case V4L2_CID_ANALOGUE_GAIN:
1756 + ret = imx477_write_reg(imx477, IMX477_REG_ANALOG_GAIN,
1757 + IMX477_REG_VALUE_16BIT, ctrl->val);
1758 + break;
1759 + case V4L2_CID_EXPOSURE:
1760 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
1761 + IMX477_REG_VALUE_16BIT, ctrl->val >>
1762 + imx477->long_exp_shift);
1763 + break;
1764 + case V4L2_CID_DIGITAL_GAIN:
1765 + ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
1766 + IMX477_REG_VALUE_16BIT, ctrl->val);
1767 + break;
1768 + case V4L2_CID_TEST_PATTERN:
1769 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN,
1770 + IMX477_REG_VALUE_16BIT,
1771 + imx477_test_pattern_val[ctrl->val]);
1772 + break;
1773 + case V4L2_CID_TEST_PATTERN_RED:
1774 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_R,
1775 + IMX477_REG_VALUE_16BIT, ctrl->val);
1776 + break;
1777 + case V4L2_CID_TEST_PATTERN_GREENR:
1778 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GR,
1779 + IMX477_REG_VALUE_16BIT, ctrl->val);
1780 + break;
1781 + case V4L2_CID_TEST_PATTERN_BLUE:
1782 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_B,
1783 + IMX477_REG_VALUE_16BIT, ctrl->val);
1784 + break;
1785 + case V4L2_CID_TEST_PATTERN_GREENB:
1786 + ret = imx477_write_reg(imx477, IMX477_REG_TEST_PATTERN_GB,
1787 + IMX477_REG_VALUE_16BIT, ctrl->val);
1788 + break;
1789 + case V4L2_CID_HFLIP:
1790 + case V4L2_CID_VFLIP:
1791 + ret = imx477_write_reg(imx477, IMX477_REG_ORIENTATION, 1,
1792 + imx477->hflip->val |
1793 + imx477->vflip->val << 1);
1794 + break;
1795 + case V4L2_CID_VBLANK:
1796 + ret = imx477_set_frame_length(imx477,
1797 + imx477->mode->height + ctrl->val);
1798 + break;
1799 + case V4L2_CID_HBLANK:
1800 + ret = imx477_write_reg(imx477, IMX477_REG_LINE_LENGTH, 2,
1801 + imx477->mode->width + ctrl->val);
1802 + break;
1803 + default:
1804 + dev_info(&client->dev,
1805 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1806 + ctrl->id, ctrl->val);
1807 + ret = -EINVAL;
1808 + break;
1809 + }
1810 +
1811 + pm_runtime_put(&client->dev);
1812 +
1813 + return ret;
1814 +}
1815 +
1816 +static const struct v4l2_ctrl_ops imx477_ctrl_ops = {
1817 + .s_ctrl = imx477_set_ctrl,
1818 +};
1819 +
1820 +static int imx477_enum_mbus_code(struct v4l2_subdev *sd,
1821 + struct v4l2_subdev_pad_config *cfg,
1822 + struct v4l2_subdev_mbus_code_enum *code)
1823 +{
1824 + struct imx477 *imx477 = to_imx477(sd);
1825 +
1826 + if (code->pad >= NUM_PADS)
1827 + return -EINVAL;
1828 +
1829 + if (code->pad == IMAGE_PAD) {
1830 + if (code->index >= (ARRAY_SIZE(codes) / 4))
1831 + return -EINVAL;
1832 +
1833 + code->code = imx477_get_format_code(imx477,
1834 + codes[code->index * 4]);
1835 + } else {
1836 + if (code->index > 0)
1837 + return -EINVAL;
1838 +
1839 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1840 + }
1841 +
1842 + return 0;
1843 +}
1844 +
1845 +static int imx477_enum_frame_size(struct v4l2_subdev *sd,
1846 + struct v4l2_subdev_pad_config *cfg,
1847 + struct v4l2_subdev_frame_size_enum *fse)
1848 +{
1849 + struct imx477 *imx477 = to_imx477(sd);
1850 +
1851 + if (fse->pad >= NUM_PADS)
1852 + return -EINVAL;
1853 +
1854 + if (fse->pad == IMAGE_PAD) {
1855 + const struct imx477_mode *mode_list;
1856 + unsigned int num_modes;
1857 +
1858 + get_mode_table(fse->code, &mode_list, &num_modes);
1859 +
1860 + if (fse->index >= num_modes)
1861 + return -EINVAL;
1862 +
1863 + if (fse->code != imx477_get_format_code(imx477, fse->code))
1864 + return -EINVAL;
1865 +
1866 + fse->min_width = mode_list[fse->index].width;
1867 + fse->max_width = fse->min_width;
1868 + fse->min_height = mode_list[fse->index].height;
1869 + fse->max_height = fse->min_height;
1870 + } else {
1871 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1872 + return -EINVAL;
1873 +
1874 + fse->min_width = IMX477_EMBEDDED_LINE_WIDTH;
1875 + fse->max_width = fse->min_width;
1876 + fse->min_height = IMX477_NUM_EMBEDDED_LINES;
1877 + fse->max_height = fse->min_height;
1878 + }
1879 +
1880 + return 0;
1881 +}
1882 +
1883 +static void imx477_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1884 +{
1885 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1886 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1887 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1888 + fmt->colorspace,
1889 + fmt->ycbcr_enc);
1890 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1891 +}
1892 +
1893 +static void imx477_update_image_pad_format(struct imx477 *imx477,
1894 + const struct imx477_mode *mode,
1895 + struct v4l2_subdev_format *fmt)
1896 +{
1897 + fmt->format.width = mode->width;
1898 + fmt->format.height = mode->height;
1899 + fmt->format.field = V4L2_FIELD_NONE;
1900 + imx477_reset_colorspace(&fmt->format);
1901 +}
1902 +
1903 +static void imx477_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1904 +{
1905 + fmt->format.width = IMX477_EMBEDDED_LINE_WIDTH;
1906 + fmt->format.height = IMX477_NUM_EMBEDDED_LINES;
1907 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1908 + fmt->format.field = V4L2_FIELD_NONE;
1909 +}
1910 +
1911 +static int imx477_get_pad_format(struct v4l2_subdev *sd,
1912 + struct v4l2_subdev_pad_config *cfg,
1913 + struct v4l2_subdev_format *fmt)
1914 +{
1915 + struct imx477 *imx477 = to_imx477(sd);
1916 +
1917 + if (fmt->pad >= NUM_PADS)
1918 + return -EINVAL;
1919 +
1920 + mutex_lock(&imx477->mutex);
1921 +
1922 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1923 + struct v4l2_mbus_framefmt *try_fmt =
1924 + v4l2_subdev_get_try_format(&imx477->sd, cfg, fmt->pad);
1925 + /* update the code which could change due to vflip or hflip: */
1926 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1927 + imx477_get_format_code(imx477, try_fmt->code) :
1928 + MEDIA_BUS_FMT_SENSOR_DATA;
1929 + fmt->format = *try_fmt;
1930 + } else {
1931 + if (fmt->pad == IMAGE_PAD) {
1932 + imx477_update_image_pad_format(imx477, imx477->mode,
1933 + fmt);
1934 + fmt->format.code =
1935 + imx477_get_format_code(imx477, imx477->fmt_code);
1936 + } else {
1937 + imx477_update_metadata_pad_format(fmt);
1938 + }
1939 + }
1940 +
1941 + mutex_unlock(&imx477->mutex);
1942 + return 0;
1943 +}
1944 +
1945 +static
1946 +unsigned int imx477_get_frame_length(const struct imx477_mode *mode,
1947 + const struct v4l2_fract *timeperframe)
1948 +{
1949 + u64 frame_length;
1950 +
1951 + frame_length = (u64)timeperframe->numerator * IMX477_PIXEL_RATE;
1952 + do_div(frame_length,
1953 + (u64)timeperframe->denominator * mode->line_length_pix);
1954 +
1955 + if (WARN_ON(frame_length > IMX477_FRAME_LENGTH_MAX))
1956 + frame_length = IMX477_FRAME_LENGTH_MAX;
1957 +
1958 + return max_t(unsigned int, frame_length, mode->height);
1959 +}
1960 +
1961 +static void imx477_set_framing_limits(struct imx477 *imx477)
1962 +{
1963 + unsigned int frm_length_min, frm_length_default, hblank_min;
1964 + const struct imx477_mode *mode = imx477->mode;
1965 +
1966 + frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
1967 + frm_length_default =
1968 + imx477_get_frame_length(mode, &mode->timeperframe_default);
1969 +
1970 + /* Default to no long exposure multiplier. */
1971 + imx477->long_exp_shift = 0;
1972 +
1973 + /* Update limits and set FPS to default */
1974 + __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
1975 + ((1 << IMX477_LONG_EXP_SHIFT_MAX) *
1976 + IMX477_FRAME_LENGTH_MAX) - mode->height,
1977 + 1, frm_length_default - mode->height);
1978 +
1979 + /* Setting this will adjust the exposure limits as well. */
1980 + __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
1981 +
1982 + hblank_min = mode->line_length_pix - mode->width;
1983 + __v4l2_ctrl_modify_range(imx477->hblank, hblank_min,
1984 + IMX477_LINE_LENGTH_MAX, 1, hblank_min);
1985 + __v4l2_ctrl_s_ctrl(imx477->hblank, hblank_min);
1986 +}
1987 +
1988 +static int imx477_set_pad_format(struct v4l2_subdev *sd,
1989 + struct v4l2_subdev_pad_config *cfg,
1990 + struct v4l2_subdev_format *fmt)
1991 +{
1992 + struct v4l2_mbus_framefmt *framefmt;
1993 + const struct imx477_mode *mode;
1994 + struct imx477 *imx477 = to_imx477(sd);
1995 +
1996 + if (fmt->pad >= NUM_PADS)
1997 + return -EINVAL;
1998 +
1999 + mutex_lock(&imx477->mutex);
2000 +
2001 + if (fmt->pad == IMAGE_PAD) {
2002 + const struct imx477_mode *mode_list;
2003 + unsigned int num_modes;
2004 +
2005 + /* Bayer order varies with flips */
2006 + fmt->format.code = imx477_get_format_code(imx477,
2007 + fmt->format.code);
2008 +
2009 + get_mode_table(fmt->format.code, &mode_list, &num_modes);
2010 +
2011 + mode = v4l2_find_nearest_size(mode_list,
2012 + num_modes,
2013 + width, height,
2014 + fmt->format.width,
2015 + fmt->format.height);
2016 + imx477_update_image_pad_format(imx477, mode, fmt);
2017 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2018 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
2019 + fmt->pad);
2020 + *framefmt = fmt->format;
2021 + } else if (imx477->mode != mode) {
2022 + imx477->mode = mode;
2023 + imx477->fmt_code = fmt->format.code;
2024 + imx477_set_framing_limits(imx477);
2025 + }
2026 + } else {
2027 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
2028 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
2029 + fmt->pad);
2030 + *framefmt = fmt->format;
2031 + } else {
2032 + /* Only one embedded data mode is supported */
2033 + imx477_update_metadata_pad_format(fmt);
2034 + }
2035 + }
2036 +
2037 + mutex_unlock(&imx477->mutex);
2038 +
2039 + return 0;
2040 +}
2041 +
2042 +static const struct v4l2_rect *
2043 +__imx477_get_pad_crop(struct imx477 *imx477, struct v4l2_subdev_pad_config *cfg,
2044 + unsigned int pad, enum v4l2_subdev_format_whence which)
2045 +{
2046 + switch (which) {
2047 + case V4L2_SUBDEV_FORMAT_TRY:
2048 + return v4l2_subdev_get_try_crop(&imx477->sd, cfg, pad);
2049 + case V4L2_SUBDEV_FORMAT_ACTIVE:
2050 + return &imx477->mode->crop;
2051 + }
2052 +
2053 + return NULL;
2054 +}
2055 +
2056 +static int imx477_get_selection(struct v4l2_subdev *sd,
2057 + struct v4l2_subdev_pad_config *cfg,
2058 + struct v4l2_subdev_selection *sel)
2059 +{
2060 + switch (sel->target) {
2061 + case V4L2_SEL_TGT_CROP: {
2062 + struct imx477 *imx477 = to_imx477(sd);
2063 +
2064 + mutex_lock(&imx477->mutex);
2065 + sel->r = *__imx477_get_pad_crop(imx477, cfg, sel->pad,
2066 + sel->which);
2067 + mutex_unlock(&imx477->mutex);
2068 +
2069 + return 0;
2070 + }
2071 +
2072 + case V4L2_SEL_TGT_NATIVE_SIZE:
2073 + sel->r.left = 0;
2074 + sel->r.top = 0;
2075 + sel->r.width = IMX477_NATIVE_WIDTH;
2076 + sel->r.height = IMX477_NATIVE_HEIGHT;
2077 +
2078 + return 0;
2079 +
2080 + case V4L2_SEL_TGT_CROP_DEFAULT:
2081 + case V4L2_SEL_TGT_CROP_BOUNDS:
2082 + sel->r.left = IMX477_PIXEL_ARRAY_LEFT;
2083 + sel->r.top = IMX477_PIXEL_ARRAY_TOP;
2084 + sel->r.width = IMX477_PIXEL_ARRAY_WIDTH;
2085 + sel->r.height = IMX477_PIXEL_ARRAY_HEIGHT;
2086 +
2087 + return 0;
2088 + }
2089 +
2090 + return -EINVAL;
2091 +}
2092 +
2093 +/* Start streaming */
2094 +static int imx477_start_streaming(struct imx477 *imx477)
2095 +{
2096 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2097 + const struct imx477_reg_list *reg_list;
2098 + const struct imx477_reg_list *extra_regs;
2099 + int ret;
2100 +
2101 + if (!imx477->common_regs_written) {
2102 + ret = imx477_write_regs(imx477, mode_common_regs,
2103 + ARRAY_SIZE(mode_common_regs));
2104 + if (!ret) {
2105 + extra_regs = &imx477->compatible_data->extra_regs;
2106 + ret = imx477_write_regs(imx477, extra_regs->regs,
2107 + extra_regs->num_of_regs);
2108 + }
2109 +
2110 + if (ret) {
2111 + dev_err(&client->dev, "%s failed to set common settings\n",
2112 + __func__);
2113 + return ret;
2114 + }
2115 + imx477->common_regs_written = true;
2116 + }
2117 +
2118 + /* Apply default values of current mode */
2119 + reg_list = &imx477->mode->reg_list;
2120 + ret = imx477_write_regs(imx477, reg_list->regs, reg_list->num_of_regs);
2121 + if (ret) {
2122 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
2123 + return ret;
2124 + }
2125 +
2126 + /* Set on-sensor DPC. */
2127 + imx477_write_reg(imx477, 0x0b05, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2128 + imx477_write_reg(imx477, 0x0b06, IMX477_REG_VALUE_08BIT, !!dpc_enable);
2129 +
2130 + /* Set vsync trigger mode */
2131 + if (trigger_mode != 0) {
2132 + /* trigger_mode == 1 for source, 2 for sink */
2133 + const u32 val = (trigger_mode == 1) ? 1 : 0;
2134 +
2135 + imx477_write_reg(imx477, IMX477_REG_MC_MODE,
2136 + IMX477_REG_VALUE_08BIT, 1);
2137 + imx477_write_reg(imx477, IMX477_REG_MS_SEL,
2138 + IMX477_REG_VALUE_08BIT, val);
2139 + imx477_write_reg(imx477, IMX477_REG_XVS_IO_CTRL,
2140 + IMX477_REG_VALUE_08BIT, val);
2141 + imx477_write_reg(imx477, IMX477_REG_EXTOUT_EN,
2142 + IMX477_REG_VALUE_08BIT, val);
2143 + }
2144 +
2145 + /* Apply customized values from user */
2146 + ret = __v4l2_ctrl_handler_setup(imx477->sd.ctrl_handler);
2147 + if (ret)
2148 + return ret;
2149 +
2150 + /* set stream on register */
2151 + return imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2152 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STREAMING);
2153 +}
2154 +
2155 +/* Stop streaming */
2156 +static void imx477_stop_streaming(struct imx477 *imx477)
2157 +{
2158 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2159 + int ret;
2160 +
2161 + /* set stream off register */
2162 + ret = imx477_write_reg(imx477, IMX477_REG_MODE_SELECT,
2163 + IMX477_REG_VALUE_08BIT, IMX477_MODE_STANDBY);
2164 + if (ret)
2165 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
2166 +}
2167 +
2168 +static int imx477_set_stream(struct v4l2_subdev *sd, int enable)
2169 +{
2170 + struct imx477 *imx477 = to_imx477(sd);
2171 + struct i2c_client *client = v4l2_get_subdevdata(sd);
2172 + int ret = 0;
2173 +
2174 + mutex_lock(&imx477->mutex);
2175 + if (imx477->streaming == enable) {
2176 + mutex_unlock(&imx477->mutex);
2177 + return 0;
2178 + }
2179 +
2180 + if (enable) {
2181 + ret = pm_runtime_get_sync(&client->dev);
2182 + if (ret < 0) {
2183 + pm_runtime_put_noidle(&client->dev);
2184 + goto err_unlock;
2185 + }
2186 +
2187 + /*
2188 + * Apply default & customized values
2189 + * and then start streaming.
2190 + */
2191 + ret = imx477_start_streaming(imx477);
2192 + if (ret)
2193 + goto err_rpm_put;
2194 + } else {
2195 + imx477_stop_streaming(imx477);
2196 + pm_runtime_put(&client->dev);
2197 + }
2198 +
2199 + imx477->streaming = enable;
2200 +
2201 + /* vflip and hflip cannot change during streaming */
2202 + __v4l2_ctrl_grab(imx477->vflip, enable);
2203 + __v4l2_ctrl_grab(imx477->hflip, enable);
2204 +
2205 + mutex_unlock(&imx477->mutex);
2206 +
2207 + return ret;
2208 +
2209 +err_rpm_put:
2210 + pm_runtime_put(&client->dev);
2211 +err_unlock:
2212 + mutex_unlock(&imx477->mutex);
2213 +
2214 + return ret;
2215 +}
2216 +
2217 +/* Power/clock management functions */
2218 +static int imx477_power_on(struct device *dev)
2219 +{
2220 + struct i2c_client *client = to_i2c_client(dev);
2221 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2222 + struct imx477 *imx477 = to_imx477(sd);
2223 + int ret;
2224 +
2225 + ret = regulator_bulk_enable(IMX477_NUM_SUPPLIES,
2226 + imx477->supplies);
2227 + if (ret) {
2228 + dev_err(&client->dev, "%s: failed to enable regulators\n",
2229 + __func__);
2230 + return ret;
2231 + }
2232 +
2233 + ret = clk_prepare_enable(imx477->xclk);
2234 + if (ret) {
2235 + dev_err(&client->dev, "%s: failed to enable clock\n",
2236 + __func__);
2237 + goto reg_off;
2238 + }
2239 +
2240 + gpiod_set_value_cansleep(imx477->reset_gpio, 1);
2241 + usleep_range(IMX477_XCLR_MIN_DELAY_US,
2242 + IMX477_XCLR_MIN_DELAY_US + IMX477_XCLR_DELAY_RANGE_US);
2243 +
2244 + return 0;
2245 +
2246 +reg_off:
2247 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2248 + return ret;
2249 +}
2250 +
2251 +static int imx477_power_off(struct device *dev)
2252 +{
2253 + struct i2c_client *client = to_i2c_client(dev);
2254 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2255 + struct imx477 *imx477 = to_imx477(sd);
2256 +
2257 + gpiod_set_value_cansleep(imx477->reset_gpio, 0);
2258 + regulator_bulk_disable(IMX477_NUM_SUPPLIES, imx477->supplies);
2259 + clk_disable_unprepare(imx477->xclk);
2260 +
2261 + /* Force reprogramming of the common registers when powered up again. */
2262 + imx477->common_regs_written = false;
2263 +
2264 + return 0;
2265 +}
2266 +
2267 +static int __maybe_unused imx477_suspend(struct device *dev)
2268 +{
2269 + struct i2c_client *client = to_i2c_client(dev);
2270 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2271 + struct imx477 *imx477 = to_imx477(sd);
2272 +
2273 + if (imx477->streaming)
2274 + imx477_stop_streaming(imx477);
2275 +
2276 + return 0;
2277 +}
2278 +
2279 +static int __maybe_unused imx477_resume(struct device *dev)
2280 +{
2281 + struct i2c_client *client = to_i2c_client(dev);
2282 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2283 + struct imx477 *imx477 = to_imx477(sd);
2284 + int ret;
2285 +
2286 + if (imx477->streaming) {
2287 + ret = imx477_start_streaming(imx477);
2288 + if (ret)
2289 + goto error;
2290 + }
2291 +
2292 + return 0;
2293 +
2294 +error:
2295 + imx477_stop_streaming(imx477);
2296 + imx477->streaming = 0;
2297 + return ret;
2298 +}
2299 +
2300 +static int imx477_get_regulators(struct imx477 *imx477)
2301 +{
2302 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2303 + unsigned int i;
2304 +
2305 + for (i = 0; i < IMX477_NUM_SUPPLIES; i++)
2306 + imx477->supplies[i].supply = imx477_supply_name[i];
2307 +
2308 + return devm_regulator_bulk_get(&client->dev,
2309 + IMX477_NUM_SUPPLIES,
2310 + imx477->supplies);
2311 +}
2312 +
2313 +/* Verify chip ID */
2314 +static int imx477_identify_module(struct imx477 *imx477, u32 expected_id)
2315 +{
2316 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2317 + int ret;
2318 + u32 val;
2319 +
2320 + ret = imx477_read_reg(imx477, IMX477_REG_CHIP_ID,
2321 + IMX477_REG_VALUE_16BIT, &val);
2322 + if (ret) {
2323 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
2324 + expected_id, ret);
2325 + return ret;
2326 + }
2327 +
2328 + if (val != expected_id) {
2329 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
2330 + expected_id, val);
2331 + return -EIO;
2332 + }
2333 +
2334 + dev_info(&client->dev, "Device found is imx%x\n", val);
2335 +
2336 + return 0;
2337 +}
2338 +
2339 +static const struct v4l2_subdev_core_ops imx477_core_ops = {
2340 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2341 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2342 +};
2343 +
2344 +static const struct v4l2_subdev_video_ops imx477_video_ops = {
2345 + .s_stream = imx477_set_stream,
2346 +};
2347 +
2348 +static const struct v4l2_subdev_pad_ops imx477_pad_ops = {
2349 + .enum_mbus_code = imx477_enum_mbus_code,
2350 + .get_fmt = imx477_get_pad_format,
2351 + .set_fmt = imx477_set_pad_format,
2352 + .get_selection = imx477_get_selection,
2353 + .enum_frame_size = imx477_enum_frame_size,
2354 +};
2355 +
2356 +static const struct v4l2_subdev_ops imx477_subdev_ops = {
2357 + .core = &imx477_core_ops,
2358 + .video = &imx477_video_ops,
2359 + .pad = &imx477_pad_ops,
2360 +};
2361 +
2362 +static const struct v4l2_subdev_internal_ops imx477_internal_ops = {
2363 + .open = imx477_open,
2364 +};
2365 +
2366 +/* Initialize control handlers */
2367 +static int imx477_init_controls(struct imx477 *imx477)
2368 +{
2369 + struct v4l2_ctrl_handler *ctrl_hdlr;
2370 + struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
2371 + struct v4l2_fwnode_device_properties props;
2372 + unsigned int i;
2373 + int ret;
2374 +
2375 + ctrl_hdlr = &imx477->ctrl_handler;
2376 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2377 + if (ret)
2378 + return ret;
2379 +
2380 + mutex_init(&imx477->mutex);
2381 + ctrl_hdlr->lock = &imx477->mutex;
2382 +
2383 + /* By default, PIXEL_RATE is read only */
2384 + imx477->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2385 + V4L2_CID_PIXEL_RATE,
2386 + IMX477_PIXEL_RATE,
2387 + IMX477_PIXEL_RATE, 1,
2388 + IMX477_PIXEL_RATE);
2389 +
2390 + /*
2391 + * Create the controls here, but mode specific limits are setup
2392 + * in the imx477_set_framing_limits() call below.
2393 + */
2394 + imx477->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2395 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2396 + imx477->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2397 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2398 +
2399 + imx477->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2400 + V4L2_CID_EXPOSURE,
2401 + IMX477_EXPOSURE_MIN,
2402 + IMX477_EXPOSURE_MAX,
2403 + IMX477_EXPOSURE_STEP,
2404 + IMX477_EXPOSURE_DEFAULT);
2405 +
2406 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2407 + IMX477_ANA_GAIN_MIN, IMX477_ANA_GAIN_MAX,
2408 + IMX477_ANA_GAIN_STEP, IMX477_ANA_GAIN_DEFAULT);
2409 +
2410 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2411 + IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
2412 + IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
2413 +
2414 + imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2415 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2416 + if (imx477->hflip)
2417 + imx477->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2418 +
2419 + imx477->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2420 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2421 + if (imx477->vflip)
2422 + imx477->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2423 +
2424 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx477_ctrl_ops,
2425 + V4L2_CID_TEST_PATTERN,
2426 + ARRAY_SIZE(imx477_test_pattern_menu) - 1,
2427 + 0, 0, imx477_test_pattern_menu);
2428 + for (i = 0; i < 4; i++) {
2429 + /*
2430 + * The assumption is that
2431 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2432 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2433 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2434 + */
2435 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
2436 + V4L2_CID_TEST_PATTERN_RED + i,
2437 + IMX477_TEST_PATTERN_COLOUR_MIN,
2438 + IMX477_TEST_PATTERN_COLOUR_MAX,
2439 + IMX477_TEST_PATTERN_COLOUR_STEP,
2440 + IMX477_TEST_PATTERN_COLOUR_MAX);
2441 + /* The "Solid color" pattern is white by default */
2442 + }
2443 +
2444 + if (ctrl_hdlr->error) {
2445 + ret = ctrl_hdlr->error;
2446 + dev_err(&client->dev, "%s control init failed (%d)\n",
2447 + __func__, ret);
2448 + goto error;
2449 + }
2450 +
2451 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2452 + if (ret)
2453 + goto error;
2454 +
2455 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx477_ctrl_ops,
2456 + &props);
2457 + if (ret)
2458 + goto error;
2459 +
2460 + imx477->sd.ctrl_handler = ctrl_hdlr;
2461 +
2462 + /* Setup exposure and frame/line length limits. */
2463 + imx477_set_framing_limits(imx477);
2464 +
2465 + return 0;
2466 +
2467 +error:
2468 + v4l2_ctrl_handler_free(ctrl_hdlr);
2469 + mutex_destroy(&imx477->mutex);
2470 +
2471 + return ret;
2472 +}
2473 +
2474 +static void imx477_free_controls(struct imx477 *imx477)
2475 +{
2476 + v4l2_ctrl_handler_free(imx477->sd.ctrl_handler);
2477 + mutex_destroy(&imx477->mutex);
2478 +}
2479 +
2480 +static int imx477_check_hwcfg(struct device *dev)
2481 +{
2482 + struct fwnode_handle *endpoint;
2483 + struct v4l2_fwnode_endpoint ep_cfg = {
2484 + .bus_type = V4L2_MBUS_CSI2_DPHY
2485 + };
2486 + int ret = -EINVAL;
2487 +
2488 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2489 + if (!endpoint) {
2490 + dev_err(dev, "endpoint node not found\n");
2491 + return -EINVAL;
2492 + }
2493 +
2494 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2495 + dev_err(dev, "could not parse endpoint\n");
2496 + goto error_out;
2497 + }
2498 +
2499 + /* Check the number of MIPI CSI2 data lanes */
2500 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2501 + dev_err(dev, "only 2 data lanes are currently supported\n");
2502 + goto error_out;
2503 + }
2504 +
2505 + /* Check the link frequency set in device tree */
2506 + if (!ep_cfg.nr_of_link_frequencies) {
2507 + dev_err(dev, "link-frequency property not found in DT\n");
2508 + goto error_out;
2509 + }
2510 +
2511 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2512 + ep_cfg.link_frequencies[0] != IMX477_DEFAULT_LINK_FREQ) {
2513 + dev_err(dev, "Link frequency not supported: %lld\n",
2514 + ep_cfg.link_frequencies[0]);
2515 + goto error_out;
2516 + }
2517 +
2518 + ret = 0;
2519 +
2520 +error_out:
2521 + v4l2_fwnode_endpoint_free(&ep_cfg);
2522 + fwnode_handle_put(endpoint);
2523 +
2524 + return ret;
2525 +}
2526 +
2527 +static const struct imx477_compatible_data imx477_compatible = {
2528 + .chip_id = IMX477_CHIP_ID,
2529 + .extra_regs = {
2530 + .num_of_regs = 0,
2531 + .regs = NULL
2532 + }
2533 +};
2534 +
2535 +static const struct imx477_reg imx378_regs[] = {
2536 + {0x3e35, 0x01},
2537 + {0x4421, 0x08},
2538 + {0x3ff9, 0x00},
2539 +};
2540 +
2541 +static const struct imx477_compatible_data imx378_compatible = {
2542 + .chip_id = IMX378_CHIP_ID,
2543 + .extra_regs = {
2544 + .num_of_regs = ARRAY_SIZE(imx378_regs),
2545 + .regs = imx378_regs
2546 + }
2547 +};
2548 +
2549 +static const struct of_device_id imx477_dt_ids[] = {
2550 + { .compatible = "sony,imx477", .data = &imx477_compatible },
2551 + { .compatible = "sony,imx378", .data = &imx378_compatible },
2552 + { /* sentinel */ }
2553 +};
2554 +
2555 +static int imx477_probe(struct i2c_client *client)
2556 +{
2557 + struct device *dev = &client->dev;
2558 + struct imx477 *imx477;
2559 + const struct of_device_id *match;
2560 + int ret;
2561 +
2562 + imx477 = devm_kzalloc(&client->dev, sizeof(*imx477), GFP_KERNEL);
2563 + if (!imx477)
2564 + return -ENOMEM;
2565 +
2566 + v4l2_i2c_subdev_init(&imx477->sd, client, &imx477_subdev_ops);
2567 +
2568 + match = of_match_device(imx477_dt_ids, dev);
2569 + if (!match)
2570 + return -ENODEV;
2571 + imx477->compatible_data =
2572 + (const struct imx477_compatible_data *)match->data;
2573 +
2574 + /* Check the hardware configuration in device tree */
2575 + if (imx477_check_hwcfg(dev))
2576 + return -EINVAL;
2577 +
2578 + /* Get system clock (xclk) */
2579 + imx477->xclk = devm_clk_get(dev, NULL);
2580 + if (IS_ERR(imx477->xclk)) {
2581 + dev_err(dev, "failed to get xclk\n");
2582 + return PTR_ERR(imx477->xclk);
2583 + }
2584 +
2585 + imx477->xclk_freq = clk_get_rate(imx477->xclk);
2586 + if (imx477->xclk_freq != IMX477_XCLK_FREQ) {
2587 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2588 + imx477->xclk_freq);
2589 + return -EINVAL;
2590 + }
2591 +
2592 + ret = imx477_get_regulators(imx477);
2593 + if (ret) {
2594 + dev_err(dev, "failed to get regulators\n");
2595 + return ret;
2596 + }
2597 +
2598 + /* Request optional enable pin */
2599 + imx477->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2600 + GPIOD_OUT_HIGH);
2601 +
2602 + /*
2603 + * The sensor must be powered for imx477_identify_module()
2604 + * to be able to read the CHIP_ID register
2605 + */
2606 + ret = imx477_power_on(dev);
2607 + if (ret)
2608 + return ret;
2609 +
2610 + ret = imx477_identify_module(imx477, imx477->compatible_data->chip_id);
2611 + if (ret)
2612 + goto error_power_off;
2613 +
2614 + /* Initialize default format */
2615 + imx477_set_default_format(imx477);
2616 +
2617 + /* Enable runtime PM and turn off the device */
2618 + pm_runtime_set_active(dev);
2619 + pm_runtime_enable(dev);
2620 + pm_runtime_idle(dev);
2621 +
2622 + /* This needs the pm runtime to be registered. */
2623 + ret = imx477_init_controls(imx477);
2624 + if (ret)
2625 + goto error_power_off;
2626 +
2627 + /* Initialize subdev */
2628 + imx477->sd.internal_ops = &imx477_internal_ops;
2629 + imx477->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2630 + V4L2_SUBDEV_FL_HAS_EVENTS;
2631 + imx477->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2632 +
2633 + /* Initialize source pads */
2634 + imx477->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2635 + imx477->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2636 +
2637 + ret = media_entity_pads_init(&imx477->sd.entity, NUM_PADS, imx477->pad);
2638 + if (ret) {
2639 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2640 + goto error_handler_free;
2641 + }
2642 +
2643 + ret = v4l2_async_register_subdev_sensor(&imx477->sd);
2644 + if (ret < 0) {
2645 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2646 + goto error_media_entity;
2647 + }
2648 +
2649 + return 0;
2650 +
2651 +error_media_entity:
2652 + media_entity_cleanup(&imx477->sd.entity);
2653 +
2654 +error_handler_free:
2655 + imx477_free_controls(imx477);
2656 +
2657 +error_power_off:
2658 + pm_runtime_disable(&client->dev);
2659 + pm_runtime_set_suspended(&client->dev);
2660 + imx477_power_off(&client->dev);
2661 +
2662 + return ret;
2663 +}
2664 +
2665 +static void imx477_remove(struct i2c_client *client)
2666 +{
2667 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2668 + struct imx477 *imx477 = to_imx477(sd);
2669 +
2670 + v4l2_async_unregister_subdev(sd);
2671 + media_entity_cleanup(&sd->entity);
2672 + imx477_free_controls(imx477);
2673 +
2674 + pm_runtime_disable(&client->dev);
2675 + if (!pm_runtime_status_suspended(&client->dev))
2676 + imx477_power_off(&client->dev);
2677 + pm_runtime_set_suspended(&client->dev);
2678 +}
2679 +
2680 +MODULE_DEVICE_TABLE(of, imx477_dt_ids);
2681 +
2682 +static const struct dev_pm_ops imx477_pm_ops = {
2683 + SET_SYSTEM_SLEEP_PM_OPS(imx477_suspend, imx477_resume)
2684 + SET_RUNTIME_PM_OPS(imx477_power_off, imx477_power_on, NULL)
2685 +};
2686 +
2687 +static struct i2c_driver imx477_i2c_driver = {
2688 + .driver = {
2689 + .name = "imx477",
2690 + .of_match_table = imx477_dt_ids,
2691 + .pm = &imx477_pm_ops,
2692 + },
2693 + .probe_new = imx477_probe,
2694 + .remove = imx477_remove,
2695 +};
2696 +
2697 +module_i2c_driver(imx477_i2c_driver);
2698 +
2699 +MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
2700 +MODULE_DESCRIPTION("Sony IMX477 sensor driver");
2701 +MODULE_LICENSE("GPL v2");