1231905150d05cd87ef8f297c2b225aaf6c5224d
[sfrench/cifs-2.6.git] / drivers / gpu / drm / pl111 / pl111_drv.c
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
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>
68 #include <drm/drm_gem_framebuffer_helper.h>
69 #include <drm/drm_fb_helper.h>
70 #include <drm/drm_fb_cma_helper.h>
71 #include <drm/drm_of.h>
72 #include <drm/drm_bridge.h>
73 #include <drm/drm_panel.h>
74
75 #include "pl111_drm.h"
76 #include "pl111_versatile.h"
77
78 #define DRIVER_DESC      "DRM module for PL111"
79
80 static const struct drm_mode_config_funcs mode_config_funcs = {
81         .fb_create = drm_gem_fb_create,
82         .atomic_check = drm_atomic_helper_check,
83         .atomic_commit = drm_atomic_helper_commit,
84 };
85
86 static 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;
90         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;
95         int ret = 0;
96         int i;
97
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
106         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
154         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                 }
161         } 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;
172         }
173
174         ret = pl111_display_init(dev);
175         if (ret != 0) {
176                 dev_err(dev->dev, "Failed to init display\n");
177                 goto out_bridge;
178         }
179
180         ret = drm_simple_display_pipe_attach_bridge(&priv->pipe,
181                                                     bridge);
182         if (ret)
183                 return ret;
184
185         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                 }
191         }
192
193         drm_mode_config_reset(dev);
194
195         drm_fb_cma_fbdev_init(dev, 32, 0);
196
197         drm_kms_helper_poll_init(dev);
198
199         goto finish;
200
201 out_bridge:
202         if (panel)
203                 drm_panel_bridge_remove(bridge);
204 out_config:
205         drm_mode_config_cleanup(dev);
206 finish:
207         return ret;
208 }
209
210 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
211
212 static struct drm_driver pl111_drm_driver = {
213         .driver_features =
214                 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
215         .lastclose = drm_fb_helper_lastclose,
216         .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,
225         .gem_free_object_unlocked = drm_gem_cma_free_object,
226         .gem_vm_ops = &drm_gem_cma_vm_ops,
227         .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,
233
234 #if defined(CONFIG_DEBUG_FS)
235         .debugfs_init = pl111_debugfs_init,
236 #endif
237 };
238
239 static 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;
244         const struct pl111_variant_data *variant = id->data;
245         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         if (!variant->broken_vblank) {
253                 pl111_drm_driver.enable_vblank = pl111_enable_vblank;
254                 pl111_drm_driver.disable_vblank = pl111_disable_vblank;
255         }
256
257         drm = drm_dev_alloc(&pl111_drm_driver, dev);
258         if (IS_ERR(drm))
259                 return PTR_ERR(drm);
260         amba_set_drvdata(amba_dev, drm);
261         priv->drm = drm;
262         drm->dev_private = priv;
263         priv->variant = variant;
264
265         /* The two variants swap this register */
266         if (variant->is_pl110) {
267                 priv->ienb = CLCD_PL110_IENB;
268                 priv->ctrl = CLCD_PL110_CNTL;
269         } else {
270                 priv->ienb = CLCD_PL111_IENB;
271                 priv->ctrl = CLCD_PL111_CNTL;
272         }
273
274         priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
275         if (IS_ERR(priv->regs)) {
276                 dev_err(dev, "%s failed mmio\n", __func__);
277                 return PTR_ERR(priv->regs);
278         }
279
280         /* This may override some variant settings */
281         ret = pl111_versatile_init(dev, priv);
282         if (ret)
283                 goto dev_unref;
284
285         /* turn off interrupts before requesting the irq */
286         writel(0, priv->regs + priv->ienb);
287
288         ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
289                                variant->name, priv);
290         if (ret != 0) {
291                 dev_err(dev, "%s failed irq %d\n", __func__, ret);
292                 return ret;
293         }
294
295         ret = pl111_modeset_init(drm);
296         if (ret != 0)
297                 goto dev_unref;
298
299         ret = drm_dev_register(drm, 0);
300         if (ret < 0)
301                 goto dev_unref;
302
303         return 0;
304
305 dev_unref:
306         drm_dev_unref(drm);
307         return ret;
308 }
309
310 static int pl111_amba_remove(struct amba_device *amba_dev)
311 {
312         struct drm_device *drm = amba_get_drvdata(amba_dev);
313         struct pl111_drm_dev_private *priv = drm->dev_private;
314
315         drm_dev_unregister(drm);
316         drm_fb_cma_fbdev_fini(drm);
317         if (priv->panel)
318                 drm_panel_bridge_remove(priv->bridge);
319         drm_mode_config_cleanup(drm);
320         drm_dev_unref(drm);
321
322         return 0;
323 }
324
325 /*
326  * This early variant lacks the 565 and 444 pixel formats.
327  */
328 static const u32 pl110_pixel_formats[] = {
329         DRM_FORMAT_ABGR8888,
330         DRM_FORMAT_XBGR8888,
331         DRM_FORMAT_ARGB8888,
332         DRM_FORMAT_XRGB8888,
333         DRM_FORMAT_ABGR1555,
334         DRM_FORMAT_XBGR1555,
335         DRM_FORMAT_ARGB1555,
336         DRM_FORMAT_XRGB1555,
337 };
338
339 static const struct pl111_variant_data pl110_variant = {
340         .name = "PL110",
341         .is_pl110 = true,
342         .formats = pl110_pixel_formats,
343         .nformats = ARRAY_SIZE(pl110_pixel_formats),
344 };
345
346 /* RealView, Versatile Express etc use this modern variant */
347 static const u32 pl111_pixel_formats[] = {
348         DRM_FORMAT_ABGR8888,
349         DRM_FORMAT_XBGR8888,
350         DRM_FORMAT_ARGB8888,
351         DRM_FORMAT_XRGB8888,
352         DRM_FORMAT_BGR565,
353         DRM_FORMAT_RGB565,
354         DRM_FORMAT_ABGR1555,
355         DRM_FORMAT_XBGR1555,
356         DRM_FORMAT_ARGB1555,
357         DRM_FORMAT_XRGB1555,
358         DRM_FORMAT_ABGR4444,
359         DRM_FORMAT_XBGR4444,
360         DRM_FORMAT_ARGB4444,
361         DRM_FORMAT_XRGB4444,
362 };
363
364 static const struct pl111_variant_data pl111_variant = {
365         .name = "PL111",
366         .formats = pl111_pixel_formats,
367         .nformats = ARRAY_SIZE(pl111_pixel_formats),
368 };
369
370 static const struct amba_id pl111_id_table[] = {
371         {
372                 .id = 0x00041110,
373                 .mask = 0x000fffff,
374                 .data = (void*)&pl110_variant,
375         },
376         {
377                 .id = 0x00041111,
378                 .mask = 0x000fffff,
379                 .data = (void*)&pl111_variant,
380         },
381         {0, 0},
382 };
383
384 static struct amba_driver pl111_amba_driver __maybe_unused = {
385         .drv = {
386                 .name = "drm-clcd-pl111",
387         },
388         .probe = pl111_amba_probe,
389         .remove = pl111_amba_remove,
390         .id_table = pl111_id_table,
391 };
392
393 #ifdef CONFIG_ARM_AMBA
394 module_amba_driver(pl111_amba_driver);
395 #endif
396
397 MODULE_DESCRIPTION(DRIVER_DESC);
398 MODULE_AUTHOR("ARM Ltd.");
399 MODULE_LICENSE("GPL");