2 * Support for OLPC XO-1.5 System Control Interrupts (SCI)
4 * Copyright (C) 2009-2010 One Laptop per Child
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.
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/workqueue.h>
15 #include <linux/power_supply.h>
17 #include <acpi/acpi_bus.h>
18 #include <acpi/acpi_drivers.h>
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"
26 static unsigned long xo15_sci_gpe;
27 static bool lid_wake_on_close;
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.
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.
38 * This is controlled through a custom method in the XO-1.5 DSDT.
40 static int set_lid_wake_behavior(bool wake_on_close)
42 struct acpi_object_list arg_list;
43 union acpi_object arg;
47 arg_list.pointer = &arg;
48 arg.type = ACPI_TYPE_INTEGER;
49 arg.integer.value = wake_on_close;
51 status = acpi_evaluate_object(NULL, "\\_SB.PCI0.LID.LIDW", &arg_list, NULL);
52 if (ACPI_FAILURE(status)) {
53 pr_warning(PFX "failed to set lid behavior\n");
57 lid_wake_on_close = wake_on_close;
63 lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
65 return sprintf(buf, "%u\n", lid_wake_on_close);
68 static ssize_t lid_wake_on_close_store(struct kobject *s,
69 struct kobj_attribute *attr,
70 const char *buf, size_t n)
74 if (sscanf(buf, "%u", &val) != 1)
77 set_lid_wake_behavior(!!val);
82 static struct kobj_attribute lid_wake_on_close_attr =
83 __ATTR(lid_wake_on_close, 0644,
84 lid_wake_on_close_show,
85 lid_wake_on_close_store);
87 static void battery_status_changed(void)
89 struct power_supply *psy = power_supply_get_by_name("olpc-battery");
92 power_supply_changed(psy);
97 static void ac_status_changed(void)
99 struct power_supply *psy = power_supply_get_by_name("olpc-ac");
102 power_supply_changed(psy);
103 put_device(psy->dev);
107 static void process_sci_queue(void)
113 r = olpc_ec_sci_query(&data);
117 pr_debug(PFX "SCI 0x%x received\n", data);
120 case EC_SCI_SRC_BATERR:
121 case EC_SCI_SRC_BATSOC:
122 case EC_SCI_SRC_BATTERY:
123 case EC_SCI_SRC_BATCRIT:
124 battery_status_changed();
126 case EC_SCI_SRC_ACPWR:
133 pr_err(PFX "Failed to clear SCI queue");
136 static void process_sci_queue_work(struct work_struct *work)
141 static DECLARE_WORK(sci_work, process_sci_queue_work);
143 static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
145 schedule_work(&sci_work);
146 return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
149 static int xo15_sci_add(struct acpi_device *device)
151 unsigned long long tmp;
158 strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
159 strcpy(acpi_device_class(device), XO15_SCI_CLASS);
161 /* Get GPE bit assignment (EC events). */
162 status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
163 if (ACPI_FAILURE(status))
167 status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
168 ACPI_GPE_EDGE_TRIGGERED,
169 xo15_sci_gpe_handler, device);
170 if (ACPI_FAILURE(status))
173 dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
175 r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
179 /* Flush queue, and enable all SCI events */
181 olpc_ec_mask_write(EC_SCI_SRC_ALL);
183 acpi_enable_gpe(NULL, xo15_sci_gpe);
185 /* Enable wake-on-EC */
186 if (device->wakeup.flags.valid)
187 device_init_wakeup(&device->dev, true);
192 acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
193 cancel_work_sync(&sci_work);
197 static int xo15_sci_remove(struct acpi_device *device, int type)
199 acpi_disable_gpe(NULL, xo15_sci_gpe);
200 acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
201 cancel_work_sync(&sci_work);
202 sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
206 static int xo15_sci_resume(struct device *dev)
208 /* Enable all EC events */
209 olpc_ec_mask_write(EC_SCI_SRC_ALL);
211 /* Power/battery status might have changed */
212 battery_status_changed();
218 static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
220 static const struct acpi_device_id xo15_sci_device_ids[] = {
225 static struct acpi_driver xo15_sci_drv = {
227 .class = XO15_SCI_CLASS,
228 .ids = xo15_sci_device_ids,
231 .remove = xo15_sci_remove,
233 .drv.pm = &xo15_sci_pm,
236 static int __init xo15_sci_init(void)
238 return acpi_bus_register_driver(&xo15_sci_drv);
240 device_initcall(xo15_sci_init);