sun4i_crtc.c 6.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2015 Free Electrons
 * Copyright (C) 2015 NextThing Co
 *
 * Maxime Ripard <maxime.ripard@free-electrons.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

#include <drm/drmP.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_modes.h>

#include <linux/clk-provider.h>
#include <linux/ioport.h>
#include <linux/of_address.h>
22
#include <linux/of_graph.h>
23 24 25 26 27
#include <linux/of_irq.h>
#include <linux/regmap.h>

#include <video/videomode.h>

28
#include "sun4i_backend.h"
29 30
#include "sun4i_crtc.h"
#include "sun4i_drv.h"
31
#include "sunxi_engine.h"
32 33
#include "sun4i_tcon.h"

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * While this isn't really working in the DRM theory, in practice we
 * can only ever have one encoder per TCON since we have a mux in our
 * TCON.
 */
static struct drm_encoder *sun4i_crtc_get_encoder(struct drm_crtc *crtc)
{
	struct drm_encoder *encoder;

	drm_for_each_encoder(encoder, crtc->dev)
		if (encoder->crtc == crtc)
			return encoder;

	return NULL;
}

50 51 52 53 54 55 56 57 58 59 60 61 62
static int sun4i_crtc_atomic_check(struct drm_crtc *crtc,
				    struct drm_crtc_state *state)
{
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
	struct sunxi_engine *engine = scrtc->engine;
	int ret = 0;

	if (engine && engine->ops && engine->ops->atomic_check)
		ret = engine->ops->atomic_check(engine, state);

	return ret;
}

63 64 65 66 67
static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_state)
{
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
	struct drm_device *dev = crtc->dev;
68
	struct sunxi_engine *engine = scrtc->engine;
69 70 71 72 73 74 75 76 77
	unsigned long flags;

	if (crtc->state->event) {
		WARN_ON(drm_crtc_vblank_get(crtc) != 0);

		spin_lock_irqsave(&dev->event_lock, flags);
		scrtc->event = crtc->state->event;
		spin_unlock_irqrestore(&dev->event_lock, flags);
		crtc->state->event = NULL;
78 79 80 81
	}

	if (engine->ops->atomic_begin)
		engine->ops->atomic_begin(engine, old_state);
82 83 84 85 86 87
}

static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_state)
{
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
88
	struct drm_pending_vblank_event *event = crtc->state->event;
89 90 91

	DRM_DEBUG_DRIVER("Committing plane changes\n");

92
	sunxi_engine_commit(scrtc->engine);
93 94 95 96 97 98 99 100 101 102 103

	if (event) {
		crtc->state->event = NULL;

		spin_lock_irq(&crtc->dev->event_lock);
		if (drm_crtc_vblank_get(crtc) == 0)
			drm_crtc_arm_vblank_event(crtc, event);
		else
			drm_crtc_send_vblank_event(crtc, event);
		spin_unlock_irq(&crtc->dev->event_lock);
	}
104 105
}

106 107
static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
				      struct drm_crtc_state *old_state)
108
{
109
	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
110 111 112 113
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);

	DRM_DEBUG_DRIVER("Disabling the CRTC\n");

114
	sun4i_tcon_set_status(scrtc->tcon, encoder, false);
115 116 117 118 119 120 121 122

	if (crtc->state->event && !crtc->state->active) {
		spin_lock_irq(&crtc->dev->event_lock);
		drm_crtc_send_vblank_event(crtc, crtc->state->event);
		spin_unlock_irq(&crtc->dev->event_lock);

		crtc->state->event = NULL;
	}
123 124
}

125 126
static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
				     struct drm_crtc_state *old_state)
127
{
128
	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
129 130 131 132
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);

	DRM_DEBUG_DRIVER("Enabling the CRTC\n");

133
	sun4i_tcon_set_status(scrtc->tcon, encoder, true);
134 135
}

136 137 138 139 140 141 142 143 144
static void sun4i_crtc_mode_set_nofb(struct drm_crtc *crtc)
{
	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	struct drm_encoder *encoder = sun4i_crtc_get_encoder(crtc);
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);

	sun4i_tcon_mode_set(scrtc->tcon, encoder, mode);
}

145
static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
146
	.atomic_check	= sun4i_crtc_atomic_check,
147 148
	.atomic_begin	= sun4i_crtc_atomic_begin,
	.atomic_flush	= sun4i_crtc_atomic_flush,
149
	.atomic_enable	= sun4i_crtc_atomic_enable,
150
	.atomic_disable	= sun4i_crtc_atomic_disable,
151
	.mode_set_nofb	= sun4i_crtc_mode_set_nofb,
152 153
};

154 155 156 157 158 159
static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
{
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);

	DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);

160
	sun4i_tcon_enable_vblank(scrtc->tcon, true);
161 162 163 164 165 166 167 168 169 170

	return 0;
}

static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
{
	struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);

	DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);

171
	sun4i_tcon_enable_vblank(scrtc->tcon, false);
172 173
}

174 175 176 177 178 179 180
static const struct drm_crtc_funcs sun4i_crtc_funcs = {
	.atomic_destroy_state	= drm_atomic_helper_crtc_destroy_state,
	.atomic_duplicate_state	= drm_atomic_helper_crtc_duplicate_state,
	.destroy		= drm_crtc_cleanup,
	.page_flip		= drm_atomic_helper_page_flip,
	.reset			= drm_atomic_helper_crtc_reset,
	.set_config		= drm_atomic_helper_set_config,
181 182
	.enable_vblank		= sun4i_crtc_enable_vblank,
	.disable_vblank		= sun4i_crtc_disable_vblank,
183 184
};

185
struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
186
				   struct sunxi_engine *engine,
187
				   struct sun4i_tcon *tcon)
188 189
{
	struct sun4i_crtc *scrtc;
190
	struct drm_plane **planes;
191 192
	struct drm_plane *primary = NULL, *cursor = NULL;
	int ret, i;
193 194 195

	scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
	if (!scrtc)
196
		return ERR_PTR(-ENOMEM);
197
	scrtc->engine = engine;
198
	scrtc->tcon = tcon;
199

200
	/* Create our layers */
201
	planes = sunxi_engine_layers_init(drm, engine);
202
	if (IS_ERR(planes)) {
203
		dev_err(drm->dev, "Couldn't create the planes\n");
204 205 206 207
		return NULL;
	}

	/* find primary and cursor planes for drm_crtc_init_with_planes */
208 209
	for (i = 0; planes[i]; i++) {
		struct drm_plane *plane = planes[i];
210

211
		switch (plane->type) {
212
		case DRM_PLANE_TYPE_PRIMARY:
213
			primary = plane;
214 215
			break;
		case DRM_PLANE_TYPE_CURSOR:
216
			cursor = plane;
217 218 219 220
			break;
		default:
			break;
		}
221 222
	}

223
	ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
224 225
					primary,
					cursor,
226 227 228 229
					&sun4i_crtc_funcs,
					NULL);
	if (ret) {
		dev_err(drm->dev, "Couldn't init DRM CRTC\n");
230
		return ERR_PTR(ret);
231 232 233 234
	}

	drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);

235
	/* Set crtc.port to output port node of the tcon */
236
	scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
237 238
						   1);

239
	/* Set possible_crtcs to this crtc for overlay planes */
240
	for (i = 0; planes[i]; i++) {
241
		uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
242
		struct drm_plane *plane = planes[i];
243

244 245
		if (plane->type == DRM_PLANE_TYPE_OVERLAY)
			plane->possible_crtcs = possible_crtcs;
246 247
	}

248 249
	return scrtc;
}