cros_usbpd_notify.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2020 Google LLC
  4. *
  5. * This driver serves as the receiver of cros_ec PD host events.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_data/cros_ec_proto.h>
  11. #include <linux/platform_data/cros_usbpd_notify.h>
  12. #include <linux/platform_device.h>
  13. #define DRV_NAME "cros-usbpd-notify"
  14. #define DRV_NAME_PLAT_ACPI "cros-usbpd-notify-acpi"
  15. #define ACPI_DRV_NAME "GOOG0003"
  16. static BLOCKING_NOTIFIER_HEAD(cros_usbpd_notifier_list);
  17. struct cros_usbpd_notify_data {
  18. struct device *dev;
  19. struct cros_ec_device *ec;
  20. struct notifier_block nb;
  21. };
  22. /**
  23. * cros_usbpd_register_notify - Register a notifier callback for PD events.
  24. * @nb: Notifier block pointer to register
  25. *
  26. * On ACPI platforms this corresponds to host events on the ECPD
  27. * "GOOG0003" ACPI device. On non-ACPI platforms this will filter mkbp events
  28. * for USB PD events.
  29. *
  30. * Return: 0 on success or negative error code.
  31. */
  32. int cros_usbpd_register_notify(struct notifier_block *nb)
  33. {
  34. return blocking_notifier_chain_register(&cros_usbpd_notifier_list,
  35. nb);
  36. }
  37. EXPORT_SYMBOL_GPL(cros_usbpd_register_notify);
  38. /**
  39. * cros_usbpd_unregister_notify - Unregister notifier callback for PD events.
  40. * @nb: Notifier block pointer to unregister
  41. *
  42. * Unregister a notifier callback that was previously registered with
  43. * cros_usbpd_register_notify().
  44. */
  45. void cros_usbpd_unregister_notify(struct notifier_block *nb)
  46. {
  47. blocking_notifier_chain_unregister(&cros_usbpd_notifier_list, nb);
  48. }
  49. EXPORT_SYMBOL_GPL(cros_usbpd_unregister_notify);
  50. static void cros_usbpd_get_event_and_notify(struct device *dev,
  51. struct cros_ec_device *ec_dev)
  52. {
  53. struct ec_response_host_event_status host_event_status;
  54. u32 event = 0;
  55. int ret;
  56. /*
  57. * We still send a 0 event out to older devices which don't
  58. * have the updated device heirarchy.
  59. */
  60. if (!ec_dev) {
  61. dev_dbg(dev,
  62. "EC device inaccessible; sending 0 event status.\n");
  63. goto send_notify;
  64. }
  65. /* Check for PD host events on EC. */
  66. ret = cros_ec_cmd(ec_dev, 0, EC_CMD_PD_HOST_EVENT_STATUS,
  67. NULL, 0, &host_event_status, sizeof(host_event_status));
  68. if (ret < 0) {
  69. dev_warn(dev, "Can't get host event status (err: %d)\n", ret);
  70. goto send_notify;
  71. }
  72. event = host_event_status.status;
  73. send_notify:
  74. blocking_notifier_call_chain(&cros_usbpd_notifier_list, event, NULL);
  75. }
  76. #ifdef CONFIG_ACPI
  77. static void cros_usbpd_notify_acpi(acpi_handle device, u32 event, void *data)
  78. {
  79. struct cros_usbpd_notify_data *pdnotify = data;
  80. cros_usbpd_get_event_and_notify(pdnotify->dev, pdnotify->ec);
  81. }
  82. static int cros_usbpd_notify_probe_acpi(struct platform_device *pdev)
  83. {
  84. struct cros_usbpd_notify_data *pdnotify;
  85. struct device *dev = &pdev->dev;
  86. struct acpi_device *adev;
  87. struct cros_ec_device *ec_dev;
  88. acpi_status status;
  89. adev = ACPI_COMPANION(dev);
  90. pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
  91. if (!pdnotify)
  92. return -ENOMEM;
  93. /* Get the EC device pointer needed to talk to the EC. */
  94. ec_dev = dev_get_drvdata(dev->parent);
  95. if (!ec_dev) {
  96. /*
  97. * We continue even for older devices which don't have the
  98. * correct device heirarchy, namely, GOOG0003 is a child
  99. * of GOOG0004.
  100. */
  101. dev_warn(dev, "Couldn't get Chrome EC device pointer.\n");
  102. }
  103. pdnotify->dev = dev;
  104. pdnotify->ec = ec_dev;
  105. status = acpi_install_notify_handler(adev->handle,
  106. ACPI_ALL_NOTIFY,
  107. cros_usbpd_notify_acpi,
  108. pdnotify);
  109. if (ACPI_FAILURE(status)) {
  110. dev_warn(dev, "Failed to register notify handler %08x\n",
  111. status);
  112. return -EINVAL;
  113. }
  114. return 0;
  115. }
  116. static void cros_usbpd_notify_remove_acpi(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. struct acpi_device *adev = ACPI_COMPANION(dev);
  120. acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
  121. cros_usbpd_notify_acpi);
  122. }
  123. static const struct acpi_device_id cros_usbpd_notify_acpi_device_ids[] = {
  124. { ACPI_DRV_NAME, 0 },
  125. { }
  126. };
  127. MODULE_DEVICE_TABLE(acpi, cros_usbpd_notify_acpi_device_ids);
  128. static struct platform_driver cros_usbpd_notify_acpi_driver = {
  129. .driver = {
  130. .name = DRV_NAME_PLAT_ACPI,
  131. .acpi_match_table = cros_usbpd_notify_acpi_device_ids,
  132. },
  133. .probe = cros_usbpd_notify_probe_acpi,
  134. .remove_new = cros_usbpd_notify_remove_acpi,
  135. };
  136. #endif /* CONFIG_ACPI */
  137. static int cros_usbpd_notify_plat(struct notifier_block *nb,
  138. unsigned long queued_during_suspend,
  139. void *data)
  140. {
  141. struct cros_usbpd_notify_data *pdnotify = container_of(nb,
  142. struct cros_usbpd_notify_data, nb);
  143. struct cros_ec_device *ec_dev = (struct cros_ec_device *)data;
  144. u32 host_event = cros_ec_get_host_event(ec_dev);
  145. if (!host_event)
  146. return NOTIFY_DONE;
  147. if (host_event & (EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
  148. EC_HOST_EVENT_MASK(EC_HOST_EVENT_USB_MUX))) {
  149. cros_usbpd_get_event_and_notify(pdnotify->dev, ec_dev);
  150. return NOTIFY_OK;
  151. }
  152. return NOTIFY_DONE;
  153. }
  154. static int cros_usbpd_notify_probe_plat(struct platform_device *pdev)
  155. {
  156. struct device *dev = &pdev->dev;
  157. struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
  158. struct cros_usbpd_notify_data *pdnotify;
  159. int ret;
  160. pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
  161. if (!pdnotify)
  162. return -ENOMEM;
  163. pdnotify->dev = dev;
  164. pdnotify->ec = ecdev->ec_dev;
  165. pdnotify->nb.notifier_call = cros_usbpd_notify_plat;
  166. dev_set_drvdata(dev, pdnotify);
  167. ret = blocking_notifier_chain_register(&ecdev->ec_dev->event_notifier,
  168. &pdnotify->nb);
  169. if (ret < 0) {
  170. dev_err(dev, "Failed to register notifier\n");
  171. return ret;
  172. }
  173. return 0;
  174. }
  175. static void cros_usbpd_notify_remove_plat(struct platform_device *pdev)
  176. {
  177. struct device *dev = &pdev->dev;
  178. struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
  179. struct cros_usbpd_notify_data *pdnotify =
  180. (struct cros_usbpd_notify_data *)dev_get_drvdata(dev);
  181. blocking_notifier_chain_unregister(&ecdev->ec_dev->event_notifier,
  182. &pdnotify->nb);
  183. }
  184. static const struct platform_device_id cros_usbpd_notify_id[] = {
  185. { DRV_NAME, 0 },
  186. {}
  187. };
  188. MODULE_DEVICE_TABLE(platform, cros_usbpd_notify_id);
  189. static struct platform_driver cros_usbpd_notify_plat_driver = {
  190. .driver = {
  191. .name = DRV_NAME,
  192. },
  193. .probe = cros_usbpd_notify_probe_plat,
  194. .remove_new = cros_usbpd_notify_remove_plat,
  195. .id_table = cros_usbpd_notify_id,
  196. };
  197. static int __init cros_usbpd_notify_init(void)
  198. {
  199. int ret;
  200. ret = platform_driver_register(&cros_usbpd_notify_plat_driver);
  201. if (ret < 0)
  202. return ret;
  203. #ifdef CONFIG_ACPI
  204. ret = platform_driver_register(&cros_usbpd_notify_acpi_driver);
  205. if (ret) {
  206. platform_driver_unregister(&cros_usbpd_notify_plat_driver);
  207. return ret;
  208. }
  209. #endif
  210. return 0;
  211. }
  212. static void __exit cros_usbpd_notify_exit(void)
  213. {
  214. #ifdef CONFIG_ACPI
  215. platform_driver_unregister(&cros_usbpd_notify_acpi_driver);
  216. #endif
  217. platform_driver_unregister(&cros_usbpd_notify_plat_driver);
  218. }
  219. module_init(cros_usbpd_notify_init);
  220. module_exit(cros_usbpd_notify_exit);
  221. MODULE_LICENSE("GPL");
  222. MODULE_DESCRIPTION("ChromeOS power delivery notifier device");
  223. MODULE_AUTHOR("Jon Flatley <jflat@chromium.org>");