blob: 4621259d538705ec9723fd9a7f6f495c02bc2338 [file] [log] [blame]
Tom Cookseybed41002017-04-12 20:17:46 -07001/*
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 *
Tom Cookseybed41002017-04-12 20:17:46 -070044 * - 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 *
Eric Anholt032838f2017-05-08 12:33:48 -070050 * - Use the CLKSEL bit to support switching between the two external
51 * clock parents.
Tom Cookseybed41002017-04-12 20:17:46 -070052 */
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>
Linus Walleij3b6ec452018-02-06 10:35:40 +010061#include <linux/of.h>
62#include <linux/of_graph.h>
Tom Cookseybed41002017-04-12 20:17:46 -070063
64#include <drm/drmP.h>
65#include <drm/drm_atomic_helper.h>
66#include <drm/drm_crtc_helper.h>
67#include <drm/drm_gem_cma_helper.h>
Noralf Trønnesb6eb01a2017-08-13 15:31:56 +020068#include <drm/drm_gem_framebuffer_helper.h>
Noralf Trønnesd18df742017-11-15 15:19:49 +010069#include <drm/drm_fb_helper.h>
Tom Cookseybed41002017-04-12 20:17:46 -070070#include <drm/drm_fb_cma_helper.h>
Linus Walleij001485d2017-09-08 14:47:05 +020071#include <drm/drm_of.h>
72#include <drm/drm_bridge.h>
73#include <drm/drm_panel.h>
Tom Cookseybed41002017-04-12 20:17:46 -070074
75#include "pl111_drm.h"
Linus Walleij72fe9152017-09-08 14:47:09 +020076#include "pl111_versatile.h"
Tom Cookseybed41002017-04-12 20:17:46 -070077
78#define DRIVER_DESC "DRM module for PL111"
79
Bhumika Goyala5cb37d2017-08-08 20:35:51 +053080static const struct drm_mode_config_funcs mode_config_funcs = {
Noralf Trønnesb6eb01a2017-08-13 15:31:56 +020081 .fb_create = drm_gem_fb_create,
Tom Cookseybed41002017-04-12 20:17:46 -070082 .atomic_check = drm_atomic_helper_check,
83 .atomic_commit = drm_atomic_helper_commit,
84};
85
86static int pl111_modeset_init(struct drm_device *dev)
87{
88 struct drm_mode_config *mode_config;
89 struct pl111_drm_dev_private *priv = dev->dev_private;
Linus Walleij3b6ec452018-02-06 10:35:40 +010090 struct device_node *np = dev->dev->of_node;
91 struct device_node *remote;
92 struct drm_panel *panel = NULL;
93 struct drm_bridge *bridge = NULL;
94 bool defer = false;
Tom Cookseybed41002017-04-12 20:17:46 -070095 int ret = 0;
Linus Walleij3b6ec452018-02-06 10:35:40 +010096 int i;
Tom Cookseybed41002017-04-12 20:17:46 -070097
98 drm_mode_config_init(dev);
99 mode_config = &dev->mode_config;
100 mode_config->funcs = &mode_config_funcs;
101 mode_config->min_width = 1;
102 mode_config->max_width = 1024;
103 mode_config->min_height = 1;
104 mode_config->max_height = 768;
105
Linus Walleij3b6ec452018-02-06 10:35:40 +0100106 i = 0;
107 for_each_endpoint_of_node(np, remote) {
108 struct drm_panel *tmp_panel;
109 struct drm_bridge *tmp_bridge;
110
111 dev_dbg(dev->dev, "checking endpoint %d\n", i);
112
113 ret = drm_of_find_panel_or_bridge(dev->dev->of_node,
114 0, i,
115 &tmp_panel,
116 &tmp_bridge);
117 if (ret) {
118 if (ret == -EPROBE_DEFER) {
119 /*
120 * Something deferred, but that is often just
121 * another way of saying -ENODEV, but let's
122 * cast a vote for later deferral.
123 */
124 defer = true;
125 } else if (ret != -ENODEV) {
126 /* Continue, maybe something else is working */
127 dev_err(dev->dev,
128 "endpoint %d returns %d\n", i, ret);
129 }
130 }
131
132 if (tmp_panel) {
133 dev_info(dev->dev,
134 "found panel on endpoint %d\n", i);
135 panel = tmp_panel;
136 }
137 if (tmp_bridge) {
138 dev_info(dev->dev,
139 "found bridge on endpoint %d\n", i);
140 bridge = tmp_bridge;
141 }
142
143 i++;
144 }
145
146 /*
147 * If we can't find neither panel nor bridge on any of the
148 * endpoints, and any of them retured -EPROBE_DEFER, then
149 * let's defer this driver too.
150 */
151 if ((!panel && !bridge) && defer)
152 return -EPROBE_DEFER;
153
Linus Walleij001485d2017-09-08 14:47:05 +0200154 if (panel) {
155 bridge = drm_panel_bridge_add(panel,
156 DRM_MODE_CONNECTOR_Unknown);
157 if (IS_ERR(bridge)) {
158 ret = PTR_ERR(bridge);
159 goto out_config;
160 }
Linus Walleij49f81d82018-01-12 08:48:54 +0100161 } else if (bridge) {
162 dev_info(dev->dev, "Using non-panel bridge\n");
163 } else {
164 dev_err(dev->dev, "No bridge, exiting\n");
165 return -ENODEV;
166 }
167
168 priv->bridge = bridge;
169 if (panel) {
170 priv->panel = panel;
171 priv->connector = panel->connector;
Tom Cookseybed41002017-04-12 20:17:46 -0700172 }
173
174 ret = pl111_display_init(dev);
175 if (ret != 0) {
176 dev_err(dev->dev, "Failed to init display\n");
Linus Walleij001485d2017-09-08 14:47:05 +0200177 goto out_bridge;
Tom Cookseybed41002017-04-12 20:17:46 -0700178 }
179
Linus Walleij001485d2017-09-08 14:47:05 +0200180 ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
181 bridge);
182 if (ret)
183 return ret;
184
Linus Walleij08e32112018-02-06 10:35:39 +0100185 if (!priv->variant->broken_vblank) {
186 ret = drm_vblank_init(dev, 1);
187 if (ret != 0) {
188 dev_err(dev->dev, "Failed to init vblank\n");
189 goto out_bridge;
190 }
Tom Cookseybed41002017-04-12 20:17:46 -0700191 }
192
193 drm_mode_config_reset(dev);
194
Linus Walleij9f8d4fe2018-03-02 10:09:45 +0100195 drm_fb_cma_fbdev_init(dev, priv->variant->fb_bpp, 0);
Tom Cookseybed41002017-04-12 20:17:46 -0700196
197 drm_kms_helper_poll_init(dev);
198
199 goto finish;
200
Linus Walleij001485d2017-09-08 14:47:05 +0200201out_bridge:
202 if (panel)
203 drm_panel_bridge_remove(bridge);
Tom Cookseybed41002017-04-12 20:17:46 -0700204out_config:
205 drm_mode_config_cleanup(dev);
206finish:
207 return ret;
208}
209
210DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
211
Tom Cookseybed41002017-04-12 20:17:46 -0700212static struct drm_driver pl111_drm_driver = {
213 .driver_features =
214 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
Noralf Trønnesd18df742017-11-15 15:19:49 +0100215 .lastclose = drm_fb_helper_lastclose,
Tom Cookseybed41002017-04-12 20:17:46 -0700216 .ioctls = NULL,
217 .fops = &drm_fops,
218 .name = "pl111",
219 .desc = DRIVER_DESC,
220 .date = "20170317",
221 .major = 1,
222 .minor = 0,
223 .patchlevel = 0,
224 .dumb_create = drm_gem_cma_dumb_create,
Daniel Vetter8125b842017-07-17 17:10:44 +0200225 .gem_free_object_unlocked = drm_gem_cma_free_object,
Tom Cookseybed41002017-04-12 20:17:46 -0700226 .gem_vm_ops = &drm_gem_cma_vm_ops,
Tom Cookseybed41002017-04-12 20:17:46 -0700227 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
228 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
229 .gem_prime_import = drm_gem_prime_import,
230 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
231 .gem_prime_export = drm_gem_prime_export,
232 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
Eric Anholt141518d2017-05-17 17:56:40 -0700233
234#if defined(CONFIG_DEBUG_FS)
235 .debugfs_init = pl111_debugfs_init,
236#endif
Tom Cookseybed41002017-04-12 20:17:46 -0700237};
238
Tom Cookseybed41002017-04-12 20:17:46 -0700239static int pl111_amba_probe(struct amba_device *amba_dev,
240 const struct amba_id *id)
241{
242 struct device *dev = &amba_dev->dev;
243 struct pl111_drm_dev_private *priv;
Linus Walleij3d95f76a2018-02-06 10:35:36 +0100244 const struct pl111_variant_data *variant = id->data;
Tom Cookseybed41002017-04-12 20:17:46 -0700245 struct drm_device *drm;
246 int ret;
247
248 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
249 if (!priv)
250 return -ENOMEM;
251
252 drm = drm_dev_alloc(&pl111_drm_driver, dev);
253 if (IS_ERR(drm))
254 return PTR_ERR(drm);
255 amba_set_drvdata(amba_dev, drm);
256 priv->drm = drm;
257 drm->dev_private = priv;
Linus Walleijfa833062017-09-08 14:47:06 +0200258 priv->variant = variant;
259
Linus Walleijdf99dd92018-03-07 22:58:19 +0100260 if (of_property_read_u32(dev->of_node, "max-memory-bandwidth",
261 &priv->memory_bw)) {
262 dev_info(dev, "no max memory bandwidth specified, assume unlimited\n");
263 priv->memory_bw = 0;
264 }
265
Linus Walleij3d95f76a2018-02-06 10:35:36 +0100266 /* The two variants swap this register */
Linus Walleijfa833062017-09-08 14:47:06 +0200267 if (variant->is_pl110) {
Linus Walleij3d95f76a2018-02-06 10:35:36 +0100268 priv->ienb = CLCD_PL110_IENB;
269 priv->ctrl = CLCD_PL110_CNTL;
Linus Walleijfa833062017-09-08 14:47:06 +0200270 } else {
271 priv->ienb = CLCD_PL111_IENB;
272 priv->ctrl = CLCD_PL111_CNTL;
273 }
Tom Cookseybed41002017-04-12 20:17:46 -0700274
Tom Cookseybed41002017-04-12 20:17:46 -0700275 priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
Wei Yongjun665f37e2017-05-21 01:01:52 +0000276 if (IS_ERR(priv->regs)) {
Tom Cookseybed41002017-04-12 20:17:46 -0700277 dev_err(dev, "%s failed mmio\n", __func__);
Wei Yongjun665f37e2017-05-21 01:01:52 +0000278 return PTR_ERR(priv->regs);
Tom Cookseybed41002017-04-12 20:17:46 -0700279 }
280
Linus Walleij3d95f76a2018-02-06 10:35:36 +0100281 /* This may override some variant settings */
282 ret = pl111_versatile_init(dev, priv);
283 if (ret)
284 goto dev_unref;
285
Tom Cookseybed41002017-04-12 20:17:46 -0700286 /* turn off interrupts before requesting the irq */
Linus Walleijfa833062017-09-08 14:47:06 +0200287 writel(0, priv->regs + priv->ienb);
Tom Cookseybed41002017-04-12 20:17:46 -0700288
289 ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
Linus Walleijfa833062017-09-08 14:47:06 +0200290 variant->name, priv);
Tom Cookseybed41002017-04-12 20:17:46 -0700291 if (ret != 0) {
292 dev_err(dev, "%s failed irq %d\n", __func__, ret);
293 return ret;
294 }
295
296 ret = pl111_modeset_init(drm);
297 if (ret != 0)
298 goto dev_unref;
299
300 ret = drm_dev_register(drm, 0);
301 if (ret < 0)
302 goto dev_unref;
303
304 return 0;
305
306dev_unref:
307 drm_dev_unref(drm);
308 return ret;
309}
310
311static int pl111_amba_remove(struct amba_device *amba_dev)
312{
313 struct drm_device *drm = amba_get_drvdata(amba_dev);
314 struct pl111_drm_dev_private *priv = drm->dev_private;
315
316 drm_dev_unregister(drm);
Noralf Trønnesd18df742017-11-15 15:19:49 +0100317 drm_fb_cma_fbdev_fini(drm);
Linus Walleij001485d2017-09-08 14:47:05 +0200318 if (priv->panel)
319 drm_panel_bridge_remove(priv->bridge);
Tom Cookseybed41002017-04-12 20:17:46 -0700320 drm_mode_config_cleanup(drm);
321 drm_dev_unref(drm);
322
323 return 0;
324}
325
Linus Walleijfa833062017-09-08 14:47:06 +0200326/*
Linus Walleij3d95f76a2018-02-06 10:35:36 +0100327 * This early variant lacks the 565 and 444 pixel formats.
Linus Walleijfa833062017-09-08 14:47:06 +0200328 */
329static const u32 pl110_pixel_formats[] = {
330 DRM_FORMAT_ABGR8888,
331 DRM_FORMAT_XBGR8888,
332 DRM_FORMAT_ARGB8888,
333 DRM_FORMAT_XRGB8888,
334 DRM_FORMAT_ABGR1555,
335 DRM_FORMAT_XBGR1555,
336 DRM_FORMAT_ARGB1555,
337 DRM_FORMAT_XRGB1555,
338};
339
340static const struct pl111_variant_data pl110_variant = {
341 .name = "PL110",
342 .is_pl110 = true,
343 .formats = pl110_pixel_formats,
344 .nformats = ARRAY_SIZE(pl110_pixel_formats),
Linus Walleij9f8d4fe2018-03-02 10:09:45 +0100345 .fb_bpp = 16,
Linus Walleijfa833062017-09-08 14:47:06 +0200346};
347
348/* RealView, Versatile Express etc use this modern variant */
349static const u32 pl111_pixel_formats[] = {
350 DRM_FORMAT_ABGR8888,
351 DRM_FORMAT_XBGR8888,
352 DRM_FORMAT_ARGB8888,
353 DRM_FORMAT_XRGB8888,
354 DRM_FORMAT_BGR565,
355 DRM_FORMAT_RGB565,
356 DRM_FORMAT_ABGR1555,
357 DRM_FORMAT_XBGR1555,
358 DRM_FORMAT_ARGB1555,
359 DRM_FORMAT_XRGB1555,
360 DRM_FORMAT_ABGR4444,
361 DRM_FORMAT_XBGR4444,
362 DRM_FORMAT_ARGB4444,
363 DRM_FORMAT_XRGB4444,
364};
365
366static const struct pl111_variant_data pl111_variant = {
367 .name = "PL111",
368 .formats = pl111_pixel_formats,
369 .nformats = ARRAY_SIZE(pl111_pixel_formats),
Linus Walleij9f8d4fe2018-03-02 10:09:45 +0100370 .fb_bpp = 32,
Linus Walleijfa833062017-09-08 14:47:06 +0200371};
372
373static const struct amba_id pl111_id_table[] = {
374 {
375 .id = 0x00041110,
376 .mask = 0x000fffff,
377 .data = (void*)&pl110_variant,
378 },
Tom Cookseybed41002017-04-12 20:17:46 -0700379 {
380 .id = 0x00041111,
381 .mask = 0x000fffff,
Linus Walleijfa833062017-09-08 14:47:06 +0200382 .data = (void*)&pl111_variant,
Tom Cookseybed41002017-04-12 20:17:46 -0700383 },
384 {0, 0},
385};
386
Arnd Bergmann66d6dd42017-05-24 17:49:58 +0200387static struct amba_driver pl111_amba_driver __maybe_unused = {
Tom Cookseybed41002017-04-12 20:17:46 -0700388 .drv = {
389 .name = "drm-clcd-pl111",
390 },
391 .probe = pl111_amba_probe,
392 .remove = pl111_amba_remove,
393 .id_table = pl111_id_table,
394};
395
Arnd Bergmann66d6dd42017-05-24 17:49:58 +0200396#ifdef CONFIG_ARM_AMBA
Tom Cookseybed41002017-04-12 20:17:46 -0700397module_amba_driver(pl111_amba_driver);
Arnd Bergmann66d6dd42017-05-24 17:49:58 +0200398#endif
Tom Cookseybed41002017-04-12 20:17:46 -0700399
400MODULE_DESCRIPTION(DRIVER_DESC);
401MODULE_AUTHOR("ARM Ltd.");
402MODULE_LICENSE("GPL");