Commit | Line | Data |
---|---|---|
657b306a KVA |
1 | /* |
2 | * omap-usb2.c - USB PHY, talking to musb controller in OMAP. | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * Author: Kishon Vijay Abraham I <kishon@ti.com> | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/of.h> | |
23 | #include <linux/io.h> | |
24 | #include <linux/usb/omap_usb.h> | |
25 | #include <linux/usb/phy_companion.h> | |
26 | #include <linux/clk.h> | |
27 | #include <linux/err.h> | |
28 | #include <linux/pm_runtime.h> | |
29 | #include <linux/delay.h> | |
ca784be3 | 30 | #include <linux/usb/omap_control_usb.h> |
5d93d1e7 | 31 | #include <linux/phy/phy.h> |
657b306a KVA |
32 | |
33 | /** | |
34 | * omap_usb2_set_comparator - links the comparator present in the sytem with | |
35 | * this phy | |
36 | * @comparator - the companion phy(comparator) for this phy | |
37 | * | |
38 | * The phy companion driver should call this API passing the phy_companion | |
39 | * filled with set_vbus and start_srp to be used by usb phy. | |
40 | * | |
41 | * For use by phy companion driver | |
42 | */ | |
43 | int omap_usb2_set_comparator(struct phy_companion *comparator) | |
44 | { | |
45 | struct omap_usb *phy; | |
46 | struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2); | |
47 | ||
48 | if (IS_ERR(x)) | |
49 | return -ENODEV; | |
50 | ||
51 | phy = phy_to_omapusb(x); | |
52 | phy->comparator = comparator; | |
53 | return 0; | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(omap_usb2_set_comparator); | |
56 | ||
657b306a KVA |
57 | static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled) |
58 | { | |
59 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
60 | ||
61 | if (!phy->comparator) | |
62 | return -ENODEV; | |
63 | ||
64 | return phy->comparator->set_vbus(phy->comparator, enabled); | |
65 | } | |
66 | ||
67 | static int omap_usb_start_srp(struct usb_otg *otg) | |
68 | { | |
69 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
70 | ||
71 | if (!phy->comparator) | |
72 | return -ENODEV; | |
73 | ||
74 | return phy->comparator->start_srp(phy->comparator); | |
75 | } | |
76 | ||
77 | static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host) | |
78 | { | |
79 | struct usb_phy *phy = otg->phy; | |
80 | ||
81 | otg->host = host; | |
82 | if (!host) | |
83 | phy->state = OTG_STATE_UNDEFINED; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | static int omap_usb_set_peripheral(struct usb_otg *otg, | |
89 | struct usb_gadget *gadget) | |
90 | { | |
91 | struct usb_phy *phy = otg->phy; | |
92 | ||
93 | otg->gadget = gadget; | |
94 | if (!gadget) | |
95 | phy->state = OTG_STATE_UNDEFINED; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | static int omap_usb2_suspend(struct usb_phy *x, int suspend) | |
101 | { | |
657b306a | 102 | struct omap_usb *phy = phy_to_omapusb(x); |
0f827684 | 103 | int ret; |
657b306a KVA |
104 | |
105 | if (suspend && !phy->is_suspended) { | |
ca784be3 | 106 | omap_control_usb_phy_power(phy->control_dev, 0); |
657b306a KVA |
107 | pm_runtime_put_sync(phy->dev); |
108 | phy->is_suspended = 1; | |
109 | } else if (!suspend && phy->is_suspended) { | |
110 | ret = pm_runtime_get_sync(phy->dev); | |
111 | if (ret < 0) { | |
0f827684 | 112 | dev_err(phy->dev, "get_sync failed with err %d\n", ret); |
657b306a KVA |
113 | return ret; |
114 | } | |
ca784be3 | 115 | omap_control_usb_phy_power(phy->control_dev, 1); |
657b306a KVA |
116 | phy->is_suspended = 0; |
117 | } | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
5d93d1e7 KVA |
122 | static int omap_usb_power_off(struct phy *x) |
123 | { | |
124 | struct omap_usb *phy = phy_get_drvdata(x); | |
125 | ||
126 | omap_control_usb_phy_power(phy->control_dev, 0); | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | static int omap_usb_power_on(struct phy *x) | |
132 | { | |
133 | struct omap_usb *phy = phy_get_drvdata(x); | |
134 | ||
135 | omap_control_usb_phy_power(phy->control_dev, 1); | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | static struct phy_ops ops = { | |
141 | .power_on = omap_usb_power_on, | |
142 | .power_off = omap_usb_power_off, | |
143 | .owner = THIS_MODULE, | |
144 | }; | |
145 | ||
41ac7b3a | 146 | static int omap_usb2_probe(struct platform_device *pdev) |
657b306a KVA |
147 | { |
148 | struct omap_usb *phy; | |
5d93d1e7 | 149 | struct phy *generic_phy; |
657b306a | 150 | struct usb_otg *otg; |
5d93d1e7 | 151 | struct phy_provider *phy_provider; |
657b306a KVA |
152 | |
153 | phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL); | |
154 | if (!phy) { | |
155 | dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n"); | |
156 | return -ENOMEM; | |
157 | } | |
158 | ||
159 | otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); | |
160 | if (!otg) { | |
161 | dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n"); | |
162 | return -ENOMEM; | |
163 | } | |
164 | ||
165 | phy->dev = &pdev->dev; | |
166 | ||
167 | phy->phy.dev = phy->dev; | |
168 | phy->phy.label = "omap-usb2"; | |
169 | phy->phy.set_suspend = omap_usb2_suspend; | |
170 | phy->phy.otg = otg; | |
c11747f6 | 171 | phy->phy.type = USB_PHY_TYPE_USB2; |
657b306a | 172 | |
5d93d1e7 KVA |
173 | phy_provider = devm_of_phy_provider_register(phy->dev, |
174 | of_phy_simple_xlate); | |
175 | if (IS_ERR(phy_provider)) | |
176 | return PTR_ERR(phy_provider); | |
177 | ||
ca784be3 KVA |
178 | phy->control_dev = omap_get_control_dev(); |
179 | if (IS_ERR(phy->control_dev)) { | |
180 | dev_dbg(&pdev->dev, "Failed to get control device\n"); | |
181 | return -ENODEV; | |
657b306a KVA |
182 | } |
183 | ||
184 | phy->is_suspended = 1; | |
ca784be3 | 185 | omap_control_usb_phy_power(phy->control_dev, 0); |
657b306a KVA |
186 | |
187 | otg->set_host = omap_usb_set_host; | |
188 | otg->set_peripheral = omap_usb_set_peripheral; | |
189 | otg->set_vbus = omap_usb_set_vbus; | |
190 | otg->start_srp = omap_usb_start_srp; | |
191 | otg->phy = &phy->phy; | |
192 | ||
5d93d1e7 KVA |
193 | platform_set_drvdata(pdev, phy); |
194 | pm_runtime_enable(phy->dev); | |
195 | ||
196 | generic_phy = devm_phy_create(phy->dev, &ops, NULL); | |
197 | if (IS_ERR(generic_phy)) | |
198 | return PTR_ERR(generic_phy); | |
199 | ||
200 | phy_set_drvdata(generic_phy, phy); | |
201 | ||
657b306a KVA |
202 | phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k"); |
203 | if (IS_ERR(phy->wkupclk)) { | |
204 | dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n"); | |
205 | return PTR_ERR(phy->wkupclk); | |
206 | } | |
207 | clk_prepare(phy->wkupclk); | |
208 | ||
0c4c8bbb KVA |
209 | phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m"); |
210 | if (IS_ERR(phy->optclk)) | |
211 | dev_vdbg(&pdev->dev, "unable to get refclk960m\n"); | |
212 | else | |
213 | clk_prepare(phy->optclk); | |
214 | ||
c11747f6 | 215 | usb_add_phy_dev(&phy->phy); |
657b306a | 216 | |
657b306a KVA |
217 | return 0; |
218 | } | |
219 | ||
fb4e98ab | 220 | static int omap_usb2_remove(struct platform_device *pdev) |
657b306a KVA |
221 | { |
222 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
223 | ||
224 | clk_unprepare(phy->wkupclk); | |
0c4c8bbb KVA |
225 | if (!IS_ERR(phy->optclk)) |
226 | clk_unprepare(phy->optclk); | |
657b306a KVA |
227 | usb_remove_phy(&phy->phy); |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | #ifdef CONFIG_PM_RUNTIME | |
233 | ||
234 | static int omap_usb2_runtime_suspend(struct device *dev) | |
235 | { | |
236 | struct platform_device *pdev = to_platform_device(dev); | |
237 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
238 | ||
239 | clk_disable(phy->wkupclk); | |
0c4c8bbb KVA |
240 | if (!IS_ERR(phy->optclk)) |
241 | clk_disable(phy->optclk); | |
657b306a KVA |
242 | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static int omap_usb2_runtime_resume(struct device *dev) | |
247 | { | |
657b306a KVA |
248 | struct platform_device *pdev = to_platform_device(dev); |
249 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
0f827684 | 250 | int ret; |
657b306a KVA |
251 | |
252 | ret = clk_enable(phy->wkupclk); | |
0c4c8bbb | 253 | if (ret < 0) { |
657b306a | 254 | dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret); |
0c4c8bbb KVA |
255 | goto err0; |
256 | } | |
257 | ||
258 | if (!IS_ERR(phy->optclk)) { | |
259 | ret = clk_enable(phy->optclk); | |
260 | if (ret < 0) { | |
261 | dev_err(phy->dev, "Failed to enable optclk %d\n", ret); | |
262 | goto err1; | |
263 | } | |
264 | } | |
265 | ||
266 | return 0; | |
267 | ||
268 | err1: | |
269 | clk_disable(phy->wkupclk); | |
657b306a | 270 | |
0c4c8bbb | 271 | err0: |
657b306a KVA |
272 | return ret; |
273 | } | |
274 | ||
275 | static const struct dev_pm_ops omap_usb2_pm_ops = { | |
276 | SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume, | |
277 | NULL) | |
278 | }; | |
279 | ||
280 | #define DEV_PM_OPS (&omap_usb2_pm_ops) | |
281 | #else | |
282 | #define DEV_PM_OPS NULL | |
283 | #endif | |
284 | ||
285 | #ifdef CONFIG_OF | |
286 | static const struct of_device_id omap_usb2_id_table[] = { | |
287 | { .compatible = "ti,omap-usb2" }, | |
288 | {} | |
289 | }; | |
290 | MODULE_DEVICE_TABLE(of, omap_usb2_id_table); | |
291 | #endif | |
292 | ||
293 | static struct platform_driver omap_usb2_driver = { | |
294 | .probe = omap_usb2_probe, | |
7690417d | 295 | .remove = omap_usb2_remove, |
657b306a KVA |
296 | .driver = { |
297 | .name = "omap-usb2", | |
298 | .owner = THIS_MODULE, | |
299 | .pm = DEV_PM_OPS, | |
300 | .of_match_table = of_match_ptr(omap_usb2_id_table), | |
301 | }, | |
302 | }; | |
303 | ||
304 | module_platform_driver(omap_usb2_driver); | |
305 | ||
306 | MODULE_ALIAS("platform: omap_usb2"); | |
307 | MODULE_AUTHOR("Texas Instruments Inc."); | |
308 | MODULE_DESCRIPTION("OMAP USB2 phy driver"); | |
309 | MODULE_LICENSE("GPL v2"); |