]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/phy/marvell/phy-armada375-usb2.c
phy: make phy-armada375-usb2 explicitly non-modular
[linux.git] / drivers / phy / marvell / phy-armada375-usb2.c
1 /*
2  * USB cluster support for Armada 375 platform.
3  *
4  * Copyright (C) 2014 Marvell
5  *
6  * Gregory CLEMENT <gregory.clement@free-electrons.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2 or later. This program is licensed "as is"
10  * without any warranty of any kind, whether express or implied.
11  *
12  * Armada 375 comes with an USB2 host and device controller and an
13  * USB3 controller. The USB cluster control register allows to manage
14  * common features of both USB controllers.
15  */
16
17 #include <dt-bindings/phy/phy.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/of_address.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24
25 #define USB2_PHY_CONFIG_DISABLE BIT(0)
26
27 struct armada375_cluster_phy {
28         struct phy *phy;
29         void __iomem *reg;
30         bool use_usb3;
31         int phy_provided;
32 };
33
34 static int armada375_usb_phy_init(struct phy *phy)
35 {
36         struct armada375_cluster_phy *cluster_phy;
37         u32 reg;
38
39         cluster_phy = phy_get_drvdata(phy);
40         if (!cluster_phy)
41                 return -ENODEV;
42
43         reg = readl(cluster_phy->reg);
44         if (cluster_phy->use_usb3)
45                 reg |= USB2_PHY_CONFIG_DISABLE;
46         else
47                 reg &= ~USB2_PHY_CONFIG_DISABLE;
48         writel(reg, cluster_phy->reg);
49
50         return 0;
51 }
52
53 static const struct phy_ops armada375_usb_phy_ops = {
54         .init = armada375_usb_phy_init,
55         .owner = THIS_MODULE,
56 };
57
58 /*
59  * Only one controller can use this PHY. We shouldn't have the case
60  * when two controllers want to use this PHY. But if this case occurs
61  * then we provide a phy to the first one and return an error for the
62  * next one. This error has also to be an error returned by
63  * devm_phy_optional_get() so different from ENODEV for USB2. In the
64  * USB3 case it still optional and we use ENODEV.
65  */
66 static struct phy *armada375_usb_phy_xlate(struct device *dev,
67                                         struct of_phandle_args *args)
68 {
69         struct armada375_cluster_phy *cluster_phy = dev_get_drvdata(dev);
70
71         if (!cluster_phy)
72                 return  ERR_PTR(-ENODEV);
73
74         /*
75          * Either the phy had never been requested and then the first
76          * usb claiming it can get it, or it had already been
77          * requested in this case, we only allow to use it with the
78          * same configuration.
79          */
80         if (WARN_ON((cluster_phy->phy_provided != PHY_NONE) &&
81                         (cluster_phy->phy_provided != args->args[0]))) {
82                 dev_err(dev, "This PHY has already been provided!\n");
83                 dev_err(dev, "Check your device tree, only one controller can use it\n.");
84                 if (args->args[0] == PHY_TYPE_USB2)
85                         return ERR_PTR(-EBUSY);
86                 else
87                         return ERR_PTR(-ENODEV);
88         }
89
90         if (args->args[0] == PHY_TYPE_USB2)
91                 cluster_phy->use_usb3 = false;
92         else if (args->args[0] == PHY_TYPE_USB3)
93                 cluster_phy->use_usb3 = true;
94         else {
95                 dev_err(dev, "Invalid PHY mode\n");
96                 return ERR_PTR(-ENODEV);
97         }
98
99         /* Store which phy mode is used for next test */
100         cluster_phy->phy_provided = args->args[0];
101
102         return cluster_phy->phy;
103 }
104
105 static int armada375_usb_phy_probe(struct platform_device *pdev)
106 {
107         struct device *dev = &pdev->dev;
108         struct phy *phy;
109         struct phy_provider *phy_provider;
110         void __iomem *usb_cluster_base;
111         struct resource *res;
112         struct armada375_cluster_phy *cluster_phy;
113
114         cluster_phy = devm_kzalloc(dev, sizeof(*cluster_phy), GFP_KERNEL);
115         if (!cluster_phy)
116                 return  -ENOMEM;
117
118         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
119         usb_cluster_base = devm_ioremap_resource(&pdev->dev, res);
120         if (IS_ERR(usb_cluster_base))
121                 return PTR_ERR(usb_cluster_base);
122
123         phy = devm_phy_create(dev, NULL, &armada375_usb_phy_ops);
124         if (IS_ERR(phy)) {
125                 dev_err(dev, "failed to create PHY\n");
126                 return PTR_ERR(phy);
127         }
128
129         cluster_phy->phy = phy;
130         cluster_phy->reg = usb_cluster_base;
131
132         dev_set_drvdata(dev, cluster_phy);
133         phy_set_drvdata(phy, cluster_phy);
134
135         phy_provider = devm_of_phy_provider_register(&pdev->dev,
136                                                      armada375_usb_phy_xlate);
137         return PTR_ERR_OR_ZERO(phy_provider);
138 }
139
140 static const struct of_device_id of_usb_cluster_table[] = {
141         { .compatible = "marvell,armada-375-usb-cluster", },
142         { /* end of list */ },
143 };
144
145 static struct platform_driver armada375_usb_phy_driver = {
146         .probe  = armada375_usb_phy_probe,
147         .driver = {
148                 .of_match_table = of_usb_cluster_table,
149                 .name  = "armada-375-usb-cluster",
150         }
151 };
152 builtin_platform_driver(armada375_usb_phy_driver);