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 const struct mgb4_vin_config *config = vindev->config; 264 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq); 265 266 xdma_disable_user_irq(vindev->mgbdev->xdev, irq); 267 268 /* 269 * In loopback mode, the HW frame queue must be left running for 270 * the IN->OUT transmission to work! 271 */ 272 if (!loopback_active(vindev)) 273 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2, 274 0x0); 275 276 cancel_work_sync(&vindev->dma_work); 277 return_all_buffers(vindev, VB2_BUF_STATE_ERROR); 278 } 279 280 static int start_streaming(struct vb2_queue *vq, unsigned int count) 281 { 282 struct mgb4_vin_dev *vindev = vb2_get_drv_priv(vq); 283 const struct mgb4_vin_config *config = vindev->config; 284 int irq = xdma_get_user_irq(vindev->mgbdev->xdev, config->vin_irq); 285 286 vindev->sequence = 0; 287 288 /* 289 * In loopback mode, the HW frame queue is already running. 290 */ 291 if (!loopback_active(vindev)) 292 mgb4_mask_reg(&vindev->mgbdev->video, config->regs.config, 0x2, 293 0x2); 294 295 xdma_enable_user_irq(vindev->mgbdev->xdev, irq); 296 297 return 0; 298 } 299 300 static const struct vb2_ops queue_ops = { 301 .queue_setup = queue_setup, 302 .buf_init = buffer_init, 303 .buf_prepare = buffer_prepare, 304 .buf_queue = buffer_queue, 305 .start_streaming = start_streaming, 306 .stop_streaming = stop_streaming, 307 }; 308 309 static int fh_open(struct file *file) 310 { 311 struct mgb4_vin_dev *vindev = video_drvdata(file); 312 int rv; 313 314 mutex_lock(&vindev->lock); 315 316 rv = v4l2_fh_open(file); 317 if (rv) 318 goto out; 319 320 if (!v4l2_fh_is_singular_file(file)) 321 goto out; 322 323 if (get_timings(vindev, &vindev->timings) < 0) 324 vindev->timings = cea1080p60; 325 set_loopback_padding(vindev, vindev->padding); 326 327 out: 328 mutex_unlock(&vindev->lock); 329 return rv; 330 } 331 332 static int fh_release(struct file *file) 333 { 334 struct mgb4_vin_dev *vindev = video_drvdata(file); 335 int rv; 336 337 mutex_lock(&vindev->lock); 338 339 if (v4l2_fh_is_singular_file(file)) 340 set_loopback_padding(vindev, 0); 341 342 rv = _vb2_fop_release(file, NULL); 343 344 mutex_unlock(&vindev->lock); 345 346 return rv; 347 } 348 349 static const struct v4l2_file_operations video_fops = { 350 .owner = THIS_MODULE, 351 .open = fh_open, 352 .release = fh_release, 353 .unlocked_ioctl = video_ioctl2, 354 .read = vb2_fop_read, 355 .mmap = vb2_fop_mmap, 356 .poll = vb2_fop_poll, 357 }; 358 359 static int vidioc_querycap(struct file *file, void *priv, 360 struct v4l2_capability *cap) 361 { 362 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 363 strscpy(cap->card, "MGB4 PCIe Card", sizeof(cap->card)); 364 365 return 0; 366 } 367 368 static int vidioc_enum_fmt(struct file *file, void *priv, 369 struct v4l2_fmtdesc *f) 370 { 371 struct mgb4_vin_dev *vindev = video_drvdata(file); 372 struct mgb4_regs *video = &vindev->mgbdev->video; 373 374 if (f->index == 0) { 375 f->pixelformat = V4L2_PIX_FMT_ABGR32; 376 return 0; 377 } else if (f->index == 1 && has_yuv(video)) { 378 f->pixelformat = V4L2_PIX_FMT_YUYV; 379 return 0; 380 } else { 381 return -EINVAL; 382 } 383 } 384 385 static int vidioc_enum_frameintervals(struct file *file, void *priv, 386 struct v4l2_frmivalenum *ival) 387 { 388 struct mgb4_vin_dev *vindev = video_drvdata(file); 389 struct mgb4_regs *video = &vindev->mgbdev->video; 390 391 if (ival->index != 0) 392 return -EINVAL; 393 if (!(ival->pixel_format == V4L2_PIX_FMT_ABGR32 || 394 ((has_yuv(video) && ival->pixel_format == V4L2_PIX_FMT_YUYV)))) 395 return -EINVAL; 396 if (ival->width != vindev->timings.bt.width || 397 ival->height != vindev->timings.bt.height) 398 return -EINVAL; 399 400 ival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 401 ival->stepwise.max.denominator = MGB4_HW_FREQ; 402 ival->stepwise.max.numerator = 0xFFFFFFFF; 403 ival->stepwise.min.denominator = vindev->timings.bt.pixelclock; 404 ival->stepwise.min.numerator = pixel_size(&vindev->timings); 405 ival->stepwise.step.denominator = MGB4_HW_FREQ; 406 ival->stepwise.step.numerator = 1; 407 408 return 0; 409 } 410 411 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) 412 { 413 struct mgb4_vin_dev *vindev = video_drvdata(file); 414 struct mgb4_regs *video = &vindev->mgbdev->video; 415 u32 config = mgb4_read_reg(video, vindev->config->regs.config); 416 417 f->fmt.pix.width = vindev->timings.bt.width; 418 f->fmt.pix.height = vindev->timings.bt.height; 419 f->fmt.pix.field = V4L2_FIELD_NONE; 420 421 if (config & (1U << 16)) { 422 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 423 if (config & (1U << 20)) { 424 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 425 } else { 426 if (config & (1U << 19)) 427 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 428 else 429 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; 430 } 431 f->fmt.pix.bytesperline = (f->fmt.pix.width + vindev->padding) * 2; 432 } else { 433 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32; 434 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW; 435 f->fmt.pix.bytesperline = (f->fmt.pix.width + vindev->padding) * 4; 436 } 437 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height; 438 439 return 0; 440 } 441 442 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f) 443 { 444 struct mgb4_vin_dev *vindev = video_drvdata(file); 445 struct mgb4_regs *video = &vindev->mgbdev->video; 446 u32 pixelsize; 447 448 f->fmt.pix.width = vindev->timings.bt.width; 449 f->fmt.pix.height = vindev->timings.bt.height; 450 f->fmt.pix.field = V4L2_FIELD_NONE; 451 452 if (has_yuv(video) && f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) { 453 pixelsize = 2; 454 if (!(f->fmt.pix.colorspace == V4L2_COLORSPACE_REC709 || 455 f->fmt.pix.colorspace == V4L2_COLORSPACE_SMPTE170M)) 456 f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; 457 } else { 458 pixelsize = 4; 459 f->fmt.pix.pixelformat = V4L2_PIX_FMT_ABGR32; 460 f->fmt.pix.colorspace = V4L2_COLORSPACE_RAW; 461 } 462 463 if (f->fmt.pix.bytesperline > f->fmt.pix.width * pixelsize && 464 f->fmt.pix.bytesperline < f->fmt.pix.width * pixelsize * 2) 465 f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline, 466 pixelsize); 467 else 468 f->fmt.pix.bytesperline = f->fmt.pix.width * pixelsize; 469 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height; 470 471 return 0; 472 } 473 474 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f) 475 { 476 struct mgb4_vin_dev *vindev = video_drvdata(file); 477 struct mgb4_regs *video = &vindev->mgbdev->video; 478 u32 config, pixelsize; 479 480 if (vb2_is_busy(&vindev->queue)) 481 return -EBUSY; 482 483 vidioc_try_fmt(file, priv, f); 484 485 config = mgb4_read_reg(video, vindev->config->regs.config); 486 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) { 487 pixelsize = 2; 488 config |= 1U << 16; 489 490 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_REC709) { 491 config |= 1U << 20; 492 config |= 1U << 19; 493 } else if (f->fmt.pix.colorspace == V4L2_COLORSPACE_SMPTE170M) { 494 config &= ~(1U << 20); 495 config |= 1U << 19; 496 } else { 497 config &= ~(1U << 20); 498 config &= ~(1U << 19); 499 } 500 } else { 501 pixelsize = 4; 502 config &= ~(1U << 16); 503 } 504 mgb4_write_reg(video, vindev->config->regs.config, config); 505 506 vindev->padding = (f->fmt.pix.bytesperline - (f->fmt.pix.width 507 * pixelsize)) / pixelsize; 508 mgb4_write_reg(video, vindev->config->regs.padding, vindev->padding); 509 set_loopback_padding(vindev, vindev->padding); 510 511 return 0; 512 } 513 514 static int vidioc_enum_input(struct file *file, void *priv, 515 struct v4l2_input *i) 516 { 517 struct mgb4_vin_dev *vindev = video_drvdata(file); 518 struct mgb4_regs *video = &vindev->mgbdev->video; 519 u32 status; 520 521 if (i->index != 0) 522 return -EINVAL; 523 524 strscpy(i->name, "MGB4", sizeof(i->name)); 525 i->type = V4L2_INPUT_TYPE_CAMERA; 526 i->capabilities = V4L2_IN_CAP_DV_TIMINGS; 527 i->status = 0; 528 529 status = mgb4_read_reg(video, vindev->config->regs.status); 530 if (!(status & (1U << 2))) 531 i->status |= V4L2_IN_ST_NO_SYNC; 532 if (!(status & (3 << 9))) 533 i->status |= V4L2_IN_ST_NO_SIGNAL; 534 535 return 0; 536 } 537 538 static int vidioc_enum_framesizes(struct file *file, void *fh, 539 struct v4l2_frmsizeenum *fsize) 540 { 541 struct mgb4_vin_dev *vindev = video_drvdata(file); 542 543 if (fsize->index != 0 || !(fsize->pixel_format == V4L2_PIX_FMT_ABGR32 || 544 fsize->pixel_format == V4L2_PIX_FMT_YUYV)) 545 return -EINVAL; 546 547 fsize->discrete.width = vindev->timings.bt.width; 548 fsize->discrete.height = vindev->timings.bt.height; 549 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 550 551 return 0; 552 } 553 554 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 555 { 556 return (i == 0) ? 0 : -EINVAL; 557 } 558 559 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 560 { 561 *i = 0; 562 return 0; 563 } 564 565 static int vidioc_g_parm(struct file *file, void *priv, 566 struct v4l2_streamparm *parm) 567 { 568 struct mgb4_vin_dev *vindev = video_drvdata(file); 569 struct mgb4_regs *video = &vindev->mgbdev->video; 570 struct v4l2_fract *tpf = &parm->parm.output.timeperframe; 571 u32 timer; 572 573 parm->parm.capture.readbuffers = 2; 574 575 if (has_timeperframe(video)) { 576 timer = mgb4_read_reg(video, vindev->config->regs.timer); 577 if (timer < 0xFFFF) { 578 tpf->numerator = pixel_size(&vindev->timings); 579 tpf->denominator = vindev->timings.bt.pixelclock; 580 } else { 581 tpf->numerator = timer; 582 tpf->denominator = MGB4_HW_FREQ; 583 } 584 585 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 586 } 587 588 return 0; 589 } 590 591 static int vidioc_s_parm(struct file *file, void *priv, 592 struct v4l2_streamparm *parm) 593 { 594 struct mgb4_vin_dev *vindev = video_drvdata(file); 595 struct mgb4_regs *video = &vindev->mgbdev->video; 596 struct v4l2_fract *tpf = &parm->parm.output.timeperframe; 597 u32 period, timer; 598 599 if (has_timeperframe(video)) { 600 timer = tpf->denominator ? 601 MGB4_PERIOD(tpf->numerator, tpf->denominator) : 0; 602 if (timer) { 603 period = MGB4_PERIOD(pixel_size(&vindev->timings), 604 vindev->timings.bt.pixelclock); 605 if (timer < period) 606 timer = 0; 607 } 608 609 mgb4_write_reg(video, vindev->config->regs.timer, timer); 610 } 611 612 return vidioc_g_parm(file, priv, parm); 613 } 614 615 static int vidioc_s_dv_timings(struct file *file, void *fh, 616 struct v4l2_dv_timings *timings) 617 { 618 struct mgb4_vin_dev *vindev = video_drvdata(file); 619 620 if (timings->bt.width < video_timings_cap.bt.min_width || 621 timings->bt.width > video_timings_cap.bt.max_width || 622 timings->bt.height < video_timings_cap.bt.min_height || 623 timings->bt.height > video_timings_cap.bt.max_height) 624 return -EINVAL; 625 if (timings->bt.width == vindev->timings.bt.width && 626 timings->bt.height == vindev->timings.bt.height) 627 return 0; 628 if (vb2_is_busy(&vindev->queue)) 629 return -EBUSY; 630 631 vindev->timings = *timings; 632 633 return 0; 634 } 635 636 static int vidioc_g_dv_timings(struct file *file, void *fh, 637 struct v4l2_dv_timings *timings) 638 { 639 struct mgb4_vin_dev *vindev = video_drvdata(file); 640 *timings = vindev->timings; 641 642 return 0; 643 } 644 645 static int vidioc_query_dv_timings(struct file *file, void *fh, 646 struct v4l2_dv_timings *timings) 647 { 648 struct mgb4_vin_dev *vindev = video_drvdata(file); 649 650 return get_timings(vindev, timings); 651 } 652 653 static int vidioc_enum_dv_timings(struct file *file, void *fh, 654 struct v4l2_enum_dv_timings *timings) 655 { 656 return v4l2_enum_dv_timings_cap(timings, &video_timings_cap, NULL, NULL); 657 } 658 659 static int vidioc_dv_timings_cap(struct file *file, void *fh, 660 struct v4l2_dv_timings_cap *cap) 661 { 662 *cap = video_timings_cap; 663 664 return 0; 665 } 666 667 static int vidioc_subscribe_event(struct v4l2_fh *fh, 668 const struct v4l2_event_subscription *sub) 669 { 670 switch (sub->type) { 671 case V4L2_EVENT_SOURCE_CHANGE: 672 return v4l2_src_change_event_subscribe(fh, sub); 673 } 674 675 return v4l2_ctrl_subscribe_event(fh, sub); 676 } 677 678 static const struct v4l2_ioctl_ops video_ioctl_ops = { 679 .vidioc_querycap = vidioc_querycap, 680 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt, 681 .vidioc_try_fmt_vid_cap = vidioc_try_fmt, 682 .vidioc_s_fmt_vid_cap = vidioc_s_fmt, 683 .vidioc_g_fmt_vid_cap = vidioc_g_fmt, 684 .vidioc_enum_framesizes = vidioc_enum_framesizes, 685 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 686 .vidioc_enum_input = vidioc_enum_input, 687 .vidioc_g_input = vidioc_g_input, 688 .vidioc_s_input = vidioc_s_input, 689 .vidioc_reqbufs = vb2_ioctl_reqbufs, 690 .vidioc_create_bufs = vb2_ioctl_create_bufs, 691 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 692 .vidioc_querybuf = vb2_ioctl_querybuf, 693 .vidioc_qbuf = vb2_ioctl_qbuf, 694 .vidioc_dqbuf = vb2_ioctl_dqbuf, 695 .vidioc_expbuf = vb2_ioctl_expbuf, 696 .vidioc_streamon = vb2_ioctl_streamon, 697 .vidioc_streamoff = vb2_ioctl_streamoff, 698 .vidioc_g_parm = vidioc_g_parm, 699 .vidioc_s_parm = vidioc_s_parm, 700 .vidioc_dv_timings_cap = vidioc_dv_timings_cap, 701 .vidioc_enum_dv_timings = vidioc_enum_dv_timings, 702 .vidioc_g_dv_timings = vidioc_g_dv_timings, 703 .vidioc_s_dv_timings = vidioc_s_dv_timings, 704 .vidioc_query_dv_timings = vidioc_query_dv_timings, 705 .vidioc_subscribe_event = vidioc_subscribe_event, 706 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 707 }; 708 709 static void dma_transfer(struct work_struct *work) 710 { 711 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev, 712 dma_work); 713 struct mgb4_regs *video = &vindev->mgbdev->video; 714 struct device *dev = &vindev->mgbdev->pdev->dev; 715 struct mgb4_frame_buffer *buf = NULL; 716 unsigned long flags; 717 u32 addr; 718 int rv; 719 720 spin_lock_irqsave(&vindev->qlock, flags); 721 if (!list_empty(&vindev->buf_list)) { 722 buf = list_first_entry(&vindev->buf_list, 723 struct mgb4_frame_buffer, list); 724 list_del_init(vindev->buf_list.next); 725 } 726 spin_unlock_irqrestore(&vindev->qlock, flags); 727 728 if (!buf) 729 return; 730 731 addr = mgb4_read_reg(video, vindev->config->regs.address); 732 if (addr >= MGB4_ERR_QUEUE_FULL) { 733 dev_dbg(dev, "frame queue error (%d)\n", (int)addr); 734 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 735 return; 736 } 737 738 rv = mgb4_dma_transfer(vindev->mgbdev, vindev->config->dma_channel, 739 false, addr, 740 vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0)); 741 if (rv < 0) { 742 dev_warn(dev, "DMA transfer error\n"); 743 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 744 } else { 745 buf->vb.vb2_buf.timestamp = ktime_get_ns(); 746 buf->vb.sequence = vindev->sequence++; 747 buf->vb.field = V4L2_FIELD_NONE; 748 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); 749 } 750 } 751 752 static void signal_change(struct work_struct *work) 753 { 754 struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev, 755 err_work); 756 struct mgb4_regs *video = &vindev->mgbdev->video; 757 struct v4l2_bt_timings *timings = &vindev->timings.bt; 758 struct device *dev = &vindev->mgbdev->pdev->dev; 759 760 u32 resolution = mgb4_read_reg(video, vindev->config->regs.resolution); 761 u32 width = resolution >> 16; 762 u32 height = resolution & 0xFFFF; 763 764 if (timings->width != width || timings->height != height) { 765 static const struct v4l2_event ev = { 766 .type = V4L2_EVENT_SOURCE_CHANGE, 767 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, 768 }; 769 770 v4l2_event_queue(&vindev->vdev, &ev); 771 772 if (vb2_is_streaming(&vindev->queue)) 773 vb2_queue_error(&vindev->queue); 774 } 775 776 dev_dbg(dev, "stream changed to %ux%u\n", width, height); 777 } 778 779 static irqreturn_t vin_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->dma_work); 785 786 mgb4_write_reg(video, 0xB4, 1U << vindev->config->vin_irq); 787 788 return IRQ_HANDLED; 789 } 790 791 static irqreturn_t err_handler(int irq, void *ctx) 792 { 793 struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx; 794 struct mgb4_regs *video = &vindev->mgbdev->video; 795 796 schedule_work(&vindev->err_work); 797 798 mgb4_write_reg(video, 0xB4, 1U << vindev->config->err_irq); 799 800 return IRQ_HANDLED; 801 } 802 803 static int deser_init(struct mgb4_vin_dev *vindev, int id) 804 { 805 int rv, addr_size; 806 size_t values_count; 807 const struct mgb4_i2c_kv *values; 808 const struct i2c_board_info *info; 809 struct device *dev = &vindev->mgbdev->pdev->dev; 810 811 if (MGB4_IS_GMSL(vindev->mgbdev)) { 812 info = &gmsl_deser_info[id]; 813 addr_size = 16; 814 values = gmsl_i2c; 815 values_count = ARRAY_SIZE(gmsl_i2c); 816 } else { 817 info = &fpdl3_deser_info[id]; 818 addr_size = 8; 819 values = fpdl3_i2c; 820 values_count = ARRAY_SIZE(fpdl3_i2c); 821 } 822 823 rv = mgb4_i2c_init(&vindev->deser, vindev->mgbdev->i2c_adap, info, 824 addr_size); 825 if (rv < 0) { 826 dev_err(dev, "failed to create deserializer\n"); 827 return rv; 828 } 829 rv = mgb4_i2c_configure(&vindev->deser, values, values_count); 830 if (rv < 0) { 831 dev_err(dev, "failed to configure deserializer\n"); 832 goto err_i2c_dev; 833 } 834 835 return 0; 836 837 err_i2c_dev: 838 mgb4_i2c_free(&vindev->deser); 839 840 return rv; 841 } 842 843 static void fpga_init(struct mgb4_vin_dev *vindev) 844 { 845 struct mgb4_regs *video = &vindev->mgbdev->video; 846 const struct mgb4_vin_regs *regs = &vindev->config->regs; 847 848 mgb4_write_reg(video, regs->config, 0x00000001); 849 mgb4_write_reg(video, regs->sync, 0x03E80002); 850 mgb4_write_reg(video, regs->padding, 0x00000000); 851 mgb4_write_reg(video, regs->config, 1U << 9); 852 } 853 854 static void create_debugfs(struct mgb4_vin_dev *vindev) 855 { 856 #ifdef CONFIG_DEBUG_FS 857 struct mgb4_regs *video = &vindev->mgbdev->video; 858 struct dentry *entry; 859 860 if (IS_ERR_OR_NULL(vindev->mgbdev->debugfs)) 861 return; 862 entry = debugfs_create_dir(vindev->vdev.name, vindev->mgbdev->debugfs); 863 if (IS_ERR(entry)) 864 return; 865 866 vindev->regs[0].name = "CONFIG"; 867 vindev->regs[0].offset = vindev->config->regs.config; 868 vindev->regs[1].name = "STATUS"; 869 vindev->regs[1].offset = vindev->config->regs.status; 870 vindev->regs[2].name = "RESOLUTION"; 871 vindev->regs[2].offset = vindev->config->regs.resolution; 872 vindev->regs[3].name = "FRAME_PERIOD"; 873 vindev->regs[3].offset = vindev->config->regs.frame_period; 874 vindev->regs[4].name = "HS_VS_GENER_SETTINGS"; 875 vindev->regs[4].offset = vindev->config->regs.sync; 876 vindev->regs[5].name = "PCLK_FREQUENCY"; 877 vindev->regs[5].offset = vindev->config->regs.pclk; 878 vindev->regs[6].name = "VIDEO_PARAMS_1"; 879 vindev->regs[6].offset = vindev->config->regs.signal; 880 vindev->regs[7].name = "VIDEO_PARAMS_2"; 881 vindev->regs[7].offset = vindev->config->regs.signal2; 882 vindev->regs[8].name = "PADDING_PIXELS"; 883 vindev->regs[8].offset = vindev->config->regs.padding; 884 if (has_timeperframe(video)) { 885 vindev->regs[9].name = "TIMER"; 886 vindev->regs[9].offset = vindev->config->regs.timer; 887 vindev->regset.nregs = 10; 888 } else { 889 vindev->regset.nregs = 9; 890 } 891 892 vindev->regset.base = video->membase; 893 vindev->regset.regs = vindev->regs; 894 895 debugfs_create_regset32("registers", 0444, entry, &vindev->regset); 896 #endif 897 } 898 899 struct mgb4_vin_dev *mgb4_vin_create(struct mgb4_dev *mgbdev, int id) 900 { 901 int rv; 902 const struct attribute_group **groups; 903 struct mgb4_vin_dev *vindev; 904 struct pci_dev *pdev = mgbdev->pdev; 905 struct device *dev = &pdev->dev; 906 int vin_irq, err_irq; 907 908 vindev = kzalloc(sizeof(*vindev), GFP_KERNEL); 909 if (!vindev) 910 return NULL; 911 912 vindev->mgbdev = mgbdev; 913 vindev->config = &vin_cfg[id]; 914 915 /* Frame queue*/ 916 INIT_LIST_HEAD(&vindev->buf_list); 917 spin_lock_init(&vindev->qlock); 918 919 /* Work queues */ 920 INIT_WORK(&vindev->dma_work, dma_transfer); 921 INIT_WORK(&vindev->err_work, signal_change); 922 923 /* IRQ callback */ 924 vin_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->vin_irq); 925 rv = request_irq(vin_irq, vin_handler, 0, "mgb4-vin", vindev); 926 if (rv) { 927 dev_err(dev, "failed to register vin irq handler\n"); 928 goto err_alloc; 929 } 930 /* Error IRQ callback */ 931 err_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->err_irq); 932 rv = request_irq(err_irq, err_handler, 0, "mgb4-err", vindev); 933 if (rv) { 934 dev_err(dev, "failed to register err irq handler\n"); 935 goto err_vin_irq; 936 } 937 938 /* Set the FPGA registers default values */ 939 fpga_init(vindev); 940 941 /* Set the deserializer default values */ 942 rv = deser_init(vindev, id); 943 if (rv) 944 goto err_err_irq; 945 946 /* V4L2 stuff init */ 947 rv = v4l2_device_register(dev, &vindev->v4l2dev); 948 if (rv) { 949 dev_err(dev, "failed to register v4l2 device\n"); 950 goto err_err_irq; 951 } 952 953 mutex_init(&vindev->lock); 954 955 vindev->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 956 vindev->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 957 vindev->queue.buf_struct_size = sizeof(struct mgb4_frame_buffer); 958 vindev->queue.ops = &queue_ops; 959 vindev->queue.mem_ops = &vb2_dma_sg_memops; 960 vindev->queue.gfp_flags = GFP_DMA32; 961 vindev->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 962 vindev->queue.min_queued_buffers = 2; 963 vindev->queue.drv_priv = vindev; 964 vindev->queue.lock = &vindev->lock; 965 vindev->queue.dev = dev; 966 rv = vb2_queue_init(&vindev->queue); 967 if (rv) { 968 dev_err(dev, "failed to initialize vb2 queue\n"); 969 goto err_v4l2_dev; 970 } 971 972 snprintf(vindev->vdev.name, sizeof(vindev->vdev.name), "mgb4-in%d", 973 id + 1); 974 vindev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE 975 | V4L2_CAP_STREAMING; 976 vindev->vdev.fops = &video_fops; 977 vindev->vdev.ioctl_ops = &video_ioctl_ops; 978 vindev->vdev.release = video_device_release_empty; 979 vindev->vdev.v4l2_dev = &vindev->v4l2dev; 980 vindev->vdev.lock = &vindev->lock; 981 vindev->vdev.queue = &vindev->queue; 982 video_set_drvdata(&vindev->vdev, vindev); 983 984 /* Enable the video signal change watcher */ 985 xdma_enable_user_irq(vindev->mgbdev->xdev, err_irq); 986 987 /* Register the video device */ 988 rv = video_register_device(&vindev->vdev, VFL_TYPE_VIDEO, -1); 989 if (rv) { 990 dev_err(dev, "failed to register video device\n"); 991 goto err_v4l2_dev; 992 } 993 994 /* Module sysfs attributes */ 995 groups = MGB4_IS_GMSL(mgbdev) 996 ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups; 997 rv = device_add_groups(&vindev->vdev.dev, groups); 998 if (rv) { 999 dev_err(dev, "failed to create sysfs attributes\n"); 1000 goto err_video_dev; 1001 } 1002 1003 create_debugfs(vindev); 1004 1005 return vindev; 1006 1007 err_video_dev: 1008 video_unregister_device(&vindev->vdev); 1009 err_v4l2_dev: 1010 v4l2_device_unregister(&vindev->v4l2dev); 1011 err_err_irq: 1012 free_irq(err_irq, vindev); 1013 err_vin_irq: 1014 free_irq(vin_irq, vindev); 1015 err_alloc: 1016 kfree(vindev); 1017 1018 return NULL; 1019 } 1020 1021 void mgb4_vin_free(struct mgb4_vin_dev *vindev) 1022 { 1023 const struct attribute_group **groups; 1024 int vin_irq = xdma_get_user_irq(vindev->mgbdev->xdev, 1025 vindev->config->vin_irq); 1026 int err_irq = xdma_get_user_irq(vindev->mgbdev->xdev, 1027 vindev->config->err_irq); 1028 1029 xdma_disable_user_irq(vindev->mgbdev->xdev, err_irq); 1030 1031 free_irq(vin_irq, vindev); 1032 free_irq(err_irq, vindev); 1033 1034 groups = MGB4_IS_GMSL(vindev->mgbdev) 1035 ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups; 1036 device_remove_groups(&vindev->vdev.dev, groups); 1037 1038 mgb4_i2c_free(&vindev->deser); 1039 video_unregister_device(&vindev->vdev); 1040 v4l2_device_unregister(&vindev->v4l2dev); 1041 1042 kfree(vindev); 1043 } 1044