linux/drivers/gpu/drm/shmobile/shmob_drm_drv.c
<<
>>
Prefs
   1/*
   2 * shmob_drm_drv.c  --  SH Mobile DRM driver
   3 *
   4 * Copyright (C) 2012 Renesas Electronics Corporation
   5 *
   6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include <linux/clk.h>
  15#include <linux/io.h>
  16#include <linux/mm.h>
  17#include <linux/module.h>
  18#include <linux/platform_device.h>
  19#include <linux/pm.h>
  20#include <linux/slab.h>
  21
  22#include <drm/drmP.h>
  23#include <drm/drm_crtc_helper.h>
  24#include <drm/drm_gem_cma_helper.h>
  25
  26#include "shmob_drm_drv.h"
  27#include "shmob_drm_kms.h"
  28#include "shmob_drm_plane.h"
  29#include "shmob_drm_regs.h"
  30
  31/* -----------------------------------------------------------------------------
  32 * Hardware initialization
  33 */
  34
  35static int shmob_drm_init_interface(struct shmob_drm_device *sdev)
  36{
  37        static const u32 ldmt1r[] = {
  38                [SHMOB_DRM_IFACE_RGB8] = LDMT1R_MIFTYP_RGB8,
  39                [SHMOB_DRM_IFACE_RGB9] = LDMT1R_MIFTYP_RGB9,
  40                [SHMOB_DRM_IFACE_RGB12A] = LDMT1R_MIFTYP_RGB12A,
  41                [SHMOB_DRM_IFACE_RGB12B] = LDMT1R_MIFTYP_RGB12B,
  42                [SHMOB_DRM_IFACE_RGB16] = LDMT1R_MIFTYP_RGB16,
  43                [SHMOB_DRM_IFACE_RGB18] = LDMT1R_MIFTYP_RGB18,
  44                [SHMOB_DRM_IFACE_RGB24] = LDMT1R_MIFTYP_RGB24,
  45                [SHMOB_DRM_IFACE_YUV422] = LDMT1R_MIFTYP_YCBCR,
  46                [SHMOB_DRM_IFACE_SYS8A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8A,
  47                [SHMOB_DRM_IFACE_SYS8B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8B,
  48                [SHMOB_DRM_IFACE_SYS8C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8C,
  49                [SHMOB_DRM_IFACE_SYS8D] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS8D,
  50                [SHMOB_DRM_IFACE_SYS9] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS9,
  51                [SHMOB_DRM_IFACE_SYS12] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS12,
  52                [SHMOB_DRM_IFACE_SYS16A] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16A,
  53                [SHMOB_DRM_IFACE_SYS16B] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16B,
  54                [SHMOB_DRM_IFACE_SYS16C] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS16C,
  55                [SHMOB_DRM_IFACE_SYS18] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS18,
  56                [SHMOB_DRM_IFACE_SYS24] = LDMT1R_IFM | LDMT1R_MIFTYP_SYS24,
  57        };
  58
  59        if (sdev->pdata->iface.interface >= ARRAY_SIZE(ldmt1r)) {
  60                dev_err(sdev->dev, "invalid interface type %u\n",
  61                        sdev->pdata->iface.interface);
  62                return -EINVAL;
  63        }
  64
  65        sdev->ldmt1r = ldmt1r[sdev->pdata->iface.interface];
  66        return 0;
  67}
  68
  69static int shmob_drm_setup_clocks(struct shmob_drm_device *sdev,
  70                                            enum shmob_drm_clk_source clksrc)
  71{
  72        struct clk *clk;
  73        char *clkname;
  74
  75        switch (clksrc) {
  76        case SHMOB_DRM_CLK_BUS:
  77                clkname = "bus_clk";
  78                sdev->lddckr = LDDCKR_ICKSEL_BUS;
  79                break;
  80        case SHMOB_DRM_CLK_PERIPHERAL:
  81                clkname = "peripheral_clk";
  82                sdev->lddckr = LDDCKR_ICKSEL_MIPI;
  83                break;
  84        case SHMOB_DRM_CLK_EXTERNAL:
  85                clkname = NULL;
  86                sdev->lddckr = LDDCKR_ICKSEL_HDMI;
  87                break;
  88        default:
  89                return -EINVAL;
  90        }
  91
  92        clk = devm_clk_get(sdev->dev, clkname);
  93        if (IS_ERR(clk)) {
  94                dev_err(sdev->dev, "cannot get dot clock %s\n", clkname);
  95                return PTR_ERR(clk);
  96        }
  97
  98        sdev->clock = clk;
  99        return 0;
 100}
 101
 102/* -----------------------------------------------------------------------------
 103 * DRM operations
 104 */
 105
 106static irqreturn_t shmob_drm_irq(int irq, void *arg)
 107{
 108        struct drm_device *dev = arg;
 109        struct shmob_drm_device *sdev = dev->dev_private;
 110        unsigned long flags;
 111        u32 status;
 112
 113        /* Acknowledge interrupts. Putting interrupt enable and interrupt flag
 114         * bits in the same register is really brain-dead design and requires
 115         * taking a spinlock.
 116         */
 117        spin_lock_irqsave(&sdev->irq_lock, flags);
 118        status = lcdc_read(sdev, LDINTR);
 119        lcdc_write(sdev, LDINTR, status ^ LDINTR_STATUS_MASK);
 120        spin_unlock_irqrestore(&sdev->irq_lock, flags);
 121
 122        if (status & LDINTR_VES) {
 123                drm_handle_vblank(dev, 0);
 124                shmob_drm_crtc_finish_page_flip(&sdev->crtc);
 125        }
 126
 127        return IRQ_HANDLED;
 128}
 129
 130DEFINE_DRM_GEM_CMA_FOPS(shmob_drm_fops);
 131
 132static struct drm_driver shmob_drm_driver = {
 133        .driver_features        = DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET
 134                                | DRIVER_PRIME,
 135        .irq_handler            = shmob_drm_irq,
 136        .gem_free_object_unlocked = drm_gem_cma_free_object,
 137        .gem_vm_ops             = &drm_gem_cma_vm_ops,
 138        .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
 139        .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
 140        .gem_prime_import       = drm_gem_prime_import,
 141        .gem_prime_export       = drm_gem_prime_export,
 142        .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
 143        .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
 144        .gem_prime_vmap         = drm_gem_cma_prime_vmap,
 145        .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
 146        .gem_prime_mmap         = drm_gem_cma_prime_mmap,
 147        .dumb_create            = drm_gem_cma_dumb_create,
 148        .fops                   = &shmob_drm_fops,
 149        .name                   = "shmob-drm",
 150        .desc                   = "Renesas SH Mobile DRM",
 151        .date                   = "20120424",
 152        .major                  = 1,
 153        .minor                  = 0,
 154};
 155
 156/* -----------------------------------------------------------------------------
 157 * Power management
 158 */
 159
 160#ifdef CONFIG_PM_SLEEP
 161static int shmob_drm_pm_suspend(struct device *dev)
 162{
 163        struct shmob_drm_device *sdev = dev_get_drvdata(dev);
 164
 165        drm_kms_helper_poll_disable(sdev->ddev);
 166        shmob_drm_crtc_suspend(&sdev->crtc);
 167
 168        return 0;
 169}
 170
 171static int shmob_drm_pm_resume(struct device *dev)
 172{
 173        struct shmob_drm_device *sdev = dev_get_drvdata(dev);
 174
 175        drm_modeset_lock_all(sdev->ddev);
 176        shmob_drm_crtc_resume(&sdev->crtc);
 177        drm_modeset_unlock_all(sdev->ddev);
 178
 179        drm_kms_helper_poll_enable(sdev->ddev);
 180        return 0;
 181}
 182#endif
 183
 184static const struct dev_pm_ops shmob_drm_pm_ops = {
 185        SET_SYSTEM_SLEEP_PM_OPS(shmob_drm_pm_suspend, shmob_drm_pm_resume)
 186};
 187
 188/* -----------------------------------------------------------------------------
 189 * Platform driver
 190 */
 191
 192static int shmob_drm_remove(struct platform_device *pdev)
 193{
 194        struct shmob_drm_device *sdev = platform_get_drvdata(pdev);
 195        struct drm_device *ddev = sdev->ddev;
 196
 197        drm_dev_unregister(ddev);
 198        drm_kms_helper_poll_fini(ddev);
 199        drm_mode_config_cleanup(ddev);
 200        drm_irq_uninstall(ddev);
 201        drm_dev_unref(ddev);
 202
 203        return 0;
 204}
 205
 206static int shmob_drm_probe(struct platform_device *pdev)
 207{
 208        struct shmob_drm_platform_data *pdata = pdev->dev.platform_data;
 209        struct shmob_drm_device *sdev;
 210        struct drm_device *ddev;
 211        struct resource *res;
 212        unsigned int i;
 213        int ret;
 214
 215        if (pdata == NULL) {
 216                dev_err(&pdev->dev, "no platform data\n");
 217                return -EINVAL;
 218        }
 219
 220        /*
 221         * Allocate and initialize the driver private data, I/O resources and
 222         * clocks.
 223         */
 224        sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
 225        if (sdev == NULL)
 226                return -ENOMEM;
 227
 228        sdev->dev = &pdev->dev;
 229        sdev->pdata = pdata;
 230        spin_lock_init(&sdev->irq_lock);
 231
 232        platform_set_drvdata(pdev, sdev);
 233
 234        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 235        sdev->mmio = devm_ioremap_resource(&pdev->dev, res);
 236        if (sdev->mmio == NULL)
 237                return -ENOMEM;
 238
 239        ret = shmob_drm_setup_clocks(sdev, pdata->clk_source);
 240        if (ret < 0)
 241                return ret;
 242
 243        ret = shmob_drm_init_interface(sdev);
 244        if (ret < 0)
 245                return ret;
 246
 247        /* Allocate and initialize the DRM device. */
 248        ddev = drm_dev_alloc(&shmob_drm_driver, &pdev->dev);
 249        if (IS_ERR(ddev))
 250                return PTR_ERR(ddev);
 251
 252        sdev->ddev = ddev;
 253        ddev->dev_private = sdev;
 254
 255        ret = shmob_drm_modeset_init(sdev);
 256        if (ret < 0) {
 257                dev_err(&pdev->dev, "failed to initialize mode setting\n");
 258                goto err_free_drm_dev;
 259        }
 260
 261        for (i = 0; i < 4; ++i) {
 262                ret = shmob_drm_plane_create(sdev, i);
 263                if (ret < 0) {
 264                        dev_err(&pdev->dev, "failed to create plane %u\n", i);
 265                        goto err_modeset_cleanup;
 266                }
 267        }
 268
 269        ret = drm_vblank_init(ddev, 1);
 270        if (ret < 0) {
 271                dev_err(&pdev->dev, "failed to initialize vblank\n");
 272                goto err_modeset_cleanup;
 273        }
 274
 275        ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
 276        if (ret < 0) {
 277                dev_err(&pdev->dev, "failed to install IRQ handler\n");
 278                goto err_modeset_cleanup;
 279        }
 280
 281        /*
 282         * Register the DRM device with the core and the connectors with
 283         * sysfs.
 284         */
 285        ret = drm_dev_register(ddev, 0);
 286        if (ret < 0)
 287                goto err_irq_uninstall;
 288
 289        return 0;
 290
 291err_irq_uninstall:
 292        drm_irq_uninstall(ddev);
 293err_modeset_cleanup:
 294        drm_kms_helper_poll_fini(ddev);
 295        drm_mode_config_cleanup(ddev);
 296err_free_drm_dev:
 297        drm_dev_unref(ddev);
 298
 299        return ret;
 300}
 301
 302static struct platform_driver shmob_drm_platform_driver = {
 303        .probe          = shmob_drm_probe,
 304        .remove         = shmob_drm_remove,
 305        .driver         = {
 306                .name   = "shmob-drm",
 307                .pm     = &shmob_drm_pm_ops,
 308        },
 309};
 310
 311module_platform_driver(shmob_drm_platform_driver);
 312
 313MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
 314MODULE_DESCRIPTION("Renesas SH Mobile DRM Driver");
 315MODULE_LICENSE("GPL");
 316