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/pci.h>
21 #include <linux/pci_ids.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/backlight.h>
25 #include <linux/device.h>
26 #include <linux/notifier.h>
27 #include <linux/uaccess.h>
28 #include <linux/ctype.h>
29 #include <linux/reboot.h>
33 #include "olpc_dcon.h"
35 /* Module definitions */
37 static int resumeline = 898;
38 module_param(resumeline, int, 0444);
41 module_param(noinit, int, 0444);
43 /* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
45 module_param(useaa, int, 0444);
47 struct dcon_platform_data {
49 void (*bus_stabilize_wiggle)(void);
50 void (*set_dconload)(int);
51 u8 (*read_status)(void);
54 static struct dcon_platform_data *pdata;
58 static struct i2c_driver dcon_driver;
59 static struct i2c_client *dcon_client;
61 /* Platform devices */
62 static struct platform_device *dcon_device;
64 /* Backlight device */
65 static struct backlight_device *dcon_bl_dev;
67 static struct fb_info *fbinfo;
69 /* set this to 1 while controlling fb blank state from this driver */
70 static int ignore_fb_events = 0;
72 /* Current source, initialized at probe time */
73 static int dcon_source;
76 static int dcon_pending;
78 /* Current output type */
79 static int dcon_output = DCON_OUTPUT_COLOR;
81 /* Current sleep status (not yet implemented) */
82 static int dcon_sleep_val = DCON_ACTIVE;
84 /* Shadow register for the DCON_REG_MODE register */
85 static unsigned short dcon_disp_mode;
87 /* Variables used during switches */
88 static int dcon_switched;
89 static struct timespec dcon_irq_time;
90 static struct timespec dcon_load_time;
92 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
94 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
96 #define dcon_write(reg, val) i2c_smbus_write_word_data(dcon_client, reg, val)
97 #define dcon_read(reg) i2c_smbus_read_word_data(dcon_client, reg)
99 /* The current backlight value - this saves us some smbus traffic */
100 static int bl_val = -1;
102 /* ===== API functions - these are called by a variety of users ==== */
104 static int dcon_hw_init(struct i2c_client *client, int is_init)
109 ver = i2c_smbus_read_word_data(client, DCON_REG_ID);
110 if ((ver >> 8) != 0xDC) {
111 printk(KERN_ERR "olpc-dcon: DCON ID not 0xDCxx: 0x%04x "
118 printk(KERN_INFO "olpc-dcon: Discovered DCON version %x\n",
122 printk(KERN_ERR "olpc-dcon: Unable to init.\n");
127 if (ver < 0xdc02 && !noinit) {
128 /* Initialize the DCON registers */
130 /* Start with work-arounds for DCON ASIC */
131 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
132 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
133 i2c_smbus_write_word_data(client, 0x4b, 0x00cc);
134 i2c_smbus_write_word_data(client, 0x0b, 0x007a);
135 i2c_smbus_write_word_data(client, 0x36, 0x025c);
136 i2c_smbus_write_word_data(client, 0x37, 0x025e);
138 /* Initialise SDRAM */
140 i2c_smbus_write_word_data(client, 0x3b, 0x002b);
141 i2c_smbus_write_word_data(client, 0x41, 0x0101);
142 i2c_smbus_write_word_data(client, 0x42, 0x0101);
143 } else if (!noinit) {
144 /* SDRAM setup/hold time */
145 i2c_smbus_write_word_data(client, 0x3a, 0xc040);
146 i2c_smbus_write_word_data(client, 0x41, 0x0000);
147 i2c_smbus_write_word_data(client, 0x41, 0x0101);
148 i2c_smbus_write_word_data(client, 0x42, 0x0101);
151 /* Colour swizzle, AA, no passthrough, backlight */
153 dcon_disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE | MODE_CSWIZZLE;
155 dcon_disp_mode |= MODE_COL_AA;
157 i2c_smbus_write_word_data(client, DCON_REG_MODE, dcon_disp_mode);
160 /* Set the scanline to interrupt on during resume */
161 i2c_smbus_write_word_data(client, DCON_REG_SCAN_INT, resumeline);
168 * The smbus doesn't always come back due to what is believed to be
169 * hardware (power rail) bugs. For older models where this is known to
170 * occur, our solution is to attempt to wait for the bus to stabilize;
171 * if it doesn't happen, cut power to the dcon, repower it, and wait
172 * for the bus to stabilize. Rinse, repeat until we have a working
173 * smbus. For newer models, we simply BUG(); we want to know if this
174 * still happens despite the power fixes that have been made!
176 static int dcon_bus_stabilize(struct i2c_client *client, int is_powered_down)
178 unsigned long timeout;
182 if (is_powered_down) {
184 x = olpc_ec_cmd(0x26, (unsigned char *) &x, 1, NULL, 0);
186 printk(KERN_WARNING "olpc-dcon: unable to force dcon "
187 "to power up: %d!\n", x);
190 msleep(10); /* we'll be conservative */
193 pdata->bus_stabilize_wiggle();
195 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
197 x = dcon_read(DCON_REG_ID);
200 printk(KERN_ERR "olpc-dcon: unable to stabilize dcon's "
201 "smbus, reasserting power and praying.\n");
202 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
204 olpc_ec_cmd(0x26, (unsigned char *) &x, 1, NULL, 0);
207 goto power_up; /* argh, stupid hardware.. */
211 return dcon_hw_init(client, 0);
215 static int dcon_get_backlight(void)
217 if (dcon_client == NULL)
221 bl_val = dcon_read(DCON_REG_BRIGHT) & 0x0F;
227 static void dcon_set_backlight_hw(int level)
229 bl_val = level & 0x0F;
230 dcon_write(DCON_REG_BRIGHT, bl_val);
232 /* Purposely turn off the backlight when we go to level 0 */
234 dcon_disp_mode &= ~MODE_BL_ENABLE;
235 dcon_write(DCON_REG_MODE, dcon_disp_mode);
236 } else if (!(dcon_disp_mode & MODE_BL_ENABLE)) {
237 dcon_disp_mode |= MODE_BL_ENABLE;
238 dcon_write(DCON_REG_MODE, dcon_disp_mode);
242 static void dcon_set_backlight(int level)
244 if (dcon_client == NULL)
247 if (bl_val == (level & 0x0F))
250 dcon_set_backlight_hw(level);
253 /* Set the output type to either color or mono */
255 static int dcon_set_output(int arg)
257 if (dcon_output == arg)
262 if (arg == DCON_OUTPUT_MONO) {
263 dcon_disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
264 dcon_disp_mode |= MODE_MONO_LUMA;
266 dcon_disp_mode &= ~(MODE_MONO_LUMA);
267 dcon_disp_mode |= MODE_CSWIZZLE;
269 dcon_disp_mode |= MODE_COL_AA;
272 dcon_write(DCON_REG_MODE, dcon_disp_mode);
276 /* For now, this will be really stupid - we need to address how
277 * DCONLOAD works in a sleep and account for it accordingly
280 static void dcon_sleep(int state)
284 /* Turn off the backlight and put the DCON to sleep */
286 if (state == dcon_sleep_val)
289 if (!olpc_board_at_least(olpc_board(0xc2)))
292 if (state == DCON_SLEEP) {
294 x = olpc_ec_cmd(0x26, (unsigned char *) &x, 1, NULL, 0);
296 printk(KERN_WARNING "olpc-dcon: unable to force dcon "
297 "to power down: %d!\n", x);
299 dcon_sleep_val = state;
301 /* Only re-enable the backlight if the backlight value is set */
303 dcon_disp_mode |= MODE_BL_ENABLE;
304 x = dcon_bus_stabilize(dcon_client, 1);
306 printk(KERN_WARNING "olpc-dcon: unable to reinit dcon"
307 " hardware: %d!\n", x);
309 dcon_sleep_val = state;
311 /* Restore backlight */
312 dcon_set_backlight_hw(bl_val);
315 /* We should turn off some stuff in the framebuffer - but what? */
318 /* the DCON seems to get confused if we change DCONLOAD too
319 * frequently -- i.e., approximately faster than frame time.
320 * normally we don't change it this fast, so in general we won't
323 void dcon_load_holdoff(void)
325 struct timespec delta_t, now;
327 getnstimeofday(&now);
328 delta_t = timespec_sub(now, dcon_load_time);
329 if (delta_t.tv_sec != 0 ||
330 delta_t.tv_nsec > NSEC_PER_MSEC * 20) {
336 /* Set the source of the display (CPU or DCON) */
338 static void dcon_source_switch(struct work_struct *work)
340 DECLARE_WAITQUEUE(wait, current);
341 int source = dcon_pending;
343 if (dcon_source == source)
351 case DCON_SOURCE_CPU:
352 printk("dcon_source_switch to CPU\n");
353 /* Enable the scanline interrupt bit */
354 if (dcon_write(DCON_REG_MODE, dcon_disp_mode | MODE_SCAN_INT))
356 "olpc-dcon: couldn't enable scanline interrupt!\n");
358 /* Wait up to one second for the scanline interrupt */
359 wait_event_timeout(dcon_wait_queue,
360 dcon_switched == 1, HZ);
364 printk(KERN_ERR "olpc-dcon: Timeout entering CPU mode; expect a screen glitch.\n");
366 /* Turn off the scanline interrupt */
367 if (dcon_write(DCON_REG_MODE, dcon_disp_mode))
368 printk(KERN_ERR "olpc-dcon: couldn't disable scanline interrupt!\n");
371 * Ideally we'd like to disable interrupts here so that the
372 * fb unblanking and DCON turn on happen at a known time value;
373 * however, we can't do that right now with fb_blank
374 * messing with semaphores.
376 * For now, we just hope..
379 ignore_fb_events = 1;
380 if (fb_blank(fbinfo, FB_BLANK_UNBLANK)) {
381 ignore_fb_events = 0;
383 printk(KERN_ERR "olpc-dcon: Failed to enter CPU mode\n");
384 dcon_pending = DCON_SOURCE_DCON;
387 ignore_fb_events = 0;
390 /* And turn off the DCON */
391 pdata->set_dconload(1);
392 getnstimeofday(&dcon_load_time);
394 printk(KERN_INFO "olpc-dcon: The CPU has control\n");
396 case DCON_SOURCE_DCON:
399 struct timespec delta_t;
401 printk(KERN_INFO "dcon_source_switch to DCON\n");
403 add_wait_queue(&dcon_wait_queue, &wait);
404 set_current_state(TASK_UNINTERRUPTIBLE);
406 /* Clear DCONLOAD - this implies that the DCON is in control */
407 pdata->set_dconload(0);
408 getnstimeofday(&dcon_load_time);
410 t = schedule_timeout(HZ/2);
411 remove_wait_queue(&dcon_wait_queue, &wait);
412 set_current_state(TASK_RUNNING);
414 if (!dcon_switched) {
415 printk(KERN_ERR "olpc-dcon: Timeout entering DCON mode; expect a screen glitch.\n");
417 /* sometimes the DCON doesn't follow its own rules,
418 * and doesn't wait for two vsync pulses before
419 * ack'ing the frame load with an IRQ. the result
420 * is that the display shows the *previously*
421 * loaded frame. we can detect this by looking at
422 * the time between asserting DCONLOAD and the IRQ --
423 * if it's less than 20msec, then the DCON couldn't
424 * have seen two VSYNC pulses. in that case we
425 * deassert and reassert, and hope for the best.
426 * see http://dev.laptop.org/ticket/9664
428 delta_t = timespec_sub(dcon_irq_time, dcon_load_time);
429 if (dcon_switched && delta_t.tv_sec == 0 &&
430 delta_t.tv_nsec < NSEC_PER_MSEC * 20) {
431 printk(KERN_ERR "olpc-dcon: missed loading, retrying\n");
432 pdata->set_dconload(1);
434 pdata->set_dconload(0);
435 getnstimeofday(&dcon_load_time);
441 ignore_fb_events = 1;
442 if (fb_blank(fbinfo, FB_BLANK_POWERDOWN))
443 printk(KERN_ERR "olpc-dcon: couldn't blank fb!\n");
444 ignore_fb_events = 0;
447 printk(KERN_INFO "olpc-dcon: The DCON has control\n");
454 dcon_source = source;
457 static DECLARE_WORK(dcon_work, dcon_source_switch);
459 static void dcon_set_source(int arg)
461 if (dcon_pending == arg)
466 if ((dcon_source != arg) && !work_pending(&dcon_work))
467 schedule_work(&dcon_work);
470 static void dcon_set_source_sync(int arg)
472 dcon_set_source(arg);
473 flush_scheduled_work();
476 static int dconbl_set(struct backlight_device *dev)
479 int level = dev->props.brightness;
481 if (dev->props.power != FB_BLANK_UNBLANK)
484 dcon_set_backlight(level);
488 static int dconbl_get(struct backlight_device *dev)
490 return dcon_get_backlight();
493 static ssize_t dcon_mode_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
496 return sprintf(buf, "%4.4X\n", dcon_disp_mode);
499 static ssize_t dcon_sleep_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
503 return sprintf(buf, "%d\n", dcon_sleep_val);
506 static ssize_t dcon_freeze_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 return sprintf(buf, "%d\n", dcon_source == DCON_SOURCE_DCON ? 1 : 0);
512 static ssize_t dcon_output_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 return sprintf(buf, "%d\n", dcon_output);
518 static ssize_t dcon_resumeline_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 return sprintf(buf, "%d\n", resumeline);
524 static int _strtoul(const char *buf, int len, unsigned int *val)
528 unsigned int output = simple_strtoul(buf, &endp, 0);
529 int size = endp - buf;
531 if (*endp && isspace(*endp))
541 static ssize_t dcon_output_store(struct device *dev,
542 struct device_attribute *attr, const char *buf, size_t count)
547 if (_strtoul(buf, count, &output))
550 if (output == DCON_OUTPUT_COLOR || output == DCON_OUTPUT_MONO) {
551 dcon_set_output(output);
558 static ssize_t dcon_freeze_store(struct device *dev,
559 struct device_attribute *attr, const char *buf, size_t count)
563 if (_strtoul(buf, count, &output))
566 printk(KERN_INFO "dcon_freeze_store: %d\n", output);
570 dcon_set_source(DCON_SOURCE_CPU);
573 dcon_set_source_sync(DCON_SOURCE_DCON);
575 case 2: /* normally unused */
576 dcon_set_source(DCON_SOURCE_DCON);
585 static ssize_t dcon_resumeline_store(struct device *dev,
586 struct device_attribute *attr, const char *buf, size_t count)
591 if (_strtoul(buf, count, &rl))
595 dcon_write(DCON_REG_SCAN_INT, resumeline);
601 static ssize_t dcon_sleep_store(struct device *dev,
602 struct device_attribute *attr, const char *buf, size_t count)
606 if (_strtoul(buf, count, &output))
609 dcon_sleep(output ? DCON_SLEEP : DCON_ACTIVE);
613 static struct device_attribute dcon_device_files[] = {
614 __ATTR(mode, 0444, dcon_mode_show, NULL),
615 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
616 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
617 __ATTR(output, 0644, dcon_output_show, dcon_output_store),
618 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
621 static const struct backlight_ops dcon_bl_ops = {
622 .get_brightness = dconbl_get,
623 .update_status = dconbl_set
627 static int dcon_reboot_notify(struct notifier_block *nb,
628 unsigned long foo, void *bar)
630 if (dcon_client == NULL)
633 /* Turn off the DCON. Entirely. */
634 dcon_write(DCON_REG_MODE, 0x39);
635 dcon_write(DCON_REG_MODE, 0x32);
639 static struct notifier_block dcon_nb = {
640 .notifier_call = dcon_reboot_notify,
644 static int unfreeze_on_panic(struct notifier_block *nb,
645 unsigned long e, void *p)
647 pdata->set_dconload(1);
651 static struct notifier_block dcon_panic_nb = {
652 .notifier_call = unfreeze_on_panic,
656 * When the framebuffer sleeps due to external sources (e.g. user idle), power
657 * down the DCON as well. Power it back up when the fb comes back to life.
659 static int fb_notifier_callback(struct notifier_block *self,
660 unsigned long event, void *data)
662 struct fb_event *evdata = data;
663 int *blank = (int *) evdata->data;
664 if (((event != FB_EVENT_BLANK) && (event != FB_EVENT_CONBLANK)) ||
667 dcon_sleep((*blank) ? DCON_SLEEP : DCON_ACTIVE);
671 static struct notifier_block fb_nb = {
672 .notifier_call = fb_notifier_callback,
675 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
677 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
682 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
686 if (num_registered_fb >= 1)
687 fbinfo = registered_fb[0];
689 rc = dcon_hw_init(client, 1);
693 /* Add the DCON device */
695 dcon_device = platform_device_alloc("dcon", -1);
697 if (dcon_device == NULL) {
698 printk(KERN_ERR "dcon: Unable to create the DCON device\n");
703 i2c_set_clientdata(client, dcon_device);
704 rc = platform_device_add(dcon_device);
707 printk(KERN_ERR "dcon: Unable to add the DCON device\n");
711 for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
712 rc = device_create_file(&dcon_device->dev,
713 &dcon_device_files[i]);
715 dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
720 /* Add the backlight device for the DCON */
722 dcon_client = client;
724 dcon_bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
725 NULL, &dcon_bl_ops, NULL);
727 if (IS_ERR(dcon_bl_dev)) {
728 printk(KERN_ERR "Cannot register the backlight device (%ld)\n",
729 PTR_ERR(dcon_bl_dev));
732 dcon_bl_dev->props.max_brightness = 15;
733 dcon_bl_dev->props.power = FB_BLANK_UNBLANK;
734 dcon_bl_dev->props.brightness = dcon_get_backlight();
736 backlight_update_status(dcon_bl_dev);
739 register_reboot_notifier(&dcon_nb);
740 atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
741 fb_register_client(&fb_nb);
746 for (j = 0; j < i; j++)
747 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
749 platform_device_unregister(dcon_device);
752 free_irq(DCON_IRQ, &dcon_driver);
757 static int dcon_remove(struct i2c_client *client)
761 fb_unregister_client(&fb_nb);
762 unregister_reboot_notifier(&dcon_nb);
763 atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
765 free_irq(DCON_IRQ, &dcon_driver);
767 if (dcon_bl_dev != NULL)
768 backlight_device_unregister(dcon_bl_dev);
770 if (dcon_device != NULL)
771 platform_device_unregister(dcon_device);
772 cancel_work_sync(&dcon_work);
778 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
780 if (dcon_sleep_val == DCON_ACTIVE) {
781 /* Set up the DCON to have the source */
782 dcon_set_source_sync(DCON_SOURCE_DCON);
788 static int dcon_resume(struct i2c_client *client)
790 if (dcon_sleep_val == DCON_ACTIVE) {
791 dcon_bus_stabilize(client, 0);
792 dcon_set_source(DCON_SOURCE_CPU);
801 static irqreturn_t dcon_interrupt(int irq, void *id)
803 int status = pdata->read_status();
808 switch (status & 3) {
810 printk(KERN_DEBUG "olpc-dcon: DCONLOAD_MISSED interrupt\n");
813 case 2: /* switch to DCON mode */
814 case 1: /* switch to CPU mode */
816 getnstimeofday(&dcon_irq_time);
817 wake_up(&dcon_wait_queue);
821 /* workaround resume case: the DCON (on 1.5) doesn't
822 * ever assert status 0x01 when switching to CPU mode
823 * during resume. this is because DCONLOAD is de-asserted
824 * _immediately_ upon exiting S3, so the actual release
825 * of the DCON happened long before this point.
826 * see http://dev.laptop.org/ticket/9869
828 if (dcon_source != dcon_pending && !dcon_switched) {
830 getnstimeofday(&dcon_irq_time);
831 wake_up(&dcon_wait_queue);
832 printk(KERN_DEBUG "olpc-dcon: switching w/ status 0/0\n");
834 printk(KERN_DEBUG "olpc-dcon: scanline interrupt w/CPU\n");
841 static struct i2c_device_id dcon_idtable[] = {
846 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
848 static struct i2c_driver dcon_driver = {
852 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
853 .id_table = dcon_idtable,
855 .remove = __devexit_p(dcon_remove),
856 .detect = dcon_detect,
857 .address_list = normal_i2c,
859 .suspend = dcon_suspend,
860 .resume = dcon_resume,
864 #include "olpc_dcon_xo_1.c"
866 static int __init olpc_dcon_init(void)
868 pdata = &dcon_pdata_xo_1;
870 i2c_add_driver(&dcon_driver);
874 static void __exit olpc_dcon_exit(void)
876 i2c_del_driver(&dcon_driver);
879 module_init(olpc_dcon_init);
880 module_exit(olpc_dcon_exit);
882 MODULE_LICENSE("GPL");