18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Free Electrons
48c2ecf20Sopenharmony_ci * Copyright (C) 2015 NextThing Co
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Maxime Ripard <maxime.ripard@free-electrons.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _SUN4I_BACKEND_H_
108c2ecf20Sopenharmony_ci#define _SUN4I_BACKEND_H_
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/list.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/regmap.h>
168c2ecf20Sopenharmony_ci#include <linux/reset.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "sunxi_engine.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_REG		0x800
218c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_LINE_SEL			BIT(29)
228c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_ITLMOD_EN			BIT(28)
238c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OUT_SEL			GENMASK(22, 20)
248c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OUT_LCD0				(0 << 20)
258c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OUT_LCD1				(1 << 20)
268c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OUT_FE0				(6 << 20)
278c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OUT_FE1				(7 << 20)
288c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_HWC_EN			BIT(16)
298c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_LAY_EN(l)			BIT(8 + l)
308c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_OCSC_EN			BIT(5)
318c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_DFLK_EN			BIT(4)
328c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_DLP_START_CTL		BIT(2)
338c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_START_CTL			BIT(1)
348c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_MODCTL_DEBE_EN			BIT(0)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_BACKCOLOR_REG		0x804
378c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_BACKCOLOR(r, g, b)		(((r) << 16) | ((g) << 8) | (b))
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DISSIZE_REG		0x808
408c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DISSIZE(w, h)			(((((h) - 1) & 0xffff) << 16) | \
418c2ecf20Sopenharmony_ci							 (((w) - 1) & 0xffff))
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYSIZE_REG(l)		(0x810 + (0x4 * (l)))
448c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYSIZE(w, h)			(((((h) - 1) & 0x1fff) << 16) | \
458c2ecf20Sopenharmony_ci							 (((w) - 1) & 0x1fff))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYCOOR_REG(l)		(0x820 + (0x4 * (l)))
488c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYCOOR(x, y)			((((u32)(y) & 0xffff) << 16) | \
498c2ecf20Sopenharmony_ci							 ((u32)(x) & 0xffff))
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYLINEWIDTH_REG(l)	(0x840 + (0x4 * (l)))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYFB_L32ADD_REG(l)	(0x850 + (0x4 * (l)))
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYFB_H4ADD_REG		0x860
568c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYFB_H4ADD_MSK(l)		GENMASK(3 + ((l) * 8), (l) * 8)
578c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAYFB_H4ADD(l, val)		((val) << ((l) * 8))
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_REGBUFFCTL_REG		0x870
608c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_REGBUFFCTL_AUTOLOAD_DIS		BIT(1)
618c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_REGBUFFCTL_LOADCTL		BIT(0)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_CKMAX_REG			0x880
648c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_CKMIN_REG			0x884
658c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_CKCFG_REG			0x888
668c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0(l)		(0x890 + (0x4 * (l)))
678c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA_MASK	GENMASK(31, 24)
688c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA(x)		((x) << 24)
698c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL_MASK	BIT(15)
708c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_PIPESEL(x)		((x) << 15)
718c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL_MASK	GENMASK(11, 10)
728c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_PRISEL(x)			((x) << 10)
738c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_YUVEN		BIT(2)
748c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_VDOEN		BIT(1)
758c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG0_LAY_GLBALPHA_EN	BIT(0)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG1(l)		(0x8a0 + (0x4 * (l)))
788c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG1_LAY_HSCAFCT		GENMASK(15, 14)
798c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG1_LAY_WSCAFCT		GENMASK(13, 12)
808c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_ATTCTL_REG1_LAY_FBFMT		GENMASK(11, 8)
818c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_1BPP				(0 << 8)
828c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_2BPP				(1 << 8)
838c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_4BPP				(2 << 8)
848c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_8BPP				(3 << 8)
858c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGB655				(4 << 8)
868c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGB565				(5 << 8)
878c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGB556				(6 << 8)
888c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_ARGB1555			(7 << 8)
898c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGBA5551			(8 << 8)
908c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_XRGB8888			(9 << 8)
918c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_ARGB8888			(10 << 8)
928c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGB888				(11 << 8)
938c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_ARGB4444			(12 << 8)
948c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_LAY_FBFMT_RGBA4444			(13 << 8)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DLCDPCTL_REG		0x8b0
978c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DLCDPFRMBUF_ADDRCTL_REG	0x8b4
988c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DLCDPCOOR_REG0		0x8b8
998c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_DLCDPCOOR_REG1		0x8bc
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_INT_EN_REG		0x8c0
1028c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_INT_FLAG_REG		0x8c4
1038c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_REG_LOAD_FINISHED			BIT(1)
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_HWCCTL_REG		0x8d8
1068c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_HWCFBCTL_REG		0x8e0
1078c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_WBCTL_REG			0x8f0
1088c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_WBADD_REG			0x8f4
1098c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_WBLINEWIDTH_REG		0x8f8
1108c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPREN_REG			0x900
1118c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRFMTCTL_REG		0x908
1128c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRALPHACTL_REG		0x90c
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_REG		0x920
1158c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_MASK		GENMASK(14, 12)
1168c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_PACKED_YUV444		(4 << 12)
1178c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_PACKED_YUV422		(3 << 12)
1188c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_PLANAR_YUV444		(2 << 12)
1198c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_PLANAR_YUV222		(1 << 12)
1208c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBFMT_PLANAR_YUV111		(0 << 12)
1218c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_MASK			GENMASK(9, 8)
1228c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_YVYU				(3 << 8)
1238c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_VYUY				(2 << 8)
1248c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_YUYV				(1 << 8)
1258c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_UYVY				(0 << 8)
1268c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_VUYA				(1 << 8)
1278c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_FBPS_AYUV				(0 << 8)
1288c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVCTL_EN			BIT(0)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVADD_REG(c)		(0x930 + (0x4 * (c)))
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_IYUVLINEWIDTH_REG(c)	(0x940 + (0x4 * (c)))
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_YGCOEF_REG(c)		(0x950 + (0x4 * (c)))
1358c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_YGCONS_REG		0x95c
1368c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_URCOEF_REG(c)		(0x960 + (0x4 * (c)))
1378c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_URCONS_REG		0x96c
1388c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_VBCOEF_REG(c)		(0x970 + (0x4 * (c)))
1398c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_VBCONS_REG		0x97c
1408c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_KSCTL_REG			0x980
1418c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_KSBKCOLOR_REG		0x984
1428c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_KSFSTLINEWIDTH_REG	0x988
1438c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_KSVSCAFCT_REG		0x98c
1448c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_KSHSCACOEF_REG(x)		(0x9a0 + (0x4 * (x)))
1458c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCCTL_REG			0x9c0
1468c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCCTL_ENABLE			BIT(0)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCRCOEF_REG(x)		(0x9d0 + (0x4 * (x)))
1498c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCRCONS_REG		0x9dc
1508c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCGCOEF_REG(x)		(0x9e0 + (0x4 * (x)))
1518c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCGCONS_REG		0x9ec
1528c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCBCOEF_REG(x)		(0x9f0 + (0x4 * (x)))
1538c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_OCBCONS_REG		0x9fc
1548c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRCOORCTL_REG(s)		(0xa00 + (0x4 * (s)))
1558c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRATTCTL_REG(s)		(0xb00 + (0x4 * (s)))
1568c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRADD_REG(s)		(0xc00 + (0x4 * (s)))
1578c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRLINEWIDTH_REG(s)	(0xd00 + (0x4 * (s)))
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_SPRPALTAB_OFF		0x4000
1608c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_GAMMATAB_OFF		0x4400
1618c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_HWCPATTERN_OFF		0x4800
1628c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_HWCCOLORTAB_OFF		0x4c00
1638c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_PIPE_OFF(p)		(0x5000 + (0x400 * (p)))
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_NUM_LAYERS		4
1668c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_NUM_FRONTEND_LAYERS	1
1678c2ecf20Sopenharmony_ci#define SUN4I_BACKEND_NUM_YUV_PLANES		1
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct sun4i_backend {
1708c2ecf20Sopenharmony_ci	struct sunxi_engine	engine;
1718c2ecf20Sopenharmony_ci	struct sun4i_frontend	*frontend;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	struct reset_control	*reset;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	struct clk		*bus_clk;
1768c2ecf20Sopenharmony_ci	struct clk		*mod_clk;
1778c2ecf20Sopenharmony_ci	struct clk		*ram_clk;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	struct clk		*sat_clk;
1808c2ecf20Sopenharmony_ci	struct reset_control	*sat_reset;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Protects against races in the frontend teardown */
1838c2ecf20Sopenharmony_ci	spinlock_t		frontend_lock;
1848c2ecf20Sopenharmony_ci	bool			frontend_teardown;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	const struct sun4i_backend_quirks	*quirks;
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic inline struct sun4i_backend *
1908c2ecf20Sopenharmony_ciengine_to_sun4i_backend(struct sunxi_engine *engine)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	return container_of(engine, struct sun4i_backend, engine);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_civoid sun4i_backend_layer_enable(struct sun4i_backend *backend,
1968c2ecf20Sopenharmony_ci				int layer, bool enable);
1978c2ecf20Sopenharmony_cibool sun4i_backend_format_is_supported(uint32_t fmt, uint64_t modifier);
1988c2ecf20Sopenharmony_ciint sun4i_backend_update_layer_coord(struct sun4i_backend *backend,
1998c2ecf20Sopenharmony_ci				     int layer, struct drm_plane *plane);
2008c2ecf20Sopenharmony_ciint sun4i_backend_update_layer_formats(struct sun4i_backend *backend,
2018c2ecf20Sopenharmony_ci				       int layer, struct drm_plane *plane);
2028c2ecf20Sopenharmony_ciint sun4i_backend_update_layer_buffer(struct sun4i_backend *backend,
2038c2ecf20Sopenharmony_ci				      int layer, struct drm_plane *plane);
2048c2ecf20Sopenharmony_ciint sun4i_backend_update_layer_frontend(struct sun4i_backend *backend,
2058c2ecf20Sopenharmony_ci					int layer, uint32_t in_fmt);
2068c2ecf20Sopenharmony_ciint sun4i_backend_update_layer_zpos(struct sun4i_backend *backend,
2078c2ecf20Sopenharmony_ci				    int layer, struct drm_plane *plane);
2088c2ecf20Sopenharmony_civoid sun4i_backend_cleanup_layer(struct sun4i_backend *backend,
2098c2ecf20Sopenharmony_ci				 int layer);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci#endif /* _SUN4I_BACKEND_H_ */
212