Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for onboard USB devices
  4 *
  5 * Copyright (c) 2022, Google LLC
  6 */
  7
  8#include <linux/clk.h>
  9#include <linux/device.h>
 10#include <linux/export.h>
 11#include <linux/err.h>
 12#include <linux/gpio/consumer.h>
 13#include <linux/init.h>
 14#include <linux/i2c.h>
 15#include <linux/kernel.h>
 16#include <linux/list.h>
 17#include <linux/module.h>
 18#include <linux/mutex.h>
 19#include <linux/of.h>
 20#include <linux/of_platform.h>
 21#include <linux/platform_device.h>
 22#include <linux/regulator/consumer.h>
 23#include <linux/slab.h>
 24#include <linux/suspend.h>
 25#include <linux/sysfs.h>
 26#include <linux/usb.h>
 27#include <linux/usb/hcd.h>
 28#include <linux/usb/onboard_dev.h>
 29#include <linux/workqueue.h>
 30
 31#include "onboard_usb_dev.h"
 32
 33/* USB5744 register offset and mask */
 34#define USB5744_CMD_ATTACH			0xAA
 35#define USB5744_CMD_ATTACH_LSB			0x56
 36#define USB5744_CMD_CREG_ACCESS			0x99
 37#define USB5744_CMD_CREG_ACCESS_LSB		0x37
 38#define USB5744_CREG_MEM_ADDR			0x00
 39#define USB5744_CREG_WRITE			0x00
 40#define USB5744_CREG_RUNTIMEFLAGS2		0x41
 41#define USB5744_CREG_RUNTIMEFLAGS2_LSB		0x1D
 42#define USB5744_CREG_BYPASS_UDC_SUSPEND		BIT(3)
 43
 44static void onboard_dev_attach_usb_driver(struct work_struct *work);
 45
 46static struct usb_device_driver onboard_dev_usbdev_driver;
 47static DECLARE_WORK(attach_usb_driver_work, onboard_dev_attach_usb_driver);
 48
 49/************************** Platform driver **************************/
 50
 51struct usbdev_node {
 52	struct usb_device *udev;
 53	struct list_head list;
 54};
 55
 56struct onboard_dev {
 57	struct regulator_bulk_data supplies[MAX_SUPPLIES];
 58	struct device *dev;
 59	const struct onboard_dev_pdata *pdata;
 60	struct gpio_desc *reset_gpio;
 61	bool always_powered_in_suspend;
 62	bool is_powered_on;
 63	bool going_away;
 64	struct list_head udev_list;
 65	struct mutex lock;
 66	struct clk *clk;
 67};
 68
 69static int onboard_dev_get_regulators(struct onboard_dev *onboard_dev)
 70{
 71	const char * const *supply_names = onboard_dev->pdata->supply_names;
 72	unsigned int num_supplies = onboard_dev->pdata->num_supplies;
 73	struct device *dev = onboard_dev->dev;
 74	unsigned int i;
 75	int err;
 76
 77	if (num_supplies > MAX_SUPPLIES)
 78		return dev_err_probe(dev, -EINVAL, "max %d supplies supported!\n",
 79				     MAX_SUPPLIES);
 80
 81	for (i = 0; i < num_supplies; i++)
 82		onboard_dev->supplies[i].supply = supply_names[i];
 83
 84	err = devm_regulator_bulk_get(dev, num_supplies, onboard_dev->supplies);
 85	if (err)
 86		dev_err(dev, "Failed to get regulator supplies: %pe\n",
 87			ERR_PTR(err));
 88
 89	return err;
 90}
 91
 92static int onboard_dev_power_on(struct onboard_dev *onboard_dev)
 93{
 94	int err;
 95
 96	err = clk_prepare_enable(onboard_dev->clk);
 97	if (err) {
 98		dev_err(onboard_dev->dev, "failed to enable clock: %pe\n",
 99			ERR_PTR(err));
100		return err;
101	}
102
103	err = regulator_bulk_enable(onboard_dev->pdata->num_supplies,
104				    onboard_dev->supplies);
105	if (err) {
106		dev_err(onboard_dev->dev, "failed to enable supplies: %pe\n",
107			ERR_PTR(err));
108		goto disable_clk;
109	}
110
111	fsleep(onboard_dev->pdata->reset_us);
112	gpiod_set_value_cansleep(onboard_dev->reset_gpio, 0);
113	fsleep(onboard_dev->pdata->power_on_delay_us);
114
115	onboard_dev->is_powered_on = true;
116
117	return 0;
118
119disable_clk:
120	clk_disable_unprepare(onboard_dev->clk);
121	return err;
122}
123
124static int onboard_dev_power_off(struct onboard_dev *onboard_dev)
125{
126	int err;
127
128	gpiod_set_value_cansleep(onboard_dev->reset_gpio, 1);
129
130	err = regulator_bulk_disable(onboard_dev->pdata->num_supplies,
131				     onboard_dev->supplies);
132	if (err) {
133		dev_err(onboard_dev->dev, "failed to disable supplies: %pe\n",
134			ERR_PTR(err));
135		return err;
136	}
137
138	clk_disable_unprepare(onboard_dev->clk);
139
140	onboard_dev->is_powered_on = false;
141
142	return 0;
143}
144
145static int __maybe_unused onboard_dev_suspend(struct device *dev)
146{
147	struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
148	struct usbdev_node *node;
149	bool power_off = true;
150
151	if (onboard_dev->always_powered_in_suspend)
152		return 0;
153
154	mutex_lock(&onboard_dev->lock);
155
156	list_for_each_entry(node, &onboard_dev->udev_list, list) {
157		if (!device_may_wakeup(node->udev->bus->controller))
158			continue;
159
160		if (usb_wakeup_enabled_descendants(node->udev)) {
161			power_off = false;
162			break;
163		}
164	}
165
166	mutex_unlock(&onboard_dev->lock);
167
168	if (!power_off)
169		return 0;
170
171	return onboard_dev_power_off(onboard_dev);
172}
173
174static int __maybe_unused onboard_dev_resume(struct device *dev)
175{
176	struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
177
178	if (onboard_dev->is_powered_on)
179		return 0;
180
181	return onboard_dev_power_on(onboard_dev);
182}
183
184static inline void get_udev_link_name(const struct usb_device *udev, char *buf,
185				      size_t size)
186{
187	snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
188}
189
190static int onboard_dev_add_usbdev(struct onboard_dev *onboard_dev,
191				  struct usb_device *udev)
192{
193	struct usbdev_node *node;
194	char link_name[64];
195	int err;
196
197	mutex_lock(&onboard_dev->lock);
198
199	if (onboard_dev->going_away) {
200		err = -EINVAL;
201		goto error;
202	}
203
204	node = kzalloc(sizeof(*node), GFP_KERNEL);
205	if (!node) {
206		err = -ENOMEM;
207		goto error;
208	}
209
210	node->udev = udev;
211
212	list_add(&node->list, &onboard_dev->udev_list);
213
214	mutex_unlock(&onboard_dev->lock);
215
216	get_udev_link_name(udev, link_name, sizeof(link_name));
217	WARN_ON(sysfs_create_link(&onboard_dev->dev->kobj, &udev->dev.kobj,
218				  link_name));
219
220	return 0;
221
222error:
223	mutex_unlock(&onboard_dev->lock);
224
225	return err;
226}
227
228static void onboard_dev_remove_usbdev(struct onboard_dev *onboard_dev,
229				      const struct usb_device *udev)
230{
231	struct usbdev_node *node;
232	char link_name[64];
233
234	get_udev_link_name(udev, link_name, sizeof(link_name));
235	sysfs_remove_link(&onboard_dev->dev->kobj, link_name);
236
237	mutex_lock(&onboard_dev->lock);
238
239	list_for_each_entry(node, &onboard_dev->udev_list, list) {
240		if (node->udev == udev) {
241			list_del(&node->list);
242			kfree(node);
243			break;
244		}
245	}
246
247	mutex_unlock(&onboard_dev->lock);
248}
249
250static ssize_t always_powered_in_suspend_show(struct device *dev,
251					      struct device_attribute *attr,
252					      char *buf)
253{
254	const struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
255
256	return sysfs_emit(buf, "%d\n", onboard_dev->always_powered_in_suspend);
257}
258
259static ssize_t always_powered_in_suspend_store(struct device *dev,
260					       struct device_attribute *attr,
261					       const char *buf, size_t count)
262{
263	struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
264	bool val;
265	int ret;
266
267	ret = kstrtobool(buf, &val);
268	if (ret < 0)
269		return ret;
270
271	onboard_dev->always_powered_in_suspend = val;
272
273	return count;
274}
275static DEVICE_ATTR_RW(always_powered_in_suspend);
276
277static struct attribute *onboard_dev_attrs[] = {
278	&dev_attr_always_powered_in_suspend.attr,
279	NULL,
280};
281
282static umode_t onboard_dev_attrs_are_visible(struct kobject *kobj,
283					     struct attribute *attr,
284					     int n)
285{
286	struct device *dev = kobj_to_dev(kobj);
287	struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
288
289	if (attr == &dev_attr_always_powered_in_suspend.attr &&
290	    !onboard_dev->pdata->is_hub)
291		return 0;
292
293	return attr->mode;
294}
295
296static const struct attribute_group onboard_dev_group = {
297	.is_visible = onboard_dev_attrs_are_visible,
298	.attrs = onboard_dev_attrs,
299};
300__ATTRIBUTE_GROUPS(onboard_dev);
301
302
303static void onboard_dev_attach_usb_driver(struct work_struct *work)
304{
305	int err;
306
307	err = driver_attach(&onboard_dev_usbdev_driver.driver);
308	if (err)
309		pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
310}
311
312static int onboard_dev_5744_i2c_init(struct i2c_client *client)
313{
314#if IS_ENABLED(CONFIG_USB_ONBOARD_DEV_USB5744)
315	struct device *dev = &client->dev;
316	int ret;
317
318	/*
319	 * Set BYPASS_UDC_SUSPEND bit to ensure MCU is always enabled
320	 * and ready to respond to SMBus runtime commands.
321	 * The command writes 5 bytes to memory and single data byte in
322	 * configuration register.
323	 */
324	char wr_buf[7] = {USB5744_CREG_MEM_ADDR, 5,
325			  USB5744_CREG_WRITE, 1,
326			  USB5744_CREG_RUNTIMEFLAGS2,
327			  USB5744_CREG_RUNTIMEFLAGS2_LSB,
328			  USB5744_CREG_BYPASS_UDC_SUSPEND};
329
330	ret = i2c_smbus_write_block_data(client, 0, sizeof(wr_buf), wr_buf);
331	if (ret)
332		return dev_err_probe(dev, ret, "BYPASS_UDC_SUSPEND bit configuration failed\n");
333
334	ret = i2c_smbus_write_word_data(client, USB5744_CMD_CREG_ACCESS,
335					USB5744_CMD_CREG_ACCESS_LSB);
336	if (ret)
337		return dev_err_probe(dev, ret, "Configuration Register Access Command failed\n");
338
339	/* Send SMBus command to boot hub. */
340	ret = i2c_smbus_write_word_data(client, USB5744_CMD_ATTACH,
341					USB5744_CMD_ATTACH_LSB);
342	if (ret < 0)
343		return dev_err_probe(dev, ret, "USB Attach with SMBus command failed\n");
344
345	return ret;
346#else
347	return -ENODEV;
348#endif
349}
350
351static int onboard_dev_probe(struct platform_device *pdev)
352{
353	struct device *dev = &pdev->dev;
354	struct onboard_dev *onboard_dev;
355	struct device_node *i2c_node;
356	int err;
357
358	onboard_dev = devm_kzalloc(dev, sizeof(*onboard_dev), GFP_KERNEL);
359	if (!onboard_dev)
360		return -ENOMEM;
361
362	onboard_dev->pdata = device_get_match_data(dev);
363	if (!onboard_dev->pdata)
364		return -EINVAL;
365
366	if (!onboard_dev->pdata->is_hub)
367		onboard_dev->always_powered_in_suspend = true;
368
369	onboard_dev->dev = dev;
370
371	err = onboard_dev_get_regulators(onboard_dev);
372	if (err)
373		return err;
374
375	onboard_dev->clk = devm_clk_get_optional(dev, NULL);
376	if (IS_ERR(onboard_dev->clk))
377		return dev_err_probe(dev, PTR_ERR(onboard_dev->clk),
378				     "failed to get clock\n");
379
380	onboard_dev->reset_gpio = devm_gpiod_get_optional(dev, "reset",
381							  GPIOD_OUT_HIGH);
382	if (IS_ERR(onboard_dev->reset_gpio))
383		return dev_err_probe(dev, PTR_ERR(onboard_dev->reset_gpio),
384				     "failed to get reset GPIO\n");
385
386	mutex_init(&onboard_dev->lock);
387	INIT_LIST_HEAD(&onboard_dev->udev_list);
388
389	dev_set_drvdata(dev, onboard_dev);
390
391	err = onboard_dev_power_on(onboard_dev);
392	if (err)
393		return err;
394
395	i2c_node = of_parse_phandle(pdev->dev.of_node, "i2c-bus", 0);
396	if (i2c_node) {
397		struct i2c_client *client = NULL;
398
399#if IS_ENABLED(CONFIG_USB_ONBOARD_DEV_USB5744)
400		client = of_find_i2c_device_by_node(i2c_node);
401#endif
402		of_node_put(i2c_node);
403
404		if (!client) {
405			err = -EPROBE_DEFER;
406			goto err_power_off;
407		}
408
409		if (of_device_is_compatible(pdev->dev.of_node, "usb424,2744") ||
410		    of_device_is_compatible(pdev->dev.of_node, "usb424,5744")) {
411			err = onboard_dev_5744_i2c_init(client);
412			onboard_dev->always_powered_in_suspend = true;
413		}
414
415		put_device(&client->dev);
416		if (err < 0)
417			goto err_power_off;
418	}
419
420	/*
421	 * The USB driver might have been detached from the USB devices by
422	 * onboard_dev_remove() (e.g. through an 'unbind' by userspace),
423	 * make sure to re-attach it if needed.
424	 *
425	 * This needs to be done deferred to avoid self-deadlocks on systems
426	 * with nested onboard hubs.
427	 */
428	schedule_work(&attach_usb_driver_work);
429
430	return 0;
431
432err_power_off:
433	onboard_dev_power_off(onboard_dev);
434	return err;
435}
436
437static void onboard_dev_remove(struct platform_device *pdev)
438{
439	struct onboard_dev *onboard_dev = dev_get_drvdata(&pdev->dev);
440	struct usbdev_node *node;
441	struct usb_device *udev;
442
443	onboard_dev->going_away = true;
444
445	mutex_lock(&onboard_dev->lock);
446
447	/* unbind the USB devices to avoid dangling references to this device */
448	while (!list_empty(&onboard_dev->udev_list)) {
449		node = list_first_entry(&onboard_dev->udev_list,
450					struct usbdev_node, list);
451		udev = node->udev;
452
453		/*
454		 * Unbinding the driver will call onboard_dev_remove_usbdev(),
455		 * which acquires onboard_dev->lock. We must release the lock
456		 * first.
457		 */
458		get_device(&udev->dev);
459		mutex_unlock(&onboard_dev->lock);
460		device_release_driver(&udev->dev);
461		put_device(&udev->dev);
462		mutex_lock(&onboard_dev->lock);
463	}
464
465	mutex_unlock(&onboard_dev->lock);
466
467	onboard_dev_power_off(onboard_dev);
468}
469
470MODULE_DEVICE_TABLE(of, onboard_dev_match);
471
472static const struct dev_pm_ops __maybe_unused onboard_dev_pm_ops = {
473	SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_dev_suspend, onboard_dev_resume)
474};
475
476static struct platform_driver onboard_dev_driver = {
477	.probe = onboard_dev_probe,
478	.remove = onboard_dev_remove,
479
480	.driver = {
481		.name = "onboard-usb-dev",
482		.of_match_table = onboard_dev_match,
483		.pm = pm_ptr(&onboard_dev_pm_ops),
484		.dev_groups = onboard_dev_groups,
485	},
486};
487
488/************************** USB driver **************************/
489
490#define VENDOR_ID_CYPRESS	0x04b4
491#define VENDOR_ID_GENESYS	0x05e3
492#define VENDOR_ID_MICROCHIP	0x0424
493#define VENDOR_ID_REALTEK	0x0bda
494#define VENDOR_ID_TI		0x0451
495#define VENDOR_ID_VIA		0x2109
496#define VENDOR_ID_XMOS		0x20B1
497
498/*
499 * Returns the onboard_dev platform device that is associated with the USB
500 * device passed as parameter.
501 */
502static struct onboard_dev *_find_onboard_dev(struct device *dev)
503{
504	struct platform_device *pdev;
505	struct device_node *np;
506	struct onboard_dev *onboard_dev;
507
508	pdev = of_find_device_by_node(dev->of_node);
509	if (!pdev) {
510		np = of_parse_phandle(dev->of_node, "peer-hub", 0);
511		if (!np) {
512			dev_err(dev, "failed to find device node for peer hub\n");
513			return ERR_PTR(-EINVAL);
514		}
515
516		pdev = of_find_device_by_node(np);
517		of_node_put(np);
518
519		if (!pdev)
520			return ERR_PTR(-ENODEV);
521	}
522
523	onboard_dev = dev_get_drvdata(&pdev->dev);
524	put_device(&pdev->dev);
525
526	/*
527	 * The presence of drvdata indicates that the platform driver finished
528	 * probing. This handles the case where (conceivably) we could be
529	 * running at the exact same time as the platform driver's probe. If
530	 * we detect the race we request probe deferral and we'll come back and
531	 * try again.
532	 */
533	if (!onboard_dev)
534		return ERR_PTR(-EPROBE_DEFER);
535
536	return onboard_dev;
537}
538
539static bool onboard_dev_usbdev_match(struct usb_device *udev)
540{
541	/* Onboard devices using this driver must have a device tree node */
542	return !!udev->dev.of_node;
543}
544
545static int onboard_dev_usbdev_probe(struct usb_device *udev)
546{
547	struct device *dev = &udev->dev;
548	struct onboard_dev *onboard_dev;
549	int err;
550
551	onboard_dev = _find_onboard_dev(dev);
552	if (IS_ERR(onboard_dev))
553		return PTR_ERR(onboard_dev);
554
555	dev_set_drvdata(dev, onboard_dev);
556
557	err = onboard_dev_add_usbdev(onboard_dev, udev);
558	if (err)
559		return err;
560
561	return 0;
562}
563
564static void onboard_dev_usbdev_disconnect(struct usb_device *udev)
565{
566	struct onboard_dev *onboard_dev = dev_get_drvdata(&udev->dev);
567
568	onboard_dev_remove_usbdev(onboard_dev, udev);
569}
570
571static const struct usb_device_id onboard_dev_id_table[] = {
572	{ USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 HUB */
573	{ USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 HUB */
574	{ USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) }, /* CY7C6563x 2.0 HUB */
575	{ USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 HUB */
576	{ USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 HUB */
577	{ USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 HUB */
578	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) }, /* USB2412 USB 2.0 HUB */
579	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 HUB */
580	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 HUB */
581	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) }, /* USB5744 USB 2.0 HUB */
582	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) }, /* USB5744 USB 3.0 HUB */
583	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 HUB */
584	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 HUB */
585	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 HUB */
586	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 HUB */
587	{ USB_DEVICE(VENDOR_ID_TI, 0x8025) }, /* TI USB8020B 3.0 HUB */
588	{ USB_DEVICE(VENDOR_ID_TI, 0x8027) }, /* TI USB8020B 2.0 HUB */
589	{ USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 HUB */
590	{ USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 HUB */
591	{ USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 HUB */
592	{ USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 HUB */
593	{ USB_DEVICE(VENDOR_ID_XMOS, 0x0013) }, /* XMOS XVF3500 Voice Processor */
594	{}
595};
596MODULE_DEVICE_TABLE(usb, onboard_dev_id_table);
597
598static struct usb_device_driver onboard_dev_usbdev_driver = {
599	.name = "onboard-usb-dev",
600	.match = onboard_dev_usbdev_match,
601	.probe = onboard_dev_usbdev_probe,
602	.disconnect = onboard_dev_usbdev_disconnect,
603	.generic_subclass = 1,
604	.supports_autosuspend =	1,
605	.id_table = onboard_dev_id_table,
606};
607
608static int __init onboard_dev_init(void)
609{
610	int ret;
611
612	ret = usb_register_device_driver(&onboard_dev_usbdev_driver, THIS_MODULE);
613	if (ret)
614		return ret;
615
616	ret = platform_driver_register(&onboard_dev_driver);
617	if (ret)
618		usb_deregister_device_driver(&onboard_dev_usbdev_driver);
619
620	return ret;
621}
622module_init(onboard_dev_init);
623
624static void __exit onboard_dev_exit(void)
625{
626	usb_deregister_device_driver(&onboard_dev_usbdev_driver);
627	platform_driver_unregister(&onboard_dev_driver);
628
629	cancel_work_sync(&attach_usb_driver_work);
630}
631module_exit(onboard_dev_exit);
632
633MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
634MODULE_DESCRIPTION("Driver for discrete onboard USB devices");
635MODULE_LICENSE("GPL v2");