1 From a10fc0cb650be725157eca50e2ceb34efc281ac2 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 22 Apr 2013 23:20:03 +0200
4 Subject: [PATCH 29/53] phy: usb: add ralink phy
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 drivers/phy/Kconfig | 5 ++
9 drivers/phy/Makefile | 1 +
10 drivers/phy/phy-ralink-usb.c | 175 ++++++++++++++++++++++++++++++++++++++++++
11 3 files changed, 181 insertions(+)
12 create mode 100644 drivers/phy/phy-ralink-usb.c
14 --- a/drivers/phy/Kconfig
15 +++ b/drivers/phy/Kconfig
16 @@ -331,6 +331,11 @@ config PHY_XGENE
18 This option enables support for APM X-Gene SoC multi-purpose PHY.
20 +config PHY_RALINK_USB
21 + tristate "Ralink USB PHY driver"
25 config PHY_STIH407_USB
26 tristate "STMicroelectronics USB2 picoPHY driver for STiH407 family"
27 depends on RESET_CONTROLLER
28 --- a/drivers/phy/Makefile
29 +++ b/drivers/phy/Makefile
30 @@ -46,3 +46,4 @@ obj-$(CONFIG_PHY_QCOM_UFS) += phy-qcom-
31 obj-$(CONFIG_PHY_TUSB1210) += phy-tusb1210.o
32 obj-$(CONFIG_PHY_BRCMSTB_SATA) += phy-brcmstb-sata.o
33 obj-$(CONFIG_PHY_PISTACHIO_USB) += phy-pistachio-usb.o
34 +obj-$(CONFIG_PHY_RALINK_USB) += phy-ralink-usb.o
36 +++ b/drivers/phy/phy-ralink-usb.c
39 + * Allwinner ralink USB phy driver
41 + * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
43 + * Based on code from
44 + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
46 + * This program is free software; you can redistribute it and/or modify
47 + * it under the terms of the GNU General Public License as published by
48 + * the Free Software Foundation; either version 2 of the License, or
49 + * (at your option) any later version.
51 + * This program is distributed in the hope that it will be useful,
52 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 + * GNU General Public License for more details.
57 +#include <linux/delay.h>
58 +#include <linux/err.h>
59 +#include <linux/io.h>
60 +#include <linux/kernel.h>
61 +#include <linux/module.h>
62 +#include <linux/mutex.h>
63 +#include <linux/phy/phy.h>
64 +#include <linux/platform_device.h>
65 +#include <linux/reset.h>
66 +#include <linux/of_platform.h>
68 +#include <asm/mach-ralink/ralink_regs.h>
70 +#define RT_SYSC_REG_SYSCFG1 0x014
71 +#define RT_SYSC_REG_CLKCFG1 0x030
72 +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
74 +#define RT_RSTCTRL_UDEV BIT(25)
75 +#define RT_RSTCTRL_UHST BIT(22)
76 +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
78 +#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
79 +#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
80 +#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
81 +#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
83 +#define USB_PHY_UTMI_8B60M BIT(1)
84 +#define UDEV_WAKEUP BIT(0)
86 +static atomic_t usb_pwr_ref = ATOMIC_INIT(0);
87 +static struct reset_control *rstdev;
88 +static struct reset_control *rsthost;
90 +static struct phy *rt_phy;
92 +static void usb_phy_enable(int state)
95 + rt_sysc_m32(0, phy_clk, RT_SYSC_REG_CLKCFG1);
97 + rt_sysc_m32(phy_clk, 0, RT_SYSC_REG_CLKCFG1);
101 +static int ralink_usb_phy_init(struct phy *_phy)
106 +static int ralink_usb_phy_exit(struct phy *_phy)
111 +static int ralink_usb_phy_power_on(struct phy *_phy)
113 + if (atomic_inc_return(&usb_pwr_ref) == 1) {
120 + rt_sysc_m32(0, RT_SYSCFG1_USB0_HOST_MODE, RT_SYSC_REG_SYSCFG1);
121 + if (!IS_ERR(rsthost))
122 + reset_control_deassert(rsthost);
123 + if (!IS_ERR(rstdev))
124 + reset_control_deassert(rstdev);
126 + rt_sysc_m32(RT_SYSCFG1_USB0_HOST_MODE, 0, RT_SYSC_REG_SYSCFG1);
127 + if (!IS_ERR(rstdev))
128 + reset_control_deassert(rstdev);
132 + t = rt_sysc_r32(RT_SYSC_REG_USB_PHY_CFG);
133 + dev_info(&_phy->dev, "remote usb device wakeup %s\n",
134 + (t & UDEV_WAKEUP) ? ("enabbled") : ("disabled"));
135 + if (t & USB_PHY_UTMI_8B60M)
136 + dev_info(&_phy->dev, "UTMI 8bit 60MHz\n");
138 + dev_info(&_phy->dev, "UTMI 16bit 30MHz\n");
144 +static int ralink_usb_phy_power_off(struct phy *_phy)
146 + if (atomic_dec_return(&usb_pwr_ref) == 0) {
148 + if (!IS_ERR(rstdev))
149 + reset_control_assert(rstdev);
150 + if (!IS_ERR(rsthost))
151 + reset_control_assert(rsthost);
157 +static struct phy_ops ralink_usb_phy_ops = {
158 + .init = ralink_usb_phy_init,
159 + .exit = ralink_usb_phy_exit,
160 + .power_on = ralink_usb_phy_power_on,
161 + .power_off = ralink_usb_phy_power_off,
162 + .owner = THIS_MODULE,
165 +static struct phy *ralink_usb_phy_xlate(struct device *dev,
166 + struct of_phandle_args *args)
171 +static const struct of_device_id ralink_usb_phy_of_match[] = {
172 + { .compatible = "ralink,rt3xxx-usbphy", .data = (void *) (RT_CLKCFG1_UPHY1_CLK_EN | RT_CLKCFG1_UPHY0_CLK_EN) },
173 + { .compatible = "ralink,mt7620a-usbphy", .data = (void *) (MT7620_CLKCFG1_UPHY1_CLK_EN | MT7620_CLKCFG1_UPHY0_CLK_EN) },
176 +MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
178 +static int ralink_usb_phy_probe(struct platform_device *pdev)
180 + struct device *dev = &pdev->dev;
181 + struct phy_provider *phy_provider;
182 + const struct of_device_id *match;
184 + match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
185 + phy_clk = (int) match->data;
187 + rsthost = devm_reset_control_get(&pdev->dev, "host");
188 + rstdev = devm_reset_control_get(&pdev->dev, "device");
190 + rt_phy = devm_phy_create(dev, NULL, &ralink_usb_phy_ops);
191 + if (IS_ERR(rt_phy)) {
192 + dev_err(dev, "failed to create PHY\n");
193 + return PTR_ERR(rt_phy);
196 + phy_provider = devm_of_phy_provider_register(dev, ralink_usb_phy_xlate);
198 + return PTR_ERR_OR_ZERO(phy_provider);
201 +static struct platform_driver ralink_usb_phy_driver = {
202 + .probe = ralink_usb_phy_probe,
204 + .of_match_table = ralink_usb_phy_of_match,
205 + .name = "ralink-usb-phy",
208 +module_platform_driver(ralink_usb_phy_driver);
210 +MODULE_DESCRIPTION("Ralink USB phy driver");
211 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
212 +MODULE_LICENSE("GPL v2");