Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
1da177e4 LT |
29 | #include <linux/init.h> |
30 | #include <linux/types.h> | |
0ab5bb64 LT |
31 | #include <linux/dmi.h> |
32 | #include <linux/delay.h> | |
fdcedbba | 33 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
34 | #include <linux/proc_fs.h> |
35 | #include <linux/seq_file.h> | |
8a246ee4 | 36 | #endif |
cc8ef527 | 37 | #include <linux/platform_device.h> |
d5b4a3d0 | 38 | #include <linux/power_supply.h> |
1da177e4 LT |
39 | #include <acpi/acpi_bus.h> |
40 | #include <acpi/acpi_drivers.h> | |
41 | ||
a192a958 LB |
42 | #define PREFIX "ACPI: " |
43 | ||
1da177e4 | 44 | #define ACPI_AC_CLASS "ac_adapter" |
1da177e4 LT |
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 | |
f52fd66d | 53 | ACPI_MODULE_NAME("ac"); |
1da177e4 | 54 | |
f52fd66d | 55 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 56 | MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
1da177e4 LT |
57 | MODULE_LICENSE("GPL"); |
58 | ||
fdcedbba | 59 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 RT |
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); | |
8a246ee4 AB |
62 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
63 | #endif | |
3f86b832 | 64 | |
0ab5bb64 LT |
65 | static int ac_sleep_before_get_state_ms; |
66 | ||
1da177e4 | 67 | struct acpi_ac { |
d5b4a3d0 | 68 | struct power_supply charger; |
cc8ef527 ZR |
69 | struct acpi_device *adev; |
70 | struct platform_device *pdev; | |
27663c58 | 71 | unsigned long long state; |
1da177e4 LT |
72 | }; |
73 | ||
497888cf | 74 | #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger) |
d5b4a3d0 | 75 | |
fdcedbba | 76 | #ifdef CONFIG_ACPI_PROCFS_POWER |
d7508032 | 77 | static const struct file_operations acpi_ac_fops = { |
cf7acfab | 78 | .owner = THIS_MODULE, |
4be44fcd LB |
79 | .open = acpi_ac_open_fs, |
80 | .read = seq_read, | |
81 | .llseek = seq_lseek, | |
82 | .release = single_release, | |
1da177e4 | 83 | }; |
8a246ee4 | 84 | #endif |
d5b4a3d0 | 85 | |
1da177e4 LT |
86 | /* -------------------------------------------------------------------------- |
87 | AC Adapter Management | |
88 | -------------------------------------------------------------------------- */ | |
89 | ||
4be44fcd | 90 | static int acpi_ac_get_state(struct acpi_ac *ac) |
1da177e4 | 91 | { |
cc8ef527 | 92 | acpi_status status; |
1da177e4 | 93 | |
cc8ef527 ZR |
94 | status = acpi_evaluate_integer(ac->adev->handle, "_PSR", NULL, |
95 | &ac->state); | |
1da177e4 | 96 | if (ACPI_FAILURE(status)) { |
cc8ef527 ZR |
97 | ACPI_EXCEPTION((AE_INFO, status, |
98 | "Error reading AC Adapter state")); | |
1da177e4 | 99 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
d550d98d | 100 | return -ENODEV; |
1da177e4 | 101 | } |
4be44fcd | 102 | |
d550d98d | 103 | return 0; |
1da177e4 LT |
104 | } |
105 | ||
3151dbb0 ZR |
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 | ||
fdcedbba | 135 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
136 | /* -------------------------------------------------------------------------- |
137 | FS Interface (/proc) | |
138 | -------------------------------------------------------------------------- */ | |
139 | ||
4be44fcd | 140 | static struct proc_dir_entry *acpi_ac_dir; |
1da177e4 LT |
141 | |
142 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) | |
143 | { | |
50dd0969 | 144 | struct acpi_ac *ac = seq->private; |
1da177e4 | 145 | |
1da177e4 LT |
146 | |
147 | if (!ac) | |
d550d98d | 148 | return 0; |
1da177e4 LT |
149 | |
150 | if (acpi_ac_get_state(ac)) { | |
151 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); | |
d550d98d | 152 | return 0; |
1da177e4 LT |
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 | ||
d550d98d | 168 | return 0; |
1da177e4 | 169 | } |
4be44fcd | 170 | |
1da177e4 LT |
171 | static int acpi_ac_open_fs(struct inode *inode, struct file *file) |
172 | { | |
d9dda78b | 173 | return single_open(file, acpi_ac_seq_show, PDE_DATA(inode)); |
1da177e4 LT |
174 | } |
175 | ||
cc8ef527 | 176 | static int acpi_ac_add_fs(struct acpi_ac *ac) |
1da177e4 | 177 | { |
4be44fcd | 178 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 179 | |
6d855fcd ZR |
180 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded," |
181 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); | |
cc8ef527 ZR |
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)) | |
d550d98d | 186 | return -ENODEV; |
1da177e4 LT |
187 | } |
188 | ||
189 | /* 'state' [R] */ | |
cf7acfab | 190 | entry = proc_create_data(ACPI_AC_FILE_STATE, |
cc8ef527 ZR |
191 | S_IRUGO, acpi_device_dir(ac->adev), |
192 | &acpi_ac_fops, ac); | |
1da177e4 | 193 | if (!entry) |
d550d98d | 194 | return -ENODEV; |
d550d98d | 195 | return 0; |
1da177e4 LT |
196 | } |
197 | ||
cc8ef527 | 198 | static int acpi_ac_remove_fs(struct acpi_ac *ac) |
1da177e4 | 199 | { |
1da177e4 | 200 | |
cc8ef527 ZR |
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; | |
1da177e4 LT |
206 | } |
207 | ||
d550d98d | 208 | return 0; |
1da177e4 | 209 | } |
8a246ee4 | 210 | #endif |
1da177e4 | 211 | |
1da177e4 LT |
212 | /* -------------------------------------------------------------------------- |
213 | Driver Model | |
214 | -------------------------------------------------------------------------- */ | |
215 | ||
cc8ef527 | 216 | static void acpi_ac_notify_handler(acpi_handle handle, u32 event, void *data) |
1da177e4 | 217 | { |
cc8ef527 | 218 | struct acpi_ac *ac = data; |
1da177e4 LT |
219 | |
220 | if (!ac) | |
d550d98d | 221 | return; |
1da177e4 | 222 | |
1da177e4 | 223 | switch (event) { |
f163ff51 LB |
224 | default: |
225 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
226 | "Unsupported event [0x%x]\n", event)); | |
1da177e4 | 227 | case ACPI_AC_NOTIFY_STATUS: |
03d78252 CL |
228 | case ACPI_NOTIFY_BUS_CHECK: |
229 | case ACPI_NOTIFY_DEVICE_CHECK: | |
0ab5bb64 LT |
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 | ||
1da177e4 | 240 | acpi_ac_get_state(ac); |
cc8ef527 ZR |
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); | |
d5b4a3d0 | 245 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
1da177e4 LT |
246 | } |
247 | ||
d550d98d | 248 | return; |
1da177e4 LT |
249 | } |
250 | ||
0ab5bb64 LT |
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 | ||
cc8ef527 | 269 | static int acpi_ac_probe(struct platform_device *pdev) |
1da177e4 | 270 | { |
4be44fcd | 271 | int result = 0; |
4be44fcd | 272 | struct acpi_ac *ac = NULL; |
cc8ef527 | 273 | struct acpi_device *adev; |
1da177e4 | 274 | |
cc8ef527 | 275 | if (!pdev) |
d550d98d | 276 | return -EINVAL; |
1da177e4 | 277 | |
cc8ef527 ZR |
278 | result = acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev); |
279 | if (result) | |
280 | return -ENODEV; | |
281 | ||
36bcbec7 | 282 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
1da177e4 | 283 | if (!ac) |
d550d98d | 284 | return -ENOMEM; |
1da177e4 | 285 | |
cc8ef527 ZR |
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); | |
1da177e4 LT |
291 | |
292 | result = acpi_ac_get_state(ac); | |
293 | if (result) | |
294 | goto end; | |
295 | ||
fdcedbba | 296 | #ifdef CONFIG_ACPI_PROCFS_POWER |
cc8ef527 | 297 | result = acpi_ac_add_fs(ac); |
1da177e4 LT |
298 | if (result) |
299 | goto end; | |
cc8ef527 ZR |
300 | #endif |
301 | ac->charger.name = acpi_device_bid(adev); | |
d5b4a3d0 AS |
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; | |
cc8ef527 | 306 | result = power_supply_register(&pdev->dev, &ac->charger); |
f197ac13 LT |
307 | if (result) |
308 | goto end; | |
1da177e4 | 309 | |
cc8ef527 ZR |
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 | } | |
4be44fcd | 316 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
cc8ef527 | 317 | acpi_device_name(adev), acpi_device_bid(adev), |
4be44fcd | 318 | ac->state ? "on-line" : "off-line"); |
1da177e4 | 319 | |
4be44fcd | 320 | end: |
1da177e4 | 321 | if (result) { |
fdcedbba | 322 | #ifdef CONFIG_ACPI_PROCFS_POWER |
cc8ef527 | 323 | acpi_ac_remove_fs(ac); |
8a246ee4 | 324 | #endif |
1da177e4 LT |
325 | kfree(ac); |
326 | } | |
327 | ||
0ab5bb64 | 328 | dmi_check_system(ac_dmi_table); |
d550d98d | 329 | return result; |
1da177e4 LT |
330 | } |
331 | ||
90692404 | 332 | #ifdef CONFIG_PM_SLEEP |
ccda7069 | 333 | static int acpi_ac_resume(struct device *dev) |
5bfeca31 AS |
334 | { |
335 | struct acpi_ac *ac; | |
336 | unsigned old_state; | |
ccda7069 RW |
337 | |
338 | if (!dev) | |
339 | return -EINVAL; | |
340 | ||
cc8ef527 | 341 | ac = platform_get_drvdata(to_platform_device(dev)); |
ccda7069 | 342 | if (!ac) |
5bfeca31 | 343 | return -EINVAL; |
ccda7069 | 344 | |
5bfeca31 AS |
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 | } | |
90692404 | 352 | #endif |
cc8ef527 | 353 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops, NULL, acpi_ac_resume); |
5bfeca31 | 354 | |
cc8ef527 | 355 | static int acpi_ac_remove(struct platform_device *pdev) |
1da177e4 | 356 | { |
cc8ef527 | 357 | struct acpi_ac *ac; |
1da177e4 | 358 | |
cc8ef527 | 359 | if (!pdev) |
d550d98d | 360 | return -EINVAL; |
1da177e4 | 361 | |
cc8ef527 ZR |
362 | acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), |
363 | ACPI_DEVICE_NOTIFY, acpi_ac_notify_handler); | |
1da177e4 | 364 | |
cc8ef527 | 365 | ac = platform_get_drvdata(pdev); |
d5b4a3d0 AS |
366 | if (ac->charger.dev) |
367 | power_supply_unregister(&ac->charger); | |
cc8ef527 | 368 | |
fdcedbba | 369 | #ifdef CONFIG_ACPI_PROCFS_POWER |
cc8ef527 | 370 | acpi_ac_remove_fs(ac); |
8a246ee4 | 371 | #endif |
1da177e4 LT |
372 | |
373 | kfree(ac); | |
374 | ||
d550d98d | 375 | return 0; |
1da177e4 LT |
376 | } |
377 | ||
cc8ef527 ZR |
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 | ||
4be44fcd | 395 | static int __init acpi_ac_init(void) |
1da177e4 | 396 | { |
3f86b832 | 397 | int result; |
1da177e4 | 398 | |
4d8316d5 PM |
399 | if (acpi_disabled) |
400 | return -ENODEV; | |
1da177e4 | 401 | |
fdcedbba | 402 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 403 | acpi_ac_dir = acpi_lock_ac_dir(); |
1da177e4 | 404 | if (!acpi_ac_dir) |
d550d98d | 405 | return -ENODEV; |
8a246ee4 | 406 | #endif |
1da177e4 | 407 | |
cc8ef527 | 408 | result = platform_driver_register(&acpi_ac_driver); |
1da177e4 | 409 | if (result < 0) { |
fdcedbba | 410 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 411 | acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee4 | 412 | #endif |
d550d98d | 413 | return -ENODEV; |
1da177e4 LT |
414 | } |
415 | ||
d550d98d | 416 | return 0; |
1da177e4 LT |
417 | } |
418 | ||
4be44fcd | 419 | static void __exit acpi_ac_exit(void) |
1da177e4 | 420 | { |
cc8ef527 | 421 | platform_driver_unregister(&acpi_ac_driver); |
fdcedbba | 422 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 423 | acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee4 | 424 | #endif |
1da177e4 | 425 | } |
1da177e4 LT |
426 | module_init(acpi_ac_init); |
427 | module_exit(acpi_ac_exit); |