blob: 6f3855a5a2f783dce23282d5a1fa08bf03d9e4b4 [file] [log] [blame]
Daniel Drakebf1ebf02010-10-10 10:40:32 +01001/*
Daniel Drakea3128582011-06-25 17:34:10 +01002 * Support for power management features of the OLPC XO-1 laptop
Daniel Drakebf1ebf02010-10-10 10:40:32 +01003 *
Andres Salomon419cdc52010-11-29 15:45:06 -08004 * Copyright (C) 2010 Andres Salomon <dilinger@queued.net>
Daniel Drakebf1ebf02010-10-10 10:40:32 +01005 * 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 Drake7a0d4fc2011-06-25 17:34:09 +010015#include <linux/cs5535.h>
Daniel Drakebf1ebf02010-10-10 10:40:32 +010016#include <linux/platform_device.h>
17#include <linux/pm.h>
Andres Salomon1310e6d2011-02-17 19:07:36 -080018#include <linux/mfd/core.h>
Daniel Drake97c4cb72011-06-25 17:34:11 +010019#include <linux/suspend.h>
Daniel Drakebf1ebf02010-10-10 10:40:32 +010020
21#include <asm/io.h>
22#include <asm/olpc.h>
23
Daniel Drakea3128582011-06-25 17:34:10 +010024#define DRV_NAME "olpc-xo1-pm"
Daniel Drakebf1ebf02010-10-10 10:40:32 +010025
Daniel Drakebf1ebf02010-10-10 10:40:32 +010026static unsigned long acpi_base;
27static unsigned long pms_base;
28
Daniel Drake97c4cb72011-06-25 17:34:11 +010029static u16 wakeup_mask = CS5536_PM_PWRBTN;
30
31static struct {
32 unsigned long address;
33 unsigned short segment;
34} ofw_bios_entry = { 0xF0000 + PAGE_OFFSET, __KERNEL_CS };
35
36/* Set bits in the wakeup mask */
37void olpc_xo1_pm_wakeup_set(u16 value)
38{
39 wakeup_mask |= value;
40}
41EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_set);
42
43/* Clear bits in the wakeup mask */
44void olpc_xo1_pm_wakeup_clear(u16 value)
45{
46 wakeup_mask &= ~value;
47}
48EXPORT_SYMBOL_GPL(olpc_xo1_pm_wakeup_clear);
49
50static int xo1_power_state_enter(suspend_state_t pm_state)
51{
52 unsigned long saved_sci_mask;
53 int r;
54
55 /* Only STR is supported */
56 if (pm_state != PM_SUSPEND_MEM)
57 return -EINVAL;
58
59 r = olpc_ec_cmd(EC_SET_SCI_INHIBIT, NULL, 0, NULL, 0);
60 if (r)
61 return r;
62
63 /*
64 * Save SCI mask (this gets lost since PM1_EN is used as a mask for
65 * wakeup events, which is not necessarily the same event set)
66 */
67 saved_sci_mask = inl(acpi_base + CS5536_PM1_STS);
68 saved_sci_mask &= 0xffff0000;
69
70 /* Save CPU state */
71 do_olpc_suspend_lowlevel();
72
73 /* Resume path starts here */
74
75 /* Restore SCI mask (using dword access to CS5536_PM1_EN) */
76 outl(saved_sci_mask, acpi_base + CS5536_PM1_STS);
77
78 /* Tell the EC to stop inhibiting SCIs */
79 olpc_ec_cmd(EC_SET_SCI_INHIBIT_RELEASE, NULL, 0, NULL, 0);
80
81 /*
82 * Tell the wireless module to restart USB communication.
83 * Must be done twice.
84 */
85 olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
86 olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
87
88 return 0;
89}
90
91asmlinkage int xo1_do_sleep(u8 sleep_state)
92{
93 void *pgd_addr = __va(read_cr3());
94
95 /* Program wakeup mask (using dword access to CS5536_PM1_EN) */
96 outl(wakeup_mask << 16, acpi_base + CS5536_PM1_STS);
97
98 __asm__("movl %0,%%eax" : : "r" (pgd_addr));
99 __asm__("call *(%%edi); cld"
100 : : "D" (&ofw_bios_entry));
101 __asm__("movb $0x34, %al\n\t"
102 "outb %al, $0x70\n\t"
103 "movb $0x30, %al\n\t"
104 "outb %al, $0x71\n\t");
105 return 0;
106}
107
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100108static void xo1_power_off(void)
109{
110 printk(KERN_INFO "OLPC XO-1 power off sequence...\n");
111
112 /* Enable all of these controls with 0 delay */
Daniel Drake7a0d4fc2011-06-25 17:34:09 +0100113 outl(0x40000000, pms_base + CS5536_PM_SCLK);
114 outl(0x40000000, pms_base + CS5536_PM_IN_SLPCTL);
115 outl(0x40000000, pms_base + CS5536_PM_WKXD);
116 outl(0x40000000, pms_base + CS5536_PM_WKD);
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100117
118 /* Clear status bits (possibly unnecessary) */
Daniel Drake7a0d4fc2011-06-25 17:34:09 +0100119 outl(0x0002ffff, pms_base + CS5536_PM_SSC);
120 outl(0xffffffff, acpi_base + CS5536_PM_GPE0_STS);
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100121
122 /* Write SLP_EN bit to start the machinery */
Daniel Drake7a0d4fc2011-06-25 17:34:09 +0100123 outl(0x00002000, acpi_base + CS5536_PM1_CNT);
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100124}
125
Daniel Drake97c4cb72011-06-25 17:34:11 +0100126static int xo1_power_state_valid(suspend_state_t pm_state)
127{
128 /* suspend-to-RAM only */
129 return pm_state == PM_SUSPEND_MEM;
130}
131
132static const struct platform_suspend_ops xo1_suspend_ops = {
133 .valid = xo1_power_state_valid,
134 .enter = xo1_power_state_enter,
135};
136
Daniel Drakea3128582011-06-25 17:34:10 +0100137static int __devinit xo1_pm_probe(struct platform_device *pdev)
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100138{
Andres Salomon419cdc52010-11-29 15:45:06 -0800139 struct resource *res;
Andres Salomon1310e6d2011-02-17 19:07:36 -0800140 int err;
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100141
Andres Salomon419cdc52010-11-29 15:45:06 -0800142 /* don't run on non-XOs */
143 if (!machine_is_olpc())
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100144 return -ENODEV;
145
Andres Salomonf77289a2011-03-03 09:51:58 -0800146 err = mfd_cell_enable(pdev);
Andres Salomon1310e6d2011-02-17 19:07:36 -0800147 if (err)
148 return err;
149
Andres Salomon419cdc52010-11-29 15:45:06 -0800150 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
151 if (!res) {
152 dev_err(&pdev->dev, "can't fetch device resource info\n");
153 return -EIO;
154 }
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700155 if (strcmp(pdev->name, "cs5535-pms") == 0)
Andres Salomon419cdc52010-11-29 15:45:06 -0800156 pms_base = res->start;
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700157 else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
Andres Salomon419cdc52010-11-29 15:45:06 -0800158 acpi_base = res->start;
159
160 /* If we have both addresses, we can override the poweroff hook */
161 if (pms_base && acpi_base) {
Daniel Drake97c4cb72011-06-25 17:34:11 +0100162 suspend_set_ops(&xo1_suspend_ops);
Andres Salomon419cdc52010-11-29 15:45:06 -0800163 pm_power_off = xo1_power_off;
164 printk(KERN_INFO "OLPC XO-1 support registered\n");
165 }
166
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100167 return 0;
168}
169
Daniel Drakea3128582011-06-25 17:34:10 +0100170static int __devexit xo1_pm_remove(struct platform_device *pdev)
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100171{
Andres Salomonf77289a2011-03-03 09:51:58 -0800172 mfd_cell_disable(pdev);
Andres Salomon419cdc52010-11-29 15:45:06 -0800173
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700174 if (strcmp(pdev->name, "cs5535-pms") == 0)
Andres Salomon419cdc52010-11-29 15:45:06 -0800175 pms_base = 0;
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700176 else if (strcmp(pdev->name, "olpc-xo1-pm-acpi") == 0)
Andres Salomon419cdc52010-11-29 15:45:06 -0800177 acpi_base = 0;
178
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100179 pm_power_off = NULL;
180 return 0;
181}
182
Daniel Drakea3128582011-06-25 17:34:10 +0100183static struct platform_driver cs5535_pms_driver = {
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100184 .driver = {
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700185 .name = "cs5535-pms",
Andres Salomon419cdc52010-11-29 15:45:06 -0800186 .owner = THIS_MODULE,
187 },
Daniel Drakea3128582011-06-25 17:34:10 +0100188 .probe = xo1_pm_probe,
189 .remove = __devexit_p(xo1_pm_remove),
Andres Salomon419cdc52010-11-29 15:45:06 -0800190};
191
Daniel Drakea3128582011-06-25 17:34:10 +0100192static struct platform_driver cs5535_acpi_driver = {
Andres Salomon419cdc52010-11-29 15:45:06 -0800193 .driver = {
Daniel Drakeadfa4bd2011-03-22 13:50:39 -0700194 .name = "olpc-xo1-pm-acpi",
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100195 .owner = THIS_MODULE,
196 },
Daniel Drakea3128582011-06-25 17:34:10 +0100197 .probe = xo1_pm_probe,
198 .remove = __devexit_p(xo1_pm_remove),
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100199};
200
Daniel Drakea3128582011-06-25 17:34:10 +0100201static int __init xo1_pm_init(void)
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100202{
Andres Salomon419cdc52010-11-29 15:45:06 -0800203 int r;
204
Daniel Drakea3128582011-06-25 17:34:10 +0100205 r = platform_driver_register(&cs5535_pms_driver);
Andres Salomon419cdc52010-11-29 15:45:06 -0800206 if (r)
207 return r;
208
Daniel Drakea3128582011-06-25 17:34:10 +0100209 r = platform_driver_register(&cs5535_acpi_driver);
Andres Salomon419cdc52010-11-29 15:45:06 -0800210 if (r)
Daniel Drakea3128582011-06-25 17:34:10 +0100211 platform_driver_unregister(&cs5535_pms_driver);
Andres Salomon419cdc52010-11-29 15:45:06 -0800212
213 return r;
Daniel Drakebf1ebf02010-10-10 10:40:32 +0100214}
Daniel Drakea3128582011-06-25 17:34:10 +0100215arch_initcall(xo1_pm_init);