ACPI / Battery: Add a _BIX quirk for NEC LZ750/LS
[linux-3.10.git] / drivers / acpi / battery.c
index ffce2f0..99427d7 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/dmi.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <asm/unaligned.h>
 
 #ifdef CONFIG_ACPI_PROCFS_POWER
 #include <linux/proc_fs.h>
@@ -67,6 +68,7 @@ MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
 MODULE_DESCRIPTION("ACPI Battery Driver");
 MODULE_LICENSE("GPL");
 
+static int battery_bix_broken_package;
 static unsigned int cache_time = 1000;
 module_param(cache_time, uint, 0644);
 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
@@ -95,14 +97,28 @@ enum {
        ACPI_BATTERY_ALARM_PRESENT,
        ACPI_BATTERY_XINFO_PRESENT,
        ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
+       /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
+          switches between mWh and mAh depending on whether the system
+          is running on battery or not.  When mAh is the unit, most
+          reported values are incorrect and need to be adjusted by
+          10000/design_voltage.  Verified on x201, t410, t410s, and x220.
+          Pre-2010 and 2012 models appear to always report in mWh and
+          are thus unaffected (tested with t42, t61, t500, x200, x300,
+          and x230).  Also, in mid-2012 Lenovo issued a BIOS update for
+          the 2011 models that fixes the issue (tested on x220 with a
+          post-1.29 BIOS), but as of Nov. 2012, no such update is
+          available for the 2010 models.  */
+       ACPI_BATTERY_QUIRK_THINKPAD_MAH,
 };
 
 struct acpi_battery {
        struct mutex lock;
+       struct mutex sysfs_lock;
        struct power_supply bat;
        struct acpi_device *device;
        struct notifier_block pm_nb;
        unsigned long update_time;
+       int revision;
        int rate_now;
        int capacity_now;
        int voltage_now;
@@ -130,9 +146,9 @@ struct acpi_battery {
        unsigned long flags;
 };
 
-#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
+#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
 
-inline int acpi_battery_present(struct acpi_battery *battery)
+static inline int acpi_battery_present(struct acpi_battery *battery)
 {
        return battery->device->status.battery_present;
 }
@@ -249,6 +265,13 @@ static int acpi_battery_get_property(struct power_supply *psy,
                else
                        val->intval = battery->capacity_now * 1000;
                break;
+       case POWER_SUPPLY_PROP_CAPACITY:
+               if (battery->capacity_now && battery->full_charge_capacity)
+                       val->intval = battery->capacity_now * 100/
+                                       battery->full_charge_capacity;
+               else
+                       val->intval = 0;
+               break;
        case POWER_SUPPLY_PROP_MODEL_NAME:
                val->strval = battery->model_number;
                break;
@@ -275,6 +298,7 @@ static enum power_supply_property charge_battery_props[] = {
        POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
        POWER_SUPPLY_PROP_CHARGE_FULL,
        POWER_SUPPLY_PROP_CHARGE_NOW,
+       POWER_SUPPLY_PROP_CAPACITY,
        POWER_SUPPLY_PROP_MODEL_NAME,
        POWER_SUPPLY_PROP_MANUFACTURER,
        POWER_SUPPLY_PROP_SERIAL_NUMBER,
@@ -291,6 +315,7 @@ static enum power_supply_property energy_battery_props[] = {
        POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
        POWER_SUPPLY_PROP_ENERGY_FULL,
        POWER_SUPPLY_PROP_ENERGY_NOW,
+       POWER_SUPPLY_PROP_CAPACITY,
        POWER_SUPPLY_PROP_MODEL_NAME,
        POWER_SUPPLY_PROP_MANUFACTURER,
        POWER_SUPPLY_PROP_SERIAL_NUMBER,
@@ -336,6 +361,7 @@ static struct acpi_offsets info_offsets[] = {
 };
 
 static struct acpi_offsets extended_info_offsets[] = {
+       {offsetof(struct acpi_battery, revision), 0},
        {offsetof(struct acpi_battery, power_unit), 0},
        {offsetof(struct acpi_battery, design_capacity), 0},
        {offsetof(struct acpi_battery, full_charge_capacity), 0},
@@ -418,7 +444,12 @@ static int acpi_battery_get_info(struct acpi_battery *battery)
                ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
                return -ENODEV;
        }
-       if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
+
+       if (battery_bix_broken_package)
+               result = extract_package(battery, buffer.pointer,
+                               extended_info_offsets + 1,
+                               ARRAY_SIZE(extended_info_offsets) - 1);
+       else if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
                result = extract_package(battery, buffer.pointer,
                                extended_info_offsets,
                                ARRAY_SIZE(extended_info_offsets));
@@ -428,6 +459,21 @@ static int acpi_battery_get_info(struct acpi_battery *battery)
        kfree(buffer.pointer);
        if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
                battery->full_charge_capacity = battery->design_capacity;
+       if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
+           battery->power_unit && battery->design_voltage) {
+               battery->design_capacity = battery->design_capacity *
+                   10000 / battery->design_voltage;
+               battery->full_charge_capacity = battery->full_charge_capacity *
+                   10000 / battery->design_voltage;
+               battery->design_capacity_warning =
+                   battery->design_capacity_warning *
+                   10000 / battery->design_voltage;
+               /* Curiously, design_capacity_low, unlike the rest of them,
+                  is correct.  */
+               /* capacity_granularity_* equal 1 on the systems tested, so
+                  it's impossible to tell if they would need an adjustment
+                  or not if their values were higher.  */
+       }
        return result;
 }
 
@@ -476,6 +522,11 @@ static int acpi_battery_get_state(struct acpi_battery *battery)
            && battery->capacity_now >= 0 && battery->capacity_now <= 100)
                battery->capacity_now = (battery->capacity_now *
                                battery->full_charge_capacity) / 100;
+       if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
+           battery->power_unit && battery->design_voltage) {
+               battery->capacity_now = battery->capacity_now *
+                   10000 / battery->design_voltage;
+       }
        return result;
 }
 
