1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 BayLibre, SAS
5 * Copyright (C) 2014 Endless Mobile
11 #include <linux/component.h>
12 #include <linux/module.h>
13 #include <linux/of_graph.h>
14 #include <linux/platform_device.h>
15 #include <linux/soc/amlogic/meson-canvas.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_irq.h>
23 #include <drm/drm_modeset_helper_vtables.h>
24 #include <drm/drm_probe_helper.h>
25 #include <drm/drm_vblank.h>
27 #include "meson_crtc.h"
28 #include "meson_drv.h"
29 #include "meson_overlay.h"
30 #include "meson_plane.h"
31 #include "meson_osd_afbcd.h"
32 #include "meson_registers.h"
33 #include "meson_venc_cvbs.h"
34 #include "meson_viu.h"
35 #include "meson_vpp.h"
36 #include "meson_rdma.h"
38 #define DRIVER_NAME "meson"
39 #define DRIVER_DESC "Amlogic Meson DRM driver"
42 * DOC: Video Processing Unit
44 * VPU Handles the Global Video Processing, it includes management of the
45 * clocks gates, blocks reset lines and power domains.
49 * - Full reset of entire video processing HW blocks
50 * - Scaling and setup of the VPU clock
52 * - Powering up video processing HW blocks
53 * - Powering Up HDMI controller and PHY
56 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
57 .atomic_check = drm_atomic_helper_check,
58 .atomic_commit = drm_atomic_helper_commit,
59 .fb_create = drm_gem_fb_create,
62 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = {
63 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
66 static irqreturn_t meson_irq(int irq, void *arg)
68 struct drm_device *dev = arg;
69 struct meson_drm *priv = dev->dev_private;
71 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
78 static int meson_dumb_create(struct drm_file *file, struct drm_device *dev,
79 struct drm_mode_create_dumb *args)
82 * We need 64bytes aligned stride, and PAGE aligned size
84 args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), SZ_64);
85 args->size = PAGE_ALIGN(args->pitch * args->height);
87 return drm_gem_cma_dumb_create_internal(file, dev, args);
90 DEFINE_DRM_GEM_CMA_FOPS(fops);
92 static struct drm_driver meson_driver = {
93 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
96 .irq_handler = meson_irq,
99 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
100 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
101 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
102 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
103 .gem_prime_vmap = drm_gem_cma_prime_vmap,
104 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
105 .gem_prime_mmap = drm_gem_cma_prime_mmap,
108 .dumb_create = meson_dumb_create,
109 .gem_free_object_unlocked = drm_gem_cma_free_object,
110 .gem_vm_ops = &drm_gem_cma_vm_ops,
121 static bool meson_vpu_has_available_connectors(struct device *dev)
123 struct device_node *ep, *remote;
125 /* Parses each endpoint and check if remote exists */
126 for_each_endpoint_of_node(dev->of_node, ep) {
127 /* If the endpoint node exists, consider it enabled */
128 remote = of_graph_get_remote_port(ep);
136 static struct regmap_config meson_regmap_config = {
140 .max_register = 0x1000,
143 static void meson_vpu_init(struct meson_drm *priv)
148 * Slave dc0 and dc5 connected to master port 1.
149 * By default other slaves are connected to master port 0.
151 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1) |
152 VPU_RDARB_SLAVE_TO_MASTER_PORT(5, 1);
153 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
155 /* Slave dc0 connected to master port 1 */
156 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1);
157 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
159 /* Slave dc4 and dc7 connected to master port 1 */
160 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(4, 1) |
161 VPU_RDARB_SLAVE_TO_MASTER_PORT(7, 1);
162 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
164 /* Slave dc1 connected to master port 1 */
165 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(1, 1);
166 writel_relaxed(value, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
169 static void meson_remove_framebuffers(void)
171 struct apertures_struct *ap;
173 ap = alloc_apertures(1);
177 /* The framebuffer can be located anywhere in RAM */
178 ap->ranges[0].base = 0;
179 ap->ranges[0].size = ~0;
181 drm_fb_helper_remove_conflicting_framebuffers(ap, "meson-drm-fb",
186 static int meson_drv_bind_master(struct device *dev, bool has_components)
188 struct platform_device *pdev = to_platform_device(dev);
189 const struct meson_drm_match_data *match;
190 struct meson_drm *priv;
191 struct drm_device *drm;
192 struct resource *res;
196 /* Checks if an output connector is available */
197 if (!meson_vpu_has_available_connectors(dev)) {
198 dev_err(dev, "No output connector available\n");
202 match = of_device_get_match_data(dev);
206 drm = drm_dev_alloc(&meson_driver, dev);
210 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
215 drm->dev_private = priv;
218 priv->compat = match->compat;
219 priv->afbcd.ops = match->afbcd_ops;
221 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
222 regs = devm_ioremap_resource(dev, res);
228 priv->io_base = regs;
230 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
235 /* Simply ioremap since it may be a shared register zone */
236 regs = devm_ioremap(dev, res->start, resource_size(res));
238 ret = -EADDRNOTAVAIL;
242 priv->hhi = devm_regmap_init_mmio(dev, regs,
243 &meson_regmap_config);
244 if (IS_ERR(priv->hhi)) {
245 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
246 ret = PTR_ERR(priv->hhi);
250 priv->canvas = meson_canvas_get(dev);
251 if (IS_ERR(priv->canvas)) {
252 ret = PTR_ERR(priv->canvas);
256 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_osd1);
259 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_0);
261 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
264 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_1);
266 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
267 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
270 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_2);
272 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
273 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
274 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
278 priv->vsync_irq = platform_get_irq(pdev, 0);
280 ret = drm_vblank_init(drm, 1);
284 /* Remove early framebuffers (ie. simplefb) */
285 meson_remove_framebuffers();
287 ret = drmm_mode_config_init(drm);
290 drm->mode_config.max_width = 3840;
291 drm->mode_config.max_height = 2160;
292 drm->mode_config.funcs = &meson_mode_config_funcs;
293 drm->mode_config.helper_private = &meson_mode_config_helpers;
295 /* Hardware Initialization */
297 meson_vpu_init(priv);
298 meson_venc_init(priv);
299 meson_vpp_init(priv);
300 meson_viu_init(priv);
301 if (priv->afbcd.ops) {
302 ret = priv->afbcd.ops->init(priv);
307 /* Encoder Initialization */
309 ret = meson_venc_cvbs_create(priv);
313 if (has_components) {
314 ret = component_bind_all(drm->dev, drm);
316 dev_err(drm->dev, "Couldn't bind all components\n");
321 ret = meson_plane_create(priv);
325 ret = meson_overlay_create(priv);
329 ret = meson_crtc_create(priv);
333 ret = drm_irq_install(drm, priv->vsync_irq);
337 drm_mode_config_reset(drm);
339 drm_kms_helper_poll_init(drm);
341 platform_set_drvdata(pdev, priv);
343 ret = drm_dev_register(drm, 0);
347 drm_fbdev_generic_setup(drm, 32);
352 drm_irq_uninstall(drm);
359 static int meson_drv_bind(struct device *dev)
361 return meson_drv_bind_master(dev, true);
364 static void meson_drv_unbind(struct device *dev)
366 struct meson_drm *priv = dev_get_drvdata(dev);
367 struct drm_device *drm = priv->drm;
370 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
371 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
372 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
373 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_2);
376 if (priv->afbcd.ops) {
377 priv->afbcd.ops->reset(priv);
378 meson_rdma_free(priv);
381 drm_dev_unregister(drm);
382 drm_irq_uninstall(drm);
383 drm_kms_helper_poll_fini(drm);
387 static const struct component_master_ops meson_drv_master_ops = {
388 .bind = meson_drv_bind,
389 .unbind = meson_drv_unbind,
392 static int __maybe_unused meson_drv_pm_suspend(struct device *dev)
394 struct meson_drm *priv = dev_get_drvdata(dev);
399 return drm_mode_config_helper_suspend(priv->drm);
402 static int __maybe_unused meson_drv_pm_resume(struct device *dev)
404 struct meson_drm *priv = dev_get_drvdata(dev);
409 meson_vpu_init(priv);
410 meson_venc_init(priv);
411 meson_vpp_init(priv);
412 meson_viu_init(priv);
414 priv->afbcd.ops->init(priv);
416 drm_mode_config_helper_resume(priv->drm);
421 static int compare_of(struct device *dev, void *data)
423 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
426 return dev->of_node == data;
429 /* Possible connectors nodes to ignore */
430 static const struct of_device_id connectors_match[] = {
431 { .compatible = "composite-video-connector" },
432 { .compatible = "svideo-connector" },
433 { .compatible = "hdmi-connector" },
434 { .compatible = "dvi-connector" },
438 static int meson_probe_remote(struct platform_device *pdev,
439 struct component_match **match,
440 struct device_node *parent,
441 struct device_node *remote)
443 struct device_node *ep, *remote_node;
446 /* If node is a connector, return and do not add to match table */
447 if (of_match_node(connectors_match, remote))
450 component_match_add(&pdev->dev, match, compare_of, remote);
452 for_each_endpoint_of_node(remote, ep) {
453 remote_node = of_graph_get_remote_port_parent(ep);
455 remote_node == parent || /* Ignore parent endpoint */
456 !of_device_is_available(remote_node)) {
457 of_node_put(remote_node);
461 count += meson_probe_remote(pdev, match, remote, remote_node);
463 of_node_put(remote_node);
469 static int meson_drv_probe(struct platform_device *pdev)
471 struct component_match *match = NULL;
472 struct device_node *np = pdev->dev.of_node;
473 struct device_node *ep, *remote;
476 for_each_endpoint_of_node(np, ep) {
477 remote = of_graph_get_remote_port_parent(ep);
478 if (!remote || !of_device_is_available(remote)) {
483 count += meson_probe_remote(pdev, &match, np, remote);
488 return meson_drv_bind_master(&pdev->dev, false);
490 /* If some endpoints were found, initialize the nodes */
492 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
494 return component_master_add_with_match(&pdev->dev,
495 &meson_drv_master_ops,
499 /* If no output endpoints were available, simply bail out */
503 static struct meson_drm_match_data meson_drm_gxbb_data = {
504 .compat = VPU_COMPATIBLE_GXBB,
507 static struct meson_drm_match_data meson_drm_gxl_data = {
508 .compat = VPU_COMPATIBLE_GXL,
511 static struct meson_drm_match_data meson_drm_gxm_data = {
512 .compat = VPU_COMPATIBLE_GXM,
513 .afbcd_ops = &meson_afbcd_gxm_ops,
516 static struct meson_drm_match_data meson_drm_g12a_data = {
517 .compat = VPU_COMPATIBLE_G12A,
518 .afbcd_ops = &meson_afbcd_g12a_ops,
521 static const struct of_device_id dt_match[] = {
522 { .compatible = "amlogic,meson-gxbb-vpu",
523 .data = (void *)&meson_drm_gxbb_data },
524 { .compatible = "amlogic,meson-gxl-vpu",
525 .data = (void *)&meson_drm_gxl_data },
526 { .compatible = "amlogic,meson-gxm-vpu",
527 .data = (void *)&meson_drm_gxm_data },
528 { .compatible = "amlogic,meson-g12a-vpu",
529 .data = (void *)&meson_drm_g12a_data },
532 MODULE_DEVICE_TABLE(of, dt_match);
534 static const struct dev_pm_ops meson_drv_pm_ops = {
535 SET_SYSTEM_SLEEP_PM_OPS(meson_drv_pm_suspend, meson_drv_pm_resume)
538 static struct platform_driver meson_drm_platform_driver = {
539 .probe = meson_drv_probe,
542 .of_match_table = dt_match,
543 .pm = &meson_drv_pm_ops,
547 module_platform_driver(meson_drm_platform_driver);
551 MODULE_DESCRIPTION(DRIVER_DESC);
552 MODULE_LICENSE("GPL");