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 <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.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");
59 #ifdef CONFIG_ACPI_PROCFS_POWER
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
);
62 static int acpi_ac_open_fs(struct inode
*inode
, struct file
*file
);
65 static int ac_sleep_before_get_state_ms
;
68 struct power_supply charger
;
69 struct acpi_device
*adev
;
70 struct platform_device
*pdev
;
71 unsigned long long state
;
74 #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
76 #ifdef CONFIG_ACPI_PROCFS_POWER
77 static const struct file_operations acpi_ac_fops
= {
79 .open
= acpi_ac_open_fs
,
82 .release
= single_release
,
86 /* --------------------------------------------------------------------------
88 -------------------------------------------------------------------------- */
90 static int acpi_ac_get_state(struct acpi_ac
*ac
)
94 status
= acpi_evaluate_integer(ac
->adev
->handle
, "_PSR", NULL
,
96 if (ACPI_FAILURE(status
)) {
97 ACPI_EXCEPTION((AE_INFO
, status
,
98 "Error reading AC Adapter state"));
99 ac
->state
= ACPI_AC_STATUS_UNKNOWN
;
106 /* --------------------------------------------------------------------------
108 -------------------------------------------------------------------------- */
109 static int get_ac_property(struct power_supply
*psy
,
110 enum power_supply_property psp
,
111 union power_supply_propval
*val
)
113 struct acpi_ac
*ac
= to_acpi_ac(psy
);
118 if (acpi_ac_get_state(ac
))
122 case POWER_SUPPLY_PROP_ONLINE
:
123 val
->intval
= ac
->state
;
131 static enum power_supply_property ac_props
[] = {
132 POWER_SUPPLY_PROP_ONLINE
,
135 #ifdef CONFIG_ACPI_PROCFS_POWER
136 /* --------------------------------------------------------------------------
138 -------------------------------------------------------------------------- */
140 static struct proc_dir_entry
*acpi_ac_dir
;
142 static int acpi_ac_seq_show(struct seq_file
*seq
, void *offset
)
144 struct acpi_ac
*ac
= seq
->private;
150 if (acpi_ac_get_state(ac
)) {
151 seq_puts(seq
, "ERROR: Unable to read AC Adapter state\n");
155 seq_puts(seq
, "state: ");
157 case ACPI_AC_STATUS_OFFLINE
:
158 seq_puts(seq
, "off-line\n");
160 case ACPI_AC_STATUS_ONLINE
:
161 seq_puts(seq
, "on-line\n");
164 seq_puts(seq
, "unknown\n");
171 static int acpi_ac_open_fs(struct inode
*inode
, struct file
*file
)
173 return single_open(file
, acpi_ac_seq_show
, PDE_DATA(inode
));
176 static int acpi_ac_add_fs(struct acpi_ac
*ac
)
178 struct proc_dir_entry
*entry
= NULL
;
180 printk(KERN_WARNING PREFIX
"Deprecated procfs I/F for AC is loaded,"
181 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
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
))
190 entry
= proc_create_data(ACPI_AC_FILE_STATE
,
191 S_IRUGO
, acpi_device_dir(ac
->adev
),
198 static int acpi_ac_remove_fs(struct acpi_ac
*ac
)
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
;
212 /* --------------------------------------------------------------------------
214 -------------------------------------------------------------------------- */
216 static void acpi_ac_notify_handler(acpi_handle handle
, u32 event
, void *data
)
218 struct acpi_ac
*ac
= data
;
225 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
226 "Unsupported event [0x%x]\n", event
));
227 case ACPI_AC_NOTIFY_STATUS
:
228 case ACPI_NOTIFY_BUS_CHECK
:
229 case ACPI_NOTIFY_DEVICE_CHECK
:
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.
237 if (ac_sleep_before_get_state_ms
> 0)
238 msleep(ac_sleep_before_get_state_ms
);
240 acpi_ac_get_state(ac
);
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
);
245 kobject_uevent(&ac
->charger
.dev
->kobj
, KOBJ_CHANGE
);
251 static int thinkpad_e530_quirk(const struct dmi_system_id
*d
)
253 ac_sleep_before_get_state_ms
= 1000;
257 static struct dmi_system_id ac_dmi_table
[] = {
259 .callback
= thinkpad_e530_quirk
,
260 .ident
= "thinkpad e530",
262 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
263 DMI_MATCH(DMI_PRODUCT_NAME
, "32597CG"),
269 static int acpi_ac_probe(struct platform_device
*pdev
)
272 struct acpi_ac
*ac
= NULL
;
273 struct acpi_device
*adev
;
278 result
= acpi_bus_get_device(ACPI_HANDLE(&pdev
->dev
), &adev
);
282 ac
= kzalloc(sizeof(struct acpi_ac
), GFP_KERNEL
);
286 strcpy(acpi_device_name(adev
), ACPI_AC_DEVICE_NAME
);
287 strcpy(acpi_device_class(adev
), ACPI_AC_CLASS
);
290 platform_set_drvdata(pdev
, ac
);
292 result
= acpi_ac_get_state(ac
);
296 #ifdef CONFIG_ACPI_PROCFS_POWER
297 result
= acpi_ac_add_fs(ac
);
301 ac
->charger
.name
= acpi_device_bid(adev
);
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
;
306 result
= power_supply_register(&pdev
->dev
, &ac
->charger
);
310 result
= acpi_install_notify_handler(ACPI_HANDLE(&pdev
->dev
),
311 ACPI_DEVICE_NOTIFY
, acpi_ac_notify_handler
, ac
);
313 power_supply_unregister(&ac
->charger
);
316 printk(KERN_INFO PREFIX
"%s [%s] (%s)\n",
317 acpi_device_name(adev
), acpi_device_bid(adev
),
318 ac
->state
? "on-line" : "off-line");
322 #ifdef CONFIG_ACPI_PROCFS_POWER
323 acpi_ac_remove_fs(ac
);
328 dmi_check_system(ac_dmi_table
);
332 #ifdef CONFIG_PM_SLEEP
333 static int acpi_ac_resume(struct device
*dev
)
341 ac
= platform_get_drvdata(to_platform_device(dev
));
345 old_state
= ac
->state
;
346 if (acpi_ac_get_state(ac
))
348 if (old_state
!= ac
->state
)
349 kobject_uevent(&ac
->charger
.dev
->kobj
, KOBJ_CHANGE
);
353 static SIMPLE_DEV_PM_OPS(acpi_ac_pm_ops
, NULL
, acpi_ac_resume
);
355 static int acpi_ac_remove(struct platform_device
*pdev
)
362 acpi_remove_notify_handler(ACPI_HANDLE(&pdev
->dev
),
363 ACPI_DEVICE_NOTIFY
, acpi_ac_notify_handler
);
365 ac
= platform_get_drvdata(pdev
);
367 power_supply_unregister(&ac
->charger
);
369 #ifdef CONFIG_ACPI_PROCFS_POWER
370 acpi_ac_remove_fs(ac
);
378 static const struct acpi_device_id acpi_ac_match
[] = {
382 MODULE_DEVICE_TABLE(acpi
, acpi_ac_match
);
384 static struct platform_driver acpi_ac_driver
= {
385 .probe
= acpi_ac_probe
,
386 .remove
= acpi_ac_remove
,
389 .owner
= THIS_MODULE
,
390 .pm
= &acpi_ac_pm_ops
,
391 .acpi_match_table
= ACPI_PTR(acpi_ac_match
),
395 static int __init
acpi_ac_init(void)
402 #ifdef CONFIG_ACPI_PROCFS_POWER
403 acpi_ac_dir
= acpi_lock_ac_dir();
408 result
= platform_driver_register(&acpi_ac_driver
);
410 #ifdef CONFIG_ACPI_PROCFS_POWER
411 acpi_unlock_ac_dir(acpi_ac_dir
);
419 static void __exit
acpi_ac_exit(void)
421 platform_driver_unregister(&acpi_ac_driver
);
422 #ifdef CONFIG_ACPI_PROCFS_POWER
423 acpi_unlock_ac_dir(acpi_ac_dir
);
426 module_init(acpi_ac_init
);
427 module_exit(acpi_ac_exit
);