1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for onboard USB hubs
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_hub.h>
28 #include <linux/workqueue.h>
30 #include "onboard_usb_hub.h"
33 * Use generic names, as the actual names might differ between hubs. If a new
34 * hub requires more than the currently supported supplies, add a new one here.
36 static const char * const supply_names[] = {
41 #define MAX_SUPPLIES ARRAY_SIZE(supply_names)
43 static void onboard_hub_attach_usb_driver(struct work_struct *work);
45 static struct usb_device_driver onboard_hub_usbdev_driver;
46 static DECLARE_WORK(attach_usb_driver_work, onboard_hub_attach_usb_driver);
48 /************************** Platform driver **************************/
51 struct usb_device *udev;
52 struct list_head list;
56 struct regulator_bulk_data supplies[MAX_SUPPLIES];
58 const struct onboard_hub_pdata *pdata;
59 struct gpio_desc *reset_gpio;
60 bool always_powered_in_suspend;
63 struct list_head udev_list;
68 static int onboard_hub_power_on(struct onboard_hub *hub)
72 err = clk_prepare_enable(hub->clk);
74 dev_err(hub->dev, "failed to enable clock: %pe\n", ERR_PTR(err));
78 err = regulator_bulk_enable(hub->pdata->num_supplies, hub->supplies);
80 dev_err(hub->dev, "failed to enable supplies: %pe\n", ERR_PTR(err));
84 fsleep(hub->pdata->reset_us);
85 gpiod_set_value_cansleep(hub->reset_gpio, 0);
87 hub->is_powered_on = true;
92 static int onboard_hub_power_off(struct onboard_hub *hub)
96 gpiod_set_value_cansleep(hub->reset_gpio, 1);
98 err = regulator_bulk_disable(hub->pdata->num_supplies, hub->supplies);
100 dev_err(hub->dev, "failed to disable supplies: %pe\n", ERR_PTR(err));
104 clk_disable_unprepare(hub->clk);
106 hub->is_powered_on = false;
111 static int __maybe_unused onboard_hub_suspend(struct device *dev)
113 struct onboard_hub *hub = dev_get_drvdata(dev);
114 struct usbdev_node *node;
115 bool power_off = true;
117 if (hub->always_powered_in_suspend)
120 mutex_lock(&hub->lock);
122 list_for_each_entry(node, &hub->udev_list, list) {
123 if (!device_may_wakeup(node->udev->bus->controller))
126 if (usb_wakeup_enabled_descendants(node->udev)) {
132 mutex_unlock(&hub->lock);
137 return onboard_hub_power_off(hub);
140 static int __maybe_unused onboard_hub_resume(struct device *dev)
142 struct onboard_hub *hub = dev_get_drvdata(dev);
144 if (hub->is_powered_on)
147 return onboard_hub_power_on(hub);
150 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
152 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
155 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
157 struct usbdev_node *node;
161 mutex_lock(&hub->lock);
163 if (hub->going_away) {
168 node = kzalloc(sizeof(*node), GFP_KERNEL);
176 list_add(&node->list, &hub->udev_list);
178 mutex_unlock(&hub->lock);
180 get_udev_link_name(udev, link_name, sizeof(link_name));
181 WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
186 mutex_unlock(&hub->lock);
191 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
193 struct usbdev_node *node;
196 get_udev_link_name(udev, link_name, sizeof(link_name));
197 sysfs_remove_link(&hub->dev->kobj, link_name);
199 mutex_lock(&hub->lock);
201 list_for_each_entry(node, &hub->udev_list, list) {
202 if (node->udev == udev) {
203 list_del(&node->list);
209 mutex_unlock(&hub->lock);
212 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
215 const struct onboard_hub *hub = dev_get_drvdata(dev);
217 return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
220 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
221 const char *buf, size_t count)
223 struct onboard_hub *hub = dev_get_drvdata(dev);
227 ret = kstrtobool(buf, &val);
231 hub->always_powered_in_suspend = val;
235 static DEVICE_ATTR_RW(always_powered_in_suspend);
237 static struct attribute *onboard_hub_attrs[] = {
238 &dev_attr_always_powered_in_suspend.attr,
241 ATTRIBUTE_GROUPS(onboard_hub);
243 static void onboard_hub_attach_usb_driver(struct work_struct *work)
247 err = driver_attach(&onboard_hub_usbdev_driver.driver);
249 pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err));
252 static int onboard_hub_probe(struct platform_device *pdev)
254 struct device *dev = &pdev->dev;
255 struct onboard_hub *hub;
259 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
263 hub->pdata = device_get_match_data(&pdev->dev);
267 if (hub->pdata->num_supplies > MAX_SUPPLIES)
268 return dev_err_probe(dev, -EINVAL, "max %zu supplies supported!\n",
271 for (i = 0; i < hub->pdata->num_supplies; i++)
272 hub->supplies[i].supply = supply_names[i];
274 err = devm_regulator_bulk_get(dev, hub->pdata->num_supplies, hub->supplies);
276 dev_err(dev, "Failed to get regulator supplies: %pe\n", ERR_PTR(err));
280 hub->clk = devm_clk_get_optional(dev, NULL);
281 if (IS_ERR(hub->clk))
282 return dev_err_probe(dev, PTR_ERR(hub->clk), "failed to get clock\n");
284 hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
286 if (IS_ERR(hub->reset_gpio))
287 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
290 mutex_init(&hub->lock);
291 INIT_LIST_HEAD(&hub->udev_list);
293 dev_set_drvdata(dev, hub);
295 err = onboard_hub_power_on(hub);
300 * The USB driver might have been detached from the USB devices by
301 * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
302 * make sure to re-attach it if needed.
304 * This needs to be done deferred to avoid self-deadlocks on systems
305 * with nested onboard hubs.
307 schedule_work(&attach_usb_driver_work);
312 static void onboard_hub_remove(struct platform_device *pdev)
314 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
315 struct usbdev_node *node;
316 struct usb_device *udev;
318 hub->going_away = true;
320 mutex_lock(&hub->lock);
322 /* unbind the USB devices to avoid dangling references to this device */
323 while (!list_empty(&hub->udev_list)) {
324 node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
328 * Unbinding the driver will call onboard_hub_remove_usbdev(),
329 * which acquires hub->lock. We must release the lock first.
331 get_device(&udev->dev);
332 mutex_unlock(&hub->lock);
333 device_release_driver(&udev->dev);
334 put_device(&udev->dev);
335 mutex_lock(&hub->lock);
338 mutex_unlock(&hub->lock);
340 onboard_hub_power_off(hub);
343 MODULE_DEVICE_TABLE(of, onboard_hub_match);
345 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
346 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
349 static struct platform_driver onboard_hub_driver = {
350 .probe = onboard_hub_probe,
351 .remove_new = onboard_hub_remove,
354 .name = "onboard-usb-hub",
355 .of_match_table = onboard_hub_match,
356 .pm = pm_ptr(&onboard_hub_pm_ops),
357 .dev_groups = onboard_hub_groups,
361 /************************** USB driver **************************/
363 #define VENDOR_ID_CYPRESS 0x04b4
364 #define VENDOR_ID_GENESYS 0x05e3
365 #define VENDOR_ID_MICROCHIP 0x0424
366 #define VENDOR_ID_REALTEK 0x0bda
367 #define VENDOR_ID_TI 0x0451
368 #define VENDOR_ID_VIA 0x2109
371 * Returns the onboard_hub platform device that is associated with the USB
372 * device passed as parameter.
374 static struct onboard_hub *_find_onboard_hub(struct device *dev)
376 struct platform_device *pdev;
377 struct device_node *np;
378 struct onboard_hub *hub;
380 pdev = of_find_device_by_node(dev->of_node);
382 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
384 dev_err(dev, "failed to find device node for peer hub\n");
385 return ERR_PTR(-EINVAL);
388 pdev = of_find_device_by_node(np);
392 return ERR_PTR(-ENODEV);
395 hub = dev_get_drvdata(&pdev->dev);
396 put_device(&pdev->dev);
399 * The presence of drvdata ('hub') indicates that the platform driver
400 * finished probing. This handles the case where (conceivably) we could
401 * be running at the exact same time as the platform driver's probe. If
402 * we detect the race we request probe deferral and we'll come back and
406 return ERR_PTR(-EPROBE_DEFER);
411 static int onboard_hub_usbdev_probe(struct usb_device *udev)
413 struct device *dev = &udev->dev;
414 struct onboard_hub *hub;
417 /* ignore supported hubs without device tree node */
421 hub = _find_onboard_hub(dev);
425 dev_set_drvdata(dev, hub);
427 err = onboard_hub_add_usbdev(hub, udev);
434 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
436 struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
438 onboard_hub_remove_usbdev(hub, udev);
441 static const struct usb_device_id onboard_hub_id_table[] = {
442 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 */
443 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 */
444 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) }, /* CY7C6563x 2.0 */
445 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 */
446 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 */
447 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 */
448 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) }, /* USB2412 USB 2.0 */
449 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
450 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 */
451 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) }, /* USB5744 USB 2.0 */
452 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) }, /* USB5744 USB 3.0 */
453 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
454 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
455 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
456 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
457 { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 */
458 { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 */
459 { USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 */
460 { USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 */
463 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
465 static struct usb_device_driver onboard_hub_usbdev_driver = {
466 .name = "onboard-usb-hub",
467 .probe = onboard_hub_usbdev_probe,
468 .disconnect = onboard_hub_usbdev_disconnect,
469 .generic_subclass = 1,
470 .supports_autosuspend = 1,
471 .id_table = onboard_hub_id_table,
474 static int __init onboard_hub_init(void)
478 ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
482 ret = platform_driver_register(&onboard_hub_driver);
484 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
488 module_init(onboard_hub_init);
490 static void __exit onboard_hub_exit(void)
492 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
493 platform_driver_unregister(&onboard_hub_driver);
495 cancel_work_sync(&attach_usb_driver_work);
497 module_exit(onboard_hub_exit);
499 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
500 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
501 MODULE_LICENSE("GPL v2");