2 * Copyright (C) 2016 BayLibre, SAS
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/>.
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_gem_framebuffer_helper.h>
38 #include <drm/drm_fb_cma_helper.h>
39 #include <drm/drm_rect.h>
40 #include <drm/drm_fb_helper.h>
42 #include "meson_drv.h"
43 #include "meson_plane.h"
44 #include "meson_crtc.h"
45 #include "meson_venc_cvbs.h"
47 #include "meson_vpp.h"
48 #include "meson_viu.h"
49 #include "meson_venc.h"
50 #include "meson_canvas.h"
51 #include "meson_registers.h"
53 #define DRIVER_NAME "meson"
54 #define DRIVER_DESC "Amlogic Meson DRM driver"
57 * DOC: Video Processing Unit
59 * VPU Handles the Global Video Processing, it includes management of the
60 * clocks gates, blocks reset lines and power domains.
64 * - Full reset of entire video processing HW blocks
65 * - Scaling and setup of the VPU clock
67 * - Powering up video processing HW blocks
68 * - Powering Up HDMI controller and PHY
71 static void meson_fb_output_poll_changed(struct drm_device *dev)
73 struct meson_drm *priv = dev->dev_private;
75 drm_fbdev_cma_hotplug_event(priv->fbdev);
78 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
79 .output_poll_changed = meson_fb_output_poll_changed,
80 .atomic_check = drm_atomic_helper_check,
81 .atomic_commit = drm_atomic_helper_commit,
82 .fb_create = drm_gem_fb_create,
85 static irqreturn_t meson_irq(int irq, void *arg)
87 struct drm_device *dev = arg;
88 struct meson_drm *priv = dev->dev_private;
90 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
97 DEFINE_DRM_GEM_CMA_FOPS(fops);
99 static struct drm_driver meson_driver = {
100 .driver_features = DRIVER_HAVE_IRQ | DRIVER_GEM |
101 DRIVER_MODESET | DRIVER_PRIME |
105 .irq_handler = meson_irq,
108 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
109 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
110 .gem_prime_import = drm_gem_prime_import,
111 .gem_prime_export = drm_gem_prime_export,
112 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
113 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
114 .gem_prime_vmap = drm_gem_cma_prime_vmap,
115 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
116 .gem_prime_mmap = drm_gem_cma_prime_mmap,
119 .dumb_create = drm_gem_cma_dumb_create,
120 .gem_free_object_unlocked = drm_gem_cma_free_object,
121 .gem_vm_ops = &drm_gem_cma_vm_ops,
132 static bool meson_vpu_has_available_connectors(struct device *dev)
134 struct device_node *ep, *remote;
136 /* Parses each endpoint and check if remote exists */
137 for_each_endpoint_of_node(dev->of_node, ep) {
138 /* If the endpoint node exists, consider it enabled */
139 remote = of_graph_get_remote_port(ep);
147 static struct regmap_config meson_regmap_config = {
151 .max_register = 0x1000,
154 static void meson_vpu_init(struct meson_drm *priv)
156 writel_relaxed(0x210000, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
157 writel_relaxed(0x10000, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
158 writel_relaxed(0x900000, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
159 writel_relaxed(0x20000, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
162 static int meson_drv_bind_master(struct device *dev, bool has_components)
164 struct platform_device *pdev = to_platform_device(dev);
165 struct meson_drm *priv;
166 struct drm_device *drm;
167 struct resource *res;
171 /* Checks if an output connector is available */
172 if (!meson_vpu_has_available_connectors(dev)) {
173 dev_err(dev, "No output connector available\n");
177 drm = drm_dev_alloc(&meson_driver, dev);
181 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
186 drm->dev_private = priv;
190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
191 regs = devm_ioremap_resource(dev, res);
197 priv->io_base = regs;
199 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
202 /* Simply ioremap since it may be a shared register zone */
203 regs = devm_ioremap(dev, res->start, resource_size(res));
205 ret = -EADDRNOTAVAIL;
209 priv->hhi = devm_regmap_init_mmio(dev, regs,
210 &meson_regmap_config);
211 if (IS_ERR(priv->hhi)) {
212 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
213 ret = PTR_ERR(priv->hhi);
217 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
220 /* Simply ioremap since it may be a shared register zone */
221 regs = devm_ioremap(dev, res->start, resource_size(res));
223 ret = -EADDRNOTAVAIL;
227 priv->dmc = devm_regmap_init_mmio(dev, regs,
228 &meson_regmap_config);
229 if (IS_ERR(priv->dmc)) {
230 dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
231 ret = PTR_ERR(priv->dmc);
235 priv->vsync_irq = platform_get_irq(pdev, 0);
237 ret = drm_vblank_init(drm, 1);
241 drm_mode_config_init(drm);
242 drm->mode_config.max_width = 3840;
243 drm->mode_config.max_height = 2160;
244 drm->mode_config.funcs = &meson_mode_config_funcs;
246 /* Hardware Initialization */
248 meson_vpu_init(priv);
249 meson_venc_init(priv);
250 meson_vpp_init(priv);
251 meson_viu_init(priv);
253 /* Encoder Initialization */
255 ret = meson_venc_cvbs_create(priv);
259 if (has_components) {
260 ret = component_bind_all(drm->dev, drm);
262 dev_err(drm->dev, "Couldn't bind all components\n");
267 ret = meson_plane_create(priv);
271 ret = meson_crtc_create(priv);
275 ret = drm_irq_install(drm, priv->vsync_irq);
279 drm_mode_config_reset(drm);
281 priv->fbdev = drm_fbdev_cma_init(drm, 32,
282 drm->mode_config.num_connector);
283 if (IS_ERR(priv->fbdev)) {
284 ret = PTR_ERR(priv->fbdev);
288 drm_kms_helper_poll_init(drm);
290 platform_set_drvdata(pdev, priv);
292 ret = drm_dev_register(drm, 0);
304 static int meson_drv_bind(struct device *dev)
306 return meson_drv_bind_master(dev, true);
309 static void meson_drv_unbind(struct device *dev)
311 struct drm_device *drm = dev_get_drvdata(dev);
312 struct meson_drm *priv = drm->dev_private;
314 drm_dev_unregister(drm);
315 drm_kms_helper_poll_fini(drm);
316 drm_fbdev_cma_fini(priv->fbdev);
317 drm_mode_config_cleanup(drm);
322 static const struct component_master_ops meson_drv_master_ops = {
323 .bind = meson_drv_bind,
324 .unbind = meson_drv_unbind,
327 static int compare_of(struct device *dev, void *data)
329 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
332 return dev->of_node == data;
335 /* Possible connectors nodes to ignore */
336 static const struct of_device_id connectors_match[] = {
337 { .compatible = "composite-video-connector" },
338 { .compatible = "svideo-connector" },
339 { .compatible = "hdmi-connector" },
340 { .compatible = "dvi-connector" },
344 static int meson_probe_remote(struct platform_device *pdev,
345 struct component_match **match,
346 struct device_node *parent,
347 struct device_node *remote)
349 struct device_node *ep, *remote_node;
352 /* If node is a connector, return and do not add to match table */
353 if (of_match_node(connectors_match, remote))
356 component_match_add(&pdev->dev, match, compare_of, remote);
358 for_each_endpoint_of_node(remote, ep) {
359 remote_node = of_graph_get_remote_port_parent(ep);
361 remote_node == parent || /* Ignore parent endpoint */
362 !of_device_is_available(remote_node))
365 count += meson_probe_remote(pdev, match, remote, remote_node);
367 of_node_put(remote_node);
373 static int meson_drv_probe(struct platform_device *pdev)
375 struct component_match *match = NULL;
376 struct device_node *np = pdev->dev.of_node;
377 struct device_node *ep, *remote;
380 for_each_endpoint_of_node(np, ep) {
381 remote = of_graph_get_remote_port_parent(ep);
382 if (!remote || !of_device_is_available(remote))
385 count += meson_probe_remote(pdev, &match, np, remote);
389 return meson_drv_bind_master(&pdev->dev, false);
391 /* If some endpoints were found, initialize the nodes */
393 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
395 return component_master_add_with_match(&pdev->dev,
396 &meson_drv_master_ops,
400 /* If no output endpoints were available, simply bail out */
404 static const struct of_device_id dt_match[] = {
405 { .compatible = "amlogic,meson-gxbb-vpu" },
406 { .compatible = "amlogic,meson-gxl-vpu" },
407 { .compatible = "amlogic,meson-gxm-vpu" },
410 MODULE_DEVICE_TABLE(of, dt_match);
412 static struct platform_driver meson_drm_platform_driver = {
413 .probe = meson_drv_probe,
416 .of_match_table = dt_match,
420 module_platform_driver(meson_drm_platform_driver);
424 MODULE_DESCRIPTION(DRIVER_DESC);
425 MODULE_LICENSE("GPL");