2 * ec.c - ACPI Embedded Controller Driver (v2.0)
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/actypes.h>
42 #define _COMPONENT ACPI_EC_COMPONENT
43 ACPI_MODULE_NAME("ec");
44 #define ACPI_EC_COMPONENT 0x00100000
45 #define ACPI_EC_CLASS "embedded_controller"
46 #define ACPI_EC_HID "PNP0C09"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
50 #define PREFIX "ACPI: EC: "
51 /* EC status register */
52 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
53 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
54 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
55 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
58 ACPI_EC_COMMAND_READ
= 0x80,
59 ACPI_EC_COMMAND_WRITE
= 0x81,
60 ACPI_EC_BURST_ENABLE
= 0x82,
61 ACPI_EC_BURST_DISABLE
= 0x83,
62 ACPI_EC_COMMAND_QUERY
= 0x84,
66 ACPI_EC_EVENT_OBF_1
= 1, /* Output buffer full */
67 ACPI_EC_EVENT_IBF_0
, /* Input buffer empty */
70 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
71 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
74 EC_INTR
= 1, /* Output buffer full */
75 EC_POLL
, /* Input buffer empty */
76 } acpi_ec_mode
= EC_INTR
;
78 static int acpi_ec_remove(struct acpi_device
*device
, int type
);
79 static int acpi_ec_start(struct acpi_device
*device
);
80 static int acpi_ec_stop(struct acpi_device
*device
, int type
);
81 static int acpi_ec_add(struct acpi_device
*device
);
83 static struct acpi_driver acpi_ec_driver
= {
85 .class = ACPI_EC_CLASS
,
89 .remove
= acpi_ec_remove
,
90 .start
= acpi_ec_start
,
95 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
96 /* External interfaces use first EC only, so remember */
97 static struct acpi_ec
{
100 unsigned long command_addr
;
101 unsigned long data_addr
;
102 unsigned long global_lock
;
104 atomic_t query_pending
;
105 atomic_t event_count
;
106 wait_queue_head_t wait
;
107 } *boot_ec
, *first_ec
;
109 /* --------------------------------------------------------------------------
110 Transaction Management
111 -------------------------------------------------------------------------- */
113 static inline u8
acpi_ec_read_status(struct acpi_ec
*ec
)
115 return inb(ec
->command_addr
);
118 static inline u8
acpi_ec_read_data(struct acpi_ec
*ec
)
120 return inb(ec
->data_addr
);
123 static inline void acpi_ec_write_cmd(struct acpi_ec
*ec
, u8 command
)
125 outb(command
, ec
->command_addr
);
128 static inline void acpi_ec_write_data(struct acpi_ec
*ec
, u8 data
)
130 outb(data
, ec
->data_addr
);
133 static inline int acpi_ec_check_status(struct acpi_ec
*ec
, enum ec_event event
,
136 u8 status
= acpi_ec_read_status(ec
);
137 if (old_count
== atomic_read(&ec
->event_count
))
139 if (event
== ACPI_EC_EVENT_OBF_1
) {
140 if (status
& ACPI_EC_FLAG_OBF
)
142 } else if (event
== ACPI_EC_EVENT_IBF_0
) {
143 if (!(status
& ACPI_EC_FLAG_IBF
))
150 static int acpi_ec_wait(struct acpi_ec
*ec
, enum ec_event event
, unsigned count
)
152 if (acpi_ec_mode
== EC_POLL
) {
153 unsigned long delay
= jiffies
+ msecs_to_jiffies(ACPI_EC_DELAY
);
154 while (time_before(jiffies
, delay
)) {
155 if (acpi_ec_check_status(ec
, event
, 0))
159 if (wait_event_timeout(ec
->wait
,
160 acpi_ec_check_status(ec
, event
, count
),
161 msecs_to_jiffies(ACPI_EC_DELAY
)) ||
162 acpi_ec_check_status(ec
, event
, 0)) {
165 printk(KERN_ERR PREFIX
"acpi_ec_wait timeout,"
166 " status = %d, expect_event = %d\n",
167 acpi_ec_read_status(ec
), event
);
174 static int acpi_ec_transaction_unlocked(struct acpi_ec
*ec
, u8 command
,
175 const u8
* wdata
, unsigned wdata_len
,
176 u8
* rdata
, unsigned rdata_len
)
179 unsigned count
= atomic_read(&ec
->event_count
);
180 acpi_ec_write_cmd(ec
, command
);
182 for (; wdata_len
> 0; --wdata_len
) {
183 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, count
);
185 printk(KERN_ERR PREFIX
186 "write_cmd timeout, command = %d\n", command
);
189 count
= atomic_read(&ec
->event_count
);
190 acpi_ec_write_data(ec
, *(wdata
++));
194 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, count
);
196 printk(KERN_ERR PREFIX
197 "finish-write timeout, command = %d\n", command
);
200 } else if (command
== ACPI_EC_COMMAND_QUERY
) {
201 atomic_set(&ec
->query_pending
, 0);
204 for (; rdata_len
> 0; --rdata_len
) {
205 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_OBF_1
, count
);
207 printk(KERN_ERR PREFIX
"read timeout, command = %d\n",
211 count
= atomic_read(&ec
->event_count
);
212 *(rdata
++) = acpi_ec_read_data(ec
);
218 static int acpi_ec_transaction(struct acpi_ec
*ec
, u8 command
,
219 const u8
* wdata
, unsigned wdata_len
,
220 u8
* rdata
, unsigned rdata_len
)
225 if (!ec
|| (wdata_len
&& !wdata
) || (rdata_len
&& !rdata
))
229 memset(rdata
, 0, rdata_len
);
231 mutex_lock(&ec
->lock
);
232 if (ec
->global_lock
) {
233 status
= acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK
, &glk
);
234 if (ACPI_FAILURE(status
)) {
235 mutex_unlock(&ec
->lock
);
240 /* Make sure GPE is enabled before doing transaction */
241 acpi_enable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
243 status
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, 0);
245 printk(KERN_DEBUG PREFIX
246 "input buffer is not empty, aborting transaction\n");
250 status
= acpi_ec_transaction_unlocked(ec
, command
,
257 acpi_release_global_lock(glk
);
258 mutex_unlock(&ec
->lock
);
264 * Note: samsung nv5000 doesn't work with ec burst mode.
265 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
267 int acpi_ec_burst_enable(struct acpi_ec
*ec
)
270 return acpi_ec_transaction(ec
, ACPI_EC_BURST_ENABLE
, NULL
, 0, &d
, 1);
273 int acpi_ec_burst_disable(struct acpi_ec
*ec
)
275 return acpi_ec_transaction(ec
, ACPI_EC_BURST_DISABLE
, NULL
, 0, NULL
, 0);
278 static int acpi_ec_read(struct acpi_ec
*ec
, u8 address
, u8
* data
)
283 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_READ
,
289 static int acpi_ec_write(struct acpi_ec
*ec
, u8 address
, u8 data
)
291 u8 wdata
[2] = { address
, data
};
292 return acpi_ec_transaction(ec
, ACPI_EC_COMMAND_WRITE
,
297 * Externally callable EC access functions. For now, assume 1 EC only
299 int ec_burst_enable(void)
303 return acpi_ec_burst_enable(first_ec
);
306 EXPORT_SYMBOL(ec_burst_enable
);
308 int ec_burst_disable(void)
312 return acpi_ec_burst_disable(first_ec
);
315 EXPORT_SYMBOL(ec_burst_disable
);
317 int ec_read(u8 addr
, u8
* val
)
325 err
= acpi_ec_read(first_ec
, addr
, &temp_data
);
334 EXPORT_SYMBOL(ec_read
);
336 int ec_write(u8 addr
, u8 val
)
343 err
= acpi_ec_write(first_ec
, addr
, val
);
348 EXPORT_SYMBOL(ec_write
);
350 int ec_transaction(u8 command
,
351 const u8
* wdata
, unsigned wdata_len
,
352 u8
* rdata
, unsigned rdata_len
)
357 return acpi_ec_transaction(first_ec
, command
, wdata
,
358 wdata_len
, rdata
, rdata_len
);
361 EXPORT_SYMBOL(ec_transaction
);
363 static int acpi_ec_query(struct acpi_ec
*ec
, u8
* data
)
372 * Query the EC to find out which _Qxx method we need to evaluate.
373 * Note that successful completion of the query causes the ACPI_EC_SCI
374 * bit to be cleared (and thus clearing the interrupt source).
377 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_QUERY
, NULL
, 0, &d
, 1);
388 /* --------------------------------------------------------------------------
390 -------------------------------------------------------------------------- */
392 static void acpi_ec_gpe_query(void *ec_cxt
)
394 struct acpi_ec
*ec
= ec_cxt
;
398 if (!ec
|| acpi_ec_query(ec
, &value
))
401 snprintf(object_name
, 8, "_Q%2.2X", value
);
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Evaluating %s", object_name
));
405 acpi_evaluate_object(ec
->handle
, object_name
, NULL
, NULL
);
408 static u32
acpi_ec_gpe_handler(void *data
)
410 acpi_status status
= AE_OK
;
412 struct acpi_ec
*ec
= data
;
413 atomic_inc(&ec
->event_count
);
415 if (acpi_ec_mode
== EC_INTR
) {
419 value
= acpi_ec_read_status(ec
);
420 if ((value
& ACPI_EC_FLAG_SCI
) && !atomic_read(&ec
->query_pending
)) {
421 atomic_set(&ec
->query_pending
, 1);
423 acpi_os_execute(OSL_EC_BURST_HANDLER
, acpi_ec_gpe_query
,
427 return status
== AE_OK
?
428 ACPI_INTERRUPT_HANDLED
: ACPI_INTERRUPT_NOT_HANDLED
;
431 /* --------------------------------------------------------------------------
432 Address Space Management
433 -------------------------------------------------------------------------- */
436 acpi_ec_space_setup(acpi_handle region_handle
,
437 u32 function
, void *handler_context
, void **return_context
)
440 * The EC object is in the handler context and is needed
441 * when calling the acpi_ec_space_handler.
443 *return_context
= (function
!= ACPI_REGION_DEACTIVATE
) ?
444 handler_context
: NULL
;
450 acpi_ec_space_handler(u32 function
,
451 acpi_physical_address address
,
453 acpi_integer
* value
,
454 void *handler_context
, void *region_context
)
457 struct acpi_ec
*ec
= handler_context
;
459 acpi_integer f_v
= 0;
462 if ((address
> 0xFF) || !value
|| !handler_context
)
463 return AE_BAD_PARAMETER
;
465 if (bit_width
!= 8 && acpi_strict
) {
466 return AE_BAD_PARAMETER
;
473 result
= acpi_ec_read(ec
, (u8
) address
, (u8
*) & temp
);
476 result
= acpi_ec_write(ec
, (u8
) address
, (u8
) temp
);
486 if (function
== ACPI_READ
)
487 f_v
|= temp
<< 8 * i
;
488 if (function
== ACPI_WRITE
)
495 if (function
== ACPI_READ
) {
496 f_v
|= temp
<< 8 * i
;
503 return AE_BAD_PARAMETER
;
516 /* --------------------------------------------------------------------------
518 -------------------------------------------------------------------------- */
520 static struct proc_dir_entry
*acpi_ec_dir
;
522 static int acpi_ec_read_info(struct seq_file
*seq
, void *offset
)
524 struct acpi_ec
*ec
= seq
->private;
529 seq_printf(seq
, "gpe:\t\t\t0x%02x\n", (u32
) ec
->gpe
);
530 seq_printf(seq
, "ports:\t\t\t0x%02x, 0x%02x\n",
531 (unsigned)ec
->command_addr
, (unsigned)ec
->data_addr
);
532 seq_printf(seq
, "use global lock:\t%s\n",
533 ec
->global_lock
? "yes" : "no");
538 static int acpi_ec_info_open_fs(struct inode
*inode
, struct file
*file
)
540 return single_open(file
, acpi_ec_read_info
, PDE(inode
)->data
);
543 static struct file_operations acpi_ec_info_ops
= {
544 .open
= acpi_ec_info_open_fs
,
547 .release
= single_release
,
548 .owner
= THIS_MODULE
,
551 static int acpi_ec_add_fs(struct acpi_device
*device
)
553 struct proc_dir_entry
*entry
= NULL
;
555 if (!acpi_device_dir(device
)) {
556 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
558 if (!acpi_device_dir(device
))
562 entry
= create_proc_entry(ACPI_EC_FILE_INFO
, S_IRUGO
,
563 acpi_device_dir(device
));
567 entry
->proc_fops
= &acpi_ec_info_ops
;
568 entry
->data
= acpi_driver_data(device
);
569 entry
->owner
= THIS_MODULE
;
575 static int acpi_ec_remove_fs(struct acpi_device
*device
)
578 if (acpi_device_dir(device
)) {
579 remove_proc_entry(ACPI_EC_FILE_INFO
, acpi_device_dir(device
));
580 remove_proc_entry(acpi_device_bid(device
), acpi_ec_dir
);
581 acpi_device_dir(device
) = NULL
;
587 /* --------------------------------------------------------------------------
589 -------------------------------------------------------------------------- */
591 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
);
594 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
);
596 static struct acpi_ec
*make_acpi_ec(void)
598 struct acpi_ec
*ec
= kzalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
602 atomic_set(&ec
->query_pending
, 1);
603 atomic_set(&ec
->event_count
, 1);
604 mutex_init(&ec
->lock
);
605 init_waitqueue_head(&ec
->wait
);
610 static int acpi_ec_add(struct acpi_device
*device
)
612 acpi_status status
= AE_OK
;
613 struct acpi_ec
*ec
= NULL
;
618 strcpy(acpi_device_name(device
), ACPI_EC_DEVICE_NAME
);
619 strcpy(acpi_device_class(device
), ACPI_EC_CLASS
);
625 status
= ec_parse_device(device
->handle
, 0, ec
, NULL
);
626 if (status
!= AE_CTRL_TERMINATE
) {
631 /* Check if we found the boot EC */
633 if (boot_ec
->gpe
== ec
->gpe
) {
634 /* We might have incorrect info for GL at boot time */
635 mutex_lock(&boot_ec
->lock
);
636 boot_ec
->global_lock
= ec
->global_lock
;
637 mutex_unlock(&boot_ec
->lock
);
643 ec
->handle
= device
->handle
;
644 acpi_driver_data(device
) = ec
;
646 acpi_ec_add_fs(device
);
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "%s [%s] (gpe %d) interrupt mode.",
649 acpi_device_name(device
), acpi_device_bid(device
),
655 static int acpi_ec_remove(struct acpi_device
*device
, int type
)
662 ec
= acpi_driver_data(device
);
663 acpi_ec_remove_fs(device
);
664 acpi_driver_data(device
) = NULL
;
668 /* Don't touch boot EC */
675 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
)
677 struct acpi_ec
*ec
= context
;
679 if (resource
->type
!= ACPI_RESOURCE_TYPE_IO
)
683 * The first address region returned is the data port, and
684 * the second address region returned is the status/command
687 if (ec
->data_addr
== 0)
688 ec
->data_addr
= resource
->data
.io
.minimum
;
689 else if (ec
->command_addr
== 0)
690 ec
->command_addr
= resource
->data
.io
.minimum
;
692 return AE_CTRL_TERMINATE
;
697 static int ec_install_handlers(struct acpi_ec
*ec
)
700 status
= acpi_install_gpe_handler(NULL
, ec
->gpe
,
701 ACPI_GPE_EDGE_TRIGGERED
,
702 &acpi_ec_gpe_handler
, ec
);
703 if (ACPI_FAILURE(status
))
706 acpi_set_gpe_type(NULL
, ec
->gpe
, ACPI_GPE_TYPE_RUNTIME
);
707 acpi_enable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
709 status
= acpi_install_address_space_handler(ec
->handle
,
711 &acpi_ec_space_handler
,
712 &acpi_ec_space_setup
, ec
);
713 if (ACPI_FAILURE(status
)) {
714 acpi_remove_gpe_handler(NULL
, ec
->gpe
, &acpi_ec_gpe_handler
);
718 /* EC is fully operational, allow queries */
719 atomic_set(&ec
->query_pending
, 0);
724 static int acpi_ec_start(struct acpi_device
*device
)
731 ec
= acpi_driver_data(device
);
736 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
737 ec
->gpe
, ec
->command_addr
, ec
->data_addr
));
739 /* Boot EC is already working */
743 return ec_install_handlers(ec
);
746 static int acpi_ec_stop(struct acpi_device
*device
, int type
)
754 ec
= acpi_driver_data(device
);
758 /* Don't touch boot EC */
762 status
= acpi_remove_address_space_handler(ec
->handle
,
764 &acpi_ec_space_handler
);
765 if (ACPI_FAILURE(status
))
768 status
= acpi_remove_gpe_handler(NULL
, ec
->gpe
, &acpi_ec_gpe_handler
);
769 if (ACPI_FAILURE(status
))
776 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
)
780 struct acpi_ec
*ec
= context
;
781 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
782 ec_parse_io_ports
, ec
);
783 if (ACPI_FAILURE(status
))
786 /* Get GPE bit assignment (EC events). */
787 /* TODO: Add support for _GPE returning a package */
788 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &ec
->gpe
);
789 if (ACPI_FAILURE(status
))
792 /* Use the global lock for all EC transactions? */
793 acpi_evaluate_integer(handle
, "_GLK", NULL
, &ec
->global_lock
);
797 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
798 ec
->gpe
, ec
->command_addr
, ec
->data_addr
));
800 return AE_CTRL_TERMINATE
;
803 int __init
acpi_ec_ecdt_probe(void)
807 struct acpi_table_ecdt
*ecdt_ptr
;
809 boot_ec
= make_acpi_ec();
813 * Generate a boot ec context
816 status
= acpi_get_table(ACPI_SIG_ECDT
, 1,
817 (struct acpi_table_header
**)&ecdt_ptr
);
818 if (ACPI_FAILURE(status
))
821 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found ECDT"));
823 boot_ec
->command_addr
= ecdt_ptr
->control
.address
;
824 boot_ec
->data_addr
= ecdt_ptr
->data
.address
;
825 boot_ec
->gpe
= ecdt_ptr
->gpe
;
826 boot_ec
->handle
= ACPI_ROOT_OBJECT
;
828 ret
= ec_install_handlers(boot_ec
);
840 static int __init
acpi_ec_init(void)
847 acpi_ec_dir
= proc_mkdir(ACPI_EC_CLASS
, acpi_root_dir
);
851 /* Now register the driver for the EC */
852 result
= acpi_bus_register_driver(&acpi_ec_driver
);
854 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
861 subsys_initcall(acpi_ec_init
);
863 /* EC driver currently not unloadable */
865 static void __exit
acpi_ec_exit(void)
868 acpi_bus_unregister_driver(&acpi_ec_driver
);
870 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
876 static int __init
acpi_ec_set_intr_mode(char *str
)
880 if (!get_option(&str
, &intr
))
883 acpi_ec_mode
= (intr
) ? EC_INTR
: EC_POLL
;
885 printk(KERN_NOTICE PREFIX
"%s mode.\n", intr
? "interrupt" : "polling");
890 __setup("ec_intr=", acpi_ec_set_intr_mode
);