1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
295b482a8SLen Brown /*
395b482a8SLen Brown * wakeup.c - support wakeup devices
495b482a8SLen Brown * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
595b482a8SLen Brown */
695b482a8SLen Brown
795b482a8SLen Brown #include <linux/init.h>
895b482a8SLen Brown #include <linux/acpi.h>
995b482a8SLen Brown #include <linux/kernel.h>
1095b482a8SLen Brown #include <linux/types.h>
11e60cc7a6SBjorn Helgaas
12e60cc7a6SBjorn Helgaas #include "internal.h"
1395b482a8SLen Brown #include "sleep.h"
1495b482a8SLen Brown
15*ddfd9dcfSHans de Goede struct acpi_wakeup_handler {
16*ddfd9dcfSHans de Goede struct list_head list_node;
17*ddfd9dcfSHans de Goede bool (*wakeup)(void *context);
18*ddfd9dcfSHans de Goede void *context;
19*ddfd9dcfSHans de Goede };
20*ddfd9dcfSHans de Goede
21*ddfd9dcfSHans de Goede static LIST_HEAD(acpi_wakeup_handler_head);
22*ddfd9dcfSHans de Goede static DEFINE_MUTEX(acpi_wakeup_handler_mutex);
23*ddfd9dcfSHans de Goede
249090589dSShaohua Li /*
259090589dSShaohua Li * We didn't lock acpi_device_lock in the file, because it invokes oops in
269090589dSShaohua Li * suspend/resume and isn't really required as this is called in S-state. At
279090589dSShaohua Li * that time, there is no device hotplug
289090589dSShaohua Li **/
2995b482a8SLen Brown
3095b482a8SLen Brown /**
3178f5f023SRafael J. Wysocki * acpi_enable_wakeup_devices - Enable wake-up device GPEs.
329630bdd9SRafael J. Wysocki * @sleep_state: ACPI system sleep state.
339630bdd9SRafael J. Wysocki *
3478f5f023SRafael J. Wysocki * Enable wakeup device power of devices with the state.enable flag set and set
3578f5f023SRafael J. Wysocki * the wakeup enable mask bits in the GPE registers that correspond to wakeup
3678f5f023SRafael J. Wysocki * devices.
3795b482a8SLen Brown */
acpi_enable_wakeup_devices(u8 sleep_state)3878f5f023SRafael J. Wysocki void acpi_enable_wakeup_devices(u8 sleep_state)
3995b482a8SLen Brown {
4007761a4cSchenqiwu struct acpi_device *dev, *tmp;
4195b482a8SLen Brown
4207761a4cSchenqiwu list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
4307761a4cSchenqiwu wakeup_list) {
44e8b6f970SRafael J. Wysocki if (!dev->wakeup.flags.valid
45f2b56bc8SRafael J. Wysocki || sleep_state > (u32) dev->wakeup.sleep_state
46f2b56bc8SRafael J. Wysocki || !(device_may_wakeup(&dev->dev)
47f2b56bc8SRafael J. Wysocki || dev->wakeup.prepare_count))
4895b482a8SLen Brown continue;
499630bdd9SRafael J. Wysocki
50f2b56bc8SRafael J. Wysocki if (device_may_wakeup(&dev->dev))
5178f5f023SRafael J. Wysocki acpi_enable_wakeup_device_power(dev, sleep_state);
5278f5f023SRafael J. Wysocki
539630bdd9SRafael J. Wysocki /* The wake-up power should have been enabled already. */
543a37898dSLin Ming acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
55e8b6f970SRafael J. Wysocki ACPI_GPE_ENABLE);
5695b482a8SLen Brown }
5795b482a8SLen Brown }
5895b482a8SLen Brown
5995b482a8SLen Brown /**
6078f5f023SRafael J. Wysocki * acpi_disable_wakeup_devices - Disable devices' wakeup capability.
619630bdd9SRafael J. Wysocki * @sleep_state: ACPI system sleep state.
6295b482a8SLen Brown */
acpi_disable_wakeup_devices(u8 sleep_state)6378f5f023SRafael J. Wysocki void acpi_disable_wakeup_devices(u8 sleep_state)
6495b482a8SLen Brown {
6507761a4cSchenqiwu struct acpi_device *dev, *tmp;
6695b482a8SLen Brown
6707761a4cSchenqiwu list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
6807761a4cSchenqiwu wakeup_list) {
69e8b6f970SRafael J. Wysocki if (!dev->wakeup.flags.valid
70f2b56bc8SRafael J. Wysocki || sleep_state > (u32) dev->wakeup.sleep_state
71f2b56bc8SRafael J. Wysocki || !(device_may_wakeup(&dev->dev)
72f2b56bc8SRafael J. Wysocki || dev->wakeup.prepare_count))
7395b482a8SLen Brown continue;
7495b482a8SLen Brown
753a37898dSLin Ming acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
76e8b6f970SRafael J. Wysocki ACPI_GPE_DISABLE);
77e8b6f970SRafael J. Wysocki
78f2b56bc8SRafael J. Wysocki if (device_may_wakeup(&dev->dev))
7995b482a8SLen Brown acpi_disable_wakeup_device_power(dev);
8095b482a8SLen Brown }
8195b482a8SLen Brown }
8295b482a8SLen Brown
acpi_wakeup_device_init(void)83201b8c65SBjorn Helgaas int __init acpi_wakeup_device_init(void)
8495b482a8SLen Brown {
8507761a4cSchenqiwu struct acpi_device *dev, *tmp;
8695b482a8SLen Brown
879090589dSShaohua Li mutex_lock(&acpi_device_lock);
8807761a4cSchenqiwu list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
8907761a4cSchenqiwu wakeup_list) {
902a5d2428SRafael J. Wysocki if (device_can_wakeup(&dev->dev)) {
912a5d2428SRafael J. Wysocki /* Button GPEs are supposed to be always enabled. */
922a5d2428SRafael J. Wysocki acpi_enable_gpe(dev->wakeup.gpe_device,
932a5d2428SRafael J. Wysocki dev->wakeup.gpe_number);
94f2b56bc8SRafael J. Wysocki device_set_wakeup_enable(&dev->dev, true);
9595b482a8SLen Brown }
962a5d2428SRafael J. Wysocki }
979090589dSShaohua Li mutex_unlock(&acpi_device_lock);
9895b482a8SLen Brown return 0;
9995b482a8SLen Brown }
100*ddfd9dcfSHans de Goede
101*ddfd9dcfSHans de Goede /**
102*ddfd9dcfSHans de Goede * acpi_register_wakeup_handler - Register wakeup handler
103*ddfd9dcfSHans de Goede * @wake_irq: The IRQ through which the device may receive wakeups
104*ddfd9dcfSHans de Goede * @wakeup: Wakeup-handler to call when the SCI has triggered a wakeup
105*ddfd9dcfSHans de Goede * @context: Context to pass to the handler when calling it
106*ddfd9dcfSHans de Goede *
107*ddfd9dcfSHans de Goede * Drivers which may share an IRQ with the SCI can use this to register
108*ddfd9dcfSHans de Goede * a handler which returns true when the device they are managing wants
109*ddfd9dcfSHans de Goede * to trigger a wakeup.
110*ddfd9dcfSHans de Goede */
acpi_register_wakeup_handler(int wake_irq,bool (* wakeup)(void * context),void * context)111*ddfd9dcfSHans de Goede int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context),
112*ddfd9dcfSHans de Goede void *context)
113*ddfd9dcfSHans de Goede {
114*ddfd9dcfSHans de Goede struct acpi_wakeup_handler *handler;
115*ddfd9dcfSHans de Goede
116*ddfd9dcfSHans de Goede /*
117*ddfd9dcfSHans de Goede * If the device is not sharing its IRQ with the SCI, there is no
118*ddfd9dcfSHans de Goede * need to register the handler.
119*ddfd9dcfSHans de Goede */
120*ddfd9dcfSHans de Goede if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
121*ddfd9dcfSHans de Goede return 0;
122*ddfd9dcfSHans de Goede
123*ddfd9dcfSHans de Goede handler = kmalloc(sizeof(*handler), GFP_KERNEL);
124*ddfd9dcfSHans de Goede if (!handler)
125*ddfd9dcfSHans de Goede return -ENOMEM;
126*ddfd9dcfSHans de Goede
127*ddfd9dcfSHans de Goede handler->wakeup = wakeup;
128*ddfd9dcfSHans de Goede handler->context = context;
129*ddfd9dcfSHans de Goede
130*ddfd9dcfSHans de Goede mutex_lock(&acpi_wakeup_handler_mutex);
131*ddfd9dcfSHans de Goede list_add(&handler->list_node, &acpi_wakeup_handler_head);
132*ddfd9dcfSHans de Goede mutex_unlock(&acpi_wakeup_handler_mutex);
133*ddfd9dcfSHans de Goede
134*ddfd9dcfSHans de Goede return 0;
135*ddfd9dcfSHans de Goede }
136*ddfd9dcfSHans de Goede EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler);
137*ddfd9dcfSHans de Goede
138*ddfd9dcfSHans de Goede /**
139*ddfd9dcfSHans de Goede * acpi_unregister_wakeup_handler - Unregister wakeup handler
140*ddfd9dcfSHans de Goede * @wakeup: Wakeup-handler passed to acpi_register_wakeup_handler()
141*ddfd9dcfSHans de Goede * @context: Context passed to acpi_register_wakeup_handler()
142*ddfd9dcfSHans de Goede */
acpi_unregister_wakeup_handler(bool (* wakeup)(void * context),void * context)143*ddfd9dcfSHans de Goede void acpi_unregister_wakeup_handler(bool (*wakeup)(void *context),
144*ddfd9dcfSHans de Goede void *context)
145*ddfd9dcfSHans de Goede {
146*ddfd9dcfSHans de Goede struct acpi_wakeup_handler *handler;
147*ddfd9dcfSHans de Goede
148*ddfd9dcfSHans de Goede mutex_lock(&acpi_wakeup_handler_mutex);
149*ddfd9dcfSHans de Goede list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
150*ddfd9dcfSHans de Goede if (handler->wakeup == wakeup && handler->context == context) {
151*ddfd9dcfSHans de Goede list_del(&handler->list_node);
152*ddfd9dcfSHans de Goede kfree(handler);
153*ddfd9dcfSHans de Goede break;
154*ddfd9dcfSHans de Goede }
155*ddfd9dcfSHans de Goede }
156*ddfd9dcfSHans de Goede mutex_unlock(&acpi_wakeup_handler_mutex);
157*ddfd9dcfSHans de Goede }
158*ddfd9dcfSHans de Goede EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler);
159*ddfd9dcfSHans de Goede
acpi_check_wakeup_handlers(void)160*ddfd9dcfSHans de Goede bool acpi_check_wakeup_handlers(void)
161*ddfd9dcfSHans de Goede {
162*ddfd9dcfSHans de Goede struct acpi_wakeup_handler *handler;
163*ddfd9dcfSHans de Goede
164*ddfd9dcfSHans de Goede /* No need to lock, nothing else is running when we're called. */
165*ddfd9dcfSHans de Goede list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
166*ddfd9dcfSHans de Goede if (handler->wakeup(handler->context))
167*ddfd9dcfSHans de Goede return true;
168*ddfd9dcfSHans de Goede }
169*ddfd9dcfSHans de Goede
170*ddfd9dcfSHans de Goede return false;
171*ddfd9dcfSHans de Goede }
172