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