1 // SPDX-License-Identifier: GPL-2.0 2 // SPI interface for ChromeOS Embedded Controller 3 // 4 // Copyright (C) 2012 Google, Inc 5 6 #include <linux/delay.h> 7 #include <linux/kernel.h> 8 #include <linux/module.h> 9 #include <linux/mfd/cros_ec.h> 10 #include <linux/mfd/cros_ec_commands.h> 11 #include <linux/of.h> 12 #include <linux/platform_device.h> 13 #include <linux/slab.h> 14 #include <linux/spi/spi.h> 15 16 17 /* The header byte, which follows the preamble */ 18 #define EC_MSG_HEADER 0xec 19 20 /* 21 * Number of EC preamble bytes we read at a time. Since it takes 22 * about 400-500us for the EC to respond there is not a lot of 23 * point in tuning this. If the EC could respond faster then 24 * we could increase this so that might expect the preamble and 25 * message to occur in a single transaction. However, the maximum 26 * SPI transfer size is 256 bytes, so at 5MHz we need a response 27 * time of perhaps <320us (200 bytes / 1600 bits). 28 */ 29 #define EC_MSG_PREAMBLE_COUNT 32 30 31 /* 32 * Allow for a long time for the EC to respond. We support i2c 33 * tunneling and support fairly long messages for the tunnel (249 34 * bytes long at the moment). If we're talking to a 100 kHz device 35 * on the other end and need to transfer ~256 bytes, then we need: 36 * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms 37 * 38 * We'll wait 8 times that to handle clock stretching and other 39 * paranoia. Note that some battery gas gauge ICs claim to have a 40 * clock stretch of 144ms in rare situations. That's incentive for 41 * not directly passing i2c through, but it's too late for that for 42 * existing hardware. 43 * 44 * It's pretty unlikely that we'll really see a 249 byte tunnel in 45 * anything other than testing. If this was more common we might 46 * consider having slow commands like this require a GET_STATUS 47 * wait loop. The 'flash write' command would be another candidate 48 * for this, clocking in at 2-3ms. 49 */ 50 #define EC_MSG_DEADLINE_MS 200 51 52 /* 53 * Time between raising the SPI chip select (for the end of a 54 * transaction) and dropping it again (for the next transaction). 55 * If we go too fast, the EC will miss the transaction. We know that we 56 * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be 57 * safe. 58 */ 59 #define EC_SPI_RECOVERY_TIME_NS (200 * 1000) 60 61 /** 62 * struct cros_ec_spi - information about a SPI-connected EC 63 * 64 * @spi: SPI device we are connected to 65 * @last_transfer_ns: time that we last finished a transfer. 66 * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that 67 * is sent when we want to turn on CS at the start of a transaction. 68 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that 69 * is sent when we want to turn off CS at the end of a transaction. 70 */ 71 struct cros_ec_spi { 72 struct spi_device *spi; 73 s64 last_transfer_ns; 74 unsigned int start_of_msg_delay; 75 unsigned int end_of_msg_delay; 76 }; 77 78 typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev, 79 struct cros_ec_command *ec_msg); 80 81 /** 82 * struct cros_ec_xfer_work_params - params for our high priority workers 83 * 84 * @work: The work_struct needed to queue work 85 * @fn: The function to use to transfer 86 * @ec_dev: ChromeOS EC device 87 * @ec_msg: Message to transfer 88 * @ret: The return value of the function 89 */ 90 91 struct cros_ec_xfer_work_params { 92 struct work_struct work; 93 cros_ec_xfer_fn_t fn; 94 struct cros_ec_device *ec_dev; 95 struct cros_ec_command *ec_msg; 96 int ret; 97 }; 98 99 static void debug_packet(struct device *dev, const char *name, u8 *ptr, 100 int len) 101 { 102 #ifdef DEBUG 103 int i; 104 105 dev_dbg(dev, "%s: ", name); 106 for (i = 0; i < len; i++) 107 pr_cont(" %02x", ptr[i]); 108 109 pr_cont("\n"); 110 #endif 111 } 112 113 static int terminate_request(struct cros_ec_device *ec_dev) 114 { 115 struct cros_ec_spi *ec_spi = ec_dev->priv; 116 struct spi_message msg; 117 struct spi_transfer trans; 118 int ret; 119 120 /* 121 * Turn off CS, possibly adding a delay to ensure the rising edge 122 * doesn't come too soon after the end of the data. 123 */ 124 spi_message_init(&msg); 125 memset(&trans, 0, sizeof(trans)); 126 trans.delay_usecs = ec_spi->end_of_msg_delay; 127 spi_message_add_tail(&trans, &msg); 128 129 ret = spi_sync_locked(ec_spi->spi, &msg); 130 131 /* Reset end-of-response timer */ 132 ec_spi->last_transfer_ns = ktime_get_ns(); 133 if (ret < 0) { 134 dev_err(ec_dev->dev, 135 "cs-deassert spi transfer failed: %d\n", 136 ret); 137 } 138 139 return ret; 140 } 141 142 /** 143 * receive_n_bytes - receive n bytes from the EC. 144 * 145 * Assumes buf is a pointer into the ec_dev->din buffer 146 */ 147 static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) 148 { 149 struct cros_ec_spi *ec_spi = ec_dev->priv; 150 struct spi_transfer trans; 151 struct spi_message msg; 152 int ret; 153 154 BUG_ON(buf - ec_dev->din + n > ec_dev->din_size); 155 156 memset(&trans, 0, sizeof(trans)); 157 trans.cs_change = 1; 158 trans.rx_buf = buf; 159 trans.len = n; 160 161 spi_message_init(&msg); 162 spi_message_add_tail(&trans, &msg); 163 ret = spi_sync_locked(ec_spi->spi, &msg); 164 if (ret < 0) 165 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); 166 167 return ret; 168 } 169 170 /** 171 * cros_ec_spi_receive_packet - Receive a packet from the EC. 172 * 173 * This function has two phases: reading the preamble bytes (since if we read 174 * data from the EC before it is ready to send, we just get preamble) and 175 * reading the actual message. 176 * 177 * The received data is placed into ec_dev->din. 178 * 179 * @ec_dev: ChromeOS EC device 180 * @need_len: Number of message bytes we need to read 181 */ 182 static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev, 183 int need_len) 184 { 185 struct ec_host_response *response; 186 u8 *ptr, *end; 187 int ret; 188 unsigned long deadline; 189 int todo; 190 191 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); 192 193 /* Receive data until we see the header byte */ 194 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); 195 while (true) { 196 unsigned long start_jiffies = jiffies; 197 198 ret = receive_n_bytes(ec_dev, 199 ec_dev->din, 200 EC_MSG_PREAMBLE_COUNT); 201 if (ret < 0) 202 return ret; 203 204 ptr = ec_dev->din; 205 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { 206 if (*ptr == EC_SPI_FRAME_START) { 207 dev_dbg(ec_dev->dev, "msg found at %zd\n", 208 ptr - ec_dev->din); 209 break; 210 } 211 } 212 if (ptr != end) 213 break; 214 215 /* 216 * Use the time at the start of the loop as a timeout. This 217 * gives us one last shot at getting the transfer and is useful 218 * in case we got context switched out for a while. 219 */ 220 if (time_after(start_jiffies, deadline)) { 221 dev_warn(ec_dev->dev, "EC failed to respond in time\n"); 222 return -ETIMEDOUT; 223 } 224 } 225 226 /* 227 * ptr now points to the header byte. Copy any valid data to the 228 * start of our buffer 229 */ 230 todo = end - ++ptr; 231 BUG_ON(todo < 0 || todo > ec_dev->din_size); 232 todo = min(todo, need_len); 233 memmove(ec_dev->din, ptr, todo); 234 ptr = ec_dev->din + todo; 235 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", 236 need_len, todo); 237 need_len -= todo; 238 239 /* If the entire response struct wasn't read, get the rest of it. */ 240 if (todo < sizeof(*response)) { 241 ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo); 242 if (ret < 0) 243 return -EBADMSG; 244 ptr += (sizeof(*response) - todo); 245 todo = sizeof(*response); 246 } 247 248 response = (struct ec_host_response *)ec_dev->din; 249 250 /* Abort if data_len is too large. */ 251 if (response->data_len > ec_dev->din_size) 252 return -EMSGSIZE; 253 254 /* Receive data until we have it all */ 255 while (need_len > 0) { 256 /* 257 * We can't support transfers larger than the SPI FIFO size 258 * unless we have DMA. We don't have DMA on the ISP SPI ports 259 * for Exynos. We need a way of asking SPI driver for 260 * maximum-supported transfer size. 261 */ 262 todo = min(need_len, 256); 263 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", 264 todo, need_len, ptr - ec_dev->din); 265 266 ret = receive_n_bytes(ec_dev, ptr, todo); 267 if (ret < 0) 268 return ret; 269 270 ptr += todo; 271 need_len -= todo; 272 } 273 274 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); 275 276 return 0; 277 } 278 279 /** 280 * cros_ec_spi_receive_response - Receive a response from the EC. 281 * 282 * This function has two phases: reading the preamble bytes (since if we read 283 * data from the EC before it is ready to send, we just get preamble) and 284 * reading the actual message. 285 * 286 * The received data is placed into ec_dev->din. 287 * 288 * @ec_dev: ChromeOS EC device 289 * @need_len: Number of message bytes we need to read 290 */ 291 static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev, 292 int need_len) 293 { 294 u8 *ptr, *end; 295 int ret; 296 unsigned long deadline; 297 int todo; 298 299 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT); 300 301 /* Receive data until we see the header byte */ 302 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS); 303 while (true) { 304 unsigned long start_jiffies = jiffies; 305 306 ret = receive_n_bytes(ec_dev, 307 ec_dev->din, 308 EC_MSG_PREAMBLE_COUNT); 309 if (ret < 0) 310 return ret; 311 312 ptr = ec_dev->din; 313 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) { 314 if (*ptr == EC_SPI_FRAME_START) { 315 dev_dbg(ec_dev->dev, "msg found at %zd\n", 316 ptr - ec_dev->din); 317 break; 318 } 319 } 320 if (ptr != end) 321 break; 322 323 /* 324 * Use the time at the start of the loop as a timeout. This 325 * gives us one last shot at getting the transfer and is useful 326 * in case we got context switched out for a while. 327 */ 328 if (time_after(start_jiffies, deadline)) { 329 dev_warn(ec_dev->dev, "EC failed to respond in time\n"); 330 return -ETIMEDOUT; 331 } 332 } 333 334 /* 335 * ptr now points to the header byte. Copy any valid data to the 336 * start of our buffer 337 */ 338 todo = end - ++ptr; 339 BUG_ON(todo < 0 || todo > ec_dev->din_size); 340 todo = min(todo, need_len); 341 memmove(ec_dev->din, ptr, todo); 342 ptr = ec_dev->din + todo; 343 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n", 344 need_len, todo); 345 need_len -= todo; 346 347 /* Receive data until we have it all */ 348 while (need_len > 0) { 349 /* 350 * We can't support transfers larger than the SPI FIFO size 351 * unless we have DMA. We don't have DMA on the ISP SPI ports 352 * for Exynos. We need a way of asking SPI driver for 353 * maximum-supported transfer size. 354 */ 355 todo = min(need_len, 256); 356 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n", 357 todo, need_len, ptr - ec_dev->din); 358 359 ret = receive_n_bytes(ec_dev, ptr, todo); 360 if (ret < 0) 361 return ret; 362 363 debug_packet(ec_dev->dev, "interim", ptr, todo); 364 ptr += todo; 365 need_len -= todo; 366 } 367 368 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din); 369 370 return 0; 371 } 372 373 /** 374 * do_cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply 375 * 376 * @ec_dev: ChromeOS EC device 377 * @ec_msg: Message to transfer 378 */ 379 static int do_cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, 380 struct cros_ec_command *ec_msg) 381 { 382 struct ec_host_response *response; 383 struct cros_ec_spi *ec_spi = ec_dev->priv; 384 struct spi_transfer trans, trans_delay; 385 struct spi_message msg; 386 int i, len; 387 u8 *ptr; 388 u8 *rx_buf; 389 u8 sum; 390 u8 rx_byte; 391 int ret = 0, final_ret; 392 unsigned long delay; 393 394 len = cros_ec_prepare_tx(ec_dev, ec_msg); 395 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); 396 397 /* If it's too soon to do another transaction, wait */ 398 delay = ktime_get_ns() - ec_spi->last_transfer_ns; 399 if (delay < EC_SPI_RECOVERY_TIME_NS) 400 ndelay(EC_SPI_RECOVERY_TIME_NS - delay); 401 402 rx_buf = kzalloc(len, GFP_KERNEL); 403 if (!rx_buf) 404 return -ENOMEM; 405 406 spi_bus_lock(ec_spi->spi->master); 407 408 /* 409 * Leave a gap between CS assertion and clocking of data to allow the 410 * EC time to wakeup. 411 */ 412 spi_message_init(&msg); 413 if (ec_spi->start_of_msg_delay) { 414 memset(&trans_delay, 0, sizeof(trans_delay)); 415 trans_delay.delay_usecs = ec_spi->start_of_msg_delay; 416 spi_message_add_tail(&trans_delay, &msg); 417 } 418 419 /* Transmit phase - send our message */ 420 memset(&trans, 0, sizeof(trans)); 421 trans.tx_buf = ec_dev->dout; 422 trans.rx_buf = rx_buf; 423 trans.len = len; 424 trans.cs_change = 1; 425 spi_message_add_tail(&trans, &msg); 426 ret = spi_sync_locked(ec_spi->spi, &msg); 427 428 /* Get the response */ 429 if (!ret) { 430 /* Verify that EC can process command */ 431 for (i = 0; i < len; i++) { 432 rx_byte = rx_buf[i]; 433 /* 434 * Seeing the PAST_END, RX_BAD_DATA, or NOT_READY 435 * markers are all signs that the EC didn't fully 436 * receive our command. e.g., if the EC is flashing 437 * itself, it can't respond to any commands and instead 438 * clocks out EC_SPI_PAST_END from its SPI hardware 439 * buffer. Similar occurrences can happen if the AP is 440 * too slow to clock out data after asserting CS -- the 441 * EC will abort and fill its buffer with 442 * EC_SPI_RX_BAD_DATA. 443 * 444 * In all cases, these errors should be safe to retry. 445 * Report -EAGAIN and let the caller decide what to do 446 * about that. 447 */ 448 if (rx_byte == EC_SPI_PAST_END || 449 rx_byte == EC_SPI_RX_BAD_DATA || 450 rx_byte == EC_SPI_NOT_READY) { 451 ret = -EAGAIN; 452 break; 453 } 454 } 455 } 456 457 if (!ret) 458 ret = cros_ec_spi_receive_packet(ec_dev, 459 ec_msg->insize + sizeof(*response)); 460 else if (ret != -EAGAIN) 461 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); 462 463 final_ret = terminate_request(ec_dev); 464 465 spi_bus_unlock(ec_spi->spi->master); 466 467 if (!ret) 468 ret = final_ret; 469 if (ret < 0) 470 goto exit; 471 472 ptr = ec_dev->din; 473 474 /* check response error code */ 475 response = (struct ec_host_response *)ptr; 476 ec_msg->result = response->result; 477 478 ret = cros_ec_check_result(ec_dev, ec_msg); 479 if (ret) 480 goto exit; 481 482 len = response->data_len; 483 sum = 0; 484 if (len > ec_msg->insize) { 485 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", 486 len, ec_msg->insize); 487 ret = -EMSGSIZE; 488 goto exit; 489 } 490 491 for (i = 0; i < sizeof(*response); i++) 492 sum += ptr[i]; 493 494 /* copy response packet payload and compute checksum */ 495 memcpy(ec_msg->data, ptr + sizeof(*response), len); 496 for (i = 0; i < len; i++) 497 sum += ec_msg->data[i]; 498 499 if (sum) { 500 dev_err(ec_dev->dev, 501 "bad packet checksum, calculated %x\n", 502 sum); 503 ret = -EBADMSG; 504 goto exit; 505 } 506 507 ret = len; 508 exit: 509 kfree(rx_buf); 510 if (ec_msg->command == EC_CMD_REBOOT_EC) 511 msleep(EC_REBOOT_DELAY_MS); 512 513 return ret; 514 } 515 516 /** 517 * do_cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply 518 * 519 * @ec_dev: ChromeOS EC device 520 * @ec_msg: Message to transfer 521 */ 522 static int do_cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, 523 struct cros_ec_command *ec_msg) 524 { 525 struct cros_ec_spi *ec_spi = ec_dev->priv; 526 struct spi_transfer trans; 527 struct spi_message msg; 528 int i, len; 529 u8 *ptr; 530 u8 *rx_buf; 531 u8 rx_byte; 532 int sum; 533 int ret = 0, final_ret; 534 unsigned long delay; 535 536 len = cros_ec_prepare_tx(ec_dev, ec_msg); 537 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); 538 539 /* If it's too soon to do another transaction, wait */ 540 delay = ktime_get_ns() - ec_spi->last_transfer_ns; 541 if (delay < EC_SPI_RECOVERY_TIME_NS) 542 ndelay(EC_SPI_RECOVERY_TIME_NS - delay); 543 544 rx_buf = kzalloc(len, GFP_KERNEL); 545 if (!rx_buf) 546 return -ENOMEM; 547 548 spi_bus_lock(ec_spi->spi->master); 549 550 /* Transmit phase - send our message */ 551 debug_packet(ec_dev->dev, "out", ec_dev->dout, len); 552 memset(&trans, 0, sizeof(trans)); 553 trans.tx_buf = ec_dev->dout; 554 trans.rx_buf = rx_buf; 555 trans.len = len; 556 trans.cs_change = 1; 557 spi_message_init(&msg); 558 spi_message_add_tail(&trans, &msg); 559 ret = spi_sync_locked(ec_spi->spi, &msg); 560 561 /* Get the response */ 562 if (!ret) { 563 /* Verify that EC can process command */ 564 for (i = 0; i < len; i++) { 565 rx_byte = rx_buf[i]; 566 /* See comments in cros_ec_pkt_xfer_spi() */ 567 if (rx_byte == EC_SPI_PAST_END || 568 rx_byte == EC_SPI_RX_BAD_DATA || 569 rx_byte == EC_SPI_NOT_READY) { 570 ret = -EAGAIN; 571 break; 572 } 573 } 574 } 575 576 if (!ret) 577 ret = cros_ec_spi_receive_response(ec_dev, 578 ec_msg->insize + EC_MSG_TX_PROTO_BYTES); 579 else if (ret != -EAGAIN) 580 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); 581 582 final_ret = terminate_request(ec_dev); 583 584 spi_bus_unlock(ec_spi->spi->master); 585 586 if (!ret) 587 ret = final_ret; 588 if (ret < 0) 589 goto exit; 590 591 ptr = ec_dev->din; 592 593 /* check response error code */ 594 ec_msg->result = ptr[0]; 595 ret = cros_ec_check_result(ec_dev, ec_msg); 596 if (ret) 597 goto exit; 598 599 len = ptr[1]; 600 sum = ptr[0] + ptr[1]; 601 if (len > ec_msg->insize) { 602 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", 603 len, ec_msg->insize); 604 ret = -ENOSPC; 605 goto exit; 606 } 607 608 /* copy response packet payload and compute checksum */ 609 for (i = 0; i < len; i++) { 610 sum += ptr[i + 2]; 611 if (ec_msg->insize) 612 ec_msg->data[i] = ptr[i + 2]; 613 } 614 sum &= 0xff; 615 616 debug_packet(ec_dev->dev, "in", ptr, len + 3); 617 618 if (sum != ptr[len + 2]) { 619 dev_err(ec_dev->dev, 620 "bad packet checksum, expected %02x, got %02x\n", 621 sum, ptr[len + 2]); 622 ret = -EBADMSG; 623 goto exit; 624 } 625 626 ret = len; 627 exit: 628 kfree(rx_buf); 629 if (ec_msg->command == EC_CMD_REBOOT_EC) 630 msleep(EC_REBOOT_DELAY_MS); 631 632 return ret; 633 } 634 635 static void cros_ec_xfer_high_pri_work(struct work_struct *work) 636 { 637 struct cros_ec_xfer_work_params *params; 638 639 params = container_of(work, struct cros_ec_xfer_work_params, work); 640 params->ret = params->fn(params->ec_dev, params->ec_msg); 641 } 642 643 static int cros_ec_xfer_high_pri(struct cros_ec_device *ec_dev, 644 struct cros_ec_command *ec_msg, 645 cros_ec_xfer_fn_t fn) 646 { 647 struct cros_ec_xfer_work_params params; 648 649 INIT_WORK_ONSTACK(¶ms.work, cros_ec_xfer_high_pri_work); 650 params.ec_dev = ec_dev; 651 params.ec_msg = ec_msg; 652 params.fn = fn; 653 654 /* 655 * This looks a bit ridiculous. Why do the work on a 656 * different thread if we're just going to block waiting for 657 * the thread to finish? The key here is that the thread is 658 * running at high priority but the calling context might not 659 * be. We need to be at high priority to avoid getting 660 * context switched out for too long and the EC giving up on 661 * the transfer. 662 */ 663 queue_work(system_highpri_wq, ¶ms.work); 664 flush_work(¶ms.work); 665 destroy_work_on_stack(¶ms.work); 666 667 return params.ret; 668 } 669 670 static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, 671 struct cros_ec_command *ec_msg) 672 { 673 return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_pkt_xfer_spi); 674 } 675 676 static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, 677 struct cros_ec_command *ec_msg) 678 { 679 return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi); 680 } 681 682 static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev) 683 { 684 struct device_node *np = dev->of_node; 685 u32 val; 686 int ret; 687 688 ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val); 689 if (!ret) 690 ec_spi->start_of_msg_delay = val; 691 692 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val); 693 if (!ret) 694 ec_spi->end_of_msg_delay = val; 695 } 696 697 static int cros_ec_spi_probe(struct spi_device *spi) 698 { 699 struct device *dev = &spi->dev; 700 struct cros_ec_device *ec_dev; 701 struct cros_ec_spi *ec_spi; 702 int err; 703 704 spi->bits_per_word = 8; 705 spi->mode = SPI_MODE_0; 706 err = spi_setup(spi); 707 if (err < 0) 708 return err; 709 710 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL); 711 if (ec_spi == NULL) 712 return -ENOMEM; 713 ec_spi->spi = spi; 714 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); 715 if (!ec_dev) 716 return -ENOMEM; 717 718 /* Check for any DT properties */ 719 cros_ec_spi_dt_probe(ec_spi, dev); 720 721 spi_set_drvdata(spi, ec_dev); 722 ec_dev->dev = dev; 723 ec_dev->priv = ec_spi; 724 ec_dev->irq = spi->irq; 725 ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi; 726 ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi; 727 ec_dev->phys_name = dev_name(&ec_spi->spi->dev); 728 ec_dev->din_size = EC_MSG_PREAMBLE_COUNT + 729 sizeof(struct ec_host_response) + 730 sizeof(struct ec_response_get_protocol_info); 731 ec_dev->dout_size = sizeof(struct ec_host_request); 732 733 ec_spi->last_transfer_ns = ktime_get_ns(); 734 735 err = cros_ec_register(ec_dev); 736 if (err) { 737 dev_err(dev, "cannot register EC\n"); 738 return err; 739 } 740 741 device_init_wakeup(&spi->dev, true); 742 743 return 0; 744 } 745 746 #ifdef CONFIG_PM_SLEEP 747 static int cros_ec_spi_suspend(struct device *dev) 748 { 749 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 750 751 return cros_ec_suspend(ec_dev); 752 } 753 754 static int cros_ec_spi_resume(struct device *dev) 755 { 756 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 757 758 return cros_ec_resume(ec_dev); 759 } 760 #endif 761 762 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend, 763 cros_ec_spi_resume); 764 765 static const struct of_device_id cros_ec_spi_of_match[] = { 766 { .compatible = "google,cros-ec-spi", }, 767 { /* sentinel */ }, 768 }; 769 MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match); 770 771 static const struct spi_device_id cros_ec_spi_id[] = { 772 { "cros-ec-spi", 0 }, 773 { } 774 }; 775 MODULE_DEVICE_TABLE(spi, cros_ec_spi_id); 776 777 static struct spi_driver cros_ec_driver_spi = { 778 .driver = { 779 .name = "cros-ec-spi", 780 .of_match_table = of_match_ptr(cros_ec_spi_of_match), 781 .pm = &cros_ec_spi_pm_ops, 782 }, 783 .probe = cros_ec_spi_probe, 784 .id_table = cros_ec_spi_id, 785 }; 786 787 module_spi_driver(cros_ec_driver_spi); 788 789 MODULE_LICENSE("GPL v2"); 790 MODULE_DESCRIPTION("SPI interface for ChromeOS Embedded Controller"); 791