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