linux/drivers/gpu/drm/meson/meson_drv.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2016 BayLibre, SAS
   3 * Author: Neil Armstrong <narmstrong@baylibre.com>
   4 * Copyright (C) 2014 Endless Mobile
   5 *
   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.
  10 *
  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.
  15 *
  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/>.
  18 *
  19 * Written by:
  20 *     Jasper St. Pierre <jstpierre@mecheye.net>
  21 */
  22
  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>
  29
  30#include <drm/drmP.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_fb_cma_helper.h>
  38#include <drm/drm_rect.h>
  39#include <drm/drm_fb_helper.h>
  40
  41#include "meson_drv.h"
  42#include "meson_plane.h"
  43#include "meson_crtc.h"
  44#include "meson_venc_cvbs.h"
  45
  46#include "meson_vpp.h"
  47#include "meson_viu.h"
  48#include "meson_venc.h"
  49#include "meson_canvas.h"
  50#include "meson_registers.h"
  51
  52#define DRIVER_NAME "meson"
  53#define DRIVER_DESC "Amlogic Meson DRM driver"
  54
  55/**
  56 * DOC: Video Processing Unit
  57 *
  58 * VPU Handles the Global Video Processing, it includes management of the
  59 * clocks gates, blocks reset lines and power domains.
  60 *
  61 * What is missing :
  62 *
  63 * - Full reset of entire video processing HW blocks
  64 * - Scaling and setup of the VPU clock
  65 * - Bus clock gates
  66 * - Powering up video processing HW blocks
  67 * - Powering Up HDMI controller and PHY
  68 */
  69
  70static void meson_fb_output_poll_changed(struct drm_device *dev)
  71{
  72        struct meson_drm *priv = dev->dev_private;
  73
  74        drm_fbdev_cma_hotplug_event(priv->fbdev);
  75}
  76
  77static const struct drm_mode_config_funcs meson_mode_config_funcs = {
  78        .output_poll_changed = meson_fb_output_poll_changed,
  79        .atomic_check        = drm_atomic_helper_check,
  80        .atomic_commit       = drm_atomic_helper_commit,
  81        .fb_create           = drm_fb_cma_create,
  82};
  83
  84static irqreturn_t meson_irq(int irq, void *arg)
  85{
  86        struct drm_device *dev = arg;
  87        struct meson_drm *priv = dev->dev_private;
  88
  89        (void)readl_relaxed(priv->io_base + _REG(VENC_INTFLAG));
  90
  91        meson_crtc_irq(priv);
  92
  93        return IRQ_HANDLED;
  94}
  95
  96DEFINE_DRM_GEM_CMA_FOPS(fops);
  97
  98static struct drm_driver meson_driver = {
  99        .driver_features        = DRIVER_HAVE_IRQ | DRIVER_GEM |
 100                                  DRIVER_MODESET | DRIVER_PRIME |
 101                                  DRIVER_ATOMIC,
 102
 103        /* IRQ */
 104        .irq_handler            = meson_irq,
 105
 106        /* PRIME Ops */
 107        .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
 108        .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
 109        .gem_prime_import       = drm_gem_prime_import,
 110        .gem_prime_export       = drm_gem_prime_export,
 111        .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
 112        .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
 113        .gem_prime_vmap         = drm_gem_cma_prime_vmap,
 114        .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
 115        .gem_prime_mmap         = drm_gem_cma_prime_mmap,
 116
 117        /* GEM Ops */
 118        .dumb_create            = drm_gem_cma_dumb_create,
 119        .gem_free_object_unlocked = drm_gem_cma_free_object,
 120        .gem_vm_ops             = &drm_gem_cma_vm_ops,
 121
 122        /* Misc */
 123        .fops                   = &fops,
 124        .name                   = DRIVER_NAME,
 125        .desc                   = DRIVER_DESC,
 126        .date                   = "20161109",
 127        .major                  = 1,
 128        .minor                  = 0,
 129};
 130
 131static bool meson_vpu_has_available_connectors(struct device *dev)
 132{
 133        struct device_node *ep, *remote;
 134
 135        /* Parses each endpoint and check if remote exists */
 136        for_each_endpoint_of_node(dev->of_node, ep) {
 137                /* If the endpoint node exists, consider it enabled */
 138                remote = of_graph_get_remote_port(ep);
 139                if (remote)
 140                        return true;
 141        }
 142
 143        return false;
 144}
 145
 146static struct regmap_config meson_regmap_config = {
 147        .reg_bits       = 32,
 148        .val_bits       = 32,
 149        .reg_stride     = 4,
 150        .max_register   = 0x1000,
 151};
 152
 153static int meson_drv_bind_master(struct device *dev, bool has_components)
 154{
 155        struct platform_device *pdev = to_platform_device(dev);
 156        struct meson_drm *priv;
 157        struct drm_device *drm;
 158        struct resource *res;
 159        void __iomem *regs;
 160        int ret;
 161
 162        /* Checks if an output connector is available */
 163        if (!meson_vpu_has_available_connectors(dev)) {
 164                dev_err(dev, "No output connector available\n");
 165                return -ENODEV;
 166        }
 167
 168        drm = drm_dev_alloc(&meson_driver, dev);
 169        if (IS_ERR(drm))
 170                return PTR_ERR(drm);
 171
 172        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 173        if (!priv) {
 174                ret = -ENOMEM;
 175                goto free_drm;
 176        }
 177        drm->dev_private = priv;
 178        priv->drm = drm;
 179        priv->dev = dev;
 180
 181        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vpu");
 182        regs = devm_ioremap_resource(dev, res);
 183        if (IS_ERR(regs))
 184                return PTR_ERR(regs);
 185
 186        priv->io_base = regs;
 187
 188        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hhi");
 189        /* Simply ioremap since it may be a shared register zone */
 190        regs = devm_ioremap(dev, res->start, resource_size(res));
 191        if (!regs)
 192                return -EADDRNOTAVAIL;
 193
 194        priv->hhi = devm_regmap_init_mmio(dev, regs,
 195                                          &meson_regmap_config);
 196        if (IS_ERR(priv->hhi)) {
 197                dev_err(&pdev->dev, "Couldn't create the HHI regmap\n");
 198                return PTR_ERR(priv->hhi);
 199        }
 200
 201        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmc");
 202        /* Simply ioremap since it may be a shared register zone */
 203        regs = devm_ioremap(dev, res->start, resource_size(res));
 204        if (!regs)
 205                return -EADDRNOTAVAIL;
 206
 207        priv->dmc = devm_regmap_init_mmio(dev, regs,
 208                                          &meson_regmap_config);
 209        if (IS_ERR(priv->dmc)) {
 210                dev_err(&pdev->dev, "Couldn't create the DMC regmap\n");
 211                return PTR_ERR(priv->dmc);
 212        }
 213
 214        priv->vsync_irq = platform_get_irq(pdev, 0);
 215
 216        drm_vblank_init(drm, 1);
 217        drm_mode_config_init(drm);
 218        drm->mode_config.max_width = 3840;
 219        drm->mode_config.max_height = 2160;
 220        drm->mode_config.funcs = &meson_mode_config_funcs;
 221
 222        /* Hardware Initialization */
 223
 224        meson_venc_init(priv);
 225        meson_vpp_init(priv);
 226        meson_viu_init(priv);
 227
 228        /* Encoder Initialization */
 229
 230        ret = meson_venc_cvbs_create(priv);
 231        if (ret)
 232                goto free_drm;
 233
 234        if (has_components) {
 235                ret = component_bind_all(drm->dev, drm);
 236                if (ret) {
 237                        dev_err(drm->dev, "Couldn't bind all components\n");
 238                        goto free_drm;
 239                }
 240        }
 241
 242        ret = meson_plane_create(priv);
 243        if (ret)
 244                goto free_drm;
 245
 246        ret = meson_crtc_create(priv);
 247        if (ret)
 248                goto free_drm;
 249
 250        ret = drm_irq_install(drm, priv->vsync_irq);
 251        if (ret)
 252                goto free_drm;
 253
 254        drm_mode_config_reset(drm);
 255
 256        priv->fbdev = drm_fbdev_cma_init(drm, 32,
 257                                         drm->mode_config.num_connector);
 258        if (IS_ERR(priv->fbdev)) {
 259                ret = PTR_ERR(priv->fbdev);
 260                goto free_drm;
 261        }
 262
 263        drm_kms_helper_poll_init(drm);
 264
 265        platform_set_drvdata(pdev, priv);
 266
 267        ret = drm_dev_register(drm, 0);
 268        if (ret)
 269                goto free_drm;
 270
 271        return 0;
 272
 273free_drm:
 274        drm_dev_unref(drm);
 275
 276        return ret;
 277}
 278
 279static int meson_drv_bind(struct device *dev)
 280{
 281        return meson_drv_bind_master(dev, true);
 282}
 283
 284static void meson_drv_unbind(struct device *dev)
 285{
 286        struct drm_device *drm = dev_get_drvdata(dev);
 287        struct meson_drm *priv = drm->dev_private;
 288
 289        drm_dev_unregister(drm);
 290        drm_kms_helper_poll_fini(drm);
 291        drm_fbdev_cma_fini(priv->fbdev);
 292        drm_mode_config_cleanup(drm);
 293        drm_dev_unref(drm);
 294
 295}
 296
 297static const struct component_master_ops meson_drv_master_ops = {
 298        .bind   = meson_drv_bind,
 299        .unbind = meson_drv_unbind,
 300};
 301
 302static int compare_of(struct device *dev, void *data)
 303{
 304        DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
 305                         dev->of_node, data);
 306
 307        return dev->of_node == data;
 308}
 309
 310/* Possible connectors nodes to ignore */
 311static const struct of_device_id connectors_match[] = {
 312        { .compatible = "composite-video-connector" },
 313        { .compatible = "svideo-connector" },
 314        { .compatible = "hdmi-connector" },
 315        { .compatible = "dvi-connector" },
 316        {}
 317};
 318
 319static int meson_probe_remote(struct platform_device *pdev,
 320                              struct component_match **match,
 321                              struct device_node *parent,
 322                              struct device_node *remote)
 323{
 324        struct device_node *ep, *remote_node;
 325        int count = 1;
 326
 327        /* If node is a connector, return and do not add to match table */
 328        if (of_match_node(connectors_match, remote))
 329                return 1;
 330
 331        component_match_add(&pdev->dev, match, compare_of, remote);
 332
 333        for_each_endpoint_of_node(remote, ep) {
 334                remote_node = of_graph_get_remote_port_parent(ep);
 335                if (!remote_node ||
 336                    remote_node == parent || /* Ignore parent endpoint */
 337                    !of_device_is_available(remote_node))
 338                        continue;
 339
 340                count += meson_probe_remote(pdev, match, remote, remote_node);
 341
 342                of_node_put(remote_node);
 343        }
 344
 345        return count;
 346}
 347
 348static int meson_drv_probe(struct platform_device *pdev)
 349{
 350        struct component_match *match = NULL;
 351        struct device_node *np = pdev->dev.of_node;
 352        struct device_node *ep, *remote;
 353        int count = 0;
 354
 355        for_each_endpoint_of_node(np, ep) {
 356                remote = of_graph_get_remote_port_parent(ep);
 357                if (!remote || !of_device_is_available(remote))
 358                        continue;
 359
 360                count += meson_probe_remote(pdev, &match, np, remote);
 361        }
 362
 363        if (count && !match)
 364                return meson_drv_bind_master(&pdev->dev, false);
 365
 366        /* If some endpoints were found, initialize the nodes */
 367        if (count) {
 368                dev_info(&pdev->dev, "Queued %d outputs on vpu\n", count);
 369
 370                return component_master_add_with_match(&pdev->dev,
 371                                                       &meson_drv_master_ops,
 372                                                       match);
 373        }
 374
 375        /* If no output endpoints were available, simply bail out */
 376        return 0;
 377};
 378
 379static const struct of_device_id dt_match[] = {
 380        { .compatible = "amlogic,meson-gxbb-vpu" },
 381        { .compatible = "amlogic,meson-gxl-vpu" },
 382        { .compatible = "amlogic,meson-gxm-vpu" },
 383        {}
 384};
 385MODULE_DEVICE_TABLE(of, dt_match);
 386
 387static struct platform_driver meson_drm_platform_driver = {
 388        .probe      = meson_drv_probe,
 389        .driver     = {
 390                .name   = "meson-drm",
 391                .of_match_table = dt_match,
 392        },
 393};
 394
 395module_platform_driver(meson_drm_platform_driver);
 396
 397MODULE_AUTHOR("Jasper St. Pierre <jstpierre@mecheye.net>");
 398MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
 399MODULE_DESCRIPTION(DRIVER_DESC);
 400MODULE_LICENSE("GPL");
 401