]>
Commit | Line | Data |
---|---|---|
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 | 32 | static 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 | 37 | static 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 | 42 | static 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 | 48 | static 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 |
54 | static irqreturn_t ec_irq_thread(int irq, void *data) |
55 | { | |
56 | struct cros_ec_device *ec_dev = data; | |
57 | int ret; | |
58 | ||
59 | if (device_may_wakeup(ec_dev->dev)) | |
60 | pm_wakeup_event(ec_dev->dev, 0); | |
61 | ||
62 | ret = cros_ec_get_next_event(ec_dev); | |
63 | if (ret > 0) | |
64 | blocking_notifier_call_chain(&ec_dev->event_notifier, | |
65 | 0, ec_dev); | |
66 | return IRQ_HANDLED; | |
67 | } | |
68 | ||
f00c06fd SN |
69 | static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event) |
70 | { | |
71 | struct { | |
72 | struct cros_ec_command msg; | |
73 | struct ec_params_host_sleep_event req; | |
74 | } __packed buf; | |
75 | ||
76 | memset(&buf, 0, sizeof(buf)); | |
77 | ||
78 | buf.req.sleep_event = sleep_event; | |
79 | ||
80 | buf.msg.command = EC_CMD_HOST_SLEEP_EVENT; | |
81 | buf.msg.version = 0; | |
82 | buf.msg.outsize = sizeof(buf.req); | |
83 | ||
84 | return cros_ec_cmd_xfer(ec_dev, &buf.msg); | |
85 | } | |
86 | ||
4ab6174e SG |
87 | int cros_ec_register(struct cros_ec_device *ec_dev) |
88 | { | |
89 | struct device *dev = ec_dev->dev; | |
90 | int err = 0; | |
91 | ||
6f1d912b VY |
92 | BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier); |
93 | ||
2c7589af SB |
94 | ec_dev->max_request = sizeof(struct ec_params_hello); |
95 | ec_dev->max_response = sizeof(struct ec_response_get_protocol_info); | |
96 | ec_dev->max_passthru = 0; | |
97 | ||
98 | ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); | |
99 | if (!ec_dev->din) | |
100 | return -ENOMEM; | |
101 | ||
102 | ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); | |
103 | if (!ec_dev->dout) | |
104 | return -ENOMEM; | |
4ab6174e | 105 | |
63427530 AB |
106 | mutex_init(&ec_dev->lock); |
107 | ||
2c7589af SB |
108 | cros_ec_query_all(ec_dev); |
109 | ||
6f1d912b VY |
110 | if (ec_dev->irq) { |
111 | err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread, | |
112 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | |
113 | "chromeos-ec", ec_dev); | |
114 | if (err) { | |
115 | dev_err(dev, "Failed to request IRQ %d: %d", | |
116 | ec_dev->irq, err); | |
117 | return err; | |
118 | } | |
119 | } | |
120 | ||
57b33ff0 | 121 | err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, &ec_cell, 1, |
4ab6174e SG |
122 | NULL, ec_dev->irq, NULL); |
123 | if (err) { | |
57b33ff0 GG |
124 | dev_err(dev, |
125 | "Failed to register Embedded Controller subdevice %d\n", | |
126 | err); | |
6f1d912b | 127 | goto fail_mfd; |
4ab6174e SG |
128 | } |
129 | ||
57b33ff0 GG |
130 | if (ec_dev->max_passthru) { |
131 | /* | |
132 | * Register a PD device as well on top of this device. | |
133 | * We make the following assumptions: | |
134 | * - behind an EC, we have a pd | |
135 | * - only one device added. | |
136 | * - the EC is responsive at init time (it is not true for a | |
137 | * sensor hub. | |
138 | */ | |
139 | err = mfd_add_devices(ec_dev->dev, PLATFORM_DEVID_AUTO, | |
140 | &ec_pd_cell, 1, NULL, ec_dev->irq, NULL); | |
141 | if (err) { | |
142 | dev_err(dev, | |
143 | "Failed to register Power Delivery subdevice %d\n", | |
144 | err); | |
6f1d912b | 145 | goto fail_mfd; |
57b33ff0 GG |
146 | } |
147 | } | |
148 | ||
bb03ffb9 | 149 | if (IS_ENABLED(CONFIG_OF) && dev->of_node) { |
2eb131ef | 150 | err = devm_of_platform_populate(dev); |
bb03ffb9 TB |
151 | if (err) { |
152 | mfd_remove_devices(dev); | |
153 | dev_err(dev, "Failed to register sub-devices\n"); | |
6f1d912b | 154 | goto fail_mfd; |
bb03ffb9 TB |
155 | } |
156 | } | |
157 | ||
f00c06fd SN |
158 | /* |
159 | * Clear sleep event - this will fail harmlessly on platforms that | |
160 | * don't implement the sleep event host command. | |
161 | */ | |
162 | err = cros_ec_sleep_event(ec_dev, 0); | |
163 | if (err < 0) | |
164 | dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec", | |
165 | err); | |
166 | ||
533cec8f | 167 | dev_info(dev, "Chrome EC device registered\n"); |
4ab6174e | 168 | |
e04653a9 AP |
169 | cros_ec_acpi_install_gpe_handler(dev); |
170 | ||
4ab6174e | 171 | return 0; |
6f1d912b VY |
172 | |
173 | fail_mfd: | |
174 | if (ec_dev->irq) | |
175 | free_irq(ec_dev->irq, ec_dev); | |
176 | return err; | |
4ab6174e | 177 | } |
5ebeaff5 | 178 | EXPORT_SYMBOL(cros_ec_register); |
4ab6174e SG |
179 | |
180 | int cros_ec_remove(struct cros_ec_device *ec_dev) | |
181 | { | |
182 | mfd_remove_devices(ec_dev->dev); | |
4ab6174e | 183 | |
e04653a9 AP |
184 | cros_ec_acpi_remove_gpe_handler(); |
185 | ||
4ab6174e SG |
186 | return 0; |
187 | } | |
5ebeaff5 | 188 | EXPORT_SYMBOL(cros_ec_remove); |
4ab6174e SG |
189 | |
190 | #ifdef CONFIG_PM_SLEEP | |
191 | int cros_ec_suspend(struct cros_ec_device *ec_dev) | |
192 | { | |
193 | struct device *dev = ec_dev->dev; | |
f00c06fd SN |
194 | int ret; |
195 | u8 sleep_event; | |
196 | ||
e04653a9 AP |
197 | if (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) { |
198 | sleep_event = HOST_SLEEP_EVENT_S3_SUSPEND; | |
199 | } else { | |
200 | sleep_event = HOST_SLEEP_EVENT_S0IX_SUSPEND; | |
201 | ||
202 | /* Clearing the GPE status for any pending event */ | |
203 | cros_ec_acpi_clear_gpe(); | |
204 | } | |
f00c06fd SN |
205 | |
206 | ret = cros_ec_sleep_event(ec_dev, sleep_event); | |
207 | if (ret < 0) | |
208 | dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec", | |
209 | ret); | |
4ab6174e SG |
210 | |
211 | if (device_may_wakeup(dev)) | |
212 | ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq); | |
213 | ||
214 | disable_irq(ec_dev->irq); | |
215 | ec_dev->was_wake_device = ec_dev->wake_enabled; | |
a9eb186e | 216 | ec_dev->suspended = true; |
4ab6174e SG |
217 | |
218 | return 0; | |
219 | } | |
5ebeaff5 | 220 | EXPORT_SYMBOL(cros_ec_suspend); |
4ab6174e | 221 | |
6f1d912b VY |
222 | static void cros_ec_drain_events(struct cros_ec_device *ec_dev) |
223 | { | |
224 | while (cros_ec_get_next_event(ec_dev) > 0) | |
225 | blocking_notifier_call_chain(&ec_dev->event_notifier, | |
226 | 1, ec_dev); | |
227 | } | |
228 | ||
4ab6174e SG |
229 | int cros_ec_resume(struct cros_ec_device *ec_dev) |
230 | { | |
f00c06fd SN |
231 | int ret; |
232 | u8 sleep_event; | |
233 | ||
a9eb186e | 234 | ec_dev->suspended = false; |
4ab6174e SG |
235 | enable_irq(ec_dev->irq); |
236 | ||
9c576bd3 SN |
237 | sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ? |
238 | HOST_SLEEP_EVENT_S3_RESUME : | |
239 | HOST_SLEEP_EVENT_S0IX_RESUME; | |
f00c06fd SN |
240 | |
241 | ret = cros_ec_sleep_event(ec_dev, sleep_event); | |
242 | if (ret < 0) | |
243 | dev_dbg(ec_dev->dev, "Error %d sending resume event to ec", | |
244 | ret); | |
245 | ||
6f1d912b VY |
246 | /* |
247 | * In some cases, we need to distinguish between events that occur | |
248 | * during suspend if the EC is not a wake source. For example, | |
249 | * keypresses during suspend should be discarded if it does not wake | |
250 | * the system. | |
251 | * | |
252 | * If the EC is not a wake source, drain the event queue and mark them | |
253 | * as "queued during suspend". | |
254 | */ | |
4ab6174e SG |
255 | if (ec_dev->wake_enabled) { |
256 | disable_irq_wake(ec_dev->irq); | |
257 | ec_dev->wake_enabled = 0; | |
6f1d912b VY |
258 | } else { |
259 | cros_ec_drain_events(ec_dev); | |
4ab6174e SG |
260 | } |
261 | ||
262 | return 0; | |
263 | } | |
5ebeaff5 SO |
264 | EXPORT_SYMBOL(cros_ec_resume); |
265 | ||
4ab6174e | 266 | #endif |
a865a589 BR |
267 | |
268 | MODULE_LICENSE("GPL"); | |
269 | MODULE_DESCRIPTION("ChromeOS EC core driver"); |