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/i2c.h>
34 #include <linux/delay.h>
38 #define DEF_CAPACITY_UNIT 3
39 #define MAH_CAPACITY_UNIT 1
40 #define MWH_CAPACITY_UNIT 2
41 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
43 #define REQUEST_UPDATE_MODE 1
44 #define QUEUE_UPDATE_MODE 2
46 #define DATA_TYPE_COMMON 0
47 #define DATA_TYPE_INFO 1
48 #define DATA_TYPE_STATE 2
49 #define DATA_TYPE_ALARM 3
50 #define DATA_TYPE_AC_STATE 4
52 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
54 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
57 #define ACPI_SBS_COMPONENT 0x00080000
58 #define ACPI_SBS_CLASS "sbs"
59 #define ACPI_AC_CLASS "ac_adapter"
60 #define ACPI_BATTERY_CLASS "battery"
61 #define ACPI_SBS_HID "ACPI0002"
62 #define ACPI_SBS_DRIVER_NAME "ACPI Smart Battery System Driver"
63 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
64 #define ACPI_SBS_FILE_INFO "info"
65 #define ACPI_SBS_FILE_STATE "state"
66 #define ACPI_SBS_FILE_ALARM "alarm"
67 #define ACPI_BATTERY_DIR_NAME "BAT%i"
68 #define ACPI_AC_DIR_NAME "AC0"
69 #define ACPI_SBC_SMBUS_ADDR 0x9
70 #define ACPI_SBSM_SMBUS_ADDR 0xa
71 #define ACPI_SB_SMBUS_ADDR 0xb
72 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
73 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
74 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
76 #define _COMPONENT ACPI_SBS_COMPONENT
79 #define MAX_SMBUS_ERR 1
81 ACPI_MODULE_NAME("acpi_sbs");
83 MODULE_AUTHOR("Rich Townsend");
84 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
85 MODULE_LICENSE("GPL");
87 static struct semaphore sbs_sem;
89 #define UPDATE_MODE QUEUE_UPDATE_MODE
90 /* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */
91 #define UPDATE_INFO_MODE 0
92 #define UPDATE_TIME 60
93 #define UPDATE_TIME2 0
95 static int capacity_mode = CAPACITY_UNIT;
96 static int update_mode = UPDATE_MODE;
97 static int update_info_mode = UPDATE_INFO_MODE;
98 static int update_time = UPDATE_TIME;
99 static int update_time2 = UPDATE_TIME2;
101 module_param(capacity_mode, int, CAPACITY_UNIT);
102 module_param(update_mode, int, UPDATE_MODE);
103 module_param(update_info_mode, int, UPDATE_INFO_MODE);
104 module_param(update_time, int, UPDATE_TIME);
105 module_param(update_time2, int, UPDATE_TIME2);
107 static int acpi_sbs_add(struct acpi_device *device);
108 static int acpi_sbs_remove(struct acpi_device *device, int type);
109 static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus);
110 static void acpi_sbs_update_queue(void *data);
112 static struct acpi_driver acpi_sbs_driver = {
113 .name = ACPI_SBS_DRIVER_NAME,
114 .class = ACPI_SBS_CLASS,
118 .remove = acpi_sbs_remove,
122 struct acpi_battery_info {
124 s16 full_charge_capacity;
130 char manufacturer_name[I2C_SMBUS_BLOCK_MAX + 3];
131 char device_name[I2C_SMBUS_BLOCK_MAX + 3];
132 char device_chemistry[I2C_SMBUS_BLOCK_MAX + 3];
135 struct acpi_battery_state {
138 s16 remaining_capacity;
139 s16 average_time_to_empty;
140 s16 average_time_to_full;
144 struct acpi_battery_alarm {
145 s16 remaining_capacity;
148 struct acpi_battery {
153 struct acpi_sbs *sbs;
154 struct acpi_battery_info info;
155 struct acpi_battery_state state;
156 struct acpi_battery_alarm alarm;
157 struct proc_dir_entry *battery_entry;
162 struct acpi_device *device;
163 struct acpi_ec_smbus *smbus;
165 int sbsm_batteries_supported;
167 struct proc_dir_entry *ac_entry;
168 struct acpi_battery battery[MAX_SBS_BAT];
169 int update_info_mode;
173 struct timer_list update_timer;
176 static void acpi_update_delay(struct acpi_sbs *sbs);
177 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type);
179 /* --------------------------------------------------------------------------
181 -------------------------------------------------------------------------- */
183 static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus)
185 union i2c_smbus_data data;
192 result = smbus->adapter.algo->
193 smbus_xfer(&smbus->adapter,
195 0, I2C_SMBUS_READ, 0x16, I2C_SMBUS_BLOCK_DATA, &data);
197 err_number = (data.word & 0x000f);
199 switch (data.word & 0x000f) {
201 err_str = "unexpected bus error";
207 err_str = "reserved command";
210 err_str = "unsupported command";
213 err_str = "access denied";
216 err_str = "overflow/underflow";
219 err_str = "bad size";
222 err_str = "unknown error";
225 err_str = "unrecognized error";
227 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
228 "%s: ret %i, err %i\n", err_str, result, err_number));
232 acpi_sbs_smbus_read_word(struct acpi_ec_smbus *smbus, int addr, int func,
234 void (*err_handler) (struct acpi_ec_smbus * smbus))
236 union i2c_smbus_data data;
240 if (err_handler == NULL) {
241 err_handler = acpi_battery_smbus_err_handler;
244 for (i = 0; i < MAX_SMBUS_ERR; i++) {
246 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
247 I2C_SMBUS_READ, func,
248 I2C_SMBUS_WORD_DATA, &data);
250 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
251 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
266 acpi_sbs_smbus_read_str(struct acpi_ec_smbus *smbus, int addr, int func,
268 void (*err_handler) (struct acpi_ec_smbus * smbus))
270 union i2c_smbus_data data;
274 if (err_handler == NULL) {
275 err_handler = acpi_battery_smbus_err_handler;
278 for (i = 0; i < MAX_SMBUS_ERR; i++) {
280 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
281 I2C_SMBUS_READ, func,
282 I2C_SMBUS_BLOCK_DATA,
285 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
286 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
292 strncpy(str, (const char *)data.block + 1,
294 str[data.block[0]] = 0;
303 acpi_sbs_smbus_write_word(struct acpi_ec_smbus *smbus, int addr, int func,
305 void (*err_handler) (struct acpi_ec_smbus * smbus))
307 union i2c_smbus_data data;
311 if (err_handler == NULL) {
312 err_handler = acpi_battery_smbus_err_handler;
317 for (i = 0; i < MAX_SMBUS_ERR; i++) {
319 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
320 I2C_SMBUS_WRITE, func,
321 I2C_SMBUS_WORD_DATA, &data);
323 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
324 "try %i: smbus->adapter.algo"
325 "->smbus_xfer() failed\n", i));
337 /* --------------------------------------------------------------------------
338 Smart Battery System Management
339 -------------------------------------------------------------------------- */
343 static int acpi_sbs_generate_event(struct acpi_device *device,
344 int event, int state, char *bid, char *class)
347 char class_saved[20];
350 strcpy(bid_saved, acpi_device_bid(device));
351 strcpy(class_saved, acpi_device_class(device));
353 strcpy(acpi_device_bid(device), bid);
354 strcpy(acpi_device_class(device), class);
356 result = acpi_bus_generate_event(device, event, state);
358 strcpy(acpi_device_bid(device), bid_saved);
359 strcpy(acpi_device_class(device), class_saved);
364 static int acpi_battery_get_present(struct acpi_battery *battery)
370 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
371 ACPI_SBSM_SMBUS_ADDR, 0x01,
374 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
375 "acpi_sbs_smbus_read_word() failed"));
378 is_present = (state & 0x000f) & (1 << battery->id);
380 battery->battery_present = is_present;
385 static int acpi_battery_is_present(struct acpi_battery *battery)
387 return (battery->battery_present);
390 static int acpi_ac_is_present(struct acpi_sbs *sbs)
392 return (sbs->ac_present);
395 static int acpi_battery_select(struct acpi_battery *battery)
397 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
402 if (battery->sbs->sbsm_present) {
404 /* Take special care not to knobble other nibbles of
405 * state (aka selector_state), since
406 * it causes charging to halt on SBSELs */
409 acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
412 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
413 "acpi_sbs_smbus_read_word() failed\n"));
417 foo = (state & 0x0fff) | (1 << (battery->id + 12));
419 acpi_sbs_smbus_write_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
422 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
423 "acpi_sbs_smbus_write_word() failed\n"));
432 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
434 struct acpi_ec_smbus *smbus = sbs->smbus;
436 s16 battery_system_info;
438 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x04,
439 &battery_system_info, NULL);
441 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
442 "acpi_sbs_smbus_read_word() failed\n"));
446 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
453 static int acpi_battery_get_info(struct acpi_battery *battery)
455 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
458 s16 specification_info;
460 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
462 &acpi_battery_smbus_err_handler);
464 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
465 "acpi_sbs_smbus_read_word() failed\n"));
468 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
470 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x10,
471 &battery->info.full_charge_capacity,
472 &acpi_battery_smbus_err_handler);
474 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
475 "acpi_sbs_smbus_read_word() failed\n"));
479 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x18,
480 &battery->info.design_capacity,
481 &acpi_battery_smbus_err_handler);
487 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x19,
488 &battery->info.design_voltage,
489 &acpi_battery_smbus_err_handler);
491 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
492 "acpi_sbs_smbus_read_word() failed\n"));
496 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1a,
498 &acpi_battery_smbus_err_handler);
500 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
501 "acpi_sbs_smbus_read_word() failed\n"));
505 switch ((specification_info & 0x0f00) >> 8) {
507 battery->info.vscale = 10;
510 battery->info.vscale = 100;
513 battery->info.vscale = 1000;
516 battery->info.vscale = 1;
519 switch ((specification_info & 0xf000) >> 12) {
521 battery->info.ipscale = 10;
524 battery->info.ipscale = 100;
527 battery->info.ipscale = 1000;
530 battery->info.ipscale = 1;
533 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1c,
534 &battery->info.serial_number,
535 &acpi_battery_smbus_err_handler);
540 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x20,
541 battery->info.manufacturer_name,
542 &acpi_battery_smbus_err_handler);
544 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
545 "acpi_sbs_smbus_read_str() failed\n"));
549 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x21,
550 battery->info.device_name,
551 &acpi_battery_smbus_err_handler);
553 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
554 "acpi_sbs_smbus_read_str() failed\n"));
558 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x22,
559 battery->info.device_chemistry,
560 &acpi_battery_smbus_err_handler);
562 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
563 "acpi_sbs_smbus_read_str() failed\n"));
571 static void acpi_update_delay(struct acpi_sbs *sbs)
576 if (sbs->update_time2 > 0) {
577 msleep(sbs->update_time2 * 1000);
581 static int acpi_battery_get_state(struct acpi_battery *battery)
583 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
586 acpi_update_delay(battery->sbs);
587 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x09,
588 &battery->state.voltage,
589 &acpi_battery_smbus_err_handler);
591 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
592 "acpi_sbs_smbus_read_word() failed\n"));
596 acpi_update_delay(battery->sbs);
597 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0a,
598 &battery->state.amperage,
599 &acpi_battery_smbus_err_handler);
601 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
602 "acpi_sbs_smbus_read_word() failed\n"));
606 acpi_update_delay(battery->sbs);
607 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0f,
608 &battery->state.remaining_capacity,
609 &acpi_battery_smbus_err_handler);
611 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
612 "acpi_sbs_smbus_read_word() failed\n"));
616 acpi_update_delay(battery->sbs);
617 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x12,
618 &battery->state.average_time_to_empty,
619 &acpi_battery_smbus_err_handler);
621 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
622 "acpi_sbs_smbus_read_word() failed\n"));
626 acpi_update_delay(battery->sbs);
627 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x13,
628 &battery->state.average_time_to_full,
629 &acpi_battery_smbus_err_handler);
631 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
632 "acpi_sbs_smbus_read_word() failed\n"));
636 acpi_update_delay(battery->sbs);
637 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x16,
638 &battery->state.battery_status,
639 &acpi_battery_smbus_err_handler);
641 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
642 "acpi_sbs_smbus_read_word() failed\n"));
646 acpi_update_delay(battery->sbs);
652 static int acpi_battery_get_alarm(struct acpi_battery *battery)
654 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
657 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
658 &battery->alarm.remaining_capacity,
659 &acpi_battery_smbus_err_handler);
661 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
662 "acpi_sbs_smbus_read_word() failed\n"));
666 acpi_update_delay(battery->sbs);
673 static int acpi_battery_set_alarm(struct acpi_battery *battery,
676 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
681 result = acpi_battery_select(battery);
683 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
684 "acpi_battery_select() failed\n"));
688 /* If necessary, enable the alarm */
692 acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
694 &acpi_battery_smbus_err_handler);
696 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
697 "acpi_sbs_smbus_read_word() failed\n"));
702 acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
703 battery_mode & 0xbfff,
704 &acpi_battery_smbus_err_handler);
706 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
707 "acpi_sbs_smbus_write_word() failed\n"));
712 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
713 result = acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
715 &acpi_battery_smbus_err_handler);
717 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
718 "acpi_sbs_smbus_write_word() failed\n"));
727 static int acpi_battery_set_mode(struct acpi_battery *battery)
732 if (capacity_mode == DEF_CAPACITY_UNIT) {
736 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
737 ACPI_SB_SMBUS_ADDR, 0x03,
738 &battery_mode, NULL);
740 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
741 "acpi_sbs_smbus_read_word() failed\n"));
745 if (capacity_mode == MAH_CAPACITY_UNIT) {
746 battery_mode &= 0x7fff;
748 battery_mode |= 0x8000;
750 result = acpi_sbs_smbus_write_word(battery->sbs->smbus,
751 ACPI_SB_SMBUS_ADDR, 0x03,
754 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
755 "acpi_sbs_smbus_write_word() failed\n"));
759 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
760 ACPI_SB_SMBUS_ADDR, 0x03,
761 &battery_mode, NULL);
763 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
764 "acpi_sbs_smbus_read_word() failed\n"));
772 static int acpi_battery_init(struct acpi_battery *battery)
776 result = acpi_battery_select(battery);
778 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
779 "acpi_battery_init() failed\n"));
783 result = acpi_battery_set_mode(battery);
785 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
786 "acpi_battery_set_mode() failed\n"));
790 result = acpi_battery_get_info(battery);
792 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
793 "acpi_battery_get_info() failed\n"));
797 result = acpi_battery_get_state(battery);
799 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
800 "acpi_battery_get_state() failed\n"));
804 result = acpi_battery_get_alarm(battery);
806 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
807 "acpi_battery_get_alarm() failed\n"));
815 static int acpi_ac_get_present(struct acpi_sbs *sbs)
817 struct acpi_ec_smbus *smbus = sbs->smbus;
821 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBC_SMBUS_ADDR, 0x13,
822 &charger_status, NULL);
825 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
826 "acpi_sbs_smbus_read_word() failed\n"));
830 sbs->ac_present = (charger_status & 0x8000) >> 15;
837 /* --------------------------------------------------------------------------
838 FS Interface (/proc/acpi)
839 -------------------------------------------------------------------------- */
841 /* Generic Routines */
844 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
845 struct proc_dir_entry *parent_dir,
847 struct file_operations *info_fops,
848 struct file_operations *state_fops,
849 struct file_operations *alarm_fops, void *data)
851 struct proc_dir_entry *entry = NULL;
854 *dir = proc_mkdir(dir_name, parent_dir);
856 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
857 "proc_mkdir() failed\n"));
860 (*dir)->owner = THIS_MODULE;
865 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
867 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
868 "create_proc_entry() failed\n"));
870 entry->proc_fops = info_fops;
872 entry->owner = THIS_MODULE;
878 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
880 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
881 "create_proc_entry() failed\n"));
883 entry->proc_fops = state_fops;
885 entry->owner = THIS_MODULE;
891 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
893 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
894 "create_proc_entry() failed\n"));
896 entry->proc_fops = alarm_fops;
898 entry->owner = THIS_MODULE;
906 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
907 struct proc_dir_entry *parent_dir)
911 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
912 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
913 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
914 remove_proc_entry((*dir)->name, parent_dir);
920 /* Smart Battery Interface */
922 static struct proc_dir_entry *acpi_battery_dir = NULL;
924 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
926 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
930 if (battery->sbs->zombie) {
936 if (update_mode == REQUEST_UPDATE_MODE) {
937 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO);
939 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
940 "acpi_sbs_update_run() failed\n"));
944 if (acpi_battery_is_present(battery)) {
945 seq_printf(seq, "present: yes\n");
947 seq_printf(seq, "present: no\n");
951 if (battery->info.capacity_mode) {
952 cscale = battery->info.vscale * battery->info.ipscale;
954 cscale = battery->info.ipscale;
956 seq_printf(seq, "design capacity: %i%s",
957 battery->info.design_capacity * cscale,
958 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
960 seq_printf(seq, "last full capacity: %i%s",
961 battery->info.full_charge_capacity * cscale,
962 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
964 seq_printf(seq, "battery technology: rechargeable\n");
966 seq_printf(seq, "design voltage: %i mV\n",
967 battery->info.design_voltage * battery->info.vscale);
969 seq_printf(seq, "design capacity warning: unknown\n");
970 seq_printf(seq, "design capacity low: unknown\n");
971 seq_printf(seq, "capacity granularity 1: unknown\n");
972 seq_printf(seq, "capacity granularity 2: unknown\n");
974 seq_printf(seq, "model number: %s\n",
975 battery->info.device_name);
977 seq_printf(seq, "serial number: %i\n",
978 battery->info.serial_number);
980 seq_printf(seq, "battery type: %s\n",
981 battery->info.device_chemistry);
983 seq_printf(seq, "OEM info: %s\n",
984 battery->info.manufacturer_name);
993 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
995 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
998 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1000 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1005 if (battery->sbs->zombie) {
1011 if (update_mode == REQUEST_UPDATE_MODE) {
1012 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE);
1014 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1015 "acpi_sbs_update_run() failed\n"));
1019 if (acpi_battery_is_present(battery)) {
1020 seq_printf(seq, "present: yes\n");
1022 seq_printf(seq, "present: no\n");
1026 if (battery->info.capacity_mode) {
1027 cscale = battery->info.vscale * battery->info.ipscale;
1029 cscale = battery->info.ipscale;
1032 if (battery->state.battery_status & 0x0010) {
1033 seq_printf(seq, "capacity state: critical\n");
1035 seq_printf(seq, "capacity state: ok\n");
1037 if (battery->state.amperage < 0) {
1038 seq_printf(seq, "charging state: discharging\n");
1039 foo = battery->state.remaining_capacity * cscale * 60 /
1040 (battery->state.average_time_to_empty == 0 ? 1 :
1041 battery->state.average_time_to_empty);
1042 seq_printf(seq, "present rate: %i%s\n",
1043 foo, battery->info.capacity_mode ? "0 mW" : " mA");
1044 } else if (battery->state.amperage > 0) {
1045 seq_printf(seq, "charging state: charging\n");
1046 foo = (battery->info.full_charge_capacity -
1047 battery->state.remaining_capacity) * cscale * 60 /
1048 (battery->state.average_time_to_full == 0 ? 1 :
1049 battery->state.average_time_to_full);
1050 seq_printf(seq, "present rate: %i%s\n",
1051 foo, battery->info.capacity_mode ? "0 mW" : " mA");
1053 seq_printf(seq, "charging state: charged\n");
1054 seq_printf(seq, "present rate: 0 %s\n",
1055 battery->info.capacity_mode ? "mW" : "mA");
1058 seq_printf(seq, "remaining capacity: %i%s",
1059 battery->state.remaining_capacity * cscale,
1060 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1062 seq_printf(seq, "present voltage: %i mV\n",
1063 battery->state.voltage * battery->info.vscale);
1072 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1074 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1077 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1079 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1083 if (battery->sbs->zombie) {
1089 if (update_mode == REQUEST_UPDATE_MODE) {
1090 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1092 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1093 "acpi_sbs_update_run() failed\n"));
1097 if (!acpi_battery_is_present(battery)) {
1098 seq_printf(seq, "present: no\n");
1102 if (battery->info.capacity_mode) {
1103 cscale = battery->info.vscale * battery->info.ipscale;
1105 cscale = battery->info.ipscale;
1108 seq_printf(seq, "alarm: ");
1109 if (battery->alarm.remaining_capacity) {
1110 seq_printf(seq, "%i%s",
1111 battery->alarm.remaining_capacity * cscale,
1112 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1114 seq_printf(seq, "disabled\n");
1125 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1126 size_t count, loff_t * ppos)
1128 struct seq_file *seq = (struct seq_file *)file->private_data;
1129 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1130 char alarm_string[12] = { '\0' };
1131 int result, old_alarm, new_alarm;
1133 if (battery->sbs->zombie) {
1139 if (!acpi_battery_is_present(battery)) {
1144 if (count > sizeof(alarm_string) - 1) {
1149 if (copy_from_user(alarm_string, buffer, count)) {
1154 alarm_string[count] = 0;
1156 old_alarm = battery->alarm.remaining_capacity;
1157 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1159 result = acpi_battery_set_alarm(battery, new_alarm);
1161 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1162 "acpi_battery_set_alarm() failed\n"));
1163 (void)acpi_battery_set_alarm(battery, old_alarm);
1166 result = acpi_battery_get_alarm(battery);
1168 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1169 "acpi_battery_get_alarm() failed\n"));
1170 (void)acpi_battery_set_alarm(battery, old_alarm);
1184 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1186 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1189 static struct file_operations acpi_battery_info_fops = {
1190 .open = acpi_battery_info_open_fs,
1192 .llseek = seq_lseek,
1193 .release = single_release,
1194 .owner = THIS_MODULE,
1197 static struct file_operations acpi_battery_state_fops = {
1198 .open = acpi_battery_state_open_fs,
1200 .llseek = seq_lseek,
1201 .release = single_release,
1202 .owner = THIS_MODULE,
1205 static struct file_operations acpi_battery_alarm_fops = {
1206 .open = acpi_battery_alarm_open_fs,
1208 .write = acpi_battery_write_alarm,
1209 .llseek = seq_lseek,
1210 .release = single_release,
1211 .owner = THIS_MODULE,
1214 /* Legacy AC Adapter Interface */
1216 static struct proc_dir_entry *acpi_ac_dir = NULL;
1218 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1220 struct acpi_sbs *sbs = (struct acpi_sbs *)seq->private;
1229 if (update_mode == REQUEST_UPDATE_MODE) {
1230 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1232 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1233 "acpi_sbs_update_run() failed\n"));
1237 seq_printf(seq, "state: %s\n",
1238 sbs->ac_present ? "on-line" : "off-line");
1245 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1247 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1250 static struct file_operations acpi_ac_state_fops = {
1251 .open = acpi_ac_state_open_fs,
1253 .llseek = seq_lseek,
1254 .release = single_release,
1255 .owner = THIS_MODULE,
1258 /* --------------------------------------------------------------------------
1260 -------------------------------------------------------------------------- */
1264 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1269 struct acpi_battery *battery;
1271 battery = &sbs->battery[id];
1275 battery->init_state = 0;
1279 result = acpi_battery_select(battery);
1281 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1282 "acpi_battery_select() failed\n"));
1286 result = acpi_battery_get_present(battery);
1288 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1289 "acpi_battery_get_present() failed\n"));
1293 is_present = acpi_battery_is_present(battery);
1296 result = acpi_battery_init(battery);
1298 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1299 "acpi_battery_init() failed\n"));
1302 battery->init_state = 1;
1305 (void)sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1307 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1310 &acpi_battery_info_fops,
1311 &acpi_battery_state_fops,
1312 &acpi_battery_alarm_fops, battery);
1314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1315 "acpi_sbs_generic_add_fs() failed\n"));
1324 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1327 if (sbs->battery[id].battery_entry) {
1328 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1333 static int acpi_ac_add(struct acpi_sbs *sbs)
1337 result = acpi_ac_get_present(sbs);
1339 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1340 "acpi_ac_get_present() failed\n"));
1344 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1347 NULL, &acpi_ac_state_fops, NULL, sbs);
1349 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1350 "acpi_sbs_generic_add_fs() failed\n"));
1359 static void acpi_ac_remove(struct acpi_sbs *sbs)
1362 if (sbs->ac_entry) {
1363 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1367 static void acpi_sbs_update_queue_run(unsigned long data)
1369 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1372 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1374 struct acpi_battery *battery;
1377 int old_battery_present;
1379 int new_battery_present;
1382 int do_battery_init, do_ac_init;
1383 s16 old_remaining_capacity;
1389 old_ac_present = acpi_ac_is_present(sbs);
1391 result = acpi_ac_get_present(sbs);
1393 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1394 "acpi_ac_get_present() failed\n"));
1397 new_ac_present = acpi_ac_is_present(sbs);
1399 do_ac_init = (old_ac_present != new_ac_present);
1401 if (data_type == DATA_TYPE_AC_STATE) {
1405 for (id = 0; id < MAX_SBS_BAT; id++) {
1406 battery = &sbs->battery[id];
1407 if (battery->alive == 0) {
1411 old_remaining_capacity = battery->state.remaining_capacity;
1413 old_battery_present = acpi_battery_is_present(battery);
1415 result = acpi_battery_select(battery);
1417 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1418 "acpi_battery_select() failed\n"));
1424 result = acpi_battery_get_present(battery);
1426 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1427 "acpi_battery_get_present() failed\n"));
1433 new_battery_present = acpi_battery_is_present(battery);
1435 do_battery_init = ((old_battery_present != new_battery_present)
1436 && new_battery_present);
1441 if (do_ac_init || do_battery_init ||
1442 update_info_mode || sbs->update_info_mode) {
1443 if (sbs->update_info_mode) {
1444 sbs->update_info_mode = 0;
1446 sbs->update_info_mode = 1;
1448 result = acpi_battery_init(battery);
1450 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1451 "acpi_battery_init() "
1455 if (data_type == DATA_TYPE_INFO) {
1462 if (new_battery_present) {
1463 result = acpi_battery_get_alarm(battery);
1465 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1466 "acpi_battery_get_alarm() "
1469 if (data_type == DATA_TYPE_ALARM) {
1473 result = acpi_battery_get_state(battery);
1475 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1476 "acpi_battery_get_state() "
1483 if (data_type != DATA_TYPE_COMMON) {
1487 if (old_battery_present != new_battery_present) {
1488 (void)sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1489 result = acpi_sbs_generate_event(sbs->device,
1490 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1491 new_battery_present,
1493 ACPI_BATTERY_CLASS);
1495 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1496 "acpi_sbs_generate_event() "
1500 if (old_remaining_capacity != battery->state.remaining_capacity) {
1501 (void)sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1502 result = acpi_sbs_generate_event(sbs->device,
1503 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1504 new_battery_present,
1506 ACPI_BATTERY_CLASS);
1508 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1509 "acpi_sbs_generate_event() failed\n"));
1517 if (data_type != DATA_TYPE_COMMON) {
1521 if (old_ac_present != new_ac_present) {
1522 result = acpi_sbs_generate_event(sbs->device,
1523 ACPI_SBS_AC_NOTIFY_STATUS,
1528 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1529 "acpi_sbs_generate_event() failed\n"));
1537 static void acpi_sbs_update_queue(void *data)
1539 struct acpi_sbs *sbs = data;
1540 unsigned long delay = -1;
1547 result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1549 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1550 "acpi_sbs_update_run() failed\n"));
1557 if (update_mode == REQUEST_UPDATE_MODE) {
1561 delay = jiffies + HZ * update_time;
1562 sbs->update_timer.data = (unsigned long)data;
1563 sbs->update_timer.function = acpi_sbs_update_queue_run;
1564 sbs->update_timer.expires = delay;
1565 add_timer(&sbs->update_timer);
1570 static int acpi_sbs_add(struct acpi_device *device)
1572 struct acpi_sbs *sbs = NULL;
1573 struct acpi_ec_hc *ec_hc = NULL;
1574 int result, remove_result = 0;
1575 unsigned long sbs_obj;
1577 acpi_status status = AE_OK;
1579 sbs = kmalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1581 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "kmalloc() failed\n"));
1584 memset(sbs, 0, sizeof(struct acpi_sbs));
1589 ec_hc = acpi_get_ec_hc(device);
1597 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1598 "acpi_get_ec_hc() failed: "
1599 "NO driver found for EC HC SMBus\n"));
1604 sbs->device = device;
1605 sbs->smbus = ec_hc->smbus;
1607 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1608 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1609 acpi_driver_data(device) = sbs;
1611 sbs->update_time = 0;
1612 sbs->update_time2 = 0;
1614 result = acpi_ac_add(sbs);
1616 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_ac_add() failed\n"));
1619 result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1620 if (ACPI_FAILURE(result)) {
1621 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1622 "acpi_evaluate_integer() failed\n"));
1628 result = acpi_sbsm_get_info(sbs);
1630 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1631 "acpi_sbsm_get_info() failed\n"));
1634 sbs->sbsm_present = 1;
1636 if (sbs->sbsm_present == 0) {
1637 result = acpi_battery_add(sbs, 0);
1639 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1640 "acpi_battery_add() failed\n"));
1644 for (id = 0; id < MAX_SBS_BAT; id++) {
1645 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1646 result = acpi_battery_add(sbs, id);
1648 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1649 "acpi_battery_add() "
1657 sbs->handle = device->handle;
1659 init_timer(&sbs->update_timer);
1660 if (update_mode == QUEUE_UPDATE_MODE) {
1661 status = acpi_os_execute(OSL_GPE_HANDLER,
1662 acpi_sbs_update_queue, (void *)sbs);
1663 if (status != AE_OK) {
1664 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1665 "acpi_os_execute() failed\n"));
1668 sbs->update_time = update_time;
1669 sbs->update_time2 = update_time2;
1671 printk(KERN_INFO PREFIX "%s [%s]\n",
1672 acpi_device_name(device), acpi_device_bid(device));
1676 remove_result = acpi_sbs_remove(device, 0);
1677 if (remove_result) {
1678 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1679 "acpi_sbs_remove() failed\n"));
1686 int acpi_sbs_remove(struct acpi_device *device, int type)
1688 struct acpi_sbs *sbs = (struct acpi_sbs *)acpi_driver_data(device);
1691 if (!device || !sbs) {
1696 sbs->update_time = 0;
1697 sbs->update_time2 = 0;
1698 del_timer_sync(&sbs->update_timer);
1699 acpi_os_wait_events_complete(NULL);
1700 del_timer_sync(&sbs->update_timer);
1702 for (id = 0; id < MAX_SBS_BAT; id++) {
1703 acpi_battery_remove(sbs, id);
1706 acpi_ac_remove(sbs);
1713 static int __init acpi_sbs_init(void)
1720 init_MUTEX(&sbs_sem);
1722 if (capacity_mode != DEF_CAPACITY_UNIT
1723 && capacity_mode != MAH_CAPACITY_UNIT
1724 && capacity_mode != MWH_CAPACITY_UNIT) {
1725 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_sbs_init: "
1726 "invalid capacity_mode = %d\n",
1731 acpi_ac_dir = acpi_lock_ac_dir();
1733 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1734 "acpi_lock_ac_dir() failed\n"));
1738 acpi_battery_dir = acpi_lock_battery_dir();
1739 if (!acpi_battery_dir) {
1740 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1741 "acpi_lock_battery_dir() failed\n"));
1745 result = acpi_bus_register_driver(&acpi_sbs_driver);
1747 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1748 "acpi_bus_register_driver() failed\n"));
1755 static void __exit acpi_sbs_exit(void)
1758 acpi_bus_unregister_driver(&acpi_sbs_driver);
1760 acpi_unlock_ac_dir(acpi_ac_dir);
1762 acpi_unlock_battery_dir(acpi_battery_dir);
1763 acpi_battery_dir = NULL;
1768 module_init(acpi_sbs_init);
1769 module_exit(acpi_sbs_exit);