blob: e3101b1a86a3c8d9808d0358611dba82ad1848bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/types.h>
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000027#include <linux/dmi.h>
28#include <linux/delay.h>
Lan Tianyue63f6e22014-07-07 01:13:46 +020029#ifdef CONFIG_ACPI_PROCFS_POWER
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#endif
Zhang Ruicc8ef522013-09-25 20:39:45 +080033#include <linux/platform_device.h>
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040034#include <linux/power_supply.h>
Lv Zheng8b484632013-12-03 08:49:16 +080035#include <linux/acpi.h>
Ognjen Galicfa938542018-02-07 15:58:13 +010036#include <acpi/battery.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Len Browna192a952009-07-28 16:45:54 -040038#define PREFIX "ACPI: "
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define ACPI_AC_CLASS "ac_adapter"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define ACPI_AC_DEVICE_NAME "AC Adapter"
42#define ACPI_AC_FILE_STATE "state"
43#define ACPI_AC_NOTIFY_STATUS 0x80
44#define ACPI_AC_STATUS_OFFLINE 0x00
45#define ACPI_AC_STATUS_ONLINE 0x01
46#define ACPI_AC_STATUS_UNKNOWN 0xFF
47
48#define _COMPONENT ACPI_AC_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050049ACPI_MODULE_NAME("ac");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Len Brownf52fd662007-02-12 22:42:12 -050051MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050052MODULE_DESCRIPTION("ACPI AC Adapter Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053MODULE_LICENSE("GPL");
54
Lan Tianyue63f6e22014-07-07 01:13:46 +020055
Guenter Roeck98012842014-05-06 19:18:28 -070056static int acpi_ac_add(struct acpi_device *device);
57static int acpi_ac_remove(struct acpi_device *device);
58static void acpi_ac_notify(struct acpi_device *device, u32 event);
59
Hans de Goedeaf3ec832017-04-19 14:02:11 +020060struct acpi_ac_bl {
61 const char *hid;
62 int hrv;
63};
64
Guenter Roeck98012842014-05-06 19:18:28 -070065static const struct acpi_device_id ac_device_ids[] = {
66 {"ACPI0003", 0},
67 {"", 0},
68};
69MODULE_DEVICE_TABLE(acpi, ac_device_ids);
70
Hans de Goedeaf3ec832017-04-19 14:02:11 +020071/* Lists of PMIC ACPI HIDs with an (often better) native charger driver */
72static const struct acpi_ac_bl acpi_ac_blacklist[] = {
73 { "INT33F4", -1 }, /* X-Powers AXP288 PMIC */
74 { "INT34D3", 3 }, /* Intel Cherrytrail Whiskey Cove PMIC */
75};
76
Guenter Roeck98012842014-05-06 19:18:28 -070077#ifdef CONFIG_PM_SLEEP
78static int acpi_ac_resume(struct device *dev);
79#endif
80static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
81
Lan Tianyue63f6e22014-07-07 01:13:46 +020082#ifdef CONFIG_ACPI_PROCFS_POWER
83extern struct proc_dir_entry *acpi_lock_ac_dir(void);
84extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
85static int acpi_ac_open_fs(struct inode *inode, struct file *file);
86#endif
87
88
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000089static int ac_sleep_before_get_state_ms;
Carlo Caione91ea5b12018-04-18 14:04:40 +020090static int ac_check_pmic = 1;
Lan Tianyu0ab5bb62013-05-08 07:28:46 +000091
Guenter Roeck98012842014-05-06 19:18:28 -070092static struct acpi_driver acpi_ac_driver = {
93 .name = "ac",
94 .class = ACPI_AC_CLASS,
95 .ids = ac_device_ids,
96 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
97 .ops = {
98 .add = acpi_ac_add,
99 .remove = acpi_ac_remove,
100 .notify = acpi_ac_notify,
101 },
102 .drv.pm = &acpi_ac_pm,
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105struct acpi_ac {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100106 struct power_supply *charger;
107 struct power_supply_desc charger_desc;
Guenter Roeck98012842014-05-06 19:18:28 -0700108 struct acpi_device * device;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400109 unsigned long long state;
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700110 struct notifier_block battery_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100113#define to_acpi_ac(x) power_supply_get_drvdata(x)
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +0400114
Lan Tianyue63f6e22014-07-07 01:13:46 +0200115#ifdef CONFIG_ACPI_PROCFS_POWER
116static const struct file_operations acpi_ac_fops = {
117 .owner = THIS_MODULE,
118 .open = acpi_ac_open_fs,
119 .read = seq_read,
120 .llseek = seq_lseek,
121 .release = single_release,
122};
123#endif
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* --------------------------------------------------------------------------
126 AC Adapter Management
127 -------------------------------------------------------------------------- */
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Guenter Roeck98012842014-05-06 19:18:28 -0700131 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Guenter Roeck98012842014-05-06 19:18:28 -0700133 if (!ac)
134 return -EINVAL;
135
136 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
Zhang Ruicc8ef522013-09-25 20:39:45 +0800137 &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (ACPI_FAILURE(status)) {
Zhang Ruicc8ef522013-09-25 20:39:45 +0800139 ACPI_EXCEPTION((AE_INFO, status,
140 "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -0400142 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Len Brown4be44fc2005-08-05 00:44:28 -0400144
Patrick Mocheld550d982006-06-27 00:41:40 -0400145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Zhang Rui3151dbb2010-12-08 10:40:45 +0800148/* --------------------------------------------------------------------------
149 sysfs I/F
150 -------------------------------------------------------------------------- */
151static int get_ac_property(struct power_supply *psy,
152 enum power_supply_property psp,
153 union power_supply_propval *val)
154{
155 struct acpi_ac *ac = to_acpi_ac(psy);
156
157 if (!ac)
158 return -ENODEV;
159
160 if (acpi_ac_get_state(ac))
161 return -ENODEV;
162
163 switch (psp) {
164 case POWER_SUPPLY_PROP_ONLINE:
165 val->intval = ac->state;
166 break;
167 default:
168 return -EINVAL;
169 }
170 return 0;
171}
172
173static enum power_supply_property ac_props[] = {
174 POWER_SUPPLY_PROP_ONLINE,
175};
176
Lan Tianyue63f6e22014-07-07 01:13:46 +0200177#ifdef CONFIG_ACPI_PROCFS_POWER
178/* --------------------------------------------------------------------------
179 FS Interface (/proc)
180 -------------------------------------------------------------------------- */
181
182static struct proc_dir_entry *acpi_ac_dir;
183
184static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
185{
186 struct acpi_ac *ac = seq->private;
187
188
189 if (!ac)
190 return 0;
191
192 if (acpi_ac_get_state(ac)) {
193 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
194 return 0;
195 }
196
197 seq_puts(seq, "state: ");
198 switch (ac->state) {
199 case ACPI_AC_STATUS_OFFLINE:
200 seq_puts(seq, "off-line\n");
201 break;
202 case ACPI_AC_STATUS_ONLINE:
203 seq_puts(seq, "on-line\n");
204 break;
205 default:
206 seq_puts(seq, "unknown\n");
207 break;
208 }
209
210 return 0;
211}
212
213static int acpi_ac_open_fs(struct inode *inode, struct file *file)
214{
215 return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
216}
217
218static int acpi_ac_add_fs(struct acpi_ac *ac)
219{
220 struct proc_dir_entry *entry = NULL;
221
222 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
223 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
224 if (!acpi_device_dir(ac->device)) {
225 acpi_device_dir(ac->device) =
226 proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir);
227 if (!acpi_device_dir(ac->device))
228 return -ENODEV;
229 }
230
231 /* 'state' [R] */
232 entry = proc_create_data(ACPI_AC_FILE_STATE,
233 S_IRUGO, acpi_device_dir(ac->device),
234 &acpi_ac_fops, ac);
235 if (!entry)
236 return -ENODEV;
237 return 0;
238}
239
240static int acpi_ac_remove_fs(struct acpi_ac *ac)
241{
242
243 if (acpi_device_dir(ac->device)) {
244 remove_proc_entry(ACPI_AC_FILE_STATE,
245 acpi_device_dir(ac->device));
246 remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir);
247 acpi_device_dir(ac->device) = NULL;
248 }
249
250 return 0;
251}
252#endif
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/* --------------------------------------------------------------------------
255 Driver Model
256 -------------------------------------------------------------------------- */
257
Guenter Roeck98012842014-05-06 19:18:28 -0700258static void acpi_ac_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Guenter Roeck98012842014-05-06 19:18:28 -0700260 struct acpi_ac *ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 switch (event) {
Len Brownf163ff52008-06-14 01:26:37 -0400266 default:
267 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
268 "Unsupported event [0x%x]\n", event));
Gustavo A. R. Silva1be9c3a2017-10-12 13:49:18 -0500269 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d78252004-08-19 01:26:00 -0400271 case ACPI_NOTIFY_BUS_CHECK:
272 case ACPI_NOTIFY_DEVICE_CHECK:
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000273 /*
274 * A buggy BIOS may notify AC first and then sleep for
275 * a specific time before doing actual operations in the
276 * EC event handler (_Qxx). This will cause the AC state
277 * reported by the ACPI event to be incorrect, so wait for a
278 * specific time for the EC event handler to make progress.
279 */
280 if (ac_sleep_before_get_state_ms > 0)
281 msleep(ac_sleep_before_get_state_ms);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 acpi_ac_get_state(ac);
Guenter Roeck98012842014-05-06 19:18:28 -0700284 acpi_bus_generate_netlink_event(device->pnp.device_class,
285 dev_name(&device->dev), event,
286 (u32) ac->state);
287 acpi_notifier_call_chain(device, event, (u32) ac->state);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100288 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700294static int acpi_ac_battery_notify(struct notifier_block *nb,
295 unsigned long action, void *data)
296{
297 struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
298 struct acpi_bus_event *event = (struct acpi_bus_event *)data;
299
300 /*
301 * On HP Pavilion dv6-6179er AC status notifications aren't triggered
302 * when adapter is plugged/unplugged. However, battery status
303 * notifcations are triggered when battery starts charging or
304 * discharging. Re-reading AC status triggers lost AC notifications,
305 * if AC status has changed.
306 */
307 if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
308 event->type == ACPI_BATTERY_NOTIFY_STATUS)
309 acpi_ac_get_state(ac);
310
311 return NOTIFY_OK;
312}
313
Carlo Caione91ea5b12018-04-18 14:04:40 +0200314static int __init thinkpad_e530_quirk(const struct dmi_system_id *d)
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000315{
316 ac_sleep_before_get_state_ms = 1000;
317 return 0;
318}
319
Carlo Caione91ea5b12018-04-18 14:04:40 +0200320static int __init ac_do_not_check_pmic_quirk(const struct dmi_system_id *d)
321{
322 ac_check_pmic = 0;
323 return 0;
324}
325
326static const struct dmi_system_id ac_dmi_table[] __initconst = {
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000327 {
328 .callback = thinkpad_e530_quirk,
329 .ident = "thinkpad e530",
330 .matches = {
331 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
332 DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
333 },
334 },
Carlo Caione91ea5b12018-04-18 14:04:40 +0200335 {
336 /* ECS EF20EA */
337 .callback = ac_do_not_check_pmic_quirk,
338 .matches = {
339 DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
340 },
341 },
342 {
343 /* Lenovo Ideapad Miix 320 */
344 .callback = ac_do_not_check_pmic_quirk,
345 .matches = {
346 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
347 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
348 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
349 },
350 },
Lan Tianyu0ab5bb62013-05-08 07:28:46 +0000351 {},
352};
353
Guenter Roeck98012842014-05-06 19:18:28 -0700354static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100356 struct power_supply_config psy_cfg = {};
Len Brown4be44fc2005-08-05 00:44:28 -0400357 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400358 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Guenter Roeck98012842014-05-06 19:18:28 -0700360
361 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400362 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Burman Yan36bcbec2006-12-19 12:56:11 -0800364 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Guenter Roeck98012842014-05-06 19:18:28 -0700368 ac->device = device;
369 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
370 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
371 device->driver_data = ac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 result = acpi_ac_get_state(ac);
374 if (result)
375 goto end;
376
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100377 psy_cfg.drv_data = ac;
378
379 ac->charger_desc.name = acpi_device_bid(device);
Lan Tianyue63f6e22014-07-07 01:13:46 +0200380#ifdef CONFIG_ACPI_PROCFS_POWER
381 result = acpi_ac_add_fs(ac);
382 if (result)
383 goto end;
384#endif
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100385 ac->charger_desc.type = POWER_SUPPLY_TYPE_MAINS;
386 ac->charger_desc.properties = ac_props;
387 ac->charger_desc.num_properties = ARRAY_SIZE(ac_props);
388 ac->charger_desc.get_property = get_ac_property;
389 ac->charger = power_supply_register(&ac->device->dev,
390 &ac->charger_desc, &psy_cfg);
391 if (IS_ERR(ac->charger)) {
392 result = PTR_ERR(ac->charger);
Lan Tianyuf197ac12012-07-20 13:29:16 +0800393 goto end;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Guenter Roeck98012842014-05-06 19:18:28 -0700397 acpi_device_name(device), acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400398 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700400 ac->battery_nb.notifier_call = acpi_ac_battery_notify;
401 register_acpi_notifier(&ac->battery_nb);
Lan Tianyuab0fd672013-10-12 21:04:48 +0800402end:
Lan Tianyue63f6e22014-07-07 01:13:46 +0200403 if (result) {
404#ifdef CONFIG_ACPI_PROCFS_POWER
405 acpi_ac_remove_fs(ac);
406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 kfree(ac);
Lan Tianyue63f6e22014-07-07 01:13:46 +0200408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Patrick Mocheld550d982006-06-27 00:41:40 -0400410 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200413#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200414static int acpi_ac_resume(struct device *dev)
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800415{
416 struct acpi_ac *ac;
417 unsigned old_state;
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200418
419 if (!dev)
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800420 return -EINVAL;
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200421
Guenter Roeck98012842014-05-06 19:18:28 -0700422 ac = acpi_driver_data(to_acpi_device(dev));
Rafael J. Wysockiccda7062012-06-27 23:26:35 +0200423 if (!ac)
424 return -EINVAL;
425
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800426 old_state = ac->state;
427 if (acpi_ac_get_state(ac))
428 return 0;
429 if (old_state != ac->state)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100430 kobject_uevent(&ac->charger->dev.kobj, KOBJ_CHANGE);
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800431 return 0;
432}
Shuah Khan06521c22014-02-12 20:19:05 -0700433#else
434#define acpi_ac_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200435#endif
Alexey Starikovskiy5bfeca32007-11-14 17:00:39 -0800436
Guenter Roeck98012842014-05-06 19:18:28 -0700437static int acpi_ac_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Guenter Roeck98012842014-05-06 19:18:28 -0700439 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Guenter Roeck98012842014-05-06 19:18:28 -0700441
442 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Guenter Roeck98012842014-05-06 19:18:28 -0700445 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100447 power_supply_unregister(ac->charger);
Alexander Mezin4eee4f02014-03-12 00:58:48 +0700448 unregister_acpi_notifier(&ac->battery_nb);
Zhang Ruicc8ef522013-09-25 20:39:45 +0800449
Lan Tianyue63f6e22014-07-07 01:13:46 +0200450#ifdef CONFIG_ACPI_PROCFS_POWER
451 acpi_ac_remove_fs(ac);
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 kfree(ac);
455
Patrick Mocheld550d982006-06-27 00:41:40 -0400456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Len Brown4be44fc2005-08-05 00:44:28 -0400459static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Hans de Goedeaf3ec832017-04-19 14:02:11 +0200461 unsigned int i;
Rich Townsend3f86b832006-07-01 11:36:54 -0400462 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Pavel Machek4d8316d2006-08-14 22:37:22 -0700464 if (acpi_disabled)
465 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Carlo Caione91ea5b12018-04-18 14:04:40 +0200467 dmi_check_system(ac_dmi_table);
468
469 if (ac_check_pmic) {
470 for (i = 0; i < ARRAY_SIZE(acpi_ac_blacklist); i++)
471 if (acpi_dev_present(acpi_ac_blacklist[i].hid, "1",
472 acpi_ac_blacklist[i].hrv)) {
473 pr_info(PREFIX "AC: found native %s PMIC, not loading\n",
474 acpi_ac_blacklist[i].hid);
475 return -ENODEV;
476 }
477 }
Hans de Goedeaf3ec832017-04-19 14:02:11 +0200478
Lan Tianyue63f6e22014-07-07 01:13:46 +0200479#ifdef CONFIG_ACPI_PROCFS_POWER
480 acpi_ac_dir = acpi_lock_ac_dir();
481 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400482 return -ENODEV;
Lan Tianyue63f6e22014-07-07 01:13:46 +0200483#endif
484
485
486 result = acpi_bus_register_driver(&acpi_ac_driver);
487 if (result < 0) {
488#ifdef CONFIG_ACPI_PROCFS_POWER
489 acpi_unlock_ac_dir(acpi_ac_dir);
490#endif
491 return -ENODEV;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Len Brown4be44fc2005-08-05 00:44:28 -0400497static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Guenter Roeck98012842014-05-06 19:18:28 -0700499 acpi_bus_unregister_driver(&acpi_ac_driver);
Lan Tianyue63f6e22014-07-07 01:13:46 +0200500#ifdef CONFIG_ACPI_PROCFS_POWER
501 acpi_unlock_ac_dir(acpi_ac_dir);
502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504module_init(acpi_ac_init);
505module_exit(acpi_ac_exit);