staging: olpc_dcon: move fb event notifier block into dcon_priv struct
[firefly-linux-kernel-4.4.55.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         struct notifier_block fbevent_nb;
62
63         /* Shadow register for the DCON_REG_MODE register */
64         u8 disp_mode;
65
66         /* Current output type; true == mono, false == color */
67         bool mono;
68         bool asleep;
69 };
70
71 /* I2C structures */
72
73 static struct i2c_driver dcon_driver;
74
75 /* Platform devices */
76 static struct platform_device *dcon_device;
77
78 /* Backlight device */
79 static struct backlight_device *dcon_bl_dev;
80
81 static struct fb_info *fbinfo;
82
83 /* set this to 1 while controlling fb blank state from this driver */
84 static int ignore_fb_events = 0;
85
86 /* Current source, initialized at probe time */
87 static int dcon_source;
88
89 /* Desired source */
90 static int dcon_pending;
91
92 /* Variables used during switches */
93 static int dcon_switched;
94 static struct timespec dcon_irq_time;
95 static struct timespec dcon_load_time;
96
97 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
98
99 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
100
101 static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
102 {
103         return i2c_smbus_write_word_data(dcon->client, reg, val);
104 }
105
106 static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
107 {
108         return i2c_smbus_read_word_data(dcon->client, reg);
109 }
110
111 /* The current backlight value - this saves us some smbus traffic */
112 static int bl_val = -1;
113
114 /* ===== API functions - these are called by a variety of users ==== */
115
116 static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
117 {
118         struct i2c_client *client = dcon->client;
119         uint16_t ver;
120         int rc = 0;
121
122         ver = i2c_smbus_read_word_data(client, DCON_REG_ID);
123         if ((ver >> 8) != 0xDC) {
124                 printk(KERN_ERR "olpc-dcon:  DCON ID not 0xDCxx: 0x%04x "
125                                 "instead.\n", ver);
126                 rc = -ENXIO;
127                 goto err;
128         }
129
130         if (is_init) {
131                 printk(KERN_INFO "olpc-dcon:  Discovered DCON version %x\n",
132                                 ver & 0xFF);
133                 rc = pdata->init();
134                 if (rc != 0) {
135                         printk(KERN_ERR "olpc-dcon:  Unable to init.\n");
136                         goto err;
137                 }
138         }
139
140         if (ver < 0xdc02 && !noinit) {
141                 /* Initialize the DCON registers */
142
143                 /* Start with work-arounds for DCON ASIC */
144                 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
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, 0x0b, 0x007a);
148                 i2c_smbus_write_word_data(client, 0x36, 0x025c);
149                 i2c_smbus_write_word_data(client, 0x37, 0x025e);
150
151                 /* Initialise SDRAM */
152
153                 i2c_smbus_write_word_data(client, 0x3b, 0x002b);
154                 i2c_smbus_write_word_data(client, 0x41, 0x0101);
155                 i2c_smbus_write_word_data(client, 0x42, 0x0101);
156         } else if (!noinit) {
157                 /* SDRAM setup/hold time */
158                 i2c_smbus_write_word_data(client, 0x3a, 0xc040);
159                 i2c_smbus_write_word_data(client, 0x41, 0x0000);
160                 i2c_smbus_write_word_data(client, 0x41, 0x0101);
161                 i2c_smbus_write_word_data(client, 0x42, 0x0101);
162         }
163
164         /* Colour swizzle, AA, no passthrough, backlight */
165         if (is_init) {
166                 dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
167                                 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 static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
269 {
270         if (dcon->mono == enable_mono)
271                 return 0;
272
273         dcon->mono = enable_mono;
274
275         if (enable_mono) {
276                 dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
277                 dcon->disp_mode |= MODE_MONO_LUMA;
278         } else {
279                 dcon->disp_mode &= ~(MODE_MONO_LUMA);
280                 dcon->disp_mode |= MODE_CSWIZZLE;
281                 if (useaa)
282                         dcon->disp_mode |= MODE_COL_AA;
283         }
284
285         dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
286         return 0;
287 }
288
289 /* For now, this will be really stupid - we need to address how
290  * DCONLOAD works in a sleep and account for it accordingly
291  */
292
293 static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
294 {
295         int x;
296
297         /* Turn off the backlight and put the DCON to sleep */
298
299         if (dcon->asleep == sleep)
300                 return;
301
302         if (!olpc_board_at_least(olpc_board(0xc2)))
303                 return;
304
305         if (sleep) {
306                 x = 0;
307                 x = olpc_ec_cmd(0x26, (unsigned char *) &x, 1, NULL, 0);
308                 if (x)
309                         printk(KERN_WARNING "olpc-dcon:  unable to force dcon "
310                                         "to power down: %d!\n", x);
311                 else
312                         dcon->asleep = sleep;
313         } else {
314                 /* Only re-enable the backlight if the backlight value is set */
315                 if (bl_val != 0)
316                         dcon->disp_mode |= MODE_BL_ENABLE;
317                 x = dcon_bus_stabilize(dcon, 1);
318                 if (x)
319                         printk(KERN_WARNING "olpc-dcon:  unable to reinit dcon"
320                                         " hardware: %d!\n", x);
321                 else
322                         dcon->asleep = sleep;
323
324                 /* Restore backlight */
325                 dcon_set_backlight_hw(dcon, bl_val);
326         }
327
328         /* We should turn off some stuff in the framebuffer - but what? */
329 }
330
331 /* the DCON seems to get confused if we change DCONLOAD too
332  * frequently -- i.e., approximately faster than frame time.
333  * normally we don't change it this fast, so in general we won't
334  * delay here.
335  */
336 void dcon_load_holdoff(void)
337 {
338         struct timespec delta_t, now;
339         while (1) {
340                 getnstimeofday(&now);
341                 delta_t = timespec_sub(now, dcon_load_time);
342                 if (delta_t.tv_sec != 0 ||
343                         delta_t.tv_nsec > NSEC_PER_MSEC * 20) {
344                         break;
345                 }
346                 mdelay(4);
347         }
348 }
349 /* Set the source of the display (CPU or DCON) */
350
351 static void dcon_source_switch(struct work_struct *work)
352 {
353         struct dcon_priv *dcon = container_of(work, struct dcon_priv,
354                         switch_source);
355         DECLARE_WAITQUEUE(wait, current);
356         int source = dcon_pending;
357
358         if (dcon_source == source)
359                 return;
360
361         dcon_load_holdoff();
362
363         dcon_switched = 0;
364
365         switch (source) {
366         case DCON_SOURCE_CPU:
367                 printk("dcon_source_switch to CPU\n");
368                 /* Enable the scanline interrupt bit */
369                 if (dcon_write(dcon, DCON_REG_MODE,
370                                 dcon->disp_mode | MODE_SCAN_INT))
371                         printk(KERN_ERR
372                                "olpc-dcon:  couldn't enable scanline interrupt!\n");
373                 else {
374                         /* Wait up to one second for the scanline interrupt */
375                         wait_event_timeout(dcon_wait_queue,
376                                            dcon_switched == 1, HZ);
377                 }
378
379                 if (!dcon_switched)
380                         printk(KERN_ERR "olpc-dcon:  Timeout entering CPU mode; expect a screen glitch.\n");
381
382                 /* Turn off the scanline interrupt */
383                 if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
384                         printk(KERN_ERR "olpc-dcon:  couldn't disable scanline interrupt!\n");
385
386                 /*
387                  * Ideally we'd like to disable interrupts here so that the
388                  * fb unblanking and DCON turn on happen at a known time value;
389                  * however, we can't do that right now with fb_blank
390                  * messing with semaphores.
391                  *
392                  * For now, we just hope..
393                  */
394                 console_lock();
395                 ignore_fb_events = 1;
396                 if (fb_blank(fbinfo, FB_BLANK_UNBLANK)) {
397                         ignore_fb_events = 0;
398                         console_unlock();
399                         printk(KERN_ERR "olpc-dcon:  Failed to enter CPU mode\n");
400                         dcon_pending = DCON_SOURCE_DCON;
401                         return;
402                 }
403                 ignore_fb_events = 0;
404                 console_unlock();
405
406                 /* And turn off the DCON */
407                 pdata->set_dconload(1);
408                 getnstimeofday(&dcon_load_time);
409
410                 printk(KERN_INFO "olpc-dcon: The CPU has control\n");
411                 break;
412         case DCON_SOURCE_DCON:
413         {
414                 int t;
415                 struct timespec delta_t;
416
417                 printk(KERN_INFO "dcon_source_switch to DCON\n");
418
419                 add_wait_queue(&dcon_wait_queue, &wait);
420                 set_current_state(TASK_UNINTERRUPTIBLE);
421
422                 /* Clear DCONLOAD - this implies that the DCON is in control */
423                 pdata->set_dconload(0);
424                 getnstimeofday(&dcon_load_time);
425
426                 t = schedule_timeout(HZ/2);
427                 remove_wait_queue(&dcon_wait_queue, &wait);
428                 set_current_state(TASK_RUNNING);
429
430                 if (!dcon_switched) {
431                         printk(KERN_ERR "olpc-dcon: Timeout entering DCON mode; expect a screen glitch.\n");
432                 } else {
433                         /* sometimes the DCON doesn't follow its own rules,
434                          * and doesn't wait for two vsync pulses before
435                          * ack'ing the frame load with an IRQ.  the result
436                          * is that the display shows the *previously*
437                          * loaded frame.  we can detect this by looking at
438                          * the time between asserting DCONLOAD and the IRQ --
439                          * if it's less than 20msec, then the DCON couldn't
440                          * have seen two VSYNC pulses.  in that case we
441                          * deassert and reassert, and hope for the best.
442                          * see http://dev.laptop.org/ticket/9664
443                          */
444                         delta_t = timespec_sub(dcon_irq_time, dcon_load_time);
445                         if (dcon_switched && delta_t.tv_sec == 0 &&
446                                         delta_t.tv_nsec < NSEC_PER_MSEC * 20) {
447                                 printk(KERN_ERR "olpc-dcon: missed loading, retrying\n");
448                                 pdata->set_dconload(1);
449                                 mdelay(41);
450                                 pdata->set_dconload(0);
451                                 getnstimeofday(&dcon_load_time);
452                                 mdelay(41);
453                         }
454                 }
455
456                 console_lock();
457                 ignore_fb_events = 1;
458                 if (fb_blank(fbinfo, FB_BLANK_POWERDOWN))
459                         printk(KERN_ERR "olpc-dcon:  couldn't blank fb!\n");
460                 ignore_fb_events = 0;
461                 console_unlock();
462
463                 printk(KERN_INFO "olpc-dcon: The DCON has control\n");
464                 break;
465         }
466         default:
467                 BUG();
468         }
469
470         dcon_source = source;
471 }
472
473 static void dcon_set_source(struct dcon_priv *dcon, int arg)
474 {
475         if (dcon_pending == arg)
476                 return;
477
478         dcon_pending = arg;
479
480         if ((dcon_source != arg) && !work_pending(&dcon->switch_source))
481                 schedule_work(&dcon->switch_source);
482 }
483
484 static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
485 {
486         dcon_set_source(dcon, arg);
487         flush_scheduled_work();
488 }
489
490 static int dconbl_set(struct backlight_device *dev)
491 {
492         struct dcon_priv *dcon = bl_get_data(dev);
493         int level = dev->props.brightness;
494
495         if (dev->props.power != FB_BLANK_UNBLANK)
496                 level = 0;
497
498         dcon_set_backlight(dcon, level);
499         return 0;
500 }
501
502 static int dconbl_get(struct backlight_device *dev)
503 {
504         struct dcon_priv *dcon = bl_get_data(dev);
505         return dcon_get_backlight(dcon);
506 }
507
508 static ssize_t dcon_mode_show(struct device *dev,
509         struct device_attribute *attr, char *buf)
510 {
511         struct dcon_priv *dcon = dev_get_drvdata(dev);
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         struct dcon_priv *dcon = dev_get_drvdata(dev);
520         return sprintf(buf, "%d\n", dcon->asleep);
521 }
522
523 static ssize_t dcon_freeze_show(struct device *dev,
524         struct device_attribute *attr, char *buf)
525 {
526         return sprintf(buf, "%d\n", dcon_source == DCON_SOURCE_DCON ? 1 : 0);
527 }
528
529 static ssize_t dcon_mono_show(struct device *dev,
530         struct device_attribute *attr, char *buf)
531 {
532         struct dcon_priv *dcon = dev_get_drvdata(dev);
533         return sprintf(buf, "%d\n", dcon->mono);
534 }
535
536 static ssize_t dcon_resumeline_show(struct device *dev,
537         struct device_attribute *attr, char *buf)
538 {
539         return sprintf(buf, "%d\n", resumeline);
540 }
541
542 static int _strtoul(const char *buf, int len, unsigned int *val)
543 {
544
545         char *endp;
546         unsigned int output = simple_strtoul(buf, &endp, 0);
547         int size = endp - buf;
548
549         if (*endp && isspace(*endp))
550                 size++;
551
552         if (size != len)
553                 return -EINVAL;
554
555         *val = output;
556         return 0;
557 }
558
559 static ssize_t dcon_mono_store(struct device *dev,
560         struct device_attribute *attr, const char *buf, size_t count)
561 {
562         int enable_mono;
563         int rc = -EINVAL;
564
565         if (_strtoul(buf, count, &enable_mono))
566                 return -EINVAL;
567
568         dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
569         rc = count;
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 ? true : false);
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(monochrome, 0644, dcon_mono_show, dcon_mono_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 dcon_fb_notifier(struct notifier_block *self,
674                                 unsigned long event, void *data)
675 {
676         struct fb_event *evdata = data;
677         struct dcon_priv *dcon = container_of(self, struct dcon_priv,
678                         fbevent_nb);
679         int *blank = (int *) evdata->data;
680         if (((event != FB_EVENT_BLANK) && (event != FB_EVENT_CONBLANK)) ||
681                         ignore_fb_events)
682                 return 0;
683         dcon_sleep(dcon, *blank ? true : false);
684         return 0;
685 }
686
687 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
688 {
689         strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
690
691         return 0;
692 }
693
694 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
695 {
696         struct dcon_priv *dcon;
697         int rc, i, j;
698
699         dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
700         if (!dcon)
701                 return -ENOMEM;
702
703         dcon->client = client;
704         INIT_WORK(&dcon->switch_source, dcon_source_switch);
705         dcon->reboot_nb.notifier_call = dcon_reboot_notify;
706         dcon->reboot_nb.priority = -1;
707         dcon->fbevent_nb.notifier_call = dcon_fb_notifier;
708
709         i2c_set_clientdata(client, dcon);
710
711         if (num_registered_fb >= 1)
712                 fbinfo = registered_fb[0];
713
714         rc = dcon_hw_init(dcon, 1);
715         if (rc)
716                 goto einit;
717
718         /* Add the DCON device */
719
720         dcon_device = platform_device_alloc("dcon", -1);
721
722         if (dcon_device == NULL) {
723                 printk(KERN_ERR "dcon:  Unable to create the DCON device\n");
724                 rc = -ENOMEM;
725                 goto eirq;
726         }
727         rc = platform_device_add(dcon_device);
728         platform_set_drvdata(dcon_device, dcon);
729
730         if (rc) {
731                 printk(KERN_ERR "dcon:  Unable to add the DCON device\n");
732                 goto edev;
733         }
734
735         for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
736                 rc = device_create_file(&dcon_device->dev,
737                                         &dcon_device_files[i]);
738                 if (rc) {
739                         dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
740                         goto ecreate;
741                 }
742         }
743
744         /* Add the backlight device for the DCON */
745         dcon_bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
746                 dcon, &dcon_bl_ops, NULL);
747
748         if (IS_ERR(dcon_bl_dev)) {
749                 printk(KERN_ERR "Cannot register the backlight device (%ld)\n",
750                        PTR_ERR(dcon_bl_dev));
751                 dcon_bl_dev = NULL;
752         } else {
753                 dcon_bl_dev->props.max_brightness = 15;
754                 dcon_bl_dev->props.power = FB_BLANK_UNBLANK;
755                 dcon_bl_dev->props.brightness = dcon_get_backlight(dcon);
756
757                 backlight_update_status(dcon_bl_dev);
758         }
759
760         register_reboot_notifier(&dcon->reboot_nb);
761         atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
762         fb_register_client(&dcon->fbevent_nb);
763
764         return 0;
765
766  ecreate:
767         for (j = 0; j < i; j++)
768                 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
769  edev:
770         platform_device_unregister(dcon_device);
771         dcon_device = NULL;
772  eirq:
773         free_irq(DCON_IRQ, &dcon_driver);
774  einit:
775         i2c_set_clientdata(client, NULL);
776         kfree(dcon);
777         return rc;
778 }
779
780 static int dcon_remove(struct i2c_client *client)
781 {
782         struct dcon_priv *dcon = i2c_get_clientdata(client);
783
784         i2c_set_clientdata(client, NULL);
785
786         fb_unregister_client(&dcon->fbevent_nb);
787         unregister_reboot_notifier(&dcon->reboot_nb);
788         atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
789
790         free_irq(DCON_IRQ, &dcon_driver);
791
792         if (dcon_bl_dev != NULL)
793                 backlight_device_unregister(dcon_bl_dev);
794
795         if (dcon_device != NULL)
796                 platform_device_unregister(dcon_device);
797         cancel_work_sync(&dcon->switch_source);
798
799         kfree(dcon);
800
801         return 0;
802 }
803
804 #ifdef CONFIG_PM
805 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
806 {
807         struct dcon_priv *dcon = i2c_get_clientdata(client);
808
809         if (!dcon->asleep) {
810                 /* Set up the DCON to have the source */
811                 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
812         }
813
814         return 0;
815 }
816
817 static int dcon_resume(struct i2c_client *client)
818 {
819         struct dcon_priv *dcon = i2c_get_clientdata(client);
820
821         if (!dcon->asleep) {
822                 dcon_bus_stabilize(dcon, 0);
823                 dcon_set_source(dcon, DCON_SOURCE_CPU);
824         }
825
826         return 0;
827 }
828
829 #endif
830
831
832 static irqreturn_t dcon_interrupt(int irq, void *id)
833 {
834         int status = pdata->read_status();
835
836         if (status == -1)
837                 return IRQ_NONE;
838
839         switch (status & 3) {
840         case 3:
841                 printk(KERN_DEBUG "olpc-dcon: DCONLOAD_MISSED interrupt\n");
842                 break;
843
844         case 2: /* switch to DCON mode */
845         case 1: /* switch to CPU mode */
846                 dcon_switched = 1;
847                 getnstimeofday(&dcon_irq_time);
848                 wake_up(&dcon_wait_queue);
849                 break;
850
851         case 0:
852                 /* workaround resume case:  the DCON (on 1.5) doesn't
853                  * ever assert status 0x01 when switching to CPU mode
854                  * during resume.  this is because DCONLOAD is de-asserted
855                  * _immediately_ upon exiting S3, so the actual release
856                  * of the DCON happened long before this point.
857                  * see http://dev.laptop.org/ticket/9869
858                  */
859                 if (dcon_source != dcon_pending && !dcon_switched) {
860                         dcon_switched = 1;
861                         getnstimeofday(&dcon_irq_time);
862                         wake_up(&dcon_wait_queue);
863                         printk(KERN_DEBUG "olpc-dcon: switching w/ status 0/0\n");
864                 } else {
865                         printk(KERN_DEBUG "olpc-dcon: scanline interrupt w/CPU\n");
866                 }
867         }
868
869         return IRQ_HANDLED;
870 }
871
872 static const struct i2c_device_id dcon_idtable[] = {
873         { "olpc_dcon",  0 },
874         { }
875 };
876
877 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
878
879 static struct i2c_driver dcon_driver = {
880         .driver = {
881                 .name   = "olpc_dcon",
882         },
883         .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
884         .id_table = dcon_idtable,
885         .probe = dcon_probe,
886         .remove = __devexit_p(dcon_remove),
887         .detect = dcon_detect,
888         .address_list = normal_i2c,
889 #ifdef CONFIG_PM
890         .suspend = dcon_suspend,
891         .resume = dcon_resume,
892 #endif
893 };
894
895 #include "olpc_dcon_xo_1.c"
896
897 static int __init olpc_dcon_init(void)
898 {
899         pdata = &dcon_pdata_xo_1;
900
901         return i2c_add_driver(&dcon_driver);
902 }
903
904 static void __exit olpc_dcon_exit(void)
905 {
906         i2c_del_driver(&dcon_driver);
907 }
908
909 module_init(olpc_dcon_init);
910 module_exit(olpc_dcon_exit);
911
912 MODULE_LICENSE("GPL");