18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ChromeOS EC multi-function device
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Google, Inc
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * The ChromeOS EC multi function device is used to mux all the requests
88c2ecf20Sopenharmony_ci * to the EC device for its multiple features: keyboard controller,
98c2ecf20Sopenharmony_ci * battery charging and regulator control, firmware update.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
188c2ecf20Sopenharmony_ci#include <linux/suspend.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "cros_ec.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define CROS_EC_DEV_EC_INDEX 0
238c2ecf20Sopenharmony_ci#define CROS_EC_DEV_PD_INDEX 1
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic struct cros_ec_platform ec_p = {
268c2ecf20Sopenharmony_ci	.ec_name = CROS_EC_DEV_NAME,
278c2ecf20Sopenharmony_ci	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic struct cros_ec_platform pd_p = {
318c2ecf20Sopenharmony_ci	.ec_name = CROS_EC_DEV_PD_NAME,
328c2ecf20Sopenharmony_ci	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic irqreturn_t ec_irq_handler(int irq, void *data)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev = data;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	ec_dev->last_event_time = cros_ec_get_time_ns();
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return IRQ_WAKE_THREAD;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * cros_ec_handle_event() - process and forward pending events on EC
468c2ecf20Sopenharmony_ci * @ec_dev: Device with events to process.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * Call this function in a loop when the kernel is notified that the EC has
498c2ecf20Sopenharmony_ci * pending events.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Return: true if more events are still pending and this function should be
528c2ecf20Sopenharmony_ci * called again.
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_cibool cros_ec_handle_event(struct cros_ec_device *ec_dev)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	bool wake_event;
578c2ecf20Sopenharmony_ci	bool ec_has_more_events;
588c2ecf20Sopenharmony_ci	int ret;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/*
638c2ecf20Sopenharmony_ci	 * Signal only if wake host events or any interrupt if
648c2ecf20Sopenharmony_ci	 * cros_ec_get_next_event() returned an error (default value for
658c2ecf20Sopenharmony_ci	 * wake_event is true)
668c2ecf20Sopenharmony_ci	 */
678c2ecf20Sopenharmony_ci	if (wake_event && device_may_wakeup(ec_dev->dev))
688c2ecf20Sopenharmony_ci		pm_wakeup_event(ec_dev->dev, 0);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	if (ret > 0)
718c2ecf20Sopenharmony_ci		blocking_notifier_call_chain(&ec_dev->event_notifier,
728c2ecf20Sopenharmony_ci					     0, ec_dev);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return ec_has_more_events;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cros_ec_handle_event);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic irqreturn_t ec_irq_thread(int irq, void *data)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev = data;
818c2ecf20Sopenharmony_ci	bool ec_has_more_events;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	do {
848c2ecf20Sopenharmony_ci		ec_has_more_events = cros_ec_handle_event(ec_dev);
858c2ecf20Sopenharmony_ci	} while (ec_has_more_events);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	int ret;
938c2ecf20Sopenharmony_ci	struct {
948c2ecf20Sopenharmony_ci		struct cros_ec_command msg;
958c2ecf20Sopenharmony_ci		union {
968c2ecf20Sopenharmony_ci			struct ec_params_host_sleep_event req0;
978c2ecf20Sopenharmony_ci			struct ec_params_host_sleep_event_v1 req1;
988c2ecf20Sopenharmony_ci			struct ec_response_host_sleep_event_v1 resp1;
998c2ecf20Sopenharmony_ci		} u;
1008c2ecf20Sopenharmony_ci	} __packed buf;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	memset(&buf, 0, sizeof(buf));
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (ec_dev->host_sleep_v1) {
1058c2ecf20Sopenharmony_ci		buf.u.req1.sleep_event = sleep_event;
1068c2ecf20Sopenharmony_ci		buf.u.req1.suspend_params.sleep_timeout_ms =
1078c2ecf20Sopenharmony_ci				EC_HOST_SLEEP_TIMEOUT_DEFAULT;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		buf.msg.outsize = sizeof(buf.u.req1);
1108c2ecf20Sopenharmony_ci		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
1118c2ecf20Sopenharmony_ci		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
1128c2ecf20Sopenharmony_ci			buf.msg.insize = sizeof(buf.u.resp1);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci		buf.msg.version = 1;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	} else {
1178c2ecf20Sopenharmony_ci		buf.u.req0.sleep_event = sleep_event;
1188c2ecf20Sopenharmony_ci		buf.msg.outsize = sizeof(buf.u.req0);
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
1248c2ecf20Sopenharmony_ci	/* Report failure to transition to system wide suspend with a warning. */
1258c2ecf20Sopenharmony_ci	if (ret >= 0 && ec_dev->host_sleep_v1 &&
1268c2ecf20Sopenharmony_ci	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME ||
1278c2ecf20Sopenharmony_ci	     sleep_event == HOST_SLEEP_EVENT_S3_RESUME)) {
1288c2ecf20Sopenharmony_ci		ec_dev->last_resume_result =
1298c2ecf20Sopenharmony_ci			buf.u.resp1.resume_response.sleep_transitions;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
1328c2ecf20Sopenharmony_ci			  EC_HOST_RESUME_SLEEP_TIMEOUT,
1338c2ecf20Sopenharmony_ci			  "EC detected sleep transition timeout. Total sleep transitions: %d",
1348c2ecf20Sopenharmony_ci			  buf.u.resp1.resume_response.sleep_transitions &
1358c2ecf20Sopenharmony_ci			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return ret;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int cros_ec_ready_event(struct notifier_block *nb,
1428c2ecf20Sopenharmony_ci			       unsigned long queued_during_suspend,
1438c2ecf20Sopenharmony_ci			       void *_notify)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev = container_of(nb, struct cros_ec_device,
1468c2ecf20Sopenharmony_ci						     notifier_ready);
1478c2ecf20Sopenharmony_ci	u32 host_event = cros_ec_get_host_event(ec_dev);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY)) {
1508c2ecf20Sopenharmony_ci		mutex_lock(&ec_dev->lock);
1518c2ecf20Sopenharmony_ci		cros_ec_query_all(ec_dev);
1528c2ecf20Sopenharmony_ci		mutex_unlock(&ec_dev->lock);
1538c2ecf20Sopenharmony_ci		return NOTIFY_OK;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/**
1608c2ecf20Sopenharmony_ci * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
1618c2ecf20Sopenharmony_ci * @ec_dev: Device to register.
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * Before calling this, allocate a pointer to a new device and then fill
1648c2ecf20Sopenharmony_ci * in all the fields up to the --private-- marker.
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * Return: 0 on success or negative error code.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ciint cros_ec_register(struct cros_ec_device *ec_dev)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct device *dev = ec_dev->dev;
1718c2ecf20Sopenharmony_ci	int err = 0;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	ec_dev->max_request = sizeof(struct ec_params_hello);
1768c2ecf20Sopenharmony_ci	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
1778c2ecf20Sopenharmony_ci	ec_dev->max_passthru = 0;
1788c2ecf20Sopenharmony_ci	ec_dev->ec = NULL;
1798c2ecf20Sopenharmony_ci	ec_dev->pd = NULL;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
1828c2ecf20Sopenharmony_ci	if (!ec_dev->din)
1838c2ecf20Sopenharmony_ci		return -ENOMEM;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
1868c2ecf20Sopenharmony_ci	if (!ec_dev->dout)
1878c2ecf20Sopenharmony_ci		return -ENOMEM;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	mutex_init(&ec_dev->lock);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	err = cros_ec_query_all(ec_dev);
1928c2ecf20Sopenharmony_ci	if (err) {
1938c2ecf20Sopenharmony_ci		dev_err(dev, "Cannot identify the EC: error %d\n", err);
1948c2ecf20Sopenharmony_ci		return err;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (ec_dev->irq > 0) {
1988c2ecf20Sopenharmony_ci		err = devm_request_threaded_irq(dev, ec_dev->irq,
1998c2ecf20Sopenharmony_ci						ec_irq_handler,
2008c2ecf20Sopenharmony_ci						ec_irq_thread,
2018c2ecf20Sopenharmony_ci						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
2028c2ecf20Sopenharmony_ci						"chromeos-ec", ec_dev);
2038c2ecf20Sopenharmony_ci		if (err) {
2048c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to request IRQ %d: %d",
2058c2ecf20Sopenharmony_ci				ec_dev->irq, err);
2068c2ecf20Sopenharmony_ci			return err;
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Register a platform device for the main EC instance */
2118c2ecf20Sopenharmony_ci	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
2128c2ecf20Sopenharmony_ci					PLATFORM_DEVID_AUTO, &ec_p,
2138c2ecf20Sopenharmony_ci					sizeof(struct cros_ec_platform));
2148c2ecf20Sopenharmony_ci	if (IS_ERR(ec_dev->ec)) {
2158c2ecf20Sopenharmony_ci		dev_err(ec_dev->dev,
2168c2ecf20Sopenharmony_ci			"Failed to create CrOS EC platform device\n");
2178c2ecf20Sopenharmony_ci		return PTR_ERR(ec_dev->ec);
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	if (ec_dev->max_passthru) {
2218c2ecf20Sopenharmony_ci		/*
2228c2ecf20Sopenharmony_ci		 * Register a platform device for the PD behind the main EC.
2238c2ecf20Sopenharmony_ci		 * We make the following assumptions:
2248c2ecf20Sopenharmony_ci		 * - behind an EC, we have a pd
2258c2ecf20Sopenharmony_ci		 * - only one device added.
2268c2ecf20Sopenharmony_ci		 * - the EC is responsive at init time (it is not true for a
2278c2ecf20Sopenharmony_ci		 *   sensor hub).
2288c2ecf20Sopenharmony_ci		 */
2298c2ecf20Sopenharmony_ci		ec_dev->pd = platform_device_register_data(ec_dev->dev,
2308c2ecf20Sopenharmony_ci					"cros-ec-dev",
2318c2ecf20Sopenharmony_ci					PLATFORM_DEVID_AUTO, &pd_p,
2328c2ecf20Sopenharmony_ci					sizeof(struct cros_ec_platform));
2338c2ecf20Sopenharmony_ci		if (IS_ERR(ec_dev->pd)) {
2348c2ecf20Sopenharmony_ci			dev_err(ec_dev->dev,
2358c2ecf20Sopenharmony_ci				"Failed to create CrOS PD platform device\n");
2368c2ecf20Sopenharmony_ci			err = PTR_ERR(ec_dev->pd);
2378c2ecf20Sopenharmony_ci			goto exit;
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2428c2ecf20Sopenharmony_ci		err = devm_of_platform_populate(dev);
2438c2ecf20Sopenharmony_ci		if (err) {
2448c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to register sub-devices\n");
2458c2ecf20Sopenharmony_ci			goto exit;
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Clear sleep event - this will fail harmlessly on platforms that
2518c2ecf20Sopenharmony_ci	 * don't implement the sleep event host command.
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci	err = cros_ec_sleep_event(ec_dev, 0);
2548c2ecf20Sopenharmony_ci	if (err < 0)
2558c2ecf20Sopenharmony_ci		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
2568c2ecf20Sopenharmony_ci			err);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (ec_dev->mkbp_event_supported) {
2598c2ecf20Sopenharmony_ci		/*
2608c2ecf20Sopenharmony_ci		 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
2618c2ecf20Sopenharmony_ci		 * event.
2628c2ecf20Sopenharmony_ci		 */
2638c2ecf20Sopenharmony_ci		ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
2648c2ecf20Sopenharmony_ci		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
2658c2ecf20Sopenharmony_ci						      &ec_dev->notifier_ready);
2668c2ecf20Sopenharmony_ci		if (err)
2678c2ecf20Sopenharmony_ci			goto exit;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	dev_info(dev, "Chrome EC device registered\n");
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ciexit:
2748c2ecf20Sopenharmony_ci	platform_device_unregister(ec_dev->ec);
2758c2ecf20Sopenharmony_ci	platform_device_unregister(ec_dev->pd);
2768c2ecf20Sopenharmony_ci	return err;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cros_ec_register);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/**
2818c2ecf20Sopenharmony_ci * cros_ec_unregister() - Remove a ChromeOS EC.
2828c2ecf20Sopenharmony_ci * @ec_dev: Device to unregister.
2838c2ecf20Sopenharmony_ci *
2848c2ecf20Sopenharmony_ci * Call this to deregister a ChromeOS EC, then clean up any private data.
2858c2ecf20Sopenharmony_ci *
2868c2ecf20Sopenharmony_ci * Return: 0 on success or negative error code.
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_ciint cros_ec_unregister(struct cros_ec_device *ec_dev)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	if (ec_dev->pd)
2918c2ecf20Sopenharmony_ci		platform_device_unregister(ec_dev->pd);
2928c2ecf20Sopenharmony_ci	platform_device_unregister(ec_dev->ec);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return 0;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cros_ec_unregister);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2998c2ecf20Sopenharmony_ci/**
3008c2ecf20Sopenharmony_ci * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
3018c2ecf20Sopenharmony_ci * @ec_dev: Device to suspend.
3028c2ecf20Sopenharmony_ci *
3038c2ecf20Sopenharmony_ci * This can be called by drivers to handle a suspend event.
3048c2ecf20Sopenharmony_ci *
3058c2ecf20Sopenharmony_ci * Return: 0 on success or negative error code.
3068c2ecf20Sopenharmony_ci */
3078c2ecf20Sopenharmony_ciint cros_ec_suspend(struct cros_ec_device *ec_dev)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	struct device *dev = ec_dev->dev;
3108c2ecf20Sopenharmony_ci	int ret;
3118c2ecf20Sopenharmony_ci	u8 sleep_event;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
3148c2ecf20Sopenharmony_ci		      HOST_SLEEP_EVENT_S3_SUSPEND :
3158c2ecf20Sopenharmony_ci		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	ret = cros_ec_sleep_event(ec_dev, sleep_event);
3188c2ecf20Sopenharmony_ci	if (ret < 0)
3198c2ecf20Sopenharmony_ci		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
3208c2ecf20Sopenharmony_ci			ret);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (device_may_wakeup(dev))
3238c2ecf20Sopenharmony_ci		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	disable_irq(ec_dev->irq);
3268c2ecf20Sopenharmony_ci	ec_dev->was_wake_device = ec_dev->wake_enabled;
3278c2ecf20Sopenharmony_ci	ec_dev->suspended = true;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	return 0;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cros_ec_suspend);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	bool wake_event;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	while (ec_dev->mkbp_event_supported &&
3388c2ecf20Sopenharmony_ci	       cros_ec_get_next_event(ec_dev, &wake_event, NULL) > 0) {
3398c2ecf20Sopenharmony_ci		blocking_notifier_call_chain(&ec_dev->event_notifier,
3408c2ecf20Sopenharmony_ci					     1, ec_dev);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci		if (wake_event && device_may_wakeup(ec_dev->dev))
3438c2ecf20Sopenharmony_ci			pm_wakeup_event(ec_dev->dev, 0);
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci/**
3488c2ecf20Sopenharmony_ci * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
3498c2ecf20Sopenharmony_ci * @ec_dev: Device to resume.
3508c2ecf20Sopenharmony_ci *
3518c2ecf20Sopenharmony_ci * This can be called by drivers to handle a resume event.
3528c2ecf20Sopenharmony_ci *
3538c2ecf20Sopenharmony_ci * Return: 0 on success or negative error code.
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_ciint cros_ec_resume(struct cros_ec_device *ec_dev)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	int ret;
3588c2ecf20Sopenharmony_ci	u8 sleep_event;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	ec_dev->suspended = false;
3618c2ecf20Sopenharmony_ci	enable_irq(ec_dev->irq);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
3648c2ecf20Sopenharmony_ci		      HOST_SLEEP_EVENT_S3_RESUME :
3658c2ecf20Sopenharmony_ci		      HOST_SLEEP_EVENT_S0IX_RESUME;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	ret = cros_ec_sleep_event(ec_dev, sleep_event);
3688c2ecf20Sopenharmony_ci	if (ret < 0)
3698c2ecf20Sopenharmony_ci		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
3708c2ecf20Sopenharmony_ci			ret);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	if (ec_dev->wake_enabled) {
3738c2ecf20Sopenharmony_ci		disable_irq_wake(ec_dev->irq);
3748c2ecf20Sopenharmony_ci		ec_dev->wake_enabled = 0;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci	/*
3778c2ecf20Sopenharmony_ci	 * Let the mfd devices know about events that occur during
3788c2ecf20Sopenharmony_ci	 * suspend. This way the clients know what to do with them.
3798c2ecf20Sopenharmony_ci	 */
3808c2ecf20Sopenharmony_ci	cros_ec_report_events_during_suspend(ec_dev);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cros_ec_resume);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci#endif
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3908c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ChromeOS EC core driver");
391