diff options
author | Enric Balletbo i Serra <enric.balletbo@collabora.com> | 2018-07-02 06:21:59 -0400 |
---|---|---|
committer | Benson Leung <bleung@chromium.org> | 2018-07-03 15:40:06 -0400 |
commit | d00a8741fd8fab2dc82f1c44d4111a337d505e60 (patch) | |
tree | 6c36a99da5a0b9c8ef0f44f9bc5e5a661a311b7b /drivers/mfd | |
parent | 021c91791a5e7e85c567452f1be3e4c2c6cb6063 (diff) |
platform/chrome: Move cros-ec transport drivers to drivers/platform.
There are some cros-ec transport drivers (I2C, SPI) living in MFD, while
others (LPC) living in drivers/platform. The transport drivers are more
platform specific. So, move the I2C and SPI transport drivers to the
platform/chrome directory. The patch also removes the MFD_ prefix of
their Kconfig symbols.
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
Reviewed-by: Guenter Roeck <groeck@chromium.org>
Acked-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Benson Leung <bleung@chromium.org>
Diffstat (limited to 'drivers/mfd')
-rw-r--r-- | drivers/mfd/Kconfig | 20 | ||||
-rw-r--r-- | drivers/mfd/Makefile | 2 | ||||
-rw-r--r-- | drivers/mfd/cros_ec_i2c.c | 386 | ||||
-rw-r--r-- | drivers/mfd/cros_ec_spi.c | 743 |
4 files changed, 0 insertions, 1151 deletions
diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index b860eb5aa194..013458e728dc 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig | |||
@@ -202,26 +202,6 @@ config MFD_CROS_EC | |||
202 | You also need to enable the driver for the bus you are using. The | 202 | You also need to enable the driver for the bus you are using. The |
203 | protocol for talking to the EC is defined by the bus driver. | 203 | protocol for talking to the EC is defined by the bus driver. |
204 | 204 | ||
205 | config MFD_CROS_EC_I2C | ||
206 | tristate "ChromeOS Embedded Controller (I2C)" | ||
207 | depends on MFD_CROS_EC && I2C | ||
208 | |||
209 | help | ||
210 | If you say Y here, you get support for talking to the ChromeOS | ||
211 | EC through an I2C bus. This uses a simple byte-level protocol with | ||
212 | a checksum. Failing accesses will be retried three times to | ||
213 | improve reliability. | ||
214 | |||
215 | config MFD_CROS_EC_SPI | ||
216 | tristate "ChromeOS Embedded Controller (SPI)" | ||
217 | depends on MFD_CROS_EC && SPI | ||
218 | |||
219 | ---help--- | ||
220 | If you say Y here, you get support for talking to the ChromeOS EC | ||
221 | through a SPI bus, using a byte-level protocol. Since the EC's | ||
222 | response time cannot be guaranteed, we support ignoring | ||
223 | 'pre-amble' bytes before the response actually starts. | ||
224 | |||
225 | config MFD_CROS_EC_CHARDEV | 205 | config MFD_CROS_EC_CHARDEV |
226 | tristate "Chrome OS Embedded Controller userspace device interface" | 206 | tristate "Chrome OS Embedded Controller userspace device interface" |
227 | depends on MFD_CROS_EC | 207 | depends on MFD_CROS_EC |
diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index e9fd20dba18d..d13e859d7c1e 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile | |||
@@ -14,8 +14,6 @@ obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o | |||
14 | obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.o | 14 | obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.o |
15 | cros_ec_core-objs := cros_ec.o | 15 | cros_ec_core-objs := cros_ec.o |
16 | obj-$(CONFIG_MFD_CROS_EC) += cros_ec_core.o | 16 | obj-$(CONFIG_MFD_CROS_EC) += cros_ec_core.o |
17 | obj-$(CONFIG_MFD_CROS_EC_I2C) += cros_ec_i2c.o | ||
18 | obj-$(CONFIG_MFD_CROS_EC_SPI) += cros_ec_spi.o | ||
19 | obj-$(CONFIG_MFD_CROS_EC_CHARDEV) += cros_ec_dev.o | 17 | obj-$(CONFIG_MFD_CROS_EC_CHARDEV) += cros_ec_dev.o |
20 | obj-$(CONFIG_MFD_EXYNOS_LPASS) += exynos-lpass.o | 18 | obj-$(CONFIG_MFD_EXYNOS_LPASS) += exynos-lpass.o |
21 | 19 | ||
diff --git a/drivers/mfd/cros_ec_i2c.c b/drivers/mfd/cros_ec_i2c.c deleted file mode 100644 index ef9b4763356f..000000000000 --- a/drivers/mfd/cros_ec_i2c.c +++ /dev/null | |||
@@ -1,386 +0,0 @@ | |||
1 | /* | ||
2 | * ChromeOS EC multi-function device (I2C) | ||
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 | |||
16 | #include <linux/acpi.h> | ||
17 | #include <linux/delay.h> | ||
18 | #include <linux/kernel.h> | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/i2c.h> | ||
21 | #include <linux/interrupt.h> | ||
22 | #include <linux/mfd/cros_ec.h> | ||
23 | #include <linux/mfd/cros_ec_commands.h> | ||
24 | #include <linux/platform_device.h> | ||
25 | #include <linux/slab.h> | ||
26 | |||
27 | /** | ||
28 | * Request format for protocol v3 | ||
29 | * byte 0 0xda (EC_COMMAND_PROTOCOL_3) | ||
30 | * byte 1-8 struct ec_host_request | ||
31 | * byte 10- response data | ||
32 | */ | ||
33 | struct ec_host_request_i2c { | ||
34 | /* Always 0xda to backward compatible with v2 struct */ | ||
35 | uint8_t command_protocol; | ||
36 | struct ec_host_request ec_request; | ||
37 | } __packed; | ||
38 | |||
39 | |||
40 | /* | ||
41 | * Response format for protocol v3 | ||
42 | * byte 0 result code | ||
43 | * byte 1 packet_length | ||
44 | * byte 2-9 struct ec_host_response | ||
45 | * byte 10- response data | ||
46 | */ | ||
47 | struct ec_host_response_i2c { | ||
48 | uint8_t result; | ||
49 | uint8_t packet_length; | ||
50 | struct ec_host_response ec_response; | ||
51 | } __packed; | ||
52 | |||
53 | static inline struct cros_ec_device *to_ec_dev(struct device *dev) | ||
54 | { | ||
55 | struct i2c_client *client = to_i2c_client(dev); | ||
56 | |||
57 | return i2c_get_clientdata(client); | ||
58 | } | ||
59 | |||
60 | static int cros_ec_pkt_xfer_i2c(struct cros_ec_device *ec_dev, | ||
61 | struct cros_ec_command *msg) | ||
62 | { | ||
63 | struct i2c_client *client = ec_dev->priv; | ||
64 | int ret = -ENOMEM; | ||
65 | int i; | ||
66 | int packet_len; | ||
67 | u8 *out_buf = NULL; | ||
68 | u8 *in_buf = NULL; | ||
69 | u8 sum; | ||
70 | struct i2c_msg i2c_msg[2]; | ||
71 | struct ec_host_response *ec_response; | ||
72 | struct ec_host_request_i2c *ec_request_i2c; | ||
73 | struct ec_host_response_i2c *ec_response_i2c; | ||
74 | int request_header_size = sizeof(struct ec_host_request_i2c); | ||
75 | int response_header_size = sizeof(struct ec_host_response_i2c); | ||
76 | |||
77 | i2c_msg[0].addr = client->addr; | ||
78 | i2c_msg[0].flags = 0; | ||
79 | i2c_msg[1].addr = client->addr; | ||
80 | i2c_msg[1].flags = I2C_M_RD; | ||
81 | |||
82 | packet_len = msg->insize + response_header_size; | ||
83 | BUG_ON(packet_len > ec_dev->din_size); | ||
84 | in_buf = ec_dev->din; | ||
85 | i2c_msg[1].len = packet_len; | ||
86 | i2c_msg[1].buf = (char *) in_buf; | ||
87 | |||
88 | packet_len = msg->outsize + request_header_size; | ||
89 | BUG_ON(packet_len > ec_dev->dout_size); | ||
90 | out_buf = ec_dev->dout; | ||
91 | i2c_msg[0].len = packet_len; | ||
92 | i2c_msg[0].buf = (char *) out_buf; | ||
93 | |||
94 | /* create request data */ | ||
95 | ec_request_i2c = (struct ec_host_request_i2c *) out_buf; | ||
96 | ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3; | ||
97 | |||
98 | ec_dev->dout++; | ||
99 | ret = cros_ec_prepare_tx(ec_dev, msg); | ||
100 | ec_dev->dout--; | ||
101 | |||
102 | /* send command to EC and read answer */ | ||
103 | ret = i2c_transfer(client->adapter, i2c_msg, 2); | ||
104 | if (ret < 0) { | ||
105 | dev_dbg(ec_dev->dev, "i2c transfer failed: %d\n", ret); | ||
106 | goto done; | ||
107 | } else if (ret != 2) { | ||
108 | dev_err(ec_dev->dev, "failed to get response: %d\n", ret); | ||
109 | ret = -EIO; | ||
110 | goto done; | ||
111 | } | ||
112 | |||
113 | ec_response_i2c = (struct ec_host_response_i2c *) in_buf; | ||
114 | msg->result = ec_response_i2c->result; | ||
115 | ec_response = &ec_response_i2c->ec_response; | ||
116 | |||
117 | switch (msg->result) { | ||
118 | case EC_RES_SUCCESS: | ||
119 | break; | ||
120 | case EC_RES_IN_PROGRESS: | ||
121 | ret = -EAGAIN; | ||
122 | dev_dbg(ec_dev->dev, "command 0x%02x in progress\n", | ||
123 | msg->command); | ||
124 | goto done; | ||
125 | |||
126 | default: | ||
127 | dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n", | ||
128 | msg->command, msg->result); | ||
129 | /* | ||
130 | * When we send v3 request to v2 ec, ec won't recognize the | ||
131 | * 0xda (EC_COMMAND_PROTOCOL_3) and will return with status | ||
132 | * EC_RES_INVALID_COMMAND with zero data length. | ||
133 | * | ||
134 | * In case of invalid command for v3 protocol the data length | ||
135 | * will be at least sizeof(struct ec_host_response) | ||
136 | */ | ||
137 | if (ec_response_i2c->result == EC_RES_INVALID_COMMAND && | ||
138 | ec_response_i2c->packet_length == 0) { | ||
139 | ret = -EPROTONOSUPPORT; | ||
140 | goto done; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) { | ||
145 | dev_err(ec_dev->dev, | ||
146 | "response of %u bytes too short; not a full header\n", | ||
147 | ec_response_i2c->packet_length); | ||
148 | ret = -EBADMSG; | ||
149 | goto done; | ||
150 | } | ||
151 | |||
152 | if (msg->insize < ec_response->data_len) { | ||
153 | dev_err(ec_dev->dev, | ||
154 | "response data size is too large: expected %u, got %u\n", | ||
155 | msg->insize, | ||
156 | ec_response->data_len); | ||
157 | ret = -EMSGSIZE; | ||
158 | goto done; | ||
159 | } | ||
160 | |||
161 | /* copy response packet payload and compute checksum */ | ||
162 | sum = 0; | ||
163 | for (i = 0; i < sizeof(struct ec_host_response); i++) | ||
164 | sum += ((u8 *)ec_response)[i]; | ||
165 | |||
166 | memcpy(msg->data, | ||
167 | in_buf + response_header_size, | ||
168 | ec_response->data_len); | ||
169 | for (i = 0; i < ec_response->data_len; i++) | ||
170 | sum += msg->data[i]; | ||
171 | |||
172 | /* All bytes should sum to zero */ | ||
173 | if (sum) { | ||
174 | dev_err(ec_dev->dev, "bad packet checksum\n"); | ||
175 | ret = -EBADMSG; | ||
176 | goto done; | ||
177 | } | ||
178 | |||
179 | ret = ec_response->data_len; | ||
180 | |||
181 | done: | ||
182 | if (msg->command == EC_CMD_REBOOT_EC) | ||
183 | msleep(EC_REBOOT_DELAY_MS); | ||
184 | |||
185 | return ret; | ||
186 | } | ||
187 | |||
188 | static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev, | ||
189 | struct cros_ec_command *msg) | ||
190 | { | ||
191 | struct i2c_client *client = ec_dev->priv; | ||
192 | int ret = -ENOMEM; | ||
193 | int i; | ||
194 | int len; | ||
195 | int packet_len; | ||
196 | u8 *out_buf = NULL; | ||
197 | u8 *in_buf = NULL; | ||
198 | u8 sum; | ||
199 | struct i2c_msg i2c_msg[2]; | ||
200 | |||
201 | i2c_msg[0].addr = client->addr; | ||
202 | i2c_msg[0].flags = 0; | ||
203 | i2c_msg[1].addr = client->addr; | ||
204 | i2c_msg[1].flags = I2C_M_RD; | ||
205 | |||
206 | /* | ||
207 | * allocate larger packet (one byte for checksum, one byte for | ||
208 | * length, and one for result code) | ||
209 | */ | ||
210 | packet_len = msg->insize + 3; | ||
211 | in_buf = kzalloc(packet_len, GFP_KERNEL); | ||
212 | if (!in_buf) | ||
213 | goto done; | ||
214 | i2c_msg[1].len = packet_len; | ||
215 | i2c_msg[1].buf = (char *)in_buf; | ||
216 | |||
217 | /* | ||
218 | * allocate larger packet (one byte for checksum, one for | ||
219 | * command code, one for length, and one for command version) | ||
220 | */ | ||
221 | packet_len = msg->outsize + 4; | ||
222 | out_buf = kzalloc(packet_len, GFP_KERNEL); | ||
223 | if (!out_buf) | ||
224 | goto done; | ||
225 | i2c_msg[0].len = packet_len; | ||
226 | i2c_msg[0].buf = (char *)out_buf; | ||
227 | |||
228 | out_buf[0] = EC_CMD_VERSION0 + msg->version; | ||
229 | out_buf[1] = msg->command; | ||
230 | out_buf[2] = msg->outsize; | ||
231 | |||
232 | /* copy message payload and compute checksum */ | ||
233 | sum = out_buf[0] + out_buf[1] + out_buf[2]; | ||
234 | for (i = 0; i < msg->outsize; i++) { | ||
235 | out_buf[3 + i] = msg->data[i]; | ||
236 | sum += out_buf[3 + i]; | ||
237 | } | ||
238 | out_buf[3 + msg->outsize] = sum; | ||
239 | |||
240 | /* send command to EC and read answer */ | ||
241 | ret = i2c_transfer(client->adapter, i2c_msg, 2); | ||
242 | if (ret < 0) { | ||
243 | dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret); | ||
244 | goto done; | ||
245 | } else if (ret != 2) { | ||
246 | dev_err(ec_dev->dev, "failed to get response: %d\n", ret); | ||
247 | ret = -EIO; | ||
248 | goto done; | ||
249 | } | ||
250 | |||
251 | /* check response error code */ | ||
252 | msg->result = i2c_msg[1].buf[0]; | ||
253 | ret = cros_ec_check_result(ec_dev, msg); | ||
254 | if (ret) | ||
255 | goto done; | ||
256 | |||
257 | len = in_buf[1]; | ||
258 | if (len > msg->insize) { | ||
259 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", | ||
260 | len, msg->insize); | ||
261 | ret = -ENOSPC; | ||
262 | goto done; | ||
263 | } | ||
264 | |||
265 | /* copy response packet payload and compute checksum */ | ||
266 | sum = in_buf[0] + in_buf[1]; | ||
267 | for (i = 0; i < len; i++) { | ||
268 | msg->data[i] = in_buf[2 + i]; | ||
269 | sum += in_buf[2 + i]; | ||
270 | } | ||
271 | dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n", | ||
272 | i2c_msg[1].len, in_buf, sum); | ||
273 | if (sum != in_buf[2 + len]) { | ||
274 | dev_err(ec_dev->dev, "bad packet checksum\n"); | ||
275 | ret = -EBADMSG; | ||
276 | goto done; | ||
277 | } | ||
278 | |||
279 | ret = len; | ||
280 | done: | ||
281 | kfree(in_buf); | ||
282 | kfree(out_buf); | ||
283 | if (msg->command == EC_CMD_REBOOT_EC) | ||
284 | msleep(EC_REBOOT_DELAY_MS); | ||
285 | |||
286 | return ret; | ||
287 | } | ||
288 | |||
289 | static int cros_ec_i2c_probe(struct i2c_client *client, | ||
290 | const struct i2c_device_id *dev_id) | ||
291 | { | ||
292 | struct device *dev = &client->dev; | ||
293 | struct cros_ec_device *ec_dev = NULL; | ||
294 | int err; | ||
295 | |||
296 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); | ||
297 | if (!ec_dev) | ||
298 | return -ENOMEM; | ||
299 | |||
300 | i2c_set_clientdata(client, ec_dev); | ||
301 | ec_dev->dev = dev; | ||
302 | ec_dev->priv = client; | ||
303 | ec_dev->irq = client->irq; | ||
304 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c; | ||
305 | ec_dev->pkt_xfer = cros_ec_pkt_xfer_i2c; | ||
306 | ec_dev->phys_name = client->adapter->name; | ||
307 | ec_dev->din_size = sizeof(struct ec_host_response_i2c) + | ||
308 | sizeof(struct ec_response_get_protocol_info); | ||
309 | ec_dev->dout_size = sizeof(struct ec_host_request_i2c); | ||
310 | |||
311 | err = cros_ec_register(ec_dev); | ||
312 | if (err) { | ||
313 | dev_err(dev, "cannot register EC\n"); | ||
314 | return err; | ||
315 | } | ||
316 | |||
317 | return 0; | ||
318 | } | ||
319 | |||
320 | static int cros_ec_i2c_remove(struct i2c_client *client) | ||
321 | { | ||
322 | struct cros_ec_device *ec_dev = i2c_get_clientdata(client); | ||
323 | |||
324 | cros_ec_remove(ec_dev); | ||
325 | |||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | #ifdef CONFIG_PM_SLEEP | ||
330 | static int cros_ec_i2c_suspend(struct device *dev) | ||
331 | { | ||
332 | struct cros_ec_device *ec_dev = to_ec_dev(dev); | ||
333 | |||
334 | return cros_ec_suspend(ec_dev); | ||
335 | } | ||
336 | |||
337 | static int cros_ec_i2c_resume(struct device *dev) | ||
338 | { | ||
339 | struct cros_ec_device *ec_dev = to_ec_dev(dev); | ||
340 | |||
341 | return cros_ec_resume(ec_dev); | ||
342 | } | ||
343 | #endif | ||
344 | |||
345 | static const struct dev_pm_ops cros_ec_i2c_pm_ops = { | ||
346 | SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_i2c_suspend, cros_ec_i2c_resume) | ||
347 | }; | ||
348 | |||
349 | #ifdef CONFIG_OF | ||
350 | static const struct of_device_id cros_ec_i2c_of_match[] = { | ||
351 | { .compatible = "google,cros-ec-i2c", }, | ||
352 | { /* sentinel */ }, | ||
353 | }; | ||
354 | MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match); | ||
355 | #endif | ||
356 | |||
357 | static const struct i2c_device_id cros_ec_i2c_id[] = { | ||
358 | { "cros-ec-i2c", 0 }, | ||
359 | { } | ||
360 | }; | ||
361 | MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id); | ||
362 | |||
363 | #ifdef CONFIG_ACPI | ||
364 | static const struct acpi_device_id cros_ec_i2c_acpi_id[] = { | ||
365 | { "GOOG0008", 0 }, | ||
366 | { /* sentinel */ } | ||
367 | }; | ||
368 | MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_acpi_id); | ||
369 | #endif | ||
370 | |||
371 | static struct i2c_driver cros_ec_driver = { | ||
372 | .driver = { | ||
373 | .name = "cros-ec-i2c", | ||
374 | .acpi_match_table = ACPI_PTR(cros_ec_i2c_acpi_id), | ||
375 | .of_match_table = of_match_ptr(cros_ec_i2c_of_match), | ||
376 | .pm = &cros_ec_i2c_pm_ops, | ||
377 | }, | ||
378 | .probe = cros_ec_i2c_probe, | ||
379 | .remove = cros_ec_i2c_remove, | ||
380 | .id_table = cros_ec_i2c_id, | ||
381 | }; | ||
382 | |||
383 | module_i2c_driver(cros_ec_driver); | ||
384 | |||
385 | MODULE_LICENSE("GPL"); | ||
386 | MODULE_DESCRIPTION("ChromeOS EC multi function device"); | ||
diff --git a/drivers/mfd/cros_ec_spi.c b/drivers/mfd/cros_ec_spi.c deleted file mode 100644 index 2060d1483043..000000000000 --- a/drivers/mfd/cros_ec_spi.c +++ /dev/null | |||
@@ -1,743 +0,0 @@ | |||
1 | /* | ||
2 | * ChromeOS EC multi-function device (SPI) | ||
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 | |||
16 | #include <linux/delay.h> | ||
17 | #include <linux/kernel.h> | ||
18 | #include <linux/module.h> | ||
19 | #include <linux/mfd/cros_ec.h> | ||
20 | #include <linux/mfd/cros_ec_commands.h> | ||
21 | #include <linux/of.h> | ||
22 | #include <linux/platform_device.h> | ||
23 | #include <linux/slab.h> | ||
24 | #include <linux/spi/spi.h> | ||
25 | |||
26 | |||
27 | /* The header byte, which follows the preamble */ | ||
28 | #define EC_MSG_HEADER 0xec | ||
29 | |||
30 | /* | ||
31 | * Number of EC preamble bytes we read at a time. Since it takes | ||
32 | * about 400-500us for the EC to respond there is not a lot of | ||
33 | * point in tuning this. If the EC could respond faster then | ||
34 | * we could increase this so that might expect the preamble and | ||
35 | * message to occur in a single transaction. However, the maximum | ||
36 | * SPI transfer size is 256 bytes, so at 5MHz we need a response | ||
37 | * time of perhaps <320us (200 bytes / 1600 bits). | ||
38 | */ | ||
39 | #define EC_MSG_PREAMBLE_COUNT 32 | ||
40 | |||
41 | /* | ||
42 | * Allow for a long time for the EC to respond. We support i2c | ||
43 | * tunneling and support fairly long messages for the tunnel (249 | ||
44 | * bytes long at the moment). If we're talking to a 100 kHz device | ||
45 | * on the other end and need to transfer ~256 bytes, then we need: | ||
46 | * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms | ||
47 | * | ||
48 | * We'll wait 8 times that to handle clock stretching and other | ||
49 | * paranoia. Note that some battery gas gauge ICs claim to have a | ||
50 | * clock stretch of 144ms in rare situations. That's incentive for | ||
51 | * not directly passing i2c through, but it's too late for that for | ||
52 | * existing hardware. | ||
53 | * | ||
54 | * It's pretty unlikely that we'll really see a 249 byte tunnel in | ||
55 | * anything other than testing. If this was more common we might | ||
56 | * consider having slow commands like this require a GET_STATUS | ||
57 | * wait loop. The 'flash write' command would be another candidate | ||
58 | * for this, clocking in at 2-3ms. | ||
59 | */ | ||
60 | #define EC_MSG_DEADLINE_MS 200 | ||
61 | |||
62 | /* | ||
63 | * Time between raising the SPI chip select (for the end of a | ||
64 | * transaction) and dropping it again (for the next transaction). | ||
65 | * If we go too fast, the EC will miss the transaction. We know that we | ||
66 | * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be | ||
67 | * safe. | ||
68 | */ | ||
69 | #define EC_SPI_RECOVERY_TIME_NS (200 * 1000) | ||
70 | |||
71 | /** | ||
72 | * struct cros_ec_spi - information about a SPI-connected EC | ||
73 | * | ||
74 | * @spi: SPI device we are connected to | ||
75 | * @last_transfer_ns: time that we last finished a transfer. | ||
76 | * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that | ||
77 | * is sent when we want to turn on CS at the start of a transaction. | ||
78 | * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that | ||
79 | * is sent when we want to turn off CS at the end of a transaction. | ||
80 | */ | ||
81 | struct cros_ec_spi { | ||
82 | struct spi_device *spi; | ||
83 | s64 last_transfer_ns; | ||
84 | unsigned int start_of_msg_delay; | ||
85 | unsigned int end_of_msg_delay; | ||
86 | }; | ||
87 | |||
88 | static void debug_packet(struct device *dev, const char *name, u8 *ptr, | ||
89 | int len) | ||
90 | { | ||
91 | #ifdef DEBUG | ||
92 | int i; | ||
93 | |||
94 | dev_dbg(dev, "%s: ", name); | ||
95 | for (i = 0; i < len; i++) | ||
96 | pr_cont(" %02x", ptr[i]); | ||
97 | |||
98 | pr_cont("\n"); | ||
99 | #endif | ||
100 | } | ||
101 | |||
102 | static int terminate_request(struct cros_ec_device *ec_dev) | ||
103 | { | ||
104 | struct cros_ec_spi *ec_spi = ec_dev->priv; | ||
105 | struct spi_message msg; | ||
106 | struct spi_transfer trans; | ||
107 | int ret; | ||
108 | |||
109 | /* | ||
110 | * Turn off CS, possibly adding a delay to ensure the rising edge | ||
111 | * doesn't come too soon after the end of the data. | ||
112 | */ | ||
113 | spi_message_init(&msg); | ||
114 | memset(&trans, 0, sizeof(trans)); | ||
115 | trans.delay_usecs = ec_spi->end_of_msg_delay; | ||
116 | spi_message_add_tail(&trans, &msg); | ||
117 | |||
118 | ret = spi_sync_locked(ec_spi->spi, &msg); | ||
119 | |||
120 | /* Reset end-of-response timer */ | ||
121 | ec_spi->last_transfer_ns = ktime_get_ns(); | ||
122 | if (ret < 0) { | ||
123 | dev_err(ec_dev->dev, | ||
124 | "cs-deassert spi transfer failed: %d\n", | ||
125 | ret); | ||
126 | } | ||
127 | |||
128 | return ret; | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * receive_n_bytes - receive n bytes from the EC. | ||
133 | * | ||
134 | * Assumes buf is a pointer into the ec_dev->din buffer | ||
135 | */ | ||
136 | static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) | ||
137 | { | ||
138 | struct cros_ec_spi *ec_spi = ec_dev->priv; | ||
139 | struct spi_transfer trans; | ||
140 | struct spi_message msg; | ||
141 | int ret; | ||
142 | |||
143 | BUG_ON(buf - ec_dev->din + n > ec_dev->din_size); | ||
144 | |||
145 | memset(&trans, 0, sizeof(trans)); | ||
146 | trans.cs_change = 1; | ||
147 | trans.rx_buf = buf; | ||
148 | trans.len = n; | ||
149 | |||
150 | spi_message_init(&msg); | ||
151 | spi_message_add_tail(&trans, &msg); | ||
152 | ret = spi_sync_locked(ec_spi->spi, &msg); | ||
153 | if (ret < 0) | ||
154 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | ||
155 | |||
156 | return ret; | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * cros_ec_spi_receive_packet - Receive a packet from the EC. | ||
161 | * | ||
162 | * This function has two phases: reading the preamble bytes (since if we read | ||
163 | * data from the EC before it is ready to send, we just get preamble) and | ||
164 | * reading the actual message. | ||
165 | * | ||
166 | * The received data is placed into ec_dev->din. | ||
167 | * | ||
168 | * @ec_dev: ChromeOS EC device | ||
169 | * @need_len: Number of message bytes we need to read | ||
170 | */ | ||
171 | static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev, | ||
172 | int need_len) | ||
173 | { | ||
174 | struct ec_host_response *response; | ||
175 | u8 *ptr, *end; | ||
176 | int ret; | ||
177 | unsigned long deadline; | ||
178 | int todo; | ||
179 | |||
180 | BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); | ||
181 | |||
182 | /* Receive data until we see the header byte */ | ||
183 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); | ||
184 | while (true) { | ||
185 | unsigned long start_jiffies = jiffies; | ||
186 | |||
187 | ret = receive_n_bytes(ec_dev, | ||
188 | ec_dev->din, | ||
189 | EC_MSG_PREAMBLE_COUNT); | ||
190 | if (ret < 0) | ||
191 | return ret; | ||
192 | |||
193 | ptr = ec_dev->din; | ||
194 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { | ||
195 | if (*ptr == EC_SPI_FRAME_START) { | ||
196 | dev_dbg(ec_dev->dev, "msg found at %zd\n", | ||
197 | ptr - ec_dev->din); | ||
198 | break; | ||
199 | } | ||
200 | } | ||
201 | if (ptr != end) | ||
202 | break; | ||
203 | |||
204 | /* | ||
205 | * Use the time at the start of the loop as a timeout. This | ||
206 | * gives us one last shot at getting the transfer and is useful | ||
207 | * in case we got context switched out for a while. | ||
208 | */ | ||
209 | if (time_after(start_jiffies, deadline)) { | ||
210 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); | ||
211 | return -ETIMEDOUT; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | * ptr now points to the header byte. Copy any valid data to the | ||
217 | * start of our buffer | ||
218 | */ | ||
219 | todo = end - ++ptr; | ||
220 | BUG_ON(todo < 0 || todo > ec_dev->din_size); | ||
221 | todo = min(todo, need_len); | ||
222 | memmove(ec_dev->din, ptr, todo); | ||
223 | ptr = ec_dev->din + todo; | ||
224 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", | ||
225 | need_len, todo); | ||
226 | need_len -= todo; | ||
227 | |||
228 | /* If the entire response struct wasn't read, get the rest of it. */ | ||
229 | if (todo < sizeof(*response)) { | ||
230 | ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo); | ||
231 | if (ret < 0) | ||
232 | return -EBADMSG; | ||
233 | ptr += (sizeof(*response) - todo); | ||
234 | todo = sizeof(*response); | ||
235 | } | ||
236 | |||
237 | response = (struct ec_host_response *)ec_dev->din; | ||
238 | |||
239 | /* Abort if data_len is too large. */ | ||
240 | if (response->data_len > ec_dev->din_size) | ||
241 | return -EMSGSIZE; | ||
242 | |||
243 | /* Receive data until we have it all */ | ||
244 | while (need_len > 0) { | ||
245 | /* | ||
246 | * We can't support transfers larger than the SPI FIFO size | ||
247 | * unless we have DMA. We don't have DMA on the ISP SPI ports | ||
248 | * for Exynos. We need a way of asking SPI driver for | ||
249 | * maximum-supported transfer size. | ||
250 | */ | ||
251 | todo = min(need_len, 256); | ||
252 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", | ||
253 | todo, need_len, ptr - ec_dev->din); | ||
254 | |||
255 | ret = receive_n_bytes(ec_dev, ptr, todo); | ||
256 | if (ret < 0) | ||
257 | return ret; | ||
258 | |||
259 | ptr += todo; | ||
260 | need_len -= todo; | ||
261 | } | ||
262 | |||
263 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); | ||
264 | |||
265 | return 0; | ||
266 | } | ||
267 | |||
268 | /** | ||
269 | * cros_ec_spi_receive_response - Receive a response from the EC. | ||
270 | * | ||
271 | * This function has two phases: reading the preamble bytes (since if we read | ||
272 | * data from the EC before it is ready to send, we just get preamble) and | ||
273 | * reading the actual message. | ||
274 | * | ||
275 | * The received data is placed into ec_dev->din. | ||
276 | * | ||
277 | * @ec_dev: ChromeOS EC device | ||
278 | * @need_len: Number of message bytes we need to read | ||
279 | */ | ||
280 | static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev, | ||
281 | int need_len) | ||
282 | { | ||
283 | u8 *ptr, *end; | ||
284 | int ret; | ||
285 | unsigned long deadline; | ||
286 | int todo; | ||
287 | |||
288 | BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); | ||
289 | |||
290 | /* Receive data until we see the header byte */ | ||
291 | deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); | ||
292 | while (true) { | ||
293 | unsigned long start_jiffies = jiffies; | ||
294 | |||
295 | ret = receive_n_bytes(ec_dev, | ||
296 | ec_dev->din, | ||
297 | EC_MSG_PREAMBLE_COUNT); | ||
298 | if (ret < 0) | ||
299 | return ret; | ||
300 | |||
301 | ptr = ec_dev->din; | ||
302 | for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { | ||
303 | if (*ptr == EC_SPI_FRAME_START) { | ||
304 | dev_dbg(ec_dev->dev, "msg found at %zd\n", | ||
305 | ptr - ec_dev->din); | ||
306 | break; | ||
307 | } | ||
308 | } | ||
309 | if (ptr != end) | ||
310 | break; | ||
311 | |||
312 | /* | ||
313 | * Use the time at the start of the loop as a timeout. This | ||
314 | * gives us one last shot at getting the transfer and is useful | ||
315 | * in case we got context switched out for a while. | ||
316 | */ | ||
317 | if (time_after(start_jiffies, deadline)) { | ||
318 | dev_warn(ec_dev->dev, "EC failed to respond in time\n"); | ||
319 | return -ETIMEDOUT; | ||
320 | } | ||
321 | } | ||
322 | |||
323 | /* | ||
324 | * ptr now points to the header byte. Copy any valid data to the | ||
325 | * start of our buffer | ||
326 | */ | ||
327 | todo = end - ++ptr; | ||
328 | BUG_ON(todo < 0 || todo > ec_dev->din_size); | ||
329 | todo = min(todo, need_len); | ||
330 | memmove(ec_dev->din, ptr, todo); | ||
331 | ptr = ec_dev->din + todo; | ||
332 | dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", | ||
333 | need_len, todo); | ||
334 | need_len -= todo; | ||
335 | |||
336 | /* Receive data until we have it all */ | ||
337 | while (need_len > 0) { | ||
338 | /* | ||
339 | * We can't support transfers larger than the SPI FIFO size | ||
340 | * unless we have DMA. We don't have DMA on the ISP SPI ports | ||
341 | * for Exynos. We need a way of asking SPI driver for | ||
342 | * maximum-supported transfer size. | ||
343 | */ | ||
344 | todo = min(need_len, 256); | ||
345 | dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", | ||
346 | todo, need_len, ptr - ec_dev->din); | ||
347 | |||
348 | ret = receive_n_bytes(ec_dev, ptr, todo); | ||
349 | if (ret < 0) | ||
350 | return ret; | ||
351 | |||
352 | debug_packet(ec_dev->dev, "interim", ptr, todo); | ||
353 | ptr += todo; | ||
354 | need_len -= todo; | ||
355 | } | ||
356 | |||
357 | dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); | ||
358 | |||
359 | return 0; | ||
360 | } | ||
361 | |||
362 | /** | ||
363 | * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply | ||
364 | * | ||
365 | * @ec_dev: ChromeOS EC device | ||
366 | * @ec_msg: Message to transfer | ||
367 | */ | ||
368 | static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, | ||
369 | struct cros_ec_command *ec_msg) | ||
370 | { | ||
371 | struct ec_host_response *response; | ||
372 | struct cros_ec_spi *ec_spi = ec_dev->priv; | ||
373 | struct spi_transfer trans, trans_delay; | ||
374 | struct spi_message msg; | ||
375 | int i, len; | ||
376 | u8 *ptr; | ||
377 | u8 *rx_buf; | ||
378 | u8 sum; | ||
379 | u8 rx_byte; | ||
380 | int ret = 0, final_ret; | ||
381 | unsigned long delay; | ||
382 | |||
383 | len = cros_ec_prepare_tx(ec_dev, ec_msg); | ||
384 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); | ||
385 | |||
386 | /* If it's too soon to do another transaction, wait */ | ||
387 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; | ||
388 | if (delay < EC_SPI_RECOVERY_TIME_NS) | ||
389 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); | ||
390 | |||
391 | rx_buf = kzalloc(len, GFP_KERNEL); | ||
392 | if (!rx_buf) | ||
393 | return -ENOMEM; | ||
394 | |||
395 | spi_bus_lock(ec_spi->spi->master); | ||
396 | |||
397 | /* | ||
398 | * Leave a gap between CS assertion and clocking of data to allow the | ||
399 | * EC time to wakeup. | ||
400 | */ | ||
401 | spi_message_init(&msg); | ||
402 | if (ec_spi->start_of_msg_delay) { | ||
403 | memset(&trans_delay, 0, sizeof(trans_delay)); | ||
404 | trans_delay.delay_usecs = ec_spi->start_of_msg_delay; | ||
405 | spi_message_add_tail(&trans_delay, &msg); | ||
406 | } | ||
407 | |||
408 | /* Transmit phase - send our message */ | ||
409 | memset(&trans, 0, sizeof(trans)); | ||
410 | trans.tx_buf = ec_dev->dout; | ||
411 | trans.rx_buf = rx_buf; | ||
412 | trans.len = len; | ||
413 | trans.cs_change = 1; | ||
414 | spi_message_add_tail(&trans, &msg); | ||
415 | ret = spi_sync_locked(ec_spi->spi, &msg); | ||
416 | |||
417 | /* Get the response */ | ||
418 | if (!ret) { | ||
419 | /* Verify that EC can process command */ | ||
420 | for (i = 0; i < len; i++) { | ||
421 | rx_byte = rx_buf[i]; | ||
422 | /* | ||
423 | * Seeing the PAST_END, RX_BAD_DATA, or NOT_READY | ||
424 | * markers are all signs that the EC didn't fully | ||
425 | * receive our command. e.g., if the EC is flashing | ||
426 | * itself, it can't respond to any commands and instead | ||
427 | * clocks out EC_SPI_PAST_END from its SPI hardware | ||
428 | * buffer. Similar occurrences can happen if the AP is | ||
429 | * too slow to clock out data after asserting CS -- the | ||
430 | * EC will abort and fill its buffer with | ||
431 | * EC_SPI_RX_BAD_DATA. | ||
432 | * | ||
433 | * In all cases, these errors should be safe to retry. | ||
434 | * Report -EAGAIN and let the caller decide what to do | ||
435 | * about that. | ||
436 | */ | ||
437 | if (rx_byte == EC_SPI_PAST_END || | ||
438 | rx_byte == EC_SPI_RX_BAD_DATA || | ||
439 | rx_byte == EC_SPI_NOT_READY) { | ||
440 | ret = -EAGAIN; | ||
441 | break; | ||
442 | } | ||
443 | } | ||
444 | } | ||
445 | |||
446 | if (!ret) | ||
447 | ret = cros_ec_spi_receive_packet(ec_dev, | ||
448 | ec_msg->insize + sizeof(*response)); | ||
449 | else if (ret != -EAGAIN) | ||
450 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | ||
451 | |||
452 | final_ret = terminate_request(ec_dev); | ||
453 | |||
454 | spi_bus_unlock(ec_spi->spi->master); | ||
455 | |||
456 | if (!ret) | ||
457 | ret = final_ret; | ||
458 | if (ret < 0) | ||
459 | goto exit; | ||
460 | |||
461 | ptr = ec_dev->din; | ||
462 | |||
463 | /* check response error code */ | ||
464 | response = (struct ec_host_response *)ptr; | ||
465 | ec_msg->result = response->result; | ||
466 | |||
467 | ret = cros_ec_check_result(ec_dev, ec_msg); | ||
468 | if (ret) | ||
469 | goto exit; | ||
470 | |||
471 | len = response->data_len; | ||
472 | sum = 0; | ||
473 | if (len > ec_msg->insize) { | ||
474 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", | ||
475 | len, ec_msg->insize); | ||
476 | ret = -EMSGSIZE; | ||
477 | goto exit; | ||
478 | } | ||
479 | |||
480 | for (i = 0; i < sizeof(*response); i++) | ||
481 | sum += ptr[i]; | ||
482 | |||
483 | /* copy response packet payload and compute checksum */ | ||
484 | memcpy(ec_msg->data, ptr + sizeof(*response), len); | ||
485 | for (i = 0; i < len; i++) | ||
486 | sum += ec_msg->data[i]; | ||
487 | |||
488 | if (sum) { | ||
489 | dev_err(ec_dev->dev, | ||
490 | "bad packet checksum, calculated %x\n", | ||
491 | sum); | ||
492 | ret = -EBADMSG; | ||
493 | goto exit; | ||
494 | } | ||
495 | |||
496 | ret = len; | ||
497 | exit: | ||
498 | kfree(rx_buf); | ||
499 | if (ec_msg->command == EC_CMD_REBOOT_EC) | ||
500 | msleep(EC_REBOOT_DELAY_MS); | ||
501 | |||
502 | return ret; | ||
503 | } | ||
504 | |||
505 | /** | ||
506 | * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply | ||
507 | * | ||
508 | * @ec_dev: ChromeOS EC device | ||
509 | * @ec_msg: Message to transfer | ||
510 | */ | ||
511 | static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, | ||
512 | struct cros_ec_command *ec_msg) | ||
513 | { | ||
514 | struct cros_ec_spi *ec_spi = ec_dev->priv; | ||
515 | struct spi_transfer trans; | ||
516 | struct spi_message msg; | ||
517 | int i, len; | ||
518 | u8 *ptr; | ||
519 | u8 *rx_buf; | ||
520 | u8 rx_byte; | ||
521 | int sum; | ||
522 | int ret = 0, final_ret; | ||
523 | unsigned long delay; | ||
524 | |||
525 | len = cros_ec_prepare_tx(ec_dev, ec_msg); | ||
526 | dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); | ||
527 | |||
528 | /* If it's too soon to do another transaction, wait */ | ||
529 | delay = ktime_get_ns() - ec_spi->last_transfer_ns; | ||
530 | if (delay < EC_SPI_RECOVERY_TIME_NS) | ||
531 | ndelay(EC_SPI_RECOVERY_TIME_NS - delay); | ||
532 | |||
533 | rx_buf = kzalloc(len, GFP_KERNEL); | ||
534 | if (!rx_buf) | ||
535 | return -ENOMEM; | ||
536 | |||
537 | spi_bus_lock(ec_spi->spi->master); | ||
538 | |||
539 | /* Transmit phase - send our message */ | ||
540 | debug_packet(ec_dev->dev, "out", ec_dev->dout, len); | ||
541 | memset(&trans, 0, sizeof(trans)); | ||
542 | trans.tx_buf = ec_dev->dout; | ||
543 | trans.rx_buf = rx_buf; | ||
544 | trans.len = len; | ||
545 | trans.cs_change = 1; | ||
546 | spi_message_init(&msg); | ||
547 | spi_message_add_tail(&trans, &msg); | ||
548 | ret = spi_sync_locked(ec_spi->spi, &msg); | ||
549 | |||
550 | /* Get the response */ | ||
551 | if (!ret) { | ||
552 | /* Verify that EC can process command */ | ||
553 | for (i = 0; i < len; i++) { | ||
554 | rx_byte = rx_buf[i]; | ||
555 | /* See comments in cros_ec_pkt_xfer_spi() */ | ||
556 | if (rx_byte == EC_SPI_PAST_END || | ||
557 | rx_byte == EC_SPI_RX_BAD_DATA || | ||
558 | rx_byte == EC_SPI_NOT_READY) { | ||
559 | ret = -EAGAIN; | ||
560 | break; | ||
561 | } | ||
562 | } | ||
563 | } | ||
564 | |||
565 | if (!ret) | ||
566 | ret = cros_ec_spi_receive_response(ec_dev, | ||
567 | ec_msg->insize + EC_MSG_TX_PROTO_BYTES); | ||
568 | else if (ret != -EAGAIN) | ||
569 | dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); | ||
570 | |||
571 | final_ret = terminate_request(ec_dev); | ||
572 | |||
573 | spi_bus_unlock(ec_spi->spi->master); | ||
574 | |||
575 | if (!ret) | ||
576 | ret = final_ret; | ||
577 | if (ret < 0) | ||
578 | goto exit; | ||
579 | |||
580 | ptr = ec_dev->din; | ||
581 | |||
582 | /* check response error code */ | ||
583 | ec_msg->result = ptr[0]; | ||
584 | ret = cros_ec_check_result(ec_dev, ec_msg); | ||
585 | if (ret) | ||
586 | goto exit; | ||
587 | |||
588 | len = ptr[1]; | ||
589 | sum = ptr[0] + ptr[1]; | ||
590 | if (len > ec_msg->insize) { | ||
591 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", | ||
592 | len, ec_msg->insize); | ||
593 | ret = -ENOSPC; | ||
594 | goto exit; | ||
595 | } | ||
596 | |||
597 | /* copy response packet payload and compute checksum */ | ||
598 | for (i = 0; i < len; i++) { | ||
599 | sum += ptr[i + 2]; | ||
600 | if (ec_msg->insize) | ||
601 | ec_msg->data[i] = ptr[i + 2]; | ||
602 | } | ||
603 | sum &= 0xff; | ||
604 | |||
605 | debug_packet(ec_dev->dev, "in", ptr, len + 3); | ||
606 | |||
607 | if (sum != ptr[len + 2]) { | ||
608 | dev_err(ec_dev->dev, | ||
609 | "bad packet checksum, expected %02x, got %02x\n", | ||
610 | sum, ptr[len + 2]); | ||
611 | ret = -EBADMSG; | ||
612 | goto exit; | ||
613 | } | ||
614 | |||
615 | ret = len; | ||
616 | exit: | ||
617 | kfree(rx_buf); | ||
618 | if (ec_msg->command == EC_CMD_REBOOT_EC) | ||
619 | msleep(EC_REBOOT_DELAY_MS); | ||
620 | |||
621 | return ret; | ||
622 | } | ||
623 | |||
624 | static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev) | ||
625 | { | ||
626 | struct device_node *np = dev->of_node; | ||
627 | u32 val; | ||
628 | int ret; | ||
629 | |||
630 | ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val); | ||
631 | if (!ret) | ||
632 | ec_spi->start_of_msg_delay = val; | ||
633 | |||
634 | ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val); | ||
635 | if (!ret) | ||
636 | ec_spi->end_of_msg_delay = val; | ||
637 | } | ||
638 | |||
639 | static int cros_ec_spi_probe(struct spi_device *spi) | ||
640 | { | ||
641 | struct device *dev = &spi->dev; | ||
642 | struct cros_ec_device *ec_dev; | ||
643 | struct cros_ec_spi *ec_spi; | ||
644 | int err; | ||
645 | |||
646 | spi->bits_per_word = 8; | ||
647 | spi->mode = SPI_MODE_0; | ||
648 | err = spi_setup(spi); | ||
649 | if (err < 0) | ||
650 | return err; | ||
651 | |||
652 | ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL); | ||
653 | if (ec_spi == NULL) | ||
654 | return -ENOMEM; | ||
655 | ec_spi->spi = spi; | ||
656 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); | ||
657 | if (!ec_dev) | ||
658 | return -ENOMEM; | ||
659 | |||
660 | /* Check for any DT properties */ | ||
661 | cros_ec_spi_dt_probe(ec_spi, dev); | ||
662 | |||
663 | spi_set_drvdata(spi, ec_dev); | ||
664 | ec_dev->dev = dev; | ||
665 | ec_dev->priv = ec_spi; | ||
666 | ec_dev->irq = spi->irq; | ||
667 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; | ||
668 | ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; | ||
669 | ec_dev->phys_name = dev_name(&ec_spi->spi->dev); | ||
670 | ec_dev->din_size = EC_MSG_PREAMBLE_COUNT + | ||
671 | sizeof(struct ec_host_response) + | ||
672 | sizeof(struct ec_response_get_protocol_info); | ||
673 | ec_dev->dout_size = sizeof(struct ec_host_request); | ||
674 | |||
675 | ec_spi->last_transfer_ns = ktime_get_ns(); | ||
676 | |||
677 | err = cros_ec_register(ec_dev); | ||
678 | if (err) { | ||
679 | dev_err(dev, "cannot register EC\n"); | ||
680 | return err; | ||
681 | } | ||
682 | |||
683 | device_init_wakeup(&spi->dev, true); | ||
684 | |||
685 | return 0; | ||
686 | } | ||
687 | |||
688 | static int cros_ec_spi_remove(struct spi_device *spi) | ||
689 | { | ||
690 | struct cros_ec_device *ec_dev; | ||
691 | |||
692 | ec_dev = spi_get_drvdata(spi); | ||
693 | cros_ec_remove(ec_dev); | ||
694 | |||
695 | return 0; | ||
696 | } | ||
697 | |||
698 | #ifdef CONFIG_PM_SLEEP | ||
699 | static int cros_ec_spi_suspend(struct device *dev) | ||
700 | { | ||
701 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); | ||
702 | |||
703 | return cros_ec_suspend(ec_dev); | ||
704 | } | ||
705 | |||
706 | static int cros_ec_spi_resume(struct device *dev) | ||
707 | { | ||
708 | struct cros_ec_device *ec_dev = dev_get_drvdata(dev); | ||
709 | |||
710 | return cros_ec_resume(ec_dev); | ||
711 | } | ||
712 | #endif | ||
713 | |||
714 | static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend, | ||
715 | cros_ec_spi_resume); | ||
716 | |||
717 | static const struct of_device_id cros_ec_spi_of_match[] = { | ||
718 | { .compatible = "google,cros-ec-spi", }, | ||
719 | { /* sentinel */ }, | ||
720 | }; | ||
721 | MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match); | ||
722 | |||
723 | static const struct spi_device_id cros_ec_spi_id[] = { | ||
724 | { "cros-ec-spi", 0 }, | ||
725 | { } | ||
726 | }; | ||
727 | MODULE_DEVICE_TABLE(spi, cros_ec_spi_id); | ||
728 | |||
729 | static struct spi_driver cros_ec_driver_spi = { | ||
730 | .driver = { | ||
731 | .name = "cros-ec-spi", | ||
732 | .of_match_table = of_match_ptr(cros_ec_spi_of_match), | ||
733 | .pm = &cros_ec_spi_pm_ops, | ||
734 | }, | ||
735 | .probe = cros_ec_spi_probe, | ||
736 | .remove = cros_ec_spi_remove, | ||
737 | .id_table = cros_ec_spi_id, | ||
738 | }; | ||
739 | |||
740 | module_spi_driver(cros_ec_driver_spi); | ||
741 | |||
742 | MODULE_LICENSE("GPL v2"); | ||
743 | MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)"); | ||