Linux Audio

Check our new training course

Loading...
v6.8
  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
  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>
 13
 14#define DRV_NAME "cros-usbpd-notify"
 15#define DRV_NAME_PLAT_ACPI "cros-usbpd-notify-acpi"
 16#define ACPI_DRV_NAME "GOOG0003"
 17
 18static BLOCKING_NOTIFIER_HEAD(cros_usbpd_notifier_list);
 19
 20struct cros_usbpd_notify_data {
 21	struct device *dev;
 22	struct cros_ec_device *ec;
 23	struct notifier_block nb;
 24};
 25
 26/**
 27 * cros_usbpd_register_notify - Register a notifier callback for PD events.
 28 * @nb: Notifier block pointer to register
 29 *
 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.
 33 *
 34 * Return: 0 on success or negative error code.
 35 */
 36int cros_usbpd_register_notify(struct notifier_block *nb)
 37{
 38	return blocking_notifier_chain_register(&cros_usbpd_notifier_list,
 39						nb);
 40}
 41EXPORT_SYMBOL_GPL(cros_usbpd_register_notify);
 42
 43/**
 44 * cros_usbpd_unregister_notify - Unregister notifier callback for PD events.
 45 * @nb: Notifier block pointer to unregister
 46 *
 47 * Unregister a notifier callback that was previously registered with
 48 * cros_usbpd_register_notify().
 49 */
 50void cros_usbpd_unregister_notify(struct notifier_block *nb)
 51{
 52	blocking_notifier_chain_unregister(&cros_usbpd_notifier_list, nb);
 53}
 54EXPORT_SYMBOL_GPL(cros_usbpd_unregister_notify);
 55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56static void cros_usbpd_get_event_and_notify(struct device  *dev,
 57					    struct cros_ec_device *ec_dev)
 58{
 59	struct ec_response_host_event_status host_event_status;
 60	u32 event = 0;
 61	int ret;
 62
 63	/*
 64	 * We still send a 0 event out to older devices which don't
 65	 * have the updated device heirarchy.
 66	 */
 67	if (!ec_dev) {
 68		dev_dbg(dev,
 69			"EC device inaccessible; sending 0 event status.\n");
 70		goto send_notify;
 71	}
 72
 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));
 
 
 76	if (ret < 0) {
 77		dev_warn(dev, "Can't get host event status (err: %d)\n", ret);
 78		goto send_notify;
 79	}
 80
 81	event = host_event_status.status;
 82
 83send_notify:
 84	blocking_notifier_call_chain(&cros_usbpd_notifier_list, event, NULL);
 85}
 86
 87#ifdef CONFIG_ACPI
 88
 89static void cros_usbpd_notify_acpi(acpi_handle device, u32 event, void *data)
 90{
 91	struct cros_usbpd_notify_data *pdnotify = data;
 92
 93	cros_usbpd_get_event_and_notify(pdnotify->dev, pdnotify->ec);
 94}
 95
 96static int cros_usbpd_notify_probe_acpi(struct platform_device *pdev)
 97{
 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;
102	acpi_status status;
103
104	adev = ACPI_COMPANION(dev);
105
106	pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
107	if (!pdnotify)
108		return -ENOMEM;
109
110	/* Get the EC device pointer needed to talk to the EC. */
111	ec_dev = dev_get_drvdata(dev->parent);
112	if (!ec_dev) {
113		/*
114		 * We continue even for older devices which don't have the
115		 * correct device heirarchy, namely, GOOG0003 is a child
116		 * of GOOG0004.
117		 */
118		dev_warn(dev, "Couldn't get Chrome EC device pointer.\n");
119	}
120
121	pdnotify->dev = dev;
122	pdnotify->ec = ec_dev;
123
124	status = acpi_install_notify_handler(adev->handle,
125					     ACPI_ALL_NOTIFY,
126					     cros_usbpd_notify_acpi,
127					     pdnotify);
128	if (ACPI_FAILURE(status)) {
129		dev_warn(dev, "Failed to register notify handler %08x\n",
130			 status);
131		return -EINVAL;
132	}
133
134	return 0;
135}
136
137static void cros_usbpd_notify_remove_acpi(struct platform_device *pdev)
138{
139	struct device *dev = &pdev->dev;
140	struct acpi_device *adev = ACPI_COMPANION(dev);
141
142	acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
143				   cros_usbpd_notify_acpi);
 
 
144}
145
146static const struct acpi_device_id cros_usbpd_notify_acpi_device_ids[] = {
147	{ ACPI_DRV_NAME, 0 },
148	{ }
149};
150MODULE_DEVICE_TABLE(acpi, cros_usbpd_notify_acpi_device_ids);
151
152static struct platform_driver cros_usbpd_notify_acpi_driver = {
153	.driver = {
154		.name = DRV_NAME_PLAT_ACPI,
155		.acpi_match_table = cros_usbpd_notify_acpi_device_ids,
156	},
157	.probe = cros_usbpd_notify_probe_acpi,
158	.remove_new = cros_usbpd_notify_remove_acpi,
159};
160
161#endif /* CONFIG_ACPI */
162
163static int cros_usbpd_notify_plat(struct notifier_block *nb,
164				  unsigned long queued_during_suspend,
165				  void *data)
166{
167	struct cros_usbpd_notify_data *pdnotify = container_of(nb,
168			struct cros_usbpd_notify_data, nb);
169	struct cros_ec_device *ec_dev = (struct cros_ec_device *)data;
170	u32 host_event = cros_ec_get_host_event(ec_dev);
171
172	if (!host_event)
173		return NOTIFY_DONE;
174
175	if (host_event & (EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
176			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_USB_MUX))) {
177		cros_usbpd_get_event_and_notify(pdnotify->dev, ec_dev);
178		return NOTIFY_OK;
179	}
180	return NOTIFY_DONE;
181}
182
183static int cros_usbpd_notify_probe_plat(struct platform_device *pdev)
184{
185	struct device *dev = &pdev->dev;
186	struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
187	struct cros_usbpd_notify_data *pdnotify;
188	int ret;
189
190	pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
191	if (!pdnotify)
192		return -ENOMEM;
193
194	pdnotify->dev = dev;
195	pdnotify->ec = ecdev->ec_dev;
196	pdnotify->nb.notifier_call = cros_usbpd_notify_plat;
197
198	dev_set_drvdata(dev, pdnotify);
199
200	ret = blocking_notifier_chain_register(&ecdev->ec_dev->event_notifier,
201					       &pdnotify->nb);
202	if (ret < 0) {
203		dev_err(dev, "Failed to register notifier\n");
204		return ret;
205	}
206
207	return 0;
208}
209
210static void cros_usbpd_notify_remove_plat(struct platform_device *pdev)
211{
212	struct device *dev = &pdev->dev;
213	struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
214	struct cros_usbpd_notify_data *pdnotify =
215		(struct cros_usbpd_notify_data *)dev_get_drvdata(dev);
216
217	blocking_notifier_chain_unregister(&ecdev->ec_dev->event_notifier,
218					   &pdnotify->nb);
 
 
219}
220
221static struct platform_driver cros_usbpd_notify_plat_driver = {
222	.driver = {
223		.name = DRV_NAME,
224	},
225	.probe = cros_usbpd_notify_probe_plat,
226	.remove_new = cros_usbpd_notify_remove_plat,
227};
228
229static int __init cros_usbpd_notify_init(void)
230{
231	int ret;
232
233	ret = platform_driver_register(&cros_usbpd_notify_plat_driver);
234	if (ret < 0)
235		return ret;
236
237#ifdef CONFIG_ACPI
238	ret = platform_driver_register(&cros_usbpd_notify_acpi_driver);
239	if (ret) {
240		platform_driver_unregister(&cros_usbpd_notify_plat_driver);
241		return ret;
242	}
243#endif
244	return 0;
245}
246
247static void __exit cros_usbpd_notify_exit(void)
248{
249#ifdef CONFIG_ACPI
250	platform_driver_unregister(&cros_usbpd_notify_acpi_driver);
251#endif
252	platform_driver_unregister(&cros_usbpd_notify_plat_driver);
253}
254
255module_init(cros_usbpd_notify_init);
256module_exit(cros_usbpd_notify_exit);
257
258MODULE_LICENSE("GPL");
259MODULE_DESCRIPTION("ChromeOS power delivery notifier device");
260MODULE_AUTHOR("Jon Flatley <jflat@chromium.org>");
261MODULE_ALIAS("platform:" DRV_NAME);
v5.14.15
  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
  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>
 13
 14#define DRV_NAME "cros-usbpd-notify"
 15#define DRV_NAME_PLAT_ACPI "cros-usbpd-notify-acpi"
 16#define ACPI_DRV_NAME "GOOG0003"
 17
 18static BLOCKING_NOTIFIER_HEAD(cros_usbpd_notifier_list);
 19
 20struct cros_usbpd_notify_data {
 21	struct device *dev;
 22	struct cros_ec_device *ec;
 23	struct notifier_block nb;
 24};
 25
 26/**
 27 * cros_usbpd_register_notify - Register a notifier callback for PD events.
 28 * @nb: Notifier block pointer to register
 29 *
 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.
 33 *
 34 * Return: 0 on success or negative error code.
 35 */
 36int cros_usbpd_register_notify(struct notifier_block *nb)
 37{
 38	return blocking_notifier_chain_register(&cros_usbpd_notifier_list,
 39						nb);
 40}
 41EXPORT_SYMBOL_GPL(cros_usbpd_register_notify);
 42
 43/**
 44 * cros_usbpd_unregister_notify - Unregister notifier callback for PD events.
 45 * @nb: Notifier block pointer to unregister
 46 *
 47 * Unregister a notifier callback that was previously registered with
 48 * cros_usbpd_register_notify().
 49 */
 50void cros_usbpd_unregister_notify(struct notifier_block *nb)
 51{
 52	blocking_notifier_chain_unregister(&cros_usbpd_notifier_list, nb);
 53}
 54EXPORT_SYMBOL_GPL(cros_usbpd_unregister_notify);
 55
 56/**
 57 * cros_ec_pd_command - Send a command to the EC.
 58 *
 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
 65 *
 66 * Return: >= 0 on success, negative error number on failure.
 67 */
 68static 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)
 74{
 75	struct cros_ec_command *msg;
 76	int ret;
 77
 78	msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
 79	if (!msg)
 80		return -ENOMEM;
 81
 82	msg->command = command;
 83	msg->outsize = outsize;
 84	msg->insize = insize;
 85
 86	if (outsize)
 87		memcpy(msg->data, outdata, outsize);
 88
 89	ret = cros_ec_cmd_xfer_status(ec_dev, msg);
 90	if (ret < 0)
 91		goto error;
 92
 93	if (insize)
 94		memcpy(indata, msg->data, insize);
 95error:
 96	kfree(msg);
 97	return ret;
 98}
 99
