xref: /openbmc/linux/drivers/gpu/drm/pl111/pl111_drv.c (revision bbecb07f)
1 /*
2  * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
3  *
4  * Parts of this file were based on sources as follows:
5  *
6  * Copyright (c) 2006-2008 Intel Corporation
7  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
8  * Copyright (C) 2011 Texas Instruments
9  *
10  * This program is free software and is provided to you under the terms of the
11  * GNU General Public License version 2 as published by the Free Software
12  * Foundation, and any use by you of this program is subject to the terms of
13  * such GNU licence.
14  *
15  */
16 
17 /**
18  * DOC: ARM PrimeCell PL111 CLCD Driver
19  *
20  * The PL111 is a simple LCD controller that can support TFT and STN
21  * displays.  This driver exposes a standard KMS interface for them.
22  *
23  * This driver uses the same Device Tree binding as the fbdev CLCD
24  * driver.  While the fbdev driver supports panels that may be
25  * connected to the CLCD internally to the CLCD driver, in DRM the
26  * panels get split out to drivers/gpu/drm/panels/.  This means that,
27  * in converting from using fbdev to using DRM, you also need to write
28  * a panel driver (which may be as simple as an entry in
29  * panel-simple.c).
30  *
31  * The driver currently doesn't expose the cursor.  The DRM API for
32  * cursors requires support for 64x64 ARGB8888 cursor images, while
33  * the hardware can only support 64x64 monochrome with masking
34  * cursors.  While one could imagine trying to hack something together
35  * to look at the ARGB8888 and program reasonable in monochrome, we
36  * just don't expose the cursor at all instead, and leave cursor
37  * support to the X11 software cursor layer.
38  *
39  * TODO:
40  *
41  * - Fix race between setting plane base address and getting IRQ for
42  *   vsync firing the pageflip completion.
43  *
44  * - Use the "max-memory-bandwidth" DT property to filter the
45  *   supported formats.
46  *
47  * - Read back hardware state at boot to skip reprogramming the
48  *   hardware when doing a no-op modeset.
49  *
50  * - Use the CLKSEL bit to support switching between the two external
51  *   clock parents.
52  */
53 
54 #include <linux/amba/bus.h>
55 #include <linux/amba/clcd-regs.h>
56 #include <linux/version.h>
57 #include <linux/shmem_fs.h>
58 #include <linux/dma-buf.h>
59 #include <linux/module.h>
60 #include <linux/slab.h>
61 
62 #include <drm/drmP.h>
63 #include <drm/drm_atomic_helper.h>
64 #include <drm/drm_crtc_helper.h>
65 #include <drm/drm_gem_cma_helper.h>
66 #include <drm/drm_gem_framebuffer_helper.h>
67 #include <drm/drm_fb_cma_helper.h>
68 #include <drm/drm_of.h>
69 #include <drm/drm_bridge.h>
70 #include <drm/drm_panel.h>
71 
72 #include "pl111_drm.h"
73 #include "pl111_versatile.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 drm_panel *panel;
88 	struct drm_bridge *bridge;
89 	int ret = 0;
90 
91 	drm_mode_config_init(dev);
92 	mode_config = &dev->mode_config;
93 	mode_config->funcs = &mode_config_funcs;
94 	mode_config->min_width = 1;
95 	mode_config->max_width = 1024;
96 	mode_config->min_height = 1;
97 	mode_config->max_height = 768;
98 
99 	ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
100 					  0, 0, &panel, &bridge);
101 	if (ret && ret != -ENODEV)
102 		return ret;
103 	if (panel) {
104 		bridge = drm_panel_bridge_add(panel,
105 					      DRM_MODE_CONNECTOR_Unknown);
106 		if (IS_ERR(bridge)) {
107 			ret = PTR_ERR(bridge);
108 			goto out_config;
109 		}
110 		/*
111 		 * TODO: when we are using a different bridge than a panel
112 		 * (such as a dumb VGA connector) we need to devise a different
113 		 * method to get the connector out of the bridge.
114 		 */
115 	}
116 
117 	ret = pl111_display_init(dev);
118 	if (ret != 0) {
119 		dev_err(dev->dev, "Failed to init display\n");
120 		goto out_bridge;
121 	}
122 
123 	ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
124 						    bridge);
125 	if (ret)
126 		return ret;
127 
128 	priv->bridge = bridge;
129 	priv->panel = panel;
130 	priv->connector = panel->connector;
131 
132 	ret = drm_vblank_init(dev, 1);
133 	if (ret != 0) {
134 		dev_err(dev->dev, "Failed to init vblank\n");
135 		goto out_bridge;
136 	}
137 
138 	drm_mode_config_reset(dev);
139 
140 	priv->fbdev = drm_fbdev_cma_init(dev, 32,
141 					 dev->mode_config.num_connector);
142 
143 	drm_kms_helper_poll_init(dev);
144 
145 	goto finish;
146 
147 out_bridge:
148 	if (panel)
149 		drm_panel_bridge_remove(bridge);
150 out_config:
151 	drm_mode_config_cleanup(dev);
152 finish:
153 	return ret;
154 }
155 
156 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
157 
158 static void pl111_lastclose(struct drm_device *dev)
159 {
160 	struct pl111_drm_dev_private *priv = dev->dev_private;
161 
162 	drm_fbdev_cma_restore_mode(priv->fbdev);
163 }
164 
165 static struct drm_driver pl111_drm_driver = {
166 	.driver_features =
167 		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
168 	.lastclose = pl111_lastclose,
169 	.ioctls = NULL,
170 	.fops = &drm_fops,
171 	.name = "pl111",
172 	.desc = DRIVER_DESC,
173 	.date = "20170317",
174 	.major = 1,
175 	.minor = 0,
176 	.patchlevel = 0,
177 	.dumb_create = drm_gem_cma_dumb_create,
178 	.gem_free_object_unlocked = drm_gem_cma_free_object,
179 	.gem_vm_ops = &drm_gem_cma_vm_ops,
180 
181 	.enable_vblank = pl111_enable_vblank,
182 	.disable_vblank = pl111_disable_vblank,
183 
184 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
185 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
186 	.gem_prime_import = drm_gem_prime_import,
187 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
188 	.gem_prime_export = drm_gem_prime_export,
189 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
190 
191 #if defined(CONFIG_DEBUG_FS)
192 	.debugfs_init = pl111_debugfs_init,
193 #endif
194 };
195 
196 static int pl111_amba_probe(struct amba_device *amba_dev,
197 			    const struct amba_id *id)
198 {
199 	struct device *dev = &amba_dev->dev;
200 	struct pl111_drm_dev_private *priv;
201 	struct pl111_variant_data *variant = id->data;
202 	struct drm_device *drm;
203 	int ret;
204 
205 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
206 	if (!priv)
207 		return -ENOMEM;
208 
209 	drm = drm_dev_alloc(&pl111_drm_driver, dev);
210 	if (IS_ERR(drm))
211 		return PTR_ERR(drm);
212 	amba_set_drvdata(amba_dev, drm);
213 	priv->drm = drm;
214 	drm->dev_private = priv;
215 	priv->variant = variant;
216 
217 	/*
218 	 * The PL110 and PL111 variants have two registers
219 	 * swapped: interrupt enable and control. For this reason
220 	 * we use offsets that we can change per variant.
221 	 */
222 	if (variant->is_pl110) {
223 		/*
224 		 * The ARM Versatile boards are even more special:
225 		 * their PrimeCell ID say they are PL110 but the
226 		 * control and interrupt enable registers are anyway
227 		 * swapped to the PL111 order so they are not following
228 		 * the PL110 datasheet.
229 		 */
230 		if (of_machine_is_compatible("arm,versatile-ab") ||
231 		    of_machine_is_compatible("arm,versatile-pb")) {
232 			priv->ienb = CLCD_PL111_IENB;
233 			priv->ctrl = CLCD_PL111_CNTL;
234 		} else {
235 			priv->ienb = CLCD_PL110_IENB;
236 			priv->ctrl = CLCD_PL110_CNTL;
237 		}
238 	} else {
239 		priv->ienb = CLCD_PL111_IENB;
240 		priv->ctrl = CLCD_PL111_CNTL;
241 	}
242 
243 	priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
244 	if (IS_ERR(priv->regs)) {
245 		dev_err(dev, "%s failed mmio\n", __func__);
246 		return PTR_ERR(priv->regs);
247 	}
248 
249 	/* turn off interrupts before requesting the irq */
250 	writel(0, priv->regs + priv->ienb);
251 
252 	ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
253 			       variant->name, priv);
254 	if (ret != 0) {
255 		dev_err(dev, "%s failed irq %d\n", __func__, ret);
256 		return ret;
257 	}
258 
259 	ret = pl111_versatile_init(dev, priv);
260 	if (ret)
261 		goto dev_unref;
262 
263 	ret = pl111_modeset_init(drm);
264 	if (ret != 0)
265 		goto dev_unref;
266 
267 	ret = drm_dev_register(drm, 0);
268 	if (ret < 0)
269 		goto dev_unref;
270 
271 	return 0;
272 
273 dev_unref:
274 	drm_dev_unref(drm);
275 	return ret;
276 }
277 
278 static int pl111_amba_remove(struct amba_device *amba_dev)
279 {
280 	struct drm_device *drm = amba_get_drvdata(amba_dev);
281 	struct pl111_drm_dev_private *priv = drm->dev_private;
282 
283 	drm_dev_unregister(drm);
284 	if (priv->fbdev)
285 		drm_fbdev_cma_fini(priv->fbdev);
286 	if (priv->panel)
287 		drm_panel_bridge_remove(priv->bridge);
288 	drm_mode_config_cleanup(drm);
289 	drm_dev_unref(drm);
290 
291 	return 0;
292 }
293 
294 /*
295  * This variant exist in early versions like the ARM Integrator
296  * and this version lacks the 565 and 444 pixel formats.
297  */
298 static const u32 pl110_pixel_formats[] = {
299 	DRM_FORMAT_ABGR8888,
300 	DRM_FORMAT_XBGR8888,
301 	DRM_FORMAT_ARGB8888,
302 	DRM_FORMAT_XRGB8888,
303 	DRM_FORMAT_ABGR1555,
304 	DRM_FORMAT_XBGR1555,
305 	DRM_FORMAT_ARGB1555,
306 	DRM_FORMAT_XRGB1555,
307 };
308 
309 static const struct pl111_variant_data pl110_variant = {
310 	.name = "PL110",
311 	.is_pl110 = true,
312 	.formats = pl110_pixel_formats,
313 	.nformats = ARRAY_SIZE(pl110_pixel_formats),
314 };
315 
316 /* RealView, Versatile Express etc use this modern variant */
317 static const u32 pl111_pixel_formats[] = {
318 	DRM_FORMAT_ABGR8888,
319 	DRM_FORMAT_XBGR8888,
320 	DRM_FORMAT_ARGB8888,
321 	DRM_FORMAT_XRGB8888,
322 	DRM_FORMAT_BGR565,
323 	DRM_FORMAT_RGB565,
324 	DRM_FORMAT_ABGR1555,
325 	DRM_FORMAT_XBGR1555,
326 	DRM_FORMAT_ARGB1555,
327 	DRM_FORMAT_XRGB1555,
328 	DRM_FORMAT_ABGR4444,
329 	DRM_FORMAT_XBGR4444,
330 	DRM_FORMAT_ARGB4444,
331 	DRM_FORMAT_XRGB4444,
332 };
333 
334 static const struct pl111_variant_data pl111_variant = {
335 	.name = "PL111",
336 	.formats = pl111_pixel_formats,
337 	.nformats = ARRAY_SIZE(pl111_pixel_formats),
338 };
339 
340 static const struct amba_id pl111_id_table[] = {
341 	{
342 		.id = 0x00041110,
343 		.mask = 0x000fffff,
344 		.data = (void*)&pl110_variant,
345 	},
346 	{
347 		.id = 0x00041111,
348 		.mask = 0x000fffff,
349 		.data = (void*)&pl111_variant,
350 	},
351 	{0, 0},
352 };
353 
354 static struct amba_driver pl111_amba_driver __maybe_unused = {
355 	.drv = {
356 		.name = "drm-clcd-pl111",
357 	},
358 	.probe = pl111_amba_probe,
359 	.remove = pl111_amba_remove,
360 	.id_table = pl111_id_table,
361 };
362 
363 #ifdef CONFIG_ARM_AMBA
364 module_amba_driver(pl111_amba_driver);
365 #endif
366 
367 MODULE_DESCRIPTION(DRIVER_DESC);
368 MODULE_AUTHOR("ARM Ltd.");
369 MODULE_LICENSE("GPL");
370