Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 2 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 4 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
| 5 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 33 | #include <linux/async.h> |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 34 | #include <linux/dmi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 36 | #include <linux/suspend.h> |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 37 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 38 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/proc_fs.h> |
| 40 | #include <linux/seq_file.h> |
| 41 | #include <asm/uaccess.h> |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 42 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <acpi/acpi_bus.h> |
| 45 | #include <acpi/acpi_drivers.h> |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 46 | #include <linux/power_supply.h> |
| 47 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 48 | #define PREFIX "ACPI: " |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define ACPI_BATTERY_CLASS "battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 55 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 56 | #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Lan Tianyu | ae6f618 | 2011-06-30 11:32:40 +0800 | [diff] [blame] | 58 | /* Battery power unit: 0 means mW, 1 means mA */ |
| 59 | #define ACPI_BATTERY_POWER_UNIT_MA 1 |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 62 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 63 | ACPI_MODULE_NAME("battery"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 65 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 66 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 67 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | MODULE_LICENSE("GPL"); |
| 69 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 70 | static unsigned int cache_time = 1000; |
| 71 | module_param(cache_time, uint, 0644); |
| 72 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 73 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 74 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 75 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 76 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 77 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 78 | enum acpi_battery_files { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 79 | info_tag = 0, |
| 80 | state_tag, |
| 81 | alarm_tag, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 82 | ACPI_BATTERY_NUMFILES, |
| 83 | }; |
| 84 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 85 | #endif |
| 86 | |
| 87 | static const struct acpi_device_id battery_device_ids[] = { |
| 88 | {"PNP0C0A", 0}, |
| 89 | {"", 0}, |
| 90 | }; |
| 91 | |
| 92 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
| 93 | |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 94 | enum { |
| 95 | ACPI_BATTERY_ALARM_PRESENT, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 96 | ACPI_BATTERY_XINFO_PRESENT, |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 97 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 98 | }; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 99 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 100 | struct acpi_battery { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 101 | struct mutex lock; |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 102 | struct mutex sysfs_lock; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 103 | struct power_supply bat; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 104 | struct acpi_device *device; |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 105 | struct notifier_block pm_nb; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 106 | unsigned long update_time; |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 107 | int rate_now; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 108 | int capacity_now; |
| 109 | int voltage_now; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 110 | int design_capacity; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 111 | int full_charge_capacity; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 112 | int technology; |
| 113 | int design_voltage; |
| 114 | int design_capacity_warning; |
| 115 | int design_capacity_low; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 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; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 122 | int capacity_granularity_1; |
| 123 | int capacity_granularity_2; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 124 | int alarm; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 125 | char model_number[32]; |
| 126 | char serial_number[32]; |
| 127 | char type[32]; |
| 128 | char oem_info[32]; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 129 | int state; |
| 130 | int power_unit; |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 131 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 134 | #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 135 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 136 | inline int acpi_battery_present(struct acpi_battery *battery) |
| 137 | { |
| 138 | return battery->device->status.battery_present; |
| 139 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 140 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 141 | static 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; |
Andrey Borzenkov | ad40e68 | 2007-11-10 20:02:49 +0300 | [diff] [blame] | 149 | if (!strncasecmp("LI-ION", battery->type, 6)) |
Alexey Starikovskiy | 0bde7ee | 2007-10-28 15:33:10 +0300 | [diff] [blame] | 150 | return POWER_SUPPLY_TECHNOLOGY_LION; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 151 | if (!strcasecmp("LiP", battery->type)) |
| 152 | return POWER_SUPPLY_TECHNOLOGY_LIPO; |
| 153 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
| 154 | } |
| 155 | |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 156 | static int acpi_battery_get_state(struct acpi_battery *battery); |
Alexey Starikovskiy | b19073a | 2007-10-25 17:10:47 -0400 | [diff] [blame] | 157 | |
Richard Hughes | 56f382a | 2009-01-25 15:05:50 +0000 | [diff] [blame] | 158 | static 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 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 181 | static int acpi_battery_get_property(struct power_supply *psy, |
| 182 | enum power_supply_property psp, |
| 183 | union power_supply_propval *val) |
| 184 | { |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 185 | int ret = 0; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 186 | struct acpi_battery *battery = to_acpi_battery(psy); |
| 187 | |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 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) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 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; |
Richard Hughes | 56f382a | 2009-01-25 15:05:50 +0000 | [diff] [blame] | 199 | else if (acpi_battery_is_charged(battery)) |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 200 | val->intval = POWER_SUPPLY_STATUS_FULL; |
Roland Dreier | 4c41d3a | 2007-11-07 15:09:09 -0800 | [diff] [blame] | 201 | else |
| 202 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 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; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 210 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 211 | val->intval = battery->cycle_count; |
| 212 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 213 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 214 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 215 | ret = -ENODEV; |
| 216 | else |
| 217 | val->intval = battery->design_voltage * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 218 | break; |
| 219 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 220 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 221 | ret = -ENODEV; |
| 222 | else |
| 223 | val->intval = battery->voltage_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 224 | break; |
| 225 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 226 | case POWER_SUPPLY_PROP_POWER_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 227 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 228 | ret = -ENODEV; |
| 229 | else |
| 230 | val->intval = battery->rate_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 231 | break; |
| 232 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 233 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 234 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 235 | ret = -ENODEV; |
| 236 | else |
| 237 | val->intval = battery->design_capacity * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 238 | break; |
| 239 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 240 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 241 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 242 | ret = -ENODEV; |
| 243 | else |
| 244 | val->intval = battery->full_charge_capacity * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 245 | break; |
| 246 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 247 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 248 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
| 249 | ret = -ENODEV; |
| 250 | else |
| 251 | val->intval = battery->capacity_now * 1000; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 252 | break; |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 253 | case POWER_SUPPLY_PROP_CAPACITY: |
| 254 | if (battery->capacity_now && battery->full_charge_capacity) |
| 255 | val->intval = battery->capacity_now * 100/ |
| 256 | battery->full_charge_capacity; |
| 257 | else |
| 258 | val->intval = 0; |
| 259 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 260 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 261 | val->strval = battery->model_number; |
| 262 | break; |
| 263 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 264 | val->strval = battery->oem_info; |
| 265 | break; |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 266 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 267 | val->strval = battery->serial_number; |
| 268 | break; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 269 | default: |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 270 | ret = -EINVAL; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 271 | } |
Rafael J. Wysocki | a1b4bd6 | 2010-10-23 19:35:15 +0200 | [diff] [blame] | 272 | return ret; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static enum power_supply_property charge_battery_props[] = { |
| 276 | POWER_SUPPLY_PROP_STATUS, |
| 277 | POWER_SUPPLY_PROP_PRESENT, |
| 278 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 279 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 280 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 281 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 282 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 283 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 284 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 285 | POWER_SUPPLY_PROP_CHARGE_NOW, |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 286 | POWER_SUPPLY_PROP_CAPACITY, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 287 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 288 | POWER_SUPPLY_PROP_MANUFACTURER, |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 289 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | static enum power_supply_property energy_battery_props[] = { |
| 293 | POWER_SUPPLY_PROP_STATUS, |
| 294 | POWER_SUPPLY_PROP_PRESENT, |
| 295 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 296 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 297 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
| 298 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 299 | POWER_SUPPLY_PROP_POWER_NOW, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 300 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
| 301 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 302 | POWER_SUPPLY_PROP_ENERGY_NOW, |
srinivas pandruvada | a58e115 | 2012-04-05 17:38:54 -0700 | [diff] [blame] | 303 | POWER_SUPPLY_PROP_CAPACITY, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 304 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 305 | POWER_SUPPLY_PROP_MANUFACTURER, |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 306 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 307 | }; |
| 308 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 309 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 310 | inline char *acpi_battery_units(struct acpi_battery *battery) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 311 | { |
Lan Tianyu | ae6f618 | 2011-06-30 11:32:40 +0800 | [diff] [blame] | 312 | return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ? |
| 313 | "mA" : "mW"; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 314 | } |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 315 | #endif |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* -------------------------------------------------------------------------- |
| 318 | Battery Management |
| 319 | -------------------------------------------------------------------------- */ |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 320 | struct acpi_offsets { |
| 321 | size_t offset; /* offset inside struct acpi_sbs_battery */ |
| 322 | u8 mode; /* int or string? */ |
| 323 | }; |
| 324 | |
| 325 | static struct acpi_offsets state_offsets[] = { |
| 326 | {offsetof(struct acpi_battery, state), 0}, |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 327 | {offsetof(struct acpi_battery, rate_now), 0}, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 328 | {offsetof(struct acpi_battery, capacity_now), 0}, |
| 329 | {offsetof(struct acpi_battery, voltage_now), 0}, |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | static struct acpi_offsets info_offsets[] = { |
| 333 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 334 | {offsetof(struct acpi_battery, design_capacity), 0}, |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 335 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 336 | {offsetof(struct acpi_battery, technology), 0}, |
| 337 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 338 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 339 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 340 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 341 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 342 | {offsetof(struct acpi_battery, model_number), 1}, |
| 343 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 344 | {offsetof(struct acpi_battery, type), 1}, |
| 345 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 346 | }; |
| 347 | |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 348 | static struct acpi_offsets extended_info_offsets[] = { |
| 349 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 350 | {offsetof(struct acpi_battery, design_capacity), 0}, |
| 351 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
| 352 | {offsetof(struct acpi_battery, technology), 0}, |
| 353 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 354 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 355 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 356 | {offsetof(struct acpi_battery, cycle_count), 0}, |
| 357 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, |
| 358 | {offsetof(struct acpi_battery, max_sampling_time), 0}, |
| 359 | {offsetof(struct acpi_battery, min_sampling_time), 0}, |
| 360 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, |
| 361 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, |
| 362 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 363 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 364 | {offsetof(struct acpi_battery, model_number), 1}, |
| 365 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 366 | {offsetof(struct acpi_battery, type), 1}, |
| 367 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 368 | }; |
| 369 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 370 | static int extract_package(struct acpi_battery *battery, |
| 371 | union acpi_object *package, |
| 372 | struct acpi_offsets *offsets, int num) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 373 | { |
Alexey Starikovskiy | 106449e | 2007-10-29 23:29:40 +0300 | [diff] [blame] | 374 | int i; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 375 | union acpi_object *element; |
| 376 | if (package->type != ACPI_TYPE_PACKAGE) |
| 377 | return -EFAULT; |
| 378 | for (i = 0; i < num; ++i) { |
| 379 | if (package->package.count <= i) |
| 380 | return -EFAULT; |
| 381 | element = &package->package.elements[i]; |
| 382 | if (offsets[i].mode) { |
Alexey Starikovskiy | 106449e | 2007-10-29 23:29:40 +0300 | [diff] [blame] | 383 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
| 384 | if (element->type == ACPI_TYPE_STRING || |
| 385 | element->type == ACPI_TYPE_BUFFER) |
| 386 | strncpy(ptr, element->string.pointer, 32); |
| 387 | else if (element->type == ACPI_TYPE_INTEGER) { |
| 388 | strncpy(ptr, (u8 *)&element->integer.value, |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 389 | sizeof(u64)); |
| 390 | ptr[sizeof(u64)] = 0; |
Alexey Starikovskiy | b8a1bdb | 2008-03-17 22:37:42 -0400 | [diff] [blame] | 391 | } else |
| 392 | *ptr = 0; /* don't have value */ |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 393 | } else { |
Alexey Starikovskiy | b8a1bdb | 2008-03-17 22:37:42 -0400 | [diff] [blame] | 394 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
| 395 | *x = (element->type == ACPI_TYPE_INTEGER) ? |
| 396 | element->integer.value : -1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 397 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 398 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int acpi_battery_get_status(struct acpi_battery *battery) |
| 403 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 404 | if (acpi_bus_get_status(battery->device)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 405 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
| 406 | return -ENODEV; |
| 407 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 408 | return 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int acpi_battery_get_info(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 413 | int result = -EFAULT; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 414 | acpi_status status = 0; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 415 | char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)? |
| 416 | "_BIX" : "_BIF"; |
| 417 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 418 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 420 | if (!acpi_battery_present(battery)) |
| 421 | return 0; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 422 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 423 | status = acpi_evaluate_object(battery->device->handle, name, |
| 424 | NULL, &buffer); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 425 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (ACPI_FAILURE(status)) { |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 428 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 429 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 431 | if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)) |
| 432 | result = extract_package(battery, buffer.pointer, |
| 433 | extended_info_offsets, |
| 434 | ARRAY_SIZE(extended_info_offsets)); |
| 435 | else |
| 436 | result = extract_package(battery, buffer.pointer, |
| 437 | info_offsets, ARRAY_SIZE(info_offsets)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 438 | kfree(buffer.pointer); |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 439 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
| 440 | battery->full_charge_capacity = battery->design_capacity; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 441 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 444 | static int acpi_battery_get_state(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 446 | int result = 0; |
| 447 | acpi_status status = 0; |
| 448 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 450 | if (!acpi_battery_present(battery)) |
| 451 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 453 | if (battery->update_time && |
| 454 | time_before(jiffies, battery->update_time + |
| 455 | msecs_to_jiffies(cache_time))) |
| 456 | return 0; |
| 457 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 458 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 459 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 460 | NULL, &buffer); |
| 461 | mutex_unlock(&battery->lock); |
Len Brown | 5b31d89 | 2007-08-15 00:19:26 -0400 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 464 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 465 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 467 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 468 | result = extract_package(battery, buffer.pointer, |
| 469 | state_offsets, ARRAY_SIZE(state_offsets)); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 470 | battery->update_time = jiffies; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 471 | kfree(buffer.pointer); |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 472 | |
Lan Tianyu | 55003b2 | 2011-06-30 11:33:12 +0800 | [diff] [blame] | 473 | /* For buggy DSDTs that report negative 16-bit values for either |
| 474 | * charging or discharging current and/or report 0 as 65536 |
| 475 | * due to bad math. |
| 476 | */ |
| 477 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA && |
| 478 | battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN && |
| 479 | (s16)(battery->rate_now) < 0) { |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 480 | battery->rate_now = abs((s16)battery->rate_now); |
Lan Tianyu | 55003b2 | 2011-06-30 11:33:12 +0800 | [diff] [blame] | 481 | printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate" |
| 482 | " invalid.\n"); |
| 483 | } |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 484 | |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 485 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
| 486 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) |
| 487 | battery->capacity_now = (battery->capacity_now * |
| 488 | battery->full_charge_capacity) / 100; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 489 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 492 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 494 | acpi_status status = 0; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 495 | union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER }; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 496 | struct acpi_object_list arg_list = { 1, &arg0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 498 | if (!acpi_battery_present(battery) || |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 499 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 500 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 502 | arg0.integer.value = battery->alarm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 504 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 505 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
| 506 | &arg_list, NULL); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 507 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 510 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 512 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 513 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 516 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 518 | acpi_status status = AE_OK; |
| 519 | acpi_handle handle = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 521 | /* See if alarms are supported, and if so, set default */ |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 522 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
| 523 | if (ACPI_FAILURE(status)) { |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 524 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 525 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
Alexey Starikovskiy | 7b3bcc4 | 2009-10-15 14:31:24 +0400 | [diff] [blame] | 527 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 528 | if (!battery->alarm) |
| 529 | battery->alarm = battery->design_capacity_warning; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 530 | return acpi_battery_set_alarm(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 533 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
| 534 | struct device_attribute *attr, |
| 535 | char *buf) |
| 536 | { |
| 537 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); |
| 538 | return sprintf(buf, "%d\n", battery->alarm * 1000); |
| 539 | } |
| 540 | |
| 541 | static ssize_t acpi_battery_alarm_store(struct device *dev, |
| 542 | struct device_attribute *attr, |
| 543 | const char *buf, size_t count) |
| 544 | { |
| 545 | unsigned long x; |
| 546 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); |
| 547 | if (sscanf(buf, "%ld\n", &x) == 1) |
| 548 | battery->alarm = x/1000; |
| 549 | if (acpi_battery_present(battery)) |
| 550 | acpi_battery_set_alarm(battery); |
| 551 | return count; |
| 552 | } |
| 553 | |
| 554 | static struct device_attribute alarm_attr = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 555 | .attr = {.name = "alarm", .mode = 0644}, |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 556 | .show = acpi_battery_alarm_show, |
| 557 | .store = acpi_battery_alarm_store, |
| 558 | }; |
| 559 | |
| 560 | static int sysfs_add_battery(struct acpi_battery *battery) |
| 561 | { |
| 562 | int result; |
| 563 | |
Lan Tianyu | ae6f618 | 2011-06-30 11:32:40 +0800 | [diff] [blame] | 564 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 565 | battery->bat.properties = charge_battery_props; |
| 566 | battery->bat.num_properties = |
| 567 | ARRAY_SIZE(charge_battery_props); |
| 568 | } else { |
| 569 | battery->bat.properties = energy_battery_props; |
| 570 | battery->bat.num_properties = |
| 571 | ARRAY_SIZE(energy_battery_props); |
| 572 | } |
| 573 | |
| 574 | battery->bat.name = acpi_device_bid(battery->device); |
| 575 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 576 | battery->bat.get_property = acpi_battery_get_property; |
| 577 | |
| 578 | result = power_supply_register(&battery->device->dev, &battery->bat); |
| 579 | if (result) |
| 580 | return result; |
| 581 | return device_create_file(battery->bat.dev, &alarm_attr); |
| 582 | } |
| 583 | |
| 584 | static void sysfs_remove_battery(struct acpi_battery *battery) |
| 585 | { |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 586 | mutex_lock(&battery->sysfs_lock); |
Lan Tianyu | 9c921c22 | 2011-06-30 11:34:12 +0800 | [diff] [blame] | 587 | if (!battery->bat.dev) { |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 588 | mutex_unlock(&battery->sysfs_lock); |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 589 | return; |
Lan Tianyu | 9c921c22 | 2011-06-30 11:34:12 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 592 | device_remove_file(battery->bat.dev, &alarm_attr); |
| 593 | power_supply_unregister(&battery->bat); |
Alexey Starikovskiy | 9104476 | 2007-11-13 12:23:06 +0300 | [diff] [blame] | 594 | battery->bat.dev = NULL; |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 595 | mutex_unlock(&battery->sysfs_lock); |
Hector Martin | bc76f90 | 2009-08-06 15:57:48 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 598 | /* |
| 599 | * According to the ACPI spec, some kinds of primary batteries can |
| 600 | * report percentage battery remaining capacity directly to OS. |
| 601 | * In this case, it reports the Last Full Charged Capacity == 100 |
| 602 | * and BatteryPresentRate == 0xFFFFFFFF. |
| 603 | * |
| 604 | * Now we found some battery reports percentage remaining capacity |
| 605 | * even if it's rechargeable. |
| 606 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 |
| 607 | * |
| 608 | * Handle this correctly so that they won't break userspace. |
| 609 | */ |
Lan Tianyu | 7b78622 | 2011-06-30 11:33:27 +0800 | [diff] [blame] | 610 | static void acpi_battery_quirks(struct acpi_battery *battery) |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 611 | { |
| 612 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
| 613 | return ; |
| 614 | |
| 615 | if (battery->full_charge_capacity == 100 && |
| 616 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && |
| 617 | battery->capacity_now >=0 && battery->capacity_now <= 100) { |
| 618 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); |
| 619 | battery->full_charge_capacity = battery->design_capacity; |
| 620 | battery->capacity_now = (battery->capacity_now * |
| 621 | battery->full_charge_capacity) / 100; |
| 622 | } |
| 623 | } |
| 624 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 625 | static int acpi_battery_update(struct acpi_battery *battery) |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 626 | { |
Alexey Starikovskiy | 50b1785 | 2008-12-23 02:44:54 +0300 | [diff] [blame] | 627 | int result, old_present = acpi_battery_present(battery); |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 628 | result = acpi_battery_get_status(battery); |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 629 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 630 | return result; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 631 | if (!acpi_battery_present(battery)) { |
| 632 | sysfs_remove_battery(battery); |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 633 | battery->update_time = 0; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 634 | return 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 635 | } |
Alexey Starikovskiy | 50b1785 | 2008-12-23 02:44:54 +0300 | [diff] [blame] | 636 | if (!battery->update_time || |
| 637 | old_present != acpi_battery_present(battery)) { |
Alexey Starikovskiy | 97749cd | 2008-01-01 14:27:24 -0500 | [diff] [blame] | 638 | result = acpi_battery_get_info(battery); |
| 639 | if (result) |
| 640 | return result; |
| 641 | acpi_battery_init_alarm(battery); |
| 642 | } |
Stefan Hajnoczi | eb03cb0 | 2011-07-12 09:03:29 +0100 | [diff] [blame] | 643 | if (!battery->bat.dev) { |
| 644 | result = sysfs_add_battery(battery); |
| 645 | if (result) |
| 646 | return result; |
| 647 | } |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 648 | result = acpi_battery_get_state(battery); |
Lan Tianyu | 7b78622 | 2011-06-30 11:33:27 +0800 | [diff] [blame] | 649 | acpi_battery_quirks(battery); |
Zhang Rui | 557d586 | 2010-10-22 10:02:06 +0800 | [diff] [blame] | 650 | return result; |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 651 | } |
| 652 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 653 | static void acpi_battery_refresh(struct acpi_battery *battery) |
| 654 | { |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 655 | int power_unit; |
| 656 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 657 | if (!battery->bat.dev) |
| 658 | return; |
| 659 | |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 660 | power_unit = battery->power_unit; |
| 661 | |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 662 | acpi_battery_get_info(battery); |
Andy Whitcroft | c597145 | 2012-05-03 14:48:26 +0100 | [diff] [blame] | 663 | |
| 664 | if (power_unit == battery->power_unit) |
| 665 | return; |
| 666 | |
| 667 | /* The battery has changed its reporting units. */ |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 668 | sysfs_remove_battery(battery); |
| 669 | sysfs_add_battery(battery); |
| 670 | } |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* -------------------------------------------------------------------------- |
| 673 | FS Interface (/proc) |
| 674 | -------------------------------------------------------------------------- */ |
| 675 | |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 676 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 677 | static struct proc_dir_entry *acpi_battery_dir; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 678 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 679 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 681 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 683 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | goto end; |
| 685 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 686 | seq_printf(seq, "present: %s\n", |
| 687 | acpi_battery_present(battery)?"yes":"no"); |
| 688 | if (!acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | goto end; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 690 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | seq_printf(seq, "design capacity: unknown\n"); |
| 692 | else |
| 693 | seq_printf(seq, "design capacity: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 694 | battery->design_capacity, |
| 695 | acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 697 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | seq_printf(seq, "last full capacity: unknown\n"); |
| 699 | else |
| 700 | seq_printf(seq, "last full capacity: %d %sh\n", |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 701 | battery->full_charge_capacity, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 702 | acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 704 | seq_printf(seq, "battery technology: %srechargeable\n", |
| 705 | (!battery->technology)?"non-":""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 707 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | seq_printf(seq, "design voltage: unknown\n"); |
| 709 | else |
| 710 | seq_printf(seq, "design voltage: %d mV\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 711 | battery->design_voltage); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 712 | seq_printf(seq, "design capacity warning: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 713 | battery->design_capacity_warning, |
| 714 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 715 | seq_printf(seq, "design capacity low: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 716 | battery->design_capacity_low, |
| 717 | acpi_battery_units(battery)); |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 718 | seq_printf(seq, "cycle count: %i\n", battery->cycle_count); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 719 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 720 | battery->capacity_granularity_1, |
| 721 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 722 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 723 | battery->capacity_granularity_2, |
| 724 | acpi_battery_units(battery)); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 725 | seq_printf(seq, "model number: %s\n", battery->model_number); |
| 726 | seq_printf(seq, "serial number: %s\n", battery->serial_number); |
| 727 | seq_printf(seq, "battery type: %s\n", battery->type); |
| 728 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 729 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 730 | if (result) |
| 731 | seq_printf(seq, "ERROR: Unable to read battery info\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 732 | return result; |
| 733 | } |
| 734 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 735 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 737 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 739 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | goto end; |
| 741 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 742 | seq_printf(seq, "present: %s\n", |
| 743 | acpi_battery_present(battery)?"yes":"no"); |
| 744 | if (!acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 747 | seq_printf(seq, "capacity state: %s\n", |
| 748 | (battery->state & 0x04)?"critical":"ok"); |
| 749 | if ((battery->state & 0x01) && (battery->state & 0x02)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 750 | seq_printf(seq, |
| 751 | "charging state: charging/discharging\n"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 752 | else if (battery->state & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | seq_printf(seq, "charging state: discharging\n"); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 754 | else if (battery->state & 0x02) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | seq_printf(seq, "charging state: charging\n"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 756 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | seq_printf(seq, "charging state: charged\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 759 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | seq_printf(seq, "present rate: unknown\n"); |
| 761 | else |
| 762 | seq_printf(seq, "present rate: %d %s\n", |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 763 | battery->rate_now, acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 765 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | seq_printf(seq, "remaining capacity: unknown\n"); |
| 767 | else |
| 768 | seq_printf(seq, "remaining capacity: %d %sh\n", |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 769 | battery->capacity_now, acpi_battery_units(battery)); |
| 770 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | seq_printf(seq, "present voltage: unknown\n"); |
| 772 | else |
| 773 | seq_printf(seq, "present voltage: %d mV\n", |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 774 | battery->voltage_now); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 775 | end: |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 776 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 777 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 778 | |
| 779 | return result; |
| 780 | } |
| 781 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 782 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 784 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 786 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto end; |
| 788 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 789 | if (!acpi_battery_present(battery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | seq_printf(seq, "present: no\n"); |
| 791 | goto end; |
| 792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | seq_printf(seq, "alarm: "); |
| 794 | if (!battery->alarm) |
| 795 | seq_printf(seq, "unsupported\n"); |
| 796 | else |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 797 | seq_printf(seq, "%u %sh\n", battery->alarm, |
| 798 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 799 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 800 | if (result) |
| 801 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 802 | return result; |
| 803 | } |
| 804 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 805 | static ssize_t acpi_battery_write_alarm(struct file *file, |
| 806 | const char __user * buffer, |
| 807 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 809 | int result = 0; |
| 810 | char alarm_string[12] = { '\0' }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 811 | struct seq_file *m = file->private_data; |
| 812 | struct acpi_battery *battery = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (!battery || (count > sizeof(alarm_string) - 1)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 815 | return -EINVAL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 816 | if (!acpi_battery_present(battery)) { |
| 817 | result = -ENODEV; |
| 818 | goto end; |
| 819 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 820 | if (copy_from_user(alarm_string, buffer, count)) { |
| 821 | result = -EFAULT; |
| 822 | goto end; |
| 823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | alarm_string[count] = '\0'; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 825 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 826 | result = acpi_battery_set_alarm(battery); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 827 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 828 | if (!result) |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 829 | return count; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 830 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 833 | typedef int(*print_func)(struct seq_file *seq, int result); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 834 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 835 | static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = { |
| 836 | acpi_battery_print_info, |
| 837 | acpi_battery_print_state, |
| 838 | acpi_battery_print_alarm, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 839 | }; |
| 840 | |
| 841 | static int acpi_battery_read(int fid, struct seq_file *seq) |
| 842 | { |
| 843 | struct acpi_battery *battery = seq->private; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 844 | int result = acpi_battery_update(battery); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 845 | return acpi_print_funcs[fid](seq, result); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 846 | } |
| 847 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 848 | #define DECLARE_FILE_FUNCTIONS(_name) \ |
| 849 | static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \ |
| 850 | { \ |
| 851 | return acpi_battery_read(_name##_tag, seq); \ |
| 852 | } \ |
| 853 | static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \ |
| 854 | { \ |
| 855 | return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \ |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 856 | } |
| 857 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 858 | DECLARE_FILE_FUNCTIONS(info); |
| 859 | DECLARE_FILE_FUNCTIONS(state); |
| 860 | DECLARE_FILE_FUNCTIONS(alarm); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 861 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 862 | #undef DECLARE_FILE_FUNCTIONS |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 863 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 864 | #define FILE_DESCRIPTION_RO(_name) \ |
| 865 | { \ |
| 866 | .name = __stringify(_name), \ |
| 867 | .mode = S_IRUGO, \ |
| 868 | .ops = { \ |
| 869 | .open = acpi_battery_##_name##_open_fs, \ |
| 870 | .read = seq_read, \ |
| 871 | .llseek = seq_lseek, \ |
| 872 | .release = single_release, \ |
| 873 | .owner = THIS_MODULE, \ |
| 874 | }, \ |
| 875 | } |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 876 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 877 | #define FILE_DESCRIPTION_RW(_name) \ |
| 878 | { \ |
| 879 | .name = __stringify(_name), \ |
| 880 | .mode = S_IFREG | S_IRUGO | S_IWUSR, \ |
| 881 | .ops = { \ |
| 882 | .open = acpi_battery_##_name##_open_fs, \ |
| 883 | .read = seq_read, \ |
| 884 | .llseek = seq_lseek, \ |
| 885 | .write = acpi_battery_write_##_name, \ |
| 886 | .release = single_release, \ |
| 887 | .owner = THIS_MODULE, \ |
| 888 | }, \ |
| 889 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 891 | static const struct battery_file { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 892 | struct file_operations ops; |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 893 | umode_t mode; |
Jan Engelhardt | 070d8eb | 2009-01-12 00:07:55 +0100 | [diff] [blame] | 894 | const char *name; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 895 | } acpi_battery_file[] = { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 896 | FILE_DESCRIPTION_RO(info), |
| 897 | FILE_DESCRIPTION_RO(state), |
| 898 | FILE_DESCRIPTION_RW(alarm), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | }; |
| 900 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 901 | #undef FILE_DESCRIPTION_RO |
| 902 | #undef FILE_DESCRIPTION_RW |
| 903 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 904 | static int acpi_battery_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 906 | struct proc_dir_entry *entry = NULL; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 907 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Zhang Rui | 6d855fc | 2011-01-10 11:16:30 +0800 | [diff] [blame] | 909 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded," |
| 910 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (!acpi_device_dir(device)) { |
| 912 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 913 | acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 915 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 918 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 919 | entry = proc_create_data(acpi_battery_file[i].name, |
| 920 | acpi_battery_file[i].mode, |
| 921 | acpi_device_dir(device), |
| 922 | &acpi_battery_file[i].ops, |
| 923 | acpi_driver_data(device)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 924 | if (!entry) |
| 925 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 927 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 930 | static void acpi_battery_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 932 | int i; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 933 | if (!acpi_device_dir(device)) |
| 934 | return; |
| 935 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) |
| 936 | remove_proc_entry(acpi_battery_file[i].name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 939 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
| 940 | acpi_device_dir(device) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 943 | #endif |
Alexey Starikovskiy | 3e58ea0 | 2007-09-26 19:43:11 +0400 | [diff] [blame] | 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* -------------------------------------------------------------------------- |
| 946 | Driver Interface |
| 947 | -------------------------------------------------------------------------- */ |
| 948 | |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 949 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 951 | struct acpi_battery *battery = acpi_driver_data(device); |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 952 | struct device *old; |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 955 | return; |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 956 | old = battery->bat.dev; |
Rafael J. Wysocki | da8aeb9 | 2011-01-06 23:42:27 +0100 | [diff] [blame] | 957 | if (event == ACPI_BATTERY_NOTIFY_INFO) |
| 958 | acpi_battery_refresh(battery); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 959 | acpi_battery_update(battery); |
| 960 | acpi_bus_generate_proc_event(device, event, |
| 961 | acpi_battery_present(battery)); |
| 962 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 963 | dev_name(&device->dev), event, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 964 | acpi_battery_present(battery)); |
Justin P. Mattock | 2345baf | 2009-12-13 14:42:36 -0800 | [diff] [blame] | 965 | /* acpi_battery_update could remove power_supply object */ |
Zhang Rui | 153e500 | 2010-07-07 09:11:57 +0800 | [diff] [blame] | 966 | if (old && battery->bat.dev) |
Alan Jenkins | f79e1ce | 2009-06-30 14:36:16 +0000 | [diff] [blame] | 967 | power_supply_changed(&battery->bat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 970 | static int battery_notify(struct notifier_block *nb, |
| 971 | unsigned long mode, void *_unused) |
| 972 | { |
| 973 | struct acpi_battery *battery = container_of(nb, struct acpi_battery, |
| 974 | pm_nb); |
| 975 | switch (mode) { |
Lan Tianyu | d5a5911 | 2011-06-30 11:33:40 +0800 | [diff] [blame] | 976 | case PM_POST_HIBERNATION: |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 977 | case PM_POST_SUSPEND: |
Lan Tianyu | 6e17fb6 | 2011-06-30 11:33:58 +0800 | [diff] [blame] | 978 | if (battery->bat.dev) { |
| 979 | sysfs_remove_battery(battery); |
| 980 | sysfs_add_battery(battery); |
| 981 | } |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 982 | break; |
| 983 | } |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 988 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 990 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 991 | struct acpi_battery *battery = NULL; |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 992 | acpi_handle handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 994 | return -EINVAL; |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 995 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 997 | return -ENOMEM; |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 998 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 1000 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 1001 | device->driver_data = battery; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 1002 | mutex_init(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 1003 | mutex_init(&battery->sysfs_lock); |
Alexey Starikovskiy | c67fcd6 | 2009-10-15 14:31:44 +0400 | [diff] [blame] | 1004 | if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle, |
| 1005 | "_BIX", &handle))) |
| 1006 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); |
Stefan Hajnoczi | eb03cb0 | 2011-07-12 09:03:29 +0100 | [diff] [blame] | 1007 | result = acpi_battery_update(battery); |
| 1008 | if (result) |
| 1009 | goto fail; |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1010 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | result = acpi_battery_add_fs(device); |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1012 | #endif |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 1013 | if (result) { |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1014 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | acpi_battery_remove_fs(device); |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1016 | #endif |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 1017 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 1019 | |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 1020 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
| 1021 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 1022 | device->status.battery_present ? "present" : "absent"); |
| 1023 | |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 1024 | battery->pm_nb.notifier_call = battery_notify; |
| 1025 | register_pm_notifier(&battery->pm_nb); |
| 1026 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1027 | return result; |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 1028 | |
| 1029 | fail: |
| 1030 | sysfs_remove_battery(battery); |
| 1031 | mutex_destroy(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 1032 | mutex_destroy(&battery->sysfs_lock); |
Stefan Hajnoczi | e80bba4 | 2011-07-12 09:03:28 +0100 | [diff] [blame] | 1033 | kfree(battery); |
| 1034 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1037 | static int acpi_battery_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1039 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1042 | return -EINVAL; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1043 | battery = acpi_driver_data(device); |
Kyle McMartin | 25be582 | 2011-03-22 16:19:50 -0400 | [diff] [blame] | 1044 | unregister_pm_notifier(&battery->pm_nb); |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1045 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | acpi_battery_remove_fs(device); |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1047 | #endif |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 1048 | sysfs_remove_battery(battery); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 1049 | mutex_destroy(&battery->lock); |
Sergey Senozhatsky | 69d94ec | 2011-08-06 01:34:08 +0300 | [diff] [blame] | 1050 | mutex_destroy(&battery->sysfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | kfree(battery); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1052 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 1055 | #ifdef CONFIG_PM_SLEEP |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1056 | /* this is needed to learn about changes made in suspended state */ |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 1057 | static int acpi_battery_resume(struct device *dev) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1058 | { |
| 1059 | struct acpi_battery *battery; |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 1060 | |
| 1061 | if (!dev) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1062 | return -EINVAL; |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 1063 | |
| 1064 | battery = acpi_driver_data(to_acpi_device(dev)); |
| 1065 | if (!battery) |
| 1066 | return -EINVAL; |
| 1067 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 1068 | battery->update_time = 0; |
Andrey Borzenkov | 508df92 | 2007-10-28 12:50:09 +0300 | [diff] [blame] | 1069 | acpi_battery_update(battery); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1070 | return 0; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1071 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 1072 | #endif |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1073 | |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 1074 | static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume); |
| 1075 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1076 | static struct acpi_driver acpi_battery_driver = { |
| 1077 | .name = "battery", |
| 1078 | .class = ACPI_BATTERY_CLASS, |
| 1079 | .ids = battery_device_ids, |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 1080 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1081 | .ops = { |
| 1082 | .add = acpi_battery_add, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1083 | .remove = acpi_battery_remove, |
Bjorn Helgaas | d940669 | 2009-04-30 09:35:47 -0600 | [diff] [blame] | 1084 | .notify = acpi_battery_notify, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1085 | }, |
Rafael J. Wysocki | a6f50dc | 2012-06-27 23:26:43 +0200 | [diff] [blame] | 1086 | .drv.pm = &acpi_battery_pm, |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1087 | }; |
| 1088 | |
Linus Torvalds | b0cbc86 | 2009-04-11 12:45:20 -0700 | [diff] [blame] | 1089 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 1091 | if (acpi_disabled) |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 1092 | return; |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1093 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1094 | acpi_battery_dir = acpi_lock_battery_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if (!acpi_battery_dir) |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 1096 | return; |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1097 | #endif |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame] | 1098 | if (acpi_bus_register_driver(&acpi_battery_driver) < 0) { |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1099 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1100 | acpi_unlock_battery_dir(acpi_battery_dir); |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1101 | #endif |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 1102 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | } |
Arjan van de Ven | 0f66af5 | 2009-01-10 14:19:05 -0500 | [diff] [blame] | 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | static int __init acpi_battery_init(void) |
| 1108 | { |
| 1109 | async_schedule(acpi_battery_init_async, NULL); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1110 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1113 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | acpi_bus_unregister_driver(&acpi_battery_driver); |
Alexey Starikovskiy | fdcedbb | 2007-11-19 16:33:45 +0300 | [diff] [blame] | 1116 | #ifdef CONFIG_ACPI_PROCFS_POWER |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1117 | acpi_unlock_battery_dir(acpi_battery_dir); |
Alexey Starikovskiy | d738096 | 2007-09-26 19:43:04 +0400 | [diff] [blame] | 1118 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | module_init(acpi_battery_init); |
| 1122 | module_exit(acpi_battery_exit); |