2 * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
4 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <asm/uaccess.h>
32 #include <linux/acpi.h>
33 #include <linux/timer.h>
34 #include <linux/jiffies.h>
35 #include <linux/delay.h>
37 #define ACPI_SBS_COMPONENT 0x00080000
38 #define ACPI_SBS_CLASS "sbs"
39 #define ACPI_AC_CLASS "ac_adapter"
40 #define ACPI_BATTERY_CLASS "battery"
41 #define ACPI_SBS_HID "ACPI0002"
42 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
43 #define ACPI_SBS_FILE_INFO "info"
44 #define ACPI_SBS_FILE_STATE "state"
45 #define ACPI_SBS_FILE_ALARM "alarm"
46 #define ACPI_BATTERY_DIR_NAME "BAT%i"
47 #define ACPI_AC_DIR_NAME "AC0"
48 #define ACPI_SBC_SMBUS_ADDR 0x9
49 #define ACPI_SBSM_SMBUS_ADDR 0xa
50 #define ACPI_SB_SMBUS_ADDR 0xb
51 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
52 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
53 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
55 #define _COMPONENT ACPI_SBS_COMPONENT
57 ACPI_MODULE_NAME("sbs");
59 MODULE_AUTHOR("Rich Townsend");
60 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
61 MODULE_LICENSE("GPL");
63 #define xmsleep(t) msleep(t)
65 #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */
67 #define ACPI_EC_SMB_STS 0x01 /* status */
68 #define ACPI_EC_SMB_ADDR 0x02 /* address */
69 #define ACPI_EC_SMB_CMD 0x03 /* command */
70 #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */
71 #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */
73 #define ACPI_EC_SMB_STS_DONE 0x80
74 #define ACPI_EC_SMB_STS_STATUS 0x1f
76 #define ACPI_EC_SMB_PRTCL_WRITE 0x00
77 #define ACPI_EC_SMB_PRTCL_READ 0x01
78 #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08
79 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a
81 #define ACPI_EC_SMB_TRANSACTION_SLEEP 1
82 #define ACPI_EC_SMB_ACCESS_SLEEP1 1
83 #define ACPI_EC_SMB_ACCESS_SLEEP2 10
85 #define DEF_CAPACITY_UNIT 3
86 #define MAH_CAPACITY_UNIT 1
87 #define MWH_CAPACITY_UNIT 2
88 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
90 #define REQUEST_UPDATE_MODE 1
91 #define QUEUE_UPDATE_MODE 2
93 #define DATA_TYPE_COMMON 0
94 #define DATA_TYPE_INFO 1
95 #define DATA_TYPE_STATE 2
96 #define DATA_TYPE_ALARM 3
97 #define DATA_TYPE_AC_STATE 4
99 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
100 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
101 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
102 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
104 #define MAX_SBS_BAT 4
105 #define ACPI_SBS_BLOCK_MAX 32
107 #define ACPI_SBS_SMBUS_READ 1
108 #define ACPI_SBS_SMBUS_WRITE 2
110 #define ACPI_SBS_WORD_DATA 1
111 #define ACPI_SBS_BLOCK_DATA 2
113 #define UPDATE_DELAY 10
115 /* 0 - every time, > 0 - by update_time */
116 static unsigned int update_time = 120;
118 static unsigned int capacity_mode = CAPACITY_UNIT;
120 module_param(update_time, uint, 0644);
121 module_param(capacity_mode, uint, 0444);
123 static int acpi_sbs_add(struct acpi_device *device);
124 static int acpi_sbs_remove(struct acpi_device *device, int type);
125 static int acpi_sbs_resume(struct acpi_device *device);
127 static struct acpi_driver acpi_sbs_driver = {
129 .class = ACPI_SBS_CLASS,
133 .remove = acpi_sbs_remove,
134 .resume = acpi_sbs_resume,
142 struct acpi_battery_info {
144 s16 full_charge_capacity;
150 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
151 char device_name[ACPI_SBS_BLOCK_MAX + 3];
152 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
155 struct acpi_battery_state {
158 s16 remaining_capacity;
162 struct acpi_battery_alarm {
163 s16 remaining_capacity;
166 struct acpi_battery {
171 struct acpi_sbs *sbs;
172 struct acpi_battery_info info;
173 struct acpi_battery_state state;
174 struct acpi_battery_alarm alarm;
175 struct proc_dir_entry *battery_entry;
181 struct acpi_device *device;
182 struct acpi_ec_smbus *smbus;
185 int sbsm_batteries_supported;
186 struct proc_dir_entry *ac_entry;
188 struct acpi_battery battery[MAX_SBS_BAT];
190 struct timer_list update_timer;
195 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
196 static void acpi_sbs_update_time(void *data);
200 u8 block[ACPI_SBS_BLOCK_MAX + 2];
203 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
204 char read_write, u8 command, int size,
205 union sbs_rw_data *data);
207 /* --------------------------------------------------------------------------
209 -------------------------------------------------------------------------- */
211 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
216 err = ec_read(sbs->base + address, &val);
220 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
224 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
228 err = ec_write(sbs->base + address, data);
233 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
234 char read_write, u8 command, int size,
235 union sbs_rw_data *data)
237 unsigned char protocol, len = 0, temp[2] = { 0, 0 };
240 if (read_write == ACPI_SBS_SMBUS_READ) {
241 protocol = ACPI_EC_SMB_PRTCL_READ;
243 protocol = ACPI_EC_SMB_PRTCL_WRITE;
248 case ACPI_SBS_WORD_DATA:
249 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
250 if (read_write == ACPI_SBS_SMBUS_WRITE) {
251 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
252 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
255 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
257 case ACPI_SBS_BLOCK_DATA:
258 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
259 if (read_write == ACPI_SBS_SMBUS_WRITE) {
260 len = min_t(u8, data->block[0], 32);
261 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
262 for (i = 0; i < len; i++)
263 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
266 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
269 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
270 "unsupported transaction %d", size));
274 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
275 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
277 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
279 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
280 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1);
281 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
283 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
284 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
285 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
287 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
288 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
289 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
290 "transaction %d error", size));
294 if (read_write == ACPI_SBS_SMBUS_WRITE) {
300 case ACPI_SBS_WORD_DATA:
301 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
302 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
303 data->word = (temp[1] << 8) | temp[0];
306 case ACPI_SBS_BLOCK_DATA:
308 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
309 len = min_t(u8, len, 32);
310 for (i = 0; i < len; i++)
311 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
312 data->block + i + 1);
313 data->block[0] = len;
316 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
317 "unsupported transaction %d", size));
325 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
327 union sbs_rw_data data;
330 result = acpi_ec_sbs_access(sbs, addr,
331 ACPI_SBS_SMBUS_READ, func,
332 ACPI_SBS_WORD_DATA, &data);
334 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
335 "acpi_ec_sbs_access() failed"));
344 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
346 union sbs_rw_data data;
349 result = acpi_ec_sbs_access(sbs, addr,
350 ACPI_SBS_SMBUS_READ, func,
351 ACPI_SBS_BLOCK_DATA, &data);
353 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
354 "acpi_ec_sbs_access() failed"));
356 strncpy(str, (const char *)data.block + 1, data.block[0]);
357 str[data.block[0]] = 0;
364 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
366 union sbs_rw_data data;
371 result = acpi_ec_sbs_access(sbs, addr,
372 ACPI_SBS_SMBUS_WRITE, func,
373 ACPI_SBS_WORD_DATA, &data);
375 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
376 "acpi_ec_sbs_access() failed"));
382 static int sbs_zombie(struct acpi_sbs *sbs)
384 return (sbs->zombie);
387 static int sbs_mutex_lock(struct acpi_sbs *sbs)
389 if (sbs_zombie(sbs)) {
392 mutex_lock(&sbs->mutex);
396 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
398 mutex_unlock(&sbs->mutex);
401 /* --------------------------------------------------------------------------
402 Smart Battery System Management
403 -------------------------------------------------------------------------- */
405 static int acpi_check_update_proc(struct acpi_sbs *sbs)
407 acpi_status status = AE_OK;
409 if (update_time == 0) {
410 sbs->update_proc_flg = 0;
413 if (sbs->update_proc_flg == 0) {
414 status = acpi_os_execute(OSL_GPE_HANDLER,
415 acpi_sbs_update_time, sbs);
416 if (status != AE_OK) {
417 ACPI_EXCEPTION((AE_INFO, status,
418 "acpi_os_execute() failed"));
421 sbs->update_proc_flg = 1;
426 static int acpi_sbs_generate_event(struct acpi_device *device,
427 int event, int state, char *bid, char *class)
430 char class_saved[20];
433 strcpy(bid_saved, acpi_device_bid(device));
434 strcpy(class_saved, acpi_device_class(device));
436 strcpy(acpi_device_bid(device), bid);
437 strcpy(acpi_device_class(device), class);
439 result = acpi_bus_generate_event(device, event, state);
441 strcpy(acpi_device_bid(device), bid_saved);
442 strcpy(acpi_device_class(device), class_saved);
447 static int acpi_battery_get_present(struct acpi_battery *battery)
453 result = acpi_sbs_read_word(battery->sbs,
454 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
456 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
457 "acpi_sbs_read_word() failed"));
460 is_present = (state & 0x000f) & (1 << battery->id);
462 battery->battery_present = is_present;
467 static int acpi_battery_select(struct acpi_battery *battery)
469 struct acpi_sbs *sbs = battery->sbs;
474 if (sbs->sbsm_present) {
476 /* Take special care not to knobble other nibbles of
477 * state (aka selector_state), since
478 * it causes charging to halt on SBSELs */
481 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
483 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
484 "acpi_sbs_read_word() failed"));
488 foo = (state & 0x0fff) | (1 << (battery->id + 12));
490 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
492 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
493 "acpi_sbs_write_word() failed"));
502 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
505 s16 battery_system_info;
507 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
508 &battery_system_info);
510 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
511 "acpi_sbs_read_word() failed"));
515 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
522 static int acpi_battery_get_info(struct acpi_battery *battery)
524 struct acpi_sbs *sbs = battery->sbs;
527 s16 specification_info;
529 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
532 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
533 "acpi_sbs_read_word() failed"));
536 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
538 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
539 &battery->info.full_charge_capacity);
541 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
542 "acpi_sbs_read_word() failed"));
546 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
547 &battery->info.design_capacity);
550 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
551 "acpi_sbs_read_word() failed"));
555 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
556 &battery->info.design_voltage);
558 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
559 "acpi_sbs_read_word() failed"));
563 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
564 &specification_info);
566 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
567 "acpi_sbs_read_word() failed"));
571 switch ((specification_info & 0x0f00) >> 8) {
573 battery->info.vscale = 10;
576 battery->info.vscale = 100;
579 battery->info.vscale = 1000;
582 battery->info.vscale = 1;
585 switch ((specification_info & 0xf000) >> 12) {
587 battery->info.ipscale = 10;
590 battery->info.ipscale = 100;
593 battery->info.ipscale = 1000;
596 battery->info.ipscale = 1;
599 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
600 &battery->info.serial_number);
602 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
603 "acpi_sbs_read_word() failed"));
607 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
608 battery->info.manufacturer_name);
610 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
611 "acpi_sbs_read_str() failed"));
615 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
616 battery->info.device_name);
618 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
619 "acpi_sbs_read_str() failed"));
623 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
624 battery->info.device_chemistry);
626 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
627 "acpi_sbs_read_str() failed"));
635 static int acpi_battery_get_state(struct acpi_battery *battery)
637 struct acpi_sbs *sbs = battery->sbs;
640 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
641 &battery->state.voltage);
643 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
644 "acpi_sbs_read_word() failed"));
648 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
649 &battery->state.amperage);
651 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
652 "acpi_sbs_read_word() failed"));
656 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
657 &battery->state.remaining_capacity);
659 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
660 "acpi_sbs_read_word() failed"));
664 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
665 &battery->state.battery_state);
667 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
668 "acpi_sbs_read_word() failed"));
676 static int acpi_battery_get_alarm(struct acpi_battery *battery)
678 struct acpi_sbs *sbs = battery->sbs;
681 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
682 &battery->alarm.remaining_capacity);
684 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
685 "acpi_sbs_read_word() failed"));
694 static int acpi_battery_set_alarm(struct acpi_battery *battery,
697 struct acpi_sbs *sbs = battery->sbs;
702 result = acpi_battery_select(battery);
704 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
705 "acpi_battery_select() failed"));
709 /* If necessary, enable the alarm */
713 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
716 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
717 "acpi_sbs_read_word() failed"));
722 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
723 battery_mode & 0xbfff);
725 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
726 "acpi_sbs_write_word() failed"));
731 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
732 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
734 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
735 "acpi_sbs_write_word() failed"));
744 static int acpi_battery_set_mode(struct acpi_battery *battery)
746 struct acpi_sbs *sbs = battery->sbs;
750 if (capacity_mode == DEF_CAPACITY_UNIT) {
754 result = acpi_sbs_read_word(sbs,
755 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
757 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
758 "acpi_sbs_read_word() failed"));
762 if (capacity_mode == MAH_CAPACITY_UNIT) {
763 battery_mode &= 0x7fff;
765 battery_mode |= 0x8000;
767 result = acpi_sbs_write_word(sbs,
768 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
770 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
771 "acpi_sbs_write_word() failed"));
775 result = acpi_sbs_read_word(sbs,
776 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
778 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
779 "acpi_sbs_read_word() failed"));
787 static int acpi_battery_init(struct acpi_battery *battery)
791 result = acpi_battery_select(battery);
793 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
794 "acpi_battery_select() failed"));
798 result = acpi_battery_set_mode(battery);
800 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
801 "acpi_battery_set_mode() failed"));
805 result = acpi_battery_get_info(battery);
807 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
808 "acpi_battery_get_info() failed"));
812 result = acpi_battery_get_state(battery);
814 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
815 "acpi_battery_get_state() failed"));
819 result = acpi_battery_get_alarm(battery);
821 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
822 "acpi_battery_get_alarm() failed"));
830 static int acpi_ac_get_present(struct acpi_sbs *sbs)
835 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
839 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
840 "acpi_sbs_read_word() failed"));
844 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
851 /* --------------------------------------------------------------------------
852 FS Interface (/proc/acpi)
853 -------------------------------------------------------------------------- */
855 /* Generic Routines */
858 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
859 struct proc_dir_entry *parent_dir,
861 struct file_operations *info_fops,
862 struct file_operations *state_fops,
863 struct file_operations *alarm_fops, void *data)
865 struct proc_dir_entry *entry = NULL;
868 *dir = proc_mkdir(dir_name, parent_dir);
870 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
871 "proc_mkdir() failed"));
874 (*dir)->owner = THIS_MODULE;
879 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
881 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
882 "create_proc_entry() failed"));
884 entry->proc_fops = info_fops;
886 entry->owner = THIS_MODULE;
892 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
894 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
895 "create_proc_entry() failed"));
897 entry->proc_fops = state_fops;
899 entry->owner = THIS_MODULE;
905 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
907 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
908 "create_proc_entry() failed"));
910 entry->proc_fops = alarm_fops;
912 entry->owner = THIS_MODULE;
920 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
921 struct proc_dir_entry *parent_dir)
925 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
926 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
927 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
928 remove_proc_entry((*dir)->name, parent_dir);
934 /* Smart Battery Interface */
936 static struct proc_dir_entry *acpi_battery_dir = NULL;
938 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
940 struct acpi_battery *battery = seq->private;
941 struct acpi_sbs *sbs = battery->sbs;
945 if (sbs_mutex_lock(sbs)) {
949 result = acpi_check_update_proc(sbs);
953 if (update_time == 0) {
954 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
956 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
957 "acpi_sbs_update_run() failed"));
961 if (battery->battery_present) {
962 seq_printf(seq, "present: yes\n");
964 seq_printf(seq, "present: no\n");
968 if (battery->info.capacity_mode) {
969 cscale = battery->info.vscale * battery->info.ipscale;
971 cscale = battery->info.ipscale;
973 seq_printf(seq, "design capacity: %i%s\n",
974 battery->info.design_capacity * cscale,
975 battery->info.capacity_mode ? "0 mWh" : " mAh");
977 seq_printf(seq, "last full capacity: %i%s\n",
978 battery->info.full_charge_capacity * cscale,
979 battery->info.capacity_mode ? "0 mWh" : " mAh");
981 seq_printf(seq, "battery technology: rechargeable\n");
983 seq_printf(seq, "design voltage: %i mV\n",
984 battery->info.design_voltage * battery->info.vscale);
986 seq_printf(seq, "design capacity warning: unknown\n");
987 seq_printf(seq, "design capacity low: unknown\n");
988 seq_printf(seq, "capacity granularity 1: unknown\n");
989 seq_printf(seq, "capacity granularity 2: unknown\n");
991 seq_printf(seq, "model number: %s\n",
992 battery->info.device_name);
994 seq_printf(seq, "serial number: %i\n",
995 battery->info.serial_number);
997 seq_printf(seq, "battery type: %s\n",
998 battery->info.device_chemistry);
1000 seq_printf(seq, "OEM info: %s\n",
1001 battery->info.manufacturer_name);
1005 sbs_mutex_unlock(sbs);
1010 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1012 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1015 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1017 struct acpi_battery *battery = seq->private;
1018 struct acpi_sbs *sbs = battery->sbs;
1023 if (sbs_mutex_lock(sbs)) {
1027 result = acpi_check_update_proc(sbs);
1031 if (update_time == 0) {
1032 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1034 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1035 "acpi_sbs_update_run() failed"));
1039 if (battery->battery_present) {
1040 seq_printf(seq, "present: yes\n");
1042 seq_printf(seq, "present: no\n");
1046 if (battery->info.capacity_mode) {
1047 cscale = battery->info.vscale * battery->info.ipscale;
1049 cscale = battery->info.ipscale;
1052 if (battery->state.battery_state & 0x0010) {
1053 seq_printf(seq, "capacity state: critical\n");
1055 seq_printf(seq, "capacity state: ok\n");
1058 foo = (s16) battery->state.amperage * battery->info.ipscale;
1059 if (battery->info.capacity_mode) {
1060 foo = foo * battery->info.design_voltage / 1000;
1062 if (battery->state.amperage < 0) {
1063 seq_printf(seq, "charging state: discharging\n");
1064 seq_printf(seq, "present rate: %d %s\n",
1065 -foo, battery->info.capacity_mode ? "mW" : "mA");
1066 } else if (battery->state.amperage > 0) {
1067 seq_printf(seq, "charging state: charging\n");
1068 seq_printf(seq, "present rate: %d %s\n",
1069 foo, battery->info.capacity_mode ? "mW" : "mA");
1071 seq_printf(seq, "charging state: charged\n");
1072 seq_printf(seq, "present rate: 0 %s\n",
1073 battery->info.capacity_mode ? "mW" : "mA");
1076 seq_printf(seq, "remaining capacity: %i%s\n",
1077 battery->state.remaining_capacity * cscale,
1078 battery->info.capacity_mode ? "0 mWh" : " mAh");
1080 seq_printf(seq, "present voltage: %i mV\n",
1081 battery->state.voltage * battery->info.vscale);
1085 sbs_mutex_unlock(sbs);
1090 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1092 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1095 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1097 struct acpi_battery *battery = seq->private;
1098 struct acpi_sbs *sbs = battery->sbs;
1102 if (sbs_mutex_lock(sbs)) {
1106 result = acpi_check_update_proc(sbs);
1110 if (update_time == 0) {
1111 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1113 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1114 "acpi_sbs_update_run() failed"));
1118 if (!battery->battery_present) {
1119 seq_printf(seq, "present: no\n");
1123 if (battery->info.capacity_mode) {
1124 cscale = battery->info.vscale * battery->info.ipscale;
1126 cscale = battery->info.ipscale;
1129 seq_printf(seq, "alarm: ");
1130 if (battery->alarm.remaining_capacity) {
1131 seq_printf(seq, "%i%s\n",
1132 battery->alarm.remaining_capacity * cscale,
1133 battery->info.capacity_mode ? "0 mWh" : " mAh");
1135 seq_printf(seq, "disabled\n");
1140 sbs_mutex_unlock(sbs);
1146 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1147 size_t count, loff_t * ppos)
1149 struct seq_file *seq = file->private_data;
1150 struct acpi_battery *battery = seq->private;
1151 struct acpi_sbs *sbs = battery->sbs;
1152 char alarm_string[12] = { '\0' };
1153 int result, old_alarm, new_alarm;
1155 if (sbs_mutex_lock(sbs)) {
1159 result = acpi_check_update_proc(sbs);
1163 if (!battery->battery_present) {
1168 if (count > sizeof(alarm_string) - 1) {
1173 if (copy_from_user(alarm_string, buffer, count)) {
1178 alarm_string[count] = 0;
1180 old_alarm = battery->alarm.remaining_capacity;
1181 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1183 result = acpi_battery_set_alarm(battery, new_alarm);
1185 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1186 "acpi_battery_set_alarm() failed"));
1187 acpi_battery_set_alarm(battery, old_alarm);
1190 result = acpi_battery_get_alarm(battery);
1192 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1193 "acpi_battery_get_alarm() failed"));
1194 acpi_battery_set_alarm(battery, old_alarm);
1199 sbs_mutex_unlock(sbs);
1208 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1210 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1213 static struct file_operations acpi_battery_info_fops = {
1214 .open = acpi_battery_info_open_fs,
1216 .llseek = seq_lseek,
1217 .release = single_release,
1218 .owner = THIS_MODULE,
1221 static struct file_operations acpi_battery_state_fops = {
1222 .open = acpi_battery_state_open_fs,
1224 .llseek = seq_lseek,
1225 .release = single_release,
1226 .owner = THIS_MODULE,
1229 static struct file_operations acpi_battery_alarm_fops = {
1230 .open = acpi_battery_alarm_open_fs,
1232 .write = acpi_battery_write_alarm,
1233 .llseek = seq_lseek,
1234 .release = single_release,
1235 .owner = THIS_MODULE,
1238 /* Legacy AC Adapter Interface */
1240 static struct proc_dir_entry *acpi_ac_dir = NULL;
1242 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1244 struct acpi_sbs *sbs = seq->private;
1247 if (sbs_mutex_lock(sbs)) {
1251 if (update_time == 0) {
1252 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1254 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1255 "acpi_sbs_update_run() failed"));
1259 seq_printf(seq, "state: %s\n",
1260 sbs->ac.ac_present ? "on-line" : "off-line");
1262 sbs_mutex_unlock(sbs);
1267 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1269 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1272 static struct file_operations acpi_ac_state_fops = {
1273 .open = acpi_ac_state_open_fs,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1277 .owner = THIS_MODULE,
1280 /* --------------------------------------------------------------------------
1282 -------------------------------------------------------------------------- */
1286 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1291 struct acpi_battery *battery;
1293 battery = &sbs->battery[id];
1297 battery->init_state = 0;
1301 result = acpi_battery_select(battery);
1303 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1304 "acpi_battery_select() failed"));
1308 result = acpi_battery_get_present(battery);
1310 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1311 "acpi_battery_get_present() failed"));
1315 is_present = battery->battery_present;
1318 result = acpi_battery_init(battery);
1320 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1321 "acpi_battery_init() failed"));
1324 battery->init_state = 1;
1327 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1329 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1332 &acpi_battery_info_fops,
1333 &acpi_battery_state_fops,
1334 &acpi_battery_alarm_fops, battery);
1336 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1337 "acpi_sbs_generic_add_fs() failed"));
1342 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1343 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1344 sbs->battery->battery_present ? "present" : "absent");
1350 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1353 if (sbs->battery[id].battery_entry) {
1354 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1359 static int acpi_ac_add(struct acpi_sbs *sbs)
1363 result = acpi_ac_get_present(sbs);
1365 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1366 "acpi_ac_get_present() failed"));
1370 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1373 NULL, &acpi_ac_state_fops, NULL, sbs);
1375 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1376 "acpi_sbs_generic_add_fs() failed"));
1380 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1381 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1382 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1389 static void acpi_ac_remove(struct acpi_sbs *sbs)
1392 if (sbs->ac_entry) {
1393 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1397 static void acpi_sbs_update_time_run(unsigned long data)
1399 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1402 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1404 struct acpi_battery *battery;
1405 int result = 0, cnt;
1406 int old_ac_present = -1;
1407 int old_battery_present = -1;
1408 int new_ac_present = -1;
1409 int new_battery_present = -1;
1410 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1412 int do_battery_init = 0, do_ac_init = 0;
1413 int old_remaining_capacity = 0;
1414 int update_ac = 1, update_battery = 1;
1415 int up_tm = update_time;
1417 if (sbs_zombie(sbs)) {
1422 id_min = id_max = id;
1425 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1426 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1427 if (sbs->run_cnt % cnt != 0) {
1434 if (!update_ac && !update_battery) {
1438 old_ac_present = sbs->ac.ac_present;
1440 result = acpi_ac_get_present(sbs);
1442 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1443 "acpi_ac_get_present() failed"));
1446 new_ac_present = sbs->ac.ac_present;
1448 do_ac_init = (old_ac_present != new_ac_present);
1449 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1454 result = acpi_sbs_generate_event(sbs->device,
1455 ACPI_SBS_AC_NOTIFY_STATUS,
1460 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1461 "acpi_sbs_generate_event() failed"));
1465 if (data_type == DATA_TYPE_COMMON) {
1466 if (!do_ac_init && !update_battery) {
1471 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1475 for (id = id_min; id <= id_max; id++) {
1476 battery = &sbs->battery[id];
1477 if (battery->alive == 0) {
1481 old_remaining_capacity = battery->state.remaining_capacity;
1483 old_battery_present = battery->battery_present;
1485 result = acpi_battery_select(battery);
1487 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1488 "acpi_battery_select() failed"));
1491 result = acpi_battery_get_present(battery);
1493 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1494 "acpi_battery_get_present() failed"));
1497 new_battery_present = battery->battery_present;
1499 do_battery_init = ((old_battery_present != new_battery_present)
1500 && new_battery_present);
1501 if (!new_battery_present)
1503 if (do_ac_init || do_battery_init) {
1504 result = acpi_battery_init(battery);
1506 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1507 "acpi_battery_init() "
1511 if (sbs_zombie(sbs)) {
1515 if ((data_type == DATA_TYPE_COMMON
1516 || data_type == DATA_TYPE_INFO)
1517 && new_battery_present) {
1518 result = acpi_battery_get_info(battery);
1520 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1521 "acpi_battery_get_info() failed"));
1524 if (data_type == DATA_TYPE_INFO) {
1527 if (sbs_zombie(sbs)) {
1531 if ((data_type == DATA_TYPE_COMMON
1532 || data_type == DATA_TYPE_STATE)
1533 && new_battery_present) {
1534 result = acpi_battery_get_state(battery);
1536 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1537 "acpi_battery_get_state() failed"));
1540 if (data_type == DATA_TYPE_STATE) {
1543 if (sbs_zombie(sbs)) {
1547 if ((data_type == DATA_TYPE_COMMON
1548 || data_type == DATA_TYPE_ALARM)
1549 && new_battery_present) {
1550 result = acpi_battery_get_alarm(battery);
1552 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1553 "acpi_battery_get_alarm() "
1557 if (data_type == DATA_TYPE_ALARM) {
1560 if (sbs_zombie(sbs)) {
1566 if (old_battery_present != new_battery_present || do_ac_init ||
1567 old_remaining_capacity !=
1568 battery->state.remaining_capacity) {
1569 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1570 result = acpi_sbs_generate_event(sbs->device,
1571 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1572 new_battery_present,
1574 ACPI_BATTERY_CLASS);
1576 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1577 "acpi_sbs_generate_event() "
1588 static void acpi_sbs_update_time(void *data)
1590 struct acpi_sbs *sbs = data;
1591 unsigned long delay = -1;
1593 unsigned int up_tm = update_time;
1595 if (sbs_mutex_lock(sbs))
1598 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1600 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1601 "acpi_sbs_update_run() failed"));
1604 if (sbs_zombie(sbs)) {
1609 if (timer_pending(&sbs->update_timer))
1610 del_timer(&sbs->update_timer);
1612 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1613 delay = jiffies + HZ * delay;
1614 if (timer_pending(&sbs->update_timer)) {
1615 mod_timer(&sbs->update_timer, delay);
1617 sbs->update_timer.data = (unsigned long)data;
1618 sbs->update_timer.function = acpi_sbs_update_time_run;
1619 sbs->update_timer.expires = delay;
1620 add_timer(&sbs->update_timer);
1626 sbs_mutex_unlock(sbs);
1629 static int acpi_sbs_add(struct acpi_device *device)
1631 struct acpi_sbs *sbs = NULL;
1632 int result = 0, remove_result = 0;
1633 unsigned long sbs_obj;
1635 acpi_status status = AE_OK;
1639 acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val);
1640 if (ACPI_FAILURE(status)) {
1641 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1645 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1647 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1652 mutex_init(&sbs->mutex);
1654 sbs_mutex_lock(sbs);
1656 sbs->base = (val & 0xff00ull) >> 8;
1657 sbs->device = device;
1659 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1660 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1661 acpi_driver_data(device) = sbs;
1663 result = acpi_ac_add(sbs);
1665 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1668 status = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1670 ACPI_EXCEPTION((AE_INFO, status,
1671 "acpi_evaluate_integer() failed"));
1676 result = acpi_sbsm_get_info(sbs);
1678 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1679 "acpi_sbsm_get_info() failed"));
1682 sbs->sbsm_present = 1;
1685 if (sbs->sbsm_present == 0) {
1686 result = acpi_battery_add(sbs, 0);
1688 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1689 "acpi_battery_add() failed"));
1693 for (id = 0; id < MAX_SBS_BAT; id++) {
1694 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1695 result = acpi_battery_add(sbs, id);
1697 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1698 "acpi_battery_add() failed"));
1705 sbs->handle = device->handle;
1707 init_timer(&sbs->update_timer);
1708 result = acpi_check_update_proc(sbs);
1714 sbs_mutex_unlock(sbs);
1717 remove_result = acpi_sbs_remove(device, 0);
1718 if (remove_result) {
1719 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1720 "acpi_sbs_remove() failed"));
1727 static int acpi_sbs_remove(struct acpi_device *device, int type)
1729 struct acpi_sbs *sbs;
1736 sbs = acpi_driver_data(device);
1741 sbs_mutex_lock(sbs);
1744 del_timer_sync(&sbs->update_timer);
1745 acpi_os_wait_events_complete(NULL);
1746 del_timer_sync(&sbs->update_timer);
1748 for (id = 0; id < MAX_SBS_BAT; id++) {
1749 acpi_battery_remove(sbs, id);
1752 acpi_ac_remove(sbs);
1754 sbs_mutex_unlock(sbs);
1756 mutex_destroy(&sbs->mutex);
1763 static void acpi_sbs_rmdirs(void)
1766 acpi_unlock_ac_dir(acpi_ac_dir);
1769 if (acpi_battery_dir) {
1770 acpi_unlock_battery_dir(acpi_battery_dir);
1771 acpi_battery_dir = NULL;
1775 static int acpi_sbs_resume(struct acpi_device *device)
1777 struct acpi_sbs *sbs;
1782 sbs = device->driver_data;
1789 static int __init acpi_sbs_init(void)
1796 if (capacity_mode != DEF_CAPACITY_UNIT
1797 && capacity_mode != MAH_CAPACITY_UNIT
1798 && capacity_mode != MWH_CAPACITY_UNIT) {
1799 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1800 "invalid capacity_mode = %d", capacity_mode));
1804 acpi_ac_dir = acpi_lock_ac_dir();
1806 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1807 "acpi_lock_ac_dir() failed"));
1811 acpi_battery_dir = acpi_lock_battery_dir();
1812 if (!acpi_battery_dir) {
1813 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1814 "acpi_lock_battery_dir() failed"));
1819 result = acpi_bus_register_driver(&acpi_sbs_driver);
1821 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1822 "acpi_bus_register_driver() failed"));
1830 static void __exit acpi_sbs_exit(void)
1832 acpi_bus_unregister_driver(&acpi_sbs_driver);
1839 module_init(acpi_sbs_init);
1840 module_exit(acpi_sbs_exit);