1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021-2023 Digiteq Automotive
6 * This is the v4l2 input device module. It initializes the signal deserializers
7 * and creates the v4l2 video devices. The input signal can change at any time
8 * which is handled by the "timings" callbacks and an IRQ based watcher, that
9 * emits the V4L2_EVENT_SOURCE_CHANGE event in case of a signal source change.
11 * When the device is in loopback mode (a direct, in HW, in->out frame passing
12 * mode) the card's frame queue must be running regardless of whether a v4l2
13 * stream is running and the output parameters like frame buffers padding must
14 * be in sync with the input parameters.
17 #include <linux/pci.h>
18 #include <linux/workqueue.h>
19 #include <linux/align.h>
20 #include <linux/dma/amd_xdma.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-v4l2.h>
23 #include <media/videobuf2-dma-sg.h>
24 #include <media/v4l2-dv-timings.h>
25 #include <media/v4l2-event.h>
26 #include "mgb4_core.h"
28 #include "mgb4_sysfs.h"
30 #include "mgb4_vout.h"
33 ATTRIBUTE_GROUPS(mgb4_fpdl3_in);
34 ATTRIBUTE_GROUPS(mgb4_gmsl_in);
36 static const struct mgb4_vin_config vin_cfg[] = {
37 {0, 0, 0, 6, {0x10, 0x00, 0x04, 0x08, 0x1C, 0x14, 0x18, 0x20, 0x24, 0x28}},
38 {1, 1, 1, 7, {0x40, 0x30, 0x34, 0x38, 0x4C, 0x44, 0x48, 0x50, 0x54, 0x58}}
41 static const struct i2c_board_info fpdl3_deser_info[] = {
42 {I2C_BOARD_INFO("deserializer1", 0x38)},
43 {I2C_BOARD_INFO("deserializer2", 0x36)},
46 static const struct i2c_board_info gmsl_deser_info[] = {
47 {I2C_BOARD_INFO("deserializer1", 0x4C)},
48 {I2C_BOARD_INFO("deserializer2", 0x2A)},
51 static const struct mgb4_i2c_kv fpdl3_i2c[] = {
52 {0x06, 0xFF, 0x04}, {0x07, 0xFF, 0x01}, {0x45, 0xFF, 0xE8},
53 {0x49, 0xFF, 0x00}, {0x34, 0xFF, 0x00}, {0x23, 0xFF, 0x00}
56 static const struct mgb4_i2c_kv gmsl_i2c[] = {
57 {0x01, 0x03, 0x03}, {0x300, 0x0C, 0x0C}, {0x03, 0xC0, 0xC0},
58 {0x1CE, 0x0E, 0x0E}, {0x11, 0x05, 0x00}, {0x05, 0xC0, 0x40},
59 {0x307, 0x0F, 0x00}, {0xA0, 0x03, 0x00}, {0x3E0, 0x07, 0x07},
60 {0x308, 0x01, 0x01}, {0x10, 0x20, 0x20}, {0x300, 0x40, 0x40}
63 static const struct v4l2_dv_timings_cap video_timings_cap = {
64 .type = V4L2_DV_BT_656_1120,
70 .min_pixelclock = 1843200, /* 320 x 240 x 24Hz */
71 .max_pixelclock = 530841600, /* 4096 x 2160 x 60Hz */
72 .standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
73 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
74 .capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
75 V4L2_DV_BT_CAP_CUSTOM,
80 * Returns the video output connected with the given video input if the input
81 * is in loopback mode.
83 static struct mgb4_vout_dev *loopback_dev(struct mgb4_vin_dev *vindev, int i)
85 struct mgb4_vout_dev *voutdev;
88 voutdev = vindev->mgbdev->vout[i];
92 config = mgb4_read_reg(&voutdev->mgbdev->video,
93 voutdev->config->regs.config);
94 if ((config & 0xc) >> 2 == vindev->config->id)
101 * Check, whether the loopback mode - a HW INPUT->OUTPUT transmission - is
102 * enabled on the given input.
104 static int loopback_active(struct mgb4_vin_dev *vindev)
108 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
109 if (loopback_dev(vindev, i))
116 * Set the output frame buffer padding of all outputs connected with the given
117 * input when the video input is set to loopback mode. The paddings must be
118 * the same for the loopback to work properly.
120 static void set_loopback_padding(struct mgb4_vin_dev *vindev, u32 padding)
122 struct mgb4_regs *video = &vindev->mgbdev->video;
123 struct mgb4_vout_dev *voutdev;
126 for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
127 voutdev = loopback_dev(vindev, i);
129 mgb4_write_reg(video, voutdev->config->regs.padding,
134 static int get_timings(struct mgb4_vin_dev *vindev,
135 struct v4l2_dv_timings *timings)
137 struct mgb4_regs *video = &vindev->mgbdev->video;
138 const struct mgb4_vin_regs *regs = &vindev->config->regs;
140 u32 status = mgb4_read_reg(video, regs->status);
141 u32 pclk = mgb4_read_reg(video, regs->pclk);
142 u32 signal = mgb4_read_reg(video, regs->signal);
143 u32 signal2 = mgb4_read_reg(video, regs->signal2);
144 u32 resolution = mgb4_read_reg(video, regs->resolution);
146 if (!(status & (1U << 2)))
148 if (!(status & (3 << 9)))
151 memset(timings, 0, sizeof(*timings));
152 timings->type = V4L2_DV_BT_656_1120;
153 timings->bt.width = resolution >> 16;
154 timings->bt.height = resolution & 0xFFFF;
155 if (status & (1U << 12))
156 timings->bt.polarities |= V4L2_DV_HSYNC_POS_POL;
157 if (status & (1U << 13))
158 timings->bt.polarities |= V4L2_DV_VSYNC_POS_POL;
159 timings->bt.pixelclock = pclk * 1000;
160 timings->bt.hsync = (signal & 0x00FF0000) >> 16;
161 timings->bt.vsync = (signal2 & 0x00FF0000) >> 16;
162 timings->bt.hbackporch = (signal & 0x0000FF00) >> 8;
163 timings->bt.hfrontporch = signal & 0x000000FF;
164 timings->bt.vbackporch = (signal2 & 0x0000FF00) >> 8;
165 timings->bt.vfrontporch = signal2 & 0x000000FF;
170 static void return_all_buffers(struct mgb4_vin_dev *vindev,
171 enum vb2_buffer_state state)
173 struct mgb4_frame_buffer *buf, *node;
176 spin_lock_irqsave(&vindev->qlock, flags);
177 list_for_each_entry_safe(buf, node, &vindev->buf_list, list) {
178 vb2_buffer_done(&buf->vb.vb2_buf, state);
179 list_del(&buf->list);
181 spin_unlock_irqrestore(&vindev->qlock, flags);
184 static int queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
185 unsigned int *nplanes, unsigned int sizes[],
186 struct device *alloc_devs[])
188 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(q);
189 unsigned int size = (vindev->timings.bt.width + vindev->padding)
190 * vindev->timings.bt.height * 4;
193 * If I/O reconfiguration is in process, do not allow to start
194 * the queue. See video_source_store() in mgb4_sysfs_out.c for
197 if (test_bit(0, &vindev->mgbdev->io_reconfig))
203 return sizes[0] < size ? -EINVAL : 0;
210 static int buffer_init(struct vb2_buffer *vb)
212 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
213 struct mgb4_frame_buffer *buf = to_frame_buffer(vbuf);
215 INIT_LIST_HEAD(&buf->list);
220 static int buffer_prepare(struct vb2_buffer *vb)
222 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vb->vb2_queue);
223 struct device *dev = &vindev->mgbdev->pdev->dev;
224 unsigned int size = (vindev->timings.bt.width + vindev->padding)
225 * vindev->timings.bt.height * 4;
227 if (vb2_plane_size(vb, 0) < size) {
228 dev_err(dev, "buffer too small (%lu < %u)\n",
229 vb2_plane_size(vb, 0), size);
233 vb2_set_plane_payload(vb, 0, size);
238 static void buffer_queue(struct vb2_buffer *vb)
240 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vb->vb2_queue);
241 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
242 struct mgb4_frame_buffer *buf = to_frame_buffer(vbuf);
245 spin_lock_irqsave(&vindev->qlock, flags);
246 list_add_tail(&buf->list, &vindev->buf_list);
247 spin_unlock_irqrestore(&vindev->qlock, flags);
250 static void stop_streaming(struct vb2_queue *vq)
252 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vq);
253 const struct mgb4_vin_config *config = vindev->config;
254 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq);
256 xdma_disable_user_irq(vindev->mgbdev->xdev, irq);
259 * In loopback mode, the HW frame queue must be left running for
260 * the IN->OUT transmission to work!
262 if (!loopback_active(vindev))
263 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2,
266 cancel_work_sync(&vindev->dma_work);
267 return_all_buffers(vindev, VB2_BUF_STATE_ERROR);
270 static int start_streaming(struct vb2_queue *vq, unsigned int count)
272 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vq);
273 const struct mgb4_vin_config *config = vindev->config;
274 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq);
276 vindev->sequence = 0;
279 * In loopback mode, the HW frame queue is already running.
281 if (!loopback_active(vindev))
282 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2,
285 xdma_enable_user_irq(vindev->mgbdev->xdev, irq);
290 static const struct vb2_ops queue_ops = {
291 .queue_setup = queue_setup,
292 .buf_init = buffer_init,
293 .buf_prepare = buffer_prepare,
294 .buf_queue = buffer_queue,
295 .start_streaming = start_streaming,
296 .stop_streaming = stop_streaming,
297 .wait_prepare = vb2_ops_wait_prepare,
298 .wait_finish = vb2_ops_wait_finish
301 static int fh_open(struct file *file)
303 struct mgb4_vin_dev *vindev = video_drvdata(file);
306 mutex_lock(&vindev->lock);
308 rv = v4l2_fh_open(file);
312 if (!v4l2_fh_is_singular_file(file))
315 get_timings(vindev, &vindev->timings);
316 set_loopback_padding(vindev, vindev->padding);
319 mutex_unlock(&vindev->lock);
323 static int fh_release(struct file *file)
325 struct mgb4_vin_dev *vindev = video_drvdata(file);
328 mutex_lock(&vindev->lock);
330 if (v4l2_fh_is_singular_file(file))
331 set_loopback_padding(vindev, 0);
333 rv = _vb2_fop_release(file, NULL);
335 mutex_unlock(&vindev->lock);
340 static const struct v4l2_file_operations video_fops = {
341 .owner = THIS_MODULE,
343 .release = fh_release,
344 .unlocked_ioctl = video_ioctl2,
345 .read = vb2_fop_read,
346 .mmap = vb2_fop_mmap,
347 .poll = vb2_fop_poll,
350 static int vidioc_querycap(struct file *file, void *priv,
351 struct v4l2_capability *cap)
353 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
354 strscpy(cap->card, "MGB4 PCIe Card", sizeof(cap->card));
359 static int vidioc_enum_fmt(struct file *file, void *priv,
360 struct v4l2_fmtdesc *f)
365 f->pixelformat = V4L2_PIX_FMT_ABGR32;
370 static int vidioc_enum_frameintervals(struct file *file, void *priv,
371 struct v4l2_frmivalenum *ival)
373 struct mgb4_vin_dev *vindev = video_drvdata(file);
375 if (ival->index != 0)
377 if (ival->pixel_format != V4L2_PIX_FMT_ABGR32)
379 if (ival->width != vindev->timings.bt.width ||
380 ival->height != vindev->timings.bt.height)
383 ival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
384 ival->stepwise.min.denominator = 60;
385 ival->stepwise.min.numerator = 1;
386 ival->stepwise.max.denominator = 1;
387 ival->stepwise.max.numerator = 1;
388 ival->stepwise.step = ival->stepwise.max;
393 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
395 struct mgb4_vin_dev *vindev = video_drvdata(file);
397 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32;
398 f->fmt.pix.width = vindev->timings.bt.width;
399 f->fmt.pix.height = vindev->timings.bt.height;
400 f->fmt.pix.field = V4L2_FIELD_NONE;
401 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW;
402 f->fmt.pix.bytesperline = (f->fmt.pix.width + vindev->padding) * 4;
403 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
408 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
410 struct mgb4_vin_dev *vindev = video_drvdata(file);
412 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32;
413 f->fmt.pix.width = vindev->timings.bt.width;
414 f->fmt.pix.height = vindev->timings.bt.height;
415 f->fmt.pix.field = V4L2_FIELD_NONE;
416 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW;
417 f->fmt.pix.bytesperline = max(f->fmt.pix.width * 4,
418 ALIGN_DOWN(f->fmt.pix.bytesperline, 4));
419 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
424 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
426 struct mgb4_vin_dev *vindev = video_drvdata(file);
427 struct mgb4_regs *video = &vindev->mgbdev->video;
429 if (vb2_is_busy(&vindev->queue))
432 vidioc_try_fmt(file, priv, f);
434 vindev->padding = (f->fmt.pix.bytesperline - (f->fmt.pix.width * 4)) / 4;
435 mgb4_write_reg(video, vindev->config->regs.padding, vindev->padding);
436 set_loopback_padding(vindev, vindev->padding);
441 static int vidioc_enum_input(struct file *file, void *priv,
442 struct v4l2_input *i)
444 struct mgb4_vin_dev *vindev = video_drvdata(file);
445 struct mgb4_regs *video = &vindev->mgbdev->video;
451 strscpy(i->name, "MGB4", sizeof(i->name));
452 i->type = V4L2_INPUT_TYPE_CAMERA;
453 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
456 status = mgb4_read_reg(video, vindev->config->regs.status);
457 if (!(status & (1U << 2)))
458 i->status |= V4L2_IN_ST_NO_SYNC;
459 if (!(status & (3 << 9)))
460 i->status |= V4L2_IN_ST_NO_SIGNAL;
465 static int vidioc_enum_framesizes(struct file *file, void *fh,
466 struct v4l2_frmsizeenum *fsize)
468 struct mgb4_vin_dev *vindev = video_drvdata(file);
470 if (fsize->index != 0 || fsize->pixel_format != V4L2_PIX_FMT_ABGR32)
473 fsize->discrete.width = vindev->timings.bt.width;
474 fsize->discrete.height = vindev->timings.bt.height;
475 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
480 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
482 return (i == 0) ? 0 : -EINVAL;
485 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
491 static int vidioc_parm(struct file *file, void *priv,
492 struct v4l2_streamparm *parm)
494 struct mgb4_vin_dev *vindev = video_drvdata(file);
495 struct mgb4_regs *video = &vindev->mgbdev->video;
496 const struct mgb4_vin_regs *regs = &vindev->config->regs;
497 struct v4l2_fract timeperframe = {
498 .numerator = mgb4_read_reg(video, regs->frame_period),
499 .denominator = 125000000,
502 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
505 parm->parm.capture.readbuffers = 2;
506 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
507 parm->parm.capture.timeperframe = timeperframe;
512 static int vidioc_s_dv_timings(struct file *file, void *fh,
513 struct v4l2_dv_timings *timings)
515 struct mgb4_vin_dev *vindev = video_drvdata(file);
517 if (timings->bt.width < video_timings_cap.bt.min_width ||
518 timings->bt.width > video_timings_cap.bt.max_width ||
519 timings->bt.height < video_timings_cap.bt.min_height ||
520 timings->bt.height > video_timings_cap.bt.max_height)
522 if (timings->bt.width == vindev->timings.bt.width &&
523 timings->bt.height == vindev->timings.bt.height)
525 if (vb2_is_busy(&vindev->queue))
528 vindev->timings = *timings;
533 static int vidioc_g_dv_timings(struct file *file, void *fh,
534 struct v4l2_dv_timings *timings)
536 struct mgb4_vin_dev *vindev = video_drvdata(file);
537 *timings = vindev->timings;
542 static int vidioc_query_dv_timings(struct file *file, void *fh,
543 struct v4l2_dv_timings *timings)
545 struct mgb4_vin_dev *vindev = video_drvdata(file);
547 return get_timings(vindev, timings);
550 static int vidioc_enum_dv_timings(struct file *file, void *fh,
551 struct v4l2_enum_dv_timings *timings)
553 return v4l2_enum_dv_timings_cap(timings, &video_timings_cap, NULL, NULL);
556 static int vidioc_dv_timings_cap(struct file *file, void *fh,
557 struct v4l2_dv_timings_cap *cap)
559 *cap = video_timings_cap;
564 static int vidioc_subscribe_event(struct v4l2_fh *fh,
565 const struct v4l2_event_subscription *sub)
568 case V4L2_EVENT_SOURCE_CHANGE:
569 return v4l2_src_change_event_subscribe(fh, sub);
572 return v4l2_ctrl_subscribe_event(fh, sub);
575 static const struct v4l2_ioctl_ops video_ioctl_ops = {
576 .vidioc_querycap = vidioc_querycap,
577 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
578 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
579 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
580 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
581 .vidioc_enum_framesizes = vidioc_enum_framesizes,
582 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
583 .vidioc_enum_input = vidioc_enum_input,
584 .vidioc_g_input = vidioc_g_input,
585 .vidioc_s_input = vidioc_s_input,
586 .vidioc_reqbufs = vb2_ioctl_reqbufs,
587 .vidioc_create_bufs = vb2_ioctl_create_bufs,
588 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
589 .vidioc_querybuf = vb2_ioctl_querybuf,
590 .vidioc_qbuf = vb2_ioctl_qbuf,
591 .vidioc_dqbuf = vb2_ioctl_dqbuf,
592 .vidioc_expbuf = vb2_ioctl_expbuf,
593 .vidioc_streamon = vb2_ioctl_streamon,
594 .vidioc_streamoff = vb2_ioctl_streamoff,
595 .vidioc_g_parm = vidioc_parm,
596 .vidioc_s_parm = vidioc_parm,
597 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
598 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
599 .vidioc_g_dv_timings = vidioc_g_dv_timings,
600 .vidioc_s_dv_timings = vidioc_s_dv_timings,
601 .vidioc_query_dv_timings = vidioc_query_dv_timings,
602 .vidioc_subscribe_event = vidioc_subscribe_event,
603 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
606 static void dma_transfer(struct work_struct *work)
608 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
610 struct mgb4_regs *video = &vindev->mgbdev->video;
611 struct device *dev = &vindev->mgbdev->pdev->dev;
612 struct mgb4_frame_buffer *buf = NULL;
617 spin_lock_irqsave(&vindev->qlock, flags);
618 if (!list_empty(&vindev->buf_list)) {
619 buf = list_first_entry(&vindev->buf_list,
620 struct mgb4_frame_buffer, list);
621 list_del_init(vindev->buf_list.next);
623 spin_unlock_irqrestore(&vindev->qlock, flags);
628 addr = mgb4_read_reg(video, vindev->config->regs.address);
629 if (addr >= MGB4_ERR_QUEUE_FULL) {
630 dev_dbg(dev, "frame queue error (%d)\n", (int)addr);
631 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
635 rv = mgb4_dma_transfer(vindev->mgbdev, vindev->config->dma_channel,
637 vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0));
639 dev_warn(dev, "DMA transfer error\n");
640 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
642 buf->vb.vb2_buf.timestamp = ktime_get_ns();
643 buf->vb.sequence = vindev->sequence++;
644 buf->vb.field = V4L2_FIELD_NONE;
645 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
649 static void signal_change(struct work_struct *work)
651 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
653 struct mgb4_regs *video = &vindev->mgbdev->video;
654 struct v4l2_bt_timings *timings = &vindev->timings.bt;
655 struct device *dev = &vindev->mgbdev->pdev->dev;
657 u32 resolution = mgb4_read_reg(video, vindev->config->regs.resolution);
658 u32 width = resolution >> 16;
659 u32 height = resolution & 0xFFFF;
661 if (timings->width != width || timings->height != height) {
662 static const struct v4l2_event ev = {
663 .type = V4L2_EVENT_SOURCE_CHANGE,
664 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
667 v4l2_event_queue(&vindev->vdev, &ev);
669 if (vb2_is_streaming(&vindev->queue))
670 vb2_queue_error(&vindev->queue);
673 dev_dbg(dev, "stream changed to %ux%u\n", width, height);
676 static irqreturn_t vin_handler(int irq, void *ctx)
678 struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
679 struct mgb4_regs *video = &vindev->mgbdev->video;
681 schedule_work(&vindev->dma_work);
683 mgb4_write_reg(video, 0xB4, 1U << vindev->config->vin_irq);
688 static irqreturn_t err_handler(int irq, void *ctx)
690 struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
691 struct mgb4_regs *video = &vindev->mgbdev->video;
693 schedule_work(&vindev->err_work);
695 mgb4_write_reg(video, 0xB4, 1U << vindev->config->err_irq);
700 static int deser_init(struct mgb4_vin_dev *vindev, int id)
704 const struct mgb4_i2c_kv *values;
705 const struct i2c_board_info *info;
706 struct device *dev = &vindev->mgbdev->pdev->dev;
708 if (MGB4_IS_GMSL(vindev->mgbdev)) {
709 info = &gmsl_deser_info[id];
712 values_count = ARRAY_SIZE(gmsl_i2c);
714 info = &fpdl3_deser_info[id];
717 values_count = ARRAY_SIZE(fpdl3_i2c);
720 rv = mgb4_i2c_init(&vindev->deser, vindev->mgbdev->i2c_adap, info,
723 dev_err(dev, "failed to create deserializer\n");
726 rv = mgb4_i2c_configure(&vindev->deser, values, values_count);
728 dev_err(dev, "failed to configure deserializer\n");
735 mgb4_i2c_free(&vindev->deser);
740 static void fpga_init(struct mgb4_vin_dev *vindev)
742 struct mgb4_regs *video = &vindev->mgbdev->video;
743 const struct mgb4_vin_regs *regs = &vindev->config->regs;
745 mgb4_write_reg(video, regs->config, 0x00000001);
746 mgb4_write_reg(video, regs->sync, 0x03E80002);
747 mgb4_write_reg(video, regs->padding, 0x00000000);
748 mgb4_write_reg(video, regs->config, 1U << 9);
751 #ifdef CONFIG_DEBUG_FS
752 static void debugfs_init(struct mgb4_vin_dev *vindev)
754 struct mgb4_regs *video = &vindev->mgbdev->video;
756 vindev->debugfs = debugfs_create_dir(vindev->vdev.name,
757 vindev->mgbdev->debugfs);
758 if (!vindev->debugfs)
761 vindev->regs[0].name = "CONFIG";
762 vindev->regs[0].offset = vindev->config->regs.config;
763 vindev->regs[1].name = "STATUS";
764 vindev->regs[1].offset = vindev->config->regs.status;
765 vindev->regs[2].name = "RESOLUTION";
766 vindev->regs[2].offset = vindev->config->regs.resolution;
767 vindev->regs[3].name = "FRAME_PERIOD";
768 vindev->regs[3].offset = vindev->config->regs.frame_period;
769 vindev->regs[4].name = "HS_VS_GENER_SETTINGS";
770 vindev->regs[4].offset = vindev->config->regs.sync;
771 vindev->regs[5].name = "PCLK_FREQUENCY";
772 vindev->regs[5].offset = vindev->config->regs.pclk;
773 vindev->regs[6].name = "VIDEO_PARAMS_1";
774 vindev->regs[6].offset = vindev->config->regs.signal;
775 vindev->regs[7].name = "VIDEO_PARAMS_2";
776 vindev->regs[7].offset = vindev->config->regs.signal2;
777 vindev->regs[8].name = "PADDING_PIXELS";
778 vindev->regs[8].offset = vindev->config->regs.padding;
780 vindev->regset.base = video->membase;
781 vindev->regset.regs = vindev->regs;
782 vindev->regset.nregs = ARRAY_SIZE(vindev->regs);
784 debugfs_create_regset32("registers", 0444, vindev->debugfs,
789 struct mgb4_vin_dev *mgb4_vin_create(struct mgb4_dev *mgbdev, int id)
792 const struct attribute_group **groups;
793 struct mgb4_vin_dev *vindev;
794 struct pci_dev *pdev = mgbdev->pdev;
795 struct device *dev = &pdev->dev;
796 int vin_irq, err_irq;
798 vindev = kzalloc(sizeof(*vindev), GFP_KERNEL);
802 vindev->mgbdev = mgbdev;
803 vindev->config = &vin_cfg[id];
806 INIT_LIST_HEAD(&vindev->buf_list);
807 spin_lock_init(&vindev->qlock);
810 INIT_WORK(&vindev->dma_work, dma_transfer);
811 INIT_WORK(&vindev->err_work, signal_change);
814 vin_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->vin_irq);
815 rv = request_irq(vin_irq, vin_handler, 0, "mgb4-vin", vindev);
817 dev_err(dev, "failed to register vin irq handler\n");
820 /* Error IRQ callback */
821 err_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->err_irq);
822 rv = request_irq(err_irq, err_handler, 0, "mgb4-err", vindev);
824 dev_err(dev, "failed to register err irq handler\n");
828 /* Set the FPGA registers default values */
831 /* Set the deserializer default values */
832 rv = deser_init(vindev, id);
836 /* V4L2 stuff init */
837 rv = v4l2_device_register(dev, &vindev->v4l2dev);
839 dev_err(dev, "failed to register v4l2 device\n");
843 mutex_init(&vindev->lock);
845 vindev->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
846 vindev->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
847 vindev->queue.buf_struct_size = sizeof(struct mgb4_frame_buffer);
848 vindev->queue.ops = &queue_ops;
849 vindev->queue.mem_ops = &vb2_dma_sg_memops;
850 vindev->queue.gfp_flags = GFP_DMA32;
851 vindev->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
852 vindev->queue.min_queued_buffers = 2;
853 vindev->queue.drv_priv = vindev;
854 vindev->queue.lock = &vindev->lock;
855 vindev->queue.dev = dev;
856 rv = vb2_queue_init(&vindev->queue);
858 dev_err(dev, "failed to initialize vb2 queue\n");
862 snprintf(vindev->vdev.name, sizeof(vindev->vdev.name), "mgb4-in%d",
864 vindev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE
865 | V4L2_CAP_STREAMING;
866 vindev->vdev.fops = &video_fops;
867 vindev->vdev.ioctl_ops = &video_ioctl_ops;
868 vindev->vdev.release = video_device_release_empty;
869 vindev->vdev.v4l2_dev = &vindev->v4l2dev;
870 vindev->vdev.lock = &vindev->lock;
871 vindev->vdev.queue = &vindev->queue;
872 video_set_drvdata(&vindev->vdev, vindev);
874 /* Enable the video signal change watcher */
875 xdma_enable_user_irq(vindev->mgbdev->xdev, err_irq);
877 /* Register the video device */
878 rv = video_register_device(&vindev->vdev, VFL_TYPE_VIDEO, -1);
880 dev_err(dev, "failed to register video device\n");
884 /* Module sysfs attributes */
885 groups = MGB4_IS_GMSL(mgbdev)
886 ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups;
887 rv = device_add_groups(&vindev->vdev.dev, groups);
889 dev_err(dev, "failed to create sysfs attributes\n");
893 #ifdef CONFIG_DEBUG_FS
894 debugfs_init(vindev);
900 video_unregister_device(&vindev->vdev);
902 v4l2_device_unregister(&vindev->v4l2dev);
904 free_irq(err_irq, vindev);
906 free_irq(vin_irq, vindev);
913 void mgb4_vin_free(struct mgb4_vin_dev *vindev)
915 const struct attribute_group **groups;
916 int vin_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
917 vindev->config->vin_irq);
918 int err_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
919 vindev->config->err_irq);
921 xdma_disable_user_irq(vindev->mgbdev->xdev, err_irq);
923 free_irq(vin_irq, vindev);
924 free_irq(err_irq, vindev);
926 #ifdef CONFIG_DEBUG_FS
927 debugfs_remove_recursive(vindev->debugfs);
930 groups = MGB4_IS_GMSL(vindev->mgbdev)
931 ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups;
932 device_remove_groups(&vindev->vdev.dev, groups);
934 mgb4_i2c_free(&vindev->deser);
935 video_unregister_device(&vindev->vdev);
936 v4l2_device_unregister(&vindev->v4l2dev);