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_DEVICE_NAME "Smart Battery System"
42 #define ACPI_SBS_FILE_INFO "info"
43 #define ACPI_SBS_FILE_STATE "state"
44 #define ACPI_SBS_FILE_ALARM "alarm"
45 #define ACPI_BATTERY_DIR_NAME "BAT%i"
46 #define ACPI_AC_DIR_NAME "AC0"
47 #define ACPI_SBC_SMBUS_ADDR 0x9
48 #define ACPI_SBSM_SMBUS_ADDR 0xa
49 #define ACPI_SB_SMBUS_ADDR 0xb
50 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
51 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
52 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
54 #define _COMPONENT ACPI_SBS_COMPONENT
56 ACPI_MODULE_NAME("sbs");
58 MODULE_AUTHOR("Rich Townsend");
59 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
60 MODULE_LICENSE("GPL");
62 #define xmsleep(t) msleep(t)
64 #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */
66 #define ACPI_EC_SMB_STS 0x01 /* status */
67 #define ACPI_EC_SMB_ADDR 0x02 /* address */
68 #define ACPI_EC_SMB_CMD 0x03 /* command */
69 #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */
70 #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */
72 #define ACPI_EC_SMB_STS_DONE 0x80
73 #define ACPI_EC_SMB_STS_STATUS 0x1f
75 #define ACPI_EC_SMB_PRTCL_WRITE 0x00
76 #define ACPI_EC_SMB_PRTCL_READ 0x01
77 #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08
78 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a
80 #define ACPI_EC_SMB_TRANSACTION_SLEEP 1
81 #define ACPI_EC_SMB_ACCESS_SLEEP1 1
82 #define ACPI_EC_SMB_ACCESS_SLEEP2 10
84 #define DEF_CAPACITY_UNIT 3
85 #define MAH_CAPACITY_UNIT 1
86 #define MWH_CAPACITY_UNIT 2
87 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
89 #define REQUEST_UPDATE_MODE 1
90 #define QUEUE_UPDATE_MODE 2
92 #define DATA_TYPE_COMMON 0
93 #define DATA_TYPE_INFO 1
94 #define DATA_TYPE_STATE 2
95 #define DATA_TYPE_ALARM 3
96 #define DATA_TYPE_AC_STATE 4
98 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
99 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
100 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
101 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
103 #define MAX_SBS_BAT 4
104 #define ACPI_SBS_BLOCK_MAX 32
106 #define ACPI_SBS_SMBUS_READ 1
107 #define ACPI_SBS_SMBUS_WRITE 2
109 #define ACPI_SBS_WORD_DATA 1
110 #define ACPI_SBS_BLOCK_DATA 2
112 #define UPDATE_DELAY 10
114 /* 0 - every time, > 0 - by update_time */
115 static unsigned int update_time = 120;
117 static unsigned int capacity_mode = CAPACITY_UNIT;
119 module_param(update_time, uint, 0644);
120 module_param(capacity_mode, uint, 0444);
122 static int acpi_sbs_add(struct acpi_device *device);
123 static int acpi_sbs_remove(struct acpi_device *device, int type);
124 static int acpi_sbs_resume(struct acpi_device *device);
126 static const struct acpi_device_id sbs_device_ids[] = {
131 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
133 static struct acpi_driver acpi_sbs_driver = {
135 .class = ACPI_SBS_CLASS,
136 .ids = sbs_device_ids,
139 .remove = acpi_sbs_remove,
140 .resume = acpi_sbs_resume,
148 struct acpi_battery_info {
150 s16 full_charge_capacity;
156 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
157 char device_name[ACPI_SBS_BLOCK_MAX + 3];
158 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
161 struct acpi_battery_state {
164 s16 remaining_capacity;
168 struct acpi_battery_alarm {
169 s16 remaining_capacity;
172 struct acpi_battery {
177 struct acpi_sbs *sbs;
178 struct acpi_battery_info info;
179 struct acpi_battery_state state;
180 struct acpi_battery_alarm alarm;
181 struct proc_dir_entry *battery_entry;
186 struct acpi_device *device;
189 int sbsm_batteries_supported;
190 struct proc_dir_entry *ac_entry;
192 struct acpi_battery battery[MAX_SBS_BAT];
194 struct timer_list update_timer;
199 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
200 static void acpi_sbs_update_time(void *data);
204 u8 block[ACPI_SBS_BLOCK_MAX + 2];
207 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
208 char read_write, u8 command, int size,
209 union sbs_rw_data *data);
211 /* --------------------------------------------------------------------------
213 -------------------------------------------------------------------------- */
215 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
220 err = ec_read(sbs->base + address, &val);
224 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
228 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
232 err = ec_write(sbs->base + address, data);
237 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
238 char read_write, u8 command, int size,
239 union sbs_rw_data *data)
241 unsigned char protocol, len = 0, temp[2] = { 0, 0 };
244 if (read_write == ACPI_SBS_SMBUS_READ) {
245 protocol = ACPI_EC_SMB_PRTCL_READ;
247 protocol = ACPI_EC_SMB_PRTCL_WRITE;
252 case ACPI_SBS_WORD_DATA:
253 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
254 if (read_write == ACPI_SBS_SMBUS_WRITE) {
255 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
256 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
259 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
261 case ACPI_SBS_BLOCK_DATA:
262 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
263 if (read_write == ACPI_SBS_SMBUS_WRITE) {
264 len = min_t(u8, data->block[0], 32);
265 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
266 for (i = 0; i < len; i++)
267 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
270 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
273 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
274 "unsupported transaction %d", size));
278 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
279 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
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_SLEEP1);
285 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
287 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
288 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
289 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
291 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
292 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
293 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
294 "transaction %d error", size));
298 if (read_write == ACPI_SBS_SMBUS_WRITE) {
304 case ACPI_SBS_WORD_DATA:
305 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
306 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
307 data->word = (temp[1] << 8) | temp[0];
310 case ACPI_SBS_BLOCK_DATA:
312 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
313 len = min_t(u8, len, 32);
314 for (i = 0; i < len; i++)
315 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
316 data->block + i + 1);
317 data->block[0] = len;
320 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
321 "unsupported transaction %d", size));
329 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
331 union sbs_rw_data data;
334 result = acpi_ec_sbs_access(sbs, addr,
335 ACPI_SBS_SMBUS_READ, func,
336 ACPI_SBS_WORD_DATA, &data);
338 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
339 "acpi_ec_sbs_access() failed"));
348 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
350 union sbs_rw_data data;
353 result = acpi_ec_sbs_access(sbs, addr,
354 ACPI_SBS_SMBUS_READ, func,
355 ACPI_SBS_BLOCK_DATA, &data);
357 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
358 "acpi_ec_sbs_access() failed"));
360 strncpy(str, (const char *)data.block + 1, data.block[0]);
361 str[data.block[0]] = 0;
368 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
370 union sbs_rw_data data;
375 result = acpi_ec_sbs_access(sbs, addr,
376 ACPI_SBS_SMBUS_WRITE, func,
377 ACPI_SBS_WORD_DATA, &data);
379 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
380 "acpi_ec_sbs_access() failed"));
386 static int sbs_zombie(struct acpi_sbs *sbs)
388 return (sbs->zombie);
391 static int sbs_mutex_lock(struct acpi_sbs *sbs)
393 if (sbs_zombie(sbs)) {
396 mutex_lock(&sbs->mutex);
400 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
402 mutex_unlock(&sbs->mutex);
405 /* --------------------------------------------------------------------------
406 Smart Battery System Management
407 -------------------------------------------------------------------------- */
409 static int acpi_check_update_proc(struct acpi_sbs *sbs)
411 acpi_status status = AE_OK;
413 if (update_time == 0) {
414 sbs->update_proc_flg = 0;
417 if (sbs->update_proc_flg == 0) {
418 status = acpi_os_execute(OSL_GPE_HANDLER,
419 acpi_sbs_update_time, sbs);
420 if (status != AE_OK) {
421 ACPI_EXCEPTION((AE_INFO, status,
422 "acpi_os_execute() failed"));
425 sbs->update_proc_flg = 1;
430 static int acpi_sbs_generate_event(struct acpi_device *device,
431 int event, int state, char *bid, char *class)
434 char class_saved[20];
437 strcpy(bid_saved, acpi_device_bid(device));
438 strcpy(class_saved, acpi_device_class(device));
440 strcpy(acpi_device_bid(device), bid);
441 strcpy(acpi_device_class(device), class);
443 result = acpi_bus_generate_event(device, event, state);
445 strcpy(acpi_device_bid(device), bid_saved);
446 strcpy(acpi_device_class(device), class_saved);
451 static int acpi_battery_get_present(struct acpi_battery *battery)
457 result = acpi_sbs_read_word(battery->sbs,
458 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
460 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
461 "acpi_sbs_read_word() failed"));
464 is_present = (state & 0x000f) & (1 << battery->id);
466 battery->battery_present = is_present;
471 static int acpi_battery_select(struct acpi_battery *battery)
473 struct acpi_sbs *sbs = battery->sbs;
478 if (sbs->sbsm_present) {
480 /* Take special care not to knobble other nibbles of
481 * state (aka selector_state), since
482 * it causes charging to halt on SBSELs */
485 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
487 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
488 "acpi_sbs_read_word() failed"));
492 foo = (state & 0x0fff) | (1 << (battery->id + 12));
494 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
496 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
497 "acpi_sbs_write_word() failed"));
506 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
509 s16 battery_system_info;
511 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
512 &battery_system_info);
514 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
515 "acpi_sbs_read_word() failed"));
518 sbs->sbsm_present = 1;
519 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
526 static int acpi_battery_get_info(struct acpi_battery *battery)
528 struct acpi_sbs *sbs = battery->sbs;
531 s16 specification_info;
533 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
536 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
537 "acpi_sbs_read_word() failed"));
540 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
542 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
543 &battery->info.full_charge_capacity);
545 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
546 "acpi_sbs_read_word() failed"));
550 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
551 &battery->info.design_capacity);
554 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
555 "acpi_sbs_read_word() failed"));
559 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
560 &battery->info.design_voltage);
562 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
563 "acpi_sbs_read_word() failed"));
567 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
568 &specification_info);
570 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
571 "acpi_sbs_read_word() failed"));
575 switch ((specification_info & 0x0f00) >> 8) {
577 battery->info.vscale = 10;
580 battery->info.vscale = 100;
583 battery->info.vscale = 1000;
586 battery->info.vscale = 1;
589 switch ((specification_info & 0xf000) >> 12) {
591 battery->info.ipscale = 10;
594 battery->info.ipscale = 100;
597 battery->info.ipscale = 1000;
600 battery->info.ipscale = 1;
603 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
604 &battery->info.serial_number);
606 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
607 "acpi_sbs_read_word() failed"));
611 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
612 battery->info.manufacturer_name);
614 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
615 "acpi_sbs_read_str() failed"));
619 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
620 battery->info.device_name);
622 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
623 "acpi_sbs_read_str() failed"));
627 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
628 battery->info.device_chemistry);
630 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
631 "acpi_sbs_read_str() failed"));
639 static int acpi_battery_get_state(struct acpi_battery *battery)
641 struct acpi_sbs *sbs = battery->sbs;
644 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
645 &battery->state.voltage);
647 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
648 "acpi_sbs_read_word() failed"));
652 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
653 &battery->state.amperage);
655 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
656 "acpi_sbs_read_word() failed"));
660 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
661 &battery->state.remaining_capacity);
663 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
664 "acpi_sbs_read_word() failed"));
668 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
669 &battery->state.battery_state);
671 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
672 "acpi_sbs_read_word() failed"));
680 static int acpi_battery_get_alarm(struct acpi_battery *battery)
682 struct acpi_sbs *sbs = battery->sbs;
685 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
686 &battery->alarm.remaining_capacity);
688 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
689 "acpi_sbs_read_word() failed"));
698 static int acpi_battery_set_alarm(struct acpi_battery *battery,
701 struct acpi_sbs *sbs = battery->sbs;
706 result = acpi_battery_select(battery);
708 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
709 "acpi_battery_select() failed"));
713 /* If necessary, enable the alarm */
717 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
720 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
721 "acpi_sbs_read_word() failed"));
726 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
727 battery_mode & 0xbfff);
729 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
730 "acpi_sbs_write_word() failed"));
735 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
736 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
738 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
739 "acpi_sbs_write_word() failed"));
748 static int acpi_battery_set_mode(struct acpi_battery *battery)
750 struct acpi_sbs *sbs = battery->sbs;
754 if (capacity_mode == DEF_CAPACITY_UNIT) {
758 result = acpi_sbs_read_word(sbs,
759 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
761 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
762 "acpi_sbs_read_word() failed"));
766 if (capacity_mode == MAH_CAPACITY_UNIT) {
767 battery_mode &= 0x7fff;
769 battery_mode |= 0x8000;
771 result = acpi_sbs_write_word(sbs,
772 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
774 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
775 "acpi_sbs_write_word() failed"));
779 result = acpi_sbs_read_word(sbs,
780 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
782 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
783 "acpi_sbs_read_word() failed"));
791 static int acpi_battery_init(struct acpi_battery *battery)
795 result = acpi_battery_select(battery);
797 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
798 "acpi_battery_select() failed"));
802 result = acpi_battery_set_mode(battery);
804 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
805 "acpi_battery_set_mode() failed"));
809 result = acpi_battery_get_info(battery);
811 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
812 "acpi_battery_get_info() failed"));
816 result = acpi_battery_get_state(battery);
818 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
819 "acpi_battery_get_state() failed"));
823 result = acpi_battery_get_alarm(battery);
825 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
826 "acpi_battery_get_alarm() failed"));
834 static int acpi_ac_get_present(struct acpi_sbs *sbs)
839 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
843 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
844 "acpi_sbs_read_word() failed"));
848 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
855 /* --------------------------------------------------------------------------
856 FS Interface (/proc/acpi)
857 -------------------------------------------------------------------------- */
859 /* Generic Routines */
862 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
863 struct proc_dir_entry *parent_dir,
865 struct file_operations *info_fops,
866 struct file_operations *state_fops,
867 struct file_operations *alarm_fops, void *data)
869 struct proc_dir_entry *entry = NULL;
872 *dir = proc_mkdir(dir_name, parent_dir);
874 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
875 "proc_mkdir() failed"));
878 (*dir)->owner = THIS_MODULE;
883 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
885 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
886 "create_proc_entry() failed"));
888 entry->proc_fops = info_fops;
890 entry->owner = THIS_MODULE;
896 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
898 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
899 "create_proc_entry() failed"));
901 entry->proc_fops = state_fops;
903 entry->owner = THIS_MODULE;
909 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
911 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
912 "create_proc_entry() failed"));
914 entry->proc_fops = alarm_fops;
916 entry->owner = THIS_MODULE;
924 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
925 struct proc_dir_entry *parent_dir)
929 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
930 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
931 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
932 remove_proc_entry((*dir)->name, parent_dir);
938 /* Smart Battery Interface */
940 static struct proc_dir_entry *acpi_battery_dir = NULL;
942 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
944 struct acpi_battery *battery = seq->private;
945 struct acpi_sbs *sbs = battery->sbs;
949 if (sbs_mutex_lock(sbs)) {
953 result = acpi_check_update_proc(sbs);
957 if (update_time == 0) {
958 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
960 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
961 "acpi_sbs_update_run() failed"));
965 if (battery->battery_present) {
966 seq_printf(seq, "present: yes\n");
968 seq_printf(seq, "present: no\n");
972 if (battery->info.capacity_mode) {
973 cscale = battery->info.vscale * battery->info.ipscale;
975 cscale = battery->info.ipscale;
977 seq_printf(seq, "design capacity: %i%s\n",
978 battery->info.design_capacity * cscale,
979 battery->info.capacity_mode ? "0 mWh" : " mAh");
981 seq_printf(seq, "last full capacity: %i%s\n",
982 battery->info.full_charge_capacity * cscale,
983 battery->info.capacity_mode ? "0 mWh" : " mAh");
985 seq_printf(seq, "battery technology: rechargeable\n");
987 seq_printf(seq, "design voltage: %i mV\n",
988 battery->info.design_voltage * battery->info.vscale);
990 seq_printf(seq, "design capacity warning: unknown\n");
991 seq_printf(seq, "design capacity low: unknown\n");
992 seq_printf(seq, "capacity granularity 1: unknown\n");
993 seq_printf(seq, "capacity granularity 2: unknown\n");
995 seq_printf(seq, "model number: %s\n",
996 battery->info.device_name);
998 seq_printf(seq, "serial number: %i\n",
999 battery->info.serial_number);
1001 seq_printf(seq, "battery type: %s\n",
1002 battery->info.device_chemistry);
1004 seq_printf(seq, "OEM info: %s\n",
1005 battery->info.manufacturer_name);
1009 sbs_mutex_unlock(sbs);
1014 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1016 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1019 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1021 struct acpi_battery *battery = seq->private;
1022 struct acpi_sbs *sbs = battery->sbs;
1027 if (sbs_mutex_lock(sbs)) {
1031 result = acpi_check_update_proc(sbs);
1035 if (update_time == 0) {
1036 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1038 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1039 "acpi_sbs_update_run() failed"));
1043 if (battery->battery_present) {
1044 seq_printf(seq, "present: yes\n");
1046 seq_printf(seq, "present: no\n");
1050 if (battery->info.capacity_mode) {
1051 cscale = battery->info.vscale * battery->info.ipscale;
1053 cscale = battery->info.ipscale;
1056 if (battery->state.battery_state & 0x0010) {
1057 seq_printf(seq, "capacity state: critical\n");
1059 seq_printf(seq, "capacity state: ok\n");
1062 foo = (s16) battery->state.amperage * battery->info.ipscale;
1063 if (battery->info.capacity_mode) {
1064 foo = foo * battery->info.design_voltage / 1000;
1066 if (battery->state.amperage < 0) {
1067 seq_printf(seq, "charging state: discharging\n");
1068 seq_printf(seq, "present rate: %d %s\n",
1069 -foo, battery->info.capacity_mode ? "mW" : "mA");
1070 } else if (battery->state.amperage > 0) {
1071 seq_printf(seq, "charging state: charging\n");
1072 seq_printf(seq, "present rate: %d %s\n",
1073 foo, battery->info.capacity_mode ? "mW" : "mA");
1075 seq_printf(seq, "charging state: charged\n");
1076 seq_printf(seq, "present rate: 0 %s\n",
1077 battery->info.capacity_mode ? "mW" : "mA");
1080 seq_printf(seq, "remaining capacity: %i%s\n",
1081 battery->state.remaining_capacity * cscale,
1082 battery->info.capacity_mode ? "0 mWh" : " mAh");
1084 seq_printf(seq, "present voltage: %i mV\n",
1085 battery->state.voltage * battery->info.vscale);
1089 sbs_mutex_unlock(sbs);
1094 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1096 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1099 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1101 struct acpi_battery *battery = seq->private;
1102 struct acpi_sbs *sbs = battery->sbs;
1106 if (sbs_mutex_lock(sbs)) {
1110 result = acpi_check_update_proc(sbs);
1114 if (update_time == 0) {
1115 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1117 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1118 "acpi_sbs_update_run() failed"));
1122 if (!battery->battery_present) {
1123 seq_printf(seq, "present: no\n");
1127 if (battery->info.capacity_mode) {
1128 cscale = battery->info.vscale * battery->info.ipscale;
1130 cscale = battery->info.ipscale;
1133 seq_printf(seq, "alarm: ");
1134 if (battery->alarm.remaining_capacity) {
1135 seq_printf(seq, "%i%s\n",
1136 battery->alarm.remaining_capacity * cscale,
1137 battery->info.capacity_mode ? "0 mWh" : " mAh");
1139 seq_printf(seq, "disabled\n");
1144 sbs_mutex_unlock(sbs);
1150 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1151 size_t count, loff_t * ppos)
1153 struct seq_file *seq = file->private_data;
1154 struct acpi_battery *battery = seq->private;
1155 struct acpi_sbs *sbs = battery->sbs;
1156 char alarm_string[12] = { '\0' };
1157 int result, old_alarm, new_alarm;
1159 if (sbs_mutex_lock(sbs)) {
1163 result = acpi_check_update_proc(sbs);
1167 if (!battery->battery_present) {
1172 if (count > sizeof(alarm_string) - 1) {
1177 if (copy_from_user(alarm_string, buffer, count)) {
1182 alarm_string[count] = 0;
1184 old_alarm = battery->alarm.remaining_capacity;
1185 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1187 result = acpi_battery_set_alarm(battery, new_alarm);
1189 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1190 "acpi_battery_set_alarm() failed"));
1191 acpi_battery_set_alarm(battery, old_alarm);
1194 result = acpi_battery_get_alarm(battery);
1196 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1197 "acpi_battery_get_alarm() failed"));
1198 acpi_battery_set_alarm(battery, old_alarm);
1203 sbs_mutex_unlock(sbs);
1212 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1214 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1217 static struct file_operations acpi_battery_info_fops = {
1218 .open = acpi_battery_info_open_fs,
1220 .llseek = seq_lseek,
1221 .release = single_release,
1222 .owner = THIS_MODULE,
1225 static struct file_operations acpi_battery_state_fops = {
1226 .open = acpi_battery_state_open_fs,
1228 .llseek = seq_lseek,
1229 .release = single_release,
1230 .owner = THIS_MODULE,
1233 static struct file_operations acpi_battery_alarm_fops = {
1234 .open = acpi_battery_alarm_open_fs,
1236 .write = acpi_battery_write_alarm,
1237 .llseek = seq_lseek,
1238 .release = single_release,
1239 .owner = THIS_MODULE,
1242 /* Legacy AC Adapter Interface */
1244 static struct proc_dir_entry *acpi_ac_dir = NULL;
1246 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1248 struct acpi_sbs *sbs = seq->private;
1251 if (sbs_mutex_lock(sbs)) {
1255 if (update_time == 0) {
1256 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1258 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1259 "acpi_sbs_update_run() failed"));
1263 seq_printf(seq, "state: %s\n",
1264 sbs->ac.ac_present ? "on-line" : "off-line");
1266 sbs_mutex_unlock(sbs);
1271 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1273 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1276 static struct file_operations acpi_ac_state_fops = {
1277 .open = acpi_ac_state_open_fs,
1279 .llseek = seq_lseek,
1280 .release = single_release,
1281 .owner = THIS_MODULE,
1284 /* --------------------------------------------------------------------------
1286 -------------------------------------------------------------------------- */
1290 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1295 struct acpi_battery *battery;
1297 battery = &sbs->battery[id];
1301 battery->init_state = 0;
1305 result = acpi_battery_select(battery);
1307 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1308 "acpi_battery_select() failed"));
1312 result = acpi_battery_get_present(battery);
1314 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1315 "acpi_battery_get_present() failed"));
1319 is_present = battery->battery_present;
1322 result = acpi_battery_init(battery);
1324 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1325 "acpi_battery_init() failed"));
1328 battery->init_state = 1;
1331 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1333 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1336 &acpi_battery_info_fops,
1337 &acpi_battery_state_fops,
1338 &acpi_battery_alarm_fops, battery);
1340 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1341 "acpi_sbs_generic_add_fs() failed"));
1346 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1347 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1348 sbs->battery->battery_present ? "present" : "absent");
1354 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1357 if (sbs->battery[id].battery_entry) {
1358 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1363 static int acpi_ac_add(struct acpi_sbs *sbs)
1367 result = acpi_ac_get_present(sbs);
1369 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1370 "acpi_ac_get_present() failed"));
1374 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1377 NULL, &acpi_ac_state_fops, NULL, sbs);
1379 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1380 "acpi_sbs_generic_add_fs() failed"));
1384 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1385 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1386 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1393 static void acpi_ac_remove(struct acpi_sbs *sbs)
1396 if (sbs->ac_entry) {
1397 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1401 static void acpi_sbs_update_time_run(unsigned long data)
1403 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1406 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1408 struct acpi_battery *battery;
1409 int result = 0, cnt;
1410 int old_ac_present = -1;
1411 int old_battery_present = -1;
1412 int new_ac_present = -1;
1413 int new_battery_present = -1;
1414 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1416 int do_battery_init = 0, do_ac_init = 0;
1417 int old_remaining_capacity = 0;
1418 int update_ac = 1, update_battery = 1;
1419 int up_tm = update_time;
1421 if (sbs_zombie(sbs)) {
1426 id_min = id_max = id;
1429 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1430 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1431 if (sbs->run_cnt % cnt != 0) {
1438 if (!update_ac && !update_battery) {
1442 old_ac_present = sbs->ac.ac_present;
1444 result = acpi_ac_get_present(sbs);
1446 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1447 "acpi_ac_get_present() failed"));
1450 new_ac_present = sbs->ac.ac_present;
1452 do_ac_init = (old_ac_present != new_ac_present);
1453 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1458 result = acpi_sbs_generate_event(sbs->device,
1459 ACPI_SBS_AC_NOTIFY_STATUS,
1464 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1465 "acpi_sbs_generate_event() failed"));
1469 if (data_type == DATA_TYPE_COMMON) {
1470 if (!do_ac_init && !update_battery) {
1475 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1479 for (id = id_min; id <= id_max; id++) {
1480 battery = &sbs->battery[id];
1481 if (battery->alive == 0) {
1485 old_remaining_capacity = battery->state.remaining_capacity;
1487 old_battery_present = battery->battery_present;
1489 result = acpi_battery_select(battery);
1491 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1492 "acpi_battery_select() failed"));
1495 result = acpi_battery_get_present(battery);
1497 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1498 "acpi_battery_get_present() failed"));
1501 new_battery_present = battery->battery_present;
1503 do_battery_init = ((old_battery_present != new_battery_present)
1504 && new_battery_present);
1505 if (!new_battery_present)
1507 if (do_ac_init || do_battery_init) {
1508 result = acpi_battery_init(battery);
1510 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1511 "acpi_battery_init() "
1515 if (sbs_zombie(sbs)) {
1519 if ((data_type == DATA_TYPE_COMMON
1520 || data_type == DATA_TYPE_INFO)
1521 && new_battery_present) {
1522 result = acpi_battery_get_info(battery);
1524 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1525 "acpi_battery_get_info() failed"));
1528 if (data_type == DATA_TYPE_INFO) {
1531 if (sbs_zombie(sbs)) {
1535 if ((data_type == DATA_TYPE_COMMON
1536 || data_type == DATA_TYPE_STATE)
1537 && new_battery_present) {
1538 result = acpi_battery_get_state(battery);
1540 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1541 "acpi_battery_get_state() failed"));
1544 if (data_type == DATA_TYPE_STATE) {
1547 if (sbs_zombie(sbs)) {
1551 if ((data_type == DATA_TYPE_COMMON
1552 || data_type == DATA_TYPE_ALARM)
1553 && new_battery_present) {
1554 result = acpi_battery_get_alarm(battery);
1556 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1557 "acpi_battery_get_alarm() "
1561 if (data_type == DATA_TYPE_ALARM) {
1564 if (sbs_zombie(sbs)) {
1570 if (old_battery_present != new_battery_present || do_ac_init ||
1571 old_remaining_capacity !=
1572 battery->state.remaining_capacity) {
1573 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1574 result = acpi_sbs_generate_event(sbs->device,
1575 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1576 new_battery_present,
1578 ACPI_BATTERY_CLASS);
1580 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1581 "acpi_sbs_generate_event() "
1592 static void acpi_sbs_update_time(void *data)
1594 struct acpi_sbs *sbs = data;
1595 unsigned long delay = -1;
1597 unsigned int up_tm = update_time;
1599 if (sbs_mutex_lock(sbs))
1602 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1604 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1605 "acpi_sbs_update_run() failed"));
1608 if (sbs_zombie(sbs)) {
1613 if (timer_pending(&sbs->update_timer))
1614 del_timer(&sbs->update_timer);
1616 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1617 delay = jiffies + HZ * delay;
1618 if (timer_pending(&sbs->update_timer)) {
1619 mod_timer(&sbs->update_timer, delay);
1621 sbs->update_timer.data = (unsigned long)data;
1622 sbs->update_timer.function = acpi_sbs_update_time_run;
1623 sbs->update_timer.expires = delay;
1624 add_timer(&sbs->update_timer);
1630 sbs_mutex_unlock(sbs);
1633 static int acpi_sbs_add(struct acpi_device *device)
1635 struct acpi_sbs *sbs = NULL;
1636 int result = 0, remove_result = 0;
1638 acpi_status status = AE_OK;
1642 acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
1643 if (ACPI_FAILURE(status)) {
1644 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1648 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1650 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1655 mutex_init(&sbs->mutex);
1657 sbs_mutex_lock(sbs);
1659 sbs->base = 0xff & (val >> 8);
1660 sbs->device = device;
1662 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1663 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1664 acpi_driver_data(device) = sbs;
1666 result = acpi_ac_add(sbs);
1668 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1672 acpi_sbsm_get_info(sbs);
1674 if (!sbs->sbsm_present) {
1675 result = acpi_battery_add(sbs, 0);
1677 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1678 "acpi_battery_add() failed"));
1682 for (id = 0; id < MAX_SBS_BAT; id++) {
1683 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1684 result = acpi_battery_add(sbs, id);
1686 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1687 "acpi_battery_add() failed"));
1694 init_timer(&sbs->update_timer);
1695 result = acpi_check_update_proc(sbs);
1701 sbs_mutex_unlock(sbs);
1704 remove_result = acpi_sbs_remove(device, 0);
1705 if (remove_result) {
1706 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1707 "acpi_sbs_remove() failed"));
1714 static int acpi_sbs_remove(struct acpi_device *device, int type)
1716 struct acpi_sbs *sbs;
1723 sbs = acpi_driver_data(device);
1728 sbs_mutex_lock(sbs);
1731 del_timer_sync(&sbs->update_timer);
1732 acpi_os_wait_events_complete(NULL);
1733 del_timer_sync(&sbs->update_timer);
1735 for (id = 0; id < MAX_SBS_BAT; id++) {
1736 acpi_battery_remove(sbs, id);
1739 acpi_ac_remove(sbs);
1741 sbs_mutex_unlock(sbs);
1743 mutex_destroy(&sbs->mutex);
1750 static void acpi_sbs_rmdirs(void)
1753 acpi_unlock_ac_dir(acpi_ac_dir);
1756 if (acpi_battery_dir) {
1757 acpi_unlock_battery_dir(acpi_battery_dir);
1758 acpi_battery_dir = NULL;
1762 static int acpi_sbs_resume(struct acpi_device *device)
1764 struct acpi_sbs *sbs;
1769 sbs = device->driver_data;
1776 static int __init acpi_sbs_init(void)
1783 if (capacity_mode != DEF_CAPACITY_UNIT
1784 && capacity_mode != MAH_CAPACITY_UNIT
1785 && capacity_mode != MWH_CAPACITY_UNIT) {
1786 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1787 "invalid capacity_mode = %d", capacity_mode));
1791 acpi_ac_dir = acpi_lock_ac_dir();
1793 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1794 "acpi_lock_ac_dir() failed"));
1798 acpi_battery_dir = acpi_lock_battery_dir();
1799 if (!acpi_battery_dir) {
1800 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1801 "acpi_lock_battery_dir() failed"));
1806 result = acpi_bus_register_driver(&acpi_sbs_driver);
1808 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1809 "acpi_bus_register_driver() failed"));
1817 static void __exit acpi_sbs_exit(void)
1819 acpi_bus_unregister_driver(&acpi_sbs_driver);
1826 module_init(acpi_sbs_init);
1827 module_exit(acpi_sbs_exit);