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
63 #define EC_POLLING 0xFF
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_burst_add(struct acpi_device *device);
69 static int acpi_ec_polling_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_polling_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_polling_wait(union acpi_ec *ec, u8 event);
124 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
125 static int acpi_ec_polling_read(union acpi_ec *ec, u8 address, u32 * data);
126 static int acpi_ec_burst_read(union acpi_ec *ec, u8 address, u32 * data);
127 static int acpi_ec_polling_write(union acpi_ec *ec, u8 address, u8 data);
128 static int acpi_ec_burst_write(union acpi_ec *ec, u8 address, u8 data);
129 static int acpi_ec_polling_query(union acpi_ec *ec, u32 * data);
130 static int acpi_ec_burst_query(union acpi_ec *ec, u32 * data);
131 static void acpi_ec_gpe_polling_query(void *ec_cxt);
132 static void acpi_ec_gpe_burst_query(void *ec_cxt);
133 static u32 acpi_ec_gpe_polling_handler(void *data);
134 static u32 acpi_ec_gpe_burst_handler(void *data);
135 static acpi_status __init
136 acpi_fake_ecdt_polling_callback(acpi_handle handle,
137 u32 Level, void *context, void **retval);
139 static acpi_status __init
140 acpi_fake_ecdt_burst_callback(acpi_handle handle,
141 u32 Level, void *context, void **retval);
143 static int __init acpi_ec_polling_get_real_ecdt(void);
144 static int __init acpi_ec_burst_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_polling_mode = EC_POLLING;
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_polling_mode)
167 return acpi_ec_polling_wait(ec, event);
169 return acpi_ec_burst_wait(ec, event);
172 static int acpi_ec_polling_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_burst_wait(union acpi_ec *ec, unsigned int event)
210 ACPI_FUNCTION_TRACE("acpi_ec_wait");
212 ec->burst.expect_event = event;
216 case ACPI_EC_EVENT_OBF:
217 if (acpi_ec_read_status(ec) & event) {
218 ec->burst.expect_event = 0;
223 case ACPI_EC_EVENT_IBE:
224 if (~acpi_ec_read_status(ec) & event) {
225 ec->burst.expect_event = 0;
231 result = wait_event_timeout(ec->burst.wait,
232 !ec->burst.expect_event,
233 msecs_to_jiffies(ACPI_EC_DELAY));
235 ec->burst.expect_event = 0;
239 * Verify that the event in question has actually happened by
240 * querying EC status. Do the check even if operation timed-out
241 * to make sure that we did not miss interrupt.
244 case ACPI_EC_EVENT_OBF:
245 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
249 case ACPI_EC_EVENT_IBE:
250 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
255 return_VALUE(-ETIME);
258 static 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);
274 return_VALUE(-EINVAL);
275 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
276 if (tmp != 0x90) { /* Burst ACK byte */
277 return_VALUE(-EINVAL);
281 atomic_set(&ec->burst.leaving_burst, 0);
284 printk("Error in acpi_ec_wait\n");
288 static int acpi_ec_leave_burst_mode(union acpi_ec *ec)
291 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
293 atomic_set(&ec->burst.leaving_burst, 1);
297 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
299 if (acpi_ec_polling_mode)
300 return acpi_ec_polling_read(ec, address, data);
302 return acpi_ec_burst_read(ec, address, data);
304 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
306 if (acpi_ec_polling_mode)
307 return acpi_ec_polling_write(ec, address, data);
309 return acpi_ec_burst_write(ec, address, data);
311 static int acpi_ec_polling_read(union acpi_ec *ec, u8 address, u32 * data)
313 acpi_status status = AE_OK;
315 unsigned long flags = 0;
318 ACPI_FUNCTION_TRACE("acpi_ec_read");
321 return_VALUE(-EINVAL);
325 if (ec->common.global_lock) {
326 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
327 if (ACPI_FAILURE(status))
328 return_VALUE(-ENODEV);
331 spin_lock_irqsave(&ec->polling.lock, flags);
333 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
334 &ec->common.command_addr);
335 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
339 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
340 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
344 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
350 spin_unlock_irqrestore(&ec->polling.lock, flags);
352 if (ec->common.global_lock)
353 acpi_release_global_lock(glk);
355 return_VALUE(result);
358 static int acpi_ec_polling_write(union acpi_ec *ec, u8 address, u8 data)
361 acpi_status status = AE_OK;
362 unsigned long flags = 0;
365 ACPI_FUNCTION_TRACE("acpi_ec_write");
368 return_VALUE(-EINVAL);
370 if (ec->common.global_lock) {
371 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
372 if (ACPI_FAILURE(status))
373 return_VALUE(-ENODEV);
376 spin_lock_irqsave(&ec->polling.lock, flags);
378 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
379 &ec->common.command_addr);
380 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
384 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
385 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
389 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
390 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
394 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
398 spin_unlock_irqrestore(&ec->polling.lock, flags);
400 if (ec->common.global_lock)
401 acpi_release_global_lock(glk);
403 return_VALUE(result);
406 static int acpi_ec_burst_read(union acpi_ec *ec, u8 address, u32 * data)
411 ACPI_FUNCTION_TRACE("acpi_ec_read");
414 return_VALUE(-EINVAL);
418 if (ec->common.global_lock) {
419 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
420 if (ACPI_FAILURE(status))
421 return_VALUE(-ENODEV);
424 WARN_ON(in_interrupt());
425 down(&ec->burst.sem);
427 acpi_ec_enter_burst_mode(ec);
428 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
430 printk("read EC, IB not empty\n");
433 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
434 &ec->common.command_addr);
435 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
437 printk("read EC, IB not empty\n");
440 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
441 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
443 printk("read EC, OB not full\n");
446 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
447 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
451 acpi_ec_leave_burst_mode(ec);
454 if (ec->common.global_lock)
455 acpi_release_global_lock(glk);
457 return_VALUE(status);
460 static int acpi_ec_burst_write(union acpi_ec *ec, u8 address, u8 data)
465 ACPI_FUNCTION_TRACE("acpi_ec_write");
468 return_VALUE(-EINVAL);
470 if (ec->common.global_lock) {
471 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
472 if (ACPI_FAILURE(status))
473 return_VALUE(-ENODEV);
476 WARN_ON(in_interrupt());
477 down(&ec->burst.sem);
479 acpi_ec_enter_burst_mode(ec);
481 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
483 printk("write EC, IB not empty\n");
485 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
486 &ec->common.command_addr);
487 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
489 printk("write EC, IB not empty\n");
492 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
493 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
495 printk("write EC, IB not empty\n");
498 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
500 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
503 acpi_ec_leave_burst_mode(ec);
506 if (ec->common.global_lock)
507 acpi_release_global_lock(glk);
509 return_VALUE(status);
513 * Externally callable EC access functions. For now, assume 1 EC only
515 int ec_read(u8 addr, u8 * val)
524 ec = acpi_driver_data(first_ec);
526 err = acpi_ec_read(ec, addr, &temp_data);
535 EXPORT_SYMBOL(ec_read);
537 int ec_write(u8 addr, u8 val)
545 ec = acpi_driver_data(first_ec);
547 err = acpi_ec_write(ec, addr, val);
552 EXPORT_SYMBOL(ec_write);
554 static int acpi_ec_query(union acpi_ec *ec, u32 * data)
556 if (acpi_ec_polling_mode)
557 return acpi_ec_polling_query(ec, data);
559 return acpi_ec_burst_query(ec, data);
561 static int acpi_ec_polling_query(union acpi_ec *ec, u32 * data)
564 acpi_status status = AE_OK;
565 unsigned long flags = 0;
568 ACPI_FUNCTION_TRACE("acpi_ec_query");
571 return_VALUE(-EINVAL);
575 if (ec->common.global_lock) {
576 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
577 if (ACPI_FAILURE(status))
578 return_VALUE(-ENODEV);
582 * Query the EC to find out which _Qxx method we need to evaluate.
583 * Note that successful completion of the query causes the ACPI_EC_SCI
584 * bit to be cleared (and thus clearing the interrupt source).
586 spin_lock_irqsave(&ec->polling.lock, flags);
588 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
589 &ec->common.command_addr);
590 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
594 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
599 spin_unlock_irqrestore(&ec->polling.lock, flags);
601 if (ec->common.global_lock)
602 acpi_release_global_lock(glk);
604 return_VALUE(result);
606 static int acpi_ec_burst_query(union acpi_ec *ec, u32 * data)
611 ACPI_FUNCTION_TRACE("acpi_ec_query");
614 return_VALUE(-EINVAL);
617 if (ec->common.global_lock) {
618 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
619 if (ACPI_FAILURE(status))
620 return_VALUE(-ENODEV);
623 down(&ec->burst.sem);
625 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
627 printk("query EC, IB not empty\n");
631 * Query the EC to find out which _Qxx method we need to evaluate.
632 * Note that successful completion of the query causes the ACPI_EC_SCI
633 * bit to be cleared (and thus clearing the interrupt source).
635 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
636 &ec->common.command_addr);
637 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
639 printk("query EC, OB not full\n");
643 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
650 if (ec->common.global_lock)
651 acpi_release_global_lock(glk);
653 return_VALUE(status);
656 /* --------------------------------------------------------------------------
658 -------------------------------------------------------------------------- */
660 union acpi_ec_query_data {
665 static void acpi_ec_gpe_query(void *ec_cxt)
667 if (acpi_ec_polling_mode)
668 acpi_ec_gpe_polling_query(ec_cxt);
670 acpi_ec_gpe_burst_query(ec_cxt);
673 static void acpi_ec_gpe_polling_query(void *ec_cxt)
675 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
677 unsigned long flags = 0;
678 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
679 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
680 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
683 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
688 spin_lock_irqsave(&ec->polling.lock, flags);
689 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
690 spin_unlock_irqrestore(&ec->polling.lock, flags);
692 /* TBD: Implement asynch events!
693 * NOTE: All we care about are EC-SCI's. Other EC events are
694 * handled via polling (yuck!). This is because some systems
695 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
696 * a purely interrupt-driven approach (grumble, grumble).
698 if (!(value & ACPI_EC_FLAG_SCI))
701 if (acpi_ec_query(ec, &value))
704 object_name[2] = hex[((value >> 4) & 0x0F)];
705 object_name[3] = hex[(value & 0x0F)];
707 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
709 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
712 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
714 static void acpi_ec_gpe_burst_query(void *ec_cxt)
716 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
718 int result = -ENODATA;
719 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
720 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
721 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
724 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
726 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
727 result = acpi_ec_query(ec, &value);
732 object_name[2] = hex[((value >> 4) & 0x0F)];
733 object_name[3] = hex[(value & 0x0F)];
735 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
737 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
739 atomic_dec(&ec->burst.pending_gpe);
743 static u32 acpi_ec_gpe_handler(void *data)
745 if (acpi_ec_polling_mode)
746 return acpi_ec_gpe_polling_handler(data);
748 return acpi_ec_gpe_burst_handler(data);
750 static u32 acpi_ec_gpe_polling_handler(void *data)
752 acpi_status status = AE_OK;
753 union acpi_ec *ec = (union acpi_ec *)data;
756 return ACPI_INTERRUPT_NOT_HANDLED;
758 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
760 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
761 acpi_ec_gpe_query, ec);
764 return ACPI_INTERRUPT_HANDLED;
766 return ACPI_INTERRUPT_NOT_HANDLED;
768 static u32 acpi_ec_gpe_burst_handler(void *data)
770 acpi_status status = AE_OK;
772 union acpi_ec *ec = (union acpi_ec *)data;
775 return ACPI_INTERRUPT_NOT_HANDLED;
777 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
778 value = acpi_ec_read_status(ec);
780 switch (ec->burst.expect_event) {
781 case ACPI_EC_EVENT_OBF:
782 if (!(value & ACPI_EC_FLAG_OBF))
784 case ACPI_EC_EVENT_IBE:
785 if ((value & ACPI_EC_FLAG_IBF))
787 ec->burst.expect_event = 0;
788 wake_up(&ec->burst.wait);
789 return ACPI_INTERRUPT_HANDLED;
794 if (value & ACPI_EC_FLAG_SCI) {
795 atomic_add(1, &ec->burst.pending_gpe);
796 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
797 acpi_ec_gpe_query, ec);
798 return status == AE_OK ?
799 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
801 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
802 return status == AE_OK ?
803 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
806 /* --------------------------------------------------------------------------
807 Address Space Management
808 -------------------------------------------------------------------------- */
811 acpi_ec_space_setup(acpi_handle region_handle,
812 u32 function, void *handler_context, void **return_context)
815 * The EC object is in the handler context and is needed
816 * when calling the acpi_ec_space_handler.
818 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
819 handler_context : NULL;
825 acpi_ec_space_handler(u32 function,
826 acpi_physical_address address,
828 acpi_integer * value,
829 void *handler_context, void *region_context)
832 union acpi_ec *ec = NULL;
834 acpi_integer f_v = 0;
837 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
839 if ((address > 0xFF) || !value || !handler_context)
840 return_VALUE(AE_BAD_PARAMETER);
842 if (bit_width != 8 && acpi_strict) {
843 printk(KERN_WARNING PREFIX
844 "acpi_ec_space_handler: bit_width should be 8\n");
845 return_VALUE(AE_BAD_PARAMETER);
848 ec = (union acpi_ec *)handler_context;
854 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
857 result = acpi_ec_write(ec, (u8) address, (u8) temp);
867 if (function == ACPI_READ)
868 f_v |= temp << 8 * i;
869 if (function == ACPI_WRITE)
876 if (function == ACPI_READ) {
877 f_v |= temp << 8 * i;
884 return_VALUE(AE_BAD_PARAMETER);
887 return_VALUE(AE_NOT_FOUND);
890 return_VALUE(AE_TIME);
897 /* --------------------------------------------------------------------------
899 -------------------------------------------------------------------------- */
901 static struct proc_dir_entry *acpi_ec_dir;
903 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
905 union acpi_ec *ec = (union acpi_ec *)seq->private;
907 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
912 seq_printf(seq, "gpe bit: 0x%02x\n",
913 (u32) ec->common.gpe_bit);
914 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
915 (u32) ec->common.status_addr.address,
916 (u32) ec->common.data_addr.address);
917 seq_printf(seq, "use global lock: %s\n",
918 ec->common.global_lock ? "yes" : "no");
919 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
925 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
927 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
930 static struct file_operations acpi_ec_info_ops = {
931 .open = acpi_ec_info_open_fs,
934 .release = single_release,
935 .owner = THIS_MODULE,
938 static int acpi_ec_add_fs(struct acpi_device *device)
940 struct proc_dir_entry *entry = NULL;
942 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
944 if (!acpi_device_dir(device)) {
945 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
947 if (!acpi_device_dir(device))
948 return_VALUE(-ENODEV);
951 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
952 acpi_device_dir(device));
954 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
955 "Unable to create '%s' fs entry\n",
958 entry->proc_fops = &acpi_ec_info_ops;
959 entry->data = acpi_driver_data(device);
960 entry->owner = THIS_MODULE;
966 static int acpi_ec_remove_fs(struct acpi_device *device)
968 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
970 if (acpi_device_dir(device)) {
971 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
972 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
973 acpi_device_dir(device) = NULL;
979 /* --------------------------------------------------------------------------
981 -------------------------------------------------------------------------- */
983 static int acpi_ec_polling_add(struct acpi_device *device)
986 acpi_status status = AE_OK;
987 union acpi_ec *ec = NULL;
990 ACPI_FUNCTION_TRACE("acpi_ec_add");
993 return_VALUE(-EINVAL);
995 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
997 return_VALUE(-ENOMEM);
998 memset(ec, 0, sizeof(union acpi_ec));
1000 ec->common.handle = device->handle;
1001 ec->common.uid = -1;
1002 spin_lock_init(&ec->polling.lock);
1003 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1004 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1005 acpi_driver_data(device) = ec;
1007 /* Use the global lock for all EC transactions? */
1008 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1009 &ec->common.global_lock);
1011 /* If our UID matches the UID for the ECDT-enumerated EC,
1012 we now have the *real* EC info, so kill the makeshift one. */
1013 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1014 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1015 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1017 &acpi_ec_space_handler);
1019 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1020 &acpi_ec_gpe_handler);
1025 /* Get GPE bit assignment (EC events). */
1026 /* TODO: Add support for _GPE returning a package */
1028 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1029 &ec->common.gpe_bit);
1030 if (ACPI_FAILURE(status)) {
1031 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1032 "Error obtaining GPE bit assignment\n"));
1037 result = acpi_ec_add_fs(device);
1041 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1042 acpi_device_name(device), acpi_device_bid(device),
1043 (u32) ec->common.gpe_bit);
1052 return_VALUE(result);
1054 static int acpi_ec_burst_add(struct acpi_device *device)
1057 acpi_status status = AE_OK;
1058 union acpi_ec *ec = NULL;
1061 ACPI_FUNCTION_TRACE("acpi_ec_add");
1064 return_VALUE(-EINVAL);
1066 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1068 return_VALUE(-ENOMEM);
1069 memset(ec, 0, sizeof(union acpi_ec));
1071 ec->common.handle = device->handle;
1072 ec->common.uid = -1;
1073 atomic_set(&ec->burst.pending_gpe, 0);
1074 atomic_set(&ec->burst.leaving_burst, 1);
1075 init_MUTEX(&ec->burst.sem);
1076 init_waitqueue_head(&ec->burst.wait);
1077 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1078 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1079 acpi_driver_data(device) = ec;
1081 /* Use the global lock for all EC transactions? */
1082 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1083 &ec->common.global_lock);
1085 /* If our UID matches the UID for the ECDT-enumerated EC,
1086 we now have the *real* EC info, so kill the makeshift one. */
1087 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1088 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1089 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1091 &acpi_ec_space_handler);
1093 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1094 &acpi_ec_gpe_handler);
1099 /* Get GPE bit assignment (EC events). */
1100 /* TODO: Add support for _GPE returning a package */
1102 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1103 &ec->common.gpe_bit);
1104 if (ACPI_FAILURE(status)) {
1105 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1106 "Error obtaining GPE bit assignment\n"));
1111 result = acpi_ec_add_fs(device);
1115 printk("burst-mode-ec-10-Aug\n");
1116 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1117 acpi_device_name(device), acpi_device_bid(device),
1118 (u32) ec->common.gpe_bit);
1127 return_VALUE(result);
1130 static int acpi_ec_remove(struct acpi_device *device, int type)
1132 union acpi_ec *ec = NULL;
1134 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1137 return_VALUE(-EINVAL);
1139 ec = acpi_driver_data(device);
1141 acpi_ec_remove_fs(device);
1149 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1151 union acpi_ec *ec = (union acpi_ec *)context;
1152 struct acpi_generic_address *addr;
1154 if (resource->id != ACPI_RSTYPE_IO) {
1159 * The first address region returned is the data port, and
1160 * the second address region returned is the status/command
1163 if (ec->common.data_addr.register_bit_width == 0) {
1164 addr = &ec->common.data_addr;
1165 } else if (ec->common.command_addr.register_bit_width == 0) {
1166 addr = &ec->common.command_addr;
1168 return AE_CTRL_TERMINATE;
1171 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1172 addr->register_bit_width = 8;
1173 addr->register_bit_offset = 0;
1174 addr->address = resource->data.io.min_base_address;
1179 static int acpi_ec_start(struct acpi_device *device)
1181 acpi_status status = AE_OK;
1182 union acpi_ec *ec = NULL;
1184 ACPI_FUNCTION_TRACE("acpi_ec_start");
1187 return_VALUE(-EINVAL);
1189 ec = acpi_driver_data(device);
1192 return_VALUE(-EINVAL);
1195 * Get I/O port addresses. Convert to GAS format.
1197 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1198 acpi_ec_io_ports, ec);
1199 if (ACPI_FAILURE(status)
1200 || ec->common.command_addr.register_bit_width == 0) {
1201 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1202 "Error getting I/O port addresses"));
1203 return_VALUE(-ENODEV);
1206 ec->common.status_addr = ec->common.command_addr;
1208 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1209 (u32) ec->common.gpe_bit,
1210 (u32) ec->common.command_addr.address,
1211 (u32) ec->common.data_addr.address));
1214 * Install GPE handler
1216 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1217 ACPI_GPE_EDGE_TRIGGERED,
1218 &acpi_ec_gpe_handler, ec);
1219 if (ACPI_FAILURE(status)) {
1220 return_VALUE(-ENODEV);
1222 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1223 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1225 status = acpi_install_address_space_handler(ec->common.handle,
1227 &acpi_ec_space_handler,
1228 &acpi_ec_space_setup, ec);
1229 if (ACPI_FAILURE(status)) {
1230 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1231 &acpi_ec_gpe_handler);
1232 return_VALUE(-ENODEV);
1235 return_VALUE(AE_OK);
1238 static int acpi_ec_stop(struct acpi_device *device, int type)
1240 acpi_status status = AE_OK;
1241 union acpi_ec *ec = NULL;
1243 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1246 return_VALUE(-EINVAL);
1248 ec = acpi_driver_data(device);
1250 status = acpi_remove_address_space_handler(ec->common.handle,
1252 &acpi_ec_space_handler);
1253 if (ACPI_FAILURE(status))
1254 return_VALUE(-ENODEV);
1257 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1258 &acpi_ec_gpe_handler);
1259 if (ACPI_FAILURE(status))
1260 return_VALUE(-ENODEV);
1265 static acpi_status __init
1266 acpi_fake_ecdt_callback(acpi_handle handle,
1267 u32 Level, void *context, void **retval)
1270 if (acpi_ec_polling_mode)
1271 return acpi_fake_ecdt_polling_callback(handle,
1272 Level, context, retval);
1274 return acpi_fake_ecdt_burst_callback(handle,
1275 Level, context, retval);
1278 static acpi_status __init
1279 acpi_fake_ecdt_polling_callback(acpi_handle handle,
1280 u32 Level, void *context, void **retval)
1284 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1285 acpi_ec_io_ports, ec_ecdt);
1286 if (ACPI_FAILURE(status))
1288 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1290 ec_ecdt->common.uid = -1;
1291 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1294 acpi_evaluate_integer(handle, "_GPE", NULL,
1295 &ec_ecdt->common.gpe_bit);
1296 if (ACPI_FAILURE(status))
1298 spin_lock_init(&ec_ecdt->polling.lock);
1299 ec_ecdt->common.global_lock = TRUE;
1300 ec_ecdt->common.handle = handle;
1302 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1303 (u32) ec_ecdt->common.gpe_bit,
1304 (u32) ec_ecdt->common.command_addr.address,
1305 (u32) ec_ecdt->common.data_addr.address);
1307 return AE_CTRL_TERMINATE;
1310 static acpi_status __init
1311 acpi_fake_ecdt_burst_callback(acpi_handle handle,
1312 u32 Level, void *context, void **retval)
1316 init_MUTEX(&ec_ecdt->burst.sem);
1317 init_waitqueue_head(&ec_ecdt->burst.wait);
1318 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1319 acpi_ec_io_ports, ec_ecdt);
1320 if (ACPI_FAILURE(status))
1322 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1324 ec_ecdt->common.uid = -1;
1325 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1328 acpi_evaluate_integer(handle, "_GPE", NULL,
1329 &ec_ecdt->common.gpe_bit);
1330 if (ACPI_FAILURE(status))
1332 ec_ecdt->common.global_lock = TRUE;
1333 ec_ecdt->common.handle = handle;
1335 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1336 (u32) ec_ecdt->common.gpe_bit,
1337 (u32) ec_ecdt->common.command_addr.address,
1338 (u32) ec_ecdt->common.data_addr.address);
1340 return AE_CTRL_TERMINATE;
1344 * Some BIOS (such as some from Gateway laptops) access EC region very early
1345 * such as in BAT0._INI or EC._INI before an EC device is found and
1346 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1347 * required, but if EC regison is accessed early, it is required.
1348 * The routine tries to workaround the BIOS bug by pre-scan EC device
1349 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1350 * op region (since _REG isn't invoked yet). The assumption is true for
1351 * all systems found.
1353 static int __init acpi_ec_fake_ecdt(void)
1358 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1360 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1365 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1367 status = acpi_get_devices(ACPI_EC_HID,
1368 acpi_fake_ecdt_callback, NULL, NULL);
1369 if (ACPI_FAILURE(status)) {
1377 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1381 static int __init acpi_ec_get_real_ecdt(void)
1383 if (acpi_ec_polling_mode)
1384 return acpi_ec_polling_get_real_ecdt();
1386 return acpi_ec_burst_get_real_ecdt();
1389 static int __init acpi_ec_polling_get_real_ecdt(void)
1392 struct acpi_table_ecdt *ecdt_ptr;
1394 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1395 (struct acpi_table_header **)
1397 if (ACPI_FAILURE(status))
1400 printk(KERN_INFO PREFIX "Found ECDT\n");
1403 * Generate a temporary ec context to use until the namespace is scanned
1405 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1408 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1410 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1411 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1412 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1413 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1414 spin_lock_init(&ec_ecdt->polling.lock);
1415 /* use the GL just to be safe */
1416 ec_ecdt->common.global_lock = TRUE;
1417 ec_ecdt->common.uid = ecdt_ptr->uid;
1420 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1421 if (ACPI_FAILURE(status)) {
1427 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1434 static int __init acpi_ec_burst_get_real_ecdt(void)
1437 struct acpi_table_ecdt *ecdt_ptr;
1439 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1440 (struct acpi_table_header **)
1442 if (ACPI_FAILURE(status))
1445 printk(KERN_INFO PREFIX "Found ECDT\n");
1448 * Generate a temporary ec context to use until the namespace is scanned
1450 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1453 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1455 init_MUTEX(&ec_ecdt->burst.sem);
1456 init_waitqueue_head(&ec_ecdt->burst.wait);
1457 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1458 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1459 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1460 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1461 /* use the GL just to be safe */
1462 ec_ecdt->common.global_lock = TRUE;
1463 ec_ecdt->common.uid = ecdt_ptr->uid;
1466 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1467 if (ACPI_FAILURE(status)) {
1473 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1480 static int __initdata acpi_fake_ecdt_enabled;
1481 int __init acpi_ec_ecdt_probe(void)
1486 ret = acpi_ec_get_real_ecdt();
1487 /* Try to make a fake ECDT */
1488 if (ret && acpi_fake_ecdt_enabled) {
1489 ret = acpi_ec_fake_ecdt();
1496 * Install GPE handler
1498 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1499 ACPI_GPE_EDGE_TRIGGERED,
1500 &acpi_ec_gpe_handler, ec_ecdt);
1501 if (ACPI_FAILURE(status)) {
1504 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1505 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1507 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1509 &acpi_ec_space_handler,
1510 &acpi_ec_space_setup,
1512 if (ACPI_FAILURE(status)) {
1513 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1514 &acpi_ec_gpe_handler);
1521 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1528 static int __init acpi_ec_init(void)
1532 ACPI_FUNCTION_TRACE("acpi_ec_init");
1537 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1539 return_VALUE(-ENODEV);
1541 /* Now register the driver for the EC */
1542 result = acpi_bus_register_driver(&acpi_ec_driver);
1544 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1545 return_VALUE(-ENODEV);
1548 return_VALUE(result);
1551 subsys_initcall(acpi_ec_init);
1553 /* EC driver currently not unloadable */
1555 static void __exit acpi_ec_exit(void)
1557 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1559 acpi_bus_unregister_driver(&acpi_ec_driver);
1561 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1567 static int __init acpi_fake_ecdt_setup(char *str)
1569 acpi_fake_ecdt_enabled = 1;
1573 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1574 static int __init acpi_ec_set_polling_mode(char *str)
1578 if (!get_option(&str, &burst))
1582 acpi_ec_polling_mode = EC_BURST;
1583 acpi_ec_driver.ops.add = acpi_ec_burst_add;
1585 acpi_ec_polling_mode = EC_POLLING;
1586 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1588 printk(KERN_INFO PREFIX "EC %s mode.\n", burst ? "burst" : "polling");
1592 __setup("ec_burst=", acpi_ec_set_polling_mode);