cros_ec_spi.c 19 KB

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