blob: 7ae5ebd1e70ec541f0218f8983148506d768663e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04002 * battery.c - ACPI Battery Driver (Revision: 2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04004 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 Starikovskiyf1d46612007-09-26 19:42:52 +040032#include <linux/jiffies.h>
Arjan van de Ven0f66af52009-01-10 14:19:05 -050033#include <linux/async.h>
Hector Martinbc76f902009-08-06 15:57:48 -070034#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Kyle McMartin25be5822011-03-22 16:19:50 -040036#include <linux/suspend.h>
Lan Tianyu3fef2d52014-07-07 15:47:12 +080037#include <linux/delay.h>
Kamil Iskra4000e622012-11-16 22:28:58 +010038#include <asm/unaligned.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040039
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030040#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/proc_fs.h>
42#include <linux/seq_file.h>
43#include <asm/uaccess.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040048#include <linux/power_supply.h>
49
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ACPI_BATTERY_NOTIFY_STATUS 0x80
57#define ACPI_BATTERY_NOTIFY_INFO 0x81
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040058#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Lan Tianyuae6f6182011-06-30 11:32:40 +080060/* Battery power unit: 0 means mW, 1 means mA */
61#define ACPI_BATTERY_POWER_UNIT_MA 1
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030064
Len Brownf52fd662007-02-12 22:42:12 -050065ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Len Brownf52fd662007-02-12 22:42:12 -050067MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040068MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050069MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070MODULE_LICENSE("GPL");
71
Lan Tianyu290a6992014-01-06 22:50:37 +080072static int battery_bix_broken_package;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040073static unsigned int cache_time = 1000;
74module_param(cache_time, uint, 0644);
75MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030076
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030077#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040078extern struct proc_dir_entry *acpi_lock_battery_dir(void);
79extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
80
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040081enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040082 info_tag = 0,
83 state_tag,
84 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040085 ACPI_BATTERY_NUMFILES,
86};
87
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040088#endif
89
90static const struct acpi_device_id battery_device_ids[] = {
91 {"PNP0C0A", 0},
92 {"", 0},
93};
94
95MODULE_DEVICE_TABLE(acpi, battery_device_ids);
96
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040097enum {
98 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040099 ACPI_BATTERY_XINFO_PRESENT,
Zhang Rui557d5862010-10-22 10:02:06 +0800100 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Kamil Iskra4000e622012-11-16 22:28:58 +0100101 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
102 switches between mWh and mAh depending on whether the system
103 is running on battery or not. When mAh is the unit, most
104 reported values are incorrect and need to be adjusted by
105 10000/design_voltage. Verified on x201, t410, t410s, and x220.
106 Pre-2010 and 2012 models appear to always report in mWh and
107 are thus unaffected (tested with t42, t61, t500, x200, x300,
108 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
109 the 2011 models that fixes the issue (tested on x220 with a
110 post-1.29 BIOS), but as of Nov. 2012, no such update is
111 available for the 2010 models. */
112 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400113};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400114
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400115struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400116 struct mutex lock;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300117 struct mutex sysfs_lock;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400118 struct power_supply bat;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400119 struct acpi_device *device;
Kyle McMartin25be5822011-03-22 16:19:50 -0400120 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400121 unsigned long update_time;
Lan Tianyu6813e1d2013-07-30 14:00:42 +0200122 int revision;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400123 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400124 int capacity_now;
125 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400126 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400127 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400128 int technology;
129 int design_voltage;
130 int design_capacity_warning;
131 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400132 int cycle_count;
133 int measurement_accuracy;
134 int max_sampling_time;
135 int min_sampling_time;
136 int max_averaging_interval;
137 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400138 int capacity_granularity_1;
139 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400140 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400141 char model_number[32];
142 char serial_number[32];
143 char type[32];
144 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400145 int state;
146 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400147 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Phil Carmody497888c2011-07-14 15:07:13 +0300150#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400151
Andy Shevchenkoefd941f2013-03-11 09:17:06 +0000152static inline int acpi_battery_present(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400153{
154 return battery->device->status.battery_present;
155}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400156
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400157static int acpi_battery_technology(struct acpi_battery *battery)
158{
159 if (!strcasecmp("NiCd", battery->type))
160 return POWER_SUPPLY_TECHNOLOGY_NiCd;
161 if (!strcasecmp("NiMH", battery->type))
162 return POWER_SUPPLY_TECHNOLOGY_NiMH;
163 if (!strcasecmp("LION", battery->type))
164 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300165 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300166 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400167 if (!strcasecmp("LiP", battery->type))
168 return POWER_SUPPLY_TECHNOLOGY_LIPO;
169 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
170}
171
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300172static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400173
Richard Hughes56f382a2009-01-25 15:05:50 +0000174static int acpi_battery_is_charged(struct acpi_battery *battery)
175{
176 /* either charging or discharging */
177 if (battery->state != 0)
178 return 0;
179
180 /* battery not reporting charge */
181 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
182 battery->capacity_now == 0)
183 return 0;
184
185 /* good batteries update full_charge as the batteries degrade */
186 if (battery->full_charge_capacity == battery->capacity_now)
187 return 1;
188
189 /* fallback to using design values for broken batteries */
190 if (battery->design_capacity == battery->capacity_now)
191 return 1;
192
193 /* we don't do any sort of metric based on percentages */
194 return 0;
195}
196
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400197static int acpi_battery_get_property(struct power_supply *psy,
198 enum power_supply_property psp,
199 union power_supply_propval *val)
200{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200201 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400202 struct acpi_battery *battery = to_acpi_battery(psy);
203
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300204 if (acpi_battery_present(battery)) {
205 /* run battery update only if it is present */
206 acpi_battery_get_state(battery);
207 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400208 return -ENODEV;
209 switch (psp) {
210 case POWER_SUPPLY_PROP_STATUS:
211 if (battery->state & 0x01)
212 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
213 else if (battery->state & 0x02)
214 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000215 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400216 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800217 else
218 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400219 break;
220 case POWER_SUPPLY_PROP_PRESENT:
221 val->intval = acpi_battery_present(battery);
222 break;
223 case POWER_SUPPLY_PROP_TECHNOLOGY:
224 val->intval = acpi_battery_technology(battery);
225 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400226 case POWER_SUPPLY_PROP_CYCLE_COUNT:
227 val->intval = battery->cycle_count;
228 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400229 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200230 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
231 ret = -ENODEV;
232 else
233 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400234 break;
235 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200236 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
237 ret = -ENODEV;
238 else
239 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400240 break;
241 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400242 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200243 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
244 ret = -ENODEV;
245 else
246 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400247 break;
248 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
249 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200250 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
251 ret = -ENODEV;
252 else
253 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400254 break;
255 case POWER_SUPPLY_PROP_CHARGE_FULL:
256 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200257 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
258 ret = -ENODEV;
259 else
260 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400261 break;
262 case POWER_SUPPLY_PROP_CHARGE_NOW:
263 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200264 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
265 ret = -ENODEV;
266 else
267 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400268 break;
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700269 case POWER_SUPPLY_PROP_CAPACITY:
270 if (battery->capacity_now && battery->full_charge_capacity)
271 val->intval = battery->capacity_now * 100/
272 battery->full_charge_capacity;
273 else
274 val->intval = 0;
275 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400276 case POWER_SUPPLY_PROP_MODEL_NAME:
277 val->strval = battery->model_number;
278 break;
279 case POWER_SUPPLY_PROP_MANUFACTURER:
280 val->strval = battery->oem_info;
281 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100282 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
283 val->strval = battery->serial_number;
284 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400285 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200286 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400287 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200288 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400289}
290
291static enum power_supply_property charge_battery_props[] = {
292 POWER_SUPPLY_PROP_STATUS,
293 POWER_SUPPLY_PROP_PRESENT,
294 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400295 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400296 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
297 POWER_SUPPLY_PROP_VOLTAGE_NOW,
298 POWER_SUPPLY_PROP_CURRENT_NOW,
299 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
300 POWER_SUPPLY_PROP_CHARGE_FULL,
301 POWER_SUPPLY_PROP_CHARGE_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700302 POWER_SUPPLY_PROP_CAPACITY,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400303 POWER_SUPPLY_PROP_MODEL_NAME,
304 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100305 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400306};
307
308static enum power_supply_property energy_battery_props[] = {
309 POWER_SUPPLY_PROP_STATUS,
310 POWER_SUPPLY_PROP_PRESENT,
311 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400312 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400313 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
314 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400315 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400316 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
317 POWER_SUPPLY_PROP_ENERGY_FULL,
318 POWER_SUPPLY_PROP_ENERGY_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700319 POWER_SUPPLY_PROP_CAPACITY,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400320 POWER_SUPPLY_PROP_MODEL_NAME,
321 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100322 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400323};
324
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300325#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400326inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400327{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800328 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
329 "mA" : "mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400330}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400331#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* --------------------------------------------------------------------------
334 Battery Management
335 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400336struct acpi_offsets {
337 size_t offset; /* offset inside struct acpi_sbs_battery */
338 u8 mode; /* int or string? */
339};
340
341static struct acpi_offsets state_offsets[] = {
342 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400343 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400344 {offsetof(struct acpi_battery, capacity_now), 0},
345 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400346};
347
348static struct acpi_offsets info_offsets[] = {
349 {offsetof(struct acpi_battery, power_unit), 0},
350 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400351 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400352 {offsetof(struct acpi_battery, technology), 0},
353 {offsetof(struct acpi_battery, design_voltage), 0},
354 {offsetof(struct acpi_battery, design_capacity_warning), 0},
355 {offsetof(struct acpi_battery, design_capacity_low), 0},
356 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
357 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
358 {offsetof(struct acpi_battery, model_number), 1},
359 {offsetof(struct acpi_battery, serial_number), 1},
360 {offsetof(struct acpi_battery, type), 1},
361 {offsetof(struct acpi_battery, oem_info), 1},
362};
363
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400364static struct acpi_offsets extended_info_offsets[] = {
Lan Tianyu6813e1d2013-07-30 14:00:42 +0200365 {offsetof(struct acpi_battery, revision), 0},
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400366 {offsetof(struct acpi_battery, power_unit), 0},
367 {offsetof(struct acpi_battery, design_capacity), 0},
368 {offsetof(struct acpi_battery, full_charge_capacity), 0},
369 {offsetof(struct acpi_battery, technology), 0},
370 {offsetof(struct acpi_battery, design_voltage), 0},
371 {offsetof(struct acpi_battery, design_capacity_warning), 0},
372 {offsetof(struct acpi_battery, design_capacity_low), 0},
373 {offsetof(struct acpi_battery, cycle_count), 0},
374 {offsetof(struct acpi_battery, measurement_accuracy), 0},
375 {offsetof(struct acpi_battery, max_sampling_time), 0},
376 {offsetof(struct acpi_battery, min_sampling_time), 0},
377 {offsetof(struct acpi_battery, max_averaging_interval), 0},
378 {offsetof(struct acpi_battery, min_averaging_interval), 0},
379 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
380 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
381 {offsetof(struct acpi_battery, model_number), 1},
382 {offsetof(struct acpi_battery, serial_number), 1},
383 {offsetof(struct acpi_battery, type), 1},
384 {offsetof(struct acpi_battery, oem_info), 1},
385};
386
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400387static int extract_package(struct acpi_battery *battery,
388 union acpi_object *package,
389 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300390{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300391 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400392 union acpi_object *element;
393 if (package->type != ACPI_TYPE_PACKAGE)
394 return -EFAULT;
395 for (i = 0; i < num; ++i) {
396 if (package->package.count <= i)
397 return -EFAULT;
398 element = &package->package.elements[i];
399 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300400 u8 *ptr = (u8 *)battery + offsets[i].offset;
401 if (element->type == ACPI_TYPE_STRING ||
402 element->type == ACPI_TYPE_BUFFER)
403 strncpy(ptr, element->string.pointer, 32);
404 else if (element->type == ACPI_TYPE_INTEGER) {
405 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800406 sizeof(u64));
407 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400408 } else
409 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400410 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400411 int *x = (int *)((u8 *)battery + offsets[i].offset);
412 *x = (element->type == ACPI_TYPE_INTEGER) ?
413 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300414 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300415 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300416 return 0;
417}
418
419static int acpi_battery_get_status(struct acpi_battery *battery)
420{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400421 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300422 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
423 return -ENODEV;
424 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400425 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300426}
427
428static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400430 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400431 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400432 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
433 "_BIX" : "_BIF";
434
Len Brown4be44fc2005-08-05 00:44:28 -0400435 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300437 if (!acpi_battery_present(battery))
438 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400439 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400440 status = acpi_evaluate_object(battery->device->handle, name,
441 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400442 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400445 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400446 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Lan Tianyu290a6992014-01-06 22:50:37 +0800448
449 if (battery_bix_broken_package)
450 result = extract_package(battery, buffer.pointer,
451 extended_info_offsets + 1,
452 ARRAY_SIZE(extended_info_offsets) - 1);
453 else if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400454 result = extract_package(battery, buffer.pointer,
455 extended_info_offsets,
456 ARRAY_SIZE(extended_info_offsets));
457 else
458 result = extract_package(battery, buffer.pointer,
459 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400460 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800461 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
462 battery->full_charge_capacity = battery->design_capacity;
Kamil Iskra4000e622012-11-16 22:28:58 +0100463 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
464 battery->power_unit && battery->design_voltage) {
465 battery->design_capacity = battery->design_capacity *
466 10000 / battery->design_voltage;
467 battery->full_charge_capacity = battery->full_charge_capacity *
468 10000 / battery->design_voltage;
469 battery->design_capacity_warning =
470 battery->design_capacity_warning *
471 10000 / battery->design_voltage;
472 /* Curiously, design_capacity_low, unlike the rest of them,
473 is correct. */
474 /* capacity_granularity_* equal 1 on the systems tested, so
475 it's impossible to tell if they would need an adjustment
476 or not if their values were higher. */
477 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400478 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300481static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown4be44fc2005-08-05 00:44:28 -0400483 int result = 0;
484 acpi_status status = 0;
485 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300487 if (!acpi_battery_present(battery))
488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400490 if (battery->update_time &&
491 time_before(jiffies, battery->update_time +
492 msecs_to_jiffies(cache_time)))
493 return 0;
494
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400495 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400496 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400497 NULL, &buffer);
498 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400501 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400502 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400504
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400505 result = extract_package(battery, buffer.pointer,
506 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400507 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400508 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700509
Lan Tianyu55003b22011-06-30 11:33:12 +0800510 /* For buggy DSDTs that report negative 16-bit values for either
511 * charging or discharging current and/or report 0 as 65536
512 * due to bad math.
513 */
514 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
515 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
516 (s16)(battery->rate_now) < 0) {
Hector Martinbc76f902009-08-06 15:57:48 -0700517 battery->rate_now = abs((s16)battery->rate_now);
Lan Tianyu55003b22011-06-30 11:33:12 +0800518 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
519 " invalid.\n");
520 }
Hector Martinbc76f902009-08-06 15:57:48 -0700521
Zhang Rui557d5862010-10-22 10:02:06 +0800522 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
523 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
524 battery->capacity_now = (battery->capacity_now *
525 battery->full_charge_capacity) / 100;
Kamil Iskra4000e622012-11-16 22:28:58 +0100526 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
527 battery->power_unit && battery->design_voltage) {
528 battery->capacity_now = battery->capacity_now *
529 10000 / battery->design_voltage;
530 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400531 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400534static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Len Brown4be44fc2005-08-05 00:44:28 -0400536 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400537 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400538 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400540 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400541 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300542 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400544 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400546 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400547 status = acpi_evaluate_object(battery->device->handle, "_BTP",
548 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400549 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400554 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300558static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560 acpi_status status = AE_OK;
561 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300563 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400564 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
565 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400566 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400569 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400570 if (!battery->alarm)
571 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400572 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Andrey Borzenkov508df922007-10-28 12:50:09 +0300575static ssize_t acpi_battery_alarm_show(struct device *dev,
576 struct device_attribute *attr,
577 char *buf)
578{
579 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
580 return sprintf(buf, "%d\n", battery->alarm * 1000);
581}
582
583static ssize_t acpi_battery_alarm_store(struct device *dev,
584 struct device_attribute *attr,
585 const char *buf, size_t count)
586{
587 unsigned long x;
588 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
589 if (sscanf(buf, "%ld\n", &x) == 1)
590 battery->alarm = x/1000;
591 if (acpi_battery_present(battery))
592 acpi_battery_set_alarm(battery);
593 return count;
594}
595
596static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700597 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300598 .show = acpi_battery_alarm_show,
599 .store = acpi_battery_alarm_store,
600};
601
602static int sysfs_add_battery(struct acpi_battery *battery)
603{
604 int result;
605
Lan Tianyuae6f6182011-06-30 11:32:40 +0800606 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Andrey Borzenkov508df922007-10-28 12:50:09 +0300607 battery->bat.properties = charge_battery_props;
608 battery->bat.num_properties =
609 ARRAY_SIZE(charge_battery_props);
610 } else {
611 battery->bat.properties = energy_battery_props;
612 battery->bat.num_properties =
613 ARRAY_SIZE(energy_battery_props);
614 }
615
616 battery->bat.name = acpi_device_bid(battery->device);
617 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
618 battery->bat.get_property = acpi_battery_get_property;
619
620 result = power_supply_register(&battery->device->dev, &battery->bat);
621 if (result)
622 return result;
623 return device_create_file(battery->bat.dev, &alarm_attr);
624}
625
626static void sysfs_remove_battery(struct acpi_battery *battery)
627{
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300628 mutex_lock(&battery->sysfs_lock);
Lan Tianyu9c921c222011-06-30 11:34:12 +0800629 if (!battery->bat.dev) {
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300630 mutex_unlock(&battery->sysfs_lock);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300631 return;
Lan Tianyu9c921c222011-06-30 11:34:12 +0800632 }
633
Andrey Borzenkov508df922007-10-28 12:50:09 +0300634 device_remove_file(battery->bat.dev, &alarm_attr);
635 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300636 battery->bat.dev = NULL;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300637 mutex_unlock(&battery->sysfs_lock);
Hector Martinbc76f902009-08-06 15:57:48 -0700638}
639
Kamil Iskra4000e622012-11-16 22:28:58 +0100640static void find_battery(const struct dmi_header *dm, void *private)
641{
642 struct acpi_battery *battery = (struct acpi_battery *)private;
643 /* Note: the hardcoded offsets below have been extracted from
644 the source code of dmidecode. */
645 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
646 const u8 *dmi_data = (const u8 *)(dm + 1);
647 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
648 if (dm->length >= 18)
649 dmi_capacity *= dmi_data[17];
650 if (battery->design_capacity * battery->design_voltage / 1000
651 != dmi_capacity &&
652 battery->design_capacity * 10 == dmi_capacity)
653 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
654 &battery->flags);
655 }
656}
657
Zhang Rui557d5862010-10-22 10:02:06 +0800658/*
659 * According to the ACPI spec, some kinds of primary batteries can
660 * report percentage battery remaining capacity directly to OS.
661 * In this case, it reports the Last Full Charged Capacity == 100
662 * and BatteryPresentRate == 0xFFFFFFFF.
663 *
664 * Now we found some battery reports percentage remaining capacity
665 * even if it's rechargeable.
666 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
667 *
668 * Handle this correctly so that they won't break userspace.
669 */
Lan Tianyu7b786222011-06-30 11:33:27 +0800670static void acpi_battery_quirks(struct acpi_battery *battery)
Zhang Rui557d5862010-10-22 10:02:06 +0800671{
672 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
673 return ;
674
675 if (battery->full_charge_capacity == 100 &&
676 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
677 battery->capacity_now >=0 && battery->capacity_now <= 100) {
678 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
679 battery->full_charge_capacity = battery->design_capacity;
680 battery->capacity_now = (battery->capacity_now *
681 battery->full_charge_capacity) / 100;
682 }
Kamil Iskra4000e622012-11-16 22:28:58 +0100683
684 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
685 return ;
686
687 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
688 const char *s;
689 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
690 if (s && !strnicmp(s, "ThinkPad", 8)) {
691 dmi_walk(find_battery, battery);
692 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
693 &battery->flags) &&
694 battery->design_voltage) {
695 battery->design_capacity =
696 battery->design_capacity *
697 10000 / battery->design_voltage;
698 battery->full_charge_capacity =
699 battery->full_charge_capacity *
700 10000 / battery->design_voltage;
701 battery->design_capacity_warning =
702 battery->design_capacity_warning *
703 10000 / battery->design_voltage;
704 battery->capacity_now = battery->capacity_now *
705 10000 / battery->design_voltage;
706 }
707 }
708 }
Zhang Rui557d5862010-10-22 10:02:06 +0800709}
710
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400711static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500712{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300713 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500714 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300715 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300716 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300717 if (!acpi_battery_present(battery)) {
718 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500719 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300720 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300721 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300722 if (!battery->update_time ||
723 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500724 result = acpi_battery_get_info(battery);
725 if (result)
726 return result;
727 acpi_battery_init_alarm(battery);
728 }
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +0100729 if (!battery->bat.dev) {
730 result = sysfs_add_battery(battery);
731 if (result)
732 return result;
733 }
Zhang Rui557d5862010-10-22 10:02:06 +0800734 result = acpi_battery_get_state(battery);
Lan Tianyu7b786222011-06-30 11:33:27 +0800735 acpi_battery_quirks(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800736 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500737}
738
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100739static void acpi_battery_refresh(struct acpi_battery *battery)
740{
Andy Whitcroftc5971452012-05-03 14:48:26 +0100741 int power_unit;
742
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100743 if (!battery->bat.dev)
744 return;
745
Andy Whitcroftc5971452012-05-03 14:48:26 +0100746 power_unit = battery->power_unit;
747
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100748 acpi_battery_get_info(battery);
Andy Whitcroftc5971452012-05-03 14:48:26 +0100749
750 if (power_unit == battery->power_unit)
751 return;
752
753 /* The battery has changed its reporting units. */
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100754 sysfs_remove_battery(battery);
755 sysfs_add_battery(battery);
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/* --------------------------------------------------------------------------
759 FS Interface (/proc)
760 -------------------------------------------------------------------------- */
761
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300762#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400763static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300764
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400765static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200767 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300769 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto end;
771
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400772 seq_printf(seq, "present: %s\n",
773 acpi_battery_present(battery)?"yes":"no");
774 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400776 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 seq_printf(seq, "design capacity: unknown\n");
778 else
779 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400780 battery->design_capacity,
781 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400783 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 seq_printf(seq, "last full capacity: unknown\n");
785 else
786 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400787 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400788 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400790 seq_printf(seq, "battery technology: %srechargeable\n",
791 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400793 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 seq_printf(seq, "design voltage: unknown\n");
795 else
796 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400797 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400798 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400799 battery->design_capacity_warning,
800 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400801 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400802 battery->design_capacity_low,
803 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400804 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400805 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400806 battery->capacity_granularity_1,
807 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400808 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400809 battery->capacity_granularity_2,
810 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400811 seq_printf(seq, "model number: %s\n", battery->model_number);
812 seq_printf(seq, "serial number: %s\n", battery->serial_number);
813 seq_printf(seq, "battery type: %s\n", battery->type);
814 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400815 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300816 if (result)
817 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300818 return result;
819}
820
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400821static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200823 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300825 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto end;
827
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400828 seq_printf(seq, "present: %s\n",
829 acpi_battery_present(battery)?"yes":"no");
830 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400833 seq_printf(seq, "capacity state: %s\n",
834 (battery->state & 0x04)?"critical":"ok");
835 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400836 seq_printf(seq,
837 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400838 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400840 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400842 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400845 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 seq_printf(seq, "present rate: unknown\n");
847 else
848 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400849 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400851 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 seq_printf(seq, "remaining capacity: unknown\n");
853 else
854 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400855 battery->capacity_now, acpi_battery_units(battery));
856 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 seq_printf(seq, "present voltage: unknown\n");
858 else
859 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400860 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400861 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400862 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300863 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300864
865 return result;
866}
867
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400868static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200870 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300872 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto end;
874
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300875 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 seq_printf(seq, "present: no\n");
877 goto end;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 seq_printf(seq, "alarm: ");
880 if (!battery->alarm)
881 seq_printf(seq, "unsupported\n");
882 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400883 seq_printf(seq, "%u %sh\n", battery->alarm,
884 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400885 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300886 if (result)
887 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300888 return result;
889}
890
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400891static ssize_t acpi_battery_write_alarm(struct file *file,
892 const char __user * buffer,
893 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Len Brown4be44fc2005-08-05 00:44:28 -0400895 int result = 0;
896 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200897 struct seq_file *m = file->private_data;
898 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400901 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300902 if (!acpi_battery_present(battery)) {
903 result = -ENODEV;
904 goto end;
905 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300906 if (copy_from_user(alarm_string, buffer, count)) {
907 result = -EFAULT;
908 goto end;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400911 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400912 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300913 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300914 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400915 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300916 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400919typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400920
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400921static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
922 acpi_battery_print_info,
923 acpi_battery_print_state,
924 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400925};
926
927static int acpi_battery_read(int fid, struct seq_file *seq)
928{
929 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400930 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400931 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400932}
933
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400934#define DECLARE_FILE_FUNCTIONS(_name) \
935static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
936{ \
937 return acpi_battery_read(_name##_tag, seq); \
938} \
939static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
940{ \
Al Virod9dda782013-03-31 18:16:14 -0400941 return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400942}
943
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400944DECLARE_FILE_FUNCTIONS(info);
945DECLARE_FILE_FUNCTIONS(state);
946DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400947
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400948#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400949
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400950#define FILE_DESCRIPTION_RO(_name) \
951 { \
952 .name = __stringify(_name), \
953 .mode = S_IRUGO, \
954 .ops = { \
955 .open = acpi_battery_##_name##_open_fs, \
956 .read = seq_read, \
957 .llseek = seq_lseek, \
958 .release = single_release, \
959 .owner = THIS_MODULE, \
960 }, \
961 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400962
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400963#define FILE_DESCRIPTION_RW(_name) \
964 { \
965 .name = __stringify(_name), \
966 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
967 .ops = { \
968 .open = acpi_battery_##_name##_open_fs, \
969 .read = seq_read, \
970 .llseek = seq_lseek, \
971 .write = acpi_battery_write_##_name, \
972 .release = single_release, \
973 .owner = THIS_MODULE, \
974 }, \
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400977static const struct battery_file {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400978 struct file_operations ops;
Al Virod161a132011-07-24 03:36:29 -0400979 umode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100980 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400981} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400982 FILE_DESCRIPTION_RO(info),
983 FILE_DESCRIPTION_RO(state),
984 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985};
986
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400987#undef FILE_DESCRIPTION_RO
988#undef FILE_DESCRIPTION_RW
989
Len Brown4be44fc2005-08-05 00:44:28 -0400990static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Len Brown4be44fc2005-08-05 00:44:28 -0400992 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400993 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Zhang Rui6d855fc2011-01-10 11:16:30 +0800995 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
996 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!acpi_device_dir(device)) {
998 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400999 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Alexey Starikovskiy78490d82007-05-11 13:18:55 -04001004 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001005 entry = proc_create_data(acpi_battery_file[i].name,
1006 acpi_battery_file[i].mode,
1007 acpi_device_dir(device),
1008 &acpi_battery_file[i].ops,
1009 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -04001010 if (!entry)
1011 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001016static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -04001018 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001019 if (!acpi_device_dir(device))
1020 return;
1021 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
1022 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001025 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
1026 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001029#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +04001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/* --------------------------------------------------------------------------
1032 Driver Interface
1033 -------------------------------------------------------------------------- */
1034
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001035static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001037 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +08001038 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -04001041 return;
Zhang Rui153e5002010-07-07 09:11:57 +08001042 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +01001043 if (event == ACPI_BATTERY_NOTIFY_INFO)
1044 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001045 acpi_battery_update(battery);
1046 acpi_bus_generate_proc_event(device, event,
1047 acpi_battery_present(battery));
1048 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001049 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +03001050 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -08001051 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +08001052 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +00001053 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Kyle McMartin25be5822011-03-22 16:19:50 -04001056static int battery_notify(struct notifier_block *nb,
1057 unsigned long mode, void *_unused)
1058{
1059 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1060 pm_nb);
1061 switch (mode) {
Lan Tianyud5a59112011-06-30 11:33:40 +08001062 case PM_POST_HIBERNATION:
Kyle McMartin25be5822011-03-22 16:19:50 -04001063 case PM_POST_SUSPEND:
Lan Tianyu6e17fb62011-06-30 11:33:58 +08001064 if (battery->bat.dev) {
1065 sysfs_remove_battery(battery);
1066 sysfs_add_battery(battery);
1067 }
Kyle McMartin25be5822011-03-22 16:19:50 -04001068 break;
1069 }
1070
1071 return 0;
1072}
1073
Lan Tianyu290a6992014-01-06 22:50:37 +08001074static struct dmi_system_id bat_dmi_table[] = {
1075 {
1076 .ident = "NEC LZ750/LS",
1077 .matches = {
1078 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1079 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1080 },
1081 },
1082 {},
1083};
1084
Lan Tianyu3fef2d52014-07-07 15:47:12 +08001085/*
1086 * Some machines'(E,G Lenovo Z480) ECs are not stable
1087 * during boot up and this causes battery driver fails to be
1088 * probed due to failure of getting battery information
1089 * from EC sometimes. After several retries, the operation
1090 * may work. So add retry code here and 20ms sleep between
1091 * every retries.
1092 */
1093static int acpi_battery_update_retry(struct acpi_battery *battery)
1094{
1095 int retry, ret;
1096
1097 for (retry = 5; retry; retry--) {
1098 ret = acpi_battery_update(battery);
1099 if (!ret)
1100 break;
1101
1102 msleep(20);
1103 }
1104 return ret;
1105}
1106
Len Brown4be44fc2005-08-05 00:44:28 -04001107static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Len Brown4be44fc2005-08-05 00:44:28 -04001109 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001110 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +04001111 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001113 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -08001114 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -04001117 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1119 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001120 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001121 mutex_init(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001122 mutex_init(&battery->sysfs_lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +04001123 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
1124 "_BIX", &handle)))
1125 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Lan Tianyu3fef2d52014-07-07 15:47:12 +08001126
1127 result = acpi_battery_update_retry(battery);
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +01001128 if (result)
1129 goto fail;
Lan Tianyu3fef2d52014-07-07 15:47:12 +08001130
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001131#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001133#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001134 if (result) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001135#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001137#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001138 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Kyle McMartin25be5822011-03-22 16:19:50 -04001140
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001141 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1142 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1143 device->status.battery_present ? "present" : "absent");
1144
Kyle McMartin25be5822011-03-22 16:19:50 -04001145 battery->pm_nb.notifier_call = battery_notify;
1146 register_pm_notifier(&battery->pm_nb);
1147
Patrick Mocheld550d982006-06-27 00:41:40 -04001148 return result;
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001149
1150fail:
1151 sysfs_remove_battery(battery);
1152 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001153 mutex_destroy(&battery->sysfs_lock);
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001154 kfree(battery);
1155 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001158static int acpi_battery_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Len Brown4be44fc2005-08-05 00:44:28 -04001160 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001163 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001164 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001165 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001166#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001168#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001169 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001170 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001171 mutex_destroy(&battery->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001176#ifdef CONFIG_PM_SLEEP
Jiri Kosina34c44152006-10-10 14:20:41 -07001177/* this is needed to learn about changes made in suspended state */
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001178static int acpi_battery_resume(struct device *dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001179{
1180 struct acpi_battery *battery;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001181
1182 if (!dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001183 return -EINVAL;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001184
1185 battery = acpi_driver_data(to_acpi_device(dev));
1186 if (!battery)
1187 return -EINVAL;
1188
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001189 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001190 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001191 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001192}
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001193#endif
Jiri Kosina34c44152006-10-10 14:20:41 -07001194
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001195static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1196
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001197static struct acpi_driver acpi_battery_driver = {
1198 .name = "battery",
1199 .class = ACPI_BATTERY_CLASS,
1200 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001201 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001202 .ops = {
1203 .add = acpi_battery_add,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001204 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001205 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001206 },
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001207 .drv.pm = &acpi_battery_pm,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001208};
1209
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001210static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001212 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001213 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001214#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001215 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001217 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001218#endif
Lan Tianyu290a6992014-01-06 22:50:37 +08001219 if (dmi_check_system(bat_dmi_table))
1220 battery_bix_broken_package = 1;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001221 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001222#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001223 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001224#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001227 return;
1228}
1229
1230static int __init acpi_battery_init(void)
1231{
1232 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Len Brown4be44fc2005-08-05 00:44:28 -04001236static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001239#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001240 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001241#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244module_init(acpi_battery_init);
1245module_exit(acpi_battery_exit);