linux/drivers/staging/olpc_dcon/olpc_dcon.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Mainly by David Woodhouse, somewhat modified by Jordan Crouse
   4 *
   5 * Copyright © 2006-2007  Red Hat, Inc.
   6 * Copyright © 2006-2007  Advanced Micro Devices, Inc.
   7 * Copyright © 2009       VIA Technology, Inc.
   8 * Copyright (c) 2010-2011  Andres Salomon <dilinger@queued.net>
   9 */
  10
  11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12
  13#include <linux/kernel.h>
  14#include <linux/fb.h>
  15#include <linux/console.h>
  16#include <linux/i2c.h>
  17#include <linux/platform_device.h>
  18#include <linux/interrupt.h>
  19#include <linux/delay.h>
  20#include <linux/module.h>
  21#include <linux/backlight.h>
  22#include <linux/device.h>
  23#include <linux/uaccess.h>
  24#include <linux/ctype.h>
  25#include <linux/reboot.h>
  26#include <linux/olpc-ec.h>
  27#include <asm/tsc.h>
  28#include <asm/olpc.h>
  29
  30#include "olpc_dcon.h"
  31
  32/* Module definitions */
  33
  34static ushort resumeline = 898;
  35module_param(resumeline, ushort, 0444);
  36
  37static struct dcon_platform_data *pdata;
  38
  39/* I2C structures */
  40
  41/* Platform devices */
  42static struct platform_device *dcon_device;
  43
  44static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
  45
  46static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
  47{
  48        return i2c_smbus_write_word_data(dcon->client, reg, val);
  49}
  50
  51static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
  52{
  53        return i2c_smbus_read_word_data(dcon->client, reg);
  54}
  55
  56/* ===== API functions - these are called by a variety of users ==== */
  57
  58static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
  59{
  60        u16 ver;
  61        int rc = 0;
  62
  63        ver = dcon_read(dcon, DCON_REG_ID);
  64        if ((ver >> 8) != 0xDC) {
  65                pr_err("DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
  66                rc = -ENXIO;
  67                goto err;
  68        }
  69
  70        if (is_init) {
  71                pr_info("Discovered DCON version %x\n", ver & 0xFF);
  72                rc = pdata->init(dcon);
  73                if (rc != 0) {
  74                        pr_err("Unable to init.\n");
  75                        goto err;
  76                }
  77        }
  78
  79        if (ver < 0xdc02) {
  80                dev_err(&dcon->client->dev,
  81                        "DCON v1 is unsupported, giving up..\n");
  82                rc = -ENODEV;
  83                goto err;
  84        }
  85
  86        /* SDRAM setup/hold time */
  87        dcon_write(dcon, 0x3a, 0xc040);
  88        dcon_write(dcon, DCON_REG_MEM_OPT_A, 0x0000);  /* clear option bits */
  89        dcon_write(dcon, DCON_REG_MEM_OPT_A,
  90                   MEM_DLL_CLOCK_DELAY | MEM_POWER_DOWN);
  91        dcon_write(dcon, DCON_REG_MEM_OPT_B, MEM_SOFT_RESET);
  92
  93        /* Colour swizzle, AA, no passthrough, backlight */
  94        if (is_init) {
  95                dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
  96                                MODE_CSWIZZLE | MODE_COL_AA;
  97        }
  98        dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
  99
 100        /* Set the scanline to interrupt on during resume */
 101        dcon_write(dcon, DCON_REG_SCAN_INT, resumeline);
 102
 103err:
 104        return rc;
 105}
 106
 107/*
 108 * The smbus doesn't always come back due to what is believed to be
 109 * hardware (power rail) bugs.  For older models where this is known to
 110 * occur, our solution is to attempt to wait for the bus to stabilize;
 111 * if it doesn't happen, cut power to the dcon, repower it, and wait
 112 * for the bus to stabilize.  Rinse, repeat until we have a working
 113 * smbus.  For newer models, we simply BUG(); we want to know if this
 114 * still happens despite the power fixes that have been made!
 115 */
 116static int dcon_bus_stabilize(struct dcon_priv *dcon, int is_powered_down)
 117{
 118        unsigned long timeout;
 119        u8 pm;
 120        int x;
 121
 122power_up:
 123        if (is_powered_down) {
 124                pm = 1;
 125                x = olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
 126                if (x) {
 127                        pr_warn("unable to force dcon to power up: %d!\n", x);
 128                        return x;
 129                }
 130                usleep_range(10000, 11000);  /* we'll be conservative */
 131        }
 132
 133        pdata->bus_stabilize_wiggle();
 134
 135        for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
 136                usleep_range(1000, 1100);
 137                x = dcon_read(dcon, DCON_REG_ID);
 138        }
 139        if (x < 0) {
 140                pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
 141                BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
 142                pm = 0;
 143                olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
 144                msleep(100);
 145                is_powered_down = 1;
 146                goto power_up;  /* argh, stupid hardware.. */
 147        }
 148
 149        if (is_powered_down)
 150                return dcon_hw_init(dcon, 0);
 151        return 0;
 152}
 153
 154static void dcon_set_backlight(struct dcon_priv *dcon, u8 level)
 155{
 156        dcon->bl_val = level;
 157        dcon_write(dcon, DCON_REG_BRIGHT, dcon->bl_val);
 158
 159        /* Purposely turn off the backlight when we go to level 0 */
 160        if (dcon->bl_val == 0) {
 161                dcon->disp_mode &= ~MODE_BL_ENABLE;
 162                dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
 163        } else if (!(dcon->disp_mode & MODE_BL_ENABLE)) {
 164                dcon->disp_mode |= MODE_BL_ENABLE;
 165                dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
 166        }
 167}
 168
 169/* Set the output type to either color or mono */
 170static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
 171{
 172        if (dcon->mono == enable_mono)
 173                return 0;
 174
 175        dcon->mono = enable_mono;
 176
 177        if (enable_mono) {
 178                dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
 179                dcon->disp_mode |= MODE_MONO_LUMA;
 180        } else {
 181                dcon->disp_mode &= ~(MODE_MONO_LUMA);
 182                dcon->disp_mode |= MODE_CSWIZZLE | MODE_COL_AA;
 183        }
 184
 185        dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
 186        return 0;
 187}
 188
 189/* For now, this will be really stupid - we need to address how
 190 * DCONLOAD works in a sleep and account for it accordingly
 191 */
 192
 193static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
 194{
 195        int x;
 196
 197        /* Turn off the backlight and put the DCON to sleep */
 198
 199        if (dcon->asleep == sleep)
 200                return;
 201
 202        if (!olpc_board_at_least(olpc_board(0xc2)))
 203                return;
 204
 205        if (sleep) {
 206                u8 pm = 0;
 207
 208                x = olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
 209                if (x)
 210                        pr_warn("unable to force dcon to power down: %d!\n", x);
 211                else
 212                        dcon->asleep = sleep;
 213        } else {
 214                /* Only re-enable the backlight if the backlight value is set */
 215                if (dcon->bl_val != 0)
 216                        dcon->disp_mode |= MODE_BL_ENABLE;
 217                x = dcon_bus_stabilize(dcon, 1);
 218                if (x)
 219                        pr_warn("unable to reinit dcon hardware: %d!\n", x);
 220                else
 221                        dcon->asleep = sleep;
 222
 223                /* Restore backlight */
 224                dcon_set_backlight(dcon, dcon->bl_val);
 225        }
 226
 227        /* We should turn off some stuff in the framebuffer - but what? */
 228}
 229
 230/* the DCON seems to get confused if we change DCONLOAD too
 231 * frequently -- i.e., approximately faster than frame time.
 232 * normally we don't change it this fast, so in general we won't
 233 * delay here.
 234 */
 235static void dcon_load_holdoff(struct dcon_priv *dcon)
 236{
 237        ktime_t delta_t, now;
 238
 239        while (1) {
 240                now = ktime_get();
 241                delta_t = ktime_sub(now, dcon->load_time);
 242                if (ktime_to_ns(delta_t) > NSEC_PER_MSEC * 20)
 243                        break;
 244                mdelay(4);
 245        }
 246}
 247
 248static bool dcon_blank_fb(struct dcon_priv *dcon, bool blank)
 249{
 250        int err;
 251
 252        console_lock();
 253        if (!lock_fb_info(dcon->fbinfo)) {
 254                console_unlock();
 255                dev_err(&dcon->client->dev, "unable to lock framebuffer\n");
 256                return false;
 257        }
 258
 259        dcon->ignore_fb_events = true;
 260        err = fb_blank(dcon->fbinfo,
 261                       blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
 262        dcon->ignore_fb_events = false;
 263        unlock_fb_info(dcon->fbinfo);
 264        console_unlock();
 265
 266        if (err) {
 267                dev_err(&dcon->client->dev, "couldn't %sblank framebuffer\n",
 268                        blank ? "" : "un");
 269                return false;
 270        }
 271        return true;
 272}
 273
 274/* Set the source of the display (CPU or DCON) */
 275static void dcon_source_switch(struct work_struct *work)
 276{
 277        struct dcon_priv *dcon = container_of(work, struct dcon_priv,
 278                        switch_source);
 279        int source = dcon->pending_src;
 280
 281        if (dcon->curr_src == source)
 282                return;
 283
 284        dcon_load_holdoff(dcon);
 285
 286        dcon->switched = false;
 287
 288        switch (source) {
 289        case DCON_SOURCE_CPU:
 290                pr_info("%s to CPU\n", __func__);
 291                /* Enable the scanline interrupt bit */
 292                if (dcon_write(dcon, DCON_REG_MODE,
 293                               dcon->disp_mode | MODE_SCAN_INT))
 294                        pr_err("couldn't enable scanline interrupt!\n");
 295                else
 296                        /* Wait up to one second for the scanline interrupt */
 297                        wait_event_timeout(dcon->waitq, dcon->switched, HZ);
 298
 299                if (!dcon->switched)
 300                        pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
 301
 302                /* Turn off the scanline interrupt */
 303                if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
 304                        pr_err("couldn't disable scanline interrupt!\n");
 305
 306                /*
 307                 * Ideally we'd like to disable interrupts here so that the
 308                 * fb unblanking and DCON turn on happen at a known time value;
 309                 * however, we can't do that right now with fb_blank
 310                 * messing with semaphores.
 311                 *
 312                 * For now, we just hope..
 313                 */
 314                if (!dcon_blank_fb(dcon, false)) {
 315                        pr_err("Failed to enter CPU mode\n");
 316                        dcon->pending_src = DCON_SOURCE_DCON;
 317                        return;
 318                }
 319
 320                /* And turn off the DCON */
 321                pdata->set_dconload(1);
 322                dcon->load_time = ktime_get();
 323
 324                pr_info("The CPU has control\n");
 325                break;
 326        case DCON_SOURCE_DCON:
 327        {
 328                ktime_t delta_t;
 329
 330                pr_info("%s to DCON\n", __func__);
 331
 332                /* Clear DCONLOAD - this implies that the DCON is in control */
 333                pdata->set_dconload(0);
 334                dcon->load_time = ktime_get();
 335
 336                wait_event_timeout(dcon->waitq, dcon->switched, HZ / 2);
 337
 338                if (!dcon->switched) {
 339                        pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
 340                } else {
 341                        /* sometimes the DCON doesn't follow its own rules,
 342                         * and doesn't wait for two vsync pulses before
 343                         * ack'ing the frame load with an IRQ.  the result
 344                         * is that the display shows the *previously*
 345                         * loaded frame.  we can detect this by looking at
 346                         * the time between asserting DCONLOAD and the IRQ --
 347                         * if it's less than 20msec, then the DCON couldn't
 348                         * have seen two VSYNC pulses.  in that case we
 349                         * deassert and reassert, and hope for the best.
 350                         * see http://dev.laptop.org/ticket/9664
 351                         */
 352                        delta_t = ktime_sub(dcon->irq_time, dcon->load_time);
 353                        if (dcon->switched && ktime_to_ns(delta_t)
 354                            < NSEC_PER_MSEC * 20) {
 355                                pr_err("missed loading, retrying\n");
 356                                pdata->set_dconload(1);
 357                                mdelay(41);
 358                                pdata->set_dconload(0);
 359                                dcon->load_time = ktime_get();
 360                                mdelay(41);
 361                        }
 362                }
 363
 364                dcon_blank_fb(dcon, true);
 365                pr_info("The DCON has control\n");
 366                break;
 367        }
 368        default:
 369                BUG();
 370        }
 371
 372        dcon->curr_src = source;
 373}
 374
 375static void dcon_set_source(struct dcon_priv *dcon, int arg)
 376{
 377        if (dcon->pending_src == arg)
 378                return;
 379
 380        dcon->pending_src = arg;
 381
 382        if (dcon->curr_src != arg)
 383                schedule_work(&dcon->switch_source);
 384}
 385
 386static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
 387{
 388        dcon_set_source(dcon, arg);
 389        flush_scheduled_work();
 390}
 391
 392static ssize_t dcon_mode_show(struct device *dev,
 393                              struct device_attribute *attr,
 394                              char *buf)
 395{
 396        struct dcon_priv *dcon = dev_get_drvdata(dev);
 397
 398        return sprintf(buf, "%4.4X\n", dcon->disp_mode);
 399}
 400
 401static ssize_t dcon_sleep_show(struct device *dev,
 402                               struct device_attribute *attr,
 403                               char *buf)
 404{
 405        struct dcon_priv *dcon = dev_get_drvdata(dev);
 406
 407        return sprintf(buf, "%d\n", dcon->asleep);
 408}
 409
 410static ssize_t dcon_freeze_show(struct device *dev,
 411                                struct device_attribute *attr,
 412                                char *buf)
 413{
 414        struct dcon_priv *dcon = dev_get_drvdata(dev);
 415
 416        return sprintf(buf, "%d\n", dcon->curr_src == DCON_SOURCE_DCON ? 1 : 0);
 417}
 418
 419static ssize_t dcon_mono_show(struct device *dev,
 420                              struct device_attribute *attr,
 421                              char *buf)
 422{
 423        struct dcon_priv *dcon = dev_get_drvdata(dev);
 424
 425        return sprintf(buf, "%d\n", dcon->mono);
 426}
 427
 428static ssize_t dcon_resumeline_show(struct device *dev,
 429                                    struct device_attribute *attr,
 430                                    char *buf)
 431{
 432        return sprintf(buf, "%d\n", resumeline);
 433}
 434
 435static ssize_t dcon_mono_store(struct device *dev,
 436                               struct device_attribute *attr,
 437                               const char *buf, size_t count)
 438{
 439        unsigned long enable_mono;
 440        int rc;
 441
 442        rc = kstrtoul(buf, 10, &enable_mono);
 443        if (rc)
 444                return rc;
 445
 446        dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
 447
 448        return count;
 449}
 450
 451static ssize_t dcon_freeze_store(struct device *dev,
 452                                 struct device_attribute *attr,
 453                                 const char *buf, size_t count)
 454{
 455        struct dcon_priv *dcon = dev_get_drvdata(dev);
 456        unsigned long output;
 457        int ret;
 458
 459        ret = kstrtoul(buf, 10, &output);
 460        if (ret)
 461                return ret;
 462
 463        switch (output) {
 464        case 0:
 465                dcon_set_source(dcon, DCON_SOURCE_CPU);
 466                break;
 467        case 1:
 468                dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
 469                break;
 470        case 2:  /* normally unused */
 471                dcon_set_source(dcon, DCON_SOURCE_DCON);
 472                break;
 473        default:
 474                return -EINVAL;
 475        }
 476
 477        return count;
 478}
 479
 480static ssize_t dcon_resumeline_store(struct device *dev,
 481                                     struct device_attribute *attr,
 482                                     const char *buf, size_t count)
 483{
 484        unsigned short rl;
 485        int rc;
 486
 487        rc = kstrtou16(buf, 10, &rl);
 488        if (rc)
 489                return rc;
 490
 491        resumeline = rl;
 492        dcon_write(dev_get_drvdata(dev), DCON_REG_SCAN_INT, resumeline);
 493
 494        return count;
 495}
 496
 497static ssize_t dcon_sleep_store(struct device *dev,
 498                                struct device_attribute *attr,
 499                                const char *buf, size_t count)
 500{
 501        unsigned long output;
 502        int ret;
 503
 504        ret = kstrtoul(buf, 10, &output);
 505        if (ret)
 506                return ret;
 507
 508        dcon_sleep(dev_get_drvdata(dev), output ? true : false);
 509        return count;
 510}
 511
 512static struct device_attribute dcon_device_files[] = {
 513        __ATTR(mode, 0444, dcon_mode_show, NULL),
 514        __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
 515        __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
 516        __ATTR(monochrome, 0644, dcon_mono_show, dcon_mono_store),
 517        __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
 518};
 519
 520static int dcon_bl_update(struct backlight_device *dev)
 521{
 522        struct dcon_priv *dcon = bl_get_data(dev);
 523        u8 level = dev->props.brightness & 0x0F;
 524
 525        if (dev->props.power != FB_BLANK_UNBLANK)
 526                level = 0;
 527
 528        if (level != dcon->bl_val)
 529                dcon_set_backlight(dcon, level);
 530
 531        /* power down the DCON when the screen is blanked */
 532        if (!dcon->ignore_fb_events)
 533                dcon_sleep(dcon, !!(dev->props.state & BL_CORE_FBBLANK));
 534
 535        return 0;
 536}
 537
 538static int dcon_bl_get(struct backlight_device *dev)
 539{
 540        struct dcon_priv *dcon = bl_get_data(dev);
 541
 542        return dcon->bl_val;
 543}
 544
 545static const struct backlight_ops dcon_bl_ops = {
 546        .update_status = dcon_bl_update,
 547        .get_brightness = dcon_bl_get,
 548};
 549
 550static struct backlight_properties dcon_bl_props = {
 551        .max_brightness = 15,
 552        .type = BACKLIGHT_RAW,
 553        .power = FB_BLANK_UNBLANK,
 554};
 555
 556static int dcon_reboot_notify(struct notifier_block *nb,
 557                              unsigned long foo, void *bar)
 558{
 559        struct dcon_priv *dcon = container_of(nb, struct dcon_priv, reboot_nb);
 560
 561        if (!dcon || !dcon->client)
 562                return NOTIFY_DONE;
 563
 564        /* Turn off the DCON. Entirely. */
 565        dcon_write(dcon, DCON_REG_MODE, 0x39);
 566        dcon_write(dcon, DCON_REG_MODE, 0x32);
 567        return NOTIFY_DONE;
 568}
 569
 570static int unfreeze_on_panic(struct notifier_block *nb,
 571                             unsigned long e, void *p)
 572{
 573        pdata->set_dconload(1);
 574        return NOTIFY_DONE;
 575}
 576
 577static struct notifier_block dcon_panic_nb = {
 578        .notifier_call = unfreeze_on_panic,
 579};
 580
 581static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
 582{
 583        strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
 584
 585        return 0;
 586}
 587
 588static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
 589{
 590        struct dcon_priv *dcon;
 591        int rc, i, j;
 592
 593        if (!pdata)
 594                return -ENXIO;
 595
 596        dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
 597        if (!dcon)
 598                return -ENOMEM;
 599
 600        dcon->client = client;
 601        init_waitqueue_head(&dcon->waitq);
 602        INIT_WORK(&dcon->switch_source, dcon_source_switch);
 603        dcon->reboot_nb.notifier_call = dcon_reboot_notify;
 604        dcon->reboot_nb.priority = -1;
 605
 606        i2c_set_clientdata(client, dcon);
 607
 608        if (num_registered_fb < 1) {
 609                dev_err(&client->dev, "DCON driver requires a registered fb\n");
 610                rc = -EIO;
 611                goto einit;
 612        }
 613        dcon->fbinfo = registered_fb[0];
 614
 615        rc = dcon_hw_init(dcon, 1);
 616        if (rc)
 617                goto einit;
 618
 619        /* Add the DCON device */
 620
 621        dcon_device = platform_device_alloc("dcon", -1);
 622
 623        if (!dcon_device) {
 624                pr_err("Unable to create the DCON device\n");
 625                rc = -ENOMEM;
 626                goto eirq;
 627        }
 628        rc = platform_device_add(dcon_device);
 629        platform_set_drvdata(dcon_device, dcon);
 630
 631        if (rc) {
 632                pr_err("Unable to add the DCON device\n");
 633                goto edev;
 634        }
 635
 636        for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
 637                rc = device_create_file(&dcon_device->dev,
 638                                        &dcon_device_files[i]);
 639                if (rc) {
 640                        dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
 641                        goto ecreate;
 642                }
 643        }
 644
 645        dcon->bl_val = dcon_read(dcon, DCON_REG_BRIGHT) & 0x0F;
 646
 647        /* Add the backlight device for the DCON */
 648        dcon_bl_props.brightness = dcon->bl_val;
 649        dcon->bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
 650                                                 dcon, &dcon_bl_ops,
 651                                                 &dcon_bl_props);
 652        if (IS_ERR(dcon->bl_dev)) {
 653                dev_err(&client->dev, "cannot register backlight dev (%ld)\n",
 654                        PTR_ERR(dcon->bl_dev));
 655                dcon->bl_dev = NULL;
 656        }
 657
 658        register_reboot_notifier(&dcon->reboot_nb);
 659        atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
 660
 661        return 0;
 662
 663 ecreate:
 664        for (j = 0; j < i; j++)
 665                device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
 666 edev:
 667        platform_device_unregister(dcon_device);
 668        dcon_device = NULL;
 669 eirq:
 670        free_irq(DCON_IRQ, dcon);
 671 einit:
 672        kfree(dcon);
 673        return rc;
 674}
 675
 676static int dcon_remove(struct i2c_client *client)
 677{
 678        struct dcon_priv *dcon = i2c_get_clientdata(client);
 679
 680        unregister_reboot_notifier(&dcon->reboot_nb);
 681        atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
 682
 683        free_irq(DCON_IRQ, dcon);
 684
 685        backlight_device_unregister(dcon->bl_dev);
 686
 687        if (dcon_device)
 688                platform_device_unregister(dcon_device);
 689        cancel_work_sync(&dcon->switch_source);
 690
 691        kfree(dcon);
 692
 693        return 0;
 694}
 695
 696#ifdef CONFIG_PM
 697static int dcon_suspend(struct device *dev)
 698{
 699        struct i2c_client *client = to_i2c_client(dev);
 700        struct dcon_priv *dcon = i2c_get_clientdata(client);
 701
 702        if (!dcon->asleep) {
 703                /* Set up the DCON to have the source */
 704                dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
 705        }
 706
 707        return 0;
 708}
 709
 710static int dcon_resume(struct device *dev)
 711{
 712        struct i2c_client *client = to_i2c_client(dev);
 713        struct dcon_priv *dcon = i2c_get_clientdata(client);
 714
 715        if (!dcon->asleep) {
 716                dcon_bus_stabilize(dcon, 0);
 717                dcon_set_source(dcon, DCON_SOURCE_CPU);
 718        }
 719
 720        return 0;
 721}
 722
 723#else
 724
 725#define dcon_suspend NULL
 726#define dcon_resume NULL
 727
 728#endif /* CONFIG_PM */
 729
 730irqreturn_t dcon_interrupt(int irq, void *id)
 731{
 732        struct dcon_priv *dcon = id;
 733        u8 status;
 734
 735        if (pdata->read_status(&status))
 736                return IRQ_NONE;
 737
 738        switch (status & 3) {
 739        case 3:
 740                pr_debug("DCONLOAD_MISSED interrupt\n");
 741                break;
 742
 743        case 2: /* switch to DCON mode */
 744        case 1: /* switch to CPU mode */
 745                dcon->switched = true;
 746                dcon->irq_time = ktime_get();
 747                wake_up(&dcon->waitq);
 748                break;
 749
 750        case 0:
 751                /* workaround resume case:  the DCON (on 1.5) doesn't
 752                 * ever assert status 0x01 when switching to CPU mode
 753                 * during resume.  this is because DCONLOAD is de-asserted
 754                 * _immediately_ upon exiting S3, so the actual release
 755                 * of the DCON happened long before this point.
 756                 * see http://dev.laptop.org/ticket/9869
 757                 */
 758                if (dcon->curr_src != dcon->pending_src && !dcon->switched) {
 759                        dcon->switched = true;
 760                        dcon->irq_time = ktime_get();
 761                        wake_up(&dcon->waitq);
 762                        pr_debug("switching w/ status 0/0\n");
 763                } else {
 764                        pr_debug("scanline interrupt w/CPU\n");
 765                }
 766        }
 767
 768        return IRQ_HANDLED;
 769}
 770
 771static const struct dev_pm_ops dcon_pm_ops = {
 772        .suspend = dcon_suspend,
 773        .resume = dcon_resume,
 774};
 775
 776static const struct i2c_device_id dcon_idtable[] = {
 777        { "olpc_dcon",  0 },
 778        { }
 779};
 780MODULE_DEVICE_TABLE(i2c, dcon_idtable);
 781
 782static struct i2c_driver dcon_driver = {
 783        .driver = {
 784                .name   = "olpc_dcon",
 785                .pm = &dcon_pm_ops,
 786        },
 787        .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
 788        .id_table = dcon_idtable,
 789        .probe = dcon_probe,
 790        .remove = dcon_remove,
 791        .detect = dcon_detect,
 792        .address_list = normal_i2c,
 793};
 794
 795static int __init olpc_dcon_init(void)
 796{
 797#ifdef CONFIG_FB_OLPC_DCON_1_5
 798        /* XO-1.5 */
 799        if (olpc_board_at_least(olpc_board(0xd0)))
 800                pdata = &dcon_pdata_xo_1_5;
 801#endif
 802#ifdef CONFIG_FB_OLPC_DCON_1
 803        if (!pdata)
 804                pdata = &dcon_pdata_xo_1;
 805#endif
 806
 807        return i2c_add_driver(&dcon_driver);
 808}
 809
 810static void __exit olpc_dcon_exit(void)
 811{
 812        i2c_del_driver(&dcon_driver);
 813}
 814
 815module_init(olpc_dcon_init);
 816module_exit(olpc_dcon_exit);
 817
 818MODULE_LICENSE("GPL");
 819