2 * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/dmi.h>
32 #include <linux/delay.h>
33 #ifdef CONFIG_ACPI_PROCFS_POWER
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
37 #include <linux/platform_device.h>
38 #include <linux/power_supply.h>
39 #include <linux/acpi.h>
42 #define PREFIX "ACPI: "
44 #define ACPI_AC_CLASS "ac_adapter"
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
52 #define _COMPONENT ACPI_AC_COMPONENT
53 ACPI_MODULE_NAME("ac");
55 MODULE_AUTHOR("Paul Diefenbaugh");
56 MODULE_DESCRIPTION("ACPI AC Adapter Driver");
57 MODULE_LICENSE("GPL");
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
);
64 static const struct acpi_device_id ac_device_ids
[] = {
68 MODULE_DEVICE_TABLE(acpi
, ac_device_ids
);
70 #ifdef CONFIG_PM_SLEEP
71 static int acpi_ac_resume(struct device
*dev
);
73 static SIMPLE_DEV_PM_OPS(acpi_ac_pm
, NULL
, acpi_ac_resume
);
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
);
82 static int ac_sleep_before_get_state_ms
;
84 static struct acpi_driver acpi_ac_driver
= {
86 .class = ACPI_AC_CLASS
,
88 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
91 .remove
= acpi_ac_remove
,
92 .notify
= acpi_ac_notify
,
94 .drv
.pm
= &acpi_ac_pm
,
98 struct power_supply
*charger
;
99 struct power_supply_desc charger_desc
;
100 struct acpi_device
* device
;
101 unsigned long long state
;
102 struct notifier_block battery_nb
;
105 #define to_acpi_ac(x) power_supply_get_drvdata(x)
107 #ifdef CONFIG_ACPI_PROCFS_POWER
108 static const struct file_operations acpi_ac_fops
= {
109 .owner
= THIS_MODULE
,
110 .open
= acpi_ac_open_fs
,
113 .release
= single_release
,
117 /* --------------------------------------------------------------------------
118 AC Adapter Management
119 -------------------------------------------------------------------------- */
121 static int acpi_ac_get_state(struct acpi_ac
*ac
)
123 acpi_status status
= AE_OK
;
128 status
= acpi_evaluate_integer(ac
->device
->handle
, "_PSR", NULL
,
130 if (ACPI_FAILURE(status
)) {
131 ACPI_EXCEPTION((AE_INFO
, status
,
132 "Error reading AC Adapter state"));
133 ac
->state
= ACPI_AC_STATUS_UNKNOWN
;
140 /* --------------------------------------------------------------------------
142 -------------------------------------------------------------------------- */
143 static int get_ac_property(struct power_supply
*psy
,
144 enum power_supply_property psp
,
145 union power_supply_propval
*val
)
147 struct acpi_ac
*ac
= to_acpi_ac(psy
);
152 if (acpi_ac_get_state(ac
))
156 case POWER_SUPPLY_PROP_ONLINE
:
157 val
->intval
= ac
->state
;
165 static enum power_supply_property ac_props
[] = {
166 POWER_SUPPLY_PROP_ONLINE
,
169 #ifdef CONFIG_ACPI_PROCFS_POWER
170 /* --------------------------------------------------------------------------
172 -------------------------------------------------------------------------- */
174 static struct proc_dir_entry
*acpi_ac_dir
;
176 static int acpi_ac_seq_show(struct seq_file
*seq
, void *offset
)
178 struct acpi_ac
*ac
= seq
->private;
184 if (acpi_ac_get_state(ac
)) {
185 seq_puts(seq
, "ERROR: Unable to read AC Adapter state\n");
189 seq_puts(seq
, "state: ");
191 case ACPI_AC_STATUS_OFFLINE
:
192 seq_puts(seq
, "off-line\n");
194 case ACPI_AC_STATUS_ONLINE
:
195 seq_puts(seq
, "on-line\n");
198 seq_puts(seq
, "unknown\n");
205 static int acpi_ac_open_fs(struct inode
*inode
, struct file
*file
)
207 return single_open(file
, acpi_ac_seq_show
, PDE_DATA(inode
));
210 static int acpi_ac_add_fs(struct acpi_ac
*ac
)
212 struct proc_dir_entry
*entry
= NULL
;
214 printk(KERN_WARNING PREFIX
"Deprecated procfs I/F for AC is loaded,"
215 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
216 if (!acpi_device_dir(ac
->device
)) {
217 acpi_device_dir(ac
->device
) =
218 proc_mkdir(acpi_device_bid(ac
->device
), acpi_ac_dir
);
219 if (!acpi_device_dir(ac
->device
))
224 entry
= proc_create_data(ACPI_AC_FILE_STATE
,
225 S_IRUGO
, acpi_device_dir(ac
->device
),
232 static int acpi_ac_remove_fs(struct acpi_ac
*ac
)
235 if (acpi_device_dir(ac
->device
)) {
236 remove_proc_entry(ACPI_AC_FILE_STATE
,
237 acpi_device_dir(ac
->device
));
238 remove_proc_entry(acpi_device_bid(ac
->device
), acpi_ac_dir
);
239 acpi_device_dir(ac
->device
) = NULL
;
246 /* --------------------------------------------------------------------------
248 -------------------------------------------------------------------------- */
250 static void acpi_ac_notify(struct acpi_device
*device
, u32 event
)
252 struct acpi_ac
*ac
= acpi_driver_data(device
);
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
260 "Unsupported event [0x%x]\n", event
));
261 case ACPI_AC_NOTIFY_STATUS
:
262 case ACPI_NOTIFY_BUS_CHECK
:
263 case ACPI_NOTIFY_DEVICE_CHECK
:
265 * A buggy BIOS may notify AC first and then sleep for
266 * a specific time before doing actual operations in the
267 * EC event handler (_Qxx). This will cause the AC state
268 * reported by the ACPI event to be incorrect, so wait for a
269 * specific time for the EC event handler to make progress.
271 if (ac_sleep_before_get_state_ms
> 0)
272 msleep(ac_sleep_before_get_state_ms
);
274 acpi_ac_get_state(ac
);
275 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
276 dev_name(&device
->dev
), event
,
278 acpi_notifier_call_chain(device
, event
, (u32
) ac
->state
);
279 kobject_uevent(&ac
->charger
->dev
.kobj
, KOBJ_CHANGE
);
285 static int acpi_ac_battery_notify(struct notifier_block
*nb
,
286 unsigned long action
, void *data
)
288 struct acpi_ac
*ac
= container_of(nb
, struct acpi_ac
, battery_nb
);
289 struct acpi_bus_event
*event
= (struct acpi_bus_event
*)data
;
292 * On HP Pavilion dv6-6179er AC status notifications aren't triggered
293 * when adapter is plugged/unplugged. However, battery status
294 * notifcations are triggered when battery starts charging or
295 * discharging. Re-reading AC status triggers lost AC notifications,
296 * if AC status has changed.
298 if (strcmp(event
->device_class
, ACPI_BATTERY_CLASS
) == 0 &&
299 event
->type
== ACPI_BATTERY_NOTIFY_STATUS
)
300 acpi_ac_get_state(ac
);
305 static int thinkpad_e530_quirk(const struct dmi_system_id
*d
)
307 ac_sleep_before_get_state_ms
= 1000;
311 static struct dmi_system_id ac_dmi_table
[] = {
313 .callback
= thinkpad_e530_quirk
,
314 .ident
= "thinkpad e530",
316 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
317 DMI_MATCH(DMI_PRODUCT_NAME
, "32597CG"),
323 static int acpi_ac_add(struct acpi_device
*device
)
325 struct power_supply_config psy_cfg
= {};
327 struct acpi_ac
*ac
= NULL
;
333 ac
= kzalloc(sizeof(struct acpi_ac
), GFP_KERNEL
);
338 strcpy(acpi_device_name(device
), ACPI_AC_DEVICE_NAME
);
339 strcpy(acpi_device_class(device
), ACPI_AC_CLASS
);
340 device
->driver_data
= ac
;
342 result
= acpi_ac_get_state(ac
);
346 psy_cfg
.drv_data
= ac
;
348 ac
->charger_desc
.name
= acpi_device_bid(device
);
349 #ifdef CONFIG_ACPI_PROCFS_POWER
350 result
= acpi_ac_add_fs(ac
);
354 ac
->charger_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
355 ac
->charger_desc
.properties
= ac_props
;
356 ac
->charger_desc
.num_properties
= ARRAY_SIZE(ac_props
);
357 ac
->charger_desc
.get_property
= get_ac_property
;
358 ac
->charger
= power_supply_register(&ac
->device
->dev
,
359 &ac
->charger_desc
, &psy_cfg
);
360 if (IS_ERR(ac
->charger
)) {
361 result
= PTR_ERR(ac
->charger
);
365 printk(KERN_INFO PREFIX
"%s [%s] (%s)\n",
366 acpi_device_name(device
), acpi_device_bid(device
),
367 ac
->state
? "on-line" : "off-line");
369 ac
->battery_nb
.notifier_call
= acpi_ac_battery_notify
;
370 register_acpi_notifier(&ac
->battery_nb
);
373 #ifdef CONFIG_ACPI_PROCFS_POWER
374 acpi_ac_remove_fs(ac
);
379 dmi_check_system(ac_dmi_table
);
383 #ifdef CONFIG_PM_SLEEP
384 static int acpi_ac_resume(struct device
*dev
)
392 ac
= acpi_driver_data(to_acpi_device(dev
));
396 old_state
= ac
->state
;
397 if (acpi_ac_get_state(ac
))
399 if (old_state
!= ac
->state
)
400 kobject_uevent(&ac
->charger
->dev
.kobj
, KOBJ_CHANGE
);
404 #define acpi_ac_resume NULL
407 static int acpi_ac_remove(struct acpi_device
*device
)
409 struct acpi_ac
*ac
= NULL
;
412 if (!device
|| !acpi_driver_data(device
))
415 ac
= acpi_driver_data(device
);
417 power_supply_unregister(ac
->charger
);
418 unregister_acpi_notifier(&ac
->battery_nb
);
420 #ifdef CONFIG_ACPI_PROCFS_POWER
421 acpi_ac_remove_fs(ac
);
429 static int __init
acpi_ac_init(void)
436 #ifdef CONFIG_ACPI_PROCFS_POWER
437 acpi_ac_dir
= acpi_lock_ac_dir();
443 result
= acpi_bus_register_driver(&acpi_ac_driver
);
445 #ifdef CONFIG_ACPI_PROCFS_POWER
446 acpi_unlock_ac_dir(acpi_ac_dir
);
454 static void __exit
acpi_ac_exit(void)
456 acpi_bus_unregister_driver(&acpi_ac_driver
);
457 #ifdef CONFIG_ACPI_PROCFS_POWER
458 acpi_unlock_ac_dir(acpi_ac_dir
);
461 module_init(acpi_ac_init
);
462 module_exit(acpi_ac_exit
);