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 static struct dcon_platform_data *pdata;
46 /* Platform devices */
47 static struct platform_device *dcon_device;
49 static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
51 static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
53 return i2c_smbus_write_word_data(dcon->client, reg, val);
56 static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
58 return i2c_smbus_read_word_data(dcon->client, reg);
61 /* ===== API functions - these are called by a variety of users ==== */
63 static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
68 ver = dcon_read(dcon, DCON_REG_ID);
69 if ((ver >> 8) != 0xDC) {
70 pr_err("DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
76 pr_info("Discovered DCON version %x\n", ver & 0xFF);
77 rc = pdata->init(dcon);
79 pr_err("Unable to init.\n");
85 dev_err(&dcon->client->dev,
86 "DCON v1 is unsupported, giving up..\n");
91 /* SDRAM setup/hold time */
92 dcon_write(dcon, 0x3a, 0xc040);
93 dcon_write(dcon, 0x41, 0x0000);
94 dcon_write(dcon, 0x41, 0x0101);
95 dcon_write(dcon, 0x42, 0x0101);
97 /* Colour swizzle, AA, no passthrough, backlight */
99 dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
100 MODE_CSWIZZLE | MODE_COL_AA;
102 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
105 /* Set the scanline to interrupt on during resume */
106 dcon_write(dcon, DCON_REG_SCAN_INT, resumeline);
113 * The smbus doesn't always come back due to what is believed to be
114 * hardware (power rail) bugs. For older models where this is known to
115 * occur, our solution is to attempt to wait for the bus to stabilize;
116 * if it doesn't happen, cut power to the dcon, repower it, and wait
117 * for the bus to stabilize. Rinse, repeat until we have a working
118 * smbus. For newer models, we simply BUG(); we want to know if this
119 * still happens despite the power fixes that have been made!
121 static int dcon_bus_stabilize(struct dcon_priv *dcon, int is_powered_down)
123 unsigned long timeout;
127 if (is_powered_down) {
129 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
131 pr_warn("unable to force dcon to power up: %d!\n", x);
134 msleep(10); /* we'll be conservative */
137 pdata->bus_stabilize_wiggle();
139 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
141 x = dcon_read(dcon, DCON_REG_ID);
144 pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
145 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
147 olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
150 goto power_up; /* argh, stupid hardware.. */
154 return dcon_hw_init(dcon, 0);
158 static void dcon_set_backlight(struct dcon_priv *dcon, u8 level)
160 dcon->bl_val = level;
161 dcon_write(dcon, DCON_REG_BRIGHT, dcon->bl_val);
163 /* Purposely turn off the backlight when we go to level 0 */
164 if (dcon->bl_val == 0) {
165 dcon->disp_mode &= ~MODE_BL_ENABLE;
166 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
167 } else if (!(dcon->disp_mode & MODE_BL_ENABLE)) {
168 dcon->disp_mode |= MODE_BL_ENABLE;
169 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
173 /* Set the output type to either color or mono */
174 static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
176 if (dcon->mono == enable_mono)
179 dcon->mono = enable_mono;
182 dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
183 dcon->disp_mode |= MODE_MONO_LUMA;
185 dcon->disp_mode &= ~(MODE_MONO_LUMA);
186 dcon->disp_mode |= MODE_CSWIZZLE | MODE_COL_AA;
189 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
193 /* For now, this will be really stupid - we need to address how
194 * DCONLOAD works in a sleep and account for it accordingly
197 static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
201 /* Turn off the backlight and put the DCON to sleep */
203 if (dcon->asleep == sleep)
206 if (!olpc_board_at_least(olpc_board(0xc2)))
211 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 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 struct timespec delta_t, now;
242 getnstimeofday(&now);
243 delta_t = timespec_sub(now, dcon->load_time);
244 if (delta_t.tv_sec != 0 ||
245 delta_t.tv_nsec > NSEC_PER_MSEC * 20) {
252 static bool dcon_blank_fb(struct dcon_priv *dcon, bool blank)
256 if (!lock_fb_info(dcon->fbinfo)) {
257 dev_err(&dcon->client->dev, "unable to lock framebuffer\n");
261 dcon->ignore_fb_events = true;
262 err = fb_blank(dcon->fbinfo,
263 blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
264 dcon->ignore_fb_events = false;
266 unlock_fb_info(dcon->fbinfo);
269 dev_err(&dcon->client->dev, "couldn't %sblank framebuffer\n",
276 /* Set the source of the display (CPU or DCON) */
277 static void dcon_source_switch(struct work_struct *work)
279 struct dcon_priv *dcon = container_of(work, struct dcon_priv,
281 int source = dcon->pending_src;
283 if (dcon->curr_src == source)
286 dcon_load_holdoff(dcon);
288 dcon->switched = false;
291 case DCON_SOURCE_CPU:
292 pr_info("dcon_source_switch to CPU\n");
293 /* Enable the scanline interrupt bit */
294 if (dcon_write(dcon, DCON_REG_MODE,
295 dcon->disp_mode | MODE_SCAN_INT))
296 pr_err("couldn't enable scanline interrupt!\n");
298 /* Wait up to one second for the scanline interrupt */
299 wait_event_timeout(dcon->waitq, dcon->switched, HZ);
302 pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
304 /* Turn off the scanline interrupt */
305 if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
306 pr_err("couldn't disable scanline interrupt!\n");
309 * Ideally we'd like to disable interrupts here so that the
310 * fb unblanking and DCON turn on happen at a known time value;
311 * however, we can't do that right now with fb_blank
312 * messing with semaphores.
314 * For now, we just hope..
316 if (!dcon_blank_fb(dcon, false)) {
317 pr_err("Failed to enter CPU mode\n");
318 dcon->pending_src = DCON_SOURCE_DCON;
322 /* And turn off the DCON */
323 pdata->set_dconload(1);
324 getnstimeofday(&dcon->load_time);
326 pr_info("The CPU has control\n");
328 case DCON_SOURCE_DCON:
330 struct timespec delta_t;
332 pr_info("dcon_source_switch to DCON\n");
334 /* Clear DCONLOAD - this implies that the DCON is in control */
335 pdata->set_dconload(0);
336 getnstimeofday(&dcon->load_time);
338 wait_event_timeout(dcon->waitq, dcon->switched, HZ/2);
340 if (!dcon->switched) {
341 pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
343 /* sometimes the DCON doesn't follow its own rules,
344 * and doesn't wait for two vsync pulses before
345 * ack'ing the frame load with an IRQ. the result
346 * is that the display shows the *previously*
347 * loaded frame. we can detect this by looking at
348 * the time between asserting DCONLOAD and the IRQ --
349 * if it's less than 20msec, then the DCON couldn't
350 * have seen two VSYNC pulses. in that case we
351 * deassert and reassert, and hope for the best.
352 * see http://dev.laptop.org/ticket/9664
354 delta_t = timespec_sub(dcon->irq_time, dcon->load_time);
355 if (dcon->switched && delta_t.tv_sec == 0 &&
356 delta_t.tv_nsec < NSEC_PER_MSEC * 20) {
357 pr_err("missed loading, retrying\n");
358 pdata->set_dconload(1);
360 pdata->set_dconload(0);
361 getnstimeofday(&dcon->load_time);
366 dcon_blank_fb(dcon, true);
367 pr_info("The DCON has control\n");
374 dcon->curr_src = source;
377 static void dcon_set_source(struct dcon_priv *dcon, int arg)
379 if (dcon->pending_src == arg)
382 dcon->pending_src = arg;
384 if ((dcon->curr_src != arg) && !work_pending(&dcon->switch_source))
385 schedule_work(&dcon->switch_source);
388 static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
390 dcon_set_source(dcon, arg);
391 flush_scheduled_work();
394 static ssize_t dcon_mode_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct dcon_priv *dcon = dev_get_drvdata(dev);
398 return sprintf(buf, "%4.4X\n", dcon->disp_mode);
401 static ssize_t dcon_sleep_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
405 struct dcon_priv *dcon = dev_get_drvdata(dev);
406 return sprintf(buf, "%d\n", dcon->asleep);
409 static ssize_t dcon_freeze_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct dcon_priv *dcon = dev_get_drvdata(dev);
413 return sprintf(buf, "%d\n", dcon->curr_src == DCON_SOURCE_DCON ? 1 : 0);
416 static ssize_t dcon_mono_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct dcon_priv *dcon = dev_get_drvdata(dev);
420 return sprintf(buf, "%d\n", dcon->mono);
423 static ssize_t dcon_resumeline_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 return sprintf(buf, "%d\n", resumeline);
429 static ssize_t dcon_mono_store(struct device *dev,
430 struct device_attribute *attr, const char *buf, size_t count)
432 unsigned long enable_mono;
435 rc = kstrtoul(buf, 10, &enable_mono);
439 dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
444 static ssize_t dcon_freeze_store(struct device *dev,
445 struct device_attribute *attr, const char *buf, size_t count)
447 struct dcon_priv *dcon = dev_get_drvdata(dev);
448 unsigned long output;
451 ret = kstrtoul(buf, 10, &output);
455 pr_info("dcon_freeze_store: %lu\n", output);
459 dcon_set_source(dcon, DCON_SOURCE_CPU);
462 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
464 case 2: /* normally unused */
465 dcon_set_source(dcon, DCON_SOURCE_DCON);
474 static ssize_t dcon_resumeline_store(struct device *dev,
475 struct device_attribute *attr, const char *buf, size_t count)
480 rc = kstrtou16(buf, 10, &rl);
485 dcon_write(dev_get_drvdata(dev), DCON_REG_SCAN_INT, resumeline);
490 static ssize_t dcon_sleep_store(struct device *dev,
491 struct device_attribute *attr, const char *buf, size_t count)
493 unsigned long output;
496 ret = kstrtoul(buf, 10, &output);
500 dcon_sleep(dev_get_drvdata(dev), output ? true : false);
504 static struct device_attribute dcon_device_files[] = {
505 __ATTR(mode, 0444, dcon_mode_show, NULL),
506 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
507 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
508 __ATTR(monochrome, 0644, dcon_mono_show, dcon_mono_store),
509 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
512 static int dcon_bl_update(struct backlight_device *dev)
514 struct dcon_priv *dcon = bl_get_data(dev);
515 u8 level = dev->props.brightness & 0x0F;
517 if (dev->props.power != FB_BLANK_UNBLANK)
520 if (level != dcon->bl_val)
521 dcon_set_backlight(dcon, level);
526 static int dcon_bl_get(struct backlight_device *dev)
528 struct dcon_priv *dcon = bl_get_data(dev);
532 static const struct backlight_ops dcon_bl_ops = {
533 .update_status = dcon_bl_update,
534 .get_brightness = dcon_bl_get,
537 static struct backlight_properties dcon_bl_props = {
538 .max_brightness = 15,
539 .type = BACKLIGHT_RAW,
540 .power = FB_BLANK_UNBLANK,
543 static int dcon_reboot_notify(struct notifier_block *nb,
544 unsigned long foo, void *bar)
546 struct dcon_priv *dcon = container_of(nb, struct dcon_priv, reboot_nb);
548 if (!dcon || !dcon->client)
551 /* Turn off the DCON. Entirely. */
552 dcon_write(dcon, DCON_REG_MODE, 0x39);
553 dcon_write(dcon, DCON_REG_MODE, 0x32);
557 static int unfreeze_on_panic(struct notifier_block *nb,
558 unsigned long e, void *p)
560 pdata->set_dconload(1);
564 static struct notifier_block dcon_panic_nb = {
565 .notifier_call = unfreeze_on_panic,
569 * When the framebuffer sleeps due to external sources (e.g. user idle), power
570 * down the DCON as well. Power it back up when the fb comes back to life.
572 static int dcon_fb_notifier(struct notifier_block *self,
573 unsigned long event, void *data)
575 struct fb_event *evdata = data;
576 struct dcon_priv *dcon = container_of(self, struct dcon_priv,
578 int *blank = (int *)evdata->data;
579 if (((event != FB_EVENT_BLANK) && (event != FB_EVENT_CONBLANK)) ||
580 dcon->ignore_fb_events)
582 dcon_sleep(dcon, *blank ? true : false);
586 static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
588 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
593 static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
595 struct dcon_priv *dcon;
601 dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
605 dcon->client = client;
606 init_waitqueue_head(&dcon->waitq);
607 INIT_WORK(&dcon->switch_source, dcon_source_switch);
608 dcon->reboot_nb.notifier_call = dcon_reboot_notify;
609 dcon->reboot_nb.priority = -1;
610 dcon->fbevent_nb.notifier_call = dcon_fb_notifier;
612 i2c_set_clientdata(client, dcon);
614 if (num_registered_fb < 1) {
615 dev_err(&client->dev, "DCON driver requires a registered fb\n");
619 dcon->fbinfo = registered_fb[0];
621 rc = dcon_hw_init(dcon, 1);
625 /* Add the DCON device */
627 dcon_device = platform_device_alloc("dcon", -1);
629 if (dcon_device == NULL) {
630 pr_err("Unable to create the DCON device\n");
634 rc = platform_device_add(dcon_device);
635 platform_set_drvdata(dcon_device, dcon);
638 pr_err("Unable to add the DCON device\n");
642 for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
643 rc = device_create_file(&dcon_device->dev,
644 &dcon_device_files[i]);
646 dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
651 dcon->bl_val = dcon_read(dcon, DCON_REG_BRIGHT) & 0x0F;
653 /* Add the backlight device for the DCON */
654 dcon_bl_props.brightness = dcon->bl_val;
655 dcon->bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
656 dcon, &dcon_bl_ops, &dcon_bl_props);
657 if (IS_ERR(dcon->bl_dev)) {
658 dev_err(&client->dev, "cannot register backlight dev (%ld)\n",
659 PTR_ERR(dcon->bl_dev));
663 register_reboot_notifier(&dcon->reboot_nb);
664 atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
665 fb_register_client(&dcon->fbevent_nb);
670 for (j = 0; j < i; j++)
671 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
673 platform_device_unregister(dcon_device);
676 free_irq(DCON_IRQ, dcon);
682 static int dcon_remove(struct i2c_client *client)
684 struct dcon_priv *dcon = i2c_get_clientdata(client);
686 fb_unregister_client(&dcon->fbevent_nb);
687 unregister_reboot_notifier(&dcon->reboot_nb);
688 atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
690 free_irq(DCON_IRQ, dcon);
693 backlight_device_unregister(dcon->bl_dev);
695 if (dcon_device != NULL)
696 platform_device_unregister(dcon_device);
697 cancel_work_sync(&dcon->switch_source);
705 static int dcon_suspend(struct i2c_client *client, pm_message_t state)
707 struct dcon_priv *dcon = i2c_get_clientdata(client);
710 /* Set up the DCON to have the source */
711 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
717 static int dcon_resume(struct i2c_client *client)
719 struct dcon_priv *dcon = i2c_get_clientdata(client);
722 dcon_bus_stabilize(dcon, 0);
723 dcon_set_source(dcon, DCON_SOURCE_CPU);
732 irqreturn_t dcon_interrupt(int irq, void *id)
734 struct dcon_priv *dcon = id;
737 if (pdata->read_status(&status))
740 switch (status & 3) {
742 pr_debug("DCONLOAD_MISSED interrupt\n");
745 case 2: /* switch to DCON mode */
746 case 1: /* switch to CPU mode */
747 dcon->switched = true;
748 getnstimeofday(&dcon->irq_time);
749 wake_up(&dcon->waitq);
753 /* workaround resume case: the DCON (on 1.5) doesn't
754 * ever assert status 0x01 when switching to CPU mode
755 * during resume. this is because DCONLOAD is de-asserted
756 * _immediately_ upon exiting S3, so the actual release
757 * of the DCON happened long before this point.
758 * see http://dev.laptop.org/ticket/9869
760 if (dcon->curr_src != dcon->pending_src && !dcon->switched) {
761 dcon->switched = true;
762 getnstimeofday(&dcon->irq_time);
763 wake_up(&dcon->waitq);
764 pr_debug("switching w/ status 0/0\n");
766 pr_debug("scanline interrupt w/CPU\n");
773 static const struct i2c_device_id dcon_idtable[] = {
778 MODULE_DEVICE_TABLE(i2c, dcon_idtable);
780 struct i2c_driver dcon_driver = {
784 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
785 .id_table = dcon_idtable,
787 .remove = __devexit_p(dcon_remove),
788 .detect = dcon_detect,
789 .address_list = normal_i2c,
791 .suspend = dcon_suspend,
792 .resume = dcon_resume,
796 static int __init olpc_dcon_init(void)
798 #ifdef CONFIG_FB_OLPC_DCON_1_5
800 if (olpc_board_at_least(olpc_board(0xd0)))
801 pdata = &dcon_pdata_xo_1_5;
803 #ifdef CONFIG_FB_OLPC_DCON_1
805 pdata = &dcon_pdata_xo_1;
808 return i2c_add_driver(&dcon_driver);
811 static void __exit olpc_dcon_exit(void)
813 i2c_del_driver(&dcon_driver);
816 module_init(olpc_dcon_init);
817 module_exit(olpc_dcon_exit);
819 MODULE_LICENSE("GPL");