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