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