1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27)
9 #define pr_fmt(fmt) "ACPI: AC: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/dmi.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/acpi.h>
21 #include <acpi/battery.h>
23 #define ACPI_AC_CLASS "ac_adapter"
24 #define ACPI_AC_DEVICE_NAME "AC Adapter"
25 #define ACPI_AC_FILE_STATE "state"
26 #define ACPI_AC_NOTIFY_STATUS 0x80
27 #define ACPI_AC_STATUS_OFFLINE 0x00
28 #define ACPI_AC_STATUS_ONLINE 0x01
29 #define ACPI_AC_STATUS_UNKNOWN 0xFF
31 MODULE_AUTHOR("Paul Diefenbaugh");
32 MODULE_DESCRIPTION("ACPI AC Adapter Driver");
33 MODULE_LICENSE("GPL");
36 static int acpi_ac_add(struct acpi_device *device);
37 static int acpi_ac_remove(struct acpi_device *device);
38 static void acpi_ac_notify(struct acpi_device *device, u32 event);
45 static const struct acpi_device_id ac_device_ids[] = {
49 MODULE_DEVICE_TABLE(acpi, ac_device_ids);
51 #ifdef CONFIG_PM_SLEEP
52 static int acpi_ac_resume(struct device *dev);
54 static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
56 static int ac_sleep_before_get_state_ms;
59 static struct acpi_driver acpi_ac_driver = {
61 .class = ACPI_AC_CLASS,
63 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
66 .remove = acpi_ac_remove,
67 .notify = acpi_ac_notify,
69 .drv.pm = &acpi_ac_pm,
73 struct power_supply *charger;
74 struct power_supply_desc charger_desc;
75 struct acpi_device *device;
76 unsigned long long state;
77 struct notifier_block battery_nb;
80 #define to_acpi_ac(x) power_supply_get_drvdata(x)
82 /* AC Adapter Management */
83 static int acpi_ac_get_state(struct acpi_ac *ac)
85 acpi_status status = AE_OK;
95 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
97 if (ACPI_FAILURE(status)) {
98 acpi_handle_info(ac->device->handle,
99 "Error reading AC Adapter state: %s\n",
100 acpi_format_exception(status));
101 ac->state = ACPI_AC_STATUS_UNKNOWN;
109 static int get_ac_property(struct power_supply *psy,
110 enum power_supply_property psp,
111 union power_supply_propval *val)
113 struct acpi_ac *ac = to_acpi_ac(psy);
118 if (acpi_ac_get_state(ac))
122 case POWER_SUPPLY_PROP_ONLINE:
123 val->intval = ac->state;
131 static enum power_supply_property ac_props[] = {
132 POWER_SUPPLY_PROP_ONLINE,
136 static void acpi_ac_notify(struct acpi_device *device, u32 event)
138 struct acpi_ac *ac = acpi_driver_data(device);
145 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
148 case ACPI_AC_NOTIFY_STATUS:
149 case ACPI_NOTIFY_BUS_CHECK:
150 case ACPI_NOTIFY_DEVICE_CHECK:
152 * A buggy BIOS may notify AC first and then sleep for
153 * a specific time before doing actual operations in the
154 * EC event handler (_Qxx). This will cause the AC state
155 * reported by the ACPI event to be incorrect, so wait for a
156 * specific time for the EC event handler to make progress.
158 if (ac_sleep_before_get_state_ms > 0)
159 msleep(ac_sleep_before_get_state_ms);
161 acpi_ac_get_state(ac);
162 acpi_bus_generate_netlink_event(device->pnp.device_class,
163 dev_name(&device->dev), event,
165 acpi_notifier_call_chain(device, event, (u32) ac->state);
166 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
170 static int acpi_ac_battery_notify(struct notifier_block *nb,
171 unsigned long action, void *data)
173 struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
174 struct acpi_bus_event *event = (struct acpi_bus_event *)data;
177 * On HP Pavilion dv6-6179er AC status notifications aren't triggered
178 * when adapter is plugged/unplugged. However, battery status
179 * notifications are triggered when battery starts charging or
180 * discharging. Re-reading AC status triggers lost AC notifications,
181 * if AC status has changed.
183 if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
184 event->type == ACPI_BATTERY_NOTIFY_STATUS)
185 acpi_ac_get_state(ac);
190 static int __init thinkpad_e530_quirk(const struct dmi_system_id *d)
192 ac_sleep_before_get_state_ms = 1000;
196 static int __init ac_only_quirk(const struct dmi_system_id *d)
202 /* Please keep this list alphabetically sorted */
203 static const struct dmi_system_id ac_dmi_table[] __initconst = {
205 /* Kodlix GK45 returning incorrect state */
206 .callback = ac_only_quirk,
208 DMI_MATCH(DMI_PRODUCT_NAME, "GK45"),
212 /* Lenovo Thinkpad e530, see comment in acpi_ac_notify() */
213 .callback = thinkpad_e530_quirk,
215 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
216 DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
222 static int acpi_ac_add(struct acpi_device *device)
224 struct power_supply_config psy_cfg = {};
226 struct acpi_ac *ac = NULL;
232 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
237 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
238 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
239 device->driver_data = ac;
241 result = acpi_ac_get_state(ac);
245 psy_cfg.drv_data = ac;
247 ac->charger_desc.name = acpi_device_bid(device);
248 ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
249 ac->charger_desc.properties = ac_props;
250 ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
251 ac->charger_desc.get_property = get_ac_property;
252 ac->charger = power_supply_register(&ac->device->dev,
253 &ac->charger_desc, &psy_cfg);
254 if (IS_ERR(ac->charger)) {
255 result = PTR_ERR(ac->charger);
259 pr_info("%s [%s] (%s)\n", acpi_device_name(device),
260 acpi_device_bid(device), ac->state ? "on-line" : "off-line");
262 ac->battery_nb.notifier_call = acpi_ac_battery_notify;
263 register_acpi_notifier(&ac->battery_nb);
271 #ifdef CONFIG_PM_SLEEP
272 static int acpi_ac_resume(struct device *dev)
275 unsigned int old_state;
280 ac = acpi_driver_data(to_acpi_device(dev));
284 old_state = ac->state;
285 if (acpi_ac_get_state(ac))
287 if (old_state != ac->state)
288 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
292 #define acpi_ac_resume NULL
295 static int acpi_ac_remove(struct acpi_device *device)
297 struct acpi_ac *ac = NULL;
300 if (!device || !acpi_driver_data(device))
303 ac = acpi_driver_data(device);
305 power_supply_unregister(ac->charger);
306 unregister_acpi_notifier(&ac->battery_nb);
313 static int __init acpi_ac_init(void)
320 if (acpi_quirk_skip_acpi_ac_and_battery())
323 dmi_check_system(ac_dmi_table);
325 result = acpi_bus_register_driver(&acpi_ac_driver);
332 static void __exit acpi_ac_exit(void)
334 acpi_bus_unregister_driver(&acpi_ac_driver);
336 module_init(acpi_ac_init);
337 module_exit(acpi_ac_exit);