1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2020 Google LLC
5 * This driver serves as the receiver of cros_ec PD host events.
8 #include <linux/acpi.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>
14 #define DRV_NAME "cros-usbpd-notify"
15 #define DRV_NAME_PLAT_ACPI "cros-usbpd-notify-acpi"
16 #define ACPI_DRV_NAME "GOOG0003"
18 static BLOCKING_NOTIFIER_HEAD(cros_usbpd_notifier_list);
20 struct cros_usbpd_notify_data {
22 struct cros_ec_device *ec;
23 struct notifier_block nb;
27 * cros_usbpd_register_notify - Register a notifier callback for PD events.
28 * @nb: Notifier block pointer to register
30 * On ACPI platforms this corresponds to host events on the ECPD
31 * "GOOG0003" ACPI device. On non-ACPI platforms this will filter mkbp events
34 * Return: 0 on success or negative error code.
36 int cros_usbpd_register_notify(struct notifier_block *nb)
38 return blocking_notifier_chain_register(&cros_usbpd_notifier_list,
41 EXPORT_SYMBOL_GPL(cros_usbpd_register_notify);
44 * cros_usbpd_unregister_notify - Unregister notifier callback for PD events.
45 * @nb: Notifier block pointer to unregister
47 * Unregister a notifier callback that was previously registered with
48 * cros_usbpd_register_notify().
50 void cros_usbpd_unregister_notify(struct notifier_block *nb)
52 blocking_notifier_chain_unregister(&cros_usbpd_notifier_list, nb);
54 EXPORT_SYMBOL_GPL(cros_usbpd_unregister_notify);
56 static void cros_usbpd_get_event_and_notify(struct device *dev,
57 struct cros_ec_device *ec_dev)
59 struct ec_response_host_event_status host_event_status;
64 * We still send a 0 event out to older devices which don't
65 * have the updated device heirarchy.
69 "EC device inaccessible; sending 0 event status.\n");
73 /* Check for PD host events on EC. */
74 ret = cros_ec_cmd(ec_dev, 0, EC_CMD_PD_HOST_EVENT_STATUS,
75 NULL, 0, &host_event_status, sizeof(host_event_status));
77 dev_warn(dev, "Can't get host event status (err: %d)\n", ret);
81 event = host_event_status.status;
84 blocking_notifier_call_chain(&cros_usbpd_notifier_list, event, NULL);
89 static void cros_usbpd_notify_acpi(acpi_handle device, u32 event, void *data)
91 struct cros_usbpd_notify_data *pdnotify = data;
93 cros_usbpd_get_event_and_notify(pdnotify->dev, pdnotify->ec);
96 static int cros_usbpd_notify_probe_acpi(struct platform_device *pdev)
98 struct cros_usbpd_notify_data *pdnotify;
99 struct device *dev = &pdev->dev;
100 struct acpi_device *adev;
101 struct cros_ec_device *ec_dev;
104 adev = ACPI_COMPANION(dev);
106 pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
110 /* Get the EC device pointer needed to talk to the EC. */
111 ec_dev = dev_get_drvdata(dev->parent);
114 * We continue even for older devices which don't have the
115 * correct device heirarchy, namely, GOOG0003 is a child
118 dev_warn(dev, "Couldn't get Chrome EC device pointer.\n");
122 pdnotify->ec = ec_dev;
124 status = acpi_install_notify_handler(adev->handle,
126 cros_usbpd_notify_acpi,
128 if (ACPI_FAILURE(status)) {
129 dev_warn(dev, "Failed to register notify handler %08x\n",
137 static int cros_usbpd_notify_remove_acpi(struct platform_device *pdev)
139 struct device *dev = &pdev->dev;
140 struct acpi_device *adev = ACPI_COMPANION(dev);
142 acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
143 cros_usbpd_notify_acpi);
148 static const struct acpi_device_id cros_usbpd_notify_acpi_device_ids[] = {
149 { ACPI_DRV_NAME, 0 },
152 MODULE_DEVICE_TABLE(acpi, cros_usbpd_notify_acpi_device_ids);
154 static struct platform_driver cros_usbpd_notify_acpi_driver = {
156 .name = DRV_NAME_PLAT_ACPI,
157 .acpi_match_table = cros_usbpd_notify_acpi_device_ids,
159 .probe = cros_usbpd_notify_probe_acpi,
160 .remove = cros_usbpd_notify_remove_acpi,
163 #endif /* CONFIG_ACPI */
165 static int cros_usbpd_notify_plat(struct notifier_block *nb,
166 unsigned long queued_during_suspend,
169 struct cros_usbpd_notify_data *pdnotify = container_of(nb,
170 struct cros_usbpd_notify_data, nb);
171 struct cros_ec_device *ec_dev = (struct cros_ec_device *)data;
172 u32 host_event = cros_ec_get_host_event(ec_dev);
177 if (host_event & (EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
178 EC_HOST_EVENT_MASK(EC_HOST_EVENT_USB_MUX))) {
179 cros_usbpd_get_event_and_notify(pdnotify->dev, ec_dev);
185 static int cros_usbpd_notify_probe_plat(struct platform_device *pdev)
187 struct device *dev = &pdev->dev;
188 struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
189 struct cros_usbpd_notify_data *pdnotify;
192 pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
197 pdnotify->ec = ecdev->ec_dev;
198 pdnotify->nb.notifier_call = cros_usbpd_notify_plat;
200 dev_set_drvdata(dev, pdnotify);
202 ret = blocking_notifier_chain_register(&ecdev->ec_dev->event_notifier,
205 dev_err(dev, "Failed to register notifier\n");
212 static int cros_usbpd_notify_remove_plat(struct platform_device *pdev)
214 struct device *dev = &pdev->dev;
215 struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
216 struct cros_usbpd_notify_data *pdnotify =
217 (struct cros_usbpd_notify_data *)dev_get_drvdata(dev);
219 blocking_notifier_chain_unregister(&ecdev->ec_dev->event_notifier,
225 static struct platform_driver cros_usbpd_notify_plat_driver = {
229 .probe = cros_usbpd_notify_probe_plat,
230 .remove = cros_usbpd_notify_remove_plat,
233 static int __init cros_usbpd_notify_init(void)
237 ret = platform_driver_register(&cros_usbpd_notify_plat_driver);
242 ret = platform_driver_register(&cros_usbpd_notify_acpi_driver);
244 platform_driver_unregister(&cros_usbpd_notify_plat_driver);
251 static void __exit cros_usbpd_notify_exit(void)
254 platform_driver_unregister(&cros_usbpd_notify_acpi_driver);
256 platform_driver_unregister(&cros_usbpd_notify_plat_driver);
259 module_init(cros_usbpd_notify_init);
260 module_exit(cros_usbpd_notify_exit);
262 MODULE_LICENSE("GPL");
263 MODULE_DESCRIPTION("ChromeOS power delivery notifier device");
265 MODULE_ALIAS("platform:" DRV_NAME);