xref: /openbmc/linux/drivers/gpu/drm/pl111/pl111_drv.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
4  *
5  * Parts of this file were based on sources as follows:
6  *
7  * Copyright (c) 2006-2008 Intel Corporation
8  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
9  * Copyright (C) 2011 Texas Instruments
10  */
11 
12 /**
13  * DOC: ARM PrimeCell PL111 CLCD Driver
14  *
15  * The PL111 is a simple LCD controller that can support TFT and STN
16  * displays.  This driver exposes a standard KMS interface for them.
17  *
18  * This driver uses the same Device Tree binding as the fbdev CLCD
19  * driver.  While the fbdev driver supports panels that may be
20  * connected to the CLCD internally to the CLCD driver, in DRM the
21  * panels get split out to drivers/gpu/drm/panels/.  This means that,
22  * in converting from using fbdev to using DRM, you also need to write
23  * a panel driver (which may be as simple as an entry in
24  * panel-simple.c).
25  *
26  * The driver currently doesn't expose the cursor.  The DRM API for
27  * cursors requires support for 64x64 ARGB8888 cursor images, while
28  * the hardware can only support 64x64 monochrome with masking
29  * cursors.  While one could imagine trying to hack something together
30  * to look at the ARGB8888 and program reasonable in monochrome, we
31  * just don't expose the cursor at all instead, and leave cursor
32  * support to the X11 software cursor layer.
33  *
34  * TODO:
35  *
36  * - Fix race between setting plane base address and getting IRQ for
37  *   vsync firing the pageflip completion.
38  *
39  * - Use the "max-memory-bandwidth" DT property to filter the
40  *   supported formats.
41  *
42  * - Read back hardware state at boot to skip reprogramming the
43  *   hardware when doing a no-op modeset.
44  *
45  * - Use the CLKSEL bit to support switching between the two external
46  *   clock parents.
47  */
48 
49 #include <linux/amba/bus.h>
50 #include <linux/amba/clcd-regs.h>
51 #include <linux/version.h>
52 #include <linux/shmem_fs.h>
53 #include <linux/dma-buf.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/of.h>
57 #include <linux/of_graph.h>
58 #include <linux/of_reserved_mem.h>
59 
60 #include <drm/drmP.h>
61 #include <drm/drm_atomic_helper.h>
62 #include <drm/drm_bridge.h>
63 #include <drm/drm_fb_cma_helper.h>
64 #include <drm/drm_fb_helper.h>
65 #include <drm/drm_gem_cma_helper.h>
66 #include <drm/drm_gem_framebuffer_helper.h>
67 #include <drm/drm_of.h>
68 #include <drm/drm_panel.h>
69 #include <drm/drm_probe_helper.h>
70 
71 #include "pl111_drm.h"
72 #include "pl111_versatile.h"
73 #include "pl111_nomadik.h"
74 
75 #define DRIVER_DESC      "DRM module for PL111"
76 
77 static const struct drm_mode_config_funcs mode_config_funcs = {
78 	.fb_create = drm_gem_fb_create,
79 	.atomic_check = drm_atomic_helper_check,
80 	.atomic_commit = drm_atomic_helper_commit,
81 };
82 
83 static int pl111_modeset_init(struct drm_device *dev)
84 {
85 	struct drm_mode_config *mode_config;
86 	struct pl111_drm_dev_private *priv = dev->dev_private;
87 	struct device_node *np = dev->dev->of_node;
88 	struct device_node *remote;
89 	struct drm_panel *panel = NULL;
90 	struct drm_bridge *bridge = NULL;
91 	bool defer = false;
92 	int ret = 0;
93 	int i;
94 
95 	drm_mode_config_init(dev);
96 	mode_config = &dev->mode_config;
97 	mode_config->funcs = &mode_config_funcs;
98 	mode_config->min_width = 1;
99 	mode_config->max_width = 1024;
100 	mode_config->min_height = 1;
101 	mode_config->max_height = 768;
102 
103 	i = 0;
104 	for_each_endpoint_of_node(np, remote) {
105 		struct drm_panel *tmp_panel;
106 		struct drm_bridge *tmp_bridge;
107 
108 		dev_dbg(dev->dev, "checking endpoint %d\n", i);
109 
110 		ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
111 						  0, i,
112 						  &tmp_panel,
113 						  &tmp_bridge);
114 		if (ret) {
115 			if (ret == -EPROBE_DEFER) {
116 				/*
117 				 * Something deferred, but that is often just
118 				 * another way of saying -ENODEV, but let's
119 				 * cast a vote for later deferral.
120 				 */
121 				defer = true;
122 			} else if (ret != -ENODEV) {
123 				/* Continue, maybe something else is working */
124 				dev_err(dev->dev,
125 					"endpoint %d returns %d\n", i, ret);
126 			}
127 		}
128 
129 		if (tmp_panel) {
130 			dev_info(dev->dev,
131 				 "found panel on endpoint %d\n", i);
132 			panel = tmp_panel;
133 		}
134 		if (tmp_bridge) {
135 			dev_info(dev->dev,
136 				 "found bridge on endpoint %d\n", i);
137 			bridge = tmp_bridge;
138 		}
139 
140 		i++;
141 	}
142 
143 	/*
144 	 * If we can't find neither panel nor bridge on any of the
145 	 * endpoints, and any of them retured -EPROBE_DEFER, then
146 	 * let's defer this driver too.
147 	 */
148 	if ((!panel && !bridge) && defer)
149 		return -EPROBE_DEFER;
150 
151 	if (panel) {
152 		bridge = drm_panel_bridge_add(panel,
153 					      DRM_MODE_CONNECTOR_Unknown);
154 		if (IS_ERR(bridge)) {
155 			ret = PTR_ERR(bridge);
156 			goto out_config;
157 		}
158 	} else if (bridge) {
159 		dev_info(dev->dev, "Using non-panel bridge\n");
160 	} else {
161 		dev_err(dev->dev, "No bridge, exiting\n");
162 		return -ENODEV;
163 	}
164 
165 	priv->bridge = bridge;
166 	if (panel) {
167 		priv->panel = panel;
168 		priv->connector = panel->connector;
169 	}
170 
171 	ret = pl111_display_init(dev);
172 	if (ret != 0) {
173 		dev_err(dev->dev, "Failed to init display\n");
174 		goto out_bridge;
175 	}
176 
177 	ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
178 						    bridge);
179 	if (ret)
180 		return ret;
181 
182 	if (!priv->variant->broken_vblank) {
183 		ret = drm_vblank_init(dev, 1);
184 		if (ret != 0) {
185 			dev_err(dev->dev, "Failed to init vblank\n");
186 			goto out_bridge;
187 		}
188 	}
189 
190 	drm_mode_config_reset(dev);
191 
192 	drm_kms_helper_poll_init(dev);
193 
194 	goto finish;
195 
196 out_bridge:
197 	if (panel)
198 		drm_panel_bridge_remove(bridge);
199 out_config:
200 	drm_mode_config_cleanup(dev);
201 finish:
202 	return ret;
203 }
204 
205 static struct drm_gem_object *
206 pl111_gem_import_sg_table(struct drm_device *dev,
207 			  struct dma_buf_attachment *attach,
208 			  struct sg_table *sgt)
209 {
210 	struct pl111_drm_dev_private *priv = dev->dev_private;
211 
212 	/*
213 	 * When using device-specific reserved memory we can't import
214 	 * DMA buffers: those are passed by reference in any global
215 	 * memory and we can only handle a specific range of memory.
216 	 */
217 	if (priv->use_device_memory)
218 		return ERR_PTR(-EINVAL);
219 
220 	return drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
221 }
222 
223 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
224 
225 static struct drm_driver pl111_drm_driver = {
226 	.driver_features =
227 		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
228 	.ioctls = NULL,
229 	.fops = &drm_fops,
230 	.name = "pl111",
231 	.desc = DRIVER_DESC,
232 	.date = "20170317",
233 	.major = 1,
234 	.minor = 0,
235 	.patchlevel = 0,
236 	.dumb_create = drm_gem_cma_dumb_create,
237 	.gem_free_object_unlocked = drm_gem_cma_free_object,
238 	.gem_vm_ops = &drm_gem_cma_vm_ops,
239 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
240 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
241 	.gem_prime_import = drm_gem_prime_import,
242 	.gem_prime_import_sg_table = pl111_gem_import_sg_table,
243 	.gem_prime_export = drm_gem_prime_export,
244 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
245 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
246 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
247 
248 #if defined(CONFIG_DEBUG_FS)
249 	.debugfs_init = pl111_debugfs_init,
250 #endif
251 };
252 
253 static int pl111_amba_probe(struct amba_device *amba_dev,
254 			    const struct amba_id *id)
255 {
256 	struct device *dev = &amba_dev->dev;
257 	struct pl111_drm_dev_private *priv;
258 	const struct pl111_variant_data *variant = id->data;
259 	struct drm_device *drm;
260 	int ret;
261 
262 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
263 	if (!priv)
264 		return -ENOMEM;
265 
266 	drm = drm_dev_alloc(&pl111_drm_driver, dev);
267 	if (IS_ERR(drm))
268 		return PTR_ERR(drm);
269 	amba_set_drvdata(amba_dev, drm);
270 	priv->drm = drm;
271 	drm->dev_private = priv;
272 	priv->variant = variant;
273 
274 	ret = of_reserved_mem_device_init(dev);
275 	if (!ret) {
276 		dev_info(dev, "using device-specific reserved memory\n");
277 		priv->use_device_memory = true;
278 	}
279 
280 	if (of_property_read_u32(dev->of_node, "max-memory-bandwidth",
281 				 &priv->memory_bw)) {
282 		dev_info(dev, "no max memory bandwidth specified, assume unlimited\n");
283 		priv->memory_bw = 0;
284 	}
285 
286 	/* The two main variants swap this register */
287 	if (variant->is_pl110 || variant->is_lcdc) {
288 		priv->ienb = CLCD_PL110_IENB;
289 		priv->ctrl = CLCD_PL110_CNTL;
290 	} else {
291 		priv->ienb = CLCD_PL111_IENB;
292 		priv->ctrl = CLCD_PL111_CNTL;
293 	}
294 
295 	priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
296 	if (IS_ERR(priv->regs)) {
297 		dev_err(dev, "%s failed mmio\n", __func__);
298 		ret = PTR_ERR(priv->regs);
299 		goto dev_put;
300 	}
301 
302 	/* This may override some variant settings */
303 	ret = pl111_versatile_init(dev, priv);
304 	if (ret)
305 		goto dev_put;
306 
307 	pl111_nomadik_init(dev);
308 
309 	/* turn off interrupts before requesting the irq */
310 	writel(0, priv->regs + priv->ienb);
311 
312 	ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
313 			       variant->name, priv);
314 	if (ret != 0) {
315 		dev_err(dev, "%s failed irq %d\n", __func__, ret);
316 		return ret;
317 	}
318 
319 	ret = pl111_modeset_init(drm);
320 	if (ret != 0)
321 		goto dev_put;
322 
323 	ret = drm_dev_register(drm, 0);
324 	if (ret < 0)
325 		goto dev_put;
326 
327 	drm_fbdev_generic_setup(drm, priv->variant->fb_bpp);
328 
329 	return 0;
330 
331 dev_put:
332 	drm_dev_put(drm);
333 	of_reserved_mem_device_release(dev);
334 
335 	return ret;
336 }
337 
338 static int pl111_amba_remove(struct amba_device *amba_dev)
339 {
340 	struct device *dev = &amba_dev->dev;
341 	struct drm_device *drm = amba_get_drvdata(amba_dev);
342 	struct pl111_drm_dev_private *priv = drm->dev_private;
343 
344 	drm_dev_unregister(drm);
345 	if (priv->panel)
346 		drm_panel_bridge_remove(priv->bridge);
347 	drm_mode_config_cleanup(drm);
348 	drm_dev_put(drm);
349 	of_reserved_mem_device_release(dev);
350 
351 	return 0;
352 }
353 
354 /*
355  * This early variant lacks the 565 and 444 pixel formats.
356  */
357 static const u32 pl110_pixel_formats[] = {
358 	DRM_FORMAT_ABGR8888,
359 	DRM_FORMAT_XBGR8888,
360 	DRM_FORMAT_ARGB8888,
361 	DRM_FORMAT_XRGB8888,
362 	DRM_FORMAT_ABGR1555,
363 	DRM_FORMAT_XBGR1555,
364 	DRM_FORMAT_ARGB1555,
365 	DRM_FORMAT_XRGB1555,
366 };
367 
368 static const struct pl111_variant_data pl110_variant = {
369 	.name = "PL110",
370 	.is_pl110 = true,
371 	.formats = pl110_pixel_formats,
372 	.nformats = ARRAY_SIZE(pl110_pixel_formats),
373 	.fb_bpp = 16,
374 };
375 
376 /* RealView, Versatile Express etc use this modern variant */
377 static const u32 pl111_pixel_formats[] = {
378 	DRM_FORMAT_ABGR8888,
379 	DRM_FORMAT_XBGR8888,
380 	DRM_FORMAT_ARGB8888,
381 	DRM_FORMAT_XRGB8888,
382 	DRM_FORMAT_BGR565,
383 	DRM_FORMAT_RGB565,
384 	DRM_FORMAT_ABGR1555,
385 	DRM_FORMAT_XBGR1555,
386 	DRM_FORMAT_ARGB1555,
387 	DRM_FORMAT_XRGB1555,
388 	DRM_FORMAT_ABGR4444,
389 	DRM_FORMAT_XBGR4444,
390 	DRM_FORMAT_ARGB4444,
391 	DRM_FORMAT_XRGB4444,
392 };
393 
394 static const struct pl111_variant_data pl111_variant = {
395 	.name = "PL111",
396 	.formats = pl111_pixel_formats,
397 	.nformats = ARRAY_SIZE(pl111_pixel_formats),
398 	.fb_bpp = 32,
399 };
400 
401 static const u32 pl110_nomadik_pixel_formats[] = {
402 	DRM_FORMAT_RGB888,
403 	DRM_FORMAT_BGR888,
404 	DRM_FORMAT_ABGR8888,
405 	DRM_FORMAT_XBGR8888,
406 	DRM_FORMAT_ARGB8888,
407 	DRM_FORMAT_XRGB8888,
408 	DRM_FORMAT_BGR565,
409 	DRM_FORMAT_RGB565,
410 	DRM_FORMAT_ABGR1555,
411 	DRM_FORMAT_XBGR1555,
412 	DRM_FORMAT_ARGB1555,
413 	DRM_FORMAT_XRGB1555,
414 	DRM_FORMAT_ABGR4444,
415 	DRM_FORMAT_XBGR4444,
416 	DRM_FORMAT_ARGB4444,
417 	DRM_FORMAT_XRGB4444,
418 };
419 
420 static const struct pl111_variant_data pl110_nomadik_variant = {
421 	.name = "LCDC (PL110 Nomadik)",
422 	.formats = pl110_nomadik_pixel_formats,
423 	.nformats = ARRAY_SIZE(pl110_nomadik_pixel_formats),
424 	.is_lcdc = true,
425 	.st_bitmux_control = true,
426 	.broken_vblank = true,
427 	.fb_bpp = 16,
428 };
429 
430 static const struct amba_id pl111_id_table[] = {
431 	{
432 		.id = 0x00041110,
433 		.mask = 0x000fffff,
434 		.data = (void *)&pl110_variant,
435 	},
436 	{
437 		.id = 0x00180110,
438 		.mask = 0x00fffffe,
439 		.data = (void *)&pl110_nomadik_variant,
440 	},
441 	{
442 		.id = 0x00041111,
443 		.mask = 0x000fffff,
444 		.data = (void *)&pl111_variant,
445 	},
446 	{0, 0},
447 };
448 
449 static struct amba_driver pl111_amba_driver __maybe_unused = {
450 	.drv = {
451 		.name = "drm-clcd-pl111",
452 	},
453 	.probe = pl111_amba_probe,
454 	.remove = pl111_amba_remove,
455 	.id_table = pl111_id_table,
456 };
457 
458 #ifdef CONFIG_ARM_AMBA
459 module_amba_driver(pl111_amba_driver);
460 #endif
461 
462 MODULE_DESCRIPTION(DRIVER_DESC);
463 MODULE_AUTHOR("ARM Ltd.");
464 MODULE_LICENSE("GPL");
465