Linux 5.1.15
[linux/fpc-iii.git] / drivers / phy / tegra / xusb.c
blob5b3b8863363ed79a87db6f49e98cb4e1ab337619
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 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
106 struct device_node *np)
108 struct device *dev = &lane->pad->dev;
109 const char *function;
110 int err;
112 err = of_property_read_string(np, "nvidia,function", &function);
113 if (err < 0)
114 return err;
116 err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
117 if (err < 0) {
118 dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
119 function, np);
120 return err;
123 lane->function = err;
125 return 0;
128 static void tegra_xusb_lane_destroy(struct phy *phy)
130 if (phy) {
131 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
133 lane->pad->ops->remove(lane);
134 phy_destroy(phy);
138 static void tegra_xusb_pad_release(struct device *dev)
140 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
142 pad->soc->ops->remove(pad);
145 static struct device_type tegra_xusb_pad_type = {
146 .release = tegra_xusb_pad_release,
149 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
150 struct tegra_xusb_padctl *padctl,
151 struct device_node *np)
153 int err;
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;
162 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
163 if (err < 0)
164 goto unregister;
166 err = device_add(&pad->dev);
167 if (err < 0)
168 goto unregister;
170 return 0;
172 unregister:
173 device_unregister(&pad->dev);
174 return err;
177 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
178 const struct phy_ops *ops)
180 struct device_node *children;
181 struct phy *lane;
182 unsigned int i;
183 int err;
185 children = of_get_child_by_name(pad->dev.of_node, "lanes");
186 if (!children)
187 return -ENODEV;
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;
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;
200 /* skip disabled lanes */
201 if (!np || !of_device_is_available(np)) {
202 of_node_put(np);
203 continue;
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;
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;
220 list_add_tail(&lane->list, &pad->padctl->lanes);
221 phy_set_drvdata(pad->lanes[i], lane);
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;
231 return 0;
233 remove:
234 while (i--)
235 tegra_xusb_lane_destroy(pad->lanes[i]);
237 of_node_put(children);
239 return err;
242 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
244 unsigned int i = pad->soc->num_lanes;
246 of_phy_provider_unregister(pad->provider);
248 while (i--)
249 tegra_xusb_lane_destroy(pad->lanes[i]);
251 device_unregister(&pad->dev);
254 static struct tegra_xusb_pad *
255 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
256 const struct tegra_xusb_pad_soc *soc)
258 struct tegra_xusb_pad *pad;
259 struct device_node *np;
260 int err;
262 np = tegra_xusb_find_pad_node(padctl, soc->name);
263 if (!np || !of_device_is_available(np))
264 return NULL;
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);
274 /* XXX move this into ->probe() to avoid string comparison */
275 if (strcmp(soc->name, "pcie") == 0)
276 padctl->pcie = pad;
278 if (strcmp(soc->name, "sata") == 0)
279 padctl->sata = pad;
281 if (strcmp(soc->name, "usb2") == 0)
282 padctl->usb2 = pad;
284 if (strcmp(soc->name, "ulpi") == 0)
285 padctl->ulpi = pad;
287 if (strcmp(soc->name, "hsic") == 0)
288 padctl->hsic = pad;
290 return pad;
293 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
295 struct tegra_xusb_pad *pad, *tmp;
297 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
298 list_del(&pad->list);
299 tegra_xusb_pad_unregister(pad);
303 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
305 mutex_lock(&padctl->lock);
306 __tegra_xusb_remove_pads(padctl);
307 mutex_unlock(&padctl->lock);
310 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
312 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
313 const struct tegra_xusb_lane_soc *soc = lane->soc;
314 u32 value;
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);
323 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
325 unsigned int i;
327 for (i = 0; i < pad->soc->num_lanes; i++) {
328 struct tegra_xusb_lane *lane;
330 if (pad->lanes[i]) {
331 lane = phy_get_drvdata(pad->lanes[i]);
332 tegra_xusb_lane_program(lane);
337 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
339 struct tegra_xusb_pad *pad;
340 unsigned int i;
342 mutex_lock(&padctl->lock);
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;
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;
358 if (!pad)
359 continue;
361 list_add_tail(&pad->list, &padctl->pads);
364 list_for_each_entry(pad, &padctl->pads, list)
365 tegra_xusb_pad_program(pad);
367 mutex_unlock(&padctl->lock);
368 return 0;
371 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
372 const char *function)
374 const char *func = lane->soc->funcs[lane->function];
376 return strcmp(function, func) == 0;
379 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
380 const char *type,
381 unsigned int index)
383 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
384 char *name;
386 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
387 if (!name)
388 return ERR_PTR(-ENOMEM);
390 list_for_each_entry(lane, &padctl->lanes, list) {
391 if (strcmp(lane->soc->name, name) == 0) {
392 hit = lane;
393 break;
397 kfree(name);
398 return hit;
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)
406 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
408 for (; map->type; map++) {
409 if (port->index != map->port)
410 continue;
412 lane = tegra_xusb_find_lane(port->padctl, map->type,
413 map->index);
414 if (IS_ERR(lane))
415 continue;
417 if (!tegra_xusb_lane_check(lane, function))
418 continue;
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;
427 return match;
430 static struct device_node *
431 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
432 unsigned int index)
434 struct device_node *ports, *np;
435 char *name;
437 ports = of_get_child_by_name(padctl->dev->of_node, "ports");
438 if (!ports)
439 return NULL;
441 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
442 if (!name) {
443 of_node_put(ports);
444 return ERR_PTR(-ENOMEM);
446 np = of_get_child_by_name(ports, name);
447 kfree(name);
448 of_node_put(ports);
450 return np;
453 struct tegra_xusb_port *
454 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
455 unsigned int index)
457 struct tegra_xusb_port *port;
458 struct device_node *np;
460 np = tegra_xusb_find_port_node(padctl, type, index);
461 if (!np)
462 return NULL;
464 list_for_each_entry(port, &padctl->ports, list) {
465 if (np == port->dev.of_node) {
466 of_node_put(np);
467 return port;
471 of_node_put(np);
473 return NULL;
476 struct tegra_xusb_usb2_port *
477 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
479 struct tegra_xusb_port *port;
481 port = tegra_xusb_find_port(padctl, "usb2", index);
482 if (port)
483 return to_usb2_port(port);
485 return NULL;
488 struct tegra_xusb_usb3_port *
489 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
491 struct tegra_xusb_port *port;
493 port = tegra_xusb_find_port(padctl, "usb3", index);
494 if (port)
495 return to_usb3_port(port);
497 return NULL;
500 static void tegra_xusb_port_release(struct device *dev)
504 static struct device_type tegra_xusb_port_type = {
505 .release = tegra_xusb_port_release,
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)
514 int err;
516 INIT_LIST_HEAD(&port->list);
517 port->padctl = padctl;
518 port->index = index;
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;
525 err = dev_set_name(&port->dev, "%s-%u", name, index);
526 if (err < 0)
527 goto unregister;
529 err = device_add(&port->dev);
530 if (err < 0)
531 goto unregister;
533 return 0;
535 unregister:
536 device_unregister(&port->dev);
537 return err;
540 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
542 device_unregister(&port->dev);
545 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
547 struct tegra_xusb_port *port = &usb2->base;
548 struct device_node *np = port->dev.of_node;
550 usb2->internal = of_property_read_bool(np, "nvidia,internal");
552 usb2->supply = devm_regulator_get(&port->dev, "vbus");
553 return PTR_ERR_OR_ZERO(usb2->supply);
556 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
557 unsigned int index)
559 struct tegra_xusb_usb2_port *usb2;
560 struct device_node *np;
561 int err = 0;
564 * USB2 ports don't require additional properties, but if the port is
565 * marked as disabled there is no reason to register it.
567 np = tegra_xusb_find_port_node(padctl, "usb2", index);
568 if (!np || !of_device_is_available(np))
569 goto out;
571 usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
572 if (!usb2) {
573 err = -ENOMEM;
574 goto out;
577 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
578 if (err < 0)
579 goto out;
581 usb2->base.ops = padctl->soc->ports.usb2.ops;
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;
589 err = tegra_xusb_usb2_port_parse_dt(usb2);
590 if (err < 0) {
591 tegra_xusb_port_unregister(&usb2->base);
592 goto out;
595 list_add_tail(&usb2->base.list, &padctl->ports);
597 out:
598 of_node_put(np);
599 return err;
602 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
604 struct tegra_xusb_port *port = &ulpi->base;
605 struct device_node *np = port->dev.of_node;
607 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
609 return 0;
612 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
613 unsigned int index)
615 struct tegra_xusb_ulpi_port *ulpi;
616 struct device_node *np;
617 int err = 0;
619 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
620 if (!np || !of_device_is_available(np))
621 goto out;
623 ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
624 if (!ulpi) {
625 err = -ENOMEM;
626 goto out;
629 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
630 if (err < 0)
631 goto out;
633 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
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;
641 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
642 if (err < 0) {
643 tegra_xusb_port_unregister(&ulpi->base);
644 goto out;
647 list_add_tail(&ulpi->base.list, &padctl->ports);
649 out:
650 of_node_put(np);
651 return err;
654 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
656 /* XXX */
657 return 0;
660 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
661 unsigned int index)
663 struct tegra_xusb_hsic_port *hsic;
664 struct device_node *np;
665 int err = 0;
667 np = tegra_xusb_find_port_node(padctl, "hsic", index);
668 if (!np || !of_device_is_available(np))
669 goto out;
671 hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
672 if (!hsic) {
673 err = -ENOMEM;
674 goto out;
677 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
678 if (err < 0)
679 goto out;
681 hsic->base.ops = padctl->soc->ports.hsic.ops;
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;
689 err = tegra_xusb_hsic_port_parse_dt(hsic);
690 if (err < 0) {
691 tegra_xusb_port_unregister(&hsic->base);
692 goto out;
695 list_add_tail(&hsic->base.list, &padctl->ports);
697 out:
698 of_node_put(np);
699 return err;
702 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
704 struct tegra_xusb_port *port = &usb3->base;
705 struct device_node *np = port->dev.of_node;
706 u32 value;
707 int err;
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;
715 usb3->port = value;
717 usb3->internal = of_property_read_bool(np, "nvidia,internal");
719 usb3->supply = devm_regulator_get(&port->dev, "vbus");
720 return PTR_ERR_OR_ZERO(usb3->supply);
723 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
724 unsigned int index)
726 struct tegra_xusb_usb3_port *usb3;
727 struct device_node *np;
728 int err = 0;
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.
735 np = tegra_xusb_find_port_node(padctl, "usb3", index);
736 if (!np || !of_device_is_available(np))
737 goto out;
739 usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
740 if (!usb3) {
741 err = -ENOMEM;
742 goto out;
745 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
746 if (err < 0)
747 goto out;
749 usb3->base.ops = padctl->soc->ports.usb3.ops;
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;
757 err = tegra_xusb_usb3_port_parse_dt(usb3);
758 if (err < 0) {
759 tegra_xusb_port_unregister(&usb3->base);
760 goto out;
763 list_add_tail(&usb3->base.list, &padctl->ports);
765 out:
766 of_node_put(np);
767 return err;
770 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
772 struct tegra_xusb_port *port, *tmp;
774 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
775 list_del(&port->list);
776 tegra_xusb_port_unregister(port);
780 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
782 struct tegra_xusb_port *port;
783 unsigned int i;
784 int err = 0;
786 mutex_lock(&padctl->lock);
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;
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;
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;
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;
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);
819 goto unlock;
821 remove_ports:
822 __tegra_xusb_remove_ports(padctl);
823 unlock:
824 mutex_unlock(&padctl->lock);
825 return err;
828 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
830 mutex_lock(&padctl->lock);
831 __tegra_xusb_remove_ports(padctl);
832 mutex_unlock(&padctl->lock);
835 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
837 struct device_node *np = pdev->dev.of_node;
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;
844 /* for backwards compatibility with old device trees */
845 np = of_get_child_by_name(np, "pads");
846 if (!np) {
847 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
848 return tegra_xusb_padctl_legacy_probe(pdev);
851 of_node_put(np);
853 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
854 soc = match->data;
856 padctl = soc->ops->probe(&pdev->dev, soc);
857 if (IS_ERR(padctl))
858 return PTR_ERR(padctl);
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);
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;
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;
879 err = reset_control_deassert(padctl->rst);
880 if (err < 0)
881 goto remove;
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;
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;
895 return 0;
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;
906 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
908 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
909 int err;
911 tegra_xusb_remove_ports(padctl);
912 tegra_xusb_remove_pads(padctl);
914 err = reset_control_assert(padctl->rst);
915 if (err < 0)
916 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
918 padctl->soc->ops->remove(padctl);
920 return err;
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,
928 .probe = tegra_xusb_padctl_probe,
929 .remove = tegra_xusb_padctl_remove,
931 module_platform_driver(tegra_xusb_padctl_driver);
933 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
935 struct tegra_xusb_padctl *padctl;
936 struct platform_device *pdev;
937 struct device_node *np;
939 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
940 if (!np)
941 return ERR_PTR(-EINVAL);
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.
948 pdev = of_find_device_by_node(np);
949 if (!pdev) {
950 of_node_put(np);
951 return ERR_PTR(-ENODEV);
954 of_node_put(np);
956 padctl = platform_get_drvdata(pdev);
957 if (!padctl) {
958 put_device(&pdev->dev);
959 return ERR_PTR(-EPROBE_DEFER);
962 return padctl;
964 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
966 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
968 if (padctl)
969 put_device(padctl->dev);
971 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
973 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
974 unsigned int port)
976 if (padctl->soc->ops->usb3_save_context)
977 return padctl->soc->ops->usb3_save_context(padctl, port);
979 return -ENOSYS;
981 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
983 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
984 unsigned int port, bool idle)
986 if (padctl->soc->ops->hsic_set_idle)
987 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
989 return -ENOSYS;
991 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
993 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
994 unsigned int port, bool enable)
996 if (padctl->soc->ops->usb3_set_lfps_detect)
997 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
998 enable);
1000 return -ENOSYS;
1002 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1004 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1005 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1006 MODULE_LICENSE("GPL v2");