GNU Linux-libre 4.14.266-gnu1
[releases.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  * - Expose the correct set of formats we can support based on the
45  *   "arm,pl11x,tft-r0g0b0-pads" DT property.
46  *
47  * - Use the "max-memory-bandwidth" DT property to filter the
48  *   supported formats.
49  *
50  * - Read back hardware state at boot to skip reprogramming the
51  *   hardware when doing a no-op modeset.
52  *
53  * - Use the CLKSEL bit to support switching between the two external
54  *   clock parents.
55  */
56
57 #include <linux/amba/bus.h>
58 #include <linux/amba/clcd-regs.h>
59 #include <linux/version.h>
60 #include <linux/shmem_fs.h>
61 #include <linux/dma-buf.h>
62 #include <linux/module.h>
63 #include <linux/slab.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_cma_helper.h>
71
72 #include "pl111_drm.h"
73
74 #define DRIVER_DESC      "DRM module for PL111"
75
76 static const struct drm_mode_config_funcs mode_config_funcs = {
77         .fb_create = drm_gem_fb_create,
78         .atomic_check = drm_atomic_helper_check,
79         .atomic_commit = drm_atomic_helper_commit,
80 };
81
82 static int pl111_modeset_init(struct drm_device *dev)
83 {
84         struct drm_mode_config *mode_config;
85         struct pl111_drm_dev_private *priv = dev->dev_private;
86         int ret = 0;
87
88         drm_mode_config_init(dev);
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         ret = pl111_connector_init(dev);
97         if (ret) {
98                 dev_err(dev->dev, "Failed to create pl111_drm_connector\n");
99                 goto out_config;
100         }
101
102         /* Don't actually attach if we didn't find a drm_panel
103          * attached to us.  This will allow a kernel to include both
104          * the fbdev pl111 driver and this one, and choose between
105          * them based on which subsystem has support for the panel.
106          */
107         if (!priv->connector.panel) {
108                 dev_info(dev->dev,
109                          "Disabling due to lack of DRM panel device.\n");
110                 ret = -ENODEV;
111                 goto out_config;
112         }
113
114         ret = pl111_display_init(dev);
115         if (ret != 0) {
116                 dev_err(dev->dev, "Failed to init display\n");
117                 goto out_config;
118         }
119
120         ret = drm_vblank_init(dev, 1);
121         if (ret != 0) {
122                 dev_err(dev->dev, "Failed to init vblank\n");
123                 goto out_config;
124         }
125
126         drm_mode_config_reset(dev);
127
128         priv->fbdev = drm_fbdev_cma_init(dev, 32,
129                                          dev->mode_config.num_connector);
130
131         drm_kms_helper_poll_init(dev);
132
133         goto finish;
134
135 out_config:
136         drm_mode_config_cleanup(dev);
137 finish:
138         return ret;
139 }
140
141 DEFINE_DRM_GEM_CMA_FOPS(drm_fops);
142
143 static void pl111_lastclose(struct drm_device *dev)
144 {
145         struct pl111_drm_dev_private *priv = dev->dev_private;
146
147         drm_fbdev_cma_restore_mode(priv->fbdev);
148 }
149
150 static struct drm_driver pl111_drm_driver = {
151         .driver_features =
152                 DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC,
153         .lastclose = pl111_lastclose,
154         .ioctls = NULL,
155         .fops = &drm_fops,
156         .name = "pl111",
157         .desc = DRIVER_DESC,
158         .date = "20170317",
159         .major = 1,
160         .minor = 0,
161         .patchlevel = 0,
162         .dumb_create = drm_gem_cma_dumb_create,
163         .gem_free_object_unlocked = drm_gem_cma_free_object,
164         .gem_vm_ops = &drm_gem_cma_vm_ops,
165
166         .enable_vblank = pl111_enable_vblank,
167         .disable_vblank = pl111_disable_vblank,
168
169         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
170         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
171         .gem_prime_import = drm_gem_prime_import,
172         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
173         .gem_prime_export = drm_gem_prime_export,
174         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
175
176 #if defined(CONFIG_DEBUG_FS)
177         .debugfs_init = pl111_debugfs_init,
178 #endif
179 };
180
181 static int pl111_amba_probe(struct amba_device *amba_dev,
182                             const struct amba_id *id)
183 {
184         struct device *dev = &amba_dev->dev;
185         struct pl111_drm_dev_private *priv;
186         struct drm_device *drm;
187         int ret;
188
189         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
190         if (!priv)
191                 return -ENOMEM;
192
193         drm = drm_dev_alloc(&pl111_drm_driver, dev);
194         if (IS_ERR(drm))
195                 return PTR_ERR(drm);
196         amba_set_drvdata(amba_dev, drm);
197         priv->drm = drm;
198         drm->dev_private = priv;
199
200         priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
201         if (IS_ERR(priv->regs)) {
202                 dev_err(dev, "%s failed mmio\n", __func__);
203                 return PTR_ERR(priv->regs);
204         }
205
206         /* turn off interrupts before requesting the irq */
207         writel(0, priv->regs + CLCD_PL111_IENB);
208
209         ret = devm_request_irq(dev, amba_dev->irq[0], pl111_irq, 0,
210                                "pl111", priv);
211         if (ret != 0) {
212                 dev_err(dev, "%s failed irq %d\n", __func__, ret);
213                 return ret;
214         }
215
216         ret = pl111_modeset_init(drm);
217         if (ret != 0)
218                 goto dev_unref;
219
220         ret = drm_dev_register(drm, 0);
221         if (ret < 0)
222                 goto dev_unref;
223
224         return 0;
225
226 dev_unref:
227         drm_dev_unref(drm);
228         return ret;
229 }
230
231 static int pl111_amba_remove(struct amba_device *amba_dev)
232 {
233         struct drm_device *drm = amba_get_drvdata(amba_dev);
234         struct pl111_drm_dev_private *priv = drm->dev_private;
235
236         drm_dev_unregister(drm);
237         if (priv->fbdev)
238                 drm_fbdev_cma_fini(priv->fbdev);
239         drm_mode_config_cleanup(drm);
240         drm_dev_unref(drm);
241
242         return 0;
243 }
244
245 static struct amba_id pl111_id_table[] = {
246         {
247                 .id = 0x00041111,
248                 .mask = 0x000fffff,
249         },
250         {0, 0},
251 };
252
253 static struct amba_driver pl111_amba_driver __maybe_unused = {
254         .drv = {
255                 .name = "drm-clcd-pl111",
256         },
257         .probe = pl111_amba_probe,
258         .remove = pl111_amba_remove,
259         .id_table = pl111_id_table,
260 };
261
262 #ifdef CONFIG_ARM_AMBA
263 module_amba_driver(pl111_amba_driver);
264 #endif
265
266 MODULE_DESCRIPTION(DRIVER_DESC);
267 MODULE_AUTHOR("ARM Ltd.");
268 MODULE_LICENSE("GPL");