bae8b88490f39fa6010a4ea87000567bb3f9ea4a
[openwrt/staging/stintel.git] / target / linux / bcm27xx / patches-6.1 / 950-0196-media-i2c-imx519-Support-for-the-Sony-IMX519-sensor.patch
1 From 9b5a7e22d42b137d7ced182ded00337c6f819795 Mon Sep 17 00:00:00 2001
2 From: Lee Jackson <info@arducam.com>
3 Date: Fri, 27 Aug 2021 14:36:55 +0800
4 Subject: [PATCH] media: i2c: imx519: Support for the Sony IMX519
5 sensor
6
7 dt-bindings: media: i2c: Add IMX519 CMOS sensor binding
8
9 Add YAML device tree binding for IMX519 CMOS image sensor, and
10 the relevant MAINTAINERS entries.
11
12 Signed-off-by: Lee Jackson <info@arducam.com>
13
14 media: i2c: Add driver for IMX519 sensor
15
16 Adds a driver for the 16MPix IMX519 CSI2 sensor.
17 Whilst the sensor supports 2 or 4 CSI2 data lanes, this driver
18 currently only supports 2 lanes.
19
20 The following Bayer modes are currently available:
21
22 4656x3496 10-bit @ 10fps
23 3840x2160 10-bit (cropped) @ 21fps
24 2328x1748 10-bit (binned) @ 30fps
25 1920x1080 10-bit (cropped/binned) @ 60fps
26 1280x720 10-bit (cropped/binned) @ 120fps
27
28 Signed-off-by: Lee Jackson <info@arducam.com>
29
30 media: i2c: imx519: Advertise embedded data node on media pad 1
31
32 This commit updates the imx519 driver to adverise support for embedded
33 data streams.
34
35 The imx519 sensor subdevice overloads the media pad to differentiate
36 between image stream (pad 0) and embedded data stream (pad 1) when
37 performing the v4l2_subdev_pad_ops functions.
38
39 Signed-off-by: Lee Jackson <info@arducam.com>
40
41 media: i2c: imx519: Sensor should report RAW color space
42
43 Tested on Raspberry Pi running libcamera.
44
45 Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
46
47 media: i2c: Update imx519 Kconfig entry
48
49 Bring the IMX519 Kconfig declaration in line with the upstream entries.
50
51 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
52 ---
53 .../devicetree/bindings/media/i2c/imx519.yaml | 113 +
54 MAINTAINERS | 8 +
55 drivers/media/i2c/Kconfig | 11 +
56 drivers/media/i2c/Makefile | 1 +
57 drivers/media/i2c/imx519.c | 2090 +++++++++++++++++
58 5 files changed, 2223 insertions(+)
59 create mode 100644 Documentation/devicetree/bindings/media/i2c/imx519.yaml
60 create mode 100644 drivers/media/i2c/imx519.c
61
62 --- /dev/null
63 +++ b/Documentation/devicetree/bindings/media/i2c/imx519.yaml
64 @@ -0,0 +1,113 @@
65 +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
66 +%YAML 1.2
67 +---
68 +$id: http://devicetree.org/schemas/media/i2c/imx519.yaml#
69 +$schema: http://devicetree.org/meta-schemas/core.yaml#
70 +
71 +title: Sony 1/2.5-Inch 16Mpixel CMOS Digital Image Sensor
72 +
73 +maintainers:
74 + - Lee Jackson <info@arducam.com>
75 +
76 +description: |-
77 + The Sony IMX519 is a 1/2.5-inch CMOS active pixel digital image sensor
78 + with an active array size of 4656H x 3496V. It is programmable through
79 + I2C interface. The I2C address is fixed to 0x1A as per sensor data sheet.
80 + Image data is sent through MIPI CSI-2, which is configured as either 2 or
81 + 4 data lanes.
82 +
83 +properties:
84 + compatible:
85 + const: sony,imx519
86 +
87 + reg:
88 + description: I2C device address
89 + maxItems: 1
90 +
91 + clocks:
92 + maxItems: 1
93 +
94 + VDIG-supply:
95 + description:
96 + Digital I/O voltage supply, 1.05 volts
97 +
98 + VANA-supply:
99 + description:
100 + Analog voltage supply, 2.8 volts
101 +
102 + VDDL-supply:
103 + description:
104 + Digital core voltage supply, 1.8 volts
105 +
106 + reset-gpios:
107 + description: |-
108 + Reference to the GPIO connected to the xclr pin, if any.
109 + Must be released (set high) after all supplies and INCK are applied.
110 +
111 + # See ../video-interfaces.txt for more details
112 + port:
113 + type: object
114 + properties:
115 + endpoint:
116 + type: object
117 + properties:
118 + data-lanes:
119 + description: |-
120 + The sensor supports either two-lane, or four-lane operation.
121 + For two-lane operation the property must be set to <1 2>.
122 + items:
123 + - const: 1
124 + - const: 2
125 +
126 + clock-noncontinuous:
127 + type: boolean
128 + description: |-
129 + MIPI CSI-2 clock is non-continuous if this property is present,
130 + otherwise it's continuous.
131 +
132 + link-frequencies:
133 + allOf:
134 + - $ref: /schemas/types.yaml#/definitions/uint64-array
135 + description:
136 + Allowed data bus frequencies.
137 +
138 + required:
139 + - link-frequencies
140 +
141 +required:
142 + - compatible
143 + - reg
144 + - clocks
145 + - VANA-supply
146 + - VDIG-supply
147 + - VDDL-supply
148 + - port
149 +
150 +additionalProperties: false
151 +
152 +examples:
153 + - |
154 + i2c0 {
155 + #address-cells = <1>;
156 + #size-cells = <0>;
157 +
158 + imx519: sensor@1a {
159 + compatible = "sony,imx519";
160 + reg = <0x1a>;
161 + clocks = <&imx519_clk>;
162 + VANA-supply = <&imx519_vana>; /* 2.8v */
163 + VDIG-supply = <&imx519_vdig>; /* 1.05v */
164 + VDDL-supply = <&imx519_vddl>; /* 1.8v */
165 +
166 + port {
167 + imx519_0: endpoint {
168 + remote-endpoint = <&csi1_ep>;
169 + data-lanes = <1 2>;
170 + clock-noncontinuous;
171 + link-frequencies = /bits/ 64 <493500000>;
172 + };
173 + };
174 + };
175 + };
176 +
177 +...
178 --- a/MAINTAINERS
179 +++ b/MAINTAINERS
180 @@ -19291,6 +19291,14 @@ T: git git://linuxtv.org/media_tree.git
181 F: Documentation/devicetree/bindings/media/i2c/imx477.yaml
182 F: drivers/media/i2c/imx477.c
183
184 +SONY IMX519 SENSOR DRIVER
185 +M: Arducam Kernel Maintenance <info@arducam.com>
186 +L: linux-media@vger.kernel.org
187 +S: Maintained
188 +T: git git://linuxtv.org/media_tree.git
189 +F: Documentation/devicetree/bindings/media/i2c/imx519.yaml
190 +F: drivers/media/i2c/imx519.c
191 +
192 SONY MEMORYSTICK SUBSYSTEM
193 M: Maxim Levitsky <maximlevitsky@gmail.com>
194 M: Alex Dubov <oakad@yahoo.com>
195 --- a/drivers/media/i2c/Kconfig
196 +++ b/drivers/media/i2c/Kconfig
197 @@ -235,6 +235,17 @@ config VIDEO_IMX412
198 To compile this driver as a module, choose M here: the
199 module will be called imx412.
200
201 +config VIDEO_IMX519
202 + tristate "Arducam IMX519 sensor support"
203 + depends on I2C && VIDEO_DEV
204 + select VIDEO_V4L2_SUBDEV_API
205 + help
206 + This is a Video4Linux2 sensor driver for the Arducam
207 + IMX519 camera.
208 +
209 + To compile this driver as a module, choose M here: the
210 + module will be called IMX519.
211 +
212 config VIDEO_MAX9271_LIB
213 tristate
214
215 --- a/drivers/media/i2c/Makefile
216 +++ b/drivers/media/i2c/Makefile
217 @@ -49,6 +49,7 @@ obj-$(CONFIG_VIDEO_IMX335) += imx335.o
218 obj-$(CONFIG_VIDEO_IMX355) += imx355.o
219 obj-$(CONFIG_VIDEO_IMX412) += imx412.o
220 obj-$(CONFIG_VIDEO_IMX477) += imx477.o
221 +obj-$(CONFIG_VIDEO_IMX519) += imx519.o
222 obj-$(CONFIG_VIDEO_IR_I2C) += ir-kbd-i2c.o
223 obj-$(CONFIG_VIDEO_ISL7998X) += isl7998x.o
224 obj-$(CONFIG_VIDEO_KS0127) += ks0127.o
225 --- /dev/null
226 +++ b/drivers/media/i2c/imx519.c
227 @@ -0,0 +1,2090 @@
228 +// SPDX-License-Identifier: GPL-2.0
229 +/*
230 + * A V4L2 driver for Sony IMX519 cameras.
231 + * Copyright (C) 2021 Arducam Technology co., Ltd.
232 + *
233 + * Based on Sony IMX477 camera driver
234 + * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
235 + */
236 +#include <asm/unaligned.h>
237 +#include <linux/clk.h>
238 +#include <linux/delay.h>
239 +#include <linux/gpio/consumer.h>
240 +#include <linux/i2c.h>
241 +#include <linux/module.h>
242 +#include <linux/of_device.h>
243 +#include <linux/pm_runtime.h>
244 +#include <linux/regulator/consumer.h>
245 +#include <media/v4l2-ctrls.h>
246 +#include <media/v4l2-device.h>
247 +#include <media/v4l2-event.h>
248 +#include <media/v4l2-fwnode.h>
249 +#include <media/v4l2-mediabus.h>
250 +
251 +#define IMX519_REG_VALUE_08BIT 1
252 +#define IMX519_REG_VALUE_16BIT 2
253 +
254 +/* Chip ID */
255 +#define IMX519_REG_CHIP_ID 0x0016
256 +#define IMX519_CHIP_ID 0x0519
257 +
258 +#define IMX519_REG_MODE_SELECT 0x0100
259 +#define IMX519_MODE_STANDBY 0x00
260 +#define IMX519_MODE_STREAMING 0x01
261 +
262 +#define IMX519_REG_ORIENTATION 0x101
263 +
264 +#define IMX519_XCLK_FREQ 24000000
265 +
266 +#define IMX519_DEFAULT_LINK_FREQ 493500000
267 +
268 +/* Pixel rate is fixed at 686MHz for all the modes */
269 +#define IMX519_PIXEL_RATE 686000000
270 +
271 +/* V_TIMING internal */
272 +#define IMX519_REG_FRAME_LENGTH 0x0340
273 +#define IMX519_FRAME_LENGTH_MAX 0xffdc
274 +
275 +/* Long exposure multiplier */
276 +#define IMX519_LONG_EXP_SHIFT_MAX 7
277 +#define IMX519_LONG_EXP_SHIFT_REG 0x3100
278 +
279 +/* Exposure control */
280 +#define IMX519_REG_EXPOSURE 0x0202
281 +#define IMX519_EXPOSURE_OFFSET 32
282 +#define IMX519_EXPOSURE_MIN 1
283 +#define IMX519_EXPOSURE_STEP 1
284 +#define IMX519_EXPOSURE_DEFAULT 0x3e8
285 +#define IMX519_EXPOSURE_MAX (IMX519_FRAME_LENGTH_MAX - \
286 + IMX519_EXPOSURE_OFFSET)
287 +
288 +/* Analog gain control */
289 +#define IMX519_REG_ANALOG_GAIN 0x0204
290 +#define IMX519_ANA_GAIN_MIN 0
291 +#define IMX519_ANA_GAIN_MAX 960
292 +#define IMX519_ANA_GAIN_STEP 1
293 +#define IMX519_ANA_GAIN_DEFAULT 0x0
294 +
295 +/* Digital gain control */
296 +#define IMX519_REG_DIGITAL_GAIN 0x020e
297 +#define IMX519_DGTL_GAIN_MIN 0x0100
298 +#define IMX519_DGTL_GAIN_MAX 0xffff
299 +#define IMX519_DGTL_GAIN_DEFAULT 0x0100
300 +#define IMX519_DGTL_GAIN_STEP 1
301 +
302 +/* Test Pattern Control */
303 +#define IMX519_REG_TEST_PATTERN 0x0600
304 +#define IMX519_TEST_PATTERN_DISABLE 0
305 +#define IMX519_TEST_PATTERN_SOLID_COLOR 1
306 +#define IMX519_TEST_PATTERN_COLOR_BARS 2
307 +#define IMX519_TEST_PATTERN_GREY_COLOR 3
308 +#define IMX519_TEST_PATTERN_PN9 4
309 +
310 +/* Test pattern colour components */
311 +#define IMX519_REG_TEST_PATTERN_R 0x0602
312 +#define IMX519_REG_TEST_PATTERN_GR 0x0604
313 +#define IMX519_REG_TEST_PATTERN_B 0x0606
314 +#define IMX519_REG_TEST_PATTERN_GB 0x0608
315 +#define IMX519_TEST_PATTERN_COLOUR_MIN 0
316 +#define IMX519_TEST_PATTERN_COLOUR_MAX 0x0fff
317 +#define IMX519_TEST_PATTERN_COLOUR_STEP 1
318 +#define IMX519_TEST_PATTERN_R_DEFAULT IMX519_TEST_PATTERN_COLOUR_MAX
319 +#define IMX519_TEST_PATTERN_GR_DEFAULT 0
320 +#define IMX519_TEST_PATTERN_B_DEFAULT 0
321 +#define IMX519_TEST_PATTERN_GB_DEFAULT 0
322 +
323 +/* Embedded metadata stream structure */
324 +#define IMX519_EMBEDDED_LINE_WIDTH 16384
325 +#define IMX519_NUM_EMBEDDED_LINES 1
326 +
327 +enum pad_types {
328 + IMAGE_PAD,
329 + METADATA_PAD,
330 + NUM_PADS
331 +};
332 +
333 +/* IMX519 native and active pixel array size. */
334 +#define IMX519_NATIVE_WIDTH 4672U
335 +#define IMX519_NATIVE_HEIGHT 3648U
336 +#define IMX519_PIXEL_ARRAY_LEFT 8U
337 +#define IMX519_PIXEL_ARRAY_TOP 48U
338 +#define IMX519_PIXEL_ARRAY_WIDTH 4656U
339 +#define IMX519_PIXEL_ARRAY_HEIGHT 3496U
340 +
341 +struct imx519_reg {
342 + u16 address;
343 + u8 val;
344 +};
345 +
346 +struct imx519_reg_list {
347 + unsigned int num_of_regs;
348 + const struct imx519_reg *regs;
349 +};
350 +
351 +/* Mode : resolution and related config&values */
352 +struct imx519_mode {
353 + /* Frame width */
354 + unsigned int width;
355 +
356 + /* Frame height */
357 + unsigned int height;
358 +
359 + /* H-timing in pixels */
360 + unsigned int line_length_pix;
361 +
362 + /* Analog crop rectangle. */
363 + struct v4l2_rect crop;
364 +
365 + /* Highest possible framerate. */
366 + struct v4l2_fract timeperframe_min;
367 +
368 + /* Default framerate. */
369 + struct v4l2_fract timeperframe_default;
370 +
371 + /* Default register values */
372 + struct imx519_reg_list reg_list;
373 +};
374 +
375 +static const struct imx519_reg mode_common_regs[] = {
376 + {0x0136, 0x18},
377 + {0x0137, 0x00},
378 + {0x3c7e, 0x01},
379 + {0x3c7f, 0x07},
380 + {0x3020, 0x00},
381 + {0x3e35, 0x01},
382 + {0x3f7f, 0x01},
383 + {0x5609, 0x57},
384 + {0x5613, 0x51},
385 + {0x561f, 0x5e},
386 + {0x5623, 0xd2},
387 + {0x5637, 0x11},
388 + {0x5657, 0x11},
389 + {0x5659, 0x12},
390 + {0x5733, 0x60},
391 + {0x5905, 0x57},
392 + {0x590f, 0x51},
393 + {0x591b, 0x5e},
394 + {0x591f, 0xd2},
395 + {0x5933, 0x11},
396 + {0x5953, 0x11},
397 + {0x5955, 0x12},
398 + {0x5a2f, 0x60},
399 + {0x5a85, 0x57},
400 + {0x5a8f, 0x51},
401 + {0x5a9b, 0x5e},
402 + {0x5a9f, 0xd2},
403 + {0x5ab3, 0x11},
404 + {0x5ad3, 0x11},
405 + {0x5ad5, 0x12},
406 + {0x5baf, 0x60},
407 + {0x5c15, 0x2a},
408 + {0x5c17, 0x80},
409 + {0x5c19, 0x31},
410 + {0x5c1b, 0x87},
411 + {0x5c25, 0x25},
412 + {0x5c27, 0x7b},
413 + {0x5c29, 0x2a},
414 + {0x5c2b, 0x80},
415 + {0x5c2d, 0x31},
416 + {0x5c2f, 0x87},
417 + {0x5c35, 0x2b},
418 + {0x5c37, 0x81},
419 + {0x5c39, 0x31},
420 + {0x5c3b, 0x87},
421 + {0x5c45, 0x25},
422 + {0x5c47, 0x7b},
423 + {0x5c49, 0x2a},
424 + {0x5c4b, 0x80},
425 + {0x5c4d, 0x31},
426 + {0x5c4f, 0x87},
427 + {0x5c55, 0x2d},
428 + {0x5c57, 0x83},
429 + {0x5c59, 0x32},
430 + {0x5c5b, 0x88},
431 + {0x5c65, 0x29},
432 + {0x5c67, 0x7f},
433 + {0x5c69, 0x2e},
434 + {0x5c6b, 0x84},
435 + {0x5c6d, 0x32},
436 + {0x5c6f, 0x88},
437 + {0x5e69, 0x04},
438 + {0x5e9d, 0x00},
439 + {0x5f18, 0x10},
440 + {0x5f1a, 0x0e},
441 + {0x5f20, 0x12},
442 + {0x5f22, 0x10},
443 + {0x5f24, 0x0e},
444 + {0x5f28, 0x10},
445 + {0x5f2a, 0x0e},
446 + {0x5f30, 0x12},
447 + {0x5f32, 0x10},
448 + {0x5f34, 0x0e},
449 + {0x5f38, 0x0f},
450 + {0x5f39, 0x0d},
451 + {0x5f3c, 0x11},
452 + {0x5f3d, 0x0f},
453 + {0x5f3e, 0x0d},
454 + {0x5f61, 0x07},
455 + {0x5f64, 0x05},
456 + {0x5f67, 0x03},
457 + {0x5f6a, 0x03},
458 + {0x5f6d, 0x07},
459 + {0x5f70, 0x07},
460 + {0x5f73, 0x05},
461 + {0x5f76, 0x02},
462 + {0x5f79, 0x07},
463 + {0x5f7c, 0x07},
464 + {0x5f7f, 0x07},
465 + {0x5f82, 0x07},
466 + {0x5f85, 0x03},
467 + {0x5f88, 0x02},
468 + {0x5f8b, 0x01},
469 + {0x5f8e, 0x01},
470 + {0x5f91, 0x04},
471 + {0x5f94, 0x05},
472 + {0x5f97, 0x02},
473 + {0x5f9d, 0x07},
474 + {0x5fa0, 0x07},
475 + {0x5fa3, 0x07},
476 + {0x5fa6, 0x07},
477 + {0x5fa9, 0x03},
478 + {0x5fac, 0x01},
479 + {0x5faf, 0x01},
480 + {0x5fb5, 0x03},
481 + {0x5fb8, 0x02},
482 + {0x5fbb, 0x01},
483 + {0x5fc1, 0x07},
484 + {0x5fc4, 0x07},
485 + {0x5fc7, 0x07},
486 + {0x5fd1, 0x00},
487 + {0x6302, 0x79},
488 + {0x6305, 0x78},
489 + {0x6306, 0xa5},
490 + {0x6308, 0x03},
491 + {0x6309, 0x20},
492 + {0x630b, 0x0a},
493 + {0x630d, 0x48},
494 + {0x630f, 0x06},
495 + {0x6311, 0xa4},
496 + {0x6313, 0x03},
497 + {0x6314, 0x20},
498 + {0x6316, 0x0a},
499 + {0x6317, 0x31},
500 + {0x6318, 0x4a},
501 + {0x631a, 0x06},
502 + {0x631b, 0x40},
503 + {0x631c, 0xa4},
504 + {0x631e, 0x03},
505 + {0x631f, 0x20},
506 + {0x6321, 0x0a},
507 + {0x6323, 0x4a},
508 + {0x6328, 0x80},
509 + {0x6329, 0x01},
510 + {0x632a, 0x30},
511 + {0x632b, 0x02},
512 + {0x632c, 0x20},
513 + {0x632d, 0x02},
514 + {0x632e, 0x30},
515 + {0x6330, 0x60},
516 + {0x6332, 0x90},
517 + {0x6333, 0x01},
518 + {0x6334, 0x30},
519 + {0x6335, 0x02},
520 + {0x6336, 0x20},
521 + {0x6338, 0x80},
522 + {0x633a, 0xa0},
523 + {0x633b, 0x01},
524 + {0x633c, 0x60},
525 + {0x633d, 0x02},
526 + {0x633e, 0x60},
527 + {0x633f, 0x01},
528 + {0x6340, 0x30},
529 + {0x6341, 0x02},
530 + {0x6342, 0x20},
531 + {0x6343, 0x03},
532 + {0x6344, 0x80},
533 + {0x6345, 0x03},
534 + {0x6346, 0x90},
535 + {0x6348, 0xf0},
536 + {0x6349, 0x01},
537 + {0x634a, 0x20},
538 + {0x634b, 0x02},
539 + {0x634c, 0x10},
540 + {0x634d, 0x03},
541 + {0x634e, 0x60},
542 + {0x6350, 0xa0},
543 + {0x6351, 0x01},
544 + {0x6352, 0x60},
545 + {0x6353, 0x02},
546 + {0x6354, 0x50},
547 + {0x6355, 0x02},
548 + {0x6356, 0x60},
549 + {0x6357, 0x01},
550 + {0x6358, 0x30},
551 + {0x6359, 0x02},
552 + {0x635a, 0x30},
553 + {0x635b, 0x03},
554 + {0x635c, 0x90},
555 + {0x635f, 0x01},
556 + {0x6360, 0x10},
557 + {0x6361, 0x01},
558 + {0x6362, 0x40},
559 + {0x6363, 0x02},
560 + {0x6364, 0x50},
561 + {0x6368, 0x70},
562 + {0x636a, 0xa0},
563 + {0x636b, 0x01},
564 + {0x636c, 0x50},
565 + {0x637d, 0xe4},
566 + {0x637e, 0xb4},
567 + {0x638c, 0x8e},
568 + {0x638d, 0x38},
569 + {0x638e, 0xe3},
570 + {0x638f, 0x4c},
571 + {0x6390, 0x30},
572 + {0x6391, 0xc3},
573 + {0x6392, 0xae},
574 + {0x6393, 0xba},
575 + {0x6394, 0xeb},
576 + {0x6395, 0x6e},
577 + {0x6396, 0x34},
578 + {0x6397, 0xe3},
579 + {0x6398, 0xcf},
580 + {0x6399, 0x3c},
581 + {0x639a, 0xf3},
582 + {0x639b, 0x0c},
583 + {0x639c, 0x30},
584 + {0x639d, 0xc1},
585 + {0x63b9, 0xa3},
586 + {0x63ba, 0xfe},
587 + {0x7600, 0x01},
588 + {0x79a0, 0x01},
589 + {0x79a1, 0x01},
590 + {0x79a2, 0x01},
591 + {0x79a3, 0x01},
592 + {0x79a4, 0x01},
593 + {0x79a5, 0x20},
594 + {0x79a9, 0x00},
595 + {0x79aa, 0x01},
596 + {0x79ad, 0x00},
597 + {0x79af, 0x00},
598 + {0x8173, 0x01},
599 + {0x835c, 0x01},
600 + {0x8a74, 0x01},
601 + {0x8c1f, 0x00},
602 + {0x8c27, 0x00},
603 + {0x8c3b, 0x03},
604 + {0x9004, 0x0b},
605 + {0x920c, 0x6a},
606 + {0x920d, 0x22},
607 + {0x920e, 0x6a},
608 + {0x920f, 0x23},
609 + {0x9214, 0x6a},
610 + {0x9215, 0x20},
611 + {0x9216, 0x6a},
612 + {0x9217, 0x21},
613 + {0x9385, 0x3e},
614 + {0x9387, 0x1b},
615 + {0x938d, 0x4d},
616 + {0x938f, 0x43},
617 + {0x9391, 0x1b},
618 + {0x9395, 0x4d},
619 + {0x9397, 0x43},
620 + {0x9399, 0x1b},
621 + {0x939d, 0x3e},
622 + {0x939f, 0x2f},
623 + {0x93a5, 0x43},
624 + {0x93a7, 0x2f},
625 + {0x93a9, 0x2f},
626 + {0x93ad, 0x34},
627 + {0x93af, 0x2f},
628 + {0x93b5, 0x3e},
629 + {0x93b7, 0x2f},
630 + {0x93bd, 0x4d},
631 + {0x93bf, 0x43},
632 + {0x93c1, 0x2f},
633 + {0x93c5, 0x4d},
634 + {0x93c7, 0x43},
635 + {0x93c9, 0x2f},
636 + {0x974b, 0x02},
637 + {0x995c, 0x8c},
638 + {0x995d, 0x00},
639 + {0x995e, 0x00},
640 + {0x9963, 0x64},
641 + {0x9964, 0x50},
642 + {0xaa0a, 0x26},
643 + {0xae03, 0x04},
644 + {0xae04, 0x03},
645 + {0xae05, 0x03},
646 + {0xbc1c, 0x08},
647 + {0xbcf1, 0x02},
648 +};
649 +
650 +/* 16 mpix 10fps */
651 +static const struct imx519_reg mode_4656x3496_regs[] = {
652 + {0x0111, 0x02},
653 + {0x0112, 0x0a},
654 + {0x0113, 0x0a},
655 + {0x0114, 0x01},
656 + {0x0342, 0x42},
657 + {0x0343, 0x00},
658 + {0x0340, 0x0d},
659 + {0x0341, 0xf4},
660 + {0x0344, 0x00},
661 + {0x0345, 0x00},
662 + {0x0346, 0x00},
663 + {0x0347, 0x00},
664 + {0x0348, 0x12},
665 + {0x0349, 0x2f},
666 + {0x034a, 0x0d},
667 + {0x034b, 0xa7},
668 + {0x0220, 0x00},
669 + {0x0221, 0x11},
670 + {0x0222, 0x01},
671 + {0x0900, 0x00},
672 + {0x0901, 0x11},
673 + {0x0902, 0x0a},
674 + {0x3f4c, 0x01},
675 + {0x3f4d, 0x01},
676 + {0x4254, 0x7f},
677 + {0x0401, 0x00},
678 + {0x0404, 0x00},
679 + {0x0405, 0x10},
680 + {0x0408, 0x00},
681 + {0x0409, 0x00},
682 + {0x040a, 0x00},
683 + {0x040b, 0x00},
684 + {0x040c, 0x12},
685 + {0x040d, 0x30},
686 + {0x040e, 0x0d},
687 + {0x040f, 0xa8},
688 + {0x034c, 0x12},
689 + {0x034d, 0x30},
690 + {0x034e, 0x0d},
691 + {0x034f, 0xa8},
692 + {0x0301, 0x06},
693 + {0x0303, 0x04},
694 + {0x0305, 0x04},
695 + {0x0306, 0x01},
696 + {0x0307, 0x57},
697 + {0x0309, 0x0a},
698 + {0x030b, 0x02},
699 + {0x030d, 0x04},
700 + {0x030e, 0x01},
701 + {0x030f, 0x49},
702 + {0x0310, 0x01},
703 + {0x0820, 0x07},
704 + {0x0821, 0xb6},
705 + {0x0822, 0x00},
706 + {0x0823, 0x00},
707 + {0x3e20, 0x01},
708 + {0x3e37, 0x00},
709 + {0x3e3b, 0x00},
710 + {0x0106, 0x00},
711 + {0x0b00, 0x00},
712 + {0x3230, 0x00},
713 + {0x3f14, 0x01},
714 + {0x3f3c, 0x01},
715 + {0x3f0d, 0x0a},
716 + {0x3fbc, 0x00},
717 + {0x3c06, 0x00},
718 + {0x3c07, 0x48},
719 + {0x3c0a, 0x00},
720 + {0x3c0b, 0x00},
721 + {0x3f78, 0x00},
722 + {0x3f79, 0x40},
723 + {0x3f7c, 0x00},
724 + {0x3f7d, 0x00},
725 +};
726 +
727 +/* 4k 21fps mode */
728 +static const struct imx519_reg mode_3840x2160_regs[] = {
729 + {0x0111, 0x02},
730 + {0x0112, 0x0a},
731 + {0x0113, 0x0a},
732 + {0x0114, 0x01},
733 + {0x0342, 0x38},
734 + {0x0343, 0x70},
735 + {0x0340, 0x08},
736 + {0x0341, 0xd4},
737 + {0x0344, 0x01},
738 + {0x0345, 0x98},
739 + {0x0346, 0x02},
740 + {0x0347, 0xa0},
741 + {0x0348, 0x10},
742 + {0x0349, 0x97},
743 + {0x034a, 0x0b},
744 + {0x034b, 0x17},
745 + {0x0220, 0x00},
746 + {0x0221, 0x11},
747 + {0x0222, 0x01},
748 + {0x0900, 0x00},
749 + {0x0901, 0x11},
750 + {0x0902, 0x0a},
751 + {0x3f4c, 0x01},
752 + {0x3f4d, 0x01},
753 + {0x4254, 0x7f},
754 + {0x0401, 0x00},
755 + {0x0404, 0x00},
756 + {0x0405, 0x10},
757 + {0x0408, 0x00},
758 + {0x0409, 0x00},
759 + {0x040a, 0x00},
760 + {0x040b, 0x00},
761 + {0x040c, 0x0f},
762 + {0x040d, 0x00},
763 + {0x040e, 0x08},
764 + {0x040f, 0x70},
765 + {0x034c, 0x0f},
766 + {0x034d, 0x00},
767 + {0x034e, 0x08},
768 + {0x034f, 0x70},
769 + {0x0301, 0x06},
770 + {0x0303, 0x04},
771 + {0x0305, 0x04},
772 + {0x0306, 0x01},
773 + {0x0307, 0x57},
774 + {0x0309, 0x0a},
775 + {0x030b, 0x02},
776 + {0x030d, 0x04},
777 + {0x030e, 0x01},
778 + {0x030f, 0x49},
779 + {0x0310, 0x01},
780 + {0x0820, 0x07},
781 + {0x0821, 0xb6},
782 + {0x0822, 0x00},
783 + {0x0823, 0x00},
784 + {0x3e20, 0x01},
785 + {0x3e37, 0x00},
786 + {0x3e3b, 0x00},
787 + {0x0106, 0x00},
788 + {0x0b00, 0x00},
789 + {0x3230, 0x00},
790 + {0x3f14, 0x01},
791 + {0x3f3c, 0x01},
792 + {0x3f0d, 0x0a},
793 + {0x3fbc, 0x00},
794 + {0x3c06, 0x00},
795 + {0x3c07, 0x48},
796 + {0x3c0a, 0x00},
797 + {0x3c0b, 0x00},
798 + {0x3f78, 0x00},
799 + {0x3f79, 0x40},
800 + {0x3f7c, 0x00},
801 + {0x3f7d, 0x00},
802 +};
803 +
804 +/* 2x2 binned 30fps mode */
805 +static const struct imx519_reg mode_2328x1748_regs[] = {
806 + {0x0111, 0x02},
807 + {0x0112, 0x0a},
808 + {0x0113, 0x0a},
809 + {0x0114, 0x01},
810 + {0x0342, 0x24},
811 + {0x0343, 0x12},
812 + {0x0340, 0x09},
813 + {0x0341, 0xac},
814 + {0x0344, 0x00},
815 + {0x0345, 0x00},
816 + {0x0346, 0x00},
817 + {0x0347, 0x00},
818 + {0x0348, 0x12},
819 + {0x0349, 0x2f},
820 + {0x034a, 0x0d},
821 + {0x034b, 0xa7},
822 + {0x0220, 0x00},
823 + {0x0221, 0x11},
824 + {0x0222, 0x01},
825 + {0x0900, 0x01},
826 + {0x0901, 0x22},
827 + {0x0902, 0x0a},
828 + {0x3f4c, 0x01},
829 + {0x3f4d, 0x01},
830 + {0x4254, 0x7f},
831 + {0x0401, 0x00},
832 + {0x0404, 0x00},
833 + {0x0405, 0x10},
834 + {0x0408, 0x00},
835 + {0x0409, 0x00},
836 + {0x040a, 0x00},
837 + {0x040b, 0x00},
838 + {0x040c, 0x09},
839 + {0x040d, 0x18},
840 + {0x040e, 0x06},
841 + {0x040f, 0xd4},
842 + {0x034c, 0x09},
843 + {0x034d, 0x18},
844 + {0x034e, 0x06},
845 + {0x034f, 0xd4},
846 + {0x0301, 0x06},
847 + {0x0303, 0x04},
848 + {0x0305, 0x04},
849 + {0x0306, 0x01},
850 + {0x0307, 0x57},
851 + {0x0309, 0x0a},
852 + {0x030b, 0x02},
853 + {0x030d, 0x04},
854 + {0x030e, 0x01},
855 + {0x030f, 0x49},
856 + {0x0310, 0x01},
857 + {0x0820, 0x07},
858 + {0x0821, 0xb6},
859 + {0x0822, 0x00},
860 + {0x0823, 0x00},
861 + {0x3e20, 0x01},
862 + {0x3e37, 0x00},
863 + {0x3e3b, 0x00},
864 + {0x0106, 0x00},
865 + {0x0b00, 0x00},
866 + {0x3230, 0x00},
867 + {0x3f14, 0x01},
868 + {0x3f3c, 0x01},
869 + {0x3f0d, 0x0a},
870 + {0x3fbc, 0x00},
871 + {0x3c06, 0x00},
872 + {0x3c07, 0x48},
873 + {0x3c0a, 0x00},
874 + {0x3c0b, 0x00},
875 + {0x3f78, 0x00},
876 + {0x3f79, 0x40},
877 + {0x3f7c, 0x00},
878 + {0x3f7d, 0x00},
879 +};
880 +
881 +/* 1080p 60fps mode */
882 +static const struct imx519_reg mode_1920x1080_regs[] = {
883 + {0x0111, 0x02},
884 + {0x0112, 0x0a},
885 + {0x0113, 0x0a},
886 + {0x0114, 0x01},
887 + {0x0342, 0x25},
888 + {0x0343, 0xd9},
889 + {0x0340, 0x04},
890 + {0x0341, 0x9c},
891 + {0x0344, 0x01},
892 + {0x0345, 0x98},
893 + {0x0346, 0x02},
894 + {0x0347, 0xa2},
895 + {0x0348, 0x10},
896 + {0x0349, 0x97},
897 + {0x034a, 0x0b},
898 + {0x034b, 0x15},
899 + {0x0220, 0x00},
900 + {0x0221, 0x11},
901 + {0x0222, 0x01},
902 + {0x0900, 0x01},
903 + {0x0901, 0x22},
904 + {0x0902, 0x0a},
905 + {0x3f4c, 0x01},
906 + {0x3f4d, 0x01},
907 + {0x4254, 0x7f},
908 + {0x0401, 0x00},
909 + {0x0404, 0x00},
910 + {0x0405, 0x10},
911 + {0x0408, 0x00},
912 + {0x0409, 0x00},
913 + {0x040a, 0x00},
914 + {0x040b, 0x00},
915 + {0x040c, 0x07},
916 + {0x040d, 0x80},
917 + {0x040e, 0x04},
918 + {0x040f, 0x38},
919 + {0x034c, 0x07},
920 + {0x034d, 0x80},
921 + {0x034e, 0x04},
922 + {0x034f, 0x38},
923 + {0x0301, 0x06},
924 + {0x0303, 0x04},
925 + {0x0305, 0x04},
926 + {0x0306, 0x01},
927 + {0x0307, 0x57},
928 + {0x0309, 0x0a},
929 + {0x030b, 0x02},
930 + {0x030d, 0x04},
931 + {0x030e, 0x01},
932 + {0x030f, 0x49},
933 + {0x0310, 0x01},
934 + {0x0820, 0x07},
935 + {0x0821, 0xb6},
936 + {0x0822, 0x00},
937 + {0x0823, 0x00},
938 + {0x3e20, 0x01},
939 + {0x3e37, 0x00},
940 + {0x3e3b, 0x00},
941 + {0x0106, 0x00},
942 + {0x0b00, 0x00},
943 + {0x3230, 0x00},
944 + {0x3f14, 0x01},
945 + {0x3f3c, 0x01},
946 + {0x3f0d, 0x0a},
947 + {0x3fbc, 0x00},
948 + {0x3c06, 0x00},
949 + {0x3c07, 0x48},
950 + {0x3c0a, 0x00},
951 + {0x3c0b, 0x00},
952 + {0x3f78, 0x00},
953 + {0x3f79, 0x40},
954 + {0x3f7c, 0x00},
955 + {0x3f7d, 0x00},
956 +};
957 +
958 +/* 720p 120fps mode */
959 +static const struct imx519_reg mode_1280x720_regs[] = {
960 + {0x0111, 0x02},
961 + {0x0112, 0x0a},
962 + {0x0113, 0x0a},
963 + {0x0114, 0x01},
964 + {0x0342, 0x1b},
965 + {0x0343, 0x3b},
966 + {0x0340, 0x03},
967 + {0x0341, 0x34},
968 + {0x0344, 0x04},
969 + {0x0345, 0x18},
970 + {0x0346, 0x04},
971 + {0x0347, 0x12},
972 + {0x0348, 0x0e},
973 + {0x0349, 0x17},
974 + {0x034a, 0x09},
975 + {0x034b, 0xb6},
976 + {0x0220, 0x00},
977 + {0x0221, 0x11},
978 + {0x0222, 0x01},
979 + {0x0900, 0x01},
980 + {0x0901, 0x22},
981 + {0x0902, 0x0a},
982 + {0x3f4c, 0x01},
983 + {0x3f4d, 0x01},
984 + {0x4254, 0x7f},
985 + {0x0401, 0x00},
986 + {0x0404, 0x00},
987 + {0x0405, 0x10},
988 + {0x0408, 0x00},
989 + {0x0409, 0x00},
990 + {0x040a, 0x00},
991 + {0x040b, 0x00},
992 + {0x040c, 0x05},
993 + {0x040d, 0x00},
994 + {0x040e, 0x02},
995 + {0x040f, 0xd0},
996 + {0x034c, 0x05},
997 + {0x034d, 0x00},
998 + {0x034e, 0x02},
999 + {0x034f, 0xd0},
1000 + {0x0301, 0x06},
1001 + {0x0303, 0x04},
1002 + {0x0305, 0x04},
1003 + {0x0306, 0x01},
1004 + {0x0307, 0x57},
1005 + {0x0309, 0x0a},
1006 + {0x030b, 0x02},
1007 + {0x030d, 0x04},
1008 + {0x030e, 0x01},
1009 + {0x030f, 0x49},
1010 + {0x0310, 0x01},
1011 + {0x0820, 0x07},
1012 + {0x0821, 0xb6},
1013 + {0x0822, 0x00},
1014 + {0x0823, 0x00},
1015 + {0x3e20, 0x01},
1016 + {0x3e37, 0x00},
1017 + {0x3e3b, 0x00},
1018 + {0x0106, 0x00},
1019 + {0x0b00, 0x00},
1020 + {0x3230, 0x00},
1021 + {0x3f14, 0x01},
1022 + {0x3f3c, 0x01},
1023 + {0x3f0d, 0x0a},
1024 + {0x3fbc, 0x00},
1025 + {0x3c06, 0x00},
1026 + {0x3c07, 0x48},
1027 + {0x3c0a, 0x00},
1028 + {0x3c0b, 0x00},
1029 + {0x3f78, 0x00},
1030 + {0x3f79, 0x40},
1031 + {0x3f7c, 0x00},
1032 + {0x3f7d, 0x00},
1033 +};
1034 +
1035 +/* Mode configs */
1036 +static const struct imx519_mode supported_modes_10bit[] = {
1037 + {
1038 + .width = 4656,
1039 + .height = 3496,
1040 + .line_length_pix = 0x4200,
1041 + .crop = {
1042 + .left = IMX519_PIXEL_ARRAY_LEFT,
1043 + .top = IMX519_PIXEL_ARRAY_TOP,
1044 + .width = 4656,
1045 + .height = 3496,
1046 + },
1047 + .timeperframe_min = {
1048 + .numerator = 100,
1049 + .denominator = 1000
1050 + },
1051 + .timeperframe_default = {
1052 + .numerator = 100,
1053 + .denominator = 1000
1054 + },
1055 + .reg_list = {
1056 + .num_of_regs = ARRAY_SIZE(mode_4656x3496_regs),
1057 + .regs = mode_4656x3496_regs,
1058 + }
1059 + },
1060 + {
1061 + .width = 3840,
1062 + .height = 2160,
1063 + .line_length_pix = 0x3870,
1064 + .crop = {
1065 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1066 + .top = IMX519_PIXEL_ARRAY_TOP + 672,
1067 + .width = 3840,
1068 + .height = 2160,
1069 + },
1070 + .timeperframe_min = {
1071 + .numerator = 100,
1072 + .denominator = 2100
1073 + },
1074 + .timeperframe_default = {
1075 + .numerator = 100,
1076 + .denominator = 2100
1077 + },
1078 + .reg_list = {
1079 + .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
1080 + .regs = mode_3840x2160_regs,
1081 + }
1082 + },
1083 + {
1084 + .width = 2328,
1085 + .height = 1748,
1086 + .line_length_pix = 0x2412,
1087 + .crop = {
1088 + .left = IMX519_PIXEL_ARRAY_LEFT,
1089 + .top = IMX519_PIXEL_ARRAY_TOP,
1090 + .width = 4656,
1091 + .height = 3496,
1092 + },
1093 + .timeperframe_min = {
1094 + .numerator = 100,
1095 + .denominator = 3000
1096 + },
1097 + .timeperframe_default = {
1098 + .numerator = 100,
1099 + .denominator = 3000
1100 + },
1101 + .reg_list = {
1102 + .num_of_regs = ARRAY_SIZE(mode_2328x1748_regs),
1103 + .regs = mode_2328x1748_regs,
1104 + }
1105 + },
1106 + {
1107 + .width = 1920,
1108 + .height = 1080,
1109 + .line_length_pix = 0x25D9,
1110 + .crop = {
1111 + .left = IMX519_PIXEL_ARRAY_LEFT + 408,
1112 + .top = IMX519_PIXEL_ARRAY_TOP + 674,
1113 + .width = 3840,
1114 + .height = 2160,
1115 + },
1116 + .timeperframe_min = {
1117 + .numerator = 100,
1118 + .denominator = 6000
1119 + },
1120 + .timeperframe_default = {
1121 + .numerator = 100,
1122 + .denominator = 6000
1123 + },
1124 + .reg_list = {
1125 + .num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
1126 + .regs = mode_1920x1080_regs,
1127 + }
1128 + },
1129 + {
1130 + .width = 1280,
1131 + .height = 720,
1132 + .line_length_pix = 0x1B3B,
1133 + .crop = {
1134 + .left = IMX519_PIXEL_ARRAY_LEFT + 1048,
1135 + .top = IMX519_PIXEL_ARRAY_TOP + 1042,
1136 + .width = 2560,
1137 + .height = 1440,
1138 + },
1139 + .timeperframe_min = {
1140 + .numerator = 100,
1141 + .denominator = 12000
1142 + },
1143 + .timeperframe_default = {
1144 + .numerator = 100,
1145 + .denominator = 12000
1146 + },
1147 + .reg_list = {
1148 + .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
1149 + .regs = mode_1280x720_regs,
1150 + }
1151 + }
1152 +};
1153 +
1154 +/*
1155 + * The supported formats.
1156 + * This table MUST contain 4 entries per format, to cover the various flip
1157 + * combinations in the order
1158 + * - no flip
1159 + * - h flip
1160 + * - v flip
1161 + * - h&v flips
1162 + */
1163 +static const u32 codes[] = {
1164 + /* 10-bit modes. */
1165 + MEDIA_BUS_FMT_SRGGB10_1X10,
1166 + MEDIA_BUS_FMT_SGRBG10_1X10,
1167 + MEDIA_BUS_FMT_SGBRG10_1X10,
1168 + MEDIA_BUS_FMT_SBGGR10_1X10,
1169 +};
1170 +
1171 +static const char * const imx519_test_pattern_menu[] = {
1172 + "Disabled",
1173 + "Color Bars",
1174 + "Solid Color",
1175 + "Grey Color Bars",
1176 + "PN9"
1177 +};
1178 +
1179 +static const int imx519_test_pattern_val[] = {
1180 + IMX519_TEST_PATTERN_DISABLE,
1181 + IMX519_TEST_PATTERN_COLOR_BARS,
1182 + IMX519_TEST_PATTERN_SOLID_COLOR,
1183 + IMX519_TEST_PATTERN_GREY_COLOR,
1184 + IMX519_TEST_PATTERN_PN9,
1185 +};
1186 +
1187 +/* regulator supplies */
1188 +static const char * const imx519_supply_name[] = {
1189 + /* Supplies can be enabled in any order */
1190 + "VANA", /* Analog (2.8V) supply */
1191 + "VDIG", /* Digital Core (1.05V) supply */
1192 + "VDDL", /* IF (1.8V) supply */
1193 +};
1194 +
1195 +#define IMX519_NUM_SUPPLIES ARRAY_SIZE(imx519_supply_name)
1196 +
1197 +/*
1198 + * Initialisation delay between XCLR low->high and the moment when the sensor
1199 + * can start capture (i.e. can leave software standby), given by T7 in the
1200 + * datasheet is 8ms. This does include I2C setup time as well.
1201 + *
1202 + * Note, that delay between XCLR low->high and reading the CCI ID register (T6
1203 + * in the datasheet) is much smaller - 600us.
1204 + */
1205 +#define IMX519_XCLR_MIN_DELAY_US 8000
1206 +#define IMX519_XCLR_DELAY_RANGE_US 1000
1207 +
1208 +struct imx519 {
1209 + struct v4l2_subdev sd;
1210 + struct media_pad pad[NUM_PADS];
1211 +
1212 + unsigned int fmt_code;
1213 +
1214 + struct clk *xclk;
1215 +
1216 + struct gpio_desc *reset_gpio;
1217 + struct regulator_bulk_data supplies[IMX519_NUM_SUPPLIES];
1218 +
1219 + struct v4l2_ctrl_handler ctrl_handler;
1220 + /* V4L2 Controls */
1221 + struct v4l2_ctrl *pixel_rate;
1222 + struct v4l2_ctrl *exposure;
1223 + struct v4l2_ctrl *vflip;
1224 + struct v4l2_ctrl *hflip;
1225 + struct v4l2_ctrl *vblank;
1226 + struct v4l2_ctrl *hblank;
1227 +
1228 + /* Current mode */
1229 + const struct imx519_mode *mode;
1230 +
1231 + /*
1232 + * Mutex for serialized access:
1233 + * Protect sensor module set pad format and start/stop streaming safely.
1234 + */
1235 + struct mutex mutex;
1236 +
1237 + /* Streaming on/off */
1238 + bool streaming;
1239 +
1240 + /* Rewrite common registers on stream on? */
1241 + bool common_regs_written;
1242 +
1243 + /* Current long exposure factor in use. Set through V4L2_CID_VBLANK */
1244 + unsigned int long_exp_shift;
1245 +};
1246 +
1247 +static inline struct imx519 *to_imx519(struct v4l2_subdev *_sd)
1248 +{
1249 + return container_of(_sd, struct imx519, sd);
1250 +}
1251 +
1252 +/* Read registers up to 2 at a time */
1253 +static int imx519_read_reg(struct imx519 *imx519, u16 reg, u32 len, u32 *val)
1254 +{
1255 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1256 + struct i2c_msg msgs[2];
1257 + u8 addr_buf[2] = { reg >> 8, reg & 0xff };
1258 + u8 data_buf[4] = { 0, };
1259 + int ret;
1260 +
1261 + if (len > 4)
1262 + return -EINVAL;
1263 +
1264 + /* Write register address */
1265 + msgs[0].addr = client->addr;
1266 + msgs[0].flags = 0;
1267 + msgs[0].len = ARRAY_SIZE(addr_buf);
1268 + msgs[0].buf = addr_buf;
1269 +
1270 + /* Read data from register */
1271 + msgs[1].addr = client->addr;
1272 + msgs[1].flags = I2C_M_RD;
1273 + msgs[1].len = len;
1274 + msgs[1].buf = &data_buf[4 - len];
1275 +
1276 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1277 + if (ret != ARRAY_SIZE(msgs))
1278 + return -EIO;
1279 +
1280 + *val = get_unaligned_be32(data_buf);
1281 +
1282 + return 0;
1283 +}
1284 +
1285 +/* Write registers up to 2 at a time */
1286 +static int imx519_write_reg(struct imx519 *imx519, u16 reg, u32 len, u32 val)
1287 +{
1288 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1289 + u8 buf[6];
1290 +
1291 + if (len > 4)
1292 + return -EINVAL;
1293 +
1294 + put_unaligned_be16(reg, buf);
1295 + put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
1296 + if (i2c_master_send(client, buf, len + 2) != len + 2)
1297 + return -EIO;
1298 +
1299 + return 0;
1300 +}
1301 +
1302 +/* Write a list of registers */
1303 +static int imx519_write_regs(struct imx519 *imx519,
1304 + const struct imx519_reg *regs, u32 len)
1305 +{
1306 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1307 + unsigned int i;
1308 + int ret;
1309 +
1310 + for (i = 0; i < len; i++) {
1311 + ret = imx519_write_reg(imx519, regs[i].address, 1, regs[i].val);
1312 + if (ret) {
1313 + dev_err_ratelimited(&client->dev,
1314 + "Failed to write reg 0x%4.4x. error = %d\n",
1315 + regs[i].address, ret);
1316 +
1317 + return ret;
1318 + }
1319 + }
1320 +
1321 + return 0;
1322 +}
1323 +
1324 +/* Get bayer order based on flip setting. */
1325 +static u32 imx519_get_format_code(struct imx519 *imx519)
1326 +{
1327 + unsigned int i;
1328 +
1329 + lockdep_assert_held(&imx519->mutex);
1330 +
1331 + i = (imx519->vflip->val ? 2 : 0) |
1332 + (imx519->hflip->val ? 1 : 0);
1333 +
1334 + return codes[i];
1335 +}
1336 +
1337 +static int imx519_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1338 +{
1339 + struct imx519 *imx519 = to_imx519(sd);
1340 + struct v4l2_mbus_framefmt *try_fmt_img =
1341 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
1342 + struct v4l2_mbus_framefmt *try_fmt_meta =
1343 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
1344 + struct v4l2_rect *try_crop;
1345 +
1346 + mutex_lock(&imx519->mutex);
1347 +
1348 + /* Initialize try_fmt for the image pad */
1349 + try_fmt_img->width = supported_modes_10bit[0].width;
1350 + try_fmt_img->height = supported_modes_10bit[0].height;
1351 + try_fmt_img->code = imx519_get_format_code(imx519);
1352 + try_fmt_img->field = V4L2_FIELD_NONE;
1353 +
1354 + /* Initialize try_fmt for the embedded metadata pad */
1355 + try_fmt_meta->width = IMX519_EMBEDDED_LINE_WIDTH;
1356 + try_fmt_meta->height = IMX519_NUM_EMBEDDED_LINES;
1357 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
1358 + try_fmt_meta->field = V4L2_FIELD_NONE;
1359 +
1360 + /* Initialize try_crop */
1361 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, IMAGE_PAD);
1362 + try_crop->left = IMX519_PIXEL_ARRAY_LEFT;
1363 + try_crop->top = IMX519_PIXEL_ARRAY_TOP;
1364 + try_crop->width = IMX519_PIXEL_ARRAY_WIDTH;
1365 + try_crop->height = IMX519_PIXEL_ARRAY_HEIGHT;
1366 +
1367 + mutex_unlock(&imx519->mutex);
1368 +
1369 + return 0;
1370 +}
1371 +
1372 +static void imx519_adjust_exposure_range(struct imx519 *imx519)
1373 +{
1374 + int exposure_max, exposure_def;
1375 +
1376 + /* Honour the VBLANK limits when setting exposure. */
1377 + exposure_max = imx519->mode->height + imx519->vblank->val -
1378 + IMX519_EXPOSURE_OFFSET;
1379 + exposure_def = min(exposure_max, imx519->exposure->val);
1380 + __v4l2_ctrl_modify_range(imx519->exposure, imx519->exposure->minimum,
1381 + exposure_max, imx519->exposure->step,
1382 + exposure_def);
1383 +}
1384 +
1385 +static int imx519_set_frame_length(struct imx519 *imx519, unsigned int val)
1386 +{
1387 + int ret = 0;
1388 +
1389 + imx519->long_exp_shift = 0;
1390 +
1391 + while (val > IMX519_FRAME_LENGTH_MAX) {
1392 + imx519->long_exp_shift++;
1393 + val >>= 1;
1394 + }
1395 +
1396 + ret = imx519_write_reg(imx519, IMX519_REG_FRAME_LENGTH,
1397 + IMX519_REG_VALUE_16BIT, val);
1398 + if (ret)
1399 + return ret;
1400 +
1401 + return imx519_write_reg(imx519, IMX519_LONG_EXP_SHIFT_REG,
1402 + IMX519_REG_VALUE_08BIT, imx519->long_exp_shift);
1403 +}
1404 +
1405 +static int imx519_set_ctrl(struct v4l2_ctrl *ctrl)
1406 +{
1407 + struct imx519 *imx519 =
1408 + container_of(ctrl->handler, struct imx519, ctrl_handler);
1409 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1410 + int ret = 0;
1411 +
1412 + /*
1413 + * The VBLANK control may change the limits of usable exposure, so check
1414 + * and adjust if necessary.
1415 + */
1416 + if (ctrl->id == V4L2_CID_VBLANK)
1417 + imx519_adjust_exposure_range(imx519);
1418 +
1419 + /*
1420 + * Applying V4L2 control value only happens
1421 + * when power is up for streaming
1422 + */
1423 + if (pm_runtime_get_if_in_use(&client->dev) == 0)
1424 + return 0;
1425 +
1426 + switch (ctrl->id) {
1427 + case V4L2_CID_ANALOGUE_GAIN:
1428 + ret = imx519_write_reg(imx519, IMX519_REG_ANALOG_GAIN,
1429 + IMX519_REG_VALUE_16BIT, ctrl->val);
1430 + break;
1431 + case V4L2_CID_EXPOSURE:
1432 + ret = imx519_write_reg(imx519, IMX519_REG_EXPOSURE,
1433 + IMX519_REG_VALUE_16BIT, ctrl->val >>
1434 + imx519->long_exp_shift);
1435 + break;
1436 + case V4L2_CID_DIGITAL_GAIN:
1437 + ret = imx519_write_reg(imx519, IMX519_REG_DIGITAL_GAIN,
1438 + IMX519_REG_VALUE_16BIT, ctrl->val);
1439 + break;
1440 + case V4L2_CID_TEST_PATTERN:
1441 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN,
1442 + IMX519_REG_VALUE_16BIT,
1443 + imx519_test_pattern_val[ctrl->val]);
1444 + break;
1445 + case V4L2_CID_TEST_PATTERN_RED:
1446 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_R,
1447 + IMX519_REG_VALUE_16BIT, ctrl->val);
1448 + break;
1449 + case V4L2_CID_TEST_PATTERN_GREENR:
1450 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GR,
1451 + IMX519_REG_VALUE_16BIT, ctrl->val);
1452 + break;
1453 + case V4L2_CID_TEST_PATTERN_BLUE:
1454 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_B,
1455 + IMX519_REG_VALUE_16BIT, ctrl->val);
1456 + break;
1457 + case V4L2_CID_TEST_PATTERN_GREENB:
1458 + ret = imx519_write_reg(imx519, IMX519_REG_TEST_PATTERN_GB,
1459 + IMX519_REG_VALUE_16BIT, ctrl->val);
1460 + break;
1461 + case V4L2_CID_HFLIP:
1462 + case V4L2_CID_VFLIP:
1463 + ret = imx519_write_reg(imx519, IMX519_REG_ORIENTATION, 1,
1464 + imx519->hflip->val |
1465 + imx519->vflip->val << 1);
1466 + break;
1467 + case V4L2_CID_VBLANK:
1468 + ret = imx519_set_frame_length(imx519,
1469 + imx519->mode->height + ctrl->val);
1470 + break;
1471 + default:
1472 + dev_info(&client->dev,
1473 + "ctrl(id:0x%x,val:0x%x) is not handled\n",
1474 + ctrl->id, ctrl->val);
1475 + ret = -EINVAL;
1476 + break;
1477 + }
1478 +
1479 + pm_runtime_put(&client->dev);
1480 +
1481 + return ret;
1482 +}
1483 +
1484 +static const struct v4l2_ctrl_ops imx519_ctrl_ops = {
1485 + .s_ctrl = imx519_set_ctrl,
1486 +};
1487 +
1488 +static int imx519_enum_mbus_code(struct v4l2_subdev *sd,
1489 + struct v4l2_subdev_state *sd_state,
1490 + struct v4l2_subdev_mbus_code_enum *code)
1491 +{
1492 + struct imx519 *imx519 = to_imx519(sd);
1493 +
1494 + if (code->pad >= NUM_PADS)
1495 + return -EINVAL;
1496 +
1497 + if (code->pad == IMAGE_PAD) {
1498 + if (code->index > 0)
1499 + return -EINVAL;
1500 +
1501 + code->code = imx519_get_format_code(imx519);
1502 + } else {
1503 + if (code->index > 0)
1504 + return -EINVAL;
1505 +
1506 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
1507 + }
1508 +
1509 + return 0;
1510 +}
1511 +
1512 +static int imx519_enum_frame_size(struct v4l2_subdev *sd,
1513 + struct v4l2_subdev_state *sd_state,
1514 + struct v4l2_subdev_frame_size_enum *fse)
1515 +{
1516 + struct imx519 *imx519 = to_imx519(sd);
1517 +
1518 + if (fse->pad >= NUM_PADS)
1519 + return -EINVAL;
1520 +
1521 + if (fse->pad == IMAGE_PAD) {
1522 + if (fse->index >= ARRAY_SIZE(supported_modes_10bit))
1523 + return -EINVAL;
1524 +
1525 + if (fse->code != imx519_get_format_code(imx519))
1526 + return -EINVAL;
1527 +
1528 + fse->min_width = supported_modes_10bit[fse->index].width;
1529 + fse->max_width = fse->min_width;
1530 + fse->min_height = supported_modes_10bit[fse->index].height;
1531 + fse->max_height = fse->min_height;
1532 + } else {
1533 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
1534 + return -EINVAL;
1535 +
1536 + fse->min_width = IMX519_EMBEDDED_LINE_WIDTH;
1537 + fse->max_width = fse->min_width;
1538 + fse->min_height = IMX519_NUM_EMBEDDED_LINES;
1539 + fse->max_height = fse->min_height;
1540 + }
1541 +
1542 + return 0;
1543 +}
1544 +
1545 +static void imx519_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
1546 +{
1547 + fmt->colorspace = V4L2_COLORSPACE_RAW;
1548 + fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
1549 + fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
1550 + fmt->colorspace,
1551 + fmt->ycbcr_enc);
1552 + fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
1553 +}
1554 +
1555 +static void imx519_update_image_pad_format(struct imx519 *imx519,
1556 + const struct imx519_mode *mode,
1557 + struct v4l2_subdev_format *fmt)
1558 +{
1559 + fmt->format.width = mode->width;
1560 + fmt->format.height = mode->height;
1561 + fmt->format.field = V4L2_FIELD_NONE;
1562 + imx519_reset_colorspace(&fmt->format);
1563 +}
1564 +
1565 +static void imx519_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
1566 +{
1567 + fmt->format.width = IMX519_EMBEDDED_LINE_WIDTH;
1568 + fmt->format.height = IMX519_NUM_EMBEDDED_LINES;
1569 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
1570 + fmt->format.field = V4L2_FIELD_NONE;
1571 +}
1572 +
1573 +static int imx519_get_pad_format(struct v4l2_subdev *sd,
1574 + struct v4l2_subdev_state *sd_state,
1575 + struct v4l2_subdev_format *fmt)
1576 +{
1577 + struct imx519 *imx519 = to_imx519(sd);
1578 +
1579 + if (fmt->pad >= NUM_PADS)
1580 + return -EINVAL;
1581 +
1582 + mutex_lock(&imx519->mutex);
1583 +
1584 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1585 + struct v4l2_mbus_framefmt *try_fmt =
1586 + v4l2_subdev_get_try_format(&imx519->sd, sd_state,
1587 + fmt->pad);
1588 + /* update the code which could change due to vflip or hflip: */
1589 + try_fmt->code = fmt->pad == IMAGE_PAD ?
1590 + imx519_get_format_code(imx519) :
1591 + MEDIA_BUS_FMT_SENSOR_DATA;
1592 + fmt->format = *try_fmt;
1593 + } else {
1594 + if (fmt->pad == IMAGE_PAD) {
1595 + imx519_update_image_pad_format(imx519, imx519->mode,
1596 + fmt);
1597 + fmt->format.code =
1598 + imx519_get_format_code(imx519);
1599 + } else {
1600 + imx519_update_metadata_pad_format(fmt);
1601 + }
1602 + }
1603 +
1604 + mutex_unlock(&imx519->mutex);
1605 + return 0;
1606 +}
1607 +
1608 +static
1609 +unsigned int imx519_get_frame_length(const struct imx519_mode *mode,
1610 + const struct v4l2_fract *timeperframe)
1611 +{
1612 + u64 frame_length;
1613 +
1614 + frame_length = (u64)timeperframe->numerator * IMX519_PIXEL_RATE;
1615 + do_div(frame_length,
1616 + (u64)timeperframe->denominator * mode->line_length_pix);
1617 +
1618 + if (WARN_ON(frame_length > IMX519_FRAME_LENGTH_MAX))
1619 + frame_length = IMX519_FRAME_LENGTH_MAX;
1620 +
1621 + return max_t(unsigned int, frame_length, mode->height);
1622 +}
1623 +
1624 +static void imx519_set_framing_limits(struct imx519 *imx519)
1625 +{
1626 + unsigned int frm_length_min, frm_length_default, hblank;
1627 + const struct imx519_mode *mode = imx519->mode;
1628 +
1629 + frm_length_min = imx519_get_frame_length(mode, &mode->timeperframe_min);
1630 + frm_length_default =
1631 + imx519_get_frame_length(mode, &mode->timeperframe_default);
1632 +
1633 + /* Default to no long exposure multiplier. */
1634 + imx519->long_exp_shift = 0;
1635 +
1636 + /* Update limits and set FPS to default */
1637 + __v4l2_ctrl_modify_range(imx519->vblank, frm_length_min - mode->height,
1638 + ((1 << IMX519_LONG_EXP_SHIFT_MAX) *
1639 + IMX519_FRAME_LENGTH_MAX) - mode->height,
1640 + 1, frm_length_default - mode->height);
1641 +
1642 + /* Setting this will adjust the exposure limits as well. */
1643 + __v4l2_ctrl_s_ctrl(imx519->vblank, frm_length_default - mode->height);
1644 +
1645 + /*
1646 + * Currently PPL is fixed to the mode specified value, so hblank
1647 + * depends on mode->width only, and is not changeable in any
1648 + * way other than changing the mode.
1649 + */
1650 + hblank = mode->line_length_pix - mode->width;
1651 + __v4l2_ctrl_modify_range(imx519->hblank, hblank, hblank, 1, hblank);
1652 +}
1653 +
1654 +static int imx519_set_pad_format(struct v4l2_subdev *sd,
1655 + struct v4l2_subdev_state *sd_state,
1656 + struct v4l2_subdev_format *fmt)
1657 +{
1658 + struct v4l2_mbus_framefmt *framefmt;
1659 + const struct imx519_mode *mode;
1660 + struct imx519 *imx519 = to_imx519(sd);
1661 +
1662 + if (fmt->pad >= NUM_PADS)
1663 + return -EINVAL;
1664 +
1665 + mutex_lock(&imx519->mutex);
1666 +
1667 + if (fmt->pad == IMAGE_PAD) {
1668 + /* Bayer order varies with flips */
1669 + fmt->format.code = imx519_get_format_code(imx519);
1670 +
1671 + mode = v4l2_find_nearest_size(supported_modes_10bit,
1672 + ARRAY_SIZE(supported_modes_10bit),
1673 + width, height,
1674 + fmt->format.width,
1675 + fmt->format.height);
1676 + imx519_update_image_pad_format(imx519, mode, fmt);
1677 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1678 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1679 + fmt->pad);
1680 + *framefmt = fmt->format;
1681 + } else {
1682 + imx519->mode = mode;
1683 + imx519->fmt_code = fmt->format.code;
1684 + imx519_set_framing_limits(imx519);
1685 + }
1686 + } else {
1687 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1688 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
1689 + fmt->pad);
1690 + *framefmt = fmt->format;
1691 + } else {
1692 + /* Only one embedded data mode is supported */
1693 + imx519_update_metadata_pad_format(fmt);
1694 + }
1695 + }
1696 +
1697 + mutex_unlock(&imx519->mutex);
1698 +
1699 + return 0;
1700 +}
1701 +
1702 +static const struct v4l2_rect *
1703 +__imx519_get_pad_crop(struct imx519 *imx519, struct v4l2_subdev_state *sd_state,
1704 + unsigned int pad, enum v4l2_subdev_format_whence which)
1705 +{
1706 + switch (which) {
1707 + case V4L2_SUBDEV_FORMAT_TRY:
1708 + return v4l2_subdev_get_try_crop(&imx519->sd, sd_state, pad);
1709 + case V4L2_SUBDEV_FORMAT_ACTIVE:
1710 + return &imx519->mode->crop;
1711 + }
1712 +
1713 + return NULL;
1714 +}
1715 +
1716 +static int imx519_get_selection(struct v4l2_subdev *sd,
1717 + struct v4l2_subdev_state *sd_state,
1718 + struct v4l2_subdev_selection *sel)
1719 +{
1720 + switch (sel->target) {
1721 + case V4L2_SEL_TGT_CROP: {
1722 + struct imx519 *imx519 = to_imx519(sd);
1723 +
1724 + mutex_lock(&imx519->mutex);
1725 + sel->r = *__imx519_get_pad_crop(imx519, sd_state, sel->pad,
1726 + sel->which);
1727 + mutex_unlock(&imx519->mutex);
1728 +
1729 + return 0;
1730 + }
1731 +
1732 + case V4L2_SEL_TGT_NATIVE_SIZE:
1733 + sel->r.left = 0;
1734 + sel->r.top = 0;
1735 + sel->r.width = IMX519_NATIVE_WIDTH;
1736 + sel->r.height = IMX519_NATIVE_HEIGHT;
1737 +
1738 + return 0;
1739 +
1740 + case V4L2_SEL_TGT_CROP_DEFAULT:
1741 + case V4L2_SEL_TGT_CROP_BOUNDS:
1742 + sel->r.left = IMX519_PIXEL_ARRAY_LEFT;
1743 + sel->r.top = IMX519_PIXEL_ARRAY_TOP;
1744 + sel->r.width = IMX519_PIXEL_ARRAY_WIDTH;
1745 + sel->r.height = IMX519_PIXEL_ARRAY_HEIGHT;
1746 +
1747 + return 0;
1748 + }
1749 +
1750 + return -EINVAL;
1751 +}
1752 +
1753 +/* Start streaming */
1754 +static int imx519_start_streaming(struct imx519 *imx519)
1755 +{
1756 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1757 + const struct imx519_reg_list *reg_list;
1758 + int ret;
1759 +
1760 + if (!imx519->common_regs_written) {
1761 + ret = imx519_write_regs(imx519, mode_common_regs,
1762 + ARRAY_SIZE(mode_common_regs));
1763 +
1764 + if (ret) {
1765 + dev_err(&client->dev, "%s failed to set common settings\n",
1766 + __func__);
1767 + return ret;
1768 + }
1769 + imx519->common_regs_written = true;
1770 + }
1771 +
1772 + /* Apply default values of current mode */
1773 + reg_list = &imx519->mode->reg_list;
1774 + ret = imx519_write_regs(imx519, reg_list->regs, reg_list->num_of_regs);
1775 + if (ret) {
1776 + dev_err(&client->dev, "%s failed to set mode\n", __func__);
1777 + return ret;
1778 + }
1779 +
1780 + /* Apply customized values from user */
1781 + ret = __v4l2_ctrl_handler_setup(imx519->sd.ctrl_handler);
1782 + if (ret)
1783 + return ret;
1784 +
1785 + /* set stream on register */
1786 + return imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1787 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STREAMING);
1788 +}
1789 +
1790 +/* Stop streaming */
1791 +static void imx519_stop_streaming(struct imx519 *imx519)
1792 +{
1793 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1794 + int ret;
1795 +
1796 + /* set stream off register */
1797 + ret = imx519_write_reg(imx519, IMX519_REG_MODE_SELECT,
1798 + IMX519_REG_VALUE_08BIT, IMX519_MODE_STANDBY);
1799 + if (ret)
1800 + dev_err(&client->dev, "%s failed to set stream\n", __func__);
1801 +}
1802 +
1803 +static int imx519_set_stream(struct v4l2_subdev *sd, int enable)
1804 +{
1805 + struct imx519 *imx519 = to_imx519(sd);
1806 + struct i2c_client *client = v4l2_get_subdevdata(sd);
1807 + int ret = 0;
1808 +
1809 + mutex_lock(&imx519->mutex);
1810 + if (imx519->streaming == enable) {
1811 + mutex_unlock(&imx519->mutex);
1812 + return 0;
1813 + }
1814 +
1815 + if (enable) {
1816 + ret = pm_runtime_get_sync(&client->dev);
1817 + if (ret < 0) {
1818 + pm_runtime_put_noidle(&client->dev);
1819 + goto err_unlock;
1820 + }
1821 +
1822 + /*
1823 + * Apply default & customized values
1824 + * and then start streaming.
1825 + */
1826 + ret = imx519_start_streaming(imx519);
1827 + if (ret)
1828 + goto err_rpm_put;
1829 + } else {
1830 + imx519_stop_streaming(imx519);
1831 + pm_runtime_put(&client->dev);
1832 + }
1833 +
1834 + imx519->streaming = enable;
1835 +
1836 + /* vflip and hflip cannot change during streaming */
1837 + __v4l2_ctrl_grab(imx519->vflip, enable);
1838 + __v4l2_ctrl_grab(imx519->hflip, enable);
1839 +
1840 + mutex_unlock(&imx519->mutex);
1841 +
1842 + return ret;
1843 +
1844 +err_rpm_put:
1845 + pm_runtime_put(&client->dev);
1846 +err_unlock:
1847 + mutex_unlock(&imx519->mutex);
1848 +
1849 + return ret;
1850 +}
1851 +
1852 +/* Power/clock management functions */
1853 +static int imx519_power_on(struct device *dev)
1854 +{
1855 + struct i2c_client *client = to_i2c_client(dev);
1856 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1857 + struct imx519 *imx519 = to_imx519(sd);
1858 + int ret;
1859 +
1860 + ret = regulator_bulk_enable(IMX519_NUM_SUPPLIES,
1861 + imx519->supplies);
1862 + if (ret) {
1863 + dev_err(&client->dev, "%s: failed to enable regulators\n",
1864 + __func__);
1865 + return ret;
1866 + }
1867 +
1868 + ret = clk_prepare_enable(imx519->xclk);
1869 + if (ret) {
1870 + dev_err(&client->dev, "%s: failed to enable clock\n",
1871 + __func__);
1872 + goto reg_off;
1873 + }
1874 +
1875 + gpiod_set_value_cansleep(imx519->reset_gpio, 1);
1876 + usleep_range(IMX519_XCLR_MIN_DELAY_US,
1877 + IMX519_XCLR_MIN_DELAY_US + IMX519_XCLR_DELAY_RANGE_US);
1878 +
1879 + return 0;
1880 +
1881 +reg_off:
1882 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1883 + return ret;
1884 +}
1885 +
1886 +static int imx519_power_off(struct device *dev)
1887 +{
1888 + struct i2c_client *client = to_i2c_client(dev);
1889 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1890 + struct imx519 *imx519 = to_imx519(sd);
1891 +
1892 + gpiod_set_value_cansleep(imx519->reset_gpio, 0);
1893 + regulator_bulk_disable(IMX519_NUM_SUPPLIES, imx519->supplies);
1894 + clk_disable_unprepare(imx519->xclk);
1895 +
1896 + /* Force reprogramming of the common registers when powered up again. */
1897 + imx519->common_regs_written = false;
1898 +
1899 + return 0;
1900 +}
1901 +
1902 +static int __maybe_unused imx519_suspend(struct device *dev)
1903 +{
1904 + struct i2c_client *client = to_i2c_client(dev);
1905 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1906 + struct imx519 *imx519 = to_imx519(sd);
1907 +
1908 + if (imx519->streaming)
1909 + imx519_stop_streaming(imx519);
1910 +
1911 + return 0;
1912 +}
1913 +
1914 +static int __maybe_unused imx519_resume(struct device *dev)
1915 +{
1916 + struct i2c_client *client = to_i2c_client(dev);
1917 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
1918 + struct imx519 *imx519 = to_imx519(sd);
1919 + int ret;
1920 +
1921 + if (imx519->streaming) {
1922 + ret = imx519_start_streaming(imx519);
1923 + if (ret)
1924 + goto error;
1925 + }
1926 +
1927 + return 0;
1928 +
1929 +error:
1930 + imx519_stop_streaming(imx519);
1931 + imx519->streaming = 0;
1932 + return ret;
1933 +}
1934 +
1935 +static int imx519_get_regulators(struct imx519 *imx519)
1936 +{
1937 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1938 + unsigned int i;
1939 +
1940 + for (i = 0; i < IMX519_NUM_SUPPLIES; i++)
1941 + imx519->supplies[i].supply = imx519_supply_name[i];
1942 +
1943 + return devm_regulator_bulk_get(&client->dev,
1944 + IMX519_NUM_SUPPLIES,
1945 + imx519->supplies);
1946 +}
1947 +
1948 +/* Verify chip ID */
1949 +static int imx519_identify_module(struct imx519 *imx519, u32 expected_id)
1950 +{
1951 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
1952 + int ret;
1953 + u32 val;
1954 +
1955 + ret = imx519_read_reg(imx519, IMX519_REG_CHIP_ID,
1956 + IMX519_REG_VALUE_16BIT, &val);
1957 + if (ret) {
1958 + dev_err(&client->dev, "failed to read chip id %x, with error %d\n",
1959 + expected_id, ret);
1960 + return ret;
1961 + }
1962 +
1963 + if (val != expected_id) {
1964 + dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1965 + expected_id, val);
1966 + return -EIO;
1967 + }
1968 +
1969 + dev_info(&client->dev, "Device found is imx%x\n", val);
1970 +
1971 + return 0;
1972 +}
1973 +
1974 +static const struct v4l2_subdev_core_ops imx519_core_ops = {
1975 + .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1976 + .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1977 +};
1978 +
1979 +static const struct v4l2_subdev_video_ops imx519_video_ops = {
1980 + .s_stream = imx519_set_stream,
1981 +};
1982 +
1983 +static const struct v4l2_subdev_pad_ops imx519_pad_ops = {
1984 + .enum_mbus_code = imx519_enum_mbus_code,
1985 + .get_fmt = imx519_get_pad_format,
1986 + .set_fmt = imx519_set_pad_format,
1987 + .get_selection = imx519_get_selection,
1988 + .enum_frame_size = imx519_enum_frame_size,
1989 +};
1990 +
1991 +static const struct v4l2_subdev_ops imx519_subdev_ops = {
1992 + .core = &imx519_core_ops,
1993 + .video = &imx519_video_ops,
1994 + .pad = &imx519_pad_ops,
1995 +};
1996 +
1997 +static const struct v4l2_subdev_internal_ops imx519_internal_ops = {
1998 + .open = imx519_open,
1999 +};
2000 +
2001 +/* Initialize control handlers */
2002 +static int imx519_init_controls(struct imx519 *imx519)
2003 +{
2004 + struct v4l2_ctrl_handler *ctrl_hdlr;
2005 + struct i2c_client *client = v4l2_get_subdevdata(&imx519->sd);
2006 + struct v4l2_fwnode_device_properties props;
2007 + unsigned int i;
2008 + int ret;
2009 +
2010 + ctrl_hdlr = &imx519->ctrl_handler;
2011 + ret = v4l2_ctrl_handler_init(ctrl_hdlr, 16);
2012 + if (ret)
2013 + return ret;
2014 +
2015 + mutex_init(&imx519->mutex);
2016 + ctrl_hdlr->lock = &imx519->mutex;
2017 +
2018 + /* By default, PIXEL_RATE is read only */
2019 + imx519->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2020 + V4L2_CID_PIXEL_RATE,
2021 + IMX519_PIXEL_RATE,
2022 + IMX519_PIXEL_RATE, 1,
2023 + IMX519_PIXEL_RATE);
2024 +
2025 + /*
2026 + * Create the controls here, but mode specific limits are setup
2027 + * in the imx519_set_framing_limits() call below.
2028 + */
2029 + imx519->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2030 + V4L2_CID_VBLANK, 0, 0xffff, 1, 0);
2031 + imx519->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2032 + V4L2_CID_HBLANK, 0, 0xffff, 1, 0);
2033 +
2034 + /* HBLANK is read-only for now, but does change with mode. */
2035 + if (imx519->hblank)
2036 + imx519->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2037 +
2038 + imx519->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2039 + V4L2_CID_EXPOSURE,
2040 + IMX519_EXPOSURE_MIN,
2041 + IMX519_EXPOSURE_MAX,
2042 + IMX519_EXPOSURE_STEP,
2043 + IMX519_EXPOSURE_DEFAULT);
2044 +
2045 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
2046 + IMX519_ANA_GAIN_MIN, IMX519_ANA_GAIN_MAX,
2047 + IMX519_ANA_GAIN_STEP, IMX519_ANA_GAIN_DEFAULT);
2048 +
2049 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
2050 + IMX519_DGTL_GAIN_MIN, IMX519_DGTL_GAIN_MAX,
2051 + IMX519_DGTL_GAIN_STEP, IMX519_DGTL_GAIN_DEFAULT);
2052 +
2053 + imx519->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2054 + V4L2_CID_HFLIP, 0, 1, 1, 0);
2055 + if (imx519->hflip)
2056 + imx519->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2057 +
2058 + imx519->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2059 + V4L2_CID_VFLIP, 0, 1, 1, 0);
2060 + if (imx519->vflip)
2061 + imx519->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
2062 +
2063 + v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx519_ctrl_ops,
2064 + V4L2_CID_TEST_PATTERN,
2065 + ARRAY_SIZE(imx519_test_pattern_menu) - 1,
2066 + 0, 0, imx519_test_pattern_menu);
2067 + for (i = 0; i < 4; i++) {
2068 + /*
2069 + * The assumption is that
2070 + * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
2071 + * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
2072 + * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
2073 + */
2074 + v4l2_ctrl_new_std(ctrl_hdlr, &imx519_ctrl_ops,
2075 + V4L2_CID_TEST_PATTERN_RED + i,
2076 + IMX519_TEST_PATTERN_COLOUR_MIN,
2077 + IMX519_TEST_PATTERN_COLOUR_MAX,
2078 + IMX519_TEST_PATTERN_COLOUR_STEP,
2079 + IMX519_TEST_PATTERN_COLOUR_MAX);
2080 + /* The "Solid color" pattern is white by default */
2081 + }
2082 +
2083 + if (ctrl_hdlr->error) {
2084 + ret = ctrl_hdlr->error;
2085 + dev_err(&client->dev, "%s control init failed (%d)\n",
2086 + __func__, ret);
2087 + goto error;
2088 + }
2089 +
2090 + ret = v4l2_fwnode_device_parse(&client->dev, &props);
2091 + if (ret)
2092 + goto error;
2093 +
2094 + ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx519_ctrl_ops,
2095 + &props);
2096 + if (ret)
2097 + goto error;
2098 +
2099 + imx519->sd.ctrl_handler = ctrl_hdlr;
2100 +
2101 + /* Setup exposure and frame/line length limits. */
2102 + imx519_set_framing_limits(imx519);
2103 +
2104 + return 0;
2105 +
2106 +error:
2107 + v4l2_ctrl_handler_free(ctrl_hdlr);
2108 + mutex_destroy(&imx519->mutex);
2109 +
2110 + return ret;
2111 +}
2112 +
2113 +static void imx519_free_controls(struct imx519 *imx519)
2114 +{
2115 + v4l2_ctrl_handler_free(imx519->sd.ctrl_handler);
2116 + mutex_destroy(&imx519->mutex);
2117 +}
2118 +
2119 +static int imx519_check_hwcfg(struct device *dev)
2120 +{
2121 + struct fwnode_handle *endpoint;
2122 + struct v4l2_fwnode_endpoint ep_cfg = {
2123 + .bus_type = V4L2_MBUS_CSI2_DPHY
2124 + };
2125 + int ret = -EINVAL;
2126 +
2127 + endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
2128 + if (!endpoint) {
2129 + dev_err(dev, "endpoint node not found\n");
2130 + return -EINVAL;
2131 + }
2132 +
2133 + if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
2134 + dev_err(dev, "could not parse endpoint\n");
2135 + goto error_out;
2136 + }
2137 +
2138 + /* Check the number of MIPI CSI2 data lanes */
2139 + if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
2140 + dev_err(dev, "only 2 data lanes are currently supported\n");
2141 + goto error_out;
2142 + }
2143 +
2144 + /* Check the link frequency set in device tree */
2145 + if (!ep_cfg.nr_of_link_frequencies) {
2146 + dev_err(dev, "link-frequency property not found in DT\n");
2147 + goto error_out;
2148 + }
2149 +
2150 + if (ep_cfg.nr_of_link_frequencies != 1 ||
2151 + ep_cfg.link_frequencies[0] != IMX519_DEFAULT_LINK_FREQ) {
2152 + dev_err(dev, "Link frequency not supported: %lld\n",
2153 + ep_cfg.link_frequencies[0]);
2154 + goto error_out;
2155 + }
2156 +
2157 + ret = 0;
2158 +
2159 +error_out:
2160 + v4l2_fwnode_endpoint_free(&ep_cfg);
2161 + fwnode_handle_put(endpoint);
2162 +
2163 + return ret;
2164 +}
2165 +
2166 +static const struct of_device_id imx519_dt_ids[] = {
2167 + { .compatible = "sony,imx519"},
2168 + { /* sentinel */ }
2169 +};
2170 +
2171 +static int imx519_probe(struct i2c_client *client)
2172 +{
2173 + struct device *dev = &client->dev;
2174 + struct imx519 *imx519;
2175 + const struct of_device_id *match;
2176 + u32 xclk_freq;
2177 + int ret;
2178 +
2179 + imx519 = devm_kzalloc(&client->dev, sizeof(*imx519), GFP_KERNEL);
2180 + if (!imx519)
2181 + return -ENOMEM;
2182 +
2183 + v4l2_i2c_subdev_init(&imx519->sd, client, &imx519_subdev_ops);
2184 +
2185 + match = of_match_device(imx519_dt_ids, dev);
2186 + if (!match)
2187 + return -ENODEV;
2188 +
2189 + /* Check the hardware configuration in device tree */
2190 + if (imx519_check_hwcfg(dev))
2191 + return -EINVAL;
2192 +
2193 + /* Get system clock (xclk) */
2194 + imx519->xclk = devm_clk_get(dev, NULL);
2195 + if (IS_ERR(imx519->xclk)) {
2196 + dev_err(dev, "failed to get xclk\n");
2197 + return PTR_ERR(imx519->xclk);
2198 + }
2199 +
2200 + xclk_freq = clk_get_rate(imx519->xclk);
2201 + if (xclk_freq != IMX519_XCLK_FREQ) {
2202 + dev_err(dev, "xclk frequency not supported: %d Hz\n",
2203 + xclk_freq);
2204 + return -EINVAL;
2205 + }
2206 +
2207 + ret = imx519_get_regulators(imx519);
2208 + if (ret) {
2209 + dev_err(dev, "failed to get regulators\n");
2210 + return ret;
2211 + }
2212 +
2213 + /* Request optional enable pin */
2214 + imx519->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2215 + GPIOD_OUT_HIGH);
2216 +
2217 + /*
2218 + * The sensor must be powered for imx519_identify_module()
2219 + * to be able to read the CHIP_ID register
2220 + */
2221 + ret = imx519_power_on(dev);
2222 + if (ret)
2223 + return ret;
2224 +
2225 + ret = imx519_identify_module(imx519, IMX519_CHIP_ID);
2226 + if (ret)
2227 + goto error_power_off;
2228 +
2229 + /* Set default mode to max resolution */
2230 + imx519->mode = &supported_modes_10bit[0];
2231 + imx519->fmt_code = MEDIA_BUS_FMT_SRGGB10_1X10;
2232 +
2233 + /* Enable runtime PM and turn off the device */
2234 + pm_runtime_set_active(dev);
2235 + pm_runtime_enable(dev);
2236 + pm_runtime_idle(dev);
2237 +
2238 + /* This needs the pm runtime to be registered. */
2239 + ret = imx519_init_controls(imx519);
2240 + if (ret)
2241 + goto error_power_off;
2242 +
2243 + /* Initialize subdev */
2244 + imx519->sd.internal_ops = &imx519_internal_ops;
2245 + imx519->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2246 + V4L2_SUBDEV_FL_HAS_EVENTS;
2247 + imx519->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2248 +
2249 + /* Initialize source pads */
2250 + imx519->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
2251 + imx519->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
2252 +
2253 + ret = media_entity_pads_init(&imx519->sd.entity, NUM_PADS, imx519->pad);
2254 + if (ret) {
2255 + dev_err(dev, "failed to init entity pads: %d\n", ret);
2256 + goto error_handler_free;
2257 + }
2258 +
2259 + ret = v4l2_async_register_subdev_sensor(&imx519->sd);
2260 + if (ret < 0) {
2261 + dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
2262 + goto error_media_entity;
2263 + }
2264 +
2265 + return 0;
2266 +
2267 +error_media_entity:
2268 + media_entity_cleanup(&imx519->sd.entity);
2269 +
2270 +error_handler_free:
2271 + imx519_free_controls(imx519);
2272 +
2273 +error_power_off:
2274 + pm_runtime_disable(&client->dev);
2275 + pm_runtime_set_suspended(&client->dev);
2276 + imx519_power_off(&client->dev);
2277 +
2278 + return ret;
2279 +}
2280 +
2281 +static void imx519_remove(struct i2c_client *client)
2282 +{
2283 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
2284 + struct imx519 *imx519 = to_imx519(sd);
2285 +
2286 + v4l2_async_unregister_subdev(sd);
2287 + media_entity_cleanup(&sd->entity);
2288 + imx519_free_controls(imx519);
2289 +
2290 + pm_runtime_disable(&client->dev);
2291 + if (!pm_runtime_status_suspended(&client->dev))
2292 + imx519_power_off(&client->dev);
2293 + pm_runtime_set_suspended(&client->dev);
2294 +}
2295 +
2296 +MODULE_DEVICE_TABLE(of, imx519_dt_ids);
2297 +
2298 +static const struct dev_pm_ops imx519_pm_ops = {
2299 + SET_SYSTEM_SLEEP_PM_OPS(imx519_suspend, imx519_resume)
2300 + SET_RUNTIME_PM_OPS(imx519_power_off, imx519_power_on, NULL)
2301 +};
2302 +
2303 +static struct i2c_driver imx519_i2c_driver = {
2304 + .driver = {
2305 + .name = "imx519",
2306 + .of_match_table = imx519_dt_ids,
2307 + .pm = &imx519_pm_ops,
2308 + },
2309 + .probe_new = imx519_probe,
2310 + .remove = imx519_remove,
2311 +};
2312 +
2313 +module_i2c_driver(imx519_i2c_driver);
2314 +
2315 +MODULE_AUTHOR("Lee Jackson <info@arducam.com>");
2316 +MODULE_DESCRIPTION("Sony IMX519 sensor driver");
2317 +MODULE_LICENSE("GPL v2");