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