aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/arm/hdlcd_crtc.c
blob: 9a81467d72f4daa46e8db77b57d34a8581ffe919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (C) 2013,2014 ARM Limited
 * Author: Liviu Dudau <Liviu.Dudau@arm.com>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive
 * for more details.
 *
 *  Implementation of a CRTC class for the HDLCD driver.
 */

#include <linux/clk.h>
#include <drm/drmP.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_fb_cma_helper.h>
#include <drm/drm_gem_cma_helper.h>

#include "hdlcd_drv.h"
#include "hdlcd_regs.h"

/*
 * The HDLCD controller is a dumb RGB streamer that gets connected to
 * a single HDMI transmitter or in the case of the ARM Models it gets
 * emulated by the software that does the actual rendering.
 *
 */
static void hdlcd_crtc_destroy(struct drm_crtc *crtc)
{
	drm_crtc_cleanup(crtc);
}

void hdlcd_set_scanout(struct hdlcd_drm_private *hdlcd, bool wait)
{
	struct drm_framebuffer *fb = hdlcd->crtc.fb;
	struct hdlcd_bo *bo;
	unsigned int depth, bpp;
	dma_addr_t scanout_start;
	int ret;

	drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
	bo = hdlcd->bo;

	scanout_start = bo->dma_addr + fb->offsets[0] +
		(hdlcd->crtc.y * fb->pitches[0]) + (hdlcd->crtc.x * bpp/8);

	hdlcd_write(hdlcd, HDLCD_REG_FB_BASE, scanout_start);

	if (wait && hdlcd->dpms == DRM_MODE_DPMS_ON) {
		drm_vblank_get(fb->dev, 0);
		hdlcd->frame_completion.done = 0;
		do {
			ret = wait_for_completion_interruptible_timeout(&hdlcd->frame_completion,
							msecs_to_jiffies(50));
		} while (ret <= 0);
		drm_vblank_put(fb->dev, 0);
	} else {
		dev_info(fb->dev->dev, "%s: wait called with DPMS set to %d\n",
			__func__, hdlcd->dpms);
	}
}

static int hdlcd_crtc_page_flip(struct drm_crtc *crtc,
			struct drm_framebuffer *fb,
			struct drm_pending_vblank_event *event)
{
	struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);

	if (hdlcd->dpms == DRM_MODE_DPMS_ON) {
		/* don't schedule any page flipping if one is in progress */
		if (hdlcd->event)
			return -EBUSY;

		hdlcd->event = event;
		drm_vblank_get(crtc->dev, 0);
	}

	crtc->fb = fb;

	if (hdlcd->dpms == DRM_MODE_DPMS_ON) {
		hdlcd_set_scanout(hdlcd, true);
	} else {
		unsigned long flags;

		/* not active, update registers immediately */
		hdlcd_set_scanout(hdlcd, false);
		spin_lock_irqsave(&crtc->dev->event_lock, flags);
		if (event)
			drm_send_vblank_event(crtc->dev, 0, event);
		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
	}

	return 0;
}

static const struct drm_crtc_funcs hdlcd_crtc_funcs = {
	.destroy	= hdlcd_crtc_destroy,
	.set_config	= drm_crtc_helper_set_config,
	.page_flip	= hdlcd_crtc_page_flip,
};

static void hdlcd_crtc_dpms(struct drm_crtc *crtc, int mode)
{
	struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);

	hdlcd->dpms = mode;
	if (mode == DRM_MODE_DPMS_ON)
		hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 1);
	else
		hdlcd_write(hdlcd, HDLCD_REG_COMMAND, 0);
}

static bool hdlcd_crtc_mode_fixup(struct drm_crtc *crtc,
			const struct drm_display_mode *mode,
			struct drm_display_mode *adjusted_mode)
{
	return true;
}

static void hdlcd_crtc_prepare(struct drm_crtc *crtc)
{
	drm_vblank_pre_modeset(crtc->dev, 0);
	hdlcd_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
}

static void hdlcd_crtc_commit(struct drm_crtc *crtc)
{
	drm_vblank_post_modeset(crtc->dev, 0);
	hdlcd_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
}

static bool hdlcd_fb_mode_equal(struct drm_framebuffer *oldfb,
				struct drm_framebuffer *newfb)
{
	if (!oldfb || !newfb)
		return false;

	if (oldfb->pixel_format == newfb->pixel_format &&
		oldfb->width == newfb->width &&
		oldfb->height == newfb->height)
		return true;

	return false;
}

static int hdlcd_crtc_mode_set(struct drm_crtc *crtc,
			struct drm_display_mode *mode,
			struct drm_display_mode *adjusted_mode,
			int x, int y, struct drm_framebuffer *oldfb)
{
	struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);
	unsigned int depth, bpp, polarities;
	unsigned char red_width = 0, green_width = 0, blue_width = 0, alpha_width = 0;
	unsigned int default_color = 0x00000000;

#ifdef HDLCD_SHOW_UNDERRUN
	default_color = 0x00ff000000;
