linux/drivers/staging/imx-drm/imx-drm-core.c
<<
>>
Prefs
   1/*
   2 * Freescale i.MX drm driver
   3 *
   4 * Copyright (C) 2011 Sascha Hauer, Pengutronix
   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
   8 * as published by the Free Software Foundation; either version 2
   9 * of the License, or (at your option) any later version.
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 */
  16
  17#include <linux/device.h>
  18#include <linux/platform_device.h>
  19#include <drm/drmP.h>
  20#include <drm/drm_fb_helper.h>
  21#include <drm/drm_crtc_helper.h>
  22#include <linux/fb.h>
  23#include <linux/module.h>
  24#include <drm/drm_gem_cma_helper.h>
  25#include <drm/drm_fb_cma_helper.h>
  26
  27#include "imx-drm.h"
  28
  29#define MAX_CRTC        4
  30
  31struct crtc_cookie {
  32        void *cookie;
  33        int id;
  34        struct list_head list;
  35};
  36
  37struct imx_drm_device {
  38        struct drm_device                       *drm;
  39        struct device                           *dev;
  40        struct list_head                        crtc_list;
  41        struct list_head                        encoder_list;
  42        struct list_head                        connector_list;
  43        struct mutex                            mutex;
  44        int                                     references;
  45        int                                     pipes;
  46        struct drm_fbdev_cma                    *fbhelper;
  47};
  48
  49struct imx_drm_crtc {
  50        struct drm_crtc                         *crtc;
  51        struct list_head                        list;
  52        struct imx_drm_device                   *imxdrm;
  53        int                                     pipe;
  54        struct imx_drm_crtc_helper_funcs        imx_drm_helper_funcs;
  55        struct module                           *owner;
  56        struct crtc_cookie                      cookie;
  57};
  58
  59struct imx_drm_encoder {
  60        struct drm_encoder                      *encoder;
  61        struct list_head                        list;
  62        struct module                           *owner;
  63        struct list_head                        possible_crtcs;
  64};
  65
  66struct imx_drm_connector {
  67        struct drm_connector                    *connector;
  68        struct list_head                        list;
  69        struct module                           *owner;
  70};
  71
  72static int imx_drm_driver_firstopen(struct drm_device *drm)
  73{
  74        if (!imx_drm_device_get())
  75                return -EINVAL;
  76
  77        return 0;
  78}
  79
  80static void imx_drm_driver_lastclose(struct drm_device *drm)
  81{
  82        struct imx_drm_device *imxdrm = drm->dev_private;
  83
  84        if (imxdrm->fbhelper)
  85                drm_fbdev_cma_restore_mode(imxdrm->fbhelper);
  86
  87        imx_drm_device_put();
  88}
  89
  90static int imx_drm_driver_unload(struct drm_device *drm)
  91{
  92        struct imx_drm_device *imxdrm = drm->dev_private;
  93
  94        drm_mode_config_cleanup(imxdrm->drm);
  95        drm_kms_helper_poll_fini(imxdrm->drm);
  96
  97        return 0;
  98}
  99
 100/*
 101 * We don't care at all for crtc numbers, but the core expects the
 102 * crtcs to be numbered
 103 */
 104static struct imx_drm_crtc *imx_drm_crtc_by_num(struct imx_drm_device *imxdrm,
 105                int num)
 106{
 107        struct imx_drm_crtc *imx_drm_crtc;
 108
 109        list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list)
 110                if (imx_drm_crtc->pipe == num)
 111                        return imx_drm_crtc;
 112        return NULL;
 113}
 114
 115int imx_drm_crtc_panel_format_pins(struct drm_crtc *crtc, u32 encoder_type,
 116                u32 interface_pix_fmt, int hsync_pin, int vsync_pin)
 117{
 118        struct imx_drm_device *imxdrm = crtc->dev->dev_private;
 119        struct imx_drm_crtc *imx_crtc;
 120        struct imx_drm_crtc_helper_funcs *helper;
 121
 122        mutex_lock(&imxdrm->mutex);
 123
 124        list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list)
 125                if (imx_crtc->crtc == crtc)
 126                        goto found;
 127
 128        mutex_unlock(&imxdrm->mutex);
 129
 130        return -EINVAL;
 131found:
 132        mutex_unlock(&imxdrm->mutex);
 133
 134        helper = &imx_crtc->imx_drm_helper_funcs;
 135        if (helper->set_interface_pix_fmt)
 136                return helper->set_interface_pix_fmt(crtc,
 137                                encoder_type, interface_pix_fmt,
 138                                hsync_pin, vsync_pin);
 139        return 0;
 140}
 141EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format_pins);
 142
 143int imx_drm_crtc_panel_format(struct drm_crtc *crtc, u32 encoder_type,
 144                u32 interface_pix_fmt)
 145{
 146        return imx_drm_crtc_panel_format_pins(crtc, encoder_type,
 147                                              interface_pix_fmt, 2, 3);
 148}
 149EXPORT_SYMBOL_GPL(imx_drm_crtc_panel_format);
 150
 151int imx_drm_crtc_vblank_get(struct imx_drm_crtc *imx_drm_crtc)
 152{
 153        return drm_vblank_get(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
 154}
 155EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_get);
 156
 157void imx_drm_crtc_vblank_put(struct imx_drm_crtc *imx_drm_crtc)
 158{
 159        drm_vblank_put(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
 160}
 161EXPORT_SYMBOL_GPL(imx_drm_crtc_vblank_put);
 162
 163void imx_drm_handle_vblank(struct imx_drm_crtc *imx_drm_crtc)
 164{
 165        drm_handle_vblank(imx_drm_crtc->imxdrm->drm, imx_drm_crtc->pipe);
 166}
 167EXPORT_SYMBOL_GPL(imx_drm_handle_vblank);
 168
 169static int imx_drm_enable_vblank(struct drm_device *drm, int crtc)
 170{
 171        struct imx_drm_device *imxdrm = drm->dev_private;
 172        struct imx_drm_crtc *imx_drm_crtc;
 173        int ret;
 174
 175        imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
 176        if (!imx_drm_crtc)
 177                return -EINVAL;
 178
 179        if (!imx_drm_crtc->imx_drm_helper_funcs.enable_vblank)
 180                return -ENOSYS;
 181
 182        ret = imx_drm_crtc->imx_drm_helper_funcs.enable_vblank(
 183                        imx_drm_crtc->crtc);
 184
 185        return ret;
 186}
 187
 188static void imx_drm_disable_vblank(struct drm_device *drm, int crtc)
 189{
 190        struct imx_drm_device *imxdrm = drm->dev_private;
 191        struct imx_drm_crtc *imx_drm_crtc;
 192
 193        imx_drm_crtc = imx_drm_crtc_by_num(imxdrm, crtc);
 194        if (!imx_drm_crtc)
 195                return;
 196
 197        if (!imx_drm_crtc->imx_drm_helper_funcs.disable_vblank)
 198                return;
 199
 200        imx_drm_crtc->imx_drm_helper_funcs.disable_vblank(imx_drm_crtc->crtc);
 201}
 202
 203static const struct file_operations imx_drm_driver_fops = {
 204        .owner = THIS_MODULE,
 205        .open = drm_open,
 206        .release = drm_release,
 207        .unlocked_ioctl = drm_ioctl,
 208        .mmap = drm_gem_cma_mmap,
 209        .poll = drm_poll,
 210        .fasync = drm_fasync,
 211        .read = drm_read,
 212        .llseek = noop_llseek,
 213};
 214
 215static struct imx_drm_device *imx_drm_device;
 216
 217static struct imx_drm_device *__imx_drm_device(void)
 218{
 219        return imx_drm_device;
 220}
 221
 222struct drm_device *imx_drm_device_get(void)
 223{
 224        struct imx_drm_device *imxdrm = __imx_drm_device();
 225        struct imx_drm_encoder *enc;
 226        struct imx_drm_connector *con;
 227        struct imx_drm_crtc *crtc;
 228
 229        mutex_lock(&imxdrm->mutex);
 230
 231        list_for_each_entry(enc, &imxdrm->encoder_list, list) {
 232                if (!try_module_get(enc->owner)) {
 233                        dev_err(imxdrm->dev, "could not get module %s\n",
 234                                        module_name(enc->owner));
 235                        goto unwind_enc;
 236                }
 237        }
 238
 239        list_for_each_entry(con, &imxdrm->connector_list, list) {
 240                if (!try_module_get(con->owner)) {
 241                        dev_err(imxdrm->dev, "could not get module %s\n",
 242                                        module_name(con->owner));
 243                        goto unwind_con;
 244                }
 245        }
 246
 247        list_for_each_entry(crtc, &imxdrm->crtc_list, list) {
 248                if (!try_module_get(crtc->owner)) {
 249                        dev_err(imxdrm->dev, "could not get module %s\n",
 250                                        module_name(crtc->owner));
 251                        goto unwind_crtc;
 252                }
 253        }
 254
 255        imxdrm->references++;
 256
 257        mutex_unlock(&imxdrm->mutex);
 258
 259        return imxdrm->drm;
 260
 261unwind_crtc:
 262        list_for_each_entry_continue_reverse(crtc, &imxdrm->crtc_list, list)
 263                module_put(crtc->owner);
 264unwind_con:
 265        list_for_each_entry_continue_reverse(con, &imxdrm->connector_list, list)
 266                module_put(con->owner);
 267unwind_enc:
 268        list_for_each_entry_continue_reverse(enc, &imxdrm->encoder_list, list)
 269                module_put(enc->owner);
 270
 271        mutex_unlock(&imxdrm->mutex);
 272
 273        return NULL;
 274
 275}
 276EXPORT_SYMBOL_GPL(imx_drm_device_get);
 277
 278void imx_drm_device_put(void)
 279{
 280        struct imx_drm_device *imxdrm = __imx_drm_device();
 281        struct imx_drm_encoder *enc;
 282        struct imx_drm_connector *con;
 283        struct imx_drm_crtc *crtc;
 284
 285        mutex_lock(&imxdrm->mutex);
 286
 287        list_for_each_entry(crtc, &imxdrm->crtc_list, list)
 288                module_put(crtc->owner);
 289
 290        list_for_each_entry(con, &imxdrm->connector_list, list)
 291                module_put(con->owner);
 292
 293        list_for_each_entry(enc, &imxdrm->encoder_list, list)
 294                module_put(enc->owner);
 295
 296        imxdrm->references--;
 297
 298        mutex_unlock(&imxdrm->mutex);
 299}
 300EXPORT_SYMBOL_GPL(imx_drm_device_put);
 301
 302static int drm_mode_group_reinit(struct drm_device *dev)
 303{
 304        struct drm_mode_group *group = &dev->primary->mode_group;
 305        uint32_t *id_list = group->id_list;
 306        int ret;
 307
 308        ret = drm_mode_group_init_legacy_group(dev, group);
 309        if (ret < 0)
 310                return ret;
 311
 312        kfree(id_list);
 313        return 0;
 314}
 315
 316/*
 317 * register an encoder to the drm core
 318 */
 319static int imx_drm_encoder_register(struct imx_drm_encoder *imx_drm_encoder)
 320{
 321        struct imx_drm_device *imxdrm = __imx_drm_device();
 322
 323        INIT_LIST_HEAD(&imx_drm_encoder->possible_crtcs);
 324
 325        drm_encoder_init(imxdrm->drm, imx_drm_encoder->encoder,
 326                        imx_drm_encoder->encoder->funcs,
 327                        imx_drm_encoder->encoder->encoder_type);
 328
 329        drm_mode_group_reinit(imxdrm->drm);
 330
 331        return 0;
 332}
 333
 334/*
 335 * unregister an encoder from the drm core
 336 */
 337static void imx_drm_encoder_unregister(struct imx_drm_encoder
 338                *imx_drm_encoder)
 339{
 340        struct imx_drm_device *imxdrm = __imx_drm_device();
 341
 342        drm_encoder_cleanup(imx_drm_encoder->encoder);
 343
 344        drm_mode_group_reinit(imxdrm->drm);
 345}
 346
 347/*
 348 * register a connector to the drm core
 349 */
 350static int imx_drm_connector_register(
 351                struct imx_drm_connector *imx_drm_connector)
 352{
 353        struct imx_drm_device *imxdrm = __imx_drm_device();
 354
 355        drm_connector_init(imxdrm->drm, imx_drm_connector->connector,
 356                        imx_drm_connector->connector->funcs,
 357                        imx_drm_connector->connector->connector_type);
 358        drm_mode_group_reinit(imxdrm->drm);
 359
 360        return drm_sysfs_connector_add(imx_drm_connector->connector);
 361}
 362
 363/*
 364 * unregister a connector from the drm core
 365 */
 366static void imx_drm_connector_unregister(
 367                struct imx_drm_connector *imx_drm_connector)
 368{
 369        struct imx_drm_device *imxdrm = __imx_drm_device();
 370
 371        drm_sysfs_connector_remove(imx_drm_connector->connector);
 372        drm_connector_cleanup(imx_drm_connector->connector);
 373
 374        drm_mode_group_reinit(imxdrm->drm);
 375}
 376
 377/*
 378 * register a crtc to the drm core
 379 */
 380static int imx_drm_crtc_register(struct imx_drm_crtc *imx_drm_crtc)
 381{
 382        struct imx_drm_device *imxdrm = __imx_drm_device();
 383        int ret;
 384
 385        drm_crtc_init(imxdrm->drm, imx_drm_crtc->crtc,
 386                        imx_drm_crtc->imx_drm_helper_funcs.crtc_funcs);
 387        ret = drm_mode_crtc_set_gamma_size(imx_drm_crtc->crtc, 256);
 388        if (ret)
 389                return ret;
 390
 391        drm_crtc_helper_add(imx_drm_crtc->crtc,
 392                        imx_drm_crtc->imx_drm_helper_funcs.crtc_helper_funcs);
 393
 394        drm_mode_group_reinit(imxdrm->drm);
 395
 396        return 0;
 397}
 398
 399/*
 400 * Called by the CRTC driver when all CRTCs are registered. This
 401 * puts all the pieces together and initializes the driver.
 402 * Once this is called no more CRTCs can be registered since
 403 * the drm core has hardcoded the number of crtcs in several
 404 * places.
 405 */
 406static int imx_drm_driver_load(struct drm_device *drm, unsigned long flags)
 407{
 408        struct imx_drm_device *imxdrm = __imx_drm_device();
 409        int ret;
 410
 411        imxdrm->drm = drm;
 412
 413        drm->dev_private = imxdrm;
 414
 415        /*
 416         * enable drm irq mode.
 417         * - with irq_enabled = 1, we can use the vblank feature.
 418         *
 419         * P.S. note that we wouldn't use drm irq handler but
 420         *      just specific driver own one instead because
 421         *      drm framework supports only one irq handler and
 422         *      drivers can well take care of their interrupts
 423         */
 424        drm->irq_enabled = 1;
 425
 426        drm_mode_config_init(drm);
 427        imx_drm_mode_config_init(drm);
 428
 429        mutex_lock(&imxdrm->mutex);
 430
 431        drm_kms_helper_poll_init(imxdrm->drm);
 432
 433        /* setup the grouping for the legacy output */
 434        ret = drm_mode_group_init_legacy_group(imxdrm->drm,
 435                        &imxdrm->drm->primary->mode_group);
 436        if (ret)
 437                goto err_init;
 438
 439        ret = drm_vblank_init(imxdrm->drm, MAX_CRTC);
 440        if (ret)
 441                goto err_init;
 442
 443        /*
 444         * with vblank_disable_allowed = 1, vblank interrupt will be disabled
 445         * by drm timer once a current process gives up ownership of
 446         * vblank event.(after drm_vblank_put function is called)
 447         */
 448        imxdrm->drm->vblank_disable_allowed = 1;
 449
 450        ret = 0;
 451
 452err_init:
 453        mutex_unlock(&imxdrm->mutex);
 454
 455        return ret;
 456}
 457
 458static void imx_drm_update_possible_crtcs(void)
 459{
 460        struct imx_drm_device *imxdrm = __imx_drm_device();
 461        struct imx_drm_crtc *imx_drm_crtc;
 462        struct imx_drm_encoder *enc;
 463        struct crtc_cookie *cookie;
 464
 465        list_for_each_entry(enc, &imxdrm->encoder_list, list) {
 466                u32 possible_crtcs = 0;
 467
 468                list_for_each_entry(cookie, &enc->possible_crtcs, list) {
 469                        list_for_each_entry(imx_drm_crtc, &imxdrm->crtc_list, list) {
 470                                if (imx_drm_crtc->cookie.cookie == cookie->cookie &&
 471                                                imx_drm_crtc->cookie.id == cookie->id) {
 472                                        possible_crtcs |= 1 << imx_drm_crtc->pipe;
 473                                }
 474                        }
 475                }
 476                enc->encoder->possible_crtcs = possible_crtcs;
 477                enc->encoder->possible_clones = possible_crtcs;
 478        }
 479}
 480
 481/*
 482 * imx_drm_add_crtc - add a new crtc
 483 *
 484 * The return value if !NULL is a cookie for the caller to pass to
 485 * imx_drm_remove_crtc later.
 486 */
 487int imx_drm_add_crtc(struct drm_crtc *crtc,
 488                struct imx_drm_crtc **new_crtc,
 489                const struct imx_drm_crtc_helper_funcs *imx_drm_helper_funcs,
 490                struct module *owner, void *cookie, int id)
 491{
 492        struct imx_drm_device *imxdrm = __imx_drm_device();
 493        struct imx_drm_crtc *imx_drm_crtc;
 494        int ret;
 495
 496        mutex_lock(&imxdrm->mutex);
 497
 498        if (imxdrm->references) {
 499                ret = -EBUSY;
 500                goto err_busy;
 501        }
 502
 503        imx_drm_crtc = kzalloc(sizeof(*imx_drm_crtc), GFP_KERNEL);
 504        if (!imx_drm_crtc) {
 505                ret = -ENOMEM;
 506                goto err_alloc;
 507        }
 508
 509        imx_drm_crtc->imx_drm_helper_funcs = *imx_drm_helper_funcs;
 510        imx_drm_crtc->pipe = imxdrm->pipes++;
 511        imx_drm_crtc->cookie.cookie = cookie;
 512        imx_drm_crtc->cookie.id = id;
 513
 514        imx_drm_crtc->crtc = crtc;
 515        imx_drm_crtc->imxdrm = imxdrm;
 516
 517        imx_drm_crtc->owner = owner;
 518
 519        list_add_tail(&imx_drm_crtc->list, &imxdrm->crtc_list);
 520
 521        *new_crtc = imx_drm_crtc;
 522
 523        ret = imx_drm_crtc_register(imx_drm_crtc);
 524        if (ret)
 525                goto err_register;
 526
 527        imx_drm_update_possible_crtcs();
 528
 529        mutex_unlock(&imxdrm->mutex);
 530
 531        return 0;
 532
 533err_register:
 534        kfree(imx_drm_crtc);
 535err_alloc:
 536err_busy:
 537        mutex_unlock(&imxdrm->mutex);
 538        return ret;
 539}
 540EXPORT_SYMBOL_GPL(imx_drm_add_crtc);
 541
 542/*
 543 * imx_drm_remove_crtc - remove a crtc
 544 */
 545int imx_drm_remove_crtc(struct imx_drm_crtc *imx_drm_crtc)
 546{
 547        struct imx_drm_device *imxdrm = imx_drm_crtc->imxdrm;
 548
 549        mutex_lock(&imxdrm->mutex);
 550
 551        drm_crtc_cleanup(imx_drm_crtc->crtc);
 552
 553        list_del(&imx_drm_crtc->list);
 554
 555        drm_mode_group_reinit(imxdrm->drm);
 556
 557        mutex_unlock(&imxdrm->mutex);
 558
 559        kfree(imx_drm_crtc);
 560
 561        return 0;
 562}
 563EXPORT_SYMBOL_GPL(imx_drm_remove_crtc);
 564
 565/*
 566 * imx_drm_add_encoder - add a new encoder
 567 */
 568int imx_drm_add_encoder(struct drm_encoder *encoder,
 569                struct imx_drm_encoder **newenc, struct module *owner)
 570{
 571        struct imx_drm_device *imxdrm = __imx_drm_device();
 572        struct imx_drm_encoder *imx_drm_encoder;
 573        int ret;
 574
 575        mutex_lock(&imxdrm->mutex);
 576
 577        if (imxdrm->references) {
 578                ret = -EBUSY;
 579                goto err_busy;
 580        }
 581
 582        imx_drm_encoder = kzalloc(sizeof(*imx_drm_encoder), GFP_KERNEL);
 583        if (!imx_drm_encoder) {
 584                ret = -ENOMEM;
 585                goto err_alloc;
 586        }
 587
 588        imx_drm_encoder->encoder = encoder;
 589        imx_drm_encoder->owner = owner;
 590
 591        ret = imx_drm_encoder_register(imx_drm_encoder);
 592        if (ret) {
 593                ret = -ENOMEM;
 594                goto err_register;
 595        }
 596
 597        list_add_tail(&imx_drm_encoder->list, &imxdrm->encoder_list);
 598
 599        *newenc = imx_drm_encoder;
 600
 601        mutex_unlock(&imxdrm->mutex);
 602
 603        return 0;
 604
 605err_register:
 606        kfree(imx_drm_encoder);
 607err_alloc:
 608err_busy:
 609        mutex_unlock(&imxdrm->mutex);
 610
 611        return ret;
 612}
 613EXPORT_SYMBOL_GPL(imx_drm_add_encoder);
 614
 615int imx_drm_encoder_add_possible_crtcs(
 616                struct imx_drm_encoder *imx_drm_encoder,
 617                struct device_node *np)
 618{
 619        struct imx_drm_device *imxdrm = __imx_drm_device();
 620        struct of_phandle_args args;
 621        struct crtc_cookie *c;
 622        int ret = 0;
 623        int i;
 624
 625        if (!list_empty(&imx_drm_encoder->possible_crtcs))
 626                return -EBUSY;
 627
 628        for (i = 0; !ret; i++) {
 629                ret = of_parse_phandle_with_args(np, "crtcs",
 630                                "#crtc-cells", i, &args);
 631                if (ret < 0)
 632                        break;
 633
 634                c = kzalloc(sizeof(*c), GFP_KERNEL);
 635                if (!c) {
 636                        of_node_put(args.np);
 637                        return -ENOMEM;
 638                }
 639
 640                c->cookie = args.np;
 641                c->id = args.args_count > 0 ? args.args[0] : 0;
 642
 643                of_node_put(args.np);
 644
 645                mutex_lock(&imxdrm->mutex);
 646
 647                list_add_tail(&c->list, &imx_drm_encoder->possible_crtcs);
 648
 649                mutex_unlock(&imxdrm->mutex);
 650        }
 651
 652        imx_drm_update_possible_crtcs();
 653
 654        return 0;
 655}
 656EXPORT_SYMBOL_GPL(imx_drm_encoder_add_possible_crtcs);
 657
 658int imx_drm_encoder_get_mux_id(struct imx_drm_encoder *imx_drm_encoder,
 659                struct drm_crtc *crtc)
 660{
 661        struct imx_drm_device *imxdrm = __imx_drm_device();
 662        struct imx_drm_crtc *imx_crtc;
 663        int i = 0;
 664
 665        mutex_lock(&imxdrm->mutex);
 666
 667        list_for_each_entry(imx_crtc, &imxdrm->crtc_list, list) {
 668                if (imx_crtc->crtc == crtc)
 669                        goto found;
 670                i++;
 671        }
 672
 673        mutex_unlock(&imxdrm->mutex);
 674
 675        return -EINVAL;
 676found:
 677        mutex_unlock(&imxdrm->mutex);
 678
 679        return i;
 680}
 681
 682/*
 683 * imx_drm_remove_encoder - remove an encoder
 684 */
 685int imx_drm_remove_encoder(struct imx_drm_encoder *imx_drm_encoder)
 686{
 687        struct imx_drm_device *imxdrm = __imx_drm_device();
 688        struct crtc_cookie *c, *tmp;
 689
 690        mutex_lock(&imxdrm->mutex);
 691
 692        imx_drm_encoder_unregister(imx_drm_encoder);
 693
 694        list_del(&imx_drm_encoder->list);
 695
 696        list_for_each_entry_safe(c, tmp, &imx_drm_encoder->possible_crtcs,
 697                        list)
 698                kfree(c);
 699
 700        mutex_unlock(&imxdrm->mutex);
 701
 702        kfree(imx_drm_encoder);
 703
 704        return 0;
 705}
 706EXPORT_SYMBOL_GPL(imx_drm_remove_encoder);
 707
 708/*
 709 * imx_drm_add_connector - add a connector
 710 */
 711int imx_drm_add_connector(struct drm_connector *connector,
 712                struct imx_drm_connector **new_con,
 713                struct module *owner)
 714{
 715        struct imx_drm_device *imxdrm = __imx_drm_device();
 716        struct imx_drm_connector *imx_drm_connector;
 717        int ret;
 718
 719        mutex_lock(&imxdrm->mutex);
 720
 721        if (imxdrm->references) {
 722                ret = -EBUSY;
 723                goto err_busy;
 724        }
 725
 726        imx_drm_connector = kzalloc(sizeof(*imx_drm_connector), GFP_KERNEL);
 727        if (!imx_drm_connector) {
 728                ret = -ENOMEM;
 729                goto err_alloc;
 730        }
 731
 732        imx_drm_connector->connector = connector;
 733        imx_drm_connector->owner = owner;
 734
 735        ret = imx_drm_connector_register(imx_drm_connector);
 736        if (ret)
 737                goto err_register;
 738
 739        list_add_tail(&imx_drm_connector->list, &imxdrm->connector_list);
 740
 741        *new_con = imx_drm_connector;
 742
 743        mutex_unlock(&imxdrm->mutex);
 744
 745        return 0;
 746
 747err_register:
 748        kfree(imx_drm_connector);
 749err_alloc:
 750err_busy:
 751        mutex_unlock(&imxdrm->mutex);
 752
 753        return ret;
 754}
 755EXPORT_SYMBOL_GPL(imx_drm_add_connector);
 756
 757void imx_drm_fb_helper_set(struct drm_fbdev_cma *fbdev_helper)
 758{
 759        struct imx_drm_device *imxdrm = __imx_drm_device();
 760
 761        imxdrm->fbhelper = fbdev_helper;
 762}
 763EXPORT_SYMBOL_GPL(imx_drm_fb_helper_set);
 764
 765/*
 766 * imx_drm_remove_connector - remove a connector
 767 */
 768int imx_drm_remove_connector(struct imx_drm_connector *imx_drm_connector)
 769{
 770        struct imx_drm_device *imxdrm = __imx_drm_device();
 771
 772        mutex_lock(&imxdrm->mutex);
 773
 774        imx_drm_connector_unregister(imx_drm_connector);
 775
 776        list_del(&imx_drm_connector->list);
 777
 778        mutex_unlock(&imxdrm->mutex);
 779
 780        kfree(imx_drm_connector);
 781
 782        return 0;
 783}
 784EXPORT_SYMBOL_GPL(imx_drm_remove_connector);
 785
 786static struct drm_ioctl_desc imx_drm_ioctls[] = {
 787        /* none so far */
 788};
 789
 790static struct drm_driver imx_drm_driver = {
 791        .driver_features        = DRIVER_MODESET | DRIVER_GEM,
 792        .load                   = imx_drm_driver_load,
 793        .unload                 = imx_drm_driver_unload,
 794        .firstopen              = imx_drm_driver_firstopen,
 795        .lastclose              = imx_drm_driver_lastclose,
 796        .gem_free_object        = drm_gem_cma_free_object,
 797        .gem_vm_ops             = &drm_gem_cma_vm_ops,
 798        .dumb_create            = drm_gem_cma_dumb_create,
 799        .dumb_map_offset        = drm_gem_cma_dumb_map_offset,
 800        .dumb_destroy           = drm_gem_cma_dumb_destroy,
 801
 802        .get_vblank_counter     = drm_vblank_count,
 803        .enable_vblank          = imx_drm_enable_vblank,
 804        .disable_vblank         = imx_drm_disable_vblank,
 805        .ioctls                 = imx_drm_ioctls,
 806        .num_ioctls             = ARRAY_SIZE(imx_drm_ioctls),
 807        .fops                   = &imx_drm_driver_fops,
 808        .name                   = "imx-drm",
 809        .desc                   = "i.MX DRM graphics",
 810        .date                   = "20120507",
 811        .major                  = 1,
 812        .minor                  = 0,
 813        .patchlevel             = 0,
 814};
 815
 816static int imx_drm_platform_probe(struct platform_device *pdev)
 817{
 818        imx_drm_device->dev = &pdev->dev;
 819
 820        return drm_platform_init(&imx_drm_driver, pdev);
 821}
 822
 823static int imx_drm_platform_remove(struct platform_device *pdev)
 824{
 825        drm_platform_exit(&imx_drm_driver, pdev);
 826
 827        return 0;
 828}
 829
 830static struct platform_driver imx_drm_pdrv = {
 831        .probe          = imx_drm_platform_probe,
 832        .remove         = imx_drm_platform_remove,
 833        .driver         = {
 834                .owner  = THIS_MODULE,
 835                .name   = "imx-drm",
 836        },
 837};
 838
 839static struct platform_device *imx_drm_pdev;
 840
 841static int __init imx_drm_init(void)
 842{
 843        int ret;
 844
 845        imx_drm_device = kzalloc(sizeof(*imx_drm_device), GFP_KERNEL);
 846        if (!imx_drm_device)
 847                return -ENOMEM;
 848
 849        mutex_init(&imx_drm_device->mutex);
 850        INIT_LIST_HEAD(&imx_drm_device->crtc_list);
 851        INIT_LIST_HEAD(&imx_drm_device->connector_list);
 852        INIT_LIST_HEAD(&imx_drm_device->encoder_list);
 853
 854        imx_drm_pdev = platform_device_register_simple("imx-drm", -1, NULL, 0);
 855        if (!imx_drm_pdev) {
 856                ret = -EINVAL;
 857                goto err_pdev;
 858        }
 859
 860        imx_drm_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32),
 861
 862        ret = platform_driver_register(&imx_drm_pdrv);
 863        if (ret)
 864                goto err_pdrv;
 865
 866        return 0;
 867
 868err_pdrv:
 869        platform_device_unregister(imx_drm_pdev);
 870err_pdev:
 871        kfree(imx_drm_device);
 872
 873        return ret;
 874}
 875
 876static void __exit imx_drm_exit(void)
 877{
 878        platform_device_unregister(imx_drm_pdev);
 879        platform_driver_unregister(&imx_drm_pdrv);
 880
 881        kfree(imx_drm_device);
 882}
 883
 884module_init(imx_drm_init);
 885module_exit(imx_drm_exit);
 886
 887MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
 888MODULE_DESCRIPTION("i.MX drm driver core");
 889MODULE_LICENSE("GPL");
 890