cros_ec_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. #include <linux/delay.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mfd/cros_ec.h>
  19. #include <linux/mfd/cros_ec_commands.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. /* The header byte, which follows the preamble */
  25. #define EC_MSG_HEADER 0xec
  26. /*
  27. * Number of EC preamble bytes we read at a time. Since it takes
  28. * about 400-500us for the EC to respond there is not a lot of
  29. * point in tuning this. If the EC could respond faster then
  30. * we could increase this so that might expect the preamble and
  31. * message to occur in a single transaction. However, the maximum
  32. * SPI transfer size is 256 bytes, so at 5MHz we need a response
  33. * time of perhaps <320us (200 bytes / 1600 bits).
  34. */
  35. #define EC_MSG_PREAMBLE_COUNT 32
  36. /*
  37. * Allow for a long time for the EC to respond. We support i2c
  38. * tunneling and support fairly long messages for the tunnel (249
  39. * bytes long at the moment). If we're talking to a 100 kHz device
  40. * on the other end and need to transfer ~256 bytes, then we need:
  41. * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms
  42. *
  43. * We'll wait 4 times that to handle clock stretching and other
  44. * paranoia.
  45. *
  46. * It's pretty unlikely that we'll really see a 249 byte tunnel in
  47. * anything other than testing. If this was more common we might
  48. * consider having slow commands like this require a GET_STATUS
  49. * wait loop. The 'flash write' command would be another candidate
  50. * for this, clocking in at 2-3ms.
  51. */
  52. #define EC_MSG_DEADLINE_MS 100
  53. /*
  54. * Time between raising the SPI chip select (for the end of a
  55. * transaction) and dropping it again (for the next transaction).
  56. * If we go too fast, the EC will miss the transaction. We know that we
  57. * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
  58. * safe.
  59. */
  60. #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
  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, or 0 if there
  66. * if no record
  67. * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  68. * is sent when we want to turn on CS at the start of a transaction.
  69. * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  70. * is sent when we want to turn off CS at the end of a transaction.
  71. */
  72. struct cros_ec_spi {
  73. struct spi_device *spi;
  74. s64 last_transfer_ns;
  75. unsigned int start_of_msg_delay;
  76. unsigned int end_of_msg_delay;
  77. };
  78. static void debug_packet(struct device *dev, const char *name, u8 *ptr,
  79. int len)
  80. {
  81. #ifdef DEBUG
  82. int i;
  83. dev_dbg(dev, "%s: ", name);
  84. for (i = 0; i < len; i++)
  85. pr_cont(" %02x", ptr[i]);
  86. pr_cont("\n");
  87. #endif
  88. }
  89. static int terminate_request(struct cros_ec_device *ec_dev)
  90. {
  91. struct cros_ec_spi *ec_spi = ec_dev->priv;
  92. struct spi_message msg;
  93. struct spi_transfer trans;
  94. int ret;
  95. /*
  96. * Turn off CS, possibly adding a delay to ensure the rising edge
  97. * doesn't come too soon after the end of the data.
  98. */
  99. spi_message_init(&msg);
  100. memset(&trans, 0, sizeof(trans));
  101. trans.delay_usecs = ec_spi->end_of_msg_delay;
  102. spi_message_add_tail(&trans, &msg);
  103. ret = spi_sync(ec_spi->spi, &msg);
  104. /* Reset end-of-response timer */
  105. ec_spi->last_transfer_ns = ktime_get_ns();
  106. if (ret < 0) {
  107. dev_err(ec_dev->dev,
  108. "cs-deassert spi transfer failed: %d\n",
  109. ret);
  110. }
  111. return ret;
  112. }
  113. /**
  114. * receive_n_bytes - receive n bytes from the EC.
  115. *
  116. * Assumes buf is a pointer into the ec_dev->din buffer
  117. */
  118. static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
  119. {
  120. struct cros_ec_spi *ec_spi = ec_dev->priv;
  121. struct spi_transfer trans;
  122. struct spi_message msg;
  123. int ret;
  124. BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
  125. memset(&trans, 0, sizeof(trans));
  126. trans.cs_change = 1;
  127. trans.rx_buf = buf;
  128. trans.len = n;
  129. spi_message_init(&msg);
  130. spi_message_add_tail(&trans, &msg);
  131. ret = spi_sync(ec_spi->spi, &msg);
  132. if (ret < 0)
  133. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  134. return ret;
  135. }
  136. /**
  137. * cros_ec_spi_receive_packet - Receive a packet from the EC.
  138. *
  139. * This function has two phases: reading the preamble bytes (since if we read
  140. * data from the EC before it is ready to send, we just get preamble) and
  141. * reading the actual message.
  142. *
  143. * The received data is placed into ec_dev->din.
  144. *
  145. * @ec_dev: ChromeOS EC device
  146. * @need_len: Number of message bytes we need to read
  147. */
  148. static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
  149. int need_len)
  150. {
  151. struct ec_host_response *response;
  152. u8 *ptr, *end;
  153. int ret;
  154. unsigned long deadline;
  155. int todo;
  156. BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
  157. /* Receive data until we see the header byte */
  158. deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
  159. while (true) {
  160. unsigned long start_jiffies = jiffies;
  161. ret = receive_n_bytes(ec_dev,
  162. ec_dev->din,
  163. EC_MSG_PREAMBLE_COUNT);
  164. if (ret < 0)
  165. return ret;
  166. ptr = ec_dev->din;
  167. for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
  168. if (*ptr == EC_SPI_FRAME_START) {
  169. dev_dbg(ec_dev->dev, "msg found at %zd\n",
  170. ptr - ec_dev->din);
  171. break;
  172. }
  173. }
  174. if (ptr != end)
  175. break;
  176. /*
  177. * Use the time at the start of the loop as a timeout. This
  178. * gives us one last shot at getting the transfer and is useful
  179. * in case we got context switched out for a while.
  180. */
  181. if (time_after(start_jiffies, deadline)) {
  182. dev_warn(ec_dev->dev, "EC failed to respond in time\n");
  183. return -ETIMEDOUT;
  184. }
  185. }
  186. /*
  187. * ptr now points to the header byte. Copy any valid data to the
  188. * start of our buffer
  189. */
  190. todo = end - ++ptr;
  191. BUG_ON(todo < 0 || todo > ec_dev->din_size);
  192. todo = min(todo, need_len);
  193. memmove(ec_dev->din, ptr, todo);
  194. ptr = ec_dev->din + todo;
  195. dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
  196. need_len, todo);
  197. need_len -= todo;
  198. /* If the entire response struct wasn't read, get the rest of it. */
  199. if (todo < sizeof(*response)) {
  200. ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo);
  201. if (ret < 0)
  202. return -EBADMSG;
  203. ptr += (sizeof(*response) - todo);
  204. todo = sizeof(*response);
  205. }
  206. response = (struct ec_host_response *)ec_dev->din;
  207. /* Abort if data_len is too large. */
  208. if (response->data_len > ec_dev->din_size)
  209. return -EMSGSIZE;
  210. /* Receive data until we have it all */
  211. while (need_len > 0) {
  212. /*
  213. * We can't support transfers larger than the SPI FIFO size
  214. * unless we have DMA. We don't have DMA on the ISP SPI ports
  215. * for Exynos. We need a way of asking SPI driver for
  216. * maximum-supported transfer size.
  217. */
  218. todo = min(need_len, 256);
  219. dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
  220. todo, need_len, ptr - ec_dev->din);
  221. ret = receive_n_bytes(ec_dev, ptr, todo);
  222. if (ret < 0)
  223. return ret;
  224. ptr += todo;
  225. need_len -= todo;
  226. }
  227. dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
  228. return 0;
  229. }
  230. /**
  231. * cros_ec_spi_receive_response - Receive a response from the EC.
  232. *
  233. * This function has two phases: reading the preamble bytes (since if we read
  234. * data from the EC before it is ready to send, we just get preamble) and
  235. * reading the actual message.
  236. *
  237. * The received data is placed into ec_dev->din.
  238. *
  239. * @ec_dev: ChromeOS EC device
  240. * @need_len: Number of message bytes we need to read
  241. */
  242. static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
  243. int need_len)
  244. {
  245. u8 *ptr, *end;
  246. int ret;
  247. unsigned long deadline;
  248. int todo;
  249. BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
  250. /* Receive data until we see the header byte */
  251. deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
  252. while (true) {
  253. unsigned long start_jiffies = jiffies;
  254. ret = receive_n_bytes(ec_dev,
  255. ec_dev->din,
  256. EC_MSG_PREAMBLE_COUNT);
  257. if (ret < 0)
  258. return ret;
  259. ptr = ec_dev->din;
  260. for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
  261. if (*ptr == EC_SPI_FRAME_START) {
  262. dev_dbg(ec_dev->dev, "msg found at %zd\n",
  263. ptr - ec_dev->din);
  264. break;
  265. }
  266. }
  267. if (ptr != end)
  268. break;
  269. /*
  270. * Use the time at the start of the loop as a timeout. This
  271. * gives us one last shot at getting the transfer and is useful
  272. * in case we got context switched out for a while.
  273. */
  274. if (time_after(start_jiffies, deadline)) {
  275. dev_warn(ec_dev->dev, "EC failed to respond in time\n");
  276. return -ETIMEDOUT;
  277. }
  278. }
  279. /*
  280. * ptr now points to the header byte. Copy any valid data to the
  281. * start of our buffer
  282. */
  283. todo = end - ++ptr;
  284. BUG_ON(todo < 0 || todo > ec_dev->din_size);
  285. todo = min(todo, need_len);
  286. memmove(ec_dev->din, ptr, todo);
  287. ptr = ec_dev->din + todo;
  288. dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
  289. need_len, todo);
  290. need_len -= todo;
  291. /* Receive data until we have it all */
  292. while (need_len > 0) {
  293. /*
  294. * We can't support transfers larger than the SPI FIFO size
  295. * unless we have DMA. We don't have DMA on the ISP SPI ports
  296. * for Exynos. We need a way of asking SPI driver for
  297. * maximum-supported transfer size.
  298. */
  299. todo = min(need_len, 256);
  300. dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
  301. todo, need_len, ptr - ec_dev->din);
  302. ret = receive_n_bytes(ec_dev, ptr, todo);
  303. if (ret < 0)
  304. return ret;
  305. debug_packet(ec_dev->dev, "interim", ptr, todo);
  306. ptr += todo;
  307. need_len -= todo;
  308. }
  309. dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
  310. return 0;
  311. }
  312. /**
  313. * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
  314. *
  315. * @ec_dev: ChromeOS EC device
  316. * @ec_msg: Message to transfer
  317. */
  318. static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
  319. struct cros_ec_command *ec_msg)
  320. {
  321. struct ec_host_request *request;
  322. struct ec_host_response *response;
  323. struct cros_ec_spi *ec_spi = ec_dev->priv;
  324. struct spi_transfer trans, trans_delay;
  325. struct spi_message msg;
  326. int i, len;
  327. u8 *ptr;
  328. u8 *rx_buf;
  329. u8 sum;
  330. int ret = 0, final_ret;
  331. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  332. request = (struct ec_host_request *)ec_dev->dout;
  333. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  334. /* If it's too soon to do another transaction, wait */
  335. if (ec_spi->last_transfer_ns) {
  336. unsigned long delay; /* The delay completed so far */
  337. delay = ktime_get_ns() - ec_spi->last_transfer_ns;
  338. if (delay < EC_SPI_RECOVERY_TIME_NS)
  339. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  340. }
  341. rx_buf = kzalloc(len, GFP_KERNEL);
  342. if (!rx_buf) {
  343. ret = -ENOMEM;
  344. goto exit;
  345. }
  346. /*
  347. * Leave a gap between CS assertion and clocking of data to allow the
  348. * EC time to wakeup.
  349. */
  350. spi_message_init(&msg);
  351. if (ec_spi->start_of_msg_delay) {
  352. memset(&trans_delay, 0, sizeof(trans_delay));
  353. trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
  354. spi_message_add_tail(&trans_delay, &msg);
  355. }
  356. /* Transmit phase - send our message */
  357. memset(&trans, 0, sizeof(trans));
  358. trans.tx_buf = ec_dev->dout;
  359. trans.rx_buf = rx_buf;
  360. trans.len = len;
  361. trans.cs_change = 1;
  362. spi_message_add_tail(&trans, &msg);
  363. ret = spi_sync(ec_spi->spi, &msg);
  364. /* Get the response */
  365. if (!ret) {
  366. /* Verify that EC can process command */
  367. for (i = 0; i < len; i++) {
  368. switch (rx_buf[i]) {
  369. case EC_SPI_PAST_END:
  370. case EC_SPI_RX_BAD_DATA:
  371. case EC_SPI_NOT_READY:
  372. ret = -EAGAIN;
  373. ec_msg->result = EC_RES_IN_PROGRESS;
  374. default:
  375. break;
  376. }
  377. if (ret)
  378. break;
  379. }
  380. if (!ret)
  381. ret = cros_ec_spi_receive_packet(ec_dev,
  382. ec_msg->insize + sizeof(*response));
  383. } else {
  384. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  385. }
  386. final_ret = terminate_request(ec_dev);
  387. if (!ret)
  388. ret = final_ret;
  389. if (ret < 0)
  390. goto exit;
  391. ptr = ec_dev->din;
  392. /* check response error code */
  393. response = (struct ec_host_response *)ptr;
  394. ec_msg->result = response->result;
  395. ret = cros_ec_check_result(ec_dev, ec_msg);
  396. if (ret)
  397. goto exit;
  398. len = response->data_len;
  399. sum = 0;
  400. if (len > ec_msg->insize) {
  401. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  402. len, ec_msg->insize);
  403. ret = -EMSGSIZE;
  404. goto exit;
  405. }
  406. for (i = 0; i < sizeof(*response); i++)
  407. sum += ptr[i];
  408. /* copy response packet payload and compute checksum */
  409. memcpy(ec_msg->data, ptr + sizeof(*response), len);
  410. for (i = 0; i < len; i++)
  411. sum += ec_msg->data[i];
  412. if (sum) {
  413. dev_err(ec_dev->dev,
  414. "bad packet checksum, calculated %x\n",
  415. sum);
  416. ret = -EBADMSG;
  417. goto exit;
  418. }
  419. ret = len;
  420. exit:
  421. kfree(rx_buf);
  422. if (ec_msg->command == EC_CMD_REBOOT_EC)
  423. msleep(EC_REBOOT_DELAY_MS);
  424. return ret;
  425. }
  426. /**
  427. * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
  428. *
  429. * @ec_dev: ChromeOS EC device
  430. * @ec_msg: Message to transfer
  431. */
  432. static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
  433. struct cros_ec_command *ec_msg)
  434. {
  435. struct cros_ec_spi *ec_spi = ec_dev->priv;
  436. struct spi_transfer trans;
  437. struct spi_message msg;
  438. int i, len;
  439. u8 *ptr;
  440. u8 *rx_buf;
  441. int sum;
  442. int ret = 0, final_ret;
  443. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  444. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  445. /* If it's too soon to do another transaction, wait */
  446. if (ec_spi->last_transfer_ns) {
  447. unsigned long delay; /* The delay completed so far */
  448. delay = ktime_get_ns() - ec_spi->last_transfer_ns;
  449. if (delay < EC_SPI_RECOVERY_TIME_NS)
  450. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  451. }
  452. rx_buf = kzalloc(len, GFP_KERNEL);
  453. if (!rx_buf) {
  454. ret = -ENOMEM;
  455. goto exit;
  456. }
  457. /* Transmit phase - send our message */
  458. debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
  459. memset(&trans, 0, sizeof(trans));
  460. trans.tx_buf = ec_dev->dout;
  461. trans.rx_buf = rx_buf;
  462. trans.len = len;
  463. trans.cs_change = 1;
  464. spi_message_init(&msg);
  465. spi_message_add_tail(&trans, &msg);
  466. ret = spi_sync(ec_spi->spi, &msg);
  467. /* Get the response */
  468. if (!ret) {
  469. /* Verify that EC can process command */
  470. for (i = 0; i < len; i++) {
  471. switch (rx_buf[i]) {
  472. case EC_SPI_PAST_END:
  473. case EC_SPI_RX_BAD_DATA:
  474. case EC_SPI_NOT_READY:
  475. ret = -EAGAIN;
  476. ec_msg->result = EC_RES_IN_PROGRESS;
  477. default:
  478. break;
  479. }
  480. if (ret)
  481. break;
  482. }
  483. if (!ret)
  484. ret = cros_ec_spi_receive_response(ec_dev,
  485. ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
  486. } else {
  487. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  488. }
  489. final_ret = terminate_request(ec_dev);
  490. if (!ret)
  491. ret = final_ret;
  492. if (ret < 0)
  493. goto exit;
  494. ptr = ec_dev->din;
  495. /* check response error code */
  496. ec_msg->result = ptr[0];
  497. ret = cros_ec_check_result(ec_dev, ec_msg);
  498. if (ret)
  499. goto exit;
  500. len = ptr[1];
  501. sum = ptr[0] + ptr[1];
  502. if (len > ec_msg->insize) {
  503. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  504. len, ec_msg->insize);
  505. ret = -ENOSPC;
  506. goto exit;
  507. }
  508. /* copy response packet payload and compute checksum */
  509. for (i = 0; i < len; i++) {
  510. sum += ptr[i + 2];
  511. if (ec_msg->insize)
  512. ec_msg->data[i] = ptr[i + 2];
  513. }
  514. sum &= 0xff;
  515. debug_packet(ec_dev->dev, "in", ptr, len + 3);
  516. if (sum != ptr[len + 2]) {
  517. dev_err(ec_dev->dev,
  518. "bad packet checksum, expected %02x, got %02x\n",
  519. sum, ptr[len + 2]);
  520. ret = -EBADMSG;
  521. goto exit;
  522. }
  523. ret = len;
  524. exit:
  525. kfree(rx_buf);
  526. if (ec_msg->command == EC_CMD_REBOOT_EC)
  527. msleep(EC_REBOOT_DELAY_MS);
  528. return ret;
  529. }
  530. static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
  531. {
  532. struct device_node *np = dev->of_node;
  533. u32 val;
  534. int ret;
  535. ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val);
  536. if (!ret)
  537. ec_spi->start_of_msg_delay = val;
  538. ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
  539. if (!ret)
  540. ec_spi->end_of_msg_delay = val;
  541. }
  542. static int cros_ec_spi_probe(struct spi_device *spi)
  543. {
  544. struct device *dev = &spi->dev;
  545. struct cros_ec_device *ec_dev;
  546. struct cros_ec_spi *ec_spi;
  547. int err;
  548. spi->bits_per_word = 8;
  549. spi->mode = SPI_MODE_0;
  550. err = spi_setup(spi);
  551. if (err < 0)
  552. return err;
  553. ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
  554. if (ec_spi == NULL)
  555. return -ENOMEM;
  556. ec_spi->spi = spi;
  557. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  558. if (!ec_dev)
  559. return -ENOMEM;
  560. /* Check for any DT properties */
  561. cros_ec_spi_dt_probe(ec_spi, dev);
  562. spi_set_drvdata(spi, ec_dev);
  563. ec_dev->dev = dev;
  564. ec_dev->priv = ec_spi;
  565. ec_dev->irq = spi->irq;
  566. ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
  567. ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
  568. ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
  569. ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
  570. sizeof(struct ec_host_response) +
  571. sizeof(struct ec_response_get_protocol_info);
  572. ec_dev->dout_size = sizeof(struct ec_host_request);
  573. ec_spi->last_transfer_ns = ktime_get_ns();
  574. err = cros_ec_register(ec_dev);
  575. if (err) {
  576. dev_err(dev, "cannot register EC\n");
  577. return err;
  578. }
  579. device_init_wakeup(&spi->dev, true);
  580. return 0;
  581. }
  582. static int cros_ec_spi_remove(struct spi_device *spi)
  583. {
  584. struct cros_ec_device *ec_dev;
  585. ec_dev = spi_get_drvdata(spi);
  586. cros_ec_remove(ec_dev);
  587. return 0;
  588. }
  589. #ifdef CONFIG_PM_SLEEP
  590. static int cros_ec_spi_suspend(struct device *dev)
  591. {
  592. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  593. return cros_ec_suspend(ec_dev);
  594. }
  595. static int cros_ec_spi_resume(struct device *dev)
  596. {
  597. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  598. return cros_ec_resume(ec_dev);
  599. }
  600. #endif
  601. static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
  602. cros_ec_spi_resume);
  603. static const struct of_device_id cros_ec_spi_of_match[] = {
  604. { .compatible = "google,cros-ec-spi", },
  605. { /* sentinel */ },
  606. };
  607. MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match);
  608. static const struct spi_device_id cros_ec_spi_id[] = {
  609. { "cros-ec-spi", 0 },
  610. { }
  611. };
  612. MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
  613. static struct spi_driver cros_ec_driver_spi = {
  614. .driver = {
  615. .name = "cros-ec-spi",
  616. .of_match_table = of_match_ptr(cros_ec_spi_of_match),
  617. .pm = &cros_ec_spi_pm_ops,
  618. },
  619. .probe = cros_ec_spi_probe,
  620. .remove = cros_ec_spi_remove,
  621. .id_table = cros_ec_spi_id,
  622. };
  623. module_spi_driver(cros_ec_driver_spi);
  624. MODULE_LICENSE("GPL v2");
  625. MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");