Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
aa650bbd | 3 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
1da177e4 | 4 | * |
aa650bbd AS |
5 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
6 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> | |
1da177e4 LT |
7 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
8 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
1da177e4 LT |
9 | */ |
10 | ||
fa93854f OG |
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
12 | ||
53dd200a DR |
13 | #include <linux/async.h> |
14 | #include <linux/delay.h> | |
15 | #include <linux/dmi.h> | |
16 | #include <linux/jiffies.h> | |
1da177e4 | 17 | #include <linux/kernel.h> |
fa93854f | 18 | #include <linux/list.h> |
1da177e4 | 19 | #include <linux/module.h> |
fa93854f | 20 | #include <linux/mutex.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
25be5821 | 22 | #include <linux/suspend.h> |
53dd200a DR |
23 | #include <linux/types.h> |
24 | ||
4000e626 | 25 | #include <asm/unaligned.h> |
d7380965 | 26 | |
3a670cc7 LT |
27 | #ifdef CONFIG_ACPI_PROCFS_POWER |
28 | #include <linux/proc_fs.h> | |
29 | #include <linux/seq_file.h> | |
7c0f6ba6 | 30 | #include <linux/uaccess.h> |
3a670cc7 LT |
31 | #endif |
32 | ||
8b48463f | 33 | #include <linux/acpi.h> |
d7380965 AS |
34 | #include <linux/power_supply.h> |
35 | ||
fa93854f | 36 | #include <acpi/battery.h> |
f03be352 | 37 | |
a192a958 LB |
38 | #define PREFIX "ACPI: " |
39 | ||
1da177e4 | 40 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
cc99f0ad HG |
41 | #define ACPI_BATTERY_CAPACITY_VALID(capacity) \ |
42 | ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN) | |
1da177e4 | 43 | |
1da177e4 | 44 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 | 45 | |
ae6f6187 LT |
46 | /* Battery power unit: 0 means mW, 1 means mA */ |
47 | #define ACPI_BATTERY_POWER_UNIT_MA 1 | |
48 | ||
1ac5aaa6 ZR |
49 | #define ACPI_BATTERY_STATE_DISCHARGING 0x1 |
50 | #define ACPI_BATTERY_STATE_CHARGING 0x2 | |
51 | #define ACPI_BATTERY_STATE_CRITICAL 0x4 | |
52 | ||
1da177e4 | 53 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 54 | |
f52fd66d | 55 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 56 | |
f52fd66d | 57 | MODULE_AUTHOR("Paul Diefenbaugh"); |
aa650bbd | 58 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
7cda93e0 | 59 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
60 | MODULE_LICENSE("GPL"); |
61 | ||
eca21d91 | 62 | static async_cookie_t async_cookie; |
bc39fbcf | 63 | static bool battery_driver_registered; |
a90b4038 | 64 | static int battery_bix_broken_package; |
f43691c6 | 65 | static int battery_notification_delay_ms; |
1b799c5c | 66 | static int battery_ac_is_broken; |
ec625a37 | 67 | static int battery_check_pmic = 1; |
f1d4661a AS |
68 | static unsigned int cache_time = 1000; |
69 | module_param(cache_time, uint, 0644); | |
70 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 71 | |
3a670cc7 LT |
72 | #ifdef CONFIG_ACPI_PROCFS_POWER |
73 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); | |
74 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
3a670cc7 LT |
75 | #endif |
76 | ||
1ba90e3a TR |
77 | static const struct acpi_device_id battery_device_ids[] = { |
78 | {"PNP0C0A", 0}, | |
79 | {"", 0}, | |
80 | }; | |
1ba90e3a | 81 | |
aa650bbd | 82 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
1da177e4 | 83 | |
dccfae6d HG |
84 | /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */ |
85 | static const char * const acpi_battery_blacklist[] = { | |
86 | "INT33F4", /* X-Powers AXP288 PMIC */ | |
87 | }; | |
88 | ||
7b3bcc4a AS |
89 | enum { |
90 | ACPI_BATTERY_ALARM_PRESENT, | |
c67fcd67 | 91 | ACPI_BATTERY_XINFO_PRESENT, |
557d5868 | 92 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
4000e626 KI |
93 | /* On Lenovo Thinkpad models from 2010 and 2011, the power unit |
94 | switches between mWh and mAh depending on whether the system | |
95 | is running on battery or not. When mAh is the unit, most | |
96 | reported values are incorrect and need to be adjusted by | |
97 | 10000/design_voltage. Verified on x201, t410, t410s, and x220. | |
98 | Pre-2010 and 2012 models appear to always report in mWh and | |
99 | are thus unaffected (tested with t42, t61, t500, x200, x300, | |
100 | and x230). Also, in mid-2012 Lenovo issued a BIOS update for | |
101 | the 2011 models that fixes the issue (tested on x220 with a | |
102 | post-1.29 BIOS), but as of Nov. 2012, no such update is | |
103 | available for the 2010 models. */ | |
104 | ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
a20136a6 LT |
105 | /* for batteries reporting current capacity with design capacity |
106 | * on a full charge, but showing degradation in full charge cap. | |
107 | */ | |
108 | ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, | |
7b3bcc4a | 109 | }; |
78490d82 | 110 | |
1da177e4 | 111 | struct acpi_battery { |
038fdea2 | 112 | struct mutex lock; |
69d94ec6 | 113 | struct mutex sysfs_lock; |
297d716f KK |
114 | struct power_supply *bat; |
115 | struct power_supply_desc bat_desc; | |
f1d4661a | 116 | struct acpi_device *device; |
25be5821 | 117 | struct notifier_block pm_nb; |
fa93854f | 118 | struct list_head list; |
f1d4661a | 119 | unsigned long update_time; |
016d5baa | 120 | int revision; |
7faa144a | 121 | int rate_now; |
d7380965 AS |
122 | int capacity_now; |
123 | int voltage_now; | |
038fdea2 | 124 | int design_capacity; |
d7380965 | 125 | int full_charge_capacity; |
038fdea2 AS |
126 | int technology; |
127 | int design_voltage; | |
128 | int design_capacity_warning; | |
129 | int design_capacity_low; | |
c67fcd67 AS |
130 | int cycle_count; |
131 | int measurement_accuracy; | |
132 | int max_sampling_time; | |
133 | int min_sampling_time; | |
134 | int max_averaging_interval; | |
135 | int min_averaging_interval; | |
038fdea2 AS |
136 | int capacity_granularity_1; |
137 | int capacity_granularity_2; | |
f1d4661a | 138 | int alarm; |
038fdea2 AS |
139 | char model_number[32]; |
140 | char serial_number[32]; | |
141 | char type[32]; | |
142 | char oem_info[32]; | |
f1d4661a AS |
143 | int state; |
144 | int power_unit; | |
7b3bcc4a | 145 | unsigned long flags; |
1da177e4 LT |
146 | }; |
147 | ||
297d716f | 148 | #define to_acpi_battery(x) power_supply_get_drvdata(x) |
d7380965 | 149 | |
efd941f1 | 150 | static inline int acpi_battery_present(struct acpi_battery *battery) |
b6ce4083 | 151 | { |
78490d82 AS |
152 | return battery->device->status.battery_present; |
153 | } | |
038fdea2 | 154 | |
d7380965 AS |
155 | static int acpi_battery_technology(struct acpi_battery *battery) |
156 | { | |
157 | if (!strcasecmp("NiCd", battery->type)) | |
158 | return POWER_SUPPLY_TECHNOLOGY_NiCd; | |
159 | if (!strcasecmp("NiMH", battery->type)) | |
160 | return POWER_SUPPLY_TECHNOLOGY_NiMH; | |
161 | if (!strcasecmp("LION", battery->type)) | |
162 | return POWER_SUPPLY_TECHNOLOGY_LION; | |
ad40e68b | 163 | if (!strncasecmp("LI-ION", battery->type, 6)) |
0bde7eee | 164 | return POWER_SUPPLY_TECHNOLOGY_LION; |
d7380965 AS |
165 | if (!strcasecmp("LiP", battery->type)) |
166 | return POWER_SUPPLY_TECHNOLOGY_LIPO; | |
167 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
168 | } | |
169 | ||
9104476e | 170 | static int acpi_battery_get_state(struct acpi_battery *battery); |
b19073a0 | 171 | |
56f382a0 RH |
172 | static int acpi_battery_is_charged(struct acpi_battery *battery) |
173 | { | |
1ac5aaa6 | 174 | /* charging, discharging or critical low */ |
56f382a0 RH |
175 | if (battery->state != 0) |
176 | return 0; | |
177 | ||
178 | /* battery not reporting charge */ | |
179 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || | |
180 | battery->capacity_now == 0) | |
181 | return 0; | |
182 | ||
183 | /* good batteries update full_charge as the batteries degrade */ | |
184 | if (battery->full_charge_capacity == battery->capacity_now) | |
185 | return 1; | |
186 | ||
187 | /* fallback to using design values for broken batteries */ | |
188 | if (battery->design_capacity == battery->capacity_now) | |
189 | return 1; | |
190 | ||
191 | /* we don't do any sort of metric based on percentages */ | |
192 | return 0; | |
193 | } | |
194 | ||
a20136a6 LT |
195 | static bool acpi_battery_is_degraded(struct acpi_battery *battery) |
196 | { | |
cc99f0ad HG |
197 | return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && |
198 | ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) && | |
a20136a6 LT |
199 | battery->full_charge_capacity < battery->design_capacity; |
200 | } | |
201 | ||
19fffc84 HG |
202 | static int acpi_battery_handle_discharging(struct acpi_battery *battery) |
203 | { | |
204 | /* | |
205 | * Some devices wrongly report discharging if the battery's charge level | |
206 | * was above the device's start charging threshold atm the AC adapter | |
207 | * was plugged in and the device thus did not start a new charge cycle. | |
208 | */ | |
1b799c5c HG |
209 | if ((battery_ac_is_broken || power_supply_is_system_supplied()) && |
210 | battery->rate_now == 0) | |
19fffc84 HG |
211 | return POWER_SUPPLY_STATUS_NOT_CHARGING; |
212 | ||
213 | return POWER_SUPPLY_STATUS_DISCHARGING; | |
214 | } | |
215 | ||
d7380965 AS |
216 | static int acpi_battery_get_property(struct power_supply *psy, |
217 | enum power_supply_property psp, | |
218 | union power_supply_propval *val) | |
219 | { | |
5b74d1d1 | 220 | int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0; |
d7380965 AS |
221 | struct acpi_battery *battery = to_acpi_battery(psy); |
222 | ||
9104476e AS |
223 | if (acpi_battery_present(battery)) { |
224 | /* run battery update only if it is present */ | |
225 | acpi_battery_get_state(battery); | |
226 | } else if (psp != POWER_SUPPLY_PROP_PRESENT) | |
d7380965 AS |
227 | return -ENODEV; |
228 | switch (psp) { | |
229 | case POWER_SUPPLY_PROP_STATUS: | |
82bf43b2 | 230 | if (battery->state & ACPI_BATTERY_STATE_DISCHARGING) |
19fffc84 | 231 | val->intval = acpi_battery_handle_discharging(battery); |
82bf43b2 | 232 | else if (battery->state & ACPI_BATTERY_STATE_CHARGING) |
d7380965 | 233 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
56f382a0 | 234 | else if (acpi_battery_is_charged(battery)) |
d7380965 | 235 | val->intval = POWER_SUPPLY_STATUS_FULL; |
4c41d3ad RD |
236 | else |
237 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; | |
d7380965 AS |
238 | break; |
239 | case POWER_SUPPLY_PROP_PRESENT: | |
240 | val->intval = acpi_battery_present(battery); | |
241 | break; | |
242 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
243 | val->intval = acpi_battery_technology(battery); | |
244 | break; | |
c67fcd67 AS |
245 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
246 | val->intval = battery->cycle_count; | |
247 | break; | |
d7380965 | 248 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
a1b4bd69 RW |
249 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
250 | ret = -ENODEV; | |
251 | else | |
252 | val->intval = battery->design_voltage * 1000; | |
d7380965 AS |
253 | break; |
254 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
a1b4bd69 RW |
255 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
256 | ret = -ENODEV; | |
257 | else | |
258 | val->intval = battery->voltage_now * 1000; | |
d7380965 AS |
259 | break; |
260 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
7faa144a | 261 | case POWER_SUPPLY_PROP_POWER_NOW: |
a1b4bd69 RW |
262 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
263 | ret = -ENODEV; | |
264 | else | |
265 | val->intval = battery->rate_now * 1000; | |
d7380965 AS |
266 | break; |
267 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
268 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
cc99f0ad | 269 | if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) |
a1b4bd69 RW |
270 | ret = -ENODEV; |
271 | else | |
272 | val->intval = battery->design_capacity * 1000; | |
d7380965 AS |
273 | break; |
274 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
275 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
cc99f0ad | 276 | if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) |
a1b4bd69 RW |
277 | ret = -ENODEV; |
278 | else | |
279 | val->intval = battery->full_charge_capacity * 1000; | |
d7380965 AS |
280 | break; |
281 | case POWER_SUPPLY_PROP_CHARGE_NOW: | |
282 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
a1b4bd69 RW |
283 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
284 | ret = -ENODEV; | |
285 | else | |
286 | val->intval = battery->capacity_now * 1000; | |
d7380965 | 287 | break; |
a58e1150 | 288 | case POWER_SUPPLY_PROP_CAPACITY: |
5b74d1d1 HG |
289 | if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity)) |
290 | full_capacity = battery->full_charge_capacity; | |
291 | else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) | |
292 | full_capacity = battery->design_capacity; | |
293 | ||
cc99f0ad | 294 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || |
5b74d1d1 | 295 | full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
cc99f0ad HG |
296 | ret = -ENODEV; |
297 | else | |
a58e1150 | 298 | val->intval = battery->capacity_now * 100/ |
5b74d1d1 | 299 | full_capacity; |
a58e1150 | 300 | break; |
1ac5aaa6 ZR |
301 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
302 | if (battery->state & ACPI_BATTERY_STATE_CRITICAL) | |
303 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; | |
304 | else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && | |
305 | (battery->capacity_now <= battery->alarm)) | |
306 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | |
307 | else if (acpi_battery_is_charged(battery)) | |
308 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
309 | else | |
310 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
311 | break; | |
d7380965 AS |
312 | case POWER_SUPPLY_PROP_MODEL_NAME: |
313 | val->strval = battery->model_number; | |
314 | break; | |
315 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
316 | val->strval = battery->oem_info; | |
317 | break; | |
7c2670bb | 318 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
319 | val->strval = battery->serial_number; | |
320 | break; | |
d7380965 | 321 | default: |
a1b4bd69 | 322 | ret = -EINVAL; |
d7380965 | 323 | } |
a1b4bd69 | 324 | return ret; |
d7380965 AS |
325 | } |
326 | ||
327 | static enum power_supply_property charge_battery_props[] = { | |
328 | POWER_SUPPLY_PROP_STATUS, | |
329 | POWER_SUPPLY_PROP_PRESENT, | |
330 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 331 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
332 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
333 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
334 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
335 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
336 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
337 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
a58e1150 | 338 | POWER_SUPPLY_PROP_CAPACITY, |
1ac5aaa6 | 339 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
d7380965 AS |
340 | POWER_SUPPLY_PROP_MODEL_NAME, |
341 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 342 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
343 | }; |
344 | ||
ff3154d1 HG |
345 | static enum power_supply_property charge_battery_full_cap_broken_props[] = { |
346 | POWER_SUPPLY_PROP_STATUS, | |
347 | POWER_SUPPLY_PROP_PRESENT, | |
348 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
349 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
350 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
351 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
352 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
353 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
354 | POWER_SUPPLY_PROP_MODEL_NAME, | |
355 | POWER_SUPPLY_PROP_MANUFACTURER, | |
356 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
357 | }; | |
358 | ||
d7380965 AS |
359 | static enum power_supply_property energy_battery_props[] = { |
360 | POWER_SUPPLY_PROP_STATUS, | |
361 | POWER_SUPPLY_PROP_PRESENT, | |
362 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 363 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
364 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
365 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
7faa144a | 366 | POWER_SUPPLY_PROP_POWER_NOW, |
d7380965 AS |
367 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
368 | POWER_SUPPLY_PROP_ENERGY_FULL, | |
369 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
a58e1150 | 370 | POWER_SUPPLY_PROP_CAPACITY, |
1ac5aaa6 | 371 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
d7380965 AS |
372 | POWER_SUPPLY_PROP_MODEL_NAME, |
373 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 374 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
375 | }; |
376 | ||
b41901a2 HG |
377 | static enum power_supply_property energy_battery_full_cap_broken_props[] = { |
378 | POWER_SUPPLY_PROP_STATUS, | |
379 | POWER_SUPPLY_PROP_PRESENT, | |
380 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
381 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
382 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
383 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
384 | POWER_SUPPLY_PROP_POWER_NOW, | |
385 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
386 | POWER_SUPPLY_PROP_MODEL_NAME, | |
387 | POWER_SUPPLY_PROP_MANUFACTURER, | |
388 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
389 | }; | |
390 | ||
78490d82 AS |
391 | /* -------------------------------------------------------------------------- |
392 | Battery Management | |
393 | -------------------------------------------------------------------------- */ | |
038fdea2 AS |
394 | struct acpi_offsets { |
395 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
396 | u8 mode; /* int or string? */ | |
397 | }; | |
b6ce4083 | 398 | |
a4658784 | 399 | static const struct acpi_offsets state_offsets[] = { |
038fdea2 | 400 | {offsetof(struct acpi_battery, state), 0}, |
7faa144a | 401 | {offsetof(struct acpi_battery, rate_now), 0}, |
d7380965 AS |
402 | {offsetof(struct acpi_battery, capacity_now), 0}, |
403 | {offsetof(struct acpi_battery, voltage_now), 0}, | |
038fdea2 | 404 | }; |
b6ce4083 | 405 | |
a4658784 | 406 | static const struct acpi_offsets info_offsets[] = { |
038fdea2 AS |
407 | {offsetof(struct acpi_battery, power_unit), 0}, |
408 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
d7380965 | 409 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
038fdea2 AS |
410 | {offsetof(struct acpi_battery, technology), 0}, |
411 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
412 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
413 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
414 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
415 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
416 | {offsetof(struct acpi_battery, model_number), 1}, | |
417 | {offsetof(struct acpi_battery, serial_number), 1}, | |
418 | {offsetof(struct acpi_battery, type), 1}, | |
419 | {offsetof(struct acpi_battery, oem_info), 1}, | |
420 | }; | |
b6ce4083 | 421 | |
a4658784 | 422 | static const struct acpi_offsets extended_info_offsets[] = { |
016d5baa | 423 | {offsetof(struct acpi_battery, revision), 0}, |
c67fcd67 AS |
424 | {offsetof(struct acpi_battery, power_unit), 0}, |
425 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
426 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, | |
427 | {offsetof(struct acpi_battery, technology), 0}, | |
428 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
429 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
430 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
431 | {offsetof(struct acpi_battery, cycle_count), 0}, | |
432 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, | |
433 | {offsetof(struct acpi_battery, max_sampling_time), 0}, | |
434 | {offsetof(struct acpi_battery, min_sampling_time), 0}, | |
435 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, | |
436 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, | |
437 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
438 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
439 | {offsetof(struct acpi_battery, model_number), 1}, | |
440 | {offsetof(struct acpi_battery, serial_number), 1}, | |
441 | {offsetof(struct acpi_battery, type), 1}, | |
442 | {offsetof(struct acpi_battery, oem_info), 1}, | |
443 | }; | |
444 | ||
038fdea2 AS |
445 | static int extract_package(struct acpi_battery *battery, |
446 | union acpi_object *package, | |
a4658784 | 447 | const struct acpi_offsets *offsets, int num) |
038fdea2 | 448 | { |
106449e8 | 449 | int i; |
038fdea2 AS |
450 | union acpi_object *element; |
451 | if (package->type != ACPI_TYPE_PACKAGE) | |
452 | return -EFAULT; | |
453 | for (i = 0; i < num; ++i) { | |
454 | if (package->package.count <= i) | |
455 | return -EFAULT; | |
456 | element = &package->package.elements[i]; | |
457 | if (offsets[i].mode) { | |
106449e8 AS |
458 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
459 | if (element->type == ACPI_TYPE_STRING || | |
460 | element->type == ACPI_TYPE_BUFFER) | |
461 | strncpy(ptr, element->string.pointer, 32); | |
462 | else if (element->type == ACPI_TYPE_INTEGER) { | |
463 | strncpy(ptr, (u8 *)&element->integer.value, | |
439913ff LM |
464 | sizeof(u64)); |
465 | ptr[sizeof(u64)] = 0; | |
b8a1bdb1 AS |
466 | } else |
467 | *ptr = 0; /* don't have value */ | |
038fdea2 | 468 | } else { |
b8a1bdb1 AS |
469 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
470 | *x = (element->type == ACPI_TYPE_INTEGER) ? | |
471 | element->integer.value : -1; | |
038fdea2 | 472 | } |
b6ce4083 | 473 | } |
b6ce4083 VL |
474 | return 0; |
475 | } | |
476 | ||
477 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
478 | { | |
aa650bbd | 479 | if (acpi_bus_get_status(battery->device)) { |
b6ce4083 VL |
480 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
481 | return -ENODEV; | |
482 | } | |
aa650bbd | 483 | return 0; |
b6ce4083 VL |
484 | } |
485 | ||
2d09af4a DL |
486 | |
487 | static int extract_battery_info(const int use_bix, | |
488 | struct acpi_battery *battery, | |
489 | const struct acpi_buffer *buffer) | |
1da177e4 | 490 | { |
aa650bbd | 491 | int result = -EFAULT; |
1da177e4 | 492 | |
2d09af4a DL |
493 | if (use_bix && battery_bix_broken_package) |
494 | result = extract_package(battery, buffer->pointer, | |
a90b4038 LT |
495 | extended_info_offsets + 1, |
496 | ARRAY_SIZE(extended_info_offsets) - 1); | |
2d09af4a DL |
497 | else if (use_bix) |
498 | result = extract_package(battery, buffer->pointer, | |
c67fcd67 AS |
499 | extended_info_offsets, |
500 | ARRAY_SIZE(extended_info_offsets)); | |
501 | else | |
2d09af4a | 502 | result = extract_package(battery, buffer->pointer, |
c67fcd67 | 503 | info_offsets, ARRAY_SIZE(info_offsets)); |
557d5868 ZR |
504 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
505 | battery->full_charge_capacity = battery->design_capacity; | |
4000e626 KI |
506 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
507 | battery->power_unit && battery->design_voltage) { | |
508 | battery->design_capacity = battery->design_capacity * | |
509 | 10000 / battery->design_voltage; | |
510 | battery->full_charge_capacity = battery->full_charge_capacity * | |
511 | 10000 / battery->design_voltage; | |
512 | battery->design_capacity_warning = | |
513 | battery->design_capacity_warning * | |
514 | 10000 / battery->design_voltage; | |
515 | /* Curiously, design_capacity_low, unlike the rest of them, | |
516 | is correct. */ | |
517 | /* capacity_granularity_* equal 1 on the systems tested, so | |
518 | it's impossible to tell if they would need an adjustment | |
519 | or not if their values were higher. */ | |
520 | } | |
a20136a6 LT |
521 | if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && |
522 | battery->capacity_now > battery->full_charge_capacity) | |
523 | battery->capacity_now = battery->full_charge_capacity; | |
524 | ||
d550d98d | 525 | return result; |
1da177e4 LT |
526 | } |
527 | ||
2d09af4a DL |
528 | static int acpi_battery_get_info(struct acpi_battery *battery) |
529 | { | |
530 | const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); | |
531 | int use_bix; | |
532 | int result = -ENODEV; | |
533 | ||
534 | if (!acpi_battery_present(battery)) | |
535 | return 0; | |
536 | ||
537 | ||
538 | for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) { | |
539 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
540 | acpi_status status = AE_ERROR; | |
541 | ||
542 | mutex_lock(&battery->lock); | |
543 | status = acpi_evaluate_object(battery->device->handle, | |
544 | use_bix ? "_BIX":"_BIF", | |
545 | NULL, &buffer); | |
546 | mutex_unlock(&battery->lock); | |
547 | ||
548 | if (ACPI_FAILURE(status)) { | |
549 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", | |
550 | use_bix ? "_BIX":"_BIF")); | |
551 | } else { | |
552 | result = extract_battery_info(use_bix, | |
553 | battery, | |
554 | &buffer); | |
555 | ||
556 | kfree(buffer.pointer); | |
557 | break; | |
558 | } | |
559 | } | |
560 | ||
561 | if (!result && !use_bix && xinfo) | |
562 | pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n"); | |
563 | ||
564 | return result; | |
565 | } | |
566 | ||
b6ce4083 | 567 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 568 | { |
4be44fcd LB |
569 | int result = 0; |
570 | acpi_status status = 0; | |
571 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 572 | |
b6ce4083 VL |
573 | if (!acpi_battery_present(battery)) |
574 | return 0; | |
1da177e4 | 575 | |
f1d4661a AS |
576 | if (battery->update_time && |
577 | time_before(jiffies, battery->update_time + | |
578 | msecs_to_jiffies(cache_time))) | |
579 | return 0; | |
580 | ||
038fdea2 | 581 | mutex_lock(&battery->lock); |
f1d4661a | 582 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
583 | NULL, &buffer); |
584 | mutex_unlock(&battery->lock); | |
5b31d895 | 585 | |
1da177e4 | 586 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 587 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 588 | return -ENODEV; |
1da177e4 | 589 | } |
aa650bbd | 590 | |
038fdea2 AS |
591 | result = extract_package(battery, buffer.pointer, |
592 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 593 | battery->update_time = jiffies; |
78490d82 | 594 | kfree(buffer.pointer); |
bc76f90b | 595 | |
55003b21 LT |
596 | /* For buggy DSDTs that report negative 16-bit values for either |
597 | * charging or discharging current and/or report 0 as 65536 | |
598 | * due to bad math. | |
599 | */ | |
600 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && | |
601 | battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && | |
602 | (s16)(battery->rate_now) < 0) { | |
bc76f90b | 603 | battery->rate_now = abs((s16)battery->rate_now); |
dd1fca9e | 604 | pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n"); |
55003b21 | 605 | } |
bc76f90b | 606 | |
557d5868 ZR |
607 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
608 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) | |
609 | battery->capacity_now = (battery->capacity_now * | |
610 | battery->full_charge_capacity) / 100; | |
4000e626 KI |
611 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) && |
612 | battery->power_unit && battery->design_voltage) { | |
613 | battery->capacity_now = battery->capacity_now * | |
614 | 10000 / battery->design_voltage; | |
615 | } | |
a20136a6 LT |
616 | if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) && |
617 | battery->capacity_now > battery->full_charge_capacity) | |
618 | battery->capacity_now = battery->full_charge_capacity; | |
619 | ||
b6ce4083 VL |
620 | return result; |
621 | } | |
1da177e4 | 622 | |
aa650bbd | 623 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
1da177e4 | 624 | { |
4be44fcd | 625 | acpi_status status = 0; |
1da177e4 | 626 | |
c67fcd67 | 627 | if (!acpi_battery_present(battery) || |
7b3bcc4a | 628 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
d550d98d | 629 | return -ENODEV; |
1da177e4 | 630 | |
038fdea2 | 631 | mutex_lock(&battery->lock); |
0db98202 JL |
632 | status = acpi_execute_simple_method(battery->device->handle, "_BTP", |
633 | battery->alarm); | |
038fdea2 | 634 | mutex_unlock(&battery->lock); |
aa650bbd | 635 | |
1da177e4 | 636 | if (ACPI_FAILURE(status)) |
d550d98d | 637 | return -ENODEV; |
1da177e4 | 638 | |
aa650bbd | 639 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
d550d98d | 640 | return 0; |
1da177e4 LT |
641 | } |
642 | ||
b6ce4083 | 643 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 644 | { |
b6ce4083 | 645 | /* See if alarms are supported, and if so, set default */ |
952c63e9 | 646 | if (!acpi_has_method(battery->device->handle, "_BTP")) { |
7b3bcc4a | 647 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a | 648 | return 0; |
b6ce4083 | 649 | } |
7b3bcc4a | 650 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a AS |
651 | if (!battery->alarm) |
652 | battery->alarm = battery->design_capacity_warning; | |
aa650bbd | 653 | return acpi_battery_set_alarm(battery); |
b6ce4083 | 654 | } |
1da177e4 | 655 | |
508df92d AB |
656 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
657 | struct device_attribute *attr, | |
658 | char *buf) | |
659 | { | |
660 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
661 | return sprintf(buf, "%d\n", battery->alarm * 1000); | |
662 | } | |
663 | ||
664 | static ssize_t acpi_battery_alarm_store(struct device *dev, | |
665 | struct device_attribute *attr, | |
666 | const char *buf, size_t count) | |
667 | { | |
668 | unsigned long x; | |
669 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
47a08c85 | 670 | if (sscanf(buf, "%lu\n", &x) == 1) |
508df92d AB |
671 | battery->alarm = x/1000; |
672 | if (acpi_battery_present(battery)) | |
673 | acpi_battery_set_alarm(battery); | |
674 | return count; | |
675 | } | |
676 | ||
82d2b610 | 677 | static const struct device_attribute alarm_attr = { |
01e8ef11 | 678 | .attr = {.name = "alarm", .mode = 0644}, |
508df92d AB |
679 | .show = acpi_battery_alarm_show, |
680 | .store = acpi_battery_alarm_store, | |
681 | }; | |
682 | ||
fa93854f OG |
683 | /* |
684 | * The Battery Hooking API | |
685 | * | |
686 | * This API is used inside other drivers that need to expose | |
687 | * platform-specific behaviour within the generic driver in a | |
688 | * generic way. | |
689 | * | |
690 | */ | |
691 | ||
692 | static LIST_HEAD(acpi_battery_list); | |
693 | static LIST_HEAD(battery_hook_list); | |
694 | static DEFINE_MUTEX(hook_mutex); | |
695 | ||
514bcc5d | 696 | static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock) |
fa93854f OG |
697 | { |
698 | struct acpi_battery *battery; | |
699 | /* | |
700 | * In order to remove a hook, we first need to | |
701 | * de-register all the batteries that are registered. | |
702 | */ | |
703 | if (lock) | |
704 | mutex_lock(&hook_mutex); | |
705 | list_for_each_entry(battery, &acpi_battery_list, list) { | |
706 | hook->remove_battery(battery->bat); | |
707 | } | |
708 | list_del(&hook->list); | |
709 | if (lock) | |
710 | mutex_unlock(&hook_mutex); | |
711 | pr_info("extension unregistered: %s\n", hook->name); | |
712 | } | |
713 | ||
714 | void battery_hook_unregister(struct acpi_battery_hook *hook) | |
715 | { | |
716 | __battery_hook_unregister(hook, 1); | |
717 | } | |
718 | EXPORT_SYMBOL_GPL(battery_hook_unregister); | |
719 | ||
720 | void battery_hook_register(struct acpi_battery_hook *hook) | |
721 | { | |
722 | struct acpi_battery *battery; | |
723 | ||
724 | mutex_lock(&hook_mutex); | |
725 | INIT_LIST_HEAD(&hook->list); | |
726 | list_add(&hook->list, &battery_hook_list); | |
727 | /* | |
728 | * Now that the driver is registered, we need | |
729 | * to notify the hook that a battery is available | |
730 | * for each battery, so that the driver may add | |
731 | * its attributes. | |
732 | */ | |
733 | list_for_each_entry(battery, &acpi_battery_list, list) { | |
734 | if (hook->add_battery(battery->bat)) { | |
735 | /* | |
736 | * If a add-battery returns non-zero, | |
737 | * the registration of the extension has failed, | |
738 | * and we will not add it to the list of loaded | |
739 | * hooks. | |
740 | */ | |
741 | pr_err("extension failed to load: %s", hook->name); | |
742 | __battery_hook_unregister(hook, 0); | |
673b4271 | 743 | goto end; |
fa93854f OG |
744 | } |
745 | } | |
746 | pr_info("new extension: %s\n", hook->name); | |
673b4271 | 747 | end: |
fa93854f OG |
748 | mutex_unlock(&hook_mutex); |
749 | } | |
750 | EXPORT_SYMBOL_GPL(battery_hook_register); | |
751 | ||
752 | /* | |
753 | * This function gets called right after the battery sysfs | |
754 | * attributes have been added, so that the drivers that | |
755 | * define custom sysfs attributes can add their own. | |
7a4ea10c | 756 | */ |
fa93854f OG |
757 | static void battery_hook_add_battery(struct acpi_battery *battery) |
758 | { | |
673b4271 | 759 | struct acpi_battery_hook *hook_node, *tmp; |
fa93854f OG |
760 | |
761 | mutex_lock(&hook_mutex); | |
762 | INIT_LIST_HEAD(&battery->list); | |
763 | list_add(&battery->list, &acpi_battery_list); | |
764 | /* | |
765 | * Since we added a new battery to the list, we need to | |
766 | * iterate over the hooks and call add_battery for each | |
767 | * hook that was registered. This usually happens | |
768 | * when a battery gets hotplugged or initialized | |
769 | * during the battery module initialization. | |
770 | */ | |
673b4271 | 771 | list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) { |
fa93854f OG |
772 | if (hook_node->add_battery(battery->bat)) { |
773 | /* | |
774 | * The notification of the extensions has failed, to | |
775 | * prevent further errors we will unload the extension. | |
776 | */ | |
fa93854f OG |
777 | pr_err("error in extension, unloading: %s", |
778 | hook_node->name); | |
673b4271 | 779 | __battery_hook_unregister(hook_node, 0); |
fa93854f OG |
780 | } |
781 | } | |
782 | mutex_unlock(&hook_mutex); | |
783 | } | |
784 | ||
785 | static void battery_hook_remove_battery(struct acpi_battery *battery) | |
786 | { | |
787 | struct acpi_battery_hook *hook; | |
788 | ||
789 | mutex_lock(&hook_mutex); | |
790 | /* | |
791 | * Before removing the hook, we need to remove all | |
792 | * custom attributes from the battery. | |
793 | */ | |
794 | list_for_each_entry(hook, &battery_hook_list, list) { | |
795 | hook->remove_battery(battery->bat); | |
796 | } | |
797 | /* Then, just remove the battery from the list */ | |
798 | list_del(&battery->list); | |
799 | mutex_unlock(&hook_mutex); | |
800 | } | |
801 | ||
802 | static void __exit battery_hook_exit(void) | |
803 | { | |
804 | struct acpi_battery_hook *hook; | |
805 | struct acpi_battery_hook *ptr; | |
806 | /* | |
807 | * At this point, the acpi_bus_unregister_driver() | |
808 | * has called remove for all batteries. We just | |
809 | * need to remove the hooks. | |
810 | */ | |
811 | list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) { | |
812 | __battery_hook_unregister(hook, 1); | |
813 | } | |
814 | mutex_destroy(&hook_mutex); | |
815 | } | |
816 | ||
508df92d AB |
817 | static int sysfs_add_battery(struct acpi_battery *battery) |
818 | { | |
297d716f | 819 | struct power_supply_config psy_cfg = { .drv_data = battery, }; |
ff3154d1 HG |
820 | bool full_cap_broken = false; |
821 | ||
822 | if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) && | |
823 | !ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity)) | |
824 | full_cap_broken = true; | |
508df92d | 825 | |
ae6f6187 | 826 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { |
ff3154d1 HG |
827 | if (full_cap_broken) { |
828 | battery->bat_desc.properties = | |
829 | charge_battery_full_cap_broken_props; | |
830 | battery->bat_desc.num_properties = | |
831 | ARRAY_SIZE(charge_battery_full_cap_broken_props); | |
832 | } else { | |
833 | battery->bat_desc.properties = charge_battery_props; | |
834 | battery->bat_desc.num_properties = | |
835 | ARRAY_SIZE(charge_battery_props); | |
836 | } | |
508df92d | 837 | } else { |
ff3154d1 HG |
838 | if (full_cap_broken) { |
839 | battery->bat_desc.properties = | |
840 | energy_battery_full_cap_broken_props; | |
841 | battery->bat_desc.num_properties = | |
842 | ARRAY_SIZE(energy_battery_full_cap_broken_props); | |
843 | } else { | |
844 | battery->bat_desc.properties = energy_battery_props; | |
845 | battery->bat_desc.num_properties = | |
846 | ARRAY_SIZE(energy_battery_props); | |
847 | } | |
508df92d AB |
848 | } |
849 | ||
297d716f KK |
850 | battery->bat_desc.name = acpi_device_bid(battery->device); |
851 | battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; | |
852 | battery->bat_desc.get_property = acpi_battery_get_property; | |
508df92d | 853 | |
297d716f KK |
854 | battery->bat = power_supply_register_no_ws(&battery->device->dev, |
855 | &battery->bat_desc, &psy_cfg); | |
e0d1f09e | 856 | |
297d716f KK |
857 | if (IS_ERR(battery->bat)) { |
858 | int result = PTR_ERR(battery->bat); | |
859 | ||
860 | battery->bat = NULL; | |
508df92d | 861 | return result; |
297d716f | 862 | } |
fa93854f | 863 | battery_hook_add_battery(battery); |
297d716f | 864 | return device_create_file(&battery->bat->dev, &alarm_attr); |
508df92d AB |
865 | } |
866 | ||
867 | static void sysfs_remove_battery(struct acpi_battery *battery) | |
868 | { | |
69d94ec6 | 869 | mutex_lock(&battery->sysfs_lock); |
297d716f | 870 | if (!battery->bat) { |
69d94ec6 | 871 | mutex_unlock(&battery->sysfs_lock); |
508df92d | 872 | return; |
9c921c22 | 873 | } |
fa93854f | 874 | battery_hook_remove_battery(battery); |
297d716f KK |
875 | device_remove_file(&battery->bat->dev, &alarm_attr); |
876 | power_supply_unregister(battery->bat); | |
877 | battery->bat = NULL; | |
69d94ec6 | 878 | mutex_unlock(&battery->sysfs_lock); |
bc76f90b HM |
879 | } |
880 | ||
4000e626 KI |
881 | static void find_battery(const struct dmi_header *dm, void *private) |
882 | { | |
883 | struct acpi_battery *battery = (struct acpi_battery *)private; | |
884 | /* Note: the hardcoded offsets below have been extracted from | |
885 | the source code of dmidecode. */ | |
886 | if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) { | |
887 | const u8 *dmi_data = (const u8 *)(dm + 1); | |
888 | int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6)); | |
889 | if (dm->length >= 18) | |
890 | dmi_capacity *= dmi_data[17]; | |
891 | if (battery->design_capacity * battery->design_voltage / 1000 | |
892 | != dmi_capacity && | |
893 | battery->design_capacity * 10 == dmi_capacity) | |
894 | set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
895 | &battery->flags); | |
896 | } | |
897 | } | |
898 | ||
557d5868 ZR |
899 | /* |
900 | * According to the ACPI spec, some kinds of primary batteries can | |
901 | * report percentage battery remaining capacity directly to OS. | |
902 | * In this case, it reports the Last Full Charged Capacity == 100 | |
903 | * and BatteryPresentRate == 0xFFFFFFFF. | |
904 | * | |
905 | * Now we found some battery reports percentage remaining capacity | |
906 | * even if it's rechargeable. | |
907 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 | |
908 | * | |
909 | * Handle this correctly so that they won't break userspace. | |
910 | */ | |
7b78622d | 911 | static void acpi_battery_quirks(struct acpi_battery *battery) |
557d5868 ZR |
912 | { |
913 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) | |
0f4c6547 | 914 | return; |
557d5868 | 915 | |
0f4c6547 NM |
916 | if (battery->full_charge_capacity == 100 && |
917 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && | |
918 | battery->capacity_now >= 0 && battery->capacity_now <= 100) { | |
557d5868 ZR |
919 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); |
920 | battery->full_charge_capacity = battery->design_capacity; | |
921 | battery->capacity_now = (battery->capacity_now * | |
922 | battery->full_charge_capacity) / 100; | |
923 | } | |
4000e626 KI |
924 | |
925 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags)) | |
0f4c6547 | 926 | return; |
4000e626 KI |
927 | |
928 | if (battery->power_unit && dmi_name_in_vendors("LENOVO")) { | |
929 | const char *s; | |
930 | s = dmi_get_system_info(DMI_PRODUCT_VERSION); | |
ffd8a731 | 931 | if (s && !strncasecmp(s, "ThinkPad", 8)) { |
4000e626 KI |
932 | dmi_walk(find_battery, battery); |
933 | if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, | |
934 | &battery->flags) && | |
935 | battery->design_voltage) { | |
936 | battery->design_capacity = | |
937 | battery->design_capacity * | |
938 | 10000 / battery->design_voltage; | |
939 | battery->full_charge_capacity = | |
940 | battery->full_charge_capacity * | |
941 | 10000 / battery->design_voltage; | |
942 | battery->design_capacity_warning = | |
943 | battery->design_capacity_warning * | |
944 | 10000 / battery->design_voltage; | |
945 | battery->capacity_now = battery->capacity_now * | |
946 | 10000 / battery->design_voltage; | |
947 | } | |
948 | } | |
949 | } | |
a20136a6 LT |
950 | |
951 | if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags)) | |
952 | return; | |
953 | ||
954 | if (acpi_battery_is_degraded(battery) && | |
955 | battery->capacity_now > battery->full_charge_capacity) { | |
956 | set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags); | |
957 | battery->capacity_now = battery->full_charge_capacity; | |
958 | } | |
557d5868 ZR |
959 | } |
960 | ||
9e50bc14 | 961 | static int acpi_battery_update(struct acpi_battery *battery, bool resume) |
b6ce4083 | 962 | { |
82f2d305 LRM |
963 | int result = acpi_battery_get_status(battery); |
964 | ||
508df92d | 965 | if (result) |
b6ce4083 | 966 | return result; |
82f2d305 | 967 | |
508df92d AB |
968 | if (!acpi_battery_present(battery)) { |
969 | sysfs_remove_battery(battery); | |
97749cd9 | 970 | battery->update_time = 0; |
508df92d | 971 | return 0; |
b6ce4083 | 972 | } |
9e50bc14 LT |
973 | |
974 | if (resume) | |
975 | return 0; | |
976 | ||
82f2d305 | 977 | if (!battery->update_time) { |
97749cd9 AS |
978 | result = acpi_battery_get_info(battery); |
979 | if (result) | |
980 | return result; | |
981 | acpi_battery_init_alarm(battery); | |
982 | } | |
12c78ca2 CG |
983 | |
984 | result = acpi_battery_get_state(battery); | |
985 | if (result) | |
986 | return result; | |
987 | acpi_battery_quirks(battery); | |
988 | ||
297d716f | 989 | if (!battery->bat) { |
eb03cb02 SH |
990 | result = sysfs_add_battery(battery); |
991 | if (result) | |
992 | return result; | |
993 | } | |
e0d1f09e ZR |
994 | |
995 | /* | |
996 | * Wakeup the system if battery is critical low | |
997 | * or lower than the alarm level | |
998 | */ | |
999 | if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) || | |
1000 | (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) && | |
1001 | (battery->capacity_now <= battery->alarm))) | |
33e4f80e | 1002 | acpi_pm_wakeup_event(&battery->device->dev); |
e0d1f09e | 1003 | |
557d5868 | 1004 | return result; |
4bd35cdb VL |
1005 | } |
1006 | ||
da8aeb92 RW |
1007 | static void acpi_battery_refresh(struct acpi_battery *battery) |
1008 | { | |
c5971456 AW |
1009 | int power_unit; |
1010 | ||
297d716f | 1011 | if (!battery->bat) |
da8aeb92 RW |
1012 | return; |
1013 | ||
c5971456 AW |
1014 | power_unit = battery->power_unit; |
1015 | ||
da8aeb92 | 1016 | acpi_battery_get_info(battery); |
c5971456 AW |
1017 | |
1018 | if (power_unit == battery->power_unit) | |
1019 | return; | |
1020 | ||
1021 | /* The battery has changed its reporting units. */ | |
da8aeb92 RW |
1022 | sysfs_remove_battery(battery); |
1023 | sysfs_add_battery(battery); | |
1024 | } | |
1025 | ||
3a670cc7 LT |
1026 | /* -------------------------------------------------------------------------- |
1027 | FS Interface (/proc) | |
1028 | -------------------------------------------------------------------------- */ | |
1029 | ||
1030 | #ifdef CONFIG_ACPI_PROCFS_POWER | |
1031 | static struct proc_dir_entry *acpi_battery_dir; | |
1032 | ||
27059b91 MK |
1033 | static const char *acpi_battery_units(const struct acpi_battery *battery) |
1034 | { | |
1035 | return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ? | |
1036 | "mA" : "mW"; | |
1037 | } | |
1038 | ||
e7b087fc | 1039 | static int acpi_battery_info_proc_show(struct seq_file *seq, void *offset) |
3a670cc7 LT |
1040 | { |
1041 | struct acpi_battery *battery = seq->private; | |
e7b087fc | 1042 | int result = acpi_battery_update(battery, false); |
3a670cc7 LT |
1043 | |
1044 | if (result) | |
1045 | goto end; | |
1046 | ||
1047 | seq_printf(seq, "present: %s\n", | |
1048 | acpi_battery_present(battery) ? "yes" : "no"); | |
1049 | if (!acpi_battery_present(battery)) | |
1050 | goto end; | |
1051 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
1052 | seq_printf(seq, "design capacity: unknown\n"); | |
1053 | else | |
1054 | seq_printf(seq, "design capacity: %d %sh\n", | |
1055 | battery->design_capacity, | |
1056 | acpi_battery_units(battery)); | |
1057 | ||
1058 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) | |
1059 | seq_printf(seq, "last full capacity: unknown\n"); | |
1060 | else | |
1061 | seq_printf(seq, "last full capacity: %d %sh\n", | |
1062 | battery->full_charge_capacity, | |
1063 | acpi_battery_units(battery)); | |
1064 | ||
1065 | seq_printf(seq, "battery technology: %srechargeable\n", | |
2754435d | 1066 | battery->technology ? "" : "non-"); |
3a670cc7 LT |
1067 | |
1068 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) | |
1069 | seq_printf(seq, "design voltage: unknown\n"); | |
1070 | else | |
1071 | seq_printf(seq, "design voltage: %d mV\n", | |
1072 | battery->design_voltage); | |
1073 | seq_printf(seq, "design capacity warning: %d %sh\n", | |
1074 | battery->design_capacity_warning, | |
1075 | acpi_battery_units(battery)); | |
1076 | seq_printf(seq, "design capacity low: %d %sh\n", | |
1077 | battery->design_capacity_low, | |
1078 | acpi_battery_units(battery)); | |
1079 | seq_printf(seq, "cycle count: %i\n", battery->cycle_count); | |
1080 | seq_printf(seq, "capacity granularity 1: %d %sh\n", | |
1081 | battery->capacity_granularity_1, | |
1082 | acpi_battery_units(battery)); | |
1083 | seq_printf(seq, "capacity granularity 2: %d %sh\n", | |
1084 | battery->capacity_granularity_2, | |
1085 | acpi_battery_units(battery)); | |
1086 | seq_printf(seq, "model number: %s\n", battery->model_number); | |
1087 | seq_printf(seq, "serial number: %s\n", battery->serial_number); | |
1088 | seq_printf(seq, "battery type: %s\n", battery->type); | |
1089 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); | |
1090 | end: | |
1091 | if (result) | |
1092 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
1093 | return result; | |
1094 | } | |
1095 | ||
e7b087fc | 1096 | static int acpi_battery_state_proc_show(struct seq_file *seq, void *offset) |
3a670cc7 LT |
1097 | { |
1098 | struct acpi_battery *battery = seq->private; | |
e7b087fc | 1099 | int result = acpi_battery_update(battery, false); |
3a670cc7 LT |
1100 | |
1101 | if (result) | |
1102 | goto end; | |
1103 | ||
1104 | seq_printf(seq, "present: %s\n", | |
1105 | acpi_battery_present(battery) ? "yes" : "no"); | |
1106 | if (!acpi_battery_present(battery)) | |
1107 | goto end; | |
1108 | ||
1109 | seq_printf(seq, "capacity state: %s\n", | |
1110 | (battery->state & 0x04) ? "critical" : "ok"); | |
1111 | if ((battery->state & 0x01) && (battery->state & 0x02)) | |
1112 | seq_printf(seq, | |
1113 | "charging state: charging/discharging\n"); | |
1114 | else if (battery->state & 0x01) | |
1115 | seq_printf(seq, "charging state: discharging\n"); | |
1116 | else if (battery->state & 0x02) | |
1117 | seq_printf(seq, "charging state: charging\n"); | |
1118 | else | |
1119 | seq_printf(seq, "charging state: charged\n"); | |
1120 | ||
1121 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1122 | seq_printf(seq, "present rate: unknown\n"); | |
1123 | else | |
1124 | seq_printf(seq, "present rate: %d %s\n", | |
1125 | battery->rate_now, acpi_battery_units(battery)); | |
1126 | ||
1127 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1128 | seq_printf(seq, "remaining capacity: unknown\n"); | |
1129 | else | |
1130 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
1131 | battery->capacity_now, acpi_battery_units(battery)); | |
1132 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1133 | seq_printf(seq, "present voltage: unknown\n"); | |
1134 | else | |
1135 | seq_printf(seq, "present voltage: %d mV\n", | |
1136 | battery->voltage_now); | |
1137 | end: | |
1138 | if (result) | |
1139 | seq_printf(seq, "ERROR: Unable to read battery state\n"); | |
1140 | ||
1141 | return result; | |
1142 | } | |
1143 | ||
e7b087fc | 1144 | static int acpi_battery_alarm_proc_show(struct seq_file *seq, void *offset) |
3a670cc7 LT |
1145 | { |
1146 | struct acpi_battery *battery = seq->private; | |
e7b087fc | 1147 | int result = acpi_battery_update(battery, false); |
3a670cc7 LT |
1148 | |
1149 | if (result) | |
1150 | goto end; | |
1151 | ||
1152 | if (!acpi_battery_present(battery)) { | |
1153 | seq_printf(seq, "present: no\n"); | |
1154 | goto end; | |
1155 | } | |
1156 | seq_printf(seq, "alarm: "); | |
2754435d | 1157 | if (battery->alarm) { |
3a670cc7 LT |
1158 | seq_printf(seq, "%u %sh\n", battery->alarm, |
1159 | acpi_battery_units(battery)); | |
2754435d DR |
1160 | } else { |
1161 | seq_printf(seq, "unsupported\n"); | |
1162 | } | |
3a670cc7 LT |
1163 | end: |
1164 | if (result) | |
1165 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
1166 | return result; | |
1167 | } | |
1168 | ||
1169 | static ssize_t acpi_battery_write_alarm(struct file *file, | |
1170 | const char __user * buffer, | |
1171 | size_t count, loff_t * ppos) | |
1172 | { | |
1173 | int result = 0; | |
1174 | char alarm_string[12] = { '\0' }; | |
1175 | struct seq_file *m = file->private_data; | |
1176 | struct acpi_battery *battery = m->private; | |
1177 | ||
1178 | if (!battery || (count > sizeof(alarm_string) - 1)) | |
1179 | return -EINVAL; | |
1180 | if (!acpi_battery_present(battery)) { | |
1181 | result = -ENODEV; | |
1182 | goto end; | |
1183 | } | |
1184 | if (copy_from_user(alarm_string, buffer, count)) { | |
1185 | result = -EFAULT; | |
1186 | goto end; | |
1187 | } | |
1188 | alarm_string[count] = '\0'; | |
3d915894 CJ |
1189 | if (kstrtoint(alarm_string, 0, &battery->alarm)) { |
1190 | result = -EINVAL; | |
1191 | goto end; | |
1192 | } | |
3a670cc7 LT |
1193 | result = acpi_battery_set_alarm(battery); |
1194 | end: | |
2754435d DR |
1195 | if (result) |
1196 | return result; | |
1197 | return count; | |
3a670cc7 LT |
1198 | } |
1199 | ||
e7b087fc | 1200 | static int acpi_battery_alarm_proc_open(struct inode *inode, struct file *file) |
3a670cc7 | 1201 | { |
e7b087fc | 1202 | return single_open(file, acpi_battery_alarm_proc_show, PDE_DATA(inode)); |
3a670cc7 LT |
1203 | } |
1204 | ||
97a32539 AD |
1205 | static const struct proc_ops acpi_battery_alarm_proc_ops = { |
1206 | .proc_open = acpi_battery_alarm_proc_open, | |
1207 | .proc_read = seq_read, | |
1208 | .proc_write = acpi_battery_write_alarm, | |
1209 | .proc_lseek = seq_lseek, | |
1210 | .proc_release = single_release, | |
3a670cc7 LT |
1211 | }; |
1212 | ||
3a670cc7 LT |
1213 | static int acpi_battery_add_fs(struct acpi_device *device) |
1214 | { | |
933ca4e3 | 1215 | pr_warn(PREFIX "Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); |
3a670cc7 LT |
1216 | if (!acpi_device_dir(device)) { |
1217 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
1218 | acpi_battery_dir); | |
1219 | if (!acpi_device_dir(device)) | |
1220 | return -ENODEV; | |
1221 | } | |
1222 | ||
e7b087fc CH |
1223 | if (!proc_create_single_data("info", S_IRUGO, acpi_device_dir(device), |
1224 | acpi_battery_info_proc_show, acpi_driver_data(device))) | |
1225 | return -ENODEV; | |
1226 | if (!proc_create_single_data("state", S_IRUGO, acpi_device_dir(device), | |
1227 | acpi_battery_state_proc_show, acpi_driver_data(device))) | |
1228 | return -ENODEV; | |
1229 | if (!proc_create_data("alarm", S_IFREG | S_IRUGO | S_IWUSR, | |
97a32539 | 1230 | acpi_device_dir(device), &acpi_battery_alarm_proc_ops, |
e7b087fc CH |
1231 | acpi_driver_data(device))) |
1232 | return -ENODEV; | |
3a670cc7 LT |
1233 | return 0; |
1234 | } | |
1235 | ||
1236 | static void acpi_battery_remove_fs(struct acpi_device *device) | |
1237 | { | |
3a670cc7 LT |
1238 | if (!acpi_device_dir(device)) |
1239 | return; | |
e7b087fc | 1240 | remove_proc_subtree(acpi_device_bid(device), acpi_battery_dir); |
3a670cc7 LT |
1241 | acpi_device_dir(device) = NULL; |
1242 | } | |
1243 | ||
1244 | #endif | |
1245 | ||
1da177e4 LT |
1246 | /* -------------------------------------------------------------------------- |
1247 | Driver Interface | |
1248 | -------------------------------------------------------------------------- */ | |
1249 | ||
d9406691 | 1250 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
1da177e4 | 1251 | { |
d9406691 | 1252 | struct acpi_battery *battery = acpi_driver_data(device); |
297d716f | 1253 | struct power_supply *old; |
d9406691 | 1254 | |
1da177e4 | 1255 | if (!battery) |
d550d98d | 1256 | return; |
297d716f | 1257 | old = battery->bat; |
f43691c6 AM |
1258 | /* |
1259 | * On Acer Aspire V5-573G notifications are sometimes triggered too | |
1260 | * early. For example, when AC is unplugged and notification is | |
1261 | * triggered, battery state is still reported as "Full", and changes to | |
1262 | * "Discharging" only after short delay, without any notification. | |
1263 | */ | |
1264 | if (battery_notification_delay_ms > 0) | |
1265 | msleep(battery_notification_delay_ms); | |
da8aeb92 RW |
1266 | if (event == ACPI_BATTERY_NOTIFY_INFO) |
1267 | acpi_battery_refresh(battery); | |
9e50bc14 | 1268 | acpi_battery_update(battery, false); |
f1d4661a | 1269 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469d | 1270 | dev_name(&device->dev), event, |
9ea7d575 | 1271 | acpi_battery_present(battery)); |
411e0f77 | 1272 | acpi_notifier_call_chain(device, event, acpi_battery_present(battery)); |
2345baf4 | 1273 | /* acpi_battery_update could remove power_supply object */ |
297d716f KK |
1274 | if (old && battery->bat) |
1275 | power_supply_changed(battery->bat); | |
1da177e4 LT |
1276 | } |
1277 | ||
25be5821 KM |
1278 | static int battery_notify(struct notifier_block *nb, |
1279 | unsigned long mode, void *_unused) | |
1280 | { | |
1281 | struct acpi_battery *battery = container_of(nb, struct acpi_battery, | |
1282 | pm_nb); | |
9e50bc14 LT |
1283 | int result; |
1284 | ||
25be5821 | 1285 | switch (mode) { |
d5a5911b | 1286 | case PM_POST_HIBERNATION: |
25be5821 | 1287 | case PM_POST_SUSPEND: |
9e50bc14 LT |
1288 | if (!acpi_battery_present(battery)) |
1289 | return 0; | |
1290 | ||
2754435d DR |
1291 | if (battery->bat) { |
1292 | acpi_battery_refresh(battery); | |
1293 | } else { | |
9e50bc14 LT |
1294 | result = acpi_battery_get_info(battery); |
1295 | if (result) | |
1296 | return result; | |
1297 | ||
1298 | result = sysfs_add_battery(battery); | |
1299 | if (result) | |
1300 | return result; | |
2754435d | 1301 | } |
9e50bc14 LT |
1302 | |
1303 | acpi_battery_init_alarm(battery); | |
1304 | acpi_battery_get_state(battery); | |
25be5821 KM |
1305 | break; |
1306 | } | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
048d16da MK |
1311 | static int __init |
1312 | battery_bix_broken_package_quirk(const struct dmi_system_id *d) | |
3f5dc08f AM |
1313 | { |
1314 | battery_bix_broken_package = 1; | |
1315 | return 0; | |
1316 | } | |
1317 | ||
048d16da MK |
1318 | static int __init |
1319 | battery_notification_delay_quirk(const struct dmi_system_id *d) | |
f43691c6 AM |
1320 | { |
1321 | battery_notification_delay_ms = 1000; | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1b799c5c HG |
1325 | static int __init |
1326 | battery_ac_is_broken_quirk(const struct dmi_system_id *d) | |
1327 | { | |
1328 | battery_ac_is_broken = 1; | |
1329 | return 0; | |
1330 | } | |
1331 | ||
ec625a37 CC |
1332 | static int __init |
1333 | battery_do_not_check_pmic_quirk(const struct dmi_system_id *d) | |
1334 | { | |
1335 | battery_check_pmic = 0; | |
1336 | return 0; | |
1337 | } | |
1338 | ||
048d16da | 1339 | static const struct dmi_system_id bat_dmi_table[] __initconst = { |
a90b4038 | 1340 | { |
91afa076 | 1341 | /* NEC LZ750/LS */ |
3f5dc08f | 1342 | .callback = battery_bix_broken_package_quirk, |
a90b4038 LT |
1343 | .matches = { |
1344 | DMI_MATCH(DMI_SYS_VENDOR, "NEC"), | |
1345 | DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"), | |
1346 | }, | |
1347 | }, | |
f43691c6 | 1348 | { |
91afa076 | 1349 | /* Acer Aspire V5-573G */ |
f43691c6 | 1350 | .callback = battery_notification_delay_quirk, |
f43691c6 AM |
1351 | .matches = { |
1352 | DMI_MATCH(DMI_SYS_VENDOR, "Acer"), | |
1353 | DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"), | |
1354 | }, | |
1355 | }, | |
1b799c5c HG |
1356 | { |
1357 | /* Point of View mobii wintab p800w */ | |
1358 | .callback = battery_ac_is_broken_quirk, | |
1359 | .matches = { | |
1360 | DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"), | |
1361 | DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"), | |
1362 | DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"), | |
1363 | /* Above matches are too generic, add bios-date match */ | |
1364 | DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"), | |
1365 | }, | |
1366 | }, | |
ec625a37 CC |
1367 | { |
1368 | /* ECS EF20EA */ | |
1369 | .callback = battery_do_not_check_pmic_quirk, | |
1370 | .matches = { | |
1371 | DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"), | |
1372 | }, | |
1373 | }, | |
1374 | { | |
1375 | /* Lenovo Ideapad Miix 320 */ | |
1376 | .callback = battery_do_not_check_pmic_quirk, | |
1377 | .matches = { | |
1378 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"), | |
1379 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"), | |
1380 | DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"), | |
1381 | }, | |
1382 | }, | |
a90b4038 LT |
1383 | {}, |
1384 | }; | |
1385 | ||
75646e75 LT |
1386 | /* |
1387 | * Some machines'(E,G Lenovo Z480) ECs are not stable | |
1388 | * during boot up and this causes battery driver fails to be | |
1389 | * probed due to failure of getting battery information | |
1390 | * from EC sometimes. After several retries, the operation | |
1391 | * may work. So add retry code here and 20ms sleep between | |
1392 | * every retries. | |
1393 | */ | |
1394 | static int acpi_battery_update_retry(struct acpi_battery *battery) | |
1395 | { | |
1396 | int retry, ret; | |
1397 | ||
1398 | for (retry = 5; retry; retry--) { | |
1399 | ret = acpi_battery_update(battery, false); | |
1400 | if (!ret) | |
1401 | break; | |
1402 | ||
1403 | msleep(20); | |
1404 | } | |
1405 | return ret; | |
1406 | } | |
1407 | ||
4be44fcd | 1408 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 1409 | { |
4be44fcd | 1410 | int result = 0; |
4be44fcd | 1411 | struct acpi_battery *battery = NULL; |
952c63e9 | 1412 | |
1da177e4 | 1413 | if (!device) |
d550d98d | 1414 | return -EINVAL; |
40e7fcb1 LT |
1415 | |
1416 | if (device->dep_unmet) | |
1417 | return -EPROBE_DEFER; | |
1418 | ||
36bcbec7 | 1419 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 1420 | if (!battery) |
d550d98d | 1421 | return -ENOMEM; |
145def84 | 1422 | battery->device = device; |
1da177e4 LT |
1423 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
1424 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
db89b4f0 | 1425 | device->driver_data = battery; |
038fdea2 | 1426 | mutex_init(&battery->lock); |
69d94ec6 | 1427 | mutex_init(&battery->sysfs_lock); |
952c63e9 | 1428 | if (acpi_has_method(battery->device->handle, "_BIX")) |
c67fcd67 | 1429 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); |
75646e75 LT |
1430 | |
1431 | result = acpi_battery_update_retry(battery); | |
eb03cb02 SH |
1432 | if (result) |
1433 | goto fail; | |
75646e75 | 1434 | |
3a670cc7 LT |
1435 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1436 | result = acpi_battery_add_fs(device); | |
3a670cc7 | 1437 | if (result) { |
3a670cc7 | 1438 | acpi_battery_remove_fs(device); |
3a670cc7 LT |
1439 | goto fail; |
1440 | } | |
6993ce46 | 1441 | #endif |
25be5821 | 1442 | |
dd1fca9e | 1443 | pr_info(PREFIX "%s Slot [%s] (battery %s)\n", |
e80bba4b SH |
1444 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
1445 | device->status.battery_present ? "present" : "absent"); | |
1446 | ||
25be5821 KM |
1447 | battery->pm_nb.notifier_call = battery_notify; |
1448 | register_pm_notifier(&battery->pm_nb); | |
1449 | ||
e0d1f09e ZR |
1450 | device_init_wakeup(&device->dev, 1); |
1451 | ||
d550d98d | 1452 | return result; |
e80bba4b SH |
1453 | |
1454 | fail: | |
1455 | sysfs_remove_battery(battery); | |
1456 | mutex_destroy(&battery->lock); | |
69d94ec6 | 1457 | mutex_destroy(&battery->sysfs_lock); |
e80bba4b SH |
1458 | kfree(battery); |
1459 | return result; | |
1da177e4 LT |
1460 | } |
1461 | ||
51fac838 | 1462 | static int acpi_battery_remove(struct acpi_device *device) |
1da177e4 | 1463 | { |
4be44fcd | 1464 | struct acpi_battery *battery = NULL; |
1da177e4 | 1465 | |
1da177e4 | 1466 | if (!device || !acpi_driver_data(device)) |
d550d98d | 1467 | return -EINVAL; |
e0d1f09e | 1468 | device_init_wakeup(&device->dev, 0); |
50dd0969 | 1469 | battery = acpi_driver_data(device); |
25be5821 | 1470 | unregister_pm_notifier(&battery->pm_nb); |
3a670cc7 LT |
1471 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1472 | acpi_battery_remove_fs(device); | |
1473 | #endif | |
508df92d | 1474 | sysfs_remove_battery(battery); |
038fdea2 | 1475 | mutex_destroy(&battery->lock); |
69d94ec6 | 1476 | mutex_destroy(&battery->sysfs_lock); |
1da177e4 | 1477 | kfree(battery); |
d550d98d | 1478 | return 0; |
1da177e4 LT |
1479 | } |
1480 | ||
90692404 | 1481 | #ifdef CONFIG_PM_SLEEP |
34c4415a | 1482 | /* this is needed to learn about changes made in suspended state */ |
a6f50dc8 | 1483 | static int acpi_battery_resume(struct device *dev) |
34c4415a JK |
1484 | { |
1485 | struct acpi_battery *battery; | |
a6f50dc8 RW |
1486 | |
1487 | if (!dev) | |
34c4415a | 1488 | return -EINVAL; |
a6f50dc8 RW |
1489 | |
1490 | battery = acpi_driver_data(to_acpi_device(dev)); | |
1491 | if (!battery) | |
1492 | return -EINVAL; | |
1493 | ||
f1d4661a | 1494 | battery->update_time = 0; |
9e50bc14 | 1495 | acpi_battery_update(battery, true); |
b6ce4083 | 1496 | return 0; |
34c4415a | 1497 | } |
7f6895c6 SK |
1498 | #else |
1499 | #define acpi_battery_resume NULL | |
90692404 | 1500 | #endif |
34c4415a | 1501 | |
a6f50dc8 RW |
1502 | static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume); |
1503 | ||
aa650bbd AS |
1504 | static struct acpi_driver acpi_battery_driver = { |
1505 | .name = "battery", | |
1506 | .class = ACPI_BATTERY_CLASS, | |
1507 | .ids = battery_device_ids, | |
d9406691 | 1508 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
aa650bbd AS |
1509 | .ops = { |
1510 | .add = acpi_battery_add, | |
aa650bbd | 1511 | .remove = acpi_battery_remove, |
d9406691 | 1512 | .notify = acpi_battery_notify, |
aa650bbd | 1513 | }, |
a6f50dc8 | 1514 | .drv.pm = &acpi_battery_pm, |
aa650bbd AS |
1515 | }; |
1516 | ||
b0cbc861 | 1517 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
1da177e4 | 1518 | { |
dccfae6d | 1519 | unsigned int i; |
479faaf0 LH |
1520 | int result; |
1521 | ||
3f5dc08f | 1522 | dmi_check_system(bat_dmi_table); |
479faaf0 | 1523 | |
ec625a37 CC |
1524 | if (battery_check_pmic) { |
1525 | for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++) | |
1526 | if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) { | |
1527 | pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME | |
1528 | ": found native %s PMIC, not loading\n", | |
1529 | acpi_battery_blacklist[i]); | |
1530 | return; | |
1531 | } | |
1532 | } | |
1533 | ||
3a670cc7 LT |
1534 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1535 | acpi_battery_dir = acpi_lock_battery_dir(); | |
1536 | if (!acpi_battery_dir) | |
1537 | return; | |
1538 | #endif | |
479faaf0 | 1539 | result = acpi_bus_register_driver(&acpi_battery_driver); |
3a670cc7 | 1540 | #ifdef CONFIG_ACPI_PROCFS_POWER |
479faaf0 | 1541 | if (result < 0) |
3a670cc7 LT |
1542 | acpi_unlock_battery_dir(acpi_battery_dir); |
1543 | #endif | |
bc39fbcf | 1544 | battery_driver_registered = (result == 0); |
0f66af53 AV |
1545 | } |
1546 | ||
1547 | static int __init acpi_battery_init(void) | |
1548 | { | |
e234b074 LH |
1549 | if (acpi_disabled) |
1550 | return -ENODEV; | |
1551 | ||
eca21d91 | 1552 | async_cookie = async_schedule(acpi_battery_init_async, NULL); |
d550d98d | 1553 | return 0; |
1da177e4 LT |
1554 | } |
1555 | ||
4be44fcd | 1556 | static void __exit acpi_battery_exit(void) |
1da177e4 | 1557 | { |
5dfa0c73 | 1558 | async_synchronize_cookie(async_cookie + 1); |
fa93854f | 1559 | if (battery_driver_registered) { |
bc39fbcf | 1560 | acpi_bus_unregister_driver(&acpi_battery_driver); |
fa93854f OG |
1561 | battery_hook_exit(); |
1562 | } | |
3a670cc7 | 1563 | #ifdef CONFIG_ACPI_PROCFS_POWER |
bc39fbcf HG |
1564 | if (acpi_battery_dir) |
1565 | acpi_unlock_battery_dir(acpi_battery_dir); | |
3a670cc7 | 1566 | #endif |
1da177e4 LT |
1567 | } |
1568 | ||
1da177e4 LT |
1569 | module_init(acpi_battery_init); |
1570 | module_exit(acpi_battery_exit); |