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.
6 * Copyright © 2009 VIA Technology, Inc.
7 * Copyright (c) 2010 Andres Salomon <dilinger@queued.net>
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.
15 #include <linux/kernel.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>
36 #include "olpc_dcon.h"
38 /* Module definitions */
40 static int resumeline = 898;
41 module_param(resumeline, int, 0444);
44 module_param(noinit, int, 0444);
46 /* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
48 module_param(useaa, int, 0444);
50 struct dcon_platform_data {
52 void (*bus_stabilize_wiggle)(void);
53 void (*set_dconload)(int);
54 int (*read_status)(void);
57 static struct dcon_platform_data *pdata;
61 static struct i2c_driver dcon_driver;
62 static struct i2c_client *dcon_client;
64 /* Platform devices */
65 static struct platform_device *dcon_device;
67 /* Backlight device */
68 static struct backlight_device *dcon_bl_dev;
70 static struct fb_info *fbinfo;
72 /* set this to 1 while controlling fb blank state from this driver */
73 static int ignore_fb_events = 0;
75 /* Current source, initialized at probe time */
76 static int dcon_source;
79 static int dcon_pending;
81 /* Current output type */
82 static int dcon_output = DCON_OUTPUT_COLOR;
84 /* Current sleep status (not yet implemented) */
85 static int dcon_sleep_val = DCON_ACTIVE;
87 /* Shadow register for the DCON_REG_MODE register */
88 static unsigned short dcon_disp_mode;
90 /* Variables used during switches */
91 static int dcon_switched;
92 static struct timespec dcon_irq_time;
93 static struct timespec dcon_load_time;
95 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
97 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
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)
102 /* The current backlight value - this saves us some smbus traffic */
103 static int bl_val = -1;
105 /* ===== API functions - these are called by a variety of users ==== */
107 static int dcon_hw_init(struct i2c_client *client, int is_init)
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 "
121 printk(KERN_INFO "olpc-dcon: Discovered DCON version %x\n",
123 if ((rc = pdata->init()) != 0) {
124 printk(KERN_ERR "olpc-dcon: Unable to init.\n");
129 if (ver < 0xdc02 && !noinit) {
130 /* Initialize the DCON registers */
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);
140 /* Initialise SDRAM */
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);
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);
154 /* Colour swizzle, AA, no passthrough, backlight */
156 dcon_disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE | MODE_CSWIZZLE;
158 dcon_disp_mode |= MODE_COL_AA;
160 i2c_smbus_write_word_data(client, DCON_REG_MODE, dcon_disp_mode);
163 /* Set the scanline to interrupt on during resume */
164 i2c_smbus_write_word_data(client, DCON_REG_SCAN_INT, resumeline);
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!
179 static int dcon_bus_stabilize(struct i2c_client *client, int is_powered_down)
181 unsigned long timeout;
185 if (is_powered_down) {
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);
192 msleep(10); /* we'll be conservative */
195 pdata->bus_stabilize_wiggle();
197 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
199 x = dcon_read(DCON_REG_ID);
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)));
206 olpc_ec_cmd(0x26, (unsigned char *) &x, 1, NULL, 0);
209 goto power_up; /* argh, stupid hardware.. */
213 return dcon_hw_init(client, 0);
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.
223 static int dcon_get_backlight(void)
225 if (dcon_client == NULL)
229 bl_val = dcon_read(DCON_REG_BRIGHT) & 0x0F;
235 static void dcon_set_backlight_hw(int level)
237 bl_val = level & 0x0F;
238 dcon_write(DCON_REG_BRIGHT, bl_val);
240 /* Purposely turn off the backlight when we go to level 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);
250 static void dcon_set_backlight(int level)
252 if (dcon_client == NULL)
255 if (bl_val == (level & 0x0F))
258 dcon_set_backlight_hw(level);
261 /* Set the output type to either color or mono */
263 static int dcon_set_output(int arg)
265 if (dcon_output == arg)
270 if (arg == DCON_OUTPUT_MONO) {
271 dcon_disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
272 dcon_disp_mode |= MODE_MONO_LUMA;
275 dcon_disp_mode &= ~(MODE_MONO_LUMA);
276 dcon_disp_mode |= MODE_CSWIZZLE;
278 dcon_disp_mode |= MODE_COL_AA;
281 dcon_write(DCON_REG_MODE, dcon_disp_mode);
285 /* For now, this will be really stupid - we need to address how
286 * DCONLOAD works in a sleep and account for it accordingly
289 static void dcon_sleep(int state)
293 /* Turn off the backlight and put the DCON to sleep */
295 if (state == dcon_sleep_val)
298 if (!olpc_board_at_least(olpc_board(0xc2)))
301 if (state == DCON_SLEEP) {
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);
307 dcon_sleep_val = state;
310 /* Only re-enable the backlight if the backlight value is set */
312 dcon_disp_mode |= MODE_BL_ENABLE;
314 if ((x=dcon_bus_stabilize(dcon_client, 1)))
315 printk(KERN_WARNING "olpc-dcon: unable to reinit dcon"
316 " hardware: %d!\n", x);
318 dcon_sleep_val = state;
320 /* Restore backlight */
321 dcon_set_backlight_hw(bl_val);
324 /* We should turn off some stuff in the framebuffer - but what? */
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
332 void dcon_load_holdoff(void)
334 struct timespec delta_t, now;
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) {
345 /* Set the source of the display (CPU or DCON) */
347 static void dcon_source_switch(struct work_struct *work)
349 DECLARE_WAITQUEUE(wait, current);
350 int source = dcon_pending;
352 if (dcon_source == 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");
366 /* Wait up to one second for the scanline interrupt */
367 wait_event_timeout(dcon_wait_queue, dcon_switched == 1, HZ);
371 printk(KERN_ERR "olpc-dcon: Timeout entering CPU mode; expect a screen glitch.\n");
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");
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.
383 * For now, we just hope..
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;
394 ignore_fb_events = 0;
395 release_console_sem();
397 /* And turn off the DCON */
398 pdata->set_dconload(1);
399 getnstimeofday(&dcon_load_time);
401 printk(KERN_INFO "olpc-dcon: The CPU has control\n");
403 case DCON_SOURCE_DCON:
406 struct timespec delta_t;
408 printk("dcon_source_switch to DCON\n");
410 add_wait_queue(&dcon_wait_queue, &wait);
411 set_current_state(TASK_UNINTERRUPTIBLE);
413 /* Clear DCONLOAD - this implies that the DCON is in control */
414 pdata->set_dconload(0);
415 getnstimeofday(&dcon_load_time);
417 t = schedule_timeout(HZ/2);
418 remove_wait_queue(&dcon_wait_queue, &wait);
419 set_current_state(TASK_RUNNING);
421 if (!dcon_switched) {
422 printk(KERN_ERR "olpc-dcon: Timeout entering DCON mode; expect a screen glitch.\n");
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
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);
441 pdata->set_dconload(0);
442 getnstimeofday(&dcon_load_time);
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();
454 printk(KERN_INFO "olpc-dcon: The DCON has control\n");
461 dcon_source = source;
464 static DECLARE_WORK(dcon_work, dcon_source_switch);
466 static void dcon_set_source(int arg)
468 if (dcon_pending == arg)
473 if ((dcon_source != arg) && !work_pending(&dcon_work))
474 schedule_work(&dcon_work);
477 static void dcon_set_source_sync(int arg)
479 dcon_set_source(arg);
480 flush_scheduled_work();
483 static int dconbl_set(struct backlight_device *dev) {
485 int level = dev->props.brightness;
487 if (dev->props.power != FB_BLANK_UNBLANK)
490 dcon_set_backlight(level);
494 static int dconbl_get(struct backlight_device *dev) {
495 return dcon_get_backlight();
498 static ssize_t dcon_mode_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 return sprintf(buf, "%4.4X\n", dcon_disp_mode);
504 static ssize_t dcon_sleep_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
508 return sprintf(buf, "%d\n", dcon_sleep_val);
511 static ssize_t dcon_freeze_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 return sprintf(buf, "%d\n", dcon_source == DCON_SOURCE_DCON ? 1 : 0);
517 static ssize_t dcon_output_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
520 return sprintf(buf, "%d\n", dcon_output);
523 static ssize_t dcon_resumeline_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 return sprintf(buf, "%d\n", resumeline);
529 static int _strtoul(const char *buf, int len, unsigned int *val)
533 unsigned int output = simple_strtoul(buf, &endp, 0);
534 int size = endp - buf;
536 if (*endp && isspace(*endp))
546 static ssize_t dcon_output_store(struct device *dev,
547 struct device_attribute *attr, const char *buf, size_t count)
552 if (_strtoul(buf, count, &output))
555 if (output == DCON_OUTPUT_COLOR || output == DCON_OUTPUT_MONO) {
556 dcon_set_output(output);
563 static ssize_t dcon_freeze_store(struct device *dev,
564 struct device_attribute *attr, const char *buf, size_t count)
568 if (_strtoul(buf, count, &output))
571 printk("dcon_freeze_store: %d\n", output);
575 dcon_set_source(DCON_SOURCE_CPU);
578 dcon_set_source_sync(DCON_SOURCE_DCON);
580 case 2: // normally unused
581 dcon_set_source(DCON_SOURCE_DCON);
590 static ssize_t dcon_resumeline_store(struct device *dev,
591 struct device_attribute *attr, const char *buf, size_t count)
596 if (_strtoul(buf, count, &rl))
600 dcon_write(DCON_REG_SCAN_INT, resumeline);
606 static ssize_t dcon_sleep_store(struct device *dev,
607 struct device_attribute *attr, const char *buf, size_t count)
611 if (_strtoul(buf, count, &output))
614 dcon_sleep(output ? DCON_SLEEP : DCON_ACTIVE);
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),
626 static struct backlight_ops dcon_bl_ops = {
627 .get_brightness = dconbl_get,
628 .update_status = dconbl_set
632 static int dcon_reboot_notify(struct notifier_block *nb, unsigned long foo, void *bar)
634 if (dcon_client == NULL)
637 /* Turn off the DCON. Entirely. */
638 dcon_write(DCON_REG_MODE, 0x39);
639 dcon_write(DCON_REG_MODE, 0x32);
643 static int dcon_conswitch_notify(struct notifier_block *nb,
644 unsigned long mode, void *dummy)
646 if (mode == CONSOLE_EVENT_SWITCH_TEXT)
647 dcon_sleep(DCON_ACTIVE);
652 static struct notifier_block dcon_nb = {
653 .notifier_call = dcon_reboot_notify,
657 static struct notifier_block dcon_console_nb = {
658 .notifier_call = dcon_conswitch_notify,
662 static int unfreeze_on_panic(struct notifier_block *nb, unsigned long e, void *p)
664 pdata->set_dconload(1);
668 static struct notifier_block dcon_panic_nb = {
669 .notifier_call = unfreeze_on_panic,
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)
676 struct fb_event *evdata = data;
677 int *blank = (int *) evdata->data;
678 if (event != FB_EVENT_BLANK || ignore_fb_events)
680 dcon_sleep((*blank) ? DCON_SLEEP : DCON_ACTIVE);
684 static struct notifier_block fb_nb = {
685 .notifier_call = fb_notifier_callback,
688 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
690 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
695 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
699 if (num_registered_fb >= 1)
700 fbinfo = registered_fb[0];
702 rc = dcon_hw_init(client, 1);
706 /* Add the DCON device */
708 dcon_device = platform_device_alloc("dcon", -1);
710 if (dcon_device == NULL) {
711 printk("dcon: Unable to create the DCON device\n");
716 i2c_set_clientdata(client, dcon_device);
718 if ((rc = platform_device_add(dcon_device))) {
719 printk("dcon: Unable to add the DCON device\n");
723 for(i = 0; i < ARRAY_SIZE(dcon_device_files); i++)
724 device_create_file(&dcon_device->dev, &dcon_device_files[i]);
726 /* Add the backlight device for the DCON */
728 dcon_client = client;
730 dcon_bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
731 NULL, &dcon_bl_ops, NULL);
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));
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();
742 backlight_update_status(dcon_bl_dev);
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);
753 platform_device_unregister(dcon_device);
755 i2c_set_clientdata(client, NULL);
757 free_irq(DCON_IRQ, &dcon_driver);
762 static int dcon_remove(struct i2c_client *client)
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);
771 free_irq(DCON_IRQ, &dcon_driver);
773 if (dcon_bl_dev != NULL)
774 backlight_device_unregister(dcon_bl_dev);
776 if (dcon_device != NULL)
777 platform_device_unregister(dcon_device);
778 cancel_work_sync(&dcon_work);
780 i2c_set_clientdata(client, NULL);
786 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
788 if (dcon_sleep_val == DCON_ACTIVE) {
789 /* Set up the DCON to have the source */
790 dcon_set_source_sync(DCON_SOURCE_DCON);
796 static int dcon_resume(struct i2c_client *client)
798 if (dcon_sleep_val == DCON_ACTIVE) {
799 dcon_bus_stabilize(client, 0);
800 dcon_set_source(DCON_SOURCE_CPU);
809 static irqreturn_t dcon_interrupt(int irq, void *id)
811 int status = pdata->read_status();
816 switch (status & 3) {
818 printk(KERN_DEBUG "olpc-dcon: DCONLOAD_MISSED interrupt\n");
821 case 2: /* switch to DCON mode */
822 case 1: /* switch to CPU mode */
824 getnstimeofday(&dcon_irq_time);
825 wake_up(&dcon_wait_queue);
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
836 if (dcon_source != dcon_pending && !dcon_switched) {
838 getnstimeofday(&dcon_irq_time);
839 wake_up(&dcon_wait_queue);
840 printk(KERN_DEBUG "olpc-dcon: switching w/ status 0/0\n");
842 printk(KERN_DEBUG "olpc-dcon: scanline interrupt w/CPU\n");
849 static struct i2c_device_id dcon_idtable[] = {
854 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
856 static struct i2c_driver dcon_driver = {
860 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
861 .id_table = dcon_idtable,
863 .remove = __devexit_p(dcon_remove),
864 .detect = dcon_detect,
865 .address_list = normal_i2c,
867 .suspend = dcon_suspend,
868 .resume = dcon_resume,
872 #if defined(CONFIG_OLPC_XO_1)
873 #include "olpc_dcon_xo_1.c"
874 #elif defined(CONFIG_OLPC_XO_1_5)
875 #include "olpc_dcon_xo_1_5.c"
877 #error "Trying to build OLPC DCON driver but kernel not configured for XO"
880 static int __init olpc_dcon_init(void)
882 #ifdef CONFIG_OLPC_XO_1
883 pdata = &dcon_pdata_xo_1;
885 pdata = &dcon_pdata_xo_1_5;
888 i2c_add_driver(&dcon_driver);
892 static void __exit olpc_dcon_exit(void)
894 i2c_del_driver(&dcon_driver);
897 module_init(olpc_dcon_init);
898 module_exit(olpc_dcon_exit);
900 MODULE_LICENSE("GPL");