1 // SPDX-License-Identifier: GPL-2.0-only
3 * ChromeOS EC multi-function device
5 * Copyright (C) 2012 Google, Inc
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.
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 #include <asm/unaligned.h>
21 #define CROS_EC_DEV_EC_INDEX 0
22 #define CROS_EC_DEV_PD_INDEX 1
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),
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),
34 static irqreturn_t ec_irq_handler(int irq, void *data)
36 struct cros_ec_device *ec_dev = data;
38 ec_dev->last_event_time = cros_ec_get_time_ns();
40 return IRQ_WAKE_THREAD;
44 * cros_ec_handle_event() - process and forward pending events on EC
45 * @ec_dev: Device with events to process.
47 * Call this function in a loop when the kernel is notified that the EC has
50 * Return: true if more events are still pending and this function should be
53 bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
56 bool ec_has_more_events;
59 ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
62 * Signal only if wake host events or any interrupt if
63 * cros_ec_get_next_event() returned an error (default value for
66 if (wake_event && device_may_wakeup(ec_dev->dev))
67 pm_wakeup_event(ec_dev->dev, 0);
70 blocking_notifier_call_chain(&ec_dev->event_notifier,
73 return ec_has_more_events;
75 EXPORT_SYMBOL(cros_ec_handle_event);
77 static irqreturn_t ec_irq_thread(int irq, void *data)
79 struct cros_ec_device *ec_dev = data;
80 bool ec_has_more_events;
83 ec_has_more_events = cros_ec_handle_event(ec_dev);
84 } while (ec_has_more_events);
89 static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
93 struct cros_ec_command msg;
95 struct ec_params_host_sleep_event req0;
96 struct ec_params_host_sleep_event_v1 req1;
97 struct ec_response_host_sleep_event_v1 resp1;
101 memset(&buf, 0, sizeof(buf));
103 if (ec_dev->host_sleep_v1) {
104 buf.u.req1.sleep_event = sleep_event;
105 buf.u.req1.suspend_params.sleep_timeout_ms =
106 EC_HOST_SLEEP_TIMEOUT_DEFAULT;
108 buf.msg.outsize = sizeof(buf.u.req1);
109 if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
110 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
111 buf.msg.insize = sizeof(buf.u.resp1);
116 buf.u.req0.sleep_event = sleep_event;
117 buf.msg.outsize = sizeof(buf.u.req0);
120 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
122 ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
124 /* For now, report failure to transition to S0ix with a warning. */
125 if (ret >= 0 && ec_dev->host_sleep_v1 &&
126 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) {
127 ec_dev->last_resume_result =
128 buf.u.resp1.resume_response.sleep_transitions;
130 WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
131 EC_HOST_RESUME_SLEEP_TIMEOUT,
132 "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
133 buf.u.resp1.resume_response.sleep_transitions &
134 EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
141 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
142 * @ec_dev: Device to register.
144 * Before calling this, allocate a pointer to a new device and then fill
145 * in all the fields up to the --private-- marker.
147 * Return: 0 on success or negative error code.
149 int cros_ec_register(struct cros_ec_device *ec_dev)
151 struct device *dev = ec_dev->dev;
154 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
156 ec_dev->max_request = sizeof(struct ec_params_hello);
157 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
158 ec_dev->max_passthru = 0;
160 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
164 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
168 mutex_init(&ec_dev->lock);
170 err = cros_ec_query_all(ec_dev);
172 dev_err(dev, "Cannot identify the EC: error %d\n", err);
176 if (ec_dev->irq > 0) {
177 err = devm_request_threaded_irq(dev, ec_dev->irq,
180 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
181 "chromeos-ec", ec_dev);
183 dev_err(dev, "Failed to request IRQ %d: %d",
189 /* Register a platform device for the main EC instance */
190 ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
191 PLATFORM_DEVID_AUTO, &ec_p,
192 sizeof(struct cros_ec_platform));
193 if (IS_ERR(ec_dev->ec)) {
195 "Failed to create CrOS EC platform device\n");
196 return PTR_ERR(ec_dev->ec);
199 if (ec_dev->max_passthru) {
201 * Register a platform device for the PD behind the main EC.
202 * We make the following assumptions:
203 * - behind an EC, we have a pd
204 * - only one device added.
205 * - the EC is responsive at init time (it is not true for a
208 ec_dev->pd = platform_device_register_data(ec_dev->dev,
210 PLATFORM_DEVID_AUTO, &pd_p,
211 sizeof(struct cros_ec_platform));
212 if (IS_ERR(ec_dev->pd)) {
214 "Failed to create CrOS PD platform device\n");
215 platform_device_unregister(ec_dev->ec);
216 return PTR_ERR(ec_dev->pd);
220 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
221 err = devm_of_platform_populate(dev);
223 platform_device_unregister(ec_dev->pd);
224 platform_device_unregister(ec_dev->ec);
225 dev_err(dev, "Failed to register sub-devices\n");
231 * Clear sleep event - this will fail harmlessly on platforms that
232 * don't implement the sleep event host command.
234 err = cros_ec_sleep_event(ec_dev, 0);
236 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
239 dev_info(dev, "Chrome EC device registered\n");
243 EXPORT_SYMBOL(cros_ec_register);
246 * cros_ec_unregister() - Remove a ChromeOS EC.
247 * @ec_dev: Device to unregister.
249 * Call this to deregister a ChromeOS EC, then clean up any private data.
251 * Return: 0 on success or negative error code.
253 int cros_ec_unregister(struct cros_ec_device *ec_dev)
256 platform_device_unregister(ec_dev->pd);
257 platform_device_unregister(ec_dev->ec);
261 EXPORT_SYMBOL(cros_ec_unregister);
263 #ifdef CONFIG_PM_SLEEP
265 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
266 * @ec_dev: Device to suspend.
268 * This can be called by drivers to handle a suspend event.
270 * Return: 0 on success or negative error code.
272 int cros_ec_suspend(struct cros_ec_device *ec_dev)
274 struct device *dev = ec_dev->dev;
278 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
279 HOST_SLEEP_EVENT_S3_SUSPEND :
280 HOST_SLEEP_EVENT_S0IX_SUSPEND;
282 ret = cros_ec_sleep_event(ec_dev, sleep_event);
284 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
287 if (device_may_wakeup(dev))
288 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
290 disable_irq(ec_dev->irq);
291 ec_dev->was_wake_device = ec_dev->wake_enabled;
292 ec_dev->suspended = true;
296 EXPORT_SYMBOL(cros_ec_suspend);
298 static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
300 while (ec_dev->mkbp_event_supported &&
301 cros_ec_get_next_event(ec_dev, NULL, NULL) > 0)
302 blocking_notifier_call_chain(&ec_dev->event_notifier,
307 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
308 * @ec_dev: Device to resume.
310 * This can be called by drivers to handle a resume event.
312 * Return: 0 on success or negative error code.
314 int cros_ec_resume(struct cros_ec_device *ec_dev)
319 ec_dev->suspended = false;
320 enable_irq(ec_dev->irq);
322 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
323 HOST_SLEEP_EVENT_S3_RESUME :
324 HOST_SLEEP_EVENT_S0IX_RESUME;
326 ret = cros_ec_sleep_event(ec_dev, sleep_event);
328 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
331 if (ec_dev->wake_enabled) {
332 disable_irq_wake(ec_dev->irq);
333 ec_dev->wake_enabled = 0;
336 * Let the mfd devices know about events that occur during
337 * suspend. This way the clients know what to do with them.
339 cros_ec_report_events_during_suspend(ec_dev);
344 EXPORT_SYMBOL(cros_ec_resume);
348 MODULE_LICENSE("GPL");
349 MODULE_DESCRIPTION("ChromeOS EC core driver");