@@ -573,16 +624,34 @@ static int sysfs_add_battery(struct acpi_battery *battery)
 
 static void sysfs_remove_battery(struct acpi_battery *battery)
 {
-       mutex_lock(&battery->lock);
+       mutex_lock(&battery->sysfs_lock);
        if (!battery->bat.dev) {
-               mutex_unlock(&battery->lock);
+               mutex_unlock(&battery->sysfs_lock);
                return;
        }
 
        device_remove_file(battery->bat.dev, &alarm_attr);
        power_supply_unregister(&battery->bat);
        battery->bat.dev = NULL;
-       mutex_unlock(&battery->lock);
+       mutex_unlock(&battery->sysfs_lock);
+}
+
+static void find_battery(const struct dmi_header *dm, void *private)
+{
+       struct acpi_battery *battery = (struct acpi_battery *)private;
+       /* Note: the hardcoded offsets below have been extracted from
+          the source code of dmidecode.  */
+       if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
+               const u8 *dmi_data = (const u8 *)(dm + 1);
+               int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
+               if (dm->length >= 18)
+                       dmi_capacity *= dmi_data[17];
+               if (battery->design_capacity * battery->design_voltage / 1000
+                   != dmi_capacity &&
+                   battery->design_capacity * 10 == dmi_capacity)
+                       set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
+                               &battery->flags);
+       }
 }
 
 /*
@@ -610,6 +679,32 @@ static void acpi_battery_quirks(struct acpi_battery *battery)
                battery->capacity_now = (battery->capacity_now *
                                battery->full_charge_capacity) / 100;
        }
+
+       if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
+               return ;
+
+       if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
+               const char *s;
+               s = dmi_get_system_info(DMI_PRODUCT_VERSION);
+               if (s && !strnicmp(s, "ThinkPad", 8)) {
+                       dmi_walk(find_battery, battery);
+                       if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
+                                    &battery->flags) &&
+                           battery->design_voltage) {
+                               battery->design_capacity =
+                                   battery->design_capacity *
+                                   10000 / battery->design_voltage;
+                               battery->full_charge_capacity =
+                                   battery->full_charge_capacity *
+                                   10000 / battery->design_voltage;
+                               battery->design_capacity_warning =
+                                   battery->design_capacity_warning *
+                                   10000 / battery->design_voltage;
+                               battery->capacity_now = battery->capacity_now *
+                                   10000 / battery->design_voltage;
+                       }
+               }
+       }
 }
 
 static int acpi_battery_update(struct acpi_battery *battery)
@@ -642,11 +737,19 @@ static int acpi_battery_update(struct acpi_battery *battery)
 
 static void acpi_battery_refresh(struct acpi_battery *battery)
 {
+       int power_unit;
+
        if (!battery->bat.dev)
                return;
 
+       power_unit = battery->power_unit;
+
        acpi_battery_get_info(battery);
-       /* The battery may have changed its reporting units. */
+
+       if (power_unit == battery->power_unit)
+               return;
+
+       /* The battery has changed its reporting units. */
        sysfs_remove_battery(battery);
        sysfs_add_battery(battery);
 }
