2 * ec.c - ACPI Embedded Controller Driver (v2.0)
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/actypes.h>
42 #define _COMPONENT ACPI_EC_COMPONENT
43 ACPI_MODULE_NAME("ec");
44 #define ACPI_EC_COMPONENT 0x00100000
45 #define ACPI_EC_CLASS "embedded_controller"
46 #define ACPI_EC_HID "PNP0C09"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
50 #define PREFIX "ACPI: EC: "
51 /* EC status register */
52 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
53 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
54 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
55 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
58 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
66 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
67 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
70 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
71 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
74 EC_INTR = 1, /* Output buffer full */
75 EC_POLL, /* Input buffer empty */
76 } acpi_ec_mode = EC_INTR;
78 static int acpi_ec_remove(struct acpi_device *device, int type);
79 static int acpi_ec_start(struct acpi_device *device);
80 static int acpi_ec_stop(struct acpi_device *device, int type);
81 static int acpi_ec_add(struct acpi_device *device);
83 static struct acpi_driver acpi_ec_driver = {
85 .class = ACPI_EC_CLASS,
89 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
95 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
96 /* External interfaces use first EC only, so remember */
97 static struct acpi_ec {
100 unsigned long command_addr;
101 unsigned long data_addr;
102 unsigned long global_lock;
104 atomic_t query_pending;
105 atomic_t event_count;
106 wait_queue_head_t wait;
107 } *boot_ec, *first_ec;
109 /* --------------------------------------------------------------------------
110 Transaction Management
111 -------------------------------------------------------------------------- */
113 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
115 return inb(ec->command_addr);
118 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
120 return inb(ec->data_addr);
123 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
125 outb(command, ec->command_addr);
128 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
130 outb(data, ec->data_addr);
133 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
136 u8 status = acpi_ec_read_status(ec);
137 if (old_count == atomic_read(&ec->event_count))
139 if (event == ACPI_EC_EVENT_OBF_1) {
140 if (status & ACPI_EC_FLAG_OBF)
142 } else if (event == ACPI_EC_EVENT_IBF_0) {
143 if (!(status & ACPI_EC_FLAG_IBF))
150 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
151 unsigned count, int force_poll)
153 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
154 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
155 while (time_before(jiffies, delay)) {
156 if (acpi_ec_check_status(ec, event, 0))
160 if (wait_event_timeout(ec->wait,
161 acpi_ec_check_status(ec, event, count),
162 msecs_to_jiffies(ACPI_EC_DELAY)) ||
163 acpi_ec_check_status(ec, event, 0)) {
166 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
167 " status = %d, expect_event = %d\n",
168 acpi_ec_read_status(ec), event);
175 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
176 const u8 * wdata, unsigned wdata_len,
177 u8 * rdata, unsigned rdata_len,
181 unsigned count = atomic_read(&ec->event_count);
182 acpi_ec_write_cmd(ec, command);
184 for (; wdata_len > 0; --wdata_len) {
185 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
187 printk(KERN_ERR PREFIX
188 "write_cmd timeout, command = %d\n", command);
191 count = atomic_read(&ec->event_count);
192 acpi_ec_write_data(ec, *(wdata++));
196 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
198 printk(KERN_ERR PREFIX
199 "finish-write timeout, command = %d\n", command);
202 } else if (command == ACPI_EC_COMMAND_QUERY) {
203 atomic_set(&ec->query_pending, 0);
206 for (; rdata_len > 0; --rdata_len) {
207 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
209 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
213 count = atomic_read(&ec->event_count);
214 *(rdata++) = acpi_ec_read_data(ec);
220 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
221 const u8 * wdata, unsigned wdata_len,
222 u8 * rdata, unsigned rdata_len,
228 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
232 memset(rdata, 0, rdata_len);
234 mutex_lock(&ec->lock);
235 if (ec->global_lock) {
236 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
237 if (ACPI_FAILURE(status)) {
238 mutex_unlock(&ec->lock);
243 /* Make sure GPE is enabled before doing transaction */
244 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
246 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
248 printk(KERN_DEBUG PREFIX
249 "input buffer is not empty, aborting transaction\n");
253 status = acpi_ec_transaction_unlocked(ec, command,
261 acpi_release_global_lock(glk);
262 mutex_unlock(&ec->lock);
268 * Note: samsung nv5000 doesn't work with ec burst mode.
269 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
271 int acpi_ec_burst_enable(struct acpi_ec *ec)
274 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
277 int acpi_ec_burst_disable(struct acpi_ec *ec)
279 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
282 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
287 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
288 &address, 1, &d, 1, 0);
293 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
295 u8 wdata[2] = { address, data };
296 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
297 wdata, 2, NULL, 0, 0);
301 * Externally callable EC access functions. For now, assume 1 EC only
303 int ec_burst_enable(void)
307 return acpi_ec_burst_enable(first_ec);
310 EXPORT_SYMBOL(ec_burst_enable);
312 int ec_burst_disable(void)
316 return acpi_ec_burst_disable(first_ec);
319 EXPORT_SYMBOL(ec_burst_disable);
321 int ec_read(u8 addr, u8 * val)
329 err = acpi_ec_read(first_ec, addr, &temp_data);
338 EXPORT_SYMBOL(ec_read);
340 int ec_write(u8 addr, u8 val)
347 err = acpi_ec_write(first_ec, addr, val);
352 EXPORT_SYMBOL(ec_write);
354 int ec_transaction(u8 command,
355 const u8 * wdata, unsigned wdata_len,
356 u8 * rdata, unsigned rdata_len,
362 return acpi_ec_transaction(first_ec, command, wdata,
363 wdata_len, rdata, rdata_len,
367 EXPORT_SYMBOL(ec_transaction);
369 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
378 * Query the EC to find out which _Qxx method we need to evaluate.
379 * Note that successful completion of the query causes the ACPI_EC_SCI
380 * bit to be cleared (and thus clearing the interrupt source).
383 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
394 /* --------------------------------------------------------------------------
396 -------------------------------------------------------------------------- */
398 static void acpi_ec_gpe_query(void *ec_cxt)
400 struct acpi_ec *ec = ec_cxt;
404 if (!ec || acpi_ec_query(ec, &value))
407 snprintf(object_name, 8, "_Q%2.2X", value);
409 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
411 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
414 static u32 acpi_ec_gpe_handler(void *data)
416 acpi_status status = AE_OK;
418 struct acpi_ec *ec = data;
420 atomic_inc(&ec->event_count);
422 if (acpi_ec_mode == EC_INTR) {
426 value = acpi_ec_read_status(ec);
427 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
428 atomic_set(&ec->query_pending, 1);
430 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
434 return status == AE_OK ?
435 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
438 /* --------------------------------------------------------------------------
439 Address Space Management
440 -------------------------------------------------------------------------- */
443 acpi_ec_space_setup(acpi_handle region_handle,
444 u32 function, void *handler_context, void **return_context)
447 * The EC object is in the handler context and is needed
448 * when calling the acpi_ec_space_handler.
450 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
451 handler_context : NULL;
457 acpi_ec_space_handler(u32 function,
458 acpi_physical_address address,
460 acpi_integer * value,
461 void *handler_context, void *region_context)
464 struct acpi_ec *ec = handler_context;
466 acpi_integer f_v = 0;
469 if ((address > 0xFF) || !value || !handler_context)
470 return AE_BAD_PARAMETER;
472 if (bit_width != 8 && acpi_strict) {
473 return AE_BAD_PARAMETER;
480 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
483 result = acpi_ec_write(ec, (u8) address, (u8) temp);
493 if (function == ACPI_READ)
494 f_v |= temp << 8 * i;
495 if (function == ACPI_WRITE)
502 if (function == ACPI_READ) {
503 f_v |= temp << 8 * i;
510 return AE_BAD_PARAMETER;
523 /* --------------------------------------------------------------------------
525 -------------------------------------------------------------------------- */
527 static struct proc_dir_entry *acpi_ec_dir;
529 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
531 struct acpi_ec *ec = seq->private;
536 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
537 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
538 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
539 seq_printf(seq, "use global lock:\t%s\n",
540 ec->global_lock ? "yes" : "no");
545 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
547 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
550 static struct file_operations acpi_ec_info_ops = {
551 .open = acpi_ec_info_open_fs,
554 .release = single_release,
555 .owner = THIS_MODULE,
558 static int acpi_ec_add_fs(struct acpi_device *device)
560 struct proc_dir_entry *entry = NULL;
562 if (!acpi_device_dir(device)) {
563 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
565 if (!acpi_device_dir(device))
569 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
570 acpi_device_dir(device));
574 entry->proc_fops = &acpi_ec_info_ops;
575 entry->data = acpi_driver_data(device);
576 entry->owner = THIS_MODULE;
582 static int acpi_ec_remove_fs(struct acpi_device *device)
585 if (acpi_device_dir(device)) {
586 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
587 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
588 acpi_device_dir(device) = NULL;
594 /* --------------------------------------------------------------------------
596 -------------------------------------------------------------------------- */
598 ec_parse_io_ports(struct acpi_resource *resource, void *context);
601 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
603 static struct acpi_ec *make_acpi_ec(void)
605 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
609 atomic_set(&ec->query_pending, 1);
610 atomic_set(&ec->event_count, 1);
611 mutex_init(&ec->lock);
612 init_waitqueue_head(&ec->wait);
617 static int acpi_ec_add(struct acpi_device *device)
619 acpi_status status = AE_OK;
620 struct acpi_ec *ec = NULL;
625 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
626 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
632 status = ec_parse_device(device->handle, 0, ec, NULL);
633 if (status != AE_CTRL_TERMINATE) {
638 /* Check if we found the boot EC */
640 if (boot_ec->gpe == ec->gpe) {
641 /* We might have incorrect info for GL at boot time */
642 mutex_lock(&boot_ec->lock);
643 boot_ec->global_lock = ec->global_lock;
644 mutex_unlock(&boot_ec->lock);
650 ec->handle = device->handle;
651 acpi_driver_data(device) = ec;
653 acpi_ec_add_fs(device);
655 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
656 acpi_device_name(device), acpi_device_bid(device),
662 static int acpi_ec_remove(struct acpi_device *device, int type)
669 ec = acpi_driver_data(device);
670 acpi_ec_remove_fs(device);
671 acpi_driver_data(device) = NULL;
675 /* Don't touch boot EC */
682 ec_parse_io_ports(struct acpi_resource *resource, void *context)
684 struct acpi_ec *ec = context;
686 if (resource->type != ACPI_RESOURCE_TYPE_IO)
690 * The first address region returned is the data port, and
691 * the second address region returned is the status/command
694 if (ec->data_addr == 0)
695 ec->data_addr = resource->data.io.minimum;
696 else if (ec->command_addr == 0)
697 ec->command_addr = resource->data.io.minimum;
699 return AE_CTRL_TERMINATE;
704 static int ec_install_handlers(struct acpi_ec *ec)
707 status = acpi_install_gpe_handler(NULL, ec->gpe,
708 ACPI_GPE_EDGE_TRIGGERED,
709 &acpi_ec_gpe_handler, ec);
710 if (ACPI_FAILURE(status))
713 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
714 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
716 status = acpi_install_address_space_handler(ec->handle,
718 &acpi_ec_space_handler,
719 &acpi_ec_space_setup, ec);
720 if (ACPI_FAILURE(status)) {
721 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
725 /* EC is fully operational, allow queries */
726 atomic_set(&ec->query_pending, 0);
731 static int acpi_ec_start(struct acpi_device *device)
738 ec = acpi_driver_data(device);
743 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
744 ec->gpe, ec->command_addr, ec->data_addr));
746 /* Boot EC is already working */
750 return ec_install_handlers(ec);
753 static int acpi_ec_stop(struct acpi_device *device, int type)
761 ec = acpi_driver_data(device);
765 /* Don't touch boot EC */
769 status = acpi_remove_address_space_handler(ec->handle,
771 &acpi_ec_space_handler);
772 if (ACPI_FAILURE(status))
775 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
776 if (ACPI_FAILURE(status))
783 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
787 struct acpi_ec *ec = context;
788 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
789 ec_parse_io_ports, ec);
790 if (ACPI_FAILURE(status))
793 /* Get GPE bit assignment (EC events). */
794 /* TODO: Add support for _GPE returning a package */
795 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
796 if (ACPI_FAILURE(status))
799 /* Use the global lock for all EC transactions? */
800 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
804 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
805 ec->gpe, ec->command_addr, ec->data_addr));
807 return AE_CTRL_TERMINATE;
810 int __init acpi_ec_ecdt_probe(void)
814 struct acpi_table_ecdt *ecdt_ptr;
816 boot_ec = make_acpi_ec();
820 * Generate a boot ec context
823 status = acpi_get_table(ACPI_SIG_ECDT, 1,
824 (struct acpi_table_header **)&ecdt_ptr);
825 if (ACPI_FAILURE(status))
828 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
830 boot_ec->command_addr = ecdt_ptr->control.address;
831 boot_ec->data_addr = ecdt_ptr->data.address;
832 boot_ec->gpe = ecdt_ptr->gpe;
833 boot_ec->handle = ACPI_ROOT_OBJECT;
835 ret = ec_install_handlers(boot_ec);
847 static int __init acpi_ec_init(void)
854 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
858 /* Now register the driver for the EC */
859 result = acpi_bus_register_driver(&acpi_ec_driver);
861 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
868 subsys_initcall(acpi_ec_init);
870 /* EC driver currently not unloadable */
872 static void __exit acpi_ec_exit(void)
875 acpi_bus_unregister_driver(&acpi_ec_driver);
877 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
883 static int __init acpi_ec_set_intr_mode(char *str)
887 if (!get_option(&str, &intr))
890 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
892 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
897 __setup("ec_intr=", acpi_ec_set_intr_mode);