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/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/olpc-ec.h>
15
16 #include <linux/acpi.h>
17 #include <asm/olpc.h>
18
19 #define DRV_NAME "olpc-xo15-sci"
20 #define PFX DRV_NAME ": "
21
22 static unsigned long xo15_sci_gpe;
23 static 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 */
set_lid_wake_behavior(bool wake_on_close)36 static 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
51 static ssize_t
lid_wake_on_close_show(struct kobject * s,struct kobj_attribute * attr,char * buf)52 lid_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
lid_wake_on_close_store(struct kobject * s,struct kobj_attribute * attr,const char * buf,size_t n)57 static 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
71 static 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
battery_status_changed(void)76 static 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
ac_status_changed(void)86 static 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
process_sci_queue(void)96 static 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
process_sci_queue_work(struct work_struct * work)125 static void process_sci_queue_work(struct work_struct *work)
126 {
127 process_sci_queue();
128 }
129
130 static DECLARE_WORK(sci_work, process_sci_queue_work);
131
xo15_sci_gpe_handler(acpi_handle gpe_device,u32 gpe,void * context)132 static 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
xo15_sci_probe(struct platform_device * pdev)138 static int xo15_sci_probe(struct platform_device *pdev)
139 {
140 struct acpi_device *device;
141 unsigned long long tmp;
142 acpi_status status;
143 int r;
144
145 device = ACPI_COMPANION(&pdev->dev);
146 if (!device)
147 return -ENODEV;
148
149 /* Get GPE bit assignment (EC events). */
150 status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
151 if (ACPI_FAILURE(status))
152 return -EINVAL;
153
154 xo15_sci_gpe = tmp;
155 status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
156 ACPI_GPE_EDGE_TRIGGERED,
157 xo15_sci_gpe_handler, device);
158 if (ACPI_FAILURE(status))
159 return -ENODEV;
160
161 dev_info(&pdev->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
162
163 r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
164 if (r)
165 goto err_sysfs;
166
167 /* Flush queue, and enable all SCI events */
168 process_sci_queue();
169 olpc_ec_mask_write(EC_SCI_SRC_ALL);
170
171 acpi_enable_gpe(NULL, xo15_sci_gpe);
172
173 /* Enable wake-on-EC */
174 if (device->wakeup.flags.valid)
175 device_init_wakeup(&pdev->dev, true);
176
177 return 0;
178
179 err_sysfs:
180 acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
181 cancel_work_sync(&sci_work);
182 return r;
183 }
184
xo15_sci_remove(struct platform_device * pdev)185 static void xo15_sci_remove(struct platform_device *pdev)
186 {
187 struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
188
189 device_init_wakeup(&pdev->dev, false);
190 acpi_disable_gpe(NULL, xo15_sci_gpe);
191 acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
192 cancel_work_sync(&sci_work);
193 sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
194 }
195
196 #ifdef CONFIG_PM_SLEEP
xo15_sci_resume(struct device * dev)197 static int xo15_sci_resume(struct device *dev)
198 {
199 /* Enable all EC events */
200 olpc_ec_mask_write(EC_SCI_SRC_ALL);
201
202 /* Power/battery status might have changed */
203 battery_status_changed();
204 ac_status_changed();
205
206 return 0;
207 }
208 #endif
209
210 static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
211
212 static const struct acpi_device_id xo15_sci_device_ids[] = {
213 {"XO15EC", 0},
214 {"", 0},
215 };
216
217 static struct platform_driver xo15_sci_drv = {
218 .probe = xo15_sci_probe,
219 .remove = xo15_sci_remove,
220 .driver = {
221 .name = DRV_NAME,
222 .acpi_match_table = xo15_sci_device_ids,
223 .pm = &xo15_sci_pm,
224 },
225 };
226
xo15_sci_init(void)227 static int __init xo15_sci_init(void)
228 {
229 return platform_driver_register(&xo15_sci_drv);
230 }
231 device_initcall(xo15_sci_init);
232