Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/platform/olpc/olpc-xo15-sci.c
51318 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
* Support for OLPC XO-1.5 System Control Interrupts (SCI)
4
*
5
* Copyright (C) 2009-2010 One Laptop per Child
6
*/
7
8
#include <linux/device.h>
9
#include <linux/slab.h>
10
#include <linux/string.h>
11
#include <linux/workqueue.h>
12
#include <linux/power_supply.h>
13
#include <linux/olpc-ec.h>
14
15
#include <linux/acpi.h>
16
#include <asm/olpc.h>
17
18
#define DRV_NAME "olpc-xo15-sci"
19
#define PFX DRV_NAME ": "
20
#define XO15_SCI_CLASS DRV_NAME
21
#define XO15_SCI_DEVICE_NAME "OLPC XO-1.5 SCI"
22
23
static unsigned long xo15_sci_gpe;
24
static bool lid_wake_on_close;
25
26
/*
27
* The normal ACPI LID wakeup behavior is wake-on-open, but not
28
* wake-on-close. This is implemented as standard by the XO-1.5 DSDT.
29
*
30
* We provide here a sysfs attribute that will additionally enable
31
* wake-on-close behavior. This is useful (e.g.) when we opportunistically
32
* suspend with the display running; if the lid is then closed, we want to
33
* wake up to turn the display off.
34
*
35
* This is controlled through a custom method in the XO-1.5 DSDT.
36
*/
37
static int set_lid_wake_behavior(bool wake_on_close)
38
{
39
acpi_status status;
40
41
status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close);
42
if (ACPI_FAILURE(status)) {
43
pr_warn(PFX "failed to set lid behavior\n");
44
return 1;
45
}
46
47
lid_wake_on_close = wake_on_close;
48
49
return 0;
50
}
51
52
static ssize_t
53
lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
54
{
55
return sprintf(buf, "%u\n", lid_wake_on_close);
56
}
57
58
static ssize_t lid_wake_on_close_store(struct kobject *s,
59
struct kobj_attribute *attr,
60
const char *buf, size_t n)
61
{
62
unsigned int val;
63
64
if (sscanf(buf, "%u", &val) != 1)
65
return -EINVAL;
66
67
set_lid_wake_behavior(!!val);
68
69
return n;
70
}
71
72
static struct kobj_attribute lid_wake_on_close_attr =
73
__ATTR(lid_wake_on_close, 0644,
74
lid_wake_on_close_show,
75
lid_wake_on_close_store);
76
77
static void battery_status_changed(void)
78
{
79
struct power_supply *psy = power_supply_get_by_name("olpc_battery");
80
81
if (psy) {
82
power_supply_changed(psy);
83
power_supply_put(psy);
84
}
85
}
86
87
static void ac_status_changed(void)
88
{
89
struct power_supply *psy = power_supply_get_by_name("olpc_ac");
90
91
if (psy) {
92
power_supply_changed(psy);
93
power_supply_put(psy);
94
}
95
}
96
97
static void process_sci_queue(void)
98
{
99
u16 data;
100
int r;
101
102
do {
103
r = olpc_ec_sci_query(&data);
104
if (r || !data)
105
break;
106
107
pr_debug(PFX "SCI 0x%x received\n", data);
108
109
switch (data) {
110
case EC_SCI_SRC_BATERR:
111
case EC_SCI_SRC_BATSOC:
112
case EC_SCI_SRC_BATTERY:
113
case EC_SCI_SRC_BATCRIT:
114
battery_status_changed();
115
break;
116
case EC_SCI_SRC_ACPWR:
117
ac_status_changed();
118
break;
119
}
120
} while (data);
121
122
if (r)
123
pr_err(PFX "Failed to clear SCI queue");
124
}
125
126
static void process_sci_queue_work(struct work_struct *work)
127
{
128
process_sci_queue();
129
}
130
131
static DECLARE_WORK(sci_work, process_sci_queue_work);
132
133
static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
134
{
135
schedule_work(&sci_work);
136
return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
137
}
138
139
static int xo15_sci_add(struct acpi_device *device)
140
{
141
unsigned long long tmp;
142
acpi_status status;
143
int r;
144
145
if (!device)
146
return -EINVAL;
147
148
strscpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
149
strscpy(acpi_device_class(device), XO15_SCI_CLASS);
150
151
/* Get GPE bit assignment (EC events). */
152
status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
153
if (ACPI_FAILURE(status))
154
return -EINVAL;
155
156
xo15_sci_gpe = tmp;
157
status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
158
ACPI_GPE_EDGE_TRIGGERED,
159
xo15_sci_gpe_handler, device);
160
if (ACPI_FAILURE(status))
161
return -ENODEV;
162
163
dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
164
165
r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
166
if (r)
167
goto err_sysfs;
168
169
/* Flush queue, and enable all SCI events */
170
process_sci_queue();
171
olpc_ec_mask_write(EC_SCI_SRC_ALL);
172
173
acpi_enable_gpe(NULL, xo15_sci_gpe);
174
175
/* Enable wake-on-EC */
176
if (device->wakeup.flags.valid)
177
device_init_wakeup(&device->dev, true);
178
179
return 0;
180
181
err_sysfs:
182
acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
183
cancel_work_sync(&sci_work);
184
return r;
185
}
186
187
static void xo15_sci_remove(struct acpi_device *device)
188
{
189
acpi_disable_gpe(NULL, xo15_sci_gpe);
190
acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
191
cancel_work_sync(&sci_work);
192
sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
193
}
194
195
#ifdef CONFIG_PM_SLEEP
196
static int xo15_sci_resume(struct device *dev)
197
{
198
/* Enable all EC events */
199
olpc_ec_mask_write(EC_SCI_SRC_ALL);
200
201
/* Power/battery status might have changed */
202
battery_status_changed();
203
ac_status_changed();
204
205
return 0;
206
}
207
#endif
208
209
static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
210
211
static const struct acpi_device_id xo15_sci_device_ids[] = {
212
{"XO15EC", 0},
213
{"", 0},
214
};
215
216
static struct acpi_driver xo15_sci_drv = {
217
.name = DRV_NAME,
218
.class = XO15_SCI_CLASS,
219
.ids = xo15_sci_device_ids,
220
.ops = {
221
.add = xo15_sci_add,
222
.remove = xo15_sci_remove,
223
},
224
.drv.pm = &xo15_sci_pm,
225
};
226
227
static int __init xo15_sci_init(void)
228
{
229
return acpi_bus_register_driver(&xo15_sci_drv);
230
}
231
device_initcall(xo15_sci_init);
232
233