32 #include <linux/kernel.h>
33 #include <linux/module.h>
35 #include <linux/types.h>
38 #include <linux/list.h>
40 #include <linux/slab.h>
48 #define ACPI_EC_CLASS "embedded_controller"
49 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
50 #define ACPI_EC_FILE_INFO "info"
53 #define PREFIX "ACPI: EC: "
56 #define ACPI_EC_FLAG_OBF 0x01
57 #define ACPI_EC_FLAG_IBF 0x02
58 #define ACPI_EC_FLAG_BURST 0x10
59 #define ACPI_EC_FLAG_SCI 0x20
70 #define ACPI_EC_DELAY 500
71 #define ACPI_EC_UDELAY_GLK 1000
72 #define ACPI_EC_MSI_UDELAY 550
85 MODULE_PARM_DESC(ec_delay,
"Timeout(ms) waited until an EC command completes");
94 MODULE_PARM_DESC(ec_storm_threshold,
"Maxim false GPE numbers not considered as GPE storm");
123 static int EC_FLAGS_MSI;
124 static int EC_FLAGS_VALIDATE_ECDT;
125 static int EC_FLAGS_SKIP_DSDT_SCAN;
131 static inline u8 acpi_ec_read_status(
struct acpi_ec *ec)
138 static inline u8 acpi_ec_read_data(
struct acpi_ec *ec)
151 static inline void acpi_ec_write_data(
struct acpi_ec *ec,
u8 data)
157 static int ec_transaction_done(
struct acpi_ec *ec)
164 spin_unlock_irqrestore(&ec->
curr_lock, flags);
168 static void start_transaction(
struct acpi_ec *ec)
171 ec->
curr->done =
false;
172 acpi_ec_write_cmd(ec, ec->
curr->command);
181 if (ec->
curr->wlen > ec->
curr->wi) {
183 acpi_ec_write_data(ec,
187 }
else if (ec->
curr->rlen > ec->
curr->ri) {
189 ec->
curr->rdata[ec->
curr->ri++] = acpi_ec_read_data(ec);
191 ec->
curr->done =
true;
194 }
else if (ec->
curr->wlen == ec->
curr->wi &&
196 ec->
curr->done =
true;
201 ++ec->
curr->irq_count;
203 spin_unlock_irqrestore(&ec->
curr_lock, flags);
206 static int acpi_ec_sync_query(
struct acpi_ec *ec);
212 return acpi_ec_sync_query(ec);
217 static int ec_poll(
struct acpi_ec *ec)
228 if (ec_transaction_done(ec))
232 ec_transaction_done(ec),
236 advance_transaction(ec, acpi_ec_read_status(ec));
238 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
242 start_transaction(ec);
243 spin_unlock_irqrestore(&ec->
curr_lock, flags);
248 static int acpi_ec_transaction_unlocked(
struct acpi_ec *ec,
259 start_transaction(ec);
262 spin_unlock_irqrestore(&ec->
curr_lock, tmp);
266 spin_unlock_irqrestore(&ec->
curr_lock, tmp);
270 static int ec_check_ibf0(
struct acpi_ec *ec)
272 u8 status = acpi_ec_read_status(ec);
273 return (status & ACPI_EC_FLAG_IBF) == 0;
276 static int ec_wait_ibf0(
struct acpi_ec *ec)
307 if (ec_wait_ibf0(ec)) {
309 "aborting transaction\n");
320 status = acpi_ec_transaction_unlocked(ec, t);
323 ec_check_sci_sync(ec, acpi_ec_read_status(ec));
328 }
else if (t->
irq_count > ec_storm_threshold) {
330 "transactions will use polling mode\n");
342 static int acpi_ec_burst_enable(
struct acpi_ec *ec)
346 .wdata =
NULL, .rdata = &
d,
347 .wlen = 0, .rlen = 1};
349 return acpi_ec_transaction(ec, &t);
352 static int acpi_ec_burst_disable(
struct acpi_ec *ec)
356 .wlen = 0, .rlen = 0};
359 acpi_ec_transaction(ec, &t) : 0;
368 .wlen = 1, .rlen = 1};
370 result = acpi_ec_transaction(ec, &t);
380 .wlen = 2, .rlen = 0};
382 return acpi_ec_transaction(ec, &t);
392 return acpi_ec_burst_enable(first_ec);
401 return acpi_ec_burst_disable(first_ec);
414 err = acpi_ec_read(first_ec, addr, &temp_data);
432 err = acpi_ec_write(first_ec, addr, val);
440 const u8 *
wdata,
unsigned wdata_len,
441 u8 *
rdata,
unsigned rdata_len)
445 .wlen = wdata_len, .rlen = rdata_len};
449 return acpi_ec_transaction(first_ec, &t);
500 static int acpi_ec_query_unlocked(
struct acpi_ec *ec,
u8 *
data)
505 .wdata =
NULL, .rdata = &
d,
506 .wlen = 0, .rlen = 1};
514 result = acpi_ec_transaction_unlocked(ec, &t);
540 list_add(&handler->
node, &ec->
list);
562 static void acpi_ec_run(
void *cxt)
576 static int acpi_ec_sync_query(
struct acpi_ec *ec)
581 if ((status = acpi_ec_query_unlocked(ec, &value)))
589 memcpy(copy, handler,
sizeof(*copy));
590 pr_debug(
PREFIX "push query execution (0x%2x) on queue\n", value);
599 static void acpi_ec_gpe_query(
void *ec_cxt)
605 acpi_ec_sync_query(ec);
609 static int ec_check_sci(
struct acpi_ec *ec,
u8 state)
611 if (state & ACPI_EC_FLAG_SCI) {
615 acpi_ec_gpe_query, ec);
628 advance_transaction(ec, acpi_ec_read_status(ec));
629 if (ec_transaction_done(ec) &&
630 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
632 ec_check_sci(ec, acpi_ec_read_status(ec));
642 acpi_ec_space_handler(
u32 function, acpi_physical_address
address,
644 void *handler_context,
void *region_context)
646 struct acpi_ec *ec = handler_context;
647 int result = 0,
i,
bytes = bits / 8;
648 u8 *value = (
u8 *)value64;
650 if ((address > 0xFF) || !value || !handler_context)
656 if (EC_FLAGS_MSI || bits > 8)
657 acpi_ec_burst_enable(ec);
661 acpi_ec_read(ec, address, value) :
662 acpi_ec_write(ec, address, *value);
664 if (EC_FLAGS_MSI || bits > 8)
665 acpi_ec_burst_disable(ec);
688 static struct acpi_ec *make_acpi_ec(
void)
696 INIT_LIST_HEAD(&ec->
list);
723 unsigned long long tmp = 0;
731 ec_parse_io_ports, ec);
749 static int ec_install_handlers(
struct acpi_ec *ec)
756 &acpi_ec_gpe_handler, ec);
763 &acpi_ec_space_handler,
773 " of EC device. Broken bios is suspected.\n");
776 &acpi_ec_gpe_handler);
786 static void ec_remove_handlers(
struct acpi_ec *ec)
793 &acpi_ec_gpe_handler)))
798 static int acpi_ec_add(
struct acpi_device *
device)
808 (boot_ec->
handle == device->handle ||
817 if (ec_parse_device(device->handle, 0, ec,
NULL) !=
825 acpi_ec_register_query_methods,
NULL, ec,
NULL);
829 device->driver_data = ec;
832 WARN(!ret,
"Could not request EC data io port 0x%lx", ec->
data_addr);
836 pr_info(
PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
839 ret = ec_install_handlers(ec);
846 static int acpi_ec_remove(
struct acpi_device *device,
int type)
854 ec = acpi_driver_data(device);
855 ec_remove_handlers(ec);
864 device->driver_data =
NULL;
898 if (!ec_install_handlers(boot_ec)) {
913 EC_FLAGS_SKIP_DSDT_SCAN = 1;
920 EC_FLAGS_VALIDATE_ECDT = 1;
929 EC_FLAGS_VALIDATE_ECDT = 1;
937 static int ec_enlarge_storm_threshold(
const struct dmi_system_id *
id)
939 pr_debug(
"Setting the EC GPE storm threshold to 20\n");
940 ec_storm_threshold = 20;
946 ec_skip_dsdt_scan,
"Compal JFL92", {
950 ec_flag_msi,
"MSI hardware", {
953 ec_flag_msi,
"MSI hardware", {
956 ec_flag_msi,
"MSI hardware", {
959 ec_flag_msi,
"MSI hardware", {
962 ec_flag_msi,
"Quanta hardware", {
966 ec_flag_msi,
"Quanta hardware", {
970 ec_validate_ecdt,
"ASUS hardware", {
973 ec_validate_ecdt,
"ASUS hardware", {
976 ec_enlarge_storm_threshold,
"CLEVO hardware", {
988 boot_ec = make_acpi_ec();
998 pr_info(
PREFIX "EC description table is found, configuring boot EC\n");
1001 boot_ec->
gpe = ecdt_ptr->
gpe;
1005 if (!EC_FLAGS_VALIDATE_ECDT)
1013 if (EC_FLAGS_SKIP_DSDT_SCAN)
1028 saved_ec->
gpe != boot_ec->
gpe ||
1030 pr_info(PREFIX
"ASUSTek keeps feeding us with broken "
1031 "ECDT tables, which are very hard to workaround. "
1032 "Trying to use DSDT EC info instead. Please send "
1048 if (!ec_install_handlers(boot_ec)) {
1058 static struct acpi_driver acpi_ec_driver = {
1061 .ids = ec_device_ids,
1064 .remove = acpi_ec_remove,
1082 static void __exit acpi_ec_exit(
void)