/*
* V4L2 deinterlacing support.
*
* Copyright (c) 2012 Vista Silicon S.L.
* Javier Martin <javier.martin@vista-silicon.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or (at your option) any later version
*/
#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 {
char *name;
u32 fourcc;
/* Types the format can be used for */
u32 types;
};
static struct deinterlace_fmt formats[] = {
{
.name = "YUV 4:2:0 Planar",
.fourcc = V4L2_PIX_FMT_YUV420,
.types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
},
{
.name = "YUYV 4:2:2",
.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 vb2_alloc_ctx *alloc_ctx;
};
struct deinterlace_ctx {
struct deinterlace_dev *dev;
/* Abort requested by m2m */
int aborting;
enum v4l2_colorspace colorspace;
dma_cookie_t cookie;
struct v4l2_m2m_ctx *m2m_ctx;
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->m2m_ctx) > 0)
<