1 From f69e7d7aaaea4c6531b357ac2a1a522b400c7cd0 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Tue, 7 Apr 2020 10:42:14 +0100
4 Subject: [PATCH] media: bcm2835-unicam: Add support for mulitple
7 Move device node specific state out of the device state structure and
8 into a new node structure. This separation will be needed for future
9 changes where we will add an embedded data node to the driver to work
10 alongside the existing image data node.
12 Currently only use a single image node, so this commit does not add
13 any functional changes.
15 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 .../media/platform/bcm2835/bcm2835-unicam.c | 484 ++++++++++--------
18 1 file changed, 283 insertions(+), 201 deletions(-)
20 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
21 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
22 @@ -109,7 +109,8 @@ MODULE_PARM_DESC(debug, "Debug level 0-3
23 /* Define a nominal minimum image size */
27 +/* Maximum number of simulataneous streams Uncaim can handle. */
30 * struct unicam_fmt - Unicam media bus format information
31 * @pixelformat: V4L2 pixel format FCC identifier. 0 if n/a.
32 @@ -346,11 +347,37 @@ struct unicam_cfg {
34 #define MAX_POSSIBLE_PIX_FMTS (ARRAY_SIZE(formats))
36 -struct unicam_device {
37 - /* V4l2 specific parameters */
40 + unsigned int pad_id;
41 + /* Pointer pointing to current v4l2_buffer */
42 + struct unicam_buffer *cur_frm;
43 + /* Pointer pointing to next v4l2_buffer */
44 + struct unicam_buffer *next_frm;
46 + const struct unicam_fmt *fmt;
47 + /* Used to store current pixel format */
48 + struct v4l2_format v_fmt;
49 + /* Used to store current mbus frame format */
50 + struct v4l2_mbus_framefmt m_fmt;
51 + /* Buffer queue used in video-buf */
52 + struct vb2_queue buffer_queue;
53 + /* Queue of filled frames */
54 + struct unicam_dmaqueue dma_queue;
55 + /* IRQ lock for DMA queue */
56 + spinlock_t dma_queue_lock;
57 + /* lock used to access this structure */
59 /* Identifies video device for this channel */
60 struct video_device video_dev;
61 + /* Pointer to the parent handle */
62 + struct unicam_device *dev;
63 + struct media_pad pad;
64 struct v4l2_ctrl_handler ctrl_handler;
67 +struct unicam_device {
68 + /* V4l2 specific parameters */
70 struct v4l2_fwnode_endpoint endpoint;
72 @@ -363,7 +390,6 @@ struct unicam_device {
74 struct v4l2_device v4l2_dev;
75 struct media_device mdev;
76 - struct media_pad pad;
79 struct platform_device *pdev;
80 @@ -378,18 +404,6 @@ struct unicam_device {
81 /* current input at the sub device */
84 - /* Pointer pointing to current v4l2_buffer */
85 - struct unicam_buffer *cur_frm;
86 - /* Pointer pointing to next v4l2_buffer */
87 - struct unicam_buffer *next_frm;
90 - const struct unicam_fmt *fmt;
91 - /* Used to store current pixel format */
92 - struct v4l2_format v_fmt;
93 - /* Used to store current mbus frame format */
94 - struct v4l2_mbus_framefmt m_fmt;
96 unsigned int virtual_channel;
97 enum v4l2_mbus_type bus_type;
99 @@ -401,20 +415,10 @@ struct unicam_device {
100 unsigned int active_data_lanes;
102 struct v4l2_rect crop;
104 - /* Currently selected input on subdev */
107 - /* Buffer queue used in video-buf */
108 - struct vb2_queue buffer_queue;
109 - /* Queue of filled frames */
110 - struct unicam_dmaqueue dma_queue;
111 - /* IRQ lock for DMA queue */
112 - spinlock_t dma_queue_lock;
113 - /* lock used to access this structure */
115 /* Flag to denote that we are processing buffers */
118 + struct unicam_node node[MAX_NODES];
121 /* Hardware access */
122 @@ -526,10 +530,11 @@ static inline unsigned int bytes_per_lin
125 static int __subdev_get_format(struct unicam_device *dev,
126 - struct v4l2_mbus_framefmt *fmt)
127 + struct v4l2_mbus_framefmt *fmt, int pad_id)
129 struct v4l2_subdev_format sd_fmt = {
130 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
135 @@ -598,29 +603,30 @@ static int unicam_calc_format_size_bpl(s
139 -static int unicam_reset_format(struct unicam_device *dev)
140 +static int unicam_reset_format(struct unicam_node *node)
142 + struct unicam_device *dev = node->dev;
143 struct v4l2_mbus_framefmt mbus_fmt;
146 - ret = __subdev_get_format(dev, &mbus_fmt);
147 + ret = __subdev_get_format(dev, &mbus_fmt, node->pad_id);
149 unicam_err(dev, "Failed to get_format - ret %d\n", ret);
153 - if (mbus_fmt.code != dev->fmt->code) {
154 + if (mbus_fmt.code != dev->node[0].fmt->code) {
155 unicam_err(dev, "code mismatch - fmt->code %08x, mbus_fmt.code %08x\n",
156 - dev->fmt->code, mbus_fmt.code);
157 + dev->node[0].fmt->code, mbus_fmt.code);
161 - v4l2_fill_pix_format(&dev->v_fmt.fmt.pix, &mbus_fmt);
162 - dev->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
163 + v4l2_fill_pix_format(&dev->node[0].v_fmt.fmt.pix, &mbus_fmt);
164 + dev->node[0].v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
166 - unicam_calc_format_size_bpl(dev, dev->fmt, &dev->v_fmt);
167 + unicam_calc_format_size_bpl(dev, dev->node[0].fmt, &dev->node[0].v_fmt);
169 - dev->m_fmt = mbus_fmt;
170 + dev->node[0].m_fmt = mbus_fmt;
174 @@ -635,14 +641,14 @@ static void unicam_wr_dma_addr(struct un
176 reg_write(&dev->cfg, UNICAM_IBSA0, dmaaddr);
177 reg_write(&dev->cfg, UNICAM_IBEA0,
178 - dmaaddr + dev->v_fmt.fmt.pix.sizeimage);
179 + dmaaddr + dev->node[0].v_fmt.fmt.pix.sizeimage);
182 static inline unsigned int unicam_get_lines_done(struct unicam_device *dev)
184 dma_addr_t start_addr, cur_addr;
185 - unsigned int stride = dev->v_fmt.fmt.pix.bytesperline;
186 - struct unicam_buffer *frm = dev->cur_frm;
187 + unsigned int stride = dev->node[0].v_fmt.fmt.pix.bytesperline;
188 + struct unicam_buffer *frm = dev->node[0].cur_frm;
192 @@ -654,12 +660,12 @@ static inline unsigned int unicam_get_li
194 static inline void unicam_schedule_next_buffer(struct unicam_device *dev)
196 - struct unicam_dmaqueue *dma_q = &dev->dma_queue;
197 + struct unicam_dmaqueue *dma_q = &dev->node[0].dma_queue;
198 struct unicam_buffer *buf;
201 buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
202 - dev->next_frm = buf;
203 + dev->node[0].next_frm = buf;
204 list_del(&buf->list);
206 addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
207 @@ -668,11 +674,11 @@ static inline void unicam_schedule_next_
209 static inline void unicam_process_buffer_complete(struct unicam_device *dev)
211 - dev->cur_frm->vb.field = dev->m_fmt.field;
212 - dev->cur_frm->vb.sequence = dev->sequence++;
213 + dev->node[0].cur_frm->vb.field = dev->node[0].m_fmt.field;
214 + dev->node[0].cur_frm->vb.sequence = dev->sequence++;
216 - vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
217 - dev->cur_frm = dev->next_frm;
218 + vb2_buffer_done(&dev->node[0].cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
219 + dev->node[0].cur_frm = dev->node[0].next_frm;
223 @@ -687,7 +693,7 @@ static irqreturn_t unicam_isr(int irq, v
225 struct unicam_device *unicam = (struct unicam_device *)dev;
226 struct unicam_cfg *cfg = &unicam->cfg;
227 - struct unicam_dmaqueue *dma_q = &unicam->dma_queue;
228 + struct unicam_dmaqueue *dma_q = &unicam->node[0].dma_queue;
229 unsigned int lines_done = unicam_get_lines_done(dev);
230 unsigned int sequence = unicam->sequence;
232 @@ -720,8 +726,9 @@ static irqreturn_t unicam_isr(int irq, v
233 * Timestamp is to be when the first data byte was captured,
236 - if (unicam->cur_frm)
237 - unicam->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
238 + if (unicam->node[0].cur_frm)
239 + unicam->node[0].cur_frm->vb.vb2_buf.timestamp =
242 if (ista & UNICAM_FEI || sta & UNICAM_PI0) {
244 @@ -729,7 +736,8 @@ static irqreturn_t unicam_isr(int irq, v
245 * stop the peripheral. Overwrite the frame we've just
248 - if (unicam->cur_frm && unicam->cur_frm != unicam->next_frm)
249 + if (unicam->node[0].cur_frm &&
250 + unicam->node[0].cur_frm != unicam->node[0].next_frm)
251 unicam_process_buffer_complete(unicam);
254 @@ -738,11 +746,11 @@ static irqreturn_t unicam_isr(int irq, v
257 if (ista & (UNICAM_FSI | UNICAM_LCI) && !(ista & UNICAM_FEI)) {
258 - spin_lock(&unicam->dma_queue_lock);
259 + spin_lock(&unicam->node[0].dma_queue_lock);
260 if (!list_empty(&dma_q->active) &&
261 - unicam->cur_frm == unicam->next_frm)
262 + unicam->node[0].cur_frm == unicam->node[0].next_frm)
263 unicam_schedule_next_buffer(unicam);
264 - spin_unlock(&unicam->dma_queue_lock);
265 + spin_unlock(&unicam->node[0].dma_queue_lock);
268 if (reg_read(&unicam->cfg, UNICAM_ICTL) & UNICAM_FCM) {
269 @@ -756,7 +764,8 @@ static irqreturn_t unicam_isr(int irq, v
270 static int unicam_querycap(struct file *file, void *priv,
271 struct v4l2_capability *cap)
273 - struct unicam_device *dev = video_drvdata(file);
274 + struct unicam_node *node = video_drvdata(file);
275 + struct unicam_device *dev = node->dev;
277 strlcpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver));
278 strlcpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card));
279 @@ -770,7 +779,8 @@ static int unicam_querycap(struct file *
280 static int unicam_enum_fmt_vid_cap(struct file *file, void *priv,
281 struct v4l2_fmtdesc *f)
283 - struct unicam_device *dev = video_drvdata(file);
284 + struct unicam_node *node = video_drvdata(file);
285 + struct unicam_device *dev = node->dev;
286 struct v4l2_subdev_mbus_code_enum mbus_code;
287 const struct unicam_fmt *fmt = NULL;
289 @@ -815,9 +825,9 @@ static int unicam_enum_fmt_vid_cap(struc
290 static int unicam_g_fmt_vid_cap(struct file *file, void *priv,
291 struct v4l2_format *f)
293 - struct unicam_device *dev = video_drvdata(file);
294 + struct unicam_node *node = video_drvdata(file);
301 @@ -859,9 +869,11 @@ const struct unicam_fmt *get_first_suppo
302 static int unicam_try_fmt_vid_cap(struct file *file, void *priv,
303 struct v4l2_format *f)
305 - struct unicam_device *dev = video_drvdata(file);
306 + struct unicam_node *node = video_drvdata(file);
307 + struct unicam_device *dev = node->dev;
308 struct v4l2_subdev_format sd_fmt = {
309 .which = V4L2_SUBDEV_FORMAT_TRY,
312 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
313 const struct unicam_fmt *fmt;
314 @@ -939,8 +951,9 @@ static int unicam_try_fmt_vid_cap(struct
315 static int unicam_s_fmt_vid_cap(struct file *file, void *priv,
316 struct v4l2_format *f)
318 - struct unicam_device *dev = video_drvdata(file);
319 - struct vb2_queue *q = &dev->buffer_queue;
320 + struct unicam_node *node = video_drvdata(file);
321 + struct unicam_device *dev = node->dev;
322 + struct vb2_queue *q = &node->buffer_queue;
323 struct v4l2_mbus_framefmt mbus_fmt = {0};
324 const struct unicam_fmt *fmt;
326 @@ -985,17 +998,18 @@ static int unicam_s_fmt_vid_cap(struct f
331 - dev->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
332 - dev->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
333 - unicam_reset_format(dev);
335 - unicam_dbg(3, dev, "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
336 - __func__, dev->v_fmt.fmt.pix.width,
337 - dev->v_fmt.fmt.pix.height, mbus_fmt.code,
338 - dev->v_fmt.fmt.pix.pixelformat);
340 + node->v_fmt.fmt.pix.pixelformat = f->fmt.pix.pixelformat;
341 + node->v_fmt.fmt.pix.bytesperline = f->fmt.pix.bytesperline;
342 + unicam_reset_format(node);
345 + "%s %dx%d, mbus_fmt 0x%08X, V4L2 pix 0x%08X.\n",
346 + __func__, node->v_fmt.fmt.pix.width,
347 + node->v_fmt.fmt.pix.height, mbus_fmt.code,
348 + node->v_fmt.fmt.pix.pixelformat);
355 @@ -1006,8 +1020,9 @@ static int unicam_queue_setup(struct vb2
356 unsigned int sizes[],
357 struct device *alloc_devs[])
359 - struct unicam_device *dev = vb2_get_drv_priv(vq);
360 - unsigned int size = dev->v_fmt.fmt.pix.sizeimage;
361 + struct unicam_node *node = vb2_get_drv_priv(vq);
362 + struct unicam_device *dev = node->dev;
363 + unsigned int size = node->v_fmt.fmt.pix.sizeimage;
365 if (vq->num_buffers + *nbuffers < 3)
366 *nbuffers = 3 - vq->num_buffers;
367 @@ -1029,15 +1044,16 @@ static int unicam_queue_setup(struct vb2
369 static int unicam_buffer_prepare(struct vb2_buffer *vb)
371 - struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
372 + struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
373 + struct unicam_device *dev = node->dev;
374 struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
378 - if (WARN_ON(!dev->fmt))
379 + if (WARN_ON(!node->fmt))
382 - size = dev->v_fmt.fmt.pix.sizeimage;
383 + size = node->v_fmt.fmt.pix.sizeimage;
384 if (vb2_plane_size(vb, 0) < size) {
385 unicam_err(dev, "data will not fit into plane (%lu < %lu)\n",
386 vb2_plane_size(vb, 0), size);
387 @@ -1050,15 +1066,15 @@ static int unicam_buffer_prepare(struct
389 static void unicam_buffer_queue(struct vb2_buffer *vb)
391 - struct unicam_device *dev = vb2_get_drv_priv(vb->vb2_queue);
392 + struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue);
393 struct unicam_buffer *buf = container_of(vb, struct unicam_buffer,
395 - struct unicam_dmaqueue *dma_queue = &dev->dma_queue;
396 + struct unicam_dmaqueue *dma_queue = &node->dma_queue;
397 unsigned long flags = 0;
399 - spin_lock_irqsave(&dev->dma_queue_lock, flags);
400 + spin_lock_irqsave(&node->dma_queue_lock, flags);
401 list_add_tail(&buf->list, &dma_queue->active);
402 - spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
403 + spin_unlock_irqrestore(&node->dma_queue_lock, flags);
406 static void unicam_set_packing_config(struct unicam_device *dev)
407 @@ -1066,11 +1082,12 @@ static void unicam_set_packing_config(st
411 - if (dev->v_fmt.fmt.pix.pixelformat == dev->fmt->fourcc) {
412 + if (dev->node[0].v_fmt.fmt.pix.pixelformat ==
413 + dev->node[0].fmt->fourcc) {
414 unpack = UNICAM_PUM_NONE;
415 pack = UNICAM_PPM_NONE;
417 - switch (dev->fmt->depth) {
418 + switch (dev->node[0].fmt->depth) {
420 unpack = UNICAM_PUM_UNPACK8;
422 @@ -1108,17 +1125,17 @@ static void unicam_cfg_image_id(struct u
423 if (dev->bus_type == V4L2_MBUS_CSI2_DPHY) {
425 reg_write(cfg, UNICAM_IDI0,
426 - (dev->virtual_channel << 6) | dev->fmt->csi_dt);
427 + (dev->virtual_channel << 6) | dev->node[0].fmt->csi_dt);
430 - reg_write(cfg, UNICAM_IDI0, (0x80 | dev->fmt->csi_dt));
431 + reg_write(cfg, UNICAM_IDI0, (0x80 | dev->node[0].fmt->csi_dt));
435 static void unicam_start_rx(struct unicam_device *dev, unsigned long addr)
437 struct unicam_cfg *cfg = &dev->cfg;
438 - int line_int_freq = dev->v_fmt.fmt.pix.height >> 2;
439 + int line_int_freq = dev->node[0].v_fmt.fmt.pix.height >> 2;
443 @@ -1266,7 +1283,8 @@ static void unicam_start_rx(struct unica
444 reg_write(cfg, UNICAM_DAT3, val);
447 - reg_write(&dev->cfg, UNICAM_IBLS, dev->v_fmt.fmt.pix.bytesperline);
448 + reg_write(&dev->cfg, UNICAM_IBLS,
449 + dev->node[0].v_fmt.fmt.pix.bytesperline);
450 unicam_wr_dma_addr(dev, addr);
451 unicam_set_packing_config(dev);
452 unicam_cfg_image_id(dev);
453 @@ -1327,21 +1345,22 @@ static void unicam_disable(struct unicam
455 static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count)
457 - struct unicam_device *dev = vb2_get_drv_priv(vq);
458 - struct unicam_dmaqueue *dma_q = &dev->dma_queue;
459 + struct unicam_node *node = vb2_get_drv_priv(vq);
460 + struct unicam_device *dev = node->dev;
461 + struct unicam_dmaqueue *dma_q = &node->dma_queue;
462 struct unicam_buffer *buf, *tmp;
463 unsigned long addr = 0;
467 - spin_lock_irqsave(&dev->dma_queue_lock, flags);
468 + spin_lock_irqsave(&node->dma_queue_lock, flags);
469 buf = list_entry(dma_q->active.next, struct unicam_buffer, list);
470 - dev->cur_frm = buf;
471 - dev->next_frm = buf;
472 + node->cur_frm = buf;
473 + node->next_frm = buf;
474 list_del(&buf->list);
475 - spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
476 + spin_unlock_irqrestore(&node->dma_queue_lock, flags);
478 - addr = vb2_dma_contig_plane_dma_addr(&dev->cur_frm->vb.vb2_buf, 0);
479 + addr = vb2_dma_contig_plane_dma_addr(&node->cur_frm->vb.vb2_buf, 0);
482 ret = unicam_runtime_get(dev);
483 @@ -1411,20 +1430,21 @@ err_release_buffers:
484 list_del(&buf->list);
485 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
487 - if (dev->cur_frm != dev->next_frm)
488 - vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
489 + if (node->cur_frm != node->next_frm)
490 + vb2_buffer_done(&node->next_frm->vb.vb2_buf,
491 VB2_BUF_STATE_QUEUED);
492 - vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
493 - dev->next_frm = NULL;
494 - dev->cur_frm = NULL;
495 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
496 + node->next_frm = NULL;
497 + node->cur_frm = NULL;
502 static void unicam_stop_streaming(struct vb2_queue *vq)
504 - struct unicam_device *dev = vb2_get_drv_priv(vq);
505 - struct unicam_dmaqueue *dma_q = &dev->dma_queue;
506 + struct unicam_node *node = vb2_get_drv_priv(vq);
507 + struct unicam_device *dev = node->dev;
508 + struct unicam_dmaqueue *dma_q = &node->dma_queue;
509 struct unicam_buffer *buf, *tmp;
512 @@ -1434,22 +1454,24 @@ static void unicam_stop_streaming(struct
515 /* Release all active buffers */
516 - spin_lock_irqsave(&dev->dma_queue_lock, flags);
517 + spin_lock_irqsave(&node->dma_queue_lock, flags);
518 list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
519 list_del(&buf->list);
520 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
523 - if (dev->cur_frm == dev->next_frm) {
524 - vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
525 + if (node->cur_frm == node->next_frm) {
526 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
527 + VB2_BUF_STATE_ERROR);
529 - vb2_buffer_done(&dev->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
530 - vb2_buffer_done(&dev->next_frm->vb.vb2_buf,
531 + vb2_buffer_done(&node->cur_frm->vb.vb2_buf,
532 + VB2_BUF_STATE_ERROR);
533 + vb2_buffer_done(&node->next_frm->vb.vb2_buf,
534 VB2_BUF_STATE_ERROR);
536 - dev->cur_frm = NULL;
537 - dev->next_frm = NULL;
538 - spin_unlock_irqrestore(&dev->dma_queue_lock, flags);
539 + node->cur_frm = NULL;
540 + node->next_frm = NULL;
541 + spin_unlock_irqrestore(&node->dma_queue_lock, flags);
543 clk_disable_unprepare(dev->clock);
544 unicam_runtime_put(dev);
545 @@ -1458,7 +1480,8 @@ static void unicam_stop_streaming(struct
546 static int unicam_enum_input(struct file *file, void *priv,
547 struct v4l2_input *inp)
549 - struct unicam_device *dev = video_drvdata(file);
550 + struct unicam_node *node = video_drvdata(file);
551 + struct unicam_device *dev = node->dev;
555 @@ -1506,21 +1529,24 @@ static int unicam_s_input(struct file *f
556 static int unicam_querystd(struct file *file, void *priv,
559 - struct unicam_device *dev = video_drvdata(file);
560 + struct unicam_node *node = video_drvdata(file);
561 + struct unicam_device *dev = node->dev;
563 return v4l2_subdev_call(dev->sensor, video, querystd, std);
566 static int unicam_g_std(struct file *file, void *priv, v4l2_std_id *std)
568 - struct unicam_device *dev = video_drvdata(file);
569 + struct unicam_node *node = video_drvdata(file);
570 + struct unicam_device *dev = node->dev;
572 return v4l2_subdev_call(dev->sensor, video, g_std, std);
575 static int unicam_s_std(struct file *file, void *priv, v4l2_std_id std)
577 - struct unicam_device *dev = video_drvdata(file);
578 + struct unicam_node *node = video_drvdata(file);
579 + struct unicam_device *dev = node->dev;
581 v4l2_std_id current_std;
583 @@ -1531,29 +1557,31 @@ static int unicam_s_std(struct file *fil
584 if (std == current_std)
587 - if (vb2_is_busy(&dev->buffer_queue))
588 + if (vb2_is_busy(&node->buffer_queue))
591 ret = v4l2_subdev_call(dev->sensor, video, s_std, std);
593 /* Force recomputation of bytesperline */
594 - dev->v_fmt.fmt.pix.bytesperline = 0;
595 + node->v_fmt.fmt.pix.bytesperline = 0;
597 - unicam_reset_format(dev);
598 + unicam_reset_format(node);
603 static int unicam_s_edid(struct file *file, void *priv, struct v4l2_edid *edid)
605 - struct unicam_device *dev = video_drvdata(file);
606 + struct unicam_node *node = video_drvdata(file);
607 + struct unicam_device *dev = node->dev;
609 return v4l2_subdev_call(dev->sensor, pad, set_edid, edid);
612 static int unicam_g_edid(struct file *file, void *priv, struct v4l2_edid *edid)
614 - struct unicam_device *dev = video_drvdata(file);
615 + struct unicam_node *node = video_drvdata(file);
616 + struct unicam_device *dev = node->dev;
618 return v4l2_subdev_call(dev->sensor, pad, get_edid, edid);
620 @@ -1561,7 +1589,8 @@ static int unicam_g_edid(struct file *fi
621 static int unicam_enum_framesizes(struct file *file, void *priv,
622 struct v4l2_frmsizeenum *fsize)
624 - struct unicam_device *dev = video_drvdata(file);
625 + struct unicam_node *node = video_drvdata(file);
626 + struct unicam_device *dev = node->dev;
627 const struct unicam_fmt *fmt;
628 struct v4l2_subdev_frame_size_enum fse;
630 @@ -1596,7 +1625,8 @@ static int unicam_enum_framesizes(struct
631 static int unicam_enum_frameintervals(struct file *file, void *priv,
632 struct v4l2_frmivalenum *fival)
634 - struct unicam_device *dev = video_drvdata(file);
635 + struct unicam_node *node = video_drvdata(file);
636 + struct unicam_device *dev = node->dev;
637 const struct unicam_fmt *fmt;
638 struct v4l2_subdev_frame_interval_enum fie = {
639 .index = fival->index,
640 @@ -1624,14 +1654,16 @@ static int unicam_enum_frameintervals(st
642 static int unicam_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
644 - struct unicam_device *dev = video_drvdata(file);
645 + struct unicam_node *node = video_drvdata(file);
646 + struct unicam_device *dev = node->dev;
648 return v4l2_g_parm_cap(video_devdata(file), dev->sensor, a);
651 static int unicam_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
653 - struct unicam_device *dev = video_drvdata(file);
654 + struct unicam_node *node = video_drvdata(file);
655 + struct unicam_device *dev = node->dev;
657 return v4l2_s_parm_cap(video_devdata(file), dev->sensor, a);
659 @@ -1639,7 +1671,8 @@ static int unicam_s_parm(struct file *fi
660 static int unicam_g_dv_timings(struct file *file, void *priv,
661 struct v4l2_dv_timings *timings)
663 - struct unicam_device *dev = video_drvdata(file);
664 + struct unicam_node *node = video_drvdata(file);
665 + struct unicam_device *dev = node->dev;
667 return v4l2_subdev_call(dev->sensor, video, g_dv_timings, timings);
669 @@ -1647,7 +1680,8 @@ static int unicam_g_dv_timings(struct fi
670 static int unicam_s_dv_timings(struct file *file, void *priv,
671 struct v4l2_dv_timings *timings)
673 - struct unicam_device *dev = video_drvdata(file);
674 + struct unicam_node *node = video_drvdata(file);
675 + struct unicam_device *dev = node->dev;
676 struct v4l2_dv_timings current_timings;
679 @@ -1657,15 +1691,15 @@ static int unicam_s_dv_timings(struct fi
680 if (v4l2_match_dv_timings(timings, ¤t_timings, 0, false))
683 - if (vb2_is_busy(&dev->buffer_queue))
684 + if (vb2_is_busy(&node->buffer_queue))
687 ret = v4l2_subdev_call(dev->sensor, video, s_dv_timings, timings);
689 /* Force recomputation of bytesperline */
690 - dev->v_fmt.fmt.pix.bytesperline = 0;
691 + node->v_fmt.fmt.pix.bytesperline = 0;
693 - unicam_reset_format(dev);
694 + unicam_reset_format(node);
698 @@ -1673,7 +1707,8 @@ static int unicam_s_dv_timings(struct fi
699 static int unicam_query_dv_timings(struct file *file, void *priv,
700 struct v4l2_dv_timings *timings)
702 - struct unicam_device *dev = video_drvdata(file);
703 + struct unicam_node *node = video_drvdata(file);
704 + struct unicam_device *dev = node->dev;
706 return v4l2_subdev_call(dev->sensor, video, query_dv_timings, timings);
708 @@ -1681,7 +1716,8 @@ static int unicam_query_dv_timings(struc
709 static int unicam_enum_dv_timings(struct file *file, void *priv,
710 struct v4l2_enum_dv_timings *timings)
712 - struct unicam_device *dev = video_drvdata(file);
713 + struct unicam_node *node = video_drvdata(file);
714 + struct unicam_device *dev = node->dev;
716 return v4l2_subdev_call(dev->sensor, pad, enum_dv_timings, timings);
718 @@ -1689,7 +1725,8 @@ static int unicam_enum_dv_timings(struct
719 static int unicam_dv_timings_cap(struct file *file, void *priv,
720 struct v4l2_dv_timings_cap *cap)
722 - struct unicam_device *dev = video_drvdata(file);
723 + struct unicam_node *node = video_drvdata(file);
724 + struct unicam_device *dev = node->dev;
726 return v4l2_subdev_call(dev->sensor, pad, dv_timings_cap, cap);
728 @@ -1707,7 +1744,8 @@ static int unicam_subscribe_event(struct
730 static int unicam_log_status(struct file *file, void *fh)
732 - struct unicam_device *dev = video_drvdata(file);
733 + struct unicam_node *node = video_drvdata(file);
734 + struct unicam_device *dev = node->dev;
735 struct unicam_cfg *cfg = &dev->cfg;
738 @@ -1716,10 +1754,10 @@ static int unicam_log_status(struct file
740 unicam_info(dev, "-----Receiver status-----\n");
741 unicam_info(dev, "V4L2 width/height: %ux%u\n",
742 - dev->v_fmt.fmt.pix.width, dev->v_fmt.fmt.pix.height);
743 - unicam_info(dev, "Mediabus format: %08x\n", dev->fmt->code);
744 + node->v_fmt.fmt.pix.width, node->v_fmt.fmt.pix.height);
745 + unicam_info(dev, "Mediabus format: %08x\n", node->fmt->code);
746 unicam_info(dev, "V4L2 format: %08x\n",
747 - dev->v_fmt.fmt.pix.pixelformat);
748 + node->v_fmt.fmt.pix.pixelformat);
749 reg = reg_read(&dev->cfg, UNICAM_IPIPE);
750 unicam_info(dev, "Unpacking/packing: %u / %u\n",
751 get_field(reg, UNICAM_PUM_MASK),
752 @@ -1744,7 +1782,7 @@ static void unicam_notify(struct v4l2_su
754 switch (notification) {
755 case V4L2_DEVICE_NOTIFY_EVENT:
756 - v4l2_event_queue(&dev->video_dev, arg);
757 + v4l2_event_queue(&dev->node[0].video_dev, arg);
761 @@ -1767,10 +1805,11 @@ static const struct vb2_ops unicam_video
763 static int unicam_open(struct file *file)
765 - struct unicam_device *dev = video_drvdata(file);
766 + struct unicam_node *node = video_drvdata(file);
767 + struct unicam_device *dev = node->dev;
770 - mutex_lock(&dev->lock);
771 + mutex_lock(&node->lock);
773 ret = v4l2_fh_open(file);
775 @@ -1790,18 +1829,19 @@ static int unicam_open(struct file *file
779 - mutex_unlock(&dev->lock);
780 + mutex_unlock(&node->lock);
784 static int unicam_release(struct file *file)
786 - struct unicam_device *dev = video_drvdata(file);
787 + struct unicam_node *node = video_drvdata(file);
788 + struct unicam_device *dev = node->dev;
789 struct v4l2_subdev *sd = dev->sensor;
793 - mutex_lock(&dev->lock);
794 + mutex_lock(&node->lock);
796 fh_singular = v4l2_fh_is_singular_file(file);
798 @@ -1810,7 +1850,7 @@ static int unicam_release(struct file *f
800 v4l2_subdev_call(sd, core, s_power, 0);
802 - mutex_unlock(&dev->lock);
803 + mutex_unlock(&node->lock);
807 @@ -1892,7 +1932,8 @@ unicam_async_bound(struct v4l2_async_not
811 -static int unicam_probe_complete(struct unicam_device *unicam)
812 +static int register_node(struct unicam_device *unicam, struct unicam_node *node,
813 + enum v4l2_buf_type type, int pad_id)
815 struct video_device *vdev;
817 @@ -1900,15 +1941,7 @@ static int unicam_probe_complete(struct
818 const struct unicam_fmt *fmt;
821 - v4l2_set_subdev_hostdata(unicam->sensor, unicam);
823 - unicam->v4l2_dev.notify = unicam_notify;
825 - unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
826 - if (!unicam->sensor_config)
829 - ret = __subdev_get_format(unicam, &mbus_fmt);
830 + ret = __subdev_get_format(unicam, &mbus_fmt, pad_id);
832 unicam_err(unicam, "Failed to get_format - ret %d\n", ret);
834 @@ -1938,14 +1971,15 @@ static int unicam_probe_complete(struct
839 + node->pad_id = pad_id;
842 - unicam->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
843 + node->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
845 - unicam->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
846 + node->v_fmt.fmt.pix.pixelformat = fmt->repacked_fourcc;
848 /* Read current subdev format */
849 - unicam_reset_format(unicam);
850 + unicam_reset_format(node);
852 if (v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
854 @@ -1962,27 +1996,30 @@ static int unicam_probe_complete(struct
855 g_tvnorms, &tvnorms);
858 - unicam->video_dev.tvnorms |= tvnorms;
859 + node->video_dev.tvnorms |= tvnorms;
862 - spin_lock_init(&unicam->dma_queue_lock);
863 - mutex_init(&unicam->lock);
864 + spin_lock_init(&node->dma_queue_lock);
865 + mutex_init(&node->lock);
867 - /* Add controls from the subdevice */
868 - ret = v4l2_ctrl_add_handler(&unicam->ctrl_handler,
869 - unicam->sensor->ctrl_handler, NULL, true);
872 + if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
873 + /* Add controls from the subdevice */
874 + ret = v4l2_ctrl_add_handler(&node->ctrl_handler,
875 + unicam->sensor->ctrl_handler, NULL,
881 - q = &unicam->buffer_queue;
882 - q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
883 + q = &node->buffer_queue;
885 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
886 - q->drv_priv = unicam;
887 + q->drv_priv = node;
888 q->ops = &unicam_video_qops;
889 q->mem_ops = &vb2_dma_contig_memops;
890 q->buf_struct_size = sizeof(struct unicam_buffer);
891 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
892 - q->lock = &unicam->lock;
893 + q->lock = &node->lock;
894 q->min_buffers_needed = 2;
895 q->dev = &unicam->pdev->dev;
897 @@ -1992,9 +2029,9 @@ static int unicam_probe_complete(struct
901 - INIT_LIST_HEAD(&unicam->dma_queue.active);
902 + INIT_LIST_HEAD(&node->dma_queue.active);
904 - vdev = &unicam->video_dev;
905 + vdev = &node->video_dev;
906 strlcpy(vdev->name, UNICAM_MODULE_NAME, sizeof(vdev->name));
907 vdev->release = video_device_release_empty;
908 vdev->fops = &unicam_fops;
909 @@ -2002,69 +2039,113 @@ static int unicam_probe_complete(struct
910 vdev->v4l2_dev = &unicam->v4l2_dev;
911 vdev->vfl_dir = VFL_DIR_RX;
913 - vdev->lock = &unicam->lock;
914 + vdev->lock = &node->lock;
915 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
918 /* If the source has no controls then remove our ctrl handler. */
919 - if (list_empty(&unicam->ctrl_handler.ctrls))
920 + if (list_empty(&node->ctrl_handler.ctrls))
921 unicam->v4l2_dev.ctrl_handler = NULL;
923 - video_set_drvdata(vdev, unicam);
924 + node->dev = unicam;
925 + video_set_drvdata(vdev, node);
926 vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT;
928 if (!v4l2_subdev_has_op(unicam->sensor, video, s_std)) {
929 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_STD);
930 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_STD);
931 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUMSTD);
932 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_STD);
933 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_STD);
934 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUMSTD);
936 if (!v4l2_subdev_has_op(unicam->sensor, video, querystd))
937 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERYSTD);
938 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERYSTD);
939 if (!v4l2_subdev_has_op(unicam->sensor, video, s_dv_timings)) {
940 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_EDID);
941 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_EDID);
942 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_DV_TIMINGS_CAP);
943 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_DV_TIMINGS);
944 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_DV_TIMINGS);
945 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_DV_TIMINGS);
946 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_QUERY_DV_TIMINGS);
947 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_EDID);
948 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_EDID);
949 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_DV_TIMINGS_CAP);
950 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_DV_TIMINGS);
951 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_DV_TIMINGS);
952 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_DV_TIMINGS);
953 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_QUERY_DV_TIMINGS);
955 if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_interval))
956 - v4l2_disable_ioctl(&unicam->video_dev,
957 + v4l2_disable_ioctl(&node->video_dev,
958 VIDIOC_ENUM_FRAMEINTERVALS);
959 if (!v4l2_subdev_has_op(unicam->sensor, video, g_frame_interval))
960 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_G_PARM);
961 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_G_PARM);
962 if (!v4l2_subdev_has_op(unicam->sensor, video, s_frame_interval))
963 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_S_PARM);
964 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_S_PARM);
966 if (!v4l2_subdev_has_op(unicam->sensor, pad, enum_frame_size))
967 - v4l2_disable_ioctl(&unicam->video_dev, VIDIOC_ENUM_FRAMESIZES);
968 + v4l2_disable_ioctl(&node->video_dev, VIDIOC_ENUM_FRAMESIZES);
970 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
972 unicam_err(unicam, "Unable to register video device.\n");
975 + node->registered = true;
977 - ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
978 + ret = media_create_pad_link(&unicam->sensor->entity,
979 + 0, &node->video_dev.entity, 0,
980 + MEDIA_LNK_FL_ENABLED |
981 + MEDIA_LNK_FL_IMMUTABLE);
983 + unicam_err(unicam, "Unable to create pad links.\n");
988 +static void unregister_nodes(struct unicam_device *unicam)
990 + if (unicam->node[0].registered) {
991 + video_unregister_device(&unicam->node[0].video_dev);
992 + unicam->node[0].registered = false;
994 + if (unicam->node[1].registered) {
995 + video_unregister_device(&unicam->node[1].video_dev);
996 + unicam->node[1].registered = false;
1000 +static int unicam_probe_complete(struct unicam_device *unicam)
1004 + v4l2_set_subdev_hostdata(unicam->sensor, unicam);
1006 + unicam->v4l2_dev.notify = unicam_notify;
1008 + unicam->sensor_config = v4l2_subdev_alloc_pad_config(unicam->sensor);
1009 + if (!unicam->sensor_config)
1012 + ret = register_node(unicam, &unicam->node[0],
1013 + V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
1015 - unicam_err(unicam,
1016 - "Unable to register subdev nodes.\n");
1017 - video_unregister_device(&unicam->video_dev);
1019 + unicam_err(unicam, "Unable to register subdev node 0.\n");
1022 + if (unicam->sensor->entity.num_pads >= 2) {
1023 + ret = register_node(unicam, &unicam->node[1],
1024 + V4L2_BUF_TYPE_META_CAPTURE, 1);
1026 + unicam_err(unicam,
1027 + "Unable to register subdev node 1.\n");
1032 - ret = media_create_pad_link(&unicam->sensor->entity, 0,
1033 - &unicam->video_dev.entity, 0,
1034 - MEDIA_LNK_FL_ENABLED |
1035 - MEDIA_LNK_FL_IMMUTABLE);
1036 + ret = v4l2_device_register_ro_subdev_nodes(&unicam->v4l2_dev);
1038 - unicam_err(unicam, "Unable to create pad links.\n");
1039 - video_unregister_device(&unicam->video_dev);
1041 + unicam_err(unicam, "Unable to register subdev nodes.\n");
1048 + unregister_nodes(unicam);
1053 static int unicam_async_complete(struct v4l2_async_notifier *notifier)
1054 @@ -2274,7 +2355,8 @@ static int unicam_probe(struct platform_
1055 pdev->dev.driver->name, dev_name(&pdev->dev));
1056 unicam->mdev.hw_revision = 1;
1058 - media_entity_pads_init(&unicam->video_dev.entity, 1, &unicam->pad);
1059 + media_entity_pads_init(&unicam->node[0].video_dev.entity, 1,
1060 + &unicam->node[0].pad);
1061 media_device_init(&unicam->mdev);
1063 unicam->v4l2_dev.mdev = &unicam->mdev;
1064 @@ -2294,7 +2376,7 @@ static int unicam_probe(struct platform_
1067 /* Reserve space for the controls */
1068 - hdl = &unicam->ctrl_handler;
1069 + hdl = &unicam->node[0].ctrl_handler;
1070 ret = v4l2_ctrl_handler_init(hdl, 16);
1072 goto media_unregister;
1073 @@ -2335,9 +2417,9 @@ static int unicam_remove(struct platform
1074 pm_runtime_disable(&pdev->dev);
1076 v4l2_async_notifier_unregister(&unicam->notifier);
1077 - v4l2_ctrl_handler_free(&unicam->ctrl_handler);
1078 + v4l2_ctrl_handler_free(&unicam->node[0].ctrl_handler);
1079 v4l2_device_unregister(&unicam->v4l2_dev);
1080 - video_unregister_device(&unicam->video_dev);
1081 + unregister_nodes(unicam);
1082 if (unicam->sensor_config)
1083 v4l2_subdev_free_pad_config(unicam->sensor_config);
1084 media_device_unregister(&unicam->mdev);