Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/init.h> |
| 30 | #include <linux/types.h> |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 31 | #include <linux/dmi.h> |
| 32 | #include <linux/delay.h> |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 34 | #include <linux/proc_fs.h> |
| 35 | #include <linux/seq_file.h> |
| 36 | #endif |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 37 | #include <linux/platform_device.h> |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 38 | #include <linux/power_supply.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 39 | #include <linux/acpi.h> |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 40 | #include "battery.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 42 | #define PREFIX "ACPI: " |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define ACPI_AC_CLASS "ac_adapter" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define ACPI_AC_DEVICE_NAME "AC Adapter" |
| 46 | #define ACPI_AC_FILE_STATE "state" |
| 47 | #define ACPI_AC_NOTIFY_STATUS 0x80 |
| 48 | #define ACPI_AC_STATUS_OFFLINE 0x00 |
| 49 | #define ACPI_AC_STATUS_ONLINE 0x01 |
| 50 | #define ACPI_AC_STATUS_UNKNOWN 0xFF |
| 51 | |
| 52 | #define _COMPONENT ACPI_AC_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 53 | ACPI_MODULE_NAME("ac"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 55 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 56 | MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | MODULE_LICENSE("GPL"); |
| 58 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 59 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 60 | static int acpi_ac_add(struct acpi_device *device); |
| 61 | static int acpi_ac_remove(struct acpi_device *device); |
| 62 | static void acpi_ac_notify(struct acpi_device *device, u32 event); |
| 63 | |
| 64 | static const struct acpi_device_id ac_device_ids[] = { |
| 65 | {"ACPI0003", 0}, |
| 66 | {"", 0}, |
| 67 | }; |
| 68 | MODULE_DEVICE_TABLE(acpi, ac_device_ids); |
| 69 | |
| 70 | #ifdef CONFIG_PM_SLEEP |
| 71 | static int acpi_ac_resume(struct device *dev); |
| 72 | #endif |
| 73 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume); |
| 74 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 75 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 76 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
| 77 | extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); |
| 78 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
| 79 | #endif |
| 80 | |
| 81 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 82 | static int ac_sleep_before_get_state_ms; |
| 83 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 84 | static struct acpi_driver acpi_ac_driver = { |
| 85 | .name = "ac", |
| 86 | .class = ACPI_AC_CLASS, |
| 87 | .ids = ac_device_ids, |
| 88 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
| 89 | .ops = { |
| 90 | .add = acpi_ac_add, |
| 91 | .remove = acpi_ac_remove, |
| 92 | .notify = acpi_ac_notify, |
| 93 | }, |
| 94 | .drv.pm = &acpi_ac_pm, |
| 95 | }; |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct acpi_ac { |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 98 | struct power_supply charger; |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 99 | struct acpi_device * device; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 100 | unsigned long long state; |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 101 | struct notifier_block battery_nb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 104 | #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger) |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 105 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 106 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 107 | static const struct file_operations acpi_ac_fops = { |
| 108 | .owner = THIS_MODULE, |
| 109 | .open = acpi_ac_open_fs, |
| 110 | .read = seq_read, |
| 111 | .llseek = seq_lseek, |
| 112 | .release = single_release, |
| 113 | }; |
| 114 | #endif |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* -------------------------------------------------------------------------- |
| 117 | AC Adapter Management |
| 118 | -------------------------------------------------------------------------- */ |
| 119 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 120 | static int acpi_ac_get_state(struct acpi_ac *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 122 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 124 | if (!ac) |
| 125 | return -EINVAL; |
| 126 | |
| 127 | status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 128 | &ac->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (ACPI_FAILURE(status)) { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 130 | ACPI_EXCEPTION((AE_INFO, status, |
| 131 | "Error reading AC Adapter state")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 133 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 135 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 136 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Zhang Rui | 3151dbb | 2010-12-08 10:40:45 +0800 | [diff] [blame] | 139 | /* -------------------------------------------------------------------------- |
| 140 | sysfs I/F |
| 141 | -------------------------------------------------------------------------- */ |
| 142 | static int get_ac_property(struct power_supply *psy, |
| 143 | enum power_supply_property psp, |
| 144 | union power_supply_propval *val) |
| 145 | { |
| 146 | struct acpi_ac *ac = to_acpi_ac(psy); |
| 147 | |
| 148 | if (!ac) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | if (acpi_ac_get_state(ac)) |
| 152 | return -ENODEV; |
| 153 | |
| 154 | switch (psp) { |
| 155 | case POWER_SUPPLY_PROP_ONLINE: |
| 156 | val->intval = ac->state; |
| 157 | break; |
| 158 | default: |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static enum power_supply_property ac_props[] = { |
| 165 | POWER_SUPPLY_PROP_ONLINE, |
| 166 | }; |
| 167 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 169 | /* -------------------------------------------------------------------------- |
| 170 | FS Interface (/proc) |
| 171 | -------------------------------------------------------------------------- */ |
| 172 | |
| 173 | static struct proc_dir_entry *acpi_ac_dir; |
| 174 | |
| 175 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) |
| 176 | { |
| 177 | struct acpi_ac *ac = seq->private; |
| 178 | |
| 179 | |
| 180 | if (!ac) |
| 181 | return 0; |
| 182 | |
| 183 | if (acpi_ac_get_state(ac)) { |
| 184 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | seq_puts(seq, "state: "); |
| 189 | switch (ac->state) { |
| 190 | case ACPI_AC_STATUS_OFFLINE: |
| 191 | seq_puts(seq, "off-line\n"); |
| 192 | break; |
| 193 | case ACPI_AC_STATUS_ONLINE: |
| 194 | seq_puts(seq, "on-line\n"); |
| 195 | break; |
| 196 | default: |
| 197 | seq_puts(seq, "unknown\n"); |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int acpi_ac_open_fs(struct inode *inode, struct file *file) |
| 205 | { |
| 206 | return single_open(file, acpi_ac_seq_show, PDE_DATA(inode)); |
| 207 | } |
| 208 | |
| 209 | static int acpi_ac_add_fs(struct acpi_ac *ac) |
| 210 | { |
| 211 | struct proc_dir_entry *entry = NULL; |
| 212 | |
| 213 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded," |
| 214 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); |
| 215 | if (!acpi_device_dir(ac->device)) { |
| 216 | acpi_device_dir(ac->device) = |
| 217 | proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir); |
| 218 | if (!acpi_device_dir(ac->device)) |
| 219 | return -ENODEV; |
| 220 | } |
| 221 | |
| 222 | /* 'state' [R] */ |
| 223 | entry = proc_create_data(ACPI_AC_FILE_STATE, |
| 224 | S_IRUGO, acpi_device_dir(ac->device), |
| 225 | &acpi_ac_fops, ac); |
| 226 | if (!entry) |
| 227 | return -ENODEV; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int acpi_ac_remove_fs(struct acpi_ac *ac) |
| 232 | { |
| 233 | |
| 234 | if (acpi_device_dir(ac->device)) { |
| 235 | remove_proc_entry(ACPI_AC_FILE_STATE, |
| 236 | acpi_device_dir(ac->device)); |
| 237 | remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir); |
| 238 | acpi_device_dir(ac->device) = NULL; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | #endif |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* -------------------------------------------------------------------------- |
| 246 | Driver Model |
| 247 | -------------------------------------------------------------------------- */ |
| 248 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 249 | static void acpi_ac_notify(struct acpi_device *device, u32 event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 251 | struct acpi_ac *ac = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 254 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | switch (event) { |
Len Brown | f163ff5 | 2008-06-14 01:26:37 -0400 | [diff] [blame] | 257 | default: |
| 258 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 259 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | case ACPI_AC_NOTIFY_STATUS: |
Christian Lupien | 03d7825 | 2004-08-19 01:26:00 -0400 | [diff] [blame] | 261 | case ACPI_NOTIFY_BUS_CHECK: |
| 262 | case ACPI_NOTIFY_DEVICE_CHECK: |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 263 | /* |
| 264 | * A buggy BIOS may notify AC first and then sleep for |
| 265 | * a specific time before doing actual operations in the |
| 266 | * EC event handler (_Qxx). This will cause the AC state |
| 267 | * reported by the ACPI event to be incorrect, so wait for a |
| 268 | * specific time for the EC event handler to make progress. |
| 269 | */ |
| 270 | if (ac_sleep_before_get_state_ms > 0) |
| 271 | msleep(ac_sleep_before_get_state_ms); |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | acpi_ac_get_state(ac); |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 274 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
| 275 | dev_name(&device->dev), event, |
| 276 | (u32) ac->state); |
| 277 | acpi_notifier_call_chain(device, event, (u32) ac->state); |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 278 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 281 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 284 | static int acpi_ac_battery_notify(struct notifier_block *nb, |
| 285 | unsigned long action, void *data) |
| 286 | { |
| 287 | struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb); |
| 288 | struct acpi_bus_event *event = (struct acpi_bus_event *)data; |
| 289 | |
| 290 | /* |
| 291 | * On HP Pavilion dv6-6179er AC status notifications aren't triggered |
| 292 | * when adapter is plugged/unplugged. However, battery status |
| 293 | * notifcations are triggered when battery starts charging or |
| 294 | * discharging. Re-reading AC status triggers lost AC notifications, |
| 295 | * if AC status has changed. |
| 296 | */ |
| 297 | if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 && |
| 298 | event->type == ACPI_BATTERY_NOTIFY_STATUS) |
| 299 | acpi_ac_get_state(ac); |
| 300 | |
| 301 | return NOTIFY_OK; |
| 302 | } |
| 303 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 304 | static int thinkpad_e530_quirk(const struct dmi_system_id *d) |
| 305 | { |
| 306 | ac_sleep_before_get_state_ms = 1000; |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static struct dmi_system_id ac_dmi_table[] = { |
| 311 | { |
| 312 | .callback = thinkpad_e530_quirk, |
| 313 | .ident = "thinkpad e530", |
| 314 | .matches = { |
| 315 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 316 | DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"), |
| 317 | }, |
| 318 | }, |
| 319 | {}, |
| 320 | }; |
| 321 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 322 | static int acpi_ac_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 324 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 325 | struct acpi_ac *ac = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 327 | |
| 328 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 329 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 331 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 333 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 335 | ac->device = device; |
| 336 | strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME); |
| 337 | strcpy(acpi_device_class(device), ACPI_AC_CLASS); |
| 338 | device->driver_data = ac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | result = acpi_ac_get_state(ac); |
| 341 | if (result) |
| 342 | goto end; |
| 343 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 344 | ac->charger.name = acpi_device_bid(device); |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 345 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 346 | result = acpi_ac_add_fs(ac); |
| 347 | if (result) |
| 348 | goto end; |
| 349 | #endif |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 350 | ac->charger.type = POWER_SUPPLY_TYPE_MAINS; |
| 351 | ac->charger.properties = ac_props; |
| 352 | ac->charger.num_properties = ARRAY_SIZE(ac_props); |
| 353 | ac->charger.get_property = get_ac_property; |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 354 | result = power_supply_register(&ac->device->dev, &ac->charger); |
Lan Tianyu | f197ac1 | 2012-07-20 13:29:16 +0800 | [diff] [blame] | 355 | if (result) |
| 356 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 358 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 359 | acpi_device_name(device), acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 360 | ac->state ? "on-line" : "off-line"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 362 | ac->battery_nb.notifier_call = acpi_ac_battery_notify; |
| 363 | register_acpi_notifier(&ac->battery_nb); |
Lan Tianyu | ab0fd67 | 2013-10-12 21:04:48 +0800 | [diff] [blame] | 364 | end: |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 365 | if (result) { |
| 366 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 367 | acpi_ac_remove_fs(ac); |
| 368 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | kfree(ac); |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 372 | dmi_check_system(ac_dmi_table); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 373 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 376 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 377 | static int acpi_ac_resume(struct device *dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 378 | { |
| 379 | struct acpi_ac *ac; |
| 380 | unsigned old_state; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 381 | |
| 382 | if (!dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 383 | return -EINVAL; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 384 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 385 | ac = acpi_driver_data(to_acpi_device(dev)); |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 386 | if (!ac) |
| 387 | return -EINVAL; |
| 388 | |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 389 | old_state = ac->state; |
| 390 | if (acpi_ac_get_state(ac)) |
| 391 | return 0; |
| 392 | if (old_state != ac->state) |
| 393 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
| 394 | return 0; |
| 395 | } |
Shuah Khan | 06521c2 | 2014-02-12 20:19:05 -0700 | [diff] [blame] | 396 | #else |
| 397 | #define acpi_ac_resume NULL |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 398 | #endif |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 399 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 400 | static int acpi_ac_remove(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 402 | struct acpi_ac *ac = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 404 | |
| 405 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 406 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 408 | ac = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 410 | if (ac->charger.dev) |
| 411 | power_supply_unregister(&ac->charger); |
Alexander Mezin | 4eee4f0 | 2014-03-12 00:58:48 +0700 | [diff] [blame] | 412 | unregister_acpi_notifier(&ac->battery_nb); |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame] | 413 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 414 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 415 | acpi_ac_remove_fs(ac); |
| 416 | #endif |
| 417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | kfree(ac); |
| 419 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 420 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 423 | static int __init acpi_ac_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 425 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 427 | if (acpi_disabled) |
| 428 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 430 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 431 | acpi_ac_dir = acpi_lock_ac_dir(); |
| 432 | if (!acpi_ac_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 433 | return -ENODEV; |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 434 | #endif |
| 435 | |
| 436 | |
| 437 | result = acpi_bus_register_driver(&acpi_ac_driver); |
| 438 | if (result < 0) { |
| 439 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 440 | acpi_unlock_ac_dir(acpi_ac_dir); |
| 441 | #endif |
| 442 | return -ENODEV; |
| 443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 445 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 448 | static void __exit acpi_ac_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Guenter Roeck | 9801284 | 2014-05-06 19:18:28 -0700 | [diff] [blame] | 450 | acpi_bus_unregister_driver(&acpi_ac_driver); |
Lan Tianyu | e63f6e2 | 2014-07-07 01:13:46 +0200 | [diff] [blame] | 451 | #ifdef CONFIG_ACPI_PROCFS_POWER |
| 452 | acpi_unlock_ac_dir(acpi_ac_dir); |
| 453 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | module_init(acpi_ac_init); |
| 456 | module_exit(acpi_ac_exit); |