GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / gpu / drm / meson / meson_drv.c
1 /*
2  * Copyright (C) 2016 BayLibre, SAS
3  * Author: Neil Armstrong <narmstrong@baylibre.com>
4  * Copyright (C) 2014 Endless Mobile
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * Written by:
20  *     Jasper St. Pierre <jstpierre@mecheye.net>
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/platform_device.h>
27 #include <linux/component.h>
28 #include <linux/of_graph.h>
29
30 #include <drm/drmP.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_flip_work.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_plane_helper.h>
36 #include <drm/drm_gem_cma_helper.h>
37 #include <drm/drm_fb_cma_helper.h>
38 #include <drm/drm_rect.h>
39 #include <drm/drm_fb_helper.h>
40
41 #include "meson_drv.h"
42 #include "meson_plane.h"
43 #include "meson_crtc.h"
44 #include "meson_venc_cvbs.h"
45
46 #include "meson_vpp.h"
47 #include "meson_viu.h"
48 #include "meson_venc.h"
49 #include "meson_canvas.h"
50 #include "meson_registers.h"
51
52 #define DRIVER_NAME "meson"
53 #define DRIVER_DESC "Amlogic Meson DRM driver"
54
55 /**
56  * DOC: Video Processing Unit
57  *
58  * VPU Handles the Global Video Processing, it includes management of the
59  * clocks gates, blocks reset lines and power domains.
60  *
61  * What is missing :
62  *
63  * - Full reset of entire video processing HW blocks
64  * - Scaling and setup of the VPU clock
65  * - Bus clock gates
66  * - Powering up video processing HW blocks
67  * - Powering Up HDMI controller and PHY
68  */
69
70 static void meson_fb_output_poll_changed(struct drm_device *dev)
71 {
72         struct meson_drm *priv = dev->dev_private;
73
74         drm_fbdev_cma_hotplug_event(priv->fbdev);
75 }
76
77 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
78         .output_poll_changed = meson_fb_output_poll_changed,
79         .atomic_check        = drm_atomic_helper_check,
80         .atomic_commit       = drm_atomic_helper_commit,
81         .fb_create           = drm_fb_cma_create,
82 };
83
84 static irqreturn_t meson_irq(int irq, void *arg)
85 {
86         struct drm_device *dev = arg;
87         struct meson_drm *priv = dev->dev_private;
88
89         (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
90
91         meson_crtc_irq(priv);
92
93         return IRQ_HANDLED;
94 }
95
96 DEFINE_DRM_GEM_CMA_FOPS(fops);
97
98 static struct drm_driver meson_driver = {
99         .driver_features        = DRIVER_HAVE_IRQ | DRIVER_GEM |
100                                   DRIVER_MODESET | DRIVER_PRIME |
101                                   DRIVER_ATOMIC,
102
103         /* IRQ */
104         .irq_handler            = meson_irq,
105
106         /* PRIME Ops */
107         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
108         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
109         .gem_prime_import       = drm_gem_prime_import,
110         .gem_prime_export       = drm_gem_prime_export,
111         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
112         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
113         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
114         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
115         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
116
117         /* GEM Ops */
118         .dumb_create            = drm_gem_cma_dumb_create,
119         .gem_free_object_unlocked = drm_gem_cma_free_object,
120         .gem_vm_ops             = &drm_gem_cma_vm_ops,
121
122         /* Misc */
123         .fops                   = &fops,
124         .name                   = DRIVER_NAME,
125         .desc                   = DRIVER_DESC,
126         .date                   = "20161109",
127         .major                  = 1,
128         .minor                  = 0,
129 };
130
131 static bool meson_vpu_has_available_connectors(struct device *dev)
132 {
133         struct device_node *ep, *remote;
134
135         /* Parses each endpoint and check if remote exists */
136         for_each_endpoint_of_node(dev->of_node, ep) {
137                 /* If the endpoint node exists, consider it enabled */
138                 remote = of_graph_get_remote_port(ep);
139                 if (remote)
140                         return true;
141         }
142
143         return false;
144 }
145
146 static struct regmap_config meson_regmap_config = {
147         .reg_bits       = 32,
148         .val_bits       = 32,
149         .reg_stride     = 4,
150         .max_register   = 0x1000,
151 };
152
153 static int meson_drv_bind_master(struct device *dev, bool has_components)
154 {
155         struct platform_device *pdev = to_platform_device(dev);
156         struct meson_drm *priv;
157         struct drm_device *drm;
158         struct resource *res;
159         void __iomem *regs;
160         int ret;
161
162         /* Checks if an output connector is available */
163         if (!meson_vpu_has_available_connectors(dev)) {
164                 dev_err(dev, "No output connector available\n");
165                 return -ENODEV;
166         }
167
168         drm = drm_dev_alloc(&meson_driver, dev);
169         if (IS_ERR(drm))
170                 return PTR_ERR(drm);
171
172         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
173         if (!priv) {
174                 ret = -ENOMEM;
175                 goto free_drm;
176         }
177         drm->dev_private = priv;
178         priv->drm = drm;
179         priv->dev = dev;
180
181         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
182         regs = devm_ioremap_resource(dev, res);
183         if (IS_ERR(regs)) {
184                 ret = PTR_ERR(regs);
185                 goto free_drm;
186         }
187
188         priv->io_base = regs;
189
190         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
191         /* Simply ioremap since it may be a shared register zone */
192         regs = devm_ioremap(dev, res->start, resource_size(res));
193         if (!regs) {
194                 ret = -EADDRNOTAVAIL;
195                 goto free_drm;
196         }
197
198         priv->hhi = devm_regmap_init_mmio(dev, regs,
199                                           &meson_regmap_config);
200         if (IS_ERR(priv->hhi)) {
201                 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
202                 ret = PTR_ERR(priv->hhi);
203                 goto free_drm;
204         }
205
206         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
207         /* Simply ioremap since it may be a shared register zone */
208         regs = devm_ioremap(dev, res->start, resource_size(res));
209         if (!regs) {
210                 ret = -EADDRNOTAVAIL;
211                 goto free_drm;
212         }
213
214         priv->dmc = devm_regmap_init_mmio(dev, regs,
215                                           &meson_regmap_config);
216         if (IS_ERR(priv->dmc)) {
217                 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
218                 ret = PTR_ERR(priv->dmc);
219                 goto free_drm;
220         }
221
222         priv->vsync_irq = platform_get_irq(pdev, 0);
223
224         ret = drm_vblank_init(drm, 1);
225         if (ret)
226                 goto free_drm;
227
228         drm_mode_config_init(drm);
229         drm->mode_config.max_width = 3840;
230         drm->mode_config.max_height = 2160;
231         drm->mode_config.funcs = &meson_mode_config_funcs;
232
233         /* Hardware Initialization */
234
235         meson_venc_init(priv);
236         meson_vpp_init(priv);
237         meson_viu_init(priv);
238
239         /* Encoder Initialization */
240
241         ret = meson_venc_cvbs_create(priv);
242         if (ret)
243                 goto free_drm;
244
245         if (has_components) {
246                 ret = component_bind_all(drm->dev, drm);
247                 if (ret) {
248                         dev_err(drm->dev, "Couldn't bind all components\n");
249                         goto free_drm;
250                 }
251         }
252
253         ret = meson_plane_create(priv);
254         if (ret)
255                 goto free_drm;
256
257         ret = meson_crtc_create(priv);
258         if (ret)
259                 goto free_drm;
260
261         ret = drm_irq_install(drm, priv->vsync_irq);
262         if (ret)
263                 goto free_drm;
264
265         drm_mode_config_reset(drm);
266
267         priv->fbdev = drm_fbdev_cma_init(drm, 32,
268                                          drm->mode_config.num_connector);
269         if (IS_ERR(priv->fbdev)) {
270                 ret = PTR_ERR(priv->fbdev);
271                 goto free_drm;
272         }
273
274         drm_kms_helper_poll_init(drm);
275
276         platform_set_drvdata(pdev, priv);
277
278         ret = drm_dev_register(drm, 0);
279         if (ret)
280                 goto uninstall_irq;
281
282         return 0;
283
284 uninstall_irq:
285         drm_irq_uninstall(drm);
286 free_drm:
287         drm_dev_unref(drm);
288
289         return ret;
290 }
291
292 static int meson_drv_bind(struct device *dev)
293 {
294         return meson_drv_bind_master(dev, true);
295 }
296
297 static void meson_drv_unbind(struct device *dev)
298 {
299         struct meson_drm *priv = dev_get_drvdata(dev);
300         struct drm_device *drm = priv->drm;
301
302         drm_dev_unregister(drm);
303         drm_irq_uninstall(drm);
304         drm_kms_helper_poll_fini(drm);
305         drm_fbdev_cma_fini(priv->fbdev);
306         drm_mode_config_cleanup(drm);
307         drm_dev_unref(drm);
308
309 }
310
311 static const struct component_master_ops meson_drv_master_ops = {
312         .bind   = meson_drv_bind,
313         .unbind = meson_drv_unbind,
314 };
315
316 static int compare_of(struct device *dev, void *data)
317 {
318         DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
319                          dev->of_node, data);
320
321         return dev->of_node == data;
322 }
323
324 /* Possible connectors nodes to ignore */
325 static const struct of_device_id connectors_match[] = {
326         { .compatible = "composite-video-connector" },
327         { .compatible = "svideo-connector" },
328         { .compatible = "hdmi-connector" },
329         { .compatible = "dvi-connector" },
330         {}
331 };
332
333 static int meson_probe_remote(struct platform_device *pdev,
334                               struct component_match **match,
335                               struct device_node *parent,
336                               struct device_node *remote)
337 {
338         struct device_node *ep, *remote_node;
339         int count = 1;
340
341         /* If node is a connector, return and do not add to match table */
342         if (of_match_node(connectors_match, remote))
343                 return 1;
344
345         component_match_add(&pdev->dev, match, compare_of, remote);
346
347         for_each_endpoint_of_node(remote, ep) {
348                 remote_node = of_graph_get_remote_port_parent(ep);
349                 if (!remote_node ||
350                     remote_node == parent || /* Ignore parent endpoint */
351                     !of_device_is_available(remote_node)) {
352                         of_node_put(remote_node);
353                         continue;
354                 }
355
356                 count += meson_probe_remote(pdev, match, remote, remote_node);
357
358                 of_node_put(remote_node);
359         }
360
361         return count;
362 }
363
364 static void meson_drv_shutdown(struct platform_device *pdev)
365 {
366         struct meson_drm *priv = dev_get_drvdata(&pdev->dev);
367
368         if (!priv)
369                 return;
370
371         drm_kms_helper_poll_fini(priv->drm);
372         drm_atomic_helper_shutdown(priv->drm);
373 }
374
375 static int meson_drv_probe(struct platform_device *pdev)
376 {
377         struct component_match *match = NULL;
378         struct device_node *np = pdev->dev.of_node;
379         struct device_node *ep, *remote;
380         int count = 0;
381
382         for_each_endpoint_of_node(np, ep) {
383                 remote = of_graph_get_remote_port_parent(ep);
384                 if (!remote || !of_device_is_available(remote)) {
385                         of_node_put(remote);
386                         continue;
387                 }
388
389                 count += meson_probe_remote(pdev, &match, np, remote);
390                 of_node_put(remote);
391         }
392
393         if (count && !match)
394                 return meson_drv_bind_master(&pdev->dev, false);
395
396         /* If some endpoints were found, initialize the nodes */
397         if (count) {
398                 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
399
400                 return component_master_add_with_match(&pdev->dev,
401                                                        &meson_drv_master_ops,
402                                                        match);
403         }
404
405         /* If no output endpoints were available, simply bail out */
406         return 0;
407 };
408
409 static const struct of_device_id dt_match[] = {
410         { .compatible = "amlogic,meson-gxbb-vpu" },
411         { .compatible = "amlogic,meson-gxl-vpu" },
412         { .compatible = "amlogic,meson-gxm-vpu" },
413         {}
414 };
415 MODULE_DEVICE_TABLE(of, dt_match);
416
417 static struct platform_driver meson_drm_platform_driver = {
418         .probe      = meson_drv_probe,
419         .shutdown   = meson_drv_shutdown,
420         .driver     = {
421                 .name   = "meson-drm",
422                 .of_match_table = dt_match,
423         },
424 };
425
426 module_platform_driver(meson_drm_platform_driver);
427
428 MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
429 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
430 MODULE_DESCRIPTION(DRIVER_DESC);
431 MODULE_LICENSE("GPL");