cros_ec_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_locked(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_locked(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_dev->din_size < EC_MSG_PREAMBLE_COUNT);
  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_dev->din_size < EC_MSG_PREAMBLE_COUNT);
  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_response *response;
  322. struct cros_ec_spi *ec_spi = ec_dev->priv;
  323. struct spi_transfer trans, trans_delay;
  324. struct spi_message msg;
  325. int i, len;
  326. u8 *ptr;
  327. u8 *rx_buf;
  328. u8 sum;
  329. int ret = 0, final_ret;
  330. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  331. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  332. /* If it's too soon to do another transaction, wait */
  333. if (ec_spi->last_transfer_ns) {
  334. unsigned long delay; /* The delay completed so far */
  335. delay = ktime_get_ns() - ec_spi->last_transfer_ns;
  336. if (delay < EC_SPI_RECOVERY_TIME_NS)
  337. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  338. }
  339. rx_buf = kzalloc(len, GFP_KERNEL);
  340. if (!rx_buf)
  341. return -ENOMEM;
  342. spi_bus_lock(ec_spi->spi->master);
  343. /*
  344. * Leave a gap between CS assertion and clocking of data to allow the
  345. * EC time to wakeup.
  346. */
  347. spi_message_init(&msg);
  348. if (ec_spi->start_of_msg_delay) {
  349. memset(&trans_delay, 0, sizeof(trans_delay));
  350. trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
  351. spi_message_add_tail(&trans_delay, &msg);
  352. }
  353. /* Transmit phase - send our message */
  354. memset(&trans, 0, sizeof(trans));
  355. trans.tx_buf = ec_dev->dout;
  356. trans.rx_buf = rx_buf;
  357. trans.len = len;
  358. trans.cs_change = 1;
  359. spi_message_add_tail(&trans, &msg);
  360. ret = spi_sync_locked(ec_spi->spi, &msg);
  361. /* Get the response */
  362. if (!ret) {
  363. /* Verify that EC can process command */
  364. for (i = 0; i < len; i++) {
  365. switch (rx_buf[i]) {
  366. case EC_SPI_PAST_END:
  367. case EC_SPI_RX_BAD_DATA:
  368. case EC_SPI_NOT_READY:
  369. ret = -EAGAIN;
  370. ec_msg->result = EC_RES_IN_PROGRESS;
  371. default:
  372. break;
  373. }
  374. if (ret)
  375. break;
  376. }
  377. if (!ret)
  378. ret = cros_ec_spi_receive_packet(ec_dev,
  379. ec_msg->insize + sizeof(*response));
  380. } else {
  381. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  382. }
  383. final_ret = terminate_request(ec_dev);
  384. spi_bus_unlock(ec_spi->spi->master);
  385. if (!ret)
  386. ret = final_ret;
  387. if (ret < 0)
  388. goto exit;
  389. ptr = ec_dev->din;
  390. /* check response error code */
  391. response = (struct ec_host_response *)ptr;
  392. ec_msg->result = response->result;
  393. ret = cros_ec_check_result(ec_dev, ec_msg);
  394. if (ret)
  395. goto exit;
  396. len = response->data_len;
  397. sum = 0;
  398. if (len > ec_msg->insize) {
  399. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  400. len, ec_msg->insize);
  401. ret = -EMSGSIZE;
  402. goto exit;
  403. }
  404. for (i = 0; i < sizeof(*response); i++)
  405. sum += ptr[i];
  406. /* copy response packet payload and compute checksum */
  407. memcpy(ec_msg->data, ptr + sizeof(*response), len);
  408. for (i = 0; i < len; i++)
  409. sum += ec_msg->data[i];
  410. if (sum) {
  411. dev_err(ec_dev->dev,
  412. "bad packet checksum, calculated %x\n",
  413. sum);
  414. ret = -EBADMSG;
  415. goto exit;
  416. }
  417. ret = len;
  418. exit:
  419. kfree(rx_buf);
  420. if (ec_msg->command == EC_CMD_REBOOT_EC)
  421. msleep(EC_REBOOT_DELAY_MS);
  422. return ret;
  423. }
  424. /**
  425. * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
  426. *
  427. * @ec_dev: ChromeOS EC device
  428. * @ec_msg: Message to transfer
  429. */
  430. static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
  431. struct cros_ec_command *ec_msg)
  432. {
  433. struct cros_ec_spi *ec_spi = ec_dev->priv;
  434. struct spi_transfer trans;
  435. struct spi_message msg;
  436. int i, len;
  437. u8 *ptr;
  438. u8 *rx_buf;
  439. int sum;
  440. int ret = 0, final_ret;
  441. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  442. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  443. /* If it's too soon to do another transaction, wait */
  444. if (ec_spi->last_transfer_ns) {
  445. unsigned long delay; /* The delay completed so far */
  446. delay = ktime_get_ns() - ec_spi->last_transfer_ns;
  447. if (delay < EC_SPI_RECOVERY_TIME_NS)
  448. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  449. }
  450. rx_buf = kzalloc(len, GFP_KERNEL);
  451. if (!rx_buf)
  452. return -ENOMEM;
  453. spi_bus_lock(ec_spi->spi->master);
  454. /* Transmit phase - send our message */
  455. debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
  456. memset(&trans, 0, sizeof(trans));
  457. trans.tx_buf = ec_dev->dout;
  458. trans.rx_buf = rx_buf;
  459. trans.len = len;
  460. trans.cs_change = 1;
  461. spi_message_init(&msg);
  462. spi_message_add_tail(&trans, &msg);
  463. ret = spi_sync_locked(ec_spi->spi, &msg);
  464. /* Get the response */
  465. if (!ret) {
  466. /* Verify that EC can process command */
  467. for (i = 0; i < len; i++) {
  468. switch (rx_buf[i]) {
  469. case EC_SPI_PAST_END:
  470. case EC_SPI_RX_BAD_DATA:
  471. case EC_SPI_NOT_READY:
  472. ret = -EAGAIN;
  473. ec_msg->result = EC_RES_IN_PROGRESS;
  474. default:
  475. break;
  476. }
  477. if (ret)
  478. break;
  479. }
  480. if (!ret)
  481. ret = cros_ec_spi_receive_response(ec_dev,
  482. ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
  483. } else {
  484. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  485. }
  486. final_ret = terminate_request(ec_dev);
  487. spi_bus_unlock(ec_spi->spi->master);
  488. if (!ret)
  489. ret = final_ret;
  490. if (ret < 0)
  491. goto exit;
  492. ptr = ec_dev->din;
  493. /* check response error code */
  494. ec_msg->result = ptr[0];
  495. ret = cros_ec_check_result(ec_dev, ec_msg);
  496. if (ret)
  497. goto exit;
  498. len = ptr[1];
  499. sum = ptr[0] + ptr[1];
  500. if (len > ec_msg->insize) {
  501. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  502. len, ec_msg->insize);
  503. ret = -ENOSPC;
  504. goto exit;
  505. }
  506. /* copy response packet payload and compute checksum */
  507. for (i = 0; i < len; i++) {
  508. sum += ptr[i + 2];
  509. if (ec_msg->insize)
  510. ec_msg->data[i] = ptr[i + 2];
  511. }
  512. sum &= 0xff;
  513. debug_packet(ec_dev->dev, "in", ptr, len + 3);
  514. if (sum != ptr[len + 2]) {
  515. dev_err(ec_dev->dev,
  516. "bad packet checksum, expected %02x, got %02x\n",
  517. sum, ptr[len + 2]);
  518. ret = -EBADMSG;
  519. goto exit;
  520. }
  521. ret = len;
  522. exit:
  523. kfree(rx_buf);
  524. if (ec_msg->command == EC_CMD_REBOOT_EC)
  525. msleep(EC_REBOOT_DELAY_MS);
  526. return ret;
  527. }
  528. static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
  529. {
  530. struct device_node *np = dev->of_node;
  531. u32 val;
  532. int ret;
  533. ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val);
  534. if (!ret)
  535. ec_spi->start_of_msg_delay = val;
  536. ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
  537. if (!ret)
  538. ec_spi->end_of_msg_delay = val;
  539. }
  540. static int cros_ec_spi_probe(struct spi_device *spi)
  541. {
  542. struct device *dev = &spi->dev;
  543. struct cros_ec_device *ec_dev;
  544. struct cros_ec_spi *ec_spi;
  545. int err;
  546. spi->bits_per_word = 8;
  547. spi->mode = SPI_MODE_0;
  548. err = spi_setup(spi);
  549. if (err < 0)
  550. return err;
  551. ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
  552. if (ec_spi == NULL)
  553. return -ENOMEM;
  554. ec_spi->spi = spi;
  555. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  556. if (!ec_dev)
  557. return -ENOMEM;
  558. /* Check for any DT properties */
  559. cros_ec_spi_dt_probe(ec_spi, dev);
  560. spi_set_drvdata(spi, ec_dev);
  561. ec_dev->dev = dev;
  562. ec_dev->priv = ec_spi;
  563. ec_dev->irq = spi->irq;
  564. ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
  565. ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
  566. ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
  567. ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
  568. sizeof(struct ec_host_response) +
  569. sizeof(struct ec_response_get_protocol_info);
  570. ec_dev->dout_size = sizeof(struct ec_host_request);
  571. ec_spi->last_transfer_ns = ktime_get_ns();
  572. err = cros_ec_register(ec_dev);
  573. if (err) {
  574. dev_err(dev, "cannot register EC\n");
  575. return err;
  576. }
  577. device_init_wakeup(&spi->dev, true);
  578. return 0;
  579. }
  580. static int cros_ec_spi_remove(struct spi_device *spi)
  581. {
  582. struct cros_ec_device *ec_dev;
  583. ec_dev = spi_get_drvdata(spi);
  584. cros_ec_remove(ec_dev);
  585. return 0;
  586. }
  587. #ifdef CONFIG_PM_SLEEP
  588. static int cros_ec_spi_suspend(struct device *dev)
  589. {
  590. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  591. return cros_ec_suspend(ec_dev);
  592. }
  593. static int cros_ec_spi_resume(struct device *dev)
  594. {
  595. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  596. return cros_ec_resume(ec_dev);
  597. }
  598. #endif
  599. static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
  600. cros_ec_spi_resume);
  601. static const struct of_device_id cros_ec_spi_of_match[] = {
  602. { .compatible = "google,cros-ec-spi", },
  603. { /* sentinel */ },
  604. };
  605. MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match);
  606. static const struct spi_device_id cros_ec_spi_id[] = {
  607. { "cros-ec-spi", 0 },
  608. { }
  609. };
  610. MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
  611. static struct spi_driver cros_ec_driver_spi = {
  612. .driver = {
  613. .name = "cros-ec-spi",
  614. .of_match_table = of_match_ptr(cros_ec_spi_of_match),
  615. .pm = &cros_ec_spi_pm_ops,
  616. },
  617. .probe = cros_ec_spi_probe,
  618. .remove = cros_ec_spi_remove,
  619. .id_table = cros_ec_spi_id,
  620. };
  621. module_spi_driver(cros_ec_driver_spi);
  622. MODULE_LICENSE("GPL v2");
  623. MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");