2 * ec.c - ACPI Embedded Controller Driver (v2.1)
4 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
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 /* Uncomment next line to get verbose printout */
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
41 #include <linux/spinlock.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <acpi/actypes.h>
47 #define ACPI_EC_CLASS "embedded_controller"
48 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
49 #define ACPI_EC_FILE_INFO "info"
52 #define PREFIX "ACPI: EC: "
54 /* EC status register */
55 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
56 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
57 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
58 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
62 ACPI_EC_COMMAND_READ = 0x80,
63 ACPI_EC_COMMAND_WRITE = 0x81,
64 ACPI_EC_BURST_ENABLE = 0x82,
65 ACPI_EC_BURST_DISABLE = 0x83,
66 ACPI_EC_COMMAND_QUERY = 0x84,
69 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
70 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
71 #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
73 #define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts
74 per one transaction */
77 EC_FLAGS_QUERY_PENDING, /* Query is pending */
78 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
79 * for status change */
80 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
81 EC_FLAGS_GPE_STORM, /* GPE storm detected */
82 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
83 * OpReg are installed */
86 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
87 /* External interfaces use first EC only, so remember */
88 typedef int (*acpi_ec_query_func) (void *data);
90 struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
98 struct transaction_data {
101 unsigned short irq_count;
106 static struct acpi_ec {
109 unsigned long command_addr;
110 unsigned long data_addr;
111 unsigned long global_lock;
114 wait_queue_head_t wait;
115 struct list_head list;
116 struct transaction_data *t;
118 } *boot_ec, *first_ec;
121 * Some Asus system have exchanged ECDT data/command IO addresses.
123 static int print_ecdt_error(const struct dmi_system_id *id)
125 printk(KERN_NOTICE PREFIX "%s detected - "
126 "ECDT has exchanged control/data I/O address\n",
131 static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
133 print_ecdt_error, "Asus L4R", {
134 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
135 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
136 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
138 print_ecdt_error, "Asus M6R", {
139 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
141 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
145 /* --------------------------------------------------------------------------
146 Transaction Management
147 -------------------------------------------------------------------------- */
149 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
151 u8 x = inb(ec->command_addr);
152 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
156 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
158 u8 x = inb(ec->data_addr);
159 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
163 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
165 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
166 outb(command, ec->command_addr);
169 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
171 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
172 outb(data, ec->data_addr);
175 static int ec_transaction_done(struct acpi_ec *ec)
179 spin_lock_irqsave(&ec->t_lock, flags);
180 if (!ec->t || (!ec->t->wlen && !ec->t->rlen))
182 spin_unlock_irqrestore(&ec->t_lock, flags);
186 static void gpe_transaction(struct acpi_ec *ec, u8 status)
189 spin_lock_irqsave(&ec->t_lock, flags);
192 if (ec->t->wlen > 0) {
193 if ((status & ACPI_EC_FLAG_IBF) == 0) {
194 acpi_ec_write_data(ec, *(ec->t->wdata++));
197 /* false interrupt, state didn't change */
200 } else if (ec->t->rlen > 0) {
201 if ((status & ACPI_EC_FLAG_OBF) == 1) {
202 *(ec->t->rdata++) = acpi_ec_read_data(ec);
205 /* false interrupt, state didn't change */
209 spin_unlock_irqrestore(&ec->t_lock, flags);
212 static int acpi_ec_wait(struct acpi_ec *ec)
214 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
215 msecs_to_jiffies(ACPI_EC_DELAY)))
217 /* missing GPEs, switch back to poll mode */
218 if (printk_ratelimit())
219 pr_info(PREFIX "missing confirmations, "
220 "switch off interrupt mode.\n");
221 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
222 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
226 static void acpi_ec_gpe_query(void *ec_cxt);
228 static int ec_check_sci(struct acpi_ec *ec, u8 state)
230 if (state & ACPI_EC_FLAG_SCI) {
231 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
232 return acpi_os_execute(OSL_EC_BURST_HANDLER,
233 acpi_ec_gpe_query, ec);
238 static int ec_poll(struct acpi_ec *ec)
240 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
242 while (time_before(jiffies, delay)) {
243 gpe_transaction(ec, acpi_ec_read_status(ec));
245 if (ec_transaction_done(ec))
251 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
252 const u8 * wdata, unsigned wdata_len,
253 u8 * rdata, unsigned rdata_len,
257 struct transaction_data t = {.wdata = wdata, .rdata = rdata,
258 .wlen = wdata_len, .rlen = rdata_len,
261 pr_debug(PREFIX "transaction start\n");
262 /* disable GPE during transaction if storm is detected */
263 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
264 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
265 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
267 /* start transaction */
268 spin_lock_irqsave(&ec->t_lock, tmp);
269 /* following two actions should be kept atomic */
271 acpi_ec_write_cmd(ec, command);
272 if (command == ACPI_EC_COMMAND_QUERY)
273 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
274 spin_unlock_irqrestore(&ec->t_lock, tmp);
275 /* if we selected poll mode or failed in GPE-mode do a poll loop */
277 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
280 pr_debug(PREFIX "transaction end\n");
281 spin_lock_irqsave(&ec->t_lock, tmp);
283 spin_unlock_irqrestore(&ec->t_lock, tmp);
284 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
285 /* check if we received SCI during transaction */
286 ec_check_sci(ec, acpi_ec_read_status(ec));
287 /* it is safe to enable GPE outside of transaction */
288 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
289 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
290 t.irq_count > ACPI_EC_STORM_THRESHOLD) {
291 pr_debug(PREFIX "GPE storm detected\n");
292 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
297 static int ec_check_ibf0(struct acpi_ec *ec)
299 u8 status = acpi_ec_read_status(ec);
300 return (status & ACPI_EC_FLAG_IBF) == 0;
303 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
304 const u8 * wdata, unsigned wdata_len,
305 u8 * rdata, unsigned rdata_len,
310 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
313 memset(rdata, 0, rdata_len);
314 mutex_lock(&ec->lock);
315 if (ec->global_lock) {
316 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
317 if (ACPI_FAILURE(status)) {
322 if (!wait_event_timeout(ec->wait, ec_check_ibf0(ec),
323 msecs_to_jiffies(ACPI_EC_DELAY))) {
324 pr_err(PREFIX "input buffer is not empty, "
325 "aborting transaction\n");
329 status = acpi_ec_transaction_unlocked(ec, command,
335 acpi_release_global_lock(glk);
337 mutex_unlock(&ec->lock);
342 * Note: samsung nv5000 doesn't work with ec burst mode.
343 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
345 int acpi_ec_burst_enable(struct acpi_ec *ec)
348 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
351 int acpi_ec_burst_disable(struct acpi_ec *ec)
353 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
354 acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE,
355 NULL, 0, NULL, 0, 0) : 0;
358 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
363 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
364 &address, 1, &d, 1, 0);
369 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
371 u8 wdata[2] = { address, data };
372 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
373 wdata, 2, NULL, 0, 0);
377 * Externally callable EC access functions. For now, assume 1 EC only
379 int ec_burst_enable(void)
383 return acpi_ec_burst_enable(first_ec);
386 EXPORT_SYMBOL(ec_burst_enable);
388 int ec_burst_disable(void)
392 return acpi_ec_burst_disable(first_ec);
395 EXPORT_SYMBOL(ec_burst_disable);
397 int ec_read(u8 addr, u8 * val)
405 err = acpi_ec_read(first_ec, addr, &temp_data);
414 EXPORT_SYMBOL(ec_read);
416 int ec_write(u8 addr, u8 val)
423 err = acpi_ec_write(first_ec, addr, val);
428 EXPORT_SYMBOL(ec_write);
430 int ec_transaction(u8 command,
431 const u8 * wdata, unsigned wdata_len,
432 u8 * rdata, unsigned rdata_len,
438 return acpi_ec_transaction(first_ec, command, wdata,
439 wdata_len, rdata, rdata_len,
443 EXPORT_SYMBOL(ec_transaction);
445 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
454 * Query the EC to find out which _Qxx method we need to evaluate.
455 * Note that successful completion of the query causes the ACPI_EC_SCI
456 * bit to be cleared (and thus clearing the interrupt source).
459 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
470 /* --------------------------------------------------------------------------
472 -------------------------------------------------------------------------- */
473 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
474 acpi_handle handle, acpi_ec_query_func func,
477 struct acpi_ec_query_handler *handler =
478 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
482 handler->query_bit = query_bit;
483 handler->handle = handle;
484 handler->func = func;
485 handler->data = data;
486 mutex_lock(&ec->lock);
487 list_add(&handler->node, &ec->list);
488 mutex_unlock(&ec->lock);
492 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
494 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
496 struct acpi_ec_query_handler *handler, *tmp;
497 mutex_lock(&ec->lock);
498 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
499 if (query_bit == handler->query_bit) {
500 list_del(&handler->node);
504 mutex_unlock(&ec->lock);
507 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
509 static void acpi_ec_gpe_query(void *ec_cxt)
511 struct acpi_ec *ec = ec_cxt;
513 struct acpi_ec_query_handler *handler, copy;
515 if (!ec || acpi_ec_query(ec, &value))
517 mutex_lock(&ec->lock);
518 list_for_each_entry(handler, &ec->list, node) {
519 if (value == handler->query_bit) {
520 /* have custom handler for this bit */
521 memcpy(©, handler, sizeof(copy));
522 mutex_unlock(&ec->lock);
524 copy.func(copy.data);
525 } else if (copy.handle) {
526 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
531 mutex_unlock(&ec->lock);
534 static u32 acpi_ec_gpe_handler(void *data)
536 struct acpi_ec *ec = data;
539 pr_debug(PREFIX "~~~> interrupt\n");
540 status = acpi_ec_read_status(ec);
542 gpe_transaction(ec, status);
543 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
546 ec_check_sci(ec, status);
547 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
548 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
549 /* this is non-query, must be confirmation */
550 if (printk_ratelimit())
551 pr_info(PREFIX "non-query interrupt received,"
552 " switching to interrupt mode\n");
553 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
555 return ACPI_INTERRUPT_HANDLED;
558 /* --------------------------------------------------------------------------
559 Address Space Management
560 -------------------------------------------------------------------------- */
563 acpi_ec_space_handler(u32 function, acpi_physical_address address,
564 u32 bits, acpi_integer *value,
565 void *handler_context, void *region_context)
567 struct acpi_ec *ec = handler_context;
571 if ((address > 0xFF) || !value || !handler_context)
572 return AE_BAD_PARAMETER;
574 if (function != ACPI_READ && function != ACPI_WRITE)
575 return AE_BAD_PARAMETER;
577 if (bits != 8 && acpi_strict)
578 return AE_BAD_PARAMETER;
580 acpi_ec_burst_enable(ec);
582 if (function == ACPI_READ) {
583 result = acpi_ec_read(ec, address, &temp);
586 temp = 0xff & (*value);
587 result = acpi_ec_write(ec, address, temp);
590 for (i = 8; unlikely(bits - i > 0); i += 8) {
592 if (function == ACPI_READ) {
593 result = acpi_ec_read(ec, address, &temp);
594 (*value) |= ((acpi_integer)temp) << i;
596 temp = 0xff & ((*value) >> i);
597 result = acpi_ec_write(ec, address, temp);
601 acpi_ec_burst_disable(ec);
605 return AE_BAD_PARAMETER;
618 /* --------------------------------------------------------------------------
620 -------------------------------------------------------------------------- */
622 static struct proc_dir_entry *acpi_ec_dir;
624 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
626 struct acpi_ec *ec = seq->private;
631 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
632 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
633 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
634 seq_printf(seq, "use global lock:\t%s\n",
635 ec->global_lock ? "yes" : "no");
640 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
642 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
645 static struct file_operations acpi_ec_info_ops = {
646 .open = acpi_ec_info_open_fs,
649 .release = single_release,
650 .owner = THIS_MODULE,
653 static int acpi_ec_add_fs(struct acpi_device *device)
655 struct proc_dir_entry *entry = NULL;
657 if (!acpi_device_dir(device)) {
658 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
660 if (!acpi_device_dir(device))
664 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
665 acpi_device_dir(device),
666 &acpi_ec_info_ops, acpi_driver_data(device));
672 static int acpi_ec_remove_fs(struct acpi_device *device)
675 if (acpi_device_dir(device)) {
676 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
677 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
678 acpi_device_dir(device) = NULL;
684 /* --------------------------------------------------------------------------
686 -------------------------------------------------------------------------- */
688 ec_parse_io_ports(struct acpi_resource *resource, void *context);
690 static struct acpi_ec *make_acpi_ec(void)
692 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
695 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
696 mutex_init(&ec->lock);
697 init_waitqueue_head(&ec->wait);
698 INIT_LIST_HEAD(&ec->list);
699 spin_lock_init(&ec->t_lock);
704 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
705 void *context, void **return_value)
707 struct acpi_namespace_node *node = handle;
708 struct acpi_ec *ec = context;
710 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
711 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
717 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
721 struct acpi_ec *ec = context;
722 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
723 ec_parse_io_ports, ec);
724 if (ACPI_FAILURE(status))
727 /* Get GPE bit assignment (EC events). */
728 /* TODO: Add support for _GPE returning a package */
729 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
730 if (ACPI_FAILURE(status))
732 /* Use the global lock for all EC transactions? */
733 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
735 return AE_CTRL_TERMINATE;
738 static void ec_remove_handlers(struct acpi_ec *ec)
740 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
741 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
742 pr_err(PREFIX "failed to remove space handler\n");
743 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
744 &acpi_ec_gpe_handler)))
745 pr_err(PREFIX "failed to remove gpe handler\n");
746 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
749 static int acpi_ec_add(struct acpi_device *device)
751 struct acpi_ec *ec = NULL;
755 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
756 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
758 /* Check for boot EC */
760 (boot_ec->handle == device->handle ||
761 boot_ec->handle == ACPI_ROOT_OBJECT)) {
768 if (ec_parse_device(device->handle, 0, ec, NULL) !=
775 ec->handle = device->handle;
777 /* Find and register all query methods */
778 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
779 acpi_ec_register_query_methods, ec, NULL);
783 acpi_driver_data(device) = ec;
784 acpi_ec_add_fs(device);
785 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
786 ec->gpe, ec->command_addr, ec->data_addr);
787 pr_info(PREFIX "driver started in %s mode\n",
788 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
792 static int acpi_ec_remove(struct acpi_device *device, int type)
795 struct acpi_ec_query_handler *handler, *tmp;
800 ec = acpi_driver_data(device);
801 mutex_lock(&ec->lock);
802 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
803 list_del(&handler->node);
806 mutex_unlock(&ec->lock);
807 acpi_ec_remove_fs(device);
808 acpi_driver_data(device) = NULL;
816 ec_parse_io_ports(struct acpi_resource *resource, void *context)
818 struct acpi_ec *ec = context;
820 if (resource->type != ACPI_RESOURCE_TYPE_IO)
824 * The first address region returned is the data port, and
825 * the second address region returned is the status/command
828 if (ec->data_addr == 0)
829 ec->data_addr = resource->data.io.minimum;
830 else if (ec->command_addr == 0)
831 ec->command_addr = resource->data.io.minimum;
833 return AE_CTRL_TERMINATE;
838 static int ec_install_handlers(struct acpi_ec *ec)
841 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
843 status = acpi_install_gpe_handler(NULL, ec->gpe,
844 ACPI_GPE_EDGE_TRIGGERED,
845 &acpi_ec_gpe_handler, ec);
846 if (ACPI_FAILURE(status))
848 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
849 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
850 status = acpi_install_address_space_handler(ec->handle,
852 &acpi_ec_space_handler,
854 if (ACPI_FAILURE(status)) {
855 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
859 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
863 static int acpi_ec_start(struct acpi_device *device)
871 ec = acpi_driver_data(device);
876 ret = ec_install_handlers(ec);
878 /* EC is fully operational, allow queries */
879 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
883 static int acpi_ec_stop(struct acpi_device *device, int type)
888 ec = acpi_driver_data(device);
891 ec_remove_handlers(ec);
896 int __init acpi_boot_ec_enable(void)
898 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
900 if (!ec_install_handlers(boot_ec)) {
907 static const struct acpi_device_id ec_device_ids[] = {
912 int __init acpi_ec_ecdt_probe(void)
916 struct acpi_table_ecdt *ecdt_ptr;
918 boot_ec = make_acpi_ec();
922 * Generate a boot ec context
924 status = acpi_get_table(ACPI_SIG_ECDT, 1,
925 (struct acpi_table_header **)&ecdt_ptr);
926 if (ACPI_SUCCESS(status)) {
927 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
928 boot_ec->command_addr = ecdt_ptr->control.address;
929 boot_ec->data_addr = ecdt_ptr->data.address;
930 if (dmi_check_system(ec_dmi_table)) {
932 * If the board falls into ec_dmi_table, it means
933 * that ECDT table gives the incorrect command/status
934 * & data I/O address. Just fix it.
936 boot_ec->data_addr = ecdt_ptr->control.address;
937 boot_ec->command_addr = ecdt_ptr->data.address;
939 boot_ec->gpe = ecdt_ptr->gpe;
940 boot_ec->handle = ACPI_ROOT_OBJECT;
941 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
943 /* This workaround is needed only on some broken machines,
944 * which require early EC, but fail to provide ECDT */
946 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
947 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
949 /* Check that acpi_get_devices actually find something */
950 if (ACPI_FAILURE(status) || !boot_ec->handle)
952 /* We really need to limit this workaround, the only ASUS,
953 * which needs it, has fake EC._INI method, so use it as flag.
954 * Keep boot_ec struct as it will be needed soon.
956 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
960 ret = ec_install_handlers(boot_ec);
971 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
973 struct acpi_ec *ec = acpi_driver_data(device);
975 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
976 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
977 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
981 static int acpi_ec_resume(struct acpi_device *device)
983 struct acpi_ec *ec = acpi_driver_data(device);
984 /* Enable use of GPE back */
985 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
986 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
990 static struct acpi_driver acpi_ec_driver = {
992 .class = ACPI_EC_CLASS,
993 .ids = ec_device_ids,
996 .remove = acpi_ec_remove,
997 .start = acpi_ec_start,
998 .stop = acpi_ec_stop,
999 .suspend = acpi_ec_suspend,
1000 .resume = acpi_ec_resume,
1004 static int __init acpi_ec_init(void)
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1025 subsys_initcall(acpi_ec_init);
1027 /* EC driver currently not unloadable */
1029 static void __exit acpi_ec_exit(void)
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);