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