2 * Mainly by David Woodhouse, somewhat modified by Jordan Crouse
4 * Copyright © 2006-2007 Red Hat, Inc.
5 * Copyright © 2006-2007 Advanced Micro Devices, Inc.
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.
13 #include <linux/kernel.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>
31 #include "olpc_dcon.h"
33 /* Module definitions */
35 static int resumeline = 898;
36 module_param(resumeline, int, 0444);
39 module_param(noinit, int, 0444);
41 /* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
43 module_param(useaa, int, 0444);
47 static struct i2c_driver dcon_driver;
48 static struct i2c_client *dcon_client;
50 /* Platform devices */
51 static struct platform_device *dcon_device;
53 /* Backlight device */
54 static struct backlight_device *dcon_bl_dev;
56 /* Base address of the GPIO registers */
57 static unsigned long gpio_base;
59 static struct fb_info *fbinfo;
61 /* Current source, initialized at probe time */
62 static int dcon_source;
65 static int dcon_pending;
67 /* Current output type */
68 static int dcon_output = DCON_OUTPUT_COLOR;
70 /* Current sleep status (not yet implemented) */
71 static int dcon_sleep_val = DCON_ACTIVE;
73 /* Shadow register for the DCON_REG_MODE register */
74 static unsigned short dcon_disp_mode;
76 /* Variables used during switches */
77 static int dcon_switched;
79 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
81 static unsigned short normal_i2c[] = { 0x0D, I2C_CLIENT_END };
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)
87 /* The current backlight value - this saves us some smbus traffic */
88 static int bl_val = -1;
90 /* ===== API functions - these are called by a variety of users ==== */
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.
97 static int dcon_get_backlight(void)
99 if (dcon_client == NULL)
103 bl_val = dcon_read(DCON_REG_BRIGHT) & 0x0F;
108 static void dcon_set_backlight(int level)
110 if (dcon_client == NULL)
113 if (bl_val == (level & 0x0F))
116 bl_val = level & 0x0F;
117 dcon_write(DCON_REG_BRIGHT, bl_val);
119 /* Purposely turn off the backlight when we go to level 0 */
122 dcon_disp_mode &= ~MODE_BL_ENABLE;
123 dcon_write(DCON_REG_MODE, dcon_disp_mode);
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);
131 /* Set the output type to either color or mono */
133 static int dcon_set_output(int arg)
135 if (dcon_output == arg)
140 if (arg == DCON_OUTPUT_MONO) {
141 dcon_disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
142 dcon_disp_mode |= MODE_MONO_LUMA;
145 dcon_disp_mode &= ~(MODE_MONO_LUMA);
146 dcon_disp_mode |= MODE_CSWIZZLE;
148 dcon_disp_mode |= MODE_COL_AA;
151 dcon_write(DCON_REG_MODE, dcon_disp_mode);
155 /* For now, this will be really stupid - we need to address how
156 * DCONLOAD works in a sleep and account for it accordingly
159 static void dcon_sleep(int state)
161 /* Turn off the backlight and put the DCON to sleep */
163 if (state == dcon_sleep_val)
166 if (state == DCON_SLEEP) {
167 dcon_disp_mode &= ~MODE_BL_ENABLE;
168 dcon_disp_mode |= MODE_SLEEP;
171 /* Only re-enable the backlight if the backlight value is set */
174 dcon_disp_mode |= MODE_BL_ENABLE;
176 dcon_disp_mode &= ~MODE_SLEEP;
179 dcon_sleep_val = state;
180 dcon_write(DCON_REG_MODE, dcon_disp_mode);
182 /* We should turn off some stuff in the framebuffer - but what? */
185 /* Set the source of the display (CPU or DCON) */
187 static void dcon_source_switch(struct work_struct *work)
189 DECLARE_WAITQUEUE(wait, current);
190 int source = dcon_pending;
192 if (dcon_source == source)
198 case DCON_SOURCE_CPU:
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");
204 /* Wait up to one second for the scanline interrupt */
205 wait_event_timeout(dcon_wait_queue, dcon_switched == 1, HZ);
209 printk(KERN_ERR "olpc-dcon: Timeout entering CPU mode; expect a screen glitch.\n");
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.
217 * For now, we just hope..
219 if (fb_powerup(fbinfo)) {
220 printk(KERN_ERR "olpc-dcon: Failed to enter CPU mode\n");
221 dcon_pending = DCON_SOURCE_DCON;
225 /* And turn off the DCON */
226 outl(1<<11, gpio_base + GPIOx_OUT_VAL);
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");
232 printk(KERN_INFO "olpc-dcon: The CPU has control\n");
234 case DCON_SOURCE_DCON:
238 add_wait_queue(&dcon_wait_queue, &wait);
239 set_current_state(TASK_UNINTERRUPTIBLE);
241 /* Clear GPIO11 (DCONLOAD) - this implies that the DCON is in
244 outl(1 << (11 + 16), gpio_base + GPIOx_OUT_VAL);
246 t = schedule_timeout(HZ/2);
247 remove_wait_queue(&dcon_wait_queue, &wait);
248 set_current_state(TASK_RUNNING);
251 printk(KERN_ERR "olpc-dcon: Timeout entering DCON mode; expect a screen glitch.\n");
253 /* Turn off the graphics engine completely */
254 fb_powerdown(fbinfo);
256 printk(KERN_INFO "olpc-dcon: The DCON has control\n");
263 dcon_source = source;
266 static DECLARE_WORK(dcon_work, dcon_source_switch);
268 static int dcon_set_source(int arg)
270 if (arg != DCON_SOURCE_CPU && arg != DCON_SOURCE_DCON)
273 if (dcon_pending == arg)
277 if ((dcon_source != arg) && !work_pending(&dcon_work))
278 schedule_work(&dcon_work);
283 static int dcon_set_source_sync(int arg)
285 int ret = dcon_set_source(arg);
287 flush_scheduled_work();
291 static int dconbl_set(struct backlight_device *dev) {
293 int level = dev->props.brightness;
295 if (dev->props.power != FB_BLANK_UNBLANK)
298 dcon_set_backlight(level);
302 static int dconbl_get(struct backlight_device *dev) {
303 return dcon_get_backlight();
306 static ssize_t dcon_mode_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 return sprintf(buf, "%4.4X\n", dcon_disp_mode);
312 static ssize_t dcon_sleep_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 return sprintf(buf, "%d\n", dcon_sleep_val);
318 static ssize_t /* __deprecated */ dcon_source_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 printk(KERN_WARNING "olpc-dcon: using deprecated sysfs 'source' interface; use 'freeze' instead!\n");
322 return sprintf(buf, "%d\n", dcon_source);
325 static ssize_t dcon_freeze_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 return sprintf(buf, "%d\n", dcon_source == DCON_SOURCE_DCON ? 1 : 0);
331 static ssize_t dcon_output_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 return sprintf(buf, "%d\n", dcon_output);
337 static ssize_t dcon_resumeline_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 return sprintf(buf, "%d\n", resumeline);
343 static int _strtoul(const char *buf, int len, unsigned int *val)
347 unsigned int output = simple_strtoul(buf, &endp, 0);
348 int size = endp - buf;
350 if (*endp && isspace(*endp))
360 static ssize_t dcon_output_store(struct device *dev,
361 struct device_attribute *attr, const char *buf, size_t count)
366 if (_strtoul(buf, count, &output))
369 if (output == DCON_OUTPUT_COLOR || output == DCON_OUTPUT_MONO) {
370 dcon_set_output(output);
377 static ssize_t /* __deprecated */ dcon_source_store(struct device *dev,
378 struct device_attribute *attr, const char *buf, size_t count)
383 printk(KERN_WARNING "olpc-dcon: using deprecated sysfs 'source' interface; use 'freeze' instead!\n");
384 if (_strtoul(buf, count, &output))
387 dcon_set_source(output);
393 static ssize_t dcon_freeze_store(struct device *dev,
394 struct device_attribute *attr, const char *buf, size_t count)
399 if (_strtoul(buf, count, &output))
402 dcon_set_source(output ? DCON_SOURCE_DCON : DCON_SOURCE_CPU);
408 static ssize_t dcon_resumeline_store(struct device *dev,
409 struct device_attribute *attr, const char *buf, size_t count)
414 if (_strtoul(buf, count, &rl))
418 dcon_write(DCON_REG_SCAN_INT, resumeline);
424 static ssize_t dcon_sleep_store(struct device *dev,
425 struct device_attribute *attr, const char *buf, size_t count)
429 if (_strtoul(buf, count, &output))
432 dcon_sleep(output ? DCON_SLEEP : DCON_ACTIVE);
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),
445 static struct backlight_ops dcon_bl_ops = {
446 .get_brightness = dconbl_get,
447 .update_status = dconbl_set
450 /* List of GPIOs that we care about:
451 (in) GPIO12 -- DCONBLNK
452 (in) GPIO[56] -- DCONSTAT[01]
453 (out) GPIO11 -- DCONLOAD
456 #define IN_GPIOS ((1<<5) | (1<<6) | (1<<7) | (1<<12))
457 #define OUT_GPIOS (1<<11)
459 static irqreturn_t dcon_interrupt(int, void *);
461 static int dcon_request_irq(void)
463 unsigned long lo, hi;
466 rdmsr(MSR_LBAR_GPIO, lo, hi);
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");
474 /* Mask off the IO base address */
475 gpio_base = lo & 0x0000ff00;
477 /* Turn off the event enable for GPIO7 just to be safe */
478 outl(1 << (16+7), gpio_base + GPIOx_EVNT_EN);
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);
484 /* Set up the interrupt mappings */
486 /* Set the IRQ to pair 2 */
487 geode_gpio_event_irq(OLPC_GPIO_DCON_IRQ, 2);
489 /* Enable group 2 to trigger the DCON interrupt */
490 geode_gpio_set_irq(2, DCON_IRQ);
492 /* Select edge level for interrupt (in PIC) */
495 lob &= ~(1 << DCON_IRQ);
498 /* Register the interupt handler */
499 if (request_irq(DCON_IRQ, &dcon_interrupt, 0, "DCON", &dcon_driver))
502 /* Clear INV_EN for GPIO7 (DCONIRQ) */
503 outl((1<<(16+7)), gpio_base + GPIOx_INV_EN);
505 /* Enable filter for GPIO12 (DCONBLANK) */
506 outl(1<<(12), gpio_base + GPIOx_IN_FLTR_EN);
508 /* Disable filter for GPIO7 */
509 outl(1<<(16+7), gpio_base + GPIOx_IN_FLTR_EN);
511 /* Disable event counter for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
513 outl(1<<(16+7), gpio_base + GPIOx_EVNTCNT_EN);
514 outl(1<<(16+12), gpio_base + GPIOx_EVNTCNT_EN);
516 /* Add GPIO12 to the Filter Event Pair #7 */
517 outb(12, gpio_base + GPIO_FE7_SEL);
519 /* Turn off negative Edge Enable for GPIO12 */
520 outl(1<<(16+12), gpio_base + GPIOx_NEGEDGE_EN);
522 /* Enable negative Edge Enable for GPIO7 */
523 outl(1<<7, gpio_base + GPIOx_NEGEDGE_EN);
525 /* Zero the filter amount for Filter Event Pair #7 */
526 outw(0, gpio_base + GPIO_FLT7_AMNT);
528 /* Clear the negative edge status for GPIO7 and GPIO12 */
529 outl((1<<7) | (1<<12), gpio_base+0x4c);
531 /* FIXME: Clear the posiitive status as well, just to be sure */
532 outl((1<<7) | (1<<12), gpio_base+0x48);
534 /* Enable events for GPIO7 (DCONIRQ) and GPIO12 (DCONBLANK) */
535 outl((1<<(7))|(1<<12), gpio_base + GPIOx_EVNT_EN);
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)
542 dcon_pending = dcon_source;
547 static int dcon_reboot_notify(struct notifier_block *nb, unsigned long foo, void *bar)
549 if (dcon_client == NULL)
552 /* Turn off the DCON. Entirely. */
553 dcon_write(DCON_REG_MODE, 0x39);
554 dcon_write(DCON_REG_MODE, 0x32);
558 static int dcon_conswitch_notify(struct notifier_block *nb,
559 unsigned long mode, void *dummy)
561 if (mode == CONSOLE_EVENT_SWITCH_TEXT)
562 dcon_sleep(DCON_ACTIVE);
567 static struct notifier_block dcon_nb = {
568 .notifier_call = dcon_reboot_notify,
572 static struct notifier_block dcon_console_nb = {
573 .notifier_call = dcon_conswitch_notify,
577 static int dcon_probe(struct i2c_adapter *adap, int addr, int kind)
579 struct i2c_client *client;
583 if (!olpc_has_dcon()) {
584 printk("olpc-dcon: No DCON is attached.\n");
588 if (num_registered_fb >= 1)
589 fbinfo = registered_fb[0];
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);
597 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
601 strncpy(client->name, "OLPC-DCON", I2C_NAME_SIZE);
603 client->adapter = adap;
604 client->driver = &dcon_driver;
606 if ((rc = i2c_attach_client(client)) != 0) {
607 printk(KERN_ERR "olpc-dcon: Unable to attach the I2C client.\n");
611 ver = i2c_smbus_read_word_data(client, DCON_REG_ID);
613 if ((ver >> 8) != 0xDC) {
614 printk(KERN_ERR "olpc-dcon: DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
619 if ((rc = dcon_request_irq())) {
620 printk(KERN_ERR "olpc-dcon: Unable to grab IRQ.\n");
624 if (ver < 0xdc02 && !noinit) {
625 /* Initialize the DCON registers */
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);
635 /* Initialise SDRAM */
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);
642 /* Colour swizzle, AA, no passthrough, backlight */
644 dcon_disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE | MODE_CSWIZZLE;
646 dcon_disp_mode |= MODE_COL_AA;
647 i2c_smbus_write_word_data(client, DCON_REG_MODE, dcon_disp_mode);
650 /* Set the scanline to interrupt on during resume */
652 i2c_smbus_write_word_data(client, DCON_REG_SCAN_INT, resumeline);
654 /* Add the DCON device */
656 dcon_device = platform_device_alloc("dcon", -1);
658 if (dcon_device == NULL) {
659 printk(KERN_ERR "dcon: Unable to create the DCON device\n");
664 if ((rc = platform_device_add(dcon_device))) {
665 printk(KERN_ERR "dcon: Unable to add the DCON device\n");
669 for(i = 0; i < ARRAY_SIZE(dcon_device_files); i++)
670 device_create_file(&dcon_device->dev, &dcon_device_files[i]);
672 /* Add the backlight device for the DCON */
674 dcon_client = client;
676 dcon_bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
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));
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();
688 backlight_update_status(dcon_bl_dev);
691 register_reboot_notifier(&dcon_nb);
692 console_event_register(&dcon_console_nb);
694 printk(KERN_INFO "olpc-dcon: Discovered DCON version %x\n", ver & 0xFF);
699 platform_device_unregister(dcon_device);
702 free_irq(DCON_IRQ, &dcon_driver);
704 i2c_detach_client(client);
711 static int dcon_attach(struct i2c_adapter *adap)
715 ret = i2c_probe(adap, &addr_data, dcon_probe);
717 if (dcon_client == NULL)
718 printk(KERN_ERR "olpc-dcon: No DCON found on SMBus\n");
723 static int dcon_detach(struct i2c_client *client)
728 unregister_reboot_notifier(&dcon_nb);
729 console_event_unregister(&dcon_console_nb);
731 free_irq(DCON_IRQ, &dcon_driver);
733 if ((rc = i2c_detach_client(client)) == 0)
734 kfree(i2c_get_clientdata(client));
736 if (dcon_bl_dev != NULL)
737 backlight_device_unregister(dcon_bl_dev);
739 if (dcon_device != NULL)
740 platform_device_unregister(dcon_device);
741 cancel_work_sync(&dcon_work);
748 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
750 if (dcon_sleep_val != DCON_ACTIVE)
753 /* Set up the DCON to have the source */
754 return dcon_set_source_sync(DCON_SOURCE_DCON);
757 static int dcon_resume(struct i2c_client *client)
760 if (dcon_sleep_val != DCON_ACTIVE)
763 /* HACK: ensure the bus is stable */
765 x = dcon_read(DCON_REG_ID);
768 return dcon_set_source(DCON_SOURCE_CPU);
773 static irqreturn_t dcon_interrupt(int irq, void *id)
775 int status = inl(gpio_base + GPIOx_READ_BACK) >> 5;
777 /* Clear the negative edge status for GPIO7 */
778 outl(1 << 7, gpio_base + GPIOx_NEGEDGE_STS);
780 switch (status & 3) {
782 printk(KERN_DEBUG "olpc-dcon: DCONLOAD_MISSED interrupt\n");
784 case 2: /* switch to DCON mode */
785 case 1: /* switch to CPU mode */
787 wake_up(&dcon_wait_queue);
790 printk(KERN_DEBUG "olpc-dcon: scanline interrupt w/CPU\n");
796 static struct i2c_driver dcon_driver = {
800 .id = I2C_DRIVERID_DCON,
801 .attach_adapter = dcon_attach,
802 .detach_client = dcon_detach,
804 .suspend = dcon_suspend,
805 .resume = dcon_resume,
810 static int __init olpc_dcon_init(void)
812 i2c_add_driver(&dcon_driver);
816 static void __exit olpc_dcon_exit(void)
818 i2c_del_driver(&dcon_driver);
821 module_init(olpc_dcon_init);
822 module_exit(olpc_dcon_exit);
824 MODULE_LICENSE("GPL");