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_proc_event(device, event, state);
445 strcpy(acpi_device_bid(device), bid_saved);
446 strcpy(acpi_device_class(device), class_saved);
448 acpi_bus_generate_netlink_event(class, bid, event, state);
452 static int acpi_battery_get_present(struct acpi_battery *battery)
458 result = acpi_sbs_read_word(battery->sbs,
459 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
461 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
462 "acpi_sbs_read_word() failed"));
465 is_present = (state & 0x000f) & (1 << battery->id);
467 battery->battery_present = is_present;
472 static int acpi_battery_select(struct acpi_battery *battery)
474 struct acpi_sbs *sbs = battery->sbs;
479 if (sbs->sbsm_present) {
481 /* Take special care not to knobble other nibbles of
482 * state (aka selector_state), since
483 * it causes charging to halt on SBSELs */
486 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
488 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
489 "acpi_sbs_read_word() failed"));
493 foo = (state & 0x0fff) | (1 << (battery->id + 12));
495 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
497 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
498 "acpi_sbs_write_word() failed"));
507 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
510 s16 battery_system_info;
512 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
513 &battery_system_info);
515 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
516 "acpi_sbs_read_word() failed"));
519 sbs->sbsm_present = 1;
520 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
527 static int acpi_battery_get_info(struct acpi_battery *battery)
529 struct acpi_sbs *sbs = battery->sbs;
532 s16 specification_info;
534 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
537 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
538 "acpi_sbs_read_word() failed"));
541 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
543 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
544 &battery->info.full_charge_capacity);
546 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
547 "acpi_sbs_read_word() failed"));
551 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
552 &battery->info.design_capacity);
555 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
556 "acpi_sbs_read_word() failed"));
560 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
561 &battery->info.design_voltage);
563 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
564 "acpi_sbs_read_word() failed"));
568 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
569 &specification_info);
571 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
572 "acpi_sbs_read_word() failed"));
576 switch ((specification_info & 0x0f00) >> 8) {
578 battery->info.vscale = 10;
581 battery->info.vscale = 100;
584 battery->info.vscale = 1000;
587 battery->info.vscale = 1;
590 switch ((specification_info & 0xf000) >> 12) {
592 battery->info.ipscale = 10;
595 battery->info.ipscale = 100;
598 battery->info.ipscale = 1000;
601 battery->info.ipscale = 1;
604 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
605 &battery->info.serial_number);
607 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
608 "acpi_sbs_read_word() failed"));
612 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
613 battery->info.manufacturer_name);
615 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
616 "acpi_sbs_read_str() failed"));
620 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
621 battery->info.device_name);
623 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
624 "acpi_sbs_read_str() failed"));
628 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
629 battery->info.device_chemistry);
631 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
632 "acpi_sbs_read_str() failed"));
640 static int acpi_battery_get_state(struct acpi_battery *battery)
642 struct acpi_sbs *sbs = battery->sbs;
645 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
646 &battery->state.voltage);
648 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
649 "acpi_sbs_read_word() failed"));
653 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
654 &battery->state.amperage);
656 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
657 "acpi_sbs_read_word() failed"));
661 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
662 &battery->state.remaining_capacity);
664 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
665 "acpi_sbs_read_word() failed"));
669 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
670 &battery->state.battery_state);
672 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
673 "acpi_sbs_read_word() failed"));
681 static int acpi_battery_get_alarm(struct acpi_battery *battery)
683 struct acpi_sbs *sbs = battery->sbs;
686 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
687 &battery->alarm.remaining_capacity);
689 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
690 "acpi_sbs_read_word() failed"));
699 static int acpi_battery_set_alarm(struct acpi_battery *battery,
702 struct acpi_sbs *sbs = battery->sbs;
707 result = acpi_battery_select(battery);
709 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
710 "acpi_battery_select() failed"));
714 /* If necessary, enable the alarm */
718 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
721 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
722 "acpi_sbs_read_word() failed"));
727 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
728 battery_mode & 0xbfff);
730 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
731 "acpi_sbs_write_word() failed"));
736 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
737 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
739 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
740 "acpi_sbs_write_word() failed"));
749 static int acpi_battery_set_mode(struct acpi_battery *battery)
751 struct acpi_sbs *sbs = battery->sbs;
755 if (capacity_mode == DEF_CAPACITY_UNIT) {
759 result = acpi_sbs_read_word(sbs,
760 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
762 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
763 "acpi_sbs_read_word() failed"));
767 if (capacity_mode == MAH_CAPACITY_UNIT) {
768 battery_mode &= 0x7fff;
770 battery_mode |= 0x8000;
772 result = acpi_sbs_write_word(sbs,
773 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
775 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
776 "acpi_sbs_write_word() failed"));
780 result = acpi_sbs_read_word(sbs,
781 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
783 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
784 "acpi_sbs_read_word() failed"));
792 static int acpi_battery_init(struct acpi_battery *battery)
796 result = acpi_battery_select(battery);
798 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
799 "acpi_battery_select() failed"));
803 result = acpi_battery_set_mode(battery);
805 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
806 "acpi_battery_set_mode() failed"));
810 result = acpi_battery_get_info(battery);
812 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
813 "acpi_battery_get_info() failed"));
817 result = acpi_battery_get_state(battery);
819 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
820 "acpi_battery_get_state() failed"));
824 result = acpi_battery_get_alarm(battery);
826 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
827 "acpi_battery_get_alarm() failed"));
835 static int acpi_ac_get_present(struct acpi_sbs *sbs)
840 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
844 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
845 "acpi_sbs_read_word() failed"));
849 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
856 /* --------------------------------------------------------------------------
857 FS Interface (/proc/acpi)
858 -------------------------------------------------------------------------- */
860 /* Generic Routines */
863 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
864 struct proc_dir_entry *parent_dir,
866 struct file_operations *info_fops,
867 struct file_operations *state_fops,
868 struct file_operations *alarm_fops, void *data)
870 struct proc_dir_entry *entry = NULL;
873 *dir = proc_mkdir(dir_name, parent_dir);
875 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
876 "proc_mkdir() failed"));
879 (*dir)->owner = THIS_MODULE;
884 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
886 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
887 "create_proc_entry() failed"));
889 entry->proc_fops = info_fops;
891 entry->owner = THIS_MODULE;
897 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
899 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
900 "create_proc_entry() failed"));
902 entry->proc_fops = state_fops;
904 entry->owner = THIS_MODULE;
910 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
912 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
913 "create_proc_entry() failed"));
915 entry->proc_fops = alarm_fops;
917 entry->owner = THIS_MODULE;
925 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
926 struct proc_dir_entry *parent_dir)
930 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
931 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
932 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
933 remove_proc_entry((*dir)->name, parent_dir);
939 /* Smart Battery Interface */
941 static struct proc_dir_entry *acpi_battery_dir = NULL;
943 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
945 struct acpi_battery *battery = seq->private;
946 struct acpi_sbs *sbs = battery->sbs;
950 if (sbs_mutex_lock(sbs)) {
954 result = acpi_check_update_proc(sbs);
958 if (update_time == 0) {
959 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
961 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
962 "acpi_sbs_update_run() failed"));
966 if (battery->battery_present) {
967 seq_printf(seq, "present: yes\n");
969 seq_printf(seq, "present: no\n");
973 if (battery->info.capacity_mode) {
974 cscale = battery->info.vscale * battery->info.ipscale;
976 cscale = battery->info.ipscale;
978 seq_printf(seq, "design capacity: %i%s\n",
979 battery->info.design_capacity * cscale,
980 battery->info.capacity_mode ? "0 mWh" : " mAh");
982 seq_printf(seq, "last full capacity: %i%s\n",
983 battery->info.full_charge_capacity * cscale,
984 battery->info.capacity_mode ? "0 mWh" : " mAh");
986 seq_printf(seq, "battery technology: rechargeable\n");
988 seq_printf(seq, "design voltage: %i mV\n",
989 battery->info.design_voltage * battery->info.vscale);
991 seq_printf(seq, "design capacity warning: unknown\n");
992 seq_printf(seq, "design capacity low: unknown\n");
993 seq_printf(seq, "capacity granularity 1: unknown\n");
994 seq_printf(seq, "capacity granularity 2: unknown\n");
996 seq_printf(seq, "model number: %s\n",
997 battery->info.device_name);
999 seq_printf(seq, "serial number: %i\n",
1000 battery->info.serial_number);
1002 seq_printf(seq, "battery type: %s\n",
1003 battery->info.device_chemistry);
1005 seq_printf(seq, "OEM info: %s\n",
1006 battery->info.manufacturer_name);
1010 sbs_mutex_unlock(sbs);
1015 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1017 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1020 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1022 struct acpi_battery *battery = seq->private;
1023 struct acpi_sbs *sbs = battery->sbs;
1028 if (sbs_mutex_lock(sbs)) {
1032 result = acpi_check_update_proc(sbs);
1036 if (update_time == 0) {
1037 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1039 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1040 "acpi_sbs_update_run() failed"));
1044 if (battery->battery_present) {
1045 seq_printf(seq, "present: yes\n");
1047 seq_printf(seq, "present: no\n");
1051 if (battery->info.capacity_mode) {
1052 cscale = battery->info.vscale * battery->info.ipscale;
1054 cscale = battery->info.ipscale;
1057 if (battery->state.battery_state & 0x0010) {
1058 seq_printf(seq, "capacity state: critical\n");
1060 seq_printf(seq, "capacity state: ok\n");
1063 foo = (s16) battery->state.amperage * battery->info.ipscale;
1064 if (battery->info.capacity_mode) {
1065 foo = foo * battery->info.design_voltage / 1000;
1067 if (battery->state.amperage < 0) {
1068 seq_printf(seq, "charging state: discharging\n");
1069 seq_printf(seq, "present rate: %d %s\n",
1070 -foo, battery->info.capacity_mode ? "mW" : "mA");
1071 } else if (battery->state.amperage > 0) {
1072 seq_printf(seq, "charging state: charging\n");
1073 seq_printf(seq, "present rate: %d %s\n",
1074 foo, battery->info.capacity_mode ? "mW" : "mA");
1076 seq_printf(seq, "charging state: charged\n");
1077 seq_printf(seq, "present rate: 0 %s\n",
1078 battery->info.capacity_mode ? "mW" : "mA");
1081 seq_printf(seq, "remaining capacity: %i%s\n",
1082 battery->state.remaining_capacity * cscale,
1083 battery->info.capacity_mode ? "0 mWh" : " mAh");
1085 seq_printf(seq, "present voltage: %i mV\n",
1086 battery->state.voltage * battery->info.vscale);
1090 sbs_mutex_unlock(sbs);
1095 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1097 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1100 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1102 struct acpi_battery *battery = seq->private;
1103 struct acpi_sbs *sbs = battery->sbs;
1107 if (sbs_mutex_lock(sbs)) {
1111 result = acpi_check_update_proc(sbs);
1115 if (update_time == 0) {
1116 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1118 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1119 "acpi_sbs_update_run() failed"));
1123 if (!battery->battery_present) {
1124 seq_printf(seq, "present: no\n");
1128 if (battery->info.capacity_mode) {
1129 cscale = battery->info.vscale * battery->info.ipscale;
1131 cscale = battery->info.ipscale;
1134 seq_printf(seq, "alarm: ");
1135 if (battery->alarm.remaining_capacity) {
1136 seq_printf(seq, "%i%s\n",
1137 battery->alarm.remaining_capacity * cscale,
1138 battery->info.capacity_mode ? "0 mWh" : " mAh");
1140 seq_printf(seq, "disabled\n");
1145 sbs_mutex_unlock(sbs);
1151 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1152 size_t count, loff_t * ppos)
1154 struct seq_file *seq = file->private_data;
1155 struct acpi_battery *battery = seq->private;
1156 struct acpi_sbs *sbs = battery->sbs;
1157 char alarm_string[12] = { '\0' };
1158 int result, old_alarm, new_alarm;
1160 if (sbs_mutex_lock(sbs)) {
1164 result = acpi_check_update_proc(sbs);
1168 if (!battery->battery_present) {
1173 if (count > sizeof(alarm_string) - 1) {
1178 if (copy_from_user(alarm_string, buffer, count)) {
1183 alarm_string[count] = 0;
1185 old_alarm = battery->alarm.remaining_capacity;
1186 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1188 result = acpi_battery_set_alarm(battery, new_alarm);
1190 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1191 "acpi_battery_set_alarm() failed"));
1192 acpi_battery_set_alarm(battery, old_alarm);
1195 result = acpi_battery_get_alarm(battery);
1197 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1198 "acpi_battery_get_alarm() failed"));
1199 acpi_battery_set_alarm(battery, old_alarm);
1204 sbs_mutex_unlock(sbs);
1213 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1215 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1218 static struct file_operations acpi_battery_info_fops = {
1219 .open = acpi_battery_info_open_fs,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223 .owner = THIS_MODULE,
1226 static struct file_operations acpi_battery_state_fops = {
1227 .open = acpi_battery_state_open_fs,
1229 .llseek = seq_lseek,
1230 .release = single_release,
1231 .owner = THIS_MODULE,
1234 static struct file_operations acpi_battery_alarm_fops = {
1235 .open = acpi_battery_alarm_open_fs,
1237 .write = acpi_battery_write_alarm,
1238 .llseek = seq_lseek,
1239 .release = single_release,
1240 .owner = THIS_MODULE,
1243 /* Legacy AC Adapter Interface */
1245 static struct proc_dir_entry *acpi_ac_dir = NULL;
1247 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1249 struct acpi_sbs *sbs = seq->private;
1252 if (sbs_mutex_lock(sbs)) {
1256 if (update_time == 0) {
1257 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1259 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1260 "acpi_sbs_update_run() failed"));
1264 seq_printf(seq, "state: %s\n",
1265 sbs->ac.ac_present ? "on-line" : "off-line");
1267 sbs_mutex_unlock(sbs);
1272 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1274 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1277 static struct file_operations acpi_ac_state_fops = {
1278 .open = acpi_ac_state_open_fs,
1280 .llseek = seq_lseek,
1281 .release = single_release,
1282 .owner = THIS_MODULE,
1285 /* --------------------------------------------------------------------------
1287 -------------------------------------------------------------------------- */
1291 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1296 struct acpi_battery *battery;
1298 battery = &sbs->battery[id];
1302 battery->init_state = 0;
1306 result = acpi_battery_select(battery);
1308 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1309 "acpi_battery_select() failed"));
1313 result = acpi_battery_get_present(battery);
1315 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1316 "acpi_battery_get_present() failed"));
1320 is_present = battery->battery_present;
1323 result = acpi_battery_init(battery);
1325 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1326 "acpi_battery_init() failed"));
1329 battery->init_state = 1;
1332 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1334 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1337 &acpi_battery_info_fops,
1338 &acpi_battery_state_fops,
1339 &acpi_battery_alarm_fops, battery);
1341 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1342 "acpi_sbs_generic_add_fs() failed"));
1347 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1348 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1349 sbs->battery->battery_present ? "present" : "absent");
1355 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1358 if (sbs->battery[id].battery_entry) {
1359 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1364 static int acpi_ac_add(struct acpi_sbs *sbs)
1368 result = acpi_ac_get_present(sbs);
1370 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1371 "acpi_ac_get_present() failed"));
1375 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1378 NULL, &acpi_ac_state_fops, NULL, sbs);
1380 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1381 "acpi_sbs_generic_add_fs() failed"));
1385 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1386 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1387 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1394 static void acpi_ac_remove(struct acpi_sbs *sbs)
1397 if (sbs->ac_entry) {
1398 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1402 static void acpi_sbs_update_time_run(unsigned long data)
1404 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1407 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1409 struct acpi_battery *battery;
1410 int result = 0, cnt;
1411 int old_ac_present = -1;
1412 int old_battery_present = -1;
1413 int new_ac_present = -1;
1414 int new_battery_present = -1;
1415 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1417 int do_battery_init = 0, do_ac_init = 0;
1418 int old_remaining_capacity = 0;
1419 int update_battery = 1;
1420 int up_tm = update_time;
1422 if (sbs_zombie(sbs)) {
1427 id_min = id_max = id;
1430 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1431 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1432 if (sbs->run_cnt % cnt != 0) {
1439 old_ac_present = sbs->ac.ac_present;
1441 result = acpi_ac_get_present(sbs);
1443 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1444 "acpi_ac_get_present() failed"));
1447 new_ac_present = sbs->ac.ac_present;
1449 do_ac_init = (old_ac_present != new_ac_present);
1450 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1455 result = acpi_sbs_generate_event(sbs->device,
1456 ACPI_SBS_AC_NOTIFY_STATUS,
1461 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1462 "acpi_sbs_generate_event() failed"));
1466 if (data_type == DATA_TYPE_COMMON) {
1467 if (!do_ac_init && !update_battery) {
1472 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1476 for (id = id_min; id <= id_max; id++) {
1477 battery = &sbs->battery[id];
1478 if (battery->alive == 0) {
1482 old_remaining_capacity = battery->state.remaining_capacity;
1484 old_battery_present = battery->battery_present;
1486 result = acpi_battery_select(battery);
1488 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1489 "acpi_battery_select() failed"));
1492 result = acpi_battery_get_present(battery);
1494 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1495 "acpi_battery_get_present() failed"));
1498 new_battery_present = battery->battery_present;
1500 do_battery_init = ((old_battery_present != new_battery_present)
1501 && new_battery_present);
1502 if (!new_battery_present)
1504 if (do_ac_init || do_battery_init) {
1505 result = acpi_battery_init(battery);
1507 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1508 "acpi_battery_init() "
1512 if (sbs_zombie(sbs)) {
1516 if ((data_type == DATA_TYPE_COMMON
1517 || data_type == DATA_TYPE_INFO)
1518 && new_battery_present) {
1519 result = acpi_battery_get_info(battery);
1521 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1522 "acpi_battery_get_info() failed"));
1525 if (data_type == DATA_TYPE_INFO) {
1528 if (sbs_zombie(sbs)) {
1532 if ((data_type == DATA_TYPE_COMMON
1533 || data_type == DATA_TYPE_STATE)
1534 && new_battery_present) {
1535 result = acpi_battery_get_state(battery);
1537 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1538 "acpi_battery_get_state() failed"));
1541 if (data_type == DATA_TYPE_STATE) {
1544 if (sbs_zombie(sbs)) {
1548 if ((data_type == DATA_TYPE_COMMON
1549 || data_type == DATA_TYPE_ALARM)
1550 && new_battery_present) {
1551 result = acpi_battery_get_alarm(battery);
1553 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1554 "acpi_battery_get_alarm() "
1558 if (data_type == DATA_TYPE_ALARM) {
1561 if (sbs_zombie(sbs)) {
1567 if (old_battery_present != new_battery_present || do_ac_init ||
1568 old_remaining_capacity !=
1569 battery->state.remaining_capacity) {
1570 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1571 result = acpi_sbs_generate_event(sbs->device,
1572 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1573 new_battery_present,
1575 ACPI_BATTERY_CLASS);
1577 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1578 "acpi_sbs_generate_event() "
1589 static void acpi_sbs_update_time(void *data)
1591 struct acpi_sbs *sbs = data;
1592 unsigned long delay = -1;
1594 unsigned int up_tm = update_time;
1596 if (sbs_mutex_lock(sbs))
1599 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1601 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1602 "acpi_sbs_update_run() failed"));
1605 if (sbs_zombie(sbs)) {
1610 if (timer_pending(&sbs->update_timer))
1611 del_timer(&sbs->update_timer);
1613 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1614 delay = jiffies + HZ * delay;
1615 if (timer_pending(&sbs->update_timer)) {
1616 mod_timer(&sbs->update_timer, delay);
1618 sbs->update_timer.data = (unsigned long)data;
1619 sbs->update_timer.function = acpi_sbs_update_time_run;
1620 sbs->update_timer.expires = delay;
1621 add_timer(&sbs->update_timer);
1627 sbs_mutex_unlock(sbs);
1630 static int acpi_sbs_add(struct acpi_device *device)
1632 struct acpi_sbs *sbs = NULL;
1633 int result = 0, remove_result = 0;
1635 acpi_status status = AE_OK;
1639 acpi_evaluate_integer(device->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 = 0xff & (val >> 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"));
1669 acpi_sbsm_get_info(sbs);
1671 if (!sbs->sbsm_present) {
1672 result = acpi_battery_add(sbs, 0);
1674 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1675 "acpi_battery_add() failed"));
1679 for (id = 0; id < MAX_SBS_BAT; id++) {
1680 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1681 result = acpi_battery_add(sbs, id);
1683 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1684 "acpi_battery_add() failed"));
1691 init_timer(&sbs->update_timer);
1692 result = acpi_check_update_proc(sbs);
1698 sbs_mutex_unlock(sbs);
1701 remove_result = acpi_sbs_remove(device, 0);
1702 if (remove_result) {
1703 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1704 "acpi_sbs_remove() failed"));
1711 static int acpi_sbs_remove(struct acpi_device *device, int type)
1713 struct acpi_sbs *sbs;
1720 sbs = acpi_driver_data(device);
1725 sbs_mutex_lock(sbs);
1728 del_timer_sync(&sbs->update_timer);
1729 acpi_os_wait_events_complete(NULL);
1730 del_timer_sync(&sbs->update_timer);
1732 for (id = 0; id < MAX_SBS_BAT; id++) {
1733 acpi_battery_remove(sbs, id);
1736 acpi_ac_remove(sbs);
1738 sbs_mutex_unlock(sbs);
1740 mutex_destroy(&sbs->mutex);
1747 static void acpi_sbs_rmdirs(void)
1750 acpi_unlock_ac_dir(acpi_ac_dir);
1753 if (acpi_battery_dir) {
1754 acpi_unlock_battery_dir(acpi_battery_dir);
1755 acpi_battery_dir = NULL;
1759 static int acpi_sbs_resume(struct acpi_device *device)
1761 struct acpi_sbs *sbs;
1766 sbs = device->driver_data;
1773 static int __init acpi_sbs_init(void)
1780 if (capacity_mode != DEF_CAPACITY_UNIT
1781 && capacity_mode != MAH_CAPACITY_UNIT
1782 && capacity_mode != MWH_CAPACITY_UNIT) {
1783 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1784 "invalid capacity_mode = %d", capacity_mode));
1788 acpi_ac_dir = acpi_lock_ac_dir();
1790 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1791 "acpi_lock_ac_dir() failed"));
1795 acpi_battery_dir = acpi_lock_battery_dir();
1796 if (!acpi_battery_dir) {
1797 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1798 "acpi_lock_battery_dir() failed"));
1803 result = acpi_bus_register_driver(&acpi_sbs_driver);
1805 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1806 "acpi_bus_register_driver() failed"));
1814 static void __exit acpi_sbs_exit(void)
1816 acpi_bus_unregister_driver(&acpi_sbs_driver);
1823 module_init(acpi_sbs_init);
1824 module_exit(acpi_sbs_exit);