]>
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> |
478b6c74 | 32 | #include <linux/of_platform.h> |
657b306a KVA |
33 | |
34 | /** | |
35 | * omap_usb2_set_comparator - links the comparator present in the sytem with | |
36 | * this phy | |
37 | * @comparator - the companion phy(comparator) for this phy | |
38 | * | |
39 | * The phy companion driver should call this API passing the phy_companion | |
40 | * filled with set_vbus and start_srp to be used by usb phy. | |
41 | * | |
42 | * For use by phy companion driver | |
43 | */ | |
44 | int omap_usb2_set_comparator(struct phy_companion *comparator) | |
45 | { | |
46 | struct omap_usb *phy; | |
47 | struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2); | |
48 | ||
49 | if (IS_ERR(x)) | |
50 | return -ENODEV; | |
51 | ||
52 | phy = phy_to_omapusb(x); | |
53 | phy->comparator = comparator; | |
54 | return 0; | |
55 | } | |
56 | EXPORT_SYMBOL_GPL(omap_usb2_set_comparator); | |
57 | ||
657b306a KVA |
58 | static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled) |
59 | { | |
60 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
61 | ||
62 | if (!phy->comparator) | |
63 | return -ENODEV; | |
64 | ||
65 | return phy->comparator->set_vbus(phy->comparator, enabled); | |
66 | } | |
67 | ||
68 | static int omap_usb_start_srp(struct usb_otg *otg) | |
69 | { | |
70 | struct omap_usb *phy = phy_to_omapusb(otg->phy); | |
71 | ||
72 | if (!phy->comparator) | |
73 | return -ENODEV; | |
74 | ||
75 | return phy->comparator->start_srp(phy->comparator); | |
76 | } | |
77 | ||
78 | static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host) | |
79 | { | |
80 | struct usb_phy *phy = otg->phy; | |
81 | ||
82 | otg->host = host; | |
83 | if (!host) | |
84 | phy->state = OTG_STATE_UNDEFINED; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | static int omap_usb_set_peripheral(struct usb_otg *otg, | |
90 | struct usb_gadget *gadget) | |
91 | { | |
92 | struct usb_phy *phy = otg->phy; | |
93 | ||
94 | otg->gadget = gadget; | |
95 | if (!gadget) | |
96 | phy->state = OTG_STATE_UNDEFINED; | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
5d93d1e7 KVA |
101 | static int omap_usb_power_off(struct phy *x) |
102 | { | |
103 | struct omap_usb *phy = phy_get_drvdata(x); | |
104 | ||
105 | omap_control_usb_phy_power(phy->control_dev, 0); | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
110 | static int omap_usb_power_on(struct phy *x) | |
111 | { | |
112 | struct omap_usb *phy = phy_get_drvdata(x); | |
113 | ||
114 | omap_control_usb_phy_power(phy->control_dev, 1); | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | static struct phy_ops ops = { | |
120 | .power_on = omap_usb_power_on, | |
121 | .power_off = omap_usb_power_off, | |
122 | .owner = THIS_MODULE, | |
123 | }; | |
124 | ||
41ac7b3a | 125 | static int omap_usb2_probe(struct platform_device *pdev) |
657b306a | 126 | { |
478b6c74 RQ |
127 | struct omap_usb *phy; |
128 | struct phy *generic_phy; | |
129 | struct phy_provider *phy_provider; | |
130 | struct usb_otg *otg; | |
131 | struct device_node *node = pdev->dev.of_node; | |
132 | struct device_node *control_node; | |
133 | struct platform_device *control_pdev; | |
134 | ||
135 | if (!node) | |
136 | return -EINVAL; | |
657b306a KVA |
137 | |
138 | phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL); | |
139 | if (!phy) { | |
140 | dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n"); | |
141 | return -ENOMEM; | |
142 | } | |
143 | ||
144 | otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); | |
145 | if (!otg) { | |
146 | dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n"); | |
147 | return -ENOMEM; | |
148 | } | |
149 | ||
150 | phy->dev = &pdev->dev; | |
151 | ||
152 | phy->phy.dev = phy->dev; | |
153 | phy->phy.label = "omap-usb2"; | |
657b306a | 154 | phy->phy.otg = otg; |
c11747f6 | 155 | phy->phy.type = USB_PHY_TYPE_USB2; |
657b306a | 156 | |
478b6c74 RQ |
157 | control_node = of_parse_phandle(node, "ctrl-module", 0); |
158 | if (!control_node) { | |
159 | dev_err(&pdev->dev, "Failed to get control device phandle\n"); | |
160 | return -EINVAL; | |
657b306a KVA |
161 | } |
162 | ||
478b6c74 RQ |
163 | control_pdev = of_find_device_by_node(control_node); |
164 | if (!control_pdev) { | |
165 | dev_err(&pdev->dev, "Failed to get control device\n"); | |
166 | return -EINVAL; | |
167 | } | |
168 | ||
169 | phy->control_dev = &control_pdev->dev; | |
ca784be3 | 170 | omap_control_usb_phy_power(phy->control_dev, 0); |
657b306a KVA |
171 | |
172 | otg->set_host = omap_usb_set_host; | |
173 | otg->set_peripheral = omap_usb_set_peripheral; | |
174 | otg->set_vbus = omap_usb_set_vbus; | |
175 | otg->start_srp = omap_usb_start_srp; | |
176 | otg->phy = &phy->phy; | |
177 | ||
5d93d1e7 KVA |
178 | platform_set_drvdata(pdev, phy); |
179 | pm_runtime_enable(phy->dev); | |
180 | ||
181 | generic_phy = devm_phy_create(phy->dev, &ops, NULL); | |
182 | if (IS_ERR(generic_phy)) | |
183 | return PTR_ERR(generic_phy); | |
184 | ||
185 | phy_set_drvdata(generic_phy, phy); | |
186 | ||
64fe1891 KVA |
187 | phy_provider = devm_of_phy_provider_register(phy->dev, |
188 | of_phy_simple_xlate); | |
189 | if (IS_ERR(phy_provider)) | |
190 | return PTR_ERR(phy_provider); | |
191 | ||
657b306a KVA |
192 | phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k"); |
193 | if (IS_ERR(phy->wkupclk)) { | |
194 | dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n"); | |
195 | return PTR_ERR(phy->wkupclk); | |
196 | } | |
197 | clk_prepare(phy->wkupclk); | |
198 | ||
0c4c8bbb KVA |
199 | phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m"); |
200 | if (IS_ERR(phy->optclk)) | |
201 | dev_vdbg(&pdev->dev, "unable to get refclk960m\n"); | |
202 | else | |
203 | clk_prepare(phy->optclk); | |
204 | ||
c11747f6 | 205 | usb_add_phy_dev(&phy->phy); |
657b306a | 206 | |
657b306a KVA |
207 | return 0; |
208 | } | |
209 | ||
fb4e98ab | 210 | static int omap_usb2_remove(struct platform_device *pdev) |
657b306a KVA |
211 | { |
212 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
213 | ||
214 | clk_unprepare(phy->wkupclk); | |
0c4c8bbb KVA |
215 | if (!IS_ERR(phy->optclk)) |
216 | clk_unprepare(phy->optclk); | |
657b306a KVA |
217 | usb_remove_phy(&phy->phy); |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | #ifdef CONFIG_PM_RUNTIME | |
223 | ||
224 | static int omap_usb2_runtime_suspend(struct device *dev) | |
225 | { | |
226 | struct platform_device *pdev = to_platform_device(dev); | |
227 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
228 | ||
229 | clk_disable(phy->wkupclk); | |
0c4c8bbb KVA |
230 | if (!IS_ERR(phy->optclk)) |
231 | clk_disable(phy->optclk); | |
657b306a KVA |
232 | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static int omap_usb2_runtime_resume(struct device *dev) | |
237 | { | |
657b306a KVA |
238 | struct platform_device *pdev = to_platform_device(dev); |
239 | struct omap_usb *phy = platform_get_drvdata(pdev); | |
0f827684 | 240 | int ret; |
657b306a KVA |
241 | |
242 | ret = clk_enable(phy->wkupclk); | |
0c4c8bbb | 243 | if (ret < 0) { |
657b306a | 244 | dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret); |
0c4c8bbb KVA |
245 | goto err0; |
246 | } | |
247 | ||
248 | if (!IS_ERR(phy->optclk)) { | |
249 | ret = clk_enable(phy->optclk); | |
250 | if (ret < 0) { | |
251 | dev_err(phy->dev, "Failed to enable optclk %d\n", ret); | |
252 | goto err1; | |
253 | } | |
254 | } | |
255 | ||
256 | return 0; | |
257 | ||
258 | err1: | |
259 | clk_disable(phy->wkupclk); | |
657b306a | 260 | |
0c4c8bbb | 261 | err0: |
657b306a KVA |
262 | return ret; |
263 | } | |
264 | ||
265 | static const struct dev_pm_ops omap_usb2_pm_ops = { | |
266 | SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume, | |
267 | NULL) | |
268 | }; | |
269 | ||
270 | #define DEV_PM_OPS (&omap_usb2_pm_ops) | |
271 | #else | |
272 | #define DEV_PM_OPS NULL | |
273 | #endif | |
274 | ||
275 | #ifdef CONFIG_OF | |
276 | static const struct of_device_id omap_usb2_id_table[] = { | |
277 | { .compatible = "ti,omap-usb2" }, | |
278 | {} | |
279 | }; | |
280 | MODULE_DEVICE_TABLE(of, omap_usb2_id_table); | |
281 | #endif | |
282 | ||
283 | static struct platform_driver omap_usb2_driver = { | |
284 | .probe = omap_usb2_probe, | |
7690417d | 285 | .remove = omap_usb2_remove, |
657b306a KVA |
286 | .driver = { |
287 | .name = "omap-usb2", | |
288 | .owner = THIS_MODULE, | |
289 | .pm = DEV_PM_OPS, | |
290 | .of_match_table = of_match_ptr(omap_usb2_id_table), | |
291 | }, | |
292 | }; | |
293 | ||
294 | module_platform_driver(omap_usb2_driver); | |
295 | ||
296 | MODULE_ALIAS("platform: omap_usb2"); | |
297 | MODULE_AUTHOR("Texas Instruments Inc."); | |
298 | MODULE_DESCRIPTION("OMAP USB2 phy driver"); | |
299 | MODULE_LICENSE("GPL v2"); |