blob: 73c1ba314299fa28582be042e91f44c8f6bcf2df [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>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/kernel.h>
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040031
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +040032#ifdef CONFIG_ACPI_PROCFS
Rich Townsend3f86b832006-07-01 11:36:54 -040033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <asm/uaccess.h>
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +040036#endif
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040037
Rich Townsend3f86b832006-07-01 11:36:54 -040038#include <linux/acpi.h>
Vladimir Lebedev6d157022007-03-19 17:45:50 +030039#include <linux/timer.h>
Vladimir Lebedev72206232007-03-19 17:45:50 +030040#include <linux/jiffies.h>
Rich Townsend3f86b832006-07-01 11:36:54 -040041#include <linux/delay.h>
42
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040043#include <linux/power_supply.h>
44
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040045#include "sbshc.h"
46
Rich Townsend3f86b832006-07-01 11:36:54 -040047#define ACPI_SBS_CLASS "sbs"
48#define ACPI_AC_CLASS "ac_adapter"
49#define ACPI_BATTERY_CLASS "battery"
Rich Townsend3f86b832006-07-01 11:36:54 -040050#define ACPI_SBS_DEVICE_NAME "Smart Battery System"
51#define ACPI_SBS_FILE_INFO "info"
52#define ACPI_SBS_FILE_STATE "state"
53#define ACPI_SBS_FILE_ALARM "alarm"
54#define ACPI_BATTERY_DIR_NAME "BAT%i"
55#define ACPI_AC_DIR_NAME "AC0"
Rich Townsend3f86b832006-07-01 11:36:54 -040056
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040057enum acpi_sbs_device_addr {
58 ACPI_SBS_CHARGER = 0x9,
59 ACPI_SBS_MANAGER = 0xa,
60 ACPI_SBS_BATTERY = 0xb,
61};
62
63#define ACPI_SBS_NOTIFY_STATUS 0x80
64#define ACPI_SBS_NOTIFY_INFO 0x81
Rich Townsend3f86b832006-07-01 11:36:54 -040065
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040066MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Rich Townsend3f86b832006-07-01 11:36:54 -040067MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
68MODULE_LICENSE("GPL");
69
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040070static unsigned int cache_time = 1000;
71module_param(cache_time, uint, 0644);
72MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedev6d157022007-03-19 17:45:50 +030073
74extern struct proc_dir_entry *acpi_lock_ac_dir(void);
75extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
77extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
78
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040079#define MAX_SBS_BAT 4
Vladimir Lebedev6d157022007-03-19 17:45:50 +030080#define ACPI_SBS_BLOCK_MAX 32
81
Thomas Renninger1ba90e32007-07-23 14:44:41 +020082static const struct acpi_device_id sbs_device_ids[] = {
Alexey Starikovskiy91087df2007-09-26 19:43:28 +040083 {"ACPI0002", 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +020084 {"", 0},
85};
86MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
87
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040088struct acpi_battery {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040089 struct power_supply bat;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040090 struct acpi_sbs *sbs;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +040091#ifdef CONFIG_ACPI_PROCFS
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040092 struct proc_dir_entry *proc_entry;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +040093#endif
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +040094 unsigned long update_time;
95 char name[8];
Alexey Starikovskiy89862e32007-09-26 19:43:35 +040096 char manufacturer_name[ACPI_SBS_BLOCK_MAX];
97 char device_name[ACPI_SBS_BLOCK_MAX];
98 char device_chemistry[ACPI_SBS_BLOCK_MAX];
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +040099 u16 alarm_capacity;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400100 u16 full_charge_capacity;
101 u16 design_capacity;
102 u16 design_voltage;
103 u16 serial_number;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400104 u16 cycle_count;
105 u16 temp_now;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400106 u16 voltage_now;
107 s16 current_now;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400108 s16 current_avg;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400109 u16 capacity_now;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400110 u16 state_of_charge;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400111 u16 state;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400112 u16 mode;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400113 u16 spec;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400114 u8 id;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400115 u8 present:1;
Jeff Garzik037cbc62007-11-18 00:32:31 +0300116 u8 have_sysfs_alarm:1;
Rich Townsend3f86b832006-07-01 11:36:54 -0400117};
118
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400119#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
120
Rich Townsend3f86b832006-07-01 11:36:54 -0400121struct acpi_sbs {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400122 struct power_supply charger;
Rich Townsend3f86b832006-07-01 11:36:54 -0400123 struct acpi_device *device;
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400124 struct acpi_smb_hc *hc;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400125 struct mutex lock;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400126#ifdef CONFIG_ACPI_PROCFS
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400127 struct proc_dir_entry *charger_entry;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400128#endif
Rich Townsend3f86b832006-07-01 11:36:54 -0400129 struct acpi_battery battery[MAX_SBS_BAT];
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400130 u8 batteries_supported:4;
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400131 u8 manager_present:1;
132 u8 charger_present:1;
Rich Townsend3f86b832006-07-01 11:36:54 -0400133};
134
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400135#define to_acpi_sbs(x) container_of(x, struct acpi_sbs, charger)
136
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400137static inline int battery_scale(int log)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300138{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400139 int scale = 1;
140 while (log--)
141 scale *= 10;
142 return scale;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300143}
144
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400145static inline int acpi_battery_vscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300146{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400147 return battery_scale((battery->spec & 0x0f00) >> 8);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300148}
149
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400150static inline int acpi_battery_ipscale(struct acpi_battery *battery)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300151{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400152 return battery_scale((battery->spec & 0xf000) >> 12);
153}
154
155static inline int acpi_battery_mode(struct acpi_battery *battery)
156{
157 return (battery->mode & 0x8000);
158}
159
160static inline int acpi_battery_scale(struct acpi_battery *battery)
161{
162 return (acpi_battery_mode(battery) ? 10 : 1) *
163 acpi_battery_ipscale(battery);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300164}
165
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400166static int sbs_get_ac_property(struct power_supply *psy,
167 enum power_supply_property psp,
168 union power_supply_propval *val)
169{
170 struct acpi_sbs *sbs = to_acpi_sbs(psy);
171 switch (psp) {
172 case POWER_SUPPLY_PROP_ONLINE:
173 val->intval = sbs->charger_present;
174 break;
175 default:
176 return -EINVAL;
177 }
178 return 0;
179}
180
181static int acpi_battery_technology(struct acpi_battery *battery)
182{
183 if (!strcasecmp("NiCd", battery->device_chemistry))
184 return POWER_SUPPLY_TECHNOLOGY_NiCd;
185 if (!strcasecmp("NiMH", battery->device_chemistry))
186 return POWER_SUPPLY_TECHNOLOGY_NiMH;
187 if (!strcasecmp("LION", battery->device_chemistry))
188 return POWER_SUPPLY_TECHNOLOGY_LION;
189 if (!strcasecmp("LiP", battery->device_chemistry))
190 return POWER_SUPPLY_TECHNOLOGY_LIPO;
191 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
192}
193
194static int acpi_sbs_battery_get_property(struct power_supply *psy,
195 enum power_supply_property psp,
196 union power_supply_propval *val)
197{
198 struct acpi_battery *battery = to_acpi_battery(psy);
199
200 if ((!battery->present) && psp != POWER_SUPPLY_PROP_PRESENT)
201 return -ENODEV;
202 switch (psp) {
203 case POWER_SUPPLY_PROP_STATUS:
204 if (battery->current_now < 0)
205 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
206 else if (battery->current_now > 0)
207 val->intval = POWER_SUPPLY_STATUS_CHARGING;
208 else
209 val->intval = POWER_SUPPLY_STATUS_FULL;
210 break;
211 case POWER_SUPPLY_PROP_PRESENT:
212 val->intval = battery->present;
213 break;
214 case POWER_SUPPLY_PROP_TECHNOLOGY:
215 val->intval = acpi_battery_technology(battery);
216 break;
217 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
218 val->intval = battery->design_voltage *
219 acpi_battery_vscale(battery) * 1000;
220 break;
221 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
222 val->intval = battery->voltage_now *
223 acpi_battery_vscale(battery) * 1000;
224 break;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
226 val->intval = abs(battery->current_now) *
227 acpi_battery_ipscale(battery) * 1000;
228 break;
229 case POWER_SUPPLY_PROP_CURRENT_AVG:
230 val->intval = abs(battery->current_avg) *
231 acpi_battery_ipscale(battery) * 1000;
232 break;
233 case POWER_SUPPLY_PROP_CAPACITY:
234 val->intval = battery->state_of_charge;
235 break;
236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
237 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
238 val->intval = battery->design_capacity *
239 acpi_battery_scale(battery) * 1000;
240 break;
241 case POWER_SUPPLY_PROP_CHARGE_FULL:
242 case POWER_SUPPLY_PROP_ENERGY_FULL:
243 val->intval = battery->full_charge_capacity *
244 acpi_battery_scale(battery) * 1000;
245 break;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
248 val->intval = battery->capacity_now *
249 acpi_battery_scale(battery) * 1000;
250 break;
251 case POWER_SUPPLY_PROP_TEMP:
252 val->intval = battery->temp_now - 2730; // dK -> dC
253 break;
254 case POWER_SUPPLY_PROP_MODEL_NAME:
255 val->strval = battery->device_name;
256 break;
257 case POWER_SUPPLY_PROP_MANUFACTURER:
258 val->strval = battery->manufacturer_name;
259 break;
260 default:
261 return -EINVAL;
262 }
263 return 0;
264}
265
266static enum power_supply_property sbs_ac_props[] = {
267 POWER_SUPPLY_PROP_ONLINE,
268};
269
270static enum power_supply_property sbs_charge_battery_props[] = {
271 POWER_SUPPLY_PROP_STATUS,
272 POWER_SUPPLY_PROP_PRESENT,
273 POWER_SUPPLY_PROP_TECHNOLOGY,
274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
277 POWER_SUPPLY_PROP_CURRENT_AVG,
278 POWER_SUPPLY_PROP_CAPACITY,
279 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
280 POWER_SUPPLY_PROP_CHARGE_FULL,
281 POWER_SUPPLY_PROP_CHARGE_NOW,
282 POWER_SUPPLY_PROP_TEMP,
283 POWER_SUPPLY_PROP_MODEL_NAME,
284 POWER_SUPPLY_PROP_MANUFACTURER,
285};
286
287static enum power_supply_property sbs_energy_battery_props[] = {
288 POWER_SUPPLY_PROP_STATUS,
289 POWER_SUPPLY_PROP_PRESENT,
290 POWER_SUPPLY_PROP_TECHNOLOGY,
291 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
292 POWER_SUPPLY_PROP_VOLTAGE_NOW,
293 POWER_SUPPLY_PROP_CURRENT_NOW,
294 POWER_SUPPLY_PROP_CURRENT_AVG,
295 POWER_SUPPLY_PROP_CAPACITY,
296 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
297 POWER_SUPPLY_PROP_ENERGY_FULL,
298 POWER_SUPPLY_PROP_ENERGY_NOW,
299 POWER_SUPPLY_PROP_TEMP,
300 POWER_SUPPLY_PROP_MODEL_NAME,
301 POWER_SUPPLY_PROP_MANUFACTURER,
302};
303
Rich Townsend3f86b832006-07-01 11:36:54 -0400304/* --------------------------------------------------------------------------
305 Smart Battery System Management
306 -------------------------------------------------------------------------- */
307
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400308struct acpi_battery_reader {
309 u8 command; /* command for battery */
310 u8 mode; /* word or block? */
311 size_t offset; /* offset inside struct acpi_sbs_battery */
312};
Vladimir Lebedev72206232007-03-19 17:45:50 +0300313
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400314static struct acpi_battery_reader info_readers[] = {
315 {0x01, SMBUS_READ_WORD, offsetof(struct acpi_battery, alarm_capacity)},
316 {0x03, SMBUS_READ_WORD, offsetof(struct acpi_battery, mode)},
317 {0x10, SMBUS_READ_WORD, offsetof(struct acpi_battery, full_charge_capacity)},
318 {0x17, SMBUS_READ_WORD, offsetof(struct acpi_battery, cycle_count)},
319 {0x18, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_capacity)},
320 {0x19, SMBUS_READ_WORD, offsetof(struct acpi_battery, design_voltage)},
321 {0x1a, SMBUS_READ_WORD, offsetof(struct acpi_battery, spec)},
322 {0x1c, SMBUS_READ_WORD, offsetof(struct acpi_battery, serial_number)},
323 {0x20, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, manufacturer_name)},
324 {0x21, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_name)},
325 {0x22, SMBUS_READ_BLOCK, offsetof(struct acpi_battery, device_chemistry)},
326};
Rich Townsend3f86b832006-07-01 11:36:54 -0400327
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400328static struct acpi_battery_reader state_readers[] = {
329 {0x08, SMBUS_READ_WORD, offsetof(struct acpi_battery, temp_now)},
330 {0x09, SMBUS_READ_WORD, offsetof(struct acpi_battery, voltage_now)},
331 {0x0a, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_now)},
332 {0x0b, SMBUS_READ_WORD, offsetof(struct acpi_battery, current_avg)},
333 {0x0f, SMBUS_READ_WORD, offsetof(struct acpi_battery, capacity_now)},
334 {0x0e, SMBUS_READ_WORD, offsetof(struct acpi_battery, state_of_charge)},
335 {0x16, SMBUS_READ_WORD, offsetof(struct acpi_battery, state)},
336};
Rich Townsend3f86b832006-07-01 11:36:54 -0400337
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400338static int acpi_manager_get_info(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400339{
Rich Townsend3f86b832006-07-01 11:36:54 -0400340 int result = 0;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400341 u16 battery_system_info;
Rich Townsend3f86b832006-07-01 11:36:54 -0400342
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400343 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400344 0x04, (u8 *)&battery_system_info);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400345 if (!result)
346 sbs->batteries_supported = battery_system_info & 0x000f;
Len Brown635227e2006-07-01 16:48:23 -0400347 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400348}
349
350static int acpi_battery_get_info(struct acpi_battery *battery)
351{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400352 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400353
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400354 for (i = 0; i < ARRAY_SIZE(info_readers); ++i) {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400355 result = acpi_smbus_read(battery->sbs->hc,
356 info_readers[i].mode,
357 ACPI_SBS_BATTERY,
358 info_readers[i].command,
359 (u8 *) battery +
360 info_readers[i].offset);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400361 if (result)
362 break;
Rich Townsend3f86b832006-07-01 11:36:54 -0400363 }
Len Brown635227e2006-07-01 16:48:23 -0400364 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400365}
366
Rich Townsend3f86b832006-07-01 11:36:54 -0400367static int acpi_battery_get_state(struct acpi_battery *battery)
368{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400369 int i, result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400370
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400371 if (battery->update_time &&
372 time_before(jiffies, battery->update_time +
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400373 msecs_to_jiffies(cache_time)))
374 return 0;
375 for (i = 0; i < ARRAY_SIZE(state_readers); ++i) {
376 result = acpi_smbus_read(battery->sbs->hc,
377 state_readers[i].mode,
378 ACPI_SBS_BATTERY,
379 state_readers[i].command,
380 (u8 *)battery +
381 state_readers[i].offset);
382 if (result)
383 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400384 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400385 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400386 battery->update_time = jiffies;
Len Brown635227e2006-07-01 16:48:23 -0400387 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400388}
389
390static int acpi_battery_get_alarm(struct acpi_battery *battery)
391{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400392 return acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
393 ACPI_SBS_BATTERY, 0x01,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400394 (u8 *)&battery->alarm_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400395}
396
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400397static int acpi_battery_set_alarm(struct acpi_battery *battery)
Rich Townsend3f86b832006-07-01 11:36:54 -0400398{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400399 struct acpi_sbs *sbs = battery->sbs;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400400 u16 value, sel = 1 << (battery->id + 12);
401
402 int ret;
403
Rich Townsend3f86b832006-07-01 11:36:54 -0400404
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400405 if (sbs->manager_present) {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400406 ret = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_MANAGER,
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400407 0x01, (u8 *)&value);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400408 if (ret)
409 goto end;
410 if ((value & 0xf000) != sel) {
411 value &= 0x0fff;
412 value |= sel;
413 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD,
414 ACPI_SBS_MANAGER,
415 0x01, (u8 *)&value, 2);
416 if (ret)
417 goto end;
418 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400419 }
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400420 ret = acpi_smbus_write(sbs->hc, SMBUS_WRITE_WORD, ACPI_SBS_BATTERY,
421 0x01, (u8 *)&battery->alarm_capacity, 2);
422 end:
423 return ret;
Rich Townsend3f86b832006-07-01 11:36:54 -0400424}
425
426static int acpi_ac_get_present(struct acpi_sbs *sbs)
427{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400428 int result;
429 u16 status;
Rich Townsend3f86b832006-07-01 11:36:54 -0400430
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400431 result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBS_CHARGER,
432 0x13, (u8 *) & status);
433 if (!result)
434 sbs->charger_present = (status >> 15) & 0x1;
Len Brown635227e2006-07-01 16:48:23 -0400435 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400436}
437
Alexey Starikovskiy8bd95532007-09-26 19:44:00 +0400438static ssize_t acpi_battery_alarm_show(struct device *dev,
439 struct device_attribute *attr,
440 char *buf)
441{
442 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
443 acpi_battery_get_alarm(battery);
444 return sprintf(buf, "%d\n", battery->alarm_capacity *
445 acpi_battery_scale(battery) * 1000);
446}
447
448static ssize_t acpi_battery_alarm_store(struct device *dev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
451{
452 unsigned long x;
453 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
454 if (sscanf(buf, "%ld\n", &x) == 1)
455 battery->alarm_capacity = x /
456 (1000 * acpi_battery_scale(battery));
457 if (battery->present)
458 acpi_battery_set_alarm(battery);
459 return count;
460}
461
462static struct device_attribute alarm_attr = {
463 .attr = {.name = "alarm", .mode = 0644, .owner = THIS_MODULE},
464 .show = acpi_battery_alarm_show,
465 .store = acpi_battery_alarm_store,
466};
467
Rich Townsend3f86b832006-07-01 11:36:54 -0400468/* --------------------------------------------------------------------------
469 FS Interface (/proc/acpi)
470 -------------------------------------------------------------------------- */
471
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400472#ifdef CONFIG_ACPI_PROCFS
Rich Townsend3f86b832006-07-01 11:36:54 -0400473/* Generic Routines */
Rich Townsend3f86b832006-07-01 11:36:54 -0400474static int
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400475acpi_sbs_add_fs(struct proc_dir_entry **dir,
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400476 struct proc_dir_entry *parent_dir,
477 char *dir_name,
478 struct file_operations *info_fops,
479 struct file_operations *state_fops,
480 struct file_operations *alarm_fops, void *data)
Rich Townsend3f86b832006-07-01 11:36:54 -0400481{
482 struct proc_dir_entry *entry = NULL;
483
Rich Townsend3f86b832006-07-01 11:36:54 -0400484 if (!*dir) {
485 *dir = proc_mkdir(dir_name, parent_dir);
486 if (!*dir) {
Len Brown635227e2006-07-01 16:48:23 -0400487 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400488 }
489 (*dir)->owner = THIS_MODULE;
490 }
491
492 /* 'info' [R] */
493 if (info_fops) {
494 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400495 if (entry) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400496 entry->proc_fops = info_fops;
497 entry->data = data;
498 entry->owner = THIS_MODULE;
499 }
500 }
501
502 /* 'state' [R] */
503 if (state_fops) {
504 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400505 if (entry) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400506 entry->proc_fops = state_fops;
507 entry->data = data;
508 entry->owner = THIS_MODULE;
509 }
510 }
511
512 /* 'alarm' [R/W] */
513 if (alarm_fops) {
514 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400515 if (entry) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400516 entry->proc_fops = alarm_fops;
517 entry->data = data;
518 entry->owner = THIS_MODULE;
519 }
520 }
Len Brown635227e2006-07-01 16:48:23 -0400521 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400522}
523
524static void
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400525acpi_sbs_remove_fs(struct proc_dir_entry **dir,
Rich Townsend3f86b832006-07-01 11:36:54 -0400526 struct proc_dir_entry *parent_dir)
527{
Rich Townsend3f86b832006-07-01 11:36:54 -0400528 if (*dir) {
529 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
530 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
531 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
532 remove_proc_entry((*dir)->name, parent_dir);
533 *dir = NULL;
534 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400535}
536
537/* Smart Battery Interface */
Rich Townsend3f86b832006-07-01 11:36:54 -0400538static struct proc_dir_entry *acpi_battery_dir = NULL;
539
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400540static inline char *acpi_battery_units(struct acpi_battery *battery)
541{
542 return acpi_battery_mode(battery) ? " mWh" : " mAh";
543}
544
545
Rich Townsend3f86b832006-07-01 11:36:54 -0400546static int acpi_battery_read_info(struct seq_file *seq, void *offset)
547{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200548 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300549 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400550 int result = 0;
551
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400552 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400553
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400554 seq_printf(seq, "present: %s\n",
555 (battery->present) ? "yes" : "no");
556 if (!battery->present)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300557 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400558
Vladimir Lebedev72206232007-03-19 17:45:50 +0300559 seq_printf(seq, "design capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400560 battery->design_capacity * acpi_battery_scale(battery),
561 acpi_battery_units(battery));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300562 seq_printf(seq, "last full capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400563 battery->full_charge_capacity * acpi_battery_scale(battery),
564 acpi_battery_units(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400565 seq_printf(seq, "battery technology: rechargeable\n");
Rich Townsend3f86b832006-07-01 11:36:54 -0400566 seq_printf(seq, "design voltage: %i mV\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400567 battery->design_voltage * acpi_battery_vscale(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400568 seq_printf(seq, "design capacity warning: unknown\n");
569 seq_printf(seq, "design capacity low: unknown\n");
570 seq_printf(seq, "capacity granularity 1: unknown\n");
571 seq_printf(seq, "capacity granularity 2: unknown\n");
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400572 seq_printf(seq, "model number: %s\n", battery->device_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400573 seq_printf(seq, "serial number: %i\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400574 battery->serial_number);
Rich Townsend3f86b832006-07-01 11:36:54 -0400575 seq_printf(seq, "battery type: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400576 battery->device_chemistry);
Rich Townsend3f86b832006-07-01 11:36:54 -0400577 seq_printf(seq, "OEM info: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400578 battery->manufacturer_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400579 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400580 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400581 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400582}
583
584static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
585{
586 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
587}
588
589static int acpi_battery_read_state(struct seq_file *seq, void *offset)
590{
Vladimir Lebedev72206232007-03-19 17:45:50 +0300591 struct acpi_battery *battery = seq->private;
592 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400593 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400594
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400595 mutex_lock(&sbs->lock);
596 seq_printf(seq, "present: %s\n",
597 (battery->present) ? "yes" : "no");
598 if (!battery->present)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300599 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400600
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400601 acpi_battery_get_state(battery);
602 seq_printf(seq, "capacity state: %s\n",
603 (battery->state & 0x0010) ? "critical" : "ok");
604 seq_printf(seq, "charging state: %s\n",
605 (battery->current_now < 0) ? "discharging" :
606 ((battery->current_now > 0) ? "charging" : "charged"));
607 seq_printf(seq, "present rate: %d mA\n",
608 abs(battery->current_now) * acpi_battery_ipscale(battery));
Vladimir Lebedev72206232007-03-19 17:45:50 +0300609 seq_printf(seq, "remaining capacity: %i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400610 battery->capacity_now * acpi_battery_scale(battery),
611 acpi_battery_units(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400612 seq_printf(seq, "present voltage: %i mV\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400613 battery->voltage_now * acpi_battery_vscale(battery));
Rich Townsend3f86b832006-07-01 11:36:54 -0400614
615 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400616 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400617 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400618}
619
620static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
621{
622 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
623}
624
625static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
626{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200627 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300628 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400629 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400630
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400631 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400632
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400633 if (!battery->present) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400634 seq_printf(seq, "present: no\n");
635 goto end;
636 }
637
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400638 acpi_battery_get_alarm(battery);
Rich Townsend3f86b832006-07-01 11:36:54 -0400639 seq_printf(seq, "alarm: ");
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400640 if (battery->alarm_capacity)
Vladimir Lebedev72206232007-03-19 17:45:50 +0300641 seq_printf(seq, "%i%s\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400642 battery->alarm_capacity *
643 acpi_battery_scale(battery),
644 acpi_battery_units(battery));
645 else
Rich Townsend3f86b832006-07-01 11:36:54 -0400646 seq_printf(seq, "disabled\n");
Rich Townsend3f86b832006-07-01 11:36:54 -0400647 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400648 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400649 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400650}
651
652static ssize_t
653acpi_battery_write_alarm(struct file *file, const char __user * buffer,
654 size_t count, loff_t * ppos)
655{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200656 struct seq_file *seq = file->private_data;
657 struct acpi_battery *battery = seq->private;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300658 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400659 char alarm_string[12] = { '\0' };
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400660 int result = 0;
661 mutex_lock(&sbs->lock);
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400662 if (!battery->present) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400663 result = -ENODEV;
664 goto end;
665 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400666 if (count > sizeof(alarm_string) - 1) {
667 result = -EINVAL;
668 goto end;
669 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400670 if (copy_from_user(alarm_string, buffer, count)) {
671 result = -EFAULT;
672 goto end;
673 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400674 alarm_string[count] = 0;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400675 battery->alarm_capacity = simple_strtoul(alarm_string, NULL, 0) /
676 acpi_battery_scale(battery);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400677 acpi_battery_set_alarm(battery);
Rich Townsend3f86b832006-07-01 11:36:54 -0400678 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400679 mutex_unlock(&sbs->lock);
680 if (result)
Len Brown635227e2006-07-01 16:48:23 -0400681 return result;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400682 return count;
Rich Townsend3f86b832006-07-01 11:36:54 -0400683}
684
685static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
686{
687 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
688}
689
690static struct file_operations acpi_battery_info_fops = {
691 .open = acpi_battery_info_open_fs,
692 .read = seq_read,
693 .llseek = seq_lseek,
694 .release = single_release,
695 .owner = THIS_MODULE,
696};
697
698static struct file_operations acpi_battery_state_fops = {
699 .open = acpi_battery_state_open_fs,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703 .owner = THIS_MODULE,
704};
705
706static struct file_operations acpi_battery_alarm_fops = {
707 .open = acpi_battery_alarm_open_fs,
708 .read = seq_read,
709 .write = acpi_battery_write_alarm,
710 .llseek = seq_lseek,
711 .release = single_release,
712 .owner = THIS_MODULE,
713};
714
715/* Legacy AC Adapter Interface */
716
717static struct proc_dir_entry *acpi_ac_dir = NULL;
718
719static int acpi_ac_read_state(struct seq_file *seq, void *offset)
720{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400721
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200722 struct acpi_sbs *sbs = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -0400723
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400724 mutex_lock(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400725
726 seq_printf(seq, "state: %s\n",
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400727 sbs->charger_present ? "on-line" : "off-line");
Rich Townsend3f86b832006-07-01 11:36:54 -0400728
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400729 mutex_unlock(&sbs->lock);
Len Brown635227e2006-07-01 16:48:23 -0400730 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400731}
732
733static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
734{
735 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
736}
737
738static struct file_operations acpi_ac_state_fops = {
739 .open = acpi_ac_state_open_fs,
740 .read = seq_read,
741 .llseek = seq_lseek,
742 .release = single_release,
743 .owner = THIS_MODULE,
744};
745
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400746#endif
747
Rich Townsend3f86b832006-07-01 11:36:54 -0400748/* --------------------------------------------------------------------------
749 Driver Interface
750 -------------------------------------------------------------------------- */
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400751static int acpi_battery_read(struct acpi_battery *battery)
752{
753 int result = 0, saved_present = battery->present;
754 u16 state;
755
756 if (battery->sbs->manager_present) {
757 result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
758 ACPI_SBS_MANAGER, 0x01, (u8 *)&state);
759 if (!result)
760 battery->present = state & (1 << battery->id);
761 state &= 0x0fff;
762 state |= 1 << (battery->id + 12);
763 acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
764 ACPI_SBS_MANAGER, 0x01, (u8 *)&state, 2);
765 } else if (battery->id == 0)
766 battery->present = 1;
767 if (result || !battery->present)
768 return result;
769
770 if (saved_present != battery->present) {
771 battery->update_time = 0;
772 result = acpi_battery_get_info(battery);
773 if (result)
774 return result;
775 }
776 result = acpi_battery_get_state(battery);
777 return result;
778}
779
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400780/* Smart Battery */
Rich Townsend3f86b832006-07-01 11:36:54 -0400781static int acpi_battery_add(struct acpi_sbs *sbs, int id)
782{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400783 struct acpi_battery *battery = &sbs->battery[id];
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400784 int result;
785
Rich Townsend3f86b832006-07-01 11:36:54 -0400786 battery->id = id;
787 battery->sbs = sbs;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400788 result = acpi_battery_read(battery);
789 if (result)
790 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400791
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400792 sprintf(battery->name, ACPI_BATTERY_DIR_NAME, id);
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400793#ifdef CONFIG_ACPI_PROCFS
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400794 acpi_sbs_add_fs(&battery->proc_entry, acpi_battery_dir,
795 battery->name, &acpi_battery_info_fops,
796 &acpi_battery_state_fops, &acpi_battery_alarm_fops,
797 battery);
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400798#endif
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400799 battery->bat.name = battery->name;
800 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
801 if (!acpi_battery_mode(battery)) {
802 battery->bat.properties = sbs_charge_battery_props;
803 battery->bat.num_properties =
804 ARRAY_SIZE(sbs_charge_battery_props);
805 } else {
806 battery->bat.properties = sbs_energy_battery_props;
807 battery->bat.num_properties =
808 ARRAY_SIZE(sbs_energy_battery_props);
809 }
810 battery->bat.get_property = acpi_sbs_battery_get_property;
811 result = power_supply_register(&sbs->device->dev, &battery->bat);
Jeff Garzik037cbc62007-11-18 00:32:31 +0300812 if (result)
813 goto end;
814 result = device_create_file(battery->bat.dev, &alarm_attr);
815 if (result)
816 goto end;
817 battery->have_sysfs_alarm = 1;
818 end:
Vladimir Lebedev72206232007-03-19 17:45:50 +0300819 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400820 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
821 battery->name, sbs->battery->present ? "present" : "absent");
Len Brown635227e2006-07-01 16:48:23 -0400822 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400823}
824
825static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
826{
Jeff Garzik037cbc62007-11-18 00:32:31 +0300827 struct acpi_battery *battery = &sbs->battery[id];
828
829 if (battery->bat.dev) {
830 if (battery->have_sysfs_alarm)
831 device_remove_file(battery->bat.dev, &alarm_attr);
832 power_supply_unregister(&battery->bat);
Rich Townsend3f86b832006-07-01 11:36:54 -0400833 }
Jeff Garzik037cbc62007-11-18 00:32:31 +0300834#ifdef CONFIG_ACPI_PROCFS
835 if (battery->proc_entry)
836 acpi_sbs_remove_fs(&battery->proc_entry, acpi_battery_dir);
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400837#endif
Rich Townsend3f86b832006-07-01 11:36:54 -0400838}
839
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400840static int acpi_charger_add(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400841{
842 int result;
843
Rich Townsend3f86b832006-07-01 11:36:54 -0400844 result = acpi_ac_get_present(sbs);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400845 if (result)
Rich Townsend3f86b832006-07-01 11:36:54 -0400846 goto end;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400847#ifdef CONFIG_ACPI_PROCFS
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400848 result = acpi_sbs_add_fs(&sbs->charger_entry, acpi_ac_dir,
849 ACPI_AC_DIR_NAME, NULL,
850 &acpi_ac_state_fops, NULL, sbs);
851 if (result)
Rich Townsend3f86b832006-07-01 11:36:54 -0400852 goto end;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400853#endif
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400854 sbs->charger.name = "sbs-charger";
855 sbs->charger.type = POWER_SUPPLY_TYPE_MAINS;
856 sbs->charger.properties = sbs_ac_props;
857 sbs->charger.num_properties = ARRAY_SIZE(sbs_ac_props);
858 sbs->charger.get_property = sbs_get_ac_property;
859 power_supply_register(&sbs->device->dev, &sbs->charger);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300860 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
861 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
Alexey Starikovskiy89862e32007-09-26 19:43:35 +0400862 ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
Rich Townsend3f86b832006-07-01 11:36:54 -0400863 end:
Len Brown635227e2006-07-01 16:48:23 -0400864 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400865}
866
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400867static void acpi_charger_remove(struct acpi_sbs *sbs)
Rich Townsend3f86b832006-07-01 11:36:54 -0400868{
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400869 if (sbs->charger.dev)
870 power_supply_unregister(&sbs->charger);
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400871#ifdef CONFIG_ACPI_PROCFS
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400872 if (sbs->charger_entry)
873 acpi_sbs_remove_fs(&sbs->charger_entry, acpi_ac_dir);
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400874#endif
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400875}
876
877void acpi_sbs_callback(void *context)
878{
879 int id;
880 struct acpi_sbs *sbs = context;
881 struct acpi_battery *bat;
882 u8 saved_charger_state = sbs->charger_present;
883 u8 saved_battery_state;
884 acpi_ac_get_present(sbs);
885 if (sbs->charger_present != saved_charger_state) {
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400886#ifdef CONFIG_ACPI_PROC_EVENT
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400887 acpi_bus_generate_proc_event4(ACPI_AC_CLASS, ACPI_AC_DIR_NAME,
888 ACPI_SBS_NOTIFY_STATUS,
889 sbs->charger_present);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400890#endif
891 kobject_uevent(&sbs->charger.dev->kobj, KOBJ_CHANGE);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400892 }
893 if (sbs->manager_present) {
894 for (id = 0; id < MAX_SBS_BAT; ++id) {
895 if (!(sbs->batteries_supported & (1 << id)))
896 continue;
897 bat = &sbs->battery[id];
898 saved_battery_state = bat->present;
899 acpi_battery_read(bat);
900 if (saved_battery_state == bat->present)
901 continue;
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400902#ifdef CONFIG_ACPI_PROC_EVENT
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400903 acpi_bus_generate_proc_event4(ACPI_BATTERY_CLASS,
904 bat->name,
905 ACPI_SBS_NOTIFY_STATUS,
906 bat->present);
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400907#endif
908 kobject_uevent(&bat->bat.dev->kobj, KOBJ_CHANGE);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400909 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400910 }
911}
912
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400913static int acpi_sbs_remove(struct acpi_device *device, int type);
Rich Townsend3f86b832006-07-01 11:36:54 -0400914
915static int acpi_sbs_add(struct acpi_device *device)
916{
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400917 struct acpi_sbs *sbs;
918 int result = 0;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300919 int id;
Rich Townsend3f86b832006-07-01 11:36:54 -0400920
Burman Yan36bcbec2006-12-19 12:56:11 -0800921 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -0400922 if (!sbs) {
Vladimir Lebedev72206232007-03-19 17:45:50 +0300923 result = -ENOMEM;
924 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400925 }
Rich Townsend3f86b832006-07-01 11:36:54 -0400926
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400927 mutex_init(&sbs->lock);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300928
Alexey Starikovskiy91087df2007-09-26 19:43:28 +0400929 sbs->hc = acpi_driver_data(device->parent);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400930 sbs->device = device;
Rich Townsend3f86b832006-07-01 11:36:54 -0400931 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
932 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
933 acpi_driver_data(device) = sbs;
934
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400935 result = acpi_charger_add(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300936 if (result)
937 goto end;
Rich Townsend3f86b832006-07-01 11:36:54 -0400938
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400939 result = acpi_manager_get_info(sbs);
940 if (!result) {
941 sbs->manager_present = 1;
942 for (id = 0; id < MAX_SBS_BAT; ++id)
943 if ((sbs->batteries_supported & (1 << id)))
944 acpi_battery_add(sbs, id);
945 } else
946 acpi_battery_add(sbs, 0);
947 acpi_smbus_register_callback(sbs->hc, acpi_sbs_callback, sbs);
Rich Townsend3f86b832006-07-01 11:36:54 -0400948 end:
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400949 if (result)
950 acpi_sbs_remove(device, 0);
Len Brown635227e2006-07-01 16:48:23 -0400951 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400952}
953
Vladimir Lebedev72206232007-03-19 17:45:50 +0300954static int acpi_sbs_remove(struct acpi_device *device, int type)
Rich Townsend3f86b832006-07-01 11:36:54 -0400955{
Len Browncece9012006-12-16 01:04:27 -0500956 struct acpi_sbs *sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400957 int id;
958
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400959 if (!device)
Lebedev, Vladimir P963497c2006-09-05 19:49:13 +0400960 return -EINVAL;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300961 sbs = acpi_driver_data(device);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400962 if (!sbs)
Len Brown635227e2006-07-01 16:48:23 -0400963 return -EINVAL;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400964 mutex_lock(&sbs->lock);
965 acpi_smbus_unregister_callback(sbs->hc);
966 for (id = 0; id < MAX_SBS_BAT; ++id)
Rich Townsend3f86b832006-07-01 11:36:54 -0400967 acpi_battery_remove(sbs, id);
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400968 acpi_charger_remove(sbs);
969 mutex_unlock(&sbs->lock);
970 mutex_destroy(&sbs->lock);
Rich Townsend3f86b832006-07-01 11:36:54 -0400971 kfree(sbs);
Len Brown635227e2006-07-01 16:48:23 -0400972 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400973}
974
Vladimir Lebedev72206232007-03-19 17:45:50 +0300975static void acpi_sbs_rmdirs(void)
976{
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400977#ifdef CONFIG_ACPI_PROCFS
Vladimir Lebedev72206232007-03-19 17:45:50 +0300978 if (acpi_ac_dir) {
979 acpi_unlock_ac_dir(acpi_ac_dir);
980 acpi_ac_dir = NULL;
981 }
982 if (acpi_battery_dir) {
983 acpi_unlock_battery_dir(acpi_battery_dir);
984 acpi_battery_dir = NULL;
985 }
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +0400986#endif
Vladimir Lebedev72206232007-03-19 17:45:50 +0300987}
988
989static int acpi_sbs_resume(struct acpi_device *device)
990{
991 struct acpi_sbs *sbs;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300992 if (!device)
993 return -EINVAL;
Vladimir Lebedev72206232007-03-19 17:45:50 +0300994 sbs = device->driver_data;
Alexey Starikovskiydb1c2912007-09-26 19:43:41 +0400995 acpi_sbs_callback(sbs);
Vladimir Lebedev72206232007-03-19 17:45:50 +0300996 return 0;
997}
998
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +0400999static struct acpi_driver acpi_sbs_driver = {
1000 .name = "sbs",
1001 .class = ACPI_SBS_CLASS,
1002 .ids = sbs_device_ids,
1003 .ops = {
1004 .add = acpi_sbs_add,
1005 .remove = acpi_sbs_remove,
1006 .resume = acpi_sbs_resume,
1007 },
1008};
1009
Rich Townsend3f86b832006-07-01 11:36:54 -04001010static int __init acpi_sbs_init(void)
1011{
1012 int result = 0;
1013
Len Brownb20d2ae2006-08-15 23:21:37 -04001014 if (acpi_disabled)
1015 return -ENODEV;
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +04001016#ifdef CONFIG_ACPI_PROCFS
Rich Townsend3f86b832006-07-01 11:36:54 -04001017 acpi_ac_dir = acpi_lock_ac_dir();
Alexey Starikovskiy94f6c082007-09-26 19:43:48 +04001018 if (!acpi_ac_dir)
Len Brown635227e2006-07-01 16:48:23 -04001019 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001020 acpi_battery_dir = acpi_lock_battery_dir();
1021 if (!acpi_battery_dir) {
Vladimir Lebedev72206232007-03-19 17:45:50 +03001022 acpi_sbs_rmdirs();
Len Brown635227e2006-07-01 16:48:23 -04001023 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001024 }
Alexey Starikovskiy66e4b722007-09-26 19:43:54 +04001025#endif
Rich Townsend3f86b832006-07-01 11:36:54 -04001026 result = acpi_bus_register_driver(&acpi_sbs_driver);
1027 if (result < 0) {
Vladimir Lebedev72206232007-03-19 17:45:50 +03001028 acpi_sbs_rmdirs();
Len Brown635227e2006-07-01 16:48:23 -04001029 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001030 }
Len Brown635227e2006-07-01 16:48:23 -04001031 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001032}
1033
1034static void __exit acpi_sbs_exit(void)
1035{
Rich Townsend3f86b832006-07-01 11:36:54 -04001036 acpi_bus_unregister_driver(&acpi_sbs_driver);
Vladimir Lebedev72206232007-03-19 17:45:50 +03001037 acpi_sbs_rmdirs();
Len Brown635227e2006-07-01 16:48:23 -04001038 return;
Rich Townsend3f86b832006-07-01 11:36:54 -04001039}
1040
1041module_init(acpi_sbs_init);
1042module_exit(acpi_sbs_exit);