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-2011 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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.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>
35 #include "olpc_dcon.h"
37 /* Module definitions */
39 static ushort resumeline = 898;
40 module_param(resumeline, ushort, 0444);
42 /* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
44 module_param(useaa, int, 0444);
46 static struct dcon_platform_data *pdata;
50 /* Platform devices */
51 static struct platform_device *dcon_device;
53 static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
55 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
57 static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
59 return i2c_smbus_write_word_data(dcon->client, reg, val);
62 static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
64 return i2c_smbus_read_word_data(dcon->client, reg);
67 /* ===== API functions - these are called by a variety of users ==== */
69 static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
74 ver = dcon_read(dcon, DCON_REG_ID);
75 if ((ver >> 8) != 0xDC) {
76 pr_err("DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
82 pr_info("Discovered DCON version %x\n", ver & 0xFF);
83 rc = pdata->init(dcon);
85 pr_err("Unable to init.\n");
91 dev_err(&dcon->client->dev,
92 "DCON v1 is unsupported, giving up..\n");
97 /* SDRAM setup/hold time */
98 dcon_write(dcon, 0x3a, 0xc040);
99 dcon_write(dcon, 0x41, 0x0000);
100 dcon_write(dcon, 0x41, 0x0101);
101 dcon_write(dcon, 0x42, 0x0101);
103 /* Colour swizzle, AA, no passthrough, backlight */
105 dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
108 dcon->disp_mode |= MODE_COL_AA;
110 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
113 /* Set the scanline to interrupt on during resume */
114 dcon_write(dcon, DCON_REG_SCAN_INT, resumeline);
121 * The smbus doesn't always come back due to what is believed to be
122 * hardware (power rail) bugs. For older models where this is known to
123 * occur, our solution is to attempt to wait for the bus to stabilize;
124 * if it doesn't happen, cut power to the dcon, repower it, and wait
125 * for the bus to stabilize. Rinse, repeat until we have a working
126 * smbus. For newer models, we simply BUG(); we want to know if this
127 * still happens despite the power fixes that have been made!
129 static int dcon_bus_stabilize(struct dcon_priv *dcon, int is_powered_down)
131 unsigned long timeout;
135 if (is_powered_down) {
137 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
139 pr_warn("unable to force dcon to power up: %d!\n", x);
142 msleep(10); /* we'll be conservative */
145 pdata->bus_stabilize_wiggle();
147 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
149 x = dcon_read(dcon, DCON_REG_ID);
152 pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
153 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
155 olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
158 goto power_up; /* argh, stupid hardware.. */
162 return dcon_hw_init(dcon, 0);
166 static void dcon_set_backlight(struct dcon_priv *dcon, u8 level)
168 dcon->bl_val = level;
169 dcon_write(dcon, DCON_REG_BRIGHT, dcon->bl_val);
171 /* Purposely turn off the backlight when we go to level 0 */
172 if (dcon->bl_val == 0) {
173 dcon->disp_mode &= ~MODE_BL_ENABLE;
174 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
175 } else if (!(dcon->disp_mode & MODE_BL_ENABLE)) {
176 dcon->disp_mode |= MODE_BL_ENABLE;
177 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
181 /* Set the output type to either color or mono */
182 static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
184 if (dcon->mono == enable_mono)
187 dcon->mono = enable_mono;
190 dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
191 dcon->disp_mode |= MODE_MONO_LUMA;
193 dcon->disp_mode &= ~(MODE_MONO_LUMA);
194 dcon->disp_mode |= MODE_CSWIZZLE;
196 dcon->disp_mode |= MODE_COL_AA;
199 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
203 /* For now, this will be really stupid - we need to address how
204 * DCONLOAD works in a sleep and account for it accordingly
207 static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
211 /* Turn off the backlight and put the DCON to sleep */
213 if (dcon->asleep == sleep)
216 if (!olpc_board_at_least(olpc_board(0xc2)))
221 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
223 pr_warn("unable to force dcon to power down: %d!\n", x);
225 dcon->asleep = sleep;
227 /* Only re-enable the backlight if the backlight value is set */
228 if (dcon->bl_val != 0)
229 dcon->disp_mode |= MODE_BL_ENABLE;
230 x = dcon_bus_stabilize(dcon, 1);
232 pr_warn("unable to reinit dcon hardware: %d!\n", x);
234 dcon->asleep = sleep;
236 /* Restore backlight */
237 dcon_set_backlight(dcon, dcon->bl_val);
240 /* We should turn off some stuff in the framebuffer - but what? */
243 /* the DCON seems to get confused if we change DCONLOAD too
244 * frequently -- i.e., approximately faster than frame time.
245 * normally we don't change it this fast, so in general we won't
248 static void dcon_load_holdoff(struct dcon_priv *dcon)
250 struct timespec delta_t, now;
252 getnstimeofday(&now);
253 delta_t = timespec_sub(now, dcon->load_time);
254 if (delta_t.tv_sec != 0 ||
255 delta_t.tv_nsec > NSEC_PER_MSEC * 20) {
262 static bool dcon_blank_fb(struct dcon_priv *dcon, bool blank)
266 if (!lock_fb_info(dcon->fbinfo)) {
267 dev_err(&dcon->client->dev, "unable to lock framebuffer\n");
271 dcon->ignore_fb_events = true;
272 err = fb_blank(dcon->fbinfo,
273 blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
274 dcon->ignore_fb_events = false;
276 unlock_fb_info(dcon->fbinfo);
279 dev_err(&dcon->client->dev, "couldn't %sblank framebuffer\n",
286 /* Set the source of the display (CPU or DCON) */
287 static void dcon_source_switch(struct work_struct *work)
289 struct dcon_priv *dcon = container_of(work, struct dcon_priv,
291 DECLARE_WAITQUEUE(wait, current);
292 int source = dcon->pending_src;
294 if (dcon->curr_src == source)
297 dcon_load_holdoff(dcon);
299 dcon->switched = false;
302 case DCON_SOURCE_CPU:
303 pr_info("dcon_source_switch to CPU\n");
304 /* Enable the scanline interrupt bit */
305 if (dcon_write(dcon, DCON_REG_MODE,
306 dcon->disp_mode | MODE_SCAN_INT))
307 pr_err("couldn't enable scanline interrupt!\n");
309 /* Wait up to one second for the scanline interrupt */
310 wait_event_timeout(dcon_wait_queue,
311 dcon->switched == true, HZ);
315 pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
317 /* Turn off the scanline interrupt */
318 if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
319 pr_err("couldn't disable scanline interrupt!\n");
322 * Ideally we'd like to disable interrupts here so that the
323 * fb unblanking and DCON turn on happen at a known time value;
324 * however, we can't do that right now with fb_blank
325 * messing with semaphores.
327 * For now, we just hope..
329 if (!dcon_blank_fb(dcon, false)) {
330 pr_err("Failed to enter CPU mode\n");
331 dcon->pending_src = DCON_SOURCE_DCON;
335 /* And turn off the DCON */
336 pdata->set_dconload(1);
337 getnstimeofday(&dcon->load_time);
339 pr_info("The CPU has control\n");
341 case DCON_SOURCE_DCON:
344 struct timespec delta_t;
346 pr_info("dcon_source_switch to DCON\n");
348 add_wait_queue(&dcon_wait_queue, &wait);
349 set_current_state(TASK_UNINTERRUPTIBLE);
351 /* Clear DCONLOAD - this implies that the DCON is in control */
352 pdata->set_dconload(0);
353 getnstimeofday(&dcon->load_time);
355 t = schedule_timeout(HZ/2);
356 remove_wait_queue(&dcon_wait_queue, &wait);
357 set_current_state(TASK_RUNNING);
359 if (!dcon->switched) {
360 pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
362 /* sometimes the DCON doesn't follow its own rules,
363 * and doesn't wait for two vsync pulses before
364 * ack'ing the frame load with an IRQ. the result
365 * is that the display shows the *previously*
366 * loaded frame. we can detect this by looking at
367 * the time between asserting DCONLOAD and the IRQ --
368 * if it's less than 20msec, then the DCON couldn't
369 * have seen two VSYNC pulses. in that case we
370 * deassert and reassert, and hope for the best.
371 * see http://dev.laptop.org/ticket/9664
373 delta_t = timespec_sub(dcon->irq_time, dcon->load_time);
374 if (dcon->switched && delta_t.tv_sec == 0 &&
375 delta_t.tv_nsec < NSEC_PER_MSEC * 20) {
376 pr_err("missed loading, retrying\n");
377 pdata->set_dconload(1);
379 pdata->set_dconload(0);
380 getnstimeofday(&dcon->load_time);
385 dcon_blank_fb(dcon, true);
386 pr_info("The DCON has control\n");
393 dcon->curr_src = source;
396 static void dcon_set_source(struct dcon_priv *dcon, int arg)
398 if (dcon->pending_src == arg)
401 dcon->pending_src = arg;
403 if ((dcon->curr_src != arg) && !work_pending(&dcon->switch_source))
404 schedule_work(&dcon->switch_source);
407 static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
409 dcon_set_source(dcon, arg);
410 flush_scheduled_work();
413 static ssize_t dcon_mode_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct dcon_priv *dcon = dev_get_drvdata(dev);
417 return sprintf(buf, "%4.4X\n", dcon->disp_mode);
420 static ssize_t dcon_sleep_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
424 struct dcon_priv *dcon = dev_get_drvdata(dev);
425 return sprintf(buf, "%d\n", dcon->asleep);
428 static ssize_t dcon_freeze_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct dcon_priv *dcon = dev_get_drvdata(dev);
432 return sprintf(buf, "%d\n", dcon->curr_src == DCON_SOURCE_DCON ? 1 : 0);
435 static ssize_t dcon_mono_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 struct dcon_priv *dcon = dev_get_drvdata(dev);
439 return sprintf(buf, "%d\n", dcon->mono);
442 static ssize_t dcon_resumeline_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 return sprintf(buf, "%d\n", resumeline);
448 static ssize_t dcon_mono_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t count)
451 unsigned long enable_mono;
454 rc = kstrtoul(buf, 10, &enable_mono);
458 dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
463 static ssize_t dcon_freeze_store(struct device *dev,
464 struct device_attribute *attr, const char *buf, size_t count)
466 struct dcon_priv *dcon = dev_get_drvdata(dev);
467 unsigned long output;
470 ret = kstrtoul(buf, 10, &output);
474 pr_info("dcon_freeze_store: %lu\n", output);
478 dcon_set_source(dcon, DCON_SOURCE_CPU);
481 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
483 case 2: /* normally unused */
484 dcon_set_source(dcon, DCON_SOURCE_DCON);
493 static ssize_t dcon_resumeline_store(struct device *dev,
494 struct device_attribute *attr, const char *buf, size_t count)
499 rc = kstrtou16(buf, 10, &rl);
504 dcon_write(dev_get_drvdata(dev), DCON_REG_SCAN_INT, resumeline);
509 static ssize_t dcon_sleep_store(struct device *dev,
510 struct device_attribute *attr, const char *buf, size_t count)
512 unsigned long output;
515 ret = kstrtoul(buf, 10, &output);
519 dcon_sleep(dev_get_drvdata(dev), output ? true : false);
523 static struct device_attribute dcon_device_files[] = {
524 __ATTR(mode, 0444, dcon_mode_show, NULL),
525 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
526 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
527 __ATTR(monochrome, 0644, dcon_mono_show, dcon_mono_store),
528 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
531 static int dcon_bl_update(struct backlight_device *dev)
533 struct dcon_priv *dcon = bl_get_data(dev);
534 u8 level = dev->props.brightness & 0x0F;
536 if (dev->props.power != FB_BLANK_UNBLANK)
539 if (level != dcon->bl_val)
540 dcon_set_backlight(dcon, level);
545 static int dcon_bl_get(struct backlight_device *dev)
547 struct dcon_priv *dcon = bl_get_data(dev);
551 static const struct backlight_ops dcon_bl_ops = {
552 .update_status = dcon_bl_update,
553 .get_brightness = dcon_bl_get,
556 static struct backlight_properties dcon_bl_props = {
557 .max_brightness = 15,
558 .type = BACKLIGHT_RAW,
559 .power = FB_BLANK_UNBLANK,
562 static int dcon_reboot_notify(struct notifier_block *nb,
563 unsigned long foo, void *bar)
565 struct dcon_priv *dcon = container_of(nb, struct dcon_priv, reboot_nb);
567 if (!dcon || !dcon->client)
570 /* Turn off the DCON. Entirely. */
571 dcon_write(dcon, DCON_REG_MODE, 0x39);
572 dcon_write(dcon, DCON_REG_MODE, 0x32);
576 static int unfreeze_on_panic(struct notifier_block *nb,
577 unsigned long e, void *p)
579 pdata->set_dconload(1);
583 static struct notifier_block dcon_panic_nb = {
584 .notifier_call = unfreeze_on_panic,
588 * When the framebuffer sleeps due to external sources (e.g. user idle), power
589 * down the DCON as well. Power it back up when the fb comes back to life.
591 static int dcon_fb_notifier(struct notifier_block *self,
592 unsigned long event, void *data)
594 struct fb_event *evdata = data;
595 struct dcon_priv *dcon = container_of(self, struct dcon_priv,
597 int *blank = (int *)evdata->data;
598 if (((event != FB_EVENT_BLANK) && (event != FB_EVENT_CONBLANK)) ||
599 dcon->ignore_fb_events)
601 dcon_sleep(dcon, *blank ? true : false);
605 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
607 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
612 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
614 struct dcon_priv *dcon;
620 dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
624 dcon->client = client;
625 INIT_WORK(&dcon->switch_source, dcon_source_switch);
626 dcon->reboot_nb.notifier_call = dcon_reboot_notify;
627 dcon->reboot_nb.priority = -1;
628 dcon->fbevent_nb.notifier_call = dcon_fb_notifier;
630 i2c_set_clientdata(client, dcon);
632 if (num_registered_fb < 1) {
633 dev_err(&client->dev, "DCON driver requires a registered fb\n");
637 dcon->fbinfo = registered_fb[0];
639 rc = dcon_hw_init(dcon, 1);
643 /* Add the DCON device */
645 dcon_device = platform_device_alloc("dcon", -1);
647 if (dcon_device == NULL) {
648 pr_err("Unable to create the DCON device\n");
652 rc = platform_device_add(dcon_device);
653 platform_set_drvdata(dcon_device, dcon);
656 pr_err("Unable to add the DCON device\n");
660 for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
661 rc = device_create_file(&dcon_device->dev,
662 &dcon_device_files[i]);
664 dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
669 dcon->bl_val = dcon_read(dcon, DCON_REG_BRIGHT) & 0x0F;
671 /* Add the backlight device for the DCON */
672 dcon_bl_props.brightness = dcon->bl_val;
673 dcon->bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
674 dcon, &dcon_bl_ops, &dcon_bl_props);
675 if (IS_ERR(dcon->bl_dev)) {
676 dev_err(&client->dev, "cannot register backlight dev (%ld)\n",
677 PTR_ERR(dcon->bl_dev));
681 register_reboot_notifier(&dcon->reboot_nb);
682 atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
683 fb_register_client(&dcon->fbevent_nb);
688 for (j = 0; j < i; j++)
689 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
691 platform_device_unregister(dcon_device);
694 free_irq(DCON_IRQ, dcon);
700 static int dcon_remove(struct i2c_client *client)
702 struct dcon_priv *dcon = i2c_get_clientdata(client);
704 fb_unregister_client(&dcon->fbevent_nb);
705 unregister_reboot_notifier(&dcon->reboot_nb);
706 atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
708 free_irq(DCON_IRQ, dcon);
711 backlight_device_unregister(dcon->bl_dev);
713 if (dcon_device != NULL)
714 platform_device_unregister(dcon_device);
715 cancel_work_sync(&dcon->switch_source);
723 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
725 struct dcon_priv *dcon = i2c_get_clientdata(client);
728 /* Set up the DCON to have the source */
729 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
735 static int dcon_resume(struct i2c_client *client)
737 struct dcon_priv *dcon = i2c_get_clientdata(client);
740 dcon_bus_stabilize(dcon, 0);
741 dcon_set_source(dcon, DCON_SOURCE_CPU);
750 irqreturn_t dcon_interrupt(int irq, void *id)
752 struct dcon_priv *dcon = id;
755 if (pdata->read_status(&status))
758 switch (status & 3) {
760 pr_debug("DCONLOAD_MISSED interrupt\n");
763 case 2: /* switch to DCON mode */
764 case 1: /* switch to CPU mode */
765 dcon->switched = true;
766 getnstimeofday(&dcon->irq_time);
767 wake_up(&dcon_wait_queue);
771 /* workaround resume case: the DCON (on 1.5) doesn't
772 * ever assert status 0x01 when switching to CPU mode
773 * during resume. this is because DCONLOAD is de-asserted
774 * _immediately_ upon exiting S3, so the actual release
775 * of the DCON happened long before this point.
776 * see http://dev.laptop.org/ticket/9869
778 if (dcon->curr_src != dcon->pending_src && !dcon->switched) {
779 dcon->switched = true;
780 getnstimeofday(&dcon->irq_time);
781 wake_up(&dcon_wait_queue);
782 pr_debug("switching w/ status 0/0\n");
784 pr_debug("scanline interrupt w/CPU\n");
791 static const struct i2c_device_id dcon_idtable[] = {
796 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
798 struct i2c_driver dcon_driver = {
802 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
803 .id_table = dcon_idtable,
805 .remove = __devexit_p(dcon_remove),
806 .detect = dcon_detect,
807 .address_list = normal_i2c,
809 .suspend = dcon_suspend,
810 .resume = dcon_resume,
814 static int __init olpc_dcon_init(void)
816 #ifdef CONFIG_FB_OLPC_DCON_1_5
818 if (olpc_board_at_least(olpc_board(0xd0)))
819 pdata = &dcon_pdata_xo_1_5;
821 #ifdef CONFIG_FB_OLPC_DCON_1
823 pdata = &dcon_pdata_xo_1;
826 return i2c_add_driver(&dcon_driver);
829 static void __exit olpc_dcon_exit(void)
831 i2c_del_driver(&dcon_driver);
834 module_init(olpc_dcon_init);
835 module_exit(olpc_dcon_exit);
837 MODULE_LICENSE("GPL");