blob: 4e6f2f6b1095cf758d3b14fa8ed0385908e44d26 [file] [log] [blame]
Simon Glassa17d94f2013-02-25 14:08:39 -08001/*
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>
Rhyland Klein01e73c82013-12-09 12:36:09 +010021#include <linux/of.h>
Simon Glassa17d94f2013-02-25 14:08:39 -080022#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/*
Doug Anderson9c0b54a2014-04-30 10:44:07 -070042 * 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 4 times that to handle clock stretching and other
49 * paranoia.
50 *
51 * It's pretty unlikely that we'll really see a 249 byte tunnel in
52 * anything other than testing. If this was more common we might
53 * consider having slow commands like this require a GET_STATUS
54 * wait loop. The 'flash write' command would be another candidate
55 * for this, clocking in at 2-3ms.
56 */
57#define EC_MSG_DEADLINE_MS 100
Simon Glassa17d94f2013-02-25 14:08:39 -080058
59/*
60 * Time between raising the SPI chip select (for the end of a
61 * transaction) and dropping it again (for the next transaction).
Derek Basehore49f91ac2013-11-18 11:30:48 +010062 * If we go too fast, the EC will miss the transaction. We know that we
63 * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
64 * safe.
Simon Glassa17d94f2013-02-25 14:08:39 -080065 */
Derek Basehore49f91ac2013-11-18 11:30:48 +010066#define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
Simon Glassa17d94f2013-02-25 14:08:39 -080067
68/**
69 * struct cros_ec_spi - information about a SPI-connected EC
70 *
71 * @spi: SPI device we are connected to
72 * @last_transfer_ns: time that we last finished a transfer, or 0 if there
73 * if no record
Rhyland Klein01e73c82013-12-09 12:36:09 +010074 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
75 * is sent when we want to turn off CS at the end of a transaction.
Simon Glassa17d94f2013-02-25 14:08:39 -080076 */
77struct cros_ec_spi {
78 struct spi_device *spi;
79 s64 last_transfer_ns;
Rhyland Klein01e73c82013-12-09 12:36:09 +010080 unsigned int end_of_msg_delay;
Simon Glassa17d94f2013-02-25 14:08:39 -080081};
82
83static void debug_packet(struct device *dev, const char *name, u8 *ptr,
Stephen Barberd3654072015-06-09 13:04:46 +020084 int len)
Simon Glassa17d94f2013-02-25 14:08:39 -080085{
86#ifdef DEBUG
87 int i;
88
89 dev_dbg(dev, "%s: ", name);
90 for (i = 0; i < len; i++)
Thierry Reding9e146f42013-11-18 11:30:49 +010091 pr_cont(" %02x", ptr[i]);
92
93 pr_cont("\n");
Simon Glassa17d94f2013-02-25 14:08:39 -080094#endif
95}
96
Stephen Barberd3654072015-06-09 13:04:46 +020097static int terminate_request(struct cros_ec_device *ec_dev)
98{
99 struct cros_ec_spi *ec_spi = ec_dev->priv;
100 struct spi_message msg;
101 struct spi_transfer trans;
102 int ret;
103
104 /*
105 * Turn off CS, possibly adding a delay to ensure the rising edge
106 * doesn't come too soon after the end of the data.
107 */
108 spi_message_init(&msg);
109 memset(&trans, 0, sizeof(trans));
110 trans.delay_usecs = ec_spi->end_of_msg_delay;
111 spi_message_add_tail(&trans, &msg);
112
113 ret = spi_sync(ec_spi->spi, &msg);
114
115 /* Reset end-of-response timer */
116 ec_spi->last_transfer_ns = ktime_get_ns();
117 if (ret < 0) {
118 dev_err(ec_dev->dev,
119 "cs-deassert spi transfer failed: %d\n",
120 ret);
121 }
122
123 return ret;
124}
125
126/**
127 * receive_n_bytes - receive n bytes from the EC.
128 *
129 * Assumes buf is a pointer into the ec_dev->din buffer
130 */
131static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
132{
133 struct cros_ec_spi *ec_spi = ec_dev->priv;
134 struct spi_transfer trans;
135 struct spi_message msg;
136 int ret;
137
138 BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
139
140 memset(&trans, 0, sizeof(trans));
141 trans.cs_change = 1;
142 trans.rx_buf = buf;
143 trans.len = n;
144
145 spi_message_init(&msg);
146 spi_message_add_tail(&trans, &msg);
147 ret = spi_sync(ec_spi->spi, &msg);
148 if (ret < 0)
149 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
150
151 return ret;
152}
153
154/**
155 * cros_ec_spi_receive_packet - Receive a packet from the EC.
156 *
157 * This function has two phases: reading the preamble bytes (since if we read
158 * data from the EC before it is ready to send, we just get preamble) and
159 * reading the actual message.
160 *
161 * The received data is placed into ec_dev->din.
162 *
163 * @ec_dev: ChromeOS EC device
164 * @need_len: Number of message bytes we need to read
165 */
166static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
167 int need_len)
168{
169 struct ec_host_response *response;
170 u8 *ptr, *end;
171 int ret;
172 unsigned long deadline;
173 int todo;
174
175 BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
176
177 /* Receive data until we see the header byte */
178 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
179 while (true) {
180 unsigned long start_jiffies = jiffies;
181
182 ret = receive_n_bytes(ec_dev,
183 ec_dev->din,
184 EC_MSG_PREAMBLE_COUNT);
185 if (ret < 0)
186 return ret;
187
188 ptr = ec_dev->din;
189 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
190 if (*ptr == EC_SPI_FRAME_START) {
191 dev_dbg(ec_dev->dev, "msg found at %zd\n",
192 ptr - ec_dev->din);
193 break;
194 }
195 }
196 if (ptr != end)
197 break;
198
199 /*
200 * Use the time at the start of the loop as a timeout. This
201 * gives us one last shot at getting the transfer and is useful
202 * in case we got context switched out for a while.
203 */
204 if (time_after(start_jiffies, deadline)) {
205 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
206 return -ETIMEDOUT;
207 }
208 }
209
210 /*
211 * ptr now points to the header byte. Copy any valid data to the
212 * start of our buffer
213 */
214 todo = end - ++ptr;
215 BUG_ON(todo < 0 || todo > ec_dev->din_size);
216 todo = min(todo, need_len);
217 memmove(ec_dev->din, ptr, todo);
218 ptr = ec_dev->din + todo;
219 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
220 need_len, todo);
221 need_len -= todo;
222
223 /* If the entire response struct wasn't read, get the rest of it. */
224 if (todo < sizeof(*response)) {
225 ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo);
226 if (ret < 0)
227 return -EBADMSG;
228 ptr += (sizeof(*response) - todo);
229 todo = sizeof(*response);
230 }
231
232 response = (struct ec_host_response *)ec_dev->din;
233
234 /* Abort if data_len is too large. */
235 if (response->data_len > ec_dev->din_size)
236 return -EMSGSIZE;
237
238 /* Receive data until we have it all */
239 while (need_len > 0) {
240 /*
241 * We can't support transfers larger than the SPI FIFO size
242 * unless we have DMA. We don't have DMA on the ISP SPI ports
243 * for Exynos. We need a way of asking SPI driver for
244 * maximum-supported transfer size.
245 */
246 todo = min(need_len, 256);
247 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
248 todo, need_len, ptr - ec_dev->din);
249
250 ret = receive_n_bytes(ec_dev, ptr, todo);
251 if (ret < 0)
252 return ret;
253
254 ptr += todo;
255 need_len -= todo;
256 }
257
258 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
259
260 return 0;
261}
262
Simon Glassa17d94f2013-02-25 14:08:39 -0800263/**
264 * cros_ec_spi_receive_response - Receive a response from the EC.
265 *
266 * This function has two phases: reading the preamble bytes (since if we read
267 * data from the EC before it is ready to send, we just get preamble) and
268 * reading the actual message.
269 *
270 * The received data is placed into ec_dev->din.
271 *
272 * @ec_dev: ChromeOS EC device
273 * @need_len: Number of message bytes we need to read
274 */
275static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
276 int need_len)
277{
Simon Glassa17d94f2013-02-25 14:08:39 -0800278 u8 *ptr, *end;
279 int ret;
280 unsigned long deadline;
281 int todo;
282
Stephen Barberd3654072015-06-09 13:04:46 +0200283 BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
284
Simon Glassa17d94f2013-02-25 14:08:39 -0800285 /* Receive data until we see the header byte */
286 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
Doug Andersonc9a81d62014-04-30 10:44:06 -0700287 while (true) {
288 unsigned long start_jiffies = jiffies;
289
Stephen Barberd3654072015-06-09 13:04:46 +0200290 ret = receive_n_bytes(ec_dev,
291 ec_dev->din,
292 EC_MSG_PREAMBLE_COUNT);
293 if (ret < 0)
Simon Glassa17d94f2013-02-25 14:08:39 -0800294 return ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800295
Stephen Barberd3654072015-06-09 13:04:46 +0200296 ptr = ec_dev->din;
Simon Glassa17d94f2013-02-25 14:08:39 -0800297 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
Stephen Barberd3654072015-06-09 13:04:46 +0200298 if (*ptr == EC_SPI_FRAME_START) {
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200299 dev_dbg(ec_dev->dev, "msg found at %zd\n",
Simon Glassa17d94f2013-02-25 14:08:39 -0800300 ptr - ec_dev->din);
301 break;
302 }
303 }
Doug Andersonc9a81d62014-04-30 10:44:06 -0700304 if (ptr != end)
305 break;
Simon Glassa17d94f2013-02-25 14:08:39 -0800306
Doug Andersonc9a81d62014-04-30 10:44:06 -0700307 /*
308 * Use the time at the start of the loop as a timeout. This
309 * gives us one last shot at getting the transfer and is useful
310 * in case we got context switched out for a while.
311 */
312 if (time_after(start_jiffies, deadline)) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800313 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
314 return -ETIMEDOUT;
315 }
Doug Andersonc9a81d62014-04-30 10:44:06 -0700316 }
Simon Glassa17d94f2013-02-25 14:08:39 -0800317
318 /*
319 * ptr now points to the header byte. Copy any valid data to the
320 * start of our buffer
321 */
322 todo = end - ++ptr;
323 BUG_ON(todo < 0 || todo > ec_dev->din_size);
324 todo = min(todo, need_len);
325 memmove(ec_dev->din, ptr, todo);
326 ptr = ec_dev->din + todo;
327 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
328 need_len, todo);
329 need_len -= todo;
330
331 /* Receive data until we have it all */
332 while (need_len > 0) {
333 /*
334 * We can't support transfers larger than the SPI FIFO size
335 * unless we have DMA. We don't have DMA on the ISP SPI ports
336 * for Exynos. We need a way of asking SPI driver for
337 * maximum-supported transfer size.
338 */
339 todo = min(need_len, 256);
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200340 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
Simon Glassa17d94f2013-02-25 14:08:39 -0800341 todo, need_len, ptr - ec_dev->din);
342
Stephen Barberd3654072015-06-09 13:04:46 +0200343 ret = receive_n_bytes(ec_dev, ptr, todo);
344 if (ret < 0)
Simon Glassa17d94f2013-02-25 14:08:39 -0800345 return ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800346
347 debug_packet(ec_dev->dev, "interim", ptr, todo);
348 ptr += todo;
349 need_len -= todo;
350 }
351
Geert Uytterhoevenc34924b2013-05-08 23:37:45 +0200352 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
Simon Glassa17d94f2013-02-25 14:08:39 -0800353
354 return 0;
355}
356
357/**
Stephen Barberd3654072015-06-09 13:04:46 +0200358 * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
359 *
360 * @ec_dev: ChromeOS EC device
361 * @ec_msg: Message to transfer
362 */
363static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
364 struct cros_ec_command *ec_msg)
365{
366 struct ec_host_request *request;
367 struct ec_host_response *response;
368 struct cros_ec_spi *ec_spi = ec_dev->priv;
369 struct spi_transfer trans;
370 struct spi_message msg;
371 int i, len;
372 u8 *ptr;
373 u8 *rx_buf;
374 u8 sum;
375 int ret = 0, final_ret;
376
377 len = cros_ec_prepare_tx(ec_dev, ec_msg);
378 request = (struct ec_host_request *)ec_dev->dout;
379 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
380
381 /* If it's too soon to do another transaction, wait */
382 if (ec_spi->last_transfer_ns) {
383 unsigned long delay; /* The delay completed so far */
384
385 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
386 if (delay < EC_SPI_RECOVERY_TIME_NS)
387 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
388 }
389
390 rx_buf = kzalloc(len, GFP_KERNEL);
391 if (!rx_buf) {
392 ret = -ENOMEM;
393 goto exit;
394 }
395
396 /* Transmit phase - send our message */
397 memset(&trans, 0, sizeof(trans));
398 trans.tx_buf = ec_dev->dout;
399 trans.rx_buf = rx_buf;
400 trans.len = len;
401 trans.cs_change = 1;
402 spi_message_init(&msg);
403 spi_message_add_tail(&trans, &msg);
404 ret = spi_sync(ec_spi->spi, &msg);
405
406 /* Get the response */
407 if (!ret) {
408 /* Verify that EC can process command */
409 for (i = 0; i < len; i++) {
410 switch (rx_buf[i]) {
411 case EC_SPI_PAST_END:
412 case EC_SPI_RX_BAD_DATA:
413 case EC_SPI_NOT_READY:
414 ret = -EAGAIN;
415 ec_msg->result = EC_RES_IN_PROGRESS;
416 default:
417 break;
418 }
419 if (ret)
420 break;
421 }
422 if (!ret)
423 ret = cros_ec_spi_receive_packet(ec_dev,
424 ec_msg->insize + sizeof(*response));
425 } else {
426 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
427 }
428
429 final_ret = terminate_request(ec_dev);
430 if (!ret)
431 ret = final_ret;
432 if (ret < 0)
433 goto exit;
434
435 ptr = ec_dev->din;
436
437 /* check response error code */
438 response = (struct ec_host_response *)ptr;
439 ec_msg->result = response->result;
440
441 ret = cros_ec_check_result(ec_dev, ec_msg);
442 if (ret)
443 goto exit;
444
445 len = response->data_len;
446 sum = 0;
447 if (len > ec_msg->insize) {
448 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
449 len, ec_msg->insize);
450 ret = -EMSGSIZE;
451 goto exit;
452 }
453
454 for (i = 0; i < sizeof(*response); i++)
455 sum += ptr[i];
456
457 /* copy response packet payload and compute checksum */
458 memcpy(ec_msg->data, ptr + sizeof(*response), len);
459 for (i = 0; i < len; i++)
460 sum += ec_msg->data[i];
461
462 if (sum) {
463 dev_err(ec_dev->dev,
464 "bad packet checksum, calculated %x\n",
465 sum);
466 ret = -EBADMSG;
467 goto exit;
468 }
469
470 ret = len;
471exit:
472 kfree(rx_buf);
473 if (ec_msg->command == EC_CMD_REBOOT_EC)
474 msleep(EC_REBOOT_DELAY_MS);
475
476 return ret;
477}
478
479/**
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700480 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
Simon Glassa17d94f2013-02-25 14:08:39 -0800481 *
482 * @ec_dev: ChromeOS EC device
483 * @ec_msg: Message to transfer
484 */
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700485static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -0700486 struct cros_ec_command *ec_msg)
Simon Glassa17d94f2013-02-25 14:08:39 -0800487{
488 struct cros_ec_spi *ec_spi = ec_dev->priv;
489 struct spi_transfer trans;
490 struct spi_message msg;
491 int i, len;
492 u8 *ptr;
Stephen Barberd3654072015-06-09 13:04:46 +0200493 u8 *rx_buf;
Simon Glassa17d94f2013-02-25 14:08:39 -0800494 int sum;
495 int ret = 0, final_ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800496
497 len = cros_ec_prepare_tx(ec_dev, ec_msg);
498 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
499
500 /* If it's too soon to do another transaction, wait */
501 if (ec_spi->last_transfer_ns) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800502 unsigned long delay; /* The delay completed so far */
503
Thomas Gleixner154adc12014-07-16 21:04:41 +0000504 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
Simon Glassa17d94f2013-02-25 14:08:39 -0800505 if (delay < EC_SPI_RECOVERY_TIME_NS)
David Hendricks1fe36862014-04-30 10:44:04 -0700506 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
Simon Glassa17d94f2013-02-25 14:08:39 -0800507 }
508
Stephen Barberd3654072015-06-09 13:04:46 +0200509 rx_buf = kzalloc(len, GFP_KERNEL);
510 if (!rx_buf) {
511 ret = -ENOMEM;
512 goto exit;
513 }
514
Simon Glassa17d94f2013-02-25 14:08:39 -0800515 /* Transmit phase - send our message */
516 debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
Thierry Redingdaf93d22013-12-09 12:36:10 +0100517 memset(&trans, 0, sizeof(trans));
Simon Glassa17d94f2013-02-25 14:08:39 -0800518 trans.tx_buf = ec_dev->dout;
Stephen Barberd3654072015-06-09 13:04:46 +0200519 trans.rx_buf = rx_buf;
Simon Glassa17d94f2013-02-25 14:08:39 -0800520 trans.len = len;
521 trans.cs_change = 1;
522 spi_message_init(&msg);
523 spi_message_add_tail(&trans, &msg);
524 ret = spi_sync(ec_spi->spi, &msg);
525
526 /* Get the response */
527 if (!ret) {
Stephen Barberd3654072015-06-09 13:04:46 +0200528 /* Verify that EC can process command */
529 for (i = 0; i < len; i++) {
530 switch (rx_buf[i]) {
531 case EC_SPI_PAST_END:
532 case EC_SPI_RX_BAD_DATA:
533 case EC_SPI_NOT_READY:
534 ret = -EAGAIN;
535 ec_msg->result = EC_RES_IN_PROGRESS;
536 default:
537 break;
538 }
539 if (ret)
540 break;
541 }
542 if (!ret)
543 ret = cros_ec_spi_receive_response(ec_dev,
544 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
Simon Glassa17d94f2013-02-25 14:08:39 -0800545 } else {
546 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
547 }
548
Stephen Barberd3654072015-06-09 13:04:46 +0200549 final_ret = terminate_request(ec_dev);
Simon Glassa17d94f2013-02-25 14:08:39 -0800550 if (!ret)
551 ret = final_ret;
Stephen Barberd3654072015-06-09 13:04:46 +0200552 if (ret < 0)
Doug Anderson362196e2014-04-30 10:44:05 -0700553 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800554
Simon Glassa17d94f2013-02-25 14:08:39 -0800555 ptr = ec_dev->din;
Bill Richardson6db07b62014-06-18 11:14:05 -0700556
557 /* check response error code */
558 ec_msg->result = ptr[0];
559 ret = cros_ec_check_result(ec_dev, ec_msg);
560 if (ret)
Doug Anderson362196e2014-04-30 10:44:05 -0700561 goto exit;
Bill Richardson6db07b62014-06-18 11:14:05 -0700562
Simon Glassa17d94f2013-02-25 14:08:39 -0800563 len = ptr[1];
564 sum = ptr[0] + ptr[1];
Bill Richardson5d4773e2014-06-18 11:14:02 -0700565 if (len > ec_msg->insize) {
Simon Glassa17d94f2013-02-25 14:08:39 -0800566 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
Bill Richardson5d4773e2014-06-18 11:14:02 -0700567 len, ec_msg->insize);
Doug Anderson362196e2014-04-30 10:44:05 -0700568 ret = -ENOSPC;
569 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800570 }
571
572 /* copy response packet payload and compute checksum */
573 for (i = 0; i < len; i++) {
574 sum += ptr[i + 2];
Bill Richardson5d4773e2014-06-18 11:14:02 -0700575 if (ec_msg->insize)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200576 ec_msg->data[i] = ptr[i + 2];
Simon Glassa17d94f2013-02-25 14:08:39 -0800577 }
578 sum &= 0xff;
579
580 debug_packet(ec_dev->dev, "in", ptr, len + 3);
581
582 if (sum != ptr[len + 2]) {
583 dev_err(ec_dev->dev,
584 "bad packet checksum, expected %02x, got %02x\n",
585 sum, ptr[len + 2]);
Doug Anderson362196e2014-04-30 10:44:05 -0700586 ret = -EBADMSG;
587 goto exit;
Simon Glassa17d94f2013-02-25 14:08:39 -0800588 }
589
Bill Richardson12ebc8a2014-06-18 11:14:06 -0700590 ret = len;
Doug Anderson362196e2014-04-30 10:44:05 -0700591exit:
Stephen Barberd3654072015-06-09 13:04:46 +0200592 kfree(rx_buf);
Doug Anderson659e1422014-09-18 17:18:54 +0200593 if (ec_msg->command == EC_CMD_REBOOT_EC)
594 msleep(EC_REBOOT_DELAY_MS);
595
Doug Anderson362196e2014-04-30 10:44:05 -0700596 return ret;
Simon Glassa17d94f2013-02-25 14:08:39 -0800597}
598
Rhyland Klein01e73c82013-12-09 12:36:09 +0100599static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
600{
601 struct device_node *np = dev->of_node;
602 u32 val;
603 int ret;
604
605 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
606 if (!ret)
607 ec_spi->end_of_msg_delay = val;
608}
609
Thierry Reding9922b412013-11-25 13:22:30 +0100610static int cros_ec_spi_probe(struct spi_device *spi)
Simon Glassa17d94f2013-02-25 14:08:39 -0800611{
612 struct device *dev = &spi->dev;
613 struct cros_ec_device *ec_dev;
614 struct cros_ec_spi *ec_spi;
615 int err;
616
617 spi->bits_per_word = 8;
618 spi->mode = SPI_MODE_0;
619 err = spi_setup(spi);
620 if (err < 0)
621 return err;
622
623 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
624 if (ec_spi == NULL)
625 return -ENOMEM;
626 ec_spi->spi = spi;
627 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
628 if (!ec_dev)
629 return -ENOMEM;
630
Rhyland Klein01e73c82013-12-09 12:36:09 +0100631 /* Check for any DT properties */
632 cros_ec_spi_dt_probe(ec_spi, dev);
633
Simon Glassa17d94f2013-02-25 14:08:39 -0800634 spi_set_drvdata(spi, ec_dev);
Simon Glassa17d94f2013-02-25 14:08:39 -0800635 ec_dev->dev = dev;
636 ec_dev->priv = ec_spi;
637 ec_dev->irq = spi->irq;
Bill Richardson7e6cb5b2014-06-18 11:14:00 -0700638 ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
Stephen Barberd3654072015-06-09 13:04:46 +0200639 ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
Simon Glassa17d94f2013-02-25 14:08:39 -0800640 ec_dev->ec_name = ec_spi->spi->modalias;
641 ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
Stephen Barber2c7589a2015-06-09 13:04:45 +0200642 ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
643 sizeof(struct ec_host_response) +
644 sizeof(struct ec_response_get_protocol_info);
645 ec_dev->dout_size = sizeof(struct ec_host_request);
Simon Glassa17d94f2013-02-25 14:08:39 -0800646
Stephen Barberd3654072015-06-09 13:04:46 +0200647
Simon Glassa17d94f2013-02-25 14:08:39 -0800648 err = cros_ec_register(ec_dev);
649 if (err) {
650 dev_err(dev, "cannot register EC\n");
651 return err;
652 }
653
Prathyush Kfb504972014-06-16 14:12:23 -0700654 device_init_wakeup(&spi->dev, true);
655
Simon Glassa17d94f2013-02-25 14:08:39 -0800656 return 0;
657}
658
Thierry Reding9922b412013-11-25 13:22:30 +0100659static int cros_ec_spi_remove(struct spi_device *spi)
Simon Glassa17d94f2013-02-25 14:08:39 -0800660{
661 struct cros_ec_device *ec_dev;
662
663 ec_dev = spi_get_drvdata(spi);
664 cros_ec_remove(ec_dev);
665
666 return 0;
667}
668
669#ifdef CONFIG_PM_SLEEP
670static int cros_ec_spi_suspend(struct device *dev)
671{
672 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
673
674 return cros_ec_suspend(ec_dev);
675}
676
677static int cros_ec_spi_resume(struct device *dev)
678{
679 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
680
681 return cros_ec_resume(ec_dev);
682}
683#endif
684
685static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
686 cros_ec_spi_resume);
687
688static const struct spi_device_id cros_ec_spi_id[] = {
689 { "cros-ec-spi", 0 },
690 { }
691};
692MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
693
694static struct spi_driver cros_ec_driver_spi = {
695 .driver = {
696 .name = "cros-ec-spi",
697 .owner = THIS_MODULE,
698 .pm = &cros_ec_spi_pm_ops,
699 },
Thierry Reding9922b412013-11-25 13:22:30 +0100700 .probe = cros_ec_spi_probe,
701 .remove = cros_ec_spi_remove,
Simon Glassa17d94f2013-02-25 14:08:39 -0800702 .id_table = cros_ec_spi_id,
703};
704
705module_spi_driver(cros_ec_driver_spi);
706
Thierry Redingea0f8b02013-12-09 11:05:38 +0100707MODULE_LICENSE("GPL v2");
Simon Glassa17d94f2013-02-25 14:08:39 -0800708MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");