usb: phy: omap-usb2: use the new generic PHY framework
[firefly-linux-kernel-4.4.55.git] / drivers / phy / phy-omap-usb2.c
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>
30 #include <linux/usb/omap_control_usb.h>
31 #include <linux/phy/phy.h>
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
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 {
102         struct omap_usb *phy = phy_to_omapusb(x);
103         int ret;
104
105         if (suspend && !phy->is_suspended) {
106                 omap_control_usb_phy_power(phy->control_dev, 0);
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) {
112                         dev_err(phy->dev, "get_sync failed with err %d\n", ret);
113                         return ret;
114                 }
115                 omap_control_usb_phy_power(phy->control_dev, 1);
116                 phy->is_suspended = 0;
117         }
118
119         return 0;
120 }
121
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
146 static int omap_usb2_probe(struct platform_device *pdev)
147 {
148         struct omap_usb                 *phy;
149         struct phy                      *generic_phy;
150         struct usb_otg                  *otg;
151         struct phy_provider             *phy_provider;
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;
171         phy->phy.type           = USB_PHY_TYPE_USB2;
172
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
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;
182         }
183
184         phy->is_suspended       = 1;
185         omap_control_usb_phy_power(phy->control_dev, 0);
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
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
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
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
215         usb_add_phy_dev(&phy->phy);
216
217         return 0;
218 }
219
220 static int omap_usb2_remove(struct platform_device *pdev)
221 {
222         struct omap_usb *phy = platform_get_drvdata(pdev);
223
224         clk_unprepare(phy->wkupclk);
225         if (!IS_ERR(phy->optclk))
226                 clk_unprepare(phy->optclk);
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);
240         if (!IS_ERR(phy->optclk))
241                 clk_disable(phy->optclk);
242
243         return 0;
244 }
245
246 static int omap_usb2_runtime_resume(struct device *dev)
247 {
248         struct platform_device  *pdev = to_platform_device(dev);
249         struct omap_usb *phy = platform_get_drvdata(pdev);
250         int ret;
251
252         ret = clk_enable(phy->wkupclk);
253         if (ret < 0) {
254                 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
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);
270
271 err0:
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,
295         .remove         = omap_usb2_remove,
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");