From nobody Fri Dec 19 14:08:49 2025 Received: from mx.gpxsee.org (mx.gpxsee.org [37.205.14.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97D163CF5E; Mon, 5 Aug 2024 15:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.14.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; cv=none; b=bn3tTuge17hl3oWsyyfPmgN5pGl6GWiSJv9JylWq8SmHx1wVwGi4NOPO0fV97kdTLIqjf8sNYeix7my5G3zDxN2X5Co0JJ91q0hBCE0i3rkZVQ62mzT7TS+w5FcVhk4eAQs1NaH0P4C56+h32mZA3YeA46K2iHIO9EnmhBftld8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; c=relaxed/simple; bh=k2dTbUgL2xrvU2m0TLwYcJBI7SipedC4gdovXsfzdrU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GoOAYgCi/U6pahNwJIlQB2Mu9YO1VA55DOsUkQXqjVsKPjCMHJlviChUAPakrPHAsYH+aiRp6WDIKBfzBqRDHdt9wC7zIfHQcfAuCqj8P0+BcbybnLYI30PEh8umVuwdmhHpVR9QqzjBienIUHyZTe3P1/cxVWASgCqr5Uc7hOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org; spf=pass smtp.mailfrom=gpxsee.org; arc=none smtp.client-ip=37.205.14.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gpxsee.org Received: from mgb4.. (unknown [62.77.71.229]) by mx.gpxsee.org (Postfix) with ESMTPSA id 0ACDF3CCDC; Mon, 05 Aug 2024 17:41:02 +0200 (CEST) From: tumic@gpxsee.org To: Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Martin=20T=C5=AFma?= Subject: [PATCH v6 1/4 RESEND] media: mgb4: Add support for YUV image formats Date: Mon, 5 Aug 2024 17:40:51 +0200 Message-ID: <20240805154054.8633-2-tumic@gpxsee.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240805154054.8633-1-tumic@gpxsee.org> References: <20240805154054.8633-1-tumic@gpxsee.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Martin T=C5=AFma Recent mgb4 firmwares support YUV in addition to the RGB image format. Enab= le YUV in the driver when the FW supports it. Signed-off-by: Martin T=C5=AFma --- drivers/media/pci/mgb4/mgb4_io.h | 7 ++ drivers/media/pci/mgb4/mgb4_vin.c | 107 ++++++++++++++++++++++------ drivers/media/pci/mgb4/mgb4_vout.c | 109 +++++++++++++++++++++++------ 3 files changed, 183 insertions(+), 40 deletions(-) diff --git a/drivers/media/pci/mgb4/mgb4_io.h b/drivers/media/pci/mgb4/mgb4= _io.h index 8698db1be4a9..204613a6685c 100644 --- a/drivers/media/pci/mgb4/mgb4_io.h +++ b/drivers/media/pci/mgb4/mgb4_io.h @@ -30,4 +30,11 @@ static inline struct mgb4_frame_buffer *to_frame_buffer(= struct vb2_v4l2_buffer * return container_of(vbuf, struct mgb4_frame_buffer, vb); } =20 +static inline bool has_yuv(struct mgb4_regs *video) +{ + u32 status =3D mgb4_read_reg(video, 0xD0); + + return (status & (1U << 8)); +} + #endif diff --git a/drivers/media/pci/mgb4/mgb4_vin.c b/drivers/media/pci/mgb4/mgb= 4_vin.c index 2cd78c539889..7fb14e867e8d 100644 --- a/drivers/media/pci/mgb4/mgb4_vin.c +++ b/drivers/media/pci/mgb4/mgb4_vin.c @@ -186,8 +186,11 @@ static int queue_setup(struct vb2_queue *q, unsigned i= nt *nbuffers, struct device *alloc_devs[]) { struct mgb4_vin_dev *vindev =3D vb2_get_drv_priv(q); + struct mgb4_regs *video =3D &vindev->mgbdev->video; + u32 config =3D mgb4_read_reg(video, vindev->config->regs.config); + u32 pixelsize =3D (config & (1U << 16)) ? 2 : 4; unsigned int size =3D (vindev->timings.bt.width + vindev->padding) - * vindev->timings.bt.height * 4; + * vindev->timings.bt.height * pixelsize; =20 /* * If I/O reconfiguration is in process, do not allow to start @@ -220,9 +223,12 @@ static int buffer_init(struct vb2_buffer *vb) static int buffer_prepare(struct vb2_buffer *vb) { struct mgb4_vin_dev *vindev =3D vb2_get_drv_priv(vb->vb2_queue); + struct mgb4_regs *video =3D &vindev->mgbdev->video; struct device *dev =3D &vindev->mgbdev->pdev->dev; + u32 config =3D mgb4_read_reg(video, vindev->config->regs.config); + u32 pixelsize =3D (config & (1U << 16)) ? 2 : 4; unsigned int size =3D (vindev->timings.bt.width + vindev->padding) - * vindev->timings.bt.height * 4; + * vindev->timings.bt.height * pixelsize; =20 if (vb2_plane_size(vb, 0) < size) { dev_err(dev, "buffer too small (%lu < %u)\n", @@ -359,22 +365,30 @@ static int vidioc_querycap(struct file *file, void *p= riv, static int vidioc_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f) { - if (f->index !=3D 0) - return -EINVAL; - - f->pixelformat =3D V4L2_PIX_FMT_ABGR32; + struct mgb4_vin_dev *vindev =3D video_drvdata(file); + struct mgb4_regs *video =3D &vindev->mgbdev->video; =20 - return 0; + if (f->index =3D=3D 0) { + f->pixelformat =3D V4L2_PIX_FMT_ABGR32; + return 0; + } else if (f->index =3D=3D 1 && has_yuv(video)) { + f->pixelformat =3D V4L2_PIX_FMT_YUYV; + return 0; + } else { + return -EINVAL; + } } =20 static int vidioc_enum_frameintervals(struct file *file, void *priv, struct v4l2_frmivalenum *ival) { struct mgb4_vin_dev *vindev =3D video_drvdata(file); + struct mgb4_regs *video =3D &vindev->mgbdev->video; =20 if (ival->index !=3D 0) return -EINVAL; - if (ival->pixel_format !=3D V4L2_PIX_FMT_ABGR32) + if (!(ival->pixel_format =3D=3D V4L2_PIX_FMT_ABGR32 || + ((has_yuv(video) && ival->pixel_format =3D=3D V4L2_PIX_FMT_YUYV)))) return -EINVAL; if (ival->width !=3D vindev->timings.bt.width || ival->height !=3D vindev->timings.bt.height) @@ -393,13 +407,29 @@ static int vidioc_enum_frameintervals(struct file *fi= le, void *priv, static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format = *f) { struct mgb4_vin_dev *vindev =3D video_drvdata(file); + struct mgb4_regs *video =3D &vindev->mgbdev->video; + u32 config =3D mgb4_read_reg(video, vindev->config->regs.config); =20 - f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; f->fmt.pix.width =3D vindev->timings.bt.width; f->fmt.pix.height =3D vindev->timings.bt.height; f->fmt.pix.field =3D V4L2_FIELD_NONE; - f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; - f->fmt.pix.bytesperline =3D (f->fmt.pix.width + vindev->padding) * 4; + + if (config & (1U << 16)) { + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_YUYV; + if (config & (1U << 20)) { + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_REC709; + } else { + if (config & (1U << 19)) + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SMPTE170M; + else + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SRGB; + } + f->fmt.pix.bytesperline =3D (f->fmt.pix.width + vindev->padding) * 2; + } else { + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; + f->fmt.pix.bytesperline =3D (f->fmt.pix.width + vindev->padding) * 4; + } f->fmt.pix.sizeimage =3D f->fmt.pix.bytesperline * f->fmt.pix.height; =20 return 0; @@ -408,14 +438,30 @@ static int vidioc_g_fmt(struct file *file, void *priv= , struct v4l2_format *f) static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_forma= t *f) { struct mgb4_vin_dev *vindev =3D video_drvdata(file); + struct mgb4_regs *video =3D &vindev->mgbdev->video; + u32 pixelsize; =20 - f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; f->fmt.pix.width =3D vindev->timings.bt.width; f->fmt.pix.height =3D vindev->timings.bt.height; f->fmt.pix.field =3D V4L2_FIELD_NONE; - f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; - f->fmt.pix.bytesperline =3D max(f->fmt.pix.width * 4, - ALIGN_DOWN(f->fmt.pix.bytesperline, 4)); + + if (has_yuv(video) && f->fmt.pix.pixelformat =3D=3D V4L2_PIX_FMT_YUYV) { + pixelsize =3D 2; + if (!(f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_REC709 || + f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_SMPTE170M)) + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SRGB; + } else { + pixelsize =3D 4; + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; + } + + if (f->fmt.pix.bytesperline > f->fmt.pix.width * pixelsize && + f->fmt.pix.bytesperline < f->fmt.pix.width * pixelsize * 2) + f->fmt.pix.bytesperline =3D ALIGN(f->fmt.pix.bytesperline, + pixelsize); + else + f->fmt.pix.bytesperline =3D f->fmt.pix.width * pixelsize; f->fmt.pix.sizeimage =3D f->fmt.pix.bytesperline * f->fmt.pix.height; =20 return 0; @@ -425,13 +471,36 @@ static int vidioc_s_fmt(struct file *file, void *priv= , struct v4l2_format *f) { struct mgb4_vin_dev *vindev =3D video_drvdata(file); struct mgb4_regs *video =3D &vindev->mgbdev->video; + u32 config, pixelsize; =20 if (vb2_is_busy(&vindev->queue)) return -EBUSY; =20 vidioc_try_fmt(file, priv, f); =20 - vindev->padding =3D (f->fmt.pix.bytesperline - (f->fmt.pix.width * 4)) / = 4; + config =3D mgb4_read_reg(video, vindev->config->regs.config); + if (f->fmt.pix.pixelformat =3D=3D V4L2_PIX_FMT_YUYV) { + pixelsize =3D 2; + config |=3D 1U << 16; + + if (f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_REC709) { + config |=3D 1U << 20; + config |=3D 1U << 19; + } else if (f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_SMPTE170M) { + config &=3D ~(1U << 20); + config |=3D 1U << 19; + } else { + config &=3D ~(1U << 20); + config &=3D ~(1U << 19); + } + } else { + pixelsize =3D 4; + config &=3D ~(1U << 16); + } + mgb4_write_reg(video, vindev->config->regs.config, config); + + vindev->padding =3D (f->fmt.pix.bytesperline - (f->fmt.pix.width + * pixelsize)) / pixelsize; mgb4_write_reg(video, vindev->config->regs.padding, vindev->padding); set_loopback_padding(vindev, vindev->padding); =20 @@ -467,7 +536,8 @@ static int vidioc_enum_framesizes(struct file *file, vo= id *fh, { struct mgb4_vin_dev *vindev =3D video_drvdata(file); =20 - if (fsize->index !=3D 0 || fsize->pixel_format !=3D V4L2_PIX_FMT_ABGR32) + if (fsize->index !=3D 0 || !(fsize->pixel_format =3D=3D V4L2_PIX_FMT_ABGR= 32 || + fsize->pixel_format =3D=3D V4L2_PIX_FMT_YUYV)) return -EINVAL; =20 fsize->discrete.width =3D vindev->timings.bt.width; @@ -499,9 +569,6 @@ static int vidioc_parm(struct file *file, void *priv, .denominator =3D 125000000, }; =20 - if (parm->type !=3D V4L2_BUF_TYPE_VIDEO_CAPTURE) - return -EINVAL; - parm->parm.capture.readbuffers =3D 2; parm->parm.capture.capability =3D V4L2_CAP_TIMEPERFRAME; parm->parm.capture.timeperframe =3D timeperframe; diff --git a/drivers/media/pci/mgb4/mgb4_vout.c b/drivers/media/pci/mgb4/mg= b4_vout.c index 241353ee77a5..2ee9606d9072 100644 --- a/drivers/media/pci/mgb4/mgb4_vout.c +++ b/drivers/media/pci/mgb4/mgb4_vout.c @@ -59,7 +59,11 @@ static int queue_setup(struct vb2_queue *q, unsigned int= *nbuffers, struct device *alloc_devs[]) { struct mgb4_vout_dev *voutdev =3D vb2_get_drv_priv(q); - unsigned int size; + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + u32 config =3D mgb4_read_reg(video, voutdev->config->regs.config); + u32 pixelsize =3D (config & (1U << 16)) ? 2 : 4; + unsigned int size =3D (voutdev->width + voutdev->padding) * voutdev->heig= ht + * pixelsize; =20 /* * If I/O reconfiguration is in process, do not allow to start @@ -69,8 +73,6 @@ static int queue_setup(struct vb2_queue *q, unsigned int = *nbuffers, if (test_bit(0, &voutdev->mgbdev->io_reconfig)) return -EBUSY; =20 - size =3D (voutdev->width + voutdev->padding) * voutdev->height * 4; - if (*nplanes) return sizes[0] < size ? -EINVAL : 0; *nplanes =3D 1; @@ -93,9 +95,11 @@ static int buffer_prepare(struct vb2_buffer *vb) { struct mgb4_vout_dev *voutdev =3D vb2_get_drv_priv(vb->vb2_queue); struct device *dev =3D &voutdev->mgbdev->pdev->dev; - unsigned int size; - - size =3D (voutdev->width + voutdev->padding) * voutdev->height * 4; + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + u32 config =3D mgb4_read_reg(video, voutdev->config->regs.config); + u32 pixelsize =3D (config & (1U << 16)) ? 2 : 4; + unsigned int size =3D (voutdev->width + voutdev->padding) * voutdev->heig= ht + * pixelsize; =20 if (vb2_plane_size(vb, 0) < size) { dev_err(dev, "buffer too small (%lu < %u)\n", @@ -194,24 +198,47 @@ static int vidioc_querycap(struct file *file, void *p= riv, static int vidioc_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f) { - if (f->index !=3D 0) - return -EINVAL; - - f->pixelformat =3D V4L2_PIX_FMT_ABGR32; + struct mgb4_vin_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; =20 - return 0; + if (f->index =3D=3D 0) { + f->pixelformat =3D V4L2_PIX_FMT_ABGR32; + return 0; + } else if (f->index =3D=3D 1 && has_yuv(video)) { + f->pixelformat =3D V4L2_PIX_FMT_YUYV; + return 0; + } else { + return -EINVAL; + } } =20 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format = *f) { struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + u32 config =3D mgb4_read_reg(video, voutdev->config->regs.config); =20 - f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; f->fmt.pix.width =3D voutdev->width; f->fmt.pix.height =3D voutdev->height; f->fmt.pix.field =3D V4L2_FIELD_NONE; - f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; - f->fmt.pix.bytesperline =3D (f->fmt.pix.width + voutdev->padding) * 4; + + if (config & (1U << 16)) { + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_YUYV; + if (config & (1U << 20)) { + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_REC709; + } else { + if (config & (1U << 19)) + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SMPTE170M; + else + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SRGB; + } + f->fmt.pix.bytesperline =3D (f->fmt.pix.width + voutdev->padding) * 2; + } else { + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; + f->fmt.pix.bytesperline =3D (f->fmt.pix.width + voutdev->padding) * 4; + } + f->fmt.pix.sizeimage =3D f->fmt.pix.bytesperline * f->fmt.pix.height; =20 return 0; @@ -220,14 +247,30 @@ static int vidioc_g_fmt(struct file *file, void *priv= , struct v4l2_format *f) static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_forma= t *f) { struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + u32 pixelsize; =20 - f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; f->fmt.pix.width =3D voutdev->width; f->fmt.pix.height =3D voutdev->height; f->fmt.pix.field =3D V4L2_FIELD_NONE; - f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; - f->fmt.pix.bytesperline =3D max(f->fmt.pix.width * 4, - ALIGN_DOWN(f->fmt.pix.bytesperline, 4)); + + if (has_yuv(video) && f->fmt.pix.pixelformat =3D=3D V4L2_PIX_FMT_YUYV) { + pixelsize =3D 2; + if (!(f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_REC709 || + f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_SMPTE170M)) + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_SRGB; + } else { + pixelsize =3D 4; + f->fmt.pix.pixelformat =3D V4L2_PIX_FMT_ABGR32; + f->fmt.pix.colorspace =3D V4L2_COLORSPACE_RAW; + } + + if (f->fmt.pix.bytesperline > f->fmt.pix.width * pixelsize && + f->fmt.pix.bytesperline < f->fmt.pix.width * pixelsize * 2) + f->fmt.pix.bytesperline =3D ALIGN(f->fmt.pix.bytesperline, + pixelsize); + else + f->fmt.pix.bytesperline =3D f->fmt.pix.width * pixelsize; f->fmt.pix.sizeimage =3D f->fmt.pix.bytesperline * f->fmt.pix.height; =20 return 0; @@ -237,13 +280,39 @@ static int vidioc_s_fmt(struct file *file, void *priv= , struct v4l2_format *f) { struct mgb4_vout_dev *voutdev =3D video_drvdata(file); struct mgb4_regs *video =3D &voutdev->mgbdev->video; + u32 config, pixelsize; + int ret; =20 if (vb2_is_busy(&voutdev->queue)) return -EBUSY; =20 - vidioc_try_fmt(file, priv, f); + ret =3D vidioc_try_fmt(file, priv, f); + if (ret < 0) + return ret; + + config =3D mgb4_read_reg(video, voutdev->config->regs.config); + if (f->fmt.pix.pixelformat =3D=3D V4L2_PIX_FMT_YUYV) { + pixelsize =3D 2; + config |=3D 1U << 16; + + if (f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_REC709) { + config |=3D 1U << 20; + config |=3D 1U << 19; + } else if (f->fmt.pix.colorspace =3D=3D V4L2_COLORSPACE_SMPTE170M) { + config &=3D ~(1U << 20); + config |=3D 1U << 19; + } else { + config &=3D ~(1U << 20); + config &=3D ~(1U << 19); + } + } else { + pixelsize =3D 4; + config &=3D ~(1U << 16); + } + mgb4_write_reg(video, voutdev->config->regs.config, config); =20 - voutdev->padding =3D (f->fmt.pix.bytesperline - (f->fmt.pix.width * 4)) /= 4; + voutdev->padding =3D (f->fmt.pix.bytesperline - (f->fmt.pix.width + * pixelsize)) / pixelsize; mgb4_write_reg(video, voutdev->config->regs.padding, voutdev->padding); =20 return 0; --=20 2.45.1 From nobody Fri Dec 19 14:08:49 2025 Received: from mx.gpxsee.org (mx.gpxsee.org [37.205.14.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DDA45589B; Mon, 5 Aug 2024 15:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.14.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; cv=none; b=dWbmGk5SnaZ9FtM7b8O2JS/VJDwc9Y9etzfpQ5J/a+Chbz6MkCQ8SJyEet93OmxVcwwac8o9C2uM3j3laUPedXHfg1rCzMZ0pbIq92jtcAe3wSCP8CtRBu0h5XM2575kcr/XAV0H2UTVpj+Sk9Ya8tk6KXgqAsox+5K2MuLqa2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; c=relaxed/simple; bh=jeBJ1MHD6FKZant11EmycpvNvP6FvXl0TXaYZPAQBAA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AYoGWDmXTWJnIYG8yB43uDyxE09ovQhKut+swZzbFHnLqM0QaFMMsGurnXsSFAhao/P+q2MbrvScNbwBi9I3zWZilpOsqmaWsilxHp2KGlJLypVcV5qKmehrXDbmHfCOJ8SFI2W5oHlFFViFiMtVFNMBULhIv0Noj8xJfI8rGvY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org; spf=pass smtp.mailfrom=gpxsee.org; arc=none smtp.client-ip=37.205.14.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gpxsee.org Received: from mgb4.. (unknown [62.77.71.229]) by mx.gpxsee.org (Postfix) with ESMTPSA id 1E23B3CCDD; Mon, 05 Aug 2024 17:41:02 +0200 (CEST) From: tumic@gpxsee.org To: Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Martin=20T=C5=AFma?= Subject: [PATCH v6 2/4 RESEND] media: mgb4: Add support for V4L2_CAP_TIMEPERFRAME Date: Mon, 5 Aug 2024 17:40:52 +0200 Message-ID: <20240805154054.8633-3-tumic@gpxsee.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240805154054.8633-1-tumic@gpxsee.org> References: <20240805154054.8633-1-tumic@gpxsee.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Martin T=C5=AFma Recent mgb4 firmwares have support for setting a variable framerate indepen= dent of the signal framerate. Add/fix (the mgb4 driver already did promote V4L2_CAP_TIMEPERFRAME, but it didn't work) support for V4L2_CAP_TIMEPERFRAM= E to the driver to enable this feature. Additionally add support for the DV timings API (VIDIOC_G_DV_TIMINGS, VIDIOC_ENUM_DV_TIMINGS, ...) for the outputs that was missing. The timings = info is required/used for implementing the V4L2_CAP_TIMEPERFRAME functionality. Signed-off-by: Martin T=C5=AFma --- drivers/media/pci/mgb4/mgb4_core.c | 2 +- drivers/media/pci/mgb4/mgb4_core.h | 2 + drivers/media/pci/mgb4/mgb4_io.h | 24 ++- drivers/media/pci/mgb4/mgb4_sysfs_out.c | 4 +- drivers/media/pci/mgb4/mgb4_vin.c | 86 ++++++++--- drivers/media/pci/mgb4/mgb4_vin.h | 3 +- drivers/media/pci/mgb4/mgb4_vout.c | 190 +++++++++++++++++++++++- drivers/media/pci/mgb4/mgb4_vout.h | 3 +- 8 files changed, 277 insertions(+), 37 deletions(-) diff --git a/drivers/media/pci/mgb4/mgb4_core.c b/drivers/media/pci/mgb4/mg= b4_core.c index 60498a5abebf..0dd59770fe8a 100644 --- a/drivers/media/pci/mgb4/mgb4_core.c +++ b/drivers/media/pci/mgb4/mgb4_core.c @@ -302,7 +302,7 @@ static int init_i2c(struct mgb4_dev *mgbdev) /* create dummy clock required by the xiic-i2c adapter */ snprintf(clk_name, sizeof(clk_name), "xiic-i2c.%d", id); mgbdev->i2c_clk =3D clk_hw_register_fixed_rate(NULL, clk_name, NULL, - 0, 125000000); + 0, MGB4_HW_FREQ); if (IS_ERR(mgbdev->i2c_clk)) { dev_err(dev, "failed to register I2C clock\n"); return PTR_ERR(mgbdev->i2c_clk); diff --git a/drivers/media/pci/mgb4/mgb4_core.h b/drivers/media/pci/mgb4/mg= b4_core.h index 2a946e46aec1..b52cd67270b5 100644 --- a/drivers/media/pci/mgb4/mgb4_core.h +++ b/drivers/media/pci/mgb4/mgb4_core.h @@ -13,6 +13,8 @@ #include #include "mgb4_regs.h" =20 +#define MGB4_HW_FREQ 125000000 + #define MGB4_VIN_DEVICES 2 #define MGB4_VOUT_DEVICES 2 =20 diff --git a/drivers/media/pci/mgb4/mgb4_io.h b/drivers/media/pci/mgb4/mgb4= _io.h index 204613a6685c..dd8696d7df31 100644 --- a/drivers/media/pci/mgb4/mgb4_io.h +++ b/drivers/media/pci/mgb4/mgb4_io.h @@ -7,11 +7,9 @@ #ifndef __MGB4_IO_H__ #define __MGB4_IO_H__ =20 +#include #include - -#define MGB4_DEFAULT_WIDTH 1280 -#define MGB4_DEFAULT_HEIGHT 640 -#define MGB4_DEFAULT_PERIOD (125000000 / 60) +#include "mgb4_core.h" =20 /* Register access error indication */ #define MGB4_ERR_NO_REG 0xFFFFFFFE @@ -20,6 +18,9 @@ #define MGB4_ERR_QUEUE_EMPTY 0xFFFFFFFC #define MGB4_ERR_QUEUE_FULL 0xFFFFFFFB =20 +#define MGB4_PERIOD(numerator, denominator) \ + ((u32)div_u64((MGB4_HW_FREQ * (u64)(numerator)), (denominator))) + struct mgb4_frame_buffer { struct vb2_v4l2_buffer vb; struct list_head list; @@ -30,11 +31,24 @@ static inline struct mgb4_frame_buffer *to_frame_buffer= (struct vb2_v4l2_buffer * return container_of(vbuf, struct mgb4_frame_buffer, vb); } =20 -static inline bool has_yuv(struct mgb4_regs *video) +static inline bool has_yuv_and_timeperframe(struct mgb4_regs *video) { u32 status =3D mgb4_read_reg(video, 0xD0); =20 return (status & (1U << 8)); } =20 +#define has_yuv(video) has_yuv_and_timeperframe(video) +#define has_timeperframe(video) has_yuv_and_timeperframe(video) + +static inline u32 pixel_size(struct v4l2_dv_timings *timings) +{ + struct v4l2_bt_timings *bt =3D &timings->bt; + + u32 height =3D bt->height + bt->vfrontporch + bt->vsync + bt->vbackporch; + u32 width =3D bt->width + bt->hfrontporch + bt->hsync + bt->hbackporch; + + return width * height; +} + #endif diff --git a/drivers/media/pci/mgb4/mgb4_sysfs_out.c b/drivers/media/pci/mg= b4/mgb4_sysfs_out.c index 9f6e81c57726..f67ff2a48329 100644 --- a/drivers/media/pci/mgb4/mgb4_sysfs_out.c +++ b/drivers/media/pci/mgb4/mgb4_sysfs_out.c @@ -231,7 +231,7 @@ static ssize_t frame_rate_show(struct device *dev, u32 period =3D mgb4_read_reg(&voutdev->mgbdev->video, voutdev->config->regs.frame_period); =20 - return sprintf(buf, "%u\n", 125000000 / period); + return sprintf(buf, "%u\n", MGB4_HW_FREQ / period); } =20 /* @@ -252,7 +252,7 @@ static ssize_t frame_rate_store(struct device *dev, return ret; =20 mgb4_write_reg(&voutdev->mgbdev->video, - voutdev->config->regs.frame_period, 125000000 / val); + voutdev->config->regs.frame_period, MGB4_HW_FREQ / val); =20 return count; } diff --git a/drivers/media/pci/mgb4/mgb4_vin.c b/drivers/media/pci/mgb4/mgb= 4_vin.c index 7fb14e867e8d..e9332abb3172 100644 --- a/drivers/media/pci/mgb4/mgb4_vin.c +++ b/drivers/media/pci/mgb4/mgb4_vin.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -34,8 +35,8 @@ ATTRIBUTE_GROUPS(mgb4_fpdl3_in); ATTRIBUTE_GROUPS(mgb4_gmsl_in); =20 static const struct mgb4_vin_config vin_cfg[] =3D { - {0, 0, 0, 6, {0x10, 0x00, 0x04, 0x08, 0x1C, 0x14, 0x18, 0x20, 0x24, 0x28}= }, - {1, 1, 1, 7, {0x40, 0x30, 0x34, 0x38, 0x4C, 0x44, 0x48, 0x50, 0x54, 0x58}} + {0, 0, 0, 6, {0x10, 0x00, 0x04, 0x08, 0x1C, 0x14, 0x18, 0x20, 0x24, 0x28,= 0xE8}}, + {1, 1, 1, 7, {0x40, 0x30, 0x34, 0x38, 0x4C, 0x44, 0x48, 0x50, 0x54, 0x58,= 0xEC}} }; =20 static const struct i2c_board_info fpdl3_deser_info[] =3D { @@ -76,6 +77,9 @@ static const struct v4l2_dv_timings_cap video_timings_cap= =3D { }, }; =20 +/* Dummy timings when no signal present */ +static const struct v4l2_dv_timings cea1080p60 =3D V4L2_DV_BT_CEA_1920X108= 0P60; + /* * Returns the video output connected with the given video input if the in= put * is in loopback mode. @@ -318,7 +322,8 @@ static int fh_open(struct file *file) if (!v4l2_fh_is_singular_file(file)) goto out; =20 - get_timings(vindev, &vindev->timings); + if (get_timings(vindev, &vindev->timings) < 0) + vindev->timings =3D cea1080p60; set_loopback_padding(vindev, vindev->padding); =20 out: @@ -394,12 +399,13 @@ static int vidioc_enum_frameintervals(struct file *fi= le, void *priv, ival->height !=3D vindev->timings.bt.height) return -EINVAL; =20 - ival->type =3D V4L2_FRMIVAL_TYPE_CONTINUOUS; - ival->stepwise.min.denominator =3D 60; - ival->stepwise.min.numerator =3D 1; - ival->stepwise.max.denominator =3D 1; - ival->stepwise.max.numerator =3D 1; - ival->stepwise.step =3D ival->stepwise.max; + ival->type =3D V4L2_FRMIVAL_TYPE_STEPWISE; + ival->stepwise.max.denominator =3D MGB4_HW_FREQ; + ival->stepwise.max.numerator =3D 0xFFFFFFFF; + ival->stepwise.min.denominator =3D vindev->timings.bt.pixelclock; + ival->stepwise.min.numerator =3D pixel_size(&vindev->timings); + ival->stepwise.step.denominator =3D MGB4_HW_FREQ; + ival->stepwise.step.numerator =3D 1; =20 return 0; } @@ -558,24 +564,56 @@ static int vidioc_g_input(struct file *file, void *pr= iv, unsigned int *i) return 0; } =20 -static int vidioc_parm(struct file *file, void *priv, - struct v4l2_streamparm *parm) +static int vidioc_g_parm(struct file *file, void *priv, + struct v4l2_streamparm *parm) { struct mgb4_vin_dev *vindev =3D video_drvdata(file); struct mgb4_regs *video =3D &vindev->mgbdev->video; - const struct mgb4_vin_regs *regs =3D &vindev->config->regs; - struct v4l2_fract timeperframe =3D { - .numerator =3D mgb4_read_reg(video, regs->frame_period), - .denominator =3D 125000000, - }; + struct v4l2_fract *tpf =3D &parm->parm.output.timeperframe; + u32 timer; =20 parm->parm.capture.readbuffers =3D 2; - parm->parm.capture.capability =3D V4L2_CAP_TIMEPERFRAME; - parm->parm.capture.timeperframe =3D timeperframe; + + if (has_timeperframe(video)) { + timer =3D mgb4_read_reg(video, vindev->config->regs.timer); + if (timer < 0xFFFF) { + tpf->numerator =3D pixel_size(&vindev->timings); + tpf->denominator =3D vindev->timings.bt.pixelclock; + } else { + tpf->numerator =3D timer; + tpf->denominator =3D MGB4_HW_FREQ; + } + + parm->parm.output.capability =3D V4L2_CAP_TIMEPERFRAME; + } =20 return 0; } =20 +static int vidioc_s_parm(struct file *file, void *priv, + struct v4l2_streamparm *parm) +{ + struct mgb4_vin_dev *vindev =3D video_drvdata(file); + struct mgb4_regs *video =3D &vindev->mgbdev->video; + struct v4l2_fract *tpf =3D &parm->parm.output.timeperframe; + u32 period, timer; + + if (has_timeperframe(video)) { + timer =3D tpf->denominator ? + MGB4_PERIOD(tpf->numerator, tpf->denominator) : 0; + if (timer) { + period =3D MGB4_PERIOD(pixel_size(&vindev->timings), + vindev->timings.bt.pixelclock); + if (timer < period) + timer =3D 0; + } + + mgb4_write_reg(video, vindev->config->regs.timer, timer); + } + + return vidioc_g_parm(file, priv, parm); +} + static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings) { @@ -659,8 +697,8 @@ static const struct v4l2_ioctl_ops video_ioctl_ops =3D { .vidioc_expbuf =3D vb2_ioctl_expbuf, .vidioc_streamon =3D vb2_ioctl_streamon, .vidioc_streamoff =3D vb2_ioctl_streamoff, - .vidioc_g_parm =3D vidioc_parm, - .vidioc_s_parm =3D vidioc_parm, + .vidioc_g_parm =3D vidioc_g_parm, + .vidioc_s_parm =3D vidioc_s_parm, .vidioc_dv_timings_cap =3D vidioc_dv_timings_cap, .vidioc_enum_dv_timings =3D vidioc_enum_dv_timings, .vidioc_g_dv_timings =3D vidioc_g_dv_timings, @@ -843,10 +881,16 @@ static void debugfs_init(struct mgb4_vin_dev *vindev) vindev->regs[7].offset =3D vindev->config->regs.signal2; vindev->regs[8].name =3D "PADDING_PIXELS"; vindev->regs[8].offset =3D vindev->config->regs.padding; + if (has_timeperframe(video)) { + vindev->regs[9].name =3D "TIMER"; + vindev->regs[9].offset =3D vindev->config->regs.timer; + vindev->regset.nregs =3D 10; + } else { + vindev->regset.nregs =3D 9; + } =20 vindev->regset.base =3D video->membase; vindev->regset.regs =3D vindev->regs; - vindev->regset.nregs =3D ARRAY_SIZE(vindev->regs); =20 debugfs_create_regset32("registers", 0444, vindev->debugfs, &vindev->regset); diff --git a/drivers/media/pci/mgb4/mgb4_vin.h b/drivers/media/pci/mgb4/mgb= 4_vin.h index 0249b400ad4d..9693bd0ce180 100644 --- a/drivers/media/pci/mgb4/mgb4_vin.h +++ b/drivers/media/pci/mgb4/mgb4_vin.h @@ -25,6 +25,7 @@ struct mgb4_vin_regs { u32 signal; u32 signal2; u32 padding; + u32 timer; }; =20 struct mgb4_vin_config { @@ -59,7 +60,7 @@ struct mgb4_vin_dev { #ifdef CONFIG_DEBUG_FS struct dentry *debugfs; struct debugfs_regset32 regset; - struct debugfs_reg32 regs[9]; + struct debugfs_reg32 regs[sizeof(struct mgb4_vin_regs) / 4]; #endif }; =20 diff --git a/drivers/media/pci/mgb4/mgb4_vout.c b/drivers/media/pci/mgb4/mg= b4_vout.c index 2ee9606d9072..d98b6e87d71f 100644 --- a/drivers/media/pci/mgb4/mgb4_vout.c +++ b/drivers/media/pci/mgb4/mgb4_vout.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "mgb4_core.h" #include "mgb4_dma.h" #include "mgb4_sysfs.h" @@ -23,12 +24,16 @@ #include "mgb4_cmt.h" #include "mgb4_vout.h" =20 +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 640 +#define DEFAULT_PERIOD (MGB4_HW_FREQ / 60) + ATTRIBUTE_GROUPS(mgb4_fpdl3_out); ATTRIBUTE_GROUPS(mgb4_gmsl_out); =20 static const struct mgb4_vout_config vout_cfg[] =3D { - {0, 0, 8, {0x78, 0x60, 0x64, 0x68, 0x74, 0x6C, 0x70, 0x7c}}, - {1, 1, 9, {0x98, 0x80, 0x84, 0x88, 0x94, 0x8c, 0x90, 0x9c}} + {0, 0, 8, {0x78, 0x60, 0x64, 0x68, 0x74, 0x6C, 0x70, 0x7C, 0xE0}}, + {1, 1, 9, {0x98, 0x80, 0x84, 0x88, 0x94, 0x8C, 0x90, 0x9C, 0xE4}} }; =20 static const struct i2c_board_info fpdl3_ser_info[] =3D { @@ -40,6 +45,49 @@ static const struct mgb4_i2c_kv fpdl3_i2c[] =3D { {0x05, 0xFF, 0x04}, {0x06, 0xFF, 0x01}, {0xC2, 0xFF, 0x80} }; =20 +static const struct v4l2_dv_timings_cap video_timings_cap =3D { + .type =3D V4L2_DV_BT_656_1120, + .bt =3D { + .min_width =3D 320, + .max_width =3D 4096, + .min_height =3D 240, + .max_height =3D 2160, + .min_pixelclock =3D 1843200, /* 320 x 240 x 24Hz */ + .max_pixelclock =3D 530841600, /* 4096 x 2160 x 60Hz */ + .standards =3D V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT | + V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF, + .capabilities =3D V4L2_DV_BT_CAP_PROGRESSIVE | + V4L2_DV_BT_CAP_CUSTOM, + }, +}; + +static void get_timings(struct mgb4_vout_dev *voutdev, + struct v4l2_dv_timings *timings) +{ + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + const struct mgb4_vout_regs *regs =3D &voutdev->config->regs; + + u32 hsync =3D mgb4_read_reg(video, regs->hsync); + u32 vsync =3D mgb4_read_reg(video, regs->vsync); + u32 resolution =3D mgb4_read_reg(video, regs->resolution); + + memset(timings, 0, sizeof(*timings)); + timings->type =3D V4L2_DV_BT_656_1120; + timings->bt.width =3D resolution >> 16; + timings->bt.height =3D resolution & 0xFFFF; + if (hsync & (1U << 31)) + timings->bt.polarities |=3D V4L2_DV_HSYNC_POS_POL; + if (vsync & (1U << 31)) + timings->bt.polarities |=3D V4L2_DV_VSYNC_POS_POL; + timings->bt.pixelclock =3D voutdev->freq * 1000; + timings->bt.hsync =3D (hsync & 0x00FF0000) >> 16; + timings->bt.vsync =3D (vsync & 0x00FF0000) >> 16; + timings->bt.hbackporch =3D (hsync & 0x0000FF00) >> 8; + timings->bt.hfrontporch =3D hsync & 0x000000FF; + timings->bt.vbackporch =3D (vsync & 0x0000FF00) >> 8; + timings->bt.vfrontporch =3D vsync & 0x000000FF; +} + static void return_all_buffers(struct mgb4_vout_dev *voutdev, enum vb2_buffer_state state) { @@ -336,11 +384,128 @@ static int vidioc_enum_output(struct file *file, voi= d *priv, return -EINVAL; =20 out->type =3D V4L2_OUTPUT_TYPE_ANALOG; + out->capabilities =3D V4L2_OUT_CAP_DV_TIMINGS; strscpy(out->name, "MGB4", sizeof(out->name)); =20 return 0; } =20 +static int vidioc_enum_frameintervals(struct file *file, void *priv, + struct v4l2_frmivalenum *ival) +{ + struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + struct v4l2_dv_timings timings; + + if (ival->index !=3D 0) + return -EINVAL; + if (!(ival->pixel_format =3D=3D V4L2_PIX_FMT_ABGR32 || + ((has_yuv(video) && ival->pixel_format =3D=3D V4L2_PIX_FMT_YUYV)))) + return -EINVAL; + if (ival->width !=3D voutdev->width || ival->height !=3D voutdev->height) + return -EINVAL; + + get_timings(voutdev, &timings); + + ival->type =3D V4L2_FRMIVAL_TYPE_STEPWISE; + ival->stepwise.max.denominator =3D MGB4_HW_FREQ; + ival->stepwise.max.numerator =3D 0xFFFFFFFF; + ival->stepwise.min.denominator =3D timings.bt.pixelclock; + ival->stepwise.min.numerator =3D pixel_size(&timings); + ival->stepwise.step.denominator =3D MGB4_HW_FREQ; + ival->stepwise.step.numerator =3D 1; + + return 0; +} + +static int vidioc_g_parm(struct file *file, void *priv, + struct v4l2_streamparm *parm) +{ + struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + struct v4l2_fract *tpf =3D &parm->parm.output.timeperframe; + struct v4l2_dv_timings timings; + u32 timer; + + parm->parm.output.writebuffers =3D 2; + + if (has_timeperframe(video)) { + timer =3D mgb4_read_reg(video, voutdev->config->regs.timer); + if (timer < 0xFFFF) { + get_timings(voutdev, &timings); + tpf->numerator =3D pixel_size(&timings); + tpf->denominator =3D timings.bt.pixelclock; + } else { + tpf->numerator =3D timer; + tpf->denominator =3D MGB4_HW_FREQ; + } + + parm->parm.output.capability =3D V4L2_CAP_TIMEPERFRAME; + } + + return 0; +} + +static int vidioc_s_parm(struct file *file, void *priv, + struct v4l2_streamparm *parm) +{ + struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + struct mgb4_regs *video =3D &voutdev->mgbdev->video; + struct v4l2_fract *tpf =3D &parm->parm.output.timeperframe; + struct v4l2_dv_timings timings; + u32 timer, period; + + if (has_timeperframe(video)) { + timer =3D tpf->denominator ? + MGB4_PERIOD(tpf->numerator, tpf->denominator) : 0; + if (timer) { + get_timings(voutdev, &timings); + period =3D MGB4_PERIOD(pixel_size(&timings), + timings.bt.pixelclock); + if (timer < period) + timer =3D 0; + } + + mgb4_write_reg(video, voutdev->config->regs.timer, timer); + } + + return vidioc_g_parm(file, priv, parm); +} + +static int vidioc_g_dv_timings(struct file *file, void *fh, + struct v4l2_dv_timings *timings) +{ + struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + + get_timings(voutdev, timings); + + return 0; +} + +static int vidioc_s_dv_timings(struct file *file, void *fh, + struct v4l2_dv_timings *timings) +{ + struct mgb4_vout_dev *voutdev =3D video_drvdata(file); + + get_timings(voutdev, timings); + + return 0; +} + +static int vidioc_enum_dv_timings(struct file *file, void *fh, + struct v4l2_enum_dv_timings *timings) +{ + return v4l2_enum_dv_timings_cap(timings, &video_timings_cap, NULL, NULL); +} + +static int vidioc_dv_timings_cap(struct file *file, void *fh, + struct v4l2_dv_timings_cap *cap) +{ + *cap =3D video_timings_cap; + + return 0; +} + static const struct v4l2_ioctl_ops video_ioctl_ops =3D { .vidioc_querycap =3D vidioc_querycap, .vidioc_enum_fmt_vid_out =3D vidioc_enum_fmt, @@ -348,8 +513,15 @@ static const struct v4l2_ioctl_ops video_ioctl_ops =3D= { .vidioc_s_fmt_vid_out =3D vidioc_s_fmt, .vidioc_g_fmt_vid_out =3D vidioc_g_fmt, .vidioc_enum_output =3D vidioc_enum_output, + .vidioc_enum_frameintervals =3D vidioc_enum_frameintervals, .vidioc_g_output =3D vidioc_g_output, .vidioc_s_output =3D vidioc_s_output, + .vidioc_g_parm =3D vidioc_g_parm, + .vidioc_s_parm =3D vidioc_s_parm, + .vidioc_dv_timings_cap =3D vidioc_dv_timings_cap, + .vidioc_enum_dv_timings =3D vidioc_enum_dv_timings, + .vidioc_g_dv_timings =3D vidioc_g_dv_timings, + .vidioc_s_dv_timings =3D vidioc_s_dv_timings, .vidioc_reqbufs =3D vb2_ioctl_reqbufs, .vidioc_create_bufs =3D vb2_ioctl_create_bufs, .vidioc_prepare_buf =3D vb2_ioctl_prepare_buf, @@ -492,10 +664,10 @@ static void fpga_init(struct mgb4_vout_dev *voutdev) =20 mgb4_write_reg(video, regs->config, 0x00000011); mgb4_write_reg(video, regs->resolution, - (MGB4_DEFAULT_WIDTH << 16) | MGB4_DEFAULT_HEIGHT); + (DEFAULT_WIDTH << 16) | DEFAULT_HEIGHT); mgb4_write_reg(video, regs->hsync, 0x00102020); mgb4_write_reg(video, regs->vsync, 0x40020202); - mgb4_write_reg(video, regs->frame_period, MGB4_DEFAULT_PERIOD); + mgb4_write_reg(video, regs->frame_period, DEFAULT_PERIOD); mgb4_write_reg(video, regs->padding, 0x00000000); =20 voutdev->freq =3D mgb4_cmt_set_vout_freq(voutdev, 70000 >> 1) << 1; @@ -526,12 +698,18 @@ static void debugfs_init(struct mgb4_vout_dev *voutde= v) voutdev->regs[4].offset =3D voutdev->config->regs.vsync; voutdev->regs[5].name =3D "FRAME_PERIOD"; voutdev->regs[5].offset =3D voutdev->config->regs.frame_period; - voutdev->regs[6].name =3D "PADDING"; + voutdev->regs[6].name =3D "PADDING_PIXELS"; voutdev->regs[6].offset =3D voutdev->config->regs.padding; + if (has_timeperframe(video)) { + voutdev->regs[7].name =3D "TIMER"; + voutdev->regs[7].offset =3D voutdev->config->regs.timer; + voutdev->regset.nregs =3D 8; + } else { + voutdev->regset.nregs =3D 7; + } =20 voutdev->regset.base =3D video->membase; voutdev->regset.regs =3D voutdev->regs; - voutdev->regset.nregs =3D ARRAY_SIZE(voutdev->regs); =20 debugfs_create_regset32("registers", 0444, voutdev->debugfs, &voutdev->regset); diff --git a/drivers/media/pci/mgb4/mgb4_vout.h b/drivers/media/pci/mgb4/mg= b4_vout.h index b163dee711fd..ab9b58b1deb7 100644 --- a/drivers/media/pci/mgb4/mgb4_vout.h +++ b/drivers/media/pci/mgb4/mgb4_vout.h @@ -23,6 +23,7 @@ struct mgb4_vout_regs { u32 hsync; u32 vsync; u32 padding; + u32 timer; }; =20 struct mgb4_vout_config { @@ -55,7 +56,7 @@ struct mgb4_vout_dev { #ifdef CONFIG_DEBUG_FS struct dentry *debugfs; struct debugfs_regset32 regset; - struct debugfs_reg32 regs[7]; + struct debugfs_reg32 regs[sizeof(struct mgb4_vout_regs) / 4]; #endif }; =20 --=20 2.45.1 From nobody Fri Dec 19 14:08:49 2025 Received: from mx.gpxsee.org (mx.gpxsee.org [37.205.14.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A076C14901F; Mon, 5 Aug 2024 15:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.14.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; cv=none; b=s4DBJvUWDt8AEKNT+8j83bPc/LSx2wGppSaXZgLNIJDYe9RrjaGy3bLUVZAyv2lpwLdz0uTfHG0+8AtgLw82jwUH3O9nwNOv4HzkY9JFWwNjzKaQ5SnMe5U5myPJo6ypmmJ0ohKRrdmhyZaAdxullyQv7rSmPCFGaewvLJQL+xA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; c=relaxed/simple; bh=WDqR7FUN5MYbqjlweAuWRbTA/INUGEQ4XZouK1umPMA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TMV1JQnqywz3q1797Q1f77RybfJD7YosX7zPYk3X9Mbiob6MwkuwZOmeJyHqym0HJ73A4KV3YG/+7+MbM+QigyQ+ZRHtmdtsoCDgJ1pv2WUGX8VYmOsKWjiEo/k8KTF68RtJah5R6pN8Jw3NokTWynxujnekc5hO9VD3cekRT64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org; spf=pass smtp.mailfrom=gpxsee.org; arc=none smtp.client-ip=37.205.14.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gpxsee.org Received: from mgb4.. (unknown [62.77.71.229]) by mx.gpxsee.org (Postfix) with ESMTPSA id 262445EF05; Mon, 05 Aug 2024 17:41:02 +0200 (CEST) From: tumic@gpxsee.org To: Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Martin=20T=C5=AFma?= Subject: [PATCH v6 3/4 RESEND] media: mgb4: Fixed signal frame rate limit handling Date: Mon, 5 Aug 2024 17:40:53 +0200 Message-ID: <20240805154054.8633-4-tumic@gpxsee.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240805154054.8633-1-tumic@gpxsee.org> References: <20240805154054.8633-1-tumic@gpxsee.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Martin T=C5=AFma Change the default DV timings for the outputs to produce a better signal le= ss "crippled" by the frame rate limiting. While the individual values are now different, the resulting signal still matches the same default display as b= efore. Additionaly fix the corner case when the frame rate limit is set to zero ca= using a "divide by zero" kernel panic. Signed-off-by: Martin T=C5=AFma --- drivers/media/pci/mgb4/mgb4_sysfs_out.c | 9 +++++---- drivers/media/pci/mgb4/mgb4_vout.c | 12 ++++++------ drivers/media/pci/mgb4/mgb4_vout.h | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/drivers/media/pci/mgb4/mgb4_sysfs_out.c b/drivers/media/pci/mg= b4/mgb4_sysfs_out.c index f67ff2a48329..573aa61c69d4 100644 --- a/drivers/media/pci/mgb4/mgb4_sysfs_out.c +++ b/drivers/media/pci/mgb4/mgb4_sysfs_out.c @@ -229,9 +229,9 @@ static ssize_t frame_rate_show(struct device *dev, struct video_device *vdev =3D to_video_device(dev); struct mgb4_vout_dev *voutdev =3D video_get_drvdata(vdev); u32 period =3D mgb4_read_reg(&voutdev->mgbdev->video, - voutdev->config->regs.frame_period); + voutdev->config->regs.frame_limit); =20 - return sprintf(buf, "%u\n", MGB4_HW_FREQ / period); + return sprintf(buf, "%u\n", period ? MGB4_HW_FREQ / period : 0); } =20 /* @@ -245,14 +245,15 @@ static ssize_t frame_rate_store(struct device *dev, struct video_device *vdev =3D to_video_device(dev); struct mgb4_vout_dev *voutdev =3D video_get_drvdata(vdev); unsigned long val; - int ret; + int limit, ret; =20 ret =3D kstrtoul(buf, 10, &val); if (ret) return ret; =20 + limit =3D val ? MGB4_HW_FREQ / val : 0; mgb4_write_reg(&voutdev->mgbdev->video, - voutdev->config->regs.frame_period, MGB4_HW_FREQ / val); + voutdev->config->regs.frame_limit, limit); =20 return count; } diff --git a/drivers/media/pci/mgb4/mgb4_vout.c b/drivers/media/pci/mgb4/mg= b4_vout.c index d98b6e87d71f..998edcbd9723 100644 --- a/drivers/media/pci/mgb4/mgb4_vout.c +++ b/drivers/media/pci/mgb4/mgb4_vout.c @@ -665,12 +665,12 @@ static void fpga_init(struct mgb4_vout_dev *voutdev) mgb4_write_reg(video, regs->config, 0x00000011); mgb4_write_reg(video, regs->resolution, (DEFAULT_WIDTH << 16) | DEFAULT_HEIGHT); - mgb4_write_reg(video, regs->hsync, 0x00102020); - mgb4_write_reg(video, regs->vsync, 0x40020202); - mgb4_write_reg(video, regs->frame_period, DEFAULT_PERIOD); + mgb4_write_reg(video, regs->hsync, 0x00283232); + mgb4_write_reg(video, regs->vsync, 0x40141F1E); + mgb4_write_reg(video, regs->frame_limit, DEFAULT_PERIOD); mgb4_write_reg(video, regs->padding, 0x00000000); =20 - voutdev->freq =3D mgb4_cmt_set_vout_freq(voutdev, 70000 >> 1) << 1; + voutdev->freq =3D mgb4_cmt_set_vout_freq(voutdev, 61150 >> 1) << 1; =20 mgb4_write_reg(video, regs->config, (voutdev->config->id + MGB4_VIN_DEVICES) << 2 | 1 << 4); @@ -696,8 +696,8 @@ static void debugfs_init(struct mgb4_vout_dev *voutdev) voutdev->regs[3].offset =3D voutdev->config->regs.hsync; voutdev->regs[4].name =3D "VIDEO_PARAMS_2"; voutdev->regs[4].offset =3D voutdev->config->regs.vsync; - voutdev->regs[5].name =3D "FRAME_PERIOD"; - voutdev->regs[5].offset =3D voutdev->config->regs.frame_period; + voutdev->regs[5].name =3D "FRAME_LIMIT"; + voutdev->regs[5].offset =3D voutdev->config->regs.frame_limit; voutdev->regs[6].name =3D "PADDING_PIXELS"; voutdev->regs[6].offset =3D voutdev->config->regs.padding; if (has_timeperframe(video)) { diff --git a/drivers/media/pci/mgb4/mgb4_vout.h b/drivers/media/pci/mgb4/mg= b4_vout.h index ab9b58b1deb7..adc8fe1e7ae6 100644 --- a/drivers/media/pci/mgb4/mgb4_vout.h +++ b/drivers/media/pci/mgb4/mgb4_vout.h @@ -19,7 +19,7 @@ struct mgb4_vout_regs { u32 config; u32 status; u32 resolution; - u32 frame_period; + u32 frame_limit; u32 hsync; u32 vsync; u32 padding; --=20 2.45.1 From nobody Fri Dec 19 14:08:49 2025 Received: from mx.gpxsee.org (mx.gpxsee.org [37.205.14.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97D544CDE0; Mon, 5 Aug 2024 15:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.14.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; cv=none; b=eAcGP9nOgMCsbXxPbqgsse92We94rwLEpHPA0cmX1hd6COm9aKSff6lwPe8n36962rZ/X3doMvaK9MeAtDo6lRq2WbVetBsA4TgjIofuTDeii/IsXW6PjPyTnpM9kGHM4AsdGKCwxNCb8mX7lANob10hL7cBwzmQPhG9ezC3Eo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722872794; c=relaxed/simple; bh=obniZfSDW/k/GqDjUVIYlNk7TKGfBf5TeVHiB4wZLko=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=f0OR76nogWKolK5kgJpdd1S50FHdBz4/9x1OJqOthQWzo1sPxCU8dYRuR40TTP8VjAukI7h8l0vuV8AlgpyUhQqubQIP/9CHSp6rqh5paQdApP2qkux7ugan1wJXy3V1+ZDdvVwTQwQEZ5K/VrV9eQoyvmH2647ot5Ce+NjjNWE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org; spf=pass smtp.mailfrom=gpxsee.org; arc=none smtp.client-ip=37.205.14.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gpxsee.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gpxsee.org Received: from mgb4.. (unknown [62.77.71.229]) by mx.gpxsee.org (Postfix) with ESMTPSA id 35C753FFBD; Mon, 05 Aug 2024 17:41:02 +0200 (CEST) From: tumic@gpxsee.org To: Mauro Carvalho Chehab , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Martin=20T=C5=AFma?= Subject: [PATCH v6 4/4 RESEND] media: admin-guide: mgb4: Outputs DV timings documentation update Date: Mon, 5 Aug 2024 17:40:54 +0200 Message-ID: <20240805154054.8633-5-tumic@gpxsee.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240805154054.8633-1-tumic@gpxsee.org> References: <20240805154054.8633-1-tumic@gpxsee.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Martin T=C5=AFma Properly document the function of the mgb4 output "frame_rate" sysfs parame= ter and update the default DV timings values according to the latest code chang= es. Signed-off-by: Martin T=C5=AFma --- Documentation/admin-guide/media/mgb4.rst | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/media/mgb4.rst b/Documentation/admin= -guide/media/mgb4.rst index e434d4a9eeb3..b9da127c074d 100644 --- a/Documentation/admin-guide/media/mgb4.rst +++ b/Documentation/admin-guide/media/mgb4.rst @@ -227,8 +227,13 @@ Common FPDL3/GMSL output parameters open.* =20 **frame_rate** (RW): - Output video frame rate in frames per second. The default frame rate is - 60Hz. + Output video signal frame rate limit in frames per second. Due to + the limited output pixel clock steps, the card can not always generate + a frame rate perfectly matching the value required by the connected di= splay. + Using this parameter one can limit the frame rate by "crippling" the s= ignal + so that the lines are not equal (the porches of the last line differ) = but + the signal appears like having the exact frame rate to the connected d= isplay. + The default frame rate limit is 60Hz. =20 **hsync_polarity** (RW): HSYNC signal polarity. @@ -253,33 +258,33 @@ Common FPDL3/GMSL output parameters and there is a non-linear stepping between two consecutive allowed frequencies. The driver finds the nearest allowed frequency to the giv= en value and sets it. When reading this property, you get the exact - frequency set by the driver. The default frequency is 70000kHz. + frequency set by the driver. The default frequency is 61150kHz. =20 *Note: This parameter can not be changed while the output v4l2 device = is open.* =20 **hsync_width** (RW): - Width of the HSYNC signal in pixels. The default value is 16. + Width of the HSYNC signal in pixels. The default value is 40. =20 **vsync_width** (RW): - Width of the VSYNC signal in video lines. The default value is 2. + Width of the VSYNC signal in video lines. The default value is 20. =20 **hback_porch** (RW): Number of PCLK pulses between deassertion of the HSYNC signal and the = first - valid pixel in the video line (marked by DE=3D1). The default value is= 32. + valid pixel in the video line (marked by DE=3D1). The default value is= 50. =20 **hfront_porch** (RW): Number of PCLK pulses between the end of the last valid pixel in the v= ideo line (marked by DE=3D1) and assertion of the HSYNC signal. The default= value - is 32. + is 50. =20 **vback_porch** (RW): Number of video lines between deassertion of the VSYNC signal and the = video - line with the first valid pixel (marked by DE=3D1). The default value = is 2. + line with the first valid pixel (marked by DE=3D1). The default value = is 31. =20 **vfront_porch** (RW): Number of video lines between the end of the last valid pixel line (ma= rked - by DE=3D1) and assertion of the VSYNC signal. The default value is 2. + by DE=3D1) and assertion of the VSYNC signal. The default value is 30. =20 FPDL3 specific input parameters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --=20 2.45.1