blob: d465ae6cdd004b9813cc333529c54ef29abcb16f [file] [log] [blame]
Rich Townsend3f86b832006-07-01 11:36:54 -04001/*
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +04002 * sbs.c - ACPI Smart Battery System Driver ($Revision: 2.0 $)
Rich Townsend3f86b832006-07-01 11:36:54 -04003 *
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +04004 * Copyright (c) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (c) 2005-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
Rich Townsend3f86b832006-07-01 11:36:54 -04006 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Rich Townsend3f86b832006-07-01 11:36:54 -040029#include <linux/module.h>
30#include <linux/moduleparam.h>
31#include <linux/kernel.h>
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040032
Rich Townsend3f86b832006-07-01 11:36:54 -040033#include <linux/acpi.h>
Vladimir Lebedev6d157022007-03-19 17:45:50 +030034#include <linux/timer.h>
Vladimir Lebedev72206232007-03-19 17:45:50 +030035#include <linux/jiffies.h>
Rich Townsend3f86b832006-07-01 11:36:54 -040036#include <linux/delay.h>
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040037#include <linux/power_supply.h>
38
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040039#include "sbshc.h"
40
Len Browna192a952009-07-28 16:45:54 -040041#define PREFIX "ACPI: "
42
Rich Townsend3f86b832006-07-01 11:36:54 -040043#define ACPI_SBS_CLASS "sbs"
44#define ACPI_AC_CLASS "ac_adapter"
45#define ACPI_BATTERY_CLASS "battery"
Rich Townsend3f86b832006-07-01 11:36:54 -040046#define ACPI_SBS_DEVICE_NAME "Smart Battery System"
47#define ACPI_SBS_FILE_INFO "info"
48#define ACPI_SBS_FILE_STATE "state"
49#define ACPI_SBS_FILE_ALARM "alarm"
50#define ACPI_BATTERY_DIR_NAME "BAT%i"
51#define ACPI_AC_DIR_NAME "AC0"
Rich Townsend3f86b832006-07-01 11:36:54 -040052
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040053#define ACPI_SBS_NOTIFY_STATUS 0x80
54#define ACPI_SBS_NOTIFY_INFO 0x81
Rich Townsend3f86b832006-07-01 11:36:54 -040055
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040056MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Rich Townsend3f86b832006-07-01 11:36:54 -040057MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
58MODULE_LICENSE("GPL");
59
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040060static unsigned int cache_time = 1000;
61module_param(cache_time, uint, 0644);
62MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedev6d157022007-03-19 17:45:50 +030063
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040064#define MAX_SBS_BAT 4
Vladimir Lebedev6d157022007-03-19 17:45:50 +030065#define ACPI_SBS_BLOCK_MAX 32
66
Thomas Renninger1ba90e32007-07-23 14:44:41 +020067static const struct acpi_device_id sbs_device_ids[] = {
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040068 {"ACPI0002", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020069 {"", 0},
70};
71MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
72
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040073struct acpi_battery {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040074 struct power_supply bat;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040075 struct acpi_sbs *sbs;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040076 unsigned long update_time;
77 char name[8];
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040078 char manufacturer_name[ACPI_SBS_BLOCK_MAX];
79 char device_name[ACPI_SBS_BLOCK_MAX];
80 char device_chemistry[ACPI_SBS_BLOCK_MAX];
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040081 u16 alarm_capacity;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040082 u16 full_charge_capacity;
83 u16 design_capacity;
84 u16 design_voltage;
85 u16 serial_number;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040086 u16 cycle_count;
87 u16 temp_now;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040088 u16 voltage_now;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -040089 s16 rate_now;
90 s16 rate_avg;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040091 u16 capacity_now;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040092 u16 state_of_charge;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040093 u16 state;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040094 u16 mode;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040095 u16 spec;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040096 u8 id;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040097 u8 present:1;
Jeff Garzik037cbc62007-11-18 00:32:31 +030098 u8 have_sysfs_alarm:1;
Rich Townsend3f86b832006-07-01 11:36:54 -040099};
100
Phil Carmody497888c2011-07-14 15:07:13 +0300101#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400102
Rich Townsend3f86b832006-07-01 11:36:54 -0400103struct acpi_sbs {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400104 struct power_supply charger;
Rich Townsend3f86b832006-07-01 11:36:54 -0400105 struct acpi_device *device;
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400106 struct acpi_smb_hc *hc;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400107 struct mutex lock;
Rich Townsend3f86b832006-07-01 11:36:54 -0400108 struct acpi_battery battery[MAX_SBS_BAT];
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400109 u8 batteries_supported:4;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400110 u8 manager_present:1;
111 u8 charger_present:1;
Rich Townsend3f86b832006-07-01 11:36:54 -0400112};
113
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400114#define to_acpi_sbs(x) container_of(x, struct acpi_sbs, charger)
115
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100116static int acpi_sbs_remove(struct acpi_device *device);
Lan Tianyu1dd5c712011-07-01 16:03:15 +0800117static int acpi_battery_get_state(struct acpi_battery *battery);
118
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400119static inline int battery_scale(int log)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300120{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400121 int scale = 1;
122 while (log--)
123 scale *= 10;
124 return scale;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300125}
126
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400127static inline int acpi_battery_vscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300128{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400129 return battery_scale((battery->spec & 0x0f00) >> 8);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300130}
131
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400132static inline int acpi_battery_ipscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300133{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400134 return battery_scale((battery->spec & 0xf000) >> 12);
135}
136
137static inline int acpi_battery_mode(struct acpi_battery *battery)
138{
139 return (battery->mode & 0x8000);
140}
141
142static inline int acpi_battery_scale(struct acpi_battery *battery)
143{
144 return (acpi_battery_mode(battery) ? 10 : 1) *
145 acpi_battery_ipscale(battery);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300146}
147
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400148static int sbs_get_ac_property(struct power_supply *psy,
149 enum power_supply_property psp,
150 union power_supply_propval *val)
151{
152 struct acpi_sbs *sbs = to_acpi_sbs(psy);
153 switch (psp) {
154 case POWER_SUPPLY_PROP_ONLINE:
155 val->intval = sbs->charger_present;
156 break;
157 default:
158 return -EINVAL;
159 }
160 return 0;
161}
162
163static int acpi_battery_technology(struct acpi_battery *battery)
164{
165 if (!strcasecmp("NiCd", battery->device_chemistry))
166 return POWER_SUPPLY_TECHNOLOGY_NiCd;
167 if (!strcasecmp("NiMH", battery->device_chemistry))
168 return POWER_SUPPLY_TECHNOLOGY_NiMH;
169 if (!strcasecmp("LION", battery->device_chemistry))
170 return POWER_SUPPLY_TECHNOLOGY_LION;
171 if (!strcasecmp("LiP", battery->device_chemistry))
172 return POWER_SUPPLY_TECHNOLOGY_LIPO;
173 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
174}
175
176static int acpi_sbs_battery_get_property(struct power_supply *psy,
177 enum power_supply_property psp,
178 union power_supply_propval *val)
179{
180 struct acpi_battery *battery = to_acpi_battery(psy);
181
182 if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT)
183 return -ENODEV;
Lan Tianyu1dd5c712011-07-01 16:03:15 +0800184
185 acpi_battery_get_state(battery);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400186 switch (psp) {
187 case POWER_SUPPLY_PROP_STATUS:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400188 if (battery->rate_now < 0)
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400189 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400190 else if (battery->rate_now > 0)
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400191 val->intval = POWER_SUPPLY_STATUS_CHARGING;
192 else
193 val->intval = POWER_SUPPLY_STATUS_FULL;
194 break;
195 case POWER_SUPPLY_PROP_PRESENT:
196 val->intval = battery->present;
197 break;
198 case POWER_SUPPLY_PROP_TECHNOLOGY:
199 val->intval = acpi_battery_technology(battery);
200 break;
Alexey Starikovskiy16698852009-10-15 14:31:37 +0400201 case POWER_SUPPLY_PROP_CYCLE_COUNT:
202 val->intval = battery->cycle_count;
203 break;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400204 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
205 val->intval = battery->design_voltage *
206 acpi_battery_vscale(battery) * 1000;
207 break;
208 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
209 val->intval = battery->voltage_now *
210 acpi_battery_vscale(battery) * 1000;
211 break;
212 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400213 case POWER_SUPPLY_PROP_POWER_NOW:
214 val->intval = abs(battery->rate_now) *
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400215 acpi_battery_ipscale(battery) * 1000;
Lan Tianyue4108292011-07-01 16:03:39 +0800216 val->intval *= (acpi_battery_mode(battery)) ?
217 (battery->voltage_now *
218 acpi_battery_vscale(battery) / 1000) : 1;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400219 break;
220 case POWER_SUPPLY_PROP_CURRENT_AVG:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400221 case POWER_SUPPLY_PROP_POWER_AVG:
222 val->intval = abs(battery->rate_avg) *
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400223 acpi_battery_ipscale(battery) * 1000;
Lan Tianyue4108292011-07-01 16:03:39 +0800224 val->intval *= (acpi_battery_mode(battery)) ?
225 (battery->voltage_now *
226 acpi_battery_vscale(battery) / 1000) : 1;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400227 break;
228 case POWER_SUPPLY_PROP_CAPACITY:
229 val->intval = battery->state_of_charge;
230 break;
231 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
232 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
233 val->intval = battery->design_capacity *
234 acpi_battery_scale(battery) * 1000;
235 break;
236 case POWER_SUPPLY_PROP_CHARGE_FULL:
237 case POWER_SUPPLY_PROP_ENERGY_FULL:
238 val->intval = battery->full_charge_capacity *
239 acpi_battery_scale(battery) * 1000;
240 break;
241 case POWER_SUPPLY_PROP_CHARGE_NOW:
242 case POWER_SUPPLY_PROP_ENERGY_NOW:
243 val->intval = battery->capacity_now *
244 acpi_battery_scale(battery) * 1000;
245 break;
246 case POWER_SUPPLY_PROP_TEMP:
247 val->intval = battery->temp_now - 2730; // dK -> dC
248 break;
249 case POWER_SUPPLY_PROP_MODEL_NAME:
250 val->strval = battery->device_name;
251 break;
252 case POWER_SUPPLY_PROP_MANUFACTURER:
253 val->strval = battery->manufacturer_name;
254 break;
255 default:
256 return -EINVAL;
257 }
258 return 0;
259}
260
261static enum power_supply_property sbs_ac_props[] = {
262 POWER_SUPPLY_PROP_ONLINE,
263};
264
265static enum power_supply_property sbs_charge_battery_props[] = {
266 POWER_SUPPLY_PROP_STATUS,
267 POWER_SUPPLY_PROP_PRESENT,
268 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiy16698852009-10-15 14:31:37 +0400269 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400270 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
271 POWER_SUPPLY_PROP_VOLTAGE_NOW,
272 POWER_SUPPLY_PROP_CURRENT_NOW,
273 POWER_SUPPLY_PROP_CURRENT_AVG,
274 POWER_SUPPLY_PROP_CAPACITY,
275 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
276 POWER_SUPPLY_PROP_CHARGE_FULL,
277 POWER_SUPPLY_PROP_CHARGE_NOW,
278 POWER_SUPPLY_PROP_TEMP,
279 POWER_SUPPLY_PROP_MODEL_NAME,
280 POWER_SUPPLY_PROP_MANUFACTURER,
281};
282
283static enum power_supply_property sbs_energy_battery_props[] = {
284 POWER_SUPPLY_PROP_STATUS,
285 POWER_SUPPLY_PROP_PRESENT,
286 POWER_SUPPLY_PROP_TECHNOLOGY,
287 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
288 POWER_SUPPLY_PROP_VOLTAGE_NOW,
289 POWER_SUPPLY_PROP_CURRENT_NOW,
290 POWER_SUPPLY_PROP_CURRENT_AVG,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400291 POWER_SUPPLY_PROP_POWER_NOW,
292 POWER_SUPPLY_PROP_POWER_AVG,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400293 POWER_SUPPLY_PROP_CAPACITY,
294 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
295 POWER_SUPPLY_PROP_ENERGY_FULL,
296 POWER_SUPPLY_PROP_ENERGY_NOW,
297 POWER_SUPPLY_PROP_TEMP,
298 POWER_SUPPLY_PROP_MODEL_NAME,
299 POWER_SUPPLY_PROP_MANUFACTURER,
300};
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400301
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400302
Rich Townsend3f86b832006-07-01 11:36:54 -0400303/* --------------------------------------------------------------------------
304 Smart Battery System Management
305 -------------------------------------------------------------------------- */
306
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400307struct acpi_battery_reader {
308 u8 command; /* command for battery */
309 u8 mode; /* word or block? */
310 size_t offset; /* offset inside struct acpi_sbs_battery */
311};
Vladimir Lebedev72206232007-03-19 17:45:50 +0300312
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400313static struct acpi_battery_reader info_readers[] = {
314 {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
315 {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
316 {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
317 {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
318 {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
319 {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
320 {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
321 {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
322 {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
323 {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
324 {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
325};
Rich Townsend3f86b832006-07-01 11:36:54 -0400326
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400327static struct acpi_battery_reader state_readers[] = {
328 {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
329 {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400330 {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_now)},
331 {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, rate_avg)},
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400332 {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
333 {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
334 {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
335};
Rich Townsend3f86b832006-07-01 11:36:54 -0400336
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400337static int acpi_manager_get_info(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400338{
Rich Townsend3f86b832006-07-01 11:36:54 -0400339 int result = 0;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400340 u16 battery_system_info;
Rich Townsend3f86b832006-07-01 11:36:54 -0400341
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400342 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400343 0x04, (u8 *)&battery_system_info);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400344 if (!result)
345 sbs->batteries_supported = battery_system_info & 0x000f;
Len Brown635227e2006-07-01 16:48:23 -0400346 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400347}
348
349static int acpi_battery_get_info(struct acpi_battery *battery)
350{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400351 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400352
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400353 for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400354 result = acpi_smbus_read(battery->sbs->hc,
355 info_readers[i].mode,
356 ACPI_SBS_BATTERY,
357 info_readers[i].command,
358 (u8 *) battery +
359 info_readers[i].offset);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400360 if (result)
361 break;
Rich Townsend3f86b832006-07-01 11:36:54 -0400362 }
Len Brown635227e2006-07-01 16:48:23 -0400363 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400364}
365
Rich Townsend3f86b832006-07-01 11:36:54 -0400366static int acpi_battery_get_state(struct acpi_battery *battery)
367{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400368 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400369
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400370 if (battery->update_time &&
371 time_before(jiffies, battery->update_time +
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400372 msecs_to_jiffies(cache_time)))
373 return 0;
374 for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
375 result = acpi_smbus_read(battery->sbs->hc,
376 state_readers[i].mode,
377 ACPI_SBS_BATTERY,
378 state_readers[i].command,
379 (u8 *)battery +
380 state_readers[i].offset);
381 if (result)
382 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400383 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400384 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400385 battery->update_time = jiffies;
Len Brown635227e2006-07-01 16:48:23 -0400386 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400387}
388
389static int acpi_battery_get_alarm(struct acpi_battery *battery)
390{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400391 return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
392 ACPI_SBS_BATTERY, 0x01,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400393 (u8 *)&battery->alarm_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400394}
395
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400396static int acpi_battery_set_alarm(struct acpi_battery *battery)
Rich Townsend3f86b832006-07-01 11:36:54 -0400397{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400398 struct acpi_sbs *sbs = battery->sbs;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400399 u16 value, sel = 1 << (battery->id + 12);
400
401 int ret;
402
Rich Townsend3f86b832006-07-01 11:36:54 -0400403
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400404 if (sbs->manager_present) {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400405 ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400406 0x01, (u8 *)&value);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400407 if (ret)
408 goto end;
409 if ((value & 0xf000) != sel) {
410 value &= 0x0fff;
411 value |= sel;
412 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD,
413 ACPI_SBS_MANAGER,
414 0x01, (u8 *)&value, 2);
415 if (ret)
416 goto end;
417 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400418 }
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400419 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
420 0x01, (u8 *)&battery->alarm_capacity, 2);
421 end:
422 return ret;
Rich Townsend3f86b832006-07-01 11:36:54 -0400423}
424
425static int acpi_ac_get_present(struct acpi_sbs *sbs)
426{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400427 int result;
428 u16 status;
Rich Townsend3f86b832006-07-01 11:36:54 -0400429
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400430 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
431 0x13, (u8 *) & status);
432 if (!result)
433 sbs->charger_present = (status >> 15) & 0x1;
Len Brown635227e2006-07-01 16:48:23 -0400434 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400435}
436
Alexey Starikovskiy8bd95532007-09-26 19:44:00 +0400437static ssize_t acpi_battery_alarm_show(struct device *dev,
438 struct device_attribute *attr,
439 char *buf)
440{
441 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
442 acpi_battery_get_alarm(battery);
443 return sprintf(buf, "%d\n", battery->alarm_capacity *
444 acpi_battery_scale(battery) * 1000);
445}
446
447static ssize_t acpi_battery_alarm_store(struct device *dev,
448 struct device_attribute *attr,
449 const char *buf, size_t count)
450{
451 unsigned long x;
452 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
453 if (sscanf(buf, "%ld\n", &x) == 1)
454 battery->alarm_capacity = x /
455 (1000 * acpi_battery_scale(battery));
456 if (battery->present)
457 acpi_battery_set_alarm(battery);
458 return count;
459}
460
461static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700462 .attr = {.name = "alarm", .mode = 0644},
Alexey Starikovskiy8bd95532007-09-26 19:44:00 +0400463 .show = acpi_battery_alarm_show,
464 .store = acpi_battery_alarm_store,
465};
466
Rich Townsend3f86b832006-07-01 11:36:54 -0400467/* --------------------------------------------------------------------------
Rich Townsend3f86b832006-07-01 11:36:54 -0400468 Driver Interface
469 -------------------------------------------------------------------------- */
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400470static int acpi_battery_read(struct acpi_battery *battery)
471{
472 int result = 0, saved_present = battery->present;
473 u16 state;
474
475 if (battery->sbs->manager_present) {
476 result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
477 ACPI_SBS_MANAGER, 0x01, (u8 *)&state);
478 if (!result)
479 battery->present = state & (1 << battery->id);
480 state &= 0x0fff;
481 state |= 1 << (battery->id + 12);
482 acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
483 ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2);
484 } else if (battery->id == 0)
485 battery->present = 1;
486 if (result || !battery->present)
487 return result;
488
489 if (saved_present != battery->present) {
490 battery->update_time = 0;
491 result = acpi_battery_get_info(battery);
492 if (result)
493 return result;
494 }
495 result = acpi_battery_get_state(battery);
496 return result;
497}
498
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400499/* Smart Battery */
Rich Townsend3f86b832006-07-01 11:36:54 -0400500static int acpi_battery_add(struct acpi_sbs *sbs, int id)
501{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400502 struct acpi_battery *battery = &sbs->battery[id];
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400503 int result;
504
Rich Townsend3f86b832006-07-01 11:36:54 -0400505 battery->id = id;
506 battery->sbs = sbs;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400507 result = acpi_battery_read(battery);
508 if (result)
509 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400510
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400511 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400512 battery->bat.name = battery->name;
513 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
514 if (!acpi_battery_mode(battery)) {
515 battery->bat.properties = sbs_charge_battery_props;
516 battery->bat.num_properties =
517 ARRAY_SIZE(sbs_charge_battery_props);
518 } else {
519 battery->bat.properties = sbs_energy_battery_props;
520 battery->bat.num_properties =
521 ARRAY_SIZE(sbs_energy_battery_props);
522 }
523 battery->bat.get_property = acpi_sbs_battery_get_property;
524 result = power_supply_register(&sbs->device->dev, &battery->bat);
Jeff Garzik037cbc62007-11-18 00:32:31 +0300525 if (result)
526 goto end;
527 result = device_create_file(battery->bat.dev, &alarm_attr);
528 if (result)
529 goto end;
530 battery->have_sysfs_alarm = 1;
531 end:
Vladimir Lebedev72206232007-03-19 17:45:50 +0300532 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400533 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
Alexey Starikovskiybbafbec2008-02-09 03:22:13 -0500534 battery->name, battery->present ? "present" : "absent");
Len Brown635227e2006-07-01 16:48:23 -0400535 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400536}
537
538static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
539{
Jeff Garzik037cbc62007-11-18 00:32:31 +0300540 struct acpi_battery *battery = &sbs->battery[id];
Rakib Mullickc19bdb62010-01-03 19:27:56 +0600541
Jeff Garzik037cbc62007-11-18 00:32:31 +0300542 if (battery->bat.dev) {
543 if (battery->have_sysfs_alarm)
544 device_remove_file(battery->bat.dev, &alarm_attr);
545 power_supply_unregister(&battery->bat);
Rich Townsend3f86b832006-07-01 11:36:54 -0400546 }
547}
548
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400549static int acpi_charger_add(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400550{
551 int result;
552
Rich Townsend3f86b832006-07-01 11:36:54 -0400553 result = acpi_ac_get_present(sbs);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400554 if (result)
Rich Townsend3f86b832006-07-01 11:36:54 -0400555 goto end;
Lan Tianyu2a68b992013-10-11 09:54:09 +0800556
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400557 sbs->charger.name = "sbs-charger";
558 sbs->charger.type = POWER_SUPPLY_TYPE_MAINS;
559 sbs->charger.properties = sbs_ac_props;
560 sbs->charger.num_properties = ARRAY_SIZE(sbs_ac_props);
561 sbs->charger.get_property = sbs_get_ac_property;
562 power_supply_register(&sbs->device->dev, &sbs->charger);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300563 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
564 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400565 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
Rich Townsend3f86b832006-07-01 11:36:54 -0400566 end:
Len Brown635227e2006-07-01 16:48:23 -0400567 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400568}
569
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400570static void acpi_charger_remove(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400571{
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400572 if (sbs->charger.dev)
573 power_supply_unregister(&sbs->charger);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400574}
575
Adrian Bunke5685b92007-10-24 18:24:42 +0200576static void acpi_sbs_callback(void *context)
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400577{
578 int id;
579 struct acpi_sbs *sbs = context;
580 struct acpi_battery *bat;
581 u8 saved_charger_state = sbs->charger_present;
582 u8 saved_battery_state;
583 acpi_ac_get_present(sbs);
Thomas Renninger1696d9d2013-07-15 10:15:09 +0200584 if (sbs->charger_present != saved_charger_state)
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400585 kobject_uevent(&sbs->charger.dev->kobj, KOBJ_CHANGE);
Thomas Renninger1696d9d2013-07-15 10:15:09 +0200586
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400587 if (sbs->manager_present) {
588 for (id = 0; id < MAX_SBS_BAT; ++id) {
589 if (!(sbs->batteries_supported & (1 << id)))
590 continue;
591 bat = &sbs->battery[id];
592 saved_battery_state = bat->present;
593 acpi_battery_read(bat);
594 if (saved_battery_state == bat->present)
595 continue;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400596 kobject_uevent(&bat->bat.dev->kobj, KOBJ_CHANGE);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400597 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400598 }
599}
600
Rich Townsend3f86b832006-07-01 11:36:54 -0400601static int acpi_sbs_add(struct acpi_device *device)
602{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400603 struct acpi_sbs *sbs;
604 int result = 0;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300605 int id;
Rich Townsend3f86b832006-07-01 11:36:54 -0400606
Burman Yan36bcbec2006-12-19 12:56:11 -0800607 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -0400608 if (!sbs) {
Vladimir Lebedev72206232007-03-19 17:45:50 +0300609 result = -ENOMEM;
610 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400611 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400612
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400613 mutex_init(&sbs->lock);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300614
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400615 sbs->hc = acpi_driver_data(device->parent);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400616 sbs->device = device;
Rich Townsend3f86b832006-07-01 11:36:54 -0400617 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
618 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700619 device->driver_data = sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400620
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400621 result = acpi_charger_add(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300622 if (result)
623 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400624
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400625 result = acpi_manager_get_info(sbs);
626 if (!result) {
627 sbs->manager_present = 1;
628 for (id = 0; id < MAX_SBS_BAT; ++id)
629 if ((sbs->batteries_supported & (1 << id)))
630 acpi_battery_add(sbs, id);
631 } else
632 acpi_battery_add(sbs, 0);
633 acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs);
Rich Townsend3f86b832006-07-01 11:36:54 -0400634 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400635 if (result)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100636 acpi_sbs_remove(device);
Len Brown635227e2006-07-01 16:48:23 -0400637 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400638}
639
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100640static int acpi_sbs_remove(struct acpi_device *device)
Rich Townsend3f86b832006-07-01 11:36:54 -0400641{
Len Browncece9012006-12-16 01:04:27 -0500642 struct acpi_sbs *sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400643 int id;
644
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400645 if (!device)
Lebedev, Vladimir P963497c2006-09-05 19:49:13 +0400646 return -EINVAL;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300647 sbs = acpi_driver_data(device);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400648 if (!sbs)
Len Brown635227e2006-07-01 16:48:23 -0400649 return -EINVAL;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400650 mutex_lock(&sbs->lock);
651 acpi_smbus_unregister_callback(sbs->hc);
652 for (id = 0; id < MAX_SBS_BAT; ++id)
Rich Townsend3f86b832006-07-01 11:36:54 -0400653 acpi_battery_remove(sbs, id);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400654 acpi_charger_remove(sbs);
655 mutex_unlock(&sbs->lock);
656 mutex_destroy(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400657 kfree(sbs);
Len Brown635227e2006-07-01 16:48:23 -0400658 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400659}
660
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200661#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockid202f772012-06-27 23:27:07 +0200662static int acpi_sbs_resume(struct device *dev)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300663{
664 struct acpi_sbs *sbs;
Rafael J. Wysockid202f772012-06-27 23:27:07 +0200665 if (!dev)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300666 return -EINVAL;
Rafael J. Wysockid202f772012-06-27 23:27:07 +0200667 sbs = to_acpi_device(dev)->driver_data;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400668 acpi_sbs_callback(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300669 return 0;
670}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200671#endif
Vladimir Lebedev72206232007-03-19 17:45:50 +0300672
Rafael J. Wysockid202f772012-06-27 23:27:07 +0200673static SIMPLE_DEV_PM_OPS(acpi_sbs_pm, NULL, acpi_sbs_resume);
674
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400675static struct acpi_driver acpi_sbs_driver = {
676 .name = "sbs",
677 .class = ACPI_SBS_CLASS,
678 .ids = sbs_device_ids,
679 .ops = {
680 .add = acpi_sbs_add,
681 .remove = acpi_sbs_remove,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400682 },
Rafael J. Wysockid202f772012-06-27 23:27:07 +0200683 .drv.pm = &acpi_sbs_pm,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400684};
685
Rich Townsend3f86b832006-07-01 11:36:54 -0400686static int __init acpi_sbs_init(void)
687{
688 int result = 0;
689
Len Brownb20d2ae2006-08-15 23:21:37 -0400690 if (acpi_disabled)
691 return -ENODEV;
Lan Tianyu2a68b992013-10-11 09:54:09 +0800692
Rich Townsend3f86b832006-07-01 11:36:54 -0400693 result = acpi_bus_register_driver(&acpi_sbs_driver);
Lan Tianyu2a68b992013-10-11 09:54:09 +0800694 if (result < 0)
Len Brown635227e2006-07-01 16:48:23 -0400695 return -ENODEV;
Lan Tianyu2a68b992013-10-11 09:54:09 +0800696
Len Brown635227e2006-07-01 16:48:23 -0400697 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400698}
699
700static void __exit acpi_sbs_exit(void)
701{
Rich Townsend3f86b832006-07-01 11:36:54 -0400702 acpi_bus_unregister_driver(&acpi_sbs_driver);
Len Brown635227e2006-07-01 16:48:23 -0400703 return;
Rich Townsend3f86b832006-07-01 11:36:54 -0400704}
705
706module_init(acpi_sbs_init);
707module_exit(acpi_sbs_exit);