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