@@ -834,7 +937,7 @@ static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
 } \
 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
 { \
-       return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
+       return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
 }
 
 DECLARE_FILE_FUNCTIONS(info);
@@ -870,9 +973,9 @@ DECLARE_FILE_FUNCTIONS(alarm);
                }, \
        }
 
-static struct battery_file {
+static const struct battery_file {
        struct file_operations ops;
-       mode_t mode;
+       umode_t mode;
        const char *name;
 } acpi_battery_file[] = {
        FILE_DESCRIPTION_RO(info),
@@ -967,6 +1070,17 @@ static int battery_notify(struct notifier_block *nb,
        return 0;
 }
 
+static struct dmi_system_id bat_dmi_table[] = {
+       {
+               .ident = "NEC LZ750/LS",
+               .matches = {
+                       DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
+                       DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
+               },
+       },
+       {},
+};
+
 static int acpi_battery_add(struct acpi_device *device)
 {
        int result = 0;
@@ -982,6 +1096,7 @@ static int acpi_battery_add(struct acpi_device *device)
        strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
        device->driver_data = battery;
        mutex_init(&battery->lock);
+       mutex_init(&battery->sysfs_lock);
        if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
                        "_BIX", &handle)))
                set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
@@ -1010,11 +1125,12 @@ static int acpi_battery_add(struct acpi_device *device)
 fail:
        sysfs_remove_battery(battery);
        mutex_destroy(&battery->lock);
+       mutex_destroy(&battery->sysfs_lock);
        kfree(battery);
        return result;
 }
 
-static int acpi_battery_remove(struct acpi_device *device, int type)
+static int acpi_battery_remove(struct acpi_device *device)
 {
        struct acpi_battery *battery = NULL;
 
@@ -1027,21 +1143,31 @@ static int acpi_battery_remove(struct acpi_device *device, int type)
 #endif
        sysfs_remove_battery(battery);
        mutex_destroy(&battery->lock);
+       mutex_destroy(&battery->sysfs_lock);
        kfree(battery);
        return 0;
 }
 
+#ifdef CONFIG_PM_SLEEP
 /* this is needed to learn about changes made in suspended state */
-static int acpi_battery_resume(struct acpi_device *device)
+static int acpi_battery_resume(struct device *dev)
 {
        struct acpi_battery *battery;
-       if (!device)
+
+       if (!dev)
                return -EINVAL;
-       battery = acpi_driver_data(device);
+
+       battery = acpi_driver_data(to_acpi_device(dev));
+       if (!battery)
+               return -EINVAL;
+
        battery->update_time = 0;
        acpi_battery_update(battery);
        return 0;
 }
+#endif
+
+static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
 
 static struct acpi_driver acpi_battery_driver = {
        .name = "battery",
@@ -1050,10 +1176,10 @@ static struct acpi_driver acpi_battery_driver = {
        .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
        .ops = {
                .add = acpi_battery_add,
-               .resume = acpi_battery_resume,
                .remove = acpi_battery_remove,
                .notify = acpi_battery_notify,
                },
+       .drv.pm = &acpi_battery_pm,
 };
 
 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
@@ -1065,6 +1191,8 @@ static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
        if (!acpi_battery_dir)
                return;
 #endif
+       if (dmi_check_system(bat_dmi_table))
+               battery_bix_broken_package = 1;
        if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
 #ifdef CONFIG_ACPI_PROCFS_POWER
                acpi_unlock_battery_dir(acpi_battery_dir);