#endif

	/* This function gets called when the only change is the start of
	   the scanout buffer. Detect that and bail out early */
	if (hdlcd->initialised && hdlcd_fb_mode_equal(oldfb, crtc->fb)) {
		hdlcd_set_scanout(hdlcd, true);
		return 0;
	}

	/* Preset the number of bits per colour */
	drm_fb_get_bpp_depth(crtc->fb->pixel_format, &depth, &bpp);
	switch (depth) {
	case 32:
		alpha_width = 8;
	case 24:
	case 8:	 /* pseudocolor */
		red_width = 8; green_width = 8; blue_width = 8;
		break;
	case 16: /* 565 format */
		red_width = 5; green_width = 6; blue_width = 5;
		break;
	}

	/* switch to using the more useful bytes per pixel */
	bpp = (bpp + 7) / 8;

	polarities = HDLCD_POLARITY_DATAEN | HDLCD_POLARITY_DATA;

	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
		polarities |= HDLCD_POLARITY_HSYNC;
	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
		polarities |= HDLCD_POLARITY_VSYNC;

	/* Allow max number of outstanding requests and largest burst size */
	hdlcd_write(hdlcd, HDLCD_REG_BUS_OPTIONS,
		    HDLCD_BUS_MAX_OUTSTAND | HDLCD_BUS_BURST_16);

	hdlcd_write(hdlcd, HDLCD_REG_PIXEL_FORMAT, (bpp - 1) << 3);

	hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_LENGTH, crtc->fb->width * bpp);
	hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_COUNT, crtc->fb->height - 1);
	hdlcd_write(hdlcd, HDLCD_REG_FB_LINE_PITCH, crtc->fb->width * bpp);
	hdlcd_write(hdlcd, HDLCD_REG_V_BACK_PORCH,
				mode->vtotal - mode->vsync_end - 1);
	hdlcd_write(hdlcd, HDLCD_REG_V_FRONT_PORCH,
				mode->vsync_start - mode->vdisplay - 1);
	hdlcd_write(hdlcd, HDLCD_REG_V_SYNC,
				mode->vsync_end - mode->vsync_start - 1);
	hdlcd_write(hdlcd, HDLCD_REG_V_DATA, mode->vdisplay - 1);
	hdlcd_write(hdlcd, HDLCD_REG_H_BACK_PORCH,
				mode->htotal - mode->hsync_end - 1);
	hdlcd_write(hdlcd, HDLCD_REG_H_FRONT_PORCH,
				mode->hsync_start - mode->hdisplay - 1);
	hdlcd_write(hdlcd, HDLCD_REG_H_SYNC,
				mode->hsync_end - mode->hsync_start - 1);
	hdlcd_write(hdlcd, HDLCD_REG_H_DATA, mode->hdisplay - 1);
	hdlcd_write(hdlcd, HDLCD_REG_POLARITIES, polarities);

	/*
	 * The format of the HDLCD_REG_<color>_SELECT register is:
	 *   - bits[23:16] - default value for that color component
	 *   - bits[11:8]  - number of bits to extract for each color component
	 *   - bits[4:0]   - index of the lowest bit to extract
	 *
	 * The default color value is used when bits[11:8] read zero, when the
	 * pixel is outside the visible frame area or when there is a
	 * buffer underrun.
	 */
	hdlcd_write(hdlcd, HDLCD_REG_BLUE_SELECT, default_color |
		alpha_width |   /* offset */
		(blue_width & 0xf) << 8);
	hdlcd_write(hdlcd, HDLCD_REG_GREEN_SELECT, default_color |
		(blue_width + alpha_width) |  /* offset */
		((green_width & 0xf) << 8));
	hdlcd_write(hdlcd, HDLCD_REG_RED_SELECT, default_color |
		(blue_width + green_width + alpha_width) |  /* offset */
		((red_width & 0xf) << 8));

	clk_prepare(hdlcd->clk);
	clk_set_rate(hdlcd->clk, mode->clock * 1000);
	clk_enable(hdlcd->clk);

	hdlcd_set_scanout(hdlcd, false);
	hdlcd->initialised = true;

	return 0;
}

int hdlcd_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
			struct drm_framebuffer *oldfb)
{
	struct hdlcd_drm_private *hdlcd = crtc_to_hdlcd_priv(crtc);

	hdlcd_set_scanout(hdlcd, true);
	return 0;
}

static void hdlcd_crtc_load_lut(struct drm_crtc *crtc)
{
}

static const struct drm_crtc_helper_funcs hdlcd_crtc_helper_funcs = {
	.dpms		= hdlcd_crtc_dpms,
	.mode_fixup	= hdlcd_crtc_mode_fixup,
	.prepare	= hdlcd_crtc_prepare,
	.commit		= hdlcd_crtc_commit,
	.mode_set	= hdlcd_crtc_mode_set,
	.mode_set_base	= hdlcd_crtc_mode_set_base,
	.load_lut	= hdlcd_crtc_load_lut,
};

int hdlcd_setup_crtc(struct drm_device *dev)
{
	struct hdlcd_drm_private *hdlcd = dev->dev_private;
	int ret;

	drm_mode_config_init(dev);
	hdlcd_drm_mode_config_init(dev);

	ret = drm_crtc_init(dev, &hdlcd->crtc, &hdlcd_crtc_funcs);
	if (ret < 0)
		goto crtc_setup_err;

	drm_crtc_helper_add(&hdlcd->crtc, &hdlcd_crtc_helper_funcs);

	return 0;

crtc_setup_err:
	drm_mode_config_cleanup(dev);

	return ret;
}