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#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/ioport.h>
118c2ecf20Sopenharmony_ci#include <linux/of_address.h>
128c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
138c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
148c2ecf20Sopenharmony_ci#include <linux/regmap.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <video/videomode.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h>
198c2ecf20Sopenharmony_ci#include <drm/drm_crtc.h>
208c2ecf20Sopenharmony_ci#include <drm/drm_modes.h>
218c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
228c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h>
238c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "sun4i_backend.h"
268c2ecf20Sopenharmony_ci#include "sun4i_crtc.h"
278c2ecf20Sopenharmony_ci#include "sun4i_drv.h"
288c2ecf20Sopenharmony_ci#include "sunxi_engine.h"
298c2ecf20Sopenharmony_ci#include "sun4i_tcon.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * While this isn't really working in the DRM theory, in practice we
338c2ecf20Sopenharmony_ci * can only ever have one encoder per TCON since we have a mux in our
348c2ecf20Sopenharmony_ci * TCON.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistatic struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct drm_encoder *encoder;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	drm_for_each_encoder(encoder, crtc->dev)
418c2ecf20Sopenharmony_ci		if (encoder->crtc == crtc)
428c2ecf20Sopenharmony_ci			return encoder;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return NULL;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int sun4i_crtc_atomic_check(struct drm_crtc *crtc,
488c2ecf20Sopenharmony_ci				    struct drm_crtc_state *state)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
518c2ecf20Sopenharmony_ci	struct sunxi_engine *engine = scrtc->engine;
528c2ecf20Sopenharmony_ci	int ret = 0;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (engine && engine->ops && engine->ops->atomic_check)
558c2ecf20Sopenharmony_ci		ret = engine->ops->atomic_check(engine, state);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return ret;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
618c2ecf20Sopenharmony_ci				    struct drm_crtc_state *old_state)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
648c2ecf20Sopenharmony_ci	struct drm_device *dev = crtc->dev;
658c2ecf20Sopenharmony_ci	struct sunxi_engine *engine = scrtc->engine;
668c2ecf20Sopenharmony_ci	unsigned long flags;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (crtc->state->event) {
698c2ecf20Sopenharmony_ci		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		spin_lock_irqsave(&dev->event_lock, flags);
728c2ecf20Sopenharmony_ci		scrtc->event = crtc->state->event;
738c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dev->event_lock, flags);
748c2ecf20Sopenharmony_ci		crtc->state->event = NULL;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (engine->ops->atomic_begin)
788c2ecf20Sopenharmony_ci		engine->ops->atomic_begin(engine, old_state);
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
828c2ecf20Sopenharmony_ci				    struct drm_crtc_state *old_state)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
858c2ecf20Sopenharmony_ci	struct drm_pending_vblank_event *event = crtc->state->event;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	DRM_DEBUG_DRIVER("Committing plane changes\n");
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	sunxi_engine_commit(scrtc->engine);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (event) {
928c2ecf20Sopenharmony_ci		crtc->state->event = NULL;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		spin_lock_irq(&crtc->dev->event_lock);
958c2ecf20Sopenharmony_ci		if (drm_crtc_vblank_get(crtc) == 0)
968c2ecf20Sopenharmony_ci			drm_crtc_arm_vblank_event(crtc, event);
978c2ecf20Sopenharmony_ci		else
988c2ecf20Sopenharmony_ci			drm_crtc_send_vblank_event(crtc, event);
998c2ecf20Sopenharmony_ci		spin_unlock_irq(&crtc->dev->event_lock);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
1048c2ecf20Sopenharmony_ci				      struct drm_crtc_state *old_state)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
1078c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	DRM_DEBUG_DRIVER("Disabling the CRTC\n");
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	drm_crtc_vblank_off(crtc);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	sun4i_tcon_set_status(scrtc->tcon, encoder, false);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (crtc->state->event && !crtc->state->active) {
1168c2ecf20Sopenharmony_ci		spin_lock_irq(&crtc->dev->event_lock);
1178c2ecf20Sopenharmony_ci		drm_crtc_send_vblank_event(crtc, crtc->state->event);
1188c2ecf20Sopenharmony_ci		spin_unlock_irq(&crtc->dev->event_lock);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		crtc->state->event = NULL;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
1258c2ecf20Sopenharmony_ci				     struct drm_crtc_state *old_state)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
1288c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	DRM_DEBUG_DRIVER("Enabling the CRTC\n");
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	sun4i_tcon_set_status(scrtc->tcon, encoder, true);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	drm_crtc_vblank_on(crtc);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
1408c2ecf20Sopenharmony_ci	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
1418c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
1478c2ecf20Sopenharmony_ci	.atomic_check	= sun4i_crtc_atomic_check,
1488c2ecf20Sopenharmony_ci	.atomic_begin	= sun4i_crtc_atomic_begin,
1498c2ecf20Sopenharmony_ci	.atomic_flush	= sun4i_crtc_atomic_flush,
1508c2ecf20Sopenharmony_ci	.atomic_enable	= sun4i_crtc_atomic_enable,
1518c2ecf20Sopenharmony_ci	.atomic_disable	= sun4i_crtc_atomic_disable,
1528c2ecf20Sopenharmony_ci	.mode_set_nofb	= sun4i_crtc_mode_set_nofb,
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	sun4i_tcon_enable_vblank(scrtc->tcon, true);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	sun4i_tcon_enable_vblank(scrtc->tcon, false);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic const struct drm_crtc_funcs sun4i_crtc_funcs = {
1768c2ecf20Sopenharmony_ci	.atomic_destroy_state	= drm_atomic_helper_crtc_destroy_state,
1778c2ecf20Sopenharmony_ci	.atomic_duplicate_state	= drm_atomic_helper_crtc_duplicate_state,
1788c2ecf20Sopenharmony_ci	.destroy		= drm_crtc_cleanup,
1798c2ecf20Sopenharmony_ci	.page_flip		= drm_atomic_helper_page_flip,
1808c2ecf20Sopenharmony_ci	.reset			= drm_atomic_helper_crtc_reset,
1818c2ecf20Sopenharmony_ci	.set_config		= drm_atomic_helper_set_config,
1828c2ecf20Sopenharmony_ci	.enable_vblank		= sun4i_crtc_enable_vblank,
1838c2ecf20Sopenharmony_ci	.disable_vblank		= sun4i_crtc_disable_vblank,
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistruct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
1878c2ecf20Sopenharmony_ci				   struct sunxi_engine *engine,
1888c2ecf20Sopenharmony_ci				   struct sun4i_tcon *tcon)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct sun4i_crtc *scrtc;
1918c2ecf20Sopenharmony_ci	struct drm_plane **planes;
1928c2ecf20Sopenharmony_ci	struct drm_plane *primary = NULL, *cursor = NULL;
1938c2ecf20Sopenharmony_ci	int ret, i;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
1968c2ecf20Sopenharmony_ci	if (!scrtc)
1978c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1988c2ecf20Sopenharmony_ci	scrtc->engine = engine;
1998c2ecf20Sopenharmony_ci	scrtc->tcon = tcon;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/* Create our layers */
2028c2ecf20Sopenharmony_ci	planes = sunxi_engine_layers_init(drm, engine);
2038c2ecf20Sopenharmony_ci	if (IS_ERR(planes)) {
2048c2ecf20Sopenharmony_ci		dev_err(drm->dev, "Couldn't create the planes\n");
2058c2ecf20Sopenharmony_ci		return NULL;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* find primary and cursor planes for drm_crtc_init_with_planes */
2098c2ecf20Sopenharmony_ci	for (i = 0; planes[i]; i++) {
2108c2ecf20Sopenharmony_ci		struct drm_plane *plane = planes[i];
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		switch (plane->type) {
2138c2ecf20Sopenharmony_ci		case DRM_PLANE_TYPE_PRIMARY:
2148c2ecf20Sopenharmony_ci			primary = plane;
2158c2ecf20Sopenharmony_ci			break;
2168c2ecf20Sopenharmony_ci		case DRM_PLANE_TYPE_CURSOR:
2178c2ecf20Sopenharmony_ci			cursor = plane;
2188c2ecf20Sopenharmony_ci			break;
2198c2ecf20Sopenharmony_ci		default:
2208c2ecf20Sopenharmony_ci			break;
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
2258c2ecf20Sopenharmony_ci					primary,
2268c2ecf20Sopenharmony_ci					cursor,
2278c2ecf20Sopenharmony_ci					&sun4i_crtc_funcs,
2288c2ecf20Sopenharmony_ci					NULL);
2298c2ecf20Sopenharmony_ci	if (ret) {
2308c2ecf20Sopenharmony_ci		dev_err(drm->dev, "Couldn't init DRM CRTC\n");
2318c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* Set crtc.port to output port node of the tcon */
2378c2ecf20Sopenharmony_ci	scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
2388c2ecf20Sopenharmony_ci						   1);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	/* Set possible_crtcs to this crtc for overlay planes */
2418c2ecf20Sopenharmony_ci	for (i = 0; planes[i]; i++) {
2428c2ecf20Sopenharmony_ci		uint32_t possible_crtcs = drm_crtc_mask(&scrtc->crtc);
2438c2ecf20Sopenharmony_ci		struct drm_plane *plane = planes[i];
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci		if (plane->type == DRM_PLANE_TYPE_OVERLAY)
2468c2ecf20Sopenharmony_ci			plane->possible_crtcs = possible_crtcs;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return scrtc;
2508c2ecf20Sopenharmony_ci}
251