2 * Copyright (C) 2016 BayLibre, SAS
3 * Author: Neil Armstrong <narmstrong@baylibre.com>
4 * Copyright (C) 2014 Endless Mobile
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.
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.
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/>.
20 * Jasper St. Pierre <jstpierre@mecheye.net>
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>
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>
41 #include "meson_drv.h"
42 #include "meson_plane.h"
43 #include "meson_crtc.h"
44 #include "meson_venc_cvbs.h"
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"
52 #define DRIVER_NAME "meson"
53 #define DRIVER_DESC "Amlogic Meson DRM driver"
56 * DOC: Video Processing Unit
58 * VPU Handles the Global Video Processing, it includes management of the
59 * clocks gates, blocks reset lines and power domains.
63 * - Full reset of entire video processing HW blocks
64 * - Scaling and setup of the VPU clock
66 * - Powering up video processing HW blocks
67 * - Powering Up HDMI controller and PHY
70 static void meson_fb_output_poll_changed(struct drm_device *dev)
72 struct meson_drm *priv = dev->dev_private;
74 drm_fbdev_cma_hotplug_event(priv->fbdev);
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,
84 static irqreturn_t meson_irq(int irq, void *arg)
86 struct drm_device *dev = arg;
87 struct meson_drm *priv = dev->dev_private;
89 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
96 DEFINE_DRM_GEM_CMA_FOPS(fops);
98 static struct drm_driver meson_driver = {
99 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
100 DRIVER_MODESET | DRIVER_PRIME |
104 .irq_handler = meson_irq,
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,
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,
131 static bool meson_vpu_has_available_connectors(struct device *dev)
133 struct device_node *ep, *remote;
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);
149 static struct regmap_config meson_regmap_config = {
153 .max_register = 0x1000,
156 static int meson_drv_bind_master(struct device *dev, bool has_components)
158 struct platform_device *pdev = to_platform_device(dev);
159 struct meson_drm *priv;
160 struct drm_device *drm;
161 struct resource *res;
165 /* Checks if an output connector is available */
166 if (!meson_vpu_has_available_connectors(dev)) {
167 dev_err(dev, "No output connector available\n");
171 drm = drm_dev_alloc(&meson_driver, dev);
175 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
180 drm->dev_private = priv;
184 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
185 regs = devm_ioremap_resource(dev, res);
191 priv->io_base = regs;
193 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
194 /* Simply ioremap since it may be a shared register zone */
195 regs = devm_ioremap(dev, res->start, resource_size(res));
197 ret = -EADDRNOTAVAIL;
201 priv->hhi = devm_regmap_init_mmio(dev, regs,
202 &meson_regmap_config);
203 if (IS_ERR(priv->hhi)) {
204 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
205 ret = PTR_ERR(priv->hhi);
209 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
210 /* Simply ioremap since it may be a shared register zone */
211 regs = devm_ioremap(dev, res->start, resource_size(res));
213 ret = -EADDRNOTAVAIL;
217 priv->dmc = devm_regmap_init_mmio(dev, regs,
218 &meson_regmap_config);
219 if (IS_ERR(priv->dmc)) {
220 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
221 ret = PTR_ERR(priv->dmc);
225 priv->vsync_irq = platform_get_irq(pdev, 0);
227 ret = drm_vblank_init(drm, 1);
231 drm_mode_config_init(drm);
232 drm->mode_config.max_width = 3840;
233 drm->mode_config.max_height = 2160;
234 drm->mode_config.funcs = &meson_mode_config_funcs;
236 /* Hardware Initialization */
238 meson_venc_init(priv);
239 meson_vpp_init(priv);
240 meson_viu_init(priv);
242 /* Encoder Initialization */
244 ret = meson_venc_cvbs_create(priv);
248 if (has_components) {
249 ret = component_bind_all(drm->dev, drm);
251 dev_err(drm->dev, "Couldn't bind all components\n");
256 ret = meson_plane_create(priv);
260 ret = meson_crtc_create(priv);
264 ret = drm_irq_install(drm, priv->vsync_irq);
268 drm_mode_config_reset(drm);
270 priv->fbdev = drm_fbdev_cma_init(drm, 32,
271 drm->mode_config.num_connector);
272 if (IS_ERR(priv->fbdev)) {
273 ret = PTR_ERR(priv->fbdev);
277 drm_kms_helper_poll_init(drm);
279 platform_set_drvdata(pdev, priv);
281 ret = drm_dev_register(drm, 0);
288 drm_irq_uninstall(drm);
295 static int meson_drv_bind(struct device *dev)
297 return meson_drv_bind_master(dev, true);
300 static void meson_drv_unbind(struct device *dev)
302 struct meson_drm *priv = dev_get_drvdata(dev);
303 struct drm_device *drm = priv->drm;
305 drm_dev_unregister(drm);
306 drm_irq_uninstall(drm);
307 drm_kms_helper_poll_fini(drm);
308 drm_fbdev_cma_fini(priv->fbdev);
309 drm_mode_config_cleanup(drm);
314 static const struct component_master_ops meson_drv_master_ops = {
315 .bind = meson_drv_bind,
316 .unbind = meson_drv_unbind,
319 static int compare_of(struct device *dev, void *data)
321 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
324 return dev->of_node == data;
327 /* Possible connectors nodes to ignore */
328 static const struct of_device_id connectors_match[] = {
329 { .compatible = "composite-video-connector" },
330 { .compatible = "svideo-connector" },
331 { .compatible = "hdmi-connector" },
332 { .compatible = "dvi-connector" },
336 static int meson_probe_remote(struct platform_device *pdev,
337 struct component_match **match,
338 struct device_node *parent,
339 struct device_node *remote)
341 struct device_node *ep, *remote_node;
344 /* If node is a connector, return and do not add to match table */
345 if (of_match_node(connectors_match, remote))
348 component_match_add(&pdev->dev, match, compare_of, remote);
350 for_each_endpoint_of_node(remote, ep) {
351 remote_node = of_graph_get_remote_port_parent(ep);
353 remote_node == parent || /* Ignore parent endpoint */
354 !of_device_is_available(remote_node)) {
355 of_node_put(remote_node);
359 count += meson_probe_remote(pdev, match, remote, remote_node);
361 of_node_put(remote_node);
367 static void meson_drv_shutdown(struct platform_device *pdev)
369 struct meson_drm *priv = dev_get_drvdata(&pdev->dev);
374 drm_kms_helper_poll_fini(priv->drm);
375 drm_atomic_helper_shutdown(priv->drm);
378 static int meson_drv_probe(struct platform_device *pdev)
380 struct component_match *match = NULL;
381 struct device_node *np = pdev->dev.of_node;
382 struct device_node *ep, *remote;
385 for_each_endpoint_of_node(np, ep) {
386 remote = of_graph_get_remote_port_parent(ep);
387 if (!remote || !of_device_is_available(remote)) {
392 count += meson_probe_remote(pdev, &match, np, remote);
397 return meson_drv_bind_master(&pdev->dev, false);
399 /* If some endpoints were found, initialize the nodes */
401 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
403 return component_master_add_with_match(&pdev->dev,
404 &meson_drv_master_ops,
408 /* If no output endpoints were available, simply bail out */
412 static const struct of_device_id dt_match[] = {
413 { .compatible = "amlogic,meson-gxbb-vpu" },
414 { .compatible = "amlogic,meson-gxl-vpu" },
415 { .compatible = "amlogic,meson-gxm-vpu" },
418 MODULE_DEVICE_TABLE(of, dt_match);
420 static struct platform_driver meson_drm_platform_driver = {
421 .probe = meson_drv_probe,
422 .shutdown = meson_drv_shutdown,
425 .of_match_table = dt_match,
429 module_platform_driver(meson_drm_platform_driver);
431 MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
432 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
433 MODULE_DESCRIPTION(DRIVER_DESC);
434 MODULE_LICENSE("GPL");