Merge branch 'asoc-5.3' into asoc-linus
[linux-2.6-block.git] / drivers / mfd / cros_ec.c
CommitLineData
9c92ab61 1// SPDX-License-Identifier: GPL-2.0-only
4ab6174e
SG
2/*
3 * ChromeOS EC multi-function device
4 *
5 * Copyright (C) 2012 Google, Inc
6 *
4ab6174e
SG
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
bb03ffb9 12#include <linux/of_platform.h>
4ab6174e
SG
13#include <linux/interrupt.h>
14#include <linux/slab.h>
5ebeaff5 15#include <linux/module.h>
4ab6174e
SG
16#include <linux/mfd/core.h>
17#include <linux/mfd/cros_ec.h>
f00c06fd 18#include <linux/suspend.h>
6f1d912b 19#include <asm/unaligned.h>
a6551a76 20
57b33ff0
GG
21#define CROS_EC_DEV_EC_INDEX 0
22#define CROS_EC_DEV_PD_INDEX 1
23
cf649e00 24static struct cros_ec_platform ec_p = {
57b33ff0
GG
25 .ec_name = CROS_EC_DEV_NAME,
26 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
27};
28
cf649e00 29static struct cros_ec_platform pd_p = {
57b33ff0
GG
30 .ec_name = CROS_EC_DEV_PD_NAME,
31 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
32};
33
cf649e00 34static const struct mfd_cell ec_cell = {
ea01a31b 35 .name = "cros-ec-dev",
57b33ff0
GG
36 .platform_data = &ec_p,
37 .pdata_size = sizeof(ec_p),
38};
39
cf649e00 40static const struct mfd_cell ec_pd_cell = {
ea01a31b 41 .name = "cros-ec-dev",
57b33ff0
GG
42 .platform_data = &pd_p,
43 .pdata_size = sizeof(pd_p),
4ab6174e
SG
44};
45
6f1d912b
VY
46static irqreturn_t ec_irq_thread(int irq, void *data)
47{
48 struct cros_ec_device *ec_dev = data;
29d99b96 49 bool wake_event = true;
6f1d912b
VY
50 int ret;
51
29d99b96
SN
52 ret = cros_ec_get_next_event(ec_dev, &wake_event);
53
54 /*
55 * Signal only if wake host events or any interrupt if
56 * cros_ec_get_next_event() returned an error (default value for
57 * wake_event is true)
58 */
59 if (wake_event && device_may_wakeup(ec_dev->dev))
6f1d912b
VY
60 pm_wakeup_event(ec_dev->dev, 0);
61
6f1d912b
VY
62 if (ret > 0)
63 blocking_notifier_call_chain(&ec_dev->event_notifier,
64 0, ec_dev);
65 return IRQ_HANDLED;
66}
67
f00c06fd
SN
68static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
69{
7235560a 70 int ret;
f00c06fd
SN
71 struct {
72 struct cros_ec_command msg;
7235560a
EG
73 union {
74 struct ec_params_host_sleep_event req0;
75 struct ec_params_host_sleep_event_v1 req1;
76 struct ec_response_host_sleep_event_v1 resp1;
77 } u;
f00c06fd
SN
78 } __packed buf;
79
80 memset(&buf, 0, sizeof(buf));
81
7235560a
EG
82 if (ec_dev->host_sleep_v1) {
83 buf.u.req1.sleep_event = sleep_event;
84 buf.u.req1.suspend_params.sleep_timeout_ms =
85 EC_HOST_SLEEP_TIMEOUT_DEFAULT;
86
87 buf.msg.outsize = sizeof(buf.u.req1);
88 if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
89 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
90 buf.msg.insize = sizeof(buf.u.resp1);
91
92 buf.msg.version = 1;
93
94 } else {
95 buf.u.req0.sleep_event = sleep_event;
96 buf.msg.outsize = sizeof(buf.u.req0);
97 }
f00c06fd
SN
98
99 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
f00c06fd 100
7235560a
EG
101 ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
102
103 /* For now, report failure to transition to S0ix with a warning. */
104 if (ret >= 0 && ec_dev->host_sleep_v1 &&
8c3166e1
EG
105 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME)) {
106 ec_dev->last_resume_result =
107 buf.u.resp1.resume_response.sleep_transitions;
108
7235560a
EG
109 WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
110 EC_HOST_RESUME_SLEEP_TIMEOUT,
111 "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
112 buf.u.resp1.resume_response.sleep_transitions &
113 EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
8c3166e1 114 }
7235560a
EG
115
116 return ret;
f00c06fd
SN
117}
118
4ab6174e
SG
119int cros_ec_register(struct cros_ec_device *ec_dev)
120{
121 struct device *dev = ec_dev->dev;
122 int err = 0;
123
6f1d912b
VY
124 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
125
2c7589af
SB
126 ec_dev->max_request = sizeof(struct ec_params_hello);
127 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
128 ec_dev->max_passthru = 0;
129
130 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
131 if (!ec_dev->din)
132 return -ENOMEM;
133
134 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
135 if (!ec_dev->dout)
136 return -ENOMEM;
4ab6174e 137
63427530
AB
138 mutex_init(&ec_dev->lock);
139
0dbbf255
VP
140 err = cros_ec_query_all(ec_dev);
141 if (err) {
142 dev_err(dev, "Cannot identify the EC: error %d\n", err);
143 return err;
144 }
2c7589af 145
6f1d912b 146 if (ec_dev->irq) {
eb3f2f23
VP
147 err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
148 ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
149 "chromeos-ec", ec_dev);
6f1d912b
VY
150 if (err) {
151 dev_err(dev, "Failed to request IRQ %d: %d",
152 ec_dev->irq, err);
153 return err;
154 }
155 }
156
4bc59c2f
EBS
157 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell,
158 1, NULL, ec_dev->irq, NULL);
4ab6174e 159 if (err) {
57b33ff0
GG
160 dev_err(dev,
161 "Failed to register Embedded Controller subdevice %d\n",
162 err);
eb3f2f23 163 return err;
4ab6174e
SG
164 }
165
57b33ff0
GG
166 if (ec_dev->max_passthru) {
167 /*
168 * Register a PD device as well on top of this device.
169 * We make the following assumptions:
170 * - behind an EC, we have a pd
171 * - only one device added.
172 * - the EC is responsive at init time (it is not true for a
173 * sensor hub.
174 */
4bc59c2f 175 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
57b33ff0
GG
176 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
177 if (err) {
178 dev_err(dev,
179 "Failed to register Power Delivery subdevice %d\n",
180 err);
eb3f2f23 181 return err;
57b33ff0
GG
182 }
183 }
184
bb03ffb9 185 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2eb131ef 186 err = devm_of_platform_populate(dev);
bb03ffb9
TB
187 if (err) {
188 mfd_remove_devices(dev);
189 dev_err(dev, "Failed to register sub-devices\n");
eb3f2f23 190 return err;
bb03ffb9
TB
191 }
192 }
193
f00c06fd
SN
194 /*
195 * Clear sleep event - this will fail harmlessly on platforms that
196 * don't implement the sleep event host command.
197 */
198 err = cros_ec_sleep_event(ec_dev, 0);
199 if (err < 0)
200 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
201 err);
202
533cec8f 203 dev_info(dev, "Chrome EC device registered\n");
4ab6174e
SG
204
205 return 0;
4ab6174e 206}
5ebeaff5 207EXPORT_SYMBOL(cros_ec_register);
4ab6174e 208
4ab6174e
SG
209#ifdef CONFIG_PM_SLEEP
210int cros_ec_suspend(struct cros_ec_device *ec_dev)
211{
212 struct device *dev = ec_dev->dev;
f00c06fd
SN
213 int ret;
214 u8 sleep_event;
215
99fb0f25
WD
216 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
217 HOST_SLEEP_EVENT_S3_SUSPEND :
218 HOST_SLEEP_EVENT_S0IX_SUSPEND;
f00c06fd
SN
219
220 ret = cros_ec_sleep_event(ec_dev, sleep_event);
221 if (ret < 0)
222 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
223 ret);
4ab6174e
SG
224
225 if (device_may_wakeup(dev))
226 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
227
228 disable_irq(ec_dev->irq);
229 ec_dev->was_wake_device = ec_dev->wake_enabled;
a9eb186e 230 ec_dev->suspended = true;
4ab6174e
SG
231
232 return 0;
233}
5ebeaff5 234EXPORT_SYMBOL(cros_ec_suspend);
4ab6174e 235
38ba34a4 236static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
6f1d912b 237{
61cc15da
RS
238 while (ec_dev->mkbp_event_supported &&
239 cros_ec_get_next_event(ec_dev, NULL) > 0)
6f1d912b
VY
240 blocking_notifier_call_chain(&ec_dev->event_notifier,
241 1, ec_dev);
242}
243
4ab6174e
SG
244int cros_ec_resume(struct cros_ec_device *ec_dev)
245{
f00c06fd
SN
246 int ret;
247 u8 sleep_event;
248
a9eb186e 249 ec_dev->suspended = false;
4ab6174e
SG
250 enable_irq(ec_dev->irq);
251
9c576bd3
SN
252 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
253 HOST_SLEEP_EVENT_S3_RESUME :
254 HOST_SLEEP_EVENT_S0IX_RESUME;
f00c06fd
SN
255
256 ret = cros_ec_sleep_event(ec_dev, sleep_event);
257 if (ret < 0)
258 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
259 ret);
260
4ab6174e
SG
261 if (ec_dev->wake_enabled) {
262 disable_irq_wake(ec_dev->irq);
263 ec_dev->wake_enabled = 0;
264 }
38ba34a4
RCS
265 /*
266 * Let the mfd devices know about events that occur during
267 * suspend. This way the clients know what to do with them.
268 */
269 cros_ec_report_events_during_suspend(ec_dev);
270
4ab6174e
SG
271
272 return 0;
273}
5ebeaff5
SO
274EXPORT_SYMBOL(cros_ec_resume);
275
4ab6174e 276#endif
a865a589
BR
277
278MODULE_LICENSE("GPL");
279MODULE_DESCRIPTION("ChromeOS EC core driver");