blob: a3cb25cb74f833a3735ea4ca5eb876afd5214a3e [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02002#include <linux/module.h>
3#include <linux/platform_device.h>
Sebastian Andrzej Siewior8b841cb2013-08-13 10:57:08 +02004#include <linux/err.h>
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02005#include <linux/of.h>
6#include <linux/io.h>
Daniel Macka31a9422014-04-16 17:11:16 +02007#include <linux/delay.h>
Bin Liu59f042f2015-12-08 10:31:50 -06008#include <linux/usb/otg.h>
Bin Liu53066612015-11-20 16:13:07 -06009#include "phy-am335x-control.h"
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020010
11struct am335x_control_usb {
12 struct device *dev;
13 void __iomem *phy_reg;
14 void __iomem *wkup;
15 spinlock_t lock;
16 struct phy_control phy_ctrl;
17};
18
19#define AM335X_USB0_CTRL 0x0
20#define AM335X_USB1_CTRL 0x8
21#define AM335x_USB_WKUP 0x0
22
23#define USBPHY_CM_PWRDN (1 << 0)
24#define USBPHY_OTG_PWRDN (1 << 1)
25#define USBPHY_OTGVDET_EN (1 << 19)
26#define USBPHY_OTGSESSEND_EN (1 << 20)
27
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +020028#define AM335X_PHY0_WK_EN (1 << 0)
29#define AM335X_PHY1_WK_EN (1 << 8)
30
31static void am335x_phy_wkup(struct phy_control *phy_ctrl, u32 id, bool on)
32{
33 struct am335x_control_usb *usb_ctrl;
34 u32 val;
35 u32 reg;
36
37 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
38
39 switch (id) {
40 case 0:
41 reg = AM335X_PHY0_WK_EN;
42 break;
43 case 1:
44 reg = AM335X_PHY1_WK_EN;
45 break;
46 default:
47 WARN_ON(1);
48 return;
49 }
50
51 spin_lock(&usb_ctrl->lock);
52 val = readl(usb_ctrl->wkup);
53
54 if (on)
55 val |= reg;
56 else
57 val &= ~reg;
58
59 writel(val, usb_ctrl->wkup);
60 spin_unlock(&usb_ctrl->lock);
61}
62
Bin Liu59f042f2015-12-08 10:31:50 -060063static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id,
64 enum usb_dr_mode dr_mode, bool on)
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020065{
66 struct am335x_control_usb *usb_ctrl;
67 u32 val;
68 u32 reg;
69
70 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
71
72 switch (id) {
73 case 0:
74 reg = AM335X_USB0_CTRL;
75 break;
76 case 1:
77 reg = AM335X_USB1_CTRL;
78 break;
79 default:
Sebastian Andrzej Siewior4ff74572013-08-16 17:32:49 +020080 WARN_ON(1);
81 return;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020082 }
83
84 val = readl(usb_ctrl->phy_reg + reg);
85 if (on) {
Bin Liu59f042f2015-12-08 10:31:50 -060086 if (dr_mode == USB_DR_MODE_HOST) {
87 val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN |
88 USBPHY_OTGVDET_EN);
89 val |= USBPHY_OTGSESSEND_EN;
90 } else {
91 val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
92 val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
93 }
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020094 } else {
95 val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
96 }
97
98 writel(val, usb_ctrl->phy_reg + reg);
Daniel Macka31a9422014-04-16 17:11:16 +020099
100 /*
101 * Give the PHY ~1ms to complete the power up operation.
102 * Tests have shown unstable behaviour if other USB PHY related
103 * registers are written too shortly after such a transition.
104 */
105 if (on)
106 mdelay(1);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200107}
108
109static const struct phy_control ctrl_am335x = {
110 .phy_power = am335x_phy_power,
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200111 .phy_wkup = am335x_phy_wkup,
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200112};
113
114static const struct of_device_id omap_control_usb_id_table[] = {
115 { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
116 {}
117};
118MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
119
120static struct platform_driver am335x_control_driver;
121static int match(struct device *dev, void *data)
122{
123 struct device_node *node = (struct device_node *)data;
124 return dev->of_node == node &&
125 dev->driver == &am335x_control_driver.driver;
126}
127
128struct phy_control *am335x_get_phy_control(struct device *dev)
129{
130 struct device_node *node;
131 struct am335x_control_usb *ctrl_usb;
132
133 node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
134 if (!node)
135 return NULL;
136
137 dev = bus_find_device(&platform_bus_type, NULL, node, match);
Johan Hovold015105b2016-11-01 11:40:25 +0100138 of_node_put(node);
David Dueckd0f347d2015-02-08 16:29:30 +0100139 if (!dev)
140 return NULL;
141
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200142 ctrl_usb = dev_get_drvdata(dev);
Johan Hovold015105b2016-11-01 11:40:25 +0100143 put_device(dev);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200144 if (!ctrl_usb)
145 return NULL;
146 return &ctrl_usb->phy_ctrl;
147}
148EXPORT_SYMBOL_GPL(am335x_get_phy_control);
149
150static int am335x_control_usb_probe(struct platform_device *pdev)
151{
152 struct resource *res;
153 struct am335x_control_usb *ctrl_usb;
154 const struct of_device_id *of_id;
155 const struct phy_control *phy_ctrl;
156
157 of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
158 if (!of_id)
159 return -EINVAL;
160
161 phy_ctrl = of_id->data;
162
163 ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
Peter Chen9aabd032014-10-14 15:56:14 +0800164 if (!ctrl_usb)
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200165 return -ENOMEM;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200166
167 ctrl_usb->dev = &pdev->dev;
168
169 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
170 ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res);
171 if (IS_ERR(ctrl_usb->phy_reg))
172 return PTR_ERR(ctrl_usb->phy_reg);
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200173
174 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup");
175 ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res);
176 if (IS_ERR(ctrl_usb->wkup))
177 return PTR_ERR(ctrl_usb->wkup);
178
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200179 spin_lock_init(&ctrl_usb->lock);
180 ctrl_usb->phy_ctrl = *phy_ctrl;
181
182 dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
183 return 0;
184}
185
186static struct platform_driver am335x_control_driver = {
187 .probe = am335x_control_usb_probe,
188 .driver = {
189 .name = "am335x-control-usb",
Sachin Kamatc4df9ae2013-09-30 09:44:45 +0530190 .of_match_table = omap_control_usb_id_table,
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200191 },
192};
193
194module_platform_driver(am335x_control_driver);
195MODULE_LICENSE("GPL v2");