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