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 44 static void onboard_dev_attach_usb_driver(struct work_struct *work); 45 46 static struct usb_device_driver onboard_dev_usbdev_driver; 47 static DECLARE_WORK(attach_usb_driver_work, onboard_dev_attach_usb_driver); 48 49 /************************** Platform driver **************************/ 50 51 struct usbdev_node { 52 struct usb_device *udev; 53 struct list_head list; 54 }; 55 56 struct 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 69 static 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 92 static 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 119 disable_clk: 120 clk_disable_unprepare(onboard_dev->clk); 121 return err; 122 } 123 124 static 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 145 static 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 174 static 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 184 static 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 190 static 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 222 error: 223 mutex_unlock(&onboard_dev->lock); 224 225 return err; 226 } 227 228 static 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 250 static 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 259 static 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 } 275 static DEVICE_ATTR_RW(always_powered_in_suspend); 276 277 static struct attribute *onboard_dev_attrs[] = { 278 &dev_attr_always_powered_in_suspend.attr, 279 NULL, 280 }; 281 282 static 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 296 static 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 303 static 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 312 static 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 351 static 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 413 put_device(&client->dev); 414 if (err < 0) 415 goto err_power_off; 416 } 417 418 /* 419 * The USB driver might have been detached from the USB devices by 420 * onboard_dev_remove() (e.g. through an 'unbind' by userspace), 421 * make sure to re-attach it if needed. 422 * 423 * This needs to be done deferred to avoid self-deadlocks on systems 424 * with nested onboard hubs. 425 */ 426 schedule_work(&attach_usb_driver_work); 427 428 return 0; 429 430 err_power_off: 431 onboard_dev_power_off(onboard_dev); 432 return err; 433 } 434 435 static void onboard_dev_remove(struct platform_device *pdev) 436 { 437 struct onboard_dev *onboard_dev = dev_get_drvdata(&pdev->dev); 438 struct usbdev_node *node; 439 struct usb_device *udev; 440 441 onboard_dev->going_away = true; 442 443 mutex_lock(&onboard_dev->lock); 444 445 /* unbind the USB devices to avoid dangling references to this device */ 446 while (!list_empty(&onboard_dev->udev_list)) { 447 node = list_first_entry(&onboard_dev->udev_list, 448 struct usbdev_node, list); 449 udev = node->udev; 450 451 /* 452 * Unbinding the driver will call onboard_dev_remove_usbdev(), 453 * which acquires onboard_dev->lock. We must release the lock 454 * first. 455 */ 456 get_device(&udev->dev); 457 mutex_unlock(&onboard_dev->lock); 458 device_release_driver(&udev->dev); 459 put_device(&udev->dev); 460 mutex_lock(&onboard_dev->lock); 461 } 462 463 mutex_unlock(&onboard_dev->lock); 464 465 onboard_dev_power_off(onboard_dev); 466 } 467 468 MODULE_DEVICE_TABLE(of, onboard_dev_match); 469 470 static const struct dev_pm_ops __maybe_unused onboard_dev_pm_ops = { 471 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_dev_suspend, onboard_dev_resume) 472 }; 473 474 static struct platform_driver onboard_dev_driver = { 475 .probe = onboard_dev_probe, 476 .remove_new = onboard_dev_remove, 477 478 .driver = { 479 .name = "onboard-usb-dev", 480 .of_match_table = onboard_dev_match, 481 .pm = pm_ptr(&onboard_dev_pm_ops), 482 .dev_groups = onboard_dev_groups, 483 }, 484 }; 485 486 /************************** USB driver **************************/ 487 488 #define VENDOR_ID_CYPRESS 0x04b4 489 #define VENDOR_ID_GENESYS 0x05e3 490 #define VENDOR_ID_MICROCHIP 0x0424 491 #define VENDOR_ID_REALTEK 0x0bda 492 #define VENDOR_ID_TI 0x0451 493 #define VENDOR_ID_VIA 0x2109 494 #define VENDOR_ID_XMOS 0x20B1 495 496 /* 497 * Returns the onboard_dev platform device that is associated with the USB 498 * device passed as parameter. 499 */ 500 static struct onboard_dev *_find_onboard_dev(struct device *dev) 501 { 502 struct platform_device *pdev; 503 struct device_node *np; 504 struct onboard_dev *onboard_dev; 505 506 pdev = of_find_device_by_node(dev->of_node); 507 if (!pdev) { 508 np = of_parse_phandle(dev->of_node, "peer-hub", 0); 509 if (!np) { 510 dev_err(dev, "failed to find device node for peer hub\n"); 511 return ERR_PTR(-EINVAL); 512 } 513 514 pdev = of_find_device_by_node(np); 515 of_node_put(np); 516 517 if (!pdev) 518 return ERR_PTR(-ENODEV); 519 } 520 521 onboard_dev = dev_get_drvdata(&pdev->dev); 522 put_device(&pdev->dev); 523 524 /* 525 * The presence of drvdata indicates that the platform driver finished 526 * probing. This handles the case where (conceivably) we could be 527 * running at the exact same time as the platform driver's probe. If 528 * we detect the race we request probe deferral and we'll come back and 529 * try again. 530 */ 531 if (!onboard_dev) 532 return ERR_PTR(-EPROBE_DEFER); 533 534 return onboard_dev; 535 } 536 537 static bool onboard_dev_usbdev_match(struct usb_device *udev) 538 { 539 /* Onboard devices using this driver must have a device tree node */ 540 return !!udev->dev.of_node; 541 } 542 543 static int onboard_dev_usbdev_probe(struct usb_device *udev) 544 { 545 struct device *dev = &udev->dev; 546 struct onboard_dev *onboard_dev; 547 int err; 548 549 onboard_dev = _find_onboard_dev(dev); 550 if (IS_ERR(onboard_dev)) 551 return PTR_ERR(onboard_dev); 552 553 dev_set_drvdata(dev, onboard_dev); 554 555 err = onboard_dev_add_usbdev(onboard_dev, udev); 556 if (err) 557 return err; 558 559 return 0; 560 } 561 562 static void onboard_dev_usbdev_disconnect(struct usb_device *udev) 563 { 564 struct onboard_dev *onboard_dev = dev_get_drvdata(&udev->dev); 565 566 onboard_dev_remove_usbdev(onboard_dev, udev); 567 } 568 569 static const struct usb_device_id onboard_dev_id_table[] = { 570 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 HUB */ 571 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 HUB */ 572 { USB_DEVICE(VENDOR_ID_CYPRESS, 0x6570) }, /* CY7C6563x 2.0 HUB */ 573 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 HUB */ 574 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 HUB */ 575 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 HUB */ 576 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2412) }, /* USB2412 USB 2.0 HUB */ 577 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 HUB */ 578 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 HUB */ 579 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2744) }, /* USB5744 USB 2.0 HUB */ 580 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x5744) }, /* USB5744 USB 3.0 HUB */ 581 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 HUB */ 582 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 HUB */ 583 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 HUB */ 584 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 HUB */ 585 { USB_DEVICE(VENDOR_ID_TI, 0x8025) }, /* TI USB8020B 3.0 HUB */ 586 { USB_DEVICE(VENDOR_ID_TI, 0x8027) }, /* TI USB8020B 2.0 HUB */ 587 { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 HUB */ 588 { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 HUB */ 589 { USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 HUB */ 590 { USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 HUB */ 591 { USB_DEVICE(VENDOR_ID_XMOS, 0x0013) }, /* XMOS XVF3500 Voice Processor */ 592 {} 593 }; 594 MODULE_DEVICE_TABLE(usb, onboard_dev_id_table); 595 596 static struct usb_device_driver onboard_dev_usbdev_driver = { 597 .name = "onboard-usb-dev", 598 .match = onboard_dev_usbdev_match, 599 .probe = onboard_dev_usbdev_probe, 600 .disconnect = onboard_dev_usbdev_disconnect, 601 .generic_subclass = 1, 602 .supports_autosuspend = 1, 603 .id_table = onboard_dev_id_table, 604 }; 605 606 static int __init onboard_dev_init(void) 607 { 608 int ret; 609 610 ret = usb_register_device_driver(&onboard_dev_usbdev_driver, THIS_MODULE); 611 if (ret) 612 return ret; 613 614 ret = platform_driver_register(&onboard_dev_driver); 615 if (ret) 616 usb_deregister_device_driver(&onboard_dev_usbdev_driver); 617 618 return ret; 619 } 620 module_init(onboard_dev_init); 621 622 static void __exit onboard_dev_exit(void) 623 { 624 usb_deregister_device_driver(&onboard_dev_usbdev_driver); 625 platform_driver_unregister(&onboard_dev_driver); 626 627 cancel_work_sync(&attach_usb_driver_work); 628 } 629 module_exit(onboard_dev_exit); 630 631 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>"); 632 MODULE_DESCRIPTION("Driver for discrete onboard USB devices"); 633 MODULE_LICENSE("GPL v2"); 634