36893a99ea490df5ca04fda0e3f6d87ffbcde4a2
[openwrt/staging/dangole.git] /
1 From 606a02e0617f3adbcdd7aa82913771403d137269 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Tue, 23 Jun 2020 10:05:57 +0100
4 Subject: [PATCH] drivers: media: Remove the downstream version of
5 bcm2835-unicam
6
7 About to be replaced by the upstream version.
8
9 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
10 ---
11 drivers/media/platform/bcm2835/Kconfig | 14 -
12 drivers/media/platform/bcm2835/Makefile | 3 -
13 .../media/platform/bcm2835/bcm2835-unicam.c | 2873 -----------------
14 .../media/platform/bcm2835/vc4-regs-unicam.h | 253 --
15 4 files changed, 3143 deletions(-)
16 delete mode 100644 drivers/media/platform/bcm2835/Kconfig
17 delete mode 100644 drivers/media/platform/bcm2835/Makefile
18 delete mode 100644 drivers/media/platform/bcm2835/bcm2835-unicam.c
19 delete mode 100644 drivers/media/platform/bcm2835/vc4-regs-unicam.h
20
21 --- a/drivers/media/platform/bcm2835/Kconfig
22 +++ /dev/null
23 @@ -1,14 +0,0 @@
24 -# Broadcom VideoCore4 V4L2 camera support
25 -
26 -config VIDEO_BCM2835_UNICAM
27 - tristate "Broadcom BCM2835 Unicam video capture driver"
28 - depends on VIDEO_V4L2 && VIDEO_V4L2_SUBDEV_API && MEDIA_CONTROLLER
29 - depends on ARCH_BCM2835 || COMPILE_TEST
30 - select VIDEOBUF2_DMA_CONTIG
31 - select V4L2_FWNODE
32 - help
33 - Say Y here to enable V4L2 subdevice for CSI2 receiver.
34 - This is a V4L2 subdevice that interfaces directly to the VC4 peripheral.
35 -
36 - To compile this driver as a module, choose M here. The module
37 - will be called bcm2835-unicam.
38 --- a/drivers/media/platform/bcm2835/Makefile
39 +++ /dev/null
40 @@ -1,3 +0,0 @@
41 -# Makefile for BCM2835 Unicam driver
42 -
43 -obj-$(CONFIG_VIDEO_BCM2835_UNICAM) += bcm2835-unicam.o
44 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
45 +++ /dev/null
46 @@ -1,2873 +0,0 @@
47 -// SPDX-License-Identifier: GPL-2.0-only
48 -/*
49 - * BCM2835 Unicam Capture Driver
50 - *
51 - * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd.
52 - *
53 - * Dave Stevenson <dave.stevenson@raspberrypi.com>
54 - *
55 - * Based on TI am437x driver by
56 - * Benoit Parrot <bparrot@ti.com>
57 - * Lad, Prabhakar <prabhakar.csengg@gmail.com>
58 - *
59 - * and TI CAL camera interface driver by
60 - * Benoit Parrot <bparrot@ti.com>
61 - *
62 - *
63 - * There are two camera drivers in the kernel for BCM283x - this one
64 - * and bcm2835-camera (currently in staging).
65 - *
66 - * This driver directly controls the Unicam peripheral - there is no
67 - * involvement with the VideoCore firmware. Unicam receives CSI-2 or
68 - * CCP2 data and writes it into SDRAM.
69 - * The only potential processing options are to repack Bayer data into an
70 - * alternate format, and applying windowing.
71 - * The repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P
72 - * to V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12,
73 - * but not generically up to V4L2_PIX_FMT_Sxxxx16. The driver will add both
74 - * formats where the relevant formats are defined, and will automatically
75 - * configure the repacking as required.
76 - * Support for windowing may be added later.
77 - *
78 - * It should be possible to connect this driver to any sensor with a
79 - * suitable output interface and V4L2 subdevice driver.
80 - *
81 - * bcm2835-camera uses the VideoCore firmware to control the sensor,
82 - * Unicam, ISP, and all tuner control loops. Fully processed frames are
83 - * delivered to the driver by the firmware. It only has sensor drivers
84 - * for Omnivision OV5647, and Sony IMX219 sensors.
85 - *
86 - * The two drivers are mutually exclusive for the same Unicam instance.
87 - * The VideoCore firmware checks the device tree configuration during boot.
88 - * If it finds device tree nodes called csi0 or csi1 it will block the
89 - * firmware from accessing the peripheral, and bcm2835-camera will
90 - * not be able to stream data.
91 - */
92 -
93 -#include <linux/clk.h>
94 -#include <linux/delay.h>
95 -#include <linux/device.h>
96 -#include <linux/dma-mapping.h>
97 -#include <linux/err.h>
98 -#include <linux/init.h>
99 -#include <linux/interrupt.h>
100 -#include <linux/io.h>
101 -#include <linux/module.h>
102 -#include <linux/of_device.h>
103 -#include <linux/of_graph.h>
104 -#include <linux/pinctrl/consumer.h>
105 -#include <linux/platform_device.h>
106 -#include <linux/pm_runtime.h>
107 -#include <linux/slab.h>
108 -#include <linux/uaccess.h>
109 -#include <linux/videodev2.h>
110 -
111 -#include <media/v4l2-common.h>
112 -#include <media/v4l2-ctrls.h>
113 -#include <media/v4l2-dev.h>
114 -#include <media/v4l2-device.h>
115 -#include <media/v4l2-dv-timings.h>
116 -#include <media/v4l2-event.h>
117 -#include <media/v4l2-ioctl.h>
118 -#include <media/v4l2-fwnode.h>
119 -#include <media/videobuf2-dma-contig.h>
120 -
121 -#include "vc4-regs-unicam.h"
122 -
123 -#define UNICAM_MODULE_NAME "unicam"
124 -#define UNICAM_VERSION "0.1.0"
125 -
126 -static int debug;
127 -module_param(debug, int, 0644);
128 -MODULE_PARM_DESC(debug, "Debug level 0-3");
129 -
130 -#define unicam_dbg(level, dev, fmt, arg...) \
131 - v4l2_dbg(level, debug, &(dev)->v4l2_dev, fmt, ##arg)
132 -#define unicam_info(dev, fmt, arg...) \
133 - v4l2_info(&(dev)->v4l2_dev, fmt, ##arg)
134 -#define unicam_err(dev, fmt, arg...) \
135 - v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
136 -
137 -/* To protect against a dodgy sensor driver never returning an error from
138 - * enum_mbus_code, set a maximum index value to be used.
139 - */
140 -#define MAX_ENUM_MBUS_CODE 128
141 -
142 -/*
143 - * Stride is a 16 bit register, but also has to be a multiple of 32.
144 - */
145 -#define BPL_ALIGNMENT 32
146 -#define MAX_BYTESPERLINE ((1 << 16) - BPL_ALIGNMENT)
147 -/*
148 - * Max width is therefore determined by the max stride divided by
149 - * the number of bits per pixel. Take 32bpp as a
150 - * worst case.
151 - * No imposed limit on the height, so adopt a square image for want
152 - * of anything better.
153 - */
154 -#define MAX_WIDTH (MAX_BYTESPERLINE / 4)
155 -#define MAX_HEIGHT MAX_WIDTH
156 -/* Define a nominal minimum image size */
157 -#define MIN_WIDTH 16
158 -#define MIN_HEIGHT 16
159 -/* Default size of the embedded buffer */
160 -#define UNICAM_EMBEDDED_SIZE 8192
161 -
162 -/*
163 - * Size of the dummy buffer. Can be any size really, but the DMA
164 - * allocation works in units of page sizes.
165 - */
166 -#define DUMMY_BUF_SIZE (PAGE_SIZE)
167 -
168 -enum pad_types {
169 - IMAGE_PAD,
170 - METADATA_PAD,
171 - MAX_NODES
172 -};
173 -
174 -/*
175 - * struct unicam_fmt - Unicam media bus format information
176 - * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
177 - * @repacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded
178 - * out to 16bpp. 0 if n/a.
179 - * @code: V4L2 media bus format code.
180 - * @depth: Bits per pixel as delivered from the source.
181 - * @csi_dt: CSI data type.
182 - * @check_variants: Flag to denote that there are multiple mediabus formats
183 - * still in the list that could match this V4L2 format.
184 - */
185 -struct unicam_fmt {
186 - u32 fourcc;
187 - u32 repacked_fourcc;
188 - u32 code;
189 - u8 depth;
190 - u8 csi_dt;
191 - u8 check_variants;
192 -};
193 -
194 -static const struct unicam_fmt formats[] = {
195 - /* YUV Formats */
196 - {
197 - .fourcc = V4L2_PIX_FMT_YUYV,
198 - .code = MEDIA_BUS_FMT_YUYV8_2X8,
199 - .depth = 16,
200 - .csi_dt = 0x1e,
201 - .check_variants = 1,
202 - }, {
203 - .fourcc = V4L2_PIX_FMT_UYVY,
204 - .code = MEDIA_BUS_FMT_UYVY8_2X8,
205 - .depth = 16,
206 - .csi_dt = 0x1e,
207 - .check_variants = 1,
208 - }, {
209 - .fourcc = V4L2_PIX_FMT_YVYU,
210 - .code = MEDIA_BUS_FMT_YVYU8_2X8,
211 - .depth = 16,
212 - .csi_dt = 0x1e,
213 - .check_variants = 1,
214 - }, {
215 - .fourcc = V4L2_PIX_FMT_VYUY,
216 - .code = MEDIA_BUS_FMT_VYUY8_2X8,
217 - .depth = 16,
218 - .csi_dt = 0x1e,
219 - .check_variants = 1,
220 - }, {
221 - .fourcc = V4L2_PIX_FMT_YUYV,
222 - .code = MEDIA_BUS_FMT_YUYV8_1X16,
223 - .depth = 16,
224 - .csi_dt = 0x1e,
225 - }, {
226 - .fourcc = V4L2_PIX_FMT_UYVY,
227 - .code = MEDIA_BUS_FMT_UYVY8_1X16,
228 - .depth = 16,
229 - .csi_dt = 0x1e,
230 - }, {
231 - .fourcc = V4L2_PIX_FMT_YVYU,
232 - .code = MEDIA_BUS_FMT_YVYU8_1X16,
233 - .depth = 16,
234 - .csi_dt = 0x1e,
235 - }, {
236 - .fourcc = V4L2_PIX_FMT_VYUY,
237 - .code = MEDIA_BUS_FMT_VYUY8_1X16,
238 - .depth = 16,
239 - .csi_dt = 0x1e,
240 - }, {
241 - /* RGB Formats */
242 - .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
243 - .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
244 - .depth = 16,
245 - .csi_dt = 0x22,
246 - }, {
247 - .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
248 - .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
249 - .depth = 16,
250 - .csi_dt = 0x22
251 - }, {
252 - .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
253 - .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
254 - .depth = 16,
255 - .csi_dt = 0x21,
256 - }, {
257 - .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
258 - .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
259 - .depth = 16,
260 - .csi_dt = 0x21,
261 - }, {
262 - .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
263 - .code = MEDIA_BUS_FMT_RGB888_1X24,
264 - .depth = 24,
265 - .csi_dt = 0x24,
266 - }, {
267 - .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
268 - .code = MEDIA_BUS_FMT_BGR888_1X24,
269 - .depth = 24,
270 - .csi_dt = 0x24,
271 - }, {
272 - .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
273 - .code = MEDIA_BUS_FMT_ARGB8888_1X32,
274 - .depth = 32,
275 - .csi_dt = 0x0,
276 - }, {
277 - /* Bayer Formats */
278 - .fourcc = V4L2_PIX_FMT_SBGGR8,
279 - .code = MEDIA_BUS_FMT_SBGGR8_1X8,
280 - .depth = 8,
281 - .csi_dt = 0x2a,
282 - }, {
283 - .fourcc = V4L2_PIX_FMT_SGBRG8,
284 - .code = MEDIA_BUS_FMT_SGBRG8_1X8,
285 - .depth = 8,
286 - .csi_dt = 0x2a,
287 - }, {
288 - .fourcc = V4L2_PIX_FMT_SGRBG8,
289 - .code = MEDIA_BUS_FMT_SGRBG8_1X8,
290 - .depth = 8,
291 - .csi_dt = 0x2a,
292 - }, {
293 - .fourcc = V4L2_PIX_FMT_SRGGB8,
294 - .code = MEDIA_BUS_FMT_SRGGB8_1X8,
295 - .depth = 8,
296 - .csi_dt = 0x2a,
297 - }, {
298 - .fourcc = V4L2_PIX_FMT_SBGGR10P,
299 - .repacked_fourcc = V4L2_PIX_FMT_SBGGR10,
300 - .code = MEDIA_BUS_FMT_SBGGR10_1X10,
301 - .depth = 10,
302 - .csi_dt = 0x2b,
303 - }, {
304 - .fourcc = V4L2_PIX_FMT_SGBRG10P,
305 - .repacked_fourcc = V4L2_PIX_FMT_SGBRG10,
306 - .code = MEDIA_BUS_FMT_SGBRG10_1X10,
307 - .depth = 10,
308 - .csi_dt = 0x2b,
309 - }, {
310 - .fourcc = V4L2_PIX_FMT_SGRBG10P,
311 - .repacked_fourcc = V4L2_PIX_FMT_SGRBG10,
312 - .code = MEDIA_BUS_FMT_SGRBG10_1X10,
313 - .depth = 10,
314 - .csi_dt = 0x2b,
315 - }, {
316 - .fourcc = V4L2_PIX_FMT_SRGGB10P,
317 - .repacked_fourcc = V4L2_PIX_FMT_SRGGB10,
318 - .code = MEDIA_BUS_FMT_SRGGB10_1X10,
319 - .depth = 10,
320 - .csi_dt = 0x2b,
321 - }, {
322 - .fourcc = V4L2_PIX_FMT_SBGGR12P,
323 - .repacked_fourcc = V4L2_PIX_FMT_SBGGR12,
324 - .code = MEDIA_BUS_FMT_SBGGR12_1X12,
325 - .depth = 12,
326 - .csi_dt = 0x2c,
327 - }, {
328 - .fourcc = V4L2_PIX_FMT_SGBRG12P,
329 - .repacked_fourcc = V4L2_PIX_FMT_SGBRG12,
330 - .code = MEDIA_BUS_FMT_SGBRG12_1X12,
331 - .depth = 12,
332 - .csi_dt = 0x2c,
333 - }, {
334 - .fourcc = V4L2_PIX_FMT_SGRBG12P,
335 - .repacked_fourcc = V4L2_PIX_FMT_SGRBG12,
336 - .code = MEDIA_BUS_FMT_SGRBG12_1X12,
337 - .depth = 12,
338 - .csi_dt = 0x2c,
339 - }, {
340 - .fourcc = V4L2_PIX_FMT_SRGGB12P,
341 - .repacked_fourcc = V4L2_PIX_FMT_SRGGB12,
342 - .code = MEDIA_BUS_FMT_SRGGB12_1X12,
343 - .depth = 12,
344 - .csi_dt = 0x2c,
345 - }, {
346 - .fourcc = V4L2_PIX_FMT_SBGGR14P,
347 - .code = MEDIA_BUS_FMT_SBGGR14_1X14,
348 - .depth = 14,
349 - .csi_dt = 0x2d,
350 - }, {
351 - .fourcc = V4L2_PIX_FMT_SGBRG14P,
352 - .code = MEDIA_BUS_FMT_SGBRG14_1X14,
353 - .depth = 14,
354 - .csi_dt = 0x2d,
355 - }, {
356 - .fourcc = V4L2_PIX_FMT_SGRBG14P,
357 - .code = MEDIA_BUS_FMT_SGRBG14_1X14,
358 - .depth = 14,
359 - .csi_dt = 0x2d,
360 - }, {
361 - .fourcc = V4L2_PIX_FMT_SRGGB14P,
362 - .code = MEDIA_BUS_FMT_SRGGB14_1X14,
363 - .depth = 14,
364 - .csi_dt = 0x2d,
365 - }, {
366 - /*
367 - * 16 bit Bayer formats could be supported, but there is no CSI2
368 - * data_type defined for raw 16, and no sensors that produce it at
369 - * present.
370 - */
371 -
372 - /* Greyscale formats */
373 - .fourcc = V4L2_PIX_FMT_GREY,
374 - .code = MEDIA_BUS_FMT_Y8_1X8,
375 - .depth = 8,
376 - .csi_dt = 0x2a,
377 - }, {
378 - .fourcc = V4L2_PIX_FMT_Y10P,
379 - .repacked_fourcc = V4L2_PIX_FMT_Y10,
380 - .code = MEDIA_BUS_FMT_Y10_1X10,
381 - .depth = 10,
382 - .csi_dt = 0x2b,
383 - }, {
384 - /* NB There is no packed V4L2 fourcc for this format. */
385 - .repacked_fourcc = V4L2_PIX_FMT_Y12,
386 - .code = MEDIA_BUS_FMT_Y12_1X12,
387 - .depth = 12,
388 - .csi_dt = 0x2c,
389 - },
390 - /* Embedded data format */
391 - {
392 - .fourcc = V4L2_META_FMT_SENSOR_DATA,
393 - .code = MEDIA_BUS_FMT_SENSOR_DATA,
394 - .depth = 8,
395 - }
396 -};
397 -
398 -struct unicam_dmaqueue {
399 - struct list_head active;
400 -};
401 -
402 -struct unicam_buffer {
403 - struct vb2_v4l2_buffer vb;
404 - struct list_head list;
405 -};
406 -
407 -struct unicam_cfg {
408 - /* peripheral base address */
409 - void __iomem *base;
410 - /* clock gating base address */
411 - void __iomem *clk_gate_base;
412 -};
413 -
414 -#define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
415 -
416 -struct unicam_node {
417 - int registered;
418 - int open;
419 - int streaming;
420 - unsigned int pad_id;
421 - /* Pointer pointing to current v4l2_buffer */
422 - struct unicam_buffer *cur_frm;
423 - /* Pointer pointing to next v4l2_buffer */
424 - struct unicam_buffer *next_frm;
425 - /* video capture */
426 - const struct unicam_fmt *fmt;
427 - /* Used to store current pixel format */
428 - struct v4l2_format v_fmt;
429 - /* Used to store current mbus frame format */
430 - struct v4l2_mbus_framefmt m_fmt;
431 - /* Buffer queue used in video-buf */
432 - struct vb2_queue buffer_queue;
433 - /* Queue of filled frames */
434 - struct unicam_dmaqueue dma_queue;
435 - /* IRQ lock for DMA queue */
436 - spinlock_t dma_queue_lock;
437 - /* lock used to access this structure */
438 - struct mutex lock;
439 - /* Identifies video device for this channel */
440 - struct video_device video_dev;
441 - /* Pointer to the parent handle */
442 - struct unicam_device *dev;
443 - struct media_pad pad;
444 - struct v4l2_ctrl_handler ctrl_handler;
445 - unsigned int embedded_lines;
446 - /*
447 - * Dummy buffer intended to be used by unicam
448 - * if we have no other queued buffers to swap to.
449 - */
450 - void *dummy_buf_cpu_addr;
451 - dma_addr_t dummy_buf_dma_addr;
452 -};
453 -
454 -struct unicam_device {
455 - /* V4l2 specific parameters */
456 -
457 - struct v4l2_fwnode_endpoint endpoint;
458 -
459 - struct v4l2_async_subdev asd;
460 -
461 - /* unicam cfg */
462 - struct unicam_cfg cfg;
463 - /* clock handle */
464 - struct clk *clock;
465 - /* V4l2 device */
466 - struct v4l2_device v4l2_dev;
467 - struct media_device mdev;
468 -
469 - /* parent device */
470 - struct platform_device *pdev;
471 - /* subdevice async Notifier */
472 - struct v4l2_async_notifier notifier;
473 - unsigned int sequence;
474 -
475 - /* ptr to sub device */
476 - struct v4l2_subdev *sensor;
477 - /* Pad config for the sensor */
478 - struct v4l2_subdev_pad_config *sensor_config;
479 -
480 - unsigned int virtual_channel;
481 - enum v4l2_mbus_type bus_type;
482 - /*
483 - * Stores bus.mipi_csi2.flags for CSI2 sensors, or
484 - * bus.mipi_csi1.strobe for CCP2.
485 - */
486 - unsigned int bus_flags;
487 - unsigned int max_data_lanes;
488 - unsigned int active_data_lanes;
489 - bool sensor_embedded_data;
490 -
491 - struct unicam_node node[MAX_NODES];
492 -};
493 -
494 -/* Hardware access */
495 -#define clk_write(dev, val) writel((val) | 0x5a000000, (dev)->clk_gate_base)
496 -#define clk_read(dev) readl((dev)->clk_gate_base)
497 -
498 -#define reg_read(dev, offset) readl((dev)->base + (offset))
499 -#define reg_write(dev, offset, val) writel(val, (dev)->base + (offset))
500 -
501 -#define reg_read_field(dev, offset, mask) get_field(reg_read((dev), (offset), \
502 - mask))
503 -
504 -static inline int get_field(u32 value, u32 mask)
505 -{
506 - return (value & mask) >> __ffs(mask);
507 -}
508 -
509 -static inline void set_field(u32 *valp, u32 field, u32 mask)
510 -{
511 - u32 val = *valp;
512 -
513 - val &= ~mask;
514 - val |= (field << __ffs(mask)) & mask;
515 - *valp = val;
516 -}
517 -
518 -static inline void reg_write_field(struct unicam_cfg *dev, u32 offset,
519 - u32 field, u32 mask)
520 -{
521 - u32 val = reg_read((dev), (offset));
522 -
523 - set_field(&val, field, mask);
524 - reg_write((dev), (offset), val);
525 -}
526 -
527 -/* Power management functions */
528 -static inline int unicam_runtime_get(struct unicam_device *dev)
529 -{
530 - return pm_runtime_get_sync(&dev->pdev->dev);
531 -}
532 -
533 -static inline void unicam_runtime_put(struct unicam_device *dev)
534 -{
535 - pm_runtime_put_sync(&dev->pdev->dev);
536 -}
537 -
538 -/* Format setup functions */
539 -static const struct unicam_fmt *find_format_by_code(u32 code)
540 -{
541 - unsigned int i;
542 -
543 - for (i = 0; i < ARRAY_SIZE(formats); i++) {
544 - if (formats[i].code == code)
545 - return &formats[i];
546 - }
547 -
548 - return NULL;
549 -}
550 -
551 -static int check_mbus_format(struct unicam_device *dev,
552 - const struct unicam_fmt *format)
553 -{
554 - struct v4l2_subdev_mbus_code_enum mbus_code;
555 - int ret = 0;
556 - int i;
557 -
558 - for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
559 - memset(&mbus_code, 0, sizeof(mbus_code));
560 - mbus_code.index = i;
561 - mbus_code.pad = IMAGE_PAD;
562 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
563 -
564 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
565 - NULL, &mbus_code);
566 -
567 - if (!ret && mbus_code.code == format->code)
568 - return 1;
569 - }
570 -
571 - return 0;
572 -}
573 -
574 -static const struct unicam_fmt *find_format_by_pix(struct unicam_device *dev,
575 - u32 pixelformat)
576 -{
577 - unsigned int i;
578 -
579 - for (i = 0; i < ARRAY_SIZE(formats); i++) {
580 - if (formats[i].fourcc == pixelformat ||
581 - formats[i].repacked_fourcc == pixelformat) {
582 - if (formats[i].check_variants &&
583 - !check_mbus_format(dev, &formats[i]))
584 - continue;
585 - return &formats[i];
586 - }
587 - }
588 -
589 - return NULL;
590 -}
591 -
592 -static inline unsigned int bytes_per_line(u32 width,
593 - const struct unicam_fmt *fmt,
594 - u32 v4l2_fourcc)
595 -{
596 - if (v4l2_fourcc == fmt->repacked_fourcc)
597 - /* Repacking always goes to 16bpp */
598 - return ALIGN(width << 1, BPL_ALIGNMENT);
599 - else
600 - return ALIGN((width * fmt->depth) >> 3, BPL_ALIGNMENT);
601 -}
602 -
603 -static int __subdev_get_format(struct unicam_device *dev,
604 - struct v4l2_mbus_framefmt *fmt, int pad_id)
605 -{
606 - struct v4l2_subdev_format sd_fmt = {
607 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
608 - .pad = pad_id
609 - };
610 - int ret;
611 -
612 - ret = v4l2_subdev_call(dev->sensor, pad, get_fmt, dev->sensor_config,
613 - &sd_fmt);
614 - if (ret < 0)
615 - return ret;
616 -
617 - *fmt = sd_fmt.format;
618 -
619 - unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__,
620 - fmt->width, fmt->height, fmt->code);
621 -
622 - return 0;
623 -}
624 -
625 -static int __subdev_set_format(struct unicam_device *dev,
626 - struct v4l2_mbus_framefmt *fmt, int pad_id)
627 -{
628 - struct v4l2_subdev_format sd_fmt = {
629 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
630 - .pad = pad_id
631 - };
632 - int ret;
633 -
634 - sd_fmt.format = *fmt;
635 -
636 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
637 - &sd_fmt);
638 - if (ret < 0)
639 - return ret;
640 -
641 - if (pad_id == IMAGE_PAD)
642 - unicam_dbg(1, dev, "%s %dx%d code:%04x\n", __func__, fmt->width,
643 - fmt->height, fmt->code);
644 - else
645 - unicam_dbg(1, dev, "%s Embedded data code:%04x\n", __func__,
646 - sd_fmt.format.code);
647 -
648 - return 0;
649 -}
650 -
651 -static int unicam_calc_format_size_bpl(struct unicam_device *dev,
652 - const struct unicam_fmt *fmt,
653 - struct v4l2_format *f)
654 -{
655 - unsigned int min_bytesperline;
656 -
657 - v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 2,
658 - &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 0,
659 - 0);
660 -
661 - min_bytesperline = bytes_per_line(f->fmt.pix.width, fmt,
662 - f->fmt.pix.pixelformat);
663 -
664 - if (f->fmt.pix.bytesperline > min_bytesperline &&
665 - f->fmt.pix.bytesperline <= MAX_BYTESPERLINE)
666 - f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
667 - BPL_ALIGNMENT);
668 - else
669 - f->fmt.pix.bytesperline = min_bytesperline;
670 -
671 - f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
672 -
673 - unicam_dbg(3, dev, "%s: fourcc: %08X size: %dx%d bpl:%d img_size:%d\n",
674 - __func__,
675 - f->fmt.pix.pixelformat,
676 - f->fmt.pix.width, f->fmt.pix.height,
677 - f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
678 -
679 - return 0;
680 -}
681 -
682 -static int unicam_reset_format(struct unicam_node *node)
683 -{
684 - struct unicam_device *dev = node->dev;
685 - struct v4l2_mbus_framefmt mbus_fmt;
686 - int ret;
687 -
688 - if (dev->sensor_embedded_data || node->pad_id != METADATA_PAD) {
689 - ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
690 - if (ret) {
691 - unicam_err(dev, "Failed to get_format - ret %d\n", ret);
692 - return ret;
693 - }
694 -
695 - if (mbus_fmt.code != node->fmt->code) {
696 - unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
697 - node->fmt->code, mbus_fmt.code);
698 - return ret;
699 - }
700 - }
701 -
702 - if (node->pad_id == IMAGE_PAD) {
703 - v4l2_fill_pix_format(&node->v_fmt.fmt.pix, &mbus_fmt);
704 - node->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
705 - unicam_calc_format_size_bpl(dev, node->fmt, &node->v_fmt);
706 - } else {
707 - node->v_fmt.type = V4L2_BUF_TYPE_META_CAPTURE;
708 - node->v_fmt.fmt.meta.dataformat = V4L2_META_FMT_SENSOR_DATA;
709 - if (dev->sensor_embedded_data) {
710 - node->v_fmt.fmt.meta.buffersize =
711 - mbus_fmt.width * mbus_fmt.height;
712 - node->embedded_lines = mbus_fmt.height;
713 - } else {
714 - node->v_fmt.fmt.meta.buffersize = UNICAM_EMBEDDED_SIZE;
715 - node->embedded_lines = 1;
716 - }
717 - }
718 -
719 - node->m_fmt = mbus_fmt;
720 - return 0;
721 -}
722 -
723 -static void unicam_wr_dma_addr(struct unicam_cfg *cfg, dma_addr_t dmaaddr,
724 - unsigned int buffer_size, int pad_id)
725 -{
726 - dma_addr_t endaddr = dmaaddr + buffer_size;
727 -
728 - /*
729 - * dmaaddr and endaddr should be a 32-bit address with the top two bits
730 - * set to 0x3 to signify uncached access through the Videocore memory
731 - * controller.
732 - */
733 - BUG_ON((dmaaddr >> 30) != 0x3 && (endaddr >> 30) != 0x3);
734 -
735 - if (pad_id == IMAGE_PAD) {
736 - reg_write(cfg, UNICAM_IBSA0, dmaaddr);
737 - reg_write(cfg, UNICAM_IBEA0, endaddr);
738 - } else {
739 - reg_write(cfg, UNICAM_DBSA0, dmaaddr);
740 - reg_write(cfg, UNICAM_DBEA0, endaddr);
741 - }
742 -}
743 -
744 -static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
745 -{
746 - dma_addr_t start_addr, cur_addr;
747 - unsigned int stride = dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline;
748 - struct unicam_buffer *frm = dev->node[IMAGE_PAD].cur_frm;
749 -
750 - if (!frm)
751 - return 0;
752 -
753 - start_addr = vb2_dma_contig_plane_dma_addr(&frm->vb.vb2_buf, 0);
754 - cur_addr = reg_read(&dev->cfg, UNICAM_IBWP);
755 - return (unsigned int)(cur_addr - start_addr) / stride;
756 -}
757 -
758 -static inline void unicam_schedule_next_buffer(struct unicam_node *node)
759 -{
760 - struct unicam_device *dev = node->dev;
761 - struct unicam_dmaqueue *dma_q = &node->dma_queue;
762 - struct unicam_buffer *buf;
763 - unsigned int size;
764 - dma_addr_t addr;
765 -
766 - buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
767 - node->next_frm = buf;
768 - list_del(&buf->list);
769 -
770 - addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
771 - size = (node->pad_id == IMAGE_PAD) ?
772 - dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage :
773 - dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
774 -
775 - unicam_wr_dma_addr(&dev->cfg, addr, size, node->pad_id);
776 -}
777 -
778 -static inline void unicam_schedule_dummy_buffer(struct unicam_node *node)
779 -{
780 - struct unicam_device *dev = node->dev;
781 - dma_addr_t addr = node->dummy_buf_dma_addr;
782 -
783 - unicam_dbg(3, dev, "Scheduling dummy buffer for node %d\n",
784 - node->pad_id);
785 -
786 - unicam_wr_dma_addr(&dev->cfg, addr, DUMMY_BUF_SIZE, node->pad_id);
787 - node->next_frm = NULL;
788 -}
789 -
790 -static inline void unicam_process_buffer_complete(struct unicam_node *node,
791 - unsigned int sequence)
792 -{
793 - node->cur_frm->vb.field = node->m_fmt.field;
794 - node->cur_frm->vb.sequence = sequence;
795 -
796 - vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
797 -}
798 -
799 -static int unicam_num_nodes_streaming(struct unicam_device *dev)
800 -{
801 - return dev->node[IMAGE_PAD].streaming +
802 - dev->node[METADATA_PAD].streaming;
803 -}
804 -
805 -static int unicam_all_nodes_streaming(struct unicam_device *dev)
806 -{
807 - int ret;
808 -
809 - ret = dev->node[IMAGE_PAD].open && dev->node[IMAGE_PAD].streaming;
810 - ret &= !dev->node[METADATA_PAD].open ||
811 - dev->node[METADATA_PAD].streaming;
812 - return ret;
813 -}
814 -
815 -static void unicam_queue_event_sof(struct unicam_device *unicam)
816 -{
817 - struct v4l2_event event = {
818 - .type = V4L2_EVENT_FRAME_SYNC,
819 - .u.frame_sync.frame_sequence = unicam->sequence,
820 - };
821 -
822 - v4l2_event_queue(&unicam->node[IMAGE_PAD].video_dev, &event);
823 -}
824 -
825 -/*
826 - * unicam_isr : ISR handler for unicam capture
827 - * @irq: irq number
828 - * @dev_id: dev_id ptr
829 - *
830 - * It changes status of the captured buffer, takes next buffer from the queue
831 - * and sets its address in unicam registers
832 - */
833 -static irqreturn_t unicam_isr(int irq, void *dev)
834 -{
835 - struct unicam_device *unicam = (struct unicam_device *)dev;
836 - struct unicam_cfg *cfg = &unicam->cfg;
837 - unsigned int lines_done = unicam_get_lines_done(dev);
838 - unsigned int sequence = unicam->sequence;
839 - int num_nodes_streaming = unicam_num_nodes_streaming(dev);
840 - int ista, sta;
841 - u64 ts;
842 - int i;
843 -
844 - sta = reg_read(cfg, UNICAM_STA);
845 - /* Write value back to clear the interrupts */
846 - reg_write(cfg, UNICAM_STA, sta);
847 -
848 - ista = reg_read(cfg, UNICAM_ISTA);
849 - /* Write value back to clear the interrupts */
850 - reg_write(cfg, UNICAM_ISTA, ista);
851 -
852 - unicam_dbg(3, unicam, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d",
853 - ista, sta, sequence, lines_done);
854 -
855 - if (!(sta && (UNICAM_IS | UNICAM_PI0)))
856 - return IRQ_HANDLED;
857 -
858 - /*
859 - * We must run the frame end handler first. If we have a valid next_frm
860 - * and we get a simultaneout FE + FS interrupt, running the FS handler
861 - * first would null out the next_frm ptr and we would have lost the
862 - * buffer forever.
863 - */
864 - if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
865 - /*
866 - * Ensure we have swapped buffers already as we can't
867 - * stop the peripheral. If no buffer is available, use a
868 - * dummy buffer to dump out frames until we get a new buffer
869 - * to use.
870 - */
871 - for (i = 0; i < num_nodes_streaming; i++) {
872 - if (unicam->node[i].cur_frm)
873 - unicam_process_buffer_complete(&unicam->node[i],
874 - sequence);
875 - unicam->node[i].cur_frm = unicam->node[i].next_frm;
876 - }
877 - unicam->sequence++;
878 - }
879 -
880 - if (ista & UNICAM_FSI) {
881 - /*
882 - * Timestamp is to be when the first data byte was captured,
883 - * aka frame start.
884 - */
885 - ts = ktime_get_ns();
886 - for (i = 0; i < num_nodes_streaming; i++) {
887 - if (unicam->node[i].cur_frm)
888 - unicam->node[i].cur_frm->vb.vb2_buf.timestamp =
889 - ts;
890 - /*
891 - * Set the next frame output to go to a dummy frame
892 - * if we have not managed to obtain another frame
893 - * from the queue.
894 - */
895 - unicam_schedule_dummy_buffer(&unicam->node[i]);
896 - }
897 -
898 - unicam_queue_event_sof(unicam);
899 - }
900 - /*
901 - * Cannot swap buffer at frame end, there may be a race condition
902 - * where the HW does not actually swap it if the new frame has
903 - * already started.
904 - */
905 - if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
906 - for (i = 0; i < num_nodes_streaming; i++) {
907 - spin_lock(&unicam->node[i].dma_queue_lock);
908 - if (!list_empty(&unicam->node[i].dma_queue.active) &&
909 - !unicam->node[i].next_frm)
910 - unicam_schedule_next_buffer(&unicam->node[i]);
911 - spin_unlock(&unicam->node[i].dma_queue_lock);
912 - }
913 - }
914 -
915 - if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
916 - /* Switch out of trigger mode if selected */
917 - reg_write_field(&unicam->cfg, UNICAM_ICTL, 1, UNICAM_TFC);
918 - reg_write_field(&unicam->cfg, UNICAM_ICTL, 0, UNICAM_FCM);
919 - }
920 - return IRQ_HANDLED;
921 -}
922 -
923 -static int unicam_querycap(struct file *file, void *priv,
924 - struct v4l2_capability *cap)
925 -{
926 - struct unicam_node *node = video_drvdata(file);
927 - struct unicam_device *dev = node->dev;
928 -
929 - strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
930 - strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
931 -
932 - snprintf(cap->bus_info, sizeof(cap->bus_info),
933 - "platform:%s", dev->v4l2_dev.name);
934 -
935 - cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
936 - V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS |
937 - V4L2_CAP_META_CAPTURE;
938 -
939 - if (node->pad_id == IMAGE_PAD)
940 - cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
941 - else
942 - cap->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING;
943 -
944 - return 0;
945 -}
946 -
947 -static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
948 - struct v4l2_fmtdesc *f)
949 -{
950 - struct unicam_node *node = video_drvdata(file);
951 - struct unicam_device *dev = node->dev;
952 - struct v4l2_subdev_mbus_code_enum mbus_code;
953 - const struct unicam_fmt *fmt = NULL;
954 - int index = 0;
955 - int ret = 0;
956 - int i;
957 -
958 - if (node->pad_id == METADATA_PAD)
959 - return -EINVAL;
960 -
961 - for (i = 0; !ret && i < MAX_ENUM_MBUS_CODE; i++) {
962 - memset(&mbus_code, 0, sizeof(mbus_code));
963 - mbus_code.index = i;
964 - mbus_code.pad = IMAGE_PAD;
965 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
966 -
967 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code,
968 - NULL, &mbus_code);
969 - if (ret < 0) {
970 - unicam_dbg(2, dev,
971 - "subdev->enum_mbus_code idx %d returned %d - index invalid\n",
972 - i, ret);
973 - return -EINVAL;
974 - }
975 -
976 - fmt = find_format_by_code(mbus_code.code);
977 - if (fmt) {
978 - if (fmt->fourcc) {
979 - if (index == f->index) {
980 - f->pixelformat = fmt->fourcc;
981 - break;
982 - }
983 - index++;
984 - }
985 - if (fmt->repacked_fourcc) {
986 - if (index == f->index) {
987 - f->pixelformat = fmt->repacked_fourcc;
988 - break;
989 - }
990 - index++;
991 - }
992 - }
993 - }
994 -
995 - return 0;
996 -}
997 -
998 -static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
999 - struct v4l2_format *f)
1000 -{
1001 - struct v4l2_mbus_framefmt mbus_fmt = {0};
1002 - struct unicam_node *node = video_drvdata(file);
1003 - struct unicam_device *dev = node->dev;
1004 - const struct unicam_fmt *fmt = NULL;
1005 - int ret;
1006 -
1007 - if (node->pad_id != IMAGE_PAD)
1008 - return -EINVAL;
1009 -
1010 - /*
1011 - * If a flip has occurred in the sensor, the fmt code might have
1012 - * changed. So we will need to re-fetch the format from the subdevice.
1013 - */
1014 - ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
1015 - if (ret)
1016 - return -EINVAL;
1017 -
1018 - /* Find the V4L2 format from mbus code. We must match a known format. */
1019 - fmt = find_format_by_code(mbus_fmt.code);
1020 - if (!fmt)
1021 - return -EINVAL;
1022 -
1023 - if (node->fmt != fmt) {
1024 - /*
1025 - * The sensor format has changed so the pixelformat needs to
1026 - * be updated. Try and retain the packed/unpacked choice if
1027 - * at all possible.
1028 - */
1029 - if (node->fmt->repacked_fourcc ==
1030 - node->v_fmt.fmt.pix.pixelformat)
1031 - /* Using the repacked format */
1032 - node->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
1033 - else
1034 - /* Using the native format */
1035 - node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
1036 -
1037 - node->fmt = fmt;
1038 - }
1039 -
1040 - *f = node->v_fmt;
1041 -
1042 - return 0;
1043 -}
1044 -
1045 -static
1046 -const struct unicam_fmt *get_first_supported_format(struct unicam_device *dev)
1047 -{
1048 - struct v4l2_subdev_mbus_code_enum mbus_code;
1049 - const struct unicam_fmt *fmt = NULL;
1050 - int ret = 0;
1051 - int j;
1052 -
1053 - for (j = 0; ret != -EINVAL && ret != -ENOIOCTLCMD; ++j) {
1054 - memset(&mbus_code, 0, sizeof(mbus_code));
1055 - mbus_code.index = j;
1056 - mbus_code.pad = IMAGE_PAD;
1057 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1058 -
1059 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1060 - &mbus_code);
1061 - if (ret < 0) {
1062 - unicam_dbg(2, dev,
1063 - "subdev->enum_mbus_code idx %d returned %d - continue\n",
1064 - j, ret);
1065 - continue;
1066 - }
1067 -
1068 - unicam_dbg(2, dev, "subdev %s: code: 0x%08x idx: %d\n",
1069 - dev->sensor->name, mbus_code.code, j);
1070 -
1071 - fmt = find_format_by_code(mbus_code.code);
1072 - unicam_dbg(2, dev, "fmt 0x%08x returned as %p, V4L2 FOURCC 0x%08x, csi_dt 0x%02x\n",
1073 - mbus_code.code, fmt, fmt ? fmt->fourcc : 0,
1074 - fmt ? fmt->csi_dt : 0);
1075 - if (fmt)
1076 - return fmt;
1077 - }
1078 -
1079 - return NULL;
1080 -}
1081 -
1082 -static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
1083 - struct v4l2_format *f)
1084 -{
1085 - struct unicam_node *node = video_drvdata(file);
1086 - struct unicam_device *dev = node->dev;
1087 - struct v4l2_subdev_format sd_fmt = {
1088 - .which = V4L2_SUBDEV_FORMAT_TRY,
1089 - .pad = IMAGE_PAD
1090 - };
1091 - struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1092 - const struct unicam_fmt *fmt;
1093 - int ret;
1094 -
1095 - if (node->pad_id == METADATA_PAD)
1096 - return -EINVAL;
1097 -
1098 - fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1099 - if (!fmt) {
1100 - /* Pixel format not supported by unicam. Choose the first
1101 - * supported format, and let the sensor choose something else.
1102 - */
1103 - unicam_dbg(3, dev, "Fourcc format (0x%08x) not found. Use first format.\n",
1104 - f->fmt.pix.pixelformat);
1105 -
1106 - fmt = &formats[0];
1107 - f->fmt.pix.pixelformat = fmt->fourcc;
1108 - }
1109 -
1110 - v4l2_fill_mbus_format(mbus_fmt, &f->fmt.pix, fmt->code);
1111 - /*
1112 - * No support for receiving interlaced video, so never
1113 - * request it from the sensor subdev.
1114 - */
1115 - mbus_fmt->field = V4L2_FIELD_NONE;
1116 -
1117 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt, dev->sensor_config,
1118 - &sd_fmt);
1119 - if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1120 - return ret;
1121 -
1122 - if (mbus_fmt->field != V4L2_FIELD_NONE)
1123 - unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1124 -
1125 - v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1126 - if (mbus_fmt->code != fmt->code) {
1127 - /* Sensor has returned an alternate format */
1128 - fmt = find_format_by_code(mbus_fmt->code);
1129 - if (!fmt) {
1130 - /* The alternate format is one unicam can't support.
1131 - * Find the first format that is supported by both, and
1132 - * then set that.
1133 - */
1134 - fmt = get_first_supported_format(dev);
1135 - mbus_fmt->code = fmt->code;
1136 -
1137 - ret = v4l2_subdev_call(dev->sensor, pad, set_fmt,
1138 - dev->sensor_config, &sd_fmt);
1139 - if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1140 - return ret;
1141 -
1142 - if (mbus_fmt->field != V4L2_FIELD_NONE)
1143 - unicam_info(dev, "Sensor trying to send interlaced video - results may be unpredictable\n");
1144 -
1145 - v4l2_fill_pix_format(&f->fmt.pix, &sd_fmt.format);
1146 -
1147 - if (mbus_fmt->code != fmt->code) {
1148 - /* We've set a format that the sensor reports
1149 - * as being supported, but it refuses to set it.
1150 - * Not much else we can do.
1151 - * Assume that the sensor driver may accept the
1152 - * format when it is set (rather than tried).
1153 - */
1154 - unicam_err(dev, "Sensor won't accept default format, and Unicam can't support sensor default\n");
1155 - }
1156 - }
1157 -
1158 - if (fmt->fourcc)
1159 - f->fmt.pix.pixelformat = fmt->fourcc;
1160 - else
1161 - f->fmt.pix.pixelformat = fmt->repacked_fourcc;
1162 - }
1163 -
1164 - return unicam_calc_format_size_bpl(dev, fmt, f);
1165 -}
1166 -
1167 -static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
1168 - struct v4l2_format *f)
1169 -{
1170 - struct unicam_node *node = video_drvdata(file);
1171 - struct unicam_device *dev = node->dev;
1172 - struct vb2_queue *q = &node->buffer_queue;
1173 - struct v4l2_mbus_framefmt mbus_fmt = {0};
1174 - const struct unicam_fmt *fmt;
1175 - int ret;
1176 -
1177 - if (vb2_is_busy(q))
1178 - return -EBUSY;
1179 -
1180 - ret = unicam_try_fmt_vid_cap(file, priv, f);
1181 - if (ret < 0)
1182 - return ret;
1183 -
1184 - fmt = find_format_by_pix(dev, f->fmt.pix.pixelformat);
1185 - if (!fmt) {
1186 - /* Unknown pixel format - adopt a default.
1187 - * This shouldn't happen as try_fmt should have resolved any
1188 - * issues first.
1189 - */
1190 - fmt = get_first_supported_format(dev);
1191 - if (!fmt)
1192 - /* It shouldn't be possible to get here with no
1193 - * supported formats
1194 - */
1195 - return -EINVAL;
1196 - f->fmt.pix.pixelformat = fmt->fourcc;
1197 - return -EINVAL;
1198 - }
1199 -
1200 - v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
1201 -
1202 - ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1203 - if (ret) {
1204 - unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1205 - __func__, ret);
1206 - return ret;
1207 - }
1208 -
1209 - /* Just double check nothing has gone wrong */
1210 - if (mbus_fmt.code != fmt->code) {
1211 - unicam_dbg(3, dev,
1212 - "%s subdev changed format on us, this should not happen\n",
1213 - __func__);
1214 - return -EINVAL;
1215 - }
1216 -
1217 - node->fmt = fmt;
1218 - node->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
1219 - node->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
1220 - unicam_reset_format(node);
1221 -
1222 - unicam_dbg(3, dev,
1223 - "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
1224 - __func__, node->v_fmt.fmt.pix.width,
1225 - node->v_fmt.fmt.pix.height, mbus_fmt.code,
1226 - node->v_fmt.fmt.pix.pixelformat);
1227 -
1228 - *f = node->v_fmt;
1229 -
1230 - return 0;
1231 -}
1232 -
1233 -static int unicam_enum_fmt_meta_cap(struct file *file, void *priv,
1234 - struct v4l2_fmtdesc *f)
1235 -{
1236 - struct unicam_node *node = video_drvdata(file);
1237 - struct unicam_device *dev = node->dev;
1238 - struct v4l2_subdev_mbus_code_enum mbus_code;
1239 - const struct unicam_fmt *fmt = NULL;
1240 - int ret = 0;
1241 -
1242 - if (node->pad_id != METADATA_PAD || f->index != 0)
1243 - return -EINVAL;
1244 -
1245 - if (dev->sensor_embedded_data) {
1246 - memset(&mbus_code, 0, sizeof(mbus_code));
1247 - mbus_code.index = f->index;
1248 - mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1249 - mbus_code.pad = METADATA_PAD;
1250 -
1251 - ret = v4l2_subdev_call(dev->sensor, pad, enum_mbus_code, NULL,
1252 - &mbus_code);
1253 - if (ret < 0) {
1254 - unicam_dbg(2, dev,
1255 - "subdev->enum_mbus_code idx 0 returned %d - index invalid\n",
1256 - ret);
1257 - return -EINVAL;
1258 - }
1259 - } else {
1260 - mbus_code.code = MEDIA_BUS_FMT_SENSOR_DATA;
1261 - }
1262 -
1263 - fmt = find_format_by_code(mbus_code.code);
1264 - if (fmt)
1265 - f->pixelformat = fmt->fourcc;
1266 -
1267 - return 0;
1268 -}
1269 -
1270 -static int unicam_g_fmt_meta_cap(struct file *file, void *priv,
1271 - struct v4l2_format *f)
1272 -{
1273 - struct unicam_node *node = video_drvdata(file);
1274 -
1275 - if (node->pad_id != METADATA_PAD)
1276 - return -EINVAL;
1277 -
1278 - *f = node->v_fmt;
1279 -
1280 - return 0;
1281 -}
1282 -
1283 -static int unicam_try_fmt_meta_cap(struct file *file, void *priv,
1284 - struct v4l2_format *f)
1285 -{
1286 - struct unicam_node *node = video_drvdata(file);
1287 -
1288 - if (node->pad_id != METADATA_PAD)
1289 - return -EINVAL;
1290 -
1291 - *f = node->v_fmt;
1292 -
1293 - return 0;
1294 -}
1295 -
1296 -static int unicam_s_fmt_meta_cap(struct file *file, void *priv,
1297 - struct v4l2_format *f)
1298 -{
1299 - struct unicam_node *node = video_drvdata(file);
1300 - struct unicam_device *dev = node->dev;
1301 - struct v4l2_mbus_framefmt mbus_fmt = { 0 };
1302 - const struct unicam_fmt *fmt;
1303 - int ret;
1304 -
1305 - if (node->pad_id == IMAGE_PAD)
1306 - return -EINVAL;
1307 -
1308 - if (dev->sensor_embedded_data) {
1309 - fmt = find_format_by_pix(dev, f->fmt.meta.dataformat);
1310 - if (!fmt) {
1311 - unicam_err(dev, "unknown format: V4L2 pix 0x%08x\n",
1312 - f->fmt.meta.dataformat);
1313 - return -EINVAL;
1314 - }
1315 - mbus_fmt.code = fmt->code;
1316 - ret = __subdev_set_format(dev, &mbus_fmt, node->pad_id);
1317 - if (ret) {
1318 - unicam_dbg(3, dev, "%s __subdev_set_format failed %d\n",
1319 - __func__, ret);
1320 - return ret;
1321 - }
1322 - }
1323 -
1324 - *f = node->v_fmt;
1325 -
1326 - unicam_dbg(3, dev, "%s size %d, V4L2 pix 0x%08x\n",
1327 - __func__, node->v_fmt.fmt.meta.buffersize,
1328 - node->v_fmt.fmt.meta.dataformat);
1329 -
1330 - return 0;
1331 -}
1332 -
1333 -static int unicam_queue_setup(struct vb2_queue *vq,
1334 - unsigned int *nbuffers,
1335 - unsigned int *nplanes,
1336 - unsigned int sizes[],
1337 - struct device *alloc_devs[])
1338 -{
1339 - struct unicam_node *node = vb2_get_drv_priv(vq);
1340 - struct unicam_device *dev = node->dev;
1341 - unsigned int size = node->pad_id == IMAGE_PAD ?
1342 - node->v_fmt.fmt.pix.sizeimage :
1343 - node->v_fmt.fmt.meta.buffersize;
1344 -
1345 - if (vq->num_buffers + *nbuffers < 3)
1346 - *nbuffers = 3 - vq->num_buffers;
1347 -
1348 - if (*nplanes) {
1349 - if (sizes[0] < size) {
1350 - unicam_err(dev, "sizes[0] %i < size %u\n", sizes[0],
1351 - size);
1352 - return -EINVAL;
1353 - }
1354 - size = sizes[0];
1355 - }
1356 -
1357 - *nplanes = 1;
1358 - sizes[0] = size;
1359 -
1360 - return 0;
1361 -}
1362 -
1363 -static int unicam_buffer_prepare(struct vb2_buffer *vb)
1364 -{
1365 - struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1366 - struct unicam_device *dev = node->dev;
1367 - struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1368 - vb.vb2_buf);
1369 - unsigned long size;
1370 -
1371 - if (WARN_ON(!node->fmt))
1372 - return -EINVAL;
1373 -
1374 - size = node->pad_id == IMAGE_PAD ? node->v_fmt.fmt.pix.sizeimage :
1375 - node->v_fmt.fmt.meta.buffersize;
1376 - if (vb2_plane_size(vb, 0) < size) {
1377 - unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
1378 - vb2_plane_size(vb, 0), size);
1379 - return -EINVAL;
1380 - }
1381 -
1382 - vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
1383 - return 0;
1384 -}
1385 -
1386 -static void unicam_buffer_queue(struct vb2_buffer *vb)
1387 -{
1388 - struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
1389 - struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
1390 - vb.vb2_buf);
1391 - struct unicam_dmaqueue *dma_queue = &node->dma_queue;
1392 - unsigned long flags = 0;
1393 -
1394 - spin_lock_irqsave(&node->dma_queue_lock, flags);
1395 - list_add_tail(&buf->list, &dma_queue->active);
1396 - spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1397 -}
1398 -
1399 -static void unicam_set_packing_config(struct unicam_device *dev)
1400 -{
1401 - int pack, unpack;
1402 - u32 val;
1403 -
1404 - if (dev->node[IMAGE_PAD].v_fmt.fmt.pix.pixelformat ==
1405 - dev->node[IMAGE_PAD].fmt->fourcc) {
1406 - unpack = UNICAM_PUM_NONE;
1407 - pack = UNICAM_PPM_NONE;
1408 - } else {
1409 - switch (dev->node[IMAGE_PAD].fmt->depth) {
1410 - case 8:
1411 - unpack = UNICAM_PUM_UNPACK8;
1412 - break;
1413 - case 10:
1414 - unpack = UNICAM_PUM_UNPACK10;
1415 - break;
1416 - case 12:
1417 - unpack = UNICAM_PUM_UNPACK12;
1418 - break;
1419 - case 14:
1420 - unpack = UNICAM_PUM_UNPACK14;
1421 - break;
1422 - case 16:
1423 - unpack = UNICAM_PUM_UNPACK16;
1424 - break;
1425 - default:
1426 - unpack = UNICAM_PUM_NONE;
1427 - break;
1428 - }
1429 -
1430 - /* Repacking is always to 16bpp */
1431 - pack = UNICAM_PPM_PACK16;
1432 - }
1433 -
1434 - val = 0;
1435 - set_field(&val, unpack, UNICAM_PUM_MASK);
1436 - set_field(&val, pack, UNICAM_PPM_MASK);
1437 - reg_write(&dev->cfg, UNICAM_IPIPE, val);
1438 -}
1439 -
1440 -static void unicam_cfg_image_id(struct unicam_device *dev)
1441 -{
1442 - struct unicam_cfg *cfg = &dev->cfg;
1443 -
1444 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1445 - /* CSI2 mode */
1446 - reg_write(cfg, UNICAM_IDI0,
1447 - (dev->virtual_channel << 6) |
1448 - dev->node[IMAGE_PAD].fmt->csi_dt);
1449 - } else {
1450 - /* CCP2 mode */
1451 - reg_write(cfg, UNICAM_IDI0,
1452 - 0x80 | dev->node[IMAGE_PAD].fmt->csi_dt);
1453 - }
1454 -}
1455 -
1456 -static void unicam_enable_ed(struct unicam_device *dev)
1457 -{
1458 - struct unicam_cfg *cfg = &dev->cfg;
1459 - u32 val = reg_read(cfg, UNICAM_DCS);
1460 -
1461 - set_field(&val, 2, UNICAM_EDL_MASK);
1462 - /* Do not wrap at the end of the embedded data buffer */
1463 - set_field(&val, 0, UNICAM_DBOB);
1464 -
1465 - reg_write(cfg, UNICAM_DCS, val);
1466 -}
1467 -
1468 -static void unicam_start_rx(struct unicam_device *dev, dma_addr_t *addr)
1469 -{
1470 - struct unicam_cfg *cfg = &dev->cfg;
1471 - int line_int_freq = dev->node[IMAGE_PAD].v_fmt.fmt.pix.height >> 2;
1472 - unsigned int size, i;
1473 - u32 val;
1474 -
1475 - if (line_int_freq < 128)
1476 - line_int_freq = 128;
1477 -
1478 - /* Enable lane clocks */
1479 - val = 1;
1480 - for (i = 0; i < dev->active_data_lanes; i++)
1481 - val = val << 2 | 1;
1482 - clk_write(cfg, val);
1483 -
1484 - /* Basic init */
1485 - reg_write(cfg, UNICAM_CTRL, UNICAM_MEM);
1486 -
1487 - /* Enable analogue control, and leave in reset. */
1488 - val = UNICAM_AR;
1489 - set_field(&val, 7, UNICAM_CTATADJ_MASK);
1490 - set_field(&val, 7, UNICAM_PTATADJ_MASK);
1491 - reg_write(cfg, UNICAM_ANA, val);
1492 - usleep_range(1000, 2000);
1493 -
1494 - /* Come out of reset */
1495 - reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_AR);
1496 -
1497 - /* Peripheral reset */
1498 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1499 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1500 -
1501 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1502 -
1503 - /* Enable Rx control. */
1504 - val = reg_read(cfg, UNICAM_CTRL);
1505 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1506 - set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK);
1507 - set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK);
1508 - } else {
1509 - set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK);
1510 - set_field(&val, dev->bus_flags, UNICAM_DCM_MASK);
1511 - }
1512 - /* Packet framer timeout */
1513 - set_field(&val, 0xf, UNICAM_PFT_MASK);
1514 - set_field(&val, 128, UNICAM_OET_MASK);
1515 - reg_write(cfg, UNICAM_CTRL, val);
1516 -
1517 - reg_write(cfg, UNICAM_IHWIN, 0);
1518 - reg_write(cfg, UNICAM_IVWIN, 0);
1519 -
1520 - /* AXI bus access QoS setup */
1521 - val = reg_read(&dev->cfg, UNICAM_PRI);
1522 - set_field(&val, 0, UNICAM_BL_MASK);
1523 - set_field(&val, 0, UNICAM_BS_MASK);
1524 - set_field(&val, 0xe, UNICAM_PP_MASK);
1525 - set_field(&val, 8, UNICAM_NP_MASK);
1526 - set_field(&val, 2, UNICAM_PT_MASK);
1527 - set_field(&val, 1, UNICAM_PE);
1528 - reg_write(cfg, UNICAM_PRI, val);
1529 -
1530 - reg_write_field(cfg, UNICAM_ANA, 0, UNICAM_DDL);
1531 -
1532 - /* Always start in trigger frame capture mode (UNICAM_FCM set) */
1533 - val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_FCM | UNICAM_IBOB;
1534 - set_field(&val, line_int_freq, UNICAM_LCIE_MASK);
1535 - reg_write(cfg, UNICAM_ICTL, val);
1536 - reg_write(cfg, UNICAM_STA, UNICAM_STA_MASK_ALL);
1537 - reg_write(cfg, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL);
1538 -
1539 - /* tclk_term_en */
1540 - reg_write_field(cfg, UNICAM_CLT, 2, UNICAM_CLT1_MASK);
1541 - /* tclk_settle */
1542 - reg_write_field(cfg, UNICAM_CLT, 6, UNICAM_CLT2_MASK);
1543 - /* td_term_en */
1544 - reg_write_field(cfg, UNICAM_DLT, 2, UNICAM_DLT1_MASK);
1545 - /* ths_settle */
1546 - reg_write_field(cfg, UNICAM_DLT, 6, UNICAM_DLT2_MASK);
1547 - /* trx_enable */
1548 - reg_write_field(cfg, UNICAM_DLT, 0, UNICAM_DLT3_MASK);
1549 -
1550 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_SOE);
1551 -
1552 - /* Packet compare setup - required to avoid missing frame ends */
1553 - val = 0;
1554 - set_field(&val, 1, UNICAM_PCE);
1555 - set_field(&val, 1, UNICAM_GI);
1556 - set_field(&val, 1, UNICAM_CPH);
1557 - set_field(&val, 0, UNICAM_PCVC_MASK);
1558 - set_field(&val, 1, UNICAM_PCDT_MASK);
1559 - reg_write(cfg, UNICAM_CMP0, val);
1560 -
1561 - /* Enable clock lane and set up terminations */
1562 - val = 0;
1563 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1564 - /* CSI2 */
1565 - set_field(&val, 1, UNICAM_CLE);
1566 - set_field(&val, 1, UNICAM_CLLPE);
1567 - if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1568 - set_field(&val, 1, UNICAM_CLTRE);
1569 - set_field(&val, 1, UNICAM_CLHSE);
1570 - }
1571 - } else {
1572 - /* CCP2 */
1573 - set_field(&val, 1, UNICAM_CLE);
1574 - set_field(&val, 1, UNICAM_CLHSE);
1575 - set_field(&val, 1, UNICAM_CLTRE);
1576 - }
1577 - reg_write(cfg, UNICAM_CLK, val);
1578 -
1579 - /*
1580 - * Enable required data lanes with appropriate terminations.
1581 - * The same value needs to be written to UNICAM_DATn registers for
1582 - * the active lanes, and 0 for inactive ones.
1583 - */
1584 - val = 0;
1585 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
1586 - /* CSI2 */
1587 - set_field(&val, 1, UNICAM_DLE);
1588 - set_field(&val, 1, UNICAM_DLLPE);
1589 - if (dev->bus_flags & V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) {
1590 - set_field(&val, 1, UNICAM_DLTRE);
1591 - set_field(&val, 1, UNICAM_DLHSE);
1592 - }
1593 - } else {
1594 - /* CCP2 */
1595 - set_field(&val, 1, UNICAM_DLE);
1596 - set_field(&val, 1, UNICAM_DLHSE);
1597 - set_field(&val, 1, UNICAM_DLTRE);
1598 - }
1599 - reg_write(cfg, UNICAM_DAT0, val);
1600 -
1601 - if (dev->active_data_lanes == 1)
1602 - val = 0;
1603 - reg_write(cfg, UNICAM_DAT1, val);
1604 -
1605 - if (dev->max_data_lanes > 2) {
1606 - /*
1607 - * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the
1608 - * instance supports more than 2 data lanes.
1609 - */
1610 - if (dev->active_data_lanes == 2)
1611 - val = 0;
1612 - reg_write(cfg, UNICAM_DAT2, val);
1613 -
1614 - if (dev->active_data_lanes == 3)
1615 - val = 0;
1616 - reg_write(cfg, UNICAM_DAT3, val);
1617 - }
1618 -
1619 - reg_write(&dev->cfg, UNICAM_IBLS,
1620 - dev->node[IMAGE_PAD].v_fmt.fmt.pix.bytesperline);
1621 - size = dev->node[IMAGE_PAD].v_fmt.fmt.pix.sizeimage;
1622 - unicam_wr_dma_addr(&dev->cfg, addr[IMAGE_PAD], size, IMAGE_PAD);
1623 - unicam_set_packing_config(dev);
1624 - unicam_cfg_image_id(dev);
1625 -
1626 - val = reg_read(cfg, UNICAM_MISC);
1627 - set_field(&val, 1, UNICAM_FL0);
1628 - set_field(&val, 1, UNICAM_FL1);
1629 - reg_write(cfg, UNICAM_MISC, val);
1630 -
1631 - if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data) {
1632 - size = dev->node[METADATA_PAD].v_fmt.fmt.meta.buffersize;
1633 - unicam_enable_ed(dev);
1634 - unicam_wr_dma_addr(&dev->cfg, addr[METADATA_PAD], size,
1635 - METADATA_PAD);
1636 - }
1637 -
1638 - /* Enable peripheral */
1639 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPE);
1640 -
1641 - /* Load image pointers */
1642 - reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_LIP_MASK);
1643 -
1644 - /* Load embedded data buffer pointers if needed */
1645 - if (dev->node[METADATA_PAD].streaming && dev->sensor_embedded_data)
1646 - reg_write_field(cfg, UNICAM_DCS, 1, UNICAM_LDP);
1647 -
1648 - /*
1649 - * Enable trigger only for the first frame to
1650 - * sync correctly to the FS from the source.
1651 - */
1652 - reg_write_field(cfg, UNICAM_ICTL, 1, UNICAM_TFC);
1653 -}
1654 -
1655 -static void unicam_disable(struct unicam_device *dev)
1656 -{
1657 - struct unicam_cfg *cfg = &dev->cfg;
1658 -
1659 - /* Analogue lane control disable */
1660 - reg_write_field(cfg, UNICAM_ANA, 1, UNICAM_DDL);
1661 -
1662 - /* Stop the output engine */
1663 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_SOE);
1664 -
1665 - /* Disable the data lanes. */
1666 - reg_write(cfg, UNICAM_DAT0, 0);
1667 - reg_write(cfg, UNICAM_DAT1, 0);
1668 -
1669 - if (dev->max_data_lanes > 2) {
1670 - reg_write(cfg, UNICAM_DAT2, 0);
1671 - reg_write(cfg, UNICAM_DAT3, 0);
1672 - }
1673 -
1674 - /* Peripheral reset */
1675 - reg_write_field(cfg, UNICAM_CTRL, 1, UNICAM_CPR);
1676 - usleep_range(50, 100);
1677 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPR);
1678 -
1679 - /* Disable peripheral */
1680 - reg_write_field(cfg, UNICAM_CTRL, 0, UNICAM_CPE);
1681 -
1682 - /* Clear ED setup */
1683 - reg_write(cfg, UNICAM_DCS, 0);
1684 -
1685 - /* Disable all lane clocks */
1686 - clk_write(cfg, 0);
1687 -}
1688 -
1689 -static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
1690 -{
1691 - struct unicam_node *node = vb2_get_drv_priv(vq);
1692 - struct unicam_device *dev = node->dev;
1693 - struct unicam_buffer *buf;
1694 - dma_addr_t buffer_addr[MAX_NODES] = { 0 };
1695 - int num_nodes_streaming;
1696 - unsigned long flags;
1697 - int ret, i;
1698 -
1699 - node->streaming = 1;
1700 - if (!unicam_all_nodes_streaming(dev)) {
1701 - unicam_dbg(3, dev, "Not all nodes are streaming yet.");
1702 - return 0;
1703 - }
1704 -
1705 - dev->sequence = 0;
1706 - ret = unicam_runtime_get(dev);
1707 - if (ret < 0) {
1708 - unicam_dbg(3, dev, "unicam_runtime_get failed\n");
1709 - return ret;
1710 - }
1711 -
1712 - dev->active_data_lanes = dev->max_data_lanes;
1713 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY &&
1714 - v4l2_subdev_has_op(dev->sensor, video, g_mbus_config)) {
1715 - struct v4l2_mbus_config mbus_config;
1716 -
1717 - ret = v4l2_subdev_call(dev->sensor, video, g_mbus_config,
1718 - &mbus_config);
1719 - if (ret < 0) {
1720 - unicam_dbg(3, dev, "g_mbus_config failed\n");
1721 - goto err_pm_put;
1722 - }
1723 -
1724 - dev->active_data_lanes =
1725 - (mbus_config.flags & V4L2_MBUS_CSI2_LANE_MASK) >>
1726 - __ffs(V4L2_MBUS_CSI2_LANE_MASK);
1727 - if (!dev->active_data_lanes)
1728 - dev->active_data_lanes = dev->max_data_lanes;
1729 - }
1730 - if (dev->active_data_lanes > dev->max_data_lanes) {
1731 - unicam_err(dev, "Device has requested %u data lanes, which is >%u configured in DT\n",
1732 - dev->active_data_lanes, dev->max_data_lanes);
1733 - ret = -EINVAL;
1734 - goto err_pm_put;
1735 - }
1736 -
1737 - unicam_dbg(1, dev, "Running with %u data lanes\n",
1738 - dev->active_data_lanes);
1739 -
1740 - ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
1741 - if (ret) {
1742 - unicam_err(dev, "failed to set up clock\n");
1743 - goto err_pm_put;
1744 - }
1745 -
1746 - ret = clk_prepare_enable(dev->clock);
1747 - if (ret) {
1748 - unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
1749 - goto err_pm_put;
1750 - }
1751 -
1752 - num_nodes_streaming = unicam_num_nodes_streaming(dev);
1753 - for (i = 0; i < num_nodes_streaming; i++) {
1754 - spin_lock_irqsave(&dev->node[i].dma_queue_lock, flags);
1755 - buf = list_entry(dev->node[i].dma_queue.active.next,
1756 - struct unicam_buffer, list);
1757 - dev->node[i].cur_frm = buf;
1758 - dev->node[i].next_frm = buf;
1759 - list_del(&buf->list);
1760 - spin_unlock_irqrestore(&dev->node[i].dma_queue_lock, flags);
1761 - buffer_addr[i] =
1762 - vb2_dma_contig_plane_dma_addr(&dev->node[i].cur_frm->vb.vb2_buf,
1763 - 0);
1764 - }
1765 -
1766 - unicam_start_rx(dev, buffer_addr);
1767 -
1768 - ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1769 - if (ret < 0) {
1770 - unicam_err(dev, "stream on failed in subdev\n");
1771 - goto err_disable_unicam;
1772 - }
1773 -
1774 - return 0;
1775 -
1776 -err_disable_unicam:
1777 - node->streaming = 0;
1778 - unicam_disable(dev);
1779 - clk_disable_unprepare(dev->clock);
1780 -err_pm_put:
1781 - unicam_runtime_put(dev);
1782 -
1783 - return ret;
1784 -}
1785 -
1786 -static void unicam_stop_streaming(struct vb2_queue *vq)
1787 -{
1788 - struct unicam_node *node = vb2_get_drv_priv(vq);
1789 - struct unicam_device *dev = node->dev;
1790 - struct unicam_dmaqueue *dma_q = &node->dma_queue;
1791 - struct unicam_buffer *buf, *tmp;
1792 - unsigned long flags;
1793 -
1794 - node->streaming = 0;
1795 -
1796 - if (node->pad_id == IMAGE_PAD) {
1797 - /* Stop streaming the sensor and disable the peripheral.
1798 - * We cannot continue streaming embedded data with the
1799 - * image pad disabled.
1800 - */
1801 - if (v4l2_subdev_call(dev->sensor, video, s_stream, 0) < 0)
1802 - unicam_err(dev, "stream off failed in subdev\n");
1803 -
1804 - unicam_disable(dev);
1805 - clk_disable_unprepare(dev->clock);
1806 - unicam_runtime_put(dev);
1807 -
1808 - } else if (node->pad_id == METADATA_PAD) {
1809 - /* Allow the hardware to spin in the dummy buffer.
1810 - * This is only really needed if the embedded data pad is
1811 - * disabled before the image pad. The 0x3 in the top two bits
1812 - * signifies uncached accesses through the Videocore memory
1813 - * controller.
1814 - */
1815 - unicam_wr_dma_addr(&dev->cfg, node->dummy_buf_dma_addr,
1816 - DUMMY_BUF_SIZE, METADATA_PAD);
1817 - }
1818 -
1819 - /* Clear all queued buffers for the node */
1820 - spin_lock_irqsave(&node->dma_queue_lock, flags);
1821 - list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
1822 - list_del(&buf->list);
1823 - vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1824 - }
1825 -
1826 - if (node->cur_frm)
1827 - vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
1828 - VB2_BUF_STATE_ERROR);
1829 - if (node->next_frm && node->cur_frm != node->next_frm)
1830 - vb2_buffer_done(&node->next_frm->vb.vb2_buf,
1831 - VB2_BUF_STATE_ERROR);
1832 -
1833 - node->cur_frm = NULL;
1834 - node->next_frm = NULL;
1835 - spin_unlock_irqrestore(&node->dma_queue_lock, flags);
1836 -}
1837 -
1838 -static int unicam_enum_input(struct file *file, void *priv,
1839 - struct v4l2_input *inp)
1840 -{
1841 - struct unicam_node *node = video_drvdata(file);
1842 - struct unicam_device *dev = node->dev;
1843 -
1844 - if (inp->index != 0)
1845 - return -EINVAL;
1846 -
1847 - inp->type = V4L2_INPUT_TYPE_CAMERA;
1848 - if (v4l2_subdev_has_op(dev->sensor, video, s_dv_timings)) {
1849 - inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1850 - inp->std = 0;
1851 - } else if (v4l2_subdev_has_op(dev->sensor, video, s_std)) {
1852 - inp->capabilities = V4L2_IN_CAP_STD;
1853 - if (v4l2_subdev_call(dev->sensor, video, g_tvnorms, &inp->std)
1854 - < 0)
1855 - inp->std = V4L2_STD_ALL;
1856 - } else {
1857 - inp->capabilities = 0;
1858 - inp->std = 0;
1859 - }
1860 - sprintf(inp->name, "Camera 0");
1861 - return 0;
1862 -}
1863 -
1864 -static int unicam_g_input(struct file *file, void *priv, unsigned int *i)
1865 -{
1866 - *i = 0;
1867 -
1868 - return 0;
1869 -}
1870 -
1871 -static int unicam_s_input(struct file *file, void *priv, unsigned int i)
1872 -{
1873 - /*
1874 - * FIXME: Ideally we would like to be able to query the source
1875 - * subdevice for information over the input connectors it supports,
1876 - * and map that through in to a call to video_ops->s_routing.
1877 - * There is no infrastructure support for defining that within
1878 - * devicetree at present. Until that is implemented we can't
1879 - * map a user physical connector number to s_routing input number.
1880 - */
1881 - if (i > 0)
1882 - return -EINVAL;
1883 -
1884 - return 0;
1885 -}
1886 -
1887 -static int unicam_querystd(struct file *file, void *priv,
1888 - v4l2_std_id *std)
1889 -{
1890 - struct unicam_node *node = video_drvdata(file);
1891 - struct unicam_device *dev = node->dev;
1892 -
1893 - return v4l2_subdev_call(dev->sensor, video, querystd, std);
1894 -}
1895 -
1896 -static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
1897 -{
1898 - struct unicam_node *node = video_drvdata(file);
1899 - struct unicam_device *dev = node->dev;
1900 -
1901 - return v4l2_subdev_call(dev->sensor, video, g_std, std);
1902 -}
1903 -
1904 -static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
1905 -{
1906 - struct unicam_node *node = video_drvdata(file);
1907 - struct unicam_device *dev = node->dev;
1908 - int ret;
1909 - v4l2_std_id current_std;
1910 -
1911 - ret = v4l2_subdev_call(dev->sensor, video, g_std, &current_std);
1912 - if (ret)
1913 - return ret;
1914 -
1915 - if (std == current_std)
1916 - return 0;
1917 -
1918 - if (vb2_is_busy(&node->buffer_queue))
1919 - return -EBUSY;
1920 -
1921 - ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
1922 -
1923 - /* Force recomputation of bytesperline */
1924 - node->v_fmt.fmt.pix.bytesperline = 0;
1925 -
1926 - unicam_reset_format(node);
1927 -
1928 - return ret;
1929 -}
1930 -
1931 -static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1932 -{
1933 - struct unicam_node *node = video_drvdata(file);
1934 - struct unicam_device *dev = node->dev;
1935 -
1936 - return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
1937 -}
1938 -
1939 -static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
1940 -{
1941 - struct unicam_node *node = video_drvdata(file);
1942 - struct unicam_device *dev = node->dev;
1943 -
1944 - return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
1945 -}
1946 -
1947 -static int unicam_s_selection(struct file *file, void *priv,
1948 - struct v4l2_selection *sel)
1949 -{
1950 - struct unicam_node *node = video_drvdata(file);
1951 - struct unicam_device *dev = node->dev;
1952 - struct v4l2_subdev_selection sdsel = {
1953 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1954 - .target = sel->target,
1955 - .flags = sel->flags,
1956 - .r = sel->r,
1957 - };
1958 -
1959 - return v4l2_subdev_call(dev->sensor, pad, set_selection, NULL, &sdsel);
1960 -}
1961 -
1962 -static int unicam_g_selection(struct file *file, void *priv,
1963 - struct v4l2_selection *sel)
1964 -{
1965 - struct unicam_node *node = video_drvdata(file);
1966 - struct unicam_device *dev = node->dev;
1967 - struct v4l2_subdev_selection sdsel = {
1968 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1969 - .target = sel->target,
1970 - };
1971 - int ret;
1972 -
1973 - ret = v4l2_subdev_call(dev->sensor, pad, get_selection, NULL, &sdsel);
1974 - if (!ret)
1975 - sel->r = sdsel.r;
1976 -
1977 - return ret;
1978 -}
1979 -
1980 -static int unicam_enum_framesizes(struct file *file, void *priv,
1981 - struct v4l2_frmsizeenum *fsize)
1982 -{
1983 - struct unicam_node *node = video_drvdata(file);
1984 - struct unicam_device *dev = node->dev;
1985 - const struct unicam_fmt *fmt;
1986 - struct v4l2_subdev_frame_size_enum fse;
1987 - int ret;
1988 -
1989 - if (node->pad_id == IMAGE_PAD) {
1990 - /* check for valid format */
1991 - fmt = find_format_by_pix(dev, fsize->pixel_format);
1992 - if (!fmt) {
1993 - unicam_dbg(3, dev, "Invalid pixel code: %x\n",
1994 - fsize->pixel_format);
1995 - return -EINVAL;
1996 - }
1997 - fse.code = fmt->code;
1998 - } else {
1999 - /* This pad is for embedded data, so just set the format */
2000 - fse.code = MEDIA_BUS_FMT_SENSOR_DATA;
2001 - }
2002 -
2003 - fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2004 - fse.index = fsize->index;
2005 - fse.pad = node->pad_id;
2006 -
2007 - ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_size, NULL, &fse);
2008 - if (ret)
2009 - return ret;
2010 -
2011 - unicam_dbg(1, dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
2012 - __func__, fse.index, fse.code, fse.min_width, fse.max_width,
2013 - fse.min_height, fse.max_height);
2014 -
2015 - fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
2016 - fsize->discrete.width = fse.max_width;
2017 - fsize->discrete.height = fse.max_height;
2018 -
2019 - return 0;
2020 -}
2021 -
2022 -static int unicam_enum_frameintervals(struct file *file, void *priv,
2023 - struct v4l2_frmivalenum *fival)
2024 -{
2025 - struct unicam_node *node = video_drvdata(file);
2026 - struct unicam_device *dev = node->dev;
2027 - const struct unicam_fmt *fmt;
2028 - struct v4l2_subdev_frame_interval_enum fie = {
2029 - .index = fival->index,
2030 - .width = fival->width,
2031 - .height = fival->height,
2032 - .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2033 - };
2034 - int ret;
2035 -
2036 - fmt = find_format_by_pix(dev, fival->pixel_format);
2037 - if (!fmt)
2038 - return -EINVAL;
2039 -
2040 - fie.code = fmt->code;
2041 - ret = v4l2_subdev_call(dev->sensor, pad, enum_frame_interval,
2042 - NULL, &fie);
2043 - if (ret)
2044 - return ret;
2045 -
2046 - fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
2047 - fival->discrete = fie.interval;
2048 -
2049 - return 0;
2050 -}
2051 -
2052 -static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2053 -{
2054 - struct unicam_node *node = video_drvdata(file);
2055 - struct unicam_device *dev = node->dev;
2056 -
2057 - return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
2058 -}
2059 -
2060 -static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
2061 -{
2062 - struct unicam_node *node = video_drvdata(file);
2063 - struct unicam_device *dev = node->dev;
2064 -
2065 - return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
2066 -}
2067 -
2068 -static int unicam_g_dv_timings(struct file *file, void *priv,
2069 - struct v4l2_dv_timings *timings)
2070 -{
2071 - struct unicam_node *node = video_drvdata(file);
2072 - struct unicam_device *dev = node->dev;
2073 -
2074 - return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
2075 -}
2076 -
2077 -static int unicam_s_dv_timings(struct file *file, void *priv,
2078 - struct v4l2_dv_timings *timings)
2079 -{
2080 - struct unicam_node *node = video_drvdata(file);
2081 - struct unicam_device *dev = node->dev;
2082 - struct v4l2_dv_timings current_timings;
2083 - int ret;
2084 -
2085 - ret = v4l2_subdev_call(dev->sensor, video, g_dv_timings,
2086 - &current_timings);
2087 -
2088 - if (v4l2_match_dv_timings(timings, &current_timings, 0, false))
2089 - return 0;
2090 -
2091 - if (vb2_is_busy(&node->buffer_queue))
2092 - return -EBUSY;
2093 -
2094 - ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
2095 -
2096 - /* Force recomputation of bytesperline */
2097 - node->v_fmt.fmt.pix.bytesperline = 0;
2098 -
2099 - unicam_reset_format(node);
2100 -
2101 - return ret;
2102 -}
2103 -
2104 -static int unicam_query_dv_timings(struct file *file, void *priv,
2105 - struct v4l2_dv_timings *timings)
2106 -{
2107 - struct unicam_node *node = video_drvdata(file);
2108 - struct unicam_device *dev = node->dev;
2109 -
2110 - return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
2111 -}
2112 -
2113 -static int unicam_enum_dv_timings(struct file *file, void *priv,
2114 - struct v4l2_enum_dv_timings *timings)
2115 -{
2116 - struct unicam_node *node = video_drvdata(file);
2117 - struct unicam_device *dev = node->dev;
2118 -
2119 - return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
2120 -}
2121 -
2122 -static int unicam_dv_timings_cap(struct file *file, void *priv,
2123 - struct v4l2_dv_timings_cap *cap)
2124 -{
2125 - struct unicam_node *node = video_drvdata(file);
2126 - struct unicam_device *dev = node->dev;
2127 -
2128 - return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
2129 -}
2130 -
2131 -static int unicam_subscribe_event(struct v4l2_fh *fh,
2132 - const struct v4l2_event_subscription *sub)
2133 -{
2134 - switch (sub->type) {
2135 - case V4L2_EVENT_FRAME_SYNC:
2136 - return v4l2_event_subscribe(fh, sub, 2, NULL);
2137 - case V4L2_EVENT_SOURCE_CHANGE:
2138 - return v4l2_event_subscribe(fh, sub, 4, NULL);
2139 - }
2140 -
2141 - return v4l2_ctrl_subscribe_event(fh, sub);
2142 -}
2143 -
2144 -static int unicam_log_status(struct file *file, void *fh)
2145 -{
2146 - struct unicam_node *node = video_drvdata(file);
2147 - struct unicam_device *dev = node->dev;
2148 - struct unicam_cfg *cfg = &dev->cfg;
2149 - u32 reg;
2150 -
2151 - /* status for sub devices */
2152 - v4l2_device_call_all(&dev->v4l2_dev, 0, core, log_status);
2153 -
2154 - unicam_info(dev, "-----Receiver status-----\n");
2155 - unicam_info(dev, "V4L2 width/height: %ux%u\n",
2156 - node->v_fmt.fmt.pix.width, node->v_fmt.fmt.pix.height);
2157 - unicam_info(dev, "Mediabus format: %08x\n", node->fmt->code);
2158 - unicam_info(dev, "V4L2 format: %08x\n",
2159 - node->v_fmt.fmt.pix.pixelformat);
2160 - reg = reg_read(&dev->cfg, UNICAM_IPIPE);
2161 - unicam_info(dev, "Unpacking/packing: %u / %u\n",
2162 - get_field(reg, UNICAM_PUM_MASK),
2163 - get_field(reg, UNICAM_PPM_MASK));
2164 - unicam_info(dev, "----Live data----\n");
2165 - unicam_info(dev, "Programmed stride: %4u\n",
2166 - reg_read(cfg, UNICAM_IBLS));
2167 - unicam_info(dev, "Detected resolution: %ux%u\n",
2168 - reg_read(cfg, UNICAM_IHSTA),
2169 - reg_read(cfg, UNICAM_IVSTA));
2170 - unicam_info(dev, "Write pointer: %08x\n",
2171 - reg_read(cfg, UNICAM_IBWP));
2172 -
2173 - return 0;
2174 -}
2175 -
2176 -static void unicam_notify(struct v4l2_subdev *sd,
2177 - unsigned int notification, void *arg)
2178 -{
2179 - struct unicam_device *dev =
2180 - container_of(sd->v4l2_dev, struct unicam_device, v4l2_dev);
2181 -
2182 - switch (notification) {
2183 - case V4L2_DEVICE_NOTIFY_EVENT:
2184 - v4l2_event_queue(&dev->node[IMAGE_PAD].video_dev, arg);
2185 - break;
2186 - default:
2187 - break;
2188 - }
2189 -}
2190 -
2191 -static const struct vb2_ops unicam_video_qops = {
2192 - .wait_prepare = vb2_ops_wait_prepare,
2193 - .wait_finish = vb2_ops_wait_finish,
2194 - .queue_setup = unicam_queue_setup,
2195 - .buf_prepare = unicam_buffer_prepare,
2196 - .buf_queue = unicam_buffer_queue,
2197 - .start_streaming = unicam_start_streaming,
2198 - .stop_streaming = unicam_stop_streaming,
2199 -};
2200 -
2201 -/*
2202 - * unicam_open : This function is based on the v4l2_fh_open helper function.
2203 - * It has been augmented to handle sensor subdevice power management,
2204 - */
2205 -static int unicam_open(struct file *file)
2206 -{
2207 - struct unicam_node *node = video_drvdata(file);
2208 - struct unicam_device *dev = node->dev;
2209 - int ret;
2210 -
2211 - mutex_lock(&node->lock);
2212 -
2213 - ret = v4l2_fh_open(file);
2214 - if (ret) {
2215 - unicam_err(dev, "v4l2_fh_open failed\n");
2216 - goto unlock;
2217 - }
2218 -
2219 - node->open++;
2220 -
2221 - if (!v4l2_fh_is_singular_file(file))
2222 - goto unlock;
2223 -
2224 - ret = v4l2_subdev_call(dev->sensor, core, s_power, 1);
2225 - if (ret < 0 && ret != -ENOIOCTLCMD) {
2226 - v4l2_fh_release(file);
2227 - node->open--;
2228 - goto unlock;
2229 - }
2230 -
2231 - ret = 0;
2232 -
2233 -unlock:
2234 - mutex_unlock(&node->lock);
2235 - return ret;
2236 -}
2237 -
2238 -static int unicam_release(struct file *file)
2239 -{
2240 - struct unicam_node *node = video_drvdata(file);
2241 - struct unicam_device *dev = node->dev;
2242 - struct v4l2_subdev *sd = dev->sensor;
2243 - bool fh_singular;
2244 - int ret;
2245 -
2246 - mutex_lock(&node->lock);
2247 -
2248 - fh_singular = v4l2_fh_is_singular_file(file);
2249 -
2250 - ret = _vb2_fop_release(file, NULL);
2251 -
2252 - if (fh_singular)
2253 - v4l2_subdev_call(sd, core, s_power, 0);
2254 -
2255 - node->open--;
2256 - mutex_unlock(&node->lock);
2257 -
2258 - return ret;
2259 -}
2260 -
2261 -/* unicam capture driver file operations */
2262 -static const struct v4l2_file_operations unicam_fops = {
2263 - .owner = THIS_MODULE,
2264 - .open = unicam_open,
2265 - .release = unicam_release,
2266 - .read = vb2_fop_read,
2267 - .poll = vb2_fop_poll,
2268 - .unlocked_ioctl = video_ioctl2,
2269 - .mmap = vb2_fop_mmap,
2270 -};
2271 -
2272 -/* unicam capture ioctl operations */
2273 -static const struct v4l2_ioctl_ops unicam_ioctl_ops = {
2274 - .vidioc_querycap = unicam_querycap,
2275 - .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid_cap,
2276 - .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid_cap,
2277 - .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid_cap,
2278 - .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid_cap,
2279 -
2280 - .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta_cap,
2281 - .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta_cap,
2282 - .vidioc_s_fmt_meta_cap = unicam_s_fmt_meta_cap,
2283 - .vidioc_try_fmt_meta_cap = unicam_try_fmt_meta_cap,
2284 -
2285 - .vidioc_enum_input = unicam_enum_input,
2286 - .vidioc_g_input = unicam_g_input,
2287 - .vidioc_s_input = unicam_s_input,
2288 -
2289 - .vidioc_querystd = unicam_querystd,
2290 - .vidioc_s_std = unicam_s_std,
2291 - .vidioc_g_std = unicam_g_std,
2292 -
2293 - .vidioc_g_edid = unicam_g_edid,
2294 - .vidioc_s_edid = unicam_s_edid,
2295 -
2296 - .vidioc_enum_framesizes = unicam_enum_framesizes,
2297 - .vidioc_enum_frameintervals = unicam_enum_frameintervals,
2298 -
2299 - .vidioc_g_selection = unicam_g_selection,
2300 - .vidioc_s_selection = unicam_s_selection,
2301 -
2302 - .vidioc_g_parm = unicam_g_parm,
2303 - .vidioc_s_parm = unicam_s_parm,
2304 -
2305 - .vidioc_s_dv_timings = unicam_s_dv_timings,
2306 - .vidioc_g_dv_timings = unicam_g_dv_timings,
2307 - .vidioc_query_dv_timings = unicam_query_dv_timings,
2308 - .vidioc_enum_dv_timings = unicam_enum_dv_timings,
2309 - .vidioc_dv_timings_cap = unicam_dv_timings_cap,
2310 -
2311 - .vidioc_reqbufs = vb2_ioctl_reqbufs,
2312 - .vidioc_create_bufs = vb2_ioctl_create_bufs,
2313 - .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2314 - .vidioc_querybuf = vb2_ioctl_querybuf,
2315 - .vidioc_qbuf = vb2_ioctl_qbuf,
2316 - .vidioc_dqbuf = vb2_ioctl_dqbuf,
2317 - .vidioc_expbuf = vb2_ioctl_expbuf,
2318 - .vidioc_streamon = vb2_ioctl_streamon,
2319 - .vidioc_streamoff = vb2_ioctl_streamoff,
2320 -
2321 - .vidioc_log_status = unicam_log_status,
2322 - .vidioc_subscribe_event = unicam_subscribe_event,
2323 - .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2324 -};
2325 -
2326 -static int
2327 -unicam_async_bound(struct v4l2_async_notifier *notifier,
2328 - struct v4l2_subdev *subdev,
2329 - struct v4l2_async_subdev *asd)
2330 -{
2331 - struct unicam_device *unicam = container_of(notifier->v4l2_dev,
2332 - struct unicam_device, v4l2_dev);
2333 -
2334 - if (unicam->sensor) {
2335 - unicam_info(unicam, "Rejecting subdev %s (Already set!!)",
2336 - subdev->name);
2337 - return 0;
2338 - }
2339 -
2340 - unicam->sensor = subdev;
2341 - unicam_dbg(1, unicam, "Using sensor %s for capture\n", subdev->name);
2342 -
2343 - return 0;
2344 -}
2345 -
2346 -static int register_node(struct unicam_device *unicam, struct unicam_node *node,
2347 - enum v4l2_buf_type type, int pad_id)
2348 -{
2349 - struct video_device *vdev;
2350 - struct vb2_queue *q;
2351 - struct v4l2_mbus_framefmt mbus_fmt = {0};
2352 - const struct unicam_fmt *fmt;
2353 - int ret;
2354 -
2355 - if (unicam->sensor_embedded_data || pad_id != METADATA_PAD) {
2356 - ret = __subdev_get_format(unicam, &mbus_fmt, pad_id);
2357 - if (ret) {
2358 - unicam_err(unicam, "Failed to get_format - ret %d\n",
2359 - ret);
2360 - return ret;
2361 - }
2362 -
2363 - fmt = find_format_by_code(mbus_fmt.code);
2364 - if (!fmt) {
2365 - /* Find the first format that the sensor and unicam both
2366 - * support
2367 - */
2368 - fmt = get_first_supported_format(unicam);
2369 -
2370 - if (!fmt)
2371 - /* No compatible formats */
2372 - return -EINVAL;
2373 -
2374 - mbus_fmt.code = fmt->code;
2375 - ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2376 - if (ret)
2377 - return -EINVAL;
2378 - }
2379 - if (mbus_fmt.field != V4L2_FIELD_NONE) {
2380 - /* Interlaced not supported - disable it now. */
2381 - mbus_fmt.field = V4L2_FIELD_NONE;
2382 - ret = __subdev_set_format(unicam, &mbus_fmt, pad_id);
2383 - if (ret)
2384 - return -EINVAL;
2385 - }
2386 - } else {
2387 - /* Fix this node format as embedded data. */
2388 - fmt = find_format_by_code(MEDIA_BUS_FMT_SENSOR_DATA);
2389 - }
2390 -
2391 - node->dev = unicam;
2392 - node->pad_id = pad_id;
2393 - node->fmt = fmt;
2394 - if (fmt->fourcc) {
2395 - if (fmt->fourcc != V4L2_META_FMT_SENSOR_DATA)
2396 - node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
2397 - else
2398 - node->v_fmt.fmt.meta.dataformat = fmt->fourcc;
2399 - } else {
2400 - node->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
2401 - }
2402 -
2403 - /* Read current subdev format */
2404 - unicam_reset_format(node);
2405 -
2406 - if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2407 - v4l2_std_id tvnorms;
2408 -
2409 - if (WARN_ON(!v4l2_subdev_has_op(unicam->sensor, video,
2410 - g_tvnorms)))
2411 - /*
2412 - * Subdevice should not advertise s_std but not
2413 - * g_tvnorms
2414 - */
2415 - return -EINVAL;
2416 -
2417 - ret = v4l2_subdev_call(unicam->sensor, video,
2418 - g_tvnorms, &tvnorms);
2419 - if (WARN_ON(ret))
2420 - return -EINVAL;
2421 - node->video_dev.tvnorms |= tvnorms;
2422 - }
2423 -
2424 - spin_lock_init(&node->dma_queue_lock);
2425 - mutex_init(&node->lock);
2426 -
2427 - vdev = &node->video_dev;
2428 - if (pad_id == IMAGE_PAD) {
2429 - /* Add controls from the subdevice */
2430 - ret = v4l2_ctrl_add_handler(&node->ctrl_handler,
2431 - unicam->sensor->ctrl_handler, NULL,
2432 - true);
2433 - if (ret < 0)
2434 - return ret;
2435 -
2436 - /*
2437 - * If the sensor subdevice has any controls, associate the node
2438 - * with the ctrl handler to allow access from userland.
2439 - */
2440 - if (!list_empty(&node->ctrl_handler.ctrls))
2441 - vdev->ctrl_handler = &node->ctrl_handler;
2442 - }
2443 -
2444 - q = &node->buffer_queue;
2445 - q->type = type;
2446 - q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2447 - q->drv_priv = node;
2448 - q->ops = &unicam_video_qops;
2449 - q->mem_ops = &vb2_dma_contig_memops;
2450 - q->buf_struct_size = sizeof(struct unicam_buffer);
2451 - q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2452 - q->lock = &node->lock;
2453 - q->min_buffers_needed = 1;
2454 - q->dev = &unicam->pdev->dev;
2455 -
2456 - ret = vb2_queue_init(q);
2457 - if (ret) {
2458 - unicam_err(unicam, "vb2_queue_init() failed\n");
2459 - return ret;
2460 - }
2461 -
2462 - INIT_LIST_HEAD(&node->dma_queue.active);
2463 -
2464 - vdev->release = video_device_release_empty;
2465 - vdev->fops = &unicam_fops;
2466 - vdev->ioctl_ops = &unicam_ioctl_ops;
2467 - vdev->v4l2_dev = &unicam->v4l2_dev;
2468 - vdev->vfl_dir = VFL_DIR_RX;
2469 - vdev->queue = q;
2470 - vdev->lock = &node->lock;
2471 - vdev->device_caps = (pad_id == IMAGE_PAD) ?
2472 - (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING) :
2473 - (V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING);
2474 -
2475 - /* Define the device names */
2476 - snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME,
2477 - node->pad_id == IMAGE_PAD ? "image" : "embedded");
2478 -
2479 - video_set_drvdata(vdev, node);
2480 - vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
2481 -
2482 - node->dummy_buf_cpu_addr = dma_alloc_coherent(&unicam->pdev->dev,
2483 - DUMMY_BUF_SIZE,
2484 - &node->dummy_buf_dma_addr,
2485 - GFP_ATOMIC);
2486 - if (!node->dummy_buf_cpu_addr) {
2487 - unicam_err(unicam, "Unable to allocate dummy buffer.\n");
2488 - return -ENOMEM;
2489 - }
2490 -
2491 - if (node->pad_id == METADATA_PAD) {
2492 - v4l2_disable_ioctl(vdev, VIDIOC_DQEVENT);
2493 - v4l2_disable_ioctl(vdev, VIDIOC_SUBSCRIBE_EVENT);
2494 - v4l2_disable_ioctl(vdev, VIDIOC_UNSUBSCRIBE_EVENT);
2495 - }
2496 - if (node->pad_id == METADATA_PAD ||
2497 - !v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
2498 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_STD);
2499 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_STD);
2500 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUMSTD);
2501 - }
2502 - if (node->pad_id == METADATA_PAD ||
2503 - !v4l2_subdev_has_op(unicam->sensor, video, querystd))
2504 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERYSTD);
2505 - if (node->pad_id == METADATA_PAD ||
2506 - !v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
2507 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_EDID);
2508 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_EDID);
2509 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_DV_TIMINGS_CAP);
2510 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_DV_TIMINGS);
2511 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_DV_TIMINGS);
2512 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_DV_TIMINGS);
2513 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERY_DV_TIMINGS);
2514 - }
2515 - if (node->pad_id == METADATA_PAD ||
2516 - !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
2517 - v4l2_disable_ioctl(&node->video_dev,
2518 - VIDIOC_ENUM_FRAMEINTERVALS);
2519 - if (node->pad_id == METADATA_PAD ||
2520 - !v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
2521 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_PARM);
2522 - if (node->pad_id == METADATA_PAD ||
2523 - !v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
2524 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_PARM);
2525 -
2526 - if (node->pad_id == METADATA_PAD ||
2527 - !v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
2528 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_FRAMESIZES);
2529 -
2530 - if (node->pad_id == METADATA_PAD ||
2531 - !v4l2_subdev_has_op(unicam->sensor, pad, set_selection))
2532 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_SELECTION);
2533 -
2534 - if (node->pad_id == METADATA_PAD ||
2535 - !v4l2_subdev_has_op(unicam->sensor, pad, get_selection))
2536 - v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_SELECTION);
2537 -
2538 - ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
2539 - if (ret) {
2540 - unicam_err(unicam, "Unable to register video device.\n");
2541 - return ret;
2542 - }
2543 - node->registered = 1;
2544 -
2545 - if (unicam->sensor_embedded_data) {
2546 - ret = media_create_pad_link(&unicam->sensor->entity, pad_id,
2547 - &node->video_dev.entity, 0,
2548 - MEDIA_LNK_FL_ENABLED |
2549 - MEDIA_LNK_FL_IMMUTABLE);
2550 - if (ret)
2551 - unicam_err(unicam, "Unable to create pad links.\n");
2552 - }
2553 -
2554 - return ret;
2555 -}
2556 -
2557 -static void unregister_nodes(struct unicam_device *unicam)
2558 -{
2559 - struct unicam_node *node;
2560 - int i;
2561 -
2562 - for (i = 0; i < MAX_NODES; i++) {
2563 - node = &unicam->node[i];
2564 - if (node->dummy_buf_cpu_addr) {
2565 - dma_free_coherent(&unicam->pdev->dev, DUMMY_BUF_SIZE,
2566 - node->dummy_buf_cpu_addr,
2567 - node->dummy_buf_dma_addr);
2568 - }
2569 - if (node->registered) {
2570 - video_unregister_device(&node->video_dev);
2571 - node->registered = 0;
2572 - }
2573 - }
2574 -}
2575 -
2576 -static int unicam_probe_complete(struct unicam_device *unicam)
2577 -{
2578 - int ret;
2579 -
2580 - v4l2_set_subdev_hostdata(unicam->sensor, unicam);
2581 -
2582 - unicam->v4l2_dev.notify = unicam_notify;
2583 -
2584 - unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
2585 - if (!unicam->sensor_config)
2586 - return -ENOMEM;
2587 -
2588 - unicam->sensor_embedded_data = (unicam->sensor->entity.num_pads >= 2);
2589 -
2590 - ret = register_node(unicam, &unicam->node[IMAGE_PAD],
2591 - V4L2_BUF_TYPE_VIDEO_CAPTURE, IMAGE_PAD);
2592 - if (ret) {
2593 - unicam_err(unicam, "Unable to register subdev node 0.\n");
2594 - goto unregister;
2595 - }
2596 -
2597 - ret = register_node(unicam, &unicam->node[METADATA_PAD],
2598 - V4L2_BUF_TYPE_META_CAPTURE, METADATA_PAD);
2599 - if (ret) {
2600 - unicam_err(unicam, "Unable to register subdev node 1.\n");
2601 - goto unregister;
2602 - }
2603 -
2604 - ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
2605 - if (ret) {
2606 - unicam_err(unicam, "Unable to register subdev nodes.\n");
2607 - goto unregister;
2608 - }
2609 -
2610 - return 0;
2611 -
2612 -unregister:
2613 - unregister_nodes(unicam);
2614 -
2615 - return ret;
2616 -}
2617 -
2618 -static int unicam_async_complete(struct v4l2_async_notifier *notifier)
2619 -{
2620 - struct unicam_device *unicam = container_of(notifier->v4l2_dev,
2621 - struct unicam_device, v4l2_dev);
2622 -
2623 - return unicam_probe_complete(unicam);
2624 -}
2625 -
2626 -static const struct v4l2_async_notifier_operations unicam_async_ops = {
2627 - .bound = unicam_async_bound,
2628 - .complete = unicam_async_complete,
2629 -};
2630 -
2631 -static int of_unicam_connect_subdevs(struct unicam_device *dev)
2632 -{
2633 - struct platform_device *pdev = dev->pdev;
2634 - struct device_node *parent, *ep_node = NULL, *remote_ep = NULL,
2635 - *sensor_node = NULL;
2636 - struct v4l2_fwnode_endpoint *ep;
2637 - struct v4l2_async_subdev *asd;
2638 - unsigned int peripheral_data_lanes;
2639 - int ret = -EINVAL;
2640 - unsigned int lane;
2641 -
2642 - parent = pdev->dev.of_node;
2643 -
2644 - asd = &dev->asd;
2645 - ep = &dev->endpoint;
2646 -
2647 - ep_node = of_graph_get_next_endpoint(parent, NULL);
2648 - if (!ep_node) {
2649 - unicam_dbg(3, dev, "can't get next endpoint\n");
2650 - goto cleanup_exit;
2651 - }
2652 -
2653 - unicam_dbg(3, dev, "ep_node is %s\n", ep_node->name);
2654 -
2655 - v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), ep);
2656 -
2657 - for (lane = 0; lane < ep->bus.mipi_csi2.num_data_lanes; lane++) {
2658 - if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2659 - unicam_err(dev, "Local endpoint - data lane reordering not supported\n");
2660 - goto cleanup_exit;
2661 - }
2662 - }
2663 -
2664 - peripheral_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2665 -
2666 - sensor_node = of_graph_get_remote_port_parent(ep_node);
2667 - if (!sensor_node) {
2668 - unicam_dbg(3, dev, "can't get remote parent\n");
2669 - goto cleanup_exit;
2670 - }
2671 - unicam_dbg(3, dev, "sensor_node is %s\n", sensor_node->name);
2672 - asd->match_type = V4L2_ASYNC_MATCH_FWNODE;
2673 - asd->match.fwnode = of_fwnode_handle(sensor_node);
2674 -
2675 - remote_ep = of_graph_get_remote_endpoint(ep_node);
2676 - if (!remote_ep) {
2677 - unicam_dbg(3, dev, "can't get remote-endpoint\n");
2678 - goto cleanup_exit;
2679 - }
2680 - unicam_dbg(3, dev, "remote_ep is %s\n", remote_ep->name);
2681 - v4l2_fwnode_endpoint_parse(of_fwnode_handle(remote_ep), ep);
2682 - unicam_dbg(3, dev, "parsed remote_ep to endpoint. nr_of_link_frequencies %u, bus_type %u\n",
2683 - ep->nr_of_link_frequencies, ep->bus_type);
2684 -
2685 - switch (ep->bus_type) {
2686 - case V4L2_MBUS_CSI2_DPHY:
2687 - if (ep->bus.mipi_csi2.num_data_lanes >
2688 - peripheral_data_lanes) {
2689 - unicam_err(dev, "Subdevice %s wants too many data lanes (%u > %u)\n",
2690 - sensor_node->name,
2691 - ep->bus.mipi_csi2.num_data_lanes,
2692 - peripheral_data_lanes);
2693 - goto cleanup_exit;
2694 - }
2695 - for (lane = 0;
2696 - lane < ep->bus.mipi_csi2.num_data_lanes;
2697 - lane++) {
2698 - if (ep->bus.mipi_csi2.data_lanes[lane] != lane + 1) {
2699 - unicam_err(dev, "Subdevice %s - incompatible data lane config\n",
2700 - sensor_node->name);
2701 - goto cleanup_exit;
2702 - }
2703 - }
2704 - dev->max_data_lanes = ep->bus.mipi_csi2.num_data_lanes;
2705 - dev->bus_flags = ep->bus.mipi_csi2.flags;
2706 - break;
2707 - case V4L2_MBUS_CCP2:
2708 - if (ep->bus.mipi_csi1.clock_lane != 0 ||
2709 - ep->bus.mipi_csi1.data_lane != 1) {
2710 - unicam_err(dev, "Subdevice %s incompatible lane config\n",
2711 - sensor_node->name);
2712 - goto cleanup_exit;
2713 - }
2714 - dev->max_data_lanes = 1;
2715 - dev->bus_flags = ep->bus.mipi_csi1.strobe;
2716 - break;
2717 - default:
2718 - /* Unsupported bus type */
2719 - unicam_err(dev, "sub-device %s is not a CSI2 or CCP2 device %d\n",
2720 - sensor_node->name, ep->bus_type);
2721 - goto cleanup_exit;
2722 - }
2723 -
2724 - /* Store bus type - CSI2 or CCP2 */
2725 - dev->bus_type = ep->bus_type;
2726 - unicam_dbg(3, dev, "bus_type is %d\n", dev->bus_type);
2727 -
2728 - /* Store Virtual Channel number */
2729 - dev->virtual_channel = ep->base.id;
2730 -
2731 - unicam_dbg(3, dev, "v4l2-endpoint: %s\n",
2732 - dev->bus_type == V4L2_MBUS_CSI2_DPHY ? "CSI2" : "CCP2");
2733 - unicam_dbg(3, dev, "Virtual Channel=%d\n", dev->virtual_channel);
2734 - if (dev->bus_type == V4L2_MBUS_CSI2_DPHY)
2735 - unicam_dbg(3, dev, "flags=0x%08x\n", ep->bus.mipi_csi2.flags);
2736 - unicam_dbg(3, dev, "num_data_lanes=%d\n", dev->max_data_lanes);
2737 -
2738 - unicam_dbg(1, dev, "found sub-device %s\n", sensor_node->name);
2739 -
2740 - v4l2_async_notifier_init(&dev->notifier);
2741 -
2742 - ret = v4l2_async_notifier_add_subdev(&dev->notifier, asd);
2743 - if (ret) {
2744 - unicam_err(dev, "Error adding subdevice - ret %d\n", ret);
2745 - goto cleanup_exit;
2746 - }
2747 -
2748 - dev->notifier.ops = &unicam_async_ops;
2749 - ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2750 - &dev->notifier);
2751 - if (ret) {
2752 - unicam_err(dev, "Error registering async notifier - ret %d\n",
2753 - ret);
2754 - ret = -EINVAL;
2755 - }
2756 -
2757 -cleanup_exit:
2758 - if (remote_ep)
2759 - of_node_put(remote_ep);
2760 - if (sensor_node)
2761 - of_node_put(sensor_node);
2762 - if (ep_node)
2763 - of_node_put(ep_node);
2764 -
2765 - return ret;
2766 -}
2767 -
2768 -static int unicam_probe(struct platform_device *pdev)
2769 -{
2770 - struct unicam_cfg *unicam_cfg;
2771 - struct unicam_device *unicam;
2772 - struct v4l2_ctrl_handler *hdl;
2773 - struct resource *res;
2774 - int ret;
2775 -
2776 - unicam = devm_kzalloc(&pdev->dev, sizeof(*unicam), GFP_KERNEL);
2777 - if (!unicam)
2778 - return -ENOMEM;
2779 -
2780 - unicam->pdev = pdev;
2781 - unicam_cfg = &unicam->cfg;
2782 -
2783 - res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2784 - unicam_cfg->base = devm_ioremap_resource(&pdev->dev, res);
2785 - if (IS_ERR(unicam_cfg->base)) {
2786 - unicam_err(unicam, "Failed to get main io block\n");
2787 - return PTR_ERR(unicam_cfg->base);
2788 - }
2789 -
2790 - res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2791 - unicam_cfg->clk_gate_base = devm_ioremap_resource(&pdev->dev, res);
2792 - if (IS_ERR(unicam_cfg->clk_gate_base)) {
2793 - unicam_err(unicam, "Failed to get 2nd io block\n");
2794 - return PTR_ERR(unicam_cfg->clk_gate_base);
2795 - }
2796 -
2797 - unicam->clock = devm_clk_get(&pdev->dev, "lp");
2798 - if (IS_ERR(unicam->clock)) {
2799 - unicam_err(unicam, "Failed to get clock\n");
2800 - return PTR_ERR(unicam->clock);
2801 - }
2802 -
2803 - ret = platform_get_irq(pdev, 0);
2804 - if (ret <= 0) {
2805 - dev_err(&pdev->dev, "No IRQ resource\n");
2806 - return -ENODEV;
2807 - }
2808 -
2809 - ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0,
2810 - "unicam_capture0", unicam);
2811 - if (ret) {
2812 - dev_err(&pdev->dev, "Unable to request interrupt\n");
2813 - return -EINVAL;
2814 - }
2815 -
2816 - unicam->mdev.dev = &pdev->dev;
2817 - strscpy(unicam->mdev.model, UNICAM_MODULE_NAME,
2818 - sizeof(unicam->mdev.model));
2819 - strscpy(unicam->mdev.serial, "", sizeof(unicam->mdev.serial));
2820 - snprintf(unicam->mdev.bus_info, sizeof(unicam->mdev.bus_info),
2821 - "platform:%s %s",
2822 - pdev->dev.driver->name, dev_name(&pdev->dev));
2823 - unicam->mdev.hw_revision = 1;
2824 -
2825 - media_entity_pads_init(&unicam->node[IMAGE_PAD].video_dev.entity, 1,
2826 - &unicam->node[IMAGE_PAD].pad);
2827 - media_entity_pads_init(&unicam->node[METADATA_PAD].video_dev.entity, 1,
2828 - &unicam->node[METADATA_PAD].pad);
2829 - media_device_init(&unicam->mdev);
2830 -
2831 - unicam->v4l2_dev.mdev = &unicam->mdev;
2832 -
2833 - ret = v4l2_device_register(&pdev->dev, &unicam->v4l2_dev);
2834 - if (ret) {
2835 - unicam_err(unicam,
2836 - "Unable to register v4l2 device.\n");
2837 - goto media_cleanup;
2838 - }
2839 -
2840 - ret = media_device_register(&unicam->mdev);
2841 - if (ret < 0) {
2842 - unicam_err(unicam,
2843 - "Unable to register media-controller device.\n");
2844 - goto probe_out_v4l2_unregister;
2845 - }
2846 -
2847 - /* Reserve space for the controls */
2848 - hdl = &unicam->node[IMAGE_PAD].ctrl_handler;
2849 - ret = v4l2_ctrl_handler_init(hdl, 16);
2850 - if (ret < 0)
2851 - goto media_unregister;
2852 -
2853 - /* set the driver data in platform device */
2854 - platform_set_drvdata(pdev, unicam);
2855 -
2856 - ret = of_unicam_connect_subdevs(unicam);
2857 - if (ret) {
2858 - dev_err(&pdev->dev, "Failed to connect subdevs\n");
2859 - goto free_hdl;
2860 - }
2861 -
2862 - /* Enable the block power domain */
2863 - pm_runtime_enable(&pdev->dev);
2864 -
2865 - return 0;
2866 -
2867 -free_hdl:
2868 - v4l2_ctrl_handler_free(hdl);
2869 -media_unregister:
2870 - media_device_unregister(&unicam->mdev);
2871 -probe_out_v4l2_unregister:
2872 - v4l2_device_unregister(&unicam->v4l2_dev);
2873 -media_cleanup:
2874 - media_device_cleanup(&unicam->mdev);
2875 -
2876 - return ret;
2877 -}
2878 -
2879 -static int unicam_remove(struct platform_device *pdev)
2880 -{
2881 - struct unicam_device *unicam = platform_get_drvdata(pdev);
2882 -
2883 - unicam_dbg(2, unicam, "%s\n", __func__);
2884 -
2885 - pm_runtime_disable(&pdev->dev);
2886 -
2887 - v4l2_async_notifier_unregister(&unicam->notifier);
2888 - v4l2_ctrl_handler_free(&unicam->node[IMAGE_PAD].ctrl_handler);
2889 - v4l2_device_unregister(&unicam->v4l2_dev);
2890 - unregister_nodes(unicam);
2891 - if (unicam->sensor_config)
2892 - v4l2_subdev_free_pad_config(unicam->sensor_config);
2893 - media_device_unregister(&unicam->mdev);
2894 - media_device_cleanup(&unicam->mdev);
2895 -
2896 - return 0;
2897 -}
2898 -
2899 -static const struct of_device_id unicam_of_match[] = {
2900 - { .compatible = "brcm,bcm2835-unicam", },
2901 - { /* sentinel */ },
2902 -};
2903 -MODULE_DEVICE_TABLE(of, unicam_of_match);
2904 -
2905 -static struct platform_driver unicam_driver = {
2906 - .probe = unicam_probe,
2907 - .remove = unicam_remove,
2908 - .driver = {
2909 - .name = UNICAM_MODULE_NAME,
2910 - .of_match_table = of_match_ptr(unicam_of_match),
2911 - },
2912 -};
2913 -
2914 -module_platform_driver(unicam_driver);
2915 -
2916 -MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
2917 -MODULE_DESCRIPTION("BCM2835 Unicam driver");
2918 -MODULE_LICENSE("GPL");
2919 -MODULE_VERSION(UNICAM_VERSION);
2920 --- a/drivers/media/platform/bcm2835/vc4-regs-unicam.h
2921 +++ /dev/null
2922 @@ -1,253 +0,0 @@
2923 -/* SPDX-License-Identifier: GPL-2.0-only */
2924 -
2925 -/*
2926 - * Copyright (C) 2017-2020 Raspberry Pi Trading.
2927 - * Dave Stevenson <dave.stevenson@raspberrypi.com>
2928 - */
2929 -
2930 -#ifndef VC4_REGS_UNICAM_H
2931 -#define VC4_REGS_UNICAM_H
2932 -
2933 -/*
2934 - * The following values are taken from files found within the code drop
2935 - * made by Broadcom for the BCM21553 Graphics Driver, predominantly in
2936 - * brcm_usrlib/dag/vmcsx/vcinclude/hardware_vc4.h.
2937 - * They have been modified to be only the register offset.
2938 - */
2939 -#define UNICAM_CTRL 0x000
2940 -#define UNICAM_STA 0x004
2941 -#define UNICAM_ANA 0x008
2942 -#define UNICAM_PRI 0x00c
2943 -#define UNICAM_CLK 0x010
2944 -#define UNICAM_CLT 0x014
2945 -#define UNICAM_DAT0 0x018
2946 -#define UNICAM_DAT1 0x01c
2947 -#define UNICAM_DAT2 0x020
2948 -#define UNICAM_DAT3 0x024
2949 -#define UNICAM_DLT 0x028
2950 -#define UNICAM_CMP0 0x02c
2951 -#define UNICAM_CMP1 0x030
2952 -#define UNICAM_CAP0 0x034
2953 -#define UNICAM_CAP1 0x038
2954 -#define UNICAM_ICTL 0x100
2955 -#define UNICAM_ISTA 0x104
2956 -#define UNICAM_IDI0 0x108
2957 -#define UNICAM_IPIPE 0x10c
2958 -#define UNICAM_IBSA0 0x110
2959 -#define UNICAM_IBEA0 0x114
2960 -#define UNICAM_IBLS 0x118
2961 -#define UNICAM_IBWP 0x11c
2962 -#define UNICAM_IHWIN 0x120
2963 -#define UNICAM_IHSTA 0x124
2964 -#define UNICAM_IVWIN 0x128
2965 -#define UNICAM_IVSTA 0x12c
2966 -#define UNICAM_ICC 0x130
2967 -#define UNICAM_ICS 0x134
2968 -#define UNICAM_IDC 0x138
2969 -#define UNICAM_IDPO 0x13c
2970 -#define UNICAM_IDCA 0x140
2971 -#define UNICAM_IDCD 0x144
2972 -#define UNICAM_IDS 0x148
2973 -#define UNICAM_DCS 0x200
2974 -#define UNICAM_DBSA0 0x204
2975 -#define UNICAM_DBEA0 0x208
2976 -#define UNICAM_DBWP 0x20c
2977 -#define UNICAM_DBCTL 0x300
2978 -#define UNICAM_IBSA1 0x304
2979 -#define UNICAM_IBEA1 0x308
2980 -#define UNICAM_IDI1 0x30c
2981 -#define UNICAM_DBSA1 0x310
2982 -#define UNICAM_DBEA1 0x314
2983 -#define UNICAM_MISC 0x400
2984 -
2985 -/*
2986 - * The following bitmasks are from the kernel released by Broadcom
2987 - * for Android - https://android.googlesource.com/kernel/bcm/
2988 - * The Rhea, Hawaii, and Java chips all contain the same VideoCore4
2989 - * Unicam block as BCM2835, as defined in eg
2990 - * arch/arm/mach-rhea/include/mach/rdb_A0/brcm_rdb_cam.h and similar.
2991 - * Values reworked to use the kernel BIT and GENMASK macros.
2992 - *
2993 - * Some of the bit mnenomics have been amended to match the datasheet.
2994 - */
2995 -/* UNICAM_CTRL Register */
2996 -#define UNICAM_CPE BIT(0)
2997 -#define UNICAM_MEM BIT(1)
2998 -#define UNICAM_CPR BIT(2)
2999 -#define UNICAM_CPM_MASK GENMASK(3, 3)
3000 -#define UNICAM_CPM_CSI2 0
3001 -#define UNICAM_CPM_CCP2 1
3002 -#define UNICAM_SOE BIT(4)
3003 -#define UNICAM_DCM_MASK GENMASK(5, 5)
3004 -#define UNICAM_DCM_STROBE 0
3005 -#define UNICAM_DCM_DATA 1
3006 -#define UNICAM_SLS BIT(6)
3007 -#define UNICAM_PFT_MASK GENMASK(11, 8)
3008 -#define UNICAM_OET_MASK GENMASK(20, 12)
3009 -
3010 -/* UNICAM_STA Register */
3011 -#define UNICAM_SYN BIT(0)
3012 -#define UNICAM_CS BIT(1)
3013 -#define UNICAM_SBE BIT(2)
3014 -#define UNICAM_PBE BIT(3)
3015 -#define UNICAM_HOE BIT(4)
3016 -#define UNICAM_PLE BIT(5)
3017 -#define UNICAM_SSC BIT(6)
3018 -#define UNICAM_CRCE BIT(7)
3019 -#define UNICAM_OES BIT(8)
3020 -#define UNICAM_IFO BIT(9)
3021 -#define UNICAM_OFO BIT(10)
3022 -#define UNICAM_BFO BIT(11)
3023 -#define UNICAM_DL BIT(12)
3024 -#define UNICAM_PS BIT(13)
3025 -#define UNICAM_IS BIT(14)
3026 -#define UNICAM_PI0 BIT(15)
3027 -#define UNICAM_PI1 BIT(16)
3028 -#define UNICAM_FSI_S BIT(17)
3029 -#define UNICAM_FEI_S BIT(18)
3030 -#define UNICAM_LCI_S BIT(19)
3031 -#define UNICAM_BUF0_RDY BIT(20)
3032 -#define UNICAM_BUF0_NO BIT(21)
3033 -#define UNICAM_BUF1_RDY BIT(22)
3034 -#define UNICAM_BUF1_NO BIT(23)
3035 -#define UNICAM_DI BIT(24)
3036 -
3037 -#define UNICAM_STA_MASK_ALL \
3038 - (UNICAM_DL + \
3039 - UNICAM_SBE + \
3040 - UNICAM_PBE + \
3041 - UNICAM_HOE + \
3042 - UNICAM_PLE + \
3043 - UNICAM_SSC + \
3044 - UNICAM_CRCE + \
3045 - UNICAM_IFO + \
3046 - UNICAM_OFO + \
3047 - UNICAM_PS + \
3048 - UNICAM_PI0 + \
3049 - UNICAM_PI1)
3050 -
3051 -/* UNICAM_ANA Register */
3052 -#define UNICAM_APD BIT(0)
3053 -#define UNICAM_BPD BIT(1)
3054 -#define UNICAM_AR BIT(2)
3055 -#define UNICAM_DDL BIT(3)
3056 -#define UNICAM_CTATADJ_MASK GENMASK(7, 4)
3057 -#define UNICAM_PTATADJ_MASK GENMASK(11, 8)
3058 -
3059 -/* UNICAM_PRI Register */
3060 -#define UNICAM_PE BIT(0)
3061 -#define UNICAM_PT_MASK GENMASK(2, 1)
3062 -#define UNICAM_NP_MASK GENMASK(7, 4)
3063 -#define UNICAM_PP_MASK GENMASK(11, 8)
3064 -#define UNICAM_BS_MASK GENMASK(15, 12)
3065 -#define UNICAM_BL_MASK GENMASK(17, 16)
3066 -
3067 -/* UNICAM_CLK Register */
3068 -#define UNICAM_CLE BIT(0)
3069 -#define UNICAM_CLPD BIT(1)
3070 -#define UNICAM_CLLPE BIT(2)
3071 -#define UNICAM_CLHSE BIT(3)
3072 -#define UNICAM_CLTRE BIT(4)
3073 -#define UNICAM_CLAC_MASK GENMASK(8, 5)
3074 -#define UNICAM_CLSTE BIT(29)
3075 -
3076 -/* UNICAM_CLT Register */
3077 -#define UNICAM_CLT1_MASK GENMASK(7, 0)
3078 -#define UNICAM_CLT2_MASK GENMASK(15, 8)
3079 -
3080 -/* UNICAM_DATn Registers */
3081 -#define UNICAM_DLE BIT(0)
3082 -#define UNICAM_DLPD BIT(1)
3083 -#define UNICAM_DLLPE BIT(2)
3084 -#define UNICAM_DLHSE BIT(3)
3085 -#define UNICAM_DLTRE BIT(4)
3086 -#define UNICAM_DLSM BIT(5)
3087 -#define UNICAM_DLFO BIT(28)
3088 -#define UNICAM_DLSTE BIT(29)
3089 -
3090 -#define UNICAM_DAT_MASK_ALL (UNICAM_DLSTE + UNICAM_DLFO)
3091 -
3092 -/* UNICAM_DLT Register */
3093 -#define UNICAM_DLT1_MASK GENMASK(7, 0)
3094 -#define UNICAM_DLT2_MASK GENMASK(15, 8)
3095 -#define UNICAM_DLT3_MASK GENMASK(23, 16)
3096 -
3097 -/* UNICAM_ICTL Register */
3098 -#define UNICAM_FSIE BIT(0)
3099 -#define UNICAM_FEIE BIT(1)
3100 -#define UNICAM_IBOB BIT(2)
3101 -#define UNICAM_FCM BIT(3)
3102 -#define UNICAM_TFC BIT(4)
3103 -#define UNICAM_LIP_MASK GENMASK(6, 5)
3104 -#define UNICAM_LCIE_MASK GENMASK(28, 16)
3105 -
3106 -/* UNICAM_IDI0/1 Register */
3107 -#define UNICAM_ID0_MASK GENMASK(7, 0)
3108 -#define UNICAM_ID1_MASK GENMASK(15, 8)
3109 -#define UNICAM_ID2_MASK GENMASK(23, 16)
3110 -#define UNICAM_ID3_MASK GENMASK(31, 24)
3111 -
3112 -/* UNICAM_ISTA Register */
3113 -#define UNICAM_FSI BIT(0)
3114 -#define UNICAM_FEI BIT(1)
3115 -#define UNICAM_LCI BIT(2)
3116 -
3117 -#define UNICAM_ISTA_MASK_ALL (UNICAM_FSI + UNICAM_FEI + UNICAM_LCI)
3118 -
3119 -/* UNICAM_IPIPE Register */
3120 -#define UNICAM_PUM_MASK GENMASK(2, 0)
3121 - /* Unpacking modes */
3122 - #define UNICAM_PUM_NONE 0
3123 - #define UNICAM_PUM_UNPACK6 1
3124 - #define UNICAM_PUM_UNPACK7 2
3125 - #define UNICAM_PUM_UNPACK8 3
3126 - #define UNICAM_PUM_UNPACK10 4
3127 - #define UNICAM_PUM_UNPACK12 5
3128 - #define UNICAM_PUM_UNPACK14 6
3129 - #define UNICAM_PUM_UNPACK16 7
3130 -#define UNICAM_DDM_MASK GENMASK(6, 3)
3131 -#define UNICAM_PPM_MASK GENMASK(9, 7)
3132 - /* Packing modes */
3133 - #define UNICAM_PPM_NONE 0
3134 - #define UNICAM_PPM_PACK8 1
3135 - #define UNICAM_PPM_PACK10 2
3136 - #define UNICAM_PPM_PACK12 3
3137 - #define UNICAM_PPM_PACK14 4
3138 - #define UNICAM_PPM_PACK16 5
3139 -#define UNICAM_DEM_MASK GENMASK(11, 10)
3140 -#define UNICAM_DEBL_MASK GENMASK(14, 12)
3141 -#define UNICAM_ICM_MASK GENMASK(16, 15)
3142 -#define UNICAM_IDM_MASK GENMASK(17, 17)
3143 -
3144 -/* UNICAM_ICC Register */
3145 -#define UNICAM_ICFL_MASK GENMASK(4, 0)
3146 -#define UNICAM_ICFH_MASK GENMASK(9, 5)
3147 -#define UNICAM_ICST_MASK GENMASK(12, 10)
3148 -#define UNICAM_ICLT_MASK GENMASK(15, 13)
3149 -#define UNICAM_ICLL_MASK GENMASK(31, 16)
3150 -
3151 -/* UNICAM_DCS Register */
3152 -#define UNICAM_DIE BIT(0)
3153 -#define UNICAM_DIM BIT(1)
3154 -#define UNICAM_DBOB BIT(3)
3155 -#define UNICAM_FDE BIT(4)
3156 -#define UNICAM_LDP BIT(5)
3157 -#define UNICAM_EDL_MASK GENMASK(15, 8)
3158 -
3159 -/* UNICAM_DBCTL Register */
3160 -#define UNICAM_DBEN BIT(0)
3161 -#define UNICAM_BUF0_IE BIT(1)
3162 -#define UNICAM_BUF1_IE BIT(2)
3163 -
3164 -/* UNICAM_CMP[0,1] register */
3165 -#define UNICAM_PCE BIT(31)
3166 -#define UNICAM_GI BIT(9)
3167 -#define UNICAM_CPH BIT(8)
3168 -#define UNICAM_PCVC_MASK GENMASK(7, 6)
3169 -#define UNICAM_PCDT_MASK GENMASK(5, 0)
3170 -
3171 -/* UNICAM_MISC register */
3172 -#define UNICAM_FL0 BIT(6)
3173 -#define UNICAM_FL1 BIT(9)
3174 -
3175 -#endif