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_fbdev_dma.h>
22 #include <drm/drm_gem_dma_helper.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_modeset_helper_vtables.h>
25 #include <drm/drm_module.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drm_vblank.h>
29 #include "meson_crtc.h"
30 #include "meson_drv.h"
31 #include "meson_overlay.h"
32 #include "meson_plane.h"
33 #include "meson_osd_afbcd.h"
34 #include "meson_registers.h"
35 #include "meson_encoder_cvbs.h"
36 #include "meson_encoder_hdmi.h"
37 #include "meson_encoder_dsi.h"
38 #include "meson_viu.h"
39 #include "meson_vpp.h"
40 #include "meson_rdma.h"
42 #define DRIVER_NAME "meson"
43 #define DRIVER_DESC "Amlogic Meson DRM driver"
46 * DOC: Video Processing Unit
48 * VPU Handles the Global Video Processing, it includes management of the
49 * clocks gates, blocks reset lines and power domains.
53 * - Full reset of entire video processing HW blocks
54 * - Scaling and setup of the VPU clock
56 * - Powering up video processing HW blocks
57 * - Powering Up HDMI controller and PHY
60 static const struct drm_mode_config_funcs meson_mode_config_funcs = {
61 .atomic_check = drm_atomic_helper_check,
62 .atomic_commit = drm_atomic_helper_commit,
63 .fb_create = drm_gem_fb_create,
66 static const struct drm_mode_config_helper_funcs meson_mode_config_helpers = {
67 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
70 static irqreturn_t meson_irq(int irq, void *arg)
72 struct drm_device *dev = arg;
73 struct meson_drm *priv = dev->dev_private;
75 (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
82 static int meson_dumb_create(struct drm_file *file, struct drm_device *dev,
83 struct drm_mode_create_dumb *args)
86 * We need 64bytes aligned stride, and PAGE aligned size
88 args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), SZ_64);
89 args->size = PAGE_ALIGN(args->pitch * args->height);
91 return drm_gem_dma_dumb_create_internal(file, dev, args);
94 DEFINE_DRM_GEM_DMA_FOPS(fops);
96 static const struct drm_driver meson_driver = {
97 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
100 DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(meson_dumb_create),
111 static bool meson_vpu_has_available_connectors(struct device *dev)
113 struct device_node *ep, *remote;
115 /* Parses each endpoint and check if remote exists */
116 for_each_endpoint_of_node(dev->of_node, ep) {
117 /* If the endpoint node exists, consider it enabled */
118 remote = of_graph_get_remote_port(ep);
129 static struct regmap_config meson_regmap_config = {
133 .max_register = 0x1000,
136 static void meson_vpu_init(struct meson_drm *priv)
141 * Slave dc0 and dc5 connected to master port 1.
142 * By default other slaves are connected to master port 0.
144 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1) |
145 VPU_RDARB_SLAVE_TO_MASTER_PORT(5, 1);
146 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C1));
148 /* Slave dc0 connected to master port 1 */
149 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(0, 1);
150 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L1C2));
152 /* Slave dc4 and dc7 connected to master port 1 */
153 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(4, 1) |
154 VPU_RDARB_SLAVE_TO_MASTER_PORT(7, 1);
155 writel_relaxed(value, priv->io_base + _REG(VPU_RDARB_MODE_L2C1));
157 /* Slave dc1 connected to master port 1 */
158 value = VPU_RDARB_SLAVE_TO_MASTER_PORT(1, 1);
159 writel_relaxed(value, priv->io_base + _REG(VPU_WRARB_MODE_L2C1));
162 struct meson_drm_soc_attr {
163 struct meson_drm_soc_limits limits;
164 const struct soc_device_attribute *attrs;
167 static const struct meson_drm_soc_attr meson_drm_soc_attrs[] = {
168 /* S805X/S805Y HDMI PLL won't lock for HDMI PHY freq > 1,65GHz */
171 .max_hdmi_phy_freq = 1650000,
173 .attrs = (const struct soc_device_attribute []) {
174 { .soc_id = "GXL (S805*)", },
180 static int meson_drv_bind_master(struct device *dev, bool has_components)
182 struct platform_device *pdev = to_platform_device(dev);
183 const struct meson_drm_match_data *match;
184 struct meson_drm *priv;
185 struct drm_device *drm;
186 struct resource *res;
190 /* Checks if an output connector is available */
191 if (!meson_vpu_has_available_connectors(dev)) {
192 dev_err(dev, "No output connector available\n");
196 match = of_device_get_match_data(dev);
200 drm = drm_dev_alloc(&meson_driver, dev);
204 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
209 drm->dev_private = priv;
212 priv->compat = match->compat;
213 priv->afbcd.ops = match->afbcd_ops;
215 regs = devm_platform_ioremap_resource_byname(pdev, "vpu");
221 priv->io_base = regs;
223 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
228 /* Simply ioremap since it may be a shared register zone */
229 regs = devm_ioremap(dev, res->start, resource_size(res));
231 ret = -EADDRNOTAVAIL;
235 priv->hhi = devm_regmap_init_mmio(dev, regs,
236 &meson_regmap_config);
237 if (IS_ERR(priv->hhi)) {
238 dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
239 ret = PTR_ERR(priv->hhi);
243 priv->canvas = meson_canvas_get(dev);
244 if (IS_ERR(priv->canvas)) {
245 ret = PTR_ERR(priv->canvas);
249 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_osd1);
252 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_0);
254 goto free_canvas_osd1;
255 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_1);
257 goto free_canvas_vd1_0;
258 ret = meson_canvas_alloc(priv->canvas, &priv->canvas_id_vd1_2);
260 goto free_canvas_vd1_1;
262 priv->vsync_irq = platform_get_irq(pdev, 0);
264 ret = drm_vblank_init(drm, 1);
266 goto free_canvas_vd1_2;
268 /* Assign limits per soc revision/package */
269 for (i = 0 ; i < ARRAY_SIZE(meson_drm_soc_attrs) ; ++i) {
270 if (soc_device_match(meson_drm_soc_attrs[i].attrs)) {
271 priv->limits = &meson_drm_soc_attrs[i].limits;
277 * Remove early framebuffers (ie. simplefb). The framebuffer can be
278 * located anywhere in RAM
280 ret = drm_aperture_remove_framebuffers(&meson_driver);
282 goto free_canvas_vd1_2;
284 ret = drmm_mode_config_init(drm);
286 goto free_canvas_vd1_2;
287 drm->mode_config.max_width = 3840;
288 drm->mode_config.max_height = 2160;
289 drm->mode_config.funcs = &meson_mode_config_funcs;
290 drm->mode_config.helper_private = &meson_mode_config_helpers;
292 /* Hardware Initialization */
294 meson_vpu_init(priv);
295 meson_venc_init(priv);
296 meson_vpp_init(priv);
297 meson_viu_init(priv);
298 if (priv->afbcd.ops) {
299 ret = priv->afbcd.ops->init(priv);
301 goto free_canvas_vd1_2;
304 /* Encoder Initialization */
306 ret = meson_encoder_cvbs_probe(priv);
310 if (has_components) {
311 ret = component_bind_all(dev, drm);
313 dev_err(drm->dev, "Couldn't bind all components\n");
314 /* Do not try to unbind */
315 has_components = false;
320 ret = meson_encoder_hdmi_probe(priv);
324 if (meson_vpu_is_compatible(priv, VPU_COMPATIBLE_G12A)) {
325 ret = meson_encoder_dsi_probe(priv);
330 ret = meson_plane_create(priv);
334 ret = meson_overlay_create(priv);
338 ret = meson_crtc_create(priv);
342 ret = request_irq(priv->vsync_irq, meson_irq, 0, drm->driver->name, drm);
346 drm_mode_config_reset(drm);
348 drm_kms_helper_poll_init(drm);
350 platform_set_drvdata(pdev, priv);
352 ret = drm_dev_register(drm, 0);
356 drm_fbdev_dma_setup(drm, 32);
361 free_irq(priv->vsync_irq, drm);
364 priv->afbcd.ops->exit(priv);
366 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_2);
368 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
370 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
372 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
376 meson_encoder_dsi_remove(priv);
377 meson_encoder_hdmi_remove(priv);
378 meson_encoder_cvbs_remove(priv);
381 component_unbind_all(dev, drm);
386 static int meson_drv_bind(struct device *dev)
388 return meson_drv_bind_master(dev, true);
391 static void meson_drv_unbind(struct device *dev)
393 struct meson_drm *priv = dev_get_drvdata(dev);
394 struct drm_device *drm = priv->drm;
397 meson_canvas_free(priv->canvas, priv->canvas_id_osd1);
398 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_0);
399 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_1);
400 meson_canvas_free(priv->canvas, priv->canvas_id_vd1_2);
403 drm_dev_unregister(drm);
404 drm_kms_helper_poll_fini(drm);
405 drm_atomic_helper_shutdown(drm);
406 free_irq(priv->vsync_irq, drm);
409 meson_encoder_dsi_remove(priv);
410 meson_encoder_hdmi_remove(priv);
411 meson_encoder_cvbs_remove(priv);
413 component_unbind_all(dev, drm);
416 priv->afbcd.ops->exit(priv);
419 static const struct component_master_ops meson_drv_master_ops = {
420 .bind = meson_drv_bind,
421 .unbind = meson_drv_unbind,
424 static int __maybe_unused meson_drv_pm_suspend(struct device *dev)
426 struct meson_drm *priv = dev_get_drvdata(dev);
431 return drm_mode_config_helper_suspend(priv->drm);
434 static int __maybe_unused meson_drv_pm_resume(struct device *dev)
436 struct meson_drm *priv = dev_get_drvdata(dev);
441 meson_vpu_init(priv);
442 meson_venc_init(priv);
443 meson_vpp_init(priv);
444 meson_viu_init(priv);
446 priv->afbcd.ops->init(priv);
448 return drm_mode_config_helper_resume(priv->drm);
451 static void meson_drv_shutdown(struct platform_device *pdev)
453 struct meson_drm *priv = dev_get_drvdata(&pdev->dev);
458 drm_kms_helper_poll_fini(priv->drm);
459 drm_atomic_helper_shutdown(priv->drm);
463 * Only devices to use as components
464 * TOFIX: get rid of components when we can finally
465 * get meson_dx_hdmi to stop using the meson_drm
466 * private structure for HHI registers.
468 static const struct of_device_id components_dev_match[] = {
469 { .compatible = "amlogic,meson-gxbb-dw-hdmi" },
470 { .compatible = "amlogic,meson-gxl-dw-hdmi" },
471 { .compatible = "amlogic,meson-gxm-dw-hdmi" },
472 { .compatible = "amlogic,meson-g12a-dw-hdmi" },
476 static int meson_drv_probe(struct platform_device *pdev)
478 struct component_match *match = NULL;
479 struct device_node *np = pdev->dev.of_node;
480 struct device_node *ep, *remote;
483 for_each_endpoint_of_node(np, ep) {
484 remote = of_graph_get_remote_port_parent(ep);
485 if (!remote || !of_device_is_available(remote)) {
490 if (of_match_node(components_dev_match, remote)) {
491 component_match_add(&pdev->dev, &match, component_compare_of, remote);
493 dev_dbg(&pdev->dev, "parent %pOF remote match add %pOF parent %s\n",
494 np, remote, dev_name(&pdev->dev));
503 return meson_drv_bind_master(&pdev->dev, false);
505 /* If some endpoints were found, initialize the nodes */
507 dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
509 return component_master_add_with_match(&pdev->dev,
510 &meson_drv_master_ops,
514 /* If no output endpoints were available, simply bail out */
518 static void meson_drv_remove(struct platform_device *pdev)
520 component_master_del(&pdev->dev, &meson_drv_master_ops);
523 static struct meson_drm_match_data meson_drm_gxbb_data = {
524 .compat = VPU_COMPATIBLE_GXBB,
527 static struct meson_drm_match_data meson_drm_gxl_data = {
528 .compat = VPU_COMPATIBLE_GXL,
531 static struct meson_drm_match_data meson_drm_gxm_data = {
532 .compat = VPU_COMPATIBLE_GXM,
533 .afbcd_ops = &meson_afbcd_gxm_ops,
536 static struct meson_drm_match_data meson_drm_g12a_data = {
537 .compat = VPU_COMPATIBLE_G12A,
538 .afbcd_ops = &meson_afbcd_g12a_ops,
541 static const struct of_device_id dt_match[] = {
542 { .compatible = "amlogic,meson-gxbb-vpu",
543 .data = (void *)&meson_drm_gxbb_data },
544 { .compatible = "amlogic,meson-gxl-vpu",
545 .data = (void *)&meson_drm_gxl_data },
546 { .compatible = "amlogic,meson-gxm-vpu",
547 .data = (void *)&meson_drm_gxm_data },
548 { .compatible = "amlogic,meson-g12a-vpu",
549 .data = (void *)&meson_drm_g12a_data },
552 MODULE_DEVICE_TABLE(of, dt_match);
554 static const struct dev_pm_ops meson_drv_pm_ops = {
555 SET_SYSTEM_SLEEP_PM_OPS(meson_drv_pm_suspend, meson_drv_pm_resume)
558 static struct platform_driver meson_drm_platform_driver = {
559 .probe = meson_drv_probe,
560 .remove_new = meson_drv_remove,
561 .shutdown = meson_drv_shutdown,
564 .of_match_table = dt_match,
565 .pm = &meson_drv_pm_ops,
569 drm_module_platform_driver(meson_drm_platform_driver);
573 MODULE_DESCRIPTION(DRIVER_DESC);
574 MODULE_LICENSE("GPL");