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