/*
* V4L2 Deinterlacer Subdev for Freescale i.MX5/6 SOC
*
* Copyright (c) 2017 Mentor Graphics Inc.
*
* 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/delay.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-mc.h>
#include <media/v4l2-subdev.h>
#include <media/imx.h>
#include "imx-media.h"
/*
* This subdev implements two different video pipelines:
*
* CSI -> VDIC
*
* In this pipeline, the CSI sends a single interlaced field F(n-1)
* directly to the VDIC (and optionally the following field F(n)
* can be sent to memory via IDMAC channel 13). This pipeline only works
* in VDIC's high motion mode, which only requires a single field for
* processing. The other motion modes (low and medium) require three
* fields, so this pipeline does not work in those modes. Also, it is
* not clear how this pipeline can deal with the various field orders
* (sequential BT/TB, interlaced BT/TB).
*
* MEM -> CH8,9,10 -> VDIC
*
* In this pipeline, previous field F(n-1), current field F(n), and next
* field F(n+1) are transferred to the VDIC via IDMAC channels 8,9,10.
* These memory buffers can come from a video output or mem2mem device.
* All motion modes are supported by this pipeline.
*
* The "direct" CSI->VDIC pipeline requires no DMA, but it can only be
* used in high motion mode.
*/
struct vdic_priv;
struct vdic_pipeline_ops {
int (*setup)(struct vdic_priv *priv);
void (*start)(struct vdic_priv *priv);
void (*stop)(struct vdic_priv *priv);
void (*disable)(struct vdic_priv *priv);
};
/*
* Min/Max supported width and heights.
*/
#define MIN_W 176
#define MIN_H 144
#define MAX_W_VDIC 968
#define MAX_H_VDIC 2048
#define W_ALIGN 4 /* multiple of 16 pixels */
#define H_ALIGN 1 /* multiple of 2 lines */
#define S_ALIGN 1 /* multiple of 2 */
struct vdic_priv {
struct device *dev;
struct ipu_soc *ipu;
struct imx_media_dev *md;
struct v4l2_subdev sd;
struct media_pad pad[VDIC_NUM_PADS];
int ipu_id;
/* lock to protect all members below */
struct mutex lock;
/* IPU units we require */
struct ipu_vdi *vdi;
int active_input_pad;
struct ipuv3_channel *vdi_in_ch_p; /* F(n-1) transfer channel */
struct ipuv3_channel *vdi_in_ch; /* F(n) transfer channel */
struct ipuv3_channel *vdi_in_ch_n; /* F(n+1) transfer channel */
/* pipeline operations */
struct vdic_pipeline_ops *ops;
/* current and previous input buffers indirect path */
struct imx_media_buffer *curr_in_buf;
struct imx_media_buffer *prev_in_buf;
/*
* translated field type, input line stride, and field size
* for indirect path
*/
u32 fieldtype;
u32 in_stride;
u32 field_size;
/* the source (a video device or subdev) */
struct media_entity *src;
/* the sink that will receive the progressive out buffers */
struct v4l2_subdev *sink_sd;
struct v4l2_mbus_framefmt format_mbus[VDIC_NUM_PADS];
const struct imx_media_pixfmt *cc[VDIC_NUM_PADS];
struct v4l2_fract frame_interval[VDIC_NUM_PADS];
/* the video device at IDMAC input pad */
struct imx_media_video_dev *vdev;
bool csi_direct; /* using direct CSI->VDIC->IC pipeline */
/* motion select control */
struct v4l2_ctrl_handler ctrl_hdlr;
enum ipu_motion_sel motion;
int stream_count;
};
static void vdic_put_ipu_resources(struct vdic_priv *priv)
{
if (priv->vdi_in_ch_p)
ipu_idmac_put(priv->vdi_in_ch_p);
priv->vdi_in_ch_p = NULL;
if (priv->vdi_in_ch)
ipu_idmac_put(priv->vdi_in_ch);
priv->vdi_in_ch = NULL;
if (priv->vdi_in_ch_n)
ipu_idmac_put(priv->vdi_in_ch_n);
priv->vdi_in_ch_n = NULL;
if (!IS_ERR_OR_NULL(priv->vdi))
ipu_vdi_put(priv->vdi);
priv->vdi = NULL;
}
static int vdic_get_ipu_resources(struct vdic_priv *priv)
{
int ret, err_chan;
struct ipuv3_channel *ch;
struct ipu_vdi *vdi;
priv->ipu = priv->md->ipu[priv->ipu_id];
vdi = ipu_vdi_get(priv->ipu);
if (IS_ERR(vdi)) {
v4l2_err(&priv->sd, "failed to get VDIC\n");
ret = PTR_ERR(vdi);
goto out;
}
priv->vdi = vdi;
if (!priv->csi_direct) {
ch = ipu_idmac_get(priv->ipu, IPUV3_CHANNEL_MEM_VDI_PREV);
if (IS_ERR(ch)) {
err_chan = IPUV3_CHANNEL_MEM_VDI_PREV;
ret = PTR_ERR(ch);
goto out_err_chan;
}
priv->vdi_in_ch_p = ch;
ch = ipu_idmac_get(priv->ipu, IPUV3_CHANNEL_MEM_VDI_CUR);
if (IS_ERR(ch)) {
err_chan = IPUV3_CHANNEL_MEM_VDI_CUR;
ret = PTR_ERR(ch);
goto out_err_chan;