Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 1 | /* |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 2 | * Support for power management features of the OLPC XO-1 laptop |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 3 | * |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 4 | * Copyright (C) 2010 Andres Salomon <dilinger@queued.net> |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 5 | * Copyright (C) 2010 One Laptop per Child |
| 6 | * Copyright (C) 2006 Red Hat, Inc. |
| 7 | * Copyright (C) 2006 Advanced Micro Devices, Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
Daniel Drake | 7a0d4fc | 2011-06-25 17:34:09 +0100 | [diff] [blame] | 15 | #include <linux/cs5535.h> |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Paul Gortmaker | 69c60c8 | 2011-05-26 12:22:53 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 18 | #include <linux/pm.h> |
Andres Salomon | 1310e6d | 2011-02-17 19:07:36 -0800 | [diff] [blame] | 19 | #include <linux/mfd/core.h> |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 20 | #include <linux/suspend.h> |
Andres Salomon | 3bf9428 | 2012-07-11 01:16:29 -0700 | [diff] [blame] | 21 | #include <linux/olpc-ec.h> |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 22 | |
| 23 | #include <asm/io.h> |
| 24 | #include <asm/olpc.h> |
| 25 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 26 | #define DRV_NAME "olpc-xo1-pm" |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 27 | |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 28 | static unsigned long acpi_base; |
| 29 | static unsigned long pms_base; |
| 30 | |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 31 | static u16 wakeup_mask = CS5536_PM_PWRBTN; |
| 32 | |
| 33 | static struct { |
| 34 | unsigned long address; |
| 35 | unsigned short segment; |
| 36 | } ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS }; |
| 37 | |
| 38 | /* Set bits in the wakeup mask */ |
| 39 | void olpc_xo1_pm_wakeup_set(u16 value) |
| 40 | { |
| 41 | wakeup_mask |= value; |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set); |
| 44 | |
| 45 | /* Clear bits in the wakeup mask */ |
| 46 | void olpc_xo1_pm_wakeup_clear(u16 value) |
| 47 | { |
| 48 | wakeup_mask &= ~value; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear); |
| 51 | |
| 52 | static int xo1_power_state_enter(suspend_state_t pm_state) |
| 53 | { |
| 54 | unsigned long saved_sci_mask; |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 55 | |
| 56 | /* Only STR is supported */ |
| 57 | if (pm_state != PM_SUSPEND_MEM) |
| 58 | return -EINVAL; |
| 59 | |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 60 | /* |
| 61 | * Save SCI mask (this gets lost since PM1_EN is used as a mask for |
| 62 | * wakeup events, which is not necessarily the same event set) |
| 63 | */ |
| 64 | saved_sci_mask = inl(acpi_base + CS5536_PM1_STS); |
| 65 | saved_sci_mask &= 0xffff0000; |
| 66 | |
| 67 | /* Save CPU state */ |
| 68 | do_olpc_suspend_lowlevel(); |
| 69 | |
| 70 | /* Resume path starts here */ |
| 71 | |
| 72 | /* Restore SCI mask (using dword access to CS5536_PM1_EN) */ |
| 73 | outl(saved_sci_mask, acpi_base + CS5536_PM1_STS); |
| 74 | |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | asmlinkage int xo1_do_sleep(u8 sleep_state) |
| 79 | { |
| 80 | void *pgd_addr = __va(read_cr3()); |
| 81 | |
| 82 | /* Program wakeup mask (using dword access to CS5536_PM1_EN) */ |
| 83 | outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS); |
| 84 | |
| 85 | __asm__("movl %0,%%eax" : : "r" (pgd_addr)); |
| 86 | __asm__("call *(%%edi); cld" |
| 87 | : : "D" (&ofw_bios_entry)); |
| 88 | __asm__("movb $0x34, %al\n\t" |
| 89 | "outb %al, $0x70\n\t" |
| 90 | "movb $0x30, %al\n\t" |
| 91 | "outb %al, $0x71\n\t"); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 95 | static void xo1_power_off(void) |
| 96 | { |
| 97 | printk(KERN_INFO "OLPC XO-1 power off sequence...\n"); |
| 98 | |
| 99 | /* Enable all of these controls with 0 delay */ |
Daniel Drake | 7a0d4fc | 2011-06-25 17:34:09 +0100 | [diff] [blame] | 100 | outl(0x40000000, pms_base + CS5536_PM_SCLK); |
| 101 | outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL); |
| 102 | outl(0x40000000, pms_base + CS5536_PM_WKXD); |
| 103 | outl(0x40000000, pms_base + CS5536_PM_WKD); |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 104 | |
| 105 | /* Clear status bits (possibly unnecessary) */ |
Daniel Drake | 7a0d4fc | 2011-06-25 17:34:09 +0100 | [diff] [blame] | 106 | outl(0x0002ffff, pms_base + CS5536_PM_SSC); |
| 107 | outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS); |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 108 | |
| 109 | /* Write SLP_EN bit to start the machinery */ |
Daniel Drake | 7a0d4fc | 2011-06-25 17:34:09 +0100 | [diff] [blame] | 110 | outl(0x00002000, acpi_base + CS5536_PM1_CNT); |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 113 | static int xo1_power_state_valid(suspend_state_t pm_state) |
| 114 | { |
| 115 | /* suspend-to-RAM only */ |
| 116 | return pm_state == PM_SUSPEND_MEM; |
| 117 | } |
| 118 | |
| 119 | static const struct platform_suspend_ops xo1_suspend_ops = { |
| 120 | .valid = xo1_power_state_valid, |
| 121 | .enter = xo1_power_state_enter, |
| 122 | }; |
| 123 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 124 | static int __devinit xo1_pm_probe(struct platform_device *pdev) |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 125 | { |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 126 | struct resource *res; |
Andres Salomon | 1310e6d | 2011-02-17 19:07:36 -0800 | [diff] [blame] | 127 | int err; |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 128 | |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 129 | /* don't run on non-XOs */ |
| 130 | if (!machine_is_olpc()) |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 131 | return -ENODEV; |
| 132 | |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 133 | err = mfd_cell_enable(pdev); |
Andres Salomon | 1310e6d | 2011-02-17 19:07:36 -0800 | [diff] [blame] | 134 | if (err) |
| 135 | return err; |
| 136 | |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 137 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 138 | if (!res) { |
| 139 | dev_err(&pdev->dev, "can't fetch device resource info\n"); |
| 140 | return -EIO; |
| 141 | } |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 142 | if (strcmp(pdev->name, "cs5535-pms") == 0) |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 143 | pms_base = res->start; |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 144 | else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0) |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 145 | acpi_base = res->start; |
| 146 | |
| 147 | /* If we have both addresses, we can override the poweroff hook */ |
| 148 | if (pms_base && acpi_base) { |
Daniel Drake | 97c4cb7 | 2011-06-25 17:34:11 +0100 | [diff] [blame] | 149 | suspend_set_ops(&xo1_suspend_ops); |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 150 | pm_power_off = xo1_power_off; |
| 151 | printk(KERN_INFO "OLPC XO-1 support registered\n"); |
| 152 | } |
| 153 | |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 157 | static int __devexit xo1_pm_remove(struct platform_device *pdev) |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 158 | { |
Andres Salomon | f77289a | 2011-03-03 09:51:58 -0800 | [diff] [blame] | 159 | mfd_cell_disable(pdev); |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 160 | |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 161 | if (strcmp(pdev->name, "cs5535-pms") == 0) |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 162 | pms_base = 0; |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 163 | else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0) |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 164 | acpi_base = 0; |
| 165 | |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 166 | pm_power_off = NULL; |
| 167 | return 0; |
| 168 | } |
| 169 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 170 | static struct platform_driver cs5535_pms_driver = { |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 171 | .driver = { |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 172 | .name = "cs5535-pms", |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 173 | .owner = THIS_MODULE, |
| 174 | }, |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 175 | .probe = xo1_pm_probe, |
| 176 | .remove = __devexit_p(xo1_pm_remove), |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 179 | static struct platform_driver cs5535_acpi_driver = { |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 180 | .driver = { |
Daniel Drake | adfa4bd | 2011-03-22 13:50:39 -0700 | [diff] [blame] | 181 | .name = "olpc-xo1-pm-acpi", |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 182 | .owner = THIS_MODULE, |
| 183 | }, |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 184 | .probe = xo1_pm_probe, |
| 185 | .remove = __devexit_p(xo1_pm_remove), |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 186 | }; |
| 187 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 188 | static int __init xo1_pm_init(void) |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 189 | { |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 190 | int r; |
| 191 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 192 | r = platform_driver_register(&cs5535_pms_driver); |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 193 | if (r) |
| 194 | return r; |
| 195 | |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 196 | r = platform_driver_register(&cs5535_acpi_driver); |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 197 | if (r) |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 198 | platform_driver_unregister(&cs5535_pms_driver); |
Andres Salomon | 419cdc5 | 2010-11-29 15:45:06 -0800 | [diff] [blame] | 199 | |
| 200 | return r; |
Daniel Drake | bf1ebf0 | 2010-10-10 10:40:32 +0100 | [diff] [blame] | 201 | } |
Daniel Drake | a312858 | 2011-06-25 17:34:10 +0100 | [diff] [blame] | 202 | arch_initcall(xo1_pm_init); |