]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mfd/cros_ec.c
mfd: intel-lpss: Fix Intel Cannon Lake LPSS I2C input clock
[mirror_ubuntu-bionic-kernel.git] / drivers / mfd / cros_ec.c
CommitLineData
4ab6174e
SG
1/*
2 * ChromeOS EC multi-function device
3 *
4 * Copyright (C) 2012 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * The ChromeOS EC multi function device is used to mux all the requests
16 * to the EC device for its multiple features: keyboard controller,
17 * battery charging and regulator control, firmware update.
18 */
19
bb03ffb9 20#include <linux/of_platform.h>
4ab6174e
SG
21#include <linux/interrupt.h>
22#include <linux/slab.h>
5ebeaff5 23#include <linux/module.h>
4ab6174e
SG
24#include <linux/mfd/core.h>
25#include <linux/mfd/cros_ec.h>
f00c06fd 26#include <linux/suspend.h>
6f1d912b 27#include <asm/unaligned.h>
a6551a76 28
57b33ff0
GG
29#define CROS_EC_DEV_EC_INDEX 0
30#define CROS_EC_DEV_PD_INDEX 1
31
cf649e00 32static struct cros_ec_platform ec_p = {
57b33ff0
GG
33 .ec_name = CROS_EC_DEV_NAME,
34 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
35};
36
cf649e00 37static struct cros_ec_platform pd_p = {
57b33ff0
GG
38 .ec_name = CROS_EC_DEV_PD_NAME,
39 .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
40};
41
cf649e00 42static const struct mfd_cell ec_cell = {
57b33ff0
GG
43 .name = "cros-ec-ctl",
44 .platform_data = &ec_p,
45 .pdata_size = sizeof(ec_p),
46};
47
cf649e00 48static const struct mfd_cell ec_pd_cell = {
57b33ff0
GG
49 .name = "cros-ec-ctl",
50 .platform_data = &pd_p,
51 .pdata_size = sizeof(pd_p),
4ab6174e
SG
52};
53
6f1d912b
VY
54static irqreturn_t ec_irq_thread(int irq, void *data)
55{
56 struct cros_ec_device *ec_dev = data;
29d99b96 57 bool wake_event = true;
6f1d912b
VY
58 int ret;
59
29d99b96
SN
60 ret = cros_ec_get_next_event(ec_dev, &wake_event);
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))
6f1d912b
VY
68 pm_wakeup_event(ec_dev->dev, 0);
69
6f1d912b
VY
70 if (ret > 0)
71 blocking_notifier_call_chain(&ec_dev->event_notifier,
72 0, ec_dev);
73 return IRQ_HANDLED;
74}
75
f00c06fd
SN
76static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
77{
78 struct {
79 struct cros_ec_command msg;
80 struct ec_params_host_sleep_event req;
81 } __packed buf;
82
83 memset(&buf, 0, sizeof(buf));
84
85 buf.req.sleep_event = sleep_event;
86
87 buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
88 buf.msg.version = 0;
89 buf.msg.outsize = sizeof(buf.req);
90
91 return cros_ec_cmd_xfer(ec_dev, &buf.msg);
92}
93
4ab6174e
SG
94int cros_ec_register(struct cros_ec_device *ec_dev)
95{
96 struct device *dev = ec_dev->dev;
97 int err = 0;
98
6f1d912b
VY
99 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
100
2c7589af
SB
101 ec_dev->max_request = sizeof(struct ec_params_hello);
102 ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
103 ec_dev->max_passthru = 0;
104
105 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
106 if (!ec_dev->din)
107 return -ENOMEM;
108
109 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
110 if (!ec_dev->dout)
111 return -ENOMEM;
4ab6174e 112
63427530
AB
113 mutex_init(&ec_dev->lock);
114
2c7589af
SB
115 cros_ec_query_all(ec_dev);
116
6f1d912b
VY
117 if (ec_dev->irq) {
118 err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
119 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
120 "chromeos-ec", ec_dev);
121 if (err) {
122 dev_err(dev, "Failed to request IRQ %d: %d",
123 ec_dev->irq, err);
124 return err;
125 }
126 }
127
57b33ff0 128 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1,
4ab6174e
SG
129 NULL, ec_dev->irq, NULL);
130 if (err) {
57b33ff0
GG
131 dev_err(dev,
132 "Failed to register Embedded Controller subdevice %d\n",
133 err);
6f1d912b 134 goto fail_mfd;
4ab6174e
SG
135 }
136
57b33ff0
GG
137 if (ec_dev->max_passthru) {
138 /*
139 * Register a PD device as well on top of this device.
140 * We make the following assumptions:
141 * - behind an EC, we have a pd
142 * - only one device added.
143 * - the EC is responsive at init time (it is not true for a
144 * sensor hub.
145 */
146 err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO,
147 &ec_pd_cell, 1, NULL, ec_dev->irq, NULL);
148 if (err) {
149 dev_err(dev,
150 "Failed to register Power Delivery subdevice %d\n",
151 err);
6f1d912b 152 goto fail_mfd;
57b33ff0
GG
153 }
154 }
155
bb03ffb9 156 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2eb131ef 157 err = devm_of_platform_populate(dev);
bb03ffb9
TB
158 if (err) {
159 mfd_remove_devices(dev);
160 dev_err(dev, "Failed to register sub-devices\n");
6f1d912b 161 goto fail_mfd;
bb03ffb9
TB
162 }
163 }
164
f00c06fd
SN
165 /*
166 * Clear sleep event - this will fail harmlessly on platforms that
167 * don't implement the sleep event host command.
168 */
169 err = cros_ec_sleep_event(ec_dev, 0);
170 if (err < 0)
171 dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec",
172 err);
173
533cec8f 174 dev_info(dev, "Chrome EC device registered\n");
4ab6174e 175
e04653a9
AP
176 cros_ec_acpi_install_gpe_handler(dev);
177
4ab6174e 178 return 0;
6f1d912b
VY
179
180fail_mfd:
181 if (ec_dev->irq)
182 free_irq(ec_dev->irq, ec_dev);
183 return err;
4ab6174e 184}
5ebeaff5 185EXPORT_SYMBOL(cros_ec_register);
4ab6174e
SG
186
187int cros_ec_remove(struct cros_ec_device *ec_dev)
188{
189 mfd_remove_devices(ec_dev->dev);
4ab6174e 190
e04653a9
AP
191 cros_ec_acpi_remove_gpe_handler();
192
f58b14e6
JC
193 if (ec_dev->irq)
194 free_irq(ec_dev->irq, ec_dev);
195
4ab6174e
SG
196 return 0;
197}
5ebeaff5 198EXPORT_SYMBOL(cros_ec_remove);
4ab6174e
SG
199
200#ifdef CONFIG_PM_SLEEP
201int cros_ec_suspend(struct cros_ec_device *ec_dev)
202{
203 struct device *dev = ec_dev->dev;
f00c06fd
SN
204 int ret;
205 u8 sleep_event;
206
e04653a9
AP
207 if (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) {
208 sleep_event = HOST_SLEEP_EVENT_S3_SUSPEND;
209 } else {
210 sleep_event = HOST_SLEEP_EVENT_S0IX_SUSPEND;
211
212 /* Clearing the GPE status for any pending event */
213 cros_ec_acpi_clear_gpe();
214 }
f00c06fd
SN
215
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);
4ab6174e
SG
220
221 if (device_may_wakeup(dev))
222 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
223
224 disable_irq(ec_dev->irq);
225 ec_dev->was_wake_device = ec_dev->wake_enabled;
a9eb186e 226 ec_dev->suspended = true;
4ab6174e
SG
227
228 return 0;
229}
5ebeaff5 230EXPORT_SYMBOL(cros_ec_suspend);
4ab6174e 231
6f1d912b
VY
232static void cros_ec_drain_events(struct cros_ec_device *ec_dev)
233{
29d99b96 234 while (cros_ec_get_next_event(ec_dev, NULL) > 0)
6f1d912b
VY
235 blocking_notifier_call_chain(&ec_dev->event_notifier,
236 1, ec_dev);
237}
238
4ab6174e
SG
239int cros_ec_resume(struct cros_ec_device *ec_dev)
240{
f00c06fd
SN
241 int ret;
242 u8 sleep_event;
243
a9eb186e 244 ec_dev->suspended = false;
4ab6174e
SG
245 enable_irq(ec_dev->irq);
246
9c576bd3
SN
247 sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
248 HOST_SLEEP_EVENT_S3_RESUME :
249 HOST_SLEEP_EVENT_S0IX_RESUME;
f00c06fd
SN
250
251 ret = cros_ec_sleep_event(ec_dev, sleep_event);
252 if (ret < 0)
253 dev_dbg(ec_dev->dev, "Error %d sending resume event to ec",
254 ret);
255
6f1d912b
VY
256 /*
257 * In some cases, we need to distinguish between events that occur
258 * during suspend if the EC is not a wake source. For example,
259 * keypresses during suspend should be discarded if it does not wake
260 * the system.
261 *
262 * If the EC is not a wake source, drain the event queue and mark them
263 * as "queued during suspend".
264 */
4ab6174e
SG
265 if (ec_dev->wake_enabled) {
266 disable_irq_wake(ec_dev->irq);
267 ec_dev->wake_enabled = 0;
6f1d912b
VY
268 } else {
269 cros_ec_drain_events(ec_dev);
4ab6174e
SG
270 }
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");