diff options
Diffstat (limited to 'drivers/acpi/ac.c')
| -rw-r--r-- | drivers/acpi/ac.c | 212 | 
1 files changed, 148 insertions, 64 deletions
diff --git a/drivers/acpi/ac.c b/drivers/acpi/ac.c index ba9afeaa23a..36b0e61f9c0 100644 --- a/drivers/acpi/ac.c +++ b/drivers/acpi/ac.c @@ -28,13 +28,16 @@  #include <linux/slab.h>  #include <linux/init.h>  #include <linux/types.h> +#include <linux/dmi.h> +#include <linux/delay.h>  #ifdef CONFIG_ACPI_PROCFS_POWER  #include <linux/proc_fs.h>  #include <linux/seq_file.h>  #endif +#include <linux/platform_device.h>  #include <linux/power_supply.h> -#include <acpi/acpi_bus.h> -#include <acpi/acpi_drivers.h> +#include <linux/acpi.h> +#include "battery.h"  #define PREFIX "ACPI: " @@ -53,15 +56,9 @@ MODULE_AUTHOR("Paul Diefenbaugh");  MODULE_DESCRIPTION("ACPI AC Adapter Driver");  MODULE_LICENSE("GPL"); -#ifdef CONFIG_ACPI_PROCFS_POWER -extern struct proc_dir_entry *acpi_lock_ac_dir(void); -extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); -static int acpi_ac_open_fs(struct inode *inode, struct file *file); -#endif  static int acpi_ac_add(struct acpi_device *device); -static int acpi_ac_remove(struct acpi_device *device, int type); -static int acpi_ac_resume(struct acpi_device *device); +static int acpi_ac_remove(struct acpi_device *device);  static void acpi_ac_notify(struct acpi_device *device, u32 event);  static const struct acpi_device_id ac_device_ids[] = { @@ -70,6 +67,20 @@ static const struct acpi_device_id ac_device_ids[] = {  };  MODULE_DEVICE_TABLE(acpi, ac_device_ids); +#ifdef CONFIG_PM_SLEEP +static int acpi_ac_resume(struct device *dev); +#endif +static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume); + +#ifdef CONFIG_ACPI_PROCFS_POWER +extern struct proc_dir_entry *acpi_lock_ac_dir(void); +extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); +static int acpi_ac_open_fs(struct inode *inode, struct file *file); +#endif + + +static int ac_sleep_before_get_state_ms; +  static struct acpi_driver acpi_ac_driver = {  	.name = "ac",  	.class = ACPI_AC_CLASS, @@ -78,18 +89,19 @@ static struct acpi_driver acpi_ac_driver = {  	.ops = {  		.add = acpi_ac_add,  		.remove = acpi_ac_remove, -		.resume = acpi_ac_resume,  		.notify = acpi_ac_notify,  		}, +	.drv.pm = &acpi_ac_pm,  };  struct acpi_ac {  	struct power_supply charger;  	struct acpi_device * device;  	unsigned long long state; +	struct notifier_block battery_nb;  }; -#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger); +#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)  #ifdef CONFIG_ACPI_PROCFS_POWER  static const struct file_operations acpi_ac_fops = { @@ -100,24 +112,7 @@ static const struct file_operations acpi_ac_fops = {  	.release = single_release,  };  #endif -static int get_ac_property(struct power_supply *psy, -			   enum power_supply_property psp, -			   union power_supply_propval *val) -{ -	struct acpi_ac *ac = to_acpi_ac(psy); -	switch (psp) { -	case POWER_SUPPLY_PROP_ONLINE: -		val->intval = ac->state; -		break; -	default: -		return -EINVAL; -	} -	return 0; -} -static enum power_supply_property ac_props[] = { -	POWER_SUPPLY_PROP_ONLINE, -};  /* --------------------------------------------------------------------------                                 AC Adapter Management     -------------------------------------------------------------------------- */ @@ -126,13 +121,14 @@ static int acpi_ac_get_state(struct acpi_ac *ac)  {  	acpi_status status = AE_OK; -  	if (!ac)  		return -EINVAL; -	status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state); +	status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, +				       &ac->state);  	if (ACPI_FAILURE(status)) { -		ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state")); +		ACPI_EXCEPTION((AE_INFO, status, +				"Error reading AC Adapter state"));  		ac->state = ACPI_AC_STATUS_UNKNOWN;  		return -ENODEV;  	} @@ -140,6 +136,35 @@ static int acpi_ac_get_state(struct acpi_ac *ac)  	return 0;  } +/* -------------------------------------------------------------------------- +                            sysfs I/F +   -------------------------------------------------------------------------- */ +static int get_ac_property(struct power_supply *psy, +			   enum power_supply_property psp, +			   union power_supply_propval *val) +{ +	struct acpi_ac *ac = to_acpi_ac(psy); + +	if (!ac) +		return -ENODEV; + +	if (acpi_ac_get_state(ac)) +		return -ENODEV; + +	switch (psp) { +	case POWER_SUPPLY_PROP_ONLINE: +		val->intval = ac->state; +		break; +	default: +		return -EINVAL; +	} +	return 0; +} + +static enum power_supply_property ac_props[] = { +	POWER_SUPPLY_PROP_ONLINE, +}; +  #ifdef CONFIG_ACPI_PROCFS_POWER  /* --------------------------------------------------------------------------                                FS Interface (/proc) @@ -178,38 +203,39 @@ static int acpi_ac_seq_show(struct seq_file *seq, void *offset)  static int acpi_ac_open_fs(struct inode *inode, struct file *file)  { -	return single_open(file, acpi_ac_seq_show, PDE(inode)->data); +	return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));  } -static int acpi_ac_add_fs(struct acpi_device *device) +static int acpi_ac_add_fs(struct acpi_ac *ac)  {  	struct proc_dir_entry *entry = NULL; - -	if (!acpi_device_dir(device)) { -		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), -						     acpi_ac_dir); -		if (!acpi_device_dir(device)) +	printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded," +			" please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); +	if (!acpi_device_dir(ac->device)) { +		acpi_device_dir(ac->device) = +			proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir); +		if (!acpi_device_dir(ac->device))  			return -ENODEV;  	}  	/* 'state' [R] */  	entry = proc_create_data(ACPI_AC_FILE_STATE, -				 S_IRUGO, acpi_device_dir(device), -				 &acpi_ac_fops, acpi_driver_data(device)); +				 S_IRUGO, acpi_device_dir(ac->device), +				 &acpi_ac_fops, ac);  	if (!entry)  		return -ENODEV;  	return 0;  } -static int acpi_ac_remove_fs(struct acpi_device *device) +static int acpi_ac_remove_fs(struct acpi_ac *ac)  { -	if (acpi_device_dir(device)) { -		remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device)); - -		remove_proc_entry(acpi_device_bid(device), acpi_ac_dir); -		acpi_device_dir(device) = NULL; +	if (acpi_device_dir(ac->device)) { +		remove_proc_entry(ACPI_AC_FILE_STATE, +				  acpi_device_dir(ac->device)); +		remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir); +		acpi_device_dir(ac->device) = NULL;  	}  	return 0; @@ -224,7 +250,6 @@ static void acpi_ac_notify(struct acpi_device *device, u32 event)  {  	struct acpi_ac *ac = acpi_driver_data(device); -  	if (!ac)  		return; @@ -235,8 +260,17 @@ static void acpi_ac_notify(struct acpi_device *device, u32 event)  	case ACPI_AC_NOTIFY_STATUS:  	case ACPI_NOTIFY_BUS_CHECK:  	case ACPI_NOTIFY_DEVICE_CHECK: +		/* +		 * A buggy BIOS may notify AC first and then sleep for +		 * a specific time before doing actual operations in the +		 * EC event handler (_Qxx). This will cause the AC state +		 * reported by the ACPI event to be incorrect, so wait for a +		 * specific time for the EC event handler to make progress. +		 */ +		if (ac_sleep_before_get_state_ms > 0) +			msleep(ac_sleep_before_get_state_ms); +  		acpi_ac_get_state(ac); -		acpi_bus_generate_proc_event(device, event, (u32) ac->state);  		acpi_bus_generate_netlink_event(device->pnp.device_class,  						  dev_name(&device->dev), event,  						  (u32) ac->state); @@ -247,6 +281,44 @@ static void acpi_ac_notify(struct acpi_device *device, u32 event)  	return;  } +static int acpi_ac_battery_notify(struct notifier_block *nb, +				  unsigned long action, void *data) +{ +	struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb); +	struct acpi_bus_event *event = (struct acpi_bus_event *)data; + +	/* +	 * On HP Pavilion dv6-6179er AC status notifications aren't triggered +	 * when adapter is plugged/unplugged. However, battery status +	 * notifcations are triggered when battery starts charging or +	 * discharging. Re-reading AC status triggers lost AC notifications, +	 * if AC status has changed. +	 */ +	if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 && +	    event->type == ACPI_BATTERY_NOTIFY_STATUS) +		acpi_ac_get_state(ac); + +	return NOTIFY_OK; +} + +static int thinkpad_e530_quirk(const struct dmi_system_id *d) +{ +	ac_sleep_before_get_state_ms = 1000; +	return 0; +} + +static struct dmi_system_id ac_dmi_table[] = { +	{ +	.callback = thinkpad_e530_quirk, +	.ident = "thinkpad e530", +	.matches = { +		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), +		DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"), +		}, +	}, +	{}, +}; +  static int acpi_ac_add(struct acpi_device *device)  {  	int result = 0; @@ -269,40 +341,51 @@ static int acpi_ac_add(struct acpi_device *device)  	if (result)  		goto end; +	ac->charger.name = acpi_device_bid(device);  #ifdef CONFIG_ACPI_PROCFS_POWER -	result = acpi_ac_add_fs(device); -#endif +	result = acpi_ac_add_fs(ac);  	if (result)  		goto end; -	ac->charger.name = acpi_device_bid(device); +#endif  	ac->charger.type = POWER_SUPPLY_TYPE_MAINS;  	ac->charger.properties = ac_props;  	ac->charger.num_properties = ARRAY_SIZE(ac_props);  	ac->charger.get_property = get_ac_property; -	power_supply_register(&ac->device->dev, &ac->charger); +	result = power_supply_register(&ac->device->dev, &ac->charger); +	if (result) +		goto end;  	printk(KERN_INFO PREFIX "%s [%s] (%s)\n",  	       acpi_device_name(device), acpi_device_bid(device),  	       ac->state ? "on-line" : "off-line"); -      end: +	ac->battery_nb.notifier_call = acpi_ac_battery_notify; +	register_acpi_notifier(&ac->battery_nb); +end:  	if (result) {  #ifdef CONFIG_ACPI_PROCFS_POWER -		acpi_ac_remove_fs(device); +		acpi_ac_remove_fs(ac);  #endif  		kfree(ac);  	} +	dmi_check_system(ac_dmi_table);  	return result;  } -static int acpi_ac_resume(struct acpi_device *device) +#ifdef CONFIG_PM_SLEEP +static int acpi_ac_resume(struct device *dev)  {  	struct acpi_ac *ac;  	unsigned old_state; -	if (!device || !acpi_driver_data(device)) + +	if (!dev)  		return -EINVAL; -	ac = acpi_driver_data(device); + +	ac = acpi_driver_data(to_acpi_device(dev)); +	if (!ac) +		return -EINVAL; +  	old_state = ac->state;  	if (acpi_ac_get_state(ac))  		return 0; @@ -310,8 +393,11 @@ static int acpi_ac_resume(struct acpi_device *device)  		kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);  	return 0;  } +#else +#define acpi_ac_resume NULL +#endif -static int acpi_ac_remove(struct acpi_device *device, int type) +static int acpi_ac_remove(struct acpi_device *device)  {  	struct acpi_ac *ac = NULL; @@ -323,8 +409,10 @@ static int acpi_ac_remove(struct acpi_device *device, int type)  	if (ac->charger.dev)  		power_supply_unregister(&ac->charger); +	unregister_acpi_notifier(&ac->battery_nb); +  #ifdef CONFIG_ACPI_PROCFS_POWER -	acpi_ac_remove_fs(device); +	acpi_ac_remove_fs(ac);  #endif  	kfree(ac); @@ -345,6 +433,7 @@ static int __init acpi_ac_init(void)  		return -ENODEV;  #endif +  	result = acpi_bus_register_driver(&acpi_ac_driver);  	if (result < 0) {  #ifdef CONFIG_ACPI_PROCFS_POWER @@ -358,15 +447,10 @@ static int __init acpi_ac_init(void)  static void __exit acpi_ac_exit(void)  { -  	acpi_bus_unregister_driver(&acpi_ac_driver); -  #ifdef CONFIG_ACPI_PROCFS_POWER  	acpi_unlock_ac_dir(acpi_ac_dir);  #endif - -	return;  } -  module_init(acpi_ac_init);  module_exit(acpi_ac_exit);  | 
