linux/drivers/gpu/drm/mxsfb/mxsfb_drv.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
   3 *
   4 * This code is based on drivers/video/fbdev/mxsfb.c :
   5 * Copyright (C) 2010 Juergen Beisert, Pengutronix
   6 * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
   7 * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version 2
  12 * of the License, or (at your option) any later version.
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 */
  18
  19#include <linux/module.h>
  20#include <linux/spinlock.h>
  21#include <linux/clk.h>
  22#include <linux/component.h>
  23#include <linux/list.h>
  24#include <linux/of_device.h>
  25#include <linux/of_graph.h>
  26#include <linux/of_reserved_mem.h>
  27#include <linux/pm_runtime.h>
  28#include <linux/reservation.h>
  29
  30#include <drm/drmP.h>
  31#include <drm/drm_atomic.h>
  32#include <drm/drm_atomic_helper.h>
  33#include <drm/drm_crtc.h>
  34#include <drm/drm_crtc_helper.h>
  35#include <drm/drm_fb_helper.h>
  36#include <drm/drm_fb_cma_helper.h>
  37#include <drm/drm_gem_cma_helper.h>
  38#include <drm/drm_gem_framebuffer_helper.h>
  39#include <drm/drm_of.h>
  40#include <drm/drm_panel.h>
  41#include <drm/drm_simple_kms_helper.h>
  42
  43#include "mxsfb_drv.h"
  44#include "mxsfb_regs.h"
  45
  46enum mxsfb_devtype {
  47        MXSFB_V3,
  48        MXSFB_V4,
  49};
  50
  51static const struct mxsfb_devdata mxsfb_devdata[] = {
  52        [MXSFB_V3] = {
  53                .transfer_count = LCDC_V3_TRANSFER_COUNT,
  54                .cur_buf        = LCDC_V3_CUR_BUF,
  55                .next_buf       = LCDC_V3_NEXT_BUF,
  56                .debug0         = LCDC_V3_DEBUG0,
  57                .hs_wdth_mask   = 0xff,
  58                .hs_wdth_shift  = 24,
  59                .ipversion      = 3,
  60        },
  61        [MXSFB_V4] = {
  62                .transfer_count = LCDC_V4_TRANSFER_COUNT,
  63                .cur_buf        = LCDC_V4_CUR_BUF,
  64                .next_buf       = LCDC_V4_NEXT_BUF,
  65                .debug0         = LCDC_V4_DEBUG0,
  66                .hs_wdth_mask   = 0x3fff,
  67                .hs_wdth_shift  = 18,
  68                .ipversion      = 4,
  69        },
  70};
  71
  72static const uint32_t mxsfb_formats[] = {
  73        DRM_FORMAT_XRGB8888,
  74        DRM_FORMAT_RGB565
  75};
  76
  77static struct mxsfb_drm_private *
  78drm_pipe_to_mxsfb_drm_private(struct drm_simple_display_pipe *pipe)
  79{
  80        return container_of(pipe, struct mxsfb_drm_private, pipe);
  81}
  82
  83void mxsfb_enable_axi_clk(struct mxsfb_drm_private *mxsfb)
  84{
  85        if (mxsfb->clk_axi)
  86                clk_prepare_enable(mxsfb->clk_axi);
  87}
  88
  89void mxsfb_disable_axi_clk(struct mxsfb_drm_private *mxsfb)
  90{
  91        if (mxsfb->clk_axi)
  92                clk_disable_unprepare(mxsfb->clk_axi);
  93}
  94
  95static const struct drm_mode_config_funcs mxsfb_mode_config_funcs = {
  96        .fb_create              = drm_gem_fb_create,
  97        .atomic_check           = drm_atomic_helper_check,
  98        .atomic_commit          = drm_atomic_helper_commit,
  99};
 100
 101static const struct drm_mode_config_helper_funcs mxsfb_mode_config_helpers = {
 102        .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
 103};
 104
 105static void mxsfb_pipe_enable(struct drm_simple_display_pipe *pipe,
 106                              struct drm_crtc_state *crtc_state,
 107                              struct drm_plane_state *plane_state)
 108{
 109        struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
 110        struct drm_device *drm = pipe->plane.dev;
 111
 112        pm_runtime_get_sync(drm->dev);
 113        drm_panel_prepare(mxsfb->panel);
 114        mxsfb_crtc_enable(mxsfb);
 115        drm_panel_enable(mxsfb->panel);
 116}
 117
 118static void mxsfb_pipe_disable(struct drm_simple_display_pipe *pipe)
 119{
 120        struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
 121        struct drm_device *drm = pipe->plane.dev;
 122        struct drm_crtc *crtc = &pipe->crtc;
 123        struct drm_pending_vblank_event *event;
 124
 125        drm_panel_disable(mxsfb->panel);
 126        mxsfb_crtc_disable(mxsfb);
 127        drm_panel_unprepare(mxsfb->panel);
 128        pm_runtime_put_sync(drm->dev);
 129
 130        spin_lock_irq(&drm->event_lock);
 131        event = crtc->state->event;
 132        if (event) {
 133                crtc->state->event = NULL;
 134                drm_crtc_send_vblank_event(crtc, event);
 135        }
 136        spin_unlock_irq(&drm->event_lock);
 137}
 138
 139static void mxsfb_pipe_update(struct drm_simple_display_pipe *pipe,
 140                              struct drm_plane_state *plane_state)
 141{
 142        struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
 143
 144        mxsfb_plane_atomic_update(mxsfb, plane_state);
 145}
 146
 147static int mxsfb_pipe_enable_vblank(struct drm_simple_display_pipe *pipe)
 148{
 149        struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
 150
 151        /* Clear and enable VBLANK IRQ */
 152        mxsfb_enable_axi_clk(mxsfb);
 153        writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
 154        writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_SET);
 155        mxsfb_disable_axi_clk(mxsfb);
 156
 157        return 0;
 158}
 159
 160static void mxsfb_pipe_disable_vblank(struct drm_simple_display_pipe *pipe)
 161{
 162        struct mxsfb_drm_private *mxsfb = drm_pipe_to_mxsfb_drm_private(pipe);
 163
 164        /* Disable and clear VBLANK IRQ */
 165        mxsfb_enable_axi_clk(mxsfb);
 166        writel(CTRL1_CUR_FRAME_DONE_IRQ_EN, mxsfb->base + LCDC_CTRL1 + REG_CLR);
 167        writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
 168        mxsfb_disable_axi_clk(mxsfb);
 169}
 170
 171static struct drm_simple_display_pipe_funcs mxsfb_funcs = {
 172        .enable         = mxsfb_pipe_enable,
 173        .disable        = mxsfb_pipe_disable,
 174        .update         = mxsfb_pipe_update,
 175        .prepare_fb     = drm_gem_fb_simple_display_pipe_prepare_fb,
 176        .enable_vblank  = mxsfb_pipe_enable_vblank,
 177        .disable_vblank = mxsfb_pipe_disable_vblank,
 178};
 179
 180static int mxsfb_load(struct drm_device *drm, unsigned long flags)
 181{
 182        struct platform_device *pdev = to_platform_device(drm->dev);
 183        struct mxsfb_drm_private *mxsfb;
 184        struct resource *res;
 185        int ret;
 186
 187        mxsfb = devm_kzalloc(&pdev->dev, sizeof(*mxsfb), GFP_KERNEL);
 188        if (!mxsfb)
 189                return -ENOMEM;
 190
 191        drm->dev_private = mxsfb;
 192        mxsfb->devdata = &mxsfb_devdata[pdev->id_entry->driver_data];
 193
 194        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 195        mxsfb->base = devm_ioremap_resource(drm->dev, res);
 196        if (IS_ERR(mxsfb->base))
 197                return PTR_ERR(mxsfb->base);
 198
 199        mxsfb->clk = devm_clk_get(drm->dev, NULL);
 200        if (IS_ERR(mxsfb->clk))
 201                return PTR_ERR(mxsfb->clk);
 202
 203        mxsfb->clk_axi = devm_clk_get(drm->dev, "axi");
 204        if (IS_ERR(mxsfb->clk_axi))
 205                mxsfb->clk_axi = NULL;
 206
 207        mxsfb->clk_disp_axi = devm_clk_get(drm->dev, "disp_axi");
 208        if (IS_ERR(mxsfb->clk_disp_axi))
 209                mxsfb->clk_disp_axi = NULL;
 210
 211        ret = dma_set_mask_and_coherent(drm->dev, DMA_BIT_MASK(32));
 212        if (ret)
 213                return ret;
 214
 215        pm_runtime_enable(drm->dev);
 216
 217        ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
 218        if (ret < 0) {
 219                dev_err(drm->dev, "Failed to initialise vblank\n");
 220                goto err_vblank;
 221        }
 222
 223        /* Modeset init */
 224        drm_mode_config_init(drm);
 225
 226        ret = mxsfb_create_output(drm);
 227        if (ret < 0) {
 228                dev_err(drm->dev, "Failed to create outputs\n");
 229                goto err_vblank;
 230        }
 231
 232        ret = drm_simple_display_pipe_init(drm, &mxsfb->pipe, &mxsfb_funcs,
 233                        mxsfb_formats, ARRAY_SIZE(mxsfb_formats), NULL,
 234                        &mxsfb->connector);
 235        if (ret < 0) {
 236                dev_err(drm->dev, "Cannot setup simple display pipe\n");
 237                goto err_vblank;
 238        }
 239
 240        ret = drm_panel_attach(mxsfb->panel, &mxsfb->connector);
 241        if (ret) {
 242                dev_err(drm->dev, "Cannot connect panel\n");
 243                goto err_vblank;
 244        }
 245
 246        drm->mode_config.min_width      = MXSFB_MIN_XRES;
 247        drm->mode_config.min_height     = MXSFB_MIN_YRES;
 248        drm->mode_config.max_width      = MXSFB_MAX_XRES;
 249        drm->mode_config.max_height     = MXSFB_MAX_YRES;
 250        drm->mode_config.funcs          = &mxsfb_mode_config_funcs;
 251        drm->mode_config.helper_private = &mxsfb_mode_config_helpers;
 252
 253        drm_mode_config_reset(drm);
 254
 255        pm_runtime_get_sync(drm->dev);
 256        ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
 257        pm_runtime_put_sync(drm->dev);
 258
 259        if (ret < 0) {
 260                dev_err(drm->dev, "Failed to install IRQ handler\n");
 261                goto err_irq;
 262        }
 263
 264        drm_kms_helper_poll_init(drm);
 265
 266        mxsfb->fbdev = drm_fbdev_cma_init(drm, 32,
 267                                          drm->mode_config.num_connector);
 268        if (IS_ERR(mxsfb->fbdev)) {
 269                ret = PTR_ERR(mxsfb->fbdev);
 270                mxsfb->fbdev = NULL;
 271                dev_err(drm->dev, "Failed to init FB CMA area\n");
 272                goto err_cma;
 273        }
 274
 275        platform_set_drvdata(pdev, drm);
 276
 277        drm_helper_hpd_irq_event(drm);
 278
 279        return 0;
 280
 281err_cma:
 282        drm_irq_uninstall(drm);
 283err_irq:
 284        drm_panel_detach(mxsfb->panel);
 285err_vblank:
 286        pm_runtime_disable(drm->dev);
 287
 288        return ret;
 289}
 290
 291static void mxsfb_unload(struct drm_device *drm)
 292{
 293        struct mxsfb_drm_private *mxsfb = drm->dev_private;
 294
 295        if (mxsfb->fbdev)
 296                drm_fbdev_cma_fini(mxsfb->fbdev);
 297
 298        drm_kms_helper_poll_fini(drm);
 299        drm_mode_config_cleanup(drm);
 300
 301        pm_runtime_get_sync(drm->dev);
 302        drm_irq_uninstall(drm);
 303        pm_runtime_put_sync(drm->dev);
 304
 305        drm->dev_private = NULL;
 306
 307        pm_runtime_disable(drm->dev);
 308}
 309
 310static void mxsfb_lastclose(struct drm_device *drm)
 311{
 312        struct mxsfb_drm_private *mxsfb = drm->dev_private;
 313
 314        drm_fbdev_cma_restore_mode(mxsfb->fbdev);
 315}
 316
 317static void mxsfb_irq_preinstall(struct drm_device *drm)
 318{
 319        struct mxsfb_drm_private *mxsfb = drm->dev_private;
 320
 321        mxsfb_pipe_disable_vblank(&mxsfb->pipe);
 322}
 323
 324static irqreturn_t mxsfb_irq_handler(int irq, void *data)
 325{
 326        struct drm_device *drm = data;
 327        struct mxsfb_drm_private *mxsfb = drm->dev_private;
 328        u32 reg;
 329
 330        mxsfb_enable_axi_clk(mxsfb);
 331
 332        reg = readl(mxsfb->base + LCDC_CTRL1);
 333
 334        if (reg & CTRL1_CUR_FRAME_DONE_IRQ)
 335                drm_crtc_handle_vblank(&mxsfb->pipe.crtc);
 336
 337        writel(CTRL1_CUR_FRAME_DONE_IRQ, mxsfb->base + LCDC_CTRL1 + REG_CLR);
 338
 339        mxsfb_disable_axi_clk(mxsfb);
 340
 341        return IRQ_HANDLED;
 342}
 343
 344DEFINE_DRM_GEM_CMA_FOPS(fops);
 345
 346static struct drm_driver mxsfb_driver = {
 347        .driver_features        = DRIVER_GEM | DRIVER_MODESET |
 348                                  DRIVER_PRIME | DRIVER_ATOMIC |
 349                                  DRIVER_HAVE_IRQ,
 350        .lastclose              = mxsfb_lastclose,
 351        .irq_handler            = mxsfb_irq_handler,
 352        .irq_preinstall         = mxsfb_irq_preinstall,
 353        .irq_uninstall          = mxsfb_irq_preinstall,
 354        .gem_free_object_unlocked = drm_gem_cma_free_object,
 355        .gem_vm_ops             = &drm_gem_cma_vm_ops,
 356        .dumb_create            = drm_gem_cma_dumb_create,
 357        .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
 358        .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
 359        .gem_prime_export       = drm_gem_prime_export,
 360        .gem_prime_import       = drm_gem_prime_import,
 361        .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
 362        .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
 363        .gem_prime_vmap         = drm_gem_cma_prime_vmap,
 364        .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
 365        .gem_prime_mmap         = drm_gem_cma_prime_mmap,
 366        .fops   = &fops,
 367        .name   = "mxsfb-drm",
 368        .desc   = "MXSFB Controller DRM",
 369        .date   = "20160824",
 370        .major  = 1,
 371        .minor  = 0,
 372};
 373
 374static const struct platform_device_id mxsfb_devtype[] = {
 375        { .name = "imx23-fb", .driver_data = MXSFB_V3, },
 376        { .name = "imx28-fb", .driver_data = MXSFB_V4, },
 377        { .name = "imx6sx-fb", .driver_data = MXSFB_V4, },
 378        { /* sentinel */ }
 379};
 380MODULE_DEVICE_TABLE(platform, mxsfb_devtype);
 381
 382static const struct of_device_id mxsfb_dt_ids[] = {
 383        { .compatible = "fsl,imx23-lcdif", .data = &mxsfb_devtype[0], },
 384        { .compatible = "fsl,imx28-lcdif", .data = &mxsfb_devtype[1], },
 385        { .compatible = "fsl,imx6sx-lcdif", .data = &mxsfb_devtype[2], },
 386        { /* sentinel */ }
 387};
 388MODULE_DEVICE_TABLE(of, mxsfb_dt_ids);
 389
 390static int mxsfb_probe(struct platform_device *pdev)
 391{
 392        struct drm_device *drm;
 393        const struct of_device_id *of_id =
 394                        of_match_device(mxsfb_dt_ids, &pdev->dev);
 395        int ret;
 396
 397        if (!pdev->dev.of_node)
 398                return -ENODEV;
 399
 400        if (of_id)
 401                pdev->id_entry = of_id->data;
 402
 403        drm = drm_dev_alloc(&mxsfb_driver, &pdev->dev);
 404        if (IS_ERR(drm))
 405                return PTR_ERR(drm);
 406
 407        ret = mxsfb_load(drm, 0);
 408        if (ret)
 409                goto err_free;
 410
 411        ret = drm_dev_register(drm, 0);
 412        if (ret)
 413                goto err_unload;
 414
 415        return 0;
 416
 417err_unload:
 418        mxsfb_unload(drm);
 419err_free:
 420        drm_dev_put(drm);
 421
 422        return ret;
 423}
 424
 425static int mxsfb_remove(struct platform_device *pdev)
 426{
 427        struct drm_device *drm = platform_get_drvdata(pdev);
 428
 429        drm_dev_unregister(drm);
 430        mxsfb_unload(drm);
 431        drm_dev_put(drm);
 432
 433        return 0;
 434}
 435
 436#ifdef CONFIG_PM_SLEEP
 437static int mxsfb_suspend(struct device *dev)
 438{
 439        struct drm_device *drm = dev_get_drvdata(dev);
 440
 441        return drm_mode_config_helper_suspend(drm);
 442}
 443
 444static int mxsfb_resume(struct device *dev)
 445{
 446        struct drm_device *drm = dev_get_drvdata(dev);
 447
 448        return drm_mode_config_helper_resume(drm);
 449}
 450#endif
 451
 452static const struct dev_pm_ops mxsfb_pm_ops = {
 453        SET_SYSTEM_SLEEP_PM_OPS(mxsfb_suspend, mxsfb_resume)
 454};
 455
 456static struct platform_driver mxsfb_platform_driver = {
 457        .probe          = mxsfb_probe,
 458        .remove         = mxsfb_remove,
 459        .id_table       = mxsfb_devtype,
 460        .driver = {
 461                .name           = "mxsfb",
 462                .of_match_table = mxsfb_dt_ids,
 463                .pm             = &mxsfb_pm_ops,
 464        },
 465};
 466
 467module_platform_driver(mxsfb_platform_driver);
 468
 469MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
 470MODULE_DESCRIPTION("Freescale MXS DRM/KMS driver");
 471MODULE_LICENSE("GPL");
 472