cros_ec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ChromeOS EC multi-function device
  4. *
  5. * Copyright (C) 2012 Google, Inc
  6. *
  7. * The ChromeOS EC multi function device is used to mux all the requests
  8. * to the EC device for its multiple features: keyboard controller,
  9. * battery charging and regulator control, firmware update.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_data/cros_ec_commands.h>
  15. #include <linux/platform_data/cros_ec_proto.h>
  16. #include <linux/slab.h>
  17. #include <linux/suspend.h>
  18. #include "cros_ec.h"
  19. static struct cros_ec_platform ec_p = {
  20. .ec_name = CROS_EC_DEV_NAME,
  21. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_EC_INDEX),
  22. };
  23. static struct cros_ec_platform pd_p = {
  24. .ec_name = CROS_EC_DEV_PD_NAME,
  25. .cmd_offset = EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX),
  26. };
  27. /**
  28. * cros_ec_irq_handler() - top half part of the interrupt handler
  29. * @irq: IRQ id
  30. * @data: (ec_dev) Device with events to process.
  31. *
  32. * Return: Wakeup the bottom half
  33. */
  34. static irqreturn_t cros_ec_irq_handler(int irq, void *data)
  35. {
  36. struct cros_ec_device *ec_dev = data;
  37. ec_dev->last_event_time = cros_ec_get_time_ns();
  38. return IRQ_WAKE_THREAD;
  39. }
  40. /**
  41. * cros_ec_handle_event() - process and forward pending events on EC
  42. * @ec_dev: Device with events to process.
  43. *
  44. * Call this function in a loop when the kernel is notified that the EC has
  45. * pending events.
  46. *
  47. * Return: true if more events are still pending and this function should be
  48. * called again.
  49. */
  50. static bool cros_ec_handle_event(struct cros_ec_device *ec_dev)
  51. {
  52. bool wake_event;
  53. bool ec_has_more_events;
  54. int ret;
  55. ret = cros_ec_get_next_event(ec_dev, &wake_event, &ec_has_more_events);
  56. /*
  57. * Signal only if wake host events or any interrupt if
  58. * cros_ec_get_next_event() returned an error (default value for
  59. * wake_event is true)
  60. */
  61. if (wake_event && device_may_wakeup(ec_dev->dev))
  62. pm_wakeup_event(ec_dev->dev, 0);
  63. if (ret > 0)
  64. blocking_notifier_call_chain(&ec_dev->event_notifier,
  65. 0, ec_dev);
  66. return ec_has_more_events;
  67. }
  68. /**
  69. * cros_ec_irq_thread() - bottom half part of the interrupt handler
  70. * @irq: IRQ id
  71. * @data: (ec_dev) Device with events to process.
  72. *
  73. * Return: Interrupt handled.
  74. */
  75. irqreturn_t cros_ec_irq_thread(int irq, void *data)
  76. {
  77. struct cros_ec_device *ec_dev = data;
  78. bool ec_has_more_events;
  79. do {
  80. ec_has_more_events = cros_ec_handle_event(ec_dev);
  81. } while (ec_has_more_events);
  82. return IRQ_HANDLED;
  83. }
  84. EXPORT_SYMBOL(cros_ec_irq_thread);
  85. static int cros_ec_sleep_event(struct cros_ec_device *ec_dev, u8 sleep_event)
  86. {
  87. int ret;
  88. struct {
  89. struct cros_ec_command msg;
  90. union {
  91. struct ec_params_host_sleep_event req0;
  92. struct ec_params_host_sleep_event_v1 req1;
  93. struct ec_response_host_sleep_event_v1 resp1;
  94. } u;
  95. } __packed buf;
  96. memset(&buf, 0, sizeof(buf));
  97. if (ec_dev->host_sleep_v1) {
  98. buf.u.req1.sleep_event = sleep_event;
  99. buf.u.req1.suspend_params.sleep_timeout_ms =
  100. ec_dev->suspend_timeout_ms;
  101. buf.msg.outsize = sizeof(buf.u.req1);
  102. if ((sleep_event == HOST_SLEEP_EVENT_S3_RESUME) ||
  103. (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME))
  104. buf.msg.insize = sizeof(buf.u.resp1);
  105. buf.msg.version = 1;
  106. } else {
  107. buf.u.req0.sleep_event = sleep_event;
  108. buf.msg.outsize = sizeof(buf.u.req0);
  109. }
  110. buf.msg.command = EC_CMD_HOST_SLEEP_EVENT;
  111. ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
  112. /* Report failure to transition to system wide suspend with a warning. */
  113. if (ret >= 0 && ec_dev->host_sleep_v1 &&
  114. (sleep_event == HOST_SLEEP_EVENT_S0IX_RESUME ||
  115. sleep_event == HOST_SLEEP_EVENT_S3_RESUME)) {
  116. ec_dev->last_resume_result =
  117. buf.u.resp1.resume_response.sleep_transitions;
  118. WARN_ONCE(buf.u.resp1.resume_response.sleep_transitions &
  119. EC_HOST_RESUME_SLEEP_TIMEOUT,
  120. "EC detected sleep transition timeout. Total sleep transitions: %d",
  121. buf.u.resp1.resume_response.sleep_transitions &
  122. EC_HOST_RESUME_SLEEP_TRANSITIONS_MASK);
  123. }
  124. return ret;
  125. }
  126. static int cros_ec_ready_event(struct notifier_block *nb,
  127. unsigned long queued_during_suspend,
  128. void *_notify)
  129. {
  130. struct cros_ec_device *ec_dev = container_of(nb, struct cros_ec_device,
  131. notifier_ready);
  132. u32 host_event = cros_ec_get_host_event(ec_dev);
  133. if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INTERFACE_READY)) {
  134. mutex_lock(&ec_dev->lock);
  135. cros_ec_query_all(ec_dev);
  136. mutex_unlock(&ec_dev->lock);
  137. return NOTIFY_OK;
  138. }
  139. return NOTIFY_DONE;
  140. }
  141. /**
  142. * cros_ec_register() - Register a new ChromeOS EC, using the provided info.
  143. * @ec_dev: Device to register.
  144. *
  145. * Before calling this, allocate a pointer to a new device and then fill
  146. * in all the fields up to the --private-- marker.
  147. *
  148. * Return: 0 on success or negative error code.
  149. */
  150. int cros_ec_register(struct cros_ec_device *ec_dev)
  151. {
  152. struct device *dev = ec_dev->dev;
  153. int err = 0;
  154. BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
  155. ec_dev->max_request = sizeof(struct ec_params_hello);
  156. ec_dev->max_response = sizeof(struct ec_response_get_protocol_info);
  157. ec_dev->max_passthru = 0;
  158. ec_dev->ec = NULL;
  159. ec_dev->pd = NULL;
  160. ec_dev->suspend_timeout_ms = EC_HOST_SLEEP_TIMEOUT_DEFAULT;
  161. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  162. if (!ec_dev->din)
  163. return -ENOMEM;
  164. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  165. if (!ec_dev->dout)
  166. return -ENOMEM;
  167. mutex_init(&ec_dev->lock);
  168. err = cros_ec_query_all(ec_dev);
  169. if (err) {
  170. dev_err(dev, "Cannot identify the EC: error %d\n", err);
  171. return err;
  172. }
  173. if (ec_dev->irq > 0) {
  174. err = devm_request_threaded_irq(dev, ec_dev->irq,
  175. cros_ec_irq_handler,
  176. cros_ec_irq_thread,
  177. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  178. "chromeos-ec", ec_dev);
  179. if (err) {
  180. dev_err(dev, "Failed to request IRQ %d: %d\n",
  181. ec_dev->irq, err);
  182. return err;
  183. }
  184. }
  185. /* Register a platform device for the main EC instance */
  186. ec_dev->ec = platform_device_register_data(ec_dev->dev, "cros-ec-dev",
  187. PLATFORM_DEVID_AUTO, &ec_p,
  188. sizeof(struct cros_ec_platform));
  189. if (IS_ERR(ec_dev->ec)) {
  190. dev_err(ec_dev->dev,
  191. "Failed to create CrOS EC platform device\n");
  192. return PTR_ERR(ec_dev->ec);
  193. }
  194. if (ec_dev->max_passthru) {
  195. /*
  196. * Register a platform device for the PD behind the main EC.
  197. * We make the following assumptions:
  198. * - behind an EC, we have a pd
  199. * - only one device added.
  200. * - the EC is responsive at init time (it is not true for a
  201. * sensor hub).
  202. */
  203. ec_dev->pd = platform_device_register_data(ec_dev->dev,
  204. "cros-ec-dev",
  205. PLATFORM_DEVID_AUTO, &pd_p,
  206. sizeof(struct cros_ec_platform));
  207. if (IS_ERR(ec_dev->pd)) {
  208. dev_err(ec_dev->dev,
  209. "Failed to create CrOS PD platform device\n");
  210. err = PTR_ERR(ec_dev->pd);
  211. goto exit;
  212. }
  213. }
  214. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  215. err = devm_of_platform_populate(dev);
  216. if (err) {
  217. dev_err(dev, "Failed to register sub-devices\n");
  218. goto exit;
  219. }
  220. }
  221. /*
  222. * Clear sleep event - this will fail harmlessly on platforms that
  223. * don't implement the sleep event host command.
  224. */
  225. err = cros_ec_sleep_event(ec_dev, 0);
  226. if (err < 0)
  227. dev_dbg(ec_dev->dev, "Error %d clearing sleep event to ec\n",
  228. err);
  229. if (ec_dev->mkbp_event_supported) {
  230. /*
  231. * Register the notifier for EC_HOST_EVENT_INTERFACE_READY
  232. * event.
  233. */
  234. ec_dev->notifier_ready.notifier_call = cros_ec_ready_event;
  235. err = blocking_notifier_chain_register(&ec_dev->event_notifier,
  236. &ec_dev->notifier_ready);
  237. if (err)
  238. goto exit;
  239. }
  240. dev_info(dev, "Chrome EC device registered\n");
  241. /*
  242. * Unlock EC that may be waiting for AP to process MKBP events.
  243. * If the AP takes to long to answer, the EC would stop sending events.
  244. */
  245. if (ec_dev->mkbp_event_supported)
  246. cros_ec_irq_thread(0, ec_dev);
  247. return 0;
  248. exit:
  249. platform_device_unregister(ec_dev->ec);
  250. platform_device_unregister(ec_dev->pd);
  251. return err;
  252. }
  253. EXPORT_SYMBOL(cros_ec_register);
  254. /**
  255. * cros_ec_unregister() - Remove a ChromeOS EC.
  256. * @ec_dev: Device to unregister.
  257. *
  258. * Call this to deregister a ChromeOS EC, then clean up any private data.
  259. *
  260. * Return: 0 on success or negative error code.
  261. */
  262. void cros_ec_unregister(struct cros_ec_device *ec_dev)
  263. {
  264. if (ec_dev->pd)
  265. platform_device_unregister(ec_dev->pd);
  266. platform_device_unregister(ec_dev->ec);
  267. }
  268. EXPORT_SYMBOL(cros_ec_unregister);
  269. #ifdef CONFIG_PM_SLEEP
  270. /**
  271. * cros_ec_suspend() - Handle a suspend operation for the ChromeOS EC device.
  272. * @ec_dev: Device to suspend.
  273. *
  274. * This can be called by drivers to handle a suspend event.
  275. *
  276. * Return: 0 on success or negative error code.
  277. */
  278. int cros_ec_suspend(struct cros_ec_device *ec_dev)
  279. {
  280. struct device *dev = ec_dev->dev;
  281. int ret;
  282. u8 sleep_event;
  283. sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
  284. HOST_SLEEP_EVENT_S3_SUSPEND :
  285. HOST_SLEEP_EVENT_S0IX_SUSPEND;
  286. ret = cros_ec_sleep_event(ec_dev, sleep_event);
  287. if (ret < 0)
  288. dev_dbg(ec_dev->dev, "Error %d sending suspend event to ec\n",
  289. ret);
  290. if (device_may_wakeup(dev))
  291. ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
  292. else
  293. ec_dev->wake_enabled = false;
  294. disable_irq(ec_dev->irq);
  295. ec_dev->suspended = true;
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(cros_ec_suspend);
  299. static void cros_ec_report_events_during_suspend(struct cros_ec_device *ec_dev)
  300. {
  301. bool wake_event;
  302. while (ec_dev->mkbp_event_supported &&
  303. cros_ec_get_next_event(ec_dev, &wake_event, NULL) > 0) {
  304. blocking_notifier_call_chain(&ec_dev->event_notifier,
  305. 1, ec_dev);
  306. if (wake_event && device_may_wakeup(ec_dev->dev))
  307. pm_wakeup_event(ec_dev->dev, 0);
  308. }
  309. }
  310. /**
  311. * cros_ec_resume() - Handle a resume operation for the ChromeOS EC device.
  312. * @ec_dev: Device to resume.
  313. *
  314. * This can be called by drivers to handle a resume event.
  315. *
  316. * Return: 0 on success or negative error code.
  317. */
  318. int cros_ec_resume(struct cros_ec_device *ec_dev)
  319. {
  320. int ret;
  321. u8 sleep_event;
  322. ec_dev->suspended = false;
  323. enable_irq(ec_dev->irq);
  324. sleep_event = (!IS_ENABLED(CONFIG_ACPI) || pm_suspend_via_firmware()) ?
  325. HOST_SLEEP_EVENT_S3_RESUME :
  326. HOST_SLEEP_EVENT_S0IX_RESUME;
  327. ret = cros_ec_sleep_event(ec_dev, sleep_event);
  328. if (ret < 0)
  329. dev_dbg(ec_dev->dev, "Error %d sending resume event to ec\n",
  330. ret);
  331. if (ec_dev->wake_enabled)
  332. disable_irq_wake(ec_dev->irq);
  333. /*
  334. * Let the mfd devices know about events that occur during
  335. * suspend. This way the clients know what to do with them.
  336. */
  337. cros_ec_report_events_during_suspend(ec_dev);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL(cros_ec_resume);
  341. #endif
  342. MODULE_LICENSE("GPL");
  343. MODULE_DESCRIPTION("ChromeOS EC core driver");