162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Allwinner Deinterlace driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2019 Jernej Skrabec <jernej.skrabec@siol.net>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _SUN8I_DEINTERLACE_H_
962306a36Sopenharmony_ci#define _SUN8I_DEINTERLACE_H_
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <media/v4l2-device.h>
1262306a36Sopenharmony_ci#include <media/v4l2-mem2mem.h>
1362306a36Sopenharmony_ci#include <media/videobuf2-v4l2.h>
1462306a36Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <linux/platform_device.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define DEINTERLACE_NAME		"sun8i-di"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define DEINTERLACE_MOD_ENABLE			0x00
2162306a36Sopenharmony_ci#define DEINTERLACE_MOD_ENABLE_EN			BIT(0)
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL			0x04
2462306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL_REG_READY			BIT(0)
2562306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL_WB_EN			BIT(2)
2662306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL_OUT_CTRL			BIT(11)
2762306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL_START			BIT(16)
2862306a36Sopenharmony_ci#define DEINTERLACE_FRM_CTRL_COEF_ACCESS		BIT(23)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define DEINTERLACE_BYPASS			0x08
3162306a36Sopenharmony_ci#define DEINTERLACE_BYPASS_CSC				BIT(1)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define DEINTERLACE_AGTH_SEL			0x0c
3462306a36Sopenharmony_ci#define DEINTERLACE_AGTH_SEL_LINEBUF			BIT(8)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define DEINTERLACE_LINT_CTRL			0x10
3762306a36Sopenharmony_ci#define DEINTERLACE_TRD_PRELUMA			0x1c
3862306a36Sopenharmony_ci#define DEINTERLACE_BUF_ADDR0			0x20
3962306a36Sopenharmony_ci#define DEINTERLACE_BUF_ADDR1			0x24
4062306a36Sopenharmony_ci#define DEINTERLACE_BUF_ADDR2			0x28
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define DEINTERLACE_FIELD_CTRL			0x2c
4362306a36Sopenharmony_ci#define DEINTERLACE_FIELD_CTRL_FIELD_CNT(v)		((v) & 0xff)
4462306a36Sopenharmony_ci#define DEINTERLACE_FIELD_CTRL_FIELD_CNT_MSK		(0xff)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define DEINTERLACE_TB_OFFSET0			0x30
4762306a36Sopenharmony_ci#define DEINTERLACE_TB_OFFSET1			0x34
4862306a36Sopenharmony_ci#define DEINTERLACE_TB_OFFSET2			0x38
4962306a36Sopenharmony_ci#define DEINTERLACE_TRD_PRECHROMA		0x3c
5062306a36Sopenharmony_ci#define DEINTERLACE_LINE_STRIDE0		0x40
5162306a36Sopenharmony_ci#define DEINTERLACE_LINE_STRIDE1		0x44
5262306a36Sopenharmony_ci#define DEINTERLACE_LINE_STRIDE2		0x48
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define DEINTERLACE_IN_FMT			0x4c
5562306a36Sopenharmony_ci#define DEINTERLACE_IN_FMT_PS(v)			((v) & 3)
5662306a36Sopenharmony_ci#define DEINTERLACE_IN_FMT_FMT(v)			(((v) & 7) << 4)
5762306a36Sopenharmony_ci#define DEINTERLACE_IN_FMT_MOD(v)			(((v) & 7) << 8)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define DEINTERLACE_WB_ADDR0			0x50
6062306a36Sopenharmony_ci#define DEINTERLACE_WB_ADDR1			0x54
6162306a36Sopenharmony_ci#define DEINTERLACE_WB_ADDR2			0x58
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define DEINTERLACE_OUT_FMT			0x5c
6462306a36Sopenharmony_ci#define DEINTERLACE_OUT_FMT_FMT(v)			((v) & 0xf)
6562306a36Sopenharmony_ci#define DEINTERLACE_OUT_FMT_PS(v)			(((v) & 3) << 5)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define DEINTERLACE_INT_ENABLE			0x60
6862306a36Sopenharmony_ci#define DEINTERLACE_INT_ENABLE_WB_EN			BIT(7)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define DEINTERLACE_INT_STATUS			0x64
7162306a36Sopenharmony_ci#define DEINTERLACE_INT_STATUS_WRITEBACK		BIT(7)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#define DEINTERLACE_STATUS			0x68
7462306a36Sopenharmony_ci#define DEINTERLACE_STATUS_COEF_STATUS			BIT(11)
7562306a36Sopenharmony_ci#define DEINTERLACE_STATUS_WB_ERROR			BIT(12)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define DEINTERLACE_CSC_COEF			0x70 /* 12 registers */
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci#define DEINTERLACE_CTRL			0xa0
8062306a36Sopenharmony_ci#define DEINTERLACE_CTRL_EN				BIT(0)
8162306a36Sopenharmony_ci#define DEINTERLACE_CTRL_FLAG_OUT_EN			BIT(8)
8262306a36Sopenharmony_ci#define DEINTERLACE_CTRL_MODE_PASSTROUGH		(0 << 16)
8362306a36Sopenharmony_ci#define DEINTERLACE_CTRL_MODE_WEAVE			(1 << 16)
8462306a36Sopenharmony_ci#define DEINTERLACE_CTRL_MODE_BOB			(2 << 16)
8562306a36Sopenharmony_ci#define DEINTERLACE_CTRL_MODE_MIXED			(3 << 16)
8662306a36Sopenharmony_ci#define DEINTERLACE_CTRL_DIAG_INTP_EN			BIT(24)
8762306a36Sopenharmony_ci#define DEINTERLACE_CTRL_TEMP_DIFF_EN			BIT(25)
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP			0xa4
9062306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH0(v)			((v) & 0x7f)
9162306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH0_MSK			(0x7f)
9262306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH1(v)			(((v) & 0x7f) << 8)
9362306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH1_MSK			(0x7f << 8)
9462306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH3(v)			(((v) & 0xff) << 24)
9562306a36Sopenharmony_ci#define DEINTERLACE_DIAG_INTP_TH3_MSK			(0xff << 24)
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF			0xa8
9862306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_SAD_CENTRAL_TH(v)		((v) & 0x7f)
9962306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_SAD_CENTRAL_TH_MSK	(0x7f)
10062306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_AMBIGUITY_TH(v)		(((v) & 0x7f) << 8)
10162306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_AMBIGUITY_TH_MSK		(0x7f << 8)
10262306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_DIRECT_DITHER_TH(v)	(((v) & 0x7ff) << 16)
10362306a36Sopenharmony_ci#define DEINTERLACE_TEMP_DIFF_DIRECT_DITHER_TH_MSK	(0x7ff << 16)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH			0xac
10662306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_MIN_LUMA(v)			((v) & 0xff)
10762306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_MIN_LUMA_MSK		(0xff)
10862306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_MAX_LUMA(v)			(((v) & 0xff) << 8)
10962306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_MAX_LUMA_MSK		(0xff << 8)
11062306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_AVG_LUMA_SHIFT(v)		(((v) & 0xff) << 16)
11162306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_AVG_LUMA_SHIFT_MSK		(0xff << 16)
11262306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_PIXEL_STATIC(v)		(((v) & 3) << 24)
11362306a36Sopenharmony_ci#define DEINTERLACE_LUMA_TH_PIXEL_STATIC_MSK		(3 << 24)
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define DEINTERLACE_SPAT_COMP			0xb0
11662306a36Sopenharmony_ci#define DEINTERLACE_SPAT_COMP_TH2(v)			((v) & 0xff)
11762306a36Sopenharmony_ci#define DEINTERLACE_SPAT_COMP_TH2_MSK			(0xff)
11862306a36Sopenharmony_ci#define DEINTERLACE_SPAT_COMP_TH3(v)			(((v) & 0xff) << 16)
11962306a36Sopenharmony_ci#define DEINTERLACE_SPAT_COMP_TH3_MSK			(0xff << 16)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF			0xb4
12262306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_TH(v)			((v) & 0xff)
12362306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_TH_MSK			(0xff)
12462306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_LUMA(v)			(((v) & 0x3f) << 16)
12562306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_LUMA_MSK		(0x3f << 16)
12662306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_CHROMA(v)		(((v) & 0x3f) << 24)
12762306a36Sopenharmony_ci#define DEINTERLACE_CHROMA_DIFF_CHROMA_MSK		(0x3f << 24)
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#define DEINTERLACE_PRELUMA			0xb8
13062306a36Sopenharmony_ci#define DEINTERLACE_PRECHROMA			0xbc
13162306a36Sopenharmony_ci#define DEINTERLACE_TILE_FLAG0			0xc0
13262306a36Sopenharmony_ci#define DEINTERLACE_TILE_FLAG1			0xc4
13362306a36Sopenharmony_ci#define DEINTERLACE_FLAG_LINE_STRIDE		0xc8
13462306a36Sopenharmony_ci#define DEINTERLACE_FLAG_SEQ			0xcc
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define DEINTERLACE_WB_LINE_STRIDE_CTRL		0xd0
13762306a36Sopenharmony_ci#define DEINTERLACE_WB_LINE_STRIDE_CTRL_EN		BIT(0)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define DEINTERLACE_WB_LINE_STRIDE0		0xd4
14062306a36Sopenharmony_ci#define DEINTERLACE_WB_LINE_STRIDE1		0xd8
14162306a36Sopenharmony_ci#define DEINTERLACE_WB_LINE_STRIDE2		0xdc
14262306a36Sopenharmony_ci#define DEINTERLACE_TRD_CTRL			0xe0
14362306a36Sopenharmony_ci#define DEINTERLACE_TRD_BUF_ADDR0		0xe4
14462306a36Sopenharmony_ci#define DEINTERLACE_TRD_BUF_ADDR1		0xe8
14562306a36Sopenharmony_ci#define DEINTERLACE_TRD_BUF_ADDR2		0xec
14662306a36Sopenharmony_ci#define DEINTERLACE_TRD_TB_OFF0			0xf0
14762306a36Sopenharmony_ci#define DEINTERLACE_TRD_TB_OFF1			0xf4
14862306a36Sopenharmony_ci#define DEINTERLACE_TRD_TB_OFF2			0xf8
14962306a36Sopenharmony_ci#define DEINTERLACE_TRD_WB_STRIDE		0xfc
15062306a36Sopenharmony_ci#define DEINTERLACE_CH0_IN_SIZE			0x100
15162306a36Sopenharmony_ci#define DEINTERLACE_CH0_OUT_SIZE		0x104
15262306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_FACT		0x108
15362306a36Sopenharmony_ci#define DEINTERLACE_CH0_VERT_FACT		0x10c
15462306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_PHASE		0x110
15562306a36Sopenharmony_ci#define DEINTERLACE_CH0_VERT_PHASE0		0x114
15662306a36Sopenharmony_ci#define DEINTERLACE_CH0_VERT_PHASE1		0x118
15762306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_TAP0		0x120
15862306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_TAP1		0x124
15962306a36Sopenharmony_ci#define DEINTERLACE_CH0_VERT_TAP		0x128
16062306a36Sopenharmony_ci#define DEINTERLACE_CH1_IN_SIZE			0x200
16162306a36Sopenharmony_ci#define DEINTERLACE_CH1_OUT_SIZE		0x204
16262306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_FACT		0x208
16362306a36Sopenharmony_ci#define DEINTERLACE_CH1_VERT_FACT		0x20c
16462306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_PHASE		0x210
16562306a36Sopenharmony_ci#define DEINTERLACE_CH1_VERT_PHASE0		0x214
16662306a36Sopenharmony_ci#define DEINTERLACE_CH1_VERT_PHASE1		0x218
16762306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_TAP0		0x220
16862306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_TAP1		0x224
16962306a36Sopenharmony_ci#define DEINTERLACE_CH1_VERT_TAP		0x228
17062306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_COEF0		0x400 /* 32 registers */
17162306a36Sopenharmony_ci#define DEINTERLACE_CH0_HORZ_COEF1		0x480 /* 32 registers */
17262306a36Sopenharmony_ci#define DEINTERLACE_CH0_VERT_COEF		0x500 /* 32 registers */
17362306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_COEF0		0x600 /* 32 registers */
17462306a36Sopenharmony_ci#define DEINTERLACE_CH1_HORZ_COEF1		0x680 /* 32 registers */
17562306a36Sopenharmony_ci#define DEINTERLACE_CH1_VERT_COEF		0x700 /* 32 registers */
17662306a36Sopenharmony_ci#define DEINTERLACE_CH3_HORZ_COEF0		0x800 /* 32 registers */
17762306a36Sopenharmony_ci#define DEINTERLACE_CH3_HORZ_COEF1		0x880 /* 32 registers */
17862306a36Sopenharmony_ci#define DEINTERLACE_CH3_VERT_COEF		0x900 /* 32 registers */
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#define DEINTERLACE_MIN_WIDTH	2U
18162306a36Sopenharmony_ci#define DEINTERLACE_MIN_HEIGHT	2U
18262306a36Sopenharmony_ci#define DEINTERLACE_MAX_WIDTH	2048U
18362306a36Sopenharmony_ci#define DEINTERLACE_MAX_HEIGHT	1100U
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci#define DEINTERLACE_MODE_UV_COMBINED	2
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci#define DEINTERLACE_IN_FMT_YUV420	2
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define DEINTERLACE_OUT_FMT_YUV420SP	13
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci#define DEINTERLACE_PS_UVUV		0
19262306a36Sopenharmony_ci#define DEINTERLACE_PS_VUVU		1
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#define DEINTERLACE_IDENTITY_COEF	0x4000
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci#define DEINTERLACE_SIZE(w, h)	(((h) - 1) << 16 | ((w) - 1))
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistruct deinterlace_ctx {
19962306a36Sopenharmony_ci	struct v4l2_fh		fh;
20062306a36Sopenharmony_ci	struct deinterlace_dev	*dev;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	struct v4l2_pix_format	src_fmt;
20362306a36Sopenharmony_ci	struct v4l2_pix_format	dst_fmt;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	void			*flag1_buf;
20662306a36Sopenharmony_ci	dma_addr_t		flag1_buf_dma;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	void			*flag2_buf;
20962306a36Sopenharmony_ci	dma_addr_t		flag2_buf_dma;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	struct vb2_v4l2_buffer	*prev;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	unsigned int		first_field;
21462306a36Sopenharmony_ci	unsigned int		field;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	int			aborting;
21762306a36Sopenharmony_ci};
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistruct deinterlace_dev {
22062306a36Sopenharmony_ci	struct v4l2_device	v4l2_dev;
22162306a36Sopenharmony_ci	struct video_device	vfd;
22262306a36Sopenharmony_ci	struct device		*dev;
22362306a36Sopenharmony_ci	struct v4l2_m2m_dev	*m2m_dev;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* Device file mutex */
22662306a36Sopenharmony_ci	struct mutex		dev_mutex;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	void __iomem		*base;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	struct clk		*bus_clk;
23162306a36Sopenharmony_ci	struct clk		*mod_clk;
23262306a36Sopenharmony_ci	struct clk		*ram_clk;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	struct reset_control	*rstc;
23562306a36Sopenharmony_ci};
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci#endif
238