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/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/module.h>
24 #include <linux/backlight.h>
25 #include <linux/device.h>
26 #include <linux/uaccess.h>
27 #include <linux/ctype.h>
28 #include <linux/reboot.h>
29 #include <linux/olpc-ec.h>
33 #include "olpc_dcon.h"
35 /* Module definitions */
37 static ushort resumeline = 898;
38 module_param(resumeline, ushort, 0444);
40 static struct dcon_platform_data *pdata;
44 /* Platform devices */
45 static struct platform_device *dcon_device;
47 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
49 static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
51 return i2c_smbus_write_word_data(dcon->client, reg, val);
54 static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
56 return i2c_smbus_read_word_data(dcon->client, reg);
59 /* ===== API functions - these are called by a variety of users ==== */
61 static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
66 ver = dcon_read(dcon, DCON_REG_ID);
67 if ((ver >> 8) != 0xDC) {
68 pr_err("DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
74 pr_info("Discovered DCON version %x\n", ver & 0xFF);
75 rc = pdata->init(dcon);
77 pr_err("Unable to init.\n");
83 dev_err(&dcon->client->dev,
84 "DCON v1 is unsupported, giving up..\n");
89 /* SDRAM setup/hold time */
90 dcon_write(dcon, 0x3a, 0xc040);
91 dcon_write(dcon, DCON_REG_MEM_OPT_A, 0x0000); /* clear option bits */
92 dcon_write(dcon, DCON_REG_MEM_OPT_A,
93 MEM_DLL_CLOCK_DELAY | MEM_POWER_DOWN);
94 dcon_write(dcon, DCON_REG_MEM_OPT_B, MEM_SOFT_RESET);
96 /* Colour swizzle, AA, no passthrough, backlight */
98 dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
99 MODE_CSWIZZLE | MODE_COL_AA;
101 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
103 /* Set the scanline to interrupt on during resume */
104 dcon_write(dcon, DCON_REG_SCAN_INT, resumeline);
111 * The smbus doesn't always come back due to what is believed to be
112 * hardware (power rail) bugs. For older models where this is known to
113 * occur, our solution is to attempt to wait for the bus to stabilize;
114 * if it doesn't happen, cut power to the dcon, repower it, and wait
115 * for the bus to stabilize. Rinse, repeat until we have a working
116 * smbus. For newer models, we simply BUG(); we want to know if this
117 * still happens despite the power fixes that have been made!
119 static int dcon_bus_stabilize(struct dcon_priv *dcon, int is_powered_down)
121 unsigned long timeout;
126 if (is_powered_down) {
128 x = olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
130 pr_warn("unable to force dcon to power up: %d!\n", x);
133 usleep_range(10000, 11000); /* we'll be conservative */
136 pdata->bus_stabilize_wiggle();
138 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
139 usleep_range(1000, 1100);
140 x = dcon_read(dcon, DCON_REG_ID);
143 pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
144 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
146 olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
149 goto power_up; /* argh, stupid hardware.. */
153 return dcon_hw_init(dcon, 0);
157 static void dcon_set_backlight(struct dcon_priv *dcon, u8 level)
159 dcon->bl_val = level;
160 dcon_write(dcon, DCON_REG_BRIGHT, dcon->bl_val);
162 /* Purposely turn off the backlight when we go to level 0 */
163 if (dcon->bl_val == 0) {
164 dcon->disp_mode &= ~MODE_BL_ENABLE;
165 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
166 } else if (!(dcon->disp_mode & MODE_BL_ENABLE)) {
167 dcon->disp_mode |= MODE_BL_ENABLE;
168 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
172 /* Set the output type to either color or mono */
173 static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
175 if (dcon->mono == enable_mono)
178 dcon->mono = enable_mono;
181 dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
182 dcon->disp_mode |= MODE_MONO_LUMA;
184 dcon->disp_mode &= ~(MODE_MONO_LUMA);
185 dcon->disp_mode |= MODE_CSWIZZLE | MODE_COL_AA;
188 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
192 /* For now, this will be really stupid - we need to address how
193 * DCONLOAD works in a sleep and account for it accordingly
196 static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
200 /* Turn off the backlight and put the DCON to sleep */
202 if (dcon->asleep == sleep)
205 if (!olpc_board_at_least(olpc_board(0xc2)))
211 x = olpc_ec_cmd(EC_DCON_POWER_MODE, &pm, 1, NULL, 0);
213 pr_warn("unable to force dcon to power down: %d!\n", x);
215 dcon->asleep = sleep;
217 /* Only re-enable the backlight if the backlight value is set */
218 if (dcon->bl_val != 0)
219 dcon->disp_mode |= MODE_BL_ENABLE;
220 x = dcon_bus_stabilize(dcon, 1);
222 pr_warn("unable to reinit dcon hardware: %d!\n", x);
224 dcon->asleep = sleep;
226 /* Restore backlight */
227 dcon_set_backlight(dcon, dcon->bl_val);
230 /* We should turn off some stuff in the framebuffer - but what? */
233 /* the DCON seems to get confused if we change DCONLOAD too
234 * frequently -- i.e., approximately faster than frame time.
235 * normally we don't change it this fast, so in general we won't
238 static void dcon_load_holdoff(struct dcon_priv *dcon)
240 ktime_t delta_t, now;
244 delta_t = ktime_sub(now, dcon->load_time);
245 if (ktime_to_ns(delta_t) > NSEC_PER_MSEC * 20)
251 static bool dcon_blank_fb(struct dcon_priv *dcon, bool blank)
256 if (!lock_fb_info(dcon->fbinfo)) {
258 dev_err(&dcon->client->dev, "unable to lock framebuffer\n");
262 dcon->ignore_fb_events = true;
263 err = fb_blank(dcon->fbinfo,
264 blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
265 dcon->ignore_fb_events = false;
266 unlock_fb_info(dcon->fbinfo);
270 dev_err(&dcon->client->dev, "couldn't %sblank framebuffer\n",
277 /* Set the source of the display (CPU or DCON) */
278 static void dcon_source_switch(struct work_struct *work)
280 struct dcon_priv *dcon = container_of(work, struct dcon_priv,
282 int source = dcon->pending_src;
284 if (dcon->curr_src == source)
287 dcon_load_holdoff(dcon);
289 dcon->switched = false;
292 case DCON_SOURCE_CPU:
293 pr_info("%s to CPU\n", __func__);
294 /* Enable the scanline interrupt bit */
295 if (dcon_write(dcon, DCON_REG_MODE,
296 dcon->disp_mode | MODE_SCAN_INT))
297 pr_err("couldn't enable scanline interrupt!\n");
299 /* Wait up to one second for the scanline interrupt */
300 wait_event_timeout(dcon->waitq, dcon->switched, HZ);
303 pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
305 /* Turn off the scanline interrupt */
306 if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
307 pr_err("couldn't disable scanline interrupt!\n");
310 * Ideally we'd like to disable interrupts here so that the
311 * fb unblanking and DCON turn on happen at a known time value;
312 * however, we can't do that right now with fb_blank
313 * messing with semaphores.
315 * For now, we just hope..
317 if (!dcon_blank_fb(dcon, false)) {
318 pr_err("Failed to enter CPU mode\n");
319 dcon->pending_src = DCON_SOURCE_DCON;
323 /* And turn off the DCON */
324 pdata->set_dconload(1);
325 dcon->load_time = ktime_get();
327 pr_info("The CPU has control\n");
329 case DCON_SOURCE_DCON:
333 pr_info("%s to DCON\n", __func__);
335 /* Clear DCONLOAD - this implies that the DCON is in control */
336 pdata->set_dconload(0);
337 dcon->load_time = ktime_get();
339 wait_event_timeout(dcon->waitq, dcon->switched, HZ / 2);
341 if (!dcon->switched) {
342 pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
344 /* sometimes the DCON doesn't follow its own rules,
345 * and doesn't wait for two vsync pulses before
346 * ack'ing the frame load with an IRQ. the result
347 * is that the display shows the *previously*
348 * loaded frame. we can detect this by looking at
349 * the time between asserting DCONLOAD and the IRQ --
350 * if it's less than 20msec, then the DCON couldn't
351 * have seen two VSYNC pulses. in that case we
352 * deassert and reassert, and hope for the best.
353 * see http://dev.laptop.org/ticket/9664
355 delta_t = ktime_sub(dcon->irq_time, dcon->load_time);
356 if (dcon->switched && ktime_to_ns(delta_t)
357 < NSEC_PER_MSEC * 20) {
358 pr_err("missed loading, retrying\n");
359 pdata->set_dconload(1);
361 pdata->set_dconload(0);
362 dcon->load_time = ktime_get();
367 dcon_blank_fb(dcon, true);
368 pr_info("The DCON has control\n");
375 dcon->curr_src = source;
378 static void dcon_set_source(struct dcon_priv *dcon, int arg)
380 if (dcon->pending_src == arg)
383 dcon->pending_src = arg;
385 if (dcon->curr_src != arg)
386 schedule_work(&dcon->switch_source);
389 static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
391 dcon_set_source(dcon, arg);
392 flush_scheduled_work();
395 static ssize_t dcon_mode_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
398 struct dcon_priv *dcon = dev_get_drvdata(dev);
400 return sprintf(buf, "%4.4X\n", dcon->disp_mode);
403 static ssize_t dcon_sleep_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct dcon_priv *dcon = dev_get_drvdata(dev);
408 return sprintf(buf, "%d\n", dcon->asleep);
411 static ssize_t dcon_freeze_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct dcon_priv *dcon = dev_get_drvdata(dev);
416 return sprintf(buf, "%d\n", dcon->curr_src == DCON_SOURCE_DCON ? 1 : 0);
419 static ssize_t dcon_mono_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct dcon_priv *dcon = dev_get_drvdata(dev);
424 return sprintf(buf, "%d\n", dcon->mono);
427 static ssize_t dcon_resumeline_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
430 return sprintf(buf, "%d\n", resumeline);
433 static ssize_t dcon_mono_store(struct device *dev,
434 struct device_attribute *attr, const char *buf, size_t count)
436 unsigned long enable_mono;
439 rc = kstrtoul(buf, 10, &enable_mono);
443 dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
448 static ssize_t dcon_freeze_store(struct device *dev,
449 struct device_attribute *attr, const char *buf, size_t count)
451 struct dcon_priv *dcon = dev_get_drvdata(dev);
452 unsigned long output;
455 ret = kstrtoul(buf, 10, &output);
461 dcon_set_source(dcon, DCON_SOURCE_CPU);
464 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
466 case 2: /* normally unused */
467 dcon_set_source(dcon, DCON_SOURCE_DCON);
476 static ssize_t dcon_resumeline_store(struct device *dev,
477 struct device_attribute *attr, const char *buf, size_t count)
482 rc = kstrtou16(buf, 10, &rl);
487 dcon_write(dev_get_drvdata(dev), DCON_REG_SCAN_INT, resumeline);
492 static ssize_t dcon_sleep_store(struct device *dev,
493 struct device_attribute *attr, const char *buf, size_t count)
495 unsigned long output;
498 ret = kstrtoul(buf, 10, &output);
502 dcon_sleep(dev_get_drvdata(dev), output ? true : false);
506 static struct device_attribute dcon_device_files[] = {
507 __ATTR(mode, 0444, dcon_mode_show, NULL),
508 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
509 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
510 __ATTR(monochrome, 0644, dcon_mono_show, dcon_mono_store),
511 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
514 static int dcon_bl_update(struct backlight_device *dev)
516 struct dcon_priv *dcon = bl_get_data(dev);
517 u8 level = dev->props.brightness & 0x0F;
519 if (dev->props.power != FB_BLANK_UNBLANK)
522 if (level != dcon->bl_val)
523 dcon_set_backlight(dcon, level);
525 /* power down the DCON when the screen is blanked */
526 if (!dcon->ignore_fb_events)
527 dcon_sleep(dcon, !!(dev->props.state & BL_CORE_FBBLANK));
532 static int dcon_bl_get(struct backlight_device *dev)
534 struct dcon_priv *dcon = bl_get_data(dev);
539 static const struct backlight_ops dcon_bl_ops = {
540 .update_status = dcon_bl_update,
541 .get_brightness = dcon_bl_get,
544 static struct backlight_properties dcon_bl_props = {
545 .max_brightness = 15,
546 .type = BACKLIGHT_RAW,
547 .power = FB_BLANK_UNBLANK,
550 static int dcon_reboot_notify(struct notifier_block *nb,
551 unsigned long foo, void *bar)
553 struct dcon_priv *dcon = container_of(nb, struct dcon_priv, reboot_nb);
555 if (!dcon || !dcon->client)
558 /* Turn off the DCON. Entirely. */
559 dcon_write(dcon, DCON_REG_MODE, 0x39);
560 dcon_write(dcon, DCON_REG_MODE, 0x32);
564 static int unfreeze_on_panic(struct notifier_block *nb,
565 unsigned long e, void *p)
567 pdata->set_dconload(1);
571 static struct notifier_block dcon_panic_nb = {
572 .notifier_call = unfreeze_on_panic,
575 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
577 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
582 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
584 struct dcon_priv *dcon;
590 dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
594 dcon->client = client;
595 init_waitqueue_head(&dcon->waitq);
596 INIT_WORK(&dcon->switch_source, dcon_source_switch);
597 dcon->reboot_nb.notifier_call = dcon_reboot_notify;
598 dcon->reboot_nb.priority = -1;
600 i2c_set_clientdata(client, dcon);
602 if (num_registered_fb < 1) {
603 dev_err(&client->dev, "DCON driver requires a registered fb\n");
607 dcon->fbinfo = registered_fb[0];
609 rc = dcon_hw_init(dcon, 1);
613 /* Add the DCON device */
615 dcon_device = platform_device_alloc("dcon", -1);
618 pr_err("Unable to create the DCON device\n");
622 rc = platform_device_add(dcon_device);
623 platform_set_drvdata(dcon_device, dcon);
626 pr_err("Unable to add the DCON device\n");
630 for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
631 rc = device_create_file(&dcon_device->dev,
632 &dcon_device_files[i]);
634 dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
639 dcon->bl_val = dcon_read(dcon, DCON_REG_BRIGHT) & 0x0F;
641 /* Add the backlight device for the DCON */
642 dcon_bl_props.brightness = dcon->bl_val;
643 dcon->bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
644 dcon, &dcon_bl_ops, &dcon_bl_props);
645 if (IS_ERR(dcon->bl_dev)) {
646 dev_err(&client->dev, "cannot register backlight dev (%ld)\n",
647 PTR_ERR(dcon->bl_dev));
651 register_reboot_notifier(&dcon->reboot_nb);
652 atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
657 for (j = 0; j < i; j++)
658 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
660 platform_device_unregister(dcon_device);
663 free_irq(DCON_IRQ, dcon);
669 static int dcon_remove(struct i2c_client *client)
671 struct dcon_priv *dcon = i2c_get_clientdata(client);
673 unregister_reboot_notifier(&dcon->reboot_nb);
674 atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
676 free_irq(DCON_IRQ, dcon);
678 backlight_device_unregister(dcon->bl_dev);
681 platform_device_unregister(dcon_device);
682 cancel_work_sync(&dcon->switch_source);
690 static int dcon_suspend(struct device *dev)
692 struct i2c_client *client = to_i2c_client(dev);
693 struct dcon_priv *dcon = i2c_get_clientdata(client);
696 /* Set up the DCON to have the source */
697 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
703 static int dcon_resume(struct device *dev)
705 struct i2c_client *client = to_i2c_client(dev);
706 struct dcon_priv *dcon = i2c_get_clientdata(client);
709 dcon_bus_stabilize(dcon, 0);
710 dcon_set_source(dcon, DCON_SOURCE_CPU);
718 #define dcon_suspend NULL
719 #define dcon_resume NULL
721 #endif /* CONFIG_PM */
723 irqreturn_t dcon_interrupt(int irq, void *id)
725 struct dcon_priv *dcon = id;
728 if (pdata->read_status(&status))
731 switch (status & 3) {
733 pr_debug("DCONLOAD_MISSED interrupt\n");
736 case 2: /* switch to DCON mode */
737 case 1: /* switch to CPU mode */
738 dcon->switched = true;
739 dcon->irq_time = ktime_get();
740 wake_up(&dcon->waitq);
744 /* workaround resume case: the DCON (on 1.5) doesn't
745 * ever assert status 0x01 when switching to CPU mode
746 * during resume. this is because DCONLOAD is de-asserted
747 * _immediately_ upon exiting S3, so the actual release
748 * of the DCON happened long before this point.
749 * see http://dev.laptop.org/ticket/9869
751 if (dcon->curr_src != dcon->pending_src && !dcon->switched) {
752 dcon->switched = true;
753 dcon->irq_time = ktime_get();
754 wake_up(&dcon->waitq);
755 pr_debug("switching w/ status 0/0\n");
757 pr_debug("scanline interrupt w/CPU\n");
764 static const struct dev_pm_ops dcon_pm_ops = {
765 .suspend = dcon_suspend,
766 .resume = dcon_resume,
769 static const struct i2c_device_id dcon_idtable[] = {
773 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
775 static struct i2c_driver dcon_driver = {
780 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
781 .id_table = dcon_idtable,
783 .remove = dcon_remove,
784 .detect = dcon_detect,
785 .address_list = normal_i2c,
788 static int __init olpc_dcon_init(void)
790 #ifdef CONFIG_FB_OLPC_DCON_1_5
792 if (olpc_board_at_least(olpc_board(0xd0)))
793 pdata = &dcon_pdata_xo_1_5;
795 #ifdef CONFIG_FB_OLPC_DCON_1
797 pdata = &dcon_pdata_xo_1;
800 return i2c_add_driver(&dcon_driver);
803 static void __exit olpc_dcon_exit(void)
805 i2c_del_driver(&dcon_driver);
808 module_init(olpc_dcon_init);
809 module_exit(olpc_dcon_exit);
811 MODULE_LICENSE("GPL");