cros_ec.c 14 KB

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