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