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