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