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 ACPI_EC_CLASS "embedded_controller"
43 #define ACPI_EC_HID "PNP0C09"
44 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
45 #define ACPI_EC_FILE_INFO "info"
47 #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 */
57 ACPI_EC_COMMAND_READ = 0x80,
58 ACPI_EC_COMMAND_WRITE = 0x81,
59 ACPI_EC_BURST_ENABLE = 0x82,
60 ACPI_EC_BURST_DISABLE = 0x83,
61 ACPI_EC_COMMAND_QUERY = 0x84,
65 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
66 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
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 */
73 EC_INTR = 1, /* Output buffer full */
74 EC_POLL, /* Input buffer empty */
75 } acpi_ec_mode = EC_INTR;
77 static int acpi_ec_remove(struct acpi_device *device, int type);
78 static int acpi_ec_start(struct acpi_device *device);
79 static int acpi_ec_stop(struct acpi_device *device, int type);
80 static int acpi_ec_add(struct acpi_device *device);
82 static struct acpi_driver acpi_ec_driver = {
84 .class = ACPI_EC_CLASS,
88 .remove = acpi_ec_remove,
89 .start = acpi_ec_start,
94 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
95 /* External interfaces use first EC only, so remember */
96 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,
150 unsigned count, int force_poll)
152 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
153 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
154 while (time_before(jiffies, delay)) {
155 if (acpi_ec_check_status(ec, event, 0))
159 if (wait_event_timeout(ec->wait,
160 acpi_ec_check_status(ec, event, count),
161 msecs_to_jiffies(ACPI_EC_DELAY)) ||
162 acpi_ec_check_status(ec, event, 0)) {
165 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
166 " status = %d, expect_event = %d\n",
167 acpi_ec_read_status(ec), event);
174 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
175 const u8 * wdata, unsigned wdata_len,
176 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, force_poll);
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, force_poll);
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, force_poll);
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,
227 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
231 memset(rdata, 0, rdata_len);
233 mutex_lock(&ec->lock);
234 if (ec->global_lock) {
235 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
236 if (ACPI_FAILURE(status)) {
237 mutex_unlock(&ec->lock);
242 /* Make sure GPE is enabled before doing transaction */
243 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
245 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
247 printk(KERN_ERR PREFIX
248 "input buffer is not empty, aborting transaction\n");
252 status = acpi_ec_transaction_unlocked(ec, command,
260 acpi_release_global_lock(glk);
261 mutex_unlock(&ec->lock);
267 * Note: samsung nv5000 doesn't work with ec burst mode.
268 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
270 int acpi_ec_burst_enable(struct acpi_ec *ec)
273 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
276 int acpi_ec_burst_disable(struct acpi_ec *ec)
278 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
281 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
286 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
287 &address, 1, &d, 1, 0);
292 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
294 u8 wdata[2] = { address, data };
295 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
296 wdata, 2, NULL, 0, 0);
300 * Externally callable EC access functions. For now, assume 1 EC only
302 int ec_burst_enable(void)
306 return acpi_ec_burst_enable(first_ec);
309 EXPORT_SYMBOL(ec_burst_enable);
311 int ec_burst_disable(void)
315 return acpi_ec_burst_disable(first_ec);
318 EXPORT_SYMBOL(ec_burst_disable);
320 int ec_read(u8 addr, u8 * val)
328 err = acpi_ec_read(first_ec, addr, &temp_data);
337 EXPORT_SYMBOL(ec_read);
339 int ec_write(u8 addr, u8 val)
346 err = acpi_ec_write(first_ec, addr, val);
351 EXPORT_SYMBOL(ec_write);
353 int ec_transaction(u8 command,
354 const u8 * wdata, unsigned wdata_len,
355 u8 * rdata, unsigned rdata_len,
361 return acpi_ec_transaction(first_ec, command, wdata,
362 wdata_len, rdata, rdata_len,
366 EXPORT_SYMBOL(ec_transaction);
368 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
377 * Query the EC to find out which _Qxx method we need to evaluate.
378 * Note that successful completion of the query causes the ACPI_EC_SCI
379 * bit to be cleared (and thus clearing the interrupt source).
382 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
393 /* --------------------------------------------------------------------------
395 -------------------------------------------------------------------------- */
397 static void acpi_ec_gpe_query(void *ec_cxt)
399 struct acpi_ec *ec = ec_cxt;
403 if (!ec || acpi_ec_query(ec, &value))
406 snprintf(object_name, 8, "_Q%2.2X", value);
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
410 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
413 static u32 acpi_ec_gpe_handler(void *data)
415 acpi_status status = AE_OK;
417 struct acpi_ec *ec = data;
419 atomic_inc(&ec->event_count);
421 if (acpi_ec_mode == EC_INTR) {
425 value = acpi_ec_read_status(ec);
426 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
427 atomic_set(&ec->query_pending, 1);
429 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
433 return status == AE_OK ?
434 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
437 /* --------------------------------------------------------------------------
438 Address Space Management
439 -------------------------------------------------------------------------- */
442 acpi_ec_space_setup(acpi_handle region_handle,
443 u32 function, void *handler_context, void **return_context)
446 * The EC object is in the handler context and is needed
447 * when calling the acpi_ec_space_handler.
449 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
450 handler_context : NULL;
456 acpi_ec_space_handler(u32 function, acpi_physical_address address,
457 u32 bits, acpi_integer *value,
458 void *handler_context, void *region_context)
460 struct acpi_ec *ec = handler_context;
461 int result = 0, i = 0;
464 if ((address > 0xFF) || !value || !handler_context)
465 return AE_BAD_PARAMETER;
467 if (function != ACPI_READ && function != ACPI_WRITE)
468 return AE_BAD_PARAMETER;
470 if (bits != 8 && acpi_strict)
471 return AE_BAD_PARAMETER;
473 while (bits - i > 0) {
474 if (function == ACPI_READ) {
475 result = acpi_ec_read(ec, address, &temp);
476 (*value) |= ((acpi_integer)temp) << i;
478 temp = 0xff & ((*value) >> i);
479 result = acpi_ec_write(ec, address, temp);
487 return AE_BAD_PARAMETER;
500 /* --------------------------------------------------------------------------
502 -------------------------------------------------------------------------- */
504 static struct proc_dir_entry *acpi_ec_dir;
506 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
508 struct acpi_ec *ec = seq->private;
513 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
514 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
515 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
516 seq_printf(seq, "use global lock:\t%s\n",
517 ec->global_lock ? "yes" : "no");
522 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
524 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
527 static struct file_operations acpi_ec_info_ops = {
528 .open = acpi_ec_info_open_fs,
531 .release = single_release,
532 .owner = THIS_MODULE,
535 static int acpi_ec_add_fs(struct acpi_device *device)
537 struct proc_dir_entry *entry = NULL;
539 if (!acpi_device_dir(device)) {
540 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
542 if (!acpi_device_dir(device))
546 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
547 acpi_device_dir(device));
551 entry->proc_fops = &acpi_ec_info_ops;
552 entry->data = acpi_driver_data(device);
553 entry->owner = THIS_MODULE;
559 static int acpi_ec_remove_fs(struct acpi_device *device)
562 if (acpi_device_dir(device)) {
563 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
564 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
565 acpi_device_dir(device) = NULL;
571 /* --------------------------------------------------------------------------
573 -------------------------------------------------------------------------- */
575 ec_parse_io_ports(struct acpi_resource *resource, void *context);
578 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
580 static struct acpi_ec *make_acpi_ec(void)
582 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
586 atomic_set(&ec->query_pending, 1);
587 atomic_set(&ec->event_count, 1);
588 mutex_init(&ec->lock);
589 init_waitqueue_head(&ec->wait);
594 static int acpi_ec_add(struct acpi_device *device)
596 acpi_status status = AE_OK;
597 struct acpi_ec *ec = NULL;
602 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
603 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
609 status = ec_parse_device(device->handle, 0, ec, NULL);
610 if (status != AE_CTRL_TERMINATE) {
615 /* Check if we found the boot EC */
617 if (boot_ec->gpe == ec->gpe) {
618 /* We might have incorrect info for GL at boot time */
619 mutex_lock(&boot_ec->lock);
620 boot_ec->global_lock = ec->global_lock;
621 mutex_unlock(&boot_ec->lock);
627 ec->handle = device->handle;
628 acpi_driver_data(device) = ec;
630 acpi_ec_add_fs(device);
635 static int acpi_ec_remove(struct acpi_device *device, int type)
642 ec = acpi_driver_data(device);
643 acpi_ec_remove_fs(device);
644 acpi_driver_data(device) = NULL;
648 /* Don't touch boot EC */
655 ec_parse_io_ports(struct acpi_resource *resource, void *context)
657 struct acpi_ec *ec = context;
659 if (resource->type != ACPI_RESOURCE_TYPE_IO)
663 * The first address region returned is the data port, and
664 * the second address region returned is the status/command
667 if (ec->data_addr == 0)
668 ec->data_addr = resource->data.io.minimum;
669 else if (ec->command_addr == 0)
670 ec->command_addr = resource->data.io.minimum;
672 return AE_CTRL_TERMINATE;
677 static int ec_install_handlers(struct acpi_ec *ec)
680 status = acpi_install_gpe_handler(NULL, ec->gpe,
681 ACPI_GPE_EDGE_TRIGGERED,
682 &acpi_ec_gpe_handler, ec);
683 if (ACPI_FAILURE(status))
686 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
687 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
689 status = acpi_install_address_space_handler(ec->handle,
691 &acpi_ec_space_handler,
692 &acpi_ec_space_setup, ec);
693 if (ACPI_FAILURE(status)) {
694 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
698 /* EC is fully operational, allow queries */
699 atomic_set(&ec->query_pending, 0);
704 static int acpi_ec_start(struct acpi_device *device)
711 ec = acpi_driver_data(device);
716 /* Boot EC is already working */
720 return ec_install_handlers(ec);
723 static int acpi_ec_stop(struct acpi_device *device, int type)
731 ec = acpi_driver_data(device);
735 /* Don't touch boot EC */
739 status = acpi_remove_address_space_handler(ec->handle,
741 &acpi_ec_space_handler);
742 if (ACPI_FAILURE(status))
745 status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
746 if (ACPI_FAILURE(status))
753 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
757 struct acpi_ec *ec = context;
758 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
759 ec_parse_io_ports, ec);
760 if (ACPI_FAILURE(status))
763 /* Get GPE bit assignment (EC events). */
764 /* TODO: Add support for _GPE returning a package */
765 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
766 if (ACPI_FAILURE(status))
769 /* Use the global lock for all EC transactions? */
770 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
774 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
775 ec->gpe, ec->command_addr, ec->data_addr);
777 return AE_CTRL_TERMINATE;
780 int __init acpi_ec_ecdt_probe(void)
784 struct acpi_table_ecdt *ecdt_ptr;
786 boot_ec = make_acpi_ec();
790 * Generate a boot ec context
793 status = acpi_get_table(ACPI_SIG_ECDT, 1,
794 (struct acpi_table_header **)&ecdt_ptr);
795 if (ACPI_FAILURE(status))
798 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
800 boot_ec->command_addr = ecdt_ptr->control.address;
801 boot_ec->data_addr = ecdt_ptr->data.address;
802 boot_ec->gpe = ecdt_ptr->gpe;
803 boot_ec->handle = ACPI_ROOT_OBJECT;
805 ret = ec_install_handlers(boot_ec);
817 static int __init acpi_ec_init(void)
824 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
828 /* Now register the driver for the EC */
829 result = acpi_bus_register_driver(&acpi_ec_driver);
831 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
838 subsys_initcall(acpi_ec_init);
840 /* EC driver currently not unloadable */
842 static void __exit acpi_ec_exit(void)
845 acpi_bus_unregister_driver(&acpi_ec_driver);
847 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
853 static int __init acpi_ec_set_intr_mode(char *str)
857 if (!get_option(&str, &intr))
860 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
862 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
867 __setup("ec_intr=", acpi_ec_set_intr_mode);