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> |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 33 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/proc_fs.h> |
| 35 | #include <linux/seq_file.h> |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <acpi/acpi_bus.h> |
| 40 | #include <acpi/acpi_drivers.h> |
| 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 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 59 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 60 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
| 61 | extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 62 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
| 63 | #endif |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 64 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 65 | static int ac_sleep_before_get_state_ms; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct acpi_ac { |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 68 | struct power_supply charger; |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 69 | struct acpi_device *adev; |
| 70 | struct platform_device *pdev; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 71 | unsigned long long state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 74 | #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger) |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 75 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 76 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 77 | static const struct file_operations acpi_ac_fops = { |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 78 | .owner = THIS_MODULE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 79 | .open = acpi_ac_open_fs, |
| 80 | .read = seq_read, |
| 81 | .llseek = seq_lseek, |
| 82 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 84 | #endif |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* -------------------------------------------------------------------------- |
| 87 | AC Adapter Management |
| 88 | -------------------------------------------------------------------------- */ |
| 89 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 90 | static int acpi_ac_get_state(struct acpi_ac *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 92 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 94 | status = acpi_evaluate_integer(ac->adev->handle, "_PSR", NULL, |
| 95 | &ac->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (ACPI_FAILURE(status)) { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 97 | ACPI_EXCEPTION((AE_INFO, status, |
| 98 | "Error reading AC Adapter state")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 100 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 102 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 103 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Zhang Rui | 3151dbb | 2010-12-08 10:40:45 +0800 | [diff] [blame] | 106 | /* -------------------------------------------------------------------------- |
| 107 | sysfs I/F |
| 108 | -------------------------------------------------------------------------- */ |
| 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 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 135 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* -------------------------------------------------------------------------- |
| 137 | FS Interface (/proc) |
| 138 | -------------------------------------------------------------------------- */ |
| 139 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 140 | static struct proc_dir_entry *acpi_ac_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) |
| 143 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 144 | struct acpi_ac *ac = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 148 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | if (acpi_ac_get_state(ac)) { |
| 151 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 152 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | seq_puts(seq, "state: "); |
| 156 | switch (ac->state) { |
| 157 | case ACPI_AC_STATUS_OFFLINE: |
| 158 | seq_puts(seq, "off-line\n"); |
| 159 | break; |
| 160 | case ACPI_AC_STATUS_ONLINE: |
| 161 | seq_puts(seq, "on-line\n"); |
| 162 | break; |
| 163 | default: |
| 164 | seq_puts(seq, "unknown\n"); |
| 165 | break; |
| 166 | } |
| 167 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 168 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | static int acpi_ac_open_fs(struct inode *inode, struct file *file) |
| 172 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 173 | return single_open(file, acpi_ac_seq_show, PDE_DATA(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 176 | static int acpi_ac_add_fs(struct acpi_ac *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 178 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Zhang Rui | 6d855fc | 2011-01-10 11:16:30 +0800 | [diff] [blame] | 180 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded," |
| 181 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 182 | if (!acpi_device_dir(ac->adev)) { |
| 183 | acpi_device_dir(ac->adev) = |
| 184 | proc_mkdir(acpi_device_bid(ac->adev), acpi_ac_dir); |
| 185 | if (!acpi_device_dir(ac->adev)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 186 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* 'state' [R] */ |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 190 | entry = proc_create_data(ACPI_AC_FILE_STATE, |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 191 | S_IRUGO, acpi_device_dir(ac->adev), |
| 192 | &acpi_ac_fops, ac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 194 | return -ENODEV; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 195 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 198 | static int acpi_ac_remove_fs(struct acpi_ac *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 201 | if (acpi_device_dir(ac->adev)) { |
| 202 | remove_proc_entry(ACPI_AC_FILE_STATE, |
| 203 | acpi_device_dir(ac->adev)); |
| 204 | remove_proc_entry(acpi_device_bid(ac->adev), acpi_ac_dir); |
| 205 | acpi_device_dir(ac->adev) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 208 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 210 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* -------------------------------------------------------------------------- |
| 213 | Driver Model |
| 214 | -------------------------------------------------------------------------- */ |
| 215 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 216 | static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 218 | struct acpi_ac *ac = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 221 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | switch (event) { |
Len Brown | f163ff5 | 2008-06-14 01:26:37 -0400 | [diff] [blame] | 224 | default: |
| 225 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 226 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | case ACPI_AC_NOTIFY_STATUS: |
Christian Lupien | 03d7825 | 2004-08-19 01:26:00 -0400 | [diff] [blame] | 228 | case ACPI_NOTIFY_BUS_CHECK: |
| 229 | case ACPI_NOTIFY_DEVICE_CHECK: |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 230 | /* |
| 231 | * A buggy BIOS may notify AC first and then sleep for |
| 232 | * a specific time before doing actual operations in the |
| 233 | * EC event handler (_Qxx). This will cause the AC state |
| 234 | * reported by the ACPI event to be incorrect, so wait for a |
| 235 | * specific time for the EC event handler to make progress. |
| 236 | */ |
| 237 | if (ac_sleep_before_get_state_ms > 0) |
| 238 | msleep(ac_sleep_before_get_state_ms); |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | acpi_ac_get_state(ac); |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 241 | acpi_bus_generate_netlink_event(ac->adev->pnp.device_class, |
| 242 | dev_name(&ac->pdev->dev), |
| 243 | event, (u32) ac->state); |
| 244 | acpi_notifier_call_chain(ac->adev, event, (u32) ac->state); |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 245 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 248 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 251 | static int thinkpad_e530_quirk(const struct dmi_system_id *d) |
| 252 | { |
| 253 | ac_sleep_before_get_state_ms = 1000; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static struct dmi_system_id ac_dmi_table[] = { |
| 258 | { |
| 259 | .callback = thinkpad_e530_quirk, |
| 260 | .ident = "thinkpad e530", |
| 261 | .matches = { |
| 262 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), |
| 263 | DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"), |
| 264 | }, |
| 265 | }, |
| 266 | {}, |
| 267 | }; |
| 268 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 269 | static int acpi_ac_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 271 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 272 | struct acpi_ac *ac = NULL; |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 273 | struct acpi_device *adev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 275 | if (!pdev) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 276 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 278 | result = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev); |
| 279 | if (result) |
| 280 | return -ENODEV; |
| 281 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 282 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (!ac) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 284 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 286 | strcpy(acpi_device_name(adev), ACPI_AC_DEVICE_NAME); |
| 287 | strcpy(acpi_device_class(adev), ACPI_AC_CLASS); |
| 288 | ac->adev = adev; |
| 289 | ac->pdev = pdev; |
| 290 | platform_set_drvdata(pdev, ac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | result = acpi_ac_get_state(ac); |
| 293 | if (result) |
| 294 | goto end; |
| 295 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 296 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 297 | result = acpi_ac_add_fs(ac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (result) |
| 299 | goto end; |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 300 | #endif |
| 301 | ac->charger.name = acpi_device_bid(adev); |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 302 | ac->charger.type = POWER_SUPPLY_TYPE_MAINS; |
| 303 | ac->charger.properties = ac_props; |
| 304 | ac->charger.num_properties = ARRAY_SIZE(ac_props); |
| 305 | ac->charger.get_property = get_ac_property; |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 306 | result = power_supply_register(&pdev->dev, &ac->charger); |
Lan Tianyu | f197ac1 | 2012-07-20 13:29:16 +0800 | [diff] [blame] | 307 | if (result) |
| 308 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 310 | result = acpi_install_notify_handler(ACPI_HANDLE(&pdev->dev), |
| 311 | ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler, ac); |
| 312 | if (result) { |
| 313 | power_supply_unregister(&ac->charger); |
| 314 | goto end; |
| 315 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 316 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 317 | acpi_device_name(adev), acpi_device_bid(adev), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 318 | ac->state ? "on-line" : "off-line"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 320 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | if (result) { |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 322 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 323 | acpi_ac_remove_fs(ac); |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 324 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | kfree(ac); |
| 326 | } |
| 327 | |
Lan Tianyu | 0ab5bb6 | 2013-05-08 07:28:46 +0000 | [diff] [blame] | 328 | dmi_check_system(ac_dmi_table); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 329 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 332 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 333 | static int acpi_ac_resume(struct device *dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 334 | { |
| 335 | struct acpi_ac *ac; |
| 336 | unsigned old_state; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 337 | |
| 338 | if (!dev) |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 339 | return -EINVAL; |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 340 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 341 | ac = platform_get_drvdata(to_platform_device(dev)); |
Rafael J. Wysocki | ccda706 | 2012-06-27 23:26:35 +0200 | [diff] [blame] | 342 | if (!ac) |
| 343 | return -EINVAL; |
| 344 | |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 345 | old_state = ac->state; |
| 346 | if (acpi_ac_get_state(ac)) |
| 347 | return 0; |
| 348 | if (old_state != ac->state) |
| 349 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
| 350 | return 0; |
| 351 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 352 | #endif |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 353 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume); |
Alexey Starikovskiy | 5bfeca3 | 2007-11-14 17:00:39 -0800 | [diff] [blame] | 354 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 355 | static int acpi_ac_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 357 | struct acpi_ac *ac; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 359 | if (!pdev) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 360 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 362 | acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), |
| 363 | ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 365 | ac = platform_get_drvdata(pdev); |
Alexey Starikovskiy | d5b4a3d | 2007-09-26 19:44:06 +0400 | [diff] [blame] | 366 | if (ac->charger.dev) |
| 367 | power_supply_unregister(&ac->charger); |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 368 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 369 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 370 | acpi_ac_remove_fs(ac); |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 371 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | kfree(ac); |
| 374 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 375 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 378 | static const struct acpi_device_id acpi_ac_match[] = { |
| 379 | { "ACPI0003", 0 }, |
| 380 | { } |
| 381 | }; |
| 382 | MODULE_DEVICE_TABLE(acpi, acpi_ac_match); |
| 383 | |
| 384 | static struct platform_driver acpi_ac_driver = { |
| 385 | .probe = acpi_ac_probe, |
| 386 | .remove = acpi_ac_remove, |
| 387 | .driver = { |
| 388 | .name = "acpi-ac", |
| 389 | .owner = THIS_MODULE, |
| 390 | .pm = &acpi_ac_pm_ops, |
| 391 | .acpi_match_table = ACPI_PTR(acpi_ac_match), |
| 392 | }, |
| 393 | }; |
| 394 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 395 | static int __init acpi_ac_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 397 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 399 | if (acpi_disabled) |
| 400 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 402 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 403 | acpi_ac_dir = acpi_lock_ac_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (!acpi_ac_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 405 | return -ENODEV; |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 406 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 408 | result = platform_driver_register(&acpi_ac_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (result < 0) { |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 410 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 411 | acpi_unlock_ac_dir(acpi_ac_dir); |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 412 | #endif |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 413 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 416 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 419 | static void __exit acpi_ac_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
Zhang Rui | cc8ef52 | 2013-09-25 20:39:45 +0800 | [diff] [blame^] | 421 | platform_driver_unregister(&acpi_ac_driver); |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 422 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 423 | acpi_unlock_ac_dir(acpi_ac_dir); |
Andrey Borzenkov | 8a246ee | 2007-11-14 17:00:37 -0800 | [diff] [blame] | 424 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | module_init(acpi_ac_init); |
| 427 | module_exit(acpi_ac_exit); |