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_encoder_cvbs.h"
35 #include "meson_encoder_hdmi.h"
36 #include "meson_viu.h"
37 #include "meson_vpp.h"
38 #include "meson_rdma.h"
40 #define DRIVER_NAME "meson"
41 #define DRIVER_DESC "Amlogic Meson DRM driver"
44 * DOC: Video Processing Unit
46 * VPU Handles the Global Video Processing, it includes management of the
47 * clocks gates, blocks reset lines and power domains.
51 * - Full reset of entire video processing HW blocks
52 * - Scaling and setup of the VPU clock
54 * - Powering up video processing HW blocks
55 * - Powering Up HDMI controller and PHY
58 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
59 .atomic_check = drm_atomic_helper_check,
60 .atomic_commit = drm_atomic_helper_commit,
61 .fb_create = drm_gem_fb_create,
64 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = {
65 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
68 static irqreturn_t meson_irq(int irq, void *arg)
70 struct drm_device *dev = arg;
71 struct meson_drm *priv = dev->dev_private;
73 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
80 static int meson_dumb_create(struct drm_file *file, struct drm_device *dev,
81 struct drm_mode_create_dumb *args)
84 * We need 64bytes aligned stride, and PAGE aligned size
86 args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), SZ_64);
87 args->size = PAGE_ALIGN(args->pitch * args->height);
89 return drm_gem_cma_dumb_create_internal(file, dev, args);
92 DEFINE_DRM_GEM_CMA_FOPS(fops);
94 static const struct drm_driver meson_driver = {
95 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
98 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(meson_dumb_create),
109 static bool meson_vpu_has_available_connectors(struct device *dev)
111 struct device_node *ep, *remote;
113 /* Parses each endpoint and check if remote exists */
114 for_each_endpoint_of_node(dev->of_node, ep) {
115 /* If the endpoint node exists, consider it enabled */
116 remote = of_graph_get_remote_port(ep);
124 static struct regmap_config meson_regmap_config = {
128 .max_register = 0x1000,
131 static void meson_vpu_init(struct meson_drm *priv)
136 * Slave dc0 and dc5 connected to master port 1.
137 * By default other slaves are connected to master port 0.
139 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1) |
140 VPU_RDARB_SLAVE_TO_MASTER_PORT(5, 1);
141 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
143 /* Slave dc0 connected to master port 1 */
144 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1);
145 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
147 /* Slave dc4 and dc7 connected to master port 1 */
148 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(4, 1) |
149 VPU_RDARB_SLAVE_TO_MASTER_PORT(7, 1);
150 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
152 /* Slave dc1 connected to master port 1 */
153 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(1, 1);
154 writel_relaxed(value, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
157 struct meson_drm_soc_attr {
158 struct meson_drm_soc_limits limits;
159 const struct soc_device_attribute *attrs;
162 static const struct meson_drm_soc_attr meson_drm_soc_attrs[] = {
163 /* S805X/S805Y HDMI PLL won't lock for HDMI PHY freq > 1,65GHz */
166 .max_hdmi_phy_freq = 1650000,
168 .attrs = (const struct soc_device_attribute []) {
169 { .soc_id = "GXL (S805*)", },
175 static int meson_drv_bind_master(struct device *dev, bool has_components)
177 struct platform_device *pdev = to_platform_device(dev);
178 const struct meson_drm_match_data *match;
179 struct meson_drm *priv;
180 struct drm_device *drm;
181 struct resource *res;
185 /* Checks if an output connector is available */
186 if (!meson_vpu_has_available_connectors(dev)) {
187 dev_err(dev, "No output connector available\n");
191 match = of_device_get_match_data(dev);
195 drm = drm_dev_alloc(&meson_driver, dev);
199 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
204 drm->dev_private = priv;
207 priv->compat = match->compat;
208 priv->afbcd.ops = match->afbcd_ops;
210 regs = devm_platform_ioremap_resource_byname(pdev, "vpu");
216 priv->io_base = regs;
218 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
223 /* Simply ioremap since it may be a shared register zone */
224 regs = devm_ioremap(dev, res->start, resource_size(res));
226 ret = -EADDRNOTAVAIL;
230 priv->hhi = devm_regmap_init_mmio(dev, regs,
231 &meson_regmap_config);
232 if (IS_ERR(priv->hhi)) {
233 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
234 ret = PTR_ERR(priv->hhi);
238 priv->canvas = meson_canvas_get(dev);
239 if (IS_ERR(priv->canvas)) {
240 ret = PTR_ERR(priv->canvas);
244 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_osd1);
247 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_0);
249 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
252 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_1);
254 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
255 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
258 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_2);
260 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
261 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
262 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
266 priv->vsync_irq = platform_get_irq(pdev, 0);
268 ret = drm_vblank_init(drm, 1);
272 /* Assign limits per soc revision/package */
273 for (i = 0 ; i < ARRAY_SIZE(meson_drm_soc_attrs) ; ++i) {
274 if (soc_device_match(meson_drm_soc_attrs[i].attrs)) {
275 priv->limits = &meson_drm_soc_attrs[i].limits;
281 * Remove early framebuffers (ie. simplefb). The framebuffer can be
282 * located anywhere in RAM
284 ret = drm_aperture_remove_framebuffers(false, &meson_driver);
288 ret = drmm_mode_config_init(drm);
291 drm->mode_config.max_width = 3840;
292 drm->mode_config.max_height = 2160;
293 drm->mode_config.funcs = &meson_mode_config_funcs;
294 drm->mode_config.helper_private = &meson_mode_config_helpers;
296 /* Hardware Initialization */
298 meson_vpu_init(priv);
299 meson_venc_init(priv);
300 meson_vpp_init(priv);
301 meson_viu_init(priv);
302 if (priv->afbcd.ops) {
303 ret = priv->afbcd.ops->init(priv);
308 /* Encoder Initialization */
310 ret = meson_encoder_cvbs_init(priv);
314 if (has_components) {
315 ret = component_bind_all(drm->dev, drm);
317 dev_err(drm->dev, "Couldn't bind all components\n");
322 ret = meson_encoder_hdmi_init(priv);
326 ret = meson_plane_create(priv);
330 ret = meson_overlay_create(priv);
334 ret = meson_crtc_create(priv);
338 ret = request_irq(priv->vsync_irq, meson_irq, 0, drm->driver->name, drm);
342 drm_mode_config_reset(drm);
344 drm_kms_helper_poll_init(drm);
346 platform_set_drvdata(pdev, priv);
348 ret = drm_dev_register(drm, 0);
352 drm_fbdev_generic_setup(drm, 32);
357 free_irq(priv->vsync_irq, drm);
364 static int meson_drv_bind(struct device *dev)
366 return meson_drv_bind_master(dev, true);
369 static void meson_drv_unbind(struct device *dev)
371 struct meson_drm *priv = dev_get_drvdata(dev);
372 struct drm_device *drm = priv->drm;
375 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
376 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
377 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
378 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_2);
381 drm_dev_unregister(drm);
382 drm_kms_helper_poll_fini(drm);
383 drm_atomic_helper_shutdown(drm);
384 component_unbind_all(dev, drm);
385 free_irq(priv->vsync_irq, drm);
388 if (priv->afbcd.ops) {
389 priv->afbcd.ops->reset(priv);
390 meson_rdma_free(priv);
394 static const struct component_master_ops meson_drv_master_ops = {
395 .bind = meson_drv_bind,
396 .unbind = meson_drv_unbind,
399 static int __maybe_unused meson_drv_pm_suspend(struct device *dev)
401 struct meson_drm *priv = dev_get_drvdata(dev);
406 return drm_mode_config_helper_suspend(priv->drm);
409 static int __maybe_unused meson_drv_pm_resume(struct device *dev)
411 struct meson_drm *priv = dev_get_drvdata(dev);
416 meson_vpu_init(priv);
417 meson_venc_init(priv);
418 meson_vpp_init(priv);
419 meson_viu_init(priv);
421 priv->afbcd.ops->init(priv);
423 return drm_mode_config_helper_resume(priv->drm);
426 static int compare_of(struct device *dev, void *data)
428 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
431 return dev->of_node == data;
434 static void meson_drv_shutdown(struct platform_device *pdev)
436 struct meson_drm *priv = dev_get_drvdata(&pdev->dev);
441 drm_kms_helper_poll_fini(priv->drm);
442 drm_atomic_helper_shutdown(priv->drm);
445 /* Possible connectors nodes to ignore */
446 static const struct of_device_id connectors_match[] = {
447 { .compatible = "composite-video-connector" },
448 { .compatible = "svideo-connector" },
452 static int meson_drv_probe(struct platform_device *pdev)
454 struct component_match *match = NULL;
455 struct device_node *np = pdev->dev.of_node;
456 struct device_node *ep, *remote;
459 for_each_endpoint_of_node(np, ep) {
460 remote = of_graph_get_remote_port_parent(ep);
461 if (!remote || !of_device_is_available(remote)) {
466 /* If an analog connector is detected, count it as an output */
467 if (of_match_node(connectors_match, remote)) {
473 dev_dbg(&pdev->dev, "parent %pOF remote match add %pOF parent %s\n",
474 np, remote, dev_name(&pdev->dev));
476 component_match_add(&pdev->dev, &match, compare_of, remote);
484 return meson_drv_bind_master(&pdev->dev, false);
486 /* If some endpoints were found, initialize the nodes */
488 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
490 return component_master_add_with_match(&pdev->dev,
491 &meson_drv_master_ops,
495 /* If no output endpoints were available, simply bail out */
499 static struct meson_drm_match_data meson_drm_gxbb_data = {
500 .compat = VPU_COMPATIBLE_GXBB,
503 static struct meson_drm_match_data meson_drm_gxl_data = {
504 .compat = VPU_COMPATIBLE_GXL,
507 static struct meson_drm_match_data meson_drm_gxm_data = {
508 .compat = VPU_COMPATIBLE_GXM,
509 .afbcd_ops = &meson_afbcd_gxm_ops,
512 static struct meson_drm_match_data meson_drm_g12a_data = {
513 .compat = VPU_COMPATIBLE_G12A,
514 .afbcd_ops = &meson_afbcd_g12a_ops,
517 static const struct of_device_id dt_match[] = {
518 { .compatible = "amlogic,meson-gxbb-vpu",
519 .data = (void *)&meson_drm_gxbb_data },
520 { .compatible = "amlogic,meson-gxl-vpu",
521 .data = (void *)&meson_drm_gxl_data },
522 { .compatible = "amlogic,meson-gxm-vpu",
523 .data = (void *)&meson_drm_gxm_data },
524 { .compatible = "amlogic,meson-g12a-vpu",
525 .data = (void *)&meson_drm_g12a_data },
528 MODULE_DEVICE_TABLE(of, dt_match);
530 static const struct dev_pm_ops meson_drv_pm_ops = {
531 SET_SYSTEM_SLEEP_PM_OPS(meson_drv_pm_suspend, meson_drv_pm_resume)
534 static struct platform_driver meson_drm_platform_driver = {
535 .probe = meson_drv_probe,
536 .shutdown = meson_drv_shutdown,
539 .of_match_table = dt_match,
540 .pm = &meson_drv_pm_ops,
544 module_platform_driver(meson_drm_platform_driver);
548 MODULE_DESCRIPTION(DRIVER_DESC);
549 MODULE_LICENSE("GPL");