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