// SPDX-License-Identifier: GPL-2.0-or-later
/*
* V4L2 deinterlacing support.
*
* Copyright (c) 2012 Vista Silicon S.L.
* Javier Martin <javier.martin@vista-silicon.com>
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/dmaengine.h>
#include <linux/platform_device.h>
#include <media/v4l2-mem2mem.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-dma-contig.h>
#define MEM2MEM_TEST_MODULE_NAME "mem2mem-deinterlace"
MODULE_DESCRIPTION("mem2mem device which supports deinterlacing using dmaengine");
MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.0.1");
static bool debug;
module_param(debug, bool, 0644);
/* Flags that indicate a format can be used for capture/output */
#define MEM2MEM_CAPTURE (1 << 0)
#define MEM2MEM_OUTPUT (1 << 1)
#define MEM2MEM_NAME "m2m-deinterlace"
#define dprintk(dev, fmt, arg...) \
v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
struct deinterlace_fmt {
u32 fourcc;
/* Types the format can be used for */
u32 types;
};
static struct deinterlace_fmt formats[] = {
{
.fourcc = V4L2_PIX_FMT_YUV420,
.types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
},
{
.fourcc = V4L2_PIX_FMT_YUYV,
.types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
},
};
#define NUM_FORMATS ARRAY_SIZE(formats)
/* Per-queue, driver-specific private data */
struct deinterlace_q_data {
unsigned int width;
unsigned int height;
unsigned int sizeimage;
struct deinterlace_fmt *fmt;
enum v4l2_field field;
};
enum {
V4L2_M2M_SRC = 0,
V4L2_M2M_DST = 1,
};
enum {
YUV420_DMA_Y_ODD,
YUV420_DMA_Y_EVEN,
YUV420_DMA_U_ODD,
YUV420_DMA_U_EVEN,
YUV420_DMA_V_ODD,
YUV420_DMA_V_EVEN,
YUV420_DMA_Y_ODD_DOUBLING,
YUV420_DMA_U_ODD_DOUBLING,
YUV420_DMA_V_ODD_DOUBLING,
YUYV_DMA_ODD,
YUYV_DMA_EVEN,
YUYV_DMA_EVEN_DOUBLING,
};
/* Source and destination queue data */
static struct deinterlace_q_data q_data[2];
static struct deinterlace_q_data *get_q_data(enum v4l2_buf_type type)
{
switch (type) {
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
return &q_data[V4L2_M2M_SRC];
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
return &q_data[V4L2_M2M_DST];
default:
BUG();
}
return NULL;
}
static struct deinterlace_fmt *find_format(struct v4l2_format *f)
{
struct deinterlace_fmt *fmt;
unsigned int k;
for (k = 0; k < NUM_FORMATS; k++) {
fmt = &formats[k];
if ((fmt->types & f->type) &&
(fmt->fourcc == f->fmt.pix.pixelformat))
break;
}
if (k == NUM_FORMATS)
return NULL;
return &formats[k];
}
struct deinterlace_dev {
struct v4l2_device v4l2_dev;
struct video_device vfd;
atomic_t busy;
struct mutex dev_mutex;
spinlock_t irqlock;
struct dma_chan *dma_chan;
struct v4l2_m2m_dev *m2m_dev;
};
struct deinterlace_ctx {
struct v4l2_fh fh;
struct deinterlace_dev *dev;
/* Abort requested by m2m */
int aborting;
enum v4l2_colorspace colorspace;
dma_cookie_t cookie;
struct dma_interleaved_template *xt;
};
/*
* mem2mem callbacks
*/
static int deinterlace_job_ready(void *priv)
{
struct deinterlace_ctx *ctx = priv;
struct deinterlace_dev *pcdev = ctx->dev;
if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0 &&
v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) > 0 &&
!atomic_read(&ctx->dev->busy)) {
dprintk(pcdev, "Task ready\n");
return 1;
}
dprintk(pcdev, "Task not ready to run\n");
return 0;
}
static void deinterlace_job_abort(void *priv)
{
struct deinterlace_ctx *ctx = priv;
struct deinterlace_dev *pcdev = ctx->dev;
ctx->aborting = 1;
dprintk(pcdev, "Aborting task\n");
v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->fh.m2m_ctx);
}
static void dma_callback(void *data)
{
struct deinterlace_ctx *curr_ctx = data;
struct deinterlace_dev *pcdev = curr_ctx->dev;
struct vb2_v4l2_buffer *src_vb