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/of_platform.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/platform_data/cros_ec_commands.h>
17 #include <linux/platform_data/cros_ec_proto.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 static irqreturn_t ec_irq_handler(int irq, void *data)
36 {
37 	struct cros_ec_device *ec_dev = data;
38 
39 	ec_dev->last_event_time = cros_ec_get_time_ns();
40 
41 	return IRQ_WAKE_THREAD;
42 }
43 
44 /**
45  * cros_ec_handle_event() - process and forward pending events on EC
46  * @ec_dev: Device with events to process.
47  *
48  * Call this function in a loop when the kernel is notified that the EC has
49  * pending events.
50  *
51  * Return: true if more events are still pending and this function should be
52  * called again.
53  */
54 bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
55 {
56 	bool wake_event;
57 	bool ec_has_more_events;
58 	int ret;
59 
60 	ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
61 
62 	/*
63 	 * Signal only if wake host events or any interrupt if
64 	 * cros_ec_get_next_event() returned an error (default value for
65 	 * wake_event is true)
66 	 */
67 	if (wake_event && device_may_wakeup(ec_dev->dev))
68 		pm_wakeup_event(ec_dev->dev, 0);
69 
70 	if (ret > 0)
71 		blocking_notifier_call_chain(&ec_dev->event_notifier,
72 					     0, ec_dev);
73 
74 	return ec_has_more_events;
75 }
76 EXPORT_SYMBOL(cros_ec_handle_event);
77 
78 static irqreturn_t ec_irq_thread(int irq, void *data)
79 {
80 	struct cros_ec_device *ec_dev = data;
81 	bool ec_has_more_events;
82 
83 	do {
84 		ec_has_more_events = cros_ec_handle_event(ec_dev);
85 	} while (ec_has_more_events);
86 
87 	return IRQ_HANDLED;
88 }
89 
90 static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
91 {
92 	int ret;
93 	struct {
94 		struct cros_ec_command msg;
95 		union {
96 			struct ec_params_host_sleep_event req0;
97 			struct ec_params_host_sleep_event_v1 req1;
98 			struct ec_response_host_sleep_event_v1 resp1;
99 		} u;
100 	} __packed buf;
101 
102 	memset(&buf, 0, sizeof(buf));
103 
104 	if (ec_dev->host_sleep_v1) {
105 		buf.u.req1.sleep_event = sleep_event;
106 		buf.u.req1.suspend_params.sleep_timeout_ms =
107 				EC_HOST_SLEEP_TIMEOUT_DEFAULT;
108 
109 		buf.msg.outsize = sizeof(buf.u.req1);
110 		if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
111 		    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
112 			buf.msg.insize = sizeof(buf.u.resp1);
113 
114 		buf.msg.version = 1;
115 
116 	} else {
117 		buf.u.req0.sleep_event = sleep_event;
118 		buf.msg.outsize = sizeof(buf.u.req0);
119 	}
120 
121 	buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
122 
123 	ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
124 
125 	/* For now, report failure to transition to S0ix with a warning. */
126 	if (ret >= 0 && ec_dev->host_sleep_v1 &&
127 	    (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) {
128 		ec_dev->last_resume_result =
129 			buf.u.resp1.resume_response.sleep_transitions;
130 
131 		WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
132 			  EC_HOST_RESUME_SLEEP_TIMEOUT,
133 			  "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
134 			  buf.u.resp1.resume_response.sleep_transitions &
135 			  EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
136 	}
137 
138 	return ret;
139 }
140 
141 /**
142  * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
143  * @ec_dev: Device to register.
144  *
145  * Before calling this, allocate a pointer to a new device and then fill
146  * in all the fields up to the --private-- marker.
147  *
148  * Return: 0 on success or negative error code.
149  */
150 int cros_ec_register(struct cros_ec_device *ec_dev)
151 {
152 	struct device *dev = ec_dev->dev;
153 	int err = 0;
154 
155 	BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
156 
157 	ec_dev->max_request = sizeof(struct ec_params_hello);
158 	ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
159 	ec_dev->max_passthru = 0;
160 
161 	ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
162 	if (!ec_dev->din)
163 		return -ENOMEM;
164 
165 	ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
166 	if (!ec_dev->dout)
167 		return -ENOMEM;
168 
169 	mutex_init(&ec_dev->lock);
170 
171 	err = cros_ec_query_all(ec_dev);
172 	if (err) {
173 		dev_err(dev, "Cannot identify the EC: error %d\n", err);
174 		return err;
175 	}
176 
177 	if (ec_dev->irq > 0) {
178 		err = devm_request_threaded_irq(dev, ec_dev->irq,
179 						ec_irq_handler,
180 						ec_irq_thread,
181 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
182 						"chromeos-ec", ec_dev);
183 		if (err) {
184 			dev_err(dev, "Failed to request IRQ %d: %d",
185 				ec_dev->irq, err);
186 			return err;
187 		}
188 	}
189 
190 	/* Register a platform device for the main EC instance */
191 	ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
192 					PLATFORM_DEVID_AUTO, &ec_p,
193 					sizeof(struct cros_ec_platform));
194 	if (IS_ERR(ec_dev->ec)) {
195 		dev_err(ec_dev->dev,
196 			"Failed to create CrOS EC platform device\n");
197 		return PTR_ERR(ec_dev->ec);
198 	}
199 
200 	if (ec_dev->max_passthru) {
201 		/*
202 		 * Register a platform device for the PD behind the main EC.
203 		 * We make the following assumptions:
204 		 * - behind an EC, we have a pd
205 		 * - only one device added.
206 		 * - the EC is responsive at init time (it is not true for a
207 		 *   sensor hub).
208 		 */
209 		ec_dev->pd = platform_device_register_data(ec_dev->dev,
210 					"cros-ec-dev",
211 					PLATFORM_DEVID_AUTO, &pd_p,
212 					sizeof(struct cros_ec_platform));
213 		if (IS_ERR(ec_dev->pd)) {
214 			dev_err(ec_dev->dev,
215 				"Failed to create CrOS PD platform device\n");
216 			platform_device_unregister(ec_dev->ec);
217 			return PTR_ERR(ec_dev->pd);
218 		}
219 	}
220 
221 	if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
222 		err = devm_of_platform_populate(dev);
223 		if (err) {
224 			platform_device_unregister(ec_dev->pd);
225 			platform_device_unregister(ec_dev->ec);
226 			dev_err(dev, "Failed to register sub-devices\n");
227 			return err;
228 		}
229 	}
230 
231 	/*
232 	 * Clear sleep event - this will fail harmlessly on platforms that
233 	 * don't implement the sleep event host command.
234 	 */
235 	err = cros_ec_sleep_event(ec_dev, 0);
236 	if (err < 0)
237 		dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
238 			err);
239 
240 	dev_info(dev, "Chrome EC device registered\n");
241 
242 	return 0;
243 }
244 EXPORT_SYMBOL(cros_ec_register);
245 
246 /**
247  * cros_ec_unregister() - Remove a ChromeOS EC.
248  * @ec_dev: Device to unregister.
249  *
250  * Call this to deregister a ChromeOS EC, then clean up any private data.
251  *
252  * Return: 0 on success or negative error code.
253  */
254 int cros_ec_unregister(struct cros_ec_device *ec_dev)
255 {
256 	if (ec_dev->pd)
257 		platform_device_unregister(ec_dev->pd);
258 	platform_device_unregister(ec_dev->ec);
259 
260 	return 0;
261 }
262 EXPORT_SYMBOL(cros_ec_unregister);
263 
264 #ifdef CONFIG_PM_SLEEP
265 /**
266  * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
267  * @ec_dev: Device to suspend.
268  *
269  * This can be called by drivers to handle a suspend event.
270  *
271  * Return: 0 on success or negative error code.
272  */
273 int cros_ec_suspend(struct cros_ec_device *ec_dev)
274 {
275 	struct device *dev = ec_dev->dev;
276 	int ret;
277 	u8 sleep_event;
278 
279 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
280 		      HOST_SLEEP_EVENT_S3_SUSPEND :
281 		      HOST_SLEEP_EVENT_S0IX_SUSPEND;
282 
283 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
284 	if (ret < 0)
285 		dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
286 			ret);
287 
288 	if (device_may_wakeup(dev))
289 		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
290 
291 	disable_irq(ec_dev->irq);
292 	ec_dev->was_wake_device = ec_dev->wake_enabled;
293 	ec_dev->suspended = true;
294 
295 	return 0;
296 }
297 EXPORT_SYMBOL(cros_ec_suspend);
298 
299 static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
300 {
301 	while (ec_dev->mkbp_event_supported &&
302 	       cros_ec_get_next_event(ec_dev, NULL, NULL) > 0)
303 		blocking_notifier_call_chain(&ec_dev->event_notifier,
304 					     1, ec_dev);
305 }
306 
307 /**
308  * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
309  * @ec_dev: Device to resume.
310  *
311  * This can be called by drivers to handle a resume event.
312  *
313  * Return: 0 on success or negative error code.
314  */
315 int cros_ec_resume(struct cros_ec_device *ec_dev)
316 {
317 	int ret;
318 	u8 sleep_event;
319 
320 	ec_dev->suspended = false;
321 	enable_irq(ec_dev->irq);
322 
323 	sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
324 		      HOST_SLEEP_EVENT_S3_RESUME :
325 		      HOST_SLEEP_EVENT_S0IX_RESUME;
326 
327 	ret = cros_ec_sleep_event(ec_dev, sleep_event);
328 	if (ret < 0)
329 		dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
330 			ret);
331 
332 	if (ec_dev->wake_enabled) {
333 		disable_irq_wake(ec_dev->irq);
334 		ec_dev->wake_enabled = 0;
335 	}
336 	/*
337 	 * Let the mfd devices know about events that occur during
338 	 * suspend. This way the clients know what to do with them.
339 	 */
340 	cros_ec_report_events_during_suspend(ec_dev);
341 
342 
343 	return 0;
344 }
345 EXPORT_SYMBOL(cros_ec_resume);
346 
347 #endif
348 
349 MODULE_LICENSE("GPL");
350 MODULE_DESCRIPTION("ChromeOS EC core driver");
351