blob: db487ff9dd1b6313e66061f391350258a1858bf7 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Xiaofei Tan91044572021-03-27 11:47:01 +08003 * acpi_ac.c - ACPI AC Adapter Driver (Revision: 27)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Rafael J. Wysocki2249ff32021-02-03 19:43:17 +01009#define pr_fmt(fmt) "ACPI: AC: " fmt
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/types.h>
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000016#include <linux/dmi.h>
17#include <linux/delay.h>
Zhang Ruicc8ef522013-09-25 20:39:45 +080018#include <linux/platform_device.h>
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040019#include <linux/power_supply.h>
Lv Zheng8b484632013-12-03 08:49:16 +080020#include <linux/acpi.h>
Ognjen Galicfa938542018-02-07 15:58:13 +010021#include <acpi/battery.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define ACPI_AC_CLASS "ac_adapter"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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 Brownf52fd662007-02-12 22:42:12 -050031MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050032MODULE_DESCRIPTION("ACPI AC Adapter Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_LICENSE("GPL");
34
Lan Tianyue63f6e22014-07-07 01:13:46 +020035
Guenter Roeck98012842014-05-06 19:18:28 -070036static int acpi_ac_add(struct acpi_device *device);
37static int acpi_ac_remove(struct acpi_device *device);
38static void acpi_ac_notify(struct acpi_device *device, u32 event);
39
Hans de Goedeaf3ec832017-04-19 14:02:11 +020040struct acpi_ac_bl {
41 const char *hid;
42 int hrv;
43};
44
Guenter Roeck98012842014-05-06 19:18:28 -070045static const struct acpi_device_id ac_device_ids[] = {
46 {"ACPI0003", 0},
47 {"", 0},
48};
49MODULE_DEVICE_TABLE(acpi, ac_device_ids);
50
51#ifdef CONFIG_PM_SLEEP
52static int acpi_ac_resume(struct device *dev);
53#endif
54static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
55
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000056static int ac_sleep_before_get_state_ms;
Stefan Schaeckeler3d730ee2021-10-24 15:04:45 -070057static int ac_only;
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000058
Guenter Roeck98012842014-05-06 19:18:28 -070059static 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 Torvalds1da177e2005-04-16 15:20:36 -070072struct acpi_ac {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010073 struct power_supply *charger;
74 struct power_supply_desc charger_desc;
Xiaofei Tan91044572021-03-27 11:47:01 +080075 struct acpi_device *device;
Matthew Wilcox27663c52008-10-10 02:22:59 -040076 unsigned long long state;
Alexander Mezin4eee4f02014-03-12 00:58:48 +070077 struct notifier_block battery_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010080#define to_acpi_ac(x) power_supply_get_drvdata(x)
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040081
Xiaofei Tan91044572021-03-27 11:47:01 +080082/* AC Adapter Management */
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Guenter Roeck98012842014-05-06 19:18:28 -070085 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Guenter Roeck98012842014-05-06 19:18:28 -070087 if (!ac)
88 return -EINVAL;
89
Stefan Schaeckeler3d730ee2021-10-24 15:04:45 -070090 if (ac_only) {
91 ac->state = 1;
92 return 0;
93 }
94
Guenter Roeck98012842014-05-06 19:18:28 -070095 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
Zhang Ruicc8ef522013-09-25 20:39:45 +080096 &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki2249ff32021-02-03 19:43:17 +010098 acpi_handle_info(ac->device->handle,
99 "Error reading AC Adapter state: %s\n",
100 acpi_format_exception(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -0400102 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Len Brown4be44fc2005-08-05 00:44:28 -0400104
Patrick Mocheld550d982006-06-27 00:41:40 -0400105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Xiaofei Tan91044572021-03-27 11:47:01 +0800108/* sysfs I/F */
Zhang Rui3151dbb2010-12-08 10:40:45 +0800109static 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
131static enum power_supply_property ac_props[] = {
132 POWER_SUPPLY_PROP_ONLINE,
133};
134
Xiaofei Tan91044572021-03-27 11:47:01 +0800135/* Driver Model */
Guenter Roeck98012842014-05-06 19:18:28 -0700136static void acpi_ac_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Guenter Roeck98012842014-05-06 19:18:28 -0700138 struct acpi_ac *ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (event) {
Len Brownf163ff52008-06-14 01:26:37 -0400144 default:
Rafael J. Wysocki2249ff32021-02-03 19:43:17 +0100145 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
146 event);
Gustavo A. R. Silva57d2dd42020-07-07 15:09:37 -0500147 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d78252004-08-19 01:26:00 -0400149 case ACPI_NOTIFY_BUS_CHECK:
150 case ACPI_NOTIFY_DEVICE_CHECK:
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000151 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700161 acpi_ac_get_state(ac);
Guenter Roeck98012842014-05-06 19:18:28 -0700162 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 Kozlowski297d7162015-03-12 08:44:11 +0100166 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700170static 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 Saeger935ab852021-03-12 18:55:35 -0700179 * notifications are triggered when battery starts charging or
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700180 * 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 Caione91ea5b12018-04-18 14:04:40 +0200190static int __init thinkpad_e530_quirk(const struct dmi_system_id *d)
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000191{
192 ac_sleep_before_get_state_ms = 1000;
193 return 0;
194}
195
Stefan Schaeckeler3d730ee2021-10-24 15:04:45 -0700196static int __init ac_only_quirk(const struct dmi_system_id *d)
197{
198 ac_only = 1;
199 return 0;
200}
201
Hans de Goede04900fa2020-02-23 15:29:40 +0100202/* Please keep this list alphabetically sorted */
Carlo Caione91ea5b12018-04-18 14:04:40 +0200203static const struct dmi_system_id ac_dmi_table[] __initconst = {
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000204 {
Stefan Schaeckeler3d730ee2021-10-24 15:04:45 -0700205 /* Kodlix GK45 returning incorrect state */
206 .callback = ac_only_quirk,
207 .matches = {
208 DMI_MATCH(DMI_PRODUCT_NAME, "GK45"),
209 },
210 },
211 {
Hans de Goede04900fa2020-02-23 15:29:40 +0100212 /* 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 Caione91ea5b12018-04-18 14:04:40 +0200217 },
218 },
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000219 {},
220};
221
Guenter Roeck98012842014-05-06 19:18:28 -0700222static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100224 struct power_supply_config psy_cfg = {};
Len Brown4be44fc2005-08-05 00:44:28 -0400225 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400226 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Guenter Roeck98012842014-05-06 19:18:28 -0700228
229 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Burman Yan36bcbec2006-12-19 12:56:11 -0800232 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Guenter Roeck98012842014-05-06 19:18:28 -0700236 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 Torvalds1da177e2005-04-16 15:20:36 -0700240
241 result = acpi_ac_get_state(ac);
242 if (result)
243 goto end;
244
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100245 psy_cfg.drv_data = ac;
246
247 ac->charger_desc.name = acpi_device_bid(device);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100248 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 Tianyuf197ac12012-07-20 13:29:16 +0800256 goto end;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Rafael J. Wysocki2249ff32021-02-03 19:43:17 +0100259 pr_info("%s [%s] (%s)\n", acpi_device_name(device),
260 acpi_device_bid(device), ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700262 ac->battery_nb.notifier_call = acpi_ac_battery_notify;
263 register_acpi_notifier(&ac->battery_nb);
Lan Tianyuab0fd672013-10-12 21:04:48 +0800264end:
Xiaofei Tan91044572021-03-27 11:47:01 +0800265 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kfree(ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200271#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200272static int acpi_ac_resume(struct device *dev)
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800273{
274 struct acpi_ac *ac;
Xiaofei Tan91044572021-03-27 11:47:01 +0800275 unsigned int old_state;
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200276
277 if (!dev)
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800278 return -EINVAL;
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200279
Guenter Roeck98012842014-05-06 19:18:28 -0700280 ac = acpi_driver_data(to_acpi_device(dev));
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200281 if (!ac)
282 return -EINVAL;
283
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800284 old_state = ac->state;
285 if (acpi_ac_get_state(ac))
286 return 0;
287 if (old_state != ac->state)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100288 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800289 return 0;
290}
Shuah Khan06521c22014-02-12 20:19:05 -0700291#else
292#define acpi_ac_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200293#endif
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800294
Guenter Roeck98012842014-05-06 19:18:28 -0700295static int acpi_ac_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Guenter Roeck98012842014-05-06 19:18:28 -0700297 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Guenter Roeck98012842014-05-06 19:18:28 -0700299
300 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Guenter Roeck98012842014-05-06 19:18:28 -0700303 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100305 power_supply_unregister(ac->charger);
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700306 unregister_acpi_notifier(&ac->battery_nb);
Zhang Ruicc8ef522013-09-25 20:39:45 +0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 kfree(ac);
309
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Rich Townsend3f86b832006-07-01 11:36:54 -0400315 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Pavel Machek4d8316d2006-08-14 22:37:22 -0700317 if (acpi_disabled)
318 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Hans de Goede57a18322021-12-30 20:31:19 +0100320 if (acpi_quirk_skip_acpi_ac_and_battery())
321 return -ENODEV;
Carlo Caione91ea5b12018-04-18 14:04:40 +0200322
Hans de Goede57a18322021-12-30 20:31:19 +0100323 dmi_check_system(ac_dmi_table);
Hans de Goedeaf3ec832017-04-19 14:02:11 +0200324
Lan Tianyue63f6e22014-07-07 01:13:46 +0200325 result = acpi_bus_register_driver(&acpi_ac_driver);
Xiaofei Tan91044572021-03-27 11:47:01 +0800326 if (result < 0)
Lan Tianyue63f6e22014-07-07 01:13:46 +0200327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Guenter Roeck98012842014-05-06 19:18:28 -0700334 acpi_bus_unregister_driver(&acpi_ac_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336module_init(acpi_ac_init);
337module_exit(acpi_ac_exit);