fa3487d392e95f4ab5632eadad8ba7301f88bd4b
[librecmc/librecmc.git] / target / linux / ath79 / patches-4.14 / 0004-phy-add-ath79-usb-phys.patch
1 From 08c9d6ceef01893678a5d2e8a15517c745417f21 Mon Sep 17 00:00:00 2001
2 From: John Crispin <john@phrozen.org>
3 Date: Tue, 6 Mar 2018 10:04:05 +0100
4 Subject: [PATCH 04/27] phy: add ath79 usb phys
5
6 Signed-off-by: John Crispin <john@phrozen.org>
7 ---
8  drivers/phy/Kconfig          |  16 ++++++
9  drivers/phy/Makefile         |   2 +
10  drivers/phy/phy-ar7100-usb.c | 124 +++++++++++++++++++++++++++++++++++++++++++
11  drivers/phy/phy-ar7200-usb.c | 108 +++++++++++++++++++++++++++++++++++++
12  4 files changed, 250 insertions(+)
13  create mode 100644 drivers/phy/phy-ar7100-usb.c
14  create mode 100644 drivers/phy/phy-ar7200-usb.c
15
16 --- a/drivers/phy/Kconfig
17 +++ b/drivers/phy/Kconfig
18 @@ -15,6 +15,22 @@ config GENERIC_PHY
19           phy users can obtain reference to the PHY. All the users of this
20           framework should select this config.
21  
22 +config PHY_AR7100_USB
23 +       tristate "Atheros AR7100 USB PHY driver"
24 +       depends on ATH79 || COMPILE_TEST
25 +       default y if USB_EHCI_HCD_PLATFORM
26 +       select PHY_SIMPLE
27 +       help
28 +         Enable this to support the USB PHY on Atheros AR7100 SoCs.
29 +
30 +config PHY_AR7200_USB
31 +       tristate "Atheros AR7200 USB PHY driver"
32 +       depends on ATH79 || COMPILE_TEST
33 +       default y if USB_EHCI_HCD_PLATFORM
34 +       select PHY_SIMPLE
35 +       help
36 +         Enable this to support the USB PHY on Atheros AR7200 SoCs.
37 +
38  config PHY_LPC18XX_USB_OTG
39         tristate "NXP LPC18xx/43xx SoC USB OTG PHY driver"
40         depends on OF && (ARCH_LPC18XX || COMPILE_TEST)
41 --- a/drivers/phy/Makefile
42 +++ b/drivers/phy/Makefile
43 @@ -4,6 +4,8 @@
44  #
45  
46  obj-$(CONFIG_GENERIC_PHY)              += phy-core.o
47 +obj-$(CONFIG_PHY_AR7100_USB)           += phy-ar7100-usb.o
48 +obj-$(CONFIG_PHY_AR7200_USB)           += phy-ar7200-usb.o
49  obj-$(CONFIG_PHY_LPC18XX_USB_OTG)      += phy-lpc18xx-usb-otg.o
50  obj-$(CONFIG_PHY_XGENE)                        += phy-xgene.o
51  obj-$(CONFIG_PHY_PISTACHIO_USB)                += phy-pistachio-usb.o
52 --- /dev/null
53 +++ b/drivers/phy/phy-ar7100-usb.c
54 @@ -0,0 +1,140 @@
55 +/*
56 + * Copyright (C) 2018 John Crispin <john@phrozen.org>
57 + *
58 + * This program is free software; you can redistribute it and/or modify
59 + * it under the terms of the GNU General Public License as published by
60 + * the Free Software Foundation; either version 2 of the License, or
61 + * (at your option) any later version.
62 + */
63 +
64 +#include <linux/module.h>
65 +#include <linux/platform_device.h>
66 +#include <linux/phy/phy.h>
67 +#include <linux/delay.h>
68 +#include <linux/reset.h>
69 +#include <linux/of_gpio.h>
70 +
71 +#include <asm/mach-ath79/ath79.h>
72 +#include <asm/mach-ath79/ar71xx_regs.h>
73 +
74 +struct ar7100_usb_phy {
75 +       struct reset_control    *rst_phy;
76 +       struct reset_control    *rst_host;
77 +       struct reset_control    *rst_ohci_dll;
78 +       void __iomem            *io_base;
79 +       struct phy              *phy;
80 +       int                     gpio;
81 +};
82 +
83 +static int ar7100_usb_phy_power_off(struct phy *phy)
84 +{
85 +       struct ar7100_usb_phy *priv = phy_get_drvdata(phy);
86 +       int err = 0;
87 +
88 +       err |= reset_control_assert(priv->rst_host);
89 +       err |= reset_control_assert(priv->rst_phy);
90 +       err |= reset_control_assert(priv->rst_ohci_dll);
91 +
92 +       return err;
93 +}
94 +
95 +static int ar7100_usb_phy_power_on(struct phy *phy)
96 +{
97 +       struct ar7100_usb_phy *priv = phy_get_drvdata(phy);
98 +       int err = 0;
99 +
100 +       err |= ar7100_usb_phy_power_off(phy);
101 +       mdelay(100);
102 +       err |= reset_control_deassert(priv->rst_ohci_dll);
103 +       err |= reset_control_deassert(priv->rst_phy);
104 +       err |= reset_control_deassert(priv->rst_host);
105 +       mdelay(500);
106 +       iowrite32(0xf0000, priv->io_base + AR71XX_USB_CTRL_REG_CONFIG);
107 +       iowrite32(0x20c00, priv->io_base + AR71XX_USB_CTRL_REG_FLADJ);
108 +
109 +       return err;
110 +}
111 +
112 +static const struct phy_ops ar7100_usb_phy_ops = {
113 +       .power_on       = ar7100_usb_phy_power_on,
114 +       .power_off      = ar7100_usb_phy_power_off,
115 +       .owner          = THIS_MODULE,
116 +};
117 +
118 +static int ar7100_usb_phy_probe(struct platform_device *pdev)
119 +{
120 +       struct phy_provider *phy_provider;
121 +       struct resource *res;
122 +       struct ar7100_usb_phy *priv;
123 +
124 +       priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
125 +       if (!priv)
126 +               return -ENOMEM;
127 +
128 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 +       priv->io_base = devm_ioremap_resource(&pdev->dev, res);
130 +       if (IS_ERR(priv->io_base))
131 +               return PTR_ERR(priv->io_base);
132 +
133 +       priv->rst_phy = devm_reset_control_get(&pdev->dev, "usb-phy");
134 +       if (IS_ERR(priv->rst_phy)) {
135 +               dev_err(&pdev->dev, "phy reset is missing\n");
136 +               return PTR_ERR(priv->rst_phy);
137 +       }
138 +
139 +       priv->rst_host = devm_reset_control_get(&pdev->dev, "usb-host");
140 +       if (IS_ERR(priv->rst_host)) {
141 +               dev_err(&pdev->dev, "host reset is missing\n");
142 +               return PTR_ERR(priv->rst_host);
143 +       }
144 +
145 +       priv->rst_ohci_dll = devm_reset_control_get(&pdev->dev, "usb-ohci-dll");
146 +       if (IS_ERR(priv->rst_ohci_dll)) {
147 +               dev_err(&pdev->dev, "ohci-dll reset is missing\n");
148 +               return PTR_ERR(priv->rst_host);
149 +       }
150 +
151 +       priv->phy = devm_phy_create(&pdev->dev, NULL, &ar7100_usb_phy_ops);
152 +       if (IS_ERR(priv->phy)) {
153 +               dev_err(&pdev->dev, "failed to create PHY\n");
154 +               return PTR_ERR(priv->phy);
155 +       }
156 +
157 +       priv->gpio = of_get_gpio(pdev->dev.of_node, 0);
158 +       if (priv->gpio >= 0) {
159 +               int ret = devm_gpio_request(&pdev->dev, priv->gpio, dev_name(&pdev->dev));
160 +
161 +               if (ret) {
162 +                       dev_err(&pdev->dev, "failed to request gpio\n");
163 +                       return ret;
164 +               }
165 +               gpio_export_with_name(priv->gpio, 0, dev_name(&pdev->dev));
166 +               gpio_set_value(priv->gpio, 1);
167 +       }
168 +
169 +       phy_set_drvdata(priv->phy, priv);
170 +
171 +       phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
172 +
173 +
174 +       return PTR_ERR_OR_ZERO(phy_provider);
175 +}
176 +
177 +static const struct of_device_id ar7100_usb_phy_of_match[] = {
178 +       { .compatible = "qca,ar7100-usb-phy" },
179 +       {}
180 +};
181 +MODULE_DEVICE_TABLE(of, ar7100_usb_phy_of_match);
182 +
183 +static struct platform_driver ar7100_usb_phy_driver = {
184 +       .probe  = ar7100_usb_phy_probe,
185 +       .driver = {
186 +               .of_match_table = ar7100_usb_phy_of_match,
187 +               .name           = "ar7100-usb-phy",
188 +       }
189 +};
190 +module_platform_driver(ar7100_usb_phy_driver);
191 +
192 +MODULE_DESCRIPTION("ATH79 USB PHY driver");
193 +MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
194 +MODULE_LICENSE("GPL");
195 --- /dev/null
196 +++ b/drivers/phy/phy-ar7200-usb.c
197 @@ -0,0 +1,135 @@
198 +/*
199 + * Copyright (C) 2015 Alban Bedel <albeu@free.fr>
200 + *
201 + * This program is free software; you can redistribute it and/or modify
202 + * it under the terms of the GNU General Public License as published by
203 + * the Free Software Foundation; either version 2 of the License, or
204 + * (at your option) any later version.
205 + */
206 +
207 +#include <linux/module.h>
208 +#include <linux/platform_device.h>
209 +#include <linux/phy/phy.h>
210 +#include <linux/reset.h>
211 +#include <linux/of_gpio.h>
212 +
213 +struct ar7200_usb_phy {
214 +       struct reset_control    *rst_phy;
215 +       struct reset_control    *rst_phy_analog;
216 +       struct reset_control    *suspend_override;
217 +       struct phy              *phy;
218 +       int                     gpio;
219 +};
220 +
221 +static int ar7200_usb_phy_power_on(struct phy *phy)
222 +{
223 +       struct ar7200_usb_phy *priv = phy_get_drvdata(phy);
224 +       int err = 0;
225 +
226 +       if (priv->suspend_override)
227 +               err = reset_control_assert(priv->suspend_override);
228 +       if (priv->rst_phy)
229 +               err |= reset_control_deassert(priv->rst_phy);
230 +       if (priv->rst_phy_analog)
231 +               err |= reset_control_deassert(priv->rst_phy_analog);
232 +
233 +       return err;
234 +}
235 +
236 +static int ar7200_usb_phy_power_off(struct phy *phy)
237 +{
238 +       struct ar7200_usb_phy *priv = phy_get_drvdata(phy);
239 +       int err = 0;
240 +
241 +       if (priv->suspend_override)
242 +               err = reset_control_deassert(priv->suspend_override);
243 +       if (priv->rst_phy)
244 +               err |= reset_control_assert(priv->rst_phy);
245 +       if (priv->rst_phy_analog)
246 +               err |= reset_control_assert(priv->rst_phy_analog);
247 +
248 +       return err;
249 +}
250 +
251 +static const struct phy_ops ar7200_usb_phy_ops = {
252 +       .power_on       = ar7200_usb_phy_power_on,
253 +       .power_off      = ar7200_usb_phy_power_off,
254 +       .owner          = THIS_MODULE,
255 +};
256 +
257 +static int ar7200_usb_phy_probe(struct platform_device *pdev)
258 +{
259 +       struct phy_provider *phy_provider;
260 +       struct ar7200_usb_phy *priv;
261 +
262 +       priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
263 +       if (!priv)
264 +               return -ENOMEM;
265 +
266 +       priv->rst_phy = devm_reset_control_get(&pdev->dev, "usb-phy");
267 +       if (IS_ERR(priv->rst_phy)) {
268 +               dev_err(&pdev->dev, "phy reset is missing\n");
269 +               return PTR_ERR(priv->rst_phy);
270 +       }
271 +
272 +       priv->rst_phy_analog = devm_reset_control_get_optional(
273 +               &pdev->dev, "usb-phy-analog");
274 +       if (IS_ERR(priv->rst_phy_analog)) {
275 +               if (PTR_ERR(priv->rst_phy_analog) == -ENOENT)
276 +                       priv->rst_phy_analog = NULL;
277 +               else
278 +                       return PTR_ERR(priv->rst_phy_analog);
279 +       }
280 +
281 +       priv->suspend_override = devm_reset_control_get_optional(
282 +               &pdev->dev, "usb-suspend-override");
283 +       if (IS_ERR(priv->suspend_override)) {
284 +               if (PTR_ERR(priv->suspend_override) == -ENOENT)
285 +                       priv->suspend_override = NULL;
286 +               else
287 +                       return PTR_ERR(priv->suspend_override);
288 +       }
289 +
290 +       priv->phy = devm_phy_create(&pdev->dev, NULL, &ar7200_usb_phy_ops);
291 +       if (IS_ERR(priv->phy)) {
292 +               dev_err(&pdev->dev, "failed to create PHY\n");
293 +               return PTR_ERR(priv->phy);
294 +       }
295 +
296 +       priv->gpio = of_get_gpio(pdev->dev.of_node, 0);
297 +       if (priv->gpio >= 0) {
298 +               int ret = devm_gpio_request(&pdev->dev, priv->gpio, dev_name(&pdev->dev));
299 +
300 +               if (ret) {
301 +                       dev_err(&pdev->dev, "failed to request gpio\n");
302 +                       return ret;
303 +               }
304 +               gpio_export_with_name(priv->gpio, 0, dev_name(&pdev->dev));
305 +               gpio_set_value(priv->gpio, 1);
306 +       }
307 +
308 +       phy_set_drvdata(priv->phy, priv);
309 +
310 +       phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
311 +
312 +       return PTR_ERR_OR_ZERO(phy_provider);
313 +}
314 +
315 +static const struct of_device_id ar7200_usb_phy_of_match[] = {
316 +       { .compatible = "qca,ar7200-usb-phy" },
317 +       {}
318 +};
319 +MODULE_DEVICE_TABLE(of, ar7200_usb_phy_of_match);
320 +
321 +static struct platform_driver ar7200_usb_phy_driver = {
322 +       .probe  = ar7200_usb_phy_probe,
323 +       .driver = {
324 +               .of_match_table = ar7200_usb_phy_of_match,
325 +               .name           = "ar7200-usb-phy",
326 +       }
327 +};
328 +module_platform_driver(ar7200_usb_phy_driver);
329 +
330 +MODULE_DESCRIPTION("ATH79 USB PHY driver");
331 +MODULE_AUTHOR("Alban Bedel <albeu@free.fr>");
332 +MODULE_LICENSE("GPL");