14f2a8f58SJoel Stanley // SPDX-License-Identifier: GPL-2.0+
24f2a8f58SJoel Stanley // Copyright 2018 IBM Corporation
34f2a8f58SJoel Stanley 
44f2a8f58SJoel Stanley #include <linux/clk.h>
54f2a8f58SJoel Stanley #include <linux/reset.h>
64f2a8f58SJoel Stanley #include <linux/regmap.h>
74f2a8f58SJoel Stanley 
84f2a8f58SJoel Stanley #include <drm/drm_device.h>
96bcfe8eaSDanilo Krummrich #include <drm/drm_fb_dma_helper.h>
104f2a8f58SJoel Stanley #include <drm/drm_fourcc.h>
11720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
12820c1707SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
134a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
144f2a8f58SJoel Stanley #include <drm/drm_panel.h>
154f2a8f58SJoel Stanley #include <drm/drm_simple_kms_helper.h>
164f2a8f58SJoel Stanley #include <drm/drm_vblank.h>
174f2a8f58SJoel Stanley 
184f2a8f58SJoel Stanley #include "aspeed_gfx.h"
194f2a8f58SJoel Stanley 
204f2a8f58SJoel Stanley static struct aspeed_gfx *
drm_pipe_to_aspeed_gfx(struct drm_simple_display_pipe * pipe)214f2a8f58SJoel Stanley drm_pipe_to_aspeed_gfx(struct drm_simple_display_pipe *pipe)
224f2a8f58SJoel Stanley {
234f2a8f58SJoel Stanley 	return container_of(pipe, struct aspeed_gfx, pipe);
244f2a8f58SJoel Stanley }
254f2a8f58SJoel Stanley 
aspeed_gfx_set_pixel_fmt(struct aspeed_gfx * priv,u32 * bpp)264f2a8f58SJoel Stanley static int aspeed_gfx_set_pixel_fmt(struct aspeed_gfx *priv, u32 *bpp)
274f2a8f58SJoel Stanley {
284f2a8f58SJoel Stanley 	struct drm_crtc *crtc = &priv->pipe.crtc;
294f2a8f58SJoel Stanley 	struct drm_device *drm = crtc->dev;
304f2a8f58SJoel Stanley 	const u32 format = crtc->primary->state->fb->format->format;
314f2a8f58SJoel Stanley 	u32 ctrl1;
324f2a8f58SJoel Stanley 
334f2a8f58SJoel Stanley 	ctrl1 = readl(priv->base + CRT_CTRL1);
344f2a8f58SJoel Stanley 	ctrl1 &= ~CRT_CTRL_COLOR_MASK;
354f2a8f58SJoel Stanley 
364f2a8f58SJoel Stanley 	switch (format) {
374f2a8f58SJoel Stanley 	case DRM_FORMAT_RGB565:
384f2a8f58SJoel Stanley 		dev_dbg(drm->dev, "Setting up RGB565 mode\n");
394f2a8f58SJoel Stanley 		ctrl1 |= CRT_CTRL_COLOR_RGB565;
404f2a8f58SJoel Stanley 		*bpp = 16;
414f2a8f58SJoel Stanley 		break;
424f2a8f58SJoel Stanley 	case DRM_FORMAT_XRGB8888:
434f2a8f58SJoel Stanley 		dev_dbg(drm->dev, "Setting up XRGB8888 mode\n");
444f2a8f58SJoel Stanley 		ctrl1 |= CRT_CTRL_COLOR_XRGB8888;
454f2a8f58SJoel Stanley 		*bpp = 32;
464f2a8f58SJoel Stanley 		break;
474f2a8f58SJoel Stanley 	default:
484f2a8f58SJoel Stanley 		dev_err(drm->dev, "Unhandled pixel format %08x\n", format);
494f2a8f58SJoel Stanley 		return -EINVAL;
504f2a8f58SJoel Stanley 	}
514f2a8f58SJoel Stanley 
524f2a8f58SJoel Stanley 	writel(ctrl1, priv->base + CRT_CTRL1);
534f2a8f58SJoel Stanley 
544f2a8f58SJoel Stanley 	return 0;
554f2a8f58SJoel Stanley }
564f2a8f58SJoel Stanley 
aspeed_gfx_enable_controller(struct aspeed_gfx * priv)574f2a8f58SJoel Stanley static void aspeed_gfx_enable_controller(struct aspeed_gfx *priv)
584f2a8f58SJoel Stanley {
594f2a8f58SJoel Stanley 	u32 ctrl1 = readl(priv->base + CRT_CTRL1);
604f2a8f58SJoel Stanley 	u32 ctrl2 = readl(priv->base + CRT_CTRL2);
614f2a8f58SJoel Stanley 
62bce724faSJoel Stanley 	/* Set DAC source for display output to Graphics CRT (GFX) */
63bce724faSJoel Stanley 	regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), BIT(16));
644f2a8f58SJoel Stanley 
654f2a8f58SJoel Stanley 	writel(ctrl1 | CRT_CTRL_EN, priv->base + CRT_CTRL1);
664f2a8f58SJoel Stanley 	writel(ctrl2 | CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
674f2a8f58SJoel Stanley }
684f2a8f58SJoel Stanley 
aspeed_gfx_disable_controller(struct aspeed_gfx * priv)694f2a8f58SJoel Stanley static void aspeed_gfx_disable_controller(struct aspeed_gfx *priv)
704f2a8f58SJoel Stanley {
714f2a8f58SJoel Stanley 	u32 ctrl1 = readl(priv->base + CRT_CTRL1);
724f2a8f58SJoel Stanley 	u32 ctrl2 = readl(priv->base + CRT_CTRL2);
734f2a8f58SJoel Stanley 
744f2a8f58SJoel Stanley 	writel(ctrl1 & ~CRT_CTRL_EN, priv->base + CRT_CTRL1);
754f2a8f58SJoel Stanley 	writel(ctrl2 & ~CRT_CTRL_DAC_EN, priv->base + CRT_CTRL2);
764f2a8f58SJoel Stanley 
77bce724faSJoel Stanley 	regmap_update_bits(priv->scu, priv->dac_reg, BIT(16), 0);
784f2a8f58SJoel Stanley }
794f2a8f58SJoel Stanley 
aspeed_gfx_crtc_mode_set_nofb(struct aspeed_gfx * priv)804f2a8f58SJoel Stanley static void aspeed_gfx_crtc_mode_set_nofb(struct aspeed_gfx *priv)
814f2a8f58SJoel Stanley {
824f2a8f58SJoel Stanley 	struct drm_display_mode *m = &priv->pipe.crtc.state->adjusted_mode;
834f2a8f58SJoel Stanley 	u32 ctrl1, d_offset, t_count, bpp;
844f2a8f58SJoel Stanley 	int err;
854f2a8f58SJoel Stanley 
864f2a8f58SJoel Stanley 	err = aspeed_gfx_set_pixel_fmt(priv, &bpp);
874f2a8f58SJoel Stanley 	if (err)
884f2a8f58SJoel Stanley 		return;
894f2a8f58SJoel Stanley 
904f2a8f58SJoel Stanley #if 0
914f2a8f58SJoel Stanley 	/* TODO: we have only been able to test with the 40MHz USB clock. The
924f2a8f58SJoel Stanley 	 * clock is fixed, so we cannot adjust it here. */
934f2a8f58SJoel Stanley 	clk_set_rate(priv->pixel_clk, m->crtc_clock * 1000);
944f2a8f58SJoel Stanley #endif
954f2a8f58SJoel Stanley 
964f2a8f58SJoel Stanley 	ctrl1 = readl(priv->base + CRT_CTRL1);
974f2a8f58SJoel Stanley 	ctrl1 &= ~(CRT_CTRL_INTERLACED |
984f2a8f58SJoel Stanley 			CRT_CTRL_HSYNC_NEGATIVE |
994f2a8f58SJoel Stanley 			CRT_CTRL_VSYNC_NEGATIVE);
1004f2a8f58SJoel Stanley 
1014f2a8f58SJoel Stanley 	if (m->flags & DRM_MODE_FLAG_INTERLACE)
1024f2a8f58SJoel Stanley 		ctrl1 |= CRT_CTRL_INTERLACED;
1034f2a8f58SJoel Stanley 
1044f2a8f58SJoel Stanley 	if (!(m->flags & DRM_MODE_FLAG_PHSYNC))
1054f2a8f58SJoel Stanley 		ctrl1 |= CRT_CTRL_HSYNC_NEGATIVE;
1064f2a8f58SJoel Stanley 
1074f2a8f58SJoel Stanley 	if (!(m->flags & DRM_MODE_FLAG_PVSYNC))
1084f2a8f58SJoel Stanley 		ctrl1 |= CRT_CTRL_VSYNC_NEGATIVE;
1094f2a8f58SJoel Stanley 
1104f2a8f58SJoel Stanley 	writel(ctrl1, priv->base + CRT_CTRL1);
1114f2a8f58SJoel Stanley 
1124f2a8f58SJoel Stanley 	/* Horizontal timing */
1134f2a8f58SJoel Stanley 	writel(CRT_H_TOTAL(m->htotal - 1) | CRT_H_DE(m->hdisplay - 1),
1144f2a8f58SJoel Stanley 			priv->base + CRT_HORIZ0);
1154f2a8f58SJoel Stanley 	writel(CRT_H_RS_START(m->hsync_start - 1) | CRT_H_RS_END(m->hsync_end),
1164f2a8f58SJoel Stanley 			priv->base + CRT_HORIZ1);
1174f2a8f58SJoel Stanley 
1184f2a8f58SJoel Stanley 
1194f2a8f58SJoel Stanley 	/* Vertical timing */
1204f2a8f58SJoel Stanley 	writel(CRT_V_TOTAL(m->vtotal - 1) | CRT_V_DE(m->vdisplay - 1),
1214f2a8f58SJoel Stanley 			priv->base + CRT_VERT0);
1224f2a8f58SJoel Stanley 	writel(CRT_V_RS_START(m->vsync_start) | CRT_V_RS_END(m->vsync_end),
1234f2a8f58SJoel Stanley 			priv->base + CRT_VERT1);
1244f2a8f58SJoel Stanley 
1254f2a8f58SJoel Stanley 	/*
1264f2a8f58SJoel Stanley 	 * Display Offset: address difference between consecutive scan lines
1274f2a8f58SJoel Stanley 	 * Terminal Count: memory size of one scan line
1284f2a8f58SJoel Stanley 	 */
1294f2a8f58SJoel Stanley 	d_offset = m->hdisplay * bpp / 8;
130bce724faSJoel Stanley 	t_count = DIV_ROUND_UP(m->hdisplay * bpp, priv->scan_line_max);
131bce724faSJoel Stanley 
1324f2a8f58SJoel Stanley 	writel(CRT_DISP_OFFSET(d_offset) | CRT_TERM_COUNT(t_count),
1334f2a8f58SJoel Stanley 			priv->base + CRT_OFFSET);
1344f2a8f58SJoel Stanley 
1354f2a8f58SJoel Stanley 	/*
1364f2a8f58SJoel Stanley 	 * Threshold: FIFO thresholds of refill and stop (16 byte chunks
1374f2a8f58SJoel Stanley 	 * per line, rounded up)
1384f2a8f58SJoel Stanley 	 */
139bce724faSJoel Stanley 	writel(priv->throd_val, priv->base + CRT_THROD);
1404f2a8f58SJoel Stanley }
1414f2a8f58SJoel Stanley 
aspeed_gfx_pipe_enable(struct drm_simple_display_pipe * pipe,struct drm_crtc_state * crtc_state,struct drm_plane_state * plane_state)1424f2a8f58SJoel Stanley static void aspeed_gfx_pipe_enable(struct drm_simple_display_pipe *pipe,
1434f2a8f58SJoel Stanley 			      struct drm_crtc_state *crtc_state,
1444f2a8f58SJoel Stanley 			      struct drm_plane_state *plane_state)
1454f2a8f58SJoel Stanley {
1464f2a8f58SJoel Stanley 	struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
1474f2a8f58SJoel Stanley 	struct drm_crtc *crtc = &pipe->crtc;
1484f2a8f58SJoel Stanley 
1494f2a8f58SJoel Stanley 	aspeed_gfx_crtc_mode_set_nofb(priv);
1504f2a8f58SJoel Stanley 	aspeed_gfx_enable_controller(priv);
1514f2a8f58SJoel Stanley 	drm_crtc_vblank_on(crtc);
1524f2a8f58SJoel Stanley }
1534f2a8f58SJoel Stanley 
aspeed_gfx_pipe_disable(struct drm_simple_display_pipe * pipe)1544f2a8f58SJoel Stanley static void aspeed_gfx_pipe_disable(struct drm_simple_display_pipe *pipe)
1554f2a8f58SJoel Stanley {
1564f2a8f58SJoel Stanley 	struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
1574f2a8f58SJoel Stanley 	struct drm_crtc *crtc = &pipe->crtc;
1584f2a8f58SJoel Stanley 
1594f2a8f58SJoel Stanley 	drm_crtc_vblank_off(crtc);
1604f2a8f58SJoel Stanley 	aspeed_gfx_disable_controller(priv);
1614f2a8f58SJoel Stanley }
1624f2a8f58SJoel Stanley 
aspeed_gfx_pipe_update(struct drm_simple_display_pipe * pipe,struct drm_plane_state * plane_state)1634f2a8f58SJoel Stanley static void aspeed_gfx_pipe_update(struct drm_simple_display_pipe *pipe,
1644f2a8f58SJoel Stanley 				   struct drm_plane_state *plane_state)
1654f2a8f58SJoel Stanley {
1664f2a8f58SJoel Stanley 	struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
1674f2a8f58SJoel Stanley 	struct drm_crtc *crtc = &pipe->crtc;
1684f2a8f58SJoel Stanley 	struct drm_framebuffer *fb = pipe->plane.state->fb;
1694f2a8f58SJoel Stanley 	struct drm_pending_vblank_event *event;
1704a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *gem;
1714f2a8f58SJoel Stanley 
1724f2a8f58SJoel Stanley 	spin_lock_irq(&crtc->dev->event_lock);
1734f2a8f58SJoel Stanley 	event = crtc->state->event;
1744f2a8f58SJoel Stanley 	if (event) {
1754f2a8f58SJoel Stanley 		crtc->state->event = NULL;
1764f2a8f58SJoel Stanley 
1774f2a8f58SJoel Stanley 		if (drm_crtc_vblank_get(crtc) == 0)
1784f2a8f58SJoel Stanley 			drm_crtc_arm_vblank_event(crtc, event);
1794f2a8f58SJoel Stanley 		else
1804f2a8f58SJoel Stanley 			drm_crtc_send_vblank_event(crtc, event);
1814f2a8f58SJoel Stanley 	}
1824f2a8f58SJoel Stanley 	spin_unlock_irq(&crtc->dev->event_lock);
1834f2a8f58SJoel Stanley 
1844f2a8f58SJoel Stanley 	if (!fb)
1854f2a8f58SJoel Stanley 		return;
1864f2a8f58SJoel Stanley 
1876bcfe8eaSDanilo Krummrich 	gem = drm_fb_dma_get_gem_obj(fb, 0);
1884f2a8f58SJoel Stanley 	if (!gem)
1894f2a8f58SJoel Stanley 		return;
190*8c30eeccSDanilo Krummrich 	writel(gem->dma_addr, priv->base + CRT_ADDR);
1914f2a8f58SJoel Stanley }
1924f2a8f58SJoel Stanley 
aspeed_gfx_enable_vblank(struct drm_simple_display_pipe * pipe)1934f2a8f58SJoel Stanley static int aspeed_gfx_enable_vblank(struct drm_simple_display_pipe *pipe)
1944f2a8f58SJoel Stanley {
1954f2a8f58SJoel Stanley 	struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
1964f2a8f58SJoel Stanley 	u32 reg = readl(priv->base + CRT_CTRL1);
1974f2a8f58SJoel Stanley 
1984f2a8f58SJoel Stanley 	/* Clear pending VBLANK IRQ */
1994f2a8f58SJoel Stanley 	writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
2004f2a8f58SJoel Stanley 
2014f2a8f58SJoel Stanley 	reg |= CRT_CTRL_VERTICAL_INTR_EN;
2024f2a8f58SJoel Stanley 	writel(reg, priv->base + CRT_CTRL1);
2034f2a8f58SJoel Stanley 
2044f2a8f58SJoel Stanley 	return 0;
2054f2a8f58SJoel Stanley }
2064f2a8f58SJoel Stanley 
aspeed_gfx_disable_vblank(struct drm_simple_display_pipe * pipe)2074f2a8f58SJoel Stanley static void aspeed_gfx_disable_vblank(struct drm_simple_display_pipe *pipe)
2084f2a8f58SJoel Stanley {
2094f2a8f58SJoel Stanley 	struct aspeed_gfx *priv = drm_pipe_to_aspeed_gfx(pipe);
2104f2a8f58SJoel Stanley 	u32 reg = readl(priv->base + CRT_CTRL1);
2114f2a8f58SJoel Stanley 
2124f2a8f58SJoel Stanley 	reg &= ~CRT_CTRL_VERTICAL_INTR_EN;
2134f2a8f58SJoel Stanley 	writel(reg, priv->base + CRT_CTRL1);
2144f2a8f58SJoel Stanley 
2154f2a8f58SJoel Stanley 	/* Clear pending VBLANK IRQ */
2164f2a8f58SJoel Stanley 	writel(reg | CRT_CTRL_VERTICAL_INTR_STS, priv->base + CRT_CTRL1);
2174f2a8f58SJoel Stanley }
2184f2a8f58SJoel Stanley 
21995cbf02bSNishka Dasgupta static const struct drm_simple_display_pipe_funcs aspeed_gfx_funcs = {
2204f2a8f58SJoel Stanley 	.enable		= aspeed_gfx_pipe_enable,
2214f2a8f58SJoel Stanley 	.disable	= aspeed_gfx_pipe_disable,
2224f2a8f58SJoel Stanley 	.update		= aspeed_gfx_pipe_update,
2234f2a8f58SJoel Stanley 	.enable_vblank	= aspeed_gfx_enable_vblank,
2244f2a8f58SJoel Stanley 	.disable_vblank	= aspeed_gfx_disable_vblank,
2254f2a8f58SJoel Stanley };
2264f2a8f58SJoel Stanley 
2274f2a8f58SJoel Stanley static const uint32_t aspeed_gfx_formats[] = {
2284f2a8f58SJoel Stanley 	DRM_FORMAT_XRGB8888,
2294f2a8f58SJoel Stanley 	DRM_FORMAT_RGB565,
2304f2a8f58SJoel Stanley };
2314f2a8f58SJoel Stanley 
aspeed_gfx_create_pipe(struct drm_device * drm)2324f2a8f58SJoel Stanley int aspeed_gfx_create_pipe(struct drm_device *drm)
2334f2a8f58SJoel Stanley {
234cd829454SDaniel Vetter 	struct aspeed_gfx *priv = to_aspeed_gfx(drm);
2354f2a8f58SJoel Stanley 
2364f2a8f58SJoel Stanley 	return drm_simple_display_pipe_init(drm, &priv->pipe, &aspeed_gfx_funcs,
2374f2a8f58SJoel Stanley 					    aspeed_gfx_formats,
2384f2a8f58SJoel Stanley 					    ARRAY_SIZE(aspeed_gfx_formats),
2394f2a8f58SJoel Stanley 					    NULL,
2404f2a8f58SJoel Stanley 					    &priv->connector);
2414f2a8f58SJoel Stanley }
242