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> | |
6284be23 | 24 | #include <linux/phy/omap_usb.h> |
657b306a KVA |
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> | |
14da699b | 30 | #include <linux/phy/omap_control_phy.h> |
5d93d1e7 | 31 | #include <linux/phy/phy.h> |
478b6c74 | 32 | #include <linux/of_platform.h> |
657b306a | 33 | |
7e472402 AB |
34 | #define USB2PHY_DISCON_BYP_LATCH (1 << 31) |
35 | #define USB2PHY_ANA_CONFIG1 0x4c | |
36 | ||
657b306a KVA |
37 | /** |
38 | * omap_usb2_set_comparator - links the comparator present in the sytem with | |
39 | * this phy | |
40 | * @comparator - the companion phy(comparator) for this phy | |
41 | * | |
42 | * The phy companion driver should call this API passing the phy_companion | |
43 | * filled with set_vbus and start_srp to be used by usb phy. | |
44 | * | |
45 | * For use by phy companion driver | |
46 | */ | |
47 | int omap_usb2_set_comparator(struct phy_companion *comparator) | |
48 | { | |
49 | struct omap_usb *phy; | |
50 | struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2); | |
51 | ||
52 | if (IS_ERR(x)) | |
53 | return -ENODEV; | |
54 | ||
55 | phy = phy_to_omapusb(x); | |
56 | phy->comparator = comparator; | |
57 | return 0; | |
58 | } | |
59 | EXPORT_SYMBOL_GPL(omap_usb2_set_comparator); | |
60 | ||
657b306a KVA |
61 | static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled) |
62 | { | |
63 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
64 | ||
65 | if (!phy->comparator) | |
66 | return -ENODEV; | |
67 | ||
68 | return phy->comparator->set_vbus(phy->comparator, enabled); | |
69 | } | |
70 | ||
71 | static int omap_usb_start_srp(struct usb_otg *otg) | |
72 | { | |
73 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
74 | ||
75 | if (!phy->comparator) | |
76 | return -ENODEV; | |
77 | ||
78 | return phy->comparator->start_srp(phy->comparator); | |
79 | } | |
80 | ||
81 | static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host) | |
82 | { | |
83 | struct usb_phy *phy = otg->phy; | |
84 | ||
85 | otg->host = host; | |
86 | if (!host) | |
87 | phy->state = OTG_STATE_UNDEFINED; | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | static int omap_usb_set_peripheral(struct usb_otg *otg, | |
93 | struct usb_gadget *gadget) | |
94 | { | |
95 | struct usb_phy *phy = otg->phy; | |
96 | ||
97 | otg->gadget = gadget; | |
98 | if (!gadget) | |
99 | phy->state = OTG_STATE_UNDEFINED; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
5d93d1e7 KVA |
104 | static int omap_usb_power_off(struct phy *x) |
105 | { | |
106 | struct omap_usb *phy = phy_get_drvdata(x); | |
107 | ||
14da699b | 108 | omap_control_phy_power(phy->control_dev, 0); |
5d93d1e7 KVA |
109 | |
110 | return 0; | |
111 | } | |
112 | ||
113 | static int omap_usb_power_on(struct phy *x) | |
114 | { | |
115 | struct omap_usb *phy = phy_get_drvdata(x); | |
116 | ||
14da699b | 117 | omap_control_phy_power(phy->control_dev, 1); |
5d93d1e7 KVA |
118 | |
119 | return 0; | |
120 | } | |
121 | ||
7e472402 AB |
122 | static int omap_usb_init(struct phy *x) |
123 | { | |
124 | struct omap_usb *phy = phy_get_drvdata(x); | |
125 | u32 val; | |
126 | ||
127 | if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) { | |
128 | /* | |
129 | * | |
130 | * Reduce the sensitivity of internal PHY by enabling the | |
131 | * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This | |
132 | * resolves issues with certain devices which can otherwise | |
133 | * be prone to false disconnects. | |
134 | * | |
135 | */ | |
136 | val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1); | |
137 | val |= USB2PHY_DISCON_BYP_LATCH; | |
138 | omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val); | |
139 | } | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
5d93d1e7 | 144 | static struct phy_ops ops = { |
7e472402 | 145 | .init = omap_usb_init, |
5d93d1e7 KVA |
146 | .power_on = omap_usb_power_on, |
147 | .power_off = omap_usb_power_off, | |
148 | .owner = THIS_MODULE, | |
149 | }; | |
150 | ||
09a0168d GC |
151 | #ifdef CONFIG_OF |
152 | static const struct usb_phy_data omap_usb2_data = { | |
153 | .label = "omap_usb2", | |
154 | .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS, | |
155 | }; | |
156 | ||
db68e80f GC |
157 | static const struct usb_phy_data omap5_usb2_data = { |
158 | .label = "omap5_usb2", | |
159 | .flags = 0, | |
160 | }; | |
161 | ||
7e472402 AB |
162 | static const struct usb_phy_data dra7x_usb2_data = { |
163 | .label = "dra7x_usb2", | |
164 | .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT, | |
165 | }; | |
166 | ||
09a0168d GC |
167 | static const struct usb_phy_data am437x_usb2_data = { |
168 | .label = "am437x_usb2", | |
169 | .flags = 0, | |
170 | }; | |
171 | ||
172 | static const struct of_device_id omap_usb2_id_table[] = { | |
173 | { | |
174 | .compatible = "ti,omap-usb2", | |
175 | .data = &omap_usb2_data, | |
176 | }, | |
db68e80f GC |
177 | { |
178 | .compatible = "ti,omap5-usb2", | |
179 | .data = &omap5_usb2_data, | |
180 | }, | |
7e472402 AB |
181 | { |
182 | .compatible = "ti,dra7x-usb2", | |
183 | .data = &dra7x_usb2_data, | |
184 | }, | |
09a0168d GC |
185 | { |
186 | .compatible = "ti,am437x-usb2", | |
187 | .data = &am437x_usb2_data, | |
188 | }, | |
189 | {}, | |
190 | }; | |
191 | MODULE_DEVICE_TABLE(of, omap_usb2_id_table); | |
192 | #endif | |
193 | ||
41ac7b3a | 194 | static int omap_usb2_probe(struct platform_device *pdev) |
657b306a | 195 | { |
478b6c74 RQ |
196 | struct omap_usb *phy; |
197 | struct phy *generic_phy; | |
7e472402 | 198 | struct resource *res; |
478b6c74 RQ |
199 | struct phy_provider *phy_provider; |
200 | struct usb_otg *otg; | |
201 | struct device_node *node = pdev->dev.of_node; | |
202 | struct device_node *control_node; | |
203 | struct platform_device *control_pdev; | |
09a0168d GC |
204 | const struct of_device_id *of_id; |
205 | struct usb_phy_data *phy_data; | |
206 | ||
207 | of_id = of_match_device(of_match_ptr(omap_usb2_id_table), &pdev->dev); | |
478b6c74 | 208 | |
09a0168d | 209 | if (!of_id) |
478b6c74 | 210 | return -EINVAL; |
657b306a | 211 | |
09a0168d GC |
212 | phy_data = (struct usb_phy_data *)of_id->data; |
213 | ||
657b306a | 214 | phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL); |
0b68253d | 215 | if (!phy) |
657b306a | 216 | return -ENOMEM; |
657b306a KVA |
217 | |
218 | otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); | |
0b68253d | 219 | if (!otg) |
657b306a | 220 | return -ENOMEM; |
657b306a KVA |
221 | |
222 | phy->dev = &pdev->dev; | |
223 | ||
224 | phy->phy.dev = phy->dev; | |
09a0168d | 225 | phy->phy.label = phy_data->label; |
657b306a | 226 | phy->phy.otg = otg; |
c11747f6 | 227 | phy->phy.type = USB_PHY_TYPE_USB2; |
657b306a | 228 | |
7e472402 AB |
229 | if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) { |
230 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
231 | phy->phy_base = devm_ioremap_resource(&pdev->dev, res); | |
3df9fcd5 HS |
232 | if (IS_ERR(phy->phy_base)) |
233 | return PTR_ERR(phy->phy_base); | |
7e472402 AB |
234 | phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT; |
235 | } | |
236 | ||
478b6c74 RQ |
237 | control_node = of_parse_phandle(node, "ctrl-module", 0); |
238 | if (!control_node) { | |
239 | dev_err(&pdev->dev, "Failed to get control device phandle\n"); | |
240 | return -EINVAL; | |
657b306a KVA |
241 | } |
242 | ||
478b6c74 RQ |
243 | control_pdev = of_find_device_by_node(control_node); |
244 | if (!control_pdev) { | |
245 | dev_err(&pdev->dev, "Failed to get control device\n"); | |
246 | return -EINVAL; | |
247 | } | |
248 | ||
249 | phy->control_dev = &control_pdev->dev; | |
14da699b | 250 | omap_control_phy_power(phy->control_dev, 0); |
657b306a KVA |
251 | |
252 | otg->set_host = omap_usb_set_host; | |
253 | otg->set_peripheral = omap_usb_set_peripheral; | |
09a0168d GC |
254 | if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS) |
255 | otg->set_vbus = omap_usb_set_vbus; | |
256 | if (phy_data->flags & OMAP_USB2_HAS_START_SRP) | |
257 | otg->start_srp = omap_usb_start_srp; | |
657b306a KVA |
258 | otg->phy = &phy->phy; |
259 | ||
5d93d1e7 | 260 | platform_set_drvdata(pdev, phy); |
f20531a9 | 261 | pm_runtime_enable(phy->dev); |
5d93d1e7 | 262 | |
f0ed8176 | 263 | generic_phy = devm_phy_create(phy->dev, NULL, &ops, NULL); |
f20531a9 OG |
264 | if (IS_ERR(generic_phy)) { |
265 | pm_runtime_disable(phy->dev); | |
5d93d1e7 | 266 | return PTR_ERR(generic_phy); |
f20531a9 | 267 | } |
5d93d1e7 KVA |
268 | |
269 | phy_set_drvdata(generic_phy, phy); | |
270 | ||
64fe1891 KVA |
271 | phy_provider = devm_of_phy_provider_register(phy->dev, |
272 | of_phy_simple_xlate); | |
eb82a3d8 RQ |
273 | if (IS_ERR(phy_provider)) { |
274 | pm_runtime_disable(phy->dev); | |
64fe1891 | 275 | return PTR_ERR(phy_provider); |
eb82a3d8 | 276 | } |
64fe1891 | 277 | |
9f1d8ed2 | 278 | phy->wkupclk = devm_clk_get(phy->dev, "wkupclk"); |
657b306a | 279 | if (IS_ERR(phy->wkupclk)) { |
9f1d8ed2 RQ |
280 | dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n"); |
281 | phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k"); | |
282 | if (IS_ERR(phy->wkupclk)) { | |
283 | dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n"); | |
284 | return PTR_ERR(phy->wkupclk); | |
285 | } else { | |
286 | dev_warn(&pdev->dev, | |
287 | "found usb_phy_cm_clk32k, please fix DTS\n"); | |
288 | } | |
657b306a KVA |
289 | } |
290 | clk_prepare(phy->wkupclk); | |
291 | ||
9f1d8ed2 RQ |
292 | phy->optclk = devm_clk_get(phy->dev, "refclk"); |
293 | if (IS_ERR(phy->optclk)) { | |
294 | dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n"); | |
295 | phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m"); | |
296 | if (IS_ERR(phy->optclk)) { | |
297 | dev_dbg(&pdev->dev, | |
298 | "unable to get usb_otg_ss_refclk960m\n"); | |
299 | } else { | |
300 | dev_warn(&pdev->dev, | |
301 | "found usb_otg_ss_refclk960m, please fix DTS\n"); | |
302 | } | |
303 | } else { | |
0c4c8bbb | 304 | clk_prepare(phy->optclk); |
9f1d8ed2 | 305 | } |
0c4c8bbb | 306 | |
c11747f6 | 307 | usb_add_phy_dev(&phy->phy); |
657b306a | 308 | |
657b306a KVA |
309 | return 0; |
310 | } | |
311 | ||
fb4e98ab | 312 | static int omap_usb2_remove(struct platform_device *pdev) |
657b306a KVA |
313 | { |
314 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
315 | ||
316 | clk_unprepare(phy->wkupclk); | |
0c4c8bbb KVA |
317 | if (!IS_ERR(phy->optclk)) |
318 | clk_unprepare(phy->optclk); | |
657b306a | 319 | usb_remove_phy(&phy->phy); |
eb82a3d8 | 320 | pm_runtime_disable(phy->dev); |
657b306a KVA |
321 | |
322 | return 0; | |
323 | } | |
324 | ||
325 | #ifdef CONFIG_PM_RUNTIME | |
326 | ||
327 | static int omap_usb2_runtime_suspend(struct device *dev) | |
328 | { | |
329 | struct platform_device *pdev = to_platform_device(dev); | |
330 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
331 | ||
332 | clk_disable(phy->wkupclk); | |
0c4c8bbb KVA |
333 | if (!IS_ERR(phy->optclk)) |
334 | clk_disable(phy->optclk); | |
657b306a KVA |
335 | |
336 | return 0; | |
337 | } | |
338 | ||
339 | static int omap_usb2_runtime_resume(struct device *dev) | |
340 | { | |
657b306a KVA |
341 | struct platform_device *pdev = to_platform_device(dev); |
342 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
0f827684 | 343 | int ret; |
657b306a KVA |
344 | |
345 | ret = clk_enable(phy->wkupclk); | |
0c4c8bbb | 346 | if (ret < 0) { |
657b306a | 347 | dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret); |
0c4c8bbb KVA |
348 | goto err0; |
349 | } | |
350 | ||
351 | if (!IS_ERR(phy->optclk)) { | |
352 | ret = clk_enable(phy->optclk); | |
353 | if (ret < 0) { | |
354 | dev_err(phy->dev, "Failed to enable optclk %d\n", ret); | |
355 | goto err1; | |
356 | } | |
357 | } | |
358 | ||
359 | return 0; | |
360 | ||
361 | err1: | |
362 | clk_disable(phy->wkupclk); | |
657b306a | 363 | |
0c4c8bbb | 364 | err0: |
657b306a KVA |
365 | return ret; |
366 | } | |
367 | ||
368 | static const struct dev_pm_ops omap_usb2_pm_ops = { | |
369 | SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume, | |
370 | NULL) | |
371 | }; | |
372 | ||
373 | #define DEV_PM_OPS (&omap_usb2_pm_ops) | |
374 | #else | |
375 | #define DEV_PM_OPS NULL | |
376 | #endif | |
377 | ||
657b306a KVA |
378 | static struct platform_driver omap_usb2_driver = { |
379 | .probe = omap_usb2_probe, | |
7690417d | 380 | .remove = omap_usb2_remove, |
657b306a KVA |
381 | .driver = { |
382 | .name = "omap-usb2", | |
657b306a KVA |
383 | .pm = DEV_PM_OPS, |
384 | .of_match_table = of_match_ptr(omap_usb2_id_table), | |
385 | }, | |
386 | }; | |
387 | ||
388 | module_platform_driver(omap_usb2_driver); | |
389 | ||
390 | MODULE_ALIAS("platform: omap_usb2"); | |
391 | MODULE_AUTHOR("Texas Instruments Inc."); | |
392 | MODULE_DESCRIPTION("OMAP USB2 phy driver"); | |
393 | MODULE_LICENSE("GPL v2"); |