]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/acpi/wakeup.c
thermal: thermal_of: Fix error return code of thermal_of_populate_bind_params()
[mirror_ubuntu-focal-kernel.git] / drivers / acpi / wakeup.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * wakeup.c - support wakeup devices
4 * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
5 */
6
7#include <linux/init.h>
8#include <linux/acpi.h>
1da177e4
LT
9#include <linux/kernel.h>
10#include <linux/types.h>
e60cc7a6
BH
11
12#include "internal.h"
1da177e4
LT
13#include "sleep.h"
14
2a17958b
HG
15struct acpi_wakeup_handler {
16 struct list_head list_node;
17 bool (*wakeup)(void *context);
18 void *context;
19};
20
21static LIST_HEAD(acpi_wakeup_handler_head);
22static DEFINE_MUTEX(acpi_wakeup_handler_mutex);
23
9090589d
SL
24/*
25 * We didn't lock acpi_device_lock in the file, because it invokes oops in
26 * suspend/resume and isn't really required as this is called in S-state. At
27 * that time, there is no device hotplug
28 **/
1da177e4 29#define _COMPONENT ACPI_SYSTEM_COMPONENT
4be44fcd 30ACPI_MODULE_NAME("wakeup_devices")
1da177e4 31
1da177e4 32/**
78f5f023 33 * acpi_enable_wakeup_devices - Enable wake-up device GPEs.
9630bdd9
RW
34 * @sleep_state: ACPI system sleep state.
35 *
78f5f023
RW
36 * Enable wakeup device power of devices with the state.enable flag set and set
37 * the wakeup enable mask bits in the GPE registers that correspond to wakeup
38 * devices.
1da177e4 39 */
78f5f023 40void acpi_enable_wakeup_devices(u8 sleep_state)
1da177e4 41{
4be44fcd 42 struct list_head *node, *next;
1da177e4 43
1da177e4 44 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
9b039330
AS
45 struct acpi_device *dev =
46 container_of(node, struct acpi_device, wakeup_list);
eb9d0fe4 47
e8b6f970 48 if (!dev->wakeup.flags.valid
f2b56bc8
RW
49 || sleep_state > (u32) dev->wakeup.sleep_state
50 || !(device_may_wakeup(&dev->dev)
51 || dev->wakeup.prepare_count))
1da177e4 52 continue;
9630bdd9 53
f2b56bc8 54 if (device_may_wakeup(&dev->dev))
78f5f023
RW
55 acpi_enable_wakeup_device_power(dev, sleep_state);
56
9630bdd9 57 /* The wake-up power should have been enabled already. */
3a37898d 58 acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
e8b6f970 59 ACPI_GPE_ENABLE);
1da177e4 60 }
1da177e4
LT
61}
62
63/**
78f5f023 64 * acpi_disable_wakeup_devices - Disable devices' wakeup capability.
9630bdd9 65 * @sleep_state: ACPI system sleep state.
1da177e4 66 */
78f5f023 67void acpi_disable_wakeup_devices(u8 sleep_state)
1da177e4 68{
4be44fcd 69 struct list_head *node, *next;
1da177e4 70
1da177e4 71 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
9b039330
AS
72 struct acpi_device *dev =
73 container_of(node, struct acpi_device, wakeup_list);
1da177e4 74
e8b6f970 75 if (!dev->wakeup.flags.valid
f2b56bc8
RW
76 || sleep_state > (u32) dev->wakeup.sleep_state
77 || !(device_may_wakeup(&dev->dev)
78 || dev->wakeup.prepare_count))
1da177e4
LT
79 continue;
80
3a37898d 81 acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
e8b6f970
RW
82 ACPI_GPE_DISABLE);
83
f2b56bc8 84 if (device_may_wakeup(&dev->dev))
e8b6f970 85 acpi_disable_wakeup_device_power(dev);
1da177e4 86 }
1da177e4
LT
87}
88
201b8c65 89int __init acpi_wakeup_device_init(void)
1da177e4 90{
4be44fcd 91 struct list_head *node, *next;
1da177e4 92
9090589d 93 mutex_lock(&acpi_device_lock);
1da177e4 94 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
4be44fcd
LB
95 struct acpi_device *dev = container_of(node,
96 struct acpi_device,
97 wakeup_list);
2a5d2428
RW
98 if (device_can_wakeup(&dev->dev)) {
99 /* Button GPEs are supposed to be always enabled. */
100 acpi_enable_gpe(dev->wakeup.gpe_device,
101 dev->wakeup.gpe_number);
f2b56bc8 102 device_set_wakeup_enable(&dev->dev, true);
2a5d2428 103 }
1da177e4 104 }
9090589d 105 mutex_unlock(&acpi_device_lock);
1da177e4
LT
106 return 0;
107}
2a17958b
HG
108
109/**
110 * acpi_register_wakeup_handler - Register wakeup handler
111 * @wake_irq: The IRQ through which the device may receive wakeups
112 * @wakeup: Wakeup-handler to call when the SCI has triggered a wakeup
113 * @context: Context to pass to the handler when calling it
114 *
115 * Drivers which may share an IRQ with the SCI can use this to register
116 * a handler which returns true when the device they are managing wants
117 * to trigger a wakeup.
118 */
119int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context),
120 void *context)
121{
122 struct acpi_wakeup_handler *handler;
123
124 /*
125 * If the device is not sharing its IRQ with the SCI, there is no
126 * need to register the handler.
127 */
128 if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
129 return 0;
130
131 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
132 if (!handler)
133 return -ENOMEM;
134
135 handler->wakeup = wakeup;
136 handler->context = context;
137
138 mutex_lock(&acpi_wakeup_handler_mutex);
139 list_add(&handler->list_node, &acpi_wakeup_handler_head);
140 mutex_unlock(&acpi_wakeup_handler_mutex);
141
142 return 0;
143}
144EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler);
145
146/**
147 * acpi_unregister_wakeup_handler - Unregister wakeup handler
148 * @wakeup: Wakeup-handler passed to acpi_register_wakeup_handler()
149 * @context: Context passed to acpi_register_wakeup_handler()
150 */
151void acpi_unregister_wakeup_handler(bool (*wakeup)(void *context),
152 void *context)
153{
154 struct acpi_wakeup_handler *handler;
155
156 mutex_lock(&acpi_wakeup_handler_mutex);
157 list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
158 if (handler->wakeup == wakeup && handler->context == context) {
159 list_del(&handler->list_node);
160 kfree(handler);
161 break;
162 }
163 }
164 mutex_unlock(&acpi_wakeup_handler_mutex);
165}
166EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler);
167
168bool acpi_check_wakeup_handlers(void)
169{
170 struct acpi_wakeup_handler *handler;
171
172 /* No need to lock, nothing else is running when we're called. */
173 list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
174 if (handler->wakeup(handler->context))
175 return true;
176 }
177
178 return false;
179}