reset: zynq: add driver Kconfig option
[linux/fpc-iii.git] / drivers / phy / tegra / xusb.c
blobec83dfdbc206639da18166313232597d9d179063
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/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/reset.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
27 #include <soc/tegra/fuse.h>
29 #include "xusb.h"
31 static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
32 struct of_phandle_args *args)
34 struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
35 struct phy *phy = NULL;
36 unsigned int i;
38 if (args->args_count != 0)
39 return ERR_PTR(-EINVAL);
41 for (i = 0; i < pad->soc->num_lanes; i++) {
42 if (!pad->lanes[i])
43 continue;
45 if (pad->lanes[i]->dev.of_node == args->np) {
46 phy = pad->lanes[i];
47 break;
51 if (phy == NULL)
52 phy = ERR_PTR(-ENODEV);
54 return phy;
57 static const struct of_device_id tegra_xusb_padctl_of_match[] = {
58 #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
60 .compatible = "nvidia,tegra124-xusb-padctl",
61 .data = &tegra124_xusb_padctl_soc,
63 #endif
64 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
66 .compatible = "nvidia,tegra210-xusb-padctl",
67 .data = &tegra210_xusb_padctl_soc,
69 #endif
70 { }
72 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
74 static struct device_node *
75 tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
78 * of_find_node_by_name() drops a reference, so make sure to grab one.
80 struct device_node *np = of_node_get(padctl->dev->of_node);
82 np = of_find_node_by_name(np, "pads");
83 if (np)
84 np = of_find_node_by_name(np, name);
86 return np;
89 static struct device_node *
90 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
93 * of_find_node_by_name() drops a reference, so make sure to grab one.
95 struct device_node *np = of_node_get(pad->dev.of_node);
97 np = of_find_node_by_name(np, "lanes");
98 if (!np)
99 return NULL;
101 return of_find_node_by_name(np, pad->soc->lanes[index].name);
104 int tegra_xusb_lane_lookup_function(struct tegra_xusb_lane *lane,
105 const char *function)
107 unsigned int i;
109 for (i = 0; i < lane->soc->num_funcs; i++)
110 if (strcmp(function, lane->soc->funcs[i]) == 0)
111 return i;
113 return -EINVAL;
116 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
117 struct device_node *np)
119 struct device *dev = &lane->pad->dev;
120 const char *function;
121 int err;
123 err = of_property_read_string(np, "nvidia,function", &function);
124 if (err < 0)
125 return err;
127 err = tegra_xusb_lane_lookup_function(lane, function);
128 if (err < 0) {
129 dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
130 function, np->name);
131 return err;
134 lane->function = err;
136 return 0;
139 static void tegra_xusb_lane_destroy(struct phy *phy)
141 if (phy) {
142 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
144 lane->pad->ops->remove(lane);
145 phy_destroy(phy);
149 static void tegra_xusb_pad_release(struct device *dev)
151 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
153 pad->soc->ops->remove(pad);
156 static struct device_type tegra_xusb_pad_type = {
157 .release = tegra_xusb_pad_release,
160 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
161 struct tegra_xusb_padctl *padctl,
162 struct device_node *np)
164 int err;
166 device_initialize(&pad->dev);
167 INIT_LIST_HEAD(&pad->list);
168 pad->dev.parent = padctl->dev;
169 pad->dev.type = &tegra_xusb_pad_type;
170 pad->dev.of_node = np;
171 pad->padctl = padctl;
173 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
174 if (err < 0)
175 goto unregister;
177 err = device_add(&pad->dev);
178 if (err < 0)
179 goto unregister;
181 return 0;
183 unregister:
184 device_unregister(&pad->dev);
185 return err;
188 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
189 const struct phy_ops *ops)
191 struct device_node *children;
192 struct phy *lane;
193 unsigned int i;
194 int err;
196 children = of_find_node_by_name(pad->dev.of_node, "lanes");
197 if (!children)
198 return -ENODEV;
200 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
201 GFP_KERNEL);
202 if (!pad->lanes) {
203 of_node_put(children);
204 return -ENOMEM;
207 for (i = 0; i < pad->soc->num_lanes; i++) {
208 struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
209 struct tegra_xusb_lane *lane;
211 /* skip disabled lanes */
212 if (!np || !of_device_is_available(np)) {
213 of_node_put(np);
214 continue;
217 pad->lanes[i] = phy_create(&pad->dev, np, ops);
218 if (IS_ERR(pad->lanes[i])) {
219 err = PTR_ERR(pad->lanes[i]);
220 of_node_put(np);
221 goto remove;
224 lane = pad->ops->probe(pad, np, i);
225 if (IS_ERR(lane)) {
226 phy_destroy(pad->lanes[i]);
227 err = PTR_ERR(lane);
228 goto remove;
231 list_add_tail(&lane->list, &pad->padctl->lanes);
232 phy_set_drvdata(pad->lanes[i], lane);
235 pad->provider = of_phy_provider_register_full(&pad->dev, children,
236 tegra_xusb_pad_of_xlate);
237 if (IS_ERR(pad->provider)) {
238 err = PTR_ERR(pad->provider);
239 goto remove;
242 return 0;
244 remove:
245 while (i--)
246 tegra_xusb_lane_destroy(pad->lanes[i]);
248 of_node_put(children);
250 return err;
253 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
255 unsigned int i = pad->soc->num_lanes;
257 of_phy_provider_unregister(pad->provider);
259 while (i--)
260 tegra_xusb_lane_destroy(pad->lanes[i]);
262 device_unregister(&pad->dev);
265 static struct tegra_xusb_pad *
266 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
267 const struct tegra_xusb_pad_soc *soc)
269 struct tegra_xusb_pad *pad;
270 struct device_node *np;
271 int err;
273 np = tegra_xusb_find_pad_node(padctl, soc->name);
274 if (!np || !of_device_is_available(np))
275 return NULL;
277 pad = soc->ops->probe(padctl, soc, np);
278 if (IS_ERR(pad)) {
279 err = PTR_ERR(pad);
280 dev_err(padctl->dev, "failed to create pad %s: %d\n",
281 soc->name, err);
282 return ERR_PTR(err);
285 /* XXX move this into ->probe() to avoid string comparison */
286 if (strcmp(soc->name, "pcie") == 0)
287 padctl->pcie = pad;
289 if (strcmp(soc->name, "sata") == 0)
290 padctl->sata = pad;
292 if (strcmp(soc->name, "usb2") == 0)
293 padctl->usb2 = pad;
295 if (strcmp(soc->name, "ulpi") == 0)
296 padctl->ulpi = pad;
298 if (strcmp(soc->name, "hsic") == 0)
299 padctl->hsic = pad;
301 return pad;
304 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
306 struct tegra_xusb_pad *pad, *tmp;
308 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
309 list_del(&pad->list);
310 tegra_xusb_pad_unregister(pad);
314 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
316 mutex_lock(&padctl->lock);
317 __tegra_xusb_remove_pads(padctl);
318 mutex_unlock(&padctl->lock);
321 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
323 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
324 const struct tegra_xusb_lane_soc *soc = lane->soc;
325 u32 value;
327 /* choose function */
328 value = padctl_readl(padctl, soc->offset);
329 value &= ~(soc->mask << soc->shift);
330 value |= lane->function << soc->shift;
331 padctl_writel(padctl, value, soc->offset);
334 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
336 unsigned int i;
338 for (i = 0; i < pad->soc->num_lanes; i++) {
339 struct tegra_xusb_lane *lane;
341 if (pad->lanes[i]) {
342 lane = phy_get_drvdata(pad->lanes[i]);
343 tegra_xusb_lane_program(lane);
348 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
350 struct tegra_xusb_pad *pad;
351 unsigned int i;
353 mutex_lock(&padctl->lock);
355 for (i = 0; i < padctl->soc->num_pads; i++) {
356 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
357 int err;
359 pad = tegra_xusb_pad_create(padctl, soc);
360 if (IS_ERR(pad)) {
361 err = PTR_ERR(pad);
362 dev_err(padctl->dev, "failed to create pad %s: %d\n",
363 soc->name, err);
364 __tegra_xusb_remove_pads(padctl);
365 mutex_unlock(&padctl->lock);
366 return err;
369 if (!pad)
370 continue;
372 list_add_tail(&pad->list, &padctl->pads);
375 list_for_each_entry(pad, &padctl->pads, list)
376 tegra_xusb_pad_program(pad);
378 mutex_unlock(&padctl->lock);
379 return 0;
382 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
383 const char *function)
385 const char *func = lane->soc->funcs[lane->function];
387 return strcmp(function, func) == 0;
390 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
391 const char *type,
392 unsigned int index)
394 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
395 char *name;
397 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
398 if (!name)
399 return ERR_PTR(-ENOMEM);
401 list_for_each_entry(lane, &padctl->lanes, list) {
402 if (strcmp(lane->soc->name, name) == 0) {
403 hit = lane;
404 break;
408 kfree(name);
409 return hit;
412 struct tegra_xusb_lane *
413 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
414 const struct tegra_xusb_lane_map *map,
415 const char *function)
417 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
419 for (map = map; map->type; map++) {
420 if (port->index != map->port)
421 continue;
423 lane = tegra_xusb_find_lane(port->padctl, map->type,
424 map->index);
425 if (IS_ERR(lane))
426 continue;
428 if (!tegra_xusb_lane_check(lane, function))
429 continue;
431 if (!IS_ERR(match))
432 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
433 map->type, map->index, match->soc->name);
434 else
435 match = lane;
438 return match;
441 static struct device_node *
442 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
443 unsigned int index)
446 * of_find_node_by_name() drops a reference, so make sure to grab one.
448 struct device_node *np = of_node_get(padctl->dev->of_node);
450 np = of_find_node_by_name(np, "ports");
451 if (np) {
452 char *name;
454 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
455 np = of_find_node_by_name(np, name);
456 kfree(name);
459 return np;
462 struct tegra_xusb_port *
463 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
464 unsigned int index)
466 struct tegra_xusb_port *port;
467 struct device_node *np;
469 np = tegra_xusb_find_port_node(padctl, type, index);
470 if (!np)
471 return NULL;
473 list_for_each_entry(port, &padctl->ports, list) {
474 if (np == port->dev.of_node) {
475 of_node_put(np);
476 return port;
480 of_node_put(np);
482 return NULL;
485 struct tegra_xusb_usb2_port *
486 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
488 struct tegra_xusb_port *port;
490 port = tegra_xusb_find_port(padctl, "usb2", index);
491 if (port)
492 return to_usb2_port(port);
494 return NULL;
497 struct tegra_xusb_usb3_port *
498 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
500 struct tegra_xusb_port *port;
502 port = tegra_xusb_find_port(padctl, "usb3", index);
503 if (port)
504 return to_usb3_port(port);
506 return NULL;
509 static void tegra_xusb_port_release(struct device *dev)
513 static struct device_type tegra_xusb_port_type = {
514 .release = tegra_xusb_port_release,
517 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
518 struct tegra_xusb_padctl *padctl,
519 struct device_node *np,
520 const char *name,
521 unsigned int index)
523 int err;
525 INIT_LIST_HEAD(&port->list);
526 port->padctl = padctl;
527 port->index = index;
529 device_initialize(&port->dev);
530 port->dev.type = &tegra_xusb_port_type;
531 port->dev.of_node = of_node_get(np);
532 port->dev.parent = padctl->dev;
534 err = dev_set_name(&port->dev, "%s-%u", name, index);
535 if (err < 0)
536 goto unregister;
538 err = device_add(&port->dev);
539 if (err < 0)
540 goto unregister;
542 return 0;
544 unregister:
545 device_unregister(&port->dev);
546 return err;
549 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
551 device_unregister(&port->dev);
554 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
556 struct tegra_xusb_port *port = &usb2->base;
557 struct device_node *np = port->dev.of_node;
559 usb2->internal = of_property_read_bool(np, "nvidia,internal");
561 usb2->supply = devm_regulator_get(&port->dev, "vbus");
562 if (IS_ERR(usb2->supply))
563 return PTR_ERR(usb2->supply);
565 return 0;
568 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
569 unsigned int index)
571 struct tegra_xusb_usb2_port *usb2;
572 struct device_node *np;
573 int err = 0;
576 * USB2 ports don't require additional properties, but if the port is
577 * marked as disabled there is no reason to register it.
579 np = tegra_xusb_find_port_node(padctl, "usb2", index);
580 if (!np || !of_device_is_available(np))
581 goto out;
583 usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
584 if (!usb2) {
585 err = -ENOMEM;
586 goto out;
589 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
590 if (err < 0)
591 goto out;
593 usb2->base.ops = padctl->soc->ports.usb2.ops;
595 usb2->base.lane = usb2->base.ops->map(&usb2->base);
596 if (IS_ERR(usb2->base.lane)) {
597 err = PTR_ERR(usb2->base.lane);
598 goto out;
601 err = tegra_xusb_usb2_port_parse_dt(usb2);
602 if (err < 0) {
603 tegra_xusb_port_unregister(&usb2->base);
604 goto out;
607 list_add_tail(&usb2->base.list, &padctl->ports);
609 out:
610 of_node_put(np);
611 return err;
614 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
616 struct tegra_xusb_port *port = &ulpi->base;
617 struct device_node *np = port->dev.of_node;
619 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
621 return 0;
624 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
625 unsigned int index)
627 struct tegra_xusb_ulpi_port *ulpi;
628 struct device_node *np;
629 int err = 0;
631 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
632 if (!np || !of_device_is_available(np))
633 goto out;
635 ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
636 if (!ulpi) {
637 err = -ENOMEM;
638 goto out;
641 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
642 if (err < 0)
643 goto out;
645 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
647 ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
648 if (IS_ERR(ulpi->base.lane)) {
649 err = PTR_ERR(ulpi->base.lane);
650 goto out;
653 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
654 if (err < 0) {
655 tegra_xusb_port_unregister(&ulpi->base);
656 goto out;
659 list_add_tail(&ulpi->base.list, &padctl->ports);
661 out:
662 of_node_put(np);
663 return err;
666 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
668 /* XXX */
669 return 0;
672 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
673 unsigned int index)
675 struct tegra_xusb_hsic_port *hsic;
676 struct device_node *np;
677 int err = 0;
679 np = tegra_xusb_find_port_node(padctl, "hsic", index);
680 if (!np || !of_device_is_available(np))
681 goto out;
683 hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
684 if (!hsic) {
685 err = -ENOMEM;
686 goto out;
689 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
690 if (err < 0)
691 goto out;
693 hsic->base.ops = padctl->soc->ports.hsic.ops;
695 hsic->base.lane = hsic->base.ops->map(&hsic->base);
696 if (IS_ERR(hsic->base.lane)) {
697 err = PTR_ERR(hsic->base.lane);
698 goto out;
701 err = tegra_xusb_hsic_port_parse_dt(hsic);
702 if (err < 0) {
703 tegra_xusb_port_unregister(&hsic->base);
704 goto out;
707 list_add_tail(&hsic->base.list, &padctl->ports);
709 out:
710 of_node_put(np);
711 return err;
714 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
716 struct tegra_xusb_port *port = &usb3->base;
717 struct device_node *np = port->dev.of_node;
718 u32 value;
719 int err;
721 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
722 if (err < 0) {
723 dev_err(&port->dev, "failed to read port: %d\n", err);
724 return err;
727 usb3->port = value;
729 usb3->internal = of_property_read_bool(np, "nvidia,internal");
731 usb3->supply = devm_regulator_get(&port->dev, "vbus");
732 if (IS_ERR(usb3->supply))
733 return PTR_ERR(usb3->supply);
735 return 0;
738 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
739 unsigned int index)
741 struct tegra_xusb_usb3_port *usb3;
742 struct device_node *np;
743 int err = 0;
746 * If there is no supplemental configuration in the device tree the
747 * port is unusable. But it is valid to configure only a single port,
748 * hence return 0 instead of an error to allow ports to be optional.
750 np = tegra_xusb_find_port_node(padctl, "usb3", index);
751 if (!np || !of_device_is_available(np))
752 goto out;
754 usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
755 if (!usb3) {
756 err = -ENOMEM;
757 goto out;
760 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
761 if (err < 0)
762 goto out;
764 usb3->base.ops = padctl->soc->ports.usb3.ops;
766 usb3->base.lane = usb3->base.ops->map(&usb3->base);
767 if (IS_ERR(usb3->base.lane)) {
768 err = PTR_ERR(usb3->base.lane);
769 goto out;
772 err = tegra_xusb_usb3_port_parse_dt(usb3);
773 if (err < 0) {
774 tegra_xusb_port_unregister(&usb3->base);
775 goto out;
778 list_add_tail(&usb3->base.list, &padctl->ports);
780 out:
781 of_node_put(np);
782 return err;
785 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
787 struct tegra_xusb_port *port, *tmp;
789 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
790 list_del(&port->list);
791 tegra_xusb_port_unregister(port);
795 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
797 struct tegra_xusb_port *port;
798 unsigned int i;
799 int err = 0;
801 mutex_lock(&padctl->lock);
803 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
804 err = tegra_xusb_add_usb2_port(padctl, i);
805 if (err < 0)
806 goto remove_ports;
809 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
810 err = tegra_xusb_add_ulpi_port(padctl, i);
811 if (err < 0)
812 goto remove_ports;
815 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
816 err = tegra_xusb_add_hsic_port(padctl, i);
817 if (err < 0)
818 goto remove_ports;
821 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
822 err = tegra_xusb_add_usb3_port(padctl, i);
823 if (err < 0)
824 goto remove_ports;
827 list_for_each_entry(port, &padctl->ports, list) {
828 err = port->ops->enable(port);
829 if (err < 0)
830 dev_err(padctl->dev, "failed to enable port %s: %d\n",
831 dev_name(&port->dev), err);
834 goto unlock;
836 remove_ports:
837 __tegra_xusb_remove_ports(padctl);
838 unlock:
839 mutex_unlock(&padctl->lock);
840 return err;
843 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
845 mutex_lock(&padctl->lock);
846 __tegra_xusb_remove_ports(padctl);
847 mutex_unlock(&padctl->lock);
850 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
852 struct device_node *np = of_node_get(pdev->dev.of_node);
853 const struct tegra_xusb_padctl_soc *soc;
854 struct tegra_xusb_padctl *padctl;
855 const struct of_device_id *match;
856 struct resource *res;
857 int err;
859 /* for backwards compatibility with old device trees */
860 np = of_find_node_by_name(np, "pads");
861 if (!np) {
862 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
863 return tegra_xusb_padctl_legacy_probe(pdev);
866 of_node_put(np);
868 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
869 soc = match->data;
871 padctl = soc->ops->probe(&pdev->dev, soc);
872 if (IS_ERR(padctl))
873 return PTR_ERR(padctl);
875 platform_set_drvdata(pdev, padctl);
876 INIT_LIST_HEAD(&padctl->ports);
877 INIT_LIST_HEAD(&padctl->lanes);
878 INIT_LIST_HEAD(&padctl->pads);
879 mutex_init(&padctl->lock);
881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
882 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
883 if (IS_ERR(padctl->regs)) {
884 err = PTR_ERR(padctl->regs);
885 goto remove;
888 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
889 if (IS_ERR(padctl->rst)) {
890 err = PTR_ERR(padctl->rst);
891 goto remove;
894 err = reset_control_deassert(padctl->rst);
895 if (err < 0)
896 goto remove;
898 err = tegra_xusb_setup_pads(padctl);
899 if (err < 0) {
900 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
901 goto reset;
904 err = tegra_xusb_setup_ports(padctl);
905 if (err) {
906 dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
907 goto remove_pads;
910 return 0;
912 remove_pads:
913 tegra_xusb_remove_pads(padctl);
914 reset:
915 reset_control_assert(padctl->rst);
916 remove:
917 soc->ops->remove(padctl);
918 return err;
921 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
923 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
924 int err;
926 tegra_xusb_remove_ports(padctl);
927 tegra_xusb_remove_pads(padctl);
929 err = reset_control_assert(padctl->rst);
930 if (err < 0)
931 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
933 padctl->soc->ops->remove(padctl);
935 return err;
938 static struct platform_driver tegra_xusb_padctl_driver = {
939 .driver = {
940 .name = "tegra-xusb-padctl",
941 .of_match_table = tegra_xusb_padctl_of_match,
943 .probe = tegra_xusb_padctl_probe,
944 .remove = tegra_xusb_padctl_remove,
946 module_platform_driver(tegra_xusb_padctl_driver);
948 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
950 struct tegra_xusb_padctl *padctl;
951 struct platform_device *pdev;
952 struct device_node *np;
954 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
955 if (!np)
956 return ERR_PTR(-EINVAL);
959 * This is slightly ugly. A better implementation would be to keep a
960 * registry of pad controllers, but since there will almost certainly
961 * only ever be one per SoC that would be a little overkill.
963 pdev = of_find_device_by_node(np);
964 if (!pdev) {
965 of_node_put(np);
966 return ERR_PTR(-ENODEV);
969 of_node_put(np);
971 padctl = platform_get_drvdata(pdev);
972 if (!padctl) {
973 put_device(&pdev->dev);
974 return ERR_PTR(-EPROBE_DEFER);
977 return padctl;
979 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
981 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
983 if (padctl)
984 put_device(padctl->dev);
986 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
988 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
989 unsigned int port)
991 if (padctl->soc->ops->usb3_save_context)
992 return padctl->soc->ops->usb3_save_context(padctl, port);
994 return -ENOSYS;
996 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
998 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
999 unsigned int port, bool idle)
1001 if (padctl->soc->ops->hsic_set_idle)
1002 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1004 return -ENOSYS;
1006 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1008 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1009 unsigned int port, bool enable)
1011 if (padctl->soc->ops->usb3_set_lfps_detect)
1012 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1013 enable);
1015 return -ENOSYS;
1017 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1019 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1020 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1021 MODULE_LICENSE("GPL v2");