Thierry Reding | 53d2a71 | 2015-11-11 18:24:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/mailbox_client.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/phy/phy.h> |
Baoyou Xie | 0674b44 | 2016-08-31 16:56:49 +0800 | [diff] [blame^] | 21 | #include <linux/phy/tegra/xusb.h> |
Thierry Reding | 53d2a71 | 2015-11-11 18:24:21 +0100 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/consumer.h> |
| 24 | #include <linux/reset.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | |
| 28 | #include <soc/tegra/fuse.h> |
| 29 | |
| 30 | #include "xusb.h" |
| 31 | |
| 32 | static struct phy *tegra_xusb_pad_of_xlate(struct device *dev, |
| 33 | struct of_phandle_args *args) |
| 34 | { |
| 35 | struct tegra_xusb_pad *pad = dev_get_drvdata(dev); |
| 36 | struct phy *phy = NULL; |
| 37 | unsigned int i; |
| 38 | |
| 39 | if (args->args_count != 0) |
| 40 | return ERR_PTR(-EINVAL); |
| 41 | |
| 42 | for (i = 0; i < pad->soc->num_lanes; i++) { |
| 43 | if (!pad->lanes[i]) |
| 44 | continue; |
| 45 | |
| 46 | if (pad->lanes[i]->dev.of_node == args->np) { |
| 47 | phy = pad->lanes[i]; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (phy == NULL) |
| 53 | phy = ERR_PTR(-ENODEV); |
| 54 | |
| 55 | return phy; |
| 56 | } |
| 57 | |
| 58 | static const struct of_device_id tegra_xusb_padctl_of_match[] = { |
| 59 | #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC) |
| 60 | { |
| 61 | .compatible = "nvidia,tegra124-xusb-padctl", |
| 62 | .data = &tegra124_xusb_padctl_soc, |
| 63 | }, |
| 64 | #endif |
| 65 | #if defined(CONFIG_ARCH_TEGRA_210_SOC) |
| 66 | { |
| 67 | .compatible = "nvidia,tegra210-xusb-padctl", |
| 68 | .data = &tegra210_xusb_padctl_soc, |
| 69 | }, |
| 70 | #endif |
| 71 | { } |
| 72 | }; |
| 73 | MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match); |
| 74 | |
| 75 | static struct device_node * |
| 76 | tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name) |
| 77 | { |
| 78 | /* |
| 79 | * of_find_node_by_name() drops a reference, so make sure to grab one. |
| 80 | */ |
| 81 | struct device_node *np = of_node_get(padctl->dev->of_node); |
| 82 | |
| 83 | np = of_find_node_by_name(np, "pads"); |
| 84 | if (np) |
| 85 | np = of_find_node_by_name(np, name); |
| 86 | |
| 87 | return np; |
| 88 | } |
| 89 | |
| 90 | static struct device_node * |
| 91 | tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index) |
| 92 | { |
| 93 | /* |
| 94 | * of_find_node_by_name() drops a reference, so make sure to grab one. |
| 95 | */ |
| 96 | struct device_node *np = of_node_get(pad->dev.of_node); |
| 97 | |
| 98 | np = of_find_node_by_name(np, "lanes"); |
| 99 | if (!np) |
| 100 | return NULL; |
| 101 | |
| 102 | return of_find_node_by_name(np, pad->soc->lanes[index].name); |
| 103 | } |
| 104 | |
| 105 | int tegra_xusb_lane_lookup_function(struct tegra_xusb_lane *lane, |
| 106 | const char *function) |
| 107 | { |
| 108 | unsigned int i; |
| 109 | |
| 110 | for (i = 0; i < lane->soc->num_funcs; i++) |
| 111 | if (strcmp(function, lane->soc->funcs[i]) == 0) |
| 112 | return i; |
| 113 | |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane, |
| 118 | struct device_node *np) |
| 119 | { |
| 120 | struct device *dev = &lane->pad->dev; |
| 121 | const char *function; |
| 122 | int err; |
| 123 | |
| 124 | err = of_property_read_string(np, "nvidia,function", &function); |
| 125 | if (err < 0) |
| 126 | return err; |
| 127 | |
| 128 | err = tegra_xusb_lane_lookup_function(lane, function); |
| 129 | if (err < 0) { |
| 130 | dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n", |
| 131 | function, np->name); |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | lane->function = err; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static void tegra_xusb_lane_destroy(struct phy *phy) |
| 141 | { |
| 142 | if (phy) { |
| 143 | struct tegra_xusb_lane *lane = phy_get_drvdata(phy); |
| 144 | |
| 145 | lane->pad->ops->remove(lane); |
| 146 | phy_destroy(phy); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void tegra_xusb_pad_release(struct device *dev) |
| 151 | { |
| 152 | struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev); |
| 153 | |
| 154 | pad->soc->ops->remove(pad); |
| 155 | } |
| 156 | |
| 157 | static struct device_type tegra_xusb_pad_type = { |
| 158 | .release = tegra_xusb_pad_release, |
| 159 | }; |
| 160 | |
| 161 | int tegra_xusb_pad_init(struct tegra_xusb_pad *pad, |
| 162 | struct tegra_xusb_padctl *padctl, |
| 163 | struct device_node *np) |
| 164 | { |
| 165 | int err; |
| 166 | |
| 167 | device_initialize(&pad->dev); |
| 168 | INIT_LIST_HEAD(&pad->list); |
| 169 | pad->dev.parent = padctl->dev; |
| 170 | pad->dev.type = &tegra_xusb_pad_type; |
| 171 | pad->dev.of_node = np; |
| 172 | pad->padctl = padctl; |
| 173 | |
| 174 | err = dev_set_name(&pad->dev, "%s", pad->soc->name); |
| 175 | if (err < 0) |
| 176 | goto unregister; |
| 177 | |
| 178 | err = device_add(&pad->dev); |
| 179 | if (err < 0) |
| 180 | goto unregister; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | unregister: |
| 185 | device_unregister(&pad->dev); |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | int tegra_xusb_pad_register(struct tegra_xusb_pad *pad, |
| 190 | const struct phy_ops *ops) |
| 191 | { |
| 192 | struct device_node *children; |
| 193 | struct phy *lane; |
| 194 | unsigned int i; |
| 195 | int err; |
| 196 | |
| 197 | children = of_find_node_by_name(pad->dev.of_node, "lanes"); |
| 198 | if (!children) |
| 199 | return -ENODEV; |
| 200 | |
| 201 | pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane), |
| 202 | GFP_KERNEL); |
| 203 | if (!pad->lanes) { |
| 204 | of_node_put(children); |
| 205 | return -ENOMEM; |
| 206 | } |
| 207 | |
| 208 | for (i = 0; i < pad->soc->num_lanes; i++) { |
| 209 | struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i); |
| 210 | struct tegra_xusb_lane *lane; |
| 211 | |
| 212 | /* skip disabled lanes */ |
| 213 | if (!np || !of_device_is_available(np)) { |
| 214 | of_node_put(np); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | pad->lanes[i] = phy_create(&pad->dev, np, ops); |
| 219 | if (IS_ERR(pad->lanes[i])) { |
| 220 | err = PTR_ERR(pad->lanes[i]); |
| 221 | of_node_put(np); |
| 222 | goto remove; |
| 223 | } |
| 224 | |
| 225 | lane = pad->ops->probe(pad, np, i); |
| 226 | if (IS_ERR(lane)) { |
| 227 | phy_destroy(pad->lanes[i]); |
| 228 | err = PTR_ERR(lane); |
| 229 | goto remove; |
| 230 | } |
| 231 | |
| 232 | list_add_tail(&lane->list, &pad->padctl->lanes); |
| 233 | phy_set_drvdata(pad->lanes[i], lane); |
| 234 | } |
| 235 | |
| 236 | pad->provider = of_phy_provider_register_full(&pad->dev, children, |
| 237 | tegra_xusb_pad_of_xlate); |
| 238 | if (IS_ERR(pad->provider)) { |
| 239 | err = PTR_ERR(pad->provider); |
| 240 | goto remove; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | |
| 245 | remove: |
| 246 | while (i--) |
| 247 | tegra_xusb_lane_destroy(pad->lanes[i]); |
| 248 | |
| 249 | of_node_put(children); |
| 250 | |
| 251 | return err; |
| 252 | } |
| 253 | |
| 254 | void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad) |
| 255 | { |
| 256 | unsigned int i = pad->soc->num_lanes; |
| 257 | |
| 258 | of_phy_provider_unregister(pad->provider); |
| 259 | |
| 260 | while (i--) |
| 261 | tegra_xusb_lane_destroy(pad->lanes[i]); |
| 262 | |
| 263 | device_unregister(&pad->dev); |
| 264 | } |
| 265 | |
| 266 | static struct tegra_xusb_pad * |
| 267 | tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl, |
| 268 | const struct tegra_xusb_pad_soc *soc) |
| 269 | { |
| 270 | struct tegra_xusb_pad *pad; |
| 271 | struct device_node *np; |
| 272 | int err; |
| 273 | |
| 274 | np = tegra_xusb_find_pad_node(padctl, soc->name); |
| 275 | if (!np || !of_device_is_available(np)) |
| 276 | return NULL; |
| 277 | |
| 278 | pad = soc->ops->probe(padctl, soc, np); |
| 279 | if (IS_ERR(pad)) { |
| 280 | err = PTR_ERR(pad); |
| 281 | dev_err(padctl->dev, "failed to create pad %s: %d\n", |
| 282 | soc->name, err); |
| 283 | return ERR_PTR(err); |
| 284 | } |
| 285 | |
| 286 | /* XXX move this into ->probe() to avoid string comparison */ |
| 287 | if (strcmp(soc->name, "pcie") == 0) |
| 288 | padctl->pcie = pad; |
| 289 | |
| 290 | if (strcmp(soc->name, "sata") == 0) |
| 291 | padctl->sata = pad; |
| 292 | |
| 293 | if (strcmp(soc->name, "usb2") == 0) |
| 294 | padctl->usb2 = pad; |
| 295 | |
| 296 | if (strcmp(soc->name, "ulpi") == 0) |
| 297 | padctl->ulpi = pad; |
| 298 | |
| 299 | if (strcmp(soc->name, "hsic") == 0) |
| 300 | padctl->hsic = pad; |
| 301 | |
| 302 | return pad; |
| 303 | } |
| 304 | |
| 305 | static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl) |
| 306 | { |
| 307 | struct tegra_xusb_pad *pad, *tmp; |
| 308 | |
| 309 | list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) { |
| 310 | list_del(&pad->list); |
| 311 | tegra_xusb_pad_unregister(pad); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl) |
| 316 | { |
| 317 | mutex_lock(&padctl->lock); |
| 318 | __tegra_xusb_remove_pads(padctl); |
| 319 | mutex_unlock(&padctl->lock); |
| 320 | } |
| 321 | |
| 322 | static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane) |
| 323 | { |
| 324 | struct tegra_xusb_padctl *padctl = lane->pad->padctl; |
| 325 | const struct tegra_xusb_lane_soc *soc = lane->soc; |
| 326 | u32 value; |
| 327 | |
| 328 | /* choose function */ |
| 329 | value = padctl_readl(padctl, soc->offset); |
| 330 | value &= ~(soc->mask << soc->shift); |
| 331 | value |= lane->function << soc->shift; |
| 332 | padctl_writel(padctl, value, soc->offset); |
| 333 | } |
| 334 | |
| 335 | static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad) |
| 336 | { |
| 337 | unsigned int i; |
| 338 | |
| 339 | for (i = 0; i < pad->soc->num_lanes; i++) { |
| 340 | struct tegra_xusb_lane *lane; |
| 341 | |
| 342 | if (pad->lanes[i]) { |
| 343 | lane = phy_get_drvdata(pad->lanes[i]); |
| 344 | tegra_xusb_lane_program(lane); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl) |
| 350 | { |
| 351 | struct tegra_xusb_pad *pad; |
| 352 | unsigned int i; |
| 353 | |
| 354 | mutex_lock(&padctl->lock); |
| 355 | |
| 356 | for (i = 0; i < padctl->soc->num_pads; i++) { |
| 357 | const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i]; |
| 358 | int err; |
| 359 | |
| 360 | pad = tegra_xusb_pad_create(padctl, soc); |
| 361 | if (IS_ERR(pad)) { |
| 362 | err = PTR_ERR(pad); |
| 363 | dev_err(padctl->dev, "failed to create pad %s: %d\n", |
| 364 | soc->name, err); |
| 365 | __tegra_xusb_remove_pads(padctl); |
| 366 | mutex_unlock(&padctl->lock); |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | if (!pad) |
| 371 | continue; |
| 372 | |
| 373 | list_add_tail(&pad->list, &padctl->pads); |
| 374 | } |
| 375 | |
| 376 | list_for_each_entry(pad, &padctl->pads, list) |
| 377 | tegra_xusb_pad_program(pad); |
| 378 | |
| 379 | mutex_unlock(&padctl->lock); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane, |
| 384 | const char *function) |
| 385 | { |
| 386 | const char *func = lane->soc->funcs[lane->function]; |
| 387 | |
| 388 | return strcmp(function, func) == 0; |
| 389 | } |
| 390 | |
| 391 | struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl, |
| 392 | const char *type, |
| 393 | unsigned int index) |
| 394 | { |
| 395 | struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV); |
| 396 | char *name; |
| 397 | |
| 398 | name = kasprintf(GFP_KERNEL, "%s-%u", type, index); |
| 399 | if (!name) |
| 400 | return ERR_PTR(-ENOMEM); |
| 401 | |
| 402 | list_for_each_entry(lane, &padctl->lanes, list) { |
| 403 | if (strcmp(lane->soc->name, name) == 0) { |
| 404 | hit = lane; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | kfree(name); |
| 410 | return hit; |
| 411 | } |
| 412 | |
| 413 | struct tegra_xusb_lane * |
| 414 | tegra_xusb_port_find_lane(struct tegra_xusb_port *port, |
| 415 | const struct tegra_xusb_lane_map *map, |
| 416 | const char *function) |
| 417 | { |
| 418 | struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV); |
| 419 | |
| 420 | for (map = map; map->type; map++) { |
| 421 | if (port->index != map->port) |
| 422 | continue; |
| 423 | |
| 424 | lane = tegra_xusb_find_lane(port->padctl, map->type, |
| 425 | map->index); |
| 426 | if (IS_ERR(lane)) |
| 427 | continue; |
| 428 | |
| 429 | if (!tegra_xusb_lane_check(lane, function)) |
| 430 | continue; |
| 431 | |
| 432 | if (!IS_ERR(match)) |
| 433 | dev_err(&port->dev, "conflicting match: %s-%u / %s\n", |
| 434 | map->type, map->index, match->soc->name); |
| 435 | else |
| 436 | match = lane; |
| 437 | } |
| 438 | |
| 439 | return match; |
| 440 | } |
| 441 | |
| 442 | static struct device_node * |
| 443 | tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type, |
| 444 | unsigned int index) |
| 445 | { |
| 446 | /* |
| 447 | * of_find_node_by_name() drops a reference, so make sure to grab one. |
| 448 | */ |
| 449 | struct device_node *np = of_node_get(padctl->dev->of_node); |
| 450 | |
| 451 | np = of_find_node_by_name(np, "ports"); |
| 452 | if (np) { |
| 453 | char *name; |
| 454 | |
| 455 | name = kasprintf(GFP_KERNEL, "%s-%u", type, index); |
| 456 | np = of_find_node_by_name(np, name); |
| 457 | kfree(name); |
| 458 | } |
| 459 | |
| 460 | return np; |
| 461 | } |
| 462 | |
| 463 | struct tegra_xusb_port * |
| 464 | tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type, |
| 465 | unsigned int index) |
| 466 | { |
| 467 | struct tegra_xusb_port *port; |
| 468 | struct device_node *np; |
| 469 | |
| 470 | np = tegra_xusb_find_port_node(padctl, type, index); |
| 471 | if (!np) |
| 472 | return NULL; |
| 473 | |
| 474 | list_for_each_entry(port, &padctl->ports, list) { |
| 475 | if (np == port->dev.of_node) { |
| 476 | of_node_put(np); |
| 477 | return port; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | of_node_put(np); |
| 482 | |
| 483 | return NULL; |
| 484 | } |
| 485 | |
| 486 | struct tegra_xusb_usb2_port * |
| 487 | tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index) |
| 488 | { |
| 489 | struct tegra_xusb_port *port; |
| 490 | |
| 491 | port = tegra_xusb_find_port(padctl, "usb2", index); |
| 492 | if (port) |
| 493 | return to_usb2_port(port); |
| 494 | |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | struct tegra_xusb_usb3_port * |
| 499 | tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index) |
| 500 | { |
| 501 | struct tegra_xusb_port *port; |
| 502 | |
| 503 | port = tegra_xusb_find_port(padctl, "usb3", index); |
| 504 | if (port) |
| 505 | return to_usb3_port(port); |
| 506 | |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | static void tegra_xusb_port_release(struct device *dev) |
| 511 | { |
| 512 | } |
| 513 | |
| 514 | static struct device_type tegra_xusb_port_type = { |
| 515 | .release = tegra_xusb_port_release, |
| 516 | }; |
| 517 | |
| 518 | static int tegra_xusb_port_init(struct tegra_xusb_port *port, |
| 519 | struct tegra_xusb_padctl *padctl, |
| 520 | struct device_node *np, |
| 521 | const char *name, |
| 522 | unsigned int index) |
| 523 | { |
| 524 | int err; |
| 525 | |
| 526 | INIT_LIST_HEAD(&port->list); |
| 527 | port->padctl = padctl; |
| 528 | port->index = index; |
| 529 | |
| 530 | device_initialize(&port->dev); |
| 531 | port->dev.type = &tegra_xusb_port_type; |
| 532 | port->dev.of_node = of_node_get(np); |
| 533 | port->dev.parent = padctl->dev; |
| 534 | |
| 535 | err = dev_set_name(&port->dev, "%s-%u", name, index); |
| 536 | if (err < 0) |
| 537 | goto unregister; |
| 538 | |
| 539 | err = device_add(&port->dev); |
| 540 | if (err < 0) |
| 541 | goto unregister; |
| 542 | |
| 543 | return 0; |
| 544 | |
| 545 | unregister: |
| 546 | device_unregister(&port->dev); |
| 547 | return err; |
| 548 | } |
| 549 | |
| 550 | static void tegra_xusb_port_unregister(struct tegra_xusb_port *port) |
| 551 | { |
| 552 | device_unregister(&port->dev); |
| 553 | } |
| 554 | |
| 555 | static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2) |
| 556 | { |
| 557 | struct tegra_xusb_port *port = &usb2->base; |
| 558 | struct device_node *np = port->dev.of_node; |
| 559 | |
| 560 | usb2->internal = of_property_read_bool(np, "nvidia,internal"); |
| 561 | |
| 562 | usb2->supply = devm_regulator_get(&port->dev, "vbus"); |
| 563 | if (IS_ERR(usb2->supply)) |
| 564 | return PTR_ERR(usb2->supply); |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl, |
| 570 | unsigned int index) |
| 571 | { |
| 572 | struct tegra_xusb_usb2_port *usb2; |
| 573 | struct device_node *np; |
| 574 | int err = 0; |
| 575 | |
| 576 | /* |
| 577 | * USB2 ports don't require additional properties, but if the port is |
| 578 | * marked as disabled there is no reason to register it. |
| 579 | */ |
| 580 | np = tegra_xusb_find_port_node(padctl, "usb2", index); |
| 581 | if (!np || !of_device_is_available(np)) |
| 582 | goto out; |
| 583 | |
| 584 | usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL); |
| 585 | if (!usb2) { |
| 586 | err = -ENOMEM; |
| 587 | goto out; |
| 588 | } |
| 589 | |
| 590 | err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index); |
| 591 | if (err < 0) |
| 592 | goto out; |
| 593 | |
| 594 | usb2->base.ops = padctl->soc->ports.usb2.ops; |
| 595 | |
| 596 | usb2->base.lane = usb2->base.ops->map(&usb2->base); |
| 597 | if (IS_ERR(usb2->base.lane)) { |
| 598 | err = PTR_ERR(usb2->base.lane); |
| 599 | goto out; |
| 600 | } |
| 601 | |
| 602 | err = tegra_xusb_usb2_port_parse_dt(usb2); |
| 603 | if (err < 0) { |
| 604 | tegra_xusb_port_unregister(&usb2->base); |
| 605 | goto out; |
| 606 | } |
| 607 | |
| 608 | list_add_tail(&usb2->base.list, &padctl->ports); |
| 609 | |
| 610 | out: |
| 611 | of_node_put(np); |
| 612 | return err; |
| 613 | } |
| 614 | |
| 615 | static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi) |
| 616 | { |
| 617 | struct tegra_xusb_port *port = &ulpi->base; |
| 618 | struct device_node *np = port->dev.of_node; |
| 619 | |
| 620 | ulpi->internal = of_property_read_bool(np, "nvidia,internal"); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl, |
| 626 | unsigned int index) |
| 627 | { |
| 628 | struct tegra_xusb_ulpi_port *ulpi; |
| 629 | struct device_node *np; |
| 630 | int err = 0; |
| 631 | |
| 632 | np = tegra_xusb_find_port_node(padctl, "ulpi", index); |
| 633 | if (!np || !of_device_is_available(np)) |
| 634 | goto out; |
| 635 | |
| 636 | ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL); |
| 637 | if (!ulpi) { |
| 638 | err = -ENOMEM; |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index); |
| 643 | if (err < 0) |
| 644 | goto out; |
| 645 | |
| 646 | ulpi->base.ops = padctl->soc->ports.ulpi.ops; |
| 647 | |
| 648 | ulpi->base.lane = ulpi->base.ops->map(&ulpi->base); |
| 649 | if (IS_ERR(ulpi->base.lane)) { |
| 650 | err = PTR_ERR(ulpi->base.lane); |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | err = tegra_xusb_ulpi_port_parse_dt(ulpi); |
| 655 | if (err < 0) { |
| 656 | tegra_xusb_port_unregister(&ulpi->base); |
| 657 | goto out; |
| 658 | } |
| 659 | |
| 660 | list_add_tail(&ulpi->base.list, &padctl->ports); |
| 661 | |
| 662 | out: |
| 663 | of_node_put(np); |
| 664 | return err; |
| 665 | } |
| 666 | |
| 667 | static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic) |
| 668 | { |
| 669 | /* XXX */ |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl, |
| 674 | unsigned int index) |
| 675 | { |
| 676 | struct tegra_xusb_hsic_port *hsic; |
| 677 | struct device_node *np; |
| 678 | int err = 0; |
| 679 | |
| 680 | np = tegra_xusb_find_port_node(padctl, "hsic", index); |
| 681 | if (!np || !of_device_is_available(np)) |
| 682 | goto out; |
| 683 | |
| 684 | hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL); |
| 685 | if (!hsic) { |
| 686 | err = -ENOMEM; |
| 687 | goto out; |
| 688 | } |
| 689 | |
| 690 | err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index); |
| 691 | if (err < 0) |
| 692 | goto out; |
| 693 | |
| 694 | hsic->base.ops = padctl->soc->ports.hsic.ops; |
| 695 | |
| 696 | hsic->base.lane = hsic->base.ops->map(&hsic->base); |
| 697 | if (IS_ERR(hsic->base.lane)) { |
| 698 | err = PTR_ERR(hsic->base.lane); |
| 699 | goto out; |
| 700 | } |
| 701 | |
| 702 | err = tegra_xusb_hsic_port_parse_dt(hsic); |
| 703 | if (err < 0) { |
| 704 | tegra_xusb_port_unregister(&hsic->base); |
| 705 | goto out; |
| 706 | } |
| 707 | |
| 708 | list_add_tail(&hsic->base.list, &padctl->ports); |
| 709 | |
| 710 | out: |
| 711 | of_node_put(np); |
| 712 | return err; |
| 713 | } |
| 714 | |
| 715 | static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3) |
| 716 | { |
| 717 | struct tegra_xusb_port *port = &usb3->base; |
| 718 | struct device_node *np = port->dev.of_node; |
| 719 | u32 value; |
| 720 | int err; |
| 721 | |
| 722 | err = of_property_read_u32(np, "nvidia,usb2-companion", &value); |
| 723 | if (err < 0) { |
| 724 | dev_err(&port->dev, "failed to read port: %d\n", err); |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | usb3->port = value; |
| 729 | |
| 730 | usb3->internal = of_property_read_bool(np, "nvidia,internal"); |
| 731 | |
| 732 | usb3->supply = devm_regulator_get(&port->dev, "vbus"); |
| 733 | if (IS_ERR(usb3->supply)) |
| 734 | return PTR_ERR(usb3->supply); |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl, |
| 740 | unsigned int index) |
| 741 | { |
| 742 | struct tegra_xusb_usb3_port *usb3; |
| 743 | struct device_node *np; |
| 744 | int err = 0; |
| 745 | |
| 746 | /* |
| 747 | * If there is no supplemental configuration in the device tree the |
| 748 | * port is unusable. But it is valid to configure only a single port, |
| 749 | * hence return 0 instead of an error to allow ports to be optional. |
| 750 | */ |
| 751 | np = tegra_xusb_find_port_node(padctl, "usb3", index); |
| 752 | if (!np || !of_device_is_available(np)) |
| 753 | goto out; |
| 754 | |
| 755 | usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL); |
| 756 | if (!usb3) { |
| 757 | err = -ENOMEM; |
| 758 | goto out; |
| 759 | } |
| 760 | |
| 761 | err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index); |
| 762 | if (err < 0) |
| 763 | goto out; |
| 764 | |
| 765 | usb3->base.ops = padctl->soc->ports.usb3.ops; |
| 766 | |
| 767 | usb3->base.lane = usb3->base.ops->map(&usb3->base); |
| 768 | if (IS_ERR(usb3->base.lane)) { |
| 769 | err = PTR_ERR(usb3->base.lane); |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | err = tegra_xusb_usb3_port_parse_dt(usb3); |
| 774 | if (err < 0) { |
| 775 | tegra_xusb_port_unregister(&usb3->base); |
| 776 | goto out; |
| 777 | } |
| 778 | |
| 779 | list_add_tail(&usb3->base.list, &padctl->ports); |
| 780 | |
| 781 | out: |
| 782 | of_node_put(np); |
| 783 | return err; |
| 784 | } |
| 785 | |
| 786 | static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl) |
| 787 | { |
| 788 | struct tegra_xusb_port *port, *tmp; |
| 789 | |
| 790 | list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) { |
| 791 | list_del(&port->list); |
| 792 | tegra_xusb_port_unregister(port); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl) |
| 797 | { |
| 798 | struct tegra_xusb_port *port; |
| 799 | unsigned int i; |
| 800 | int err = 0; |
| 801 | |
| 802 | mutex_lock(&padctl->lock); |
| 803 | |
| 804 | for (i = 0; i < padctl->soc->ports.usb2.count; i++) { |
| 805 | err = tegra_xusb_add_usb2_port(padctl, i); |
| 806 | if (err < 0) |
| 807 | goto remove_ports; |
| 808 | } |
| 809 | |
| 810 | for (i = 0; i < padctl->soc->ports.ulpi.count; i++) { |
| 811 | err = tegra_xusb_add_ulpi_port(padctl, i); |
| 812 | if (err < 0) |
| 813 | goto remove_ports; |
| 814 | } |
| 815 | |
| 816 | for (i = 0; i < padctl->soc->ports.hsic.count; i++) { |
| 817 | err = tegra_xusb_add_hsic_port(padctl, i); |
| 818 | if (err < 0) |
| 819 | goto remove_ports; |
| 820 | } |
| 821 | |
| 822 | for (i = 0; i < padctl->soc->ports.usb3.count; i++) { |
| 823 | err = tegra_xusb_add_usb3_port(padctl, i); |
| 824 | if (err < 0) |
| 825 | goto remove_ports; |
| 826 | } |
| 827 | |
| 828 | list_for_each_entry(port, &padctl->ports, list) { |
| 829 | err = port->ops->enable(port); |
| 830 | if (err < 0) |
| 831 | dev_err(padctl->dev, "failed to enable port %s: %d\n", |
| 832 | dev_name(&port->dev), err); |
| 833 | } |
| 834 | |
| 835 | goto unlock; |
| 836 | |
| 837 | remove_ports: |
| 838 | __tegra_xusb_remove_ports(padctl); |
| 839 | unlock: |
| 840 | mutex_unlock(&padctl->lock); |
| 841 | return err; |
| 842 | } |
| 843 | |
| 844 | static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl) |
| 845 | { |
| 846 | mutex_lock(&padctl->lock); |
| 847 | __tegra_xusb_remove_ports(padctl); |
| 848 | mutex_unlock(&padctl->lock); |
| 849 | } |
| 850 | |
| 851 | static int tegra_xusb_padctl_probe(struct platform_device *pdev) |
| 852 | { |
| 853 | struct device_node *np = of_node_get(pdev->dev.of_node); |
| 854 | const struct tegra_xusb_padctl_soc *soc; |
| 855 | struct tegra_xusb_padctl *padctl; |
| 856 | const struct of_device_id *match; |
| 857 | struct resource *res; |
| 858 | int err; |
| 859 | |
| 860 | /* for backwards compatibility with old device trees */ |
| 861 | np = of_find_node_by_name(np, "pads"); |
| 862 | if (!np) { |
| 863 | dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n"); |
| 864 | return tegra_xusb_padctl_legacy_probe(pdev); |
| 865 | } |
| 866 | |
| 867 | of_node_put(np); |
| 868 | |
| 869 | match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node); |
| 870 | soc = match->data; |
| 871 | |
| 872 | padctl = soc->ops->probe(&pdev->dev, soc); |
| 873 | if (IS_ERR(padctl)) |
| 874 | return PTR_ERR(padctl); |
| 875 | |
| 876 | platform_set_drvdata(pdev, padctl); |
| 877 | INIT_LIST_HEAD(&padctl->ports); |
| 878 | INIT_LIST_HEAD(&padctl->lanes); |
| 879 | INIT_LIST_HEAD(&padctl->pads); |
| 880 | mutex_init(&padctl->lock); |
| 881 | |
| 882 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 883 | padctl->regs = devm_ioremap_resource(&pdev->dev, res); |
| 884 | if (IS_ERR(padctl->regs)) { |
| 885 | err = PTR_ERR(padctl->regs); |
| 886 | goto remove; |
| 887 | } |
| 888 | |
| 889 | padctl->rst = devm_reset_control_get(&pdev->dev, NULL); |
| 890 | if (IS_ERR(padctl->rst)) { |
| 891 | err = PTR_ERR(padctl->rst); |
| 892 | goto remove; |
| 893 | } |
| 894 | |
| 895 | err = reset_control_deassert(padctl->rst); |
| 896 | if (err < 0) |
| 897 | goto remove; |
| 898 | |
| 899 | err = tegra_xusb_setup_pads(padctl); |
| 900 | if (err < 0) { |
| 901 | dev_err(&pdev->dev, "failed to setup pads: %d\n", err); |
| 902 | goto reset; |
| 903 | } |
| 904 | |
| 905 | err = tegra_xusb_setup_ports(padctl); |
| 906 | if (err) { |
| 907 | dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err); |
| 908 | goto remove_pads; |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | |
| 913 | remove_pads: |
| 914 | tegra_xusb_remove_pads(padctl); |
| 915 | reset: |
| 916 | reset_control_assert(padctl->rst); |
| 917 | remove: |
| 918 | soc->ops->remove(padctl); |
| 919 | return err; |
| 920 | } |
| 921 | |
| 922 | static int tegra_xusb_padctl_remove(struct platform_device *pdev) |
| 923 | { |
| 924 | struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev); |
| 925 | int err; |
| 926 | |
| 927 | tegra_xusb_remove_ports(padctl); |
| 928 | tegra_xusb_remove_pads(padctl); |
| 929 | |
| 930 | err = reset_control_assert(padctl->rst); |
| 931 | if (err < 0) |
| 932 | dev_err(&pdev->dev, "failed to assert reset: %d\n", err); |
| 933 | |
| 934 | padctl->soc->ops->remove(padctl); |
| 935 | |
| 936 | return err; |
| 937 | } |
| 938 | |
| 939 | static struct platform_driver tegra_xusb_padctl_driver = { |
| 940 | .driver = { |
| 941 | .name = "tegra-xusb-padctl", |
| 942 | .of_match_table = tegra_xusb_padctl_of_match, |
| 943 | }, |
| 944 | .probe = tegra_xusb_padctl_probe, |
| 945 | .remove = tegra_xusb_padctl_remove, |
| 946 | }; |
| 947 | module_platform_driver(tegra_xusb_padctl_driver); |
| 948 | |
| 949 | struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev) |
| 950 | { |
| 951 | struct tegra_xusb_padctl *padctl; |
| 952 | struct platform_device *pdev; |
| 953 | struct device_node *np; |
| 954 | |
| 955 | np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0); |
| 956 | if (!np) |
| 957 | return ERR_PTR(-EINVAL); |
| 958 | |
| 959 | /* |
| 960 | * This is slightly ugly. A better implementation would be to keep a |
| 961 | * registry of pad controllers, but since there will almost certainly |
| 962 | * only ever be one per SoC that would be a little overkill. |
| 963 | */ |
| 964 | pdev = of_find_device_by_node(np); |
| 965 | if (!pdev) { |
| 966 | of_node_put(np); |
| 967 | return ERR_PTR(-ENODEV); |
| 968 | } |
| 969 | |
| 970 | of_node_put(np); |
| 971 | |
| 972 | padctl = platform_get_drvdata(pdev); |
| 973 | if (!padctl) { |
| 974 | put_device(&pdev->dev); |
| 975 | return ERR_PTR(-EPROBE_DEFER); |
| 976 | } |
| 977 | |
| 978 | return padctl; |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get); |
| 981 | |
| 982 | void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl) |
| 983 | { |
| 984 | if (padctl) |
| 985 | put_device(padctl->dev); |
| 986 | } |
| 987 | EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put); |
| 988 | |
| 989 | int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl, |
| 990 | unsigned int port) |
| 991 | { |
| 992 | if (padctl->soc->ops->usb3_save_context) |
| 993 | return padctl->soc->ops->usb3_save_context(padctl, port); |
| 994 | |
| 995 | return -ENOSYS; |
| 996 | } |
| 997 | EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context); |
| 998 | |
| 999 | int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl, |
| 1000 | unsigned int port, bool idle) |
| 1001 | { |
| 1002 | if (padctl->soc->ops->hsic_set_idle) |
| 1003 | return padctl->soc->ops->hsic_set_idle(padctl, port, idle); |
| 1004 | |
| 1005 | return -ENOSYS; |
| 1006 | } |
| 1007 | EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle); |
| 1008 | |
| 1009 | int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl, |
| 1010 | unsigned int port, bool enable) |
| 1011 | { |
| 1012 | if (padctl->soc->ops->usb3_set_lfps_detect) |
| 1013 | return padctl->soc->ops->usb3_set_lfps_detect(padctl, port, |
| 1014 | enable); |
| 1015 | |
| 1016 | return -ENOSYS; |
| 1017 | } |
| 1018 | EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect); |
| 1019 | |
| 1020 | MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); |
| 1021 | MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver"); |
| 1022 | MODULE_LICENSE("GPL v2"); |