]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/battery.c
ACPI: Use struct dev_pm_ops for power management in the battery driver
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / battery.c
CommitLineData
1da177e4 1/*
aa650bbd 2 * battery.c - ACPI Battery Driver (Revision: 2.0)
1da177e4 3 *
aa650bbd
AS
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
1da177e4
LT
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 *
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
f1d4661a 32#include <linux/jiffies.h>
0f66af53 33#include <linux/async.h>
bc76f90b 34#include <linux/dmi.h>
5a0e3ad6 35#include <linux/slab.h>
25be5821 36#include <linux/suspend.h>
d7380965 37
fdcedbba 38#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4
LT
39#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <asm/uaccess.h>
d7380965 42#endif
1da177e4
LT
43
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
d7380965
AS
46#include <linux/power_supply.h>
47
a192a958
LB
48#define PREFIX "ACPI: "
49
1da177e4
LT
50#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51
1da177e4 52#define ACPI_BATTERY_CLASS "battery"
1da177e4 53#define ACPI_BATTERY_DEVICE_NAME "Battery"
1da177e4
LT
54#define ACPI_BATTERY_NOTIFY_STATUS 0x80
55#define ACPI_BATTERY_NOTIFY_INFO 0x81
c67fcd67 56#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
1da177e4 57
ae6f6187
LT
58/* Battery power unit: 0 means mW, 1 means mA */
59#define ACPI_BATTERY_POWER_UNIT_MA 1
60
1da177e4 61#define _COMPONENT ACPI_BATTERY_COMPONENT
b6ce4083 62
f52fd66d 63ACPI_MODULE_NAME("battery");
1da177e4 64
f52fd66d 65MODULE_AUTHOR("Paul Diefenbaugh");
aa650bbd 66MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
7cda93e0 67MODULE_DESCRIPTION("ACPI Battery Driver");
1da177e4
LT
68MODULE_LICENSE("GPL");
69
f1d4661a
AS
70static unsigned int cache_time = 1000;
71module_param(cache_time, uint, 0644);
72MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
b6ce4083 73
fdcedbba 74#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832
RT
75extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77
d7380965
AS
78enum acpi_battery_files {
79 info_tag = 0,
80 state_tag,
81 alarm_tag,
82 ACPI_BATTERY_NUMFILES,
83};
84
85#endif
86
1ba90e3a
TR
87static const struct acpi_device_id battery_device_ids[] = {
88 {"PNP0C0A", 0},
89 {"", 0},
90};
1ba90e3a 91
aa650bbd 92MODULE_DEVICE_TABLE(acpi, battery_device_ids);
1da177e4 93
7b3bcc4a
AS
94enum {
95 ACPI_BATTERY_ALARM_PRESENT,
c67fcd67 96 ACPI_BATTERY_XINFO_PRESENT,
557d5868 97 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
7b3bcc4a 98};
78490d82 99
1da177e4 100struct acpi_battery {
038fdea2 101 struct mutex lock;
69d94ec6 102 struct mutex sysfs_lock;
d7380965 103 struct power_supply bat;
f1d4661a 104 struct acpi_device *device;
25be5821 105 struct notifier_block pm_nb;
f1d4661a 106 unsigned long update_time;
7faa144a 107 int rate_now;
d7380965
AS
108 int capacity_now;
109 int voltage_now;
038fdea2 110 int design_capacity;
d7380965 111 int full_charge_capacity;
038fdea2
AS
112 int technology;
113 int design_voltage;
114 int design_capacity_warning;
115 int design_capacity_low;
c67fcd67
AS
116 int cycle_count;
117 int measurement_accuracy;
118 int max_sampling_time;
119 int min_sampling_time;
120 int max_averaging_interval;
121 int min_averaging_interval;
038fdea2
AS
122 int capacity_granularity_1;
123 int capacity_granularity_2;
f1d4661a 124 int alarm;
038fdea2
AS
125 char model_number[32];
126 char serial_number[32];
127 char type[32];
128 char oem_info[32];
f1d4661a
AS
129 int state;
130 int power_unit;
7b3bcc4a 131 unsigned long flags;
1da177e4
LT
132};
133
497888cf 134#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
d7380965 135
78490d82 136inline int acpi_battery_present(struct acpi_battery *battery)
b6ce4083 137{
78490d82
AS
138 return battery->device->status.battery_present;
139}
038fdea2 140
d7380965
AS
141static int acpi_battery_technology(struct acpi_battery *battery)
142{
143 if (!strcasecmp("NiCd", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_NiCd;
145 if (!strcasecmp("NiMH", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_NiMH;
147 if (!strcasecmp("LION", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_LION;
ad40e68b 149 if (!strncasecmp("LI-ION", battery->type, 6))
0bde7eee 150 return POWER_SUPPLY_TECHNOLOGY_LION;
d7380965
AS
151 if (!strcasecmp("LiP", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LIPO;
153 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
154}
155
9104476e 156static int acpi_battery_get_state(struct acpi_battery *battery);
b19073a0 157
56f382a0
RH
158static int acpi_battery_is_charged(struct acpi_battery *battery)
159{
160 /* either charging or discharging */
161 if (battery->state != 0)
162 return 0;
163
164 /* battery not reporting charge */
165 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
166 battery->capacity_now == 0)
167 return 0;
168
169 /* good batteries update full_charge as the batteries degrade */
170 if (battery->full_charge_capacity == battery->capacity_now)
171 return 1;
172
173 /* fallback to using design values for broken batteries */
174 if (battery->design_capacity == battery->capacity_now)
175 return 1;
176
177 /* we don't do any sort of metric based on percentages */
178 return 0;
179}
180
d7380965
AS
181static int acpi_battery_get_property(struct power_supply *psy,
182 enum power_supply_property psp,
183 union power_supply_propval *val)
184{
a1b4bd69 185 int ret = 0;
d7380965
AS
186 struct acpi_battery *battery = to_acpi_battery(psy);
187
9104476e
AS
188 if (acpi_battery_present(battery)) {
189 /* run battery update only if it is present */
190 acpi_battery_get_state(battery);
191 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
d7380965
AS
192 return -ENODEV;
193 switch (psp) {
194 case POWER_SUPPLY_PROP_STATUS:
195 if (battery->state & 0x01)
196 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
197 else if (battery->state & 0x02)
198 val->intval = POWER_SUPPLY_STATUS_CHARGING;
56f382a0 199 else if (acpi_battery_is_charged(battery))
d7380965 200 val->intval = POWER_SUPPLY_STATUS_FULL;
4c41d3ad
RD
201 else
202 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
d7380965
AS
203 break;
204 case POWER_SUPPLY_PROP_PRESENT:
205 val->intval = acpi_battery_present(battery);
206 break;
207 case POWER_SUPPLY_PROP_TECHNOLOGY:
208 val->intval = acpi_battery_technology(battery);
209 break;
c67fcd67
AS
210 case POWER_SUPPLY_PROP_CYCLE_COUNT:
211 val->intval = battery->cycle_count;
212 break;
d7380965 213 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
a1b4bd69
RW
214 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
215 ret = -ENODEV;
216 else
217 val->intval = battery->design_voltage * 1000;
d7380965
AS
218 break;
219 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
a1b4bd69
RW
220 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
221 ret = -ENODEV;
222 else
223 val->intval = battery->voltage_now * 1000;
d7380965
AS
224 break;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
7faa144a 226 case POWER_SUPPLY_PROP_POWER_NOW:
a1b4bd69
RW
227 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
228 ret = -ENODEV;
229 else
230 val->intval = battery->rate_now * 1000;
d7380965
AS
231 break;
232 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
233 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
a1b4bd69
RW
234 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
235 ret = -ENODEV;
236 else
237 val->intval = battery->design_capacity * 1000;
d7380965
AS
238 break;
239 case POWER_SUPPLY_PROP_CHARGE_FULL:
240 case POWER_SUPPLY_PROP_ENERGY_FULL:
a1b4bd69
RW
241 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
242 ret = -ENODEV;
243 else
244 val->intval = battery->full_charge_capacity * 1000;
d7380965
AS
245 break;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
a1b4bd69
RW
248 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
249 ret = -ENODEV;
250 else
251 val->intval = battery->capacity_now * 1000;
d7380965
AS
252 break;
253 case POWER_SUPPLY_PROP_MODEL_NAME:
254 val->strval = battery->model_number;
255 break;
256 case POWER_SUPPLY_PROP_MANUFACTURER:
257 val->strval = battery->oem_info;
258 break;
7c2670bb 259 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
260 val->strval = battery->serial_number;
261 break;
d7380965 262 default:
a1b4bd69 263 ret = -EINVAL;
d7380965 264 }
a1b4bd69 265 return ret;
d7380965
AS
266}
267
268static enum power_supply_property charge_battery_props[] = {
269 POWER_SUPPLY_PROP_STATUS,
270 POWER_SUPPLY_PROP_PRESENT,
271 POWER_SUPPLY_PROP_TECHNOLOGY,
c67fcd67 272 POWER_SUPPLY_PROP_CYCLE_COUNT,
d7380965
AS
273 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
274 POWER_SUPPLY_PROP_VOLTAGE_NOW,
275 POWER_SUPPLY_PROP_CURRENT_NOW,
276 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
277 POWER_SUPPLY_PROP_CHARGE_FULL,
278 POWER_SUPPLY_PROP_CHARGE_NOW,
279 POWER_SUPPLY_PROP_MODEL_NAME,
280 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 281 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965
AS
282};
283
284static enum power_supply_property energy_battery_props[] = {
285 POWER_SUPPLY_PROP_STATUS,
286 POWER_SUPPLY_PROP_PRESENT,
287 POWER_SUPPLY_PROP_TECHNOLOGY,
c67fcd67 288 POWER_SUPPLY_PROP_CYCLE_COUNT,
d7380965
AS
289 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
290 POWER_SUPPLY_PROP_VOLTAGE_NOW,
7faa144a 291 POWER_SUPPLY_PROP_POWER_NOW,
d7380965
AS
292 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
293 POWER_SUPPLY_PROP_ENERGY_FULL,
294 POWER_SUPPLY_PROP_ENERGY_NOW,
295 POWER_SUPPLY_PROP_MODEL_NAME,
296 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 297 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965
AS
298};
299
fdcedbba 300#ifdef CONFIG_ACPI_PROCFS_POWER
f1d4661a 301inline char *acpi_battery_units(struct acpi_battery *battery)
78490d82 302{
ae6f6187
LT
303 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
304 "mA" : "mW";
b6ce4083 305}
d7380965 306#endif
b6ce4083 307
78490d82
AS
308/* --------------------------------------------------------------------------
309 Battery Management
310 -------------------------------------------------------------------------- */
038fdea2
AS
311struct acpi_offsets {
312 size_t offset; /* offset inside struct acpi_sbs_battery */
313 u8 mode; /* int or string? */
314};
b6ce4083 315
038fdea2
AS
316static struct acpi_offsets state_offsets[] = {
317 {offsetof(struct acpi_battery, state), 0},
7faa144a 318 {offsetof(struct acpi_battery, rate_now), 0},
d7380965
AS
319 {offsetof(struct acpi_battery, capacity_now), 0},
320 {offsetof(struct acpi_battery, voltage_now), 0},
038fdea2 321};
b6ce4083 322
038fdea2
AS
323static struct acpi_offsets info_offsets[] = {
324 {offsetof(struct acpi_battery, power_unit), 0},
325 {offsetof(struct acpi_battery, design_capacity), 0},
d7380965 326 {offsetof(struct acpi_battery, full_charge_capacity), 0},
038fdea2
AS
327 {offsetof(struct acpi_battery, technology), 0},
328 {offsetof(struct acpi_battery, design_voltage), 0},
329 {offsetof(struct acpi_battery, design_capacity_warning), 0},
330 {offsetof(struct acpi_battery, design_capacity_low), 0},
331 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
332 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
333 {offsetof(struct acpi_battery, model_number), 1},
334 {offsetof(struct acpi_battery, serial_number), 1},
335 {offsetof(struct acpi_battery, type), 1},
336 {offsetof(struct acpi_battery, oem_info), 1},
337};
b6ce4083 338
c67fcd67
AS
339static struct acpi_offsets extended_info_offsets[] = {
340 {offsetof(struct acpi_battery, power_unit), 0},
341 {offsetof(struct acpi_battery, design_capacity), 0},
342 {offsetof(struct acpi_battery, full_charge_capacity), 0},
343 {offsetof(struct acpi_battery, technology), 0},
344 {offsetof(struct acpi_battery, design_voltage), 0},
345 {offsetof(struct acpi_battery, design_capacity_warning), 0},
346 {offsetof(struct acpi_battery, design_capacity_low), 0},
347 {offsetof(struct acpi_battery, cycle_count), 0},
348 {offsetof(struct acpi_battery, measurement_accuracy), 0},
349 {offsetof(struct acpi_battery, max_sampling_time), 0},
350 {offsetof(struct acpi_battery, min_sampling_time), 0},
351 {offsetof(struct acpi_battery, max_averaging_interval), 0},
352 {offsetof(struct acpi_battery, min_averaging_interval), 0},
353 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
354 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
355 {offsetof(struct acpi_battery, model_number), 1},
356 {offsetof(struct acpi_battery, serial_number), 1},
357 {offsetof(struct acpi_battery, type), 1},
358 {offsetof(struct acpi_battery, oem_info), 1},
359};
360
038fdea2
AS
361static int extract_package(struct acpi_battery *battery,
362 union acpi_object *package,
363 struct acpi_offsets *offsets, int num)
364{
106449e8 365 int i;
038fdea2
AS
366 union acpi_object *element;
367 if (package->type != ACPI_TYPE_PACKAGE)
368 return -EFAULT;
369 for (i = 0; i < num; ++i) {
370 if (package->package.count <= i)
371 return -EFAULT;
372 element = &package->package.elements[i];
373 if (offsets[i].mode) {
106449e8
AS
374 u8 *ptr = (u8 *)battery + offsets[i].offset;
375 if (element->type == ACPI_TYPE_STRING ||
376 element->type == ACPI_TYPE_BUFFER)
377 strncpy(ptr, element->string.pointer, 32);
378 else if (element->type == ACPI_TYPE_INTEGER) {
379 strncpy(ptr, (u8 *)&element->integer.value,
439913ff
LM
380 sizeof(u64));
381 ptr[sizeof(u64)] = 0;
b8a1bdb1
AS
382 } else
383 *ptr = 0; /* don't have value */
038fdea2 384 } else {
b8a1bdb1
AS
385 int *x = (int *)((u8 *)battery + offsets[i].offset);
386 *x = (element->type == ACPI_TYPE_INTEGER) ?
387 element->integer.value : -1;
038fdea2 388 }
b6ce4083 389 }
b6ce4083
VL
390 return 0;
391}
392
393static int acpi_battery_get_status(struct acpi_battery *battery)
394{
aa650bbd 395 if (acpi_bus_get_status(battery->device)) {
b6ce4083
VL
396 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
397 return -ENODEV;
398 }
aa650bbd 399 return 0;
b6ce4083
VL
400}
401
402static int acpi_battery_get_info(struct acpi_battery *battery)
1da177e4 403{
aa650bbd 404 int result = -EFAULT;
4be44fcd 405 acpi_status status = 0;
c67fcd67
AS
406 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
407 "_BIX" : "_BIF";
408
4be44fcd 409 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 410
b6ce4083
VL
411 if (!acpi_battery_present(battery))
412 return 0;
038fdea2 413 mutex_lock(&battery->lock);
c67fcd67
AS
414 status = acpi_evaluate_object(battery->device->handle, name,
415 NULL, &buffer);
038fdea2 416 mutex_unlock(&battery->lock);
aa650bbd 417
1da177e4 418 if (ACPI_FAILURE(status)) {
c67fcd67 419 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
d550d98d 420 return -ENODEV;
1da177e4 421 }
c67fcd67
AS
422 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
423 result = extract_package(battery, buffer.pointer,
424 extended_info_offsets,
425 ARRAY_SIZE(extended_info_offsets));
426 else
427 result = extract_package(battery, buffer.pointer,
428 info_offsets, ARRAY_SIZE(info_offsets));
78490d82 429 kfree(buffer.pointer);
557d5868
ZR
430 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
431 battery->full_charge_capacity = battery->design_capacity;
d550d98d 432 return result;
1da177e4
LT
433}
434
b6ce4083 435static int acpi_battery_get_state(struct acpi_battery *battery)
1da177e4 436{
4be44fcd
LB
437 int result = 0;
438 acpi_status status = 0;
439 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 440
b6ce4083
VL
441 if (!acpi_battery_present(battery))
442 return 0;
1da177e4 443
f1d4661a
AS
444 if (battery->update_time &&
445 time_before(jiffies, battery->update_time +
446 msecs_to_jiffies(cache_time)))
447 return 0;
448
038fdea2 449 mutex_lock(&battery->lock);
f1d4661a 450 status = acpi_evaluate_object(battery->device->handle, "_BST",
038fdea2
AS
451 NULL, &buffer);
452 mutex_unlock(&battery->lock);
5b31d895 453
1da177e4 454 if (ACPI_FAILURE(status)) {
a6fc6720 455 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
d550d98d 456 return -ENODEV;
1da177e4 457 }
aa650bbd 458
038fdea2
AS
459 result = extract_package(battery, buffer.pointer,
460 state_offsets, ARRAY_SIZE(state_offsets));
f1d4661a 461 battery->update_time = jiffies;
78490d82 462 kfree(buffer.pointer);
bc76f90b 463
55003b21
LT
464 /* For buggy DSDTs that report negative 16-bit values for either
465 * charging or discharging current and/or report 0 as 65536
466 * due to bad math.
467 */
468 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
469 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
470 (s16)(battery->rate_now) < 0) {
bc76f90b 471 battery->rate_now = abs((s16)battery->rate_now);
55003b21
LT
472 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
473 " invalid.\n");
474 }
bc76f90b 475
557d5868
ZR
476 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
477 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
478 battery->capacity_now = (battery->capacity_now *
479 battery->full_charge_capacity) / 100;
b6ce4083
VL
480 return result;
481}
1da177e4 482
aa650bbd 483static int acpi_battery_set_alarm(struct acpi_battery *battery)
1da177e4 484{
4be44fcd 485 acpi_status status = 0;
aa650bbd 486 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
4be44fcd 487 struct acpi_object_list arg_list = { 1, &arg0 };
1da177e4 488
c67fcd67 489 if (!acpi_battery_present(battery) ||
7b3bcc4a 490 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
d550d98d 491 return -ENODEV;
1da177e4 492
aa650bbd 493 arg0.integer.value = battery->alarm;
1da177e4 494
038fdea2 495 mutex_lock(&battery->lock);
f1d4661a
AS
496 status = acpi_evaluate_object(battery->device->handle, "_BTP",
497 &arg_list, NULL);
038fdea2 498 mutex_unlock(&battery->lock);
aa650bbd 499
1da177e4 500 if (ACPI_FAILURE(status))
d550d98d 501 return -ENODEV;
1da177e4 502
aa650bbd 503 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
d550d98d 504 return 0;
1da177e4
LT
505}
506
b6ce4083 507static int acpi_battery_init_alarm(struct acpi_battery *battery)
1da177e4 508{
4be44fcd
LB
509 acpi_status status = AE_OK;
510 acpi_handle handle = NULL;
4be44fcd 511
b6ce4083 512 /* See if alarms are supported, and if so, set default */
f1d4661a
AS
513 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
514 if (ACPI_FAILURE(status)) {
7b3bcc4a 515 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
f1d4661a 516 return 0;
b6ce4083 517 }
7b3bcc4a 518 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
f1d4661a
AS
519 if (!battery->alarm)
520 battery->alarm = battery->design_capacity_warning;
aa650bbd 521 return acpi_battery_set_alarm(battery);
b6ce4083 522}
1da177e4 523
508df92d
AB
524static ssize_t acpi_battery_alarm_show(struct device *dev,
525 struct device_attribute *attr,
526 char *buf)
527{
528 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
529 return sprintf(buf, "%d\n", battery->alarm * 1000);
530}
531
532static ssize_t acpi_battery_alarm_store(struct device *dev,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
535{
536 unsigned long x;
537 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
538 if (sscanf(buf, "%ld\n", &x) == 1)
539 battery->alarm = x/1000;
540 if (acpi_battery_present(battery))
541 acpi_battery_set_alarm(battery);
542 return count;
543}
544
545static struct device_attribute alarm_attr = {
01e8ef11 546 .attr = {.name = "alarm", .mode = 0644},
508df92d
AB
547 .show = acpi_battery_alarm_show,
548 .store = acpi_battery_alarm_store,
549};
550
551static int sysfs_add_battery(struct acpi_battery *battery)
552{
553 int result;
554
ae6f6187 555 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
508df92d
AB
556 battery->bat.properties = charge_battery_props;
557 battery->bat.num_properties =
558 ARRAY_SIZE(charge_battery_props);
559 } else {
560 battery->bat.properties = energy_battery_props;
561 battery->bat.num_properties =
562 ARRAY_SIZE(energy_battery_props);
563 }
564
565 battery->bat.name = acpi_device_bid(battery->device);
566 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
567 battery->bat.get_property = acpi_battery_get_property;
568
569 result = power_supply_register(&battery->device->dev, &battery->bat);
570 if (result)
571 return result;
572 return device_create_file(battery->bat.dev, &alarm_attr);
573}
574
575static void sysfs_remove_battery(struct acpi_battery *battery)
576{
69d94ec6 577 mutex_lock(&battery->sysfs_lock);
9c921c22 578 if (!battery->bat.dev) {
69d94ec6 579 mutex_unlock(&battery->sysfs_lock);
508df92d 580 return;
9c921c22
LT
581 }
582
508df92d
AB
583 device_remove_file(battery->bat.dev, &alarm_attr);
584 power_supply_unregister(&battery->bat);
9104476e 585 battery->bat.dev = NULL;
69d94ec6 586 mutex_unlock(&battery->sysfs_lock);
bc76f90b
HM
587}
588
557d5868
ZR
589/*
590 * According to the ACPI spec, some kinds of primary batteries can
591 * report percentage battery remaining capacity directly to OS.
592 * In this case, it reports the Last Full Charged Capacity == 100
593 * and BatteryPresentRate == 0xFFFFFFFF.
594 *
595 * Now we found some battery reports percentage remaining capacity
596 * even if it's rechargeable.
597 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
598 *
599 * Handle this correctly so that they won't break userspace.
600 */
7b78622d 601static void acpi_battery_quirks(struct acpi_battery *battery)
557d5868
ZR
602{
603 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
604 return ;
605
606 if (battery->full_charge_capacity == 100 &&
607 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
608 battery->capacity_now >=0 && battery->capacity_now <= 100) {
609 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
610 battery->full_charge_capacity = battery->design_capacity;
611 battery->capacity_now = (battery->capacity_now *
612 battery->full_charge_capacity) / 100;
613 }
614}
615
f1d4661a 616static int acpi_battery_update(struct acpi_battery *battery)
b6ce4083 617{
50b17851 618 int result, old_present = acpi_battery_present(battery);
97749cd9 619 result = acpi_battery_get_status(battery);
508df92d 620 if (result)
b6ce4083 621 return result;
508df92d
AB
622 if (!acpi_battery_present(battery)) {
623 sysfs_remove_battery(battery);
97749cd9 624 battery->update_time = 0;
508df92d 625 return 0;
b6ce4083 626 }
50b17851
AS
627 if (!battery->update_time ||
628 old_present != acpi_battery_present(battery)) {
97749cd9
AS
629 result = acpi_battery_get_info(battery);
630 if (result)
631 return result;
632 acpi_battery_init_alarm(battery);
633 }
eb03cb02
SH
634 if (!battery->bat.dev) {
635 result = sysfs_add_battery(battery);
636 if (result)
637 return result;
638 }
557d5868 639 result = acpi_battery_get_state(battery);
7b78622d 640 acpi_battery_quirks(battery);
557d5868 641 return result;
4bd35cdb
VL
642}
643
da8aeb92
RW
644static void acpi_battery_refresh(struct acpi_battery *battery)
645{
c5971456
AW
646 int power_unit;
647
da8aeb92
RW
648 if (!battery->bat.dev)
649 return;
650
c5971456
AW
651 power_unit = battery->power_unit;
652
da8aeb92 653 acpi_battery_get_info(battery);
c5971456
AW
654
655 if (power_unit == battery->power_unit)
656 return;
657
658 /* The battery has changed its reporting units. */
da8aeb92
RW
659 sysfs_remove_battery(battery);
660 sysfs_add_battery(battery);
661}
662
1da177e4
LT
663/* --------------------------------------------------------------------------
664 FS Interface (/proc)
665 -------------------------------------------------------------------------- */
666
fdcedbba 667#ifdef CONFIG_ACPI_PROCFS_POWER
4be44fcd 668static struct proc_dir_entry *acpi_battery_dir;
b6ce4083 669
78490d82 670static int acpi_battery_print_info(struct seq_file *seq, int result)
1da177e4 671{
50dd0969 672 struct acpi_battery *battery = seq->private;
1da177e4 673
b6ce4083 674 if (result)
1da177e4
LT
675 goto end;
676
aa650bbd
AS
677 seq_printf(seq, "present: %s\n",
678 acpi_battery_present(battery)?"yes":"no");
679 if (!acpi_battery_present(battery))
1da177e4 680 goto end;
038fdea2 681 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
682 seq_printf(seq, "design capacity: unknown\n");
683 else
684 seq_printf(seq, "design capacity: %d %sh\n",
aa650bbd
AS
685 battery->design_capacity,
686 acpi_battery_units(battery));
1da177e4 687
d7380965 688 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
689 seq_printf(seq, "last full capacity: unknown\n");
690 else
691 seq_printf(seq, "last full capacity: %d %sh\n",
d7380965 692 battery->full_charge_capacity,
aa650bbd
AS
693 acpi_battery_units(battery));
694
695 seq_printf(seq, "battery technology: %srechargeable\n",
696 (!battery->technology)?"non-":"");
1da177e4 697
038fdea2 698 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
699 seq_printf(seq, "design voltage: unknown\n");
700 else
701 seq_printf(seq, "design voltage: %d mV\n",
aa650bbd 702 battery->design_voltage);
1da177e4 703 seq_printf(seq, "design capacity warning: %d %sh\n",
aa650bbd
AS
704 battery->design_capacity_warning,
705 acpi_battery_units(battery));
1da177e4 706 seq_printf(seq, "design capacity low: %d %sh\n",
aa650bbd
AS
707 battery->design_capacity_low,
708 acpi_battery_units(battery));
c67fcd67 709 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
1da177e4 710 seq_printf(seq, "capacity granularity 1: %d %sh\n",
aa650bbd
AS
711 battery->capacity_granularity_1,
712 acpi_battery_units(battery));
1da177e4 713 seq_printf(seq, "capacity granularity 2: %d %sh\n",
aa650bbd
AS
714 battery->capacity_granularity_2,
715 acpi_battery_units(battery));
038fdea2
AS
716 seq_printf(seq, "model number: %s\n", battery->model_number);
717 seq_printf(seq, "serial number: %s\n", battery->serial_number);
718 seq_printf(seq, "battery type: %s\n", battery->type);
719 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
4be44fcd 720 end:
b6ce4083
VL
721 if (result)
722 seq_printf(seq, "ERROR: Unable to read battery info\n");
b6ce4083
VL
723 return result;
724}
725
78490d82 726static int acpi_battery_print_state(struct seq_file *seq, int result)
1da177e4 727{
50dd0969 728 struct acpi_battery *battery = seq->private;
1da177e4 729
b6ce4083 730 if (result)
1da177e4
LT
731 goto end;
732
aa650bbd
AS
733 seq_printf(seq, "present: %s\n",
734 acpi_battery_present(battery)?"yes":"no");
735 if (!acpi_battery_present(battery))
1da177e4 736 goto end;
b6ce4083 737
aa650bbd
AS
738 seq_printf(seq, "capacity state: %s\n",
739 (battery->state & 0x04)?"critical":"ok");
740 if ((battery->state & 0x01) && (battery->state & 0x02))
4be44fcd
LB
741 seq_printf(seq,
742 "charging state: charging/discharging\n");
aa650bbd 743 else if (battery->state & 0x01)
1da177e4 744 seq_printf(seq, "charging state: discharging\n");
038fdea2 745 else if (battery->state & 0x02)
1da177e4 746 seq_printf(seq, "charging state: charging\n");
aa650bbd 747 else
1da177e4 748 seq_printf(seq, "charging state: charged\n");
1da177e4 749
7faa144a 750 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
751 seq_printf(seq, "present rate: unknown\n");
752 else
753 seq_printf(seq, "present rate: %d %s\n",
7faa144a 754 battery->rate_now, acpi_battery_units(battery));
1da177e4 755
d7380965 756 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
757 seq_printf(seq, "remaining capacity: unknown\n");
758 else
759 seq_printf(seq, "remaining capacity: %d %sh\n",
d7380965
AS
760 battery->capacity_now, acpi_battery_units(battery));
761 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
762 seq_printf(seq, "present voltage: unknown\n");
763 else
764 seq_printf(seq, "present voltage: %d mV\n",
d7380965 765 battery->voltage_now);
4be44fcd 766 end:
aa650bbd 767 if (result)
b6ce4083 768 seq_printf(seq, "ERROR: Unable to read battery state\n");
b6ce4083
VL
769
770 return result;
771}
772
78490d82 773static int acpi_battery_print_alarm(struct seq_file *seq, int result)
1da177e4 774{
50dd0969 775 struct acpi_battery *battery = seq->private;
1da177e4 776
b6ce4083 777 if (result)
1da177e4
LT
778 goto end;
779
b6ce4083 780 if (!acpi_battery_present(battery)) {
1da177e4
LT
781 seq_printf(seq, "present: no\n");
782 goto end;
783 }
1da177e4
LT
784 seq_printf(seq, "alarm: ");
785 if (!battery->alarm)
786 seq_printf(seq, "unsupported\n");
787 else
aa650bbd
AS
788 seq_printf(seq, "%u %sh\n", battery->alarm,
789 acpi_battery_units(battery));
4be44fcd 790 end:
b6ce4083
VL
791 if (result)
792 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
b6ce4083
VL
793 return result;
794}
795
f1d4661a
AS
796static ssize_t acpi_battery_write_alarm(struct file *file,
797 const char __user * buffer,
798 size_t count, loff_t * ppos)
1da177e4 799{
4be44fcd
LB
800 int result = 0;
801 char alarm_string[12] = { '\0' };
50dd0969
JE
802 struct seq_file *m = file->private_data;
803 struct acpi_battery *battery = m->private;
1da177e4 804
1da177e4 805 if (!battery || (count > sizeof(alarm_string) - 1))
d550d98d 806 return -EINVAL;
b6ce4083
VL
807 if (!acpi_battery_present(battery)) {
808 result = -ENODEV;
809 goto end;
810 }
b6ce4083
VL
811 if (copy_from_user(alarm_string, buffer, count)) {
812 result = -EFAULT;
813 goto end;
814 }
1da177e4 815 alarm_string[count] = '\0';
f1d4661a 816 battery->alarm = simple_strtol(alarm_string, NULL, 0);
aa650bbd 817 result = acpi_battery_set_alarm(battery);
b6ce4083 818 end:
b6ce4083 819 if (!result)
f1d4661a 820 return count;
78490d82
AS
821 return result;
822}
823
824typedef int(*print_func)(struct seq_file *seq, int result);
aa650bbd
AS
825
826static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
827 acpi_battery_print_info,
828 acpi_battery_print_state,
829 acpi_battery_print_alarm,
78490d82 830};
b6ce4083 831
78490d82
AS
832static int acpi_battery_read(int fid, struct seq_file *seq)
833{
834 struct acpi_battery *battery = seq->private;
f1d4661a 835 int result = acpi_battery_update(battery);
aa650bbd 836 return acpi_print_funcs[fid](seq, result);
78490d82
AS
837}
838
aa650bbd
AS
839#define DECLARE_FILE_FUNCTIONS(_name) \
840static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
841{ \
842 return acpi_battery_read(_name##_tag, seq); \
843} \
844static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
845{ \
846 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
78490d82
AS
847}
848
aa650bbd
AS
849DECLARE_FILE_FUNCTIONS(info);
850DECLARE_FILE_FUNCTIONS(state);
851DECLARE_FILE_FUNCTIONS(alarm);
852
853#undef DECLARE_FILE_FUNCTIONS
854
855#define FILE_DESCRIPTION_RO(_name) \
856 { \
857 .name = __stringify(_name), \
858 .mode = S_IRUGO, \
859 .ops = { \
860 .open = acpi_battery_##_name##_open_fs, \
861 .read = seq_read, \
862 .llseek = seq_lseek, \
863 .release = single_release, \
864 .owner = THIS_MODULE, \
865 }, \
866 }
78490d82 867
aa650bbd
AS
868#define FILE_DESCRIPTION_RW(_name) \
869 { \
870 .name = __stringify(_name), \
871 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
872 .ops = { \
873 .open = acpi_battery_##_name##_open_fs, \
874 .read = seq_read, \
875 .llseek = seq_lseek, \
876 .write = acpi_battery_write_##_name, \
877 .release = single_release, \
878 .owner = THIS_MODULE, \
879 }, \
880 }
1da177e4 881
9c8b04be 882static const struct battery_file {
78490d82 883 struct file_operations ops;
d161a13f 884 umode_t mode;
070d8eb1 885 const char *name;
78490d82 886} acpi_battery_file[] = {
aa650bbd
AS
887 FILE_DESCRIPTION_RO(info),
888 FILE_DESCRIPTION_RO(state),
889 FILE_DESCRIPTION_RW(alarm),
1da177e4
LT
890};
891
aa650bbd
AS
892#undef FILE_DESCRIPTION_RO
893#undef FILE_DESCRIPTION_RW
894
4be44fcd 895static int acpi_battery_add_fs(struct acpi_device *device)
1da177e4 896{
4be44fcd 897 struct proc_dir_entry *entry = NULL;
78490d82 898 int i;
1da177e4 899
6d855fcd
ZR
900 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
901 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1da177e4
LT
902 if (!acpi_device_dir(device)) {
903 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 904 acpi_battery_dir);
1da177e4 905 if (!acpi_device_dir(device))
d550d98d 906 return -ENODEV;
1da177e4
LT
907 }
908
78490d82 909 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
cf7acfab
DL
910 entry = proc_create_data(acpi_battery_file[i].name,
911 acpi_battery_file[i].mode,
912 acpi_device_dir(device),
913 &acpi_battery_file[i].ops,
914 acpi_driver_data(device));
78490d82
AS
915 if (!entry)
916 return -ENODEV;
1da177e4 917 }
d550d98d 918 return 0;
1da177e4
LT
919}
920
aa650bbd 921static void acpi_battery_remove_fs(struct acpi_device *device)
1da177e4 922{
78490d82 923 int i;
aa650bbd
AS
924 if (!acpi_device_dir(device))
925 return;
926 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
927 remove_proc_entry(acpi_battery_file[i].name,
1da177e4 928 acpi_device_dir(device));
1da177e4 929
aa650bbd
AS
930 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
931 acpi_device_dir(device) = NULL;
1da177e4
LT
932}
933
d7380965 934#endif
3e58ea0d 935
1da177e4
LT
936/* --------------------------------------------------------------------------
937 Driver Interface
938 -------------------------------------------------------------------------- */
939
d9406691 940static void acpi_battery_notify(struct acpi_device *device, u32 event)
1da177e4 941{
d9406691 942 struct acpi_battery *battery = acpi_driver_data(device);
153e500f 943 struct device *old;
d9406691 944
1da177e4 945 if (!battery)
d550d98d 946 return;
153e500f 947 old = battery->bat.dev;
da8aeb92
RW
948 if (event == ACPI_BATTERY_NOTIFY_INFO)
949 acpi_battery_refresh(battery);
f1d4661a
AS
950 acpi_battery_update(battery);
951 acpi_bus_generate_proc_event(device, event,
952 acpi_battery_present(battery));
953 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 954 dev_name(&device->dev), event,
9ea7d575 955 acpi_battery_present(battery));
2345baf4 956 /* acpi_battery_update could remove power_supply object */
153e500f 957 if (old && battery->bat.dev)
f79e1cec 958 power_supply_changed(&battery->bat);
1da177e4
LT
959}
960
25be5821
KM
961static int battery_notify(struct notifier_block *nb,
962 unsigned long mode, void *_unused)
963{
964 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
965 pm_nb);
966 switch (mode) {
d5a5911b 967 case PM_POST_HIBERNATION:
25be5821 968 case PM_POST_SUSPEND:
6e17fb6a
LT
969 if (battery->bat.dev) {
970 sysfs_remove_battery(battery);
971 sysfs_add_battery(battery);
972 }
25be5821
KM
973 break;
974 }
975
976 return 0;
977}
978
4be44fcd 979static int acpi_battery_add(struct acpi_device *device)
1da177e4 980{
4be44fcd 981 int result = 0;
4be44fcd 982 struct acpi_battery *battery = NULL;
c67fcd67 983 acpi_handle handle;
1da177e4 984 if (!device)
d550d98d 985 return -EINVAL;
36bcbec7 986 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1da177e4 987 if (!battery)
d550d98d 988 return -ENOMEM;
145def84 989 battery->device = device;
1da177e4
LT
990 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
991 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
db89b4f0 992 device->driver_data = battery;
038fdea2 993 mutex_init(&battery->lock);
69d94ec6 994 mutex_init(&battery->sysfs_lock);
c67fcd67
AS
995 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
996 "_BIX", &handle)))
997 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
eb03cb02
SH
998 result = acpi_battery_update(battery);
999 if (result)
1000 goto fail;
fdcedbba 1001#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4 1002 result = acpi_battery_add_fs(device);
d7380965 1003#endif
e80bba4b 1004 if (result) {
fdcedbba 1005#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4 1006 acpi_battery_remove_fs(device);
d7380965 1007#endif
e80bba4b 1008 goto fail;
1da177e4 1009 }
25be5821 1010
e80bba4b
SH
1011 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1012 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1013 device->status.battery_present ? "present" : "absent");
1014
25be5821
KM
1015 battery->pm_nb.notifier_call = battery_notify;
1016 register_pm_notifier(&battery->pm_nb);
1017
d550d98d 1018 return result;
e80bba4b
SH
1019
1020fail:
1021 sysfs_remove_battery(battery);
1022 mutex_destroy(&battery->lock);
69d94ec6 1023 mutex_destroy(&battery->sysfs_lock);
e80bba4b
SH
1024 kfree(battery);
1025 return result;
1da177e4
LT
1026}
1027
4be44fcd 1028static int acpi_battery_remove(struct acpi_device *device, int type)
1da177e4 1029{
4be44fcd 1030 struct acpi_battery *battery = NULL;
1da177e4 1031
1da177e4 1032 if (!device || !acpi_driver_data(device))
d550d98d 1033 return -EINVAL;
50dd0969 1034 battery = acpi_driver_data(device);
25be5821 1035 unregister_pm_notifier(&battery->pm_nb);
fdcedbba 1036#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4 1037 acpi_battery_remove_fs(device);
d7380965 1038#endif
508df92d 1039 sysfs_remove_battery(battery);
038fdea2 1040 mutex_destroy(&battery->lock);
69d94ec6 1041 mutex_destroy(&battery->sysfs_lock);
1da177e4 1042 kfree(battery);
d550d98d 1043 return 0;
1da177e4
LT
1044}
1045
34c4415a 1046/* this is needed to learn about changes made in suspended state */
a6f50dc8 1047static int acpi_battery_resume(struct device *dev)
34c4415a
JK
1048{
1049 struct acpi_battery *battery;
a6f50dc8
RW
1050
1051 if (!dev)
34c4415a 1052 return -EINVAL;
a6f50dc8
RW
1053
1054 battery = acpi_driver_data(to_acpi_device(dev));
1055 if (!battery)
1056 return -EINVAL;
1057
f1d4661a 1058 battery->update_time = 0;
508df92d 1059 acpi_battery_update(battery);
b6ce4083 1060 return 0;
34c4415a
JK
1061}
1062
a6f50dc8
RW
1063static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1064
aa650bbd
AS
1065static struct acpi_driver acpi_battery_driver = {
1066 .name = "battery",
1067 .class = ACPI_BATTERY_CLASS,
1068 .ids = battery_device_ids,
d9406691 1069 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
aa650bbd
AS
1070 .ops = {
1071 .add = acpi_battery_add,
aa650bbd 1072 .remove = acpi_battery_remove,
d9406691 1073 .notify = acpi_battery_notify,
aa650bbd 1074 },
a6f50dc8 1075 .drv.pm = &acpi_battery_pm,
aa650bbd
AS
1076};
1077
b0cbc861 1078static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1da177e4 1079{
4d8316d5 1080 if (acpi_disabled)
0f66af53 1081 return;
fdcedbba 1082#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 1083 acpi_battery_dir = acpi_lock_battery_dir();
1da177e4 1084 if (!acpi_battery_dir)
0f66af53 1085 return;
d7380965 1086#endif
aa650bbd 1087 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
fdcedbba 1088#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 1089 acpi_unlock_battery_dir(acpi_battery_dir);
d7380965 1090#endif
0f66af53 1091 return;
1da177e4 1092 }
0f66af53
AV
1093 return;
1094}
1095
1096static int __init acpi_battery_init(void)
1097{
1098 async_schedule(acpi_battery_init_async, NULL);
d550d98d 1099 return 0;
1da177e4
LT
1100}
1101
4be44fcd 1102static void __exit acpi_battery_exit(void)
1da177e4 1103{
1da177e4 1104 acpi_bus_unregister_driver(&acpi_battery_driver);
fdcedbba 1105#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 1106 acpi_unlock_battery_dir(acpi_battery_dir);
d7380965 1107#endif
1da177e4
LT
1108}
1109
1da177e4
LT
1110module_init(acpi_battery_init);
1111module_exit(acpi_battery_exit);