2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/interrupt.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38 #include <acpi/actypes.h>
40 #define _COMPONENT ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME("ec");
42 #define ACPI_EC_COMPONENT 0x00100000
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_HID "PNP0C09"
45 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
46 #define ACPI_EC_FILE_INFO "info"
48 #define PREFIX "ACPI: EC: "
49 /* EC status register */
50 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
52 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
53 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
56 ACPI_EC_COMMAND_READ = 0x80,
57 ACPI_EC_COMMAND_WRITE = 0x81,
58 ACPI_EC_BURST_ENABLE = 0x82,
59 ACPI_EC_BURST_DISABLE = 0x83,
60 ACPI_EC_COMMAND_QUERY = 0x84,
64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
68 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
69 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
72 EC_INTR = 1, /* Output buffer full */
73 EC_POLL, /* Input buffer empty */
74 } acpi_ec_mode = EC_INTR;
76 static int acpi_ec_remove(struct acpi_device *device, int type);
77 static int acpi_ec_start(struct acpi_device *device);
78 static int acpi_ec_stop(struct acpi_device *device, int type);
79 static int acpi_ec_add(struct acpi_device *device);
81 static struct acpi_driver acpi_ec_driver = {
83 .class = ACPI_EC_CLASS,
87 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
93 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
94 /* External interfaces use first EC only, so remember */
95 static struct acpi_ec {
99 unsigned long command_addr;
100 unsigned long data_addr;
101 unsigned long global_lock;
103 atomic_t query_pending;
104 atomic_t event_count;
105 wait_queue_head_t wait;
106 } *boot_ec, *first_ec;
108 /* --------------------------------------------------------------------------
109 Transaction Management
110 -------------------------------------------------------------------------- */
112 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
114 return inb(ec->command_addr);
117 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
119 return inb(ec->data_addr);
122 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
124 outb(command, ec->command_addr);
127 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
129 outb(data, ec->data_addr);
132 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
135 u8 status = acpi_ec_read_status(ec);
136 if (old_count == atomic_read(&ec->event_count))
138 if (event == ACPI_EC_EVENT_OBF_1) {
139 if (status & ACPI_EC_FLAG_OBF)
141 } else if (event == ACPI_EC_EVENT_IBF_0) {
142 if (!(status & ACPI_EC_FLAG_IBF))
149 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
151 if (acpi_ec_mode == EC_POLL) {
152 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
153 while (time_before(jiffies, delay)) {
154 if (acpi_ec_check_status(ec, event, 0))
158 if (wait_event_timeout(ec->wait,
159 acpi_ec_check_status(ec, event, count),
160 msecs_to_jiffies(ACPI_EC_DELAY)) ||
161 acpi_ec_check_status(ec, event, 0)) {
164 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
165 " status = %d, expect_event = %d\n",
166 acpi_ec_read_status(ec), event);
173 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
174 const u8 * wdata, unsigned wdata_len,
175 u8 * rdata, unsigned rdata_len)
178 unsigned count = atomic_read(&ec->event_count);
179 acpi_ec_write_cmd(ec, command);
181 for (; wdata_len > 0; --wdata_len) {
182 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
184 printk(KERN_ERR PREFIX
185 "write_cmd timeout, command = %d\n", command);
188 count = atomic_read(&ec->event_count);
189 acpi_ec_write_data(ec, *(wdata++));
193 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
195 printk(KERN_ERR PREFIX
196 "finish-write timeout, command = %d\n", command);
199 } else if (command == ACPI_EC_COMMAND_QUERY) {
200 atomic_set(&ec->query_pending, 0);
203 for (; rdata_len > 0; --rdata_len) {
204 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
206 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
210 count = atomic_read(&ec->event_count);
211 *(rdata++) = acpi_ec_read_data(ec);
217 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
218 const u8 * wdata, unsigned wdata_len,
219 u8 * rdata, unsigned rdata_len)
224 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
228 memset(rdata, 0, rdata_len);
230 mutex_lock(&ec->lock);
231 if (ec->global_lock) {
232 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
233 if (ACPI_FAILURE(status)) {
234 mutex_unlock(&ec->lock);
239 /* Make sure GPE is enabled before doing transaction */
240 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
242 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
244 printk(KERN_DEBUG PREFIX
245 "input buffer is not empty, aborting transaction\n");
249 status = acpi_ec_transaction_unlocked(ec, command,
256 acpi_release_global_lock(glk);
257 mutex_unlock(&ec->lock);
263 * Note: samsung nv5000 doesn't work with ec burst mode.
264 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
266 int acpi_ec_burst_enable(struct acpi_ec *ec)
269 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1);
272 int acpi_ec_burst_disable(struct acpi_ec *ec)
274 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0);
277 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
282 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
288 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
290 u8 wdata[2] = { address, data };
291 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
296 * Externally callable EC access functions. For now, assume 1 EC only
298 int ec_burst_enable(void)
302 return acpi_ec_burst_enable(first_ec);
305 EXPORT_SYMBOL(ec_burst_enable);
307 int ec_burst_disable(void)
311 return acpi_ec_burst_disable(first_ec);
314 EXPORT_SYMBOL(ec_burst_disable);
316 int ec_read(u8 addr, u8 * val)
324 err = acpi_ec_read(first_ec, addr, &temp_data);
333 EXPORT_SYMBOL(ec_read);
335 int ec_write(u8 addr, u8 val)
342 err = acpi_ec_write(first_ec, addr, val);
347 EXPORT_SYMBOL(ec_write);
349 int ec_transaction(u8 command,
350 const u8 * wdata, unsigned wdata_len,
351 u8 * rdata, unsigned rdata_len)
356 return acpi_ec_transaction(first_ec, command, wdata,
357 wdata_len, rdata, rdata_len);
360 EXPORT_SYMBOL(ec_transaction);
362 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
371 * Query the EC to find out which _Qxx method we need to evaluate.
372 * Note that successful completion of the query causes the ACPI_EC_SCI
373 * bit to be cleared (and thus clearing the interrupt source).
376 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
387 /* --------------------------------------------------------------------------
389 -------------------------------------------------------------------------- */
391 static void acpi_ec_gpe_query(void *ec_cxt)
393 struct acpi_ec *ec = ec_cxt;
397 if (!ec || acpi_ec_query(ec, &value))
400 snprintf(object_name, 8, "_Q%2.2X", value);
402 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
404 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
407 static u32 acpi_ec_gpe_handler(void *data)
409 acpi_status status = AE_OK;
411 struct acpi_ec *ec = data;
412 atomic_inc(&ec->event_count);
414 if (acpi_ec_mode == EC_INTR) {
418 value = acpi_ec_read_status(ec);
419 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
420 atomic_set(&ec->query_pending, 1);
422 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
426 return status == AE_OK ?
427 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
430 /* --------------------------------------------------------------------------
431 Address Space Management
432 -------------------------------------------------------------------------- */
435 acpi_ec_space_setup(acpi_handle region_handle,
436 u32 function, void *handler_context, void **return_context)
439 * The EC object is in the handler context and is needed
440 * when calling the acpi_ec_space_handler.
442 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
443 handler_context : NULL;
449 acpi_ec_space_handler(u32 function,
450 acpi_physical_address address,
452 acpi_integer * value,
453 void *handler_context, void *region_context)
456 struct acpi_ec *ec = handler_context;
458 acpi_integer f_v = 0;
461 if ((address > 0xFF) || !value || !handler_context)
462 return AE_BAD_PARAMETER;
464 if (bit_width != 8 && acpi_strict) {
465 return AE_BAD_PARAMETER;
472 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
475 result = acpi_ec_write(ec, (u8) address, (u8) temp);
485 if (function == ACPI_READ)
486 f_v |= temp << 8 * i;
487 if (function == ACPI_WRITE)
494 if (function == ACPI_READ) {
495 f_v |= temp << 8 * i;
502 return AE_BAD_PARAMETER;
515 /* --------------------------------------------------------------------------
517 -------------------------------------------------------------------------- */
519 static struct proc_dir_entry *acpi_ec_dir;
521 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
523 struct acpi_ec *ec = seq->private;
528 seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
529 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
530 (u32) ec->command_addr, (u32) ec->data_addr);
531 seq_printf(seq, "use global lock: %s\n",
532 ec->global_lock ? "yes" : "no");
533 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
539 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
541 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
544 static struct file_operations acpi_ec_info_ops = {
545 .open = acpi_ec_info_open_fs,
548 .release = single_release,
549 .owner = THIS_MODULE,
552 static int acpi_ec_add_fs(struct acpi_device *device)
554 struct proc_dir_entry *entry = NULL;
556 if (!acpi_device_dir(device)) {
557 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
559 if (!acpi_device_dir(device))
563 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
564 acpi_device_dir(device));
568 entry->proc_fops = &acpi_ec_info_ops;
569 entry->data = acpi_driver_data(device);
570 entry->owner = THIS_MODULE;
576 static int acpi_ec_remove_fs(struct acpi_device *device)
579 if (acpi_device_dir(device)) {
580 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
581 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
582 acpi_device_dir(device) = NULL;
588 /* --------------------------------------------------------------------------
590 -------------------------------------------------------------------------- */
592 ec_parse_io_ports(struct acpi_resource *resource, void *context);
595 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
597 static struct acpi_ec *make_acpi_ec(void)
599 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
603 atomic_set(&ec->query_pending, 0);
604 atomic_set(&ec->event_count, 1);
605 mutex_init(&ec->lock);
606 init_waitqueue_head(&ec->wait);
611 static int acpi_ec_add(struct acpi_device *device)
613 acpi_status status = AE_OK;
614 struct acpi_ec *ec = NULL;
619 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
620 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
626 status = ec_parse_device(device->handle, 0, ec, NULL);
627 if (status != AE_CTRL_TERMINATE) {
632 /* Check if we found the boot EC */
634 if (boot_ec->gpe == ec->gpe) {
635 /* We might have incorrect info for GL at boot time */
636 mutex_lock(&boot_ec->lock);
637 boot_ec->global_lock = ec->global_lock;
638 mutex_unlock(&boot_ec->lock);
644 ec->handle = device->handle;
645 acpi_driver_data(device) = ec;
647 acpi_ec_add_fs(device);
649 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
650 acpi_device_name(device), acpi_device_bid(device),
656 static int acpi_ec_remove(struct acpi_device *device, int type)
658 struct acpi_ec *ec = NULL;
663 ec = acpi_driver_data(device);
665 acpi_ec_remove_fs(device);
667 acpi_driver_data(device) = NULL;
671 /* Don't touch boot EC */
679 ec_parse_io_ports(struct acpi_resource *resource, void *context)
681 struct acpi_ec *ec = context;
683 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
688 * The first address region returned is the data port, and
689 * the second address region returned is the status/command
692 if (ec->data_addr == 0) {
693 ec->data_addr = resource->data.io.minimum;
694 } else if (ec->command_addr == 0) {
695 ec->command_addr = resource->data.io.minimum;
697 return AE_CTRL_TERMINATE;
703 static int ec_install_handlers(struct acpi_ec *ec)
706 status = acpi_install_gpe_handler(NULL, ec->gpe,
707 ACPI_GPE_EDGE_TRIGGERED,
708 &acpi_ec_gpe_handler, ec);
709 if (ACPI_FAILURE(status))
711 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
712 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
714 status = acpi_install_address_space_handler(ec->handle,
716 &acpi_ec_space_handler,
717 &acpi_ec_space_setup, ec);
718 if (ACPI_FAILURE(status)) {
719 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
726 static int acpi_ec_start(struct acpi_device *device)
733 ec = acpi_driver_data(device);
738 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
739 ec->gpe, ec->command_addr, ec->data_addr));
741 /* Boot EC is already working */
745 return ec_install_handlers(ec);
748 static int acpi_ec_stop(struct acpi_device *device, int type)
756 ec = acpi_driver_data(device);
760 /* Don't touch boot EC */
764 status = acpi_remove_address_space_handler(ec->handle,
766 &acpi_ec_space_handler);
767 if (ACPI_FAILURE(status))
770 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
771 if (ACPI_FAILURE(status))
778 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
782 struct acpi_ec *ec = context;
783 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
784 ec_parse_io_ports, ec);
785 if (ACPI_FAILURE(status))
788 /* Get GPE bit assignment (EC events). */
789 /* TODO: Add support for _GPE returning a package */
790 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
791 if (ACPI_FAILURE(status))
794 /* Use the global lock for all EC transactions? */
795 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
799 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
800 ec->gpe, ec->command_addr, ec->data_addr));
802 return AE_CTRL_TERMINATE;
805 int __init acpi_ec_ecdt_probe(void)
809 struct acpi_table_ecdt *ecdt_ptr;
811 boot_ec = make_acpi_ec();
815 * Generate a boot ec context
818 status = acpi_get_table(ACPI_SIG_ECDT, 1,
819 (struct acpi_table_header **)&ecdt_ptr);
820 if (ACPI_FAILURE(status))
823 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
825 boot_ec->command_addr = ecdt_ptr->control.address;
826 boot_ec->data_addr = ecdt_ptr->data.address;
827 boot_ec->gpe = ecdt_ptr->gpe;
828 boot_ec->uid = ecdt_ptr->uid;
829 boot_ec->handle = ACPI_ROOT_OBJECT;
831 ret = ec_install_handlers(boot_ec);
843 static int __init acpi_ec_init(void)
850 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
854 /* Now register the driver for the EC */
855 result = acpi_bus_register_driver(&acpi_ec_driver);
857 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
864 subsys_initcall(acpi_ec_init);
866 /* EC driver currently not unloadable */
868 static void __exit acpi_ec_exit(void)
871 acpi_bus_unregister_driver(&acpi_ec_driver);
873 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
879 static int __init acpi_ec_set_intr_mode(char *str)
883 if (!get_option(&str, &intr))
886 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
888 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
893 __setup("ec_intr=", acpi_ec_set_intr_mode);