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
);
523 /* power down the DCON when the screen is blanked */
524 if (!dcon
->ignore_fb_events
)
525 dcon_sleep(dcon
, !!(dev
->props
.state
& BL_CORE_FBBLANK
));
530 static int dcon_bl_get(struct backlight_device
*dev
)
532 struct dcon_priv
*dcon
= bl_get_data(dev
);
536 static const struct backlight_ops dcon_bl_ops
= {
537 .update_status
= dcon_bl_update
,
538 .get_brightness
= dcon_bl_get
,
541 static struct backlight_properties dcon_bl_props
= {
542 .max_brightness
= 15,
543 .type
= BACKLIGHT_RAW
,
544 .power
= FB_BLANK_UNBLANK
,
547 static int dcon_reboot_notify(struct notifier_block
*nb
,
548 unsigned long foo
, void *bar
)
550 struct dcon_priv
*dcon
= container_of(nb
, struct dcon_priv
, reboot_nb
);
552 if (!dcon
|| !dcon
->client
)
555 /* Turn off the DCON. Entirely. */
556 dcon_write(dcon
, DCON_REG_MODE
, 0x39);
557 dcon_write(dcon
, DCON_REG_MODE
, 0x32);
561 static int unfreeze_on_panic(struct notifier_block
*nb
,
562 unsigned long e
, void *p
)
564 pdata
->set_dconload(1);
568 static struct notifier_block dcon_panic_nb
= {
569 .notifier_call
= unfreeze_on_panic
,
572 static int dcon_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
574 strlcpy(info
->type
, "olpc_dcon", I2C_NAME_SIZE
);
579 static int dcon_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
581 struct dcon_priv
*dcon
;
587 dcon
= kzalloc(sizeof(*dcon
), GFP_KERNEL
);
591 dcon
->client
= client
;
592 init_waitqueue_head(&dcon
->waitq
);
593 INIT_WORK(&dcon
->switch_source
, dcon_source_switch
);
594 dcon
->reboot_nb
.notifier_call
= dcon_reboot_notify
;
595 dcon
->reboot_nb
.priority
= -1;
597 i2c_set_clientdata(client
, dcon
);
599 if (num_registered_fb
< 1) {
600 dev_err(&client
->dev
, "DCON driver requires a registered fb\n");
604 dcon
->fbinfo
= registered_fb
[0];
606 rc
= dcon_hw_init(dcon
, 1);
610 /* Add the DCON device */
612 dcon_device
= platform_device_alloc("dcon", -1);
614 if (dcon_device
== NULL
) {
615 pr_err("Unable to create the DCON device\n");
619 rc
= platform_device_add(dcon_device
);
620 platform_set_drvdata(dcon_device
, dcon
);
623 pr_err("Unable to add the DCON device\n");
627 for (i
= 0; i
< ARRAY_SIZE(dcon_device_files
); i
++) {
628 rc
= device_create_file(&dcon_device
->dev
,
629 &dcon_device_files
[i
]);
631 dev_err(&dcon_device
->dev
, "Cannot create sysfs file\n");
636 dcon
->bl_val
= dcon_read(dcon
, DCON_REG_BRIGHT
) & 0x0F;
638 /* Add the backlight device for the DCON */
639 dcon_bl_props
.brightness
= dcon
->bl_val
;
640 dcon
->bl_dev
= backlight_device_register("dcon-bl", &dcon_device
->dev
,
641 dcon
, &dcon_bl_ops
, &dcon_bl_props
);
642 if (IS_ERR(dcon
->bl_dev
)) {
643 dev_err(&client
->dev
, "cannot register backlight dev (%ld)\n",
644 PTR_ERR(dcon
->bl_dev
));
648 register_reboot_notifier(&dcon
->reboot_nb
);
649 atomic_notifier_chain_register(&panic_notifier_list
, &dcon_panic_nb
);
654 for (j
= 0; j
< i
; j
++)
655 device_remove_file(&dcon_device
->dev
, &dcon_device_files
[j
]);
657 platform_device_unregister(dcon_device
);
660 free_irq(DCON_IRQ
, dcon
);
666 static int dcon_remove(struct i2c_client
*client
)
668 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
670 unregister_reboot_notifier(&dcon
->reboot_nb
);
671 atomic_notifier_chain_unregister(&panic_notifier_list
, &dcon_panic_nb
);
673 free_irq(DCON_IRQ
, dcon
);
676 backlight_device_unregister(dcon
->bl_dev
);
678 if (dcon_device
!= NULL
)
679 platform_device_unregister(dcon_device
);
680 cancel_work_sync(&dcon
->switch_source
);
688 static int dcon_suspend(struct device
*dev
)
690 struct i2c_client
*client
= to_i2c_client(dev
);
691 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
694 /* Set up the DCON to have the source */
695 dcon_set_source_sync(dcon
, DCON_SOURCE_DCON
);
701 static int dcon_resume(struct device
*dev
)
703 struct i2c_client
*client
= to_i2c_client(dev
);
704 struct dcon_priv
*dcon
= i2c_get_clientdata(client
);
707 dcon_bus_stabilize(dcon
, 0);
708 dcon_set_source(dcon
, DCON_SOURCE_CPU
);
716 #define dcon_suspend NULL
717 #define dcon_resume NULL
719 #endif /* CONFIG_PM */
722 irqreturn_t
dcon_interrupt(int irq
, void *id
)
724 struct dcon_priv
*dcon
= id
;
727 if (pdata
->read_status(&status
))
730 switch (status
& 3) {
732 pr_debug("DCONLOAD_MISSED interrupt\n");
735 case 2: /* switch to DCON mode */
736 case 1: /* switch to CPU mode */
737 dcon
->switched
= true;
738 getnstimeofday(&dcon
->irq_time
);
739 wake_up(&dcon
->waitq
);
743 /* workaround resume case: the DCON (on 1.5) doesn't
744 * ever assert status 0x01 when switching to CPU mode
745 * during resume. this is because DCONLOAD is de-asserted
746 * _immediately_ upon exiting S3, so the actual release
747 * of the DCON happened long before this point.
748 * see http://dev.laptop.org/ticket/9869
750 if (dcon
->curr_src
!= dcon
->pending_src
&& !dcon
->switched
) {
751 dcon
->switched
= true;
752 getnstimeofday(&dcon
->irq_time
);
753 wake_up(&dcon
->waitq
);
754 pr_debug("switching w/ status 0/0\n");
756 pr_debug("scanline interrupt w/CPU\n");
763 static const struct dev_pm_ops dcon_pm_ops
= {
764 .suspend
= dcon_suspend
,
765 .resume
= dcon_resume
,
768 static const struct i2c_device_id dcon_idtable
[] = {
772 MODULE_DEVICE_TABLE(i2c
, dcon_idtable
);
774 struct i2c_driver dcon_driver
= {
779 .class = I2C_CLASS_DDC
| I2C_CLASS_HWMON
,
780 .id_table
= dcon_idtable
,
782 .remove
= dcon_remove
,
783 .detect
= dcon_detect
,
784 .address_list
= normal_i2c
,
787 static int __init
olpc_dcon_init(void)
789 #ifdef CONFIG_FB_OLPC_DCON_1_5
791 if (olpc_board_at_least(olpc_board(0xd0)))
792 pdata
= &dcon_pdata_xo_1_5
;
794 #ifdef CONFIG_FB_OLPC_DCON_1
796 pdata
= &dcon_pdata_xo_1
;
799 return i2c_add_driver(&dcon_driver
);
802 static void __exit
olpc_dcon_exit(void)
804 i2c_del_driver(&dcon_driver
);
807 module_init(olpc_dcon_init
);
808 module_exit(olpc_dcon_exit
);
810 MODULE_LICENSE("GPL");