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>
22 #define CROS_EC_DEV_EC_INDEX 0
23 #define CROS_EC_DEV_PD_INDEX 1
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
),
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
),
35 static irqreturn_t
ec_irq_handler(int irq
, void *data
)
37 struct cros_ec_device
*ec_dev
= data
;
39 ec_dev
->last_event_time
= cros_ec_get_time_ns();
41 return IRQ_WAKE_THREAD
;
45 * cros_ec_handle_event() - process and forward pending events on EC
46 * @ec_dev: Device with events to process.
48 * Call this function in a loop when the kernel is notified that the EC has
51 * Return: true if more events are still pending and this function should be
54 bool cros_ec_handle_event(struct cros_ec_device
*ec_dev
)
57 bool ec_has_more_events
;
60 ret
= cros_ec_get_next_event(ec_dev
, &wake_event
, &ec_has_more_events
);
63 * Signal only if wake host events or any interrupt if
64 * cros_ec_get_next_event() returned an error (default value for
67 if (wake_event
&& device_may_wakeup(ec_dev
->dev
))
68 pm_wakeup_event(ec_dev
->dev
, 0);
71 blocking_notifier_call_chain(&ec_dev
->event_notifier
,
74 return ec_has_more_events
;
76 EXPORT_SYMBOL(cros_ec_handle_event
);
78 static irqreturn_t
ec_irq_thread(int irq
, void *data
)
80 struct cros_ec_device
*ec_dev
= data
;
81 bool ec_has_more_events
;
84 ec_has_more_events
= cros_ec_handle_event(ec_dev
);
85 } while (ec_has_more_events
);
90 static int cros_ec_sleep_event(struct cros_ec_device
*ec_dev
, u8 sleep_event
)
94 struct cros_ec_command msg
;
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
;
102 memset(&buf
, 0, sizeof(buf
));
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
;
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
);
117 buf
.u
.req0
.sleep_event
= sleep_event
;
118 buf
.msg
.outsize
= sizeof(buf
.u
.req0
);
121 buf
.msg
.command
= EC_CMD_HOST_SLEEP_EVENT
;
123 ret
= cros_ec_cmd_xfer_status(ec_dev
, &buf
.msg
);
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
;
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
);
141 static int cros_ec_ready_event(struct notifier_block
*nb
,
142 unsigned long queued_during_suspend
,
145 struct cros_ec_device
*ec_dev
= container_of(nb
, struct cros_ec_device
,
147 u32 host_event
= cros_ec_get_host_event(ec_dev
);
149 if (host_event
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY
)) {
150 mutex_lock(&ec_dev
->lock
);
151 cros_ec_query_all(ec_dev
);
152 mutex_unlock(&ec_dev
->lock
);
160 * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
161 * @ec_dev: Device to register.
163 * Before calling this, allocate a pointer to a new device and then fill
164 * in all the fields up to the --private-- marker.
166 * Return: 0 on success or negative error code.
168 int cros_ec_register(struct cros_ec_device
*ec_dev
)
170 struct device
*dev
= ec_dev
->dev
;
173 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev
->event_notifier
);
175 ec_dev
->max_request
= sizeof(struct ec_params_hello
);
176 ec_dev
->max_response
= sizeof(struct ec_response_get_protocol_info
);
177 ec_dev
->max_passthru
= 0;
179 ec_dev
->din
= devm_kzalloc(dev
, ec_dev
->din_size
, GFP_KERNEL
);
183 ec_dev
->dout
= devm_kzalloc(dev
, ec_dev
->dout_size
, GFP_KERNEL
);
187 mutex_init(&ec_dev
->lock
);
189 err
= cros_ec_query_all(ec_dev
);
191 dev_err(dev
, "Cannot identify the EC: error %d\n", err
);
195 if (ec_dev
->irq
> 0) {
196 err
= devm_request_threaded_irq(dev
, ec_dev
->irq
,
199 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
200 "chromeos-ec", ec_dev
);
202 dev_err(dev
, "Failed to request IRQ %d: %d",
208 /* Register a platform device for the main EC instance */
209 ec_dev
->ec
= platform_device_register_data(ec_dev
->dev
, "cros-ec-dev",
210 PLATFORM_DEVID_AUTO
, &ec_p
,
211 sizeof(struct cros_ec_platform
));
212 if (IS_ERR(ec_dev
->ec
)) {
214 "Failed to create CrOS EC platform device\n");
215 return PTR_ERR(ec_dev
->ec
);
218 if (ec_dev
->max_passthru
) {
220 * Register a platform device for the PD behind the main EC.
221 * We make the following assumptions:
222 * - behind an EC, we have a pd
223 * - only one device added.
224 * - the EC is responsive at init time (it is not true for a
227 ec_dev
->pd
= platform_device_register_data(ec_dev
->dev
,
229 PLATFORM_DEVID_AUTO
, &pd_p
,
230 sizeof(struct cros_ec_platform
));
231 if (IS_ERR(ec_dev
->pd
)) {
233 "Failed to create CrOS PD platform device\n");
234 platform_device_unregister(ec_dev
->ec
);
235 return PTR_ERR(ec_dev
->pd
);
239 if (IS_ENABLED(CONFIG_OF
) && dev
->of_node
) {
240 err
= devm_of_platform_populate(dev
);
242 platform_device_unregister(ec_dev
->pd
);
243 platform_device_unregister(ec_dev
->ec
);
244 dev_err(dev
, "Failed to register sub-devices\n");
250 * Clear sleep event - this will fail harmlessly on platforms that
251 * don't implement the sleep event host command.
253 err
= cros_ec_sleep_event(ec_dev
, 0);
255 dev_dbg(ec_dev
->dev
, "Error %d clearing sleep event to ec",
258 if (ec_dev
->mkbp_event_supported
) {
260 * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
263 ec_dev
->notifier_ready
.notifier_call
= cros_ec_ready_event
;
264 err
= blocking_notifier_chain_register(&ec_dev
->event_notifier
,
265 &ec_dev
->notifier_ready
);
270 dev_info(dev
, "Chrome EC device registered\n");
274 EXPORT_SYMBOL(cros_ec_register
);
277 * cros_ec_unregister() - Remove a ChromeOS EC.
278 * @ec_dev: Device to unregister.
280 * Call this to deregister a ChromeOS EC, then clean up any private data.
282 * Return: 0 on success or negative error code.
284 int cros_ec_unregister(struct cros_ec_device
*ec_dev
)
287 platform_device_unregister(ec_dev
->pd
);
288 platform_device_unregister(ec_dev
->ec
);
292 EXPORT_SYMBOL(cros_ec_unregister
);
294 #ifdef CONFIG_PM_SLEEP
296 * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
297 * @ec_dev: Device to suspend.
299 * This can be called by drivers to handle a suspend event.
301 * Return: 0 on success or negative error code.
303 int cros_ec_suspend(struct cros_ec_device
*ec_dev
)
305 struct device
*dev
= ec_dev
->dev
;
309 sleep_event
= (!IS_ENABLED(CONFIG_ACPI
) || pm_suspend_via_firmware()) ?
310 HOST_SLEEP_EVENT_S3_SUSPEND
:
311 HOST_SLEEP_EVENT_S0IX_SUSPEND
;
313 ret
= cros_ec_sleep_event(ec_dev
, sleep_event
);
315 dev_dbg(ec_dev
->dev
, "Error %d sending suspend event to ec",
318 if (device_may_wakeup(dev
))
319 ec_dev
->wake_enabled
= !enable_irq_wake(ec_dev
->irq
);
321 disable_irq(ec_dev
->irq
);
322 ec_dev
->was_wake_device
= ec_dev
->wake_enabled
;
323 ec_dev
->suspended
= true;
327 EXPORT_SYMBOL(cros_ec_suspend
);
329 static void cros_ec_report_events_during_suspend(struct cros_ec_device
*ec_dev
)
331 while (ec_dev
->mkbp_event_supported
&&
332 cros_ec_get_next_event(ec_dev
, NULL
, NULL
) > 0)
333 blocking_notifier_call_chain(&ec_dev
->event_notifier
,
338 * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
339 * @ec_dev: Device to resume.
341 * This can be called by drivers to handle a resume event.
343 * Return: 0 on success or negative error code.
345 int cros_ec_resume(struct cros_ec_device
*ec_dev
)
350 ec_dev
->suspended
= false;
351 enable_irq(ec_dev
->irq
);
353 sleep_event
= (!IS_ENABLED(CONFIG_ACPI
) || pm_suspend_via_firmware()) ?
354 HOST_SLEEP_EVENT_S3_RESUME
:
355 HOST_SLEEP_EVENT_S0IX_RESUME
;
357 ret
= cros_ec_sleep_event(ec_dev
, sleep_event
);
359 dev_dbg(ec_dev
->dev
, "Error %d sending resume event to ec",
362 if (ec_dev
->wake_enabled
) {
363 disable_irq_wake(ec_dev
->irq
);
364 ec_dev
->wake_enabled
= 0;
367 * Let the mfd devices know about events that occur during
368 * suspend. This way the clients know what to do with them.
370 cros_ec_report_events_during_suspend(ec_dev
);
375 EXPORT_SYMBOL(cros_ec_resume
);
379 MODULE_LICENSE("GPL");
380 MODULE_DESCRIPTION("ChromeOS EC core driver");