xref: /linux/drivers/platform/chrome/cros_ec.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 			dev_err(dev, "Failed to request IRQ %d: %d\n",
254 				ec_dev->irq, err);
255 			goto exit;
256 		}
257 	}
258 
259 	/* Register a platform device for the main EC instance */
260 	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
261 					PLATFORM_DEVID_AUTO, &ec_p,
262 					sizeof(struct cros_ec_platform));
263 	if (IS_ERR(ec_dev->ec)) {
264 		dev_err(ec_dev->dev,
265 			"Failed to create CrOS EC platform device\n");
266 		err = PTR_ERR(ec_dev->ec);
267 		goto exit;
268 	}
269 
270 	if (ec_dev->max_passthru) {
271 		/*
272 		 * Register a platform device for the PD behind the main EC.
273 		 * We make the following assumptions:
274 		 * - behind an EC, we have a pd
275 		 * - only one device added.
276 		 * - the EC is responsive at init time (it is not true for a
277 		 *   sensor hub).
278 		 */
279 		ec_dev->pd = platform_device_register_data(ec_dev->dev,
280 					"cros-ec-dev",
281 					PLATFORM_DEVID_AUTO, &pd_p,
282 					sizeof(struct cros_ec_platform));
283 		if (IS_ERR(ec_dev->pd)) {
284 			dev_err(ec_dev->dev,
285 				"Failed to create CrOS PD platform device\n");
286 			err = PTR_ERR(ec_dev->pd);
287 			goto exit;
288 		}
289 	}
290 
291 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
292 		err = devm_of_platform_populate(dev);
293 		if (err) {
294 			dev_err(dev, "Failed to register sub-devices\n");
295 			goto exit;
296 		}
297 	}
298 
299 	/*
300 	 * Clear sleep event - this will fail harmlessly on platforms that
301 	 * don't implement the sleep event host command.
302 	 */
303 	err = cros_ec_sleep_event(ec_dev, 0);
304 	if (err < 0)
305 		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec\n",
306 			err);
307 
308 	if (ec_dev->mkbp_event_supported) {
309 		/*
310 		 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
311 		 * event.
312 		 */
313 		ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
314 		err = blocking_notifier_chain_register(&ec_dev->event_notifier,
315 						      &ec_dev->notifier_ready);
316 		if (err)
317 			goto exit;
318 	}
319 
320 	scoped_guard(mutex, &ec_dev->lock)
321 		ec_dev->registered = true;
322 
323 	dev_info(dev, "Chrome EC device registered\n");
324 
325 	/*
326 	 * Unlock EC that may be waiting for AP to process MKBP events.
327 	 * If the AP takes to long to answer, the EC would stop sending events.
328 	 */
329 	if (ec_dev->mkbp_event_supported)
330 		cros_ec_irq_thread(0, ec_dev);
331 
332 	return 0;
333 exit:
334 	platform_device_unregister(ec_dev->ec);
335 	platform_device_unregister(ec_dev->pd);
336 	return err;
337 }
338 EXPORT_SYMBOL(cros_ec_register);
339 
340 /**
341  * cros_ec_unregister() - Remove a ChromeOS EC.
342  * @ec_dev: Device to unregister.
343  *
344  * Call this to deregister a ChromeOS EC, then clean up any private data.
345  *
346  * Return: 0 on success or negative error code.
347  */
348 void cros_ec_unregister(struct cros_ec_device *ec_dev)
349 {
350 	scoped_guard(mutex, &ec_dev->lock)
351 		ec_dev->registered = false;
352 
353 	if (ec_dev->mkbp_event_supported)
354 		blocking_notifier_chain_unregister(&ec_dev->event_notifier,
355 						   &ec_dev->notifier_ready);
356 	platform_device_unregister(ec_dev->pd);
357 	platform_device_unregister(ec_dev->ec);
358 }
359 EXPORT_SYMBOL(cros_ec_unregister);
360 
361 #ifdef CONFIG_PM_SLEEP
362 static void cros_ec_send_suspend_event(struct cros_ec_device *ec_dev)
363 {
364 	int ret;
365 	u8 sleep_event;
366 
367 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
368 		      HOST_SLEEP_EVENT_S3_SUSPEND :
369 		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
370 
371 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
372 	if (ret < 0)
373 		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec\n",
374 			ret);
375 }
376 
377 /**
378  * cros_ec_suspend_prepare() - Handle a suspend prepare operation for the ChromeOS EC device.
379  * @ec_dev: Device to suspend.
380  *
381  * This can be called by drivers to handle a suspend prepare stage of suspend.
382  *
383  * Return: 0 always.
384  */
385 int cros_ec_suspend_prepare(struct cros_ec_device *ec_dev)
386 {
387 	cros_ec_send_suspend_event(ec_dev);
388 	return 0;
389 }
390 EXPORT_SYMBOL(cros_ec_suspend_prepare);
391 
392 static void cros_ec_disable_irq(struct cros_ec_device *ec_dev)
393 {
394 	struct device *dev = ec_dev->dev;
395 	if (device_may_wakeup(dev))
396 		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
397 	else
398 		ec_dev->wake_enabled = false;
399 
400 	disable_irq(ec_dev->irq);
401 	ec_dev->suspended = true;
402 }
403 
404 /**
405  * cros_ec_suspend_late() - Handle a suspend late operation for the ChromeOS EC device.
406  * @ec_dev: Device to suspend.
407  *
408  * This can be called by drivers to handle a suspend late stage of suspend.
409  *
410  * Return: 0 always.
411  */
412 int cros_ec_suspend_late(struct cros_ec_device *ec_dev)
413 {
414 	cros_ec_disable_irq(ec_dev);
415 	return 0;
416 }
417 EXPORT_SYMBOL(cros_ec_suspend_late);
418 
419 /**
420  * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
421  * @ec_dev: Device to suspend.
422  *
423  * This can be called by drivers to handle a suspend event.
424  *
425  * Return: 0 always.
426  */
427 int cros_ec_suspend(struct cros_ec_device *ec_dev)
428 {
429 	cros_ec_suspend_prepare(ec_dev);
430 	cros_ec_suspend_late(ec_dev);
431 	return 0;
432 }
433 EXPORT_SYMBOL(cros_ec_suspend);
434 
435 static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
436 {
437 	bool wake_event;
438 
439 	while (ec_dev->mkbp_event_supported &&
440 	       cros_ec_get_next_event(ec_dev, &wake_event, NULL) > 0) {
441 		blocking_notifier_call_chain(&ec_dev->event_notifier,
442 					     1, ec_dev);
443 
444 		if (wake_event && device_may_wakeup(ec_dev->dev))
445 			pm_wakeup_event(ec_dev->dev, 0);
446 	}
447 }
448 
449 static void cros_ec_send_resume_event(struct cros_ec_device *ec_dev)
450 {
451 	int ret;
452 	u8 sleep_event;
453 
454 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
455 		      HOST_SLEEP_EVENT_S3_RESUME :
456 		      HOST_SLEEP_EVENT_S0IX_RESUME;
457 
458 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
459 	if (ret < 0)
460 		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec\n",
461 			ret);
462 }
463 
464 /**
465  * cros_ec_resume_complete() - Handle a resume complete operation for the ChromeOS EC device.
466  * @ec_dev: Device to resume.
467  *
468  * This can be called by drivers to handle a resume complete stage of resume.
469  */
470 void cros_ec_resume_complete(struct cros_ec_device *ec_dev)
471 {
472 	cros_ec_send_resume_event(ec_dev);
473 
474 	/*
475 	 * Let the mfd devices know about events that occur during
476 	 * suspend. This way the clients know what to do with them.
477 	 */
478 	cros_ec_report_events_during_suspend(ec_dev);
479 }
480 EXPORT_SYMBOL(cros_ec_resume_complete);
481 
482 static void cros_ec_enable_irq(struct cros_ec_device *ec_dev)
483 {
484 	ec_dev->suspended = false;
485 	enable_irq(ec_dev->irq);
486 
487 	if (ec_dev->wake_enabled)
488 		disable_irq_wake(ec_dev->irq);
489 }
490 
491 /**
492  * cros_ec_resume_early() - Handle a resume early operation for the ChromeOS EC device.
493  * @ec_dev: Device to resume.
494  *
495  * This can be called by drivers to handle a resume early stage of resume.
496  *
497  * Return: 0 always.
498  */
499 int cros_ec_resume_early(struct cros_ec_device *ec_dev)
500 {
501 	cros_ec_enable_irq(ec_dev);
502 	return 0;
503 }
504 EXPORT_SYMBOL(cros_ec_resume_early);
505 
506 /**
507  * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
508  * @ec_dev: Device to resume.
509  *
510  * This can be called by drivers to handle a resume event.
511  *
512  * Return: 0 always.
513  */
514 int cros_ec_resume(struct cros_ec_device *ec_dev)
515 {
516 	cros_ec_resume_early(ec_dev);
517 	cros_ec_resume_complete(ec_dev);
518 	return 0;
519 }
520 EXPORT_SYMBOL(cros_ec_resume);
521 
522 #endif
523 
524 MODULE_LICENSE("GPL");
525 MODULE_DESCRIPTION("ChromeOS EC core driver");
526