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