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 /* Uncomment next line to get verbose print outs*/
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <acpi/actypes.h>
46 #define ACPI_EC_CLASS "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
51 #define PREFIX "ACPI: EC: "
53 /* EC status register */
54 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
56 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
57 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
61 ACPI_EC_COMMAND_READ
= 0x80,
62 ACPI_EC_COMMAND_WRITE
= 0x81,
63 ACPI_EC_BURST_ENABLE
= 0x82,
64 ACPI_EC_BURST_DISABLE
= 0x83,
65 ACPI_EC_COMMAND_QUERY
= 0x84,
70 ACPI_EC_EVENT_OBF_1
= 1, /* Output buffer full */
71 ACPI_EC_EVENT_IBF_0
, /* Input buffer empty */
74 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
75 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
76 #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
79 EC_FLAGS_WAIT_GPE
= 0, /* Don't check status until GPE arrives */
80 EC_FLAGS_QUERY_PENDING
, /* Query is pending */
81 EC_FLAGS_GPE_MODE
, /* Expect GPE to be sent for status change */
82 EC_FLAGS_NO_ADDRESS_GPE
, /* Expect GPE only for non-address event */
83 EC_FLAGS_ADDRESS
, /* Address is being written */
84 EC_FLAGS_NO_WDATA_GPE
, /* Don't expect WDATA GPE event */
85 EC_FLAGS_WDATA
, /* Data is being written */
86 EC_FLAGS_NO_OBF1_GPE
, /* Don't expect GPE before read */
87 EC_FLAGS_RESCHEDULE_POLL
/* Re-schedule poll */
90 static int acpi_ec_remove(struct acpi_device
*device
, int type
);
91 static int acpi_ec_start(struct acpi_device
*device
);
92 static int acpi_ec_stop(struct acpi_device
*device
, int type
);
93 static int acpi_ec_add(struct acpi_device
*device
);
95 static const struct acpi_device_id ec_device_ids
[] = {
100 static struct acpi_driver acpi_ec_driver
= {
102 .class = ACPI_EC_CLASS
,
103 .ids
= ec_device_ids
,
106 .remove
= acpi_ec_remove
,
107 .start
= acpi_ec_start
,
108 .stop
= acpi_ec_stop
,
112 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
113 /* External interfaces use first EC only, so remember */
114 typedef int (*acpi_ec_query_func
) (void *data
);
116 struct acpi_ec_query_handler
{
117 struct list_head node
;
118 acpi_ec_query_func func
;
124 static struct acpi_ec
{
127 unsigned long command_addr
;
128 unsigned long data_addr
;
129 unsigned long global_lock
;
132 wait_queue_head_t wait
;
133 struct list_head list
;
134 struct delayed_work work
;
135 u8 handlers_installed
;
136 } *boot_ec
, *first_ec
;
138 /* --------------------------------------------------------------------------
139 Transaction Management
140 -------------------------------------------------------------------------- */
142 static inline u8
acpi_ec_read_status(struct acpi_ec
*ec
)
144 u8 x
= inb(ec
->command_addr
);
145 pr_debug(PREFIX
"---> status = 0x%2.2x\n", x
);
149 static inline u8
acpi_ec_read_data(struct acpi_ec
*ec
)
151 u8 x
= inb(ec
->data_addr
);
152 pr_debug(PREFIX
"---> data = 0x%2.2x\n", x
);
153 return inb(ec
->data_addr
);
156 static inline void acpi_ec_write_cmd(struct acpi_ec
*ec
, u8 command
)
158 pr_debug(PREFIX
"<--- command = 0x%2.2x\n", command
);
159 outb(command
, ec
->command_addr
);
162 static inline void acpi_ec_write_data(struct acpi_ec
*ec
, u8 data
)
164 pr_debug(PREFIX
"<--- data = 0x%2.2x\n", data
);
165 outb(data
, ec
->data_addr
);
168 static inline int acpi_ec_check_status(struct acpi_ec
*ec
, enum ec_event event
)
170 if (test_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
))
172 if (event
== ACPI_EC_EVENT_OBF_1
) {
173 if (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_OBF
)
175 } else if (event
== ACPI_EC_EVENT_IBF_0
) {
176 if (!(acpi_ec_read_status(ec
) & ACPI_EC_FLAG_IBF
))
183 static void ec_schedule_ec_poll(struct acpi_ec
*ec
)
185 if (test_bit(EC_FLAGS_RESCHEDULE_POLL
, &ec
->flags
))
186 schedule_delayed_work(&ec
->work
,
187 msecs_to_jiffies(ACPI_EC_DELAY
));
190 static void ec_switch_to_poll_mode(struct acpi_ec
*ec
)
192 clear_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
193 acpi_disable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
194 set_bit(EC_FLAGS_RESCHEDULE_POLL
, &ec
->flags
);
197 static int acpi_ec_wait(struct acpi_ec
*ec
, enum ec_event event
, int force_poll
)
201 if (unlikely(event
== ACPI_EC_EVENT_OBF_1
&&
202 test_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
)))
204 if (unlikely(test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
) &&
205 test_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
)))
207 if (unlikely(test_bit(EC_FLAGS_WDATA
, &ec
->flags
) &&
208 test_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
)))
210 if (likely(test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
)) &&
211 likely(!force_poll
)) {
212 if (wait_event_timeout(ec
->wait
, acpi_ec_check_status(ec
, event
),
213 msecs_to_jiffies(ACPI_EC_DELAY
)))
215 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
216 if (acpi_ec_check_status(ec
, event
)) {
217 if (event
== ACPI_EC_EVENT_OBF_1
) {
218 /* miss OBF_1 GPE, don't expect it */
219 pr_info(PREFIX
"missing OBF confirmation, "
220 "don't expect it any longer.\n");
221 set_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
);
222 } else if (test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
)) {
223 /* miss address GPE, don't expect it anymore */
224 pr_info(PREFIX
"missing address confirmation, "
225 "don't expect it any longer.\n");
226 set_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
);
227 } else if (test_bit(EC_FLAGS_WDATA
, &ec
->flags
)) {
228 /* miss write data GPE, don't expect it */
229 pr_info(PREFIX
"missing write data confirmation, "
230 "don't expect it any longer.\n");
231 set_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
);
233 /* missing GPEs, switch back to poll mode */
234 if (printk_ratelimit())
235 pr_info(PREFIX
"missing confirmations, "
236 "switch off interrupt mode.\n");
237 ec_switch_to_poll_mode(ec
);
238 ec_schedule_ec_poll(ec
);
243 unsigned long delay
= jiffies
+ msecs_to_jiffies(ACPI_EC_DELAY
);
244 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
245 while (time_before(jiffies
, delay
)) {
246 if (acpi_ec_check_status(ec
, event
))
248 udelay(ACPI_EC_UDELAY
);
251 pr_err(PREFIX
"acpi_ec_wait timeout,"
252 " status = %d, expect_event = %d\n",
253 acpi_ec_read_status(ec
), event
);
256 clear_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
260 static int acpi_ec_transaction_unlocked(struct acpi_ec
*ec
, u8 command
,
261 const u8
* wdata
, unsigned wdata_len
,
262 u8
* rdata
, unsigned rdata_len
,
266 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
267 acpi_ec_write_cmd(ec
, command
);
268 pr_debug(PREFIX
"transaction start\n");
269 for (; wdata_len
> 0; --wdata_len
) {
270 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
273 "write_cmd timeout, command = %d\n", command
);
276 /* mark the address byte written to EC */
277 if (rdata_len
+ wdata_len
> 1)
278 set_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
279 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
280 acpi_ec_write_data(ec
, *(wdata
++));
284 set_bit(EC_FLAGS_WDATA
, &ec
->flags
);
285 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
288 "finish-write timeout, command = %d\n", command
);
291 } else if (command
== ACPI_EC_COMMAND_QUERY
)
292 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
294 for (; rdata_len
> 0; --rdata_len
) {
295 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_OBF_1
, force_poll
);
297 pr_err(PREFIX
"read timeout, command = %d\n", command
);
300 /* Don't expect GPE after last read */
302 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
303 *(rdata
++) = acpi_ec_read_data(ec
);
306 pr_debug(PREFIX
"transaction end\n");
310 static int acpi_ec_transaction(struct acpi_ec
*ec
, u8 command
,
311 const u8
* wdata
, unsigned wdata_len
,
312 u8
* rdata
, unsigned rdata_len
,
318 if (!ec
|| (wdata_len
&& !wdata
) || (rdata_len
&& !rdata
))
322 memset(rdata
, 0, rdata_len
);
324 mutex_lock(&ec
->lock
);
325 if (ec
->global_lock
) {
326 status
= acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK
, &glk
);
327 if (ACPI_FAILURE(status
)) {
328 mutex_unlock(&ec
->lock
);
333 status
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, 0);
335 pr_err(PREFIX
"input buffer is not empty, "
336 "aborting transaction\n");
340 status
= acpi_ec_transaction_unlocked(ec
, command
,
348 acpi_release_global_lock(glk
);
349 mutex_unlock(&ec
->lock
);
355 * Note: samsung nv5000 doesn't work with ec burst mode.
356 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
358 int acpi_ec_burst_enable(struct acpi_ec
*ec
)
361 return acpi_ec_transaction(ec
, ACPI_EC_BURST_ENABLE
, NULL
, 0, &d
, 1, 0);
364 int acpi_ec_burst_disable(struct acpi_ec
*ec
)
366 return acpi_ec_transaction(ec
, ACPI_EC_BURST_DISABLE
, NULL
, 0, NULL
, 0, 0);
369 static int acpi_ec_read(struct acpi_ec
*ec
, u8 address
, u8
* data
)
374 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_READ
,
375 &address
, 1, &d
, 1, 0);
380 static int acpi_ec_write(struct acpi_ec
*ec
, u8 address
, u8 data
)
382 u8 wdata
[2] = { address
, data
};
383 return acpi_ec_transaction(ec
, ACPI_EC_COMMAND_WRITE
,
384 wdata
, 2, NULL
, 0, 0);
388 * Externally callable EC access functions. For now, assume 1 EC only
390 int ec_burst_enable(void)
394 return acpi_ec_burst_enable(first_ec
);
397 EXPORT_SYMBOL(ec_burst_enable
);
399 int ec_burst_disable(void)
403 return acpi_ec_burst_disable(first_ec
);
406 EXPORT_SYMBOL(ec_burst_disable
);
408 int ec_read(u8 addr
, u8
* val
)
416 err
= acpi_ec_read(first_ec
, addr
, &temp_data
);
425 EXPORT_SYMBOL(ec_read
);
427 int ec_write(u8 addr
, u8 val
)
434 err
= acpi_ec_write(first_ec
, addr
, val
);
439 EXPORT_SYMBOL(ec_write
);
441 int ec_transaction(u8 command
,
442 const u8
* wdata
, unsigned wdata_len
,
443 u8
* rdata
, unsigned rdata_len
,
449 return acpi_ec_transaction(first_ec
, command
, wdata
,
450 wdata_len
, rdata
, rdata_len
,
454 EXPORT_SYMBOL(ec_transaction
);
456 static int acpi_ec_query(struct acpi_ec
*ec
, u8
* data
)
465 * Query the EC to find out which _Qxx method we need to evaluate.
466 * Note that successful completion of the query causes the ACPI_EC_SCI
467 * bit to be cleared (and thus clearing the interrupt source).
470 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_QUERY
, NULL
, 0, &d
, 1, 0);
481 /* --------------------------------------------------------------------------
483 -------------------------------------------------------------------------- */
484 int acpi_ec_add_query_handler(struct acpi_ec
*ec
, u8 query_bit
,
485 acpi_handle handle
, acpi_ec_query_func func
,
488 struct acpi_ec_query_handler
*handler
=
489 kzalloc(sizeof(struct acpi_ec_query_handler
), GFP_KERNEL
);
493 handler
->query_bit
= query_bit
;
494 handler
->handle
= handle
;
495 handler
->func
= func
;
496 handler
->data
= data
;
497 mutex_lock(&ec
->lock
);
498 list_add(&handler
->node
, &ec
->list
);
499 mutex_unlock(&ec
->lock
);
503 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler
);
505 void acpi_ec_remove_query_handler(struct acpi_ec
*ec
, u8 query_bit
)
507 struct acpi_ec_query_handler
*handler
, *tmp
;
508 mutex_lock(&ec
->lock
);
509 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
510 if (query_bit
== handler
->query_bit
) {
511 list_del(&handler
->node
);
515 mutex_unlock(&ec
->lock
);
518 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler
);
520 static void acpi_ec_gpe_query(void *ec_cxt
)
522 struct acpi_ec
*ec
= ec_cxt
;
524 struct acpi_ec_query_handler
*handler
, copy
;
526 if (!ec
|| acpi_ec_query(ec
, &value
))
528 mutex_lock(&ec
->lock
);
529 list_for_each_entry(handler
, &ec
->list
, node
) {
530 if (value
== handler
->query_bit
) {
531 /* have custom handler for this bit */
532 memcpy(©
, handler
, sizeof(copy
));
533 mutex_unlock(&ec
->lock
);
535 copy
.func(copy
.data
);
536 } else if (copy
.handle
) {
537 acpi_evaluate_object(copy
.handle
, NULL
, NULL
, NULL
);
542 mutex_unlock(&ec
->lock
);
545 static u32
acpi_ec_gpe_handler(void *data
)
547 acpi_status status
= AE_OK
;
548 struct acpi_ec
*ec
= data
;
549 u8 state
= acpi_ec_read_status(ec
);
551 pr_debug(PREFIX
"~~~> interrupt\n");
552 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
553 if (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))
556 if (state
& ACPI_EC_FLAG_SCI
) {
557 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
))
558 status
= acpi_os_execute(OSL_EC_BURST_HANDLER
,
559 acpi_ec_gpe_query
, ec
);
560 } else if (!test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
) &&
562 /* this is non-query, must be confirmation */
563 if (printk_ratelimit())
564 pr_info(PREFIX
"non-query interrupt received,"
565 " switching to interrupt mode\n");
566 set_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
567 clear_bit(EC_FLAGS_RESCHEDULE_POLL
, &ec
->flags
);
569 ec_schedule_ec_poll(ec
);
570 return ACPI_SUCCESS(status
) ?
571 ACPI_INTERRUPT_HANDLED
: ACPI_INTERRUPT_NOT_HANDLED
;
574 static void do_ec_poll(struct work_struct
*work
)
576 struct acpi_ec
*ec
= container_of(work
, struct acpi_ec
, work
.work
);
577 (void)acpi_ec_gpe_handler(ec
);
580 /* --------------------------------------------------------------------------
581 Address Space Management
582 -------------------------------------------------------------------------- */
585 acpi_ec_space_setup(acpi_handle region_handle
,
586 u32 function
, void *handler_context
, void **return_context
)
589 * The EC object is in the handler context and is needed
590 * when calling the acpi_ec_space_handler.
592 *return_context
= (function
!= ACPI_REGION_DEACTIVATE
) ?
593 handler_context
: NULL
;
599 acpi_ec_space_handler(u32 function
, acpi_physical_address address
,
600 u32 bits
, acpi_integer
*value
,
601 void *handler_context
, void *region_context
)
603 struct acpi_ec
*ec
= handler_context
;
607 if ((address
> 0xFF) || !value
|| !handler_context
)
608 return AE_BAD_PARAMETER
;
610 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
611 return AE_BAD_PARAMETER
;
613 if (bits
!= 8 && acpi_strict
)
614 return AE_BAD_PARAMETER
;
616 acpi_ec_burst_enable(ec
);
618 if (function
== ACPI_READ
) {
619 result
= acpi_ec_read(ec
, address
, &temp
);
622 temp
= 0xff & (*value
);
623 result
= acpi_ec_write(ec
, address
, temp
);
626 for (i
= 8; unlikely(bits
- i
> 0); i
+= 8) {
628 if (function
== ACPI_READ
) {
629 result
= acpi_ec_read(ec
, address
, &temp
);
630 (*value
) |= ((acpi_integer
)temp
) << i
;
632 temp
= 0xff & ((*value
) >> i
);
633 result
= acpi_ec_write(ec
, address
, temp
);
637 acpi_ec_burst_disable(ec
);
641 return AE_BAD_PARAMETER
;
654 /* --------------------------------------------------------------------------
656 -------------------------------------------------------------------------- */
658 static struct proc_dir_entry
*acpi_ec_dir
;
660 static int acpi_ec_read_info(struct seq_file
*seq
, void *offset
)
662 struct acpi_ec
*ec
= seq
->private;
667 seq_printf(seq
, "gpe:\t\t\t0x%02x\n", (u32
) ec
->gpe
);
668 seq_printf(seq
, "ports:\t\t\t0x%02x, 0x%02x\n",
669 (unsigned)ec
->command_addr
, (unsigned)ec
->data_addr
);
670 seq_printf(seq
, "use global lock:\t%s\n",
671 ec
->global_lock
? "yes" : "no");
676 static int acpi_ec_info_open_fs(struct inode
*inode
, struct file
*file
)
678 return single_open(file
, acpi_ec_read_info
, PDE(inode
)->data
);
681 static struct file_operations acpi_ec_info_ops
= {
682 .open
= acpi_ec_info_open_fs
,
685 .release
= single_release
,
686 .owner
= THIS_MODULE
,
689 static int acpi_ec_add_fs(struct acpi_device
*device
)
691 struct proc_dir_entry
*entry
= NULL
;
693 if (!acpi_device_dir(device
)) {
694 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
696 if (!acpi_device_dir(device
))
700 entry
= create_proc_entry(ACPI_EC_FILE_INFO
, S_IRUGO
,
701 acpi_device_dir(device
));
705 entry
->proc_fops
= &acpi_ec_info_ops
;
706 entry
->data
= acpi_driver_data(device
);
707 entry
->owner
= THIS_MODULE
;
713 static int acpi_ec_remove_fs(struct acpi_device
*device
)
716 if (acpi_device_dir(device
)) {
717 remove_proc_entry(ACPI_EC_FILE_INFO
, acpi_device_dir(device
));
718 remove_proc_entry(acpi_device_bid(device
), acpi_ec_dir
);
719 acpi_device_dir(device
) = NULL
;
725 /* --------------------------------------------------------------------------
727 -------------------------------------------------------------------------- */
729 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
);
731 static struct acpi_ec
*make_acpi_ec(void)
733 struct acpi_ec
*ec
= kzalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
736 ec
->flags
= 1 << EC_FLAGS_QUERY_PENDING
;
737 mutex_init(&ec
->lock
);
738 init_waitqueue_head(&ec
->wait
);
739 INIT_LIST_HEAD(&ec
->list
);
740 INIT_DELAYED_WORK_DEFERRABLE(&ec
->work
, do_ec_poll
);
745 acpi_ec_register_query_methods(acpi_handle handle
, u32 level
,
746 void *context
, void **return_value
)
748 struct acpi_namespace_node
*node
= handle
;
749 struct acpi_ec
*ec
= context
;
751 if (sscanf(node
->name
.ascii
, "_Q%x", &value
) == 1) {
752 acpi_ec_add_query_handler(ec
, value
, handle
, NULL
, NULL
);
758 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
)
762 struct acpi_ec
*ec
= context
;
763 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
764 ec_parse_io_ports
, ec
);
765 if (ACPI_FAILURE(status
))
768 /* Get GPE bit assignment (EC events). */
769 /* TODO: Add support for _GPE returning a package */
770 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &ec
->gpe
);
771 if (ACPI_FAILURE(status
))
773 /* Find and register all query methods */
774 acpi_walk_namespace(ACPI_TYPE_METHOD
, handle
, 1,
775 acpi_ec_register_query_methods
, ec
, NULL
);
776 /* Use the global lock for all EC transactions? */
777 acpi_evaluate_integer(handle
, "_GLK", NULL
, &ec
->global_lock
);
779 return AE_CTRL_TERMINATE
;
782 static void ec_poll_stop(struct acpi_ec
*ec
)
784 clear_bit(EC_FLAGS_RESCHEDULE_POLL
, &ec
->flags
);
785 cancel_delayed_work(&ec
->work
);
788 static void ec_remove_handlers(struct acpi_ec
*ec
)
791 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec
->handle
,
792 ACPI_ADR_SPACE_EC
, &acpi_ec_space_handler
)))
793 pr_err(PREFIX
"failed to remove space handler\n");
794 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL
, ec
->gpe
,
795 &acpi_ec_gpe_handler
)))
796 pr_err(PREFIX
"failed to remove gpe handler\n");
797 ec
->handlers_installed
= 0;
800 static int acpi_ec_add(struct acpi_device
*device
)
802 struct acpi_ec
*ec
= NULL
;
806 strcpy(acpi_device_name(device
), ACPI_EC_DEVICE_NAME
);
807 strcpy(acpi_device_class(device
), ACPI_EC_CLASS
);
809 /* Check for boot EC */
811 if (boot_ec
->handle
== device
->handle
) {
812 /* Pre-loaded EC from DSDT, just move pointer */
816 } else if (boot_ec
->handle
== ACPI_ROOT_OBJECT
) {
817 /* ECDT-based EC, time to shut it down */
818 ec_remove_handlers(boot_ec
);
820 first_ec
= boot_ec
= NULL
;
828 if (ec_parse_device(device
->handle
, 0, ec
, NULL
) !=
833 ec
->handle
= device
->handle
;
837 acpi_driver_data(device
) = ec
;
838 acpi_ec_add_fs(device
);
839 pr_info(PREFIX
"GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
840 ec
->gpe
, ec
->command_addr
, ec
->data_addr
);
841 pr_info(PREFIX
"driver started in %s mode\n",
842 (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))?"interrupt":"poll");
846 static int acpi_ec_remove(struct acpi_device
*device
, int type
)
849 struct acpi_ec_query_handler
*handler
, *tmp
;
854 ec
= acpi_driver_data(device
);
855 mutex_lock(&ec
->lock
);
856 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
857 list_del(&handler
->node
);
860 mutex_unlock(&ec
->lock
);
861 acpi_ec_remove_fs(device
);
862 acpi_driver_data(device
) = NULL
;
870 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
)
872 struct acpi_ec
*ec
= context
;
874 if (resource
->type
!= ACPI_RESOURCE_TYPE_IO
)
878 * The first address region returned is the data port, and
879 * the second address region returned is the status/command
882 if (ec
->data_addr
== 0)
883 ec
->data_addr
= resource
->data
.io
.minimum
;
884 else if (ec
->command_addr
== 0)
885 ec
->command_addr
= resource
->data
.io
.minimum
;
887 return AE_CTRL_TERMINATE
;
892 static int ec_install_handlers(struct acpi_ec
*ec
)
895 if (ec
->handlers_installed
)
897 status
= acpi_install_gpe_handler(NULL
, ec
->gpe
,
898 ACPI_GPE_EDGE_TRIGGERED
,
899 &acpi_ec_gpe_handler
, ec
);
900 if (ACPI_FAILURE(status
))
903 acpi_set_gpe_type(NULL
, ec
->gpe
, ACPI_GPE_TYPE_RUNTIME
);
904 acpi_enable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
906 status
= acpi_install_address_space_handler(ec
->handle
,
908 &acpi_ec_space_handler
,
909 &acpi_ec_space_setup
, ec
);
910 if (ACPI_FAILURE(status
)) {
911 acpi_remove_gpe_handler(NULL
, ec
->gpe
, &acpi_ec_gpe_handler
);
915 ec
->handlers_installed
= 1;
919 static int acpi_ec_start(struct acpi_device
*device
)
927 ec
= acpi_driver_data(device
);
932 ret
= ec_install_handlers(ec
);
934 /* EC is fully operational, allow queries */
935 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
936 ec_schedule_ec_poll(ec
);
940 static int acpi_ec_stop(struct acpi_device
*device
, int type
)
945 ec
= acpi_driver_data(device
);
948 ec_remove_handlers(ec
);
953 int __init
acpi_boot_ec_enable(void)
955 if (!boot_ec
|| boot_ec
->handlers_installed
)
957 if (!ec_install_handlers(boot_ec
)) {
964 int __init
acpi_ec_ecdt_probe(void)
968 struct acpi_table_ecdt
*ecdt_ptr
;
970 boot_ec
= make_acpi_ec();
974 * Generate a boot ec context
976 status
= acpi_get_table(ACPI_SIG_ECDT
, 1,
977 (struct acpi_table_header
**)&ecdt_ptr
);
978 if (ACPI_SUCCESS(status
)) {
979 pr_info(PREFIX
"EC description table is found, configuring boot EC\n");
980 boot_ec
->command_addr
= ecdt_ptr
->control
.address
;
981 boot_ec
->data_addr
= ecdt_ptr
->data
.address
;
982 boot_ec
->gpe
= ecdt_ptr
->gpe
;
983 boot_ec
->handle
= ACPI_ROOT_OBJECT
;
985 /* This workaround is needed only on some broken machines,
986 * which require early EC, but fail to provide ECDT */
988 printk(KERN_DEBUG PREFIX
"Look up EC in DSDT\n");
989 status
= acpi_get_devices(ec_device_ids
[0].id
, ec_parse_device
,
991 /* Check that acpi_get_devices actually find something */
992 if (ACPI_FAILURE(status
) || !boot_ec
->handle
)
994 /* We really need to limit this workaround, the only ASUS,
995 * which needs it, has fake EC._INI method, so use it as flag.
996 * Keep boot_ec struct as it will be needed soon.
998 if (ACPI_FAILURE(acpi_get_handle(boot_ec
->handle
, "_INI", &x
)))
1002 ret
= ec_install_handlers(boot_ec
);
1013 static int __init
acpi_ec_init(void)
1020 acpi_ec_dir
= proc_mkdir(ACPI_EC_CLASS
, acpi_root_dir
);
1024 /* Now register the driver for the EC */
1025 result
= acpi_bus_register_driver(&acpi_ec_driver
);
1027 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
1034 subsys_initcall(acpi_ec_init
);
1036 /* EC driver currently not unloadable */
1038 static void __exit
acpi_ec_exit(void)
1041 acpi_bus_unregister_driver(&acpi_ec_driver
);
1043 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);