olpc-xo175-ec.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for the OLPC XO-1.75 Embedded Controller.
  4. *
  5. * The EC protocol is documented at:
  6. * http://wiki.laptop.org/go/XO_1.75_HOST_to_EC_Protocol
  7. *
  8. * Copyright (C) 2010 One Laptop per Child Foundation.
  9. * Copyright (C) 2018 Lubomir Rintel <[email protected]>
  10. */
  11. #include <linux/completion.h>
  12. #include <linux/ctype.h>
  13. #include <linux/delay.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/input.h>
  16. #include <linux/kfifo.h>
  17. #include <linux/module.h>
  18. #include <linux/olpc-ec.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/reboot.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/spi/spi.h>
  25. struct ec_cmd_t {
  26. u8 cmd;
  27. u8 bytes_returned;
  28. };
  29. enum ec_chan_t {
  30. CHAN_NONE = 0,
  31. CHAN_SWITCH,
  32. CHAN_CMD_RESP,
  33. CHAN_KEYBOARD,
  34. CHAN_TOUCHPAD,
  35. CHAN_EVENT,
  36. CHAN_DEBUG,
  37. CHAN_CMD_ERROR,
  38. };
  39. /*
  40. * EC events
  41. */
  42. #define EVENT_AC_CHANGE 1 /* AC plugged/unplugged */
  43. #define EVENT_BATTERY_STATUS 2 /* Battery low/full/error/gone */
  44. #define EVENT_BATTERY_CRITICAL 3 /* Battery critical voltage */
  45. #define EVENT_BATTERY_SOC_CHANGE 4 /* 1% SOC Change */
  46. #define EVENT_BATTERY_ERROR 5 /* Abnormal error, query for cause */
  47. #define EVENT_POWER_PRESSED 6 /* Power button was pressed */
  48. #define EVENT_POWER_PRESS_WAKE 7 /* Woken up with a power button */
  49. #define EVENT_TIMED_HOST_WAKE 8 /* Host wake timer */
  50. #define EVENT_OLS_HIGH_LIMIT 9 /* OLS crossed dark threshold */
  51. #define EVENT_OLS_LOW_LIMIT 10 /* OLS crossed light threshold */
  52. /*
  53. * EC commands
  54. * (from http://dev.laptop.org/git/users/rsmith/ec-1.75/tree/ec_cmd.h)
  55. */
  56. #define CMD_GET_API_VERSION 0x08 /* out: u8 */
  57. #define CMD_READ_VOLTAGE 0x10 /* out: u16, *9.76/32, mV */
  58. #define CMD_READ_CURRENT 0x11 /* out: s16, *15.625/120, mA */
  59. #define CMD_READ_ACR 0x12 /* out: s16, *6250/15, uAh */
  60. #define CMD_READ_BATT_TEMPERATURE 0x13 /* out: u16, *100/256, deg C */
  61. #define CMD_READ_AMBIENT_TEMPERATURE 0x14 /* unimplemented, no hardware */
  62. #define CMD_READ_BATTERY_STATUS 0x15 /* out: u8, bitmask */
  63. #define CMD_READ_SOC 0x16 /* out: u8, percentage */
  64. #define CMD_READ_GAUGE_ID 0x17 /* out: u8 * 8 */
  65. #define CMD_READ_GAUGE_DATA 0x18 /* in: u8 addr, out: u8 data */
  66. #define CMD_READ_BOARD_ID 0x19 /* out: u16 (platform id) */
  67. #define CMD_READ_BATT_ERR_CODE 0x1f /* out: u8, error bitmask */
  68. #define CMD_SET_DCON_POWER 0x26 /* in: u8 */
  69. #define CMD_RESET_EC 0x28 /* none */
  70. #define CMD_READ_BATTERY_TYPE 0x2c /* out: u8 */
  71. #define CMD_SET_AUTOWAK 0x33 /* out: u8 */
  72. #define CMD_SET_EC_WAKEUP_TIMER 0x36 /* in: u32, out: ? */
  73. #define CMD_READ_EXT_SCI_MASK 0x37 /* ? */
  74. #define CMD_WRITE_EXT_SCI_MASK 0x38 /* ? */
  75. #define CMD_CLEAR_EC_WAKEUP_TIMER 0x39 /* none */
  76. #define CMD_ENABLE_RUNIN_DISCHARGE 0x3B /* none */
  77. #define CMD_DISABLE_RUNIN_DISCHARGE 0x3C /* none */
  78. #define CMD_READ_MPPT_ACTIVE 0x3d /* out: u8 */
  79. #define CMD_READ_MPPT_LIMIT 0x3e /* out: u8 */
  80. #define CMD_SET_MPPT_LIMIT 0x3f /* in: u8 */
  81. #define CMD_DISABLE_MPPT 0x40 /* none */
  82. #define CMD_ENABLE_MPPT 0x41 /* none */
  83. #define CMD_READ_VIN 0x42 /* out: u16 */
  84. #define CMD_EXT_SCI_QUERY 0x43 /* ? */
  85. #define RSP_KEYBOARD_DATA 0x48 /* ? */
  86. #define RSP_TOUCHPAD_DATA 0x49 /* ? */
  87. #define CMD_GET_FW_VERSION 0x4a /* out: u8 * 16 */
  88. #define CMD_POWER_CYCLE 0x4b /* none */
  89. #define CMD_POWER_OFF 0x4c /* none */
  90. #define CMD_RESET_EC_SOFT 0x4d /* none */
  91. #define CMD_READ_GAUGE_U16 0x4e /* ? */
  92. #define CMD_ENABLE_MOUSE 0x4f /* ? */
  93. #define CMD_ECHO 0x52 /* in: u8 * 5, out: u8 * 5 */
  94. #define CMD_GET_FW_DATE 0x53 /* out: u8 * 16 */
  95. #define CMD_GET_FW_USER 0x54 /* out: u8 * 16 */
  96. #define CMD_TURN_OFF_POWER 0x55 /* none (same as 0x4c) */
  97. #define CMD_READ_OLS 0x56 /* out: u16 */
  98. #define CMD_OLS_SMT_LEDON 0x57 /* none */
  99. #define CMD_OLS_SMT_LEDOFF 0x58 /* none */
  100. #define CMD_START_OLS_ASSY 0x59 /* none */
  101. #define CMD_STOP_OLS_ASSY 0x5a /* none */
  102. #define CMD_OLS_SMTTEST_STOP 0x5b /* none */
  103. #define CMD_READ_VIN_SCALED 0x5c /* out: u16 */
  104. #define CMD_READ_BAT_MIN_W 0x5d /* out: u16 */
  105. #define CMD_READ_BAR_MAX_W 0x5e /* out: u16 */
  106. #define CMD_RESET_BAT_MINMAX_W 0x5f /* none */
  107. #define CMD_READ_LOCATION 0x60 /* in: u16 addr, out: u8 data */
  108. #define CMD_WRITE_LOCATION 0x61 /* in: u16 addr, u8 data */
  109. #define CMD_KEYBOARD_CMD 0x62 /* in: u8, out: ? */
  110. #define CMD_TOUCHPAD_CMD 0x63 /* in: u8, out: ? */
  111. #define CMD_GET_FW_HASH 0x64 /* out: u8 * 16 */
  112. #define CMD_SUSPEND_HINT 0x65 /* in: u8 */
  113. #define CMD_ENABLE_WAKE_TIMER 0x66 /* in: u8 */
  114. #define CMD_SET_WAKE_TIMER 0x67 /* in: 32 */
  115. #define CMD_ENABLE_WAKE_AUTORESET 0x68 /* in: u8 */
  116. #define CMD_OLS_SET_LIMITS 0x69 /* in: u16, u16 */
  117. #define CMD_OLS_GET_LIMITS 0x6a /* out: u16, u16 */
  118. #define CMD_OLS_SET_CEILING 0x6b /* in: u16 */
  119. #define CMD_OLS_GET_CEILING 0x6c /* out: u16 */
  120. /*
  121. * Accepted EC commands, and how many bytes they return. There are plenty
  122. * of EC commands that are no longer implemented, or are implemented only on
  123. * certain older boards.
  124. */
  125. static const struct ec_cmd_t olpc_xo175_ec_cmds[] = {
  126. { CMD_GET_API_VERSION, 1 },
  127. { CMD_READ_VOLTAGE, 2 },
  128. { CMD_READ_CURRENT, 2 },
  129. { CMD_READ_ACR, 2 },
  130. { CMD_READ_BATT_TEMPERATURE, 2 },
  131. { CMD_READ_BATTERY_STATUS, 1 },
  132. { CMD_READ_SOC, 1 },
  133. { CMD_READ_GAUGE_ID, 8 },
  134. { CMD_READ_GAUGE_DATA, 1 },
  135. { CMD_READ_BOARD_ID, 2 },
  136. { CMD_READ_BATT_ERR_CODE, 1 },
  137. { CMD_SET_DCON_POWER, 0 },
  138. { CMD_RESET_EC, 0 },
  139. { CMD_READ_BATTERY_TYPE, 1 },
  140. { CMD_ENABLE_RUNIN_DISCHARGE, 0 },
  141. { CMD_DISABLE_RUNIN_DISCHARGE, 0 },
  142. { CMD_READ_MPPT_ACTIVE, 1 },
  143. { CMD_READ_MPPT_LIMIT, 1 },
  144. { CMD_SET_MPPT_LIMIT, 0 },
  145. { CMD_DISABLE_MPPT, 0 },
  146. { CMD_ENABLE_MPPT, 0 },
  147. { CMD_READ_VIN, 2 },
  148. { CMD_GET_FW_VERSION, 16 },
  149. { CMD_POWER_CYCLE, 0 },
  150. { CMD_POWER_OFF, 0 },
  151. { CMD_RESET_EC_SOFT, 0 },
  152. { CMD_ECHO, 5 },
  153. { CMD_GET_FW_DATE, 16 },
  154. { CMD_GET_FW_USER, 16 },
  155. { CMD_TURN_OFF_POWER, 0 },
  156. { CMD_READ_OLS, 2 },
  157. { CMD_OLS_SMT_LEDON, 0 },
  158. { CMD_OLS_SMT_LEDOFF, 0 },
  159. { CMD_START_OLS_ASSY, 0 },
  160. { CMD_STOP_OLS_ASSY, 0 },
  161. { CMD_OLS_SMTTEST_STOP, 0 },
  162. { CMD_READ_VIN_SCALED, 2 },
  163. { CMD_READ_BAT_MIN_W, 2 },
  164. { CMD_READ_BAR_MAX_W, 2 },
  165. { CMD_RESET_BAT_MINMAX_W, 0 },
  166. { CMD_READ_LOCATION, 1 },
  167. { CMD_WRITE_LOCATION, 0 },
  168. { CMD_GET_FW_HASH, 16 },
  169. { CMD_SUSPEND_HINT, 0 },
  170. { CMD_ENABLE_WAKE_TIMER, 0 },
  171. { CMD_SET_WAKE_TIMER, 0 },
  172. { CMD_ENABLE_WAKE_AUTORESET, 0 },
  173. { CMD_OLS_SET_LIMITS, 0 },
  174. { CMD_OLS_GET_LIMITS, 4 },
  175. { CMD_OLS_SET_CEILING, 0 },
  176. { CMD_OLS_GET_CEILING, 2 },
  177. { CMD_READ_EXT_SCI_MASK, 2 },
  178. { CMD_WRITE_EXT_SCI_MASK, 0 },
  179. { }
  180. };
  181. #define EC_MAX_CMD_DATA_LEN 5
  182. #define EC_MAX_RESP_LEN 16
  183. #define LOG_BUF_SIZE 128
  184. #define PM_WAKEUP_TIME 1000
  185. #define EC_ALL_EVENTS GENMASK(15, 0)
  186. enum ec_state_t {
  187. CMD_STATE_IDLE = 0,
  188. CMD_STATE_WAITING_FOR_SWITCH,
  189. CMD_STATE_CMD_IN_TX_FIFO,
  190. CMD_STATE_CMD_SENT,
  191. CMD_STATE_RESP_RECEIVED,
  192. CMD_STATE_ERROR_RECEIVED,
  193. };
  194. struct olpc_xo175_ec_cmd {
  195. u8 command;
  196. u8 nr_args;
  197. u8 data_len;
  198. u8 args[EC_MAX_CMD_DATA_LEN];
  199. };
  200. struct olpc_xo175_ec_resp {
  201. u8 channel;
  202. u8 byte;
  203. };
  204. struct olpc_xo175_ec {
  205. bool suspended;
  206. /* SPI related stuff. */
  207. struct spi_device *spi;
  208. struct spi_transfer xfer;
  209. struct spi_message msg;
  210. union {
  211. struct olpc_xo175_ec_cmd cmd;
  212. struct olpc_xo175_ec_resp resp;
  213. } tx_buf, rx_buf;
  214. /* GPIO for the CMD signals. */
  215. struct gpio_desc *gpio_cmd;
  216. /* Command handling related state. */
  217. spinlock_t cmd_state_lock;
  218. int cmd_state;
  219. bool cmd_running;
  220. struct completion cmd_done;
  221. struct olpc_xo175_ec_cmd cmd;
  222. u8 resp_data[EC_MAX_RESP_LEN];
  223. int expected_resp_len;
  224. int resp_len;
  225. /* Power button. */
  226. struct input_dev *pwrbtn;
  227. /* Debug handling. */
  228. char logbuf[LOG_BUF_SIZE];
  229. int logbuf_len;
  230. };
  231. static struct platform_device *olpc_ec;
  232. static int olpc_xo175_ec_resp_len(u8 cmd)
  233. {
  234. const struct ec_cmd_t *p;
  235. for (p = olpc_xo175_ec_cmds; p->cmd; p++) {
  236. if (p->cmd == cmd)
  237. return p->bytes_returned;
  238. }
  239. return -EINVAL;
  240. }
  241. static void olpc_xo175_ec_flush_logbuf(struct olpc_xo175_ec *priv)
  242. {
  243. dev_dbg(&priv->spi->dev, "got debug string [%*pE]\n",
  244. priv->logbuf_len, priv->logbuf);
  245. priv->logbuf_len = 0;
  246. }
  247. static void olpc_xo175_ec_complete(void *arg);
  248. static void olpc_xo175_ec_send_command(struct olpc_xo175_ec *priv, void *cmd,
  249. size_t cmdlen)
  250. {
  251. int ret;
  252. memcpy(&priv->tx_buf, cmd, cmdlen);
  253. priv->xfer.len = cmdlen;
  254. spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
  255. priv->msg.complete = olpc_xo175_ec_complete;
  256. priv->msg.context = priv;
  257. ret = spi_async(priv->spi, &priv->msg);
  258. if (ret)
  259. dev_err(&priv->spi->dev, "spi_async() failed %d\n", ret);
  260. }
  261. static void olpc_xo175_ec_read_packet(struct olpc_xo175_ec *priv)
  262. {
  263. u8 nonce[] = {0xA5, 0x5A};
  264. olpc_xo175_ec_send_command(priv, nonce, sizeof(nonce));
  265. }
  266. static void olpc_xo175_ec_complete(void *arg)
  267. {
  268. struct olpc_xo175_ec *priv = arg;
  269. struct device *dev = &priv->spi->dev;
  270. struct power_supply *psy;
  271. unsigned long flags;
  272. u8 channel;
  273. u8 byte;
  274. int ret;
  275. ret = priv->msg.status;
  276. if (ret) {
  277. dev_err(dev, "SPI transfer failed: %d\n", ret);
  278. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  279. if (priv->cmd_running) {
  280. priv->resp_len = 0;
  281. priv->cmd_state = CMD_STATE_ERROR_RECEIVED;
  282. complete(&priv->cmd_done);
  283. }
  284. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  285. if (ret != -EINTR)
  286. olpc_xo175_ec_read_packet(priv);
  287. return;
  288. }
  289. channel = priv->rx_buf.resp.channel;
  290. byte = priv->rx_buf.resp.byte;
  291. switch (channel) {
  292. case CHAN_NONE:
  293. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  294. if (!priv->cmd_running) {
  295. /* We can safely ignore these */
  296. dev_err(dev, "spurious FIFO read packet\n");
  297. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  298. return;
  299. }
  300. priv->cmd_state = CMD_STATE_CMD_SENT;
  301. if (!priv->expected_resp_len)
  302. complete(&priv->cmd_done);
  303. olpc_xo175_ec_read_packet(priv);
  304. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  305. return;
  306. case CHAN_SWITCH:
  307. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  308. if (!priv->cmd_running) {
  309. /* Just go with the flow */
  310. dev_err(dev, "spurious SWITCH packet\n");
  311. memset(&priv->cmd, 0, sizeof(priv->cmd));
  312. priv->cmd.command = CMD_ECHO;
  313. }
  314. priv->cmd_state = CMD_STATE_CMD_IN_TX_FIFO;
  315. /* Throw command into TxFIFO */
  316. gpiod_set_value_cansleep(priv->gpio_cmd, 0);
  317. olpc_xo175_ec_send_command(priv, &priv->cmd, sizeof(priv->cmd));
  318. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  319. return;
  320. case CHAN_CMD_RESP:
  321. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  322. if (!priv->cmd_running) {
  323. dev_err(dev, "spurious response packet\n");
  324. } else if (priv->resp_len >= priv->expected_resp_len) {
  325. dev_err(dev, "too many response packets\n");
  326. } else {
  327. priv->resp_data[priv->resp_len++] = byte;
  328. if (priv->resp_len == priv->expected_resp_len) {
  329. priv->cmd_state = CMD_STATE_RESP_RECEIVED;
  330. complete(&priv->cmd_done);
  331. }
  332. }
  333. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  334. break;
  335. case CHAN_CMD_ERROR:
  336. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  337. if (!priv->cmd_running) {
  338. dev_err(dev, "spurious cmd error packet\n");
  339. } else {
  340. priv->resp_data[0] = byte;
  341. priv->resp_len = 1;
  342. priv->cmd_state = CMD_STATE_ERROR_RECEIVED;
  343. complete(&priv->cmd_done);
  344. }
  345. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  346. break;
  347. case CHAN_KEYBOARD:
  348. dev_warn(dev, "keyboard is not supported\n");
  349. break;
  350. case CHAN_TOUCHPAD:
  351. dev_warn(dev, "touchpad is not supported\n");
  352. break;
  353. case CHAN_EVENT:
  354. dev_dbg(dev, "got event %.2x\n", byte);
  355. switch (byte) {
  356. case EVENT_AC_CHANGE:
  357. psy = power_supply_get_by_name("olpc_ac");
  358. if (psy) {
  359. power_supply_changed(psy);
  360. power_supply_put(psy);
  361. }
  362. break;
  363. case EVENT_BATTERY_STATUS:
  364. case EVENT_BATTERY_CRITICAL:
  365. case EVENT_BATTERY_SOC_CHANGE:
  366. case EVENT_BATTERY_ERROR:
  367. psy = power_supply_get_by_name("olpc_battery");
  368. if (psy) {
  369. power_supply_changed(psy);
  370. power_supply_put(psy);
  371. }
  372. break;
  373. case EVENT_POWER_PRESSED:
  374. input_report_key(priv->pwrbtn, KEY_POWER, 1);
  375. input_sync(priv->pwrbtn);
  376. input_report_key(priv->pwrbtn, KEY_POWER, 0);
  377. input_sync(priv->pwrbtn);
  378. fallthrough;
  379. case EVENT_POWER_PRESS_WAKE:
  380. case EVENT_TIMED_HOST_WAKE:
  381. pm_wakeup_event(priv->pwrbtn->dev.parent,
  382. PM_WAKEUP_TIME);
  383. break;
  384. default:
  385. dev_dbg(dev, "ignored unknown event %.2x\n", byte);
  386. break;
  387. }
  388. break;
  389. case CHAN_DEBUG:
  390. if (byte == '\n') {
  391. olpc_xo175_ec_flush_logbuf(priv);
  392. } else if (isprint(byte)) {
  393. priv->logbuf[priv->logbuf_len++] = byte;
  394. if (priv->logbuf_len == LOG_BUF_SIZE)
  395. olpc_xo175_ec_flush_logbuf(priv);
  396. }
  397. break;
  398. default:
  399. dev_warn(dev, "unknown channel: %d, %.2x\n", channel, byte);
  400. break;
  401. }
  402. /* Most non-command packets get the TxFIFO refilled and an ACK. */
  403. olpc_xo175_ec_read_packet(priv);
  404. }
  405. /*
  406. * This function is protected with a mutex. We can safely assume that
  407. * there will be only one instance of this function running at a time.
  408. * One of the ways in which we enforce this is by waiting until we get
  409. * all response bytes back from the EC, rather than just the number that
  410. * the caller requests (otherwise, we might start a new command while an
  411. * old command's response bytes are still incoming).
  412. */
  413. static int olpc_xo175_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *resp,
  414. size_t resp_len, void *ec_cb_arg)
  415. {
  416. struct olpc_xo175_ec *priv = ec_cb_arg;
  417. struct device *dev = &priv->spi->dev;
  418. unsigned long flags;
  419. size_t nr_bytes;
  420. int ret = 0;
  421. dev_dbg(dev, "CMD %x, %zd bytes expected\n", cmd, resp_len);
  422. if (inlen > 5) {
  423. dev_err(dev, "command len %zd too big!\n", resp_len);
  424. return -EOVERFLOW;
  425. }
  426. /* Suspending in the middle of an EC command hoses things badly! */
  427. if (WARN_ON(priv->suspended))
  428. return -EBUSY;
  429. /* Ensure a valid command and return bytes */
  430. ret = olpc_xo175_ec_resp_len(cmd);
  431. if (ret < 0) {
  432. dev_err_ratelimited(dev, "unknown command 0x%x\n", cmd);
  433. /*
  434. * Assume the best in our callers, and allow unknown commands
  435. * through. I'm not the charitable type, but it was beaten
  436. * into me. Just maintain a minimum standard of sanity.
  437. */
  438. if (resp_len > sizeof(priv->resp_data)) {
  439. dev_err(dev, "response too big: %zd!\n", resp_len);
  440. return -EOVERFLOW;
  441. }
  442. nr_bytes = resp_len;
  443. } else {
  444. nr_bytes = (size_t)ret;
  445. ret = 0;
  446. }
  447. resp_len = min(resp_len, nr_bytes);
  448. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  449. /* Initialize the state machine */
  450. init_completion(&priv->cmd_done);
  451. priv->cmd_running = true;
  452. priv->cmd_state = CMD_STATE_WAITING_FOR_SWITCH;
  453. memset(&priv->cmd, 0, sizeof(priv->cmd));
  454. priv->cmd.command = cmd;
  455. priv->cmd.nr_args = inlen;
  456. priv->cmd.data_len = 0;
  457. memcpy(priv->cmd.args, inbuf, inlen);
  458. priv->expected_resp_len = nr_bytes;
  459. priv->resp_len = 0;
  460. /* Tickle the cmd gpio to get things started */
  461. gpiod_set_value_cansleep(priv->gpio_cmd, 1);
  462. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  463. /* The irq handler should do the rest */
  464. if (!wait_for_completion_timeout(&priv->cmd_done,
  465. msecs_to_jiffies(4000))) {
  466. dev_err(dev, "EC cmd error: timeout in STATE %d\n",
  467. priv->cmd_state);
  468. gpiod_set_value_cansleep(priv->gpio_cmd, 0);
  469. spi_slave_abort(priv->spi);
  470. olpc_xo175_ec_read_packet(priv);
  471. return -ETIMEDOUT;
  472. }
  473. spin_lock_irqsave(&priv->cmd_state_lock, flags);
  474. /* Deal with the results. */
  475. if (priv->cmd_state == CMD_STATE_ERROR_RECEIVED) {
  476. /* EC-provided error is in the single response byte */
  477. dev_err(dev, "command 0x%x returned error 0x%x\n",
  478. cmd, priv->resp_data[0]);
  479. ret = -EREMOTEIO;
  480. } else if (priv->resp_len != nr_bytes) {
  481. dev_err(dev, "command 0x%x returned %d bytes, expected %zd bytes\n",
  482. cmd, priv->resp_len, nr_bytes);
  483. ret = -EREMOTEIO;
  484. } else {
  485. /*
  486. * We may have 8 bytes in priv->resp, but we only care about
  487. * what we've been asked for. If the caller asked for only 2
  488. * bytes, give them that. We've guaranteed that
  489. * resp_len <= priv->resp_len and priv->resp_len == nr_bytes.
  490. */
  491. memcpy(resp, priv->resp_data, resp_len);
  492. }
  493. /* This should already be low, but just in case. */
  494. gpiod_set_value_cansleep(priv->gpio_cmd, 0);
  495. priv->cmd_running = false;
  496. spin_unlock_irqrestore(&priv->cmd_state_lock, flags);
  497. return ret;
  498. }
  499. static int olpc_xo175_ec_set_event_mask(unsigned int mask)
  500. {
  501. u8 args[2];
  502. args[0] = mask >> 0;
  503. args[1] = mask >> 8;
  504. return olpc_ec_cmd(CMD_WRITE_EXT_SCI_MASK, args, 2, NULL, 0);
  505. }
  506. static void olpc_xo175_ec_power_off(void)
  507. {
  508. while (1) {
  509. olpc_ec_cmd(CMD_POWER_OFF, NULL, 0, NULL, 0);
  510. mdelay(1000);
  511. }
  512. }
  513. static int __maybe_unused olpc_xo175_ec_suspend(struct device *dev)
  514. {
  515. struct olpc_xo175_ec *priv = dev_get_drvdata(dev);
  516. static struct {
  517. u8 suspend;
  518. u32 suspend_count;
  519. } __packed hintargs;
  520. static unsigned int suspend_count;
  521. /*
  522. * SOC_SLEEP is not wired to the EC on B3 and earlier boards.
  523. * This command lets the EC know instead. The suspend count doesn't seem
  524. * to be used anywhere but in the EC debug output.
  525. */
  526. hintargs.suspend = 1;
  527. hintargs.suspend_count = suspend_count++;
  528. olpc_ec_cmd(CMD_SUSPEND_HINT, (void *)&hintargs, sizeof(hintargs),
  529. NULL, 0);
  530. /*
  531. * After we've sent the suspend hint, don't allow further EC commands
  532. * to be run until we've resumed. Userspace tasks should be frozen,
  533. * but kernel threads and interrupts could still schedule EC commands.
  534. */
  535. priv->suspended = true;
  536. return 0;
  537. }
  538. static int __maybe_unused olpc_xo175_ec_resume_noirq(struct device *dev)
  539. {
  540. struct olpc_xo175_ec *priv = dev_get_drvdata(dev);
  541. priv->suspended = false;
  542. return 0;
  543. }
  544. static int __maybe_unused olpc_xo175_ec_resume(struct device *dev)
  545. {
  546. u8 x = 0;
  547. /*
  548. * The resume hint is only needed if no other commands are
  549. * being sent during resume. all it does is tell the EC
  550. * the SoC is definitely awake.
  551. */
  552. olpc_ec_cmd(CMD_SUSPEND_HINT, &x, 1, NULL, 0);
  553. /* Enable all EC events while we're awake */
  554. olpc_xo175_ec_set_event_mask(EC_ALL_EVENTS);
  555. return 0;
  556. }
  557. static struct olpc_ec_driver olpc_xo175_ec_driver = {
  558. .ec_cmd = olpc_xo175_ec_cmd,
  559. };
  560. static void olpc_xo175_ec_remove(struct spi_device *spi)
  561. {
  562. if (pm_power_off == olpc_xo175_ec_power_off)
  563. pm_power_off = NULL;
  564. spi_slave_abort(spi);
  565. platform_device_unregister(olpc_ec);
  566. olpc_ec = NULL;
  567. }
  568. static int olpc_xo175_ec_probe(struct spi_device *spi)
  569. {
  570. struct olpc_xo175_ec *priv;
  571. int ret;
  572. if (olpc_ec) {
  573. dev_err(&spi->dev, "OLPC EC already registered.\n");
  574. return -EBUSY;
  575. }
  576. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  577. if (!priv)
  578. return -ENOMEM;
  579. priv->gpio_cmd = devm_gpiod_get(&spi->dev, "cmd", GPIOD_OUT_LOW);
  580. if (IS_ERR(priv->gpio_cmd)) {
  581. dev_err(&spi->dev, "failed to get cmd gpio: %ld\n",
  582. PTR_ERR(priv->gpio_cmd));
  583. return PTR_ERR(priv->gpio_cmd);
  584. }
  585. priv->spi = spi;
  586. spin_lock_init(&priv->cmd_state_lock);
  587. priv->cmd_state = CMD_STATE_IDLE;
  588. init_completion(&priv->cmd_done);
  589. priv->logbuf_len = 0;
  590. /* Set up power button input device */
  591. priv->pwrbtn = devm_input_allocate_device(&spi->dev);
  592. if (!priv->pwrbtn)
  593. return -ENOMEM;
  594. priv->pwrbtn->name = "Power Button";
  595. priv->pwrbtn->dev.parent = &spi->dev;
  596. input_set_capability(priv->pwrbtn, EV_KEY, KEY_POWER);
  597. ret = input_register_device(priv->pwrbtn);
  598. if (ret) {
  599. dev_err(&spi->dev, "error registering input device: %d\n", ret);
  600. return ret;
  601. }
  602. spi_set_drvdata(spi, priv);
  603. priv->xfer.rx_buf = &priv->rx_buf;
  604. priv->xfer.tx_buf = &priv->tx_buf;
  605. olpc_xo175_ec_read_packet(priv);
  606. olpc_ec_driver_register(&olpc_xo175_ec_driver, priv);
  607. olpc_ec = platform_device_register_resndata(&spi->dev, "olpc-ec", -1,
  608. NULL, 0, NULL, 0);
  609. /* Enable all EC events while we're awake */
  610. olpc_xo175_ec_set_event_mask(EC_ALL_EVENTS);
  611. if (pm_power_off == NULL)
  612. pm_power_off = olpc_xo175_ec_power_off;
  613. dev_info(&spi->dev, "OLPC XO-1.75 Embedded Controller driver\n");
  614. return 0;
  615. }
  616. static const struct dev_pm_ops olpc_xo175_ec_pm_ops = {
  617. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, olpc_xo175_ec_resume_noirq)
  618. SET_RUNTIME_PM_OPS(olpc_xo175_ec_suspend, olpc_xo175_ec_resume, NULL)
  619. };
  620. static const struct of_device_id olpc_xo175_ec_of_match[] = {
  621. { .compatible = "olpc,xo1.75-ec" },
  622. { }
  623. };
  624. MODULE_DEVICE_TABLE(of, olpc_xo175_ec_of_match);
  625. static const struct spi_device_id olpc_xo175_ec_id_table[] = {
  626. { "xo1.75-ec", 0 },
  627. {}
  628. };
  629. MODULE_DEVICE_TABLE(spi, olpc_xo175_ec_id_table);
  630. static struct spi_driver olpc_xo175_ec_spi_driver = {
  631. .driver = {
  632. .name = "olpc-xo175-ec",
  633. .of_match_table = olpc_xo175_ec_of_match,
  634. .pm = &olpc_xo175_ec_pm_ops,
  635. },
  636. .probe = olpc_xo175_ec_probe,
  637. .remove = olpc_xo175_ec_remove,
  638. };
  639. module_spi_driver(olpc_xo175_ec_spi_driver);
  640. MODULE_DESCRIPTION("OLPC XO-1.75 Embedded Controller driver");
  641. MODULE_AUTHOR("Lennert Buytenhek <[email protected]>"); /* Functionality */
  642. MODULE_AUTHOR("Lubomir Rintel <[email protected]>"); /* Bugs */
  643. MODULE_LICENSE("GPL");