xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_txp.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2018 Broadcom
4  *
5  * Authors:
6  *	Eric Anholt <eric@anholt.net>
7  *	Boris Brezillon <boris.brezillon@bootlin.com>
8  */
9 
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_fb_cma_helper.h>
12 #include <drm/drm_edid.h>
13 #include <drm/drm_panel.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/drm_writeback.h>
16 #include <linux/clk.h>
17 #include <linux/component.h>
18 #include <linux/of_graph.h>
19 #include <linux/of_platform.h>
20 #include <linux/pm_runtime.h>
21 
22 #include "vc4_drv.h"
23 #include "vc4_regs.h"
24 
25 /* Base address of the output.  Raster formats must be 4-byte aligned,
26  * T and LT must be 16-byte aligned or maybe utile-aligned (docs are
27  * inconsistent, but probably utile).
28  */
29 #define TXP_DST_PTR		0x00
30 
31 /* Pitch in bytes for raster images, 16-byte aligned.  For tiled, it's
32  * the width in tiles.
33  */
34 #define TXP_DST_PITCH		0x04
35 /* For T-tiled imgaes, DST_PITCH should be the number of tiles wide,
36  * shifted up.
37  */
38 # define TXP_T_TILE_WIDTH_SHIFT		7
39 /* For LT-tiled images, DST_PITCH should be the number of utiles wide,
40  * shifted up.
41  */
42 # define TXP_LT_TILE_WIDTH_SHIFT	4
43 
44 /* Pre-rotation width/height of the image.  Must match HVS config.
45  *
46  * If TFORMAT and 32-bit, limit is 1920 for 32-bit and 3840 to 16-bit
47  * and width/height must be tile or utile-aligned as appropriate.  If
48  * transposing (rotating), width is limited to 1920.
49  *
50  * Height is limited to various numbers between 4088 and 4095.  I'd
51  * just use 4088 to be safe.
52  */
53 #define TXP_DIM			0x08
54 # define TXP_HEIGHT_SHIFT		16
55 # define TXP_HEIGHT_MASK		GENMASK(31, 16)
56 # define TXP_WIDTH_SHIFT		0
57 # define TXP_WIDTH_MASK			GENMASK(15, 0)
58 
59 #define TXP_DST_CTRL		0x0c
60 /* These bits are set to 0x54 */
61 #define TXP_PILOT_SHIFT			24
62 #define TXP_PILOT_MASK			GENMASK(31, 24)
63 /* Bits 22-23 are set to 0x01 */
64 #define TXP_VERSION_SHIFT		22
65 #define TXP_VERSION_MASK		GENMASK(23, 22)
66 
67 /* Powers down the internal memory. */
68 # define TXP_POWERDOWN			BIT(21)
69 
70 /* Enables storing the alpha component in 8888/4444, instead of
71  * filling with ~ALPHA_INVERT.
72  */
73 # define TXP_ALPHA_ENABLE		BIT(20)
74 
75 /* 4 bits, each enables stores for a channel in each set of 4 bytes.
76  * Set to 0xf for normal operation.
77  */
78 # define TXP_BYTE_ENABLE_SHIFT		16
79 # define TXP_BYTE_ENABLE_MASK		GENMASK(19, 16)
80 
81 /* Debug: Generate VSTART again at EOF. */
82 # define TXP_VSTART_AT_EOF		BIT(15)
83 
84 /* Debug: Terminate the current frame immediately.  Stops AXI
85  * writes.
86  */
87 # define TXP_ABORT			BIT(14)
88 
89 # define TXP_DITHER			BIT(13)
90 
91 /* Inverts alpha if TXP_ALPHA_ENABLE, chooses fill value for
92  * !TXP_ALPHA_ENABLE.
93  */
94 # define TXP_ALPHA_INVERT		BIT(12)
95 
96 /* Note: I've listed the channels here in high bit (in byte 3/2/1) to
97  * low bit (in byte 0) order.
98  */
99 # define TXP_FORMAT_SHIFT		8
100 # define TXP_FORMAT_MASK		GENMASK(11, 8)
101 # define TXP_FORMAT_ABGR4444		0
102 # define TXP_FORMAT_ARGB4444		1
103 # define TXP_FORMAT_BGRA4444		2
104 # define TXP_FORMAT_RGBA4444		3
105 # define TXP_FORMAT_BGR565		6
106 # define TXP_FORMAT_RGB565		7
107 /* 888s are non-rotated, raster-only */
108 # define TXP_FORMAT_BGR888		8
109 # define TXP_FORMAT_RGB888		9
110 # define TXP_FORMAT_ABGR8888		12
111 # define TXP_FORMAT_ARGB8888		13
112 # define TXP_FORMAT_BGRA8888		14
113 # define TXP_FORMAT_RGBA8888		15
114 
115 /* If TFORMAT is set, generates LT instead of T format. */
116 # define TXP_LINEAR_UTILE		BIT(7)
117 
118 /* Rotate output by 90 degrees. */
119 # define TXP_TRANSPOSE			BIT(6)
120 
121 /* Generate a tiled format for V3D. */
122 # define TXP_TFORMAT			BIT(5)
123 
124 /* Generates some undefined test mode output. */
125 # define TXP_TEST_MODE			BIT(4)
126 
127 /* Request odd field from HVS. */
128 # define TXP_FIELD			BIT(3)
129 
130 /* Raise interrupt when idle. */
131 # define TXP_EI				BIT(2)
132 
133 /* Set when generating a frame, clears when idle. */
134 # define TXP_BUSY			BIT(1)
135 
136 /* Starts a frame.  Self-clearing. */
137 # define TXP_GO				BIT(0)
138 
139 /* Number of lines received and committed to memory. */
140 #define TXP_PROGRESS		0x10
141 
142 #define TXP_READ(offset) readl(txp->regs + (offset))
143 #define TXP_WRITE(offset, val) writel(val, txp->regs + (offset))
144 
145 struct vc4_txp {
146 	struct platform_device *pdev;
147 
148 	struct drm_writeback_connector connector;
149 
150 	void __iomem *regs;
151 	struct debugfs_regset32 regset;
152 };
153 
154 static inline struct vc4_txp *encoder_to_vc4_txp(struct drm_encoder *encoder)
155 {
156 	return container_of(encoder, struct vc4_txp, connector.encoder);
157 }
158 
159 static inline struct vc4_txp *connector_to_vc4_txp(struct drm_connector *conn)
160 {
161 	return container_of(conn, struct vc4_txp, connector.base);
162 }
163 
164 static const struct debugfs_reg32 txp_regs[] = {
165 	VC4_REG32(TXP_DST_PTR),
166 	VC4_REG32(TXP_DST_PITCH),
167 	VC4_REG32(TXP_DIM),
168 	VC4_REG32(TXP_DST_CTRL),
169 	VC4_REG32(TXP_PROGRESS),
170 };
171 
172 static int vc4_txp_connector_get_modes(struct drm_connector *connector)
173 {
174 	struct drm_device *dev = connector->dev;
175 
176 	return drm_add_modes_noedid(connector, dev->mode_config.max_width,
177 				    dev->mode_config.max_height);
178 }
179 
180 static enum drm_mode_status
181 vc4_txp_connector_mode_valid(struct drm_connector *connector,
182 			     struct drm_display_mode *mode)
183 {
184 	struct drm_device *dev = connector->dev;
185 	struct drm_mode_config *mode_config = &dev->mode_config;
186 	int w = mode->hdisplay, h = mode->vdisplay;
187 
188 	if (w < mode_config->min_width || w > mode_config->max_width)
189 		return MODE_BAD_HVALUE;
190 
191 	if (h < mode_config->min_height || h > mode_config->max_height)
192 		return MODE_BAD_VVALUE;
193 
194 	return MODE_OK;
195 }
196 
197 static const u32 drm_fmts[] = {
198 	DRM_FORMAT_RGB888,
199 	DRM_FORMAT_BGR888,
200 	DRM_FORMAT_XRGB8888,
201 	DRM_FORMAT_XBGR8888,
202 	DRM_FORMAT_ARGB8888,
203 	DRM_FORMAT_ABGR8888,
204 	DRM_FORMAT_RGBX8888,
205 	DRM_FORMAT_BGRX8888,
206 	DRM_FORMAT_RGBA8888,
207 	DRM_FORMAT_BGRA8888,
208 };
209 
210 static const u32 txp_fmts[] = {
211 	TXP_FORMAT_RGB888,
212 	TXP_FORMAT_BGR888,
213 	TXP_FORMAT_ARGB8888,
214 	TXP_FORMAT_ABGR8888,
215 	TXP_FORMAT_ARGB8888,
216 	TXP_FORMAT_ABGR8888,
217 	TXP_FORMAT_RGBA8888,
218 	TXP_FORMAT_BGRA8888,
219 	TXP_FORMAT_RGBA8888,
220 	TXP_FORMAT_BGRA8888,
221 };
222 
223 static int vc4_txp_connector_atomic_check(struct drm_connector *conn,
224 					  struct drm_atomic_state *state)
225 {
226 	struct drm_connector_state *conn_state;
227 	struct drm_crtc_state *crtc_state;
228 	struct drm_framebuffer *fb;
229 	int i;
230 
231 	conn_state = drm_atomic_get_new_connector_state(state, conn);
232 	if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
233 		return 0;
234 
235 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
236 
237 	fb = conn_state->writeback_job->fb;
238 	if (fb->width != crtc_state->mode.hdisplay ||
239 	    fb->height != crtc_state->mode.vdisplay) {
240 		DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
241 			      fb->width, fb->height);
242 		return -EINVAL;
243 	}
244 
245 	for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
246 		if (fb->format->format == drm_fmts[i])
247 			break;
248 	}
249 
250 	if (i == ARRAY_SIZE(drm_fmts))
251 		return -EINVAL;
252 
253 	/* Pitch must be aligned on 16 bytes. */
254 	if (fb->pitches[0] & GENMASK(3, 0))
255 		return -EINVAL;
256 
257 	vc4_crtc_txp_armed(crtc_state);
258 
259 	return 0;
260 }
261 
262 static void vc4_txp_connector_atomic_commit(struct drm_connector *conn,
263 					struct drm_connector_state *conn_state)
264 {
265 	struct vc4_txp *txp = connector_to_vc4_txp(conn);
266 	struct drm_gem_cma_object *gem;
267 	struct drm_display_mode *mode;
268 	struct drm_framebuffer *fb;
269 	u32 ctrl;
270 	int i;
271 
272 	if (WARN_ON(!conn_state->writeback_job ||
273 		    !conn_state->writeback_job->fb))
274 		return;
275 
276 	mode = &conn_state->crtc->state->adjusted_mode;
277 	fb = conn_state->writeback_job->fb;
278 
279 	for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
280 		if (fb->format->format == drm_fmts[i])
281 			break;
282 	}
283 
284 	if (WARN_ON(i == ARRAY_SIZE(drm_fmts)))
285 		return;
286 
287 	ctrl = TXP_GO | TXP_VSTART_AT_EOF | TXP_EI |
288 	       VC4_SET_FIELD(0xf, TXP_BYTE_ENABLE) |
289 	       VC4_SET_FIELD(txp_fmts[i], TXP_FORMAT);
290 
291 	if (fb->format->has_alpha)
292 		ctrl |= TXP_ALPHA_ENABLE;
293 
294 	gem = drm_fb_cma_get_gem_obj(fb, 0);
295 	TXP_WRITE(TXP_DST_PTR, gem->paddr + fb->offsets[0]);
296 	TXP_WRITE(TXP_DST_PITCH, fb->pitches[0]);
297 	TXP_WRITE(TXP_DIM,
298 		  VC4_SET_FIELD(mode->hdisplay, TXP_WIDTH) |
299 		  VC4_SET_FIELD(mode->vdisplay, TXP_HEIGHT));
300 
301 	TXP_WRITE(TXP_DST_CTRL, ctrl);
302 
303 	drm_writeback_queue_job(&txp->connector, conn_state);
304 }
305 
306 static const struct drm_connector_helper_funcs vc4_txp_connector_helper_funcs = {
307 	.get_modes = vc4_txp_connector_get_modes,
308 	.mode_valid = vc4_txp_connector_mode_valid,
309 	.atomic_check = vc4_txp_connector_atomic_check,
310 	.atomic_commit = vc4_txp_connector_atomic_commit,
311 };
312 
313 static enum drm_connector_status
314 vc4_txp_connector_detect(struct drm_connector *connector, bool force)
315 {
316 	return connector_status_connected;
317 }
318 
319 static void vc4_txp_connector_destroy(struct drm_connector *connector)
320 {
321 	drm_connector_unregister(connector);
322 	drm_connector_cleanup(connector);
323 }
324 
325 static const struct drm_connector_funcs vc4_txp_connector_funcs = {
326 	.detect = vc4_txp_connector_detect,
327 	.fill_modes = drm_helper_probe_single_connector_modes,
328 	.destroy = vc4_txp_connector_destroy,
329 	.reset = drm_atomic_helper_connector_reset,
330 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
331 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
332 };
333 
334 static void vc4_txp_encoder_disable(struct drm_encoder *encoder)
335 {
336 	struct vc4_txp *txp = encoder_to_vc4_txp(encoder);
337 
338 	if (TXP_READ(TXP_DST_CTRL) & TXP_BUSY) {
339 		unsigned long timeout = jiffies + msecs_to_jiffies(1000);
340 
341 		TXP_WRITE(TXP_DST_CTRL, TXP_ABORT);
342 
343 		while (TXP_READ(TXP_DST_CTRL) & TXP_BUSY &&
344 		       time_before(jiffies, timeout))
345 			;
346 
347 		WARN_ON(TXP_READ(TXP_DST_CTRL) & TXP_BUSY);
348 	}
349 
350 	TXP_WRITE(TXP_DST_CTRL, TXP_POWERDOWN);
351 }
352 
353 static const struct drm_encoder_helper_funcs vc4_txp_encoder_helper_funcs = {
354 	.disable = vc4_txp_encoder_disable,
355 };
356 
357 static irqreturn_t vc4_txp_interrupt(int irq, void *data)
358 {
359 	struct vc4_txp *txp = data;
360 
361 	TXP_WRITE(TXP_DST_CTRL, TXP_READ(TXP_DST_CTRL) & ~TXP_EI);
362 	vc4_crtc_handle_vblank(to_vc4_crtc(txp->connector.base.state->crtc));
363 	drm_writeback_signal_completion(&txp->connector, 0);
364 
365 	return IRQ_HANDLED;
366 }
367 
368 static int vc4_txp_bind(struct device *dev, struct device *master, void *data)
369 {
370 	struct platform_device *pdev = to_platform_device(dev);
371 	struct drm_device *drm = dev_get_drvdata(master);
372 	struct vc4_dev *vc4 = to_vc4_dev(drm);
373 	struct vc4_txp *txp;
374 	int ret, irq;
375 
376 	irq = platform_get_irq(pdev, 0);
377 	if (irq < 0)
378 		return irq;
379 
380 	txp = devm_kzalloc(dev, sizeof(*txp), GFP_KERNEL);
381 	if (!txp)
382 		return -ENOMEM;
383 
384 	txp->pdev = pdev;
385 
386 	txp->regs = vc4_ioremap_regs(pdev, 0);
387 	if (IS_ERR(txp->regs))
388 		return PTR_ERR(txp->regs);
389 	txp->regset.base = txp->regs;
390 	txp->regset.regs = txp_regs;
391 	txp->regset.nregs = ARRAY_SIZE(txp_regs);
392 
393 	drm_connector_helper_add(&txp->connector.base,
394 				 &vc4_txp_connector_helper_funcs);
395 	ret = drm_writeback_connector_init(drm, &txp->connector,
396 					   &vc4_txp_connector_funcs,
397 					   &vc4_txp_encoder_helper_funcs,
398 					   drm_fmts, ARRAY_SIZE(drm_fmts));
399 	if (ret)
400 		return ret;
401 
402 	ret = devm_request_irq(dev, irq, vc4_txp_interrupt, 0,
403 			       dev_name(dev), txp);
404 	if (ret)
405 		return ret;
406 
407 	dev_set_drvdata(dev, txp);
408 	vc4->txp = txp;
409 
410 	vc4_debugfs_add_regset32(drm, "txp_regs", &txp->regset);
411 
412 	return 0;
413 }
414 
415 static void vc4_txp_unbind(struct device *dev, struct device *master,
416 			   void *data)
417 {
418 	struct drm_device *drm = dev_get_drvdata(master);
419 	struct vc4_dev *vc4 = to_vc4_dev(drm);
420 	struct vc4_txp *txp = dev_get_drvdata(dev);
421 
422 	vc4_txp_connector_destroy(&txp->connector.base);
423 
424 	vc4->txp = NULL;
425 }
426 
427 static const struct component_ops vc4_txp_ops = {
428 	.bind   = vc4_txp_bind,
429 	.unbind = vc4_txp_unbind,
430 };
431 
432 static int vc4_txp_probe(struct platform_device *pdev)
433 {
434 	return component_add(&pdev->dev, &vc4_txp_ops);
435 }
436 
437 static int vc4_txp_remove(struct platform_device *pdev)
438 {
439 	component_del(&pdev->dev, &vc4_txp_ops);
440 	return 0;
441 }
442 
443 static const struct of_device_id vc4_txp_dt_match[] = {
444 	{ .compatible = "brcm,bcm2835-txp" },
445 	{ /* sentinel */ },
446 };
447 
448 struct platform_driver vc4_txp_driver = {
449 	.probe = vc4_txp_probe,
450 	.remove = vc4_txp_remove,
451 	.driver = {
452 		.name = "vc4_txp",
453 		.of_match_table = vc4_txp_dt_match,
454 	},
455 };
456