blob: 4d7b4e5a965909ec49547c7ba42c034dd4352cfa [file] [log] [blame]
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +05301/*
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>
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +053030#include <linux/usb/omap_control_usb.h>
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +053031#include <linux/phy/phy.h>
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053032
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 */
43int 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}
55EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
56
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053057static 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
67static 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
77static 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
88static 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
100static int omap_usb2_suspend(struct usb_phy *x, int suspend)
101{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530102 struct omap_usb *phy = phy_to_omapusb(x);
Dan Carpenter0f827682013-08-21 11:41:22 +0300103 int ret;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530104
105 if (suspend && !phy->is_suspended) {
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530106 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530107 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) {
Dan Carpenter0f827682013-08-21 11:41:22 +0300112 dev_err(phy->dev, "get_sync failed with err %d\n", ret);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530113 return ret;
114 }
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530115 omap_control_usb_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530116 phy->is_suspended = 0;
117 }
118
119 return 0;
120}
121
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530122static 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
131static 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
140static struct phy_ops ops = {
141 .power_on = omap_usb_power_on,
142 .power_off = omap_usb_power_off,
143 .owner = THIS_MODULE,
144};
145
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500146static int omap_usb2_probe(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530147{
148 struct omap_usb *phy;
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530149 struct phy *generic_phy;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530150 struct usb_otg *otg;
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530151 struct phy_provider *phy_provider;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530152
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;
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530171 phy->phy.type = USB_PHY_TYPE_USB2;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530172
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530173 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
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530178 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;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530182 }
183
184 phy->is_suspended = 1;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530185 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530186
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
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530193 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
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530202 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
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530209 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
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530215 usb_add_phy_dev(&phy->phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530216
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530217 return 0;
218}
219
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500220static int omap_usb2_remove(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530221{
222 struct omap_usb *phy = platform_get_drvdata(pdev);
223
224 clk_unprepare(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530225 if (!IS_ERR(phy->optclk))
226 clk_unprepare(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530227 usb_remove_phy(&phy->phy);
228
229 return 0;
230}
231
232#ifdef CONFIG_PM_RUNTIME
233
234static 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);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530240 if (!IS_ERR(phy->optclk))
241 clk_disable(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530242
243 return 0;
244}
245
246static int omap_usb2_runtime_resume(struct device *dev)
247{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530248 struct platform_device *pdev = to_platform_device(dev);
249 struct omap_usb *phy = platform_get_drvdata(pdev);
Dan Carpenter0f827682013-08-21 11:41:22 +0300250 int ret;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530251
252 ret = clk_enable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530253 if (ret < 0) {
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530254 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530255 goto err0;
256 }
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530257
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530258 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
268err1:
269 clk_disable(phy->wkupclk);
270
271err0:
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530272 return ret;
273}
274
275static 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
286static const struct of_device_id omap_usb2_id_table[] = {
287 { .compatible = "ti,omap-usb2" },
288 {}
289};
290MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
291#endif
292
293static struct platform_driver omap_usb2_driver = {
294 .probe = omap_usb2_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500295 .remove = omap_usb2_remove,
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530296 .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
304module_platform_driver(omap_usb2_driver);
305
306MODULE_ALIAS("platform: omap_usb2");
307MODULE_AUTHOR("Texas Instruments Inc.");
308MODULE_DESCRIPTION("OMAP USB2 phy driver");
309MODULE_LICENSE("GPL v2");