Path: blob/master/arch/x86/platform/olpc/olpc-xo15-sci.c
51318 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/string.h>10#include <linux/workqueue.h>11#include <linux/power_supply.h>12#include <linux/olpc-ec.h>1314#include <linux/acpi.h>15#include <asm/olpc.h>1617#define DRV_NAME "olpc-xo15-sci"18#define PFX DRV_NAME ": "19#define XO15_SCI_CLASS DRV_NAME20#define XO15_SCI_DEVICE_NAME "OLPC XO-1.5 SCI"2122static unsigned long xo15_sci_gpe;23static bool lid_wake_on_close;2425/*26* The normal ACPI LID wakeup behavior is wake-on-open, but not27* wake-on-close. This is implemented as standard by the XO-1.5 DSDT.28*29* We provide here a sysfs attribute that will additionally enable30* wake-on-close behavior. This is useful (e.g.) when we opportunistically31* suspend with the display running; if the lid is then closed, we want to32* wake up to turn the display off.33*34* This is controlled through a custom method in the XO-1.5 DSDT.35*/36static int set_lid_wake_behavior(bool wake_on_close)37{38acpi_status status;3940status = acpi_execute_simple_method(NULL, "\\_SB.PCI0.LID.LIDW", wake_on_close);41if (ACPI_FAILURE(status)) {42pr_warn(PFX "failed to set lid behavior\n");43return 1;44}4546lid_wake_on_close = wake_on_close;4748return 0;49}5051static ssize_t52lid_wake_on_close_show(struct kobject *s, struct kobj_attribute *attr, char *buf)53{54return sprintf(buf, "%u\n", lid_wake_on_close);55}5657static ssize_t lid_wake_on_close_store(struct kobject *s,58struct kobj_attribute *attr,59const char *buf, size_t n)60{61unsigned int val;6263if (sscanf(buf, "%u", &val) != 1)64return -EINVAL;6566set_lid_wake_behavior(!!val);6768return n;69}7071static struct kobj_attribute lid_wake_on_close_attr =72__ATTR(lid_wake_on_close, 0644,73lid_wake_on_close_show,74lid_wake_on_close_store);7576static void battery_status_changed(void)77{78struct power_supply *psy = power_supply_get_by_name("olpc_battery");7980if (psy) {81power_supply_changed(psy);82power_supply_put(psy);83}84}8586static void ac_status_changed(void)87{88struct power_supply *psy = power_supply_get_by_name("olpc_ac");8990if (psy) {91power_supply_changed(psy);92power_supply_put(psy);93}94}9596static void process_sci_queue(void)97{98u16 data;99int r;100101do {102r = olpc_ec_sci_query(&data);103if (r || !data)104break;105106pr_debug(PFX "SCI 0x%x received\n", data);107108switch (data) {109case EC_SCI_SRC_BATERR:110case EC_SCI_SRC_BATSOC:111case EC_SCI_SRC_BATTERY:112case EC_SCI_SRC_BATCRIT:113battery_status_changed();114break;115case EC_SCI_SRC_ACPWR:116ac_status_changed();117break;118}119} while (data);120121if (r)122pr_err(PFX "Failed to clear SCI queue");123}124125static void process_sci_queue_work(struct work_struct *work)126{127process_sci_queue();128}129130static DECLARE_WORK(sci_work, process_sci_queue_work);131132static u32 xo15_sci_gpe_handler(acpi_handle gpe_device, u32 gpe, void *context)133{134schedule_work(&sci_work);135return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;136}137138static int xo15_sci_add(struct acpi_device *device)139{140unsigned long long tmp;141acpi_status status;142int r;143144if (!device)145return -EINVAL;146147strscpy(acpi_device_name(device), XO15_SCI_DEVICE_NAME);148strscpy(acpi_device_class(device), XO15_SCI_CLASS);149150/* Get GPE bit assignment (EC events). */151status = acpi_evaluate_integer(device->handle, "_GPE", NULL, &tmp);152if (ACPI_FAILURE(status))153return -EINVAL;154155xo15_sci_gpe = tmp;156status = acpi_install_gpe_handler(NULL, xo15_sci_gpe,157ACPI_GPE_EDGE_TRIGGERED,158xo15_sci_gpe_handler, device);159if (ACPI_FAILURE(status))160return -ENODEV;161162dev_info(&device->dev, "Initialized, GPE = 0x%lx\n", xo15_sci_gpe);163164r = sysfs_create_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);165if (r)166goto err_sysfs;167168/* Flush queue, and enable all SCI events */169process_sci_queue();170olpc_ec_mask_write(EC_SCI_SRC_ALL);171172acpi_enable_gpe(NULL, xo15_sci_gpe);173174/* Enable wake-on-EC */175if (device->wakeup.flags.valid)176device_init_wakeup(&device->dev, true);177178return 0;179180err_sysfs:181acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);182cancel_work_sync(&sci_work);183return r;184}185186static void xo15_sci_remove(struct acpi_device *device)187{188acpi_disable_gpe(NULL, xo15_sci_gpe);189acpi_remove_gpe_handler(NULL, xo15_sci_gpe, xo15_sci_gpe_handler);190cancel_work_sync(&sci_work);191sysfs_remove_file(&device->dev.kobj, &lid_wake_on_close_attr.attr);192}193194#ifdef CONFIG_PM_SLEEP195static int xo15_sci_resume(struct device *dev)196{197/* Enable all EC events */198olpc_ec_mask_write(EC_SCI_SRC_ALL);199200/* Power/battery status might have changed */201battery_status_changed();202ac_status_changed();203204return 0;205}206#endif207208static SIMPLE_DEV_PM_OPS(xo15_sci_pm, NULL, xo15_sci_resume);209210static const struct acpi_device_id xo15_sci_device_ids[] = {211{"XO15EC", 0},212{"", 0},213};214215static struct acpi_driver xo15_sci_drv = {216.name = DRV_NAME,217.class = XO15_SCI_CLASS,218.ids = xo15_sci_device_ids,219.ops = {220.add = xo15_sci_add,221.remove = xo15_sci_remove,222},223.drv.pm = &xo15_sci_pm,224};225226static int __init xo15_sci_init(void)227{228return acpi_bus_register_driver(&xo15_sci_drv);229}230device_initcall(xo15_sci_init);231232233