Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / chrome / cros_usbpd_notify.c
blob7f36142ab12a8d1c729bfb6da038d74934168f36
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2020 Google LLC
5 * This driver serves as the receiver of cros_ec PD host events.
6 */
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 {
21 struct device *dev;
22 struct cros_ec_device *ec;
23 struct notifier_block nb;
26 /**
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
32 * for USB PD 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,
39 nb);
41 EXPORT_SYMBOL_GPL(cros_usbpd_register_notify);
43 /**
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 /**
57 * cros_ec_pd_command - Send a command to the EC.
59 * @ec_dev: EC device
60 * @command: EC command
61 * @outdata: EC command output data
62 * @outsize: Size of outdata
63 * @indata: EC command input data
64 * @insize: Size of indata
66 * Return: >= 0 on success, negative error number on failure.
68 static int cros_ec_pd_command(struct cros_ec_device *ec_dev,
69 int command,
70 uint8_t *outdata,
71 int outsize,
72 uint8_t *indata,
73 int insize)
75 struct cros_ec_command *msg;
76 int ret;
78 msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
79 if (!msg)
80 return -ENOMEM;
82 msg->command = command;
83 msg->outsize = outsize;
84 msg->insize = insize;
86 if (outsize)
87 memcpy(msg->data, outdata, outsize);
89 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
90 if (ret < 0)
91 goto error;
93 if (insize)
94 memcpy(indata, msg->data, insize);
95 error:
96 kfree(msg);
97 return ret;
100 static void cros_usbpd_get_event_and_notify(struct device *dev,
101 struct cros_ec_device *ec_dev)
103 struct ec_response_host_event_status host_event_status;
104 u32 event = 0;
105 int ret;
108 * We still send a 0 event out to older devices which don't
109 * have the updated device heirarchy.
111 if (!ec_dev) {
112 dev_dbg(dev,
113 "EC device inaccessible; sending 0 event status.\n");
114 goto send_notify;
117 /* Check for PD host events on EC. */
118 ret = cros_ec_pd_command(ec_dev, EC_CMD_PD_HOST_EVENT_STATUS,
119 NULL, 0,
120 (uint8_t *)&host_event_status,
121 sizeof(host_event_status));
122 if (ret < 0) {
123 dev_warn(dev, "Can't get host event status (err: %d)\n", ret);
124 goto send_notify;
127 event = host_event_status.status;
129 send_notify:
130 blocking_notifier_call_chain(&cros_usbpd_notifier_list, event, NULL);
133 #ifdef CONFIG_ACPI
135 static void cros_usbpd_notify_acpi(acpi_handle device, u32 event, void *data)
137 struct cros_usbpd_notify_data *pdnotify = data;
139 cros_usbpd_get_event_and_notify(pdnotify->dev, pdnotify->ec);
142 static int cros_usbpd_notify_probe_acpi(struct platform_device *pdev)
144 struct cros_usbpd_notify_data *pdnotify;
145 struct device *dev = &pdev->dev;
146 struct acpi_device *adev;
147 struct cros_ec_device *ec_dev;
148 acpi_status status;
150 adev = ACPI_COMPANION(dev);
152 pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
153 if (!pdnotify)
154 return -ENOMEM;
156 /* Get the EC device pointer needed to talk to the EC. */
157 ec_dev = dev_get_drvdata(dev->parent);
158 if (!ec_dev) {
160 * We continue even for older devices which don't have the
161 * correct device heirarchy, namely, GOOG0003 is a child
162 * of GOOG0004.
164 dev_warn(dev, "Couldn't get Chrome EC device pointer.\n");
167 pdnotify->dev = dev;
168 pdnotify->ec = ec_dev;
170 status = acpi_install_notify_handler(adev->handle,
171 ACPI_ALL_NOTIFY,
172 cros_usbpd_notify_acpi,
173 pdnotify);
174 if (ACPI_FAILURE(status)) {
175 dev_warn(dev, "Failed to register notify handler %08x\n",
176 status);
177 return -EINVAL;
180 return 0;
183 static int cros_usbpd_notify_remove_acpi(struct platform_device *pdev)
185 struct device *dev = &pdev->dev;
186 struct acpi_device *adev = ACPI_COMPANION(dev);
188 acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
189 cros_usbpd_notify_acpi);
191 return 0;
194 static const struct acpi_device_id cros_usbpd_notify_acpi_device_ids[] = {
195 { ACPI_DRV_NAME, 0 },
198 MODULE_DEVICE_TABLE(acpi, cros_usbpd_notify_acpi_device_ids);
200 static struct platform_driver cros_usbpd_notify_acpi_driver = {
201 .driver = {
202 .name = DRV_NAME_PLAT_ACPI,
203 .acpi_match_table = cros_usbpd_notify_acpi_device_ids,
205 .probe = cros_usbpd_notify_probe_acpi,
206 .remove = cros_usbpd_notify_remove_acpi,
209 #endif /* CONFIG_ACPI */
211 static int cros_usbpd_notify_plat(struct notifier_block *nb,
212 unsigned long queued_during_suspend,
213 void *data)
215 struct cros_usbpd_notify_data *pdnotify = container_of(nb,
216 struct cros_usbpd_notify_data, nb);
217 struct cros_ec_device *ec_dev = (struct cros_ec_device *)data;
218 u32 host_event = cros_ec_get_host_event(ec_dev);
220 if (!host_event)
221 return NOTIFY_DONE;
223 if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU)) {
224 cros_usbpd_get_event_and_notify(pdnotify->dev, ec_dev);
225 return NOTIFY_OK;
227 return NOTIFY_DONE;
230 static int cros_usbpd_notify_probe_plat(struct platform_device *pdev)
232 struct device *dev = &pdev->dev;
233 struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
234 struct cros_usbpd_notify_data *pdnotify;
235 int ret;
237 pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
238 if (!pdnotify)
239 return -ENOMEM;
241 pdnotify->dev = dev;
242 pdnotify->ec = ecdev->ec_dev;
243 pdnotify->nb.notifier_call = cros_usbpd_notify_plat;
245 dev_set_drvdata(dev, pdnotify);
247 ret = blocking_notifier_chain_register(&ecdev->ec_dev->event_notifier,
248 &pdnotify->nb);
249 if (ret < 0) {
250 dev_err(dev, "Failed to register notifier\n");
251 return ret;
254 return 0;
257 static int cros_usbpd_notify_remove_plat(struct platform_device *pdev)
259 struct device *dev = &pdev->dev;
260 struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
261 struct cros_usbpd_notify_data *pdnotify =
262 (struct cros_usbpd_notify_data *)dev_get_drvdata(dev);
264 blocking_notifier_chain_unregister(&ecdev->ec_dev->event_notifier,
265 &pdnotify->nb);
267 return 0;
270 static struct platform_driver cros_usbpd_notify_plat_driver = {
271 .driver = {
272 .name = DRV_NAME,
274 .probe = cros_usbpd_notify_probe_plat,
275 .remove = cros_usbpd_notify_remove_plat,
278 static int __init cros_usbpd_notify_init(void)
280 int ret;
282 ret = platform_driver_register(&cros_usbpd_notify_plat_driver);
283 if (ret < 0)
284 return ret;
286 #ifdef CONFIG_ACPI
287 platform_driver_register(&cros_usbpd_notify_acpi_driver);
288 #endif
289 return 0;
292 static void __exit cros_usbpd_notify_exit(void)
294 #ifdef CONFIG_ACPI
295 platform_driver_unregister(&cros_usbpd_notify_acpi_driver);
296 #endif
297 platform_driver_unregister(&cros_usbpd_notify_plat_driver);
300 module_init(cros_usbpd_notify_init);
301 module_exit(cros_usbpd_notify_exit);
303 MODULE_LICENSE("GPL");
304 MODULE_DESCRIPTION("ChromeOS power delivery notifier device");
305 MODULE_AUTHOR("Jon Flatley <jflat@chromium.org>");
306 MODULE_ALIAS("platform:" DRV_NAME);