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