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