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/kernel.h> 15 #include <linux/list.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 #include <linux/of.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> 29 30 #include "onboard_usb_dev.h" 31 32 static void onboard_dev_attach_usb_driver(struct work_struct *work); 33 34 static struct usb_device_driver onboard_dev_usbdev_driver; 35 static DECLARE_WORK(attach_usb_driver_work, onboard_dev_attach_usb_driver); 36 37 /************************** Platform driver **************************/ 38 39 struct usbdev_node { 40 struct usb_device *udev; 41 struct list_head list; 42 }; 43 44 struct onboard_dev { 45 struct regulator_bulk_data supplies[MAX_SUPPLIES]; 46 struct device *dev; 47 const struct onboard_dev_pdata *pdata; 48 struct gpio_desc *reset_gpio; 49 bool always_powered_in_suspend; 50 bool is_powered_on; 51 bool going_away; 52 struct list_head udev_list; 53 struct mutex lock; 54 struct clk *clk; 55 }; 56 57 static int onboard_dev_get_regulators(struct onboard_dev *onboard_dev) 58 { 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; 62 unsigned int i; 63 int err; 64 65 if (num_supplies > MAX_SUPPLIES) 66 return dev_err_probe(dev, -EINVAL, "max %d supplies supported!\n", 67 MAX_SUPPLIES); 68 69 for (i = 0; i < num_supplies; i++) 70 onboard_dev->supplies[i].supply = supply_names[i]; 71 72 err = devm_regulator_bulk_get(dev, num_supplies, onboard_dev->supplies); 73 if (err) 74 dev_err(dev, "Failed to get regulator supplies: %pe\n", 75 ERR_PTR(err)); 76 77 return err; 78 } 79 80 static int onboard_dev_power_on(struct onboard_dev *onboard_dev) 81 { 82 int err; 83 84 err = clk_prepare_enable(onboard_dev->clk); 85 if (err) { 86 dev_err(onboard_dev->dev, "failed to enable clock: %pe\n", 87 ERR_PTR(err)); 88 return err; 89 } 90 91 err = regulator_bulk_enable(onboard_dev->pdata->num_supplies, 92 onboard_dev->supplies); 93 if (err) { 94 dev_err(onboard_dev->dev, "failed to enable supplies: %pe\n", 95 ERR_PTR(err)); 96 goto disable_clk; 97 } 98 99 fsleep(onboard_dev->pdata->reset_us); 100 gpiod_set_value_cansleep(onboard_dev->reset_gpio, 0); 101 102 onboard_dev->is_powered_on = true; 103 104 return 0; 105 106 disable_clk: 107 clk_disable_unprepare(onboard_dev->clk); 108 return err; 109 } 110 111 static int onboard_dev_power_off(struct onboard_dev *onboard_dev) 112 { 113 int err; 114 115 gpiod_set_value_cansleep(onboard_dev->reset_gpio, 1); 116 117 err = regulator_bulk_disable(onboard_dev->pdata->num_supplies, 118 onboard_dev->supplies); 119 if (err) { 120 dev_err(onboard_dev->dev, "failed to disable supplies: %pe\n", 121 ERR_PTR(err)); 122 return err; 123 } 124 125 clk_disable_unprepare(onboard_dev->clk); 126 127 onboard_dev->is_powered_on = false; 128 129 return 0; 130 } 131 132 static int __maybe_unused onboard_dev_suspend(struct device *dev) 133 { 134 struct onboard_dev *onboard_dev = dev_get_drvdata(dev); 135 struct usbdev_node *node; 136 bool power_off = true; 137 138 if (onboard_dev->always_powered_in_suspend) 139 return 0; 140 141 mutex_lock(&onboard_dev->lock); 142 143 list_for_each_entry(node, &onboard_dev->udev_list, list) { 144 if (!device_may_wakeup(node->udev->bus->controller)) 145 continue; 146 147 if (usb_wakeup_enabled_descendants(node->udev)) { 148 power_off = false; 149 break; 150 } 151 } 152 153 mutex_unlock(&onboard_dev->lock); 154 155 if (!power_off) 156 return 0; 157 158 return onboard_dev_power_off(onboard_dev); 159 } 160 161 static int __maybe_unused onboard_dev_resume(struct device *dev) 162 { 163 struct onboard_dev *onboard_dev = dev_get_drvdata(dev); 164 165 if (onboard_dev->is_powered_on) 166 return 0; 167 168 return onboard_dev_power_on(onboard_dev); 169 } 170 171 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, 172 size_t size) 173 { 174 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev)); 175 } 176 177 static int onboard_dev_add_usbdev(struct onboard_dev *onboard_dev, 178 struct usb_device *udev) 179 { 180 struct usbdev_node *node; 181 char link_name[64]; 182 int err; 183 184 mutex_lock(&onboard_dev->lock); 185 186 if (onboard_dev->going_away) { 187 err = -EINVAL; 188 goto error; 189 } 190 191 node = kzalloc(sizeof(*node), GFP_KERNEL); 192 if (!node) { 193 err = -ENOMEM; 194 goto error; 195 } 196 197 node->udev = udev; 198 199 list_add(&node->list, &onboard_dev->udev_list); 200 201 mutex_unlock(&onboard_dev->lock); 202 203 get_udev_link_name(udev, link_name, sizeof(link_name)); 204 WARN_ON(sysfs_create_link(&onboard_dev->dev->kobj, &udev->dev.kobj, 205 link_name)); 206 207 return 0; 208 209 error: 210 mutex_unlock(&onboard_dev->lock); 211 212 return err; 213 } 214 215 static void onboard_dev_remove_usbdev(struct onboard_dev *onboard_dev, 216 const struct usb_device *udev) 217 { 218 struct usbdev_node *node; 219 char link_name[64]; 220 221 get_udev_link_name(udev, link_name, sizeof(link_name)); 222 sysfs_remove_link(&onboard_dev->dev->kobj, link_name); 223 224 mutex_lock(&onboard_dev->lock); 225 226 list_for_each_entry(node, &onboard_dev->udev_list, list) { 227 if (node->udev == udev) { 228 list_del(&node->list); 229 kfree(node); 230 break; 231 } 232 } 233 234 mutex_unlock(&onboard_dev->lock); 235 } 236 237 static ssize_t always_powered_in_suspend_show(struct device *dev, 238 struct device_attribute *attr, 239 char *buf) 240 { 241 const struct onboard_dev *onboard_dev = dev_get_drvdata(dev); 242 243 return sysfs_emit(buf, "%d\n", onboard_dev->always_powered_in_suspend); 244 } 245 246 static ssize_t always_powered_in_suspend_store(struct device *dev, 247 struct device_attribute *attr, 248 const char *buf, size_t count) 249 { 250 struct onboard_dev *onboard_dev = dev_get_drvdata(dev); 251 bool val; 252 int ret; 253 254 ret = kstrtobool(buf, &val); 255 if (ret < 0) 256 return ret; 257 258 onboard_dev->always_powered_in_suspend = val; 259 260 return count; 261 } 262 static DEVICE_ATTR_RW(always_powered_in_suspend); 263 264 static struct attribute *onboard_dev_attrs[] = { 265 &dev_attr_always_powered_in_suspend.attr, 266 NULL, 267 }; 268 269 static umode_t onboard_dev_attrs_are_visible(struct kobject *kobj, 270 struct attribute *attr, 271 int n) 272 { 273 struct device *dev = kobj_to_dev(kobj); 274 struct onboard_dev *onboard_dev = dev_get_drvdata(dev); 275 276 if (attr == &dev_attr_always_powered_in_suspend.attr && 277 !onboard_dev->pdata->is_hub) 278 return 0; 279 280 return attr->mode; 281 } 282 283 static const struct attribute_group onboard_dev_group = { 284 .is_visible = onboard_dev_attrs_are_visible, 285 .attrs = onboard_dev_attrs, 286 }; 287 __ATTRIBUTE_GROUPS(onboard_dev); 288 289 290 static void onboard_dev_attach_usb_driver(struct work_struct *work) 291 { 292 int err; 293 294 err = driver_attach(&onboard_dev_usbdev_driver.driver); 295 if (err) 296 pr_err("Failed to attach USB driver: %pe\n", ERR_PTR(err)); 297 } 298 299 static int onboard_dev_probe(struct platform_device *pdev) 300 { 301 struct device *dev = &pdev->dev; 302 struct onboard_dev *onboard_dev; 303 int err; 304 305 onboard_dev = devm_kzalloc(dev, sizeof(*onboard_dev), GFP_KERNEL); 306 if (!onboard_dev) 307 return -ENOMEM; 308 309 onboard_dev->pdata = device_get_match_data(dev); 310 if (!onboard_dev->pdata) 311 return -EINVAL; 312 313 if (!onboard_dev->pdata->is_hub) 314 onboard_dev->always_powered_in_suspend = true; 315 316 onboard_dev->dev = dev; 317 318 err = onboard_dev_get_regulators(onboard_dev); 319 if (err) 320 return err; 321 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"); 326 327 onboard_dev->reset_gpio = devm_gpiod_get_optional(dev, "reset", 328 GPIOD_OUT_HIGH); 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"); 332 333 mutex_init(&onboard_dev->lock); 334 INIT_LIST_HEAD(&onboard_dev->udev_list); 335 336 dev_set_drvdata(dev, onboard_dev); 337 338 err = onboard_dev_power_on(onboard_dev); 339 if (err) 340 return err; 341 342 /* 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. 346 * 347 * This needs to be done deferred to avoid self-deadlocks on systems 348 * with nested onboard hubs. 349 */ 350 schedule_work(&attach_usb_driver_work); 351 352 return 0; 353 } 354 355 static void onboard_dev_remove(struct platform_device *pdev) 356 { 357 struct onboard_dev *onboard_dev = dev_get_drvdata(&pdev->dev); 358 struct usbdev_node *node; 359 struct usb_device *udev; 360 361 onboard_dev->going_away = true; 362 363 mutex_lock(&onboard_dev->lock); 364 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); 369 udev = node->udev; 370 371 /* 372 * Unbinding the driver will call onboard_dev_remove_usbdev(), 373 * which acquires onboard_dev->lock. We must release the lock 374 * first. 375 */ 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); 381 } 382 383 mutex_unlock(&onboard_dev->lock); 384 385 onboard_dev_power_off(onboard_dev); 386 } 387 388 MODULE_DEVICE_TABLE(of, onboard_dev_match); 389 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) 392 }; 393 394 static struct platform_driver onboard_dev_driver = { 395 .probe = onboard_dev_probe, 396 .remove_new = onboard_dev_remove, 397 398 .driver = { 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, 403 }, 404 }; 405 406 /************************** USB driver **************************/ 407 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 415 416 /* 417 * Returns the onboard_dev platform device that is associated with the USB 418 * device passed as parameter. 419 */ 420 static struct onboard_dev *_find_onboard_dev(struct device *dev) 421 { 422 struct platform_device *pdev; 423 struct device_node *np; 424 struct onboard_dev *onboard_dev; 425 426 pdev = of_find_device_by_node(dev->of_node); 427 if (!pdev) { 428 np = of_parse_phandle(dev->of_node, "peer-hub", 0); 429 if (!np) { 430 dev_err(dev, "failed to find device node for peer hub\n"); 431 return ERR_PTR(-EINVAL); 432 } 433 434 pdev = of_find_device_by_node(np); 435 of_node_put(np); 436 437 if (!pdev) 438 return ERR_PTR(-ENODEV); 439 } 440 441 onboard_dev = dev_get_drvdata(&pdev->dev); 442 put_device(&pdev->dev); 443 444 /* 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 449 * try again. 450 */ 451 if (!onboard_dev) 452 return ERR_PTR(-EPROBE_DEFER); 453 454 return onboard_dev; 455 } 456 457 static bool onboard_dev_usbdev_match(struct usb_device *udev) 458 { 459 /* Onboard devices using this driver must have a device tree node */ 460 return !!udev->dev.of_node; 461 } 462 463 static int onboard_dev_usbdev_probe(struct usb_device *udev) 464 { 465 struct device *dev = &udev->dev; 466 struct onboard_dev *onboard_dev; 467 int err; 468 469 onboard_dev = _find_onboard_dev(dev); 470 if (IS_ERR(onboard_dev)) 471 return PTR_ERR(onboard_dev); 472 473 dev_set_drvdata(dev, onboard_dev); 474 475 err = onboard_dev_add_usbdev(onboard_dev, udev); 476 if (err) 477 return err; 478 479 return 0; 480 } 481 482 static void onboard_dev_usbdev_disconnect(struct usb_device *udev) 483 { 484 struct onboard_dev *onboard_dev = dev_get_drvdata(&udev->dev); 485 486 onboard_dev_remove_usbdev(onboard_dev, udev); 487 } 488 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 */ 512 {} 513 }; 514 MODULE_DEVICE_TABLE(usb, onboard_dev_id_table); 515 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, 524 }; 525 526 static int __init onboard_dev_init(void) 527 { 528 int ret; 529 530 ret = usb_register_device_driver(&onboard_dev_usbdev_driver, THIS_MODULE); 531 if (ret) 532 return ret; 533 534 ret = platform_driver_register(&onboard_dev_driver); 535 if (ret) 536 usb_deregister_device_driver(&onboard_dev_usbdev_driver); 537 538 return ret; 539 } 540 module_init(onboard_dev_init); 541 542 static void __exit onboard_dev_exit(void) 543 { 544 usb_deregister_device_driver(&onboard_dev_usbdev_driver); 545 platform_driver_unregister(&onboard_dev_driver); 546 547 cancel_work_sync(&attach_usb_driver_work); 548 } 549 module_exit(onboard_dev_exit); 550 551 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>"); 552 MODULE_DESCRIPTION("Driver for discrete onboard USB devices"); 553 MODULE_LICENSE("GPL v2"); 554