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