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