Re: [PATCH v7 2/4] media: rockchip: Add JPEG decoder driver
From: Sascha Hauer
Date: Mon Nov 23 2026 - 09:38:04 EST
On 2026-11-20 15:58, Philipp Zabel wrote:
> > +
> > +/* Error status bits used to decide whether a hardware reset is needed */
> > +#define VDPU720_ERR_MASK (VDPU720_BUS_ERR | VDPU720_DEC_ERR | \
> > + VDPU720_TIMEOUT | VDPU720_BUF_EMPTY)
> > +
> > +/*
> > + * VPU720 IRQ clear mask.
> > + *
> > + * Some revisions require a two-step IRQ acknowledgment before checking
> > + * VDPU720_IRQ_RAW: write the status back with only the enable and control
> > + * bits kept, then write 0 to fully clear. The downstream BSP computes the
> > + * first value as
> > + *
> > + * clr = (~(0x00fe7f40 & status)) & (0xff0180bf & status)
> > + *
> > + * The two masks are complements, so that is status & 0xff0180bf: bits 0-5,
> > + * 7, 15, 16 and 24-31 are kept, IRQ_RAW and the status bits 8-14 dropped.
> > + *
> > + * It is applied unconditionally here. The BSP gates it on the revision in
> > + * VDPU720_REG_VERSION matching 0xdb1f0006 exactly, and the RK3588 reports
> > + * 0xdb1f0005, so it is not needed on that part. The extra write is
> > + * harmless there and saves a revision check that would need silicon we do
> > + * not have to validate.
> > + */
> > +#define VDPU720_IRQ_CLR_KEEP 0xff0180bf
> > +
> > +#define VDPU720_REG_SYS 0x008
> > +#define VDPU720_FORCE_SOFTRST BIT(17) /* set when dec_e=0 before soft-reset */
> > +#define VDPU720_FILL_DOWN_E BIT(24) /* fill bottom padding rows */
> > +#define VDPU720_FILL_RIGHT_E BIT(25)
> > +#define VDPU720_OUT_SEQ BIT(26) /* 0=raster, 1=tile */
> > +#define VDPU720_YUV_OUT_FMT GENMASK(29, 27)
> > +#define VDPU720_YUV_OUT_FMT_NATIVE 0 /* no format conversion */
>
> This is never used.
It will be in v8 due to adding NV16 support ;)
>
> > +#define VDPU720_YUV_OUT_FMT_NV12 3 /* output as NV12 */
> > +
> > +#define VDPU720_REG_PIC_SIZE 0x00c
> > +#define VDPU720_PIC_W_M1 GENMASK(15, 0)
> > +#define VDPU720_PIC_H_M1 GENMASK(31, 16)
> > +
> > +#define VDPU720_REG_PIC_FMT 0x010
> > +#define VDPU720_JPEG_MODE GENMASK(2, 0)
> > +#define VDPU720_JPEG_MODE_YUV400 0
> > +#define VDPU720_JPEG_MODE_YUV411 1
> > +#define VDPU720_JPEG_MODE_YUV420 2
> > +#define VDPU720_JPEG_MODE_YUV422 3
> > +#define VDPU720_JPEG_MODE_YUV440 4
> > +#define VDPU720_JPEG_MODE_YUV444 5
>
> So the driver can decode 4:4:4 JPEGs but always subsamples and outputs
> them as 4:2:0 (NV12)?
For simplicity, yes. I added NV16 support for 4:2:2 sampling for the
next round, as that's a common format for webcams. I'll leave 4:4:4
to NV24 for a later exercise.
> > +#include <media/v4l2-jpeg.h>
> > +#include <media/v4l2-mem2mem.h>
> > +#include <media/videobuf2-core.h>
> > +#include <media/videobuf2-dma-contig.h>
> > +
> > +#include "rkjpegd-vdpu720-regs.h"
> > +
> > +#define RKJPEGD_NAME "rockchip-jpegd"
> > +
> > +/*
> > + * The reference manual gives 48x48 to 65536x65536, and a frame header cannot
> > + * describe more than 65535. The coded format steps in RKJPEGD_CODED_STEP.
> > + * Beyond that only the decoded frame has to fit a 32 bit sizeimage, see
> > + * rkjpegd_raw_fits().
> > + */
> > +#define RKJPEGD_MIN_WIDTH 48
> > +#define RKJPEGD_MIN_HEIGHT 48
> > +#define RKJPEGD_MAX_SIZE 65528
>
> This seems inconsistent. Why not MIN_SIZE or MAX_WIDTH/HEIGHT?
Ok, will change to RKJPEGD_MIN_SIZE
>
>
> [...]
> > +/* The capture format and crop a frame decodes to. */
> > +static void rkjpegd_fill_hdr_fmt(struct v4l2_pix_format_mplane *pix_mp,
> > + struct v4l2_rect *crop,
> > + const struct v4l2_jpeg_header *hdr)
> > +{
> > + rkjpegd_fill_raw_fmt(pix_mp, rkjpegd_raw_pixelformat(hdr),
> > + ALIGN(hdr->frame.width, RKJPEGD_RAW_STEP),
> > + ALIGN(hdr->frame.height, RKJPEGD_RAW_STEP));
>
> Aligning to 16 is safe, but for e.g. grayscale (or YUV 4:4:4) I'd
> expect aligning to 8 to be enough (see vdpu720_mcu_width/height()).
The width has to stay at 16 pixel alignment, as the hardware takes its
stride in units of 16 pixels.
For the height I'd say we have to play around with the FILL_DOWN_E bit.
When set, the reference manual says: "Fill picture height to multiple of
16 pixels". Right now we always have this bit set, same as the Rockchip
driver does. I'd rather leave this up for a later exercise to dig deeper
into that.
>
> > + crop->left = 0;
> > + crop->top = 0;
> > + crop->width = hdr->frame.width;
> > + crop->height = hdr->frame.height;
> > +}
> > +
> [...]
> > +/*
> > + * The capture format as userspace sees it. Once the decoder stopped at a
> > + * format change, that is the one of the frame it stopped in front of.
> > + */
> > +static void rkjpegd_get_cap_fmt(struct rkjpegd_ctx *ctx,
> > + struct v4l2_pix_format_mplane *pix_mp,
> > + struct v4l2_rect *crop)
> > +{
> > + struct vb2_v4l2_buffer *src;
> > +
> > + if (READ_ONCE(ctx->stopped_at_change)) {
>
> Why does TRY_FMT depend on decoder state?
The format is dictated by the JPEG stream. It depends on the decoder
state to support a midstream format change. When stopped at a format
change we have to report the new format, otherwise the current format.
>
> > + src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> > + rkjpegd_fill_hdr_fmt(pix_mp, crop,
> > + &vb2_to_rkjpegd_src_buf(&src->vb2_buf)->header);
> > + return;
> > + }
> > +
> > + *pix_mp = ctx->dst_fmt;
> > + *crop = ctx->crop;
> > +}
> [...]
> > +static int rkjpegd_enum_framesizes(struct file *file, void *priv,
> > + struct v4l2_frmsizeenum *fsize)
> > +{
> > + /* The decoded format follows the bitstream, nothing to enumerate. */
> > + if (fsize->pixel_format == V4L2_PIX_FMT_NV12 ||
> > + fsize->pixel_format == V4L2_PIX_FMT_GREY)
> > + return -ENOTTY;
> > +
> > + if (fsize->pixel_format != V4L2_PIX_FMT_JPEG)
> > + return -EINVAL;
> > +
> > + if (fsize->index)
> > + return -EINVAL;
> > +
> > + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> > + fsize->stepwise.min_width = RKJPEGD_MIN_WIDTH;
> > + fsize->stepwise.max_width = RKJPEGD_MAX_SIZE;
> > + fsize->stepwise.step_width = RKJPEGD_CODED_STEP;
> > + fsize->stepwise.min_height = RKJPEGD_MIN_HEIGHT;
> > + fsize->stepwise.max_height = RKJPEGD_MAX_SIZE;
> > + fsize->stepwise.step_height = RKJPEGD_CODED_STEP;
>
> This is the coded size. I'd expect this to be continuous. You just have
> to make sure that the decoded buffer is properly aligned and set the
> PADDED selection rectangle accordingly.
Oh, yes. That can indeed be continuous. Will change.
> > +
> [...]
> > +
> > + rkjpegd_irq_done(jpegd, state);
>
> Why is this hidden in a separate function call?
Likely historic reasons. I'll inline the code here.
>
> > + return IRQ_HANDLED;
> > +}
> > +
> > +static void rkjpegd_watchdog(struct work_struct *work)
> > +{
> > + struct rkjpegd_dev *jpegd = container_of(to_delayed_work(work),
> > + struct rkjpegd_dev,
> > + watchdog_work);
> > + /* Only armed while a job runs, so there is always a context. */
> > + struct rkjpegd_ctx *ctx = v4l2_m2m_get_curr_priv(jpegd->m2m_dev);
> > +
> > + dev_err(jpegd->dev, "frame processing timed out\n");
> > +
> > + rkjpegd_vdpu720_reset(ctx);
> > + rkjpegd_job_finish(ctx, VB2_BUF_STATE_ERROR);
> > +}
> [...]
> > +static int rkjpegd_buf_out_validate(struct vb2_buffer *vb)
> > +{
> > + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
> > +
> > + vbuf->field = V4L2_FIELD_NONE;
>
> Hmm, "drivers can use this to validate userspace-provided information".
> Is this really validation?
>
> It seems a lot of drivers have exactly the same function:
>
> vpu_vb2_buf_out_validate()
> cedrus_buf_out_validate()
> mdp_m2m_buf_out_validate()
> vb2ops_venc_buf_out_validate()
> tegra_buf_out_validate()
> dw100_buf_out_validate()
> mxc_jpeg_buf_out_validate()
> rkvdec_buf_out_validate()
> hantro_buf_out_validate()
> vicodec_buf_out_validate()
> visl_buf_out_validate()
>
> At least e5010_buf_out_validate() and
> dma2d_buf_out_validate() warn or fail, respectively, if the field value
> is invalid.
>
> Is overwriting field to NONE what this callback was designed for, and
> if so, does this warrant a common helper?
1f2f510753be ("media: vivid: add buf_out_validate callback") which Hans
upstreamed along with the patch adding the buf_out_validate hook
suggests it's really for validation rather than normalisation.
However, buf_out contains the coded stream. The field order has no
meaning in a coded stream, which is why I think that most decoder
drivers just normalize to a sane value.
Yes, this sounds an opportunity to add a generic helper function. If
that's desired I would add one as a separate series though.
>
> > +
> > + return 0;
> > +}
> >
> [...]
> > +
> > +static int rkjpegd_resume(struct device *dev)
> > +{
> > + struct rkjpegd_dev *jpegd = dev_get_drvdata(dev);
> > + int ret;
> > +
> > + ret = pm_runtime_force_resume(dev);
>
> I'd expect this to not call v4l2_m2m_resume() if runtime PM resume
> failed.
Yes. Will change to return early when it fails.
Regards,
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |