olpc-ec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic driver for the OLPC Embedded Controller.
  4. *
  5. * Author: Andres Salomon <[email protected]>
  6. *
  7. * Copyright (C) 2011-2012 One Laptop per Child Foundation.
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/olpc-ec.h>
  20. struct ec_cmd_desc {
  21. u8 cmd;
  22. u8 *inbuf, *outbuf;
  23. size_t inlen, outlen;
  24. int err;
  25. struct completion finished;
  26. struct list_head node;
  27. void *priv;
  28. };
  29. struct olpc_ec_priv {
  30. struct olpc_ec_driver *drv;
  31. u8 version;
  32. struct work_struct worker;
  33. struct mutex cmd_lock;
  34. /* DCON regulator */
  35. bool dcon_enabled;
  36. /* Pending EC commands */
  37. struct list_head cmd_q;
  38. spinlock_t cmd_q_lock;
  39. struct dentry *dbgfs_dir;
  40. /*
  41. * EC event mask to be applied during suspend (defining wakeup
  42. * sources).
  43. */
  44. u16 ec_wakeup_mask;
  45. /*
  46. * Running an EC command while suspending means we don't always finish
  47. * the command before the machine suspends. This means that the EC
  48. * is expecting the command protocol to finish, but we after a period
  49. * of time (while the OS is asleep) the EC times out and restarts its
  50. * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
  51. * middle of the command protocol, starts throwing random things at
  52. * the EC... and everyone's uphappy.
  53. */
  54. bool suspended;
  55. };
  56. static struct olpc_ec_driver *ec_driver;
  57. static struct olpc_ec_priv *ec_priv;
  58. static void *ec_cb_arg;
  59. void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
  60. {
  61. ec_driver = drv;
  62. ec_cb_arg = arg;
  63. }
  64. EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
  65. static void olpc_ec_worker(struct work_struct *w)
  66. {
  67. struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
  68. struct ec_cmd_desc *desc = NULL;
  69. unsigned long flags;
  70. /* Grab the first pending command from the queue */
  71. spin_lock_irqsave(&ec->cmd_q_lock, flags);
  72. if (!list_empty(&ec->cmd_q)) {
  73. desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
  74. list_del(&desc->node);
  75. }
  76. spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
  77. /* Do we actually have anything to do? */
  78. if (!desc)
  79. return;
  80. /* Protect the EC hw with a mutex; only run one cmd at a time */
  81. mutex_lock(&ec->cmd_lock);
  82. desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
  83. desc->outbuf, desc->outlen, ec_cb_arg);
  84. mutex_unlock(&ec->cmd_lock);
  85. /* Finished, wake up olpc_ec_cmd() */
  86. complete(&desc->finished);
  87. /* Run the worker thread again in case there are more cmds pending */
  88. schedule_work(&ec->worker);
  89. }
  90. /*
  91. * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
  92. * locking is pretty critical.
  93. */
  94. static void queue_ec_descriptor(struct ec_cmd_desc *desc,
  95. struct olpc_ec_priv *ec)
  96. {
  97. unsigned long flags;
  98. INIT_LIST_HEAD(&desc->node);
  99. spin_lock_irqsave(&ec->cmd_q_lock, flags);
  100. list_add_tail(&desc->node, &ec->cmd_q);
  101. spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
  102. schedule_work(&ec->worker);
  103. }
  104. int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
  105. {
  106. struct olpc_ec_priv *ec = ec_priv;
  107. struct ec_cmd_desc desc;
  108. /* Driver not yet registered. */
  109. if (!ec_driver)
  110. return -EPROBE_DEFER;
  111. if (WARN_ON(!ec_driver->ec_cmd))
  112. return -ENODEV;
  113. if (!ec)
  114. return -ENOMEM;
  115. /* Suspending in the middle of a command hoses things really badly */
  116. if (WARN_ON(ec->suspended))
  117. return -EBUSY;
  118. might_sleep();
  119. desc.cmd = cmd;
  120. desc.inbuf = inbuf;
  121. desc.outbuf = outbuf;
  122. desc.inlen = inlen;
  123. desc.outlen = outlen;
  124. desc.err = 0;
  125. init_completion(&desc.finished);
  126. queue_ec_descriptor(&desc, ec);
  127. /* Timeouts must be handled in the platform-specific EC hook */
  128. wait_for_completion(&desc.finished);
  129. /* The worker thread dequeues the cmd; no need to do anything here */
  130. return desc.err;
  131. }
  132. EXPORT_SYMBOL_GPL(olpc_ec_cmd);
  133. void olpc_ec_wakeup_set(u16 value)
  134. {
  135. struct olpc_ec_priv *ec = ec_priv;
  136. if (WARN_ON(!ec))
  137. return;
  138. ec->ec_wakeup_mask |= value;
  139. }
  140. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
  141. void olpc_ec_wakeup_clear(u16 value)
  142. {
  143. struct olpc_ec_priv *ec = ec_priv;
  144. if (WARN_ON(!ec))
  145. return;
  146. ec->ec_wakeup_mask &= ~value;
  147. }
  148. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
  149. int olpc_ec_mask_write(u16 bits)
  150. {
  151. struct olpc_ec_priv *ec = ec_priv;
  152. if (WARN_ON(!ec))
  153. return -ENODEV;
  154. /* EC version 0x5f adds support for wide SCI mask */
  155. if (ec->version >= 0x5f) {
  156. __be16 ec_word = cpu_to_be16(bits);
  157. return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *)&ec_word, 2, NULL, 0);
  158. } else {
  159. u8 ec_byte = bits & 0xff;
  160. return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
  164. /*
  165. * Returns true if the compile and runtime configurations allow for EC events
  166. * to wake the system.
  167. */
  168. bool olpc_ec_wakeup_available(void)
  169. {
  170. if (WARN_ON(!ec_driver))
  171. return false;
  172. return ec_driver->wakeup_available;
  173. }
  174. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
  175. int olpc_ec_sci_query(u16 *sci_value)
  176. {
  177. struct olpc_ec_priv *ec = ec_priv;
  178. int ret;
  179. if (WARN_ON(!ec))
  180. return -ENODEV;
  181. /* EC version 0x5f adds support for wide SCI mask */
  182. if (ec->version >= 0x5f) {
  183. __be16 ec_word;
  184. ret = olpc_ec_cmd(EC_EXT_SCI_QUERY, NULL, 0, (void *)&ec_word, 2);
  185. if (ret == 0)
  186. *sci_value = be16_to_cpu(ec_word);
  187. } else {
  188. u8 ec_byte;
  189. ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
  190. if (ret == 0)
  191. *sci_value = ec_byte;
  192. }
  193. return ret;
  194. }
  195. EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
  196. #ifdef CONFIG_DEBUG_FS
  197. /*
  198. * debugfs support for "generic commands", to allow sending
  199. * arbitrary EC commands from userspace.
  200. */
  201. #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
  202. #define EC_MAX_CMD_REPLY (8)
  203. static DEFINE_MUTEX(ec_dbgfs_lock);
  204. static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
  205. static unsigned int ec_dbgfs_resp_bytes;
  206. static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
  207. size_t size, loff_t *ppos)
  208. {
  209. int i, m;
  210. unsigned char ec_cmd[EC_MAX_CMD_ARGS];
  211. unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
  212. char cmdbuf[64] = "";
  213. int ec_cmd_bytes;
  214. mutex_lock(&ec_dbgfs_lock);
  215. size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
  216. m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
  217. &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
  218. &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
  219. if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
  220. /* reset to prevent overflow on read */
  221. ec_dbgfs_resp_bytes = 0;
  222. pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
  223. size = -EINVAL;
  224. goto out;
  225. }
  226. /* convert scanf'd ints to char */
  227. ec_cmd_bytes = m - 2;
  228. for (i = 0; i <= ec_cmd_bytes; i++)
  229. ec_cmd[i] = ec_cmd_int[i];
  230. pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
  231. ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
  232. ec_dbgfs_resp_bytes);
  233. olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
  234. ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
  235. pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
  236. ec_dbgfs_resp, ec_dbgfs_resp_bytes);
  237. out:
  238. mutex_unlock(&ec_dbgfs_lock);
  239. return size;
  240. }
  241. static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
  242. size_t size, loff_t *ppos)
  243. {
  244. unsigned int i, r;
  245. char *rp;
  246. char respbuf[64];
  247. mutex_lock(&ec_dbgfs_lock);
  248. rp = respbuf;
  249. rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
  250. for (i = 1; i < ec_dbgfs_resp_bytes; i++)
  251. rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
  252. mutex_unlock(&ec_dbgfs_lock);
  253. rp += sprintf(rp, "\n");
  254. r = rp - respbuf;
  255. return simple_read_from_buffer(buf, size, ppos, respbuf, r);
  256. }
  257. static const struct file_operations ec_dbgfs_ops = {
  258. .write = ec_dbgfs_cmd_write,
  259. .read = ec_dbgfs_cmd_read,
  260. };
  261. static struct dentry *olpc_ec_setup_debugfs(void)
  262. {
  263. struct dentry *dbgfs_dir;
  264. dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
  265. if (IS_ERR_OR_NULL(dbgfs_dir))
  266. return NULL;
  267. debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
  268. return dbgfs_dir;
  269. }
  270. #else
  271. static struct dentry *olpc_ec_setup_debugfs(void)
  272. {
  273. return NULL;
  274. }
  275. #endif /* CONFIG_DEBUG_FS */
  276. static int olpc_ec_set_dcon_power(struct olpc_ec_priv *ec, bool state)
  277. {
  278. unsigned char ec_byte = state;
  279. int ret;
  280. if (ec->dcon_enabled == state)
  281. return 0;
  282. ret = olpc_ec_cmd(EC_DCON_POWER_MODE, &ec_byte, 1, NULL, 0);
  283. if (ret)
  284. return ret;
  285. ec->dcon_enabled = state;
  286. return 0;
  287. }
  288. static int dcon_regulator_enable(struct regulator_dev *rdev)
  289. {
  290. struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
  291. return olpc_ec_set_dcon_power(ec, true);
  292. }
  293. static int dcon_regulator_disable(struct regulator_dev *rdev)
  294. {
  295. struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
  296. return olpc_ec_set_dcon_power(ec, false);
  297. }
  298. static int dcon_regulator_is_enabled(struct regulator_dev *rdev)
  299. {
  300. struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
  301. return ec->dcon_enabled ? 1 : 0;
  302. }
  303. static const struct regulator_ops dcon_regulator_ops = {
  304. .enable = dcon_regulator_enable,
  305. .disable = dcon_regulator_disable,
  306. .is_enabled = dcon_regulator_is_enabled,
  307. };
  308. static const struct regulator_desc dcon_desc = {
  309. .name = "dcon",
  310. .id = 0,
  311. .ops = &dcon_regulator_ops,
  312. .type = REGULATOR_VOLTAGE,
  313. .owner = THIS_MODULE,
  314. .enable_time = 25000,
  315. };
  316. static int olpc_ec_probe(struct platform_device *pdev)
  317. {
  318. struct olpc_ec_priv *ec;
  319. struct regulator_config config = { };
  320. struct regulator_dev *regulator;
  321. int err;
  322. if (!ec_driver)
  323. return -ENODEV;
  324. ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  325. if (!ec)
  326. return -ENOMEM;
  327. ec->drv = ec_driver;
  328. INIT_WORK(&ec->worker, olpc_ec_worker);
  329. mutex_init(&ec->cmd_lock);
  330. INIT_LIST_HEAD(&ec->cmd_q);
  331. spin_lock_init(&ec->cmd_q_lock);
  332. ec_priv = ec;
  333. platform_set_drvdata(pdev, ec);
  334. /* get the EC revision */
  335. err = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ec->version, 1);
  336. if (err)
  337. goto error;
  338. config.dev = pdev->dev.parent;
  339. config.driver_data = ec;
  340. ec->dcon_enabled = true;
  341. regulator = devm_regulator_register(&pdev->dev, &dcon_desc, &config);
  342. if (IS_ERR(regulator)) {
  343. dev_err(&pdev->dev, "failed to register DCON regulator\n");
  344. err = PTR_ERR(regulator);
  345. goto error;
  346. }
  347. ec->dbgfs_dir = olpc_ec_setup_debugfs();
  348. return 0;
  349. error:
  350. ec_priv = NULL;
  351. kfree(ec);
  352. return err;
  353. }
  354. static int olpc_ec_suspend(struct device *dev)
  355. {
  356. struct platform_device *pdev = to_platform_device(dev);
  357. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  358. int err = 0;
  359. olpc_ec_mask_write(ec->ec_wakeup_mask);
  360. if (ec_driver->suspend)
  361. err = ec_driver->suspend(pdev);
  362. if (!err)
  363. ec->suspended = true;
  364. return err;
  365. }
  366. static int olpc_ec_resume(struct device *dev)
  367. {
  368. struct platform_device *pdev = to_platform_device(dev);
  369. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  370. ec->suspended = false;
  371. return ec_driver->resume ? ec_driver->resume(pdev) : 0;
  372. }
  373. static const struct dev_pm_ops olpc_ec_pm_ops = {
  374. .suspend_late = olpc_ec_suspend,
  375. .resume_early = olpc_ec_resume,
  376. };
  377. static struct platform_driver olpc_ec_plat_driver = {
  378. .probe = olpc_ec_probe,
  379. .driver = {
  380. .name = "olpc-ec",
  381. .pm = &olpc_ec_pm_ops,
  382. },
  383. };
  384. static int __init olpc_ec_init_module(void)
  385. {
  386. return platform_driver_register(&olpc_ec_plat_driver);
  387. }
  388. arch_initcall(olpc_ec_init_module);