Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / phy / tegra / xusb.c
blob63e916d4d0696156b244fb2c7e0e5f6de606c8e3
1 /*
2 * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
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.
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.
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>
21 #include <linux/phy/tegra/xusb.h>
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>
28 #include <soc/tegra/fuse.h>
30 #include "xusb.h"
32 static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
33 struct of_phandle_args *args)
35 struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
36 struct phy *phy = NULL;
37 unsigned int i;
39 if (args->args_count != 0)
40 return ERR_PTR(-EINVAL);
42 for (i = 0; i < pad->soc->num_lanes; i++) {
43 if (!pad->lanes[i])
44 continue;
46 if (pad->lanes[i]->dev.of_node == args->np) {
47 phy = pad->lanes[i];
48 break;
52 if (phy == NULL)
53 phy = ERR_PTR(-ENODEV);
55 return phy;
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)
61 .compatible = "nvidia,tegra124-xusb-padctl",
62 .data = &tegra124_xusb_padctl_soc,
64 #endif
65 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
67 .compatible = "nvidia,tegra210-xusb-padctl",
68 .data = &tegra210_xusb_padctl_soc,
70 #endif
71 { }
73 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
75 static struct device_node *
76 tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
78 struct device_node *pads, *np;
80 pads = of_get_child_by_name(padctl->dev->of_node, "pads");
81 if (!pads)
82 return NULL;
84 np = of_get_child_by_name(pads, name);
85 of_node_put(pads);
87 return np;
90 static struct device_node *
91 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
93 struct device_node *np, *lanes;
95 lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
96 if (!lanes)
97 return NULL;
99 np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
100 of_node_put(lanes);
102 return np;
105 static int
106 tegra_xusb_lane_lookup_function(struct tegra_xusb_lane *lane,
107 const char *function)
109 unsigned int i;
111 for (i = 0; i < lane->soc->num_funcs; i++)
112 if (strcmp(function, lane->soc->funcs[i]) == 0)
113 return i;
115 return -EINVAL;
118 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
119 struct device_node *np)
121 struct device *dev = &lane->pad->dev;
122 const char *function;
123 int err;
125 err = of_property_read_string(np, "nvidia,function", &function);
126 if (err < 0)
127 return err;
129 err = tegra_xusb_lane_lookup_function(lane, function);
130 if (err < 0) {
131 dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
132 function, np->name);
133 return err;
136 lane->function = err;
138 return 0;
141 static void tegra_xusb_lane_destroy(struct phy *phy)
143 if (phy) {
144 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
146 lane->pad->ops->remove(lane);
147 phy_destroy(phy);
151 static void tegra_xusb_pad_release(struct device *dev)
153 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
155 pad->soc->ops->remove(pad);
158 static struct device_type tegra_xusb_pad_type = {
159 .release = tegra_xusb_pad_release,
162 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
163 struct tegra_xusb_padctl *padctl,
164 struct device_node *np)
166 int err;
168 device_initialize(&pad->dev);
169 INIT_LIST_HEAD(&pad->list);
170 pad->dev.parent = padctl->dev;
171 pad->dev.type = &tegra_xusb_pad_type;
172 pad->dev.of_node = np;
173 pad->padctl = padctl;
175 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
176 if (err < 0)
177 goto unregister;
179 err = device_add(&pad->dev);
180 if (err < 0)
181 goto unregister;
183 return 0;
185 unregister:
186 device_unregister(&pad->dev);
187 return err;
190 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
191 const struct phy_ops *ops)
193 struct device_node *children;
194 struct phy *lane;
195 unsigned int i;
196 int err;
198 children = of_get_child_by_name(pad->dev.of_node, "lanes");
199 if (!children)
200 return -ENODEV;
202 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
203 GFP_KERNEL);
204 if (!pad->lanes) {
205 of_node_put(children);
206 return -ENOMEM;
209 for (i = 0; i < pad->soc->num_lanes; i++) {
210 struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
211 struct tegra_xusb_lane *lane;
213 /* skip disabled lanes */
214 if (!np || !of_device_is_available(np)) {
215 of_node_put(np);
216 continue;
219 pad->lanes[i] = phy_create(&pad->dev, np, ops);
220 if (IS_ERR(pad->lanes[i])) {
221 err = PTR_ERR(pad->lanes[i]);
222 of_node_put(np);
223 goto remove;
226 lane = pad->ops->probe(pad, np, i);
227 if (IS_ERR(lane)) {
228 phy_destroy(pad->lanes[i]);
229 err = PTR_ERR(lane);
230 goto remove;
233 list_add_tail(&lane->list, &pad->padctl->lanes);
234 phy_set_drvdata(pad->lanes[i], lane);
237 pad->provider = of_phy_provider_register_full(&pad->dev, children,
238 tegra_xusb_pad_of_xlate);
239 if (IS_ERR(pad->provider)) {
240 err = PTR_ERR(pad->provider);
241 goto remove;
244 return 0;
246 remove:
247 while (i--)
248 tegra_xusb_lane_destroy(pad->lanes[i]);
250 of_node_put(children);
252 return err;
255 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
257 unsigned int i = pad->soc->num_lanes;
259 of_phy_provider_unregister(pad->provider);
261 while (i--)
262 tegra_xusb_lane_destroy(pad->lanes[i]);
264 device_unregister(&pad->dev);
267 static struct tegra_xusb_pad *
268 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
269 const struct tegra_xusb_pad_soc *soc)
271 struct tegra_xusb_pad *pad;
272 struct device_node *np;
273 int err;
275 np = tegra_xusb_find_pad_node(padctl, soc->name);
276 if (!np || !of_device_is_available(np))
277 return NULL;
279 pad = soc->ops->probe(padctl, soc, np);
280 if (IS_ERR(pad)) {
281 err = PTR_ERR(pad);
282 dev_err(padctl->dev, "failed to create pad %s: %d\n",
283 soc->name, err);
284 return ERR_PTR(err);
287 /* XXX move this into ->probe() to avoid string comparison */
288 if (strcmp(soc->name, "pcie") == 0)
289 padctl->pcie = pad;
291 if (strcmp(soc->name, "sata") == 0)
292 padctl->sata = pad;
294 if (strcmp(soc->name, "usb2") == 0)
295 padctl->usb2 = pad;
297 if (strcmp(soc->name, "ulpi") == 0)
298 padctl->ulpi = pad;
300 if (strcmp(soc->name, "hsic") == 0)
301 padctl->hsic = pad;
303 return pad;
306 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
308 struct tegra_xusb_pad *pad, *tmp;
310 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
311 list_del(&pad->list);
312 tegra_xusb_pad_unregister(pad);
316 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
318 mutex_lock(&padctl->lock);
319 __tegra_xusb_remove_pads(padctl);
320 mutex_unlock(&padctl->lock);
323 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
325 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
326 const struct tegra_xusb_lane_soc *soc = lane->soc;
327 u32 value;
329 /* choose function */
330 value = padctl_readl(padctl, soc->offset);
331 value &= ~(soc->mask << soc->shift);
332 value |= lane->function << soc->shift;
333 padctl_writel(padctl, value, soc->offset);
336 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
338 unsigned int i;
340 for (i = 0; i < pad->soc->num_lanes; i++) {
341 struct tegra_xusb_lane *lane;
343 if (pad->lanes[i]) {
344 lane = phy_get_drvdata(pad->lanes[i]);
345 tegra_xusb_lane_program(lane);
350 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
352 struct tegra_xusb_pad *pad;
353 unsigned int i;
355 mutex_lock(&padctl->lock);
357 for (i = 0; i < padctl->soc->num_pads; i++) {
358 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
359 int err;
361 pad = tegra_xusb_pad_create(padctl, soc);
362 if (IS_ERR(pad)) {
363 err = PTR_ERR(pad);
364 dev_err(padctl->dev, "failed to create pad %s: %d\n",
365 soc->name, err);
366 __tegra_xusb_remove_pads(padctl);
367 mutex_unlock(&padctl->lock);
368 return err;
371 if (!pad)
372 continue;
374 list_add_tail(&pad->list, &padctl->pads);
377 list_for_each_entry(pad, &padctl->pads, list)
378 tegra_xusb_pad_program(pad);
380 mutex_unlock(&padctl->lock);
381 return 0;
384 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
385 const char *function)
387 const char *func = lane->soc->funcs[lane->function];
389 return strcmp(function, func) == 0;
392 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
393 const char *type,
394 unsigned int index)
396 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
397 char *name;
399 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
400 if (!name)
401 return ERR_PTR(-ENOMEM);
403 list_for_each_entry(lane, &padctl->lanes, list) {
404 if (strcmp(lane->soc->name, name) == 0) {
405 hit = lane;
406 break;
410 kfree(name);
411 return hit;
414 struct tegra_xusb_lane *
415 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
416 const struct tegra_xusb_lane_map *map,
417 const char *function)
419 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
421 for (map = map; map->type; map++) {
422 if (port->index != map->port)
423 continue;
425 lane = tegra_xusb_find_lane(port->padctl, map->type,
426 map->index);
427 if (IS_ERR(lane))
428 continue;
430 if (!tegra_xusb_lane_check(lane, function))
431 continue;
433 if (!IS_ERR(match))
434 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
435 map->type, map->index, match->soc->name);
436 else
437 match = lane;
440 return match;
443 static struct device_node *
444 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
445 unsigned int index)
447 struct device_node *ports, *np;
448 char *name;
450 ports = of_get_child_by_name(padctl->dev->of_node, "ports");
451 if (!ports)
452 return NULL;
454 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
455 if (!name) {
456 of_node_put(ports);
457 return ERR_PTR(-ENOMEM);
459 np = of_get_child_by_name(ports, name);
460 kfree(name);
461 of_node_put(ports);
463 return np;
466 struct tegra_xusb_port *
467 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
468 unsigned int index)
470 struct tegra_xusb_port *port;
471 struct device_node *np;
473 np = tegra_xusb_find_port_node(padctl, type, index);
474 if (!np)
475 return NULL;
477 list_for_each_entry(port, &padctl->ports, list) {
478 if (np == port->dev.of_node) {
479 of_node_put(np);
480 return port;
484 of_node_put(np);
486 return NULL;
489 struct tegra_xusb_usb2_port *
490 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
492 struct tegra_xusb_port *port;
494 port = tegra_xusb_find_port(padctl, "usb2", index);
495 if (port)
496 return to_usb2_port(port);
498 return NULL;
501 struct tegra_xusb_usb3_port *
502 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
504 struct tegra_xusb_port *port;
506 port = tegra_xusb_find_port(padctl, "usb3", index);
507 if (port)
508 return to_usb3_port(port);
510 return NULL;
513 static void tegra_xusb_port_release(struct device *dev)
517 static struct device_type tegra_xusb_port_type = {
518 .release = tegra_xusb_port_release,
521 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
522 struct tegra_xusb_padctl *padctl,
523 struct device_node *np,
524 const char *name,
525 unsigned int index)
527 int err;
529 INIT_LIST_HEAD(&port->list);
530 port->padctl = padctl;
531 port->index = index;
533 device_initialize(&port->dev);
534 port->dev.type = &tegra_xusb_port_type;
535 port->dev.of_node = of_node_get(np);
536 port->dev.parent = padctl->dev;
538 err = dev_set_name(&port->dev, "%s-%u", name, index);
539 if (err < 0)
540 goto unregister;
542 err = device_add(&port->dev);
543 if (err < 0)
544 goto unregister;
546 return 0;
548 unregister:
549 device_unregister(&port->dev);
550 return err;
553 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
555 device_unregister(&port->dev);
558 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
560 struct tegra_xusb_port *port = &usb2->base;
561 struct device_node *np = port->dev.of_node;
563 usb2->internal = of_property_read_bool(np, "nvidia,internal");
565 usb2->supply = devm_regulator_get(&port->dev, "vbus");
566 return PTR_ERR_OR_ZERO(usb2->supply);
569 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
570 unsigned int index)
572 struct tegra_xusb_usb2_port *usb2;
573 struct device_node *np;
574 int err = 0;
577 * USB2 ports don't require additional properties, but if the port is
578 * marked as disabled there is no reason to register it.
580 np = tegra_xusb_find_port_node(padctl, "usb2", index);
581 if (!np || !of_device_is_available(np))
582 goto out;
584 usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
585 if (!usb2) {
586 err = -ENOMEM;
587 goto out;
590 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
591 if (err < 0)
592 goto out;
594 usb2->base.ops = padctl->soc->ports.usb2.ops;
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;
602 err = tegra_xusb_usb2_port_parse_dt(usb2);
603 if (err < 0) {
604 tegra_xusb_port_unregister(&usb2->base);
605 goto out;
608 list_add_tail(&usb2->base.list, &padctl->ports);
610 out:
611 of_node_put(np);
612 return err;
615 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
617 struct tegra_xusb_port *port = &ulpi->base;
618 struct device_node *np = port->dev.of_node;
620 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
622 return 0;
625 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
626 unsigned int index)
628 struct tegra_xusb_ulpi_port *ulpi;
629 struct device_node *np;
630 int err = 0;
632 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
633 if (!np || !of_device_is_available(np))
634 goto out;
636 ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
637 if (!ulpi) {
638 err = -ENOMEM;
639 goto out;
642 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
643 if (err < 0)
644 goto out;
646 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
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;
654 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
655 if (err < 0) {
656 tegra_xusb_port_unregister(&ulpi->base);
657 goto out;
660 list_add_tail(&ulpi->base.list, &padctl->ports);
662 out:
663 of_node_put(np);
664 return err;
667 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
669 /* XXX */
670 return 0;
673 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
674 unsigned int index)
676 struct tegra_xusb_hsic_port *hsic;
677 struct device_node *np;
678 int err = 0;
680 np = tegra_xusb_find_port_node(padctl, "hsic", index);
681 if (!np || !of_device_is_available(np))
682 goto out;
684 hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
685 if (!hsic) {
686 err = -ENOMEM;
687 goto out;
690 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
691 if (err < 0)
692 goto out;
694 hsic->base.ops = padctl->soc->ports.hsic.ops;
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;
702 err = tegra_xusb_hsic_port_parse_dt(hsic);
703 if (err < 0) {
704 tegra_xusb_port_unregister(&hsic->base);
705 goto out;
708 list_add_tail(&hsic->base.list, &padctl->ports);
710 out:
711 of_node_put(np);
712 return err;
715 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
717 struct tegra_xusb_port *port = &usb3->base;
718 struct device_node *np = port->dev.of_node;
719 u32 value;
720 int err;
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;
728 usb3->port = value;
730 usb3->internal = of_property_read_bool(np, "nvidia,internal");
732 usb3->supply = devm_regulator_get(&port->dev, "vbus");
733 return PTR_ERR_OR_ZERO(usb3->supply);
736 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
737 unsigned int index)
739 struct tegra_xusb_usb3_port *usb3;
740 struct device_node *np;
741 int err = 0;
744 * If there is no supplemental configuration in the device tree the
745 * port is unusable. But it is valid to configure only a single port,
746 * hence return 0 instead of an error to allow ports to be optional.
748 np = tegra_xusb_find_port_node(padctl, "usb3", index);
749 if (!np || !of_device_is_available(np))
750 goto out;
752 usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
753 if (!usb3) {
754 err = -ENOMEM;
755 goto out;
758 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
759 if (err < 0)
760 goto out;
762 usb3->base.ops = padctl->soc->ports.usb3.ops;
764 usb3->base.lane = usb3->base.ops->map(&usb3->base);
765 if (IS_ERR(usb3->base.lane)) {
766 err = PTR_ERR(usb3->base.lane);
767 goto out;
770 err = tegra_xusb_usb3_port_parse_dt(usb3);
771 if (err < 0) {
772 tegra_xusb_port_unregister(&usb3->base);
773 goto out;
776 list_add_tail(&usb3->base.list, &padctl->ports);
778 out:
779 of_node_put(np);
780 return err;
783 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
785 struct tegra_xusb_port *port, *tmp;
787 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
788 list_del(&port->list);
789 tegra_xusb_port_unregister(port);
793 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
795 struct tegra_xusb_port *port;
796 unsigned int i;
797 int err = 0;
799 mutex_lock(&padctl->lock);
801 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
802 err = tegra_xusb_add_usb2_port(padctl, i);
803 if (err < 0)
804 goto remove_ports;
807 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
808 err = tegra_xusb_add_ulpi_port(padctl, i);
809 if (err < 0)
810 goto remove_ports;
813 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
814 err = tegra_xusb_add_hsic_port(padctl, i);
815 if (err < 0)
816 goto remove_ports;
819 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
820 err = tegra_xusb_add_usb3_port(padctl, i);
821 if (err < 0)
822 goto remove_ports;
825 list_for_each_entry(port, &padctl->ports, list) {
826 err = port->ops->enable(port);
827 if (err < 0)
828 dev_err(padctl->dev, "failed to enable port %s: %d\n",
829 dev_name(&port->dev), err);
832 goto unlock;
834 remove_ports:
835 __tegra_xusb_remove_ports(padctl);
836 unlock:
837 mutex_unlock(&padctl->lock);
838 return err;
841 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
843 mutex_lock(&padctl->lock);
844 __tegra_xusb_remove_ports(padctl);
845 mutex_unlock(&padctl->lock);
848 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
850 struct device_node *np = pdev->dev.of_node;
851 const struct tegra_xusb_padctl_soc *soc;
852 struct tegra_xusb_padctl *padctl;
853 const struct of_device_id *match;
854 struct resource *res;
855 int err;
857 /* for backwards compatibility with old device trees */
858 np = of_get_child_by_name(np, "pads");
859 if (!np) {
860 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
861 return tegra_xusb_padctl_legacy_probe(pdev);
864 of_node_put(np);
866 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
867 soc = match->data;
869 padctl = soc->ops->probe(&pdev->dev, soc);
870 if (IS_ERR(padctl))
871 return PTR_ERR(padctl);
873 platform_set_drvdata(pdev, padctl);
874 INIT_LIST_HEAD(&padctl->ports);
875 INIT_LIST_HEAD(&padctl->lanes);
876 INIT_LIST_HEAD(&padctl->pads);
877 mutex_init(&padctl->lock);
879 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
880 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
881 if (IS_ERR(padctl->regs)) {
882 err = PTR_ERR(padctl->regs);
883 goto remove;
886 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
887 if (IS_ERR(padctl->rst)) {
888 err = PTR_ERR(padctl->rst);
889 goto remove;
892 err = reset_control_deassert(padctl->rst);
893 if (err < 0)
894 goto remove;
896 err = tegra_xusb_setup_pads(padctl);
897 if (err < 0) {
898 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
899 goto reset;
902 err = tegra_xusb_setup_ports(padctl);
903 if (err) {
904 dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
905 goto remove_pads;
908 return 0;
910 remove_pads:
911 tegra_xusb_remove_pads(padctl);
912 reset:
913 reset_control_assert(padctl->rst);
914 remove:
915 soc->ops->remove(padctl);
916 return err;
919 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
921 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
922 int err;
924 tegra_xusb_remove_ports(padctl);
925 tegra_xusb_remove_pads(padctl);
927 err = reset_control_assert(padctl->rst);
928 if (err < 0)
929 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
931 padctl->soc->ops->remove(padctl);
933 return err;
936 static struct platform_driver tegra_xusb_padctl_driver = {
937 .driver = {
938 .name = "tegra-xusb-padctl",
939 .of_match_table = tegra_xusb_padctl_of_match,
941 .probe = tegra_xusb_padctl_probe,
942 .remove = tegra_xusb_padctl_remove,
944 module_platform_driver(tegra_xusb_padctl_driver);
946 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
948 struct tegra_xusb_padctl *padctl;
949 struct platform_device *pdev;
950 struct device_node *np;
952 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
953 if (!np)
954 return ERR_PTR(-EINVAL);
957 * This is slightly ugly. A better implementation would be to keep a
958 * registry of pad controllers, but since there will almost certainly
959 * only ever be one per SoC that would be a little overkill.
961 pdev = of_find_device_by_node(np);
962 if (!pdev) {
963 of_node_put(np);
964 return ERR_PTR(-ENODEV);
967 of_node_put(np);
969 padctl = platform_get_drvdata(pdev);
970 if (!padctl) {
971 put_device(&pdev->dev);
972 return ERR_PTR(-EPROBE_DEFER);
975 return padctl;
977 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
979 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
981 if (padctl)
982 put_device(padctl->dev);
984 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
986 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
987 unsigned int port)
989 if (padctl->soc->ops->usb3_save_context)
990 return padctl->soc->ops->usb3_save_context(padctl, port);
992 return -ENOSYS;
994 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
996 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
997 unsigned int port, bool idle)
999 if (padctl->soc->ops->hsic_set_idle)
1000 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1002 return -ENOSYS;
1004 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1006 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1007 unsigned int port, bool enable)
1009 if (padctl->soc->ops->usb3_set_lfps_detect)
1010 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1011 enable);
1013 return -ENOSYS;
1015 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1017 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1018 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1019 MODULE_LICENSE("GPL v2");