Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 3 | * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 6 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Rafael J. Wysocki | 2249ff3 | 2021-02-03 19:43:17 +0100 | [diff] [blame] | 9 | #define pr_fmt(fmt) "ACPI: AC: " fmt |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/types.h> |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 16 | #include <linux/dmi.h> |
| 17 | #include <linux/delay.h> |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 19 | #include <linux/power_supply.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 20 | #include <linux/acpi.h> |
Ognjen Galic | fa93854 | 2018-02-07 15:58:13 +0100 | [diff] [blame] | 21 | #include <acpi/battery.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define ACPI_AC_CLASS "ac_adapter" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 30 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 31 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 32 | MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | MODULE_LICENSE("GPL"); |
| 34 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 35 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 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); |
| 39 | |
Hans de Goede | af3ec83 | 2017-04-19 14:02:11 +0200 | [diff] [blame] | 40 | struct acpi_ac_bl { |
| 41 | const char *hid; |
| 42 | int hrv; |
| 43 | }; |
| 44 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 45 | static const struct acpi_device_id ac_device_ids[] = { |
| 46 | {"ACPI0003", 0}, |
| 47 | {"", 0}, |
| 48 | }; |
| 49 | MODULE_DEVICE_TABLE(acpi, ac_device_ids); |
| 50 | |
| 51 | #ifdef CONFIG_PM_SLEEP |
| 52 | static int acpi_ac_resume(struct device *dev); |
| 53 | #endif |
| 54 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume); |
| 55 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 56 | static int ac_sleep_before_get_state_ms; |
Stefan Schaeckeler | 3d730ee | 2021-10-24 15:04:45 -0700 | [diff] [blame] | 57 | static int ac_only; |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 58 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 59 | static struct acpi_driver acpi_ac_driver = { |
| 60 | .name = "ac", |
| 61 | .class = ACPI_AC_CLASS, |
| 62 | .ids = ac_device_ids, |
| 63 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
| 64 | .ops = { |
| 65 | .add = acpi_ac_add, |
| 66 | .remove = acpi_ac_remove, |
| 67 | .notify = acpi_ac_notify, |
| 68 | }, |
| 69 | .drv.pm = &acpi_ac_pm, |
| 70 | }; |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct acpi_ac { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 73 | struct power_supply *charger; |
| 74 | struct power_supply_desc charger_desc; |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 75 | struct acpi_device *device; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 76 | unsigned long long state; |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 77 | struct notifier_block battery_nb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 80 | #define to_acpi_ac(x) power_supply_get_drvdata(x) |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 81 | |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 82 | /* AC Adapter Management */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 83 | static int acpi_ac_get_state(struct acpi_ac *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 85 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 87 | if (!ac) |
| 88 | return -EINVAL; |
| 89 | |
Stefan Schaeckeler | 3d730ee | 2021-10-24 15:04:45 -0700 | [diff] [blame] | 90 | if (ac_only) { |
| 91 | ac->state = 1; |
| 92 | return 0; |
| 93 | } |
| 94 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 95 | status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 96 | &ac->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (ACPI_FAILURE(status)) { |
Rafael J. Wysocki | 2249ff3 | 2021-02-03 19:43:17 +0100 | [diff] [blame] | 98 | acpi_handle_info(ac->device->handle, |
| 99 | "Error reading AC Adapter state: %s\n", |
| 100 | acpi_format_exception(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 102 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 104 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 105 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 108 | /* sysfs I/F */ |
Zhang Rui | 3151dbb | 2010-12-08 10:40:45 +0800 | [diff] [blame] | 109 | static int get_ac_property(struct power_supply *psy, |
| 110 | enum power_supply_property psp, |
| 111 | union power_supply_propval *val) |
| 112 | { |
| 113 | struct acpi_ac *ac = to_acpi_ac(psy); |
| 114 | |
| 115 | if (!ac) |
| 116 | return -ENODEV; |
| 117 | |
| 118 | if (acpi_ac_get_state(ac)) |
| 119 | return -ENODEV; |
| 120 | |
| 121 | switch (psp) { |
| 122 | case POWER_SUPPLY_PROP_ONLINE: |
| 123 | val->intval = ac->state; |
| 124 | break; |
| 125 | default: |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static enum power_supply_property ac_props[] = { |
| 132 | POWER_SUPPLY_PROP_ONLINE, |
| 133 | }; |
| 134 | |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 135 | /* Driver Model */ |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 136 | static void acpi_ac_notify(struct acpi_device *device, u32 event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 138 | struct acpi_ac *ac = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 141 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | switch (event) { |
Len Brown | f163ff5 | 2008-06-14 01:26:37 -0400 | [diff] [blame] | 144 | default: |
Rafael J. Wysocki | 2249ff3 | 2021-02-03 19:43:17 +0100 | [diff] [blame] | 145 | acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", |
| 146 | event); |
Gustavo A. R. Silva | 57d2dd4 | 2020-07-07 15:09:37 -0500 | [diff] [blame] | 147 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | case ACPI_AC_NOTIFY_STATUS: |
Christian Lupien | 03d7825 | 2004-08-19 01:26:00 -0400 | [diff] [blame] | 149 | case ACPI_NOTIFY_BUS_CHECK: |
| 150 | case ACPI_NOTIFY_DEVICE_CHECK: |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 151 | /* |
| 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. |
| 157 | */ |
| 158 | if (ac_sleep_before_get_state_ms > 0) |
| 159 | msleep(ac_sleep_before_get_state_ms); |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | acpi_ac_get_state(ac); |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 162 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
| 163 | dev_name(&device->dev), event, |
| 164 | (u32) ac->state); |
| 165 | acpi_notifier_call_chain(device, event, (u32) ac->state); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 166 | kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 170 | static int acpi_ac_battery_notify(struct notifier_block *nb, |
| 171 | unsigned long action, void *data) |
| 172 | { |
| 173 | struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb); |
| 174 | struct acpi_bus_event *event = (struct acpi_bus_event *)data; |
| 175 | |
| 176 | /* |
| 177 | * On HP Pavilion dv6-6179er AC status notifications aren't triggered |
| 178 | * when adapter is plugged/unplugged. However, battery status |
Tom Saeger | 935ab85 | 2021-03-12 18:55:35 -0700 | [diff] [blame] | 179 | * notifications are triggered when battery starts charging or |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 180 | * discharging. Re-reading AC status triggers lost AC notifications, |
| 181 | * if AC status has changed. |
| 182 | */ |
| 183 | if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 && |
| 184 | event->type == ACPI_BATTERY_NOTIFY_STATUS) |
| 185 | acpi_ac_get_state(ac); |
| 186 | |
| 187 | return NOTIFY_OK; |
| 188 | } |
| 189 | |
Carlo Caione | 91ea5b1 | 2018-04-18 14:04:40 +0200 | [diff] [blame] | 190 | static int __init thinkpad_e530_quirk(const struct dmi_system_id *d) |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 191 | { |
| 192 | ac_sleep_before_get_state_ms = 1000; |
| 193 | return 0; |
| 194 | } |
| 195 | |
Stefan Schaeckeler | 3d730ee | 2021-10-24 15:04:45 -0700 | [diff] [blame] | 196 | static int __init ac_only_quirk(const struct dmi_system_id *d) |
| 197 | { |
| 198 | ac_only = 1; |
| 199 | return 0; |
| 200 | } |
| 201 | |
Hans de Goede | 04900fa | 2020-02-23 15:29:40 +0100 | [diff] [blame] | 202 | /* Please keep this list alphabetically sorted */ |
Carlo Caione | 91ea5b1 | 2018-04-18 14:04:40 +0200 | [diff] [blame] | 203 | static const struct dmi_system_id ac_dmi_table[] __initconst = { |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 204 | { |
Stefan Schaeckeler | 3d730ee | 2021-10-24 15:04:45 -0700 | [diff] [blame] | 205 | /* Kodlix GK45 returning incorrect state */ |
| 206 | .callback = ac_only_quirk, |
| 207 | .matches = { |
| 208 | DMI_MATCH(DMI_PRODUCT_NAME, "GK45"), |
| 209 | }, |
| 210 | }, |
| 211 | { |
Hans de Goede | 04900fa | 2020-02-23 15:29:40 +0100 | [diff] [blame] | 212 | /* Lenovo Thinkpad e530, see comment in acpi_ac_notify() */ |
| 213 | .callback = thinkpad_e530_quirk, |
| 214 | .matches = { |
| 215 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 216 | DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"), |
Carlo Caione | 91ea5b1 | 2018-04-18 14:04:40 +0200 | [diff] [blame] | 217 | }, |
| 218 | }, |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 219 | {}, |
| 220 | }; |
| 221 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 222 | static int acpi_ac_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 224 | struct power_supply_config psy_cfg = {}; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 225 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 226 | struct acpi_ac *ac = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 228 | |
| 229 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 230 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 232 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 234 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 236 | ac->device = device; |
| 237 | strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME); |
| 238 | strcpy(acpi_device_class(device), ACPI_AC_CLASS); |
| 239 | device->driver_data = ac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | result = acpi_ac_get_state(ac); |
| 242 | if (result) |
| 243 | goto end; |
| 244 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 245 | psy_cfg.drv_data = ac; |
| 246 | |
| 247 | ac->charger_desc.name = acpi_device_bid(device); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 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); |
Lan Tianyu | f197ac1 | 2012-07-20 13:29:16 +0800 | [diff] [blame] | 256 | goto end; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Rafael J. Wysocki | 2249ff3 | 2021-02-03 19:43:17 +0100 | [diff] [blame] | 259 | pr_info("%s [%s] (%s)\n", acpi_device_name(device), |
| 260 | acpi_device_bid(device), ac->state ? "on-line" : "off-line"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 262 | ac->battery_nb.notifier_call = acpi_ac_battery_notify; |
| 263 | register_acpi_notifier(&ac->battery_nb); |
Lan Tianyu | ab0fd67 | 2013-10-12 21:04:48 +0800 | [diff] [blame] | 264 | end: |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 265 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | kfree(ac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 268 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 271 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 272 | static int acpi_ac_resume(struct device *dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 273 | { |
| 274 | struct acpi_ac *ac; |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 275 | unsigned int old_state; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 276 | |
| 277 | if (!dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 278 | return -EINVAL; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 279 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 280 | ac = acpi_driver_data(to_acpi_device(dev)); |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 281 | if (!ac) |
| 282 | return -EINVAL; |
| 283 | |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 284 | old_state = ac->state; |
| 285 | if (acpi_ac_get_state(ac)) |
| 286 | return 0; |
| 287 | if (old_state != ac->state) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 288 | kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE); |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 289 | return 0; |
| 290 | } |
Shuah Khan | 06521c2 | 2014-02-12 20:19:05 -0700 | [diff] [blame] | 291 | #else |
| 292 | #define acpi_ac_resume NULL |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 293 | #endif |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 294 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 295 | static int acpi_ac_remove(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 297 | struct acpi_ac *ac = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 299 | |
| 300 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 301 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 303 | ac = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 305 | power_supply_unregister(ac->charger); |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 306 | unregister_acpi_notifier(&ac->battery_nb); |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | kfree(ac); |
| 309 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 310 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 313 | static int __init acpi_ac_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 315 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 317 | if (acpi_disabled) |
| 318 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Hans de Goede | 57a1832 | 2021-12-30 20:31:19 +0100 | [diff] [blame] | 320 | if (acpi_quirk_skip_acpi_ac_and_battery()) |
| 321 | return -ENODEV; |
Carlo Caione | 91ea5b1 | 2018-04-18 14:04:40 +0200 | [diff] [blame] | 322 | |
Hans de Goede | 57a1832 | 2021-12-30 20:31:19 +0100 | [diff] [blame] | 323 | dmi_check_system(ac_dmi_table); |
Hans de Goede | af3ec83 | 2017-04-19 14:02:11 +0200 | [diff] [blame] | 324 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 325 | result = acpi_bus_register_driver(&acpi_ac_driver); |
Xiaofei Tan | 9104457 | 2021-03-27 11:47:01 +0800 | [diff] [blame] | 326 | if (result < 0) |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 327 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 329 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 332 | static void __exit acpi_ac_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 334 | acpi_bus_unregister_driver(&acpi_ac_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | module_init(acpi_ac_init); |
| 337 | module_exit(acpi_ac_exit); |