1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2021-2023 Digiteq Automotive
4 * author: Martin Tuma <martin.tuma@digiteqautomotive.com>
5 *
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.
10 *
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.
15 */
16
17 #include <linux/pci.h>
18 #include <linux/workqueue.h>
19 #include <linux/align.h>
20 #include <linux/dma/amd_xdma.h>
21 #include <linux/v4l2-dv-timings.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-sg.h>
25 #include <media/v4l2-dv-timings.h>
26 #include <media/v4l2-event.h>
27 #include "mgb4_core.h"
28 #include "mgb4_dma.h"
29 #include "mgb4_sysfs.h"
30 #include "mgb4_io.h"
31 #include "mgb4_vout.h"
32 #include "mgb4_vin.h"
33
34 ATTRIBUTE_GROUPS(mgb4_fpdl3_in);
35 ATTRIBUTE_GROUPS(mgb4_gmsl3_in);
36 ATTRIBUTE_GROUPS(mgb4_gmsl1_in);
37
38 static const struct mgb4_vin_config vin_cfg[] = {
39 {0, 0, 0, 6, {0x10, 0x00, 0x04, 0x08, 0x1C, 0x14, 0x18, 0x20, 0x24, 0x28, 0xE8}},
40 {1, 1, 1, 7, {0x40, 0x30, 0x34, 0x38, 0x4C, 0x44, 0x48, 0x50, 0x54, 0x58, 0xEC}}
41 };
42
43 static const struct i2c_board_info fpdl3_deser_info[] = {
44 {I2C_BOARD_INFO("deserializer1", 0x38)},
45 {I2C_BOARD_INFO("deserializer2", 0x36)},
46 };
47
48 static const struct i2c_board_info gmsl3_deser_info[] = {
49 {I2C_BOARD_INFO("deserializer1", 0x4C)},
50 {I2C_BOARD_INFO("deserializer2", 0x2A)},
51 };
52
53 static const struct i2c_board_info gmsl3c_deser_info[] = {
54 {I2C_BOARD_INFO("deserializer1", 0x6A)},
55 {I2C_BOARD_INFO("deserializer2", 0x6C)},
56 };
57
58 static const struct i2c_board_info gmsl1_deser_info[] = {
59 {I2C_BOARD_INFO("deserializer1", 0x2C)},
60 {I2C_BOARD_INFO("deserializer2", 0x6C)},
61 };
62
63 static const struct mgb4_i2c_kv fpdl3_i2c[] = {
64 {0x06, 0xFF, 0x04}, {0x07, 0xFF, 0x01}, {0x45, 0xFF, 0xE8},
65 {0x49, 0xFF, 0x00}, {0x34, 0xFF, 0x00}, {0x23, 0xFF, 0x00}
66 };
67
68 static const struct mgb4_i2c_kv gmsl3_i2c[] = {
69 {0x01, 0x03, 0x03}, {0x300, 0x0C, 0x0C}, {0x03, 0xC0, 0xC0},
70 {0x1CE, 0x0E, 0x0E}, {0x11, 0x05, 0x00}, {0x05, 0xC0, 0x40},
71 {0x307, 0x0F, 0x00}, {0xA0, 0x03, 0x00}, {0x3E0, 0x07, 0x07},
72 {0x308, 0x01, 0x01}, {0x10, 0x20, 0x20}, {0x300, 0x40, 0x40}
73 };
74
75 static const struct mgb4_i2c_kv gmsl3c_i2c[] = {
76 {0x01, 0x03, 0x02}, {0x300, 0x0C, 0x08}, {0x03, 0xC0, 0x00},
77 {0x1CE, 0x0E, 0x0E}, {0x11, 0x05, 0x05}, {0x05, 0xC0, 0x40},
78 {0x307, 0x0F, 0x00}, {0xA0, 0x03, 0x00}, {0x3E0, 0x07, 0x00},
79 {0x308, 0x01, 0x00}, {0x10, 0x20, 0x20}, {0x300, 0x40, 0x40}
80 };
81
82 static const struct mgb4_i2c_kv gmsl1_i2c[] = {
83 };
84
85 static const struct v4l2_dv_timings_cap video_timings_cap = {
86 .type = V4L2_DV_BT_656_1120,
87 .bt = {
88 .min_width = 240,
89 .max_width = 4096,
90 .min_height = 240,
91 .max_height = 4096,
92 .min_pixelclock = 1843200, /* 320 x 240 x 24Hz */
93 .max_pixelclock = 530841600, /* 4096 x 2160 x 60Hz */
94 .standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
95 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
96 .capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
97 V4L2_DV_BT_CAP_CUSTOM,
98 },
99 };
100
101 /* Dummy timings when no signal present */
102 static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
103
104 /*
105 * Returns the video output connected with the given video input if the input
106 * is in loopback mode.
107 */
loopback_dev(struct mgb4_vin_dev * vindev,int i)108 static struct mgb4_vout_dev *loopback_dev(struct mgb4_vin_dev *vindev, int i)
109 {
110 struct mgb4_vout_dev *voutdev;
111 u32 config;
112
113 voutdev = vindev->mgbdev->vout[i];
114 if (!voutdev)
115 return NULL;
116
117 config = mgb4_read_reg(&voutdev->mgbdev->video,
118 voutdev->config->regs.config);
119 if ((config & 0xc) >> 2 == vindev->config->id)
120 return voutdev;
121
122 return NULL;
123 }
124
125 /*
126 * Check, whether the loopback mode - a HW INPUT->OUTPUT transmission - is
127 * enabled on the given input.
128 */
loopback_active(struct mgb4_vin_dev * vindev)129 static int loopback_active(struct mgb4_vin_dev *vindev)
130 {
131 int i;
132
133 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
134 if (loopback_dev(vindev, i))
135 return 1;
136
137 return 0;
138 }
139
140 /*
141 * Set the output frame buffer padding of all outputs connected with the given
142 * input when the video input is set to loopback mode. The paddings must be
143 * the same for the loopback to work properly.
144 */
set_loopback_padding(struct mgb4_vin_dev * vindev,u32 padding)145 static void set_loopback_padding(struct mgb4_vin_dev *vindev, u32 padding)
146 {
147 struct mgb4_regs *video = &vindev->mgbdev->video;
148 struct mgb4_vout_dev *voutdev;
149 int i;
150
151 for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
152 voutdev = loopback_dev(vindev, i);
153 if (voutdev)
154 mgb4_write_reg(video, voutdev->config->regs.padding,
155 padding);
156 }
157 }
158
get_timings(struct mgb4_vin_dev * vindev,struct v4l2_dv_timings * timings)159 static int get_timings(struct mgb4_vin_dev *vindev,
160 struct v4l2_dv_timings *timings)
161 {
162 struct mgb4_regs *video = &vindev->mgbdev->video;
163 const struct mgb4_vin_regs *regs = &vindev->config->regs;
164
165 u32 status = mgb4_read_reg(video, regs->status);
166 u32 pclk = mgb4_read_reg(video, regs->pclk);
167 u32 hsync = mgb4_read_reg(video, regs->hsync);
168 u32 vsync = mgb4_read_reg(video, regs->vsync);
169 u32 resolution = mgb4_read_reg(video, regs->resolution);
170
171 if (!(status & (1U << 2)))
172 return -ENOLCK;
173 if (!(status & (3 << 9)))
174 return -ENOLINK;
175
176 memset(timings, 0, sizeof(*timings));
177 timings->type = V4L2_DV_BT_656_1120;
178 timings->bt.width = resolution >> 16;
179 timings->bt.height = resolution & 0xFFFF;
180 if (status & (1U << 12))
181 timings->bt.polarities |= V4L2_DV_HSYNC_POS_POL;
182 if (status & (1U << 13))
183 timings->bt.polarities |= V4L2_DV_VSYNC_POS_POL;
184 timings->bt.pixelclock = pclk * 1000;
185 timings->bt.hsync = (hsync & 0x00FF0000) >> 16;
186 timings->bt.vsync = (vsync & 0x00FF0000) >> 16;
187 timings->bt.hbackporch = (hsync & 0x0000FF00) >> 8;
188 timings->bt.hfrontporch = hsync & 0x000000FF;
189 timings->bt.vbackporch = (vsync & 0x0000FF00) >> 8;
190 timings->bt.vfrontporch = vsync & 0x000000FF;
191
192 return 0;
193 }
194
return_all_buffers(struct mgb4_vin_dev * vindev,enum vb2_buffer_state state)195 static void return_all_buffers(struct mgb4_vin_dev *vindev,
196 enum vb2_buffer_state state)
197 {
198 struct mgb4_frame_buffer *buf, *node;
199 unsigned long flags;
200
201 spin_lock_irqsave(&vindev->qlock, flags);
202 list_for_each_entry_safe(buf, node, &vindev->buf_list, list) {
203 vb2_buffer_done(&buf->vb.vb2_buf, state);
204 list_del(&buf->list);
205 }
206 spin_unlock_irqrestore(&vindev->qlock, flags);
207 }
208
queue_setup(struct vb2_queue * q,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])209 static int queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
210 unsigned int *nplanes, unsigned int sizes[],
211 struct device *alloc_devs[])
212 {
213 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(q);
214 struct mgb4_regs *video = &vindev->mgbdev->video;
215 u32 config = mgb4_read_reg(video, vindev->config->regs.config);
216 u32 pixelsize = (config & (1U << 16)) ? 2 : 4;
217 unsigned int size = (vindev->timings.bt.width + vindev->padding)
218 * vindev->timings.bt.height * pixelsize;
219
220 /*
221 * If I/O reconfiguration is in process, do not allow to start
222 * the queue. See video_source_store() in mgb4_sysfs_out.c for
223 * details.
224 */
225 if (test_bit(0, &vindev->mgbdev->io_reconfig))
226 return -EBUSY;
227
228 if (!size)
229 return -EINVAL;
230 if (*nplanes)
231 return sizes[0] < size ? -EINVAL : 0;
232 *nplanes = 1;
233 sizes[0] = size;
234
235 return 0;
236 }
237
buffer_init(struct vb2_buffer * vb)238 static int buffer_init(struct vb2_buffer *vb)
239 {
240 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
241 struct mgb4_frame_buffer *buf = to_frame_buffer(vbuf);
242
243 INIT_LIST_HEAD(&buf->list);
244
245 return 0;
246 }
247
buffer_prepare(struct vb2_buffer * vb)248 static int buffer_prepare(struct vb2_buffer *vb)
249 {
250 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vb->vb2_queue);
251 struct mgb4_regs *video = &vindev->mgbdev->video;
252 struct device *dev = &vindev->mgbdev->pdev->dev;
253 u32 config = mgb4_read_reg(video, vindev->config->regs.config);
254 u32 pixelsize = (config & (1U << 16)) ? 2 : 4;
255 unsigned int size = (vindev->timings.bt.width + vindev->padding)
256 * vindev->timings.bt.height * pixelsize;
257
258 if (vb2_plane_size(vb, 0) < size) {
259 dev_err(dev, "buffer too small (%lu < %u)\n",
260 vb2_plane_size(vb, 0), size);
261 return -EINVAL;
262 }
263
264 vb2_set_plane_payload(vb, 0, size);
265
266 return 0;
267 }
268
buffer_queue(struct vb2_buffer * vb)269 static void buffer_queue(struct vb2_buffer *vb)
270 {
271 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vb->vb2_queue);
272 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
273 struct mgb4_frame_buffer *buf = to_frame_buffer(vbuf);
274 unsigned long flags;
275
276 spin_lock_irqsave(&vindev->qlock, flags);
277 list_add_tail(&buf->list, &vindev->buf_list);
278 spin_unlock_irqrestore(&vindev->qlock, flags);
279 }
280
stop_streaming(struct vb2_queue * vq)281 static void stop_streaming(struct vb2_queue *vq)
282 {
283 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vq);
284 struct mgb4_regs *video = &vindev->mgbdev->video;
285 const struct mgb4_vin_config *config = vindev->config;
286 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq);
287
288 xdma_disable_user_irq(vindev->mgbdev->xdev, irq);
289
290 /*
291 * In loopback mode, the HW frame queue must be left running for
292 * the IN->OUT transmission to work!
293 */
294 if (!loopback_active(vindev))
295 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2,
296 0x0);
297
298 mgb4_write_reg(video, vindev->config->regs.padding, 0);
299 set_loopback_padding(vindev, 0);
300
301 cancel_work_sync(&vindev->dma_work);
302 return_all_buffers(vindev, VB2_BUF_STATE_ERROR);
303 }
304
start_streaming(struct vb2_queue * vq,unsigned int count)305 static int start_streaming(struct vb2_queue *vq, unsigned int count)
306 {
307 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vq);
308 struct mgb4_regs *video = &vindev->mgbdev->video;
309 const struct mgb4_vin_config *config = vindev->config;
310 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq);
311
312 vindev->sequence = 0;
313
314 /*
315 * In loopback mode, the HW frame queue is already running.
316 */
317 if (!loopback_active(vindev))
318 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2,
319 0x2);
320
321 mgb4_write_reg(video, vindev->config->regs.padding, vindev->padding);
322 set_loopback_padding(vindev, vindev->padding);
323
324 xdma_enable_user_irq(vindev->mgbdev->xdev, irq);
325
326 return 0;
327 }
328
329 static const struct vb2_ops queue_ops = {
330 .queue_setup = queue_setup,
331 .buf_init = buffer_init,
332 .buf_prepare = buffer_prepare,
333 .buf_queue = buffer_queue,
334 .start_streaming = start_streaming,
335 .stop_streaming = stop_streaming,
336 };
337
fh_open(struct file * file)338 static int fh_open(struct file *file)
339 {
340 struct mgb4_vin_dev *vindev = video_drvdata(file);
341 int rv;
342
343 mutex_lock(&vindev->lock);
344
345 rv = v4l2_fh_open(file);
346 if (rv)
347 goto out;
348
349 if (!v4l2_fh_is_singular_file(file))
350 goto out;
351
352 if (get_timings(vindev, &vindev->timings) < 0)
353 vindev->timings = cea1080p60;
354
355 out:
356 mutex_unlock(&vindev->lock);
357 return rv;
358 }
359
360 static const struct v4l2_file_operations video_fops = {
361 .owner = THIS_MODULE,
362 .open = fh_open,
363 .release = vb2_fop_release,
364 .unlocked_ioctl = video_ioctl2,
365 .read = vb2_fop_read,
366 .mmap = vb2_fop_mmap,
367 .poll = vb2_fop_poll,
368 };
369
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)370 static int vidioc_querycap(struct file *file, void *priv,
371 struct v4l2_capability *cap)
372 {
373 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
374 strscpy(cap->card, "MGB4 PCIe Card", sizeof(cap->card));
375
376 return 0;
377 }
378
vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)379 static int vidioc_enum_fmt(struct file *file, void *priv,
380 struct v4l2_fmtdesc *f)
381 {
382 struct mgb4_vin_dev *vindev = video_drvdata(file);
383 struct mgb4_regs *video = &vindev->mgbdev->video;
384
385 if (f->index == 0) {
386 f->pixelformat = V4L2_PIX_FMT_ABGR32;
387 return 0;
388 } else if (f->index == 1 && has_yuv(video)) {
389 f->pixelformat = V4L2_PIX_FMT_YUYV;
390 return 0;
391 } else {
392 return -EINVAL;
393 }
394 }
395
vidioc_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * ival)396 static int vidioc_enum_frameintervals(struct file *file, void *priv,
397 struct v4l2_frmivalenum *ival)
398 {
399 struct mgb4_vin_dev *vindev = video_drvdata(file);
400 struct mgb4_regs *video = &vindev->mgbdev->video;
401
402 if (ival->index != 0)
403 return -EINVAL;
404 if (!(ival->pixel_format == V4L2_PIX_FMT_ABGR32 ||
405 ((has_yuv(video) && ival->pixel_format == V4L2_PIX_FMT_YUYV))))
406 return -EINVAL;
407 if (ival->width != vindev->timings.bt.width ||
408 ival->height != vindev->timings.bt.height)
409 return -EINVAL;
410
411 ival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
412 ival->stepwise.max.denominator = MGB4_HW_FREQ;
413 ival->stepwise.max.numerator = 0xFFFFFFFF;
414 ival->stepwise.min.denominator = vindev->timings.bt.pixelclock;
415 ival->stepwise.min.numerator = pixel_size(&vindev->timings);
416 ival->stepwise.step.denominator = MGB4_HW_FREQ;
417 ival->stepwise.step.numerator = 1;
418
419 return 0;
420 }
421
vidioc_g_fmt(struct file * file,void * priv,struct v4l2_format * f)422 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
423 {
424 struct mgb4_vin_dev *vindev = video_drvdata(file);
425 struct mgb4_regs *video = &vindev->mgbdev->video;
426 u32 config = mgb4_read_reg(video, vindev->config->regs.config);
427
428 f->fmt.pix.width = vindev->timings.bt.width;
429 f->fmt.pix.height = vindev->timings.bt.height;
430 f->fmt.pix.field = V4L2_FIELD_NONE;
431
432 if (config & (1U << 16)) {
433 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
434 if (config & (1U << 20)) {
435 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
436 } else {
437 if (config & (1U << 19))
438 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
439 else
440 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
441 }
442 f->fmt.pix.bytesperline = (f->fmt.pix.width + vindev->padding) * 2;
443 } else {
444 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32;
445 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW;
446 f->fmt.pix.bytesperline = (f->fmt.pix.width + vindev->padding) * 4;
447 }
448 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
449
450 return 0;
451 }
452
vidioc_try_fmt(struct file * file,void * priv,struct v4l2_format * f)453 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
454 {
455 struct mgb4_vin_dev *vindev = video_drvdata(file);
456 struct mgb4_regs *video = &vindev->mgbdev->video;
457 u32 pixelsize;
458
459 f->fmt.pix.width = vindev->timings.bt.width;
460 f->fmt.pix.height = vindev->timings.bt.height;
461 f->fmt.pix.field = V4L2_FIELD_NONE;
462
463 if (has_yuv(video) && f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
464 pixelsize = 2;
465 if (!(f->fmt.pix.colorspace == V4L2_COLORSPACE_REC709 ||
466 f->fmt.pix.colorspace == V4L2_COLORSPACE_SMPTE170M))
467 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
468 } else {
469 pixelsize = 4;
470 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32;
471 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW;
472 }
473
474 if (f->fmt.pix.bytesperline > f->fmt.pix.width * pixelsize &&
475 f->fmt.pix.bytesperline < f->fmt.pix.width * pixelsize * 2)
476 f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
477 pixelsize);
478 else
479 f->fmt.pix.bytesperline = f->fmt.pix.width * pixelsize;
480 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
481
482 return 0;
483 }
484
vidioc_s_fmt(struct file * file,void * priv,struct v4l2_format * f)485 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
486 {
487 struct mgb4_vin_dev *vindev = video_drvdata(file);
488 struct mgb4_regs *video = &vindev->mgbdev->video;
489 u32 config, pixelsize;
490
491 if (vb2_is_busy(&vindev->queue))
492 return -EBUSY;
493
494 vidioc_try_fmt(file, priv, f);
495
496 config = mgb4_read_reg(video, vindev->config->regs.config);
497 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
498 pixelsize = 2;
499 config |= 1U << 16;
500
501 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_REC709) {
502 config |= 1U << 20;
503 config |= 1U << 19;
504 } else if (f->fmt.pix.colorspace == V4L2_COLORSPACE_SMPTE170M) {
505 config &= ~(1U << 20);
506 config |= 1U << 19;
507 } else {
508 config &= ~(1U << 20);
509 config &= ~(1U << 19);
510 }
511 } else {
512 pixelsize = 4;
513 config &= ~(1U << 16);
514 }
515 mgb4_write_reg(video, vindev->config->regs.config, config);
516
517 vindev->padding = (f->fmt.pix.bytesperline - (f->fmt.pix.width
518 * pixelsize)) / pixelsize;
519
520 return 0;
521 }
522
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * i)523 static int vidioc_enum_input(struct file *file, void *priv,
524 struct v4l2_input *i)
525 {
526 struct mgb4_vin_dev *vindev = video_drvdata(file);
527 struct mgb4_regs *video = &vindev->mgbdev->video;
528 u32 status;
529
530 if (i->index != 0)
531 return -EINVAL;
532
533 strscpy(i->name, "MGB4", sizeof(i->name));
534 i->type = V4L2_INPUT_TYPE_CAMERA;
535 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
536 i->status = 0;
537
538 status = mgb4_read_reg(video, vindev->config->regs.status);
539 if (!(status & (1U << 2)))
540 i->status |= V4L2_IN_ST_NO_SYNC;
541 if (!(status & (3 << 9)))
542 i->status |= V4L2_IN_ST_NO_SIGNAL;
543
544 return 0;
545 }
546
vidioc_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)547 static int vidioc_enum_framesizes(struct file *file, void *fh,
548 struct v4l2_frmsizeenum *fsize)
549 {
550 struct mgb4_vin_dev *vindev = video_drvdata(file);
551
552 if (fsize->index != 0 || !(fsize->pixel_format == V4L2_PIX_FMT_ABGR32 ||
553 fsize->pixel_format == V4L2_PIX_FMT_YUYV))
554 return -EINVAL;
555
556 fsize->discrete.width = vindev->timings.bt.width;
557 fsize->discrete.height = vindev->timings.bt.height;
558 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
559
560 return 0;
561 }
562
vidioc_s_input(struct file * file,void * priv,unsigned int i)563 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
564 {
565 return (i == 0) ? 0 : -EINVAL;
566 }
567
vidioc_g_input(struct file * file,void * priv,unsigned int * i)568 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
569 {
570 *i = 0;
571 return 0;
572 }
573
vidioc_g_parm(struct file * file,void * priv,struct v4l2_streamparm * parm)574 static int vidioc_g_parm(struct file *file, void *priv,
575 struct v4l2_streamparm *parm)
576 {
577 struct mgb4_vin_dev *vindev = video_drvdata(file);
578 struct mgb4_regs *video = &vindev->mgbdev->video;
579 struct v4l2_fract *tpf = &parm->parm.output.timeperframe;
580 u32 timer;
581
582 parm->parm.capture.readbuffers = 2;
583
584 if (has_timeperframe(video)) {
585 timer = mgb4_read_reg(video, vindev->config->regs.timer);
586 if (timer < 0xFFFF) {
587 tpf->numerator = pixel_size(&vindev->timings);
588 tpf->denominator = vindev->timings.bt.pixelclock;
589 } else {
590 tpf->numerator = timer;
591 tpf->denominator = MGB4_HW_FREQ;
592 }
593
594 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
595 }
596
597 return 0;
598 }
599
vidioc_s_parm(struct file * file,void * priv,struct v4l2_streamparm * parm)600 static int vidioc_s_parm(struct file *file, void *priv,
601 struct v4l2_streamparm *parm)
602 {
603 struct mgb4_vin_dev *vindev = video_drvdata(file);
604 struct mgb4_regs *video = &vindev->mgbdev->video;
605 struct v4l2_fract *tpf = &parm->parm.output.timeperframe;
606 u32 period, timer;
607
608 if (has_timeperframe(video)) {
609 timer = tpf->denominator ?
610 MGB4_PERIOD(tpf->numerator, tpf->denominator) : 0;
611 if (timer) {
612 period = MGB4_PERIOD(pixel_size(&vindev->timings),
613 vindev->timings.bt.pixelclock);
614 if (timer < period)
615 timer = 0;
616 }
617
618 mgb4_write_reg(video, vindev->config->regs.timer, timer);
619 }
620
621 return vidioc_g_parm(file, priv, parm);
622 }
623
vidioc_s_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)624 static int vidioc_s_dv_timings(struct file *file, void *fh,
625 struct v4l2_dv_timings *timings)
626 {
627 struct mgb4_vin_dev *vindev = video_drvdata(file);
628
629 if (timings->bt.width < video_timings_cap.bt.min_width ||
630 timings->bt.width > video_timings_cap.bt.max_width ||
631 timings->bt.height < video_timings_cap.bt.min_height ||
632 timings->bt.height > video_timings_cap.bt.max_height)
633 return -EINVAL;
634 if (v4l2_match_dv_timings(timings, &vindev->timings, 0, false))
635 return 0;
636 if (vb2_is_busy(&vindev->queue))
637 return -EBUSY;
638
639 vindev->timings = *timings;
640
641 return 0;
642 }
643
vidioc_g_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)644 static int vidioc_g_dv_timings(struct file *file, void *fh,
645 struct v4l2_dv_timings *timings)
646 {
647 struct mgb4_vin_dev *vindev = video_drvdata(file);
648 *timings = vindev->timings;
649
650 return 0;
651 }
652
vidioc_query_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)653 static int vidioc_query_dv_timings(struct file *file, void *fh,
654 struct v4l2_dv_timings *timings)
655 {
656 struct mgb4_vin_dev *vindev = video_drvdata(file);
657
658 return get_timings(vindev, timings);
659 }
660
vidioc_enum_dv_timings(struct file * file,void * fh,struct v4l2_enum_dv_timings * timings)661 static int vidioc_enum_dv_timings(struct file *file, void *fh,
662 struct v4l2_enum_dv_timings *timings)
663 {
664 struct mgb4_vin_dev *vindev = video_drvdata(file);
665
666 if (timings->index != 0)
667 return -EINVAL;
668 if (get_timings(vindev, &timings->timings) < 0)
669 return -ENODATA;
670
671 return 0;
672 }
673
vidioc_dv_timings_cap(struct file * file,void * fh,struct v4l2_dv_timings_cap * cap)674 static int vidioc_dv_timings_cap(struct file *file, void *fh,
675 struct v4l2_dv_timings_cap *cap)
676 {
677 *cap = video_timings_cap;
678
679 return 0;
680 }
681
vidioc_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)682 static int vidioc_subscribe_event(struct v4l2_fh *fh,
683 const struct v4l2_event_subscription *sub)
684 {
685 switch (sub->type) {
686 case V4L2_EVENT_SOURCE_CHANGE:
687 return v4l2_src_change_event_subscribe(fh, sub);
688 }
689
690 return v4l2_ctrl_subscribe_event(fh, sub);
691 }
692
693 static const struct v4l2_ioctl_ops video_ioctl_ops = {
694 .vidioc_querycap = vidioc_querycap,
695 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
696 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
697 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
698 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
699 .vidioc_enum_framesizes = vidioc_enum_framesizes,
700 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
701 .vidioc_enum_input = vidioc_enum_input,
702 .vidioc_g_input = vidioc_g_input,
703 .vidioc_s_input = vidioc_s_input,
704 .vidioc_reqbufs = vb2_ioctl_reqbufs,
705 .vidioc_create_bufs = vb2_ioctl_create_bufs,
706 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
707 .vidioc_querybuf = vb2_ioctl_querybuf,
708 .vidioc_qbuf = vb2_ioctl_qbuf,
709 .vidioc_dqbuf = vb2_ioctl_dqbuf,
710 .vidioc_expbuf = vb2_ioctl_expbuf,
711 .vidioc_streamon = vb2_ioctl_streamon,
712 .vidioc_streamoff = vb2_ioctl_streamoff,
713 .vidioc_g_parm = vidioc_g_parm,
714 .vidioc_s_parm = vidioc_s_parm,
715 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
716 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
717 .vidioc_g_dv_timings = vidioc_g_dv_timings,
718 .vidioc_s_dv_timings = vidioc_s_dv_timings,
719 .vidioc_query_dv_timings = vidioc_query_dv_timings,
720 .vidioc_subscribe_event = vidioc_subscribe_event,
721 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
722 };
723
dma_transfer(struct work_struct * work)724 static void dma_transfer(struct work_struct *work)
725 {
726 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
727 dma_work);
728 struct mgb4_regs *video = &vindev->mgbdev->video;
729 struct device *dev = &vindev->mgbdev->pdev->dev;
730 struct mgb4_frame_buffer *buf = NULL;
731 unsigned long flags;
732 u32 addr;
733 int rv;
734
735 spin_lock_irqsave(&vindev->qlock, flags);
736 if (!list_empty(&vindev->buf_list)) {
737 buf = list_first_entry(&vindev->buf_list,
738 struct mgb4_frame_buffer, list);
739 list_del_init(vindev->buf_list.next);
740 }
741 spin_unlock_irqrestore(&vindev->qlock, flags);
742
743 if (!buf)
744 return;
745
746 addr = mgb4_read_reg(video, vindev->config->regs.address);
747 if (addr >= MGB4_ERR_QUEUE_FULL) {
748 dev_dbg(dev, "frame queue error (%d)\n", (int)addr);
749 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
750 return;
751 }
752
753 rv = mgb4_dma_transfer(vindev->mgbdev, vindev->config->dma_channel,
754 false, addr,
755 vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0));
756 if (rv < 0) {
757 dev_warn(dev, "DMA transfer error\n");
758 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
759 } else {
760 buf->vb.vb2_buf.timestamp = ktime_get_ns();
761 buf->vb.sequence = vindev->sequence++;
762 buf->vb.field = V4L2_FIELD_NONE;
763 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
764 }
765 }
766
signal_change(struct work_struct * work)767 static void signal_change(struct work_struct *work)
768 {
769 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
770 err_work);
771 struct mgb4_regs *video = &vindev->mgbdev->video;
772 struct v4l2_bt_timings *timings = &vindev->timings.bt;
773 struct device *dev = &vindev->mgbdev->pdev->dev;
774
775 u32 resolution = mgb4_read_reg(video, vindev->config->regs.resolution);
776 u32 width = resolution >> 16;
777 u32 height = resolution & 0xFFFF;
778
779 static const struct v4l2_event ev = {
780 .type = V4L2_EVENT_SOURCE_CHANGE,
781 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
782 };
783
784 v4l2_event_queue(&vindev->vdev, &ev);
785
786 if (timings->width != width || timings->height != height) {
787 if (vb2_is_streaming(&vindev->queue))
788 vb2_queue_error(&vindev->queue);
789 }
790
791 dev_dbg(dev, "stream changed to %ux%u\n", width, height);
792 }
793
vin_handler(int irq,void * ctx)794 static irqreturn_t vin_handler(int irq, void *ctx)
795 {
796 struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
797 struct mgb4_regs *video = &vindev->mgbdev->video;
798
799 schedule_work(&vindev->dma_work);
800
801 mgb4_write_reg(video, 0xB4, 1U << vindev->config->vin_irq);
802
803 return IRQ_HANDLED;
804 }
805
err_handler(int irq,void * ctx)806 static irqreturn_t err_handler(int irq, void *ctx)
807 {
808 struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
809 struct mgb4_regs *video = &vindev->mgbdev->video;
810
811 schedule_work(&vindev->err_work);
812
813 mgb4_write_reg(video, 0xB4, 1U << vindev->config->err_irq);
814
815 return IRQ_HANDLED;
816 }
817
deser_init(struct mgb4_vin_dev * vindev,int id)818 static int deser_init(struct mgb4_vin_dev *vindev, int id)
819 {
820 int rv, addr_size = 0;
821 size_t count = 0;
822 const struct mgb4_i2c_kv *values = NULL;
823 const struct i2c_board_info *info = NULL;
824 struct device *dev = &vindev->mgbdev->pdev->dev;
825
826 if (MGB4_IS_GMSL3(vindev->mgbdev)) {
827 if (MGB4_IS_GMSL3C(vindev->mgbdev)) {
828 info = &gmsl3c_deser_info[id];
829 addr_size = 16;
830 values = gmsl3c_i2c;
831 count = ARRAY_SIZE(gmsl3c_i2c);
832 } else {
833 info = &gmsl3_deser_info[id];
834 addr_size = 16;
835 values = gmsl3_i2c;
836 count = ARRAY_SIZE(gmsl3_i2c);
837 }
838 } else if (MGB4_IS_FPDL3(vindev->mgbdev)) {
839 info = &fpdl3_deser_info[id];
840 addr_size = 8;
841 values = fpdl3_i2c;
842 count = ARRAY_SIZE(fpdl3_i2c);
843 } else if (MGB4_IS_GMSL1(vindev->mgbdev)) {
844 info = &gmsl1_deser_info[id];
845 addr_size = 8;
846 values = gmsl1_i2c;
847 count = ARRAY_SIZE(gmsl1_i2c);
848 } else {
849 return -EINVAL;
850 }
851
852 rv = mgb4_i2c_init(&vindev->deser, vindev->mgbdev->i2c_adap, info,
853 addr_size);
854 if (rv < 0) {
855 dev_err(dev, "failed to create deserializer\n");
856 return rv;
857 }
858 rv = mgb4_i2c_configure(&vindev->deser, values, count);
859 if (rv < 0) {
860 dev_err(dev, "failed to configure deserializer\n");
861 goto err_i2c_dev;
862 }
863
864 return 0;
865
866 err_i2c_dev:
867 mgb4_i2c_free(&vindev->deser);
868
869 return rv;
870 }
871
fpga_init(struct mgb4_vin_dev * vindev)872 static void fpga_init(struct mgb4_vin_dev *vindev)
873 {
874 struct mgb4_regs *video = &vindev->mgbdev->video;
875 const struct mgb4_vin_regs *regs = &vindev->config->regs;
876 int dp = MGB4_IS_GMSL1(vindev->mgbdev) ? 0 : 1;
877
878 mgb4_write_reg(video, regs->config, 0x00000001);
879 mgb4_write_reg(video, regs->sync, 0x03E80002);
880 mgb4_write_reg(video, regs->padding, 0x00000000);
881 mgb4_write_reg(video, regs->config, dp << 9);
882 }
883
create_debugfs(struct mgb4_vin_dev * vindev)884 static void create_debugfs(struct mgb4_vin_dev *vindev)
885 {
886 #ifdef CONFIG_DEBUG_FS
887 struct mgb4_regs *video = &vindev->mgbdev->video;
888 struct dentry *entry;
889
890 if (IS_ERR_OR_NULL(vindev->mgbdev->debugfs))
891 return;
892 entry = debugfs_create_dir(vindev->vdev.name, vindev->mgbdev->debugfs);
893 if (IS_ERR(entry))
894 return;
895
896 vindev->regs[0].name = "CONFIG";
897 vindev->regs[0].offset = vindev->config->regs.config;
898 vindev->regs[1].name = "STATUS";
899 vindev->regs[1].offset = vindev->config->regs.status;
900 vindev->regs[2].name = "RESOLUTION";
901 vindev->regs[2].offset = vindev->config->regs.resolution;
902 vindev->regs[3].name = "FRAME_PERIOD";
903 vindev->regs[3].offset = vindev->config->regs.frame_period;
904 vindev->regs[4].name = "HS_VS_GENER_SETTINGS";
905 vindev->regs[4].offset = vindev->config->regs.sync;
906 vindev->regs[5].name = "PCLK_FREQUENCY";
907 vindev->regs[5].offset = vindev->config->regs.pclk;
908 vindev->regs[6].name = "VIDEO_PARAMS_1";
909 vindev->regs[6].offset = vindev->config->regs.hsync;
910 vindev->regs[7].name = "VIDEO_PARAMS_2";
911 vindev->regs[7].offset = vindev->config->regs.vsync;
912 vindev->regs[8].name = "PADDING_PIXELS";
913 vindev->regs[8].offset = vindev->config->regs.padding;
914 if (has_timeperframe(video)) {
915 vindev->regs[9].name = "TIMER";
916 vindev->regs[9].offset = vindev->config->regs.timer;
917 vindev->regset.nregs = 10;
918 } else {
919 vindev->regset.nregs = 9;
920 }
921
922 vindev->regset.base = video->membase;
923 vindev->regset.regs = vindev->regs;
924
925 debugfs_create_regset32("registers", 0444, entry, &vindev->regset);
926 #endif
927 }
928
module_groups(struct mgb4_dev * mgbdev)929 static const struct attribute_group **module_groups(struct mgb4_dev *mgbdev)
930 {
931 if (MGB4_IS_FPDL3(mgbdev))
932 return mgb4_fpdl3_in_groups;
933 else if (MGB4_IS_GMSL3(mgbdev))
934 return mgb4_gmsl3_in_groups;
935 else if (MGB4_IS_GMSL1(mgbdev))
936 return mgb4_gmsl1_in_groups;
937 else
938 return NULL;
939 }
940
mgb4_vin_create(struct mgb4_dev * mgbdev,int id)941 struct mgb4_vin_dev *mgb4_vin_create(struct mgb4_dev *mgbdev, int id)
942 {
943 int rv;
944 struct mgb4_vin_dev *vindev;
945 struct pci_dev *pdev = mgbdev->pdev;
946 struct device *dev = &pdev->dev;
947 int vin_irq, err_irq;
948
949 vindev = kzalloc_obj(*vindev);
950 if (!vindev)
951 return NULL;
952
953 vindev->mgbdev = mgbdev;
954 vindev->config = &vin_cfg[id];
955
956 /* Frame queue*/
957 INIT_LIST_HEAD(&vindev->buf_list);
958 spin_lock_init(&vindev->qlock);
959
960 /* Work queues */
961 INIT_WORK(&vindev->dma_work, dma_transfer);
962 INIT_WORK(&vindev->err_work, signal_change);
963
964 /* IRQ callbacks */
965 vin_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->vin_irq);
966 rv = request_irq(vin_irq, vin_handler, 0, "mgb4-vin", vindev);
967 if (rv) {
968 dev_err(dev, "failed to register vin irq handler\n");
969 goto err_alloc;
970 }
971 err_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->err_irq);
972 rv = request_irq(err_irq, err_handler, 0, "mgb4-err", vindev);
973 if (rv) {
974 dev_err(dev, "failed to register err irq handler\n");
975 goto err_vin_irq;
976 }
977
978 /* Set the FPGA registers default values */
979 fpga_init(vindev);
980
981 /* Set the deserializer default values */
982 rv = deser_init(vindev, id);
983 if (rv)
984 goto err_err_irq;
985
986 /* V4L2 stuff init */
987 rv = v4l2_device_register(dev, &vindev->v4l2dev);
988 if (rv) {
989 dev_err(dev, "failed to register v4l2 device\n");
990 goto err_err_irq;
991 }
992
993 mutex_init(&vindev->lock);
994
995 vindev->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
996 vindev->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
997 vindev->queue.buf_struct_size = sizeof(struct mgb4_frame_buffer);
998 vindev->queue.ops = &queue_ops;
999 vindev->queue.mem_ops = &vb2_dma_sg_memops;
1000 vindev->queue.gfp_flags = GFP_DMA32;
1001 vindev->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1002 vindev->queue.min_queued_buffers = 2;
1003 vindev->queue.drv_priv = vindev;
1004 vindev->queue.lock = &vindev->lock;
1005 vindev->queue.dev = dev;
1006 rv = vb2_queue_init(&vindev->queue);
1007 if (rv) {
1008 dev_err(dev, "failed to initialize vb2 queue\n");
1009 goto err_v4l2_dev;
1010 }
1011
1012 snprintf(vindev->vdev.name, sizeof(vindev->vdev.name), "mgb4-in%d",
1013 id + 1);
1014 vindev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE
1015 | V4L2_CAP_STREAMING;
1016 vindev->vdev.fops = &video_fops;
1017 vindev->vdev.ioctl_ops = &video_ioctl_ops;
1018 vindev->vdev.release = video_device_release_empty;
1019 vindev->vdev.v4l2_dev = &vindev->v4l2dev;
1020 vindev->vdev.lock = &vindev->lock;
1021 vindev->vdev.queue = &vindev->queue;
1022 video_set_drvdata(&vindev->vdev, vindev);
1023
1024 /* Enable the video signal change watcher */
1025 xdma_enable_user_irq(vindev->mgbdev->xdev, err_irq);
1026
1027 /* Register the video device */
1028 rv = video_register_device(&vindev->vdev, VFL_TYPE_VIDEO, -1);
1029 if (rv) {
1030 dev_err(dev, "failed to register video device\n");
1031 goto err_v4l2_dev;
1032 }
1033
1034 /* Module sysfs attributes */
1035 rv = device_add_groups(&vindev->vdev.dev, module_groups(mgbdev));
1036 if (rv) {
1037 dev_err(dev, "failed to create sysfs attributes\n");
1038 goto err_video_dev;
1039 }
1040
1041 create_debugfs(vindev);
1042
1043 return vindev;
1044
1045 err_video_dev:
1046 video_unregister_device(&vindev->vdev);
1047 err_v4l2_dev:
1048 v4l2_device_unregister(&vindev->v4l2dev);
1049 err_err_irq:
1050 free_irq(err_irq, vindev);
1051 err_vin_irq:
1052 free_irq(vin_irq, vindev);
1053 err_alloc:
1054 kfree(vindev);
1055
1056 return NULL;
1057 }
1058
mgb4_vin_free(struct mgb4_vin_dev * vindev)1059 void mgb4_vin_free(struct mgb4_vin_dev *vindev)
1060 {
1061 int vin_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
1062 vindev->config->vin_irq);
1063 int err_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
1064 vindev->config->err_irq);
1065
1066 xdma_disable_user_irq(vindev->mgbdev->xdev, err_irq);
1067
1068 free_irq(vin_irq, vindev);
1069 free_irq(err_irq, vindev);
1070
1071 device_remove_groups(&vindev->vdev.dev, module_groups(vindev->mgbdev));
1072
1073 mgb4_i2c_free(&vindev->deser);
1074 video_unregister_device(&vindev->vdev);
1075 v4l2_device_unregister(&vindev->v4l2dev);
1076
1077 kfree(vindev);
1078 }
1079