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("acpi_ec")
42 #define ACPI_EC_COMPONENT 0x00100000
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_HID "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
47 #define ACPI_EC_FILE_INFO "info"
48 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
49 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
50 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
51 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
52 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
53 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
54 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
55 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
56 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
57 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
58 #define ACPI_EC_COMMAND_READ 0x80
59 #define ACPI_EC_COMMAND_WRITE 0x81
60 #define ACPI_EC_BURST_ENABLE 0x82
61 #define ACPI_EC_BURST_DISABLE 0x83
62 #define ACPI_EC_COMMAND_QUERY 0x84
65 static int acpi_ec_remove(struct acpi_device *device, int type);
66 static int acpi_ec_start(struct acpi_device *device);
67 static int acpi_ec_stop(struct acpi_device *device, int type);
68 static int acpi_ec_intr_add(struct acpi_device *device);
69 static int acpi_ec_poll_add(struct acpi_device *device);
71 static struct acpi_driver acpi_ec_driver = {
72 .name = ACPI_EC_DRIVER_NAME,
73 .class = ACPI_EC_CLASS,
76 .add = acpi_ec_intr_add,
77 .remove = acpi_ec_remove,
78 .start = acpi_ec_start,
87 unsigned long gpe_bit;
88 struct acpi_generic_address status_addr;
89 struct acpi_generic_address command_addr;
90 struct acpi_generic_address data_addr;
91 unsigned long global_lock;
98 unsigned long gpe_bit;
99 struct acpi_generic_address status_addr;
100 struct acpi_generic_address command_addr;
101 struct acpi_generic_address data_addr;
102 unsigned long global_lock;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 atomic_t pending_gpe;
106 struct semaphore sem;
107 wait_queue_head_t wait;
114 unsigned long gpe_bit;
115 struct acpi_generic_address status_addr;
116 struct acpi_generic_address command_addr;
117 struct acpi_generic_address data_addr;
118 unsigned long global_lock;
123 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131 static void acpi_ec_gpe_poll_query(void *ec_cxt);
132 static void acpi_ec_gpe_intr_query(void *ec_cxt);
133 static u32 acpi_ec_gpe_poll_handler(void *data);
134 static u32 acpi_ec_gpe_intr_handler(void *data);
135 static acpi_status __init
136 acpi_fake_ecdt_poll_callback(acpi_handle handle,
137 u32 Level, void *context, void **retval);
139 static acpi_status __init
140 acpi_fake_ecdt_intr_callback(acpi_handle handle,
141 u32 Level, void *context, void **retval);
143 static int __init acpi_ec_poll_get_real_ecdt(void);
144 static int __init acpi_ec_intr_get_real_ecdt(void);
145 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
146 static union acpi_ec *ec_ecdt;
148 /* External interfaces use first EC only, so remember */
149 static struct acpi_device *first_ec;
150 static int acpi_ec_poll_mode = EC_INTR;
152 /* --------------------------------------------------------------------------
153 Transaction Management
154 -------------------------------------------------------------------------- */
156 static u32 acpi_ec_read_status(union acpi_ec *ec)
160 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
164 static int acpi_ec_wait(union acpi_ec *ec, u8 event)
166 if (acpi_ec_poll_mode)
167 return acpi_ec_poll_wait(ec, event);
169 return acpi_ec_intr_wait(ec, event);
172 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
174 u32 acpi_ec_status = 0;
175 u32 i = ACPI_EC_UDELAY_COUNT;
180 /* Poll the EC status register waiting for the event to occur. */
182 case ACPI_EC_EVENT_OBF:
184 acpi_hw_low_level_read(8, &acpi_ec_status,
185 &ec->common.status_addr);
186 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
188 udelay(ACPI_EC_UDELAY);
191 case ACPI_EC_EVENT_IBE:
193 acpi_hw_low_level_read(8, &acpi_ec_status,
194 &ec->common.status_addr);
195 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
197 udelay(ACPI_EC_UDELAY);
206 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
210 ACPI_FUNCTION_TRACE("acpi_ec_wait");
212 ec->intr.expect_event = event;
216 case ACPI_EC_EVENT_IBE:
217 if (~acpi_ec_read_status(ec) & event) {
218 ec->intr.expect_event = 0;
226 result = wait_event_timeout(ec->intr.wait,
227 !ec->intr.expect_event,
228 msecs_to_jiffies(ACPI_EC_DELAY));
230 ec->intr.expect_event = 0;
234 * Verify that the event in question has actually happened by
235 * querying EC status. Do the check even if operation timed-out
236 * to make sure that we did not miss interrupt.
239 case ACPI_EC_EVENT_OBF:
240 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
244 case ACPI_EC_EVENT_IBE:
245 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
250 return_VALUE(-ETIME);
253 #ifdef ACPI_FUTURE_USAGE
255 * Note: samsung nv5000 doesn't work with ec burst mode.
256 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
258 int acpi_ec_enter_burst_mode(union acpi_ec *ec)
263 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
265 status = acpi_ec_read_status(ec);
266 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
270 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
271 &ec->common.command_addr);
272 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
273 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
274 if (tmp != 0x90) { /* Burst ACK byte */
275 return_VALUE(-EINVAL);
279 atomic_set(&ec->intr.leaving_burst, 0);
282 printk(KERN_WARNING PREFIX "Error in acpi_ec_wait\n");
286 int acpi_ec_leave_burst_mode(union acpi_ec *ec)
290 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
292 status = acpi_ec_read_status(ec);
293 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
294 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
297 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
298 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
300 atomic_set(&ec->intr.leaving_burst, 1);
303 printk(KERN_WARNING PREFIX "leave burst_mode:error\n");
306 #endif /* ACPI_FUTURE_USAGE */
308 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
310 if (acpi_ec_poll_mode)
311 return acpi_ec_poll_read(ec, address, data);
313 return acpi_ec_intr_read(ec, address, data);
315 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
317 if (acpi_ec_poll_mode)
318 return acpi_ec_poll_write(ec, address, data);
320 return acpi_ec_intr_write(ec, address, data);
322 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
324 acpi_status status = AE_OK;
326 unsigned long flags = 0;
329 ACPI_FUNCTION_TRACE("acpi_ec_read");
332 return_VALUE(-EINVAL);
336 if (ec->common.global_lock) {
337 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
338 if (ACPI_FAILURE(status))
339 return_VALUE(-ENODEV);
342 spin_lock_irqsave(&ec->poll.lock, flags);
344 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
345 &ec->common.command_addr);
346 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
350 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
351 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
355 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
357 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
361 spin_unlock_irqrestore(&ec->poll.lock, flags);
363 if (ec->common.global_lock)
364 acpi_release_global_lock(glk);
366 return_VALUE(result);
369 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
372 acpi_status status = AE_OK;
373 unsigned long flags = 0;
376 ACPI_FUNCTION_TRACE("acpi_ec_write");
379 return_VALUE(-EINVAL);
381 if (ec->common.global_lock) {
382 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
383 if (ACPI_FAILURE(status))
384 return_VALUE(-ENODEV);
387 spin_lock_irqsave(&ec->poll.lock, flags);
389 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
390 &ec->common.command_addr);
391 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
395 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
396 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
400 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
401 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
409 spin_unlock_irqrestore(&ec->poll.lock, flags);
411 if (ec->common.global_lock)
412 acpi_release_global_lock(glk);
414 return_VALUE(result);
417 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
422 ACPI_FUNCTION_TRACE("acpi_ec_read");
425 return_VALUE(-EINVAL);
429 if (ec->common.global_lock) {
430 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
431 if (ACPI_FAILURE(status))
432 return_VALUE(-ENODEV);
435 WARN_ON(in_interrupt());
438 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
440 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
443 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
444 &ec->common.command_addr);
445 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
447 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
450 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
451 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
453 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
456 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
457 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
463 if (ec->common.global_lock)
464 acpi_release_global_lock(glk);
466 return_VALUE(status);
469 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
474 ACPI_FUNCTION_TRACE("acpi_ec_write");
477 return_VALUE(-EINVAL);
479 if (ec->common.global_lock) {
480 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
481 if (ACPI_FAILURE(status))
482 return_VALUE(-ENODEV);
485 WARN_ON(in_interrupt());
488 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
490 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
492 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
493 &ec->common.command_addr);
494 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
496 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
499 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
500 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
502 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
505 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
512 if (ec->common.global_lock)
513 acpi_release_global_lock(glk);
515 return_VALUE(status);
519 * Externally callable EC access functions. For now, assume 1 EC only
521 int ec_read(u8 addr, u8 * val)
530 ec = acpi_driver_data(first_ec);
532 err = acpi_ec_read(ec, addr, &temp_data);
541 EXPORT_SYMBOL(ec_read);
543 int ec_write(u8 addr, u8 val)
551 ec = acpi_driver_data(first_ec);
553 err = acpi_ec_write(ec, addr, val);
558 EXPORT_SYMBOL(ec_write);
560 static int acpi_ec_query(union acpi_ec *ec, u32 * data)
562 if (acpi_ec_poll_mode)
563 return acpi_ec_poll_query(ec, data);
565 return acpi_ec_intr_query(ec, data);
567 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
570 acpi_status status = AE_OK;
571 unsigned long flags = 0;
574 ACPI_FUNCTION_TRACE("acpi_ec_query");
577 return_VALUE(-EINVAL);
581 if (ec->common.global_lock) {
582 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
583 if (ACPI_FAILURE(status))
584 return_VALUE(-ENODEV);
588 * Query the EC to find out which _Qxx method we need to evaluate.
589 * Note that successful completion of the query causes the ACPI_EC_SCI
590 * bit to be cleared (and thus clearing the interrupt source).
592 spin_lock_irqsave(&ec->poll.lock, flags);
594 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
595 &ec->common.command_addr);
596 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
600 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
605 spin_unlock_irqrestore(&ec->poll.lock, flags);
607 if (ec->common.global_lock)
608 acpi_release_global_lock(glk);
610 return_VALUE(result);
612 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
617 ACPI_FUNCTION_TRACE("acpi_ec_query");
620 return_VALUE(-EINVAL);
623 if (ec->common.global_lock) {
624 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
625 if (ACPI_FAILURE(status))
626 return_VALUE(-ENODEV);
631 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
633 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
637 * Query the EC to find out which _Qxx method we need to evaluate.
638 * Note that successful completion of the query causes the ACPI_EC_SCI
639 * bit to be cleared (and thus clearing the interrupt source).
641 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
642 &ec->common.command_addr);
643 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
645 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
649 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
656 if (ec->common.global_lock)
657 acpi_release_global_lock(glk);
659 return_VALUE(status);
662 /* --------------------------------------------------------------------------
664 -------------------------------------------------------------------------- */
666 union acpi_ec_query_data {
671 static void acpi_ec_gpe_query(void *ec_cxt)
673 if (acpi_ec_poll_mode)
674 acpi_ec_gpe_poll_query(ec_cxt);
676 acpi_ec_gpe_intr_query(ec_cxt);
679 static void acpi_ec_gpe_poll_query(void *ec_cxt)
681 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
683 unsigned long flags = 0;
684 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
685 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
686 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
689 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
694 spin_lock_irqsave(&ec->poll.lock, flags);
695 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
696 spin_unlock_irqrestore(&ec->poll.lock, flags);
698 /* TBD: Implement asynch events!
699 * NOTE: All we care about are EC-SCI's. Other EC events are
700 * handled via polling (yuck!). This is because some systems
701 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
702 * a purely interrupt-driven approach (grumble, grumble).
704 if (!(value & ACPI_EC_FLAG_SCI))
707 if (acpi_ec_query(ec, &value))
710 object_name[2] = hex[((value >> 4) & 0x0F)];
711 object_name[3] = hex[(value & 0x0F)];
713 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
715 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
718 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
720 static void acpi_ec_gpe_intr_query(void *ec_cxt)
722 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
724 int result = -ENODATA;
725 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
726 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
727 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
730 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
732 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
733 result = acpi_ec_query(ec, &value);
738 object_name[2] = hex[((value >> 4) & 0x0F)];
739 object_name[3] = hex[(value & 0x0F)];
741 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
743 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
745 atomic_dec(&ec->intr.pending_gpe);
749 static u32 acpi_ec_gpe_handler(void *data)
751 if (acpi_ec_poll_mode)
752 return acpi_ec_gpe_poll_handler(data);
754 return acpi_ec_gpe_intr_handler(data);
756 static u32 acpi_ec_gpe_poll_handler(void *data)
758 acpi_status status = AE_OK;
759 union acpi_ec *ec = (union acpi_ec *)data;
762 return ACPI_INTERRUPT_NOT_HANDLED;
764 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
766 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
769 return ACPI_INTERRUPT_HANDLED;
771 return ACPI_INTERRUPT_NOT_HANDLED;
773 static u32 acpi_ec_gpe_intr_handler(void *data)
775 acpi_status status = AE_OK;
777 union acpi_ec *ec = (union acpi_ec *)data;
780 return ACPI_INTERRUPT_NOT_HANDLED;
782 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
783 value = acpi_ec_read_status(ec);
785 switch (ec->intr.expect_event) {
786 case ACPI_EC_EVENT_OBF:
787 if (!(value & ACPI_EC_FLAG_OBF))
789 case ACPI_EC_EVENT_IBE:
790 if ((value & ACPI_EC_FLAG_IBF))
792 ec->intr.expect_event = 0;
793 wake_up(&ec->intr.wait);
794 return ACPI_INTERRUPT_HANDLED;
799 if (value & ACPI_EC_FLAG_SCI) {
800 atomic_add(1, &ec->intr.pending_gpe);
801 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
802 acpi_ec_gpe_query, ec);
803 return status == AE_OK ?
804 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
806 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
807 return status == AE_OK ?
808 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
811 /* --------------------------------------------------------------------------
812 Address Space Management
813 -------------------------------------------------------------------------- */
816 acpi_ec_space_setup(acpi_handle region_handle,
817 u32 function, void *handler_context, void **return_context)
820 * The EC object is in the handler context and is needed
821 * when calling the acpi_ec_space_handler.
823 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
824 handler_context : NULL;
830 acpi_ec_space_handler(u32 function,
831 acpi_physical_address address,
833 acpi_integer * value,
834 void *handler_context, void *region_context)
837 union acpi_ec *ec = NULL;
839 acpi_integer f_v = 0;
842 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
844 if ((address > 0xFF) || !value || !handler_context)
845 return_VALUE(AE_BAD_PARAMETER);
847 if (bit_width != 8 && acpi_strict) {
848 printk(KERN_WARNING PREFIX
849 "acpi_ec_space_handler: bit_width should be 8\n");
850 return_VALUE(AE_BAD_PARAMETER);
853 ec = (union acpi_ec *)handler_context;
859 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
862 result = acpi_ec_write(ec, (u8) address, (u8) temp);
872 if (function == ACPI_READ)
873 f_v |= temp << 8 * i;
874 if (function == ACPI_WRITE)
881 if (function == ACPI_READ) {
882 f_v |= temp << 8 * i;
889 return_VALUE(AE_BAD_PARAMETER);
892 return_VALUE(AE_NOT_FOUND);
895 return_VALUE(AE_TIME);
902 /* --------------------------------------------------------------------------
904 -------------------------------------------------------------------------- */
906 static struct proc_dir_entry *acpi_ec_dir;
908 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
910 union acpi_ec *ec = (union acpi_ec *)seq->private;
912 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
917 seq_printf(seq, "gpe bit: 0x%02x\n",
918 (u32) ec->common.gpe_bit);
919 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
920 (u32) ec->common.status_addr.address,
921 (u32) ec->common.data_addr.address);
922 seq_printf(seq, "use global lock: %s\n",
923 ec->common.global_lock ? "yes" : "no");
924 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
930 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
932 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
935 static struct file_operations acpi_ec_info_ops = {
936 .open = acpi_ec_info_open_fs,
939 .release = single_release,
940 .owner = THIS_MODULE,
943 static int acpi_ec_add_fs(struct acpi_device *device)
945 struct proc_dir_entry *entry = NULL;
947 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
949 if (!acpi_device_dir(device)) {
950 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
952 if (!acpi_device_dir(device))
953 return_VALUE(-ENODEV);
956 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
957 acpi_device_dir(device));
959 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
960 "Unable to create '%s' fs entry\n",
963 entry->proc_fops = &acpi_ec_info_ops;
964 entry->data = acpi_driver_data(device);
965 entry->owner = THIS_MODULE;
971 static int acpi_ec_remove_fs(struct acpi_device *device)
973 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
975 if (acpi_device_dir(device)) {
976 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
977 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
978 acpi_device_dir(device) = NULL;
984 /* --------------------------------------------------------------------------
986 -------------------------------------------------------------------------- */
988 static int acpi_ec_poll_add(struct acpi_device *device)
991 acpi_status status = AE_OK;
992 union acpi_ec *ec = NULL;
995 ACPI_FUNCTION_TRACE("acpi_ec_add");
998 return_VALUE(-EINVAL);
1000 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1002 return_VALUE(-ENOMEM);
1003 memset(ec, 0, sizeof(union acpi_ec));
1005 ec->common.handle = device->handle;
1006 ec->common.uid = -1;
1007 spin_lock_init(&ec->poll.lock);
1008 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1009 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1010 acpi_driver_data(device) = ec;
1012 /* Use the global lock for all EC transactions? */
1013 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1014 &ec->common.global_lock);
1016 /* If our UID matches the UID for the ECDT-enumerated EC,
1017 we now have the *real* EC info, so kill the makeshift one. */
1018 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1019 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1020 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1022 &acpi_ec_space_handler);
1024 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1025 &acpi_ec_gpe_handler);
1030 /* Get GPE bit assignment (EC events). */
1031 /* TODO: Add support for _GPE returning a package */
1033 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1034 &ec->common.gpe_bit);
1035 if (ACPI_FAILURE(status)) {
1036 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1037 "Error obtaining GPE bit assignment\n"));
1042 result = acpi_ec_add_fs(device);
1046 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
1047 acpi_device_name(device), acpi_device_bid(device),
1048 (u32) ec->common.gpe_bit);
1057 return_VALUE(result);
1059 static int acpi_ec_intr_add(struct acpi_device *device)
1062 acpi_status status = AE_OK;
1063 union acpi_ec *ec = NULL;
1066 ACPI_FUNCTION_TRACE("acpi_ec_add");
1069 return_VALUE(-EINVAL);
1071 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1073 return_VALUE(-ENOMEM);
1074 memset(ec, 0, sizeof(union acpi_ec));
1076 ec->common.handle = device->handle;
1077 ec->common.uid = -1;
1078 atomic_set(&ec->intr.pending_gpe, 0);
1079 atomic_set(&ec->intr.leaving_burst, 1);
1080 init_MUTEX(&ec->intr.sem);
1081 init_waitqueue_head(&ec->intr.wait);
1082 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1083 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1084 acpi_driver_data(device) = ec;
1086 /* Use the global lock for all EC transactions? */
1087 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1088 &ec->common.global_lock);
1090 /* If our UID matches the UID for the ECDT-enumerated EC,
1091 we now have the *real* EC info, so kill the makeshift one. */
1092 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1093 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1094 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1096 &acpi_ec_space_handler);
1098 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1099 &acpi_ec_gpe_handler);
1104 /* Get GPE bit assignment (EC events). */
1105 /* TODO: Add support for _GPE returning a package */
1107 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1108 &ec->common.gpe_bit);
1109 if (ACPI_FAILURE(status)) {
1110 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1111 "Error obtaining GPE bit assignment\n"));
1116 result = acpi_ec_add_fs(device);
1120 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
1121 acpi_device_name(device), acpi_device_bid(device),
1122 (u32) ec->common.gpe_bit);
1131 return_VALUE(result);
1134 static int acpi_ec_remove(struct acpi_device *device, int type)
1136 union acpi_ec *ec = NULL;
1138 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1141 return_VALUE(-EINVAL);
1143 ec = acpi_driver_data(device);
1145 acpi_ec_remove_fs(device);
1153 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1155 union acpi_ec *ec = (union acpi_ec *)context;
1156 struct acpi_generic_address *addr;
1158 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1163 * The first address region returned is the data port, and
1164 * the second address region returned is the status/command
1167 if (ec->common.data_addr.register_bit_width == 0) {
1168 addr = &ec->common.data_addr;
1169 } else if (ec->common.command_addr.register_bit_width == 0) {
1170 addr = &ec->common.command_addr;
1172 return AE_CTRL_TERMINATE;
1175 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1176 addr->register_bit_width = 8;
1177 addr->register_bit_offset = 0;
1178 addr->address = resource->data.io.minimum;
1183 static int acpi_ec_start(struct acpi_device *device)
1185 acpi_status status = AE_OK;
1186 union acpi_ec *ec = NULL;
1188 ACPI_FUNCTION_TRACE("acpi_ec_start");
1191 return_VALUE(-EINVAL);
1193 ec = acpi_driver_data(device);
1196 return_VALUE(-EINVAL);
1199 * Get I/O port addresses. Convert to GAS format.
1201 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1202 acpi_ec_io_ports, ec);
1203 if (ACPI_FAILURE(status)
1204 || ec->common.command_addr.register_bit_width == 0) {
1205 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1206 "Error getting I/O port addresses"));
1207 return_VALUE(-ENODEV);
1210 ec->common.status_addr = ec->common.command_addr;
1212 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1213 (u32) ec->common.gpe_bit,
1214 (u32) ec->common.command_addr.address,
1215 (u32) ec->common.data_addr.address));
1218 * Install GPE handler
1220 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1221 ACPI_GPE_EDGE_TRIGGERED,
1222 &acpi_ec_gpe_handler, ec);
1223 if (ACPI_FAILURE(status)) {
1224 return_VALUE(-ENODEV);
1226 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1227 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1229 status = acpi_install_address_space_handler(ec->common.handle,
1231 &acpi_ec_space_handler,
1232 &acpi_ec_space_setup, ec);
1233 if (ACPI_FAILURE(status)) {
1234 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1235 &acpi_ec_gpe_handler);
1236 return_VALUE(-ENODEV);
1239 return_VALUE(AE_OK);
1242 static int acpi_ec_stop(struct acpi_device *device, int type)
1244 acpi_status status = AE_OK;
1245 union acpi_ec *ec = NULL;
1247 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1250 return_VALUE(-EINVAL);
1252 ec = acpi_driver_data(device);
1254 status = acpi_remove_address_space_handler(ec->common.handle,
1256 &acpi_ec_space_handler);
1257 if (ACPI_FAILURE(status))
1258 return_VALUE(-ENODEV);
1261 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1262 &acpi_ec_gpe_handler);
1263 if (ACPI_FAILURE(status))
1264 return_VALUE(-ENODEV);
1269 static acpi_status __init
1270 acpi_fake_ecdt_callback(acpi_handle handle,
1271 u32 Level, void *context, void **retval)
1274 if (acpi_ec_poll_mode)
1275 return acpi_fake_ecdt_poll_callback(handle,
1276 Level, context, retval);
1278 return acpi_fake_ecdt_intr_callback(handle,
1279 Level, context, retval);
1282 static acpi_status __init
1283 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1284 u32 Level, void *context, void **retval)
1288 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1289 acpi_ec_io_ports, ec_ecdt);
1290 if (ACPI_FAILURE(status))
1292 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1294 ec_ecdt->common.uid = -1;
1295 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1298 acpi_evaluate_integer(handle, "_GPE", NULL,
1299 &ec_ecdt->common.gpe_bit);
1300 if (ACPI_FAILURE(status))
1302 spin_lock_init(&ec_ecdt->poll.lock);
1303 ec_ecdt->common.global_lock = TRUE;
1304 ec_ecdt->common.handle = handle;
1306 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1307 (u32) ec_ecdt->common.gpe_bit,
1308 (u32) ec_ecdt->common.command_addr.address,
1309 (u32) ec_ecdt->common.data_addr.address);
1311 return AE_CTRL_TERMINATE;
1314 static acpi_status __init
1315 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1316 u32 Level, void *context, void **retval)
1320 init_MUTEX(&ec_ecdt->intr.sem);
1321 init_waitqueue_head(&ec_ecdt->intr.wait);
1322 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1323 acpi_ec_io_ports, ec_ecdt);
1324 if (ACPI_FAILURE(status))
1326 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1328 ec_ecdt->common.uid = -1;
1329 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1332 acpi_evaluate_integer(handle, "_GPE", NULL,
1333 &ec_ecdt->common.gpe_bit);
1334 if (ACPI_FAILURE(status))
1336 ec_ecdt->common.global_lock = TRUE;
1337 ec_ecdt->common.handle = handle;
1339 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1340 (u32) ec_ecdt->common.gpe_bit,
1341 (u32) ec_ecdt->common.command_addr.address,
1342 (u32) ec_ecdt->common.data_addr.address);
1344 return AE_CTRL_TERMINATE;
1348 * Some BIOS (such as some from Gateway laptops) access EC region very early
1349 * such as in BAT0._INI or EC._INI before an EC device is found and
1350 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1351 * required, but if EC regison is accessed early, it is required.
1352 * The routine tries to workaround the BIOS bug by pre-scan EC device
1353 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1354 * op region (since _REG isn't invoked yet). The assumption is true for
1355 * all systems found.
1357 static int __init acpi_ec_fake_ecdt(void)
1362 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1364 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1369 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1371 status = acpi_get_devices(ACPI_EC_HID,
1372 acpi_fake_ecdt_callback, NULL, NULL);
1373 if (ACPI_FAILURE(status)) {
1381 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1385 static int __init acpi_ec_get_real_ecdt(void)
1387 if (acpi_ec_poll_mode)
1388 return acpi_ec_poll_get_real_ecdt();
1390 return acpi_ec_intr_get_real_ecdt();
1393 static int __init acpi_ec_poll_get_real_ecdt(void)
1396 struct acpi_table_ecdt *ecdt_ptr;
1398 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1399 (struct acpi_table_header **)
1401 if (ACPI_FAILURE(status))
1404 printk(KERN_INFO PREFIX "Found ECDT\n");
1407 * Generate a temporary ec context to use until the namespace is scanned
1409 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1412 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1414 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1415 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1416 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1417 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1418 spin_lock_init(&ec_ecdt->poll.lock);
1419 /* use the GL just to be safe */
1420 ec_ecdt->common.global_lock = TRUE;
1421 ec_ecdt->common.uid = ecdt_ptr->uid;
1424 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1425 if (ACPI_FAILURE(status)) {
1431 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1438 static int __init acpi_ec_intr_get_real_ecdt(void)
1441 struct acpi_table_ecdt *ecdt_ptr;
1443 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1444 (struct acpi_table_header **)
1446 if (ACPI_FAILURE(status))
1449 printk(KERN_INFO PREFIX "Found ECDT\n");
1452 * Generate a temporary ec context to use until the namespace is scanned
1454 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1457 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1459 init_MUTEX(&ec_ecdt->intr.sem);
1460 init_waitqueue_head(&ec_ecdt->intr.wait);
1461 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1462 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1463 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1464 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1465 /* use the GL just to be safe */
1466 ec_ecdt->common.global_lock = TRUE;
1467 ec_ecdt->common.uid = ecdt_ptr->uid;
1470 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1471 if (ACPI_FAILURE(status)) {
1477 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1484 static int __initdata acpi_fake_ecdt_enabled;
1485 int __init acpi_ec_ecdt_probe(void)
1490 ret = acpi_ec_get_real_ecdt();
1491 /* Try to make a fake ECDT */
1492 if (ret && acpi_fake_ecdt_enabled) {
1493 ret = acpi_ec_fake_ecdt();
1500 * Install GPE handler
1502 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1503 ACPI_GPE_EDGE_TRIGGERED,
1504 &acpi_ec_gpe_handler, ec_ecdt);
1505 if (ACPI_FAILURE(status)) {
1508 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1509 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1511 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1513 &acpi_ec_space_handler,
1514 &acpi_ec_space_setup,
1516 if (ACPI_FAILURE(status)) {
1517 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1518 &acpi_ec_gpe_handler);
1525 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1532 static int __init acpi_ec_init(void)
1536 ACPI_FUNCTION_TRACE("acpi_ec_init");
1541 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1543 return_VALUE(-ENODEV);
1545 /* Now register the driver for the EC */
1546 result = acpi_bus_register_driver(&acpi_ec_driver);
1548 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1549 return_VALUE(-ENODEV);
1552 return_VALUE(result);
1555 subsys_initcall(acpi_ec_init);
1557 /* EC driver currently not unloadable */
1559 static void __exit acpi_ec_exit(void)
1561 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1563 acpi_bus_unregister_driver(&acpi_ec_driver);
1565 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1571 static int __init acpi_fake_ecdt_setup(char *str)
1573 acpi_fake_ecdt_enabled = 1;
1577 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1578 static int __init acpi_ec_set_intr_mode(char *str)
1582 if (!get_option(&str, &intr))
1586 acpi_ec_poll_mode = EC_INTR;
1587 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1589 acpi_ec_poll_mode = EC_POLL;
1590 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1592 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1596 __setup("ec_intr=", acpi_ec_set_intr_mode);