100static void cros_usbpd_get_event_and_notify(struct device  *dev,
101					    struct cros_ec_device *ec_dev)
102{
103	struct ec_response_host_event_status host_event_status;
104	u32 event = 0;
105	int ret;
106
107	/*
108	 * We still send a 0 event out to older devices which don't
109	 * have the updated device heirarchy.
110	 */
111	if (!ec_dev) {
112		dev_dbg(dev,
113			"EC device inaccessible; sending 0 event status.\n");
114		goto send_notify;
115	}
116
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;
125	}
126
127	event = host_event_status.status;
128
129send_notify:
130	blocking_notifier_call_chain(&cros_usbpd_notifier_list, event, NULL);
131}
132
133#ifdef CONFIG_ACPI
134
135static void cros_usbpd_notify_acpi(acpi_handle device, u32 event, void *data)
136{
137	struct cros_usbpd_notify_data *pdnotify = data;
138
139	cros_usbpd_get_event_and_notify(pdnotify->dev, pdnotify->ec);
140}
141
142static int cros_usbpd_notify_probe_acpi(struct platform_device *pdev)
143{
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;
149
150	adev = ACPI_COMPANION(dev);
151
152	pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
153	if (!pdnotify)
154		return -ENOMEM;
155
156	/* Get the EC device pointer needed to talk to the EC. */
157	ec_dev = dev_get_drvdata(dev->parent);
158	if (!ec_dev) {
159		/*
160		 * We continue even for older devices which don't have the
161		 * correct device heirarchy, namely, GOOG0003 is a child
162		 * of GOOG0004.
163		 */
164		dev_warn(dev, "Couldn't get Chrome EC device pointer.\n");
165	}
166
167	pdnotify->dev = dev;
168	pdnotify->ec = ec_dev;
169
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;
178	}
179
180	return 0;
181}
182
183static int cros_usbpd_notify_remove_acpi(struct platform_device *pdev)
184{
185	struct device *dev = &pdev->dev;
186	struct acpi_device *adev = ACPI_COMPANION(dev);
187
188	acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
189				   cros_usbpd_notify_acpi);
190
191	return 0;
192}
193
194static const struct acpi_device_id cros_usbpd_notify_acpi_device_ids[] = {
195	{ ACPI_DRV_NAME, 0 },
196	{ }
197};
198MODULE_DEVICE_TABLE(acpi, cros_usbpd_notify_acpi_device_ids);
199
200static 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,
204	},
205	.probe = cros_usbpd_notify_probe_acpi,
206	.remove = cros_usbpd_notify_remove_acpi,
207};
208
209#endif /* CONFIG_ACPI */
210
211static int cros_usbpd_notify_plat(struct notifier_block *nb,
212				  unsigned long queued_during_suspend,
213				  void *data)
214{
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);
219
220	if (!host_event)
221		return NOTIFY_DONE;
222
223	if (host_event & (EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
224			  EC_HOST_EVENT_MASK(EC_HOST_EVENT_USB_MUX))) {
225		cros_usbpd_get_event_and_notify(pdnotify->dev, ec_dev);
226		return NOTIFY_OK;
227	}
228	return NOTIFY_DONE;
229}
230
231static int cros_usbpd_notify_probe_plat(struct platform_device *pdev)
232{
233	struct device *dev = &pdev->dev;
234	struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
235	struct cros_usbpd_notify_data *pdnotify;
236	int ret;
237
238	pdnotify = devm_kzalloc(dev, sizeof(*pdnotify), GFP_KERNEL);
239	if (!pdnotify)
240		return -ENOMEM;
241
242	pdnotify->dev = dev;
243	pdnotify->ec = ecdev->ec_dev;
244	pdnotify->nb.notifier_call = cros_usbpd_notify_plat;
245
246	dev_set_drvdata(dev, pdnotify);
247
248	ret = blocking_notifier_chain_register(&ecdev->ec_dev->event_notifier,
249					       &pdnotify->nb);
250	if (ret < 0) {
251		dev_err(dev, "Failed to register notifier\n");
252		return ret;
253	}
254
255	return 0;
256}
257
258static int cros_usbpd_notify_remove_plat(struct platform_device *pdev)
259{
260	struct device *dev = &pdev->dev;
261	struct cros_ec_dev *ecdev = dev_get_drvdata(dev->parent);
262	struct cros_usbpd_notify_data *pdnotify =
263		(struct cros_usbpd_notify_data *)dev_get_drvdata(dev);
264
265	blocking_notifier_chain_unregister(&ecdev->ec_dev->event_notifier,
266					   &pdnotify->nb);
267
268	return 0;
269}
270
271static struct platform_driver cros_usbpd_notify_plat_driver = {
272	.driver = {
273		.name = DRV_NAME,
274	},
275	.probe = cros_usbpd_notify_probe_plat,
276	.remove = cros_usbpd_notify_remove_plat,
277};
278
279static int __init cros_usbpd_notify_init(void)
280{
281	int ret;
282
283	ret = platform_driver_register(&cros_usbpd_notify_plat_driver);
284	if (ret < 0)
285		return ret;
286
287#ifdef CONFIG_ACPI
288	platform_driver_register(&cros_usbpd_notify_acpi_driver);
 
 
 
 
289#endif
290	return 0;
291}
292
293static void __exit cros_usbpd_notify_exit(void)
294{
295#ifdef CONFIG_ACPI
296	platform_driver_unregister(&cros_usbpd_notify_acpi_driver);
297#endif
298	platform_driver_unregister(&cros_usbpd_notify_plat_driver);
299}
300
301module_init(cros_usbpd_notify_init);
302module_exit(cros_usbpd_notify_exit);
303
304MODULE_LICENSE("GPL");
305MODULE_DESCRIPTION("ChromeOS power delivery notifier device");
306MODULE_AUTHOR("Jon Flatley <jflat@chromium.org>");
307MODULE_ALIAS("platform:" DRV_NAME);