blob: 19bc440820e64d80635e8a2658c26c99f27e3f65 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/types.h>
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040028#include <linux/jiffies.h>
Arjan van de Ven0f66af52009-01-10 14:19:05 -050029#include <linux/async.h>
Hector Martinbc76f902009-08-06 15:57:48 -070030#include <linux/dmi.h>
Alexander Mezinf43691c2014-06-04 02:01:23 +070031#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Kyle McMartin25be5822011-03-22 16:19:50 -040033#include <linux/suspend.h>
Kamil Iskra4000e622012-11-16 22:28:58 +010034#include <asm/unaligned.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040035
Lan Tianyu3a670cc2014-05-04 11:07:25 +080036#ifdef CONFIG_ACPI_PROCFS_POWER
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Lan Tianyu3a670cc2014-05-04 11:07:25 +080040#endif
41
Lv Zheng8b484632013-12-03 08:49:16 +080042#include <linux/acpi.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040043#include <linux/power_supply.h>
44
Alexander Mezinf03be352014-03-12 00:58:46 +070045#include "battery.h"
46
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Lan Tianyuae6f6182011-06-30 11:32:40 +080053/* Battery power unit: 0 means mW, 1 means mA */
54#define ACPI_BATTERY_POWER_UNIT_MA 1
55
Zhang Rui1ac5aaa2014-05-28 15:23:36 +080056#define ACPI_BATTERY_STATE_DISCHARGING 0x1
57#define ACPI_BATTERY_STATE_CHARGING 0x2
58#define ACPI_BATTERY_STATE_CRITICAL 0x4
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030061
Len Brownf52fd662007-02-12 22:42:12 -050062ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Len Brownf52fd662007-02-12 22:42:12 -050064MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040065MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050066MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067MODULE_LICENSE("GPL");
68
Luis Henriqueseca21d912015-05-11 22:49:05 +010069static async_cookie_t async_cookie;
Hans de Goedebc39fbc2017-04-19 14:02:09 +020070static bool battery_driver_registered;
Lan Tianyua90b4032014-01-06 22:50:37 +080071static int battery_bix_broken_package;
Alexander Mezinf43691c2014-06-04 02:01:23 +070072static int battery_notification_delay_ms;
Kai-Heng Fengc68f0672017-11-21 03:33:06 -050073static int battery_full_discharging;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040074static unsigned int cache_time = 1000;
75module_param(cache_time, uint, 0644);
76MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030077
Lan Tianyu3a670cc2014-05-04 11:07:25 +080078#ifdef CONFIG_ACPI_PROCFS_POWER
79extern struct proc_dir_entry *acpi_lock_battery_dir(void);
80extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
81
82enum acpi_battery_files {
83 info_tag = 0,
84 state_tag,
85 alarm_tag,
86 ACPI_BATTERY_NUMFILES,
87};
88
89#endif
90
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040091static const struct acpi_device_id battery_device_ids[] = {
92 {"PNP0C0A", 0},
93 {"", 0},
94};
95
96MODULE_DEVICE_TABLE(acpi, battery_device_ids);
97
Hans de Goededccfae62017-04-19 14:02:10 +020098/* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
99static const char * const acpi_battery_blacklist[] = {
100 "INT33F4", /* X-Powers AXP288 PMIC */
101};
102
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400103enum {
104 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400105 ACPI_BATTERY_XINFO_PRESENT,
Zhang Rui557d5862010-10-22 10:02:06 +0800106 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Kamil Iskra4000e622012-11-16 22:28:58 +0100107 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
108 switches between mWh and mAh depending on whether the system
109 is running on battery or not. When mAh is the unit, most
110 reported values are incorrect and need to be adjusted by
111 10000/design_voltage. Verified on x201, t410, t410s, and x220.
112 Pre-2010 and 2012 models appear to always report in mWh and
113 are thus unaffected (tested with t42, t61, t500, x200, x300,
114 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
115 the 2011 models that fixes the issue (tested on x220 with a
116 post-1.29 BIOS), but as of Nov. 2012, no such update is
117 available for the 2010 models. */
118 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400119};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400120
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400121struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400122 struct mutex lock;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300123 struct mutex sysfs_lock;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100124 struct power_supply *bat;
125 struct power_supply_desc bat_desc;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400126 struct acpi_device *device;
Kyle McMartin25be5822011-03-22 16:19:50 -0400127 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400128 unsigned long update_time;
Lan Tianyu016d5ba2013-07-30 14:00:42 +0200129 int revision;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400130 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400131 int capacity_now;
132 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400133 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400134 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400135 int technology;
136 int design_voltage;
137 int design_capacity_warning;
138 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400139 int cycle_count;
140 int measurement_accuracy;
141 int max_sampling_time;
142 int min_sampling_time;
143 int max_averaging_interval;
144 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400145 int capacity_granularity_1;
146 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400147 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400148 char model_number[32];
149 char serial_number[32];
150 char type[32];
151 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400152 int state;
153 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100157#define to_acpi_battery(x) power_supply_get_drvdata(x)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400158
Andy Shevchenkoefd941f2013-03-11 09:17:06 +0000159static inline int acpi_battery_present(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400160{
161 return battery->device->status.battery_present;
162}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400163
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400164static int acpi_battery_technology(struct acpi_battery *battery)
165{
166 if (!strcasecmp("NiCd", battery->type))
167 return POWER_SUPPLY_TECHNOLOGY_NiCd;
168 if (!strcasecmp("NiMH", battery->type))
169 return POWER_SUPPLY_TECHNOLOGY_NiMH;
170 if (!strcasecmp("LION", battery->type))
171 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300172 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300173 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400174 if (!strcasecmp("LiP", battery->type))
175 return POWER_SUPPLY_TECHNOLOGY_LIPO;
176 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
177}
178
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300179static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400180
Richard Hughes56f382a2009-01-25 15:05:50 +0000181static int acpi_battery_is_charged(struct acpi_battery *battery)
182{
Zhang Rui1ac5aaa2014-05-28 15:23:36 +0800183 /* charging, discharging or critical low */
Richard Hughes56f382a2009-01-25 15:05:50 +0000184 if (battery->state != 0)
185 return 0;
186
187 /* battery not reporting charge */
188 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
189 battery->capacity_now == 0)
190 return 0;
191
192 /* good batteries update full_charge as the batteries degrade */
193 if (battery->full_charge_capacity == battery->capacity_now)
194 return 1;
195
196 /* fallback to using design values for broken batteries */
197 if (battery->design_capacity == battery->capacity_now)
198 return 1;
199
200 /* we don't do any sort of metric based on percentages */
201 return 0;
202}
203
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400204static int acpi_battery_get_property(struct power_supply *psy,
205 enum power_supply_property psp,
206 union power_supply_propval *val)
207{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200208 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400209 struct acpi_battery *battery = to_acpi_battery(psy);
210
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300211 if (acpi_battery_present(battery)) {
212 /* run battery update only if it is present */
213 acpi_battery_get_state(battery);
214 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400215 return -ENODEV;
216 switch (psp) {
217 case POWER_SUPPLY_PROP_STATUS:
Kai-Heng Fengc68f0672017-11-21 03:33:06 -0500218 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING) {
219 if (battery_full_discharging && battery->rate_now == 0)
220 val->intval = POWER_SUPPLY_STATUS_FULL;
221 else
222 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
223 } else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400224 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000225 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400226 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800227 else
228 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400229 break;
230 case POWER_SUPPLY_PROP_PRESENT:
231 val->intval = acpi_battery_present(battery);
232 break;
233 case POWER_SUPPLY_PROP_TECHNOLOGY:
234 val->intval = acpi_battery_technology(battery);
235 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400236 case POWER_SUPPLY_PROP_CYCLE_COUNT:
237 val->intval = battery->cycle_count;
238 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400239 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200240 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
241 ret = -ENODEV;
242 else
243 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400244 break;
245 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200246 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
247 ret = -ENODEV;
248 else
249 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400250 break;
251 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400252 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200253 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
254 ret = -ENODEV;
255 else
256 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400257 break;
258 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
259 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200260 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
261 ret = -ENODEV;
262 else
263 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400264 break;
265 case POWER_SUPPLY_PROP_CHARGE_FULL:
266 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200267 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
268 ret = -ENODEV;
269 else
270 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400271 break;
272 case POWER_SUPPLY_PROP_CHARGE_NOW:
273 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200274 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
275 ret = -ENODEV;
276 else
277 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400278 break;
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700279 case POWER_SUPPLY_PROP_CAPACITY:
280 if (battery->capacity_now && battery->full_charge_capacity)
281 val->intval = battery->capacity_now * 100/
282 battery->full_charge_capacity;
283 else
284 val->intval = 0;
285 break;
Zhang Rui1ac5aaa2014-05-28 15:23:36 +0800286 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
287 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
288 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
289 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
290 (battery->capacity_now <= battery->alarm))
291 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
292 else if (acpi_battery_is_charged(battery))
293 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
294 else
295 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
296 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400297 case POWER_SUPPLY_PROP_MODEL_NAME:
298 val->strval = battery->model_number;
299 break;
300 case POWER_SUPPLY_PROP_MANUFACTURER:
301 val->strval = battery->oem_info;
302 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100303 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
304 val->strval = battery->serial_number;
305 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400306 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200307 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400308 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200309 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400310}
311
312static enum power_supply_property charge_battery_props[] = {
313 POWER_SUPPLY_PROP_STATUS,
314 POWER_SUPPLY_PROP_PRESENT,
315 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400316 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400317 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
318 POWER_SUPPLY_PROP_VOLTAGE_NOW,
319 POWER_SUPPLY_PROP_CURRENT_NOW,
320 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
321 POWER_SUPPLY_PROP_CHARGE_FULL,
322 POWER_SUPPLY_PROP_CHARGE_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700323 POWER_SUPPLY_PROP_CAPACITY,
Zhang Rui1ac5aaa2014-05-28 15:23:36 +0800324 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400325 POWER_SUPPLY_PROP_MODEL_NAME,
326 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100327 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400328};
329
330static enum power_supply_property energy_battery_props[] = {
331 POWER_SUPPLY_PROP_STATUS,
332 POWER_SUPPLY_PROP_PRESENT,
333 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400334 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400335 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
336 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400337 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400338 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
339 POWER_SUPPLY_PROP_ENERGY_FULL,
340 POWER_SUPPLY_PROP_ENERGY_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700341 POWER_SUPPLY_PROP_CAPACITY,
Zhang Rui1ac5aaa2014-05-28 15:23:36 +0800342 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400343 POWER_SUPPLY_PROP_MODEL_NAME,
344 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100345 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* --------------------------------------------------------------------------
349 Battery Management
350 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400351struct acpi_offsets {
352 size_t offset; /* offset inside struct acpi_sbs_battery */
353 u8 mode; /* int or string? */
354};
355
Mathias Krausea4658782015-06-13 14:26:53 +0200356static const struct acpi_offsets state_offsets[] = {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400357 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400358 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400359 {offsetof(struct acpi_battery, capacity_now), 0},
360 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400361};
362
Mathias Krausea4658782015-06-13 14:26:53 +0200363static const struct acpi_offsets info_offsets[] = {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400364 {offsetof(struct acpi_battery, power_unit), 0},
365 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400366 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400367 {offsetof(struct acpi_battery, technology), 0},
368 {offsetof(struct acpi_battery, design_voltage), 0},
369 {offsetof(struct acpi_battery, design_capacity_warning), 0},
370 {offsetof(struct acpi_battery, design_capacity_low), 0},
371 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
372 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
373 {offsetof(struct acpi_battery, model_number), 1},
374 {offsetof(struct acpi_battery, serial_number), 1},
375 {offsetof(struct acpi_battery, type), 1},
376 {offsetof(struct acpi_battery, oem_info), 1},
377};
378
Mathias Krausea4658782015-06-13 14:26:53 +0200379static const struct acpi_offsets extended_info_offsets[] = {
Lan Tianyu016d5ba2013-07-30 14:00:42 +0200380 {offsetof(struct acpi_battery, revision), 0},
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400381 {offsetof(struct acpi_battery, power_unit), 0},
382 {offsetof(struct acpi_battery, design_capacity), 0},
383 {offsetof(struct acpi_battery, full_charge_capacity), 0},
384 {offsetof(struct acpi_battery, technology), 0},
385 {offsetof(struct acpi_battery, design_voltage), 0},
386 {offsetof(struct acpi_battery, design_capacity_warning), 0},
387 {offsetof(struct acpi_battery, design_capacity_low), 0},
388 {offsetof(struct acpi_battery, cycle_count), 0},
389 {offsetof(struct acpi_battery, measurement_accuracy), 0},
390 {offsetof(struct acpi_battery, max_sampling_time), 0},
391 {offsetof(struct acpi_battery, min_sampling_time), 0},
392 {offsetof(struct acpi_battery, max_averaging_interval), 0},
393 {offsetof(struct acpi_battery, min_averaging_interval), 0},
394 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
395 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
396 {offsetof(struct acpi_battery, model_number), 1},
397 {offsetof(struct acpi_battery, serial_number), 1},
398 {offsetof(struct acpi_battery, type), 1},
399 {offsetof(struct acpi_battery, oem_info), 1},
400};
401
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400402static int extract_package(struct acpi_battery *battery,
403 union acpi_object *package,
Mathias Krausea4658782015-06-13 14:26:53 +0200404 const struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300405{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300406 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400407 union acpi_object *element;
408 if (package->type != ACPI_TYPE_PACKAGE)
409 return -EFAULT;
410 for (i = 0; i < num; ++i) {
411 if (package->package.count <= i)
412 return -EFAULT;
413 element = &package->package.elements[i];
414 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300415 u8 *ptr = (u8 *)battery + offsets[i].offset;
416 if (element->type == ACPI_TYPE_STRING ||
417 element->type == ACPI_TYPE_BUFFER)
418 strncpy(ptr, element->string.pointer, 32);
419 else if (element->type == ACPI_TYPE_INTEGER) {
420 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800421 sizeof(u64));
422 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400423 } else
424 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400425 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400426 int *x = (int *)((u8 *)battery + offsets[i].offset);
427 *x = (element->type == ACPI_TYPE_INTEGER) ?
428 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300429 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300430 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300431 return 0;
432}
433
434static int acpi_battery_get_status(struct acpi_battery *battery)
435{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400436 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300437 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
438 return -ENODEV;
439 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400440 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300441}
442
Dave Lambley2d09af42016-11-04 01:05:40 +0000443
444static int extract_battery_info(const int use_bix,
445 struct acpi_battery *battery,
446 const struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400448 int result = -EFAULT;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400449
Dave Lambley2d09af42016-11-04 01:05:40 +0000450 if (use_bix && battery_bix_broken_package)
451 result = extract_package(battery, buffer->pointer,
Lan Tianyua90b4032014-01-06 22:50:37 +0800452 extended_info_offsets + 1,
453 ARRAY_SIZE(extended_info_offsets) - 1);
Dave Lambley2d09af42016-11-04 01:05:40 +0000454 else if (use_bix)
455 result = extract_package(battery, buffer->pointer,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400456 extended_info_offsets,
457 ARRAY_SIZE(extended_info_offsets));
458 else
Dave Lambley2d09af42016-11-04 01:05:40 +0000459 result = extract_package(battery, buffer->pointer,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400460 info_offsets, ARRAY_SIZE(info_offsets));
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
Dave Lambley2d09af42016-11-04 01:05:40 +0000481static int acpi_battery_get_info(struct acpi_battery *battery)
482{
483 const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
484 int use_bix;
485 int result = -ENODEV;
486
487 if (!acpi_battery_present(battery))
488 return 0;
489
490
491 for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
492 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
493 acpi_status status = AE_ERROR;
494
495 mutex_lock(&battery->lock);
496 status = acpi_evaluate_object(battery->device->handle,
497 use_bix ? "_BIX":"_BIF",
498 NULL, &buffer);
499 mutex_unlock(&battery->lock);
500
501 if (ACPI_FAILURE(status)) {
502 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
503 use_bix ? "_BIX":"_BIF"));
504 } else {
505 result = extract_battery_info(use_bix,
506 battery,
507 &buffer);
508
509 kfree(buffer.pointer);
510 break;
511 }
512 }
513
514 if (!result && !use_bix && xinfo)
515 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
516
517 return result;
518}
519
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300520static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Len Brown4be44fc2005-08-05 00:44:28 -0400522 int result = 0;
523 acpi_status status = 0;
524 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300526 if (!acpi_battery_present(battery))
527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400529 if (battery->update_time &&
530 time_before(jiffies, battery->update_time +
531 msecs_to_jiffies(cache_time)))
532 return 0;
533
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400534 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400535 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400536 NULL, &buffer);
537 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400540 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400541 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400543
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400544 result = extract_package(battery, buffer.pointer,
545 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400546 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400547 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700548
Lan Tianyu55003b22011-06-30 11:33:12 +0800549 /* For buggy DSDTs that report negative 16-bit values for either
550 * charging or discharging current and/or report 0 as 65536
551 * due to bad math.
552 */
553 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
554 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
555 (s16)(battery->rate_now) < 0) {
Hector Martinbc76f902009-08-06 15:57:48 -0700556 battery->rate_now = abs((s16)battery->rate_now);
Martin Kepplinger92375162015-03-13 00:48:17 +0100557 printk_once(KERN_WARNING FW_BUG
558 "battery: (dis)charge rate invalid.\n");
Lan Tianyu55003b22011-06-30 11:33:12 +0800559 }
Hector Martinbc76f902009-08-06 15:57:48 -0700560
Zhang Rui557d5862010-10-22 10:02:06 +0800561 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
562 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
563 battery->capacity_now = (battery->capacity_now *
564 battery->full_charge_capacity) / 100;
Kamil Iskra4000e622012-11-16 22:28:58 +0100565 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
566 battery->power_unit && battery->design_voltage) {
567 battery->capacity_now = battery->capacity_now *
568 10000 / battery->design_voltage;
569 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400573static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Len Brown4be44fc2005-08-05 00:44:28 -0400575 acpi_status status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400577 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400578 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300579 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400581 mutex_lock(&battery->lock);
Jiang Liu0db98202013-06-29 00:24:39 +0800582 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
583 battery->alarm);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400584 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400587 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400589 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300593static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300595 /* See if alarms are supported, and if so, set default */
Jiang Liu952c63e2013-06-29 00:24:38 +0800596 if (!acpi_has_method(battery->device->handle, "_BTP")) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400597 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400600 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400601 if (!battery->alarm)
602 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400603 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Andrey Borzenkov508df922007-10-28 12:50:09 +0300606static ssize_t acpi_battery_alarm_show(struct device *dev,
607 struct device_attribute *attr,
608 char *buf)
609{
610 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
611 return sprintf(buf, "%d\n", battery->alarm * 1000);
612}
613
614static ssize_t acpi_battery_alarm_store(struct device *dev,
615 struct device_attribute *attr,
616 const char *buf, size_t count)
617{
618 unsigned long x;
619 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
Luis G.F47a08c82014-01-21 15:40:43 +0100620 if (sscanf(buf, "%lu\n", &x) == 1)
Andrey Borzenkov508df922007-10-28 12:50:09 +0300621 battery->alarm = x/1000;
622 if (acpi_battery_present(battery))
623 acpi_battery_set_alarm(battery);
624 return count;
625}
626
Bhumika Goyal82d2b612017-08-21 17:13:07 +0530627static const struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700628 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300629 .show = acpi_battery_alarm_show,
630 .store = acpi_battery_alarm_store,
631};
632
633static int sysfs_add_battery(struct acpi_battery *battery)
634{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100635 struct power_supply_config psy_cfg = { .drv_data = battery, };
Andrey Borzenkov508df922007-10-28 12:50:09 +0300636
Lan Tianyuae6f6182011-06-30 11:32:40 +0800637 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100638 battery->bat_desc.properties = charge_battery_props;
639 battery->bat_desc.num_properties =
Andrey Borzenkov508df922007-10-28 12:50:09 +0300640 ARRAY_SIZE(charge_battery_props);
641 } else {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100642 battery->bat_desc.properties = energy_battery_props;
643 battery->bat_desc.num_properties =
Andrey Borzenkov508df922007-10-28 12:50:09 +0300644 ARRAY_SIZE(energy_battery_props);
645 }
646
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100647 battery->bat_desc.name = acpi_device_bid(battery->device);
648 battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
649 battery->bat_desc.get_property = acpi_battery_get_property;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300650
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100651 battery->bat = power_supply_register_no_ws(&battery->device->dev,
652 &battery->bat_desc, &psy_cfg);
Zhang Ruie0d1f092014-05-28 15:23:38 +0800653
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100654 if (IS_ERR(battery->bat)) {
655 int result = PTR_ERR(battery->bat);
656
657 battery->bat = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300658 return result;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100659 }
660 return device_create_file(&battery->bat->dev, &alarm_attr);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300661}
662
663static void sysfs_remove_battery(struct acpi_battery *battery)
664{
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300665 mutex_lock(&battery->sysfs_lock);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100666 if (!battery->bat) {
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300667 mutex_unlock(&battery->sysfs_lock);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300668 return;
Lan Tianyu9c921c222011-06-30 11:34:12 +0800669 }
670
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100671 device_remove_file(&battery->bat->dev, &alarm_attr);
672 power_supply_unregister(battery->bat);
673 battery->bat = NULL;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300674 mutex_unlock(&battery->sysfs_lock);
Hector Martinbc76f902009-08-06 15:57:48 -0700675}
676
Kamil Iskra4000e622012-11-16 22:28:58 +0100677static void find_battery(const struct dmi_header *dm, void *private)
678{
679 struct acpi_battery *battery = (struct acpi_battery *)private;
680 /* Note: the hardcoded offsets below have been extracted from
681 the source code of dmidecode. */
682 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
683 const u8 *dmi_data = (const u8 *)(dm + 1);
684 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
685 if (dm->length >= 18)
686 dmi_capacity *= dmi_data[17];
687 if (battery->design_capacity * battery->design_voltage / 1000
688 != dmi_capacity &&
689 battery->design_capacity * 10 == dmi_capacity)
690 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
691 &battery->flags);
692 }
693}
694
Zhang Rui557d5862010-10-22 10:02:06 +0800695/*
696 * According to the ACPI spec, some kinds of primary batteries can
697 * report percentage battery remaining capacity directly to OS.
698 * In this case, it reports the Last Full Charged Capacity == 100
699 * and BatteryPresentRate == 0xFFFFFFFF.
700 *
701 * Now we found some battery reports percentage remaining capacity
702 * even if it's rechargeable.
703 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
704 *
705 * Handle this correctly so that they won't break userspace.
706 */
Lan Tianyu7b786222011-06-30 11:33:27 +0800707static void acpi_battery_quirks(struct acpi_battery *battery)
Zhang Rui557d5862010-10-22 10:02:06 +0800708{
709 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
Nicholas Mazzuca0f4c6542013-05-08 23:11:15 +0000710 return;
Zhang Rui557d5862010-10-22 10:02:06 +0800711
Nicholas Mazzuca0f4c6542013-05-08 23:11:15 +0000712 if (battery->full_charge_capacity == 100 &&
713 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
714 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
Zhang Rui557d5862010-10-22 10:02:06 +0800715 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
716 battery->full_charge_capacity = battery->design_capacity;
717 battery->capacity_now = (battery->capacity_now *
718 battery->full_charge_capacity) / 100;
719 }
Kamil Iskra4000e622012-11-16 22:28:58 +0100720
721 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
Nicholas Mazzuca0f4c6542013-05-08 23:11:15 +0000722 return;
Kamil Iskra4000e622012-11-16 22:28:58 +0100723
724 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
725 const char *s;
726 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
Rasmus Villemoesffd8a732014-09-16 22:51:24 +0200727 if (s && !strncasecmp(s, "ThinkPad", 8)) {
Kamil Iskra4000e622012-11-16 22:28:58 +0100728 dmi_walk(find_battery, battery);
729 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
730 &battery->flags) &&
731 battery->design_voltage) {
732 battery->design_capacity =
733 battery->design_capacity *
734 10000 / battery->design_voltage;
735 battery->full_charge_capacity =
736 battery->full_charge_capacity *
737 10000 / battery->design_voltage;
738 battery->design_capacity_warning =
739 battery->design_capacity_warning *
740 10000 / battery->design_voltage;
741 battery->capacity_now = battery->capacity_now *
742 10000 / battery->design_voltage;
743 }
744 }
745 }
Zhang Rui557d5862010-10-22 10:02:06 +0800746}
747
Lan Tianyu9e50bc12014-05-04 14:07:06 +0800748static int acpi_battery_update(struct acpi_battery *battery, bool resume)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500749{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300750 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500751 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300752 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300753 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300754 if (!acpi_battery_present(battery)) {
755 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500756 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300757 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300758 }
Lan Tianyu9e50bc12014-05-04 14:07:06 +0800759
760 if (resume)
761 return 0;
762
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300763 if (!battery->update_time ||
764 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500765 result = acpi_battery_get_info(battery);
766 if (result)
767 return result;
768 acpi_battery_init_alarm(battery);
769 }
Carlos Garnacho12c78ca2016-08-10 17:24:15 +0200770
771 result = acpi_battery_get_state(battery);
772 if (result)
773 return result;
774 acpi_battery_quirks(battery);
775
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100776 if (!battery->bat) {
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +0100777 result = sysfs_add_battery(battery);
778 if (result)
779 return result;
780 }
Zhang Ruie0d1f092014-05-28 15:23:38 +0800781
782 /*
783 * Wakeup the system if battery is critical low
784 * or lower than the alarm level
785 */
786 if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
787 (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
788 (battery->capacity_now <= battery->alarm)))
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200789 acpi_pm_wakeup_event(&battery->device->dev);
Zhang Ruie0d1f092014-05-28 15:23:38 +0800790
Zhang Rui557d5862010-10-22 10:02:06 +0800791 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500792}
793
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100794static void acpi_battery_refresh(struct acpi_battery *battery)
795{
Andy Whitcroftc5971452012-05-03 14:48:26 +0100796 int power_unit;
797
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100798 if (!battery->bat)
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100799 return;
800
Andy Whitcroftc5971452012-05-03 14:48:26 +0100801 power_unit = battery->power_unit;
802
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100803 acpi_battery_get_info(battery);
Andy Whitcroftc5971452012-05-03 14:48:26 +0100804
805 if (power_unit == battery->power_unit)
806 return;
807
808 /* The battery has changed its reporting units. */
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100809 sysfs_remove_battery(battery);
810 sysfs_add_battery(battery);
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/* --------------------------------------------------------------------------
Lan Tianyu3a670cc2014-05-04 11:07:25 +0800814 FS Interface (/proc)
815 -------------------------------------------------------------------------- */
816
817#ifdef CONFIG_ACPI_PROCFS_POWER
818static struct proc_dir_entry *acpi_battery_dir;
819
Mathias Krause27059b92015-06-13 14:26:54 +0200820static const char *acpi_battery_units(const struct acpi_battery *battery)
821{
822 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
823 "mA" : "mW";
824}
825
Lan Tianyu3a670cc2014-05-04 11:07:25 +0800826static int acpi_battery_print_info(struct seq_file *seq, int result)
827{
828 struct acpi_battery *battery = seq->private;
829
830 if (result)
831 goto end;
832
833 seq_printf(seq, "present: %s\n",
834 acpi_battery_present(battery) ? "yes" : "no");
835 if (!acpi_battery_present(battery))
836 goto end;
837 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
838 seq_printf(seq, "design capacity: unknown\n");
839 else
840 seq_printf(seq, "design capacity: %d %sh\n",
841 battery->design_capacity,
842 acpi_battery_units(battery));
843
844 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
845 seq_printf(seq, "last full capacity: unknown\n");
846 else
847 seq_printf(seq, "last full capacity: %d %sh\n",
848 battery->full_charge_capacity,
849 acpi_battery_units(battery));
850
851 seq_printf(seq, "battery technology: %srechargeable\n",
852 (!battery->technology)?"non-":"");
853
854 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
855 seq_printf(seq, "design voltage: unknown\n");
856 else
857 seq_printf(seq, "design voltage: %d mV\n",
858 battery->design_voltage);
859 seq_printf(seq, "design capacity warning: %d %sh\n",
860 battery->design_capacity_warning,
861 acpi_battery_units(battery));
862 seq_printf(seq, "design capacity low: %d %sh\n",
863 battery->design_capacity_low,
864 acpi_battery_units(battery));
865 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
866 seq_printf(seq, "capacity granularity 1: %d %sh\n",
867 battery->capacity_granularity_1,
868 acpi_battery_units(battery));
869 seq_printf(seq, "capacity granularity 2: %d %sh\n",
870 battery->capacity_granularity_2,
871 acpi_battery_units(battery));
872 seq_printf(seq, "model number: %s\n", battery->model_number);
873 seq_printf(seq, "serial number: %s\n", battery->serial_number);
874 seq_printf(seq, "battery type: %s\n", battery->type);
875 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
876 end:
877 if (result)
878 seq_printf(seq, "ERROR: Unable to read battery info\n");
879 return result;
880}
881
882static int acpi_battery_print_state(struct seq_file *seq, int result)
883{
884 struct acpi_battery *battery = seq->private;
885
886 if (result)
887 goto end;
888
889 seq_printf(seq, "present: %s\n",
890 acpi_battery_present(battery) ? "yes" : "no");
891 if (!acpi_battery_present(battery))
892 goto end;
893
894 seq_printf(seq, "capacity state: %s\n",
895 (battery->state & 0x04) ? "critical" : "ok");
896 if ((battery->state & 0x01) && (battery->state & 0x02))
897 seq_printf(seq,
898 "charging state: charging/discharging\n");
899 else if (battery->state & 0x01)
900 seq_printf(seq, "charging state: discharging\n");
901 else if (battery->state & 0x02)
902 seq_printf(seq, "charging state: charging\n");
903 else
904 seq_printf(seq, "charging state: charged\n");
905
906 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
907 seq_printf(seq, "present rate: unknown\n");
908 else
909 seq_printf(seq, "present rate: %d %s\n",
910 battery->rate_now, acpi_battery_units(battery));
911
912 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
913 seq_printf(seq, "remaining capacity: unknown\n");
914 else
915 seq_printf(seq, "remaining capacity: %d %sh\n",
916 battery->capacity_now, acpi_battery_units(battery));
917 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
918 seq_printf(seq, "present voltage: unknown\n");
919 else
920 seq_printf(seq, "present voltage: %d mV\n",
921 battery->voltage_now);
922 end:
923 if (result)
924 seq_printf(seq, "ERROR: Unable to read battery state\n");
925
926 return result;
927}
928
929static int acpi_battery_print_alarm(struct seq_file *seq, int result)
930{
931 struct acpi_battery *battery = seq->private;
932
933 if (result)
934 goto end;
935
936 if (!acpi_battery_present(battery)) {
937 seq_printf(seq, "present: no\n");
938 goto end;
939 }
940 seq_printf(seq, "alarm: ");
941 if (!battery->alarm)
942 seq_printf(seq, "unsupported\n");
943 else
944 seq_printf(seq, "%u %sh\n", battery->alarm,
945 acpi_battery_units(battery));
946 end:
947 if (result)
948 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
949 return result;
950}
951
952static ssize_t acpi_battery_write_alarm(struct file *file,
953 const char __user * buffer,
954 size_t count, loff_t * ppos)
955{
956 int result = 0;
957 char alarm_string[12] = { '\0' };
958 struct seq_file *m = file->private_data;
959 struct acpi_battery *battery = m->private;
960
961 if (!battery || (count > sizeof(alarm_string) - 1))
962 return -EINVAL;
963 if (!acpi_battery_present(battery)) {
964 result = -ENODEV;
965 goto end;
966 }
967 if (copy_from_user(alarm_string, buffer, count)) {
968 result = -EFAULT;
969 goto end;
970 }
971 alarm_string[count] = '\0';
Christoph Jaeger3d915892014-06-13 21:49:58 +0200972 if (kstrtoint(alarm_string, 0, &battery->alarm)) {
973 result = -EINVAL;
974 goto end;
975 }
Lan Tianyu3a670cc2014-05-04 11:07:25 +0800976 result = acpi_battery_set_alarm(battery);
977 end:
978 if (!result)
979 return count;
980 return result;
981}
982
983typedef int(*print_func)(struct seq_file *seq, int result);
984
985static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
986 acpi_battery_print_info,
987 acpi_battery_print_state,
988 acpi_battery_print_alarm,
989};
990
991static int acpi_battery_read(int fid, struct seq_file *seq)
992{
993 struct acpi_battery *battery = seq->private;
Lan Tianyu9e50bc12014-05-04 14:07:06 +0800994 int result = acpi_battery_update(battery, false);
Lan Tianyu3a670cc2014-05-04 11:07:25 +0800995 return acpi_print_funcs[fid](seq, result);
996}
997
998#define DECLARE_FILE_FUNCTIONS(_name) \
999static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
1000{ \
1001 return acpi_battery_read(_name##_tag, seq); \
1002} \
1003static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
1004{ \
1005 return single_open(file, acpi_battery_read_##_name, PDE_DATA(inode)); \
1006}
1007
1008DECLARE_FILE_FUNCTIONS(info);
1009DECLARE_FILE_FUNCTIONS(state);
1010DECLARE_FILE_FUNCTIONS(alarm);
1011
1012#undef DECLARE_FILE_FUNCTIONS
1013
1014#define FILE_DESCRIPTION_RO(_name) \
1015 { \
1016 .name = __stringify(_name), \
1017 .mode = S_IRUGO, \
1018 .ops = { \
1019 .open = acpi_battery_##_name##_open_fs, \
1020 .read = seq_read, \
1021 .llseek = seq_lseek, \
1022 .release = single_release, \
1023 .owner = THIS_MODULE, \
1024 }, \
1025 }
1026
1027#define FILE_DESCRIPTION_RW(_name) \
1028 { \
1029 .name = __stringify(_name), \
1030 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
1031 .ops = { \
1032 .open = acpi_battery_##_name##_open_fs, \
1033 .read = seq_read, \
1034 .llseek = seq_lseek, \
1035 .write = acpi_battery_write_##_name, \
1036 .release = single_release, \
1037 .owner = THIS_MODULE, \
1038 }, \
1039 }
1040
1041static const struct battery_file {
1042 struct file_operations ops;
1043 umode_t mode;
1044 const char *name;
1045} acpi_battery_file[] = {
1046 FILE_DESCRIPTION_RO(info),
1047 FILE_DESCRIPTION_RO(state),
1048 FILE_DESCRIPTION_RW(alarm),
1049};
1050
1051#undef FILE_DESCRIPTION_RO
1052#undef FILE_DESCRIPTION_RW
1053
1054static int acpi_battery_add_fs(struct acpi_device *device)
1055{
1056 struct proc_dir_entry *entry = NULL;
1057 int i;
1058
1059 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
1060 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1061 if (!acpi_device_dir(device)) {
1062 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1063 acpi_battery_dir);
1064 if (!acpi_device_dir(device))
1065 return -ENODEV;
1066 }
1067
1068 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
1069 entry = proc_create_data(acpi_battery_file[i].name,
1070 acpi_battery_file[i].mode,
1071 acpi_device_dir(device),
1072 &acpi_battery_file[i].ops,
1073 acpi_driver_data(device));
1074 if (!entry)
1075 return -ENODEV;
1076 }
1077 return 0;
1078}
1079
1080static void acpi_battery_remove_fs(struct acpi_device *device)
1081{
1082 int i;
1083 if (!acpi_device_dir(device))
1084 return;
1085 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
1086 remove_proc_entry(acpi_battery_file[i].name,
1087 acpi_device_dir(device));
1088
1089 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
1090 acpi_device_dir(device) = NULL;
1091}
1092
1093#endif
1094
1095/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 Driver Interface
1097 -------------------------------------------------------------------------- */
1098
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001099static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001101 struct acpi_battery *battery = acpi_driver_data(device);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001102 struct power_supply *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -04001105 return;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001106 old = battery->bat;
Alexander Mezinf43691c2014-06-04 02:01:23 +07001107 /*
1108 * On Acer Aspire V5-573G notifications are sometimes triggered too
1109 * early. For example, when AC is unplugged and notification is
1110 * triggered, battery state is still reported as "Full", and changes to
1111 * "Discharging" only after short delay, without any notification.
1112 */
1113 if (battery_notification_delay_ms > 0)
1114 msleep(battery_notification_delay_ms);
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +01001115 if (event == ACPI_BATTERY_NOTIFY_INFO)
1116 acpi_battery_refresh(battery);
Lan Tianyu9e50bc12014-05-04 14:07:06 +08001117 acpi_battery_update(battery, false);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001118 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +01001119 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +03001120 acpi_battery_present(battery));
Alexander Mezin411e0f72014-03-12 00:58:47 +07001121 acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -08001122 /* acpi_battery_update could remove power_supply object */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001123 if (old && battery->bat)
1124 power_supply_changed(battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Kyle McMartin25be5822011-03-22 16:19:50 -04001127static int battery_notify(struct notifier_block *nb,
1128 unsigned long mode, void *_unused)
1129{
1130 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1131 pm_nb);
Lan Tianyu9e50bc12014-05-04 14:07:06 +08001132 int result;
1133
Kyle McMartin25be5822011-03-22 16:19:50 -04001134 switch (mode) {
Lan Tianyud5a59112011-06-30 11:33:40 +08001135 case PM_POST_HIBERNATION:
Kyle McMartin25be5822011-03-22 16:19:50 -04001136 case PM_POST_SUSPEND:
Lan Tianyu9e50bc12014-05-04 14:07:06 +08001137 if (!acpi_battery_present(battery))
1138 return 0;
1139
Krzysztof Kozlowski31f7dc72015-04-14 22:24:13 +09001140 if (!battery->bat) {
Lan Tianyu9e50bc12014-05-04 14:07:06 +08001141 result = acpi_battery_get_info(battery);
1142 if (result)
1143 return result;
1144
1145 result = sysfs_add_battery(battery);
1146 if (result)
1147 return result;
1148 } else
1149 acpi_battery_refresh(battery);
1150
1151 acpi_battery_init_alarm(battery);
1152 acpi_battery_get_state(battery);
Kyle McMartin25be5822011-03-22 16:19:50 -04001153 break;
1154 }
1155
1156 return 0;
1157}
1158
Mathias Krause048d16d2015-06-13 14:26:55 +02001159static int __init
1160battery_bix_broken_package_quirk(const struct dmi_system_id *d)
Alexander Mezin3f5dc082014-06-04 02:01:22 +07001161{
1162 battery_bix_broken_package = 1;
1163 return 0;
1164}
1165
Mathias Krause048d16d2015-06-13 14:26:55 +02001166static int __init
1167battery_notification_delay_quirk(const struct dmi_system_id *d)
Alexander Mezinf43691c2014-06-04 02:01:23 +07001168{
1169 battery_notification_delay_ms = 1000;
1170 return 0;
1171}
1172
Kai-Heng Fengc68f0672017-11-21 03:33:06 -05001173static int __init battery_full_discharging_quirk(const struct dmi_system_id *d)
1174{
1175 battery_full_discharging = 1;
1176 return 0;
1177}
1178
Mathias Krause048d16d2015-06-13 14:26:55 +02001179static const struct dmi_system_id bat_dmi_table[] __initconst = {
Lan Tianyua90b4032014-01-06 22:50:37 +08001180 {
Alexander Mezin3f5dc082014-06-04 02:01:22 +07001181 .callback = battery_bix_broken_package_quirk,
Lan Tianyua90b4032014-01-06 22:50:37 +08001182 .ident = "NEC LZ750/LS",
1183 .matches = {
1184 DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1185 DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1186 },
1187 },
Alexander Mezinf43691c2014-06-04 02:01:23 +07001188 {
1189 .callback = battery_notification_delay_quirk,
1190 .ident = "Acer Aspire V5-573G",
1191 .matches = {
1192 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1193 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1194 },
1195 },
Kai-Heng Fengc68f0672017-11-21 03:33:06 -05001196 {
1197 .callback = battery_full_discharging_quirk,
1198 .ident = "ASUS GL502VSK",
1199 .matches = {
1200 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1201 DMI_MATCH(DMI_PRODUCT_NAME, "GL502VSK"),
1202 },
1203 },
1204 {
1205 .callback = battery_full_discharging_quirk,
1206 .ident = "ASUS UX305LA",
1207 .matches = {
1208 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1209 DMI_MATCH(DMI_PRODUCT_NAME, "UX305LA"),
1210 },
1211 },
Lan Tianyua90b4032014-01-06 22:50:37 +08001212 {},
1213};
1214
Lan Tianyu75646e72014-07-07 15:47:12 +08001215/*
1216 * Some machines'(E,G Lenovo Z480) ECs are not stable
1217 * during boot up and this causes battery driver fails to be
1218 * probed due to failure of getting battery information
1219 * from EC sometimes. After several retries, the operation
1220 * may work. So add retry code here and 20ms sleep between
1221 * every retries.
1222 */
1223static int acpi_battery_update_retry(struct acpi_battery *battery)
1224{
1225 int retry, ret;
1226
1227 for (retry = 5; retry; retry--) {
1228 ret = acpi_battery_update(battery, false);
1229 if (!ret)
1230 break;
1231
1232 msleep(20);
1233 }
1234 return ret;
1235}
1236
Len Brown4be44fc2005-08-05 00:44:28 -04001237static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Len Brown4be44fc2005-08-05 00:44:28 -04001239 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001240 struct acpi_battery *battery = NULL;
Jiang Liu952c63e2013-06-29 00:24:38 +08001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001243 return -EINVAL;
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001244
1245 if (device->dep_unmet)
1246 return -EPROBE_DEFER;
1247
Burman Yan36bcbec2006-12-19 12:56:11 -08001248 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -04001250 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -04001251 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1253 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001254 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001255 mutex_init(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001256 mutex_init(&battery->sysfs_lock);
Jiang Liu952c63e2013-06-29 00:24:38 +08001257 if (acpi_has_method(battery->device->handle, "_BIX"))
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +04001258 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Lan Tianyu75646e72014-07-07 15:47:12 +08001259
1260 result = acpi_battery_update_retry(battery);
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +01001261 if (result)
1262 goto fail;
Lan Tianyu75646e72014-07-07 15:47:12 +08001263
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001264#ifdef CONFIG_ACPI_PROCFS_POWER
1265 result = acpi_battery_add_fs(device);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001266 if (result) {
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001267 acpi_battery_remove_fs(device);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001268 goto fail;
1269 }
Bjørn Mork6993ce42017-12-05 18:46:39 +01001270#endif
Kyle McMartin25be5822011-03-22 16:19:50 -04001271
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001272 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1273 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1274 device->status.battery_present ? "present" : "absent");
1275
Kyle McMartin25be5822011-03-22 16:19:50 -04001276 battery->pm_nb.notifier_call = battery_notify;
1277 register_pm_notifier(&battery->pm_nb);
1278
Zhang Ruie0d1f092014-05-28 15:23:38 +08001279 device_init_wakeup(&device->dev, 1);
1280
Patrick Mocheld550d982006-06-27 00:41:40 -04001281 return result;
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001282
1283fail:
1284 sysfs_remove_battery(battery);
1285 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001286 mutex_destroy(&battery->sysfs_lock);
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001287 kfree(battery);
1288 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001291static int acpi_battery_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Len Brown4be44fc2005-08-05 00:44:28 -04001293 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001296 return -EINVAL;
Zhang Ruie0d1f092014-05-28 15:23:38 +08001297 device_init_wakeup(&device->dev, 0);
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001298 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001299 unregister_pm_notifier(&battery->pm_nb);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001300#ifdef CONFIG_ACPI_PROCFS_POWER
1301 acpi_battery_remove_fs(device);
1302#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001303 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001304 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001305 mutex_destroy(&battery->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001310#ifdef CONFIG_PM_SLEEP
Jiri Kosina34c44152006-10-10 14:20:41 -07001311/* this is needed to learn about changes made in suspended state */
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001312static int acpi_battery_resume(struct device *dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001313{
1314 struct acpi_battery *battery;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001315
1316 if (!dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001317 return -EINVAL;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001318
1319 battery = acpi_driver_data(to_acpi_device(dev));
1320 if (!battery)
1321 return -EINVAL;
1322
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001323 battery->update_time = 0;
Lan Tianyu9e50bc12014-05-04 14:07:06 +08001324 acpi_battery_update(battery, true);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001325 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001326}
Shuah Khan7f6895c2014-02-12 20:19:06 -07001327#else
1328#define acpi_battery_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001329#endif
Jiri Kosina34c44152006-10-10 14:20:41 -07001330
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001331static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1332
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001333static struct acpi_driver acpi_battery_driver = {
1334 .name = "battery",
1335 .class = ACPI_BATTERY_CLASS,
1336 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001337 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001338 .ops = {
1339 .add = acpi_battery_add,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001340 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001341 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001342 },
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001343 .drv.pm = &acpi_battery_pm,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001344};
1345
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001346static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Hans de Goededccfae62017-04-19 14:02:10 +02001348 unsigned int i;
Luis Henriques479faaf2015-05-11 22:48:46 +01001349 int result;
1350
Hans de Goededccfae62017-04-19 14:02:10 +02001351 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1352 if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1353 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1354 ": found native %s PMIC, not loading\n",
1355 acpi_battery_blacklist[i]);
1356 return;
1357 }
1358
Alexander Mezin3f5dc082014-06-04 02:01:22 +07001359 dmi_check_system(bat_dmi_table);
Luis Henriques479faaf2015-05-11 22:48:46 +01001360
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001361#ifdef CONFIG_ACPI_PROCFS_POWER
1362 acpi_battery_dir = acpi_lock_battery_dir();
1363 if (!acpi_battery_dir)
1364 return;
1365#endif
Luis Henriques479faaf2015-05-11 22:48:46 +01001366 result = acpi_bus_register_driver(&acpi_battery_driver);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001367#ifdef CONFIG_ACPI_PROCFS_POWER
Luis Henriques479faaf2015-05-11 22:48:46 +01001368 if (result < 0)
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001369 acpi_unlock_battery_dir(acpi_battery_dir);
1370#endif
Hans de Goedebc39fbc2017-04-19 14:02:09 +02001371 battery_driver_registered = (result == 0);
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001372}
1373
1374static int __init acpi_battery_init(void)
1375{
Luis Henriquese234b072015-05-11 22:48:38 +01001376 if (acpi_disabled)
1377 return -ENODEV;
1378
Luis Henriqueseca21d912015-05-11 22:49:05 +01001379 async_cookie = async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Len Brown4be44fc2005-08-05 00:44:28 -04001383static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Chris Wilson5dfa0c72016-05-19 09:11:52 +01001385 async_synchronize_cookie(async_cookie + 1);
Hans de Goedebc39fbc2017-04-19 14:02:09 +02001386 if (battery_driver_registered)
1387 acpi_bus_unregister_driver(&acpi_battery_driver);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001388#ifdef CONFIG_ACPI_PROCFS_POWER
Hans de Goedebc39fbc2017-04-19 14:02:09 +02001389 if (acpi_battery_dir)
1390 acpi_unlock_battery_dir(acpi_battery_dir);
Lan Tianyu3a670cc2014-05-04 11:07:25 +08001391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394module_init(acpi_battery_init);
1395module_exit(acpi_battery_exit);