Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mfd / cros_ec.c
blob5d5c41ac3845331218ce041aea68584605c4c242
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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/mfd/core.h>
17 #include <linux/mfd/cros_ec.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 const struct mfd_cell ec_cell = {
35 .name = "cros-ec-dev",
36 .platform_data = &ec_p,
37 .pdata_size = sizeof(ec_p),
40 static const struct mfd_cell ec_pd_cell = {
41 .name = "cros-ec-dev",
42 .platform_data = &pd_p,
43 .pdata_size = sizeof(pd_p),
46 static irqreturn_t ec_irq_thread(int irq, void *data)
48 struct cros_ec_device *ec_dev = data;
49 bool wake_event = true;
50 int ret;
52 ret = cros_ec_get_next_event(ec_dev, &wake_event);
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)
59 if (wake_event && device_may_wakeup(ec_dev->dev))
60 pm_wakeup_event(ec_dev->dev, 0);
62 if (ret > 0)
63 blocking_notifier_call_chain(&ec_dev->event_notifier,
64 0, ec_dev);
65 return IRQ_HANDLED;
68 static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
70 int ret;
71 struct {
72 struct cros_ec_command msg;
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;
78 } __packed buf;
80 memset(&buf, 0, sizeof(buf));
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;
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);
92 buf.msg.version = 1;
94 } else {
95 buf.u.req0.sleep_event = sleep_event;
96 buf.msg.outsize = sizeof(buf.u.req0);
99 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
101 ret = cros_ec_cmd_xfer(ec_dev, &buf.msg);
103 /* For now, report failure to transition to S0ix with a warning. */
104 if (ret >= 0 && ec_dev->host_sleep_v1 &&
105 (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
106 WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
107 EC_HOST_RESUME_SLEEP_TIMEOUT,
108 "EC detected sleep transition timeout. Total slp_s0 transitions: %d",
109 buf.u.resp1.resume_response.sleep_transitions &
110 EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
112 return ret;
115 int cros_ec_register(struct cros_ec_device *ec_dev)
117 struct device *dev = ec_dev->dev;
118 int err = 0;
120 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
122 ec_dev->max_request = sizeof(struct ec_params_hello);
123 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
124 ec_dev->max_passthru = 0;
126 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
127 if (!ec_dev->din)
128 return -ENOMEM;
130 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
131 if (!ec_dev->dout)
132 return -ENOMEM;
134 mutex_init(&ec_dev->lock);
136 err = cros_ec_query_all(ec_dev);
137 if (err) {
138 dev_err(dev, "Cannot identify the EC: error %d\n", err);
139 return err;
142 if (ec_dev->irq) {
143 err = devm_request_threaded_irq(dev, ec_dev->irq, NULL,
144 ec_irq_thread, IRQF_TRIGGER_LOW | IRQF_ONESHOT,
145 "chromeos-ec", ec_dev);
146 if (err) {
147 dev_err(dev, "Failed to request IRQ %d: %d",
148 ec_dev->irq, err);
149 return err;
153 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell,
154 1, NULL, ec_dev->irq, NULL);
155 if (err) {
156 dev_err(dev,
157 "Failed to register Embedded Controller subdevice %d\n",
158 err);
159 return err;
162 if (ec_dev->max_passthru) {
164 * Register a PD device as well on top of this device.
165 * We make the following assumptions:
166 * - behind an EC, we have a pd
167 * - only one device added.
168 * - the EC is responsive at init time (it is not true for a
169 * sensor hub.
171 err = devm_mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
172 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
173 if (err) {
174 dev_err(dev,
175 "Failed to register Power Delivery subdevice %d\n",
176 err);
177 return err;
181 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
182 err = devm_of_platform_populate(dev);
183 if (err) {
184 mfd_remove_devices(dev);
185 dev_err(dev, "Failed to register sub-devices\n");
186 return err;
191 * Clear sleep event - this will fail harmlessly on platforms that
192 * don't implement the sleep event host command.
194 err = cros_ec_sleep_event(ec_dev, 0);
195 if (err < 0)
196 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
197 err);
199 dev_info(dev, "Chrome EC device registered\n");
201 return 0;
203 EXPORT_SYMBOL(cros_ec_register);
205 #ifdef CONFIG_PM_SLEEP
206 int cros_ec_suspend(struct cros_ec_device *ec_dev)
208 struct device *dev = ec_dev->dev;
209 int ret;
210 u8 sleep_event;
212 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
213 HOST_SLEEP_EVENT_S3_SUSPEND :
214 HOST_SLEEP_EVENT_S0IX_SUSPEND;
216 ret = cros_ec_sleep_event(ec_dev, sleep_event);
217 if (ret < 0)
218 dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec",
219 ret);
221 if (device_may_wakeup(dev))
222 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
224 disable_irq(ec_dev->irq);
225 ec_dev->was_wake_device = ec_dev->wake_enabled;
226 ec_dev->suspended = true;
228 return 0;
230 EXPORT_SYMBOL(cros_ec_suspend);
232 static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
234 while (ec_dev->mkbp_event_supported &&
235 cros_ec_get_next_event(ec_dev, NULL) > 0)
236 blocking_notifier_call_chain(&ec_dev->event_notifier,
237 1, ec_dev);
240 int cros_ec_resume(struct cros_ec_device *ec_dev)
242 int ret;
243 u8 sleep_event;
245 ec_dev->suspended = false;
246 enable_irq(ec_dev->irq);
248 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
249 HOST_SLEEP_EVENT_S3_RESUME :
250 HOST_SLEEP_EVENT_S0IX_RESUME;
252 ret = cros_ec_sleep_event(ec_dev, sleep_event);
253 if (ret < 0)
254 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
255 ret);
257 if (ec_dev->wake_enabled) {
258 disable_irq_wake(ec_dev->irq);
259 ec_dev->wake_enabled = 0;
262 * Let the mfd devices know about events that occur during
263 * suspend. This way the clients know what to do with them.
265 cros_ec_report_events_during_suspend(ec_dev);
268 return 0;
270 EXPORT_SYMBOL(cros_ec_resume);
272 #endif
274 MODULE_LICENSE("GPL");
275 MODULE_DESCRIPTION("ChromeOS EC core driver");