File Index
Files and directories
Sign in
FILE

mgb4_vin.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/pci/mgb4/mgb4_vin.c C
   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
  34ATTRIBUTE_GROUPS(mgb4_fpdl3_in);
  35ATTRIBUTE_GROUPS(mgb4_gmsl_in);
  36
  37static 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
  42static const struct i2c_board_info fpdl3_deser_info[] = {
  43	{I2C_BOARD_INFO("deserializer1", 0x38)},
  44	{I2C_BOARD_INFO("deserializer2", 0x36)},
  45};
  46
  47static const struct i2c_board_info gmsl_deser_info[] = {
  48	{I2C_BOARD_INFO("deserializer1", 0x4C)},
  49	{I2C_BOARD_INFO("deserializer2", 0x2A)},
  50};
  51
  52static 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
  57static 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
  64static const struct v4l2_dv_timings_cap video_timings_cap = {
  65	.type = V4L2_DV_BT_656_1120,
  66	.bt = {
  67		.min_width = 240,
  68		.max_width = 4096,
  69		.min_height = 240,
  70		.max_height = 4096,
  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 */
  81static 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 */
  87static 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 */
 108static 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 */
 124static 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
 138static 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 hsync = mgb4_read_reg(video, regs->hsync);
 147	u32 vsync = mgb4_read_reg(video, regs->vsync);
 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 = (hsync & 0x00FF0000) >> 16;
 165	timings->bt.vsync = (vsync & 0x00FF0000) >> 16;
 166	timings->bt.hbackporch = (hsync & 0x0000FF00) >> 8;
 167	timings->bt.hfrontporch = hsync & 0x000000FF;
 168	timings->bt.vbackporch = (vsync & 0x0000FF00) >> 8;
 169	timings->bt.vfrontporch = vsync & 0x000000FF;
 170
 171	return 0;
 172}
 173
 174static 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
 188static 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
 217static 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
 227static 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
 248static 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
 260static 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
 284static 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
 308static 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
 317static 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
 334out:
 335	mutex_unlock(&vindev->lock);
 336	return rv;
 337}
 338
 339static 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
 349static 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
 358static 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
 375static 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
 401static 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
 432static 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
 464static 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
 502static 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
 526static 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
 542static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
 543{
 544	return (i == 0) ? 0 : -EINVAL;
 545}
 546
 547static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
 548{
 549	*i = 0;
 550	return 0;
 551}
 552
 553static 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
 579static 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
 603static 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 (v4l2_match_dv_timings(timings, &vindev->timings, 0, false))
 614		return 0;
 615	if (vb2_is_busy(&vindev->queue))
 616		return -EBUSY;
 617
 618	vindev->timings = *timings;
 619
 620	return 0;
 621}
 622
 623static int vidioc_g_dv_timings(struct file *file, void *fh,
 624			       struct v4l2_dv_timings *timings)
 625{
 626	struct mgb4_vin_dev *vindev = video_drvdata(file);
 627	*timings = vindev->timings;
 628
 629	return 0;
 630}
 631
 632static int vidioc_query_dv_timings(struct file *file, void *fh,
 633				   struct v4l2_dv_timings *timings)
 634{
 635	struct mgb4_vin_dev *vindev = video_drvdata(file);
 636
 637	return get_timings(vindev, timings);
 638}
 639
 640static int vidioc_enum_dv_timings(struct file *file, void *fh,
 641				  struct v4l2_enum_dv_timings *timings)
 642{
 643	struct mgb4_vin_dev *vindev = video_drvdata(file);
 644
 645	if (timings->index != 0)
 646		return -EINVAL;
 647	if (get_timings(vindev, &timings->timings) < 0)
 648		return -ENODATA;
 649
 650	return 0;
 651}
 652
 653static int vidioc_dv_timings_cap(struct file *file, void *fh,
 654				 struct v4l2_dv_timings_cap *cap)
 655{
 656	*cap = video_timings_cap;
 657
 658	return 0;
 659}
 660
 661static int vidioc_subscribe_event(struct v4l2_fh *fh,
 662				  const struct v4l2_event_subscription *sub)
 663{
 664	switch (sub->type) {
 665	case V4L2_EVENT_SOURCE_CHANGE:
 666		return v4l2_src_change_event_subscribe(fh, sub);
 667	}
 668
 669	return v4l2_ctrl_subscribe_event(fh, sub);
 670}
 671
 672static const struct v4l2_ioctl_ops video_ioctl_ops = {
 673	.vidioc_querycap = vidioc_querycap,
 674	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
 675	.vidioc_try_fmt_vid_cap = vidioc_try_fmt,
 676	.vidioc_s_fmt_vid_cap = vidioc_s_fmt,
 677	.vidioc_g_fmt_vid_cap = vidioc_g_fmt,
 678	.vidioc_enum_framesizes = vidioc_enum_framesizes,
 679	.vidioc_enum_frameintervals = vidioc_enum_frameintervals,
 680	.vidioc_enum_input = vidioc_enum_input,
 681	.vidioc_g_input = vidioc_g_input,
 682	.vidioc_s_input = vidioc_s_input,
 683	.vidioc_reqbufs = vb2_ioctl_reqbufs,
 684	.vidioc_create_bufs = vb2_ioctl_create_bufs,
 685	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
 686	.vidioc_querybuf = vb2_ioctl_querybuf,
 687	.vidioc_qbuf = vb2_ioctl_qbuf,
 688	.vidioc_dqbuf = vb2_ioctl_dqbuf,
 689	.vidioc_expbuf = vb2_ioctl_expbuf,
 690	.vidioc_streamon = vb2_ioctl_streamon,
 691	.vidioc_streamoff = vb2_ioctl_streamoff,
 692	.vidioc_g_parm = vidioc_g_parm,
 693	.vidioc_s_parm = vidioc_s_parm,
 694	.vidioc_dv_timings_cap = vidioc_dv_timings_cap,
 695	.vidioc_enum_dv_timings = vidioc_enum_dv_timings,
 696	.vidioc_g_dv_timings = vidioc_g_dv_timings,
 697	.vidioc_s_dv_timings = vidioc_s_dv_timings,
 698	.vidioc_query_dv_timings = vidioc_query_dv_timings,
 699	.vidioc_subscribe_event = vidioc_subscribe_event,
 700	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 701};
 702
 703static void dma_transfer(struct work_struct *work)
 704{
 705	struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
 706						   dma_work);
 707	struct mgb4_regs *video = &vindev->mgbdev->video;
 708	struct device *dev = &vindev->mgbdev->pdev->dev;
 709	struct mgb4_frame_buffer *buf = NULL;
 710	unsigned long flags;
 711	u32 addr;
 712	int rv;
 713
 714	spin_lock_irqsave(&vindev->qlock, flags);
 715	if (!list_empty(&vindev->buf_list)) {
 716		buf = list_first_entry(&vindev->buf_list,
 717				       struct mgb4_frame_buffer, list);
 718		list_del_init(vindev->buf_list.next);
 719	}
 720	spin_unlock_irqrestore(&vindev->qlock, flags);
 721
 722	if (!buf)
 723		return;
 724
 725	addr = mgb4_read_reg(video, vindev->config->regs.address);
 726	if (addr >= MGB4_ERR_QUEUE_FULL) {
 727		dev_dbg(dev, "frame queue error (%d)\n", (int)addr);
 728		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 729		return;
 730	}
 731
 732	rv = mgb4_dma_transfer(vindev->mgbdev, vindev->config->dma_channel,
 733			       false, addr,
 734			       vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0));
 735	if (rv < 0) {
 736		dev_warn(dev, "DMA transfer error\n");
 737		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 738	} else {
 739		buf->vb.vb2_buf.timestamp = ktime_get_ns();
 740		buf->vb.sequence = vindev->sequence++;
 741		buf->vb.field = V4L2_FIELD_NONE;
 742		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 743	}
 744}
 745
 746static void signal_change(struct work_struct *work)
 747{
 748	struct mgb4_vin_dev *vindev = container_of(work, struct mgb4_vin_dev,
 749						   err_work);
 750	struct mgb4_regs *video = &vindev->mgbdev->video;
 751	struct v4l2_bt_timings *timings = &vindev->timings.bt;
 752	struct device *dev = &vindev->mgbdev->pdev->dev;
 753
 754	u32 resolution = mgb4_read_reg(video, vindev->config->regs.resolution);
 755	u32 width = resolution >> 16;
 756	u32 height = resolution & 0xFFFF;
 757
 758	static const struct v4l2_event ev = {
 759		.type = V4L2_EVENT_SOURCE_CHANGE,
 760		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
 761	};
 762
 763	v4l2_event_queue(&vindev->vdev, &ev);
 764
 765	if (timings->width != width || timings->height != height) {
 766		if (vb2_is_streaming(&vindev->queue))
 767			vb2_queue_error(&vindev->queue);
 768	}
 769
 770	dev_dbg(dev, "stream changed to %ux%u\n", width, height);
 771}
 772
 773static irqreturn_t vin_handler(int irq, void *ctx)
 774{
 775	struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
 776	struct mgb4_regs *video = &vindev->mgbdev->video;
 777
 778	schedule_work(&vindev->dma_work);
 779
 780	mgb4_write_reg(video, 0xB4, 1U << vindev->config->vin_irq);
 781
 782	return IRQ_HANDLED;
 783}
 784
 785static irqreturn_t err_handler(int irq, void *ctx)
 786{
 787	struct mgb4_vin_dev *vindev = (struct mgb4_vin_dev *)ctx;
 788	struct mgb4_regs *video = &vindev->mgbdev->video;
 789
 790	schedule_work(&vindev->err_work);
 791
 792	mgb4_write_reg(video, 0xB4, 1U << vindev->config->err_irq);
 793
 794	return IRQ_HANDLED;
 795}
 796
 797static int deser_init(struct mgb4_vin_dev *vindev, int id)
 798{
 799	int rv, addr_size;
 800	size_t values_count;
 801	const struct mgb4_i2c_kv *values;
 802	const struct i2c_board_info *info;
 803	struct device *dev = &vindev->mgbdev->pdev->dev;
 804
 805	if (MGB4_IS_GMSL(vindev->mgbdev)) {
 806		info = &gmsl_deser_info[id];
 807		addr_size = 16;
 808		values = gmsl_i2c;
 809		values_count = ARRAY_SIZE(gmsl_i2c);
 810	} else {
 811		info = &fpdl3_deser_info[id];
 812		addr_size = 8;
 813		values = fpdl3_i2c;
 814		values_count = ARRAY_SIZE(fpdl3_i2c);
 815	}
 816
 817	rv = mgb4_i2c_init(&vindev->deser, vindev->mgbdev->i2c_adap, info,
 818			   addr_size);
 819	if (rv < 0) {
 820		dev_err(dev, "failed to create deserializer\n");
 821		return rv;
 822	}
 823	rv = mgb4_i2c_configure(&vindev->deser, values, values_count);
 824	if (rv < 0) {
 825		dev_err(dev, "failed to configure deserializer\n");
 826		goto err_i2c_dev;
 827	}
 828
 829	return 0;
 830
 831err_i2c_dev:
 832	mgb4_i2c_free(&vindev->deser);
 833
 834	return rv;
 835}
 836
 837static void fpga_init(struct mgb4_vin_dev *vindev)
 838{
 839	struct mgb4_regs *video = &vindev->mgbdev->video;
 840	const struct mgb4_vin_regs *regs = &vindev->config->regs;
 841
 842	mgb4_write_reg(video, regs->config, 0x00000001);
 843	mgb4_write_reg(video, regs->sync, 0x03E80002);
 844	mgb4_write_reg(video, regs->padding, 0x00000000);
 845	mgb4_write_reg(video, regs->config, 1U << 9);
 846}
 847
 848static void create_debugfs(struct mgb4_vin_dev *vindev)
 849{
 850#ifdef CONFIG_DEBUG_FS
 851	struct mgb4_regs *video = &vindev->mgbdev->video;
 852	struct dentry *entry;
 853
 854	if (IS_ERR_OR_NULL(vindev->mgbdev->debugfs))
 855		return;
 856	entry = debugfs_create_dir(vindev->vdev.name, vindev->mgbdev->debugfs);
 857	if (IS_ERR(entry))
 858		return;
 859
 860	vindev->regs[0].name = "CONFIG";
 861	vindev->regs[0].offset = vindev->config->regs.config;
 862	vindev->regs[1].name = "STATUS";
 863	vindev->regs[1].offset = vindev->config->regs.status;
 864	vindev->regs[2].name = "RESOLUTION";
 865	vindev->regs[2].offset = vindev->config->regs.resolution;
 866	vindev->regs[3].name = "FRAME_PERIOD";
 867	vindev->regs[3].offset = vindev->config->regs.frame_period;
 868	vindev->regs[4].name = "HS_VS_GENER_SETTINGS";
 869	vindev->regs[4].offset = vindev->config->regs.sync;
 870	vindev->regs[5].name = "PCLK_FREQUENCY";
 871	vindev->regs[5].offset = vindev->config->regs.pclk;
 872	vindev->regs[6].name = "VIDEO_PARAMS_1";
 873	vindev->regs[6].offset = vindev->config->regs.hsync;
 874	vindev->regs[7].name = "VIDEO_PARAMS_2";
 875	vindev->regs[7].offset = vindev->config->regs.vsync;
 876	vindev->regs[8].name = "PADDING_PIXELS";
 877	vindev->regs[8].offset = vindev->config->regs.padding;
 878	if (has_timeperframe(video)) {
 879		vindev->regs[9].name = "TIMER";
 880		vindev->regs[9].offset = vindev->config->regs.timer;
 881		vindev->regset.nregs = 10;
 882	} else {
 883		vindev->regset.nregs = 9;
 884	}
 885
 886	vindev->regset.base = video->membase;
 887	vindev->regset.regs = vindev->regs;
 888
 889	debugfs_create_regset32("registers", 0444, entry, &vindev->regset);
 890#endif
 891}
 892
 893struct mgb4_vin_dev *mgb4_vin_create(struct mgb4_dev *mgbdev, int id)
 894{
 895	int rv;
 896	const struct attribute_group **groups;
 897	struct mgb4_vin_dev *vindev;
 898	struct pci_dev *pdev = mgbdev->pdev;
 899	struct device *dev = &pdev->dev;
 900	int vin_irq, err_irq;
 901
 902	vindev = kzalloc(sizeof(*vindev), GFP_KERNEL);
 903	if (!vindev)
 904		return NULL;
 905
 906	vindev->mgbdev = mgbdev;
 907	vindev->config = &vin_cfg[id];
 908
 909	/* Frame queue*/
 910	INIT_LIST_HEAD(&vindev->buf_list);
 911	spin_lock_init(&vindev->qlock);
 912
 913	/* Work queues */
 914	INIT_WORK(&vindev->dma_work, dma_transfer);
 915	INIT_WORK(&vindev->err_work, signal_change);
 916
 917	/* IRQ callback */
 918	vin_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->vin_irq);
 919	rv = request_irq(vin_irq, vin_handler, 0, "mgb4-vin", vindev);
 920	if (rv) {
 921		dev_err(dev, "failed to register vin irq handler\n");
 922		goto err_alloc;
 923	}
 924	/* Error IRQ callback */
 925	err_irq = xdma_get_user_irq(mgbdev->xdev, vindev->config->err_irq);
 926	rv = request_irq(err_irq, err_handler, 0, "mgb4-err", vindev);
 927	if (rv) {
 928		dev_err(dev, "failed to register err irq handler\n");
 929		goto err_vin_irq;
 930	}
 931
 932	/* Set the FPGA registers default values */
 933	fpga_init(vindev);
 934
 935	/* Set the deserializer default values */
 936	rv = deser_init(vindev, id);
 937	if (rv)
 938		goto err_err_irq;
 939
 940	/* V4L2 stuff init */
 941	rv = v4l2_device_register(dev, &vindev->v4l2dev);
 942	if (rv) {
 943		dev_err(dev, "failed to register v4l2 device\n");
 944		goto err_err_irq;
 945	}
 946
 947	mutex_init(&vindev->lock);
 948
 949	vindev->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 950	vindev->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
 951	vindev->queue.buf_struct_size = sizeof(struct mgb4_frame_buffer);
 952	vindev->queue.ops = &queue_ops;
 953	vindev->queue.mem_ops = &vb2_dma_sg_memops;
 954	vindev->queue.gfp_flags = GFP_DMA32;
 955	vindev->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 956	vindev->queue.min_queued_buffers = 2;
 957	vindev->queue.drv_priv = vindev;
 958	vindev->queue.lock = &vindev->lock;
 959	vindev->queue.dev = dev;
 960	rv = vb2_queue_init(&vindev->queue);
 961	if (rv) {
 962		dev_err(dev, "failed to initialize vb2 queue\n");
 963		goto err_v4l2_dev;
 964	}
 965
 966	snprintf(vindev->vdev.name, sizeof(vindev->vdev.name), "mgb4-in%d",
 967		 id + 1);
 968	vindev->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE
 969	  | V4L2_CAP_STREAMING;
 970	vindev->vdev.fops = &video_fops;
 971	vindev->vdev.ioctl_ops = &video_ioctl_ops;
 972	vindev->vdev.release = video_device_release_empty;
 973	vindev->vdev.v4l2_dev = &vindev->v4l2dev;
 974	vindev->vdev.lock = &vindev->lock;
 975	vindev->vdev.queue = &vindev->queue;
 976	video_set_drvdata(&vindev->vdev, vindev);
 977
 978	/* Enable the video signal change watcher */
 979	xdma_enable_user_irq(vindev->mgbdev->xdev, err_irq);
 980
 981	/* Register the video device */
 982	rv = video_register_device(&vindev->vdev, VFL_TYPE_VIDEO, -1);
 983	if (rv) {
 984		dev_err(dev, "failed to register video device\n");
 985		goto err_v4l2_dev;
 986	}
 987
 988	/* Module sysfs attributes */
 989	groups = MGB4_IS_GMSL(mgbdev)
 990	  ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups;
 991	rv = device_add_groups(&vindev->vdev.dev, groups);
 992	if (rv) {
 993		dev_err(dev, "failed to create sysfs attributes\n");
 994		goto err_video_dev;
 995	}
 996
 997	create_debugfs(vindev);
 998
 999	return vindev;
