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