Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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> |
Andres Salomon | 3bf9428 | 2012-07-11 01:16:29 -0700 | [diff] [blame] | 16 | #include <linux/olpc-ec.h> |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 17 | |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 18 | #include <linux/acpi.h> |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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 | |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 26 | static unsigned long xo15_sci_gpe; |
| 27 | static 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 | */ |
| 40 | static int set_lid_wake_behavior(bool wake_on_close) |
| 41 | { |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 42 | acpi_status status; |
| 43 | |
Zhang Rui | b408a05 | 2013-09-03 08:31:49 +0800 | [diff] [blame] | 44 | status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close); |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 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 | |
| 55 | static ssize_t |
| 56 | lid_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 | |
| 61 | static 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 | |
| 75 | static 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); |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 79 | |
| 80 | static 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); |
Krzysztof Kozlowski | 67273a1 | 2015-03-12 08:44:16 +0100 | [diff] [blame] | 86 | power_supply_put(psy); |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | static 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); |
Krzysztof Kozlowski | 67273a1 | 2015-03-12 08:44:16 +0100 | [diff] [blame] | 96 | power_supply_put(psy); |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | static 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 | |
| 129 | static void process_sci_queue_work(struct work_struct *work) |
| 130 | { |
| 131 | process_sci_queue(); |
| 132 | } |
| 133 | |
| 134 | static DECLARE_WORK(sci_work, process_sci_queue_work); |
| 135 | |
| 136 | static 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 | |
| 142 | static int xo15_sci_add(struct acpi_device *device) |
| 143 | { |
| 144 | unsigned long long tmp; |
| 145 | acpi_status status; |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 146 | int r; |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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 | |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 168 | r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr); |
| 169 | if (r) |
| 170 | goto err_sysfs; |
| 171 | |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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) |
Daniel Drake | 07d5b38 | 2011-07-24 18:34:30 +0100 | [diff] [blame] | 180 | device_init_wakeup(&device->dev, true); |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 181 | |
| 182 | return 0; |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 183 | |
| 184 | err_sysfs: |
| 185 | acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler); |
| 186 | cancel_work_sync(&sci_work); |
| 187 | return r; |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 190 | static int xo15_sci_remove(struct acpi_device *device) |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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); |
Daniel Drake | d1f42e3 | 2012-03-05 15:01:00 -0800 | [diff] [blame] | 195 | sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr); |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Borislav Petkov | 20ab667 | 2016-11-26 15:27:06 +0100 | [diff] [blame] | 199 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 1846884 | 2012-07-12 22:36:28 +0200 | [diff] [blame] | 200 | static int xo15_sci_resume(struct device *dev) |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 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 | } |
Borislav Petkov | 20ab667 | 2016-11-26 15:27:06 +0100 | [diff] [blame] | 211 | #endif |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 212 | |
Rafael J. Wysocki | 1846884 | 2012-07-12 22:36:28 +0200 | [diff] [blame] | 213 | static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume); |
| 214 | |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 215 | static const struct acpi_device_id xo15_sci_device_ids[] = { |
| 216 | {"XO15EC", 0}, |
| 217 | {"", 0}, |
| 218 | }; |
| 219 | |
| 220 | static 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, |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 227 | }, |
Rafael J. Wysocki | 1846884 | 2012-07-12 22:36:28 +0200 | [diff] [blame] | 228 | .drv.pm = &xo15_sci_pm, |
Daniel Drake | a0f30f5 | 2011-06-25 17:34:18 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | static int __init xo15_sci_init(void) |
| 232 | { |
| 233 | return acpi_bus_register_driver(&xo15_sci_drv); |
| 234 | } |
| 235 | device_initcall(xo15_sci_init); |