sunxi: initial 3.14 patchset
[15.05/openwrt.git] / target / linux / sunxi / patches-3.14 / 196-usb-add-sunxi-phy-driver.patch
1 From 56feaa546c5ce4152fe14f725e9fc6b85f8a565b Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Sat, 4 Jan 2014 23:56:17 +0100
4 Subject: [PATCH] PHY: sunxi: Add driver for sunxi usb phy
5
6 The Allwinner A1x / A2x SoCs have 2 or 3 usb phys which are all accessed
7 through a single set of registers. Besides this there are also some other
8 phy related bits which need poking, which are per phy, but shared between the
9 ohci and ehci controllers, so these are also controlled from this new phy
10 driver.
11
12 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
13 Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
14 ---
15  .../devicetree/bindings/phy/sun4i-usb-phy.txt      |  26 ++
16  drivers/phy/Kconfig                                |  11 +
17  drivers/phy/Makefile                               |   1 +
18  drivers/phy/phy-sun4i-usb.c                        | 331 +++++++++++++++++++++
19  4 files changed, 369 insertions(+)
20  create mode 100644 Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt
21  create mode 100644 drivers/phy/phy-sun4i-usb.c
22
23 diff --git a/Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt b/Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt
24 new file mode 100644
25 index 0000000..a82361b
26 --- /dev/null
27 +++ b/Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt
28 @@ -0,0 +1,26 @@
29 +Allwinner sun4i USB PHY
30 +-----------------------
31 +
32 +Required properties:
33 +- compatible : should be one of "allwinner,sun4i-a10-usb-phy",
34 +  "allwinner,sun5i-a13-usb-phy" or "allwinner,sun7i-a20-usb-phy"
35 +- reg : a list of offset + length pairs
36 +- reg-names : "phy_ctrl", "pmu1" and for sun4i or sun7i "pmu2"
37 +- #phy-cells : from the generic phy bindings, must be 1
38 +- clocks : phandle + clock specifier for the phy clock
39 +- clock-names : "usb_phy"
40 +- resets : a list of phandle + reset specifier pairs
41 +- reset-names : "usb0_reset", "usb1_reset" and for sun4i or sun7i "usb2_reset"
42 +
43 +Example:
44 +       usbphy: phy@0x01c13400 {
45 +               #phy-cells = <1>;
46 +               compatible = "allwinner,sun4i-a10-usb-phy";
47 +               /* phy base regs, phy1 pmu reg, phy2 pmu reg */
48 +               reg = <0x01c13400 0x10 0x01c14800 0x4 0x01c1c800 0x4>;
49 +               reg-names = "phy_ctrl", "pmu1", "pmu2";
50 +               clocks = <&usb_clk 8>;
51 +               clock-names = "usb_phy";
52 +               resets = <&usb_clk 1>, <&usb_clk 2>;
53 +               reset-names = "usb1_reset", "usb2_reset";
54 +       };
55 diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig
56 index c7a551c..66f7c4e 100644
57 --- a/drivers/phy/Kconfig
58 +++ b/drivers/phy/Kconfig
59 @@ -65,4 +65,15 @@ config BCM_KONA_USB2_PHY
60         help
61           Enable this to support the Broadcom Kona USB 2.0 PHY.
62  
63 +config PHY_SUN4I_USB
64 +       tristate "Allwinner sunxi SoC USB PHY driver"
65 +       depends on ARCH_SUNXI && HAS_IOMEM && OF
66 +       select GENERIC_PHY
67 +       help
68 +         Enable this to support the transceiver that is part of Allwinner
69 +         sunxi SoCs.
70 +
71 +         This driver controls the entire USB PHY block, both the USB OTG
72 +         parts, as well as the 2 regular USB 2 host PHYs.
73 +
74  endmenu
75 diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile
76 index b57c253..9d4f8bb 100644
77 --- a/drivers/phy/Makefile
78 +++ b/drivers/phy/Makefile
79 @@ -9,3 +9,4 @@ obj-$(CONFIG_PHY_EXYNOS_MIPI_VIDEO)     += phy-exynos-mipi-video.o
80  obj-$(CONFIG_PHY_MVEBU_SATA)           += phy-mvebu-sata.o
81  obj-$(CONFIG_OMAP_USB2)                        += phy-omap-usb2.o
82  obj-$(CONFIG_TWL4030_USB)              += phy-twl4030-usb.o
83 +obj-$(CONFIG_PHY_SUN4I_USB)            += phy-sun4i-usb.o
84 diff --git a/drivers/phy/phy-sun4i-usb.c b/drivers/phy/phy-sun4i-usb.c
85 new file mode 100644
86 index 0000000..e6e6c4b
87 --- /dev/null
88 +++ b/drivers/phy/phy-sun4i-usb.c
89 @@ -0,0 +1,331 @@
90 +/*
91 + * Allwinner sun4i USB phy driver
92 + *
93 + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
94 + *
95 + * Based on code from
96 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
97 + *
98 + * Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
99 + * Copyright (C) 2013 Samsung Electronics Co., Ltd.
100 + * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
101 + *
102 + * This program is free software; you can redistribute it and/or modify
103 + * it under the terms of the GNU General Public License as published by
104 + * the Free Software Foundation; either version 2 of the License, or
105 + * (at your option) any later version.
106 + *
107 + * This program is distributed in the hope that it will be useful,
108 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
109 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
110 + * GNU General Public License for more details.
111 + */
112 +
113 +#include <linux/clk.h>
114 +#include <linux/io.h>
115 +#include <linux/kernel.h>
116 +#include <linux/module.h>
117 +#include <linux/mutex.h>
118 +#include <linux/of.h>
119 +#include <linux/of_address.h>
120 +#include <linux/phy/phy.h>
121 +#include <linux/platform_device.h>
122 +#include <linux/regulator/consumer.h>
123 +#include <linux/reset.h>
124 +
125 +#define REG_ISCR                       0x00
126 +#define REG_PHYCTL                     0x04
127 +#define REG_PHYBIST                    0x08
128 +#define REG_PHYTUNE                    0x0c
129 +
130 +#define PHYCTL_DATA                    BIT(7)
131 +
132 +#define SUNXI_AHB_ICHR8_EN             BIT(10)
133 +#define SUNXI_AHB_INCR4_BURST_EN       BIT(9)
134 +#define SUNXI_AHB_INCRX_ALIGN_EN       BIT(8)
135 +#define SUNXI_ULPI_BYPASS_EN           BIT(0)
136 +
137 +/* Common Control Bits for Both PHYs */
138 +#define PHY_PLL_BW                     0x03
139 +#define PHY_RES45_CAL_EN               0x0c
140 +
141 +/* Private Control Bits for Each PHY */
142 +#define PHY_TX_AMPLITUDE_TUNE          0x20
143 +#define PHY_TX_SLEWRATE_TUNE           0x22
144 +#define PHY_VBUSVALID_TH_SEL           0x25
145 +#define PHY_PULLUP_RES_SEL             0x27
146 +#define PHY_OTG_FUNC_EN                        0x28
147 +#define PHY_VBUS_DET_EN                        0x29
148 +#define PHY_DISCON_TH_SEL              0x2a
149 +
150 +#define MAX_PHYS                       3
151 +
152 +struct sun4i_usb_phy_data {
153 +       struct clk *clk;
154 +       void __iomem *base;
155 +       struct mutex mutex;
156 +       int num_phys;
157 +       u32 disc_thresh;
158 +       struct sun4i_usb_phy {
159 +               struct phy *phy;
160 +               void __iomem *pmu;
161 +               struct regulator *vbus;
162 +               struct reset_control *reset;
163 +               int index;
164 +       } phys[MAX_PHYS];
165 +};
166 +
167 +#define to_sun4i_usb_phy_data(phy) \
168 +       container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
169 +
170 +static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
171 +                               int len)
172 +{
173 +       struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
174 +       u32 temp, usbc_bit = BIT(phy->index * 2);
175 +       int i;
176 +
177 +       mutex_lock(&phy_data->mutex);
178 +
179 +       for (i = 0; i < len; i++) {
180 +               temp = readl(phy_data->base + REG_PHYCTL);
181 +
182 +               /* clear the address portion */
183 +               temp &= ~(0xff << 8);
184 +
185 +               /* set the address */
186 +               temp |= ((addr + i) << 8);
187 +               writel(temp, phy_data->base + REG_PHYCTL);
188 +
189 +               /* set the data bit and clear usbc bit*/
190 +               temp = readb(phy_data->base + REG_PHYCTL);
191 +               if (data & 0x1)
192 +                       temp |= PHYCTL_DATA;
193 +               else
194 +                       temp &= ~PHYCTL_DATA;
195 +               temp &= ~usbc_bit;
196 +               writeb(temp, phy_data->base + REG_PHYCTL);
197 +
198 +               /* pulse usbc_bit */
199 +               temp = readb(phy_data->base + REG_PHYCTL);
200 +               temp |= usbc_bit;
201 +               writeb(temp, phy_data->base + REG_PHYCTL);
202 +
203 +               temp = readb(phy_data->base + REG_PHYCTL);
204 +               temp &= ~usbc_bit;
205 +               writeb(temp, phy_data->base + REG_PHYCTL);
206 +
207 +               data >>= 1;
208 +       }
209 +       mutex_unlock(&phy_data->mutex);
210 +}
211 +
212 +static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
213 +{
214 +       u32 bits, reg_value;
215 +
216 +       if (!phy->pmu)
217 +               return;
218 +
219 +       bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
220 +               SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
221 +
222 +       reg_value = readl(phy->pmu);
223 +
224 +       if (enable)
225 +               reg_value |= bits;
226 +       else
227 +               reg_value &= ~bits;
228 +
229 +       writel(reg_value, phy->pmu);
230 +}
231 +
232 +static int sun4i_usb_phy_init(struct phy *_phy)
233 +{
234 +       struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
235 +       struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
236 +       int ret;
237 +
238 +       ret = clk_prepare_enable(data->clk);
239 +       if (ret)
240 +               return ret;
241 +
242 +       ret = reset_control_deassert(phy->reset);
243 +       if (ret) {
244 +               clk_disable_unprepare(data->clk);
245 +               return ret;
246 +       }
247 +
248 +       /* Adjust PHY's magnitude and rate */
249 +       sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
250 +
251 +       /* Disconnect threshold adjustment */
252 +       sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
253 +
254 +       sun4i_usb_phy_passby(phy, 1);
255 +
256 +       return 0;
257 +}
258 +
259 +static int sun4i_usb_phy_exit(struct phy *_phy)
260 +{
261 +       struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
262 +       struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
263 +
264 +       sun4i_usb_phy_passby(phy, 0);
265 +       reset_control_assert(phy->reset);
266 +       clk_disable_unprepare(data->clk);
267 +
268 +       return 0;
269 +}
270 +
271 +static int sun4i_usb_phy_power_on(struct phy *_phy)
272 +{
273 +       struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
274 +       int ret = 0;
275 +
276 +       if (phy->vbus)
277 +               ret = regulator_enable(phy->vbus);
278 +
279 +       return ret;
280 +}
281 +
282 +static int sun4i_usb_phy_power_off(struct phy *_phy)
283 +{
284 +       struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
285 +
286 +       if (phy->vbus)
287 +               regulator_disable(phy->vbus);
288 +
289 +       return 0;
290 +}
291 +
292 +static struct phy_ops sun4i_usb_phy_ops = {
293 +       .init           = sun4i_usb_phy_init,
294 +       .exit           = sun4i_usb_phy_exit,
295 +       .power_on       = sun4i_usb_phy_power_on,
296 +       .power_off      = sun4i_usb_phy_power_off,
297 +       .owner          = THIS_MODULE,
298 +};
299 +
300 +static struct phy *sun4i_usb_phy_xlate(struct device *dev,
301 +                                       struct of_phandle_args *args)
302 +{
303 +       struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
304 +
305 +       if (WARN_ON(args->args[0] == 0 || args->args[0] >= data->num_phys))
306 +               return ERR_PTR(-ENODEV);
307 +
308 +       return data->phys[args->args[0]].phy;
309 +}
310 +
311 +static int sun4i_usb_phy_probe(struct platform_device *pdev)
312 +{
313 +       struct sun4i_usb_phy_data *data;
314 +       struct device *dev = &pdev->dev;
315 +       struct device_node *np = dev->of_node;
316 +       void __iomem *pmu = NULL;
317 +       struct phy_provider *phy_provider;
318 +       struct reset_control *reset;
319 +       struct regulator *vbus;
320 +       struct resource *res;
321 +       struct phy *phy;
322 +       char name[16];
323 +       int i;
324 +
325 +       data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
326 +       if (!data)
327 +               return -ENOMEM;
328 +
329 +       mutex_init(&data->mutex);
330 +
331 +       if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy"))
332 +               data->num_phys = 2;
333 +       else
334 +               data->num_phys = 3;
335 +
336 +       if (of_device_is_compatible(np, "allwinner,sun4i-a10-usb-phy"))
337 +               data->disc_thresh = 3;
338 +       else
339 +               data->disc_thresh = 2;
340 +
341 +       res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
342 +       data->base = devm_ioremap_resource(dev, res);
343 +       if (IS_ERR(data->base))
344 +               return PTR_ERR(data->base);
345 +
346 +       data->clk = devm_clk_get(dev, "usb_phy");
347 +       if (IS_ERR(data->clk)) {
348 +               dev_err(dev, "could not get usb_phy clock\n");
349 +               return PTR_ERR(data->clk);
350 +       }
351 +
352 +       /* Skip 0, 0 is the phy for otg which is not yet supported. */
353 +       for (i = 1; i < data->num_phys; i++) {
354 +               snprintf(name, sizeof(name), "usb%d_vbus", i);
355 +               vbus = devm_regulator_get_optional(dev, name);
356 +               if (IS_ERR(vbus)) {
357 +                       if (PTR_ERR(vbus) == -EPROBE_DEFER)
358 +                               return -EPROBE_DEFER;
359 +                       vbus = NULL;
360 +               }
361 +
362 +               snprintf(name, sizeof(name), "usb%d_reset", i);
363 +               reset = devm_reset_control_get(dev, name);
364 +               if (IS_ERR(reset)) {
365 +                       dev_err(dev, "failed to get reset %s\n", name);
366 +                       return PTR_ERR(reset);
367 +               }
368 +
369 +               if (i) { /* No pmu for usbc0 */
370 +                       snprintf(name, sizeof(name), "pmu%d", i);
371 +                       res = platform_get_resource_byname(pdev,
372 +                                                       IORESOURCE_MEM, name);
373 +                       pmu = devm_ioremap_resource(dev, res);
374 +                       if (IS_ERR(pmu))
375 +                               return PTR_ERR(pmu);
376 +               }
377 +
378 +               phy = devm_phy_create(dev, &sun4i_usb_phy_ops, NULL);
379 +               if (IS_ERR(phy)) {
380 +                       dev_err(dev, "failed to create PHY %d\n", i);
381 +                       return PTR_ERR(phy);
382 +               }
383 +
384 +               data->phys[i].phy = phy;
385 +               data->phys[i].pmu = pmu;
386 +               data->phys[i].vbus = vbus;
387 +               data->phys[i].reset = reset;
388 +               data->phys[i].index = i;
389 +               phy_set_drvdata(phy, &data->phys[i]);
390 +       }
391 +
392 +       dev_set_drvdata(dev, data);
393 +       phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
394 +       if (IS_ERR(phy_provider))
395 +               return PTR_ERR(phy_provider);
396 +
397 +       return 0;
398 +}
399 +
400 +static const struct of_device_id sun4i_usb_phy_of_match[] = {
401 +       { .compatible = "allwinner,sun4i-a10-usb-phy" },
402 +       { .compatible = "allwinner,sun5i-a13-usb-phy" },
403 +       { .compatible = "allwinner,sun7i-a20-usb-phy" },
404 +       { },
405 +};
406 +MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
407 +
408 +static struct platform_driver sun4i_usb_phy_driver = {
409 +       .probe  = sun4i_usb_phy_probe,
410 +       .driver = {
411 +               .of_match_table = sun4i_usb_phy_of_match,
412 +               .name  = "sun4i-usb-phy",
413 +               .owner = THIS_MODULE,
414 +       }
415 +};
416 +module_platform_driver(sun4i_usb_phy_driver);
417 +
418 +MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
419 +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
420 +MODULE_LICENSE("GPL v2");
421 -- 
422 2.0.3
423