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 */
106 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
109 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
117 MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction");
121 MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes");
167 struct acpi_ec *ec; member
170 static int acpi_ec_submit_query(struct acpi_ec *ec);
171 static void advance_transaction(struct acpi_ec *ec, bool interrupt);
191 * Splitters used by the developers to track the boundary of the EC
227 #define ec_dbg_ref(ec, fmt, ...) \ argument
228 ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
234 static bool acpi_ec_started(struct acpi_ec *ec) in acpi_ec_started() argument
236 return test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_started()
237 !test_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_started()
240 static bool acpi_ec_event_enabled(struct acpi_ec *ec) in acpi_ec_event_enabled() argument
245 * the EC transactions are allowed to be performed. in acpi_ec_event_enabled()
247 if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in acpi_ec_event_enabled()
253 * 1. true: The EC event handling is disabled before entering in acpi_ec_event_enabled()
255 * 2. false: The EC event handling is automatically disabled as in acpi_ec_event_enabled()
256 * soon as the EC driver is stopped. in acpi_ec_event_enabled()
259 return acpi_ec_started(ec); in acpi_ec_event_enabled()
261 return test_bit(EC_FLAGS_STARTED, &ec->flags); in acpi_ec_event_enabled()
264 static bool acpi_ec_flushed(struct acpi_ec *ec) in acpi_ec_flushed() argument
266 return ec->reference_count == 1; in acpi_ec_flushed()
270 * EC Registers
273 static inline u8 acpi_ec_read_status(struct acpi_ec *ec) in acpi_ec_read_status() argument
275 u8 x = inb(ec->command_addr); in acpi_ec_read_status()
288 static inline u8 acpi_ec_read_data(struct acpi_ec *ec) in acpi_ec_read_data() argument
290 u8 x = inb(ec->data_addr); in acpi_ec_read_data()
292 ec->timestamp = jiffies; in acpi_ec_read_data()
297 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) in acpi_ec_write_cmd() argument
300 outb(command, ec->command_addr); in acpi_ec_write_cmd()
301 ec->timestamp = jiffies; in acpi_ec_write_cmd()
304 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) in acpi_ec_write_data() argument
307 outb(data, ec->data_addr); in acpi_ec_write_data()
308 ec->timestamp = jiffies; in acpi_ec_write_data()
336 static inline bool acpi_ec_gpe_status_set(struct acpi_ec *ec) in acpi_ec_gpe_status_set() argument
340 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status); in acpi_ec_gpe_status_set()
344 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open) in acpi_ec_enable_gpe() argument
347 acpi_enable_gpe(NULL, ec->gpe); in acpi_ec_enable_gpe()
349 BUG_ON(ec->reference_count < 1); in acpi_ec_enable_gpe()
350 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); in acpi_ec_enable_gpe()
352 if (acpi_ec_gpe_status_set(ec)) { in acpi_ec_enable_gpe()
359 advance_transaction(ec, false); in acpi_ec_enable_gpe()
363 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close) in acpi_ec_disable_gpe() argument
366 acpi_disable_gpe(NULL, ec->gpe); in acpi_ec_disable_gpe()
368 BUG_ON(ec->reference_count < 1); in acpi_ec_disable_gpe()
369 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); in acpi_ec_disable_gpe()
377 static void acpi_ec_submit_request(struct acpi_ec *ec) in acpi_ec_submit_request() argument
379 ec->reference_count++; in acpi_ec_submit_request()
380 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && in acpi_ec_submit_request()
381 ec->gpe >= 0 && ec->reference_count == 1) in acpi_ec_submit_request()
382 acpi_ec_enable_gpe(ec, true); in acpi_ec_submit_request()
385 static void acpi_ec_complete_request(struct acpi_ec *ec) in acpi_ec_complete_request() argument
389 ec->reference_count--; in acpi_ec_complete_request()
390 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && in acpi_ec_complete_request()
391 ec->gpe >= 0 && ec->reference_count == 0) in acpi_ec_complete_request()
392 acpi_ec_disable_gpe(ec, true); in acpi_ec_complete_request()
393 flushed = acpi_ec_flushed(ec); in acpi_ec_complete_request()
395 wake_up(&ec->wait); in acpi_ec_complete_request()
398 static void acpi_ec_mask_events(struct acpi_ec *ec) in acpi_ec_mask_events() argument
400 if (!test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { in acpi_ec_mask_events()
401 if (ec->gpe >= 0) in acpi_ec_mask_events()
402 acpi_ec_disable_gpe(ec, false); in acpi_ec_mask_events()
404 disable_irq_nosync(ec->irq); in acpi_ec_mask_events()
407 set_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); in acpi_ec_mask_events()
411 static void acpi_ec_unmask_events(struct acpi_ec *ec) in acpi_ec_unmask_events() argument
413 if (test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { in acpi_ec_unmask_events()
414 clear_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); in acpi_ec_unmask_events()
415 if (ec->gpe >= 0) in acpi_ec_unmask_events()
416 acpi_ec_enable_gpe(ec, false); in acpi_ec_unmask_events()
418 enable_irq(ec->irq); in acpi_ec_unmask_events()
428 * @ec: the EC device
435 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec) in acpi_ec_submit_flushable_request() argument
437 if (!acpi_ec_started(ec)) in acpi_ec_submit_flushable_request()
439 acpi_ec_submit_request(ec); in acpi_ec_submit_flushable_request()
443 static void acpi_ec_submit_event(struct acpi_ec *ec) in acpi_ec_submit_event() argument
449 acpi_ec_mask_events(ec); in acpi_ec_submit_event()
450 if (!acpi_ec_event_enabled(ec)) in acpi_ec_submit_event()
453 if (ec->event_state != EC_EVENT_READY) in acpi_ec_submit_event()
459 ec->event_state = EC_EVENT_IN_PROGRESS; in acpi_ec_submit_event()
467 if (ec->events_to_process++ > 0) in acpi_ec_submit_event()
470 ec->events_in_progress++; in acpi_ec_submit_event()
471 queue_work(ec_wq, &ec->work); in acpi_ec_submit_event()
474 static void acpi_ec_complete_event(struct acpi_ec *ec) in acpi_ec_complete_event() argument
476 if (ec->event_state == EC_EVENT_IN_PROGRESS) in acpi_ec_complete_event()
477 ec->event_state = EC_EVENT_COMPLETE; in acpi_ec_complete_event()
480 static void acpi_ec_close_event(struct acpi_ec *ec) in acpi_ec_close_event() argument
482 if (ec->event_state != EC_EVENT_READY) in acpi_ec_close_event()
486 ec->event_state = EC_EVENT_READY; in acpi_ec_close_event()
487 acpi_ec_unmask_events(ec); in acpi_ec_close_event()
490 static inline void __acpi_ec_enable_event(struct acpi_ec *ec) in __acpi_ec_enable_event() argument
492 if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in __acpi_ec_enable_event()
498 advance_transaction(ec, false); in __acpi_ec_enable_event()
501 static inline void __acpi_ec_disable_event(struct acpi_ec *ec) in __acpi_ec_disable_event() argument
503 if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) in __acpi_ec_disable_event()
508 * Process _Q events that might have accumulated in the EC.
509 * Run with locked ec mutex.
511 static void acpi_ec_clear(struct acpi_ec *ec) in acpi_ec_clear() argument
516 if (acpi_ec_submit_query(ec)) in acpi_ec_clear()
520 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i); in acpi_ec_clear()
522 pr_info("%d stale EC events cleared\n", i); in acpi_ec_clear()
525 static void acpi_ec_enable_event(struct acpi_ec *ec) in acpi_ec_enable_event() argument
529 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_enable_event()
530 if (acpi_ec_started(ec)) in acpi_ec_enable_event()
531 __acpi_ec_enable_event(ec); in acpi_ec_enable_event()
532 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_enable_event()
536 acpi_ec_clear(ec); in acpi_ec_enable_event()
542 flush_workqueue(ec_wq); /* flush ec->work */ in __acpi_ec_flush_work()
546 static void acpi_ec_disable_event(struct acpi_ec *ec) in acpi_ec_disable_event() argument
550 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_disable_event()
551 __acpi_ec_disable_event(ec); in acpi_ec_disable_event()
552 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_disable_event()
571 static bool acpi_ec_guard_event(struct acpi_ec *ec) in acpi_ec_guard_event() argument
576 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_guard_event()
590 ec->event_state != EC_EVENT_READY && in acpi_ec_guard_event()
591 (!ec->curr || ec->curr->command != ACPI_EC_COMMAND_QUERY); in acpi_ec_guard_event()
592 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_guard_event()
596 static int ec_transaction_polled(struct acpi_ec *ec) in ec_transaction_polled() argument
601 spin_lock_irqsave(&ec->lock, flags); in ec_transaction_polled()
602 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL)) in ec_transaction_polled()
604 spin_unlock_irqrestore(&ec->lock, flags); in ec_transaction_polled()
608 static int ec_transaction_completed(struct acpi_ec *ec) in ec_transaction_completed() argument
613 spin_lock_irqsave(&ec->lock, flags); in ec_transaction_completed()
614 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE)) in ec_transaction_completed()
616 spin_unlock_irqrestore(&ec->lock, flags); in ec_transaction_completed()
620 static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag) in ec_transaction_transition() argument
622 ec->curr->flags |= flag; in ec_transaction_transition()
624 if (ec->curr->command != ACPI_EC_COMMAND_QUERY) in ec_transaction_transition()
630 acpi_ec_close_event(ec); in ec_transaction_transition()
636 acpi_ec_close_event(ec); in ec_transaction_transition()
642 acpi_ec_complete_event(ec); in ec_transaction_transition()
646 static void acpi_ec_spurious_interrupt(struct acpi_ec *ec, struct transaction *t) in acpi_ec_spurious_interrupt() argument
653 acpi_ec_mask_events(ec); in acpi_ec_spurious_interrupt()
656 static void advance_transaction(struct acpi_ec *ec, bool interrupt) in advance_transaction() argument
658 struct transaction *t = ec->curr; in advance_transaction()
676 if (ec->gpe >= 0 && acpi_ec_gpe_status_set(ec)) in advance_transaction()
677 acpi_clear_gpe(NULL, ec->gpe); in advance_transaction()
679 status = acpi_ec_read_status(ec); in advance_transaction()
687 ec->event_state == EC_EVENT_COMPLETE) in advance_transaction()
688 acpi_ec_close_event(ec); in advance_transaction()
697 acpi_ec_write_data(ec, t->wdata[t->wi++]); in advance_transaction()
699 acpi_ec_spurious_interrupt(ec, t); in advance_transaction()
702 t->rdata[t->ri++] = acpi_ec_read_data(ec); in advance_transaction()
704 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); in advance_transaction()
711 acpi_ec_spurious_interrupt(ec, t); in advance_transaction()
714 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); in advance_transaction()
718 acpi_ec_write_cmd(ec, t->command); in advance_transaction()
719 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL); in advance_transaction()
724 acpi_ec_submit_event(ec); in advance_transaction()
727 wake_up(&ec->wait); in advance_transaction()
730 static void start_transaction(struct acpi_ec *ec) in start_transaction() argument
732 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0; in start_transaction()
733 ec->curr->flags = 0; in start_transaction()
736 static int ec_guard(struct acpi_ec *ec) in ec_guard() argument
738 unsigned long guard = usecs_to_jiffies(ec->polling_guard); in ec_guard()
739 unsigned long timeout = ec->timestamp + guard; in ec_guard()
741 /* Ensure guarding period before polling EC status */ in ec_guard()
743 if (ec->busy_polling) { in ec_guard()
745 if (ec_transaction_completed(ec)) in ec_guard()
759 if (!ec_transaction_polled(ec) && in ec_guard()
760 !acpi_ec_guard_event(ec)) in ec_guard()
762 if (wait_event_timeout(ec->wait, in ec_guard()
763 ec_transaction_completed(ec), in ec_guard()
771 static int ec_poll(struct acpi_ec *ec) in ec_poll() argument
780 if (!ec_guard(ec)) in ec_poll()
782 spin_lock_irqsave(&ec->lock, flags); in ec_poll()
783 advance_transaction(ec, false); in ec_poll()
784 spin_unlock_irqrestore(&ec->lock, flags); in ec_poll()
787 spin_lock_irqsave(&ec->lock, flags); in ec_poll()
788 start_transaction(ec); in ec_poll()
789 spin_unlock_irqrestore(&ec->lock, flags); in ec_poll()
794 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, in acpi_ec_transaction_unlocked() argument
801 spin_lock_irqsave(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
803 if (!acpi_ec_submit_flushable_request(ec)) { in acpi_ec_transaction_unlocked()
807 ec_dbg_ref(ec, "Increase command"); in acpi_ec_transaction_unlocked()
809 ec->curr = t; in acpi_ec_transaction_unlocked()
811 start_transaction(ec); in acpi_ec_transaction_unlocked()
812 spin_unlock_irqrestore(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
814 ret = ec_poll(ec); in acpi_ec_transaction_unlocked()
816 spin_lock_irqsave(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
818 acpi_ec_unmask_events(ec); in acpi_ec_transaction_unlocked()
820 ec->curr = NULL; in acpi_ec_transaction_unlocked()
822 acpi_ec_complete_request(ec); in acpi_ec_transaction_unlocked()
823 ec_dbg_ref(ec, "Decrease command"); in acpi_ec_transaction_unlocked()
825 spin_unlock_irqrestore(&ec->lock, tmp); in acpi_ec_transaction_unlocked()
829 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t) in acpi_ec_transaction() argument
834 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata)) in acpi_ec_transaction()
839 mutex_lock(&ec->mutex); in acpi_ec_transaction()
840 if (ec->global_lock) { in acpi_ec_transaction()
848 status = acpi_ec_transaction_unlocked(ec, t); in acpi_ec_transaction()
850 if (ec->global_lock) in acpi_ec_transaction()
853 mutex_unlock(&ec->mutex); in acpi_ec_transaction()
857 static int acpi_ec_burst_enable(struct acpi_ec *ec) in acpi_ec_burst_enable() argument
864 return acpi_ec_transaction(ec, &t); in acpi_ec_burst_enable()
867 static int acpi_ec_burst_disable(struct acpi_ec *ec) in acpi_ec_burst_disable() argument
873 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ? in acpi_ec_burst_disable()
874 acpi_ec_transaction(ec, &t) : 0; in acpi_ec_burst_disable()
877 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data) in acpi_ec_read() argument
885 result = acpi_ec_transaction(ec, &t); in acpi_ec_read()
890 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) in acpi_ec_write() argument
897 return acpi_ec_transaction(ec, &t); in acpi_ec_write()
942 /* Get the handle to the EC device */
951 static void acpi_ec_start(struct acpi_ec *ec, bool resuming) in acpi_ec_start() argument
955 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_start()
956 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) { in acpi_ec_start()
957 ec_dbg_drv("Starting EC"); in acpi_ec_start()
960 acpi_ec_submit_request(ec); in acpi_ec_start()
961 ec_dbg_ref(ec, "Increase driver"); in acpi_ec_start()
963 ec_log_drv("EC started"); in acpi_ec_start()
965 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_start()
968 static bool acpi_ec_stopped(struct acpi_ec *ec) in acpi_ec_stopped() argument
973 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stopped()
974 flushed = acpi_ec_flushed(ec); in acpi_ec_stopped()
975 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stopped()
979 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending) in acpi_ec_stop() argument
983 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stop()
984 if (acpi_ec_started(ec)) { in acpi_ec_stop()
985 ec_dbg_drv("Stopping EC"); in acpi_ec_stop()
986 set_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_stop()
987 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stop()
988 wait_event(ec->wait, acpi_ec_stopped(ec)); in acpi_ec_stop()
989 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_stop()
992 acpi_ec_complete_request(ec); in acpi_ec_stop()
993 ec_dbg_ref(ec, "Decrease driver"); in acpi_ec_stop()
995 __acpi_ec_disable_event(ec); in acpi_ec_stop()
996 clear_bit(EC_FLAGS_STARTED, &ec->flags); in acpi_ec_stop()
997 clear_bit(EC_FLAGS_STOPPED, &ec->flags); in acpi_ec_stop()
998 ec_log_drv("EC stopped"); in acpi_ec_stop()
1000 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_stop()
1003 static void acpi_ec_enter_noirq(struct acpi_ec *ec) in acpi_ec_enter_noirq() argument
1007 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_enter_noirq()
1008 ec->busy_polling = true; in acpi_ec_enter_noirq()
1009 ec->polling_guard = 0; in acpi_ec_enter_noirq()
1011 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_enter_noirq()
1014 static void acpi_ec_leave_noirq(struct acpi_ec *ec) in acpi_ec_leave_noirq() argument
1018 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_leave_noirq()
1019 ec->busy_polling = ec_busy_polling; in acpi_ec_leave_noirq()
1020 ec->polling_guard = ec_polling_guard; in acpi_ec_leave_noirq()
1022 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_leave_noirq()
1027 struct acpi_ec *ec = first_ec; in acpi_ec_block_transactions() local
1029 if (!ec) in acpi_ec_block_transactions()
1032 mutex_lock(&ec->mutex); in acpi_ec_block_transactions()
1034 acpi_ec_stop(ec, true); in acpi_ec_block_transactions()
1035 mutex_unlock(&ec->mutex); in acpi_ec_block_transactions()
1052 acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value) in acpi_ec_get_query_handler_by_value() argument
1056 mutex_lock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1057 list_for_each_entry(handler, &ec->list, node) { in acpi_ec_get_query_handler_by_value()
1060 mutex_unlock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1064 mutex_unlock(&ec->mutex); in acpi_ec_get_query_handler_by_value()
1081 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, in acpi_ec_add_query_handler() argument
1095 mutex_lock(&ec->mutex); in acpi_ec_add_query_handler()
1097 list_add(&handler->node, &ec->list); in acpi_ec_add_query_handler()
1098 mutex_unlock(&ec->mutex); in acpi_ec_add_query_handler()
1103 static void acpi_ec_remove_query_handlers(struct acpi_ec *ec, in acpi_ec_remove_query_handlers() argument
1109 mutex_lock(&ec->mutex); in acpi_ec_remove_query_handlers()
1110 list_for_each_entry_safe(handler, tmp, &ec->list, node) { in acpi_ec_remove_query_handlers()
1116 mutex_unlock(&ec->mutex); in acpi_ec_remove_query_handlers()
1121 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) in acpi_ec_remove_query_handler() argument
1123 acpi_ec_remove_query_handlers(ec, false, query_bit); in acpi_ec_remove_query_handler()
1131 struct acpi_ec *ec = q->ec; in acpi_ec_event_processor() local
1142 spin_lock_irq(&ec->lock); in acpi_ec_event_processor()
1143 ec->queries_in_progress--; in acpi_ec_event_processor()
1144 spin_unlock_irq(&ec->lock); in acpi_ec_event_processor()
1150 static struct acpi_ec_query *acpi_ec_create_query(struct acpi_ec *ec, u8 *pval) in acpi_ec_create_query() argument
1164 q->ec = ec; in acpi_ec_create_query()
1168 static int acpi_ec_submit_query(struct acpi_ec *ec) in acpi_ec_submit_query() argument
1174 q = acpi_ec_create_query(ec, &value); in acpi_ec_submit_query()
1179 * Query the EC to find out which _Qxx method we need to evaluate. in acpi_ec_submit_query()
1183 result = acpi_ec_transaction(ec, &q->transaction); in acpi_ec_submit_query()
1192 q->handler = acpi_ec_get_query_handler_by_value(ec, value); in acpi_ec_submit_query()
1207 spin_lock_irq(&ec->lock); in acpi_ec_submit_query()
1209 ec->queries_in_progress++; in acpi_ec_submit_query()
1212 spin_unlock_irq(&ec->lock); in acpi_ec_submit_query()
1224 struct acpi_ec *ec = container_of(work, struct acpi_ec, work); in acpi_ec_event_handler() local
1228 spin_lock_irq(&ec->lock); in acpi_ec_event_handler()
1230 while (ec->events_to_process) { in acpi_ec_event_handler()
1231 spin_unlock_irq(&ec->lock); in acpi_ec_event_handler()
1233 acpi_ec_submit_query(ec); in acpi_ec_event_handler()
1235 spin_lock_irq(&ec->lock); in acpi_ec_event_handler()
1237 ec->events_to_process--; in acpi_ec_event_handler()
1248 acpi_ec_complete_event(ec); in acpi_ec_event_handler()
1252 spin_unlock_irq(&ec->lock); in acpi_ec_event_handler()
1254 guard_timeout = !!ec_guard(ec); in acpi_ec_event_handler()
1256 spin_lock_irq(&ec->lock); in acpi_ec_event_handler()
1259 if (guard_timeout && !ec->curr) in acpi_ec_event_handler()
1260 advance_transaction(ec, false); in acpi_ec_event_handler()
1262 acpi_ec_close_event(ec); in acpi_ec_event_handler()
1267 ec->events_in_progress--; in acpi_ec_event_handler()
1269 spin_unlock_irq(&ec->lock); in acpi_ec_event_handler()
1272 static void acpi_ec_handle_interrupt(struct acpi_ec *ec) in acpi_ec_handle_interrupt() argument
1276 spin_lock_irqsave(&ec->lock, flags); in acpi_ec_handle_interrupt()
1277 advance_transaction(ec, true); in acpi_ec_handle_interrupt()
1278 spin_unlock_irqrestore(&ec->lock, flags); in acpi_ec_handle_interrupt()
1303 struct acpi_ec *ec = handler_context; in acpi_ec_space_handler() local
1313 if (ec->busy_polling || bits > 8) in acpi_ec_space_handler()
1314 acpi_ec_burst_enable(ec); in acpi_ec_space_handler()
1318 acpi_ec_read(ec, address, value) : in acpi_ec_space_handler()
1319 acpi_ec_write(ec, address, *value); in acpi_ec_space_handler()
1321 if (ec->busy_polling || bits > 8) in acpi_ec_space_handler()
1322 acpi_ec_burst_disable(ec); in acpi_ec_space_handler()
1343 static void acpi_ec_free(struct acpi_ec *ec) in acpi_ec_free() argument
1345 if (first_ec == ec) in acpi_ec_free()
1347 if (boot_ec == ec) in acpi_ec_free()
1349 kfree(ec); in acpi_ec_free()
1354 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); in acpi_ec_alloc() local
1356 if (!ec) in acpi_ec_alloc()
1358 mutex_init(&ec->mutex); in acpi_ec_alloc()
1359 init_waitqueue_head(&ec->wait); in acpi_ec_alloc()
1360 INIT_LIST_HEAD(&ec->list); in acpi_ec_alloc()
1361 spin_lock_init(&ec->lock); in acpi_ec_alloc()
1362 INIT_WORK(&ec->work, acpi_ec_event_handler); in acpi_ec_alloc()
1363 ec->timestamp = jiffies; in acpi_ec_alloc()
1364 ec->busy_polling = true; in acpi_ec_alloc()
1365 ec->polling_guard = 0; in acpi_ec_alloc()
1366 ec->gpe = -1; in acpi_ec_alloc()
1367 ec->irq = -1; in acpi_ec_alloc()
1368 return ec; in acpi_ec_alloc()
1377 struct acpi_ec *ec = context; in acpi_ec_register_query_methods() local
1384 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); in acpi_ec_register_query_methods()
1393 struct acpi_ec *ec = context; in ec_parse_device() local
1396 ec->command_addr = ec->data_addr = 0; in ec_parse_device()
1399 ec_parse_io_ports, ec); in ec_parse_device()
1402 if (ec->data_addr == 0 || ec->command_addr == 0) in ec_parse_device()
1405 /* Get GPE bit assignment (EC events). */ in ec_parse_device()
1409 ec->gpe = tmp; in ec_parse_device()
1415 /* Use the global lock for all EC transactions? */ in ec_parse_device()
1418 ec->global_lock = tmp; in ec_parse_device()
1419 ec->handle = handle; in ec_parse_device()
1423 static bool install_gpe_event_handler(struct acpi_ec *ec) in install_gpe_event_handler() argument
1427 status = acpi_install_gpe_raw_handler(NULL, ec->gpe, in install_gpe_event_handler()
1429 &acpi_ec_gpe_handler, ec); in install_gpe_event_handler()
1433 if (test_bit(EC_FLAGS_STARTED, &ec->flags) && ec->reference_count >= 1) in install_gpe_event_handler()
1434 acpi_ec_enable_gpe(ec, true); in install_gpe_event_handler()
1439 static bool install_gpio_irq_event_handler(struct acpi_ec *ec) in install_gpio_irq_event_handler() argument
1441 return request_irq(ec->irq, acpi_ec_irq_handler, IRQF_SHARED, in install_gpio_irq_event_handler()
1442 "ACPI EC", ec) >= 0; in install_gpio_irq_event_handler()
1447 * @ec: Target EC.
1448 * @device: ACPI device object corresponding to @ec.
1450 * Install a handler for the EC address space type unless it has been installed
1451 * already. If @device is not NULL, also look for EC query methods in the
1453 * handler for the EC, if possible.
1461 static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device) in ec_install_handlers() argument
1465 acpi_ec_start(ec, false); in ec_install_handlers()
1467 if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { in ec_install_handlers()
1468 acpi_ec_enter_noirq(ec); in ec_install_handlers()
1469 status = acpi_install_address_space_handler(ec->handle, in ec_install_handlers()
1472 NULL, ec); in ec_install_handlers()
1474 acpi_ec_stop(ec, false); in ec_install_handlers()
1477 set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); in ec_install_handlers()
1483 if (ec->gpe < 0) { in ec_install_handlers()
1493 ec->irq = irq; in ec_install_handlers()
1496 if (!test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { in ec_install_handlers()
1498 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1, in ec_install_handlers()
1500 NULL, ec, NULL); in ec_install_handlers()
1501 set_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); in ec_install_handlers()
1503 if (!test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in ec_install_handlers()
1506 if (ec->gpe >= 0) in ec_install_handlers()
1507 ready = install_gpe_event_handler(ec); in ec_install_handlers()
1508 else if (ec->irq >= 0) in ec_install_handlers()
1509 ready = install_gpio_irq_event_handler(ec); in ec_install_handlers()
1512 set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); in ec_install_handlers()
1513 acpi_ec_leave_noirq(ec); in ec_install_handlers()
1517 * the EC can be polled for events. in ec_install_handlers()
1520 /* EC is fully operational, allow queries */ in ec_install_handlers()
1521 acpi_ec_enable_event(ec); in ec_install_handlers()
1526 static void ec_remove_handlers(struct acpi_ec *ec) in ec_remove_handlers() argument
1528 if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1529 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, in ec_remove_handlers()
1532 clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); in ec_remove_handlers()
1536 * Stops handling the EC transactions after removing the operation in ec_remove_handlers()
1538 * invoked during the removal can result in new EC transactions. in ec_remove_handlers()
1540 * Flushes the EC requests and thus disables the GPE before in ec_remove_handlers()
1546 acpi_ec_stop(ec, false); in ec_remove_handlers()
1548 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1549 if (ec->gpe >= 0 && in ec_remove_handlers()
1550 ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, in ec_remove_handlers()
1554 if (ec->irq >= 0) in ec_remove_handlers()
1555 free_irq(ec->irq, ec); in ec_remove_handlers()
1557 clear_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); in ec_remove_handlers()
1559 if (test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { in ec_remove_handlers()
1560 acpi_ec_remove_query_handlers(ec, true, 0); in ec_remove_handlers()
1561 clear_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); in ec_remove_handlers()
1565 static int acpi_ec_setup(struct acpi_ec *ec, struct acpi_device *device) in acpi_ec_setup() argument
1569 ret = ec_install_handlers(ec, device); in acpi_ec_setup()
1573 /* First EC capable of handling transactions */ in acpi_ec_setup()
1575 first_ec = ec; in acpi_ec_setup()
1577 pr_info("EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n", ec->command_addr, in acpi_ec_setup()
1578 ec->data_addr); in acpi_ec_setup()
1580 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { in acpi_ec_setup()
1581 if (ec->gpe >= 0) in acpi_ec_setup()
1582 pr_info("GPE=0x%x\n", ec->gpe); in acpi_ec_setup()
1584 pr_info("IRQ=%d\n", ec->irq); in acpi_ec_setup()
1592 struct acpi_ec *ec; in acpi_ec_add() local
1600 /* Fast path: this device corresponds to the boot EC. */ in acpi_ec_add()
1601 ec = boot_ec; in acpi_ec_add()
1605 ec = acpi_ec_alloc(); in acpi_ec_add()
1606 if (!ec) in acpi_ec_add()
1609 status = ec_parse_device(device->handle, 0, ec, NULL); in acpi_ec_add()
1615 if (boot_ec && ec->command_addr == boot_ec->command_addr && in acpi_ec_add()
1616 ec->data_addr == boot_ec->data_addr) { in acpi_ec_add()
1620 * quirks. So do not change boot_ec->gpe to ec->gpe, in acpi_ec_add()
1623 boot_ec->handle = ec->handle; in acpi_ec_add()
1626 boot_ec->gpe = ec->gpe; 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()
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.
1894 struct acpi_ec *ec; in acpi_ec_ecdt_probe() local
1898 /* Generate a boot ec context. */ in acpi_ec_ecdt_probe()
1913 ec = acpi_ec_alloc(); in acpi_ec_ecdt_probe()
1914 if (!ec) in acpi_ec_ecdt_probe()
1918 ec->command_addr = ecdt_ptr->data.address; in acpi_ec_ecdt_probe()
1919 ec->data_addr = ecdt_ptr->control.address; in acpi_ec_ecdt_probe()
1921 ec->command_addr = ecdt_ptr->control.address; in acpi_ec_ecdt_probe()
1922 ec->data_addr = ecdt_ptr->data.address; in acpi_ec_ecdt_probe()
1930 ec->gpe = ecdt_ptr->gpe; in acpi_ec_ecdt_probe()
1932 ec->handle = ACPI_ROOT_OBJECT; in acpi_ec_ecdt_probe()
1938 ret = acpi_ec_setup(ec, NULL); in acpi_ec_ecdt_probe()
1940 acpi_ec_free(ec); in acpi_ec_ecdt_probe()
1944 boot_ec = ec; in acpi_ec_ecdt_probe()
1947 pr_info("Boot ECDT EC used to handle transactions\n"); in acpi_ec_ecdt_probe()
1956 struct acpi_ec *ec = in acpi_ec_suspend() local
1960 acpi_ec_disable_event(ec); in acpi_ec_suspend()
1966 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); in acpi_ec_suspend_noirq() local
1972 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_suspend_noirq()
1973 ec->gpe >= 0 && ec->reference_count >= 1) in acpi_ec_suspend_noirq()
1974 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); in acpi_ec_suspend_noirq()
1976 acpi_ec_enter_noirq(ec); in acpi_ec_suspend_noirq()
1983 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); in acpi_ec_resume_noirq() local
1985 acpi_ec_leave_noirq(ec); in acpi_ec_resume_noirq()
1987 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && in acpi_ec_resume_noirq()
1988 ec->gpe >= 0 && ec->reference_count >= 1) in acpi_ec_resume_noirq()
1989 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); in acpi_ec_resume_noirq()
1996 struct acpi_ec *ec = in acpi_ec_resume() local
1999 acpi_ec_enable_event(ec); in acpi_ec_resume()
2016 static bool acpi_ec_work_in_progress(struct acpi_ec *ec) in acpi_ec_work_in_progress() argument
2018 return ec->events_in_progress + ec->queries_in_progress > 0; in acpi_ec_work_in_progress()
2030 * than the EC one. in acpi_ec_dispatch_gpe()
2039 * Note that if any non-EC GPEs are active at this point, the SCI will in acpi_ec_dispatch_gpe()
2046 * Dispatch the EC GPE in-band, but do not report wakeup in any case in acpi_ec_dispatch_gpe()
2052 pm_pr_dbg("ACPI EC GPE status set\n"); in acpi_ec_dispatch_gpe()
2063 pm_pr_dbg("ACPI EC GPE dispatched\n"); in acpi_ec_dispatch_gpe()
2065 /* Drain EC work. */ in acpi_ec_dispatch_gpe()
2069 pm_pr_dbg("ACPI EC work flushed\n"); in acpi_ec_dispatch_gpe()
2127 .name = "ec",
2195 * Disable EC wakeup on following systems to prevent periodic in acpi_ec_init()
2196 * wakeup from EC GPE. in acpi_ec_init()
2200 pr_debug("Disabling EC wakeup on suspend-to-idle\n"); in acpi_ec_init()
2209 /* EC driver currently not unloadable */