1000
1001err_video_dev:
1002	video_unregister_device(&vindev->vdev);
1003err_v4l2_dev:
1004	v4l2_device_unregister(&vindev->v4l2dev);
1005err_err_irq:
1006	free_irq(err_irq, vindev);
1007err_vin_irq:
1008	free_irq(vin_irq, vindev);
1009err_alloc:
1010	kfree(vindev);
1011
1012	return NULL;
1013}
1014
1015void mgb4_vin_free(struct mgb4_vin_dev *vindev)
1016{
1017	const struct attribute_group **groups;
1018	int vin_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
1019					vindev->config->vin_irq);
1020	int err_irq = xdma_get_user_irq(vindev->mgbdev->xdev,
1021					vindev->config->err_irq);
1022
1023	xdma_disable_user_irq(vindev->mgbdev->xdev, err_irq);
1024
1025	free_irq(vin_irq, vindev);
1026	free_irq(err_irq, vindev);
1027
1028	groups = MGB4_IS_GMSL(vindev->mgbdev)
1029	  ? mgb4_gmsl_in_groups : mgb4_fpdl3_in_groups;
1030	device_remove_groups(&vindev->vdev.dev, groups);
1031
1032	mgb4_i2c_free(&vindev->deser);
1033	video_unregister_device(&vindev->vdev);
1034	v4l2_device_unregister(&vindev->v4l2dev);
1035
1036	kfree(vindev);
1037}
~/ Read-only file preview.