xref: /OK3568_Linux_fs/kernel/drivers/platform/chrome/cros_ec.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * ChromeOS EC multi-function device
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2012 Google, Inc
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * The ChromeOS EC multi function device is used to mux all the requests
8*4882a593Smuzhiyun  * to the EC device for its multiple features: keyboard controller,
9*4882a593Smuzhiyun  * battery charging and regulator control, firmware update.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/of_platform.h>
13*4882a593Smuzhiyun #include <linux/interrupt.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/platform_data/cros_ec_commands.h>
17*4882a593Smuzhiyun #include <linux/platform_data/cros_ec_proto.h>
18*4882a593Smuzhiyun #include <linux/suspend.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "cros_ec.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define CROS_EC_DEV_EC_INDEX 0
23*4882a593Smuzhiyun #define CROS_EC_DEV_PD_INDEX 1
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun static struct cros_ec_platform ec_p = {
26*4882a593Smuzhiyun 	.ec_name = CROS_EC_DEV_NAME,
27*4882a593Smuzhiyun 	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct cros_ec_platform pd_p = {
31*4882a593Smuzhiyun 	.ec_name = CROS_EC_DEV_PD_NAME,
32*4882a593Smuzhiyun 	.cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
ec_irq_handler(int irq,void * data)35*4882a593Smuzhiyun static irqreturn_t ec_irq_handler(int irq, void *data)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct cros_ec_device *ec_dev = data;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	ec_dev->last_event_time = cros_ec_get_time_ns();
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	return IRQ_WAKE_THREAD;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun  * cros_ec_handle_event() - process and forward pending events on EC
46*4882a593Smuzhiyun  * @ec_dev: Device with events to process.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * Call this function in a loop when the kernel is notified that the EC has
49*4882a593Smuzhiyun  * pending events.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Return: true if more events are still pending and this function should be
52*4882a593Smuzhiyun  * called again.
53*4882a593Smuzhiyun  */
cros_ec_handle_event(struct cros_ec_device * ec_dev)54*4882a593Smuzhiyun bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	bool wake_event;
57*4882a593Smuzhiyun 	bool ec_has_more_events;
58*4882a593Smuzhiyun 	int ret;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/*
63*4882a593Smuzhiyun 	 * Signal only if wake host events or any interrupt if
64*4882a593Smuzhiyun 	 * cros_ec_get_next_event() returned an error (default value for
65*4882a593Smuzhiyun 	 * wake_event is true)
66*4882a593Smuzhiyun 	 */
67*4882a593Smuzhiyun 	if (wake_event && device_may_wakeup(ec_dev->dev))
68*4882a593Smuzhiyun 		pm_wakeup_event(ec_dev->dev, 0);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (ret > 0)
71*4882a593Smuzhiyun 		blocking_notifier_call_chain(&ec_dev->event_notifier,
72*4882a593Smuzhiyun 					     0, ec_dev);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	return ec_has_more_events;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun EXPORT_SYMBOL(cros_ec_handle_event);
77*4882a593Smuzhiyun 
ec_irq_thread(int irq,void * data)78*4882a593Smuzhiyun static irqreturn_t ec_irq_thread(int irq, void *data)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	struct cros_ec_device *ec_dev = data;
81*4882a593Smuzhiyun 	bool ec_has_more_events;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	do {
84*4882a593Smuzhiyun 		ec_has_more_events = cros_ec_handle_event(ec_dev);
85*4882a593Smuzhiyun 	} while (ec_has_more_events);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return IRQ_HANDLED;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
cros_ec_sleep_event(struct cros_ec_device * ec_dev,u8 sleep_event)90*4882a593Smuzhiyun static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	int ret;
93*4882a593Smuzhiyun 	struct {
94*4882a593Smuzhiyun 		struct cros_ec_command msg;
95*4882a593Smuzhiyun 		union {
96*4882a593Smuzhiyun 			struct ec_params_host_sleep_event req0;
97*4882a593Smuzhiyun 			struct ec_params_host_sleep_event_v1 req1;
98*4882a593Smuzhiyun 			struct ec_response_host_sleep_event_v1 resp1;
99*4882a593Smuzhiyun 		} u;
100*4882a593Smuzhiyun 	} __packed buf;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	memset(&buf, 0, sizeof(buf));
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (ec_dev->host_sleep_v1) {
105*4882a593Smuzhiyun 		buf.u.req1.sleep_event = sleep_event;
106*4882a593Smuzhiyun 		buf.u.req1.suspend_params.sleep_timeout_ms =
107*4882a593Smuzhiyun 				EC_HOST_SLEEP_TIMEOUT_DEFAULT;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		buf.msg.outsize = sizeof(buf.u.req1);
110*4882a593Smuzhiyun 		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
111*4882a593Smuzhiyun 		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
112*4882a593Smuzhiyun 			buf.msg.insize = sizeof(buf.u.resp1);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 		buf.msg.version = 1;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	} else {
117*4882a593Smuzhiyun 		buf.u.req0.sleep_event = sleep_event;
118*4882a593Smuzhiyun 		buf.msg.outsize = sizeof(buf.u.req0);
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
124*4882a593Smuzhiyun 	/* Report failure to transition to system wide suspend with a warning. */
125*4882a593Smuzhiyun 	if (ret >= 0 && ec_dev->host_sleep_v1 &&
126*4882a593Smuzhiyun 	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME ||
127*4882a593Smuzhiyun 	     sleep_event == HOST_SLEEP_EVENT_S3_RESUME)) {
128*4882a593Smuzhiyun 		ec_dev->last_resume_result =
129*4882a593Smuzhiyun 			buf.u.resp1.resume_response.sleep_transitions;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
132*4882a593Smuzhiyun 			  EC_HOST_RESUME_SLEEP_TIMEOUT,
133*4882a593Smuzhiyun 			  "EC detected sleep transition timeout. Total sleep transitions: %d",
134*4882a593Smuzhiyun 			  buf.u.resp1.resume_response.sleep_transitions &
135*4882a593Smuzhiyun 			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return ret;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
cros_ec_ready_event(struct notifier_block * nb,unsigned long queued_during_suspend,void * _notify)141*4882a593Smuzhiyun static int cros_ec_ready_event(struct notifier_block *nb,
142*4882a593Smuzhiyun 			       unsigned long queued_during_suspend,
143*4882a593Smuzhiyun 			       void *_notify)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	struct cros_ec_device *ec_dev = container_of(nb, struct cros_ec_device,
146*4882a593Smuzhiyun 						     notifier_ready);
147*4882a593Smuzhiyun 	u32 host_event = cros_ec_get_host_event(ec_dev);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY)) {
150*4882a593Smuzhiyun 		mutex_lock(&ec_dev->lock);
151*4882a593Smuzhiyun 		cros_ec_query_all(ec_dev);
152*4882a593Smuzhiyun 		mutex_unlock(&ec_dev->lock);
153*4882a593Smuzhiyun 		return NOTIFY_OK;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	return NOTIFY_DONE;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun  * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
161*4882a593Smuzhiyun  * @ec_dev: Device to register.
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  * Before calling this, allocate a pointer to a new device and then fill
164*4882a593Smuzhiyun  * in all the fields up to the --private-- marker.
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * Return: 0 on success or negative error code.
167*4882a593Smuzhiyun  */
cros_ec_register(struct cros_ec_device * ec_dev)168*4882a593Smuzhiyun int cros_ec_register(struct cros_ec_device *ec_dev)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	struct device *dev = ec_dev->dev;
171*4882a593Smuzhiyun 	int err = 0;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	ec_dev->max_request = sizeof(struct ec_params_hello);
176*4882a593Smuzhiyun 	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
177*4882a593Smuzhiyun 	ec_dev->max_passthru = 0;
178*4882a593Smuzhiyun 	ec_dev->ec = NULL;
179*4882a593Smuzhiyun 	ec_dev->pd = NULL;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
182*4882a593Smuzhiyun 	if (!ec_dev->din)
183*4882a593Smuzhiyun 		return -ENOMEM;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
186*4882a593Smuzhiyun 	if (!ec_dev->dout)
187*4882a593Smuzhiyun 		return -ENOMEM;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	mutex_init(&ec_dev->lock);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	err = cros_ec_query_all(ec_dev);
192*4882a593Smuzhiyun 	if (err) {
193*4882a593Smuzhiyun 		dev_err(dev, "Cannot identify the EC: error %d\n", err);
194*4882a593Smuzhiyun 		return err;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (ec_dev->irq > 0) {
198*4882a593Smuzhiyun 		err = devm_request_threaded_irq(dev, ec_dev->irq,
199*4882a593Smuzhiyun 						ec_irq_handler,
200*4882a593Smuzhiyun 						ec_irq_thread,
201*4882a593Smuzhiyun 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
202*4882a593Smuzhiyun 						"chromeos-ec", ec_dev);
203*4882a593Smuzhiyun 		if (err) {
204*4882a593Smuzhiyun 			dev_err(dev, "Failed to request IRQ %d: %d",
205*4882a593Smuzhiyun 				ec_dev->irq, err);
206*4882a593Smuzhiyun 			return err;
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* Register a platform device for the main EC instance */
211*4882a593Smuzhiyun 	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
212*4882a593Smuzhiyun 					PLATFORM_DEVID_AUTO, &ec_p,
213*4882a593Smuzhiyun 					sizeof(struct cros_ec_platform));
214*4882a593Smuzhiyun 	if (IS_ERR(ec_dev->ec)) {
215*4882a593Smuzhiyun 		dev_err(ec_dev->dev,
216*4882a593Smuzhiyun 			"Failed to create CrOS EC platform device\n");
217*4882a593Smuzhiyun 		return PTR_ERR(ec_dev->ec);
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (ec_dev->max_passthru) {
221*4882a593Smuzhiyun 		/*
222*4882a593Smuzhiyun 		 * Register a platform device for the PD behind the main EC.
223*4882a593Smuzhiyun 		 * We make the following assumptions:
224*4882a593Smuzhiyun 		 * - behind an EC, we have a pd
225*4882a593Smuzhiyun 		 * - only one device added.
226*4882a593Smuzhiyun 		 * - the EC is responsive at init time (it is not true for a
227*4882a593Smuzhiyun 		 *   sensor hub).
228*4882a593Smuzhiyun 		 */
229*4882a593Smuzhiyun 		ec_dev->pd = platform_device_register_data(ec_dev->dev,
230*4882a593Smuzhiyun 					"cros-ec-dev",
231*4882a593Smuzhiyun 					PLATFORM_DEVID_AUTO, &pd_p,
232*4882a593Smuzhiyun 					sizeof(struct cros_ec_platform));
233*4882a593Smuzhiyun 		if (IS_ERR(ec_dev->pd)) {
234*4882a593Smuzhiyun 			dev_err(ec_dev->dev,
235*4882a593Smuzhiyun 				"Failed to create CrOS PD platform device\n");
236*4882a593Smuzhiyun 			err = PTR_ERR(ec_dev->pd);
237*4882a593Smuzhiyun 			goto exit;
238*4882a593Smuzhiyun 		}
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
242*4882a593Smuzhiyun 		err = devm_of_platform_populate(dev);
243*4882a593Smuzhiyun 		if (err) {
244*4882a593Smuzhiyun 			dev_err(dev, "Failed to register sub-devices\n");
245*4882a593Smuzhiyun 			goto exit;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/*
250*4882a593Smuzhiyun 	 * Clear sleep event - this will fail harmlessly on platforms that
251*4882a593Smuzhiyun 	 * don't implement the sleep event host command.
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 	err = cros_ec_sleep_event(ec_dev, 0);
254*4882a593Smuzhiyun 	if (err < 0)
255*4882a593Smuzhiyun 		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
256*4882a593Smuzhiyun 			err);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	if (ec_dev->mkbp_event_supported) {
259*4882a593Smuzhiyun 		/*
260*4882a593Smuzhiyun 		 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
261*4882a593Smuzhiyun 		 * event.
262*4882a593Smuzhiyun 		 */
263*4882a593Smuzhiyun 		ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
264*4882a593Smuzhiyun 		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
265*4882a593Smuzhiyun 						      &ec_dev->notifier_ready);
266*4882a593Smuzhiyun 		if (err)
267*4882a593Smuzhiyun 			goto exit;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	dev_info(dev, "Chrome EC device registered\n");
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return 0;
273*4882a593Smuzhiyun exit:
274*4882a593Smuzhiyun 	platform_device_unregister(ec_dev->ec);
275*4882a593Smuzhiyun 	platform_device_unregister(ec_dev->pd);
276*4882a593Smuzhiyun 	return err;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun EXPORT_SYMBOL(cros_ec_register);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /**
281*4882a593Smuzhiyun  * cros_ec_unregister() - Remove a ChromeOS EC.
282*4882a593Smuzhiyun  * @ec_dev: Device to unregister.
283*4882a593Smuzhiyun  *
284*4882a593Smuzhiyun  * Call this to deregister a ChromeOS EC, then clean up any private data.
285*4882a593Smuzhiyun  *
286*4882a593Smuzhiyun  * Return: 0 on success or negative error code.
287*4882a593Smuzhiyun  */
cros_ec_unregister(struct cros_ec_device * ec_dev)288*4882a593Smuzhiyun int cros_ec_unregister(struct cros_ec_device *ec_dev)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun 	if (ec_dev->pd)
291*4882a593Smuzhiyun 		platform_device_unregister(ec_dev->pd);
292*4882a593Smuzhiyun 	platform_device_unregister(ec_dev->ec);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	return 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun EXPORT_SYMBOL(cros_ec_unregister);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
299*4882a593Smuzhiyun /**
300*4882a593Smuzhiyun  * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
301*4882a593Smuzhiyun  * @ec_dev: Device to suspend.
302*4882a593Smuzhiyun  *
303*4882a593Smuzhiyun  * This can be called by drivers to handle a suspend event.
304*4882a593Smuzhiyun  *
305*4882a593Smuzhiyun  * Return: 0 on success or negative error code.
306*4882a593Smuzhiyun  */
cros_ec_suspend(struct cros_ec_device * ec_dev)307*4882a593Smuzhiyun int cros_ec_suspend(struct cros_ec_device *ec_dev)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	struct device *dev = ec_dev->dev;
310*4882a593Smuzhiyun 	int ret;
311*4882a593Smuzhiyun 	u8 sleep_event;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
314*4882a593Smuzhiyun 		      HOST_SLEEP_EVENT_S3_SUSPEND :
315*4882a593Smuzhiyun 		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
318*4882a593Smuzhiyun 	if (ret < 0)
319*4882a593Smuzhiyun 		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
320*4882a593Smuzhiyun 			ret);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (device_may_wakeup(dev))
323*4882a593Smuzhiyun 		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	disable_irq(ec_dev->irq);
326*4882a593Smuzhiyun 	ec_dev->was_wake_device = ec_dev->wake_enabled;
327*4882a593Smuzhiyun 	ec_dev->suspended = true;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return 0;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun EXPORT_SYMBOL(cros_ec_suspend);
332*4882a593Smuzhiyun 
cros_ec_report_events_during_suspend(struct cros_ec_device * ec_dev)333*4882a593Smuzhiyun static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	bool wake_event;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	while (ec_dev->mkbp_event_supported &&
338*4882a593Smuzhiyun 	       cros_ec_get_next_event(ec_dev, &wake_event, NULL) > 0) {
339*4882a593Smuzhiyun 		blocking_notifier_call_chain(&ec_dev->event_notifier,
340*4882a593Smuzhiyun 					     1, ec_dev);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		if (wake_event && device_may_wakeup(ec_dev->dev))
343*4882a593Smuzhiyun 			pm_wakeup_event(ec_dev->dev, 0);
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /**
348*4882a593Smuzhiyun  * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
349*4882a593Smuzhiyun  * @ec_dev: Device to resume.
350*4882a593Smuzhiyun  *
351*4882a593Smuzhiyun  * This can be called by drivers to handle a resume event.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * Return: 0 on success or negative error code.
354*4882a593Smuzhiyun  */
cros_ec_resume(struct cros_ec_device * ec_dev)355*4882a593Smuzhiyun int cros_ec_resume(struct cros_ec_device *ec_dev)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	int ret;
358*4882a593Smuzhiyun 	u8 sleep_event;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	ec_dev->suspended = false;
361*4882a593Smuzhiyun 	enable_irq(ec_dev->irq);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
364*4882a593Smuzhiyun 		      HOST_SLEEP_EVENT_S3_RESUME :
365*4882a593Smuzhiyun 		      HOST_SLEEP_EVENT_S0IX_RESUME;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
368*4882a593Smuzhiyun 	if (ret < 0)
369*4882a593Smuzhiyun 		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
370*4882a593Smuzhiyun 			ret);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (ec_dev->wake_enabled) {
373*4882a593Smuzhiyun 		disable_irq_wake(ec_dev->irq);
374*4882a593Smuzhiyun 		ec_dev->wake_enabled = 0;
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 	/*
377*4882a593Smuzhiyun 	 * Let the mfd devices know about events that occur during
378*4882a593Smuzhiyun 	 * suspend. This way the clients know what to do with them.
379*4882a593Smuzhiyun 	 */
380*4882a593Smuzhiyun 	cros_ec_report_events_during_suspend(ec_dev);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	return 0;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun EXPORT_SYMBOL(cros_ec_resume);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun #endif
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun MODULE_LICENSE("GPL");
390*4882a593Smuzhiyun MODULE_DESCRIPTION("ChromeOS EC core driver");
391