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