cros_ec.c 13 KB

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