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