Path: blob/master/arch/x86/platform/olpc/olpc-xo15-sci.c
26489 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Support for OLPC XO-1.5 System Control Interrupts (SCI)3*4* Copyright (C) 2009-2010 One Laptop per Child5*/67#include <linux/device.h>8#include <linux/slab.h>9#include <linux/workqueue.h>10#include <linux/power_supply.h>11#include <linux/olpc-ec.h>1213#include <linux/acpi.h>14#include <asm/olpc.h>1516#define DRV_NAME "olpc-xo15-sci"17#define PFX DRV_NAME ": "18#define XO15_SCI_CLASS DRV_NAME19#define XO15_SCI_DEVICE_NAME "OLPC XO-1.5 SCI"2021static unsigned long xo15_sci_gpe;22static bool lid_wake_on_close;2324/*25* The normal ACPI LID wakeup behavior is wake-on-open, but not26* wake-on-close. This is implemented as standard by the XO-1.5 DSDT.27*28* We provide here a sysfs attribute that will additionally enable29* wake-on-close behavior. This is useful (e.g.) when we opportunistically30* suspend with the display running; if the lid is then closed, we want to31* wake up to turn the display off.32*33* This is controlled through a custom method in the XO-1.5 DSDT.34*/35static int set_lid_wake_behavior(bool wake_on_close)36{37acpi_status status;3839status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close);40if (ACPI_FAILURE(status)) {41pr_warn(PFX "failed to set lid behavior\n");42return 1;43}4445lid_wake_on_close = wake_on_close;4647return 0;48}4950static ssize_t51lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)52{53return sprintf(buf, "%u\n", lid_wake_on_close);54}5556static ssize_t lid_wake_on_close_store(struct kobject *s,57struct kobj_attribute *attr,58const char *buf, size_t n)59{60unsigned int val;6162if (sscanf(buf, "%u", &val) != 1)63return -EINVAL;6465set_lid_wake_behavior(!!val);6667return n;68}6970static struct kobj_attribute lid_wake_on_close_attr =71__ATTR(lid_wake_on_close, 0644,72lid_wake_on_close_show,73lid_wake_on_close_store);7475static void battery_status_changed(void)76{77struct power_supply *psy = power_supply_get_by_name("olpc_battery");7879if (psy) {80power_supply_changed(psy);81power_supply_put(psy);82}83}8485static void ac_status_changed(void)86{87struct power_supply *psy = power_supply_get_by_name("olpc_ac");8889if (psy) {90power_supply_changed(psy);91power_supply_put(psy);92}93}9495static void process_sci_queue(void)96{97u16 data;98int r;99100do {101r = olpc_ec_sci_query(&data);102if (r || !data)103break;104105pr_debug(PFX "SCI 0x%x received\n", data);106107switch (data) {108case EC_SCI_SRC_BATERR:109case EC_SCI_SRC_BATSOC:110case EC_SCI_SRC_BATTERY:111case EC_SCI_SRC_BATCRIT:112battery_status_changed();113break;114case EC_SCI_SRC_ACPWR:115ac_status_changed();116break;117}118} while (data);119120if (r)121pr_err(PFX "Failed to clear SCI queue");122}123124static void process_sci_queue_work(struct work_struct *work)125{126process_sci_queue();127}128129static DECLARE_WORK(sci_work, process_sci_queue_work);130131static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)132{133schedule_work(&sci_work);134return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;135}136137static int xo15_sci_add(struct acpi_device *device)138{139unsigned long long tmp;140acpi_status status;141int r;142143if (!device)144return -EINVAL;145146strcpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);147strcpy(acpi_device_class(device), XO15_SCI_CLASS);148149/* Get GPE bit assignment (EC events). */150status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);151if (ACPI_FAILURE(status))152return -EINVAL;153154xo15_sci_gpe = tmp;155status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,156ACPI_GPE_EDGE_TRIGGERED,157xo15_sci_gpe_handler, device);158if (ACPI_FAILURE(status))159return -ENODEV;160161dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);162163r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);164if (r)165goto err_sysfs;166167/* Flush queue, and enable all SCI events */168process_sci_queue();169olpc_ec_mask_write(EC_SCI_SRC_ALL);170171acpi_enable_gpe(NULL, xo15_sci_gpe);172173/* Enable wake-on-EC */174if (device->wakeup.flags.valid)175device_init_wakeup(&device->dev, true);176177return 0;178179err_sysfs:180acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);181cancel_work_sync(&sci_work);182return r;183}184185static void xo15_sci_remove(struct acpi_device *device)186{187acpi_disable_gpe(NULL, xo15_sci_gpe);188acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);189cancel_work_sync(&sci_work);190sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);191}192193#ifdef CONFIG_PM_SLEEP194static int xo15_sci_resume(struct device *dev)195{196/* Enable all EC events */197olpc_ec_mask_write(EC_SCI_SRC_ALL);198199/* Power/battery status might have changed */200battery_status_changed();201ac_status_changed();202203return 0;204}205#endif206207static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);208209static const struct acpi_device_id xo15_sci_device_ids[] = {210{"XO15EC", 0},211{"", 0},212};213214static struct acpi_driver xo15_sci_drv = {215.name = DRV_NAME,216.class = XO15_SCI_CLASS,217.ids = xo15_sci_device_ids,218.ops = {219.add = xo15_sci_add,220.remove = xo15_sci_remove,221},222.drv.pm = &xo15_sci_pm,223};224225static int __init xo15_sci_init(void)226{227return acpi_bus_register_driver(&xo15_sci_drv);228}229device_initcall(xo15_sci_init);230231232