blob: 2e923c581809fa6c459842154db281c76c070bb7 [file] [log] [blame]
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02001#include <linux/module.h>
2#include <linux/platform_device.h>
Sebastian Andrzej Siewior8b841cb2013-08-13 10:57:08 +02003#include <linux/err.h>
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02004#include <linux/of.h>
5#include <linux/io.h>
Daniel Macka31a9422014-04-16 17:11:16 +02006#include <linux/delay.h>
Rashika Kheriadb67bc02013-12-19 15:44:34 +05307#include "am35x-phy-control.h"
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +02008
9struct am335x_control_usb {
10 struct device *dev;
11 void __iomem *phy_reg;
12 void __iomem *wkup;
13 spinlock_t lock;
14 struct phy_control phy_ctrl;
15};
16
17#define AM335X_USB0_CTRL 0x0
18#define AM335X_USB1_CTRL 0x8
19#define AM335x_USB_WKUP 0x0
20
21#define USBPHY_CM_PWRDN (1 << 0)
22#define USBPHY_OTG_PWRDN (1 << 1)
23#define USBPHY_OTGVDET_EN (1 << 19)
24#define USBPHY_OTGSESSEND_EN (1 << 20)
25
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +020026#define AM335X_PHY0_WK_EN (1 << 0)
27#define AM335X_PHY1_WK_EN (1 << 8)
28
29static void am335x_phy_wkup(struct phy_control *phy_ctrl, u32 id, bool on)
30{
31 struct am335x_control_usb *usb_ctrl;
32 u32 val;
33 u32 reg;
34
35 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
36
37 switch (id) {
38 case 0:
39 reg = AM335X_PHY0_WK_EN;
40 break;
41 case 1:
42 reg = AM335X_PHY1_WK_EN;
43 break;
44 default:
45 WARN_ON(1);
46 return;
47 }
48
49 spin_lock(&usb_ctrl->lock);
50 val = readl(usb_ctrl->wkup);
51
52 if (on)
53 val |= reg;
54 else
55 val &= ~reg;
56
57 writel(val, usb_ctrl->wkup);
58 spin_unlock(&usb_ctrl->lock);
59}
60
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020061static void am335x_phy_power(struct phy_control *phy_ctrl, u32 id, bool on)
62{
63 struct am335x_control_usb *usb_ctrl;
64 u32 val;
65 u32 reg;
66
67 usb_ctrl = container_of(phy_ctrl, struct am335x_control_usb, phy_ctrl);
68
69 switch (id) {
70 case 0:
71 reg = AM335X_USB0_CTRL;
72 break;
73 case 1:
74 reg = AM335X_USB1_CTRL;
75 break;
76 default:
Sebastian Andrzej Siewior4ff74572013-08-16 17:32:49 +020077 WARN_ON(1);
78 return;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020079 }
80
81 val = readl(usb_ctrl->phy_reg + reg);
82 if (on) {
83 val &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN);
84 val |= USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN;
85 } else {
86 val |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
87 }
88
89 writel(val, usb_ctrl->phy_reg + reg);
Daniel Macka31a9422014-04-16 17:11:16 +020090
91 /*
92 * Give the PHY ~1ms to complete the power up operation.
93 * Tests have shown unstable behaviour if other USB PHY related
94 * registers are written too shortly after such a transition.
95 */
96 if (on)
97 mdelay(1);
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +020098}
99
100static const struct phy_control ctrl_am335x = {
101 .phy_power = am335x_phy_power,
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200102 .phy_wkup = am335x_phy_wkup,
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200103};
104
105static const struct of_device_id omap_control_usb_id_table[] = {
106 { .compatible = "ti,am335x-usb-ctrl-module", .data = &ctrl_am335x },
107 {}
108};
109MODULE_DEVICE_TABLE(of, omap_control_usb_id_table);
110
111static struct platform_driver am335x_control_driver;
112static int match(struct device *dev, void *data)
113{
114 struct device_node *node = (struct device_node *)data;
115 return dev->of_node == node &&
116 dev->driver == &am335x_control_driver.driver;
117}
118
119struct phy_control *am335x_get_phy_control(struct device *dev)
120{
121 struct device_node *node;
122 struct am335x_control_usb *ctrl_usb;
123
124 node = of_parse_phandle(dev->of_node, "ti,ctrl_mod", 0);
125 if (!node)
126 return NULL;
127
128 dev = bus_find_device(&platform_bus_type, NULL, node, match);
129 ctrl_usb = dev_get_drvdata(dev);
130 if (!ctrl_usb)
131 return NULL;
132 return &ctrl_usb->phy_ctrl;
133}
134EXPORT_SYMBOL_GPL(am335x_get_phy_control);
135
136static int am335x_control_usb_probe(struct platform_device *pdev)
137{
138 struct resource *res;
139 struct am335x_control_usb *ctrl_usb;
140 const struct of_device_id *of_id;
141 const struct phy_control *phy_ctrl;
142
143 of_id = of_match_node(omap_control_usb_id_table, pdev->dev.of_node);
144 if (!of_id)
145 return -EINVAL;
146
147 phy_ctrl = of_id->data;
148
149 ctrl_usb = devm_kzalloc(&pdev->dev, sizeof(*ctrl_usb), GFP_KERNEL);
Peter Chen9aabd032014-10-14 15:56:14 +0800150 if (!ctrl_usb)
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200151 return -ENOMEM;
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200152
153 ctrl_usb->dev = &pdev->dev;
154
155 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
156 ctrl_usb->phy_reg = devm_ioremap_resource(&pdev->dev, res);
157 if (IS_ERR(ctrl_usb->phy_reg))
158 return PTR_ERR(ctrl_usb->phy_reg);
Sebastian Andrzej Siewiora1222632013-08-19 12:39:44 +0200159
160 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wakeup");
161 ctrl_usb->wkup = devm_ioremap_resource(&pdev->dev, res);
162 if (IS_ERR(ctrl_usb->wkup))
163 return PTR_ERR(ctrl_usb->wkup);
164
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200165 spin_lock_init(&ctrl_usb->lock);
166 ctrl_usb->phy_ctrl = *phy_ctrl;
167
168 dev_set_drvdata(ctrl_usb->dev, ctrl_usb);
169 return 0;
170}
171
172static struct platform_driver am335x_control_driver = {
173 .probe = am335x_control_usb_probe,
174 .driver = {
175 .name = "am335x-control-usb",
176 .owner = THIS_MODULE,
Sachin Kamatc4df9ae2013-09-30 09:44:45 +0530177 .of_match_table = omap_control_usb_id_table,
Sebastian Andrzej Siewior3bb869c2013-07-30 21:43:45 +0200178 },
179};
180
181module_platform_driver(am335x_control_driver);
182MODULE_LICENSE("GPL v2");