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
14 #include <linux/delay.h>
16 #include <linux/mailbox_client.h>
17 #include <linux/module.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>
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;
38 if (args->args_count != 0)
39 return ERR_PTR(-EINVAL);
41 for (i = 0; i < pad->soc->num_lanes; i++) {
45 if (pad->lanes[i]->dev.of_node == args->np) {
52 phy = ERR_PTR(-ENODEV);
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,
64 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
66 .compatible = "nvidia,tegra210-xusb-padctl",
67 .data = &tegra210_xusb_padctl_soc,
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");
84 np = of_find_node_by_name(np, name);
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");
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)
109 for (i = 0; i < lane->soc->num_funcs; i++)
110 if (strcmp(function, lane->soc->funcs[i]) == 0)
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;
123 err = of_property_read_string(np, "nvidia,function", &function);
127 err = tegra_xusb_lane_lookup_function(lane, function);
129 dev_err(dev, "invalid function \"%s\" for lane \"%s\"\n",
134 lane->function = err;
139 static void tegra_xusb_lane_destroy(struct phy *phy)
142 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
144 lane->pad->ops->remove(lane);
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)
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);
177 err = device_add(&pad->dev);
184 device_unregister(&pad->dev);
188 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
189 const struct phy_ops *ops)
191 struct device_node *children;
196 children = of_find_node_by_name(pad->dev.of_node, "lanes");
200 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
203 of_node_put(children);
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)) {
217 pad->lanes[i] = phy_create(&pad->dev, np, ops);
218 if (IS_ERR(pad->lanes[i])) {
219 err = PTR_ERR(pad->lanes[i]);
224 lane = pad->ops->probe(pad, np, i);
226 phy_destroy(pad->lanes[i]);
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);
246 tegra_xusb_lane_destroy(pad->lanes[i]);
248 of_node_put(children);
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);
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;
273 np = tegra_xusb_find_pad_node(padctl, soc->name);
274 if (!np || !of_device_is_available(np))
277 pad = soc->ops->probe(padctl, soc, np);
280 dev_err(padctl->dev, "failed to create pad %s: %d\n",
285 /* XXX move this into ->probe() to avoid string comparison */
286 if (strcmp(soc->name, "pcie") == 0)
289 if (strcmp(soc->name, "sata") == 0)
292 if (strcmp(soc->name, "usb2") == 0)
295 if (strcmp(soc->name, "ulpi") == 0)
298 if (strcmp(soc->name, "hsic") == 0)
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;
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)
338 for (i = 0; i < pad->soc->num_lanes; i++) {
339 struct tegra_xusb_lane *lane;
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;
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];
359 pad = tegra_xusb_pad_create(padctl, soc);
362 dev_err(padctl->dev, "failed to create pad %s: %d\n",
364 __tegra_xusb_remove_pads(padctl);
365 mutex_unlock(&padctl->lock);
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);
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,
394 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
397 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
399 return ERR_PTR(-ENOMEM);
401 list_for_each_entry(lane, &padctl->lanes, list) {
402 if (strcmp(lane->soc->name, name) == 0) {
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)
423 lane = tegra_xusb_find_lane(port->padctl, map->type,
428 if (!tegra_xusb_lane_check(lane, function))
432 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
433 map->type, map->index, match->soc->name);
441 static struct device_node *
442 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
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");
454 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
455 np = of_find_node_by_name(np, name);
462 struct tegra_xusb_port *
463 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
466 struct tegra_xusb_port *port;
467 struct device_node *np;
469 np = tegra_xusb_find_port_node(padctl, type, index);
473 list_for_each_entry(port, &padctl->ports, list) {
474 if (np == port->dev.of_node) {
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);
492 return to_usb2_port(port);
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);
504 return to_usb3_port(port);
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,
525 INIT_LIST_HEAD(&port->list);
526 port->padctl = padctl;
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);
538 err = device_add(&port->dev);
545 device_unregister(&port->dev);
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);
568 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
571 struct tegra_xusb_usb2_port *usb2;
572 struct device_node *np;
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))
583 usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
589 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
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);
601 err = tegra_xusb_usb2_port_parse_dt(usb2);
603 tegra_xusb_port_unregister(&usb2->base);
607 list_add_tail(&usb2->base.list, &padctl->ports);
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");
624 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
627 struct tegra_xusb_ulpi_port *ulpi;
628 struct device_node *np;
631 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
632 if (!np || !of_device_is_available(np))
635 ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
641 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
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);
653 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
655 tegra_xusb_port_unregister(&ulpi->base);
659 list_add_tail(&ulpi->base.list, &padctl->ports);
666 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
672 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
675 struct tegra_xusb_hsic_port *hsic;
676 struct device_node *np;
679 np = tegra_xusb_find_port_node(padctl, "hsic", index);
680 if (!np || !of_device_is_available(np))
683 hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
689 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
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);
701 err = tegra_xusb_hsic_port_parse_dt(hsic);
703 tegra_xusb_port_unregister(&hsic->base);
707 list_add_tail(&hsic->base.list, &padctl->ports);
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;
721 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
723 dev_err(&port->dev, "failed to read port: %d\n", err);
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);
738 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
741 struct tegra_xusb_usb3_port *usb3;
742 struct device_node *np;
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))
754 usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
760 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
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);
772 err = tegra_xusb_usb3_port_parse_dt(usb3);
774 tegra_xusb_port_unregister(&usb3->base);
778 list_add_tail(&usb3->base.list, &padctl->ports);
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;
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);
809 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
810 err = tegra_xusb_add_ulpi_port(padctl, i);
815 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
816 err = tegra_xusb_add_hsic_port(padctl, i);
821 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
822 err = tegra_xusb_add_usb3_port(padctl, i);
827 list_for_each_entry(port, &padctl->ports, list) {
828 err = port->ops->enable(port);
830 dev_err(padctl->dev, "failed to enable port %s: %d\n",
831 dev_name(&port->dev), err);
837 __tegra_xusb_remove_ports(padctl);
839 mutex_unlock(&padctl->lock);
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;
859 /* for backwards compatibility with old device trees */
860 np = of_find_node_by_name(np, "pads");
862 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
863 return tegra_xusb_padctl_legacy_probe(pdev);
868 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
871 padctl = soc->ops->probe(&pdev->dev, soc);
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);
888 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
889 if (IS_ERR(padctl->rst)) {
890 err = PTR_ERR(padctl->rst);
894 err = reset_control_deassert(padctl->rst);
898 err = tegra_xusb_setup_pads(padctl);
900 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
904 err = tegra_xusb_setup_ports(padctl);
906 dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
913 tegra_xusb_remove_pads(padctl);
915 reset_control_assert(padctl->rst);
917 soc->ops->remove(padctl);
921 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
923 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
926 tegra_xusb_remove_ports(padctl);
927 tegra_xusb_remove_pads(padctl);
929 err = reset_control_assert(padctl->rst);
931 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
933 padctl->soc->ops->remove(padctl);
938 static struct platform_driver tegra_xusb_padctl_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);
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);
966 return ERR_PTR(-ENODEV);
971 padctl = platform_get_drvdata(pdev);
973 put_device(&pdev->dev);
974 return ERR_PTR(-EPROBE_DEFER);
979 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
981 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *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,
991 if (padctl->soc->ops->usb3_save_context)
992 return padctl->soc->ops->usb3_save_context(padctl, port);
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);
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,
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");