Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 2 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 4 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
| 5 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 7 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 8 | * |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or (at |
| 14 | * your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 24 | * |
| 25 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/types.h> |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 33 | #include <linux/async.h> |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 34 | #include <linux/dmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 36 | #include <linux/suspend.h> |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 37 | #include <asm/unaligned.h> |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 38 | |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 39 | #include <linux/acpi.h> |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 40 | #include <linux/power_supply.h> |
| 41 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 42 | #define PREFIX "ACPI: " |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #define ACPI_BATTERY_CLASS "battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 49 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 50 | #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Lan Tianyu | ae6f618 | 2011-06-30 11:32:40 +0800 | [diff] [blame] | 52 | /* Battery power unit: 0 means mW, 1 means mA */ |
| 53 | #define ACPI_BATTERY_POWER_UNIT_MA 1 |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 56 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 57 | ACPI_MODULE_NAME("battery"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 59 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 60 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 61 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | MODULE_LICENSE("GPL"); |
| 63 | |
Lan Tianyu | a90b403 | 2014-01-06 22:50:37 +0800 | [diff] [blame] | 64 | static int battery_bix_broken_package; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 65 | static unsigned int cache_time = 1000; |
| 66 | module_param(cache_time, uint, 0644); |
| 67 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 68 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 69 | static const struct acpi_device_id battery_device_ids[] = { |
| 70 | {"PNP0C0A", 0}, |
| 71 | {"", 0}, |
| 72 | }; |
| 73 | |
| 74 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
| 75 | |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 76 | enum { |
| 77 | ACPI_BATTERY_ALARM_PRESENT, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 78 | ACPI_BATTERY_XINFO_PRESENT, |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 79 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 80 | /* On Lenovo Thinkpad models from 2010 and 2011, the power unit |
| 81 | switches between mWh and mAh depending on whether the system |
| 82 | is running on battery or not. When mAh is the unit, most |
| 83 | reported values are incorrect and need to be adjusted by |
| 84 | 10000/design_voltage. Verified on x201, t410, t410s, and x220. |
| 85 | Pre-2010 and 2012 models appear to always report in mWh and |
| 86 | are thus unaffected (tested with t42, t61, t500, x200, x300, |
| 87 | and x230). Also, in mid-2012 Lenovo issued a BIOS update for |
| 88 | the 2011 models that fixes the issue (tested on x220 with a |
| 89 | post-1.29 BIOS), but as of Nov. 2012, no such update is |
| 90 | available for the 2010 models. */ |
| 91 | ACPI_BATTERY_QUIRK_THINKPAD_MAH, |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 92 | }; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 93 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 94 | struct acpi_battery { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 95 | struct mutex lock; |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 96 | struct mutex sysfs_lock; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 97 | struct power_supply bat; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 98 | struct acpi_device *device; |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 99 | struct notifier_block pm_nb; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 100 | unsigned long update_time; |
Lan Tianyu | 016d5ba | 2013-07-30 14:00:42 +0200 | [diff] [blame] | 101 | int revision; |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 102 | int rate_now; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 103 | int capacity_now; |
| 104 | int voltage_now; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 105 | int design_capacity; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 106 | int full_charge_capacity; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 107 | int technology; |
| 108 | int design_voltage; |
| 109 | int design_capacity_warning; |
| 110 | int design_capacity_low; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 111 | int cycle_count; |
| 112 | int measurement_accuracy; |
| 113 | int max_sampling_time; |
| 114 | int min_sampling_time; |
| 115 | int max_averaging_interval; |
| 116 | int min_averaging_interval; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 117 | int capacity_granularity_1; |
| 118 | int capacity_granularity_2; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 119 | int alarm; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 120 | char model_number[32]; |
| 121 | char serial_number[32]; |
| 122 | char type[32]; |
| 123 | char oem_info[32]; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 124 | int state; |
| 125 | int power_unit; |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 126 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 129 | #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 130 | |
Andy Shevchenko | efd941f | 2013-03-11 09:17:06 +0000 | [diff] [blame] | 131 | static inline int acpi_battery_present(struct acpi_battery *battery) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 132 | { |
| 133 | return battery->device->status.battery_present; |
| 134 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 135 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 136 | static int acpi_battery_technology(struct acpi_battery *battery) |
| 137 | { |
| 138 | if (!strcasecmp("NiCd", battery->type)) |
| 139 | return POWER_SUPPLY_TECHNOLOGY_NiCd; |
| 140 | if (!strcasecmp("NiMH", battery->type)) |
| 141 | return POWER_SUPPLY_TECHNOLOGY_NiMH; |
| 142 | if (!strcasecmp("LION", battery->type)) |
| 143 | return POWER_SUPPLY_TECHNOLOGY_LION; |
Andrey Borzenkov | ad40e68 | 2007-11-10 20:02:49 +0300 | [diff] [blame] | 144 | if (!strncasecmp("LI-ION", battery->type, 6)) |
Alexey Starikovskiy | 0bde7ee | 2007-10-28 15:33:10 +0300 | [diff] [blame] | 145 | return POWER_SUPPLY_TECHNOLOGY_LION; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 146 | if (!strcasecmp("LiP", battery->type)) |
| 147 | return POWER_SUPPLY_TECHNOLOGY_LIPO; |
| 148 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
| 149 | } |
| 150 | |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 151 | static int acpi_battery_get_state(struct acpi_battery *battery); |
Alexey Starikovskiy | b19073a | 2007-10-25 17:10:47 -0400 | [diff] [blame] | 152 | |
Richard Hughes | 56f382a | 2009-01-25 15:05:50 +0000 | [diff] [blame] | 153 | static int acpi_battery_is_charged(struct acpi_battery *battery) |
| 154 | { |
| 155 | /* either charging or discharging */ |
| 156 | if (battery->state != 0) |
| 157 | return 0; |
| 158 | |
| 159 | /* battery not reporting charge */ |
| 160 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || |
| 161 | battery->capacity_now == 0) |
| 162 | return 0; |
| 163 | |
| 164 | /* good batteries update full_charge as the batteries degrade */ |
| 165 | if (battery->full_charge_capacity == battery->capacity_now) |
| 166 | return 1; |
| 167 | |
| 168 | /* fallback to using design values for broken batteries */ |
| 169 | if (battery->design_capacity == battery->capacity_now) |
| 170 | return 1; |
| 171 | |
| 172 | /* we don't do any sort of metric based on percentages */ |
| 173 | return 0; |
| 174 | } |
| 175 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 176 | static int acpi_battery_get_property(struct power_supply *psy, |
| 177 | enum power_supply_property psp, |
| 178 | union power_supply_propval *val) |
| 179 | { |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 180 | int ret = 0; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 181 | struct acpi_battery *battery = to_acpi_battery(psy); |
| 182 | |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 183 | if (acpi_battery_present(battery)) { |
| 184 | /* run battery update only if it is present */ |
| 185 | acpi_battery_get_state(battery); |
| 186 | } else if (psp != POWER_SUPPLY_PROP_PRESENT) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 187 | return -ENODEV; |
| 188 | switch (psp) { |
| 189 | case POWER_SUPPLY_PROP_STATUS: |
| 190 | if (battery->state & 0x01) |
| 191 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 192 | else if (battery->state & 0x02) |
| 193 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
Richard Hughes | 56f382a | 2009-01-25 15:05:50 +0000 | [diff] [blame] | 194 | else if (acpi_battery_is_charged(battery)) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 195 | val->intval = POWER_SUPPLY_STATUS_FULL; |
Roland Dreier | 4c41d3a | 2007-11-07 15:09:09 -0800 | [diff] [blame] | 196 | else |
| 197 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 198 | break; |
| 199 | case POWER_SUPPLY_PROP_PRESENT: |
| 200 | val->intval = acpi_battery_present(battery); |
| 201 | break; |
| 202 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 203 | val->intval = acpi_battery_technology(battery); |
| 204 | break; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 205 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 206 | val->intval = battery->cycle_count; |
| 207 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 208 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 209 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 210 | ret = -ENODEV; |
| 211 | else |
| 212 | val->intval = battery->design_voltage * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 213 | break; |
| 214 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 215 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 216 | ret = -ENODEV; |
| 217 | else |
| 218 | val->intval = battery->voltage_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 219 | break; |
| 220 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 221 | case POWER_SUPPLY_PROP_POWER_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 222 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 223 | ret = -ENODEV; |
| 224 | else |
| 225 | val->intval = battery->rate_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 226 | break; |
| 227 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 228 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 229 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 230 | ret = -ENODEV; |
| 231 | else |
| 232 | val->intval = battery->design_capacity * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 233 | break; |
| 234 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 235 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 236 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 237 | ret = -ENODEV; |
| 238 | else |
| 239 | val->intval = battery->full_charge_capacity * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 240 | break; |
| 241 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 242 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 243 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 244 | ret = -ENODEV; |
| 245 | else |
| 246 | val->intval = battery->capacity_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 247 | break; |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 248 | case POWER_SUPPLY_PROP_CAPACITY: |
| 249 | if (battery->capacity_now && battery->full_charge_capacity) |
| 250 | val->intval = battery->capacity_now * 100/ |
| 251 | battery->full_charge_capacity; |
| 252 | else |
| 253 | val->intval = 0; |
| 254 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 255 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 256 | val->strval = battery->model_number; |
| 257 | break; |
| 258 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 259 | val->strval = battery->oem_info; |
| 260 | break; |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 261 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 262 | val->strval = battery->serial_number; |
| 263 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 264 | default: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 265 | ret = -EINVAL; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 266 | } |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 267 | return ret; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static enum power_supply_property charge_battery_props[] = { |
| 271 | POWER_SUPPLY_PROP_STATUS, |
| 272 | POWER_SUPPLY_PROP_PRESENT, |
| 273 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 274 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 275 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 276 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 277 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 278 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 279 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 280 | POWER_SUPPLY_PROP_CHARGE_NOW, |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 281 | POWER_SUPPLY_PROP_CAPACITY, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 282 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 283 | POWER_SUPPLY_PROP_MANUFACTURER, |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 284 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | static enum power_supply_property energy_battery_props[] = { |
| 288 | POWER_SUPPLY_PROP_STATUS, |
| 289 | POWER_SUPPLY_PROP_PRESENT, |
| 290 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 291 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 292 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 293 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 294 | POWER_SUPPLY_PROP_POWER_NOW, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 295 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| 296 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 297 | POWER_SUPPLY_PROP_ENERGY_NOW, |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 298 | POWER_SUPPLY_PROP_CAPACITY, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 299 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 300 | POWER_SUPPLY_PROP_MANUFACTURER, |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 301 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 302 | }; |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* -------------------------------------------------------------------------- |
| 305 | Battery Management |
| 306 | -------------------------------------------------------------------------- */ |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 307 | struct acpi_offsets { |
| 308 | size_t offset; /* offset inside struct acpi_sbs_battery */ |
| 309 | u8 mode; /* int or string? */ |
| 310 | }; |
| 311 | |
| 312 | static struct acpi_offsets state_offsets[] = { |
| 313 | {offsetof(struct acpi_battery, state), 0}, |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 314 | {offsetof(struct acpi_battery, rate_now), 0}, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 315 | {offsetof(struct acpi_battery, capacity_now), 0}, |
| 316 | {offsetof(struct acpi_battery, voltage_now), 0}, |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | static struct acpi_offsets info_offsets[] = { |
| 320 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 321 | {offsetof(struct acpi_battery, design_capacity), 0}, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 322 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 323 | {offsetof(struct acpi_battery, technology), 0}, |
| 324 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 325 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 326 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 327 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 328 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 329 | {offsetof(struct acpi_battery, model_number), 1}, |
| 330 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 331 | {offsetof(struct acpi_battery, type), 1}, |
| 332 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 333 | }; |
| 334 | |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 335 | static struct acpi_offsets extended_info_offsets[] = { |
Lan Tianyu | 016d5ba | 2013-07-30 14:00:42 +0200 | [diff] [blame] | 336 | {offsetof(struct acpi_battery, revision), 0}, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 337 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 338 | {offsetof(struct acpi_battery, design_capacity), 0}, |
| 339 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
| 340 | {offsetof(struct acpi_battery, technology), 0}, |
| 341 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 342 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 343 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 344 | {offsetof(struct acpi_battery, cycle_count), 0}, |
| 345 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, |
| 346 | {offsetof(struct acpi_battery, max_sampling_time), 0}, |
| 347 | {offsetof(struct acpi_battery, min_sampling_time), 0}, |
| 348 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, |
| 349 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, |
| 350 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 351 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 352 | {offsetof(struct acpi_battery, model_number), 1}, |
| 353 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 354 | {offsetof(struct acpi_battery, type), 1}, |
| 355 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 356 | }; |
| 357 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 358 | static int extract_package(struct acpi_battery *battery, |
| 359 | union acpi_object *package, |
| 360 | struct acpi_offsets *offsets, int num) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 361 | { |
Alexey Starikovskiy | 106449e | 2007-10-29 23:29:40 +0300 | [diff] [blame] | 362 | int i; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 363 | union acpi_object *element; |
| 364 | if (package->type != ACPI_TYPE_PACKAGE) |
| 365 | return -EFAULT; |
| 366 | for (i = 0; i < num; ++i) { |
| 367 | if (package->package.count <= i) |
| 368 | return -EFAULT; |
| 369 | element = &package->package.elements[i]; |
| 370 | if (offsets[i].mode) { |
Alexey Starikovskiy | 106449e | 2007-10-29 23:29:40 +0300 | [diff] [blame] | 371 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
| 372 | if (element->type == ACPI_TYPE_STRING || |
| 373 | element->type == ACPI_TYPE_BUFFER) |
| 374 | strncpy(ptr, element->string.pointer, 32); |
| 375 | else if (element->type == ACPI_TYPE_INTEGER) { |
| 376 | strncpy(ptr, (u8 *)&element->integer.value, |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 377 | sizeof(u64)); |
| 378 | ptr[sizeof(u64)] = 0; |
Alexey Starikovskiy | b8a1bdb | 2008-03-17 22:37:42 -0400 | [diff] [blame] | 379 | } else |
| 380 | *ptr = 0; /* don't have value */ |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 381 | } else { |
Alexey Starikovskiy | b8a1bdb | 2008-03-17 22:37:42 -0400 | [diff] [blame] | 382 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
| 383 | *x = (element->type == ACPI_TYPE_INTEGER) ? |
| 384 | element->integer.value : -1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 385 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 386 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int acpi_battery_get_status(struct acpi_battery *battery) |
| 391 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 392 | if (acpi_bus_get_status(battery->device)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 393 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
| 394 | return -ENODEV; |
| 395 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 396 | return 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static int acpi_battery_get_info(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 401 | int result = -EFAULT; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 402 | acpi_status status = 0; |
Nicholas Mazzuca | 0f4c654 | 2013-05-08 23:11:15 +0000 | [diff] [blame] | 403 | char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags) ? |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 404 | "_BIX" : "_BIF"; |
| 405 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 406 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 408 | if (!acpi_battery_present(battery)) |
| 409 | return 0; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 410 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 411 | status = acpi_evaluate_object(battery->device->handle, name, |
| 412 | NULL, &buffer); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 413 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (ACPI_FAILURE(status)) { |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 416 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 417 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
Lan Tianyu | a90b403 | 2014-01-06 22:50:37 +0800 | [diff] [blame] | 419 | |
| 420 | if (battery_bix_broken_package) |
| 421 | result = extract_package(battery, buffer.pointer, |
| 422 | extended_info_offsets + 1, |
| 423 | ARRAY_SIZE(extended_info_offsets) - 1); |
| 424 | else if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)) |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 425 | result = extract_package(battery, buffer.pointer, |
| 426 | extended_info_offsets, |
| 427 | ARRAY_SIZE(extended_info_offsets)); |
| 428 | else |
| 429 | result = extract_package(battery, buffer.pointer, |
| 430 | info_offsets, ARRAY_SIZE(info_offsets)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 431 | kfree(buffer.pointer); |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 432 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
| 433 | battery->full_charge_capacity = battery->design_capacity; |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 434 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
| 435 | battery->power_unit && battery->design_voltage) { |
| 436 | battery->design_capacity = battery->design_capacity * |
| 437 | 10000 / battery->design_voltage; |
| 438 | battery->full_charge_capacity = battery->full_charge_capacity * |
| 439 | 10000 / battery->design_voltage; |
| 440 | battery->design_capacity_warning = |
| 441 | battery->design_capacity_warning * |
| 442 | 10000 / battery->design_voltage; |
| 443 | /* Curiously, design_capacity_low, unlike the rest of them, |
| 444 | is correct. */ |
| 445 | /* capacity_granularity_* equal 1 on the systems tested, so |
| 446 | it's impossible to tell if they would need an adjustment |
| 447 | or not if their values were higher. */ |
| 448 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 449 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 452 | static int acpi_battery_get_state(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 454 | int result = 0; |
| 455 | acpi_status status = 0; |
| 456 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 458 | if (!acpi_battery_present(battery)) |
| 459 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 461 | if (battery->update_time && |
| 462 | time_before(jiffies, battery->update_time + |
| 463 | msecs_to_jiffies(cache_time))) |
| 464 | return 0; |
| 465 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 466 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 467 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 468 | NULL, &buffer); |
| 469 | mutex_unlock(&battery->lock); |
Len Brown | 5b31d89 | 2007-08-15 00:19:26 -0400 | [diff] [blame] | 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 472 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 473 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 475 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 476 | result = extract_package(battery, buffer.pointer, |
| 477 | state_offsets, ARRAY_SIZE(state_offsets)); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 478 | battery->update_time = jiffies; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 479 | kfree(buffer.pointer); |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 480 | |
Lan Tianyu | 55003b2 | 2011-06-30 11:33:12 +0800 | [diff] [blame] | 481 | /* For buggy DSDTs that report negative 16-bit values for either |
| 482 | * charging or discharging current and/or report 0 as 65536 |
| 483 | * due to bad math. |
| 484 | */ |
| 485 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && |
| 486 | battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && |
| 487 | (s16)(battery->rate_now) < 0) { |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 488 | battery->rate_now = abs((s16)battery->rate_now); |
Lan Tianyu | 55003b2 | 2011-06-30 11:33:12 +0800 | [diff] [blame] | 489 | printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate" |
| 490 | " invalid.\n"); |
| 491 | } |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 492 | |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 493 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
| 494 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) |
| 495 | battery->capacity_now = (battery->capacity_now * |
| 496 | battery->full_charge_capacity) / 100; |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 497 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
| 498 | battery->power_unit && battery->design_voltage) { |
| 499 | battery->capacity_now = battery->capacity_now * |
| 500 | 10000 / battery->design_voltage; |
| 501 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 502 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 505 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 507 | acpi_status status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 509 | if (!acpi_battery_present(battery) || |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 510 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 511 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 513 | mutex_lock(&battery->lock); |
Jiang Liu | 0db9820 | 2013-06-29 00:24:39 +0800 | [diff] [blame] | 514 | status = acpi_execute_simple_method(battery->device->handle, "_BTP", |
| 515 | battery->alarm); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 516 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 519 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 521 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 522 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 525 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 527 | /* See if alarms are supported, and if so, set default */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 528 | if (!acpi_has_method(battery->device->handle, "_BTP")) { |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 529 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 530 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 532 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 533 | if (!battery->alarm) |
| 534 | battery->alarm = battery->design_capacity_warning; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 535 | return acpi_battery_set_alarm(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 538 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
| 539 | struct device_attribute *attr, |
| 540 | char *buf) |
| 541 | { |
| 542 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); |
| 543 | return sprintf(buf, "%d\n", battery->alarm * 1000); |
| 544 | } |
| 545 | |
| 546 | static ssize_t acpi_battery_alarm_store(struct device *dev, |
| 547 | struct device_attribute *attr, |
| 548 | const char *buf, size_t count) |
| 549 | { |
| 550 | unsigned long x; |
| 551 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); |
Luis G.F | 47a08c8 | 2014-01-21 15:40:43 +0100 | [diff] [blame] | 552 | if (sscanf(buf, "%lu\n", &x) == 1) |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 553 | battery->alarm = x/1000; |
| 554 | if (acpi_battery_present(battery)) |
| 555 | acpi_battery_set_alarm(battery); |
| 556 | return count; |
| 557 | } |
| 558 | |
| 559 | static struct device_attribute alarm_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 560 | .attr = {.name = "alarm", .mode = 0644}, |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 561 | .show = acpi_battery_alarm_show, |
| 562 | .store = acpi_battery_alarm_store, |
| 563 | }; |
| 564 | |
| 565 | static int sysfs_add_battery(struct acpi_battery *battery) |
| 566 | { |
| 567 | int result; |
| 568 | |
Lan Tianyu | ae6f618 | 2011-06-30 11:32:40 +0800 | [diff] [blame] | 569 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 570 | battery->bat.properties = charge_battery_props; |
| 571 | battery->bat.num_properties = |
| 572 | ARRAY_SIZE(charge_battery_props); |
| 573 | } else { |
| 574 | battery->bat.properties = energy_battery_props; |
| 575 | battery->bat.num_properties = |
| 576 | ARRAY_SIZE(energy_battery_props); |
| 577 | } |
| 578 | |
| 579 | battery->bat.name = acpi_device_bid(battery->device); |
| 580 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 581 | battery->bat.get_property = acpi_battery_get_property; |
| 582 | |
| 583 | result = power_supply_register(&battery->device->dev, &battery->bat); |
| 584 | if (result) |
| 585 | return result; |
| 586 | return device_create_file(battery->bat.dev, &alarm_attr); |
| 587 | } |
| 588 | |
| 589 | static void sysfs_remove_battery(struct acpi_battery *battery) |
| 590 | { |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 591 | mutex_lock(&battery->sysfs_lock); |
Lan Tianyu | 9c921c22 | 2011-06-30 11:34:12 +0800 | [diff] [blame] | 592 | if (!battery->bat.dev) { |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 593 | mutex_unlock(&battery->sysfs_lock); |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 594 | return; |
Lan Tianyu | 9c921c22 | 2011-06-30 11:34:12 +0800 | [diff] [blame] | 595 | } |
| 596 | |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 597 | device_remove_file(battery->bat.dev, &alarm_attr); |
| 598 | power_supply_unregister(&battery->bat); |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 599 | battery->bat.dev = NULL; |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 600 | mutex_unlock(&battery->sysfs_lock); |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 603 | static void find_battery(const struct dmi_header *dm, void *private) |
| 604 | { |
| 605 | struct acpi_battery *battery = (struct acpi_battery *)private; |
| 606 | /* Note: the hardcoded offsets below have been extracted from |
| 607 | the source code of dmidecode. */ |
| 608 | if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) { |
| 609 | const u8 *dmi_data = (const u8 *)(dm + 1); |
| 610 | int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6)); |
| 611 | if (dm->length >= 18) |
| 612 | dmi_capacity *= dmi_data[17]; |
| 613 | if (battery->design_capacity * battery->design_voltage / 1000 |
| 614 | != dmi_capacity && |
| 615 | battery->design_capacity * 10 == dmi_capacity) |
| 616 | set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, |
| 617 | &battery->flags); |
| 618 | } |
| 619 | } |
| 620 | |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 621 | /* |
| 622 | * According to the ACPI spec, some kinds of primary batteries can |
| 623 | * report percentage battery remaining capacity directly to OS. |
| 624 | * In this case, it reports the Last Full Charged Capacity == 100 |
| 625 | * and BatteryPresentRate == 0xFFFFFFFF. |
| 626 | * |
| 627 | * Now we found some battery reports percentage remaining capacity |
| 628 | * even if it's rechargeable. |
| 629 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 |
| 630 | * |
| 631 | * Handle this correctly so that they won't break userspace. |
| 632 | */ |
Lan Tianyu | 7b78622 | 2011-06-30 11:33:27 +0800 | [diff] [blame] | 633 | static void acpi_battery_quirks(struct acpi_battery *battery) |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 634 | { |
| 635 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
Nicholas Mazzuca | 0f4c654 | 2013-05-08 23:11:15 +0000 | [diff] [blame] | 636 | return; |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 637 | |
Nicholas Mazzuca | 0f4c654 | 2013-05-08 23:11:15 +0000 | [diff] [blame] | 638 | if (battery->full_charge_capacity == 100 && |
| 639 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && |
| 640 | battery->capacity_now >= 0 && battery->capacity_now <= 100) { |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 641 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); |
| 642 | battery->full_charge_capacity = battery->design_capacity; |
| 643 | battery->capacity_now = (battery->capacity_now * |
| 644 | battery->full_charge_capacity) / 100; |
| 645 | } |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 646 | |
| 647 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) |
Nicholas Mazzuca | 0f4c654 | 2013-05-08 23:11:15 +0000 | [diff] [blame] | 648 | return; |
Kamil Iskra | 4000e62 | 2012-11-16 22:28:58 +0100 | [diff] [blame] | 649 | |
| 650 | if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { |
| 651 | const char *s; |
| 652 | s = dmi_get_system_info(DMI_PRODUCT_VERSION); |
| 653 | if (s && !strnicmp(s, "ThinkPad", 8)) { |
| 654 | dmi_walk(find_battery, battery); |
| 655 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, |
| 656 | &battery->flags) && |
| 657 | battery->design_voltage) { |
| 658 | battery->design_capacity = |
| 659 | battery->design_capacity * |
| 660 | 10000 / battery->design_voltage; |
| 661 | battery->full_charge_capacity = |
| 662 | battery->full_charge_capacity * |
| 663 | 10000 / battery->design_voltage; |
| 664 | battery->design_capacity_warning = |
| 665 | battery->design_capacity_warning * |
| 666 | 10000 / battery->design_voltage; |
| 667 | battery->capacity_now = battery->capacity_now * |
| 668 | 10000 / battery->design_voltage; |
| 669 | } |
| 670 | } |
| 671 | } |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 672 | } |
| 673 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 674 | static int acpi_battery_update(struct acpi_battery *battery) |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 675 | { |
Alexey Starikovskiy | 50b1785 | 2008-12-23 02:44:54 +0300 | [diff] [blame] | 676 | int result, old_present = acpi_battery_present(battery); |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 677 | result = acpi_battery_get_status(battery); |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 678 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 679 | return result; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 680 | if (!acpi_battery_present(battery)) { |
| 681 | sysfs_remove_battery(battery); |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 682 | battery->update_time = 0; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 683 | return 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 684 | } |
Alexey Starikovskiy | 50b1785 | 2008-12-23 02:44:54 +0300 | [diff] [blame] | 685 | if (!battery->update_time || |
| 686 | old_present != acpi_battery_present(battery)) { |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 687 | result = acpi_battery_get_info(battery); |
| 688 | if (result) |
| 689 | return result; |
| 690 | acpi_battery_init_alarm(battery); |
| 691 | } |
Stefan Hajnoczi | eb03cb0 | 2011-07-12 09:03:29 +0100 | [diff] [blame] | 692 | if (!battery->bat.dev) { |
| 693 | result = sysfs_add_battery(battery); |
| 694 | if (result) |
| 695 | return result; |
| 696 | } |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 697 | result = acpi_battery_get_state(battery); |
Lan Tianyu | 7b78622 | 2011-06-30 11:33:27 +0800 | [diff] [blame] | 698 | acpi_battery_quirks(battery); |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 699 | return result; |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 700 | } |
| 701 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 702 | static void acpi_battery_refresh(struct acpi_battery *battery) |
| 703 | { |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 704 | int power_unit; |
| 705 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 706 | if (!battery->bat.dev) |
| 707 | return; |
| 708 | |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 709 | power_unit = battery->power_unit; |
| 710 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 711 | acpi_battery_get_info(battery); |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 712 | |
| 713 | if (power_unit == battery->power_unit) |
| 714 | return; |
| 715 | |
| 716 | /* The battery has changed its reporting units. */ |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 717 | sysfs_remove_battery(battery); |
| 718 | sysfs_add_battery(battery); |
| 719 | } |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | /* -------------------------------------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | Driver Interface |
| 723 | -------------------------------------------------------------------------- */ |
| 724 | |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 725 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 727 | struct acpi_battery *battery = acpi_driver_data(device); |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 728 | struct device *old; |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 731 | return; |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 732 | old = battery->bat.dev; |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 733 | if (event == ACPI_BATTERY_NOTIFY_INFO) |
| 734 | acpi_battery_refresh(battery); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 735 | acpi_battery_update(battery); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 736 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 737 | dev_name(&device->dev), event, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 738 | acpi_battery_present(battery)); |
Justin P. Mattock | 2345baf | 2009-12-13 14:42:36 -0800 | [diff] [blame] | 739 | /* acpi_battery_update could remove power_supply object */ |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 740 | if (old && battery->bat.dev) |
Alan Jenkins | f79e1ce | 2009-06-30 14:36:16 +0000 | [diff] [blame] | 741 | power_supply_changed(&battery->bat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 744 | static int battery_notify(struct notifier_block *nb, |
| 745 | unsigned long mode, void *_unused) |
| 746 | { |
| 747 | struct acpi_battery *battery = container_of(nb, struct acpi_battery, |
| 748 | pm_nb); |
| 749 | switch (mode) { |
Lan Tianyu | d5a5911 | 2011-06-30 11:33:40 +0800 | [diff] [blame] | 750 | case PM_POST_HIBERNATION: |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 751 | case PM_POST_SUSPEND: |
Lan Tianyu | 6e17fb6 | 2011-06-30 11:33:58 +0800 | [diff] [blame] | 752 | if (battery->bat.dev) { |
| 753 | sysfs_remove_battery(battery); |
| 754 | sysfs_add_battery(battery); |
| 755 | } |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 756 | break; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Lan Tianyu | a90b403 | 2014-01-06 22:50:37 +0800 | [diff] [blame] | 762 | static struct dmi_system_id bat_dmi_table[] = { |
| 763 | { |
| 764 | .ident = "NEC LZ750/LS", |
| 765 | .matches = { |
| 766 | DMI_MATCH(DMI_SYS_VENDOR, "NEC"), |
| 767 | DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"), |
| 768 | }, |
| 769 | }, |
| 770 | {}, |
| 771 | }; |
| 772 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 773 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 775 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 776 | struct acpi_battery *battery = NULL; |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 779 | return -EINVAL; |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 780 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 782 | return -ENOMEM; |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 783 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 785 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 786 | device->driver_data = battery; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 787 | mutex_init(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 788 | mutex_init(&battery->sysfs_lock); |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 789 | if (acpi_has_method(battery->device->handle, "_BIX")) |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 790 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); |
Stefan Hajnoczi | eb03cb0 | 2011-07-12 09:03:29 +0100 | [diff] [blame] | 791 | result = acpi_battery_update(battery); |
| 792 | if (result) |
| 793 | goto fail; |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 794 | |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 795 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
| 796 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 797 | device->status.battery_present ? "present" : "absent"); |
| 798 | |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 799 | battery->pm_nb.notifier_call = battery_notify; |
| 800 | register_pm_notifier(&battery->pm_nb); |
| 801 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 802 | return result; |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 803 | |
| 804 | fail: |
| 805 | sysfs_remove_battery(battery); |
| 806 | mutex_destroy(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 807 | mutex_destroy(&battery->sysfs_lock); |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 808 | kfree(battery); |
| 809 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 812 | static int acpi_battery_remove(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 814 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 817 | return -EINVAL; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 818 | battery = acpi_driver_data(device); |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 819 | unregister_pm_notifier(&battery->pm_nb); |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 820 | sysfs_remove_battery(battery); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 821 | mutex_destroy(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 822 | mutex_destroy(&battery->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | kfree(battery); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 824 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 827 | #ifdef CONFIG_PM_SLEEP |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 828 | /* this is needed to learn about changes made in suspended state */ |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 829 | static int acpi_battery_resume(struct device *dev) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 830 | { |
| 831 | struct acpi_battery *battery; |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 832 | |
| 833 | if (!dev) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 834 | return -EINVAL; |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 835 | |
| 836 | battery = acpi_driver_data(to_acpi_device(dev)); |
| 837 | if (!battery) |
| 838 | return -EINVAL; |
| 839 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 840 | battery->update_time = 0; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 841 | acpi_battery_update(battery); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 842 | return 0; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 843 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 844 | #endif |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 845 | |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 846 | static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume); |
| 847 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 848 | static struct acpi_driver acpi_battery_driver = { |
| 849 | .name = "battery", |
| 850 | .class = ACPI_BATTERY_CLASS, |
| 851 | .ids = battery_device_ids, |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 852 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 853 | .ops = { |
| 854 | .add = acpi_battery_add, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 855 | .remove = acpi_battery_remove, |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 856 | .notify = acpi_battery_notify, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 857 | }, |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 858 | .drv.pm = &acpi_battery_pm, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 859 | }; |
| 860 | |
Linus Torvalds | b0cbc86 | 2009-04-11 12:45:20 -0700 | [diff] [blame] | 861 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 863 | if (acpi_disabled) |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 864 | return; |
Lan Tianyu | a90b403 | 2014-01-06 22:50:37 +0800 | [diff] [blame] | 865 | |
| 866 | if (dmi_check_system(bat_dmi_table)) |
| 867 | battery_bix_broken_package = 1; |
Lan Tianyu | 1e2d9cd | 2013-10-11 09:54:08 +0800 | [diff] [blame] | 868 | acpi_bus_register_driver(&acpi_battery_driver); |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static int __init acpi_battery_init(void) |
| 872 | { |
| 873 | async_schedule(acpi_battery_init_async, NULL); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 874 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 877 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | acpi_bus_unregister_driver(&acpi_battery_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | module_init(acpi_battery_init); |
| 883 | module_exit(acpi_battery_exit); |