cleaning up olpc patch 1
[openwrt/openwrt.git] / target / linux / olpc / files / drivers / video / 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 *
7 * This program is free software. You can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
10 */
11
12
13 #include <linux/kernel.h>
14 #include <linux/fb.h>
15 #include <linux/i2c.h>
16 #include <linux/platform_device.h>
17 #include <linux/i2c-id.h>
18 #include <linux/pci.h>
19 #include <linux/vt_kern.h>
20 #include <linux/pci_ids.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/backlight.h>
24 #include <linux/device.h>
25 #include <asm/uaccess.h>
26 #include <linux/ctype.h>
27 #include <linux/reboot.h>
28 #include <asm/tsc.h>
29 #include <asm/olpc.h>
30
31 #include "olpc_dcon.h"
32
33 /* Module definitions */
34
35 static int resumeline = 898;
36 module_param(resumeline, int, 0444);
37
38 static int noinit;
39 module_param(noinit, int, 0444);
40
41 /* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
42 static int useaa = 1;
43 module_param(useaa, int, 0444);
44
45 /* I2C structures */
46
47 static struct i2c_driver dcon_driver;
48 static struct i2c_client *dcon_client;
49
50 /* Platform devices */
51 static struct platform_device *dcon_device;
52
53 /* Backlight device */
54 static struct backlight_device *dcon_bl_dev;
55
56 /* Base address of the GPIO registers */
57 static unsigned long gpio_base;
58
59 static struct fb_info *fbinfo;
60
61 /* Current source, initialized at probe time */
62 static int dcon_source;
63
64 /* Desired source */
65 static int dcon_pending;
66
67 /* Current output type */
68 static int dcon_output = DCON_OUTPUT_COLOR;
69
70 /* Current sleep status (not yet implemented) */
71 static int dcon_sleep_val = DCON_ACTIVE;
72
73 /* Shadow register for the DCON_REG_MODE register */
74 static unsigned short dcon_disp_mode;
75
76 /* Variables used during switches */
77 static int dcon_switched;
78
79 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
80
81 static unsigned short normal_i2c[] = { 0x0D, I2C_CLIENT_END };
82 I2C_CLIENT_INSMOD;
83
84 #define dcon_write(reg,val) i2c_smbus_write_word_data(dcon_client,reg,val)
85 #define dcon_read(reg) i2c_smbus_read_word_data(dcon_client,reg)
86
87 /* The current backlight value - this saves us some smbus traffic */
88 static int bl_val = -1;
89
90 /* ===== API functions - these are called by a variety of users ==== */
91
92 /* Backlight notes - turning off the backlight enable bit in the DCON
93 * doesn't save us any power over just pushing the BL to zero, so we
94 * don't use that bit in this code.
95 */
96
97 static int dcon_get_backlight(void)
98 {
99 if (dcon_client == NULL)
100 return 0;
101
102 if (bl_val == -1)
103 bl_val = dcon_read(DCON_REG_BRIGHT) & 0x0F;
104
105 return bl_val;
106 }
107
108 static void dcon_set_backlight(int level)
109 {
110 if (dcon_client == NULL)
111 return;
112
113 if (bl_val == (level & 0x0F))
114 return;
115
116 bl_val = level & 0x0F;
117 dcon_write(DCON_REG_BRIGHT, bl_val);
118
119 /* Purposely turn off the backlight when we go to level 0 */
120
121 if (bl_val == 0) {
122 dcon_disp_mode &= ~MODE_BL_ENABLE;
123 dcon_write(DCON_REG_MODE, dcon_disp_mode);
124 }
125 else if (!(dcon_disp_mode & MODE_BL_ENABLE)) {
126 dcon_disp_mode |= MODE_BL_ENABLE;
127 dcon_write(DCON_REG_MODE, dcon_disp_mode);
128 }
129 }
130
131 /* Set the output type to either color or mono */
132
133 static int dcon_set_output(int arg)
134 {
135 if (dcon_output == arg)
136 return 0;
137
138 dcon_output = arg;
139
140 if (arg == DCON_OUTPUT_MONO) {
141 dcon_disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
142 dcon_disp_mode |= MODE_MONO_LUMA;
143 }
144 else {
145 dcon_disp_mode &= ~(MODE_MONO_LUMA);
146 dcon_disp_mode |= MODE_CSWIZZLE;
147 if (useaa)
148 dcon_disp_mode |= MODE_COL_AA;
149 }
150
151 dcon_write(DCON_REG_MODE, dcon_disp_mode);
152 return 0;
153 }
154
155 /* For now, this will be really stupid - we need to address how
156 * DCONLOAD works in a sleep and account for it accordingly
157 */
158
159 static void dcon_sleep(int state)
160 {
161 /* Turn off the backlight and put the DCON to sleep */
162
163 if (state == dcon_sleep_val)
164 return;
165
166 if (state == DCON_SLEEP) {
167 dcon_disp_mode &= ~MODE_BL_ENABLE;
168 dcon_disp_mode |= MODE_SLEEP;
169 }
170 else {
171 /* Only re-enable the backlight if the backlight value is set */
172
173 if (bl_val != 0)
174 dcon_disp_mode |= MODE_BL_ENABLE;
175
176 dcon_disp_mode &= ~MODE_SLEEP;
177 }
178
179 dcon_sleep_val = state;
180 dcon_write(DCON_REG_MODE, dcon_disp_mode);
181
182 /* We should turn off some stuff in the framebuffer - but what? */
183 }
184
185 /* Set the source of the display (CPU or DCON) */
186
187 static void dcon_source_switch(struct work_struct *work)
188 {
189 DECLARE_WAITQUEUE(wait, current);
190 int source = dcon_pending;
191
192 if (dcon_source == source)
193 return;
194
195 dcon_switched = 0;
196
197 switch (source) {
198 case DCON_SOURCE_CPU:
199
200 /* Enable the scanline interrupt bit */
201 if (dcon_write(DCON_REG_MODE, dcon_disp_mode | MODE_SCAN_INT))
202 printk(KERN_ERR "olpc-dcon: couldn't enable scanline interrupt!\n");
203 else {
204 /* Wait up to one second for the scanline interrupt */
205 wait_event_timeout(dcon_wait_queue, dcon_switched == 1, HZ);
206 }
207
208 if (!dcon_switched)
209 printk(KERN_ERR "olpc-dcon: Timeout entering CPU mode; expect a screen glitch.\n");
210
211 /*
212 * Ideally we'd like to disable interrupts here so that the
213 * fb_powerup and DCON turn on happen at a known time value;
214 * however, we can't do that right now with fb_set_suspend
215 * messing with semaphores.
216 *
217 * For now, we just hope..
218 */
219 if (fb_powerup(fbinfo)) {
220 printk(KERN_ERR "olpc-dcon: Failed to enter CPU mode\n");
221 dcon_pending = DCON_SOURCE_DCON;
222 return;
223 }
224
225 /* And turn off the DCON */
226 outl(1<<11, gpio_base + GPIOx_OUT_VAL);
227
228 /* Turn off the scanline interrupt */
229 if (dcon_write(DCON_REG_MODE, dcon_disp_mode))
230 printk(KERN_ERR "olpc-dcon: couldn't disable scanline interrupt!\n");
231
232 printk(KERN_INFO "olpc-dcon: The CPU has control\n");
233 break;
234 case DCON_SOURCE_DCON:
235 {
236 int t;
237
238 add_wait_queue(&dcon_wait_queue, &wait);
239 set_current_state(TASK_UNINTERRUPTIBLE);
240
241 /* Clear GPIO11 (DCONLOAD) - this implies that the DCON is in
242 control */
243
244 outl(1 << (11 + 16), gpio_base + GPIOx_OUT_VAL);
245
246 t = schedule_timeout(HZ/2);
247 remove_wait_queue(&dcon_wait_queue, &wait);
248 set_current_state(TASK_RUNNING);
249
250 if (!dcon_switched)
251 printk(KERN_ERR "olpc-dcon: Timeout entering DCON mode; expect a screen glitch.\n");
252
253 /* Turn off the graphics engine completely */
254 fb_powerdown(fbinfo);
255
256 printk(KERN_INFO "olpc-dcon: The DCON has control\n");
257 break;
258 }
259 default:
260 BUG();
261 }
262
263 dcon_source = source;
264 }
265
266 static DECLARE_WORK(dcon_work, dcon_source_switch);
267
268 static int dcon_set_source(int arg)
269 {
270 if (arg != DCON_SOURCE_CPU && arg != DCON_SOURCE_DCON)
271 return -EINVAL;
272
273 if (dcon_pending == arg)
274 return 0;
275
276 dcon_pending = arg;
277 if ((dcon_source != arg) && !work_pending(&dcon_work))
278 schedule_work(&dcon_work);
279
280 return 0;
281 }
282
283 static int dcon_set_source_sync(int arg)
284 {
285 int ret = dcon_set_source(arg);
286 if (!ret)
287 flush_scheduled_work();
288 return ret;
289 }
290
291 static int dconbl_set(struct backlight_device *dev) {
292
293 int level = dev->props.brightness;
294
295 if (dev->props.power != FB_BLANK_UNBLANK)
296 level = 0;
297
298 dcon_set_backlight(level);
299 return 0;
300 }
301
302 static int dconbl_get(struct backlight_device *dev) {
303 return dcon_get_backlight();
304 }
305
306 static ssize_t dcon_mode_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308 {
309 return sprintf(buf, "%4.4X\n", dcon_disp_mode);
310 }
311
312 static ssize_t dcon_sleep_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314 {
315 return sprintf(buf, "%d\n", dcon_sleep_val);
316 }
317
318 static ssize_t /* __deprecated */ dcon_source_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
320 {
321 printk(KERN_WARNING "olpc-dcon: using deprecated sysfs 'source' interface; use 'freeze' instead!\n");
322 return sprintf(buf, "%d\n", dcon_source);
323 }
324
325 static ssize_t dcon_freeze_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327 {
328 return sprintf(buf, "%d\n", dcon_source == DCON_SOURCE_DCON ? 1 : 0);
329 }
330
331 static ssize_t dcon_output_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333 {
334 return sprintf(buf, "%d\n", dcon_output);
335 }
336
337 static ssize_t dcon_resumeline_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
339 {
340 return sprintf(buf, "%d\n", resumeline);
341 }
342
343 static int _strtoul(const char *buf, int len, unsigned int *val)
344 {
345
346 char *endp;
347 unsigned int output = simple_strtoul(buf, &endp, 0);
348 int size = endp - buf;
349
350 if (*endp && isspace(*endp))
351 size++;
352
353 if (size != len)
354 return -EINVAL;
355
356 *val = output;
357 return 0;
358 }
359
360 static ssize_t dcon_output_store(struct device *dev,
361 struct device_attribute *attr, const char *buf, size_t count)
362 {
363 int output;
364 int rc = -EINVAL;
365
366 if (_strtoul(buf, count, &output))
367 return -EINVAL;
368
369 if (output == DCON_OUTPUT_COLOR || output == DCON_OUTPUT_MONO) {
370 dcon_set_output(output);
371 rc = count;
372 }
373
374 return rc;
375 }
376
377 static ssize_t /* __deprecated */ dcon_source_store(struct device *dev,
378 struct device_attribute *attr, const char *buf, size_t count)
379 {
380 int output;
381 int rc = -EINVAL;
382
383 printk(KERN_WARNING "olpc-dcon: using deprecated sysfs 'source' interface; use 'freeze' instead!\n");
384 if (_strtoul(buf, count, &output))
385 return -EINVAL;
386
387 dcon_set_source(output);
388 rc = count;
389
390 return rc;
391 }
392
393 static ssize_t dcon_freeze_store(struct device *dev,
394 struct device_attribute *attr, const char *buf, size_t count)
395 {
396 int output;
397 int rc = -EINVAL;
398
399 if (_strtoul(buf, count, &output))
400 return rc;
401
402 dcon_set_source(output ? DCON_SOURCE_DCON : DCON_SOURCE_CPU);
403 rc = count;
404
405 return rc;
406 }
407
408 static ssize_t dcon_resumeline_store(struct device *dev,
409 struct device_attribute *attr, const char *buf, size_t count)
410 {
411 int rl;
412 int rc = -EINVAL;
413
414 if (_strtoul(buf, count, &rl))
415 return rc;
416
417 resumeline = rl;
418 dcon_write(DCON_REG_SCAN_INT, resumeline);
419 rc = count;
420
421 return rc;
422 }
423
424 static ssize_t dcon_sleep_store(struct device *dev,
425 struct device_attribute *attr, const char *buf, size_t count)
426 {
427 int output;
428
429 if (_strtoul(buf, count, &output))
430 return -EINVAL;
431
432 dcon_sleep(output ? DCON_SLEEP : DCON_ACTIVE);
433 return count;
434 }
435
436 static struct device_attribute dcon_device_files[] = {
437 __ATTR(mode, 0444, dcon_mode_show, NULL),
438 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
439 __ATTR(source, 0644, dcon_source_show, dcon_source_store),
440 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
441 __ATTR(output, 0644, dcon_output_show, dcon_output_store),
442 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
443 };
444
445 static struct backlight_ops dcon_bl_ops = {
446 .get_brightness = dconbl_get,
447 .update_status = dconbl_set
448 };
449
450 /* List of GPIOs that we care about:
451 (in) GPIO12 -- DCONBLNK
452 (in) GPIO[56] -- DCONSTAT[01]
453 (out) GPIO11 -- DCONLOAD
454 */
455
456 #define IN_GPIOS ((1<<5) | (1<<6) | (1<<7) | (1<<12))
457 #define OUT_GPIOS (1<<11)
458
459 static irqreturn_t dcon_interrupt(int, void *);
460
461 static int dcon_request_irq(void)
462 {
463 unsigned long lo, hi;
464 unsigned char lob;
465
466 rdmsr(MSR_LBAR_GPIO, lo, hi);
467
468 /* Check the mask and whether GPIO is enabled (sanity check) */
469 if (hi != 0x0000f001) {
470 printk(KERN_ERR "GPIO not enabled -- cannot use DCON\n");
471 return -ENODEV;
472 }
473
474 /* Mask off the IO base address */
475 gpio_base = lo & 0x0000ff00;
476
477 /* Turn off the event enable for GPIO7 just to be safe */
478 outl(1 << (16+7), gpio_base + GPIOx_EVNT_EN);
479
480 /* Set the directions for the GPIO pins */
481 outl(OUT_GPIOS | (IN_GPIOS << 16), gpio_base + GPIOx_OUT_EN);
482 outl(IN_GPIOS | (OUT_GPIOS << 16), gpio_base + GPIOx_IN_EN);
483
484 /* Set up the interrupt mappings */
485
486 /* Set the IRQ to pair 2 */
487 geode_gpio_event_irq(OLPC_GPIO_DCON_IRQ, 2);
488
489 /* Enable group 2 to trigger the DCON interrupt */
490 geode_gpio_set_irq(2, DCON_IRQ);
491
492 /* Select edge level for interrupt (in PIC) */
493
494 lob = inb(0x4d0);
495 lob &= ~(1 << DCON_IRQ);
496 outb(lob, 0x4d0);
497
498 /* Register the interupt handler */
499 if (request_irq(DCON_IRQ, &dcon_interrupt, 0, "DCON", &dcon_driver))
500 return -EIO;
501
502 /* Clear INV_EN for GPIO7 (DCONIRQ) */
503 outl((1<<(16+7)), gpio_base + GPIOx_INV_EN);
504
505 /* Enable filter for GPIO12 (DCONBLANK) */
506 outl(1<<(12), gpio_base + GPIOx_IN_FLTR_EN);
507
508 /* Disable filter for GPIO7 */
509 outl(1<<(16+7), gpio_base + GPIOx_IN_FLTR_EN);
510
511 /* Disable event counter for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
512
513 outl(1<<(16+7), gpio_base + GPIOx_EVNTCNT_EN);
514 outl(1<<(16+12), gpio_base + GPIOx_EVNTCNT_EN);
515
516 /* Add GPIO12 to the Filter Event Pair #7 */
517 outb(12, gpio_base + GPIO_FE7_SEL);
518
519 /* Turn off negative Edge Enable for GPIO12 */
520 outl(1<<(16+12), gpio_base + GPIOx_NEGEDGE_EN);
521
522 /* Enable negative Edge Enable for GPIO7 */
523 outl(1<<7, gpio_base + GPIOx_NEGEDGE_EN);
524
525 /* Zero the filter amount for Filter Event Pair #7 */
526 outw(0, gpio_base + GPIO_FLT7_AMNT);
527
528 /* Clear the negative edge status for GPIO7 and GPIO12 */
529 outl((1<<7) | (1<<12), gpio_base+0x4c);
530
531 /* FIXME: Clear the posiitive status as well, just to be sure */
532 outl((1<<7) | (1<<12), gpio_base+0x48);
533
534 /* Enable events for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
535 outl((1<<(7))|(1<<12), gpio_base + GPIOx_EVNT_EN);
536
537 /* Determine the current state by reading the GPIO bit */
538 /* Earlier stages of the boot process have established the state */
539 dcon_source = inl(gpio_base + GPIOx_OUT_VAL) & (1<<11)
540 ? DCON_SOURCE_CPU
541 : DCON_SOURCE_DCON;
542 dcon_pending = dcon_source;
543
544 return 0;
545 }
546
547 static int dcon_reboot_notify(struct notifier_block *nb, unsigned long foo, void *bar)
548 {
549 if (dcon_client == NULL)
550 return 0;
551
552 /* Turn off the DCON. Entirely. */
553 dcon_write(DCON_REG_MODE, 0x39);
554 dcon_write(DCON_REG_MODE, 0x32);
555 return 0;
556 }
557
558 static int dcon_conswitch_notify(struct notifier_block *nb,
559 unsigned long mode, void *dummy)
560 {
561 if (mode == CONSOLE_EVENT_SWITCH_TEXT)
562 dcon_sleep(DCON_ACTIVE);
563
564 return 0;
565 }
566
567 static struct notifier_block dcon_nb = {
568 .notifier_call = dcon_reboot_notify,
569 .priority = -1,
570 };
571
572 static struct notifier_block dcon_console_nb = {
573 .notifier_call = dcon_conswitch_notify,
574 .priority = -1,
575 };
576
577 static int dcon_probe(struct i2c_adapter *adap, int addr, int kind)
578 {
579 struct i2c_client *client;
580 uint16_t ver;
581 int rc, i;
582
583 if (!olpc_has_dcon()) {
584 printk("olpc-dcon: No DCON is attached.\n");
585 return -ENODEV;
586 }
587
588 if (num_registered_fb >= 1)
589 fbinfo = registered_fb[0];
590
591 if (adap->id != I2C_HW_SMBUS_SCX200) {
592 printk(KERN_ERR "olpc-dcon: Invalid I2C bus (%d not %d)\n",
593 adap->id, I2C_HW_SMBUS_SCX200);
594 return -ENXIO;
595 }
596
597 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
598 if (client == NULL)
599 return -ENOMEM;
600
601 strncpy(client->name, "OLPC-DCON", I2C_NAME_SIZE);
602 client->addr = addr;
603 client->adapter = adap;
604 client->driver = &dcon_driver;
605
606 if ((rc = i2c_attach_client(client)) != 0) {
607 printk(KERN_ERR "olpc-dcon: Unable to attach the I2C client.\n");
608 goto eclient;
609 }
610
611 ver = i2c_smbus_read_word_data(client, DCON_REG_ID);
612
613 if ((ver >> 8) != 0xDC) {
614 printk(KERN_ERR "olpc-dcon: DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
615 rc = -ENXIO;
616 goto ei2c;
617 }
618
619 if ((rc = dcon_request_irq())) {
620 printk(KERN_ERR "olpc-dcon: Unable to grab IRQ.\n");
621 goto ei2c;
622 }
623
624 if (ver < 0xdc02 && !noinit) {
625 /* Initialize the DCON registers */
626
627 /* Start with work-arounds for DCON ASIC */
628 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
629 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
630 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
631 i2c_smbus_write_word_data(client, 0x0b, 0x007a);
632 i2c_smbus_write_word_data(client, 0x36, 0x025c);
633 i2c_smbus_write_word_data(client, 0x37, 0x025e);
634
635 /* Initialise SDRAM */
636
637 i2c_smbus_write_word_data(client, 0x3b, 0x002b);
638 i2c_smbus_write_word_data(client, 0x41, 0x0101);
639 i2c_smbus_write_word_data(client, 0x42, 0x0101);
640 }
641
642 /* Colour swizzle, AA, no passthrough, backlight */
643
644 dcon_disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE | MODE_CSWIZZLE;
645 if (useaa)
646 dcon_disp_mode |= MODE_COL_AA;
647 i2c_smbus_write_word_data(client, DCON_REG_MODE, dcon_disp_mode);
648
649
650 /* Set the scanline to interrupt on during resume */
651
652 i2c_smbus_write_word_data(client, DCON_REG_SCAN_INT, resumeline);
653
654 /* Add the DCON device */
655
656 dcon_device = platform_device_alloc("dcon", -1);
657
658 if (dcon_device == NULL) {
659 printk(KERN_ERR "dcon: Unable to create the DCON device\n");
660 rc = -ENOMEM;
661 goto eirq;
662 }
663
664 if ((rc = platform_device_add(dcon_device))) {
665 printk(KERN_ERR "dcon: Unable to add the DCON device\n");
666 goto edev;
667 }
668
669 for(i = 0; i < ARRAY_SIZE(dcon_device_files); i++)
670 device_create_file(&dcon_device->dev, &dcon_device_files[i]);
671
672 /* Add the backlight device for the DCON */
673
674 dcon_client = client;
675
676 dcon_bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
677 NULL, &dcon_bl_ops);
678
679 if (IS_ERR(dcon_bl_dev)) {
680 printk(KERN_INFO "Could not register the backlight device for the DCON (%ld)\n", PTR_ERR(dcon_bl_dev));
681 dcon_bl_dev = NULL;
682 }
683 else {
684 dcon_bl_dev->props.max_brightness = 15;
685 dcon_bl_dev->props.power = FB_BLANK_UNBLANK;
686 dcon_bl_dev->props.brightness = dcon_get_backlight();
687
688 backlight_update_status(dcon_bl_dev);
689 }
690
691 register_reboot_notifier(&dcon_nb);
692 console_event_register(&dcon_console_nb);
693
694 printk(KERN_INFO "olpc-dcon: Discovered DCON version %x\n", ver & 0xFF);
695
696 return 0;
697
698 edev:
699 platform_device_unregister(dcon_device);
700 dcon_device = NULL;
701 eirq:
702 free_irq(DCON_IRQ, &dcon_driver);
703 ei2c:
704 i2c_detach_client(client);
705 eclient:
706 kfree(client);
707
708 return rc;
709 }
710
711 static int dcon_attach(struct i2c_adapter *adap)
712 {
713 int ret;
714
715 ret = i2c_probe(adap, &addr_data, dcon_probe);
716
717 if (dcon_client == NULL)
718 printk(KERN_ERR "olpc-dcon: No DCON found on SMBus\n");
719
720 return ret;
721 }
722
723 static int dcon_detach(struct i2c_client *client)
724 {
725 int rc;
726 dcon_client = NULL;
727
728 unregister_reboot_notifier(&dcon_nb);
729 console_event_unregister(&dcon_console_nb);
730
731 free_irq(DCON_IRQ, &dcon_driver);
732
733 if ((rc = i2c_detach_client(client)) == 0)
734 kfree(i2c_get_clientdata(client));
735
736 if (dcon_bl_dev != NULL)
737 backlight_device_unregister(dcon_bl_dev);
738
739 if (dcon_device != NULL)
740 platform_device_unregister(dcon_device);
741 cancel_work_sync(&dcon_work);
742
743 return rc;
744 }
745
746
747 #ifdef CONFIG_PM
748 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
749 {
750 if (dcon_sleep_val != DCON_ACTIVE)
751 return 0;
752
753 /* Set up the DCON to have the source */
754 return dcon_set_source_sync(DCON_SOURCE_DCON);
755 }
756
757 static int dcon_resume(struct i2c_client *client)
758 {
759 int x;
760 if (dcon_sleep_val != DCON_ACTIVE)
761 return 0;
762
763 /* HACK: ensure the bus is stable */
764 do {
765 x = dcon_read(DCON_REG_ID);
766 } while (x < 0);
767
768 return dcon_set_source(DCON_SOURCE_CPU);
769 }
770
771 #endif
772
773 static irqreturn_t dcon_interrupt(int irq, void *id)
774 {
775 int status = inl(gpio_base + GPIOx_READ_BACK) >> 5;
776
777 /* Clear the negative edge status for GPIO7 */
778 outl(1 << 7, gpio_base + GPIOx_NEGEDGE_STS);
779
780 switch (status & 3) {
781 case 3:
782 printk(KERN_DEBUG "olpc-dcon: DCONLOAD_MISSED interrupt\n");
783 break;
784 case 2: /* switch to DCON mode */
785 case 1: /* switch to CPU mode */
786 dcon_switched = 1;
787 wake_up(&dcon_wait_queue);
788 break;
789 case 0:
790 printk(KERN_DEBUG "olpc-dcon: scanline interrupt w/CPU\n");
791 }
792
793 return IRQ_HANDLED;
794 }
795
796 static struct i2c_driver dcon_driver = {
797 .driver = {
798 .name = "OLPC-DCON",
799 },
800 .id = I2C_DRIVERID_DCON,
801 .attach_adapter = dcon_attach,
802 .detach_client = dcon_detach,
803 #ifdef CONFIG_PM
804 .suspend = dcon_suspend,
805 .resume = dcon_resume,
806 #endif
807 };
808
809
810 static int __init olpc_dcon_init(void)
811 {
812 i2c_add_driver(&dcon_driver);
813 return 0;
814 }
815
816 static void __exit olpc_dcon_exit(void)
817 {
818 i2c_del_driver(&dcon_driver);
819 }
820
821 module_init(olpc_dcon_init);
822 module_exit(olpc_dcon_exit);
823
824 MODULE_LICENSE("GPL");