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 #include <linux/of.h>
62 #include <linux/of_graph.h>
63 #include <linux/of_reserved_mem.h>
64 
65 #include <drm/drmP.h>
66 #include <drm/drm_atomic_helper.h>
67 #include <drm/drm_crtc_helper.h>
68 #include <drm/drm_gem_cma_helper.h>
69 #include <drm/drm_gem_framebuffer_helper.h>
70 #include <drm/drm_fb_helper.h>
71 #include <drm/drm_fb_cma_helper.h>
72 #include <drm/drm_of.h>
73 #include <drm/drm_bridge.h>
74 #include <drm/drm_panel.h>
75 
76 #include "pl111_drm.h"
77 #include "pl111_versatile.h"
78 #include "pl111_nomadik.h"
79 
80 #define DRIVER_DESC      "DRM module for PL111"
81 
82 static const struct drm_mode_config_funcs mode_config_funcs = {
83 	.fb_create = drm_gem_fb_create,
84 	.atomic_check = drm_atomic_helper_check,
85 	.atomic_commit = drm_atomic_helper_commit,
86 };
87 
pl111_modeset_init(struct drm_device * dev)88 static int pl111_modeset_init(struct drm_device *dev)
89 {
90 	struct drm_mode_config *mode_config;
91 	struct pl111_drm_dev_private *priv = dev->dev_private;
92 	struct device_node *np = dev->dev->of_node;
93 	struct device_node *remote;
94 	struct drm_panel *panel = NULL;
95 	struct drm_bridge *bridge = NULL;
96 	bool defer = false;
97 	int ret = 0;
98 	int i;
99 
100 	drm_mode_config_init(dev);
101 	mode_config = &dev->mode_config;
102 	mode_config->funcs = &mode_config_funcs;
103 	mode_config->min_width = 1;
104 	mode_config->max_width = 1024;
105 	mode_config->min_height = 1;
106 	mode_config->max_height = 768;
107 
108 	i = 0;
109 	for_each_endpoint_of_node(np, remote) {
110 		struct drm_panel *tmp_panel;
111 		struct drm_bridge *tmp_bridge;
112 
113 		dev_dbg(dev->dev, "checking endpoint %d\n", i);
114 
115 		ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
116 						  0, i,
117 						  &tmp_panel,
118 						  &tmp_bridge);
119 		if (ret) {
120 			if (ret == -EPROBE_DEFER) {
121 				/*
122 				 * Something deferred, but that is often just
123 				 * another way of saying -ENODEV, but let's
124 				 * cast a vote for later deferral.
125 				 */
126 				defer = true;
127 			} else if (ret != -ENODEV) {
128 				/* Continue, maybe something else is working */
129 				dev_err(dev->dev,
130 					"endpoint %d returns %d\n", i, ret);
131 			}
132 		}
133 
134 		if (tmp_panel) {
135 			dev_info(dev->dev,
136 				 "found panel on endpoint %d\n", i);
137 			panel = tmp_panel;
138 		}
139 		if (tmp_bridge) {
140 			dev_info(dev->dev,
141 				 "found bridge on endpoint %d\n", i);
142 			bridge = tmp_bridge;
143 		}
144 
145 		i++;
146 	}
147 
148 	/*
149 	 * If we can't find neither panel nor bridge on any of the
150 	 * endpoints, and any of them retured -EPROBE_DEFER, then
151 	 * let's defer this driver too.
152 	 */
153 	if ((!panel && !bridge) && defer)
154 		return -EPROBE_DEFER;
155 
156 	if (panel) {
157 		bridge = drm_panel_bridge_add(panel,
158 					      DRM_MODE_CONNECTOR_Unknown);
159 		if (IS_ERR(bridge)) {
160 			ret = PTR_ERR(bridge);
161 			goto out_config;
162 		}
163 	} else if (bridge) {
164 		dev_info(dev->dev, "Using non-panel bridge\n");
165 	} else {
166 		dev_err(dev->dev, "No bridge, exiting\n");
167 		return -ENODEV;
168 	}
169 
170 	priv->bridge = bridge;
171 	if (panel) {
172 		priv->panel = panel;
173 		priv->connector = panel->connector;
174 	}
175 
176 	ret = pl111_display_init(dev);
177 	if (ret != 0) {
178 		dev_err(dev->dev, "Failed to init display\n");
179 		goto out_bridge;
180 	}
181 
182 	ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
183 						    bridge);
184 	if (ret)
185 		return ret;
186 
187 	if (!priv->variant->broken_vblank) {
188 		ret = drm_vblank_init(dev, 1);
189 		if (ret != 0) {
190 			dev_err(dev->dev, "Failed to init vblank\n");
191 			goto out_bridge;
192 		}
193 	}
194 
195 	drm_mode_config_reset(dev);
196 
197 	drm_fb_cma_fbdev_init(dev, priv->variant->fb_bpp, 0);
198 
199 	drm_kms_helper_poll_init(dev);
200 
201 	goto finish;
202 
203 out_bridge:
204 	if (panel)
205 		drm_panel_bridge_remove(bridge);
206 out_config:
207 	drm_mode_config_cleanup(dev);
208 finish:
209 	return ret;
210 }
211 
212 static struct drm_gem_object *
pl111_gem_import_sg_table(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * sgt)213 pl111_gem_import_sg_table(struct drm_device *dev,
214 			  struct dma_buf_attachment *attach,
215 			  struct sg_table *sgt)
216 {
217 	struct pl111_drm_dev_private *priv = dev->dev_private;
218 
219 	/*
220 	 * When using device-specific reserved memory we can't import
221 	 * DMA buffers: those are passed by reference in any global
222 	 * memory and we can only handle a specific range of memory.
223 	 */
224 	if (priv->use_device_memory)
225 		return ERR_PTR(-EINVAL);
226 
227 	return drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
228 }
229 
230 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
231 
232 static struct drm_driver pl111_drm_driver = {
233 	.driver_features =
234 		DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
235 	.lastclose = drm_fb_helper_lastclose,
236 	.ioctls = NULL,
237 	.fops = &drm_fops,
238 	.name = "pl111",
239 	.desc = DRIVER_DESC,
240 	.date = "20170317",
241 	.major = 1,
242 	.minor = 0,
243 	.patchlevel = 0,
244 	.dumb_create = drm_gem_cma_dumb_create,
245 	.gem_free_object_unlocked = drm_gem_cma_free_object,
246 	.gem_vm_ops = &drm_gem_cma_vm_ops,
247 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
248 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
249 	.gem_prime_import = drm_gem_prime_import,
250 	.gem_prime_import_sg_table = pl111_gem_import_sg_table,
251 	.gem_prime_export = drm_gem_prime_export,
252 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
253 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
254 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
255 
256 #if defined(CONFIG_DEBUG_FS)
257 	.debugfs_init = pl111_debugfs_init,
258 #endif
259 };
260 
pl111_amba_probe(struct amba_device * amba_dev,const struct amba_id * id)261 static int pl111_amba_probe(struct amba_device *amba_dev,
262 			    const struct amba_id *id)
263 {
264 	struct device *dev = &amba_dev->dev;
265 	struct pl111_drm_dev_private *priv;
266 	const struct pl111_variant_data *variant = id->data;
267 	struct drm_device *drm;
268 	int ret;
269 
270 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
271 	if (!priv)
272 		return -ENOMEM;
273 
274 	drm = drm_dev_alloc(&pl111_drm_driver, dev);
275 	if (IS_ERR(drm))
276 		return PTR_ERR(drm);
277 	amba_set_drvdata(amba_dev, drm);
278 	priv->drm = drm;
279 	drm->dev_private = priv;
280 	priv->variant = variant;
281 
282 	ret = of_reserved_mem_device_init(dev);
283 	if (!ret) {
284 		dev_info(dev, "using device-specific reserved memory\n");
285 		priv->use_device_memory = true;
286 	}
287 
288 	if (of_property_read_u32(dev->of_node, "max-memory-bandwidth",
289 				 &priv->memory_bw)) {
290 		dev_info(dev, "no max memory bandwidth specified, assume unlimited\n");
291 		priv->memory_bw = 0;
292 	}
293 
294 	/* The two main variants swap this register */
295 	if (variant->is_pl110 || variant->is_lcdc) {
296 		priv->ienb = CLCD_PL110_IENB;
297 		priv->ctrl = CLCD_PL110_CNTL;
298 	} else {
299 		priv->ienb = CLCD_PL111_IENB;
300 		priv->ctrl = CLCD_PL111_CNTL;
301 	}
302 
303 	priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
304 	if (IS_ERR(priv->regs)) {
305 		dev_err(dev, "%s failed mmio\n", __func__);
306 		ret = PTR_ERR(priv->regs);
307 		goto dev_put;
308 	}
309 
310 	/* This may override some variant settings */
311 	ret = pl111_versatile_init(dev, priv);
312 	if (ret)
313 		goto dev_put;
314 
315 	pl111_nomadik_init(dev);
316 
317 	/* turn off interrupts before requesting the irq */
318 	writel(0, priv->regs + priv->ienb);
319 
320 	ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
321 			       variant->name, priv);
322 	if (ret != 0) {
323 		dev_err(dev, "%s failed irq %d\n", __func__, ret);
324 		return ret;
325 	}
326 
327 	ret = pl111_modeset_init(drm);
328 	if (ret != 0)
329 		goto dev_put;
330 
331 	ret = drm_dev_register(drm, 0);
332 	if (ret < 0)
333 		goto dev_put;
334 
335 	return 0;
336 
337 dev_put:
338 	drm_dev_put(drm);
339 	of_reserved_mem_device_release(dev);
340 
341 	return ret;
342 }
343 
pl111_amba_remove(struct amba_device * amba_dev)344 static int pl111_amba_remove(struct amba_device *amba_dev)
345 {
346 	struct device *dev = &amba_dev->dev;
347 	struct drm_device *drm = amba_get_drvdata(amba_dev);
348 	struct pl111_drm_dev_private *priv = drm->dev_private;
349 
350 	drm_dev_unregister(drm);
351 	drm_fb_cma_fbdev_fini(drm);
352 	if (priv->panel)
353 		drm_panel_bridge_remove(priv->bridge);
354 	drm_mode_config_cleanup(drm);
355 	drm_dev_put(drm);
356 	of_reserved_mem_device_release(dev);
357 
358 	return 0;
359 }
360 
361 /*
362  * This early variant lacks the 565 and 444 pixel formats.
363  */
364 static const u32 pl110_pixel_formats[] = {
365 	DRM_FORMAT_ABGR8888,
366 	DRM_FORMAT_XBGR8888,
367 	DRM_FORMAT_ARGB8888,
368 	DRM_FORMAT_XRGB8888,
369 	DRM_FORMAT_ABGR1555,
370 	DRM_FORMAT_XBGR1555,
371 	DRM_FORMAT_ARGB1555,
372 	DRM_FORMAT_XRGB1555,
373 };
374 
375 static const struct pl111_variant_data pl110_variant = {
376 	.name = "PL110",
377 	.is_pl110 = true,
378 	.formats = pl110_pixel_formats,
379 	.nformats = ARRAY_SIZE(pl110_pixel_formats),
380 	.fb_bpp = 16,
381 };
382 
383 /* RealView, Versatile Express etc use this modern variant */
384 static const u32 pl111_pixel_formats[] = {
385 	DRM_FORMAT_ABGR8888,
386 	DRM_FORMAT_XBGR8888,
387 	DRM_FORMAT_ARGB8888,
388 	DRM_FORMAT_XRGB8888,
389 	DRM_FORMAT_BGR565,
390 	DRM_FORMAT_RGB565,
391 	DRM_FORMAT_ABGR1555,
392 	DRM_FORMAT_XBGR1555,
393 	DRM_FORMAT_ARGB1555,
394 	DRM_FORMAT_XRGB1555,
395 	DRM_FORMAT_ABGR4444,
396 	DRM_FORMAT_XBGR4444,
397 	DRM_FORMAT_ARGB4444,
398 	DRM_FORMAT_XRGB4444,
399 };
400 
401 static const struct pl111_variant_data pl111_variant = {
402 	.name = "PL111",
403 	.formats = pl111_pixel_formats,
404 	.nformats = ARRAY_SIZE(pl111_pixel_formats),
405 	.fb_bpp = 32,
406 };
407 
408 static const u32 pl110_nomadik_pixel_formats[] = {
409 	DRM_FORMAT_RGB888,
410 	DRM_FORMAT_BGR888,
411 	DRM_FORMAT_ABGR8888,
412 	DRM_FORMAT_XBGR8888,
413 	DRM_FORMAT_ARGB8888,
414 	DRM_FORMAT_XRGB8888,
415 	DRM_FORMAT_BGR565,
416 	DRM_FORMAT_RGB565,
417 	DRM_FORMAT_ABGR1555,
418 	DRM_FORMAT_XBGR1555,
419 	DRM_FORMAT_ARGB1555,
420 	DRM_FORMAT_XRGB1555,
421 	DRM_FORMAT_ABGR4444,
422 	DRM_FORMAT_XBGR4444,
423 	DRM_FORMAT_ARGB4444,
424 	DRM_FORMAT_XRGB4444,
425 };
426 
427 static const struct pl111_variant_data pl110_nomadik_variant = {
428 	.name = "LCDC (PL110 Nomadik)",
429 	.formats = pl110_nomadik_pixel_formats,
430 	.nformats = ARRAY_SIZE(pl110_nomadik_pixel_formats),
431 	.is_lcdc = true,
432 	.st_bitmux_control = true,
433 	.broken_vblank = true,
434 	.fb_bpp = 16,
435 };
436 
437 static const struct amba_id pl111_id_table[] = {
438 	{
439 		.id = 0x00041110,
440 		.mask = 0x000fffff,
441 		.data = (void *)&pl110_variant,
442 	},
443 	{
444 		.id = 0x00180110,
445 		.mask = 0x00fffffe,
446 		.data = (void *)&pl110_nomadik_variant,
447 	},
448 	{
449 		.id = 0x00041111,
450 		.mask = 0x000fffff,
451 		.data = (void *)&pl111_variant,
452 	},
453 	{0, 0},
454 };
455 
456 static struct amba_driver pl111_amba_driver __maybe_unused = {
457 	.drv = {
458 		.name = "drm-clcd-pl111",
459 	},
460 	.probe = pl111_amba_probe,
461 	.remove = pl111_amba_remove,
462 	.id_table = pl111_id_table,
463 };
464 
465 #ifdef CONFIG_ARM_AMBA
466 module_amba_driver(pl111_amba_driver);
467 #endif
468 
469 MODULE_DESCRIPTION(DRIVER_DESC);
470 MODULE_AUTHOR("ARM Ltd.");
471 MODULE_LICENSE("GPL");
472