18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2013 Texas Instruments Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * David Griego, <dagriego@biglakesoftware.com>
68c2ecf20Sopenharmony_ci * Dale Farnsworth, <dale@farnsworth.org>
78c2ecf20Sopenharmony_ci * Archit Taneja, <archit@ti.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __TI_VPDMA_H_
118c2ecf20Sopenharmony_ci#define __TI_VPDMA_H_
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define VPDMA_MAX_NUM_LIST		8
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * A vpdma_buf tracks the size, DMA address and mapping status of each
168c2ecf20Sopenharmony_ci * driver DMA area.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_cistruct vpdma_buf {
198c2ecf20Sopenharmony_ci	void			*addr;
208c2ecf20Sopenharmony_ci	dma_addr_t		dma_addr;
218c2ecf20Sopenharmony_ci	size_t			size;
228c2ecf20Sopenharmony_ci	bool			mapped;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct vpdma_desc_list {
268c2ecf20Sopenharmony_ci	struct vpdma_buf buf;
278c2ecf20Sopenharmony_ci	void *next;
288c2ecf20Sopenharmony_ci	int type;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct vpdma_data {
328c2ecf20Sopenharmony_ci	void __iomem		*base;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	struct platform_device	*pdev;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	spinlock_t		lock;
378c2ecf20Sopenharmony_ci	bool			hwlist_used[VPDMA_MAX_NUM_LIST];
388c2ecf20Sopenharmony_ci	void			*hwlist_priv[VPDMA_MAX_NUM_LIST];
398c2ecf20Sopenharmony_ci	/* callback to VPE driver when the firmware is loaded */
408c2ecf20Sopenharmony_ci	void (*cb)(struct platform_device *pdev);
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cienum vpdma_data_format_type {
448c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_TYPE_YUV,
458c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_TYPE_RGB,
468c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_TYPE_MISC,
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct vpdma_data_format {
508c2ecf20Sopenharmony_ci	enum vpdma_data_format_type type;
518c2ecf20Sopenharmony_ci	int data_type;
528c2ecf20Sopenharmony_ci	u8 depth;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define VPDMA_DESC_ALIGN		16	/* 16-byte descriptor alignment */
568c2ecf20Sopenharmony_ci#define VPDMA_STRIDE_ALIGN		16	/*
578c2ecf20Sopenharmony_ci						 * line stride of source and dest
588c2ecf20Sopenharmony_ci						 * buffers should be 16 byte aligned
598c2ecf20Sopenharmony_ci						 */
608c2ecf20Sopenharmony_ci#define VPDMA_MAX_STRIDE		65520	/* Max line stride 16 byte aligned */
618c2ecf20Sopenharmony_ci#define VPDMA_DTD_DESC_SIZE		32	/* 8 words */
628c2ecf20Sopenharmony_ci#define VPDMA_CFD_CTD_DESC_SIZE		16	/* 4 words */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define VPDMA_LIST_TYPE_NORMAL		0
658c2ecf20Sopenharmony_ci#define VPDMA_LIST_TYPE_SELF_MODIFYING	1
668c2ecf20Sopenharmony_ci#define VPDMA_LIST_TYPE_DOORBELL	2
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cienum vpdma_yuv_formats {
698c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_Y444 = 0,
708c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_Y422,
718c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_Y420,
728c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_C444,
738c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_C422,
748c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_C420,
758c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_CB420,
768c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_YCR422,
778c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_YC444,
788c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_CRY422,
798c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_CBY422,
808c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_YCB422,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cienum vpdma_rgb_formats {
848c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGB565 = 0,
858c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ARGB16_1555,
868c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ARGB16,
878c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGBA16_5551,
888c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGBA16,
898c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ARGB24,
908c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGB24,
918c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ARGB32,
928c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGBA24,
938c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RGBA32,
948c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGR565,
958c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ABGR16_1555,
968c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ABGR16,
978c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGRA16_5551,
988c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGRA16,
998c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ABGR24,
1008c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGR24,
1018c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_ABGR32,
1028c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGRA24,
1038c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_BGRA32,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cienum vpdma_raw_formats {
1078c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RAW8 = 0,
1088c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_RAW16,
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cienum vpdma_misc_formats {
1128c2ecf20Sopenharmony_ci	VPDMA_DATA_FMT_MV = 0,
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciextern const struct vpdma_data_format vpdma_yuv_fmts[];
1168c2ecf20Sopenharmony_ciextern const struct vpdma_data_format vpdma_rgb_fmts[];
1178c2ecf20Sopenharmony_ciextern const struct vpdma_data_format vpdma_raw_fmts[];
1188c2ecf20Sopenharmony_ciextern const struct vpdma_data_format vpdma_misc_fmts[];
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cienum vpdma_frame_start_event {
1218c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_HDMI_FID = 0,
1228c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_DVO2_FID,
1238c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_HDCOMP_FID,
1248c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_SD_FID,
1258c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_LM_FID0,
1268c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_LM_FID1,
1278c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_LM_FID2,
1288c2ecf20Sopenharmony_ci	VPDMA_FSEVENT_CHANNEL_ACTIVE,
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* max width configurations */
1328c2ecf20Sopenharmony_cienum vpdma_max_width {
1338c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_UNLIMITED = 0,
1348c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_REG1,
1358c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_REG2,
1368c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_REG3,
1378c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_352,
1388c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_768,
1398c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_1280,
1408c2ecf20Sopenharmony_ci	MAX_OUT_WIDTH_1920,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* max height configurations */
1448c2ecf20Sopenharmony_cienum vpdma_max_height {
1458c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_UNLIMITED = 0,
1468c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_REG1,
1478c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_REG2,
1488c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_REG3,
1498c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_288,
1508c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_576,
1518c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_720,
1528c2ecf20Sopenharmony_ci	MAX_OUT_HEIGHT_1080,
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * VPDMA channel numbers
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_cienum vpdma_channel {
1598c2ecf20Sopenharmony_ci	VPE_CHAN_LUMA1_IN,
1608c2ecf20Sopenharmony_ci	VPE_CHAN_CHROMA1_IN,
1618c2ecf20Sopenharmony_ci	VPE_CHAN_LUMA2_IN,
1628c2ecf20Sopenharmony_ci	VPE_CHAN_CHROMA2_IN,
1638c2ecf20Sopenharmony_ci	VPE_CHAN_LUMA3_IN,
1648c2ecf20Sopenharmony_ci	VPE_CHAN_CHROMA3_IN,
1658c2ecf20Sopenharmony_ci	VPE_CHAN_MV_IN,
1668c2ecf20Sopenharmony_ci	VPE_CHAN_MV_OUT,
1678c2ecf20Sopenharmony_ci	VPE_CHAN_LUMA_OUT,
1688c2ecf20Sopenharmony_ci	VPE_CHAN_CHROMA_OUT,
1698c2ecf20Sopenharmony_ci	VPE_CHAN_RGB_OUT,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define VIP_CHAN_VIP2_OFFSET		70
1738c2ecf20Sopenharmony_ci#define VIP_CHAN_MULT_PORTB_OFFSET	16
1748c2ecf20Sopenharmony_ci#define VIP_CHAN_YUV_PORTB_OFFSET	2
1758c2ecf20Sopenharmony_ci#define VIP_CHAN_RGB_PORTB_OFFSET	1
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#define VPDMA_MAX_CHANNELS		256
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* flags for VPDMA data descriptors */
1808c2ecf20Sopenharmony_ci#define VPDMA_DATA_ODD_LINE_SKIP	(1 << 0)
1818c2ecf20Sopenharmony_ci#define VPDMA_DATA_EVEN_LINE_SKIP	(1 << 1)
1828c2ecf20Sopenharmony_ci#define VPDMA_DATA_FRAME_1D		(1 << 2)
1838c2ecf20Sopenharmony_ci#define VPDMA_DATA_MODE_TILED		(1 << 3)
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/*
1868c2ecf20Sopenharmony_ci * client identifiers used for configuration descriptors
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_ci#define CFD_MMR_CLIENT		0
1898c2ecf20Sopenharmony_ci#define CFD_SC_CLIENT		4
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/* Address data block header format */
1928c2ecf20Sopenharmony_cistruct vpdma_adb_hdr {
1938c2ecf20Sopenharmony_ci	u32			offset;
1948c2ecf20Sopenharmony_ci	u32			nwords;
1958c2ecf20Sopenharmony_ci	u32			reserved0;
1968c2ecf20Sopenharmony_ci	u32			reserved1;
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* helpers for creating ADB headers for config descriptors MMRs as client */
2008c2ecf20Sopenharmony_ci#define ADB_ADDR(dma_buf, str, fld)	((dma_buf)->addr + offsetof(str, fld))
2018c2ecf20Sopenharmony_ci#define MMR_ADB_ADDR(buf, str, fld)	ADB_ADDR(&(buf), struct str, fld)
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define VPDMA_SET_MMR_ADB_HDR(buf, str, hdr, regs, offset_a)	\
2048c2ecf20Sopenharmony_ci	do {							\
2058c2ecf20Sopenharmony_ci		struct vpdma_adb_hdr *h;			\
2068c2ecf20Sopenharmony_ci		struct str *adb = NULL;				\
2078c2ecf20Sopenharmony_ci		h = MMR_ADB_ADDR(buf, str, hdr);		\
2088c2ecf20Sopenharmony_ci		h->offset = (offset_a);				\
2098c2ecf20Sopenharmony_ci		h->nwords = sizeof(adb->regs) >> 2;		\
2108c2ecf20Sopenharmony_ci	} while (0)
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/* vpdma descriptor buffer allocation and management */
2138c2ecf20Sopenharmony_ciint vpdma_alloc_desc_buf(struct vpdma_buf *buf, size_t size);
2148c2ecf20Sopenharmony_civoid vpdma_free_desc_buf(struct vpdma_buf *buf);
2158c2ecf20Sopenharmony_ciint vpdma_map_desc_buf(struct vpdma_data *vpdma, struct vpdma_buf *buf);
2168c2ecf20Sopenharmony_civoid vpdma_unmap_desc_buf(struct vpdma_data *vpdma, struct vpdma_buf *buf);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/* vpdma descriptor list funcs */
2198c2ecf20Sopenharmony_ciint vpdma_create_desc_list(struct vpdma_desc_list *list, size_t size, int type);
2208c2ecf20Sopenharmony_civoid vpdma_reset_desc_list(struct vpdma_desc_list *list);
2218c2ecf20Sopenharmony_civoid vpdma_free_desc_list(struct vpdma_desc_list *list);
2228c2ecf20Sopenharmony_ciint vpdma_submit_descs(struct vpdma_data *vpdma, struct vpdma_desc_list *list,
2238c2ecf20Sopenharmony_ci		       int list_num);
2248c2ecf20Sopenharmony_cibool vpdma_list_busy(struct vpdma_data *vpdma, int list_num);
2258c2ecf20Sopenharmony_civoid vpdma_update_dma_addr(struct vpdma_data *vpdma,
2268c2ecf20Sopenharmony_ci	struct vpdma_desc_list *list, dma_addr_t dma_addr,
2278c2ecf20Sopenharmony_ci	void *write_dtd, int drop, int idx);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci/* VPDMA hardware list funcs */
2308c2ecf20Sopenharmony_ciint vpdma_hwlist_alloc(struct vpdma_data *vpdma, void *priv);
2318c2ecf20Sopenharmony_civoid *vpdma_hwlist_get_priv(struct vpdma_data *vpdma, int list_num);
2328c2ecf20Sopenharmony_civoid *vpdma_hwlist_release(struct vpdma_data *vpdma, int list_num);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/* helpers for creating vpdma descriptors */
2358c2ecf20Sopenharmony_civoid vpdma_add_cfd_block(struct vpdma_desc_list *list, int client,
2368c2ecf20Sopenharmony_ci		struct vpdma_buf *blk, u32 dest_offset);
2378c2ecf20Sopenharmony_civoid vpdma_add_cfd_adb(struct vpdma_desc_list *list, int client,
2388c2ecf20Sopenharmony_ci		struct vpdma_buf *adb);
2398c2ecf20Sopenharmony_civoid vpdma_add_sync_on_channel_ctd(struct vpdma_desc_list *list,
2408c2ecf20Sopenharmony_ci		enum vpdma_channel chan);
2418c2ecf20Sopenharmony_civoid vpdma_add_abort_channel_ctd(struct vpdma_desc_list *list,
2428c2ecf20Sopenharmony_ci		int chan_num);
2438c2ecf20Sopenharmony_civoid vpdma_add_out_dtd(struct vpdma_desc_list *list, int width,
2448c2ecf20Sopenharmony_ci		int stride, const struct v4l2_rect *c_rect,
2458c2ecf20Sopenharmony_ci		const struct vpdma_data_format *fmt, dma_addr_t dma_addr,
2468c2ecf20Sopenharmony_ci		int max_w, int max_h, enum vpdma_channel chan, u32 flags);
2478c2ecf20Sopenharmony_civoid vpdma_rawchan_add_out_dtd(struct vpdma_desc_list *list, int width,
2488c2ecf20Sopenharmony_ci		int stride, const struct v4l2_rect *c_rect,
2498c2ecf20Sopenharmony_ci		const struct vpdma_data_format *fmt, dma_addr_t dma_addr,
2508c2ecf20Sopenharmony_ci		int max_w, int max_h, int raw_vpdma_chan, u32 flags);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_civoid vpdma_add_in_dtd(struct vpdma_desc_list *list, int width,
2538c2ecf20Sopenharmony_ci		int stride, const struct v4l2_rect *c_rect,
2548c2ecf20Sopenharmony_ci		const struct vpdma_data_format *fmt, dma_addr_t dma_addr,
2558c2ecf20Sopenharmony_ci		enum vpdma_channel chan, int field, u32 flags, int frame_width,
2568c2ecf20Sopenharmony_ci		int frame_height, int start_h, int start_v);
2578c2ecf20Sopenharmony_ciint vpdma_list_cleanup(struct vpdma_data *vpdma, int list_num,
2588c2ecf20Sopenharmony_ci		int *channels, int size);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/* vpdma list interrupt management */
2618c2ecf20Sopenharmony_civoid vpdma_enable_list_complete_irq(struct vpdma_data *vpdma, int irq_num,
2628c2ecf20Sopenharmony_ci		int list_num, bool enable);
2638c2ecf20Sopenharmony_civoid vpdma_clear_list_stat(struct vpdma_data *vpdma, int irq_num,
2648c2ecf20Sopenharmony_ci			   int list_num);
2658c2ecf20Sopenharmony_ciunsigned int vpdma_get_list_stat(struct vpdma_data *vpdma, int irq_num);
2668c2ecf20Sopenharmony_ciunsigned int vpdma_get_list_mask(struct vpdma_data *vpdma, int irq_num);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci/* vpdma client configuration */
2698c2ecf20Sopenharmony_civoid vpdma_set_line_mode(struct vpdma_data *vpdma, int line_mode,
2708c2ecf20Sopenharmony_ci		enum vpdma_channel chan);
2718c2ecf20Sopenharmony_civoid vpdma_set_frame_start_event(struct vpdma_data *vpdma,
2728c2ecf20Sopenharmony_ci		enum vpdma_frame_start_event fs_event, enum vpdma_channel chan);
2738c2ecf20Sopenharmony_civoid vpdma_set_max_size(struct vpdma_data *vpdma, int reg_addr,
2748c2ecf20Sopenharmony_ci			u32 width, u32 height);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_civoid vpdma_set_bg_color(struct vpdma_data *vpdma,
2778c2ecf20Sopenharmony_ci			struct vpdma_data_format *fmt, u32 color);
2788c2ecf20Sopenharmony_civoid vpdma_dump_regs(struct vpdma_data *vpdma);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/* initialize vpdma, passed with VPE's platform device pointer */
2818c2ecf20Sopenharmony_ciint vpdma_create(struct platform_device *pdev, struct vpdma_data *vpdma,
2828c2ecf20Sopenharmony_ci		void (*cb)(struct platform_device *pdev));
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci#endif
285