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>
37 #include <linux/list.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
45 #define ACPI_EC_FILE_INFO "info"
48 #define PREFIX "ACPI: EC: "
50 /* EC status register */
51 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
53 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
54 #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,
67 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
68 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
71 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
72 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
75 EC_INTR = 1, /* Output buffer full */
76 EC_POLL, /* Input buffer empty */
77 } acpi_ec_mode = EC_INTR;
79 static int acpi_ec_remove(struct acpi_device *device, int type);
80 static int acpi_ec_start(struct acpi_device *device);
81 static int acpi_ec_stop(struct acpi_device *device, int type);
82 static int acpi_ec_add(struct acpi_device *device);
84 static const struct acpi_device_id ec_device_ids[] = {
89 static struct acpi_driver acpi_ec_driver = {
91 .class = ACPI_EC_CLASS,
95 .remove = acpi_ec_remove,
96 .start = acpi_ec_start,
101 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
102 /* External interfaces use first EC only, so remember */
103 typedef int (*acpi_ec_query_func) (void *data);
105 struct acpi_ec_query_handler {
106 struct list_head node;
107 acpi_ec_query_func func;
113 static struct acpi_ec {
116 unsigned long command_addr;
117 unsigned long data_addr;
118 unsigned long global_lock;
120 atomic_t query_pending;
121 atomic_t event_count;
122 wait_queue_head_t wait;
123 struct list_head list;
124 } *boot_ec, *first_ec;
126 /* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
130 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
132 return inb(ec->command_addr);
135 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
137 return inb(ec->data_addr);
140 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
142 outb(command, ec->command_addr);
145 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
147 outb(data, ec->data_addr);
150 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
153 u8 status = acpi_ec_read_status(ec);
154 if (old_count == atomic_read(&ec->event_count))
156 if (event == ACPI_EC_EVENT_OBF_1) {
157 if (status & ACPI_EC_FLAG_OBF)
159 } else if (event == ACPI_EC_EVENT_IBF_0) {
160 if (!(status & ACPI_EC_FLAG_IBF))
167 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
168 unsigned count, int force_poll)
170 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
171 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
172 while (time_before(jiffies, delay)) {
173 if (acpi_ec_check_status(ec, event, 0))
177 if (wait_event_timeout(ec->wait,
178 acpi_ec_check_status(ec, event, count),
179 msecs_to_jiffies(ACPI_EC_DELAY)) ||
180 acpi_ec_check_status(ec, event, 0)) {
183 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
184 " status = %d, expect_event = %d\n",
185 acpi_ec_read_status(ec), event);
192 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
193 const u8 * wdata, unsigned wdata_len,
194 u8 * rdata, unsigned rdata_len,
198 unsigned count = atomic_read(&ec->event_count);
199 acpi_ec_write_cmd(ec, command);
201 for (; wdata_len > 0; --wdata_len) {
202 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
204 printk(KERN_ERR PREFIX
205 "write_cmd timeout, command = %d\n", command);
208 count = atomic_read(&ec->event_count);
209 acpi_ec_write_data(ec, *(wdata++));
213 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
215 printk(KERN_ERR PREFIX
216 "finish-write timeout, command = %d\n", command);
219 } else if (command == ACPI_EC_COMMAND_QUERY) {
220 atomic_set(&ec->query_pending, 0);
223 for (; rdata_len > 0; --rdata_len) {
224 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
226 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
230 count = atomic_read(&ec->event_count);
231 *(rdata++) = acpi_ec_read_data(ec);
237 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
238 const u8 * wdata, unsigned wdata_len,
239 u8 * rdata, unsigned rdata_len,
245 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
249 memset(rdata, 0, rdata_len);
251 mutex_lock(&ec->lock);
252 if (ec->global_lock) {
253 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
254 if (ACPI_FAILURE(status)) {
255 mutex_unlock(&ec->lock);
260 /* Make sure GPE is enabled before doing transaction */
261 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
263 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
265 printk(KERN_ERR PREFIX
266 "input buffer is not empty, aborting transaction\n");
270 status = acpi_ec_transaction_unlocked(ec, command,
278 acpi_release_global_lock(glk);
279 mutex_unlock(&ec->lock);
285 * Note: samsung nv5000 doesn't work with ec burst mode.
286 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
288 int acpi_ec_burst_enable(struct acpi_ec *ec)
291 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
294 int acpi_ec_burst_disable(struct acpi_ec *ec)
296 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
299 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
304 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
305 &address, 1, &d, 1, 0);
310 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
312 u8 wdata[2] = { address, data };
313 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
314 wdata, 2, NULL, 0, 0);
318 * Externally callable EC access functions. For now, assume 1 EC only
320 int ec_burst_enable(void)
324 return acpi_ec_burst_enable(first_ec);
327 EXPORT_SYMBOL(ec_burst_enable);
329 int ec_burst_disable(void)
333 return acpi_ec_burst_disable(first_ec);
336 EXPORT_SYMBOL(ec_burst_disable);
338 int ec_read(u8 addr, u8 * val)
346 err = acpi_ec_read(first_ec, addr, &temp_data);
355 EXPORT_SYMBOL(ec_read);
357 int ec_write(u8 addr, u8 val)
364 err = acpi_ec_write(first_ec, addr, val);
369 EXPORT_SYMBOL(ec_write);
371 int ec_transaction(u8 command,
372 const u8 * wdata, unsigned wdata_len,
373 u8 * rdata, unsigned rdata_len,
379 return acpi_ec_transaction(first_ec, command, wdata,
380 wdata_len, rdata, rdata_len,
384 EXPORT_SYMBOL(ec_transaction);
386 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
395 * Query the EC to find out which _Qxx method we need to evaluate.
396 * Note that successful completion of the query causes the ACPI_EC_SCI
397 * bit to be cleared (and thus clearing the interrupt source).
400 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
411 /* --------------------------------------------------------------------------
413 -------------------------------------------------------------------------- */
414 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
415 acpi_handle handle, acpi_ec_query_func func,
418 struct acpi_ec_query_handler *handler =
419 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
423 handler->query_bit = query_bit;
424 handler->handle = handle;
425 handler->func = func;
426 handler->data = data;
427 mutex_lock(&ec->lock);
428 list_add_tail(&handler->node, &ec->list);
429 mutex_unlock(&ec->lock);
433 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
435 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
437 struct acpi_ec_query_handler *handler;
438 mutex_lock(&ec->lock);
439 list_for_each_entry(handler, &ec->list, node) {
440 if (query_bit == handler->query_bit) {
441 list_del(&handler->node);
446 mutex_unlock(&ec->lock);
449 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
451 static void acpi_ec_gpe_query(void *ec_cxt)
453 struct acpi_ec *ec = ec_cxt;
455 struct acpi_ec_query_handler *handler, copy;
457 if (!ec || acpi_ec_query(ec, &value))
459 mutex_lock(&ec->lock);
460 list_for_each_entry(handler, &ec->list, node) {
461 if (value == handler->query_bit) {
462 /* have custom handler for this bit */
463 memcpy(©, handler, sizeof(copy));
464 mutex_unlock(&ec->lock);
466 copy.func(copy.data);
467 } else if (copy.handle) {
468 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
473 mutex_unlock(&ec->lock);
474 printk(KERN_ERR PREFIX "Handler for query 0x%x is not found!\n", value);
477 static u32 acpi_ec_gpe_handler(void *data)
479 acpi_status status = AE_OK;
481 struct acpi_ec *ec = data;
483 atomic_inc(&ec->event_count);
485 if (acpi_ec_mode == EC_INTR) {
489 value = acpi_ec_read_status(ec);
490 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
491 atomic_set(&ec->query_pending, 1);
493 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
496 return status == AE_OK ?
497 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
500 /* --------------------------------------------------------------------------
501 Address Space Management
502 -------------------------------------------------------------------------- */
505 acpi_ec_space_setup(acpi_handle region_handle,
506 u32 function, void *handler_context, void **return_context)
509 * The EC object is in the handler context and is needed
510 * when calling the acpi_ec_space_handler.
512 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
513 handler_context : NULL;
519 acpi_ec_space_handler(u32 function, acpi_physical_address address,
520 u32 bits, acpi_integer *value,
521 void *handler_context, void *region_context)
523 struct acpi_ec *ec = handler_context;
524 int result = 0, i = 0;
527 if ((address > 0xFF) || !value || !handler_context)
528 return AE_BAD_PARAMETER;
530 if (function != ACPI_READ && function != ACPI_WRITE)
531 return AE_BAD_PARAMETER;
533 if (bits != 8 && acpi_strict)
534 return AE_BAD_PARAMETER;
536 while (bits - i > 0) {
537 if (function == ACPI_READ) {
538 result = acpi_ec_read(ec, address, &temp);
539 (*value) |= ((acpi_integer)temp) << i;
541 temp = 0xff & ((*value) >> i);
542 result = acpi_ec_write(ec, address, temp);
550 return AE_BAD_PARAMETER;
563 /* --------------------------------------------------------------------------
565 -------------------------------------------------------------------------- */
567 static struct proc_dir_entry *acpi_ec_dir;
569 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
571 struct acpi_ec *ec = seq->private;
576 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
577 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
578 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
579 seq_printf(seq, "use global lock:\t%s\n",
580 ec->global_lock ? "yes" : "no");
585 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
587 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
590 static struct file_operations acpi_ec_info_ops = {
591 .open = acpi_ec_info_open_fs,
594 .release = single_release,
595 .owner = THIS_MODULE,
598 static int acpi_ec_add_fs(struct acpi_device *device)
600 struct proc_dir_entry *entry = NULL;
602 if (!acpi_device_dir(device)) {
603 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
605 if (!acpi_device_dir(device))
609 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
610 acpi_device_dir(device));
614 entry->proc_fops = &acpi_ec_info_ops;
615 entry->data = acpi_driver_data(device);
616 entry->owner = THIS_MODULE;
622 static int acpi_ec_remove_fs(struct acpi_device *device)
625 if (acpi_device_dir(device)) {
626 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
627 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
628 acpi_device_dir(device) = NULL;
634 /* --------------------------------------------------------------------------
636 -------------------------------------------------------------------------- */
638 ec_parse_io_ports(struct acpi_resource *resource, void *context);
640 static struct acpi_ec *make_acpi_ec(void)
642 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
646 atomic_set(&ec->query_pending, 1);
647 atomic_set(&ec->event_count, 1);
648 mutex_init(&ec->lock);
649 init_waitqueue_head(&ec->wait);
650 INIT_LIST_HEAD(&ec->list);
656 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
657 void *context, void **return_value)
659 struct acpi_namespace_node *node = handle;
660 struct acpi_ec *ec = context;
662 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
663 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
668 static int ec_parse_device(struct acpi_ec *ec, acpi_handle handle)
670 if (ACPI_FAILURE(acpi_walk_resources(handle, METHOD_NAME__CRS,
671 ec_parse_io_ports, ec)))
674 /* Get GPE bit assignment (EC events). */
675 /* TODO: Add support for _GPE returning a package */
676 if (ACPI_FAILURE(acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe)))
679 /* Use the global lock for all EC transactions? */
680 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
682 /* Find and register all query methods */
683 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
684 acpi_ec_register_query_methods, ec, NULL);
688 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
689 ec->gpe, ec->command_addr, ec->data_addr);
694 static int acpi_ec_add(struct acpi_device *device)
696 struct acpi_ec *ec = NULL;
701 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
702 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
708 if (ec_parse_device(ec, device->handle)) {
713 /* Check if we found the boot EC */
715 if (boot_ec->gpe == ec->gpe) {
716 /* We might have incorrect info for GL at boot time */
717 mutex_lock(&boot_ec->lock);
718 boot_ec->global_lock = ec->global_lock;
719 /* Copy handlers from new ec into boot ec */
720 list_splice(&ec->list, &boot_ec->list);
721 mutex_unlock(&boot_ec->lock);
727 ec->handle = device->handle;
728 acpi_driver_data(device) = ec;
730 acpi_ec_add_fs(device);
734 static int acpi_ec_remove(struct acpi_device *device, int type)
737 struct acpi_ec_query_handler *handler;
742 ec = acpi_driver_data(device);
743 mutex_lock(&ec->lock);
744 list_for_each_entry(handler, &ec->list, node) {
745 list_del(&handler->node);
748 mutex_unlock(&ec->lock);
749 acpi_ec_remove_fs(device);
750 acpi_driver_data(device) = NULL;
754 /* Don't touch boot EC */
761 ec_parse_io_ports(struct acpi_resource *resource, void *context)
763 struct acpi_ec *ec = context;
765 if (resource->type != ACPI_RESOURCE_TYPE_IO)
769 * The first address region returned is the data port, and
770 * the second address region returned is the status/command
773 if (ec->data_addr == 0)
774 ec->data_addr = resource->data.io.minimum;
775 else if (ec->command_addr == 0)
776 ec->command_addr = resource->data.io.minimum;
778 return AE_CTRL_TERMINATE;
783 static int ec_install_handlers(struct acpi_ec *ec)
786 status = acpi_install_gpe_handler(NULL, ec->gpe,
787 ACPI_GPE_EDGE_TRIGGERED,
788 &acpi_ec_gpe_handler, ec);
789 if (ACPI_FAILURE(status))
792 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
793 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
795 status = acpi_install_address_space_handler(ec->handle,
797 &acpi_ec_space_handler,
798 &acpi_ec_space_setup, ec);
799 if (ACPI_FAILURE(status)) {
800 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
807 static int acpi_ec_start(struct acpi_device *device)
815 ec = acpi_driver_data(device);
820 /* Boot EC is already working */
822 ret = ec_install_handlers(ec);
824 /* EC is fully operational, allow queries */
825 atomic_set(&ec->query_pending, 0);
830 static int acpi_ec_stop(struct acpi_device *device, int type)
838 ec = acpi_driver_data(device);
842 /* Don't touch boot EC */
846 status = acpi_remove_address_space_handler(ec->handle,
848 &acpi_ec_space_handler);
849 if (ACPI_FAILURE(status))
852 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
853 if (ACPI_FAILURE(status))
859 int __init acpi_ec_ecdt_probe(void)
863 struct acpi_table_ecdt *ecdt_ptr;
865 boot_ec = make_acpi_ec();
869 * Generate a boot ec context
872 status = acpi_get_table(ACPI_SIG_ECDT, 1,
873 (struct acpi_table_header **)&ecdt_ptr);
874 if (ACPI_FAILURE(status))
877 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
879 boot_ec->command_addr = ecdt_ptr->control.address;
880 boot_ec->data_addr = ecdt_ptr->data.address;
881 boot_ec->gpe = ecdt_ptr->gpe;
882 boot_ec->handle = ACPI_ROOT_OBJECT;
884 ret = ec_install_handlers(boot_ec);
896 static int __init acpi_ec_init(void)
903 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
907 /* Now register the driver for the EC */
908 result = acpi_bus_register_driver(&acpi_ec_driver);
910 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
917 subsys_initcall(acpi_ec_init);
919 /* EC driver currently not unloadable */
921 static void __exit acpi_ec_exit(void)
924 acpi_bus_unregister_driver(&acpi_ec_driver);
926 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
932 static int __init acpi_ec_set_intr_mode(char *str)
936 if (!get_option(&str, &intr))
939 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
941 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
946 __setup("ec_intr=", acpi_ec_set_intr_mode);