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 static struct acpi_ec {
98 unsigned long command_addr;
99 unsigned long data_addr;
100 unsigned long global_lock;
102 atomic_t query_pending;
103 atomic_t event_count;
104 wait_queue_head_t wait;
107 /* External interfaces use first EC only, so remember */
108 static struct acpi_device *first_ec;
110 /* --------------------------------------------------------------------------
111 Transaction Management
112 -------------------------------------------------------------------------- */
114 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
116 return inb(ec->command_addr);
119 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
121 return inb(ec->data_addr);
124 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
126 outb(command, ec->command_addr);
129 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
131 outb(data, ec->data_addr);
134 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
137 u8 status = acpi_ec_read_status(ec);
138 if (old_count == atomic_read(&ec->event_count))
140 if (event == ACPI_EC_EVENT_OBF_1) {
141 if (status & ACPI_EC_FLAG_OBF)
143 } else if (event == ACPI_EC_EVENT_IBF_0) {
144 if (!(status & ACPI_EC_FLAG_IBF))
151 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
153 if (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)
180 unsigned count = atomic_read(&ec->event_count);
181 acpi_ec_write_cmd(ec, command);
183 for (; wdata_len > 0; --wdata_len) {
184 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
186 printk(KERN_ERR PREFIX
187 "write_cmd timeout, command = %d\n", command);
190 count = atomic_read(&ec->event_count);
191 acpi_ec_write_data(ec, *(wdata++));
195 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
197 printk(KERN_ERR PREFIX
198 "finish-write timeout, command = %d\n", command);
201 } else if (command == ACPI_EC_COMMAND_QUERY) {
202 atomic_set(&ec->query_pending, 0);
205 for (; rdata_len > 0; --rdata_len) {
206 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
208 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
212 count = atomic_read(&ec->event_count);
213 *(rdata++) = acpi_ec_read_data(ec);
219 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
220 const u8 * wdata, unsigned wdata_len,
221 u8 * rdata, unsigned rdata_len)
226 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
230 memset(rdata, 0, rdata_len);
232 mutex_lock(&ec->lock);
233 if (ec->global_lock) {
234 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
235 if (ACPI_FAILURE(status)) {
236 mutex_unlock(&ec->lock);
241 /* Make sure GPE is enabled before doing transaction */
242 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
244 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
246 printk(KERN_DEBUG PREFIX
247 "input buffer is not empty, aborting transaction\n");
251 status = acpi_ec_transaction_unlocked(ec, command,
258 acpi_release_global_lock(glk);
259 mutex_unlock(&ec->lock);
265 * Note: samsung nv5000 doesn't work with ec burst mode.
266 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
268 int acpi_ec_burst_enable(struct acpi_ec *ec)
271 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1);
274 int acpi_ec_burst_disable(struct acpi_ec *ec)
276 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0);
279 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
284 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
290 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
292 u8 wdata[2] = { address, data };
293 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
298 * Externally callable EC access functions. For now, assume 1 EC only
300 int ec_burst_enable(void)
305 ec = acpi_driver_data(first_ec);
306 return acpi_ec_burst_enable(ec);
309 EXPORT_SYMBOL(ec_burst_enable);
311 int ec_burst_disable(void)
316 ec = acpi_driver_data(first_ec);
317 return acpi_ec_burst_disable(ec);
320 EXPORT_SYMBOL(ec_burst_disable);
322 int ec_read(u8 addr, u8 * val)
331 ec = acpi_driver_data(first_ec);
333 err = acpi_ec_read(ec, addr, &temp_data);
342 EXPORT_SYMBOL(ec_read);
344 int ec_write(u8 addr, u8 val)
352 ec = acpi_driver_data(first_ec);
354 err = acpi_ec_write(ec, addr, val);
359 EXPORT_SYMBOL(ec_write);
361 int ec_transaction(u8 command,
362 const u8 * wdata, unsigned wdata_len,
363 u8 * rdata, unsigned rdata_len)
370 ec = acpi_driver_data(first_ec);
372 return acpi_ec_transaction(ec, command, wdata,
373 wdata_len, rdata, rdata_len);
376 EXPORT_SYMBOL(ec_transaction);
378 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
387 * Query the EC to find out which _Qxx method we need to evaluate.
388 * Note that successful completion of the query causes the ACPI_EC_SCI
389 * bit to be cleared (and thus clearing the interrupt source).
392 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
403 /* --------------------------------------------------------------------------
405 -------------------------------------------------------------------------- */
407 static void acpi_ec_gpe_query(void *ec_cxt)
409 struct acpi_ec *ec = ec_cxt;
413 if (!ec || acpi_ec_query(ec, &value))
416 snprintf(object_name, 8, "_Q%2.2X", value);
418 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
420 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
423 static u32 acpi_ec_gpe_handler(void *data)
425 acpi_status status = AE_OK;
427 struct acpi_ec *ec = data;
428 atomic_inc(&ec->event_count);
430 if (acpi_ec_mode == EC_INTR) {
434 value = acpi_ec_read_status(ec);
435 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
436 atomic_set(&ec->query_pending, 1);
438 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
442 return status == AE_OK ?
443 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
446 /* --------------------------------------------------------------------------
447 Address Space Management
448 -------------------------------------------------------------------------- */
451 acpi_ec_space_setup(acpi_handle region_handle,
452 u32 function, void *handler_context, void **return_context)
455 * The EC object is in the handler context and is needed
456 * when calling the acpi_ec_space_handler.
458 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
459 handler_context : NULL;
465 acpi_ec_space_handler(u32 function,
466 acpi_physical_address address,
468 acpi_integer * value,
469 void *handler_context, void *region_context)
472 struct acpi_ec *ec = handler_context;
474 acpi_integer f_v = 0;
477 if ((address > 0xFF) || !value || !handler_context)
478 return AE_BAD_PARAMETER;
480 if (bit_width != 8 && acpi_strict) {
481 return AE_BAD_PARAMETER;
488 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
491 result = acpi_ec_write(ec, (u8) address, (u8) temp);
501 if (function == ACPI_READ)
502 f_v |= temp << 8 * i;
503 if (function == ACPI_WRITE)
510 if (function == ACPI_READ) {
511 f_v |= temp << 8 * i;
518 return AE_BAD_PARAMETER;
531 /* --------------------------------------------------------------------------
533 -------------------------------------------------------------------------- */
535 static struct proc_dir_entry *acpi_ec_dir;
537 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
539 struct acpi_ec *ec = seq->private;
544 seq_printf(seq, "gpe: 0x%02x\n", (u32) ec->gpe);
545 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
546 (u32) ec->command_addr, (u32) ec->data_addr);
547 seq_printf(seq, "use global lock: %s\n",
548 ec->global_lock ? "yes" : "no");
549 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
555 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
557 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
560 static struct file_operations acpi_ec_info_ops = {
561 .open = acpi_ec_info_open_fs,
564 .release = single_release,
565 .owner = THIS_MODULE,
568 static int acpi_ec_add_fs(struct acpi_device *device)
570 struct proc_dir_entry *entry = NULL;
572 if (!acpi_device_dir(device)) {
573 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
575 if (!acpi_device_dir(device))
579 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
580 acpi_device_dir(device));
584 entry->proc_fops = &acpi_ec_info_ops;
585 entry->data = acpi_driver_data(device);
586 entry->owner = THIS_MODULE;
592 static int acpi_ec_remove_fs(struct acpi_device *device)
595 if (acpi_device_dir(device)) {
596 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
597 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
598 acpi_device_dir(device) = NULL;
604 /* --------------------------------------------------------------------------
606 -------------------------------------------------------------------------- */
608 ec_parse_io_ports(struct acpi_resource *resource, void *context);
611 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
613 static struct acpi_ec *make_acpi_ec(void)
615 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
619 atomic_set(&ec->query_pending, 0);
620 atomic_set(&ec->event_count, 1);
621 mutex_init(&ec->lock);
622 init_waitqueue_head(&ec->wait);
627 static int acpi_ec_add(struct acpi_device *device)
629 acpi_status status = AE_OK;
630 struct acpi_ec *ec = NULL;
635 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
636 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
642 status = ec_parse_device(device->handle, 0, ec, NULL);
643 if (status != AE_CTRL_TERMINATE) {
648 /* Check if we found the boot EC */
650 if (ec_ecdt->gpe == ec->gpe) {
651 /* We might have incorrect info for GL at boot time */
652 mutex_lock(&ec_ecdt->lock);
653 ec_ecdt->global_lock = ec->global_lock;
654 mutex_unlock(&ec_ecdt->lock);
660 ec->handle = device->handle;
662 acpi_driver_data(device) = ec;
667 acpi_ec_add_fs(device);
669 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
670 acpi_device_name(device), acpi_device_bid(device),
676 static int acpi_ec_remove(struct acpi_device *device, int type)
678 struct acpi_ec *ec = NULL;
683 ec = acpi_driver_data(device);
685 acpi_ec_remove_fs(device);
687 acpi_driver_data(device) = NULL;
688 if (device == first_ec)
691 /* Don't touch boot EC */
699 ec_parse_io_ports(struct acpi_resource *resource, void *context)
701 struct acpi_ec *ec = context;
703 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
708 * The first address region returned is the data port, and
709 * the second address region returned is the status/command
712 if (ec->data_addr == 0) {
713 ec->data_addr = resource->data.io.minimum;
714 } else if (ec->command_addr == 0) {
715 ec->command_addr = resource->data.io.minimum;
717 return AE_CTRL_TERMINATE;
723 static int ec_install_handlers(struct acpi_ec *ec)
726 status = acpi_install_gpe_handler(NULL, ec->gpe,
727 ACPI_GPE_EDGE_TRIGGERED,
728 &acpi_ec_gpe_handler, ec);
729 if (ACPI_FAILURE(status))
731 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
732 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
734 status = acpi_install_address_space_handler(ec->handle,
736 &acpi_ec_space_handler,
737 &acpi_ec_space_setup, ec);
738 if (ACPI_FAILURE(status)) {
739 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
746 static int acpi_ec_start(struct acpi_device *device)
753 ec = acpi_driver_data(device);
758 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
759 ec->gpe, ec->command_addr, ec->data_addr));
761 /* Boot EC is already working */
765 return ec_install_handlers(ec);
768 static int acpi_ec_stop(struct acpi_device *device, int type)
776 ec = acpi_driver_data(device);
780 /* Don't touch boot EC */
784 status = acpi_remove_address_space_handler(ec->handle,
786 &acpi_ec_space_handler);
787 if (ACPI_FAILURE(status))
790 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
791 if (ACPI_FAILURE(status))
798 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
802 struct acpi_ec *ec = context;
803 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
804 ec_parse_io_ports, ec);
805 if (ACPI_FAILURE(status))
808 /* Get GPE bit assignment (EC events). */
809 /* TODO: Add support for _GPE returning a package */
810 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
811 if (ACPI_FAILURE(status))
814 /* Use the global lock for all EC transactions? */
815 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
819 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
820 ec->gpe, ec->command_addr, ec->data_addr));
822 return AE_CTRL_TERMINATE;
825 int __init acpi_ec_ecdt_probe(void)
829 struct acpi_table_ecdt *ecdt_ptr;
831 ec_ecdt = make_acpi_ec();
835 * Generate a boot ec context
838 status = acpi_get_table(ACPI_SIG_ECDT, 1,
839 (struct acpi_table_header **)&ecdt_ptr);
840 if (ACPI_FAILURE(status))
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
845 ec_ecdt->command_addr = ecdt_ptr->control.address;
846 ec_ecdt->data_addr = ecdt_ptr->data.address;
847 ec_ecdt->gpe = ecdt_ptr->gpe;
848 ec_ecdt->uid = ecdt_ptr->uid;
849 ec_ecdt->handle = ACPI_ROOT_OBJECT;
851 ret = ec_install_handlers(ec_ecdt);
861 static int __init acpi_ec_init(void)
868 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
872 /* Now register the driver for the EC */
873 result = acpi_bus_register_driver(&acpi_ec_driver);
875 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
882 subsys_initcall(acpi_ec_init);
884 /* EC driver currently not unloadable */
886 static void __exit acpi_ec_exit(void)
889 acpi_bus_unregister_driver(&acpi_ec_driver);
891 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
897 static int __init acpi_ec_set_intr_mode(char *str)
901 if (!get_option(&str, &intr))
904 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
906 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
911 __setup("ec_intr=", acpi_ec_set_intr_mode);