Lines Matching full:ec
3 * ec.c - ACPI Embedded Controller Driver (v3)
17 #define pr_fmt(fmt) "ACPI: EC: " fmt
38 /* EC status register */
43 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
47 * This leads to lots of practical timing issues for the host EC driver.
48 * The following variations are defined (from the target EC firmware's
57 * kind of EC firmware has implemented an event queue and will
77 /* EC commands */
86 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
88 #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */
90 * when trying to clear the EC */
108 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
111 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
119 MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction");
123 MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes");
169 struct acpi_ec *ec; member
172 static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
173 static void advance_transaction(struct acpi_ec *ec);
194 * Splitters used by the developers to track the boundary of the EC
230 #define ec_dbg_ref(ec, fmt, ...) \ argument
231 ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
237 static bool acpi_ec_started(struct acpi_ec *ec) in acpi_ec_started() argument
239 return test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_started()
240 !test_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_started()
243 static bool acpi_ec_event_enabled(struct acpi_ec *ec) in acpi_ec_event_enabled() argument
248 * the EC transactions are allowed to be performed. in acpi_ec_event_enabled()
250 if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in acpi_ec_event_enabled()
256 * 1. true: The EC event handling is disabled before entering in acpi_ec_event_enabled()
258 * 2. false: The EC event handling is automatically disabled as in acpi_ec_event_enabled()
259 * soon as the EC driver is stopped. in acpi_ec_event_enabled()
262 return acpi_ec_started(ec); in acpi_ec_event_enabled()
264 return test_bit(EC_FLAGS_STARTED, &ec->flags); in acpi_ec_event_enabled()
267 static bool acpi_ec_flushed(struct acpi_ec *ec) in acpi_ec_flushed() argument
269 return ec->reference_count == 1; in acpi_ec_flushed()
273 * EC Registers
276 static inline u8 acpi_ec_read_status(struct acpi_ec *ec) in acpi_ec_read_status() argument
278 u8 x = inb(ec->command_addr); in acpi_ec_read_status()
291 static inline u8 acpi_ec_read_data(struct acpi_ec *ec) in acpi_ec_read_data() argument
293 u8 x = inb(ec->data_addr); in acpi_ec_read_data()
295 ec->timestamp = jiffies; in acpi_ec_read_data()
300 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) in acpi_ec_write_cmd() argument
303 outb(command, ec->command_addr); in acpi_ec_write_cmd()
304 ec->timestamp = jiffies; in acpi_ec_write_cmd()
307 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) in acpi_ec_write_data() argument
310 outb(data, ec->data_addr); in acpi_ec_write_data()
311 ec->timestamp = jiffies; in acpi_ec_write_data()
339 static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec) in acpi_ec_is_gpe_raised() argument
343 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status); in acpi_ec_is_gpe_raised()
347 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open) in acpi_ec_enable_gpe() argument
350 acpi_enable_gpe(NULL, ec->gpe); in acpi_ec_enable_gpe()
352 BUG_ON(ec->reference_count < 1); in acpi_ec_enable_gpe()
353 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); in acpi_ec_enable_gpe()
355 if (acpi_ec_is_gpe_raised(ec)) { in acpi_ec_enable_gpe()
362 advance_transaction(ec); in acpi_ec_enable_gpe()
366 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close) in acpi_ec_disable_gpe() argument
369 acpi_disable_gpe(NULL, ec->gpe); in acpi_ec_disable_gpe()
371 BUG_ON(ec->reference_count < 1); in acpi_ec_disable_gpe()
372 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); in acpi_ec_disable_gpe()
376 static inline void acpi_ec_clear_gpe(struct acpi_ec *ec) in acpi_ec_clear_gpe() argument
386 * EC commands will be sent without GPE raised. in acpi_ec_clear_gpe()
388 if (!acpi_ec_is_gpe_raised(ec)) in acpi_ec_clear_gpe()
390 acpi_clear_gpe(NULL, ec->gpe); in acpi_ec_clear_gpe()
397 static void acpi_ec_submit_request(struct acpi_ec *ec) in acpi_ec_submit_request() argument
399 ec->reference_count++; in acpi_ec_submit_request()
400 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && in acpi_ec_submit_request()
401 ec->gpe >= 0 && ec->reference_count == 1) in acpi_ec_submit_request()
402 acpi_ec_enable_gpe(ec, true); in acpi_ec_submit_request()
405 static void acpi_ec_complete_request(struct acpi_ec *ec) in acpi_ec_complete_request() argument
409 ec->reference_count--; in acpi_ec_complete_request()
410 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && in acpi_ec_complete_request()
411 ec->gpe >= 0 && ec->reference_count == 0) in acpi_ec_complete_request()
412 acpi_ec_disable_gpe(ec, true); in acpi_ec_complete_request()
413 flushed = acpi_ec_flushed(ec); in acpi_ec_complete_request()
415 wake_up(&ec->wait); in acpi_ec_complete_request()
418 static void acpi_ec_mask_events(struct acpi_ec *ec) in acpi_ec_mask_events() argument
420 if (!test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { in acpi_ec_mask_events()
421 if (ec->gpe >= 0) in acpi_ec_mask_events()
422 acpi_ec_disable_gpe(ec, false); in acpi_ec_mask_events()
424 disable_irq_nosync(ec->irq); in acpi_ec_mask_events()
427 set_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); in acpi_ec_mask_events()
431 static void acpi_ec_unmask_events(struct acpi_ec *ec) in acpi_ec_unmask_events() argument
433 if (test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { in acpi_ec_unmask_events()
434 clear_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); in acpi_ec_unmask_events()
435 if (ec->gpe >= 0) in acpi_ec_unmask_events()
436 acpi_ec_enable_gpe(ec, false); in acpi_ec_unmask_events()
438 enable_irq(ec->irq); in acpi_ec_unmask_events()
448 * @ec: the EC device
455 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec) in acpi_ec_submit_flushable_request() argument
457 if (!acpi_ec_started(ec)) in acpi_ec_submit_flushable_request()
459 acpi_ec_submit_request(ec); in acpi_ec_submit_flushable_request()
463 static void acpi_ec_submit_query(struct acpi_ec *ec) in acpi_ec_submit_query() argument
465 acpi_ec_mask_events(ec); in acpi_ec_submit_query()
466 if (!acpi_ec_event_enabled(ec)) in acpi_ec_submit_query()
468 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) { in acpi_ec_submit_query()
471 ec->nr_pending_queries++; in acpi_ec_submit_query()
472 ec->events_in_progress++; in acpi_ec_submit_query()
473 queue_work(ec_wq, &ec->work); in acpi_ec_submit_query()
477 static void acpi_ec_complete_query(struct acpi_ec *ec) in acpi_ec_complete_query() argument
479 if (test_and_clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) in acpi_ec_complete_query()
482 acpi_ec_unmask_events(ec); in acpi_ec_complete_query()
485 static inline void __acpi_ec_enable_event(struct acpi_ec *ec) in __acpi_ec_enable_event() argument
487 if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in __acpi_ec_enable_event()
493 advance_transaction(ec); in __acpi_ec_enable_event()
496 static inline void __acpi_ec_disable_event(struct acpi_ec *ec) in __acpi_ec_disable_event() argument
498 if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in __acpi_ec_disable_event()
503 * Process _Q events that might have accumulated in the EC.
504 * Run with locked ec mutex.
506 static void acpi_ec_clear(struct acpi_ec *ec) in acpi_ec_clear() argument
512 status = acpi_ec_query(ec, &value); in acpi_ec_clear()
517 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i); in acpi_ec_clear()
519 pr_info("%d stale EC events cleared\n", i); in acpi_ec_clear()
522 static void acpi_ec_enable_event(struct acpi_ec *ec) in acpi_ec_enable_event() argument
526 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_enable_event()
527 if (acpi_ec_started(ec)) in acpi_ec_enable_event()
528 __acpi_ec_enable_event(ec); in acpi_ec_enable_event()
529 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_enable_event()
533 acpi_ec_clear(ec); in acpi_ec_enable_event()
539 flush_workqueue(ec_wq); /* flush ec->work */ in __acpi_ec_flush_work()
543 static void acpi_ec_disable_event(struct acpi_ec *ec) in acpi_ec_disable_event() argument
547 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_disable_event()
548 __acpi_ec_disable_event(ec); in acpi_ec_disable_event()
549 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_disable_event()
568 static bool acpi_ec_guard_event(struct acpi_ec *ec) in acpi_ec_guard_event() argument
573 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_guard_event()
590 !test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags) || in acpi_ec_guard_event()
591 (ec->curr && ec->curr->command == ACPI_EC_COMMAND_QUERY)) in acpi_ec_guard_event()
593 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_guard_event()
597 static int ec_transaction_polled(struct acpi_ec *ec) in ec_transaction_polled() argument
602 spin_lock_irqsave(&ec->lock, flags); in ec_transaction_polled()
603 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL)) in ec_transaction_polled()
605 spin_unlock_irqrestore(&ec->lock, flags); in ec_transaction_polled()
609 static int ec_transaction_completed(struct acpi_ec *ec) in ec_transaction_completed() argument
614 spin_lock_irqsave(&ec->lock, flags); in ec_transaction_completed()
615 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE)) in ec_transaction_completed()
617 spin_unlock_irqrestore(&ec->lock, flags); in ec_transaction_completed()
621 static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag) in ec_transaction_transition() argument
623 ec->curr->flags |= flag; in ec_transaction_transition()
624 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) { in ec_transaction_transition()
627 acpi_ec_complete_query(ec); in ec_transaction_transition()
630 acpi_ec_complete_query(ec); in ec_transaction_transition()
633 set_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags); in ec_transaction_transition()
637 static void advance_transaction(struct acpi_ec *ec) in advance_transaction() argument
650 if (ec->gpe >= 0) in advance_transaction()
651 acpi_ec_clear_gpe(ec); in advance_transaction()
653 status = acpi_ec_read_status(ec); in advance_transaction()
654 t = ec->curr; in advance_transaction()
661 (!ec->nr_pending_queries || in advance_transaction()
662 test_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags))) { in advance_transaction()
663 clear_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags); in advance_transaction()
664 acpi_ec_complete_query(ec); in advance_transaction()
672 acpi_ec_write_data(ec, t->wdata[t->wi++]); in advance_transaction()
677 t->rdata[t->ri++] = acpi_ec_read_data(ec); in advance_transaction()
679 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); in advance_transaction()
689 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); in advance_transaction()
694 acpi_ec_write_cmd(ec, t->command); in advance_transaction()
695 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL); in advance_transaction()
709 acpi_ec_mask_events(ec); in advance_transaction()
714 acpi_ec_submit_query(ec); in advance_transaction()
716 wake_up(&ec->wait); in advance_transaction()
719 static void start_transaction(struct acpi_ec *ec) in start_transaction() argument
721 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0; in start_transaction()
722 ec->curr->flags = 0; in start_transaction()
725 static int ec_guard(struct acpi_ec *ec) in ec_guard() argument
727 unsigned long guard = usecs_to_jiffies(ec->polling_guard); in ec_guard()
728 unsigned long timeout = ec->timestamp + guard; in ec_guard()
730 /* Ensure guarding period before polling EC status */ in ec_guard()
732 if (ec->busy_polling) { in ec_guard()
734 if (ec_transaction_completed(ec)) in ec_guard()
748 if (!ec_transaction_polled(ec) && in ec_guard()
749 !acpi_ec_guard_event(ec)) in ec_guard()
751 if (wait_event_timeout(ec->wait, in ec_guard()
752 ec_transaction_completed(ec), in ec_guard()
760 static int ec_poll(struct acpi_ec *ec) in ec_poll() argument
769 if (!ec_guard(ec)) in ec_poll()
771 spin_lock_irqsave(&ec->lock, flags); in ec_poll()
772 advance_transaction(ec); in ec_poll()
773 spin_unlock_irqrestore(&ec->lock, flags); in ec_poll()
776 spin_lock_irqsave(&ec->lock, flags); in ec_poll()
777 start_transaction(ec); in ec_poll()
778 spin_unlock_irqrestore(&ec->lock, flags); in ec_poll()
783 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, in acpi_ec_transaction_unlocked() argument
790 spin_lock_irqsave(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
792 if (!acpi_ec_submit_flushable_request(ec)) { in acpi_ec_transaction_unlocked()
796 ec_dbg_ref(ec, "Increase command"); in acpi_ec_transaction_unlocked()
798 ec->curr = t; in acpi_ec_transaction_unlocked()
800 start_transaction(ec); in acpi_ec_transaction_unlocked()
801 spin_unlock_irqrestore(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
803 ret = ec_poll(ec); in acpi_ec_transaction_unlocked()
805 spin_lock_irqsave(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
807 acpi_ec_unmask_events(ec); in acpi_ec_transaction_unlocked()
809 ec->curr = NULL; in acpi_ec_transaction_unlocked()
811 acpi_ec_complete_request(ec); in acpi_ec_transaction_unlocked()
812 ec_dbg_ref(ec, "Decrease command"); in acpi_ec_transaction_unlocked()
814 spin_unlock_irqrestore(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
818 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t) in acpi_ec_transaction() argument
823 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata)) in acpi_ec_transaction()
828 mutex_lock(&ec->mutex); in acpi_ec_transaction()
829 if (ec->global_lock) { in acpi_ec_transaction()
837 status = acpi_ec_transaction_unlocked(ec, t); in acpi_ec_transaction()
839 if (ec->global_lock) in acpi_ec_transaction()
842 mutex_unlock(&ec->mutex); in acpi_ec_transaction()
846 static int acpi_ec_burst_enable(struct acpi_ec *ec) in acpi_ec_burst_enable() argument
853 return acpi_ec_transaction(ec, &t); in acpi_ec_burst_enable()
856 static int acpi_ec_burst_disable(struct acpi_ec *ec) in acpi_ec_burst_disable() argument
862 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ? in acpi_ec_burst_disable()
863 acpi_ec_transaction(ec, &t) : 0; in acpi_ec_burst_disable()
866 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data) in acpi_ec_read() argument
874 result = acpi_ec_transaction(ec, &t); in acpi_ec_read()
879 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) in acpi_ec_write() argument
886 return acpi_ec_transaction(ec, &t); in acpi_ec_write()
935 /* Get the handle to the EC device */
944 static void acpi_ec_start(struct acpi_ec *ec, bool resuming) in acpi_ec_start() argument
948 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_start()
949 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) { in acpi_ec_start()
950 ec_dbg_drv("Starting EC"); in acpi_ec_start()
953 acpi_ec_submit_request(ec); in acpi_ec_start()
954 ec_dbg_ref(ec, "Increase driver"); in acpi_ec_start()
956 ec_log_drv("EC started"); in acpi_ec_start()
958 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_start()
961 static bool acpi_ec_stopped(struct acpi_ec *ec) in acpi_ec_stopped() argument
966 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stopped()
967 flushed = acpi_ec_flushed(ec); in acpi_ec_stopped()
968 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stopped()
972 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending) in acpi_ec_stop() argument
976 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stop()
977 if (acpi_ec_started(ec)) { in acpi_ec_stop()
978 ec_dbg_drv("Stopping EC"); in acpi_ec_stop()
979 set_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_stop()
980 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stop()
981 wait_event(ec->wait, acpi_ec_stopped(ec)); in acpi_ec_stop()
982 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stop()
985 acpi_ec_complete_request(ec); in acpi_ec_stop()
986 ec_dbg_ref(ec, "Decrease driver"); in acpi_ec_stop()
988 __acpi_ec_disable_event(ec); in acpi_ec_stop()
989 clear_bit(EC_FLAGS_STARTED, &ec->flags); in acpi_ec_stop()
990 clear_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_stop()
991 ec_log_drv("EC stopped"); in acpi_ec_stop()
993 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stop()
996 static void acpi_ec_enter_noirq(struct acpi_ec *ec) in acpi_ec_enter_noirq() argument
1000 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_enter_noirq()
1001 ec->busy_polling = true; in acpi_ec_enter_noirq()
1002 ec->polling_guard = 0; in acpi_ec_enter_noirq()
1004 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_enter_noirq()
1007 static void acpi_ec_leave_noirq(struct acpi_ec *ec) in acpi_ec_leave_noirq() argument
1011 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_leave_noirq()
1012 ec->busy_polling = ec_busy_polling; in acpi_ec_leave_noirq()
1013 ec->polling_guard = ec_polling_guard; in acpi_ec_leave_noirq()
1015 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_leave_noirq()
1020 struct acpi_ec *ec = first_ec; in acpi_ec_block_transactions() local
1022 if (!ec) in acpi_ec_block_transactions()
1025 mutex_lock(&ec->mutex); in acpi_ec_block_transactions()
1027 acpi_ec_stop(ec, true); in acpi_ec_block_transactions()
1028 mutex_unlock(&ec->mutex); in acpi_ec_block_transactions()
1045 acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value) in acpi_ec_get_query_handler_by_value() argument
1049 mutex_lock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1050 list_for_each_entry(handler, &ec->list, node) { in acpi_ec_get_query_handler_by_value()
1053 mutex_unlock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1057 mutex_unlock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1074 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, in acpi_ec_add_query_handler() argument
1088 mutex_lock(&ec->mutex); in acpi_ec_add_query_handler()
1090 list_add(&handler->node, &ec->list); in acpi_ec_add_query_handler()
1091 mutex_unlock(&ec->mutex); in acpi_ec_add_query_handler()
1096 static void acpi_ec_remove_query_handlers(struct acpi_ec *ec, in acpi_ec_remove_query_handlers() argument
1102 mutex_lock(&ec->mutex); in acpi_ec_remove_query_handlers()
1103 list_for_each_entry_safe(handler, tmp, &ec->list, node) { in acpi_ec_remove_query_handlers()
1109 mutex_unlock(&ec->mutex); in acpi_ec_remove_query_handlers()
1114 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) in acpi_ec_remove_query_handler() argument
1116 acpi_ec_remove_query_handlers(ec, false, query_bit); in acpi_ec_remove_query_handler()
1121 static struct acpi_ec_query *acpi_ec_create_query(struct acpi_ec *ec, u8 *pval) in acpi_ec_create_query() argument
1135 q->ec = ec; in acpi_ec_create_query()
1152 struct acpi_ec *ec = q->ec; in acpi_ec_event_processor() local
1163 spin_lock_irq(&ec->lock); in acpi_ec_event_processor()
1164 ec->queries_in_progress--; in acpi_ec_event_processor()
1165 spin_unlock_irq(&ec->lock); in acpi_ec_event_processor()
1170 static int acpi_ec_query(struct acpi_ec *ec, u8 *data) in acpi_ec_query() argument
1176 q = acpi_ec_create_query(ec, &value); in acpi_ec_query()
1181 * Query the EC to find out which _Qxx method we need to evaluate. in acpi_ec_query()
1185 result = acpi_ec_transaction(ec, &q->transaction); in acpi_ec_query()
1191 q->handler = acpi_ec_get_query_handler_by_value(ec, value); in acpi_ec_query()
1206 spin_lock_irq(&ec->lock); in acpi_ec_query()
1208 ec->queries_in_progress++; in acpi_ec_query()
1211 spin_unlock_irq(&ec->lock); in acpi_ec_query()
1221 static void acpi_ec_check_event(struct acpi_ec *ec) in acpi_ec_check_event() argument
1226 if (ec_guard(ec)) { in acpi_ec_check_event()
1227 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_check_event()
1232 if (!ec->curr) in acpi_ec_check_event()
1233 advance_transaction(ec); in acpi_ec_check_event()
1234 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_check_event()
1242 struct acpi_ec *ec = container_of(work, struct acpi_ec, work); in acpi_ec_event_handler() local
1246 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_event_handler()
1247 while (ec->nr_pending_queries) { in acpi_ec_event_handler()
1248 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_event_handler()
1249 (void)acpi_ec_query(ec, NULL); in acpi_ec_event_handler()
1250 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_event_handler()
1251 ec->nr_pending_queries--; in acpi_ec_event_handler()
1258 if (!ec->nr_pending_queries) { in acpi_ec_event_handler()
1261 acpi_ec_complete_query(ec); in acpi_ec_event_handler()
1264 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_event_handler()
1268 acpi_ec_check_event(ec); in acpi_ec_event_handler()
1270 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_event_handler()
1271 ec->events_in_progress--; in acpi_ec_event_handler()
1272 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_event_handler()
1275 static void acpi_ec_handle_interrupt(struct acpi_ec *ec) in acpi_ec_handle_interrupt() argument
1279 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_handle_interrupt()
1280 advance_transaction(ec); in acpi_ec_handle_interrupt()
1281 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_handle_interrupt()
1306 struct acpi_ec *ec = handler_context; in acpi_ec_space_handler() local
1316 if (ec->busy_polling || bits > 8) in acpi_ec_space_handler()
1317 acpi_ec_burst_enable(ec); in acpi_ec_space_handler()
1321 acpi_ec_read(ec, address, value) : in acpi_ec_space_handler()
1322 acpi_ec_write(ec, address, *value); in acpi_ec_space_handler()
1324 if (ec->busy_polling || bits > 8) in acpi_ec_space_handler()
1325 acpi_ec_burst_disable(ec); in acpi_ec_space_handler()
1346 static void acpi_ec_free(struct acpi_ec *ec) in acpi_ec_free() argument
1348 if (first_ec == ec) in acpi_ec_free()
1350 if (boot_ec == ec) in acpi_ec_free()
1352 kfree(ec); in acpi_ec_free()
1357 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); in acpi_ec_alloc() local
1359 if (!ec) in acpi_ec_alloc()
1361 mutex_init(&ec->mutex); in acpi_ec_alloc()
1362 init_waitqueue_head(&ec->wait); in acpi_ec_alloc()
1363 INIT_LIST_HEAD(&ec->list); in acpi_ec_alloc()
1364 spin_lock_init(&ec->lock); in acpi_ec_alloc()
1365 INIT_WORK(&ec->work, acpi_ec_event_handler); in acpi_ec_alloc()
1366 ec->timestamp = jiffies; in acpi_ec_alloc()
1367 ec->busy_polling = true; in acpi_ec_alloc()
1368 ec->polling_guard = 0; in acpi_ec_alloc()
1369 ec->gpe = -1; in acpi_ec_alloc()
1370 ec->irq = -1; in acpi_ec_alloc()
1371 return ec; in acpi_ec_alloc()
1380 struct acpi_ec *ec = context; in acpi_ec_register_query_methods() local
1387 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); in acpi_ec_register_query_methods()
1396 struct acpi_ec *ec = context; in ec_parse_device() local
1399 ec->command_addr = ec->data_addr = 0; in ec_parse_device()
1402 ec_parse_io_ports, ec); in ec_parse_device()
1405 if (ec->data_addr == 0 || ec->command_addr == 0) in ec_parse_device()
1408 /* Get GPE bit assignment (EC events). */ in ec_parse_device()
1412 ec->gpe = tmp; in ec_parse_device()
1418 /* Use the global lock for all EC transactions? */ in ec_parse_device()
1421 ec->global_lock = tmp; in ec_parse_device()
1422 ec->handle = handle; in ec_parse_device()
1426 static bool install_gpe_event_handler(struct acpi_ec *ec) in install_gpe_event_handler() argument
1430 status = acpi_install_gpe_raw_handler(NULL, ec->gpe, in install_gpe_event_handler()
1432 &acpi_ec_gpe_handler, ec); in install_gpe_event_handler()
1436 if (test_bit(EC_FLAGS_STARTED, &ec->flags) && ec->reference_count >= 1) in install_gpe_event_handler()
1437 acpi_ec_enable_gpe(ec, true); in install_gpe_event_handler()
1442 static bool install_gpio_irq_event_handler(struct acpi_ec *ec) in install_gpio_irq_event_handler() argument
1444 return request_irq(ec->irq, acpi_ec_irq_handler, IRQF_SHARED, in install_gpio_irq_event_handler()
1445 "ACPI EC", ec) >= 0; in install_gpio_irq_event_handler()
1450 * @ec: Target EC.
1451 * @device: ACPI device object corresponding to @ec.
1453 * Install a handler for the EC address space type unless it has been installed
1454 * already. If @device is not NULL, also look for EC query methods in the
1456 * handler for the EC, if possible.
1464 static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device) in ec_install_handlers() argument
1468 acpi_ec_start(ec, false); in ec_install_handlers()
1470 if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { in ec_install_handlers()
1471 acpi_ec_enter_noirq(ec); in ec_install_handlers()
1472 status = acpi_install_address_space_handler(ec->handle, in ec_install_handlers()
1475 NULL, ec); in ec_install_handlers()
1477 acpi_ec_stop(ec, false); in ec_install_handlers()
1480 set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); in ec_install_handlers()
1486 if (ec->gpe < 0) { in ec_install_handlers()
1496 ec->irq = irq; in ec_install_handlers()
1499 if (!test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { in ec_install_handlers()
1501 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1, in ec_install_handlers()
1503 NULL, ec, NULL); in ec_install_handlers()
1504 set_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); in ec_install_handlers()
1506 if (!test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in ec_install_handlers()
1509 if (ec->gpe >= 0) in ec_install_handlers()
1510 ready = install_gpe_event_handler(ec); in ec_install_handlers()
1511 else if (ec->irq >= 0) in ec_install_handlers()
1512 ready = install_gpio_irq_event_handler(ec); in ec_install_handlers()
1515 set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); in ec_install_handlers()
1516 acpi_ec_leave_noirq(ec); in ec_install_handlers()
1520 * the EC can be polled for events. in ec_install_handlers()
1523 /* EC is fully operational, allow queries */ in ec_install_handlers()
1524 acpi_ec_enable_event(ec); in ec_install_handlers()
1529 static void ec_remove_handlers(struct acpi_ec *ec) in ec_remove_handlers() argument
1531 if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1532 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, in ec_remove_handlers()
1535 clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); in ec_remove_handlers()
1539 * Stops handling the EC transactions after removing the operation in ec_remove_handlers()
1541 * invoked during the removal can result in new EC transactions. in ec_remove_handlers()
1543 * Flushes the EC requests and thus disables the GPE before in ec_remove_handlers()
1549 acpi_ec_stop(ec, false); in ec_remove_handlers()
1551 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1552 if (ec->gpe >= 0 && in ec_remove_handlers()
1553 ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, in ec_remove_handlers()
1557 if (ec->irq >= 0) in ec_remove_handlers()
1558 free_irq(ec->irq, ec); in ec_remove_handlers()
1560 clear_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); in ec_remove_handlers()
1562 if (test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1563 acpi_ec_remove_query_handlers(ec, true, 0); in ec_remove_handlers()
1564 clear_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); in ec_remove_handlers()
1568 static int acpi_ec_setup(struct acpi_ec *ec, struct acpi_device *device) in acpi_ec_setup() argument
1572 ret = ec_install_handlers(ec, device); in acpi_ec_setup()
1576 /* First EC capable of handling transactions */ in acpi_ec_setup()
1578 first_ec = ec; in acpi_ec_setup()
1580 pr_info("EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n", ec->command_addr, in acpi_ec_setup()
1581 ec->data_addr); in acpi_ec_setup()
1583 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in acpi_ec_setup()
1584 if (ec->gpe >= 0) in acpi_ec_setup()
1585 pr_info("GPE=0x%x\n", ec->gpe); in acpi_ec_setup()
1587 pr_info("IRQ=%d\n", ec->irq); in acpi_ec_setup()
1595 struct acpi_ec *ec; in acpi_ec_add() local
1603 /* Fast path: this device corresponds to the boot EC. */ in acpi_ec_add()
1604 ec = boot_ec; in acpi_ec_add()
1608 ec = acpi_ec_alloc(); in acpi_ec_add()
1609 if (!ec) in acpi_ec_add()
1612 status = ec_parse_device(device->handle, 0, ec, NULL); in acpi_ec_add()
1618 if (boot_ec && ec->command_addr == boot_ec->command_addr && in acpi_ec_add()
1619 ec->data_addr == boot_ec->data_addr && in acpi_ec_add()
1625 * boot_ec->gpe to ec->gpe. in acpi_ec_add()
1627 boot_ec->handle = ec->handle; in acpi_ec_add()
1628 acpi_handle_debug(ec->handle, "duplicated.\n"); in acpi_ec_add()
1629 acpi_ec_free(ec); in acpi_ec_add()
1630 ec = boot_ec; in acpi_ec_add()
1634 ret = acpi_ec_setup(ec, device); in acpi_ec_add()
1638 if (ec == boot_ec) in acpi_ec_add()
1640 "Boot %s EC initialization complete\n", in acpi_ec_add()
1643 acpi_handle_info(ec->handle, in acpi_ec_add()
1644 "EC: Used to handle transactions and events\n"); in acpi_ec_add()
1646 device->driver_data = ec; in acpi_ec_add()
1648 ret = !!request_region(ec->data_addr, 1, "EC data"); in acpi_ec_add()
1649 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr); in acpi_ec_add()
1650 ret = !!request_region(ec->command_addr, 1, "EC cmd"); in acpi_ec_add()
1651 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr); in acpi_ec_add()
1653 /* Reprobe devices depending on the EC */ in acpi_ec_add()
1654 acpi_walk_dep_device_list(ec->handle); in acpi_ec_add()
1656 acpi_handle_debug(ec->handle, "enumerated.\n"); in acpi_ec_add()
1660 if (ec != boot_ec) in acpi_ec_add()
1661 acpi_ec_free(ec); in acpi_ec_add()
1668 struct acpi_ec *ec; in acpi_ec_remove() local
1673 ec = acpi_driver_data(device); in acpi_ec_remove()
1674 release_region(ec->data_addr, 1); in acpi_ec_remove()
1675 release_region(ec->command_addr, 1); in acpi_ec_remove()
1677 if (ec != boot_ec) { in acpi_ec_remove()
1678 ec_remove_handlers(ec); in acpi_ec_remove()
1679 acpi_ec_free(ec); in acpi_ec_remove()
1687 struct acpi_ec *ec = context; in ec_parse_io_ports() local
1697 if (ec->data_addr == 0) in ec_parse_io_ports()
1698 ec->data_addr = resource->data.io.minimum; in ec_parse_io_ports()
1699 else if (ec->command_addr == 0) in ec_parse_io_ports()
1700 ec->command_addr = resource->data.io.minimum; in ec_parse_io_ports()
1715 * namespace EC before the main ACPI device enumeration process. It is
1720 struct acpi_ec *ec; in acpi_ec_dsdt_probe() local
1728 * picking up an invalid EC device. in acpi_ec_dsdt_probe()
1733 ec = acpi_ec_alloc(); in acpi_ec_dsdt_probe()
1734 if (!ec) in acpi_ec_dsdt_probe()
1741 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, ec, NULL); in acpi_ec_dsdt_probe()
1742 if (ACPI_FAILURE(status) || !ec->handle) { in acpi_ec_dsdt_probe()
1743 acpi_ec_free(ec); in acpi_ec_dsdt_probe()
1748 * When the DSDT EC is available, always re-configure boot EC to in acpi_ec_dsdt_probe()
1754 ret = acpi_ec_setup(ec, NULL); in acpi_ec_dsdt_probe()
1756 acpi_ec_free(ec); in acpi_ec_dsdt_probe()
1760 boot_ec = ec; in acpi_ec_dsdt_probe()
1762 acpi_handle_info(ec->handle, in acpi_ec_dsdt_probe()
1763 "Boot DSDT EC used to handle transactions\n"); in acpi_ec_dsdt_probe()
1767 * acpi_ec_ecdt_start - Finalize the boot ECDT EC initialization.
1769 * First, look for an ACPI handle for the boot ECDT EC if acpi_ec_add() has not
1772 * Next, in case the DSDT EC is not functioning, it is still necessary to
1773 * provide a functional ECDT EC to handle events, so add an extra device object
1776 * This is useful on platforms with valid ECDT and invalid DSDT EC settings,
1785 /* Bail out if a matching EC has been found in the namespace. */ in acpi_ec_ecdt_start()
1799 /* Add a special ACPI device object to represent the boot EC. */ in acpi_ec_ecdt_start()
1807 * On some hardware it is necessary to clear events accumulated by the EC during
1813 * Ideally, the EC should also be instructed NOT to accumulate events during
1825 pr_debug("Detected system needing EC poll on resume.\n"); in ec_clear_on_resume()
1845 * with DSDT EC, don't duplicate the DSDT EC with ECDT EC in this case.
1874 struct acpi_ec *ec; in acpi_ec_ecdt_probe() local
1878 /* Generate a boot ec context. */ in acpi_ec_ecdt_probe()
1893 ec = acpi_ec_alloc(); in acpi_ec_ecdt_probe()
1894 if (!ec) in acpi_ec_ecdt_probe()
1898 ec->command_addr = ecdt_ptr->data.address; in acpi_ec_ecdt_probe()
1899 ec->data_addr = ecdt_ptr->control.address; in acpi_ec_ecdt_probe()
1901 ec->command_addr = ecdt_ptr->control.address; in acpi_ec_ecdt_probe()
1902 ec->data_addr = ecdt_ptr->data.address; in acpi_ec_ecdt_probe()
1910 ec->gpe = ecdt_ptr->gpe; in acpi_ec_ecdt_probe()
1912 ec->handle = ACPI_ROOT_OBJECT; in acpi_ec_ecdt_probe()
1918 ret = acpi_ec_setup(ec, NULL); in acpi_ec_ecdt_probe()
1920 acpi_ec_free(ec); in acpi_ec_ecdt_probe()
1924 boot_ec = ec; in acpi_ec_ecdt_probe()
1927 pr_info("Boot ECDT EC used to handle transactions\n"); in acpi_ec_ecdt_probe()
1936 struct acpi_ec *ec = in acpi_ec_suspend() local
1940 acpi_ec_disable_event(ec); in acpi_ec_suspend()
1946 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); in acpi_ec_suspend_noirq() local
1952 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_suspend_noirq()
1953 ec->gpe >= 0 && ec->reference_count >= 1) in acpi_ec_suspend_noirq()
1954 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); in acpi_ec_suspend_noirq()
1956 acpi_ec_enter_noirq(ec); in acpi_ec_suspend_noirq()
1963 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); in acpi_ec_resume_noirq() local
1965 acpi_ec_leave_noirq(ec); in acpi_ec_resume_noirq()
1967 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_resume_noirq()
1968 ec->gpe >= 0 && ec->reference_count >= 1) in acpi_ec_resume_noirq()
1969 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); in acpi_ec_resume_noirq()
1976 struct acpi_ec *ec = in acpi_ec_resume() local
1979 acpi_ec_enable_event(ec); in acpi_ec_resume()
2006 * than the EC one. in acpi_ec_dispatch_gpe()
2012 * Dispatch the EC GPE in-band, but do not report wakeup in any case in acpi_ec_dispatch_gpe()
2017 pm_pr_dbg("ACPI EC GPE dispatched\n"); in acpi_ec_dispatch_gpe()
2019 /* Drain EC work. */ in acpi_ec_dispatch_gpe()
2023 pm_pr_dbg("ACPI EC work flushed\n"); in acpi_ec_dispatch_gpe()
2082 .name = "ec",
2146 * Disable EC wakeup on following systems to prevent periodic in acpi_ec_init()
2147 * wakeup from EC GPE. in acpi_ec_init()
2151 pr_debug("Disabling EC wakeup on suspend-to-idle\n"); in acpi_ec_init()
2160 /* EC driver currently not unloadable */