cros_ec_spi.c 17 KB

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