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