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