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