1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for onboard USB devices
5 * Copyright (c) 2022, Google LLC
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/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/suspend.h>
24 #include <linux/sysfs.h>
25 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/usb/onboard_dev.h>
28 #include <linux/workqueue.h>
30 #include "onboard_usb_dev.h"
32 static void onboard_dev_attach_usb_driver(struct work_struct *work);
34 static struct usb_device_driver onboard_dev_usbdev_driver;
35 static DECLARE_WORK(attach_usb_driver_work, onboard_dev_attach_usb_driver);
37 /************************** Platform driver **************************/
40 struct usb_device *udev;
41 struct list_head list;
45 struct regulator_bulk_data supplies[MAX_SUPPLIES];
47 const struct onboard_dev_pdata *pdata;
48 struct gpio_desc *reset_gpio;
49 bool always_powered_in_suspend;
52 struct list_head udev_list;
57 static int onboard_dev_get_regulators(struct onboard_dev *onboard_dev)
59 const char * const *supply_names = onboard_dev->pdata->supply_names;
60 unsigned int num_supplies = onboard_dev->pdata->num_supplies;
61 struct device *dev = onboard_dev->dev;
65 if (num_supplies > MAX_SUPPLIES)
66 return dev_err_probe(dev, -EINVAL, "max %d supplies supported!\n",
69 for (i = 0; i < num_supplies; i++)
70 onboard_dev->supplies[i].supply = supply_names[i];
72 err = devm_regulator_bulk_get(dev, num_supplies, onboard_dev->supplies);
74 dev_err(dev, "Failed to get regulator supplies: %pe\n",
80 static int onboard_dev_power_on(struct onboard_dev *onboard_dev)
84 err = clk_prepare_enable(onboard_dev->clk);
86 dev_err(onboard_dev->dev, "failed to enable clock: %pe\n",
91 err = regulator_bulk_enable(onboard_dev->pdata->num_supplies,
92 onboard_dev->supplies);
94 dev_err(onboard_dev->dev, "failed to enable supplies: %pe\n",
99 fsleep(onboard_dev->pdata->reset_us);
100 gpiod_set_value_cansleep(onboard_dev->reset_gpio, 0);
102 onboard_dev->is_powered_on = true;
107 clk_disable_unprepare(onboard_dev->clk);
111 static int onboard_dev_power_off(struct onboard_dev *onboard_dev)
115 gpiod_set_value_cansleep(onboard_dev->reset_gpio, 1);
117 err = regulator_bulk_disable(onboard_dev->pdata->num_supplies,
118 onboard_dev->supplies);
120 dev_err(onboard_dev->dev, "failed to disable supplies: %pe\n",
125 clk_disable_unprepare(onboard_dev->clk);
127 onboard_dev->is_powered_on = false;
132 static int __maybe_unused onboard_dev_suspend(struct device *dev)
134 struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
135 struct usbdev_node *node;
136 bool power_off = true;
138 if (onboard_dev->always_powered_in_suspend)
141 mutex_lock(&onboard_dev->lock);
143 list_for_each_entry(node, &onboard_dev->udev_list, list) {
144 if (!device_may_wakeup(node->udev->bus->controller))
147 if (usb_wakeup_enabled_descendants(node->udev)) {
153 mutex_unlock(&onboard_dev->lock);
158 return onboard_dev_power_off(onboard_dev);
161 static int __maybe_unused onboard_dev_resume(struct device *dev)
163 struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
165 if (onboard_dev->is_powered_on)
168 return onboard_dev_power_on(onboard_dev);
171 static inline void get_udev_link_name(const struct usb_device *udev, char *buf,
174 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
177 static int onboard_dev_add_usbdev(struct onboard_dev *onboard_dev,
178 struct usb_device *udev)
180 struct usbdev_node *node;
184 mutex_lock(&onboard_dev->lock);
186 if (onboard_dev->going_away) {
191 node = kzalloc(sizeof(*node), GFP_KERNEL);
199 list_add(&node->list, &onboard_dev->udev_list);
201 mutex_unlock(&onboard_dev->lock);
203 get_udev_link_name(udev, link_name, sizeof(link_name));
204 WARN_ON(sysfs_create_link(&onboard_dev->dev->kobj, &udev->dev.kobj,
210 mutex_unlock(&onboard_dev->lock);
215 static void onboard_dev_remove_usbdev(struct onboard_dev *onboard_dev,
216 const struct usb_device *udev)
218 struct usbdev_node *node;
221 get_udev_link_name(udev, link_name, sizeof(link_name));
222 sysfs_remove_link(&onboard_dev->dev->kobj, link_name);
224 mutex_lock(&onboard_dev->lock);
226 list_for_each_entry(node, &onboard_dev->udev_list, list) {
227 if (node->udev == udev) {
228 list_del(&node->list);
234 mutex_unlock(&onboard_dev->lock);
237 static ssize_t always_powered_in_suspend_show(struct device *dev,
238 struct device_attribute *attr,
241 const struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
243 return sysfs_emit(buf, "%d\n", onboard_dev->always_powered_in_suspend);
246 static ssize_t always_powered_in_suspend_store(struct device *dev,
247 struct device_attribute *attr,
248 const char *buf, size_t count)
250 struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
254 ret = kstrtobool(buf, &val);
258 onboard_dev->always_powered_in_suspend = val;
262 static DEVICE_ATTR_RW(always_powered_in_suspend);
264 static struct attribute *onboard_dev_attrs[] = {
265 &dev_attr_always_powered_in_suspend.attr,
269 static umode_t onboard_dev_attrs_are_visible(struct kobject *kobj,
270 struct attribute *attr,
273 struct device *dev = kobj_to_dev(kobj);
274 struct onboard_dev *onboard_dev = dev_get_drvdata(dev);
276 if (attr == &dev_attr_always_powered_in_suspend.attr &&
277 !onboard_dev->pdata->is_hub)
283 static const struct attribute_group onboard_dev_group = {
284 .is_visible = onboard_dev_attrs_are_visible,
285 .attrs = onboard_dev_attrs,
287 __ATTRIBUTE_GROUPS(onboard_dev);
290 static void onboard_dev_attach_usb_driver(struct work_struct *work)
294 err = driver_attach(&onboard_dev_usbdev_driver.driver);
296 pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
299 static int onboard_dev_probe(struct platform_device *pdev)
301 struct device *dev = &pdev->dev;
302 struct onboard_dev *onboard_dev;
305 onboard_dev = devm_kzalloc(dev, sizeof(*onboard_dev), GFP_KERNEL);
309 onboard_dev->pdata = device_get_match_data(dev);
310 if (!onboard_dev->pdata)
313 if (!onboard_dev->pdata->is_hub)
314 onboard_dev->always_powered_in_suspend = true;
316 onboard_dev->dev = dev;
318 err = onboard_dev_get_regulators(onboard_dev);
322 onboard_dev->clk = devm_clk_get_optional(dev, NULL);
323 if (IS_ERR(onboard_dev->clk))
324 return dev_err_probe(dev, PTR_ERR(onboard_dev->clk),
325 "failed to get clock\n");
327 onboard_dev->reset_gpio = devm_gpiod_get_optional(dev, "reset",
329 if (IS_ERR(onboard_dev->reset_gpio))
330 return dev_err_probe(dev, PTR_ERR(onboard_dev->reset_gpio),
331 "failed to get reset GPIO\n");
333 mutex_init(&onboard_dev->lock);
334 INIT_LIST_HEAD(&onboard_dev->udev_list);
336 dev_set_drvdata(dev, onboard_dev);
338 err = onboard_dev_power_on(onboard_dev);
343 * The USB driver might have been detached from the USB devices by
344 * onboard_dev_remove() (e.g. through an 'unbind' by userspace),
345 * make sure to re-attach it if needed.
347 * This needs to be done deferred to avoid self-deadlocks on systems
348 * with nested onboard hubs.
350 schedule_work(&attach_usb_driver_work);
355 static void onboard_dev_remove(struct platform_device *pdev)
357 struct onboard_dev *onboard_dev = dev_get_drvdata(&pdev->dev);
358 struct usbdev_node *node;
359 struct usb_device *udev;
361 onboard_dev->going_away = true;
363 mutex_lock(&onboard_dev->lock);
365 /* unbind the USB devices to avoid dangling references to this device */
366 while (!list_empty(&onboard_dev->udev_list)) {
367 node = list_first_entry(&onboard_dev->udev_list,
368 struct usbdev_node, list);
372 * Unbinding the driver will call onboard_dev_remove_usbdev(),
373 * which acquires onboard_dev->lock. We must release the lock
376 get_device(&udev->dev);
377 mutex_unlock(&onboard_dev->lock);
378 device_release_driver(&udev->dev);
379 put_device(&udev->dev);
380 mutex_lock(&onboard_dev->lock);
383 mutex_unlock(&onboard_dev->lock);
385 onboard_dev_power_off(onboard_dev);
388 MODULE_DEVICE_TABLE(of, onboard_dev_match);
390 static const struct dev_pm_ops __maybe_unused onboard_dev_pm_ops = {
391 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_dev_suspend, onboard_dev_resume)
394 static struct platform_driver onboard_dev_driver = {
395 .probe = onboard_dev_probe,
396 .remove_new = onboard_dev_remove,
399 .name = "onboard-usb-dev",
400 .of_match_table = onboard_dev_match,
401 .pm = pm_ptr(&onboard_dev_pm_ops),
402 .dev_groups = onboard_dev_groups,
406 /************************** USB driver **************************/
408 #define VENDOR_ID_CYPRESS 0x04b4
409 #define VENDOR_ID_GENESYS 0x05e3
410 #define VENDOR_ID_MICROCHIP 0x0424
411 #define VENDOR_ID_REALTEK 0x0bda
412 #define VENDOR_ID_TI 0x0451
413 #define VENDOR_ID_VIA 0x2109
414 #define VENDOR_ID_XMOS 0x20B1
417 * Returns the onboard_dev platform device that is associated with the USB
418 * device passed as parameter.
420 static struct onboard_dev *_find_onboard_dev(struct device *dev)
422 struct platform_device *pdev;
423 struct device_node *np;
424 struct onboard_dev *onboard_dev;
426 pdev = of_find_device_by_node(dev->of_node);
428 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
430 dev_err(dev, "failed to find device node for peer hub\n");
431 return ERR_PTR(-EINVAL);
434 pdev = of_find_device_by_node(np);
438 return ERR_PTR(-ENODEV);
441 onboard_dev = dev_get_drvdata(&pdev->dev);
442 put_device(&pdev->dev);
445 * The presence of drvdata indicates that the platform driver finished
446 * probing. This handles the case where (conceivably) we could be
447 * running at the exact same time as the platform driver's probe. If
448 * we detect the race we request probe deferral and we'll come back and
452 return ERR_PTR(-EPROBE_DEFER);
457 static bool onboard_dev_usbdev_match(struct usb_device *udev)
459 /* Onboard devices using this driver must have a device tree node */
460 return !!udev->dev.of_node;
463 static int onboard_dev_usbdev_probe(struct usb_device *udev)
465 struct device *dev = &udev->dev;
466 struct onboard_dev *onboard_dev;
469 onboard_dev = _find_onboard_dev(dev);
470 if (IS_ERR(onboard_dev))
471 return PTR_ERR(onboard_dev);
473 dev_set_drvdata(dev, onboard_dev);
475 err = onboard_dev_add_usbdev(onboard_dev, udev);
482 static void onboard_dev_usbdev_disconnect(struct usb_device *udev)
484 struct onboard_dev *onboard_dev = dev_get_drvdata(&udev->dev);
486 onboard_dev_remove_usbdev(onboard_dev, udev);
489 static const struct usb_device_id onboard_dev_id_table[] = {
490 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 HUB */
491 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 HUB */
492 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) }, /* CY7C6563x 2.0 HUB */
493 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 HUB */
494 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 HUB */
495 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 HUB */
496 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) }, /* USB2412 USB 2.0 HUB */
497 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 HUB */
498 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 HUB */
499 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) }, /* USB5744 USB 2.0 HUB */
500 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) }, /* USB5744 USB 3.0 HUB */
501 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 HUB */
502 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 HUB */
503 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 HUB */
504 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 HUB */
505 { USB_DEVICE(VENDOR_ID_TI, 0x8025) }, /* TI USB8020B 3.0 HUB */
506 { USB_DEVICE(VENDOR_ID_TI, 0x8027) }, /* TI USB8020B 2.0 HUB */
507 { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 HUB */
508 { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 HUB */
509 { USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 HUB */
510 { USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 HUB */
511 { USB_DEVICE(VENDOR_ID_XMOS, 0x0013) }, /* XMOS XVF3500 Voice Processor */
514 MODULE_DEVICE_TABLE(usb, onboard_dev_id_table);
516 static struct usb_device_driver onboard_dev_usbdev_driver = {
517 .name = "onboard-usb-dev",
518 .match = onboard_dev_usbdev_match,
519 .probe = onboard_dev_usbdev_probe,
520 .disconnect = onboard_dev_usbdev_disconnect,
521 .generic_subclass = 1,
522 .supports_autosuspend = 1,
523 .id_table = onboard_dev_id_table,
526 static int __init onboard_dev_init(void)
530 ret = usb_register_device_driver(&onboard_dev_usbdev_driver, THIS_MODULE);
534 ret = platform_driver_register(&onboard_dev_driver);
536 usb_deregister_device_driver(&onboard_dev_usbdev_driver);
540 module_init(onboard_dev_init);
542 static void __exit onboard_dev_exit(void)
544 usb_deregister_device_driver(&onboard_dev_usbdev_driver);
545 platform_driver_unregister(&onboard_dev_driver);
547 cancel_work_sync(&attach_usb_driver_work);
549 module_exit(onboard_dev_exit);
552 MODULE_DESCRIPTION("Driver for discrete onboard USB devices");
553 MODULE_LICENSE("GPL v2");