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/sys_soc.h>
15 #include <linux/platform_device.h>
16 #include <linux/soc/amlogic/meson-canvas.h>
18 #include <drm/drm_aperture.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_gem_cma_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_modeset_helper_vtables.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_vblank.h>
28 #include "meson_crtc.h"
29 #include "meson_drv.h"
30 #include "meson_overlay.h"
31 #include "meson_plane.h"
32 #include "meson_osd_afbcd.h"
33 #include "meson_registers.h"
34 #include "meson_venc_cvbs.h"
35 #include "meson_viu.h"
36 #include "meson_vpp.h"
37 #include "meson_rdma.h"
39 #define DRIVER_NAME "meson"
40 #define DRIVER_DESC "Amlogic Meson DRM driver"
43 * DOC: Video Processing Unit
45 * VPU Handles the Global Video Processing, it includes management of the
46 * clocks gates, blocks reset lines and power domains.
50 * - Full reset of entire video processing HW blocks
51 * - Scaling and setup of the VPU clock
53 * - Powering up video processing HW blocks
54 * - Powering Up HDMI controller and PHY
57 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
58 .atomic_check = drm_atomic_helper_check,
59 .atomic_commit = drm_atomic_helper_commit,
60 .fb_create = drm_gem_fb_create,
63 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = {
64 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
67 static irqreturn_t meson_irq(int irq, void *arg)
69 struct drm_device *dev = arg;
70 struct meson_drm *priv = dev->dev_private;
72 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
79 static int meson_dumb_create(struct drm_file *file, struct drm_device *dev,
80 struct drm_mode_create_dumb *args)
83 * We need 64bytes aligned stride, and PAGE aligned size
85 args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), SZ_64);
86 args->size = PAGE_ALIGN(args->pitch * args->height);
88 return drm_gem_cma_dumb_create_internal(file, dev, args);
91 DEFINE_DRM_GEM_CMA_FOPS(fops);
93 static const struct drm_driver meson_driver = {
94 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
97 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(meson_dumb_create),
108 static bool meson_vpu_has_available_connectors(struct device *dev)
110 struct device_node *ep, *remote;
112 /* Parses each endpoint and check if remote exists */
113 for_each_endpoint_of_node(dev->of_node, ep) {
114 /* If the endpoint node exists, consider it enabled */
115 remote = of_graph_get_remote_port(ep);
123 static struct regmap_config meson_regmap_config = {
127 .max_register = 0x1000,
130 static void meson_vpu_init(struct meson_drm *priv)
135 * Slave dc0 and dc5 connected to master port 1.
136 * By default other slaves are connected to master port 0.
138 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1) |
139 VPU_RDARB_SLAVE_TO_MASTER_PORT(5, 1);
140 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
142 /* Slave dc0 connected to master port 1 */
143 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1);
144 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
146 /* Slave dc4 and dc7 connected to master port 1 */
147 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(4, 1) |
148 VPU_RDARB_SLAVE_TO_MASTER_PORT(7, 1);
149 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
151 /* Slave dc1 connected to master port 1 */
152 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(1, 1);
153 writel_relaxed(value, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
156 struct meson_drm_soc_attr {
157 struct meson_drm_soc_limits limits;
158 const struct soc_device_attribute *attrs;
161 static const struct meson_drm_soc_attr meson_drm_soc_attrs[] = {
162 /* S805X/S805Y HDMI PLL won't lock for HDMI PHY freq > 1,65GHz */
165 .max_hdmi_phy_freq = 1650000,
167 .attrs = (const struct soc_device_attribute []) {
168 { .soc_id = "GXL (S805*)", },
174 static int meson_drv_bind_master(struct device *dev, bool has_components)
176 struct platform_device *pdev = to_platform_device(dev);
177 const struct meson_drm_match_data *match;
178 struct meson_drm *priv;
179 struct drm_device *drm;
180 struct resource *res;
184 /* Checks if an output connector is available */
185 if (!meson_vpu_has_available_connectors(dev)) {
186 dev_err(dev, "No output connector available\n");
190 match = of_device_get_match_data(dev);
194 drm = drm_dev_alloc(&meson_driver, dev);
198 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
203 drm->dev_private = priv;
206 priv->compat = match->compat;
207 priv->afbcd.ops = match->afbcd_ops;
209 regs = devm_platform_ioremap_resource_byname(pdev, "vpu");
215 priv->io_base = regs;
217 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
222 /* Simply ioremap since it may be a shared register zone */
223 regs = devm_ioremap(dev, res->start, resource_size(res));
225 ret = -EADDRNOTAVAIL;
229 priv->hhi = devm_regmap_init_mmio(dev, regs,
230 &meson_regmap_config);
231 if (IS_ERR(priv->hhi)) {
232 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
233 ret = PTR_ERR(priv->hhi);
237 priv->canvas = meson_canvas_get(dev);
238 if (IS_ERR(priv->canvas)) {
239 ret = PTR_ERR(priv->canvas);
243 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_osd1);
246 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_0);
248 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
251 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_1);
253 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
254 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
257 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_2);
259 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
260 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
261 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
265 priv->vsync_irq = platform_get_irq(pdev, 0);
267 ret = drm_vblank_init(drm, 1);
271 /* Assign limits per soc revision/package */
272 for (i = 0 ; i < ARRAY_SIZE(meson_drm_soc_attrs) ; ++i) {
273 if (soc_device_match(meson_drm_soc_attrs[i].attrs)) {
274 priv->limits = &meson_drm_soc_attrs[i].limits;
280 * Remove early framebuffers (ie. simplefb). The framebuffer can be
281 * located anywhere in RAM
283 ret = drm_aperture_remove_framebuffers(false, &meson_driver);
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 = request_irq(priv->vsync_irq, meson_irq, 0, drm->driver->name, drm);
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 free_irq(priv->vsync_irq, 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 drm_dev_unregister(drm);
377 drm_kms_helper_poll_fini(drm);
378 drm_atomic_helper_shutdown(drm);
379 component_unbind_all(dev, drm);
380 free_irq(priv->vsync_irq, drm);
383 if (priv->afbcd.ops) {
384 priv->afbcd.ops->reset(priv);
385 meson_rdma_free(priv);
389 static const struct component_master_ops meson_drv_master_ops = {
390 .bind = meson_drv_bind,
391 .unbind = meson_drv_unbind,
394 static int __maybe_unused meson_drv_pm_suspend(struct device *dev)
396 struct meson_drm *priv = dev_get_drvdata(dev);
401 return drm_mode_config_helper_suspend(priv->drm);
404 static int __maybe_unused meson_drv_pm_resume(struct device *dev)
406 struct meson_drm *priv = dev_get_drvdata(dev);
411 meson_vpu_init(priv);
412 meson_venc_init(priv);
413 meson_vpp_init(priv);
414 meson_viu_init(priv);
416 priv->afbcd.ops->init(priv);
418 return 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 void meson_drv_shutdown(struct platform_device *pdev)
471 struct meson_drm *priv = dev_get_drvdata(&pdev->dev);
476 drm_kms_helper_poll_fini(priv->drm);
477 drm_atomic_helper_shutdown(priv->drm);
480 static int meson_drv_probe(struct platform_device *pdev)
482 struct component_match *match = NULL;
483 struct device_node *np = pdev->dev.of_node;
484 struct device_node *ep, *remote;
487 for_each_endpoint_of_node(np, ep) {
488 remote = of_graph_get_remote_port_parent(ep);
489 if (!remote || !of_device_is_available(remote)) {
494 count += meson_probe_remote(pdev, &match, np, remote);
499 return meson_drv_bind_master(&pdev->dev, false);
501 /* If some endpoints were found, initialize the nodes */
503 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
505 return component_master_add_with_match(&pdev->dev,
506 &meson_drv_master_ops,
510 /* If no output endpoints were available, simply bail out */
514 static struct meson_drm_match_data meson_drm_gxbb_data = {
515 .compat = VPU_COMPATIBLE_GXBB,
518 static struct meson_drm_match_data meson_drm_gxl_data = {
519 .compat = VPU_COMPATIBLE_GXL,
522 static struct meson_drm_match_data meson_drm_gxm_data = {
523 .compat = VPU_COMPATIBLE_GXM,
524 .afbcd_ops = &meson_afbcd_gxm_ops,
527 static struct meson_drm_match_data meson_drm_g12a_data = {
528 .compat = VPU_COMPATIBLE_G12A,
529 .afbcd_ops = &meson_afbcd_g12a_ops,
532 static const struct of_device_id dt_match[] = {
533 { .compatible = "amlogic,meson-gxbb-vpu",
534 .data = (void *)&meson_drm_gxbb_data },
535 { .compatible = "amlogic,meson-gxl-vpu",
536 .data = (void *)&meson_drm_gxl_data },
537 { .compatible = "amlogic,meson-gxm-vpu",
538 .data = (void *)&meson_drm_gxm_data },
539 { .compatible = "amlogic,meson-g12a-vpu",
540 .data = (void *)&meson_drm_g12a_data },
543 MODULE_DEVICE_TABLE(of, dt_match);
545 static const struct dev_pm_ops meson_drv_pm_ops = {
546 SET_SYSTEM_SLEEP_PM_OPS(meson_drv_pm_suspend, meson_drv_pm_resume)
549 static struct platform_driver meson_drm_platform_driver = {
550 .probe = meson_drv_probe,
551 .shutdown = meson_drv_shutdown,
554 .of_match_table = dt_match,
555 .pm = &meson_drv_pm_ops,
559 module_platform_driver(meson_drm_platform_driver);
563 MODULE_DESCRIPTION(DRIVER_DESC);
564 MODULE_LICENSE("GPL");