Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Support for OLPC XO-1.5 System Control Interrupts (SCI)
0004  *
0005  * Copyright (C) 2009-2010 One Laptop per Child
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/slab.h>
0010 #include <linux/workqueue.h>
0011 #include <linux/power_supply.h>
0012 #include <linux/olpc-ec.h>
0013 
0014 #include <linux/acpi.h>
0015 #include <asm/olpc.h>
0016 
0017 #define DRV_NAME            "olpc-xo15-sci"
0018 #define PFX             DRV_NAME ": "
0019 #define XO15_SCI_CLASS          DRV_NAME
0020 #define XO15_SCI_DEVICE_NAME        "OLPC XO-1.5 SCI"
0021 
0022 static unsigned long            xo15_sci_gpe;
0023 static bool             lid_wake_on_close;
0024 
0025 /*
0026  * The normal ACPI LID wakeup behavior is wake-on-open, but not
0027  * wake-on-close. This is implemented as standard by the XO-1.5 DSDT.
0028  *
0029  * We provide here a sysfs attribute that will additionally enable
0030  * wake-on-close behavior. This is useful (e.g.) when we opportunistically
0031  * suspend with the display running; if the lid is then closed, we want to
0032  * wake up to turn the display off.
0033  *
0034  * This is controlled through a custom method in the XO-1.5 DSDT.
0035  */
0036 static int set_lid_wake_behavior(bool wake_on_close)
0037 {
0038     acpi_status status;
0039 
0040     status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close);
0041     if (ACPI_FAILURE(status)) {
0042         pr_warn(PFX "failed to set lid behavior\n");
0043         return 1;
0044     }
0045 
0046     lid_wake_on_close = wake_on_close;
0047 
0048     return 0;
0049 }
0050 
0051 static ssize_t
0052 lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)
0053 {
0054     return sprintf(buf, "%u\n", lid_wake_on_close);
0055 }
0056 
0057 static ssize_t lid_wake_on_close_store(struct kobject *s,
0058                        struct kobj_attribute *attr,
0059                        const char *buf, size_t n)
0060 {
0061     unsigned int val;
0062 
0063     if (sscanf(buf, "%u", &val) != 1)
0064         return -EINVAL;
0065 
0066     set_lid_wake_behavior(!!val);
0067 
0068     return n;
0069 }
0070 
0071 static struct kobj_attribute lid_wake_on_close_attr =
0072     __ATTR(lid_wake_on_close, 0644,
0073            lid_wake_on_close_show,
0074            lid_wake_on_close_store);
0075 
0076 static void battery_status_changed(void)
0077 {
0078     struct power_supply *psy = power_supply_get_by_name("olpc_battery");
0079 
0080     if (psy) {
0081         power_supply_changed(psy);
0082         power_supply_put(psy);
0083     }
0084 }
0085 
0086 static void ac_status_changed(void)
0087 {
0088     struct power_supply *psy = power_supply_get_by_name("olpc_ac");
0089 
0090     if (psy) {
0091         power_supply_changed(psy);
0092         power_supply_put(psy);
0093     }
0094 }
0095 
0096 static void process_sci_queue(void)
0097 {
0098     u16 data;
0099     int r;
0100 
0101     do {
0102         r = olpc_ec_sci_query(&data);
0103         if (r || !data)
0104             break;
0105 
0106         pr_debug(PFX "SCI 0x%x received\n", data);
0107 
0108         switch (data) {
0109         case EC_SCI_SRC_BATERR:
0110         case EC_SCI_SRC_BATSOC:
0111         case EC_SCI_SRC_BATTERY:
0112         case EC_SCI_SRC_BATCRIT:
0113             battery_status_changed();
0114             break;
0115         case EC_SCI_SRC_ACPWR:
0116             ac_status_changed();
0117             break;
0118         }
0119     } while (data);
0120 
0121     if (r)
0122         pr_err(PFX "Failed to clear SCI queue");
0123 }
0124 
0125 static void process_sci_queue_work(struct work_struct *work)
0126 {
0127     process_sci_queue();
0128 }
0129 
0130 static DECLARE_WORK(sci_work, process_sci_queue_work);
0131 
0132 static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)
0133 {
0134     schedule_work(&sci_work);
0135     return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
0136 }
0137 
0138 static int xo15_sci_add(struct acpi_device *device)
0139 {
0140     unsigned long long tmp;
0141     acpi_status status;
0142     int r;
0143 
0144     if (!device)
0145         return -EINVAL;
0146 
0147     strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);
0148     strcpy(acpi_device_class(device), XO15_SCI_CLASS);
0149 
0150     /* Get GPE bit assignment (EC events). */
0151     status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);
0152     if (ACPI_FAILURE(status))
0153         return -EINVAL;
0154 
0155     xo15_sci_gpe = tmp;
0156     status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,
0157                       ACPI_GPE_EDGE_TRIGGERED,
0158                       xo15_sci_gpe_handler, device);
0159     if (ACPI_FAILURE(status))
0160         return -ENODEV;
0161 
0162     dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);
0163 
0164     r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
0165     if (r)
0166         goto err_sysfs;
0167 
0168     /* Flush queue, and enable all SCI events */
0169     process_sci_queue();
0170     olpc_ec_mask_write(EC_SCI_SRC_ALL);
0171 
0172     acpi_enable_gpe(NULL, xo15_sci_gpe);
0173 
0174     /* Enable wake-on-EC */
0175     if (device->wakeup.flags.valid)
0176         device_init_wakeup(&device->dev, true);
0177 
0178     return 0;
0179 
0180 err_sysfs:
0181     acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
0182     cancel_work_sync(&sci_work);
0183     return r;
0184 }
0185 
0186 static int xo15_sci_remove(struct acpi_device *device)
0187 {
0188     acpi_disable_gpe(NULL, xo15_sci_gpe);
0189     acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);
0190     cancel_work_sync(&sci_work);
0191     sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);
0192     return 0;
0193 }
0194 
0195 #ifdef CONFIG_PM_SLEEP
0196 static int xo15_sci_resume(struct device *dev)
0197 {
0198     /* Enable all EC events */
0199     olpc_ec_mask_write(EC_SCI_SRC_ALL);
0200 
0201     /* Power/battery status might have changed */
0202     battery_status_changed();
0203     ac_status_changed();
0204 
0205     return 0;
0206 }
0207 #endif
0208 
0209 static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);
0210 
0211 static const struct acpi_device_id xo15_sci_device_ids[] = {
0212     {"XO15EC", 0},
0213     {"", 0},
0214 };
0215 
0216 static struct acpi_driver xo15_sci_drv = {
0217     .name = DRV_NAME,
0218     .class = XO15_SCI_CLASS,
0219     .ids = xo15_sci_device_ids,
0220     .ops = {
0221         .add = xo15_sci_add,
0222         .remove = xo15_sci_remove,
0223     },
0224     .drv.pm = &xo15_sci_pm,
0225 };
0226 
0227 static int __init xo15_sci_init(void)
0228 {
0229     return acpi_bus_register_driver(&xo15_sci_drv);
0230 }
0231 device_initcall(xo15_sci_init);