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
, DCON_REG_MEM_OPT_A
, 0x0000); /* clear option bits */
94 dcon_write(dcon
, DCON_REG_MEM_OPT_A
,
95 MEM_DLL_CLOCK_DELAY
| MEM_POWER_DOWN
);
96 dcon_write(dcon
, DCON_REG_MEM_OPT_B
, MEM_SOFT_RESET
);
98 /* Colour swizzle, AA, no passthrough, backlight */
100 dcon
->disp_mode
= MODE_PASSTHRU
| MODE_BL_ENABLE
|
101 MODE_CSWIZZLE
| MODE_COL_AA
;
103 dcon_write(dcon
, DCON_REG_MODE
, dcon
->disp_mode
);
106 /* Set the scanline to interrupt on during resume */
107 dcon_write(dcon
, DCON_REG_SCAN_INT
, resumeline
);
114 * The smbus doesn't always come back due to what is believed to be
115 * hardware (power rail) bugs. For older models where this is known to
116 * occur, our solution is to attempt to wait for the bus to stabilize;
117 * if it doesn't happen, cut power to the dcon, repower it, and wait
118 * for the bus to stabilize. Rinse, repeat until we have a working
119 * smbus. For newer models, we simply BUG(); we want to know if this
120 * still happens despite the power fixes that have been made!
122 static int dcon_bus_stabilize(struct dcon_priv
*dcon
, int is_powered_down
)
124 unsigned long timeout
;
129 if (is_powered_down
) {
131 x
= olpc_ec_cmd(EC_DCON_POWER_MODE
, &pm
, 1, NULL
, 0);
133 pr_warn("unable to force dcon to power up: %d!\n", x
);
136 usleep_range(10000, 11000); /* we'll be conservative */
139 pdata
->bus_stabilize_wiggle();
141 for (x
= -1, timeout
= 50; timeout
&& x
< 0; timeout
--) {
142 usleep_range(1000, 1100);
143 x
= dcon_read(dcon
, DCON_REG_ID
);
146 pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
147 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
149 olpc_ec_cmd(EC_DCON_POWER_MODE
, &pm
, 1, NULL
, 0);
152 goto power_up
; /* argh, stupid hardware.. */
156 return dcon_hw_init(dcon
, 0);
160 static void dcon_set_backlight(struct dcon_priv
*dcon
, u8 level
)
162 dcon
->bl_val
= level
;
163 dcon_write(dcon
, DCON_REG_BRIGHT
, dcon
->bl_val
);
165 /* Purposely turn off the backlight when we go to level 0 */
166 if (dcon
->bl_val
== 0) {
167 dcon
->disp_mode
&= ~MODE_BL_ENABLE
;
168 dcon_write(dcon
, DCON_REG_MODE
, dcon
->disp_mode
);
169 } else if (!(dcon
->disp_mode
& MODE_BL_ENABLE
)) {
170 dcon
->disp_mode
|= MODE_BL_ENABLE
;
171 dcon_write(dcon
, DCON_REG_MODE
, dcon
->disp_mode
);
175 /* Set the output type to either color or mono */
176 static int dcon_set_mono_mode(struct dcon_priv
*dcon
, bool enable_mono
)
178 if (dcon
->mono
== enable_mono
)
181 dcon
->mono
= enable_mono
;
184 dcon
->disp_mode
&= ~(MODE_CSWIZZLE
| MODE_COL_AA
);
185 dcon
->disp_mode
|= MODE_MONO_LUMA
;
187 dcon
->disp_mode
&= ~(MODE_MONO_LUMA
);
188 dcon
->disp_mode
|= MODE_CSWIZZLE
| MODE_COL_AA
;
191 dcon_write(dcon
, DCON_REG_MODE
, dcon
->disp_mode
);
195 /* For now, this will be really stupid - we need to address how
196 * DCONLOAD works in a sleep and account for it accordingly
199 static void dcon_sleep(struct dcon_priv
*dcon
, bool sleep
)
203 /* Turn off the backlight and put the DCON to sleep */
205 if (dcon
->asleep
== sleep
)
208 if (!olpc_board_at_least(olpc_board(0xc2)))
213 x
= olpc_ec_cmd(EC_DCON_POWER_MODE
, &pm
, 1, NULL
, 0);
215 pr_warn("unable to force dcon to power down: %d!\n", x
);
217 dcon
->asleep
= sleep
;
219 /* Only re-enable the backlight if the backlight value is set */
220 if (dcon
->bl_val
!= 0)
221 dcon
->disp_mode
|= MODE_BL_ENABLE
;
222 x
= dcon_bus_stabilize(dcon
, 1);
224 pr_warn("unable to reinit dcon hardware: %d!\n", x
);
226 dcon
->asleep
= sleep
;
228 /* Restore backlight */
229 dcon_set_backlight(dcon
, dcon
->bl_val
);
232 /* We should turn off some stuff in the framebuffer - but what? */
235 /* the DCON seems to get confused if we change DCONLOAD too
236 * frequently -- i.e., approximately faster than frame time.
237 * normally we don't change it this fast, so in general we won't
240 static void dcon_load_holdoff(struct dcon_priv
*dcon
)
242 struct timespec delta_t
, now
;
244 getnstimeofday(&now
);
245 delta_t
= timespec_sub(now
, dcon
->load_time
);
246 if (delta_t
.tv_sec
!= 0 ||
247 delta_t
.tv_nsec
> NSEC_PER_MSEC
* 20) {
254 static bool dcon_blank_fb(struct dcon_priv
*dcon
, bool blank
)
259 if (!lock_fb_info(dcon
->fbinfo
)) {
261 dev_err(&dcon
->client
->dev
, "unable to lock framebuffer\n");
265 dcon
->ignore_fb_events
= true;
266 err
= fb_blank(dcon
->fbinfo
,
267 blank
? FB_BLANK_POWERDOWN
: FB_BLANK_UNBLANK
);
268 dcon
->ignore_fb_events
= false;
269 unlock_fb_info(dcon
->fbinfo
);
273 dev_err(&dcon
->client
->dev
, "couldn't %sblank framebuffer\n",
280 /* Set the source of the display (CPU or DCON) */
281 static void dcon_source_switch(struct work_struct
*work
)
283 struct dcon_priv
*dcon
= container_of(work
, struct dcon_priv
,
285 int source
= dcon
->pending_src
;
287 if (dcon
->curr_src
== source
)
290 dcon_load_holdoff(dcon
);
292 dcon
->switched
= false;
295 case DCON_SOURCE_CPU
:
296 pr_info("dcon_source_switch to CPU\n");
297 /* Enable the scanline interrupt bit */
298 if (dcon_write(dcon
, DCON_REG_MODE
,
299 dcon
->disp_mode
| MODE_SCAN_INT
))
300 pr_err("couldn't enable scanline interrupt!\n");
302 /* Wait up to one second for the scanline interrupt */
303 wait_event_timeout(dcon
->waitq
, dcon
->switched
, HZ
);
306 pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
308 /* Turn off the scanline interrupt */
309 if (dcon_write(dcon
, DCON_REG_MODE
, dcon
->disp_mode
))
310 pr_err("couldn't disable scanline interrupt!\n");
313 * Ideally we'd like to disable interrupts here so that the
314 * fb unblanking and DCON turn on happen at a known time value;
315 * however, we can't do that right now with fb_blank
316 * messing with semaphores.
318 * For now, we just hope..
320 if (!dcon_blank_fb(dcon
, false)) {
321 pr_err("Failed to enter CPU mode\n");
322 dcon
->pending_src
= DCON_SOURCE_DCON
;
326 /* And turn off the DCON */
327 pdata
->set_dconload(1);
328 getnstimeofday(&dcon
->load_time
);
330 pr_info("The CPU has control\n");
332 case DCON_SOURCE_DCON
:
334 struct timespec delta_t
;
336 pr_info("dcon_source_switch to DCON\n");
338 /* Clear DCONLOAD - this implies that the DCON is in control */
339 pdata
->set_dconload(0);
340 getnstimeofday(&dcon
->load_time
);
342 wait_event_timeout(dcon
->waitq
, dcon
->switched
, HZ
/2);
344 if (!dcon
->switched
) {
345 pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
347 /* sometimes the DCON doesn't follow its own rules,
348 * and doesn't wait for two vsync pulses before
349 * ack'ing the frame load with an IRQ. the result
350 * is that the display shows the *previously*
351 * loaded frame. we can detect this by looking at
352 * the time between asserting DCONLOAD and the IRQ --
353 * if it's less than 20msec, then the DCON couldn't
354 * have seen two VSYNC pulses. in that case we
355 * deassert and reassert, and hope for the best.
356 * see http://dev.laptop.org/ticket/9664
358 delta_t
= timespec_sub(dcon
->irq_time
, dcon
->load_time
);
359 if (dcon
->switched
&& delta_t
.tv_sec
== 0 &&
360 delta_t
.tv_nsec
< NSEC_PER_MSEC
* 20) {
361 pr_err("missed loading, retrying\n");
362 pdata
->set_dconload(1);
364 pdata
->set_dconload(0);
365 getnstimeofday(&dcon
->load_time
);
370 dcon_blank_fb(dcon
, true);
371 pr_info("The DCON has control\n");
378 dcon
->curr_src
= source
;
381 static void dcon_set_source(struct dcon_priv
*dcon
, int arg
)
383 if (dcon
->pending_src
== arg
)
386 dcon
->pending_src
= arg
;
388 if (dcon
->curr_src
!= arg
)
389 schedule_work(&dcon
->switch_source
);
392 static void dcon_set_source_sync(struct dcon_priv
*dcon
, int arg
)
394 dcon_set_source(dcon
, arg
);
395 flush_scheduled_work();
398 static ssize_t
dcon_mode_show(struct device
*dev
,
399 struct device_attribute
*attr
, char *buf
)
401 struct dcon_priv
*dcon
= dev_get_drvdata(dev
);
402 return sprintf(buf
, "%4.4X\n", dcon
->disp_mode
);
405 static ssize_t
dcon_sleep_show(struct device
*dev
,
406 struct device_attribute
*attr
, char *buf
)
409 struct dcon_priv
*dcon
= dev_get_drvdata(dev
);
410 return sprintf(buf
, "%d\n", dcon
->asleep
);
413 static ssize_t
dcon_freeze_show(struct device
*dev
,
414 struct device_attribute
*attr
, char *buf
)
416 struct dcon_priv
*dcon
= dev_get_drvdata(dev
);
417 return sprintf(buf
, "%d\n", dcon
->curr_src
== DCON_SOURCE_DCON
? 1 : 0);
420 static ssize_t
dcon_mono_show(struct device
*dev
,
421 struct device_attribute
*attr
, char *buf
)
423 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
);
459 pr_info("dcon_freeze_store: %lu\n", output
);
463 dcon_set_source(dcon
, DCON_SOURCE_CPU
);
466 dcon_set_source_sync(dcon
, DCON_SOURCE_DCON
);
468 case 2: /* normally unused */
469 dcon_set_source(dcon
, DCON_SOURCE_DCON
);
478 static ssize_t
dcon_resumeline_store(struct device
*dev
,
479 struct device_attribute
*attr
, const char *buf
, size_t count
)
484 rc
= kstrtou16(buf
, 10, &rl
);
489 dcon_write(dev_get_drvdata(dev
), DCON_REG_SCAN_INT
, resumeline
);
494 static ssize_t
dcon_sleep_store(struct device
*dev
,
495 struct device_attribute
*attr
, const char *buf
, size_t count
)
497 unsigned long output
;
500 ret
= kstrtoul(buf
, 10, &output
);
504 dcon_sleep(dev_get_drvdata(dev
), output
? true : false);
508 static struct device_attribute dcon_device_files
[] = {
509 __ATTR(mode
, 0444, dcon_mode_show
, NULL
),
510 __ATTR(sleep
, 0644, dcon_sleep_show
, dcon_sleep_store
),
511 __ATTR(freeze
, 0644, dcon_freeze_show
, dcon_freeze_store
),
512 __ATTR(monochrome
, 0644, dcon_mono_show
, dcon_mono_store
),
513 __ATTR(resumeline
, 0644, dcon_resumeline_show
, dcon_resumeline_store
),
516 static int dcon_bl_update(struct backlight_device
*dev
)
518 struct dcon_priv
*dcon
= bl_get_data(dev
);
519 u8 level
= dev
->props
.brightness
& 0x0F;
521 if (dev
->props
.power
!= FB_BLANK_UNBLANK
)
524 if (level
!= dcon
->bl_val
)
525 dcon_set_backlight(dcon
, level
);
527 /* power down the DCON when the screen is blanked */
528 if (!dcon
->ignore_fb_events
)
529 dcon_sleep(dcon
, !!(dev
->props
.state
& BL_CORE_FBBLANK
));
534 static int dcon_bl_get(struct backlight_device
*dev
)
536 struct dcon_priv
*dcon
= bl_get_data(dev
);
540 static const struct backlight_ops dcon_bl_ops
= {
541 .update_status
= dcon_bl_update
,
542 .get_brightness
= dcon_bl_get
,
545 static struct backlight_properties dcon_bl_props
= {
546 .max_brightness
= 15,
547 .type
= BACKLIGHT_RAW
,
548 .power
= FB_BLANK_UNBLANK
,
551 static int dcon_reboot_notify(struct notifier_block
*nb
,
552 unsigned long foo
, void *bar
)
554 struct dcon_priv
*dcon
= container_of(nb
, struct dcon_priv
, reboot_nb
);
556 if (!dcon
|| !dcon
->client
)
559 /* Turn off the DCON. Entirely. */
560 dcon_write(dcon
, DCON_REG_MODE
, 0x39);
561 dcon_write(dcon
, DCON_REG_MODE
, 0x32);
565 static int unfreeze_on_panic(struct notifier_block
*nb
,
566 unsigned long e
, void *p
)
568 pdata
->set_dconload(1);
572 static struct notifier_block dcon_panic_nb
= {
573 .notifier_call
= unfreeze_on_panic
,
576 static int dcon_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
578 strlcpy(info
->type
, "olpc_dcon", I2C_NAME_SIZE
);
583 static int dcon_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
585 struct dcon_priv
*dcon
;
591 dcon
= kzalloc(sizeof(*dcon
), GFP_KERNEL
);
595 dcon
->client
= client
;
596 init_waitqueue_head(&dcon
->waitq
);
597 INIT_WORK(&dcon
->switch_source
, dcon_source_switch
);
598 dcon
->reboot_nb
.notifier_call
= dcon_reboot_notify
;
599 dcon
->reboot_nb
.priority
= -1;
601 i2c_set_clientdata(client
, dcon
);
603 if (num_registered_fb
< 1) {
604 dev_err(&client
->dev
, "DCON driver requires a registered fb\n");
608 dcon
->fbinfo
= registered_fb
[0];
610 rc
= dcon_hw_init(dcon
, 1);
614 /* Add the DCON device */
616 dcon_device
= platform_device_alloc("dcon", -1);
618 if (dcon_device
== NULL
) {
619 pr_err("Unable to create the DCON device\n");
623 rc
= platform_device_add(dcon_device
);
624 platform_set_drvdata(dcon_device
, dcon
);
627 pr_err("Unable to add the DCON device\n");
631 for (i
= 0; i
< ARRAY_SIZE(dcon_device_files
); i
++) {
632 rc
= device_create_file(&dcon_device
->dev
,
633 &dcon_device_files
[i
]);
635 dev_err(&dcon_device
->dev
, "Cannot create sysfs file\n");
640 dcon
->bl_val
= dcon_read(dcon
, DCON_REG_BRIGHT
) & 0x0F;
642 /* Add the backlight device for the DCON */
643 dcon_bl_props
.brightness
= dcon
->bl_val
;
644 dcon
->bl_dev
= backlight_device_register("dcon-bl", &dcon_device
->dev
,
645 dcon
, &dcon_bl_ops
, &dcon_bl_props
);
646 if (IS_ERR(dcon
->bl_dev
)) {
647 dev_err(&client
->dev
, "cannot register backlight dev (%ld)\n",
648 PTR_ERR(dcon
->bl_dev
));
652 register_reboot_notifier(&dcon
->reboot_nb
);
653 atomic_notifier_chain_register(&panic_notifier_list
, &dcon_panic_nb
);
658 for (j
= 0; j
< i
; j
++)
659 device_remove_file(&dcon_device
->dev
, &dcon_device_files
[j
]);
661 platform_device_unregister(dcon_device
);
664 free_irq(DCON_IRQ
, dcon
);
670 static int dcon_remove(struct i2c_client
*client
)
672 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
674 unregister_reboot_notifier(&dcon
->reboot_nb
);
675 atomic_notifier_chain_unregister(&panic_notifier_list
, &dcon_panic_nb
);
677 free_irq(DCON_IRQ
, dcon
);
680 backlight_device_unregister(dcon
->bl_dev
);
682 if (dcon_device
!= NULL
)
683 platform_device_unregister(dcon_device
);
684 cancel_work_sync(&dcon
->switch_source
);
692 static int dcon_suspend(struct device
*dev
)
694 struct i2c_client
*client
= to_i2c_client(dev
);
695 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
698 /* Set up the DCON to have the source */
699 dcon_set_source_sync(dcon
, DCON_SOURCE_DCON
);
705 static int dcon_resume(struct device
*dev
)
707 struct i2c_client
*client
= to_i2c_client(dev
);
708 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
711 dcon_bus_stabilize(dcon
, 0);
712 dcon_set_source(dcon
, DCON_SOURCE_CPU
);
720 #define dcon_suspend NULL
721 #define dcon_resume NULL
723 #endif /* CONFIG_PM */
726 irqreturn_t
dcon_interrupt(int irq
, void *id
)
728 struct dcon_priv
*dcon
= id
;
731 if (pdata
->read_status(&status
))
734 switch (status
& 3) {
736 pr_debug("DCONLOAD_MISSED interrupt\n");
739 case 2: /* switch to DCON mode */
740 case 1: /* switch to CPU mode */
741 dcon
->switched
= true;
742 getnstimeofday(&dcon
->irq_time
);
743 wake_up(&dcon
->waitq
);
747 /* workaround resume case: the DCON (on 1.5) doesn't
748 * ever assert status 0x01 when switching to CPU mode
749 * during resume. this is because DCONLOAD is de-asserted
750 * _immediately_ upon exiting S3, so the actual release
751 * of the DCON happened long before this point.
752 * see http://dev.laptop.org/ticket/9869
754 if (dcon
->curr_src
!= dcon
->pending_src
&& !dcon
->switched
) {
755 dcon
->switched
= true;
756 getnstimeofday(&dcon
->irq_time
);
757 wake_up(&dcon
->waitq
);
758 pr_debug("switching w/ status 0/0\n");
760 pr_debug("scanline interrupt w/CPU\n");
767 static const struct dev_pm_ops dcon_pm_ops
= {
768 .suspend
= dcon_suspend
,
769 .resume
= dcon_resume
,
772 static const struct i2c_device_id dcon_idtable
[] = {
776 MODULE_DEVICE_TABLE(i2c
, dcon_idtable
);
778 struct i2c_driver dcon_driver
= {
783 .class = I2C_CLASS_DDC
| I2C_CLASS_HWMON
,
784 .id_table
= dcon_idtable
,
786 .remove
= dcon_remove
,
787 .detect
= dcon_detect
,
788 .address_list
= normal_i2c
,
791 static int __init
olpc_dcon_init(void)
793 #ifdef CONFIG_FB_OLPC_DCON_1_5
795 if (olpc_board_at_least(olpc_board(0xd0)))
796 pdata
= &dcon_pdata_xo_1_5
;
798 #ifdef CONFIG_FB_OLPC_DCON_1
800 pdata
= &dcon_pdata_xo_1
;
803 return i2c_add_driver(&dcon_driver
);
806 static void __exit
olpc_dcon_exit(void)
808 i2c_del_driver(&dcon_driver
);
811 module_init(olpc_dcon_init
);
812 module_exit(olpc_dcon_exit
);
814 MODULE_LICENSE("GPL");