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