0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <dt-bindings/phy/phy.h>
0015 #include <linux/init.h>
0016 #include <linux/io.h>
0017 #include <linux/kernel.h>
0018 #include <linux/of_address.h>
0019 #include <linux/phy/phy.h>
0020 #include <linux/platform_device.h>
0021
0022 #define USB2_PHY_CONFIG_DISABLE BIT(0)
0023
0024 struct armada375_cluster_phy {
0025 struct phy *phy;
0026 void __iomem *reg;
0027 bool use_usb3;
0028 int phy_provided;
0029 };
0030
0031 static int armada375_usb_phy_init(struct phy *phy)
0032 {
0033 struct armada375_cluster_phy *cluster_phy;
0034 u32 reg;
0035
0036 cluster_phy = phy_get_drvdata(phy);
0037 if (!cluster_phy)
0038 return -ENODEV;
0039
0040 reg = readl(cluster_phy->reg);
0041 if (cluster_phy->use_usb3)
0042 reg |= USB2_PHY_CONFIG_DISABLE;
0043 else
0044 reg &= ~USB2_PHY_CONFIG_DISABLE;
0045 writel(reg, cluster_phy->reg);
0046
0047 return 0;
0048 }
0049
0050 static const struct phy_ops armada375_usb_phy_ops = {
0051 .init = armada375_usb_phy_init,
0052 .owner = THIS_MODULE,
0053 };
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 static struct phy *armada375_usb_phy_xlate(struct device *dev,
0064 struct of_phandle_args *args)
0065 {
0066 struct armada375_cluster_phy *cluster_phy = dev_get_drvdata(dev);
0067
0068 if (!cluster_phy)
0069 return ERR_PTR(-ENODEV);
0070
0071
0072
0073
0074
0075
0076
0077 if (WARN_ON((cluster_phy->phy_provided != PHY_NONE) &&
0078 (cluster_phy->phy_provided != args->args[0]))) {
0079 dev_err(dev, "This PHY has already been provided!\n");
0080 dev_err(dev, "Check your device tree, only one controller can use it\n.");
0081 if (args->args[0] == PHY_TYPE_USB2)
0082 return ERR_PTR(-EBUSY);
0083 else
0084 return ERR_PTR(-ENODEV);
0085 }
0086
0087 if (args->args[0] == PHY_TYPE_USB2)
0088 cluster_phy->use_usb3 = false;
0089 else if (args->args[0] == PHY_TYPE_USB3)
0090 cluster_phy->use_usb3 = true;
0091 else {
0092 dev_err(dev, "Invalid PHY mode\n");
0093 return ERR_PTR(-ENODEV);
0094 }
0095
0096
0097 cluster_phy->phy_provided = args->args[0];
0098
0099 return cluster_phy->phy;
0100 }
0101
0102 static int armada375_usb_phy_probe(struct platform_device *pdev)
0103 {
0104 struct device *dev = &pdev->dev;
0105 struct phy *phy;
0106 struct phy_provider *phy_provider;
0107 void __iomem *usb_cluster_base;
0108 struct armada375_cluster_phy *cluster_phy;
0109
0110 cluster_phy = devm_kzalloc(dev, sizeof(*cluster_phy), GFP_KERNEL);
0111 if (!cluster_phy)
0112 return -ENOMEM;
0113
0114 usb_cluster_base = devm_platform_ioremap_resource(pdev, 0);
0115 if (IS_ERR(usb_cluster_base))
0116 return PTR_ERR(usb_cluster_base);
0117
0118 phy = devm_phy_create(dev, NULL, &armada375_usb_phy_ops);
0119 if (IS_ERR(phy)) {
0120 dev_err(dev, "failed to create PHY\n");
0121 return PTR_ERR(phy);
0122 }
0123
0124 cluster_phy->phy = phy;
0125 cluster_phy->reg = usb_cluster_base;
0126
0127 dev_set_drvdata(dev, cluster_phy);
0128 phy_set_drvdata(phy, cluster_phy);
0129
0130 phy_provider = devm_of_phy_provider_register(&pdev->dev,
0131 armada375_usb_phy_xlate);
0132 return PTR_ERR_OR_ZERO(phy_provider);
0133 }
0134
0135 static const struct of_device_id of_usb_cluster_table[] = {
0136 { .compatible = "marvell,armada-375-usb-cluster", },
0137 { },
0138 };
0139
0140 static struct platform_driver armada375_usb_phy_driver = {
0141 .probe = armada375_usb_phy_probe,
0142 .driver = {
0143 .of_match_table = of_usb_cluster_table,
0144 .name = "armada-375-usb-cluster",
0145 }
0146 };
0147 builtin_platform_driver(armada375_usb_phy_driver);