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