blob: 0cd88ac0e0952b4113489479a37ef4669a536e08 [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 I657b3062012-09-06 20:27:06 +053031
32/**
33 * omap_usb2_set_comparator - links the comparator present in the sytem with
34 * this phy
35 * @comparator - the companion phy(comparator) for this phy
36 *
37 * The phy companion driver should call this API passing the phy_companion
38 * filled with set_vbus and start_srp to be used by usb phy.
39 *
40 * For use by phy companion driver
41 */
42int omap_usb2_set_comparator(struct phy_companion *comparator)
43{
44 struct omap_usb *phy;
45 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
46
47 if (IS_ERR(x))
48 return -ENODEV;
49
50 phy = phy_to_omapusb(x);
51 phy->comparator = comparator;
52 return 0;
53}
54EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
55
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053056static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
57{
58 struct omap_usb *phy = phy_to_omapusb(otg->phy);
59
60 if (!phy->comparator)
61 return -ENODEV;
62
63 return phy->comparator->set_vbus(phy->comparator, enabled);
64}
65
66static int omap_usb_start_srp(struct usb_otg *otg)
67{
68 struct omap_usb *phy = phy_to_omapusb(otg->phy);
69
70 if (!phy->comparator)
71 return -ENODEV;
72
73 return phy->comparator->start_srp(phy->comparator);
74}
75
76static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
77{
78 struct usb_phy *phy = otg->phy;
79
80 otg->host = host;
81 if (!host)
82 phy->state = OTG_STATE_UNDEFINED;
83
84 return 0;
85}
86
87static int omap_usb_set_peripheral(struct usb_otg *otg,
88 struct usb_gadget *gadget)
89{
90 struct usb_phy *phy = otg->phy;
91
92 otg->gadget = gadget;
93 if (!gadget)
94 phy->state = OTG_STATE_UNDEFINED;
95
96 return 0;
97}
98
99static int omap_usb2_suspend(struct usb_phy *x, int suspend)
100{
101 u32 ret;
102 struct omap_usb *phy = phy_to_omapusb(x);
103
104 if (suspend && !phy->is_suspended) {
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530105 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530106 pm_runtime_put_sync(phy->dev);
107 phy->is_suspended = 1;
108 } else if (!suspend && phy->is_suspended) {
109 ret = pm_runtime_get_sync(phy->dev);
110 if (ret < 0) {
111 dev_err(phy->dev, "get_sync failed with err %d\n",
112 ret);
113 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
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500122static int omap_usb2_probe(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530123{
124 struct omap_usb *phy;
125 struct usb_otg *otg;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530126
127 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
128 if (!phy) {
129 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
130 return -ENOMEM;
131 }
132
133 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
134 if (!otg) {
135 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
136 return -ENOMEM;
137 }
138
139 phy->dev = &pdev->dev;
140
141 phy->phy.dev = phy->dev;
142 phy->phy.label = "omap-usb2";
143 phy->phy.set_suspend = omap_usb2_suspend;
144 phy->phy.otg = otg;
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530145 phy->phy.type = USB_PHY_TYPE_USB2;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530146
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530147 phy->control_dev = omap_get_control_dev();
148 if (IS_ERR(phy->control_dev)) {
149 dev_dbg(&pdev->dev, "Failed to get control device\n");
150 return -ENODEV;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530151 }
152
153 phy->is_suspended = 1;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530154 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530155
156 otg->set_host = omap_usb_set_host;
157 otg->set_peripheral = omap_usb_set_peripheral;
158 otg->set_vbus = omap_usb_set_vbus;
159 otg->start_srp = omap_usb_start_srp;
160 otg->phy = &phy->phy;
161
162 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
163 if (IS_ERR(phy->wkupclk)) {
164 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
165 return PTR_ERR(phy->wkupclk);
166 }
167 clk_prepare(phy->wkupclk);
168
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530169 usb_add_phy_dev(&phy->phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530170
171 platform_set_drvdata(pdev, phy);
172
173 pm_runtime_enable(phy->dev);
174
175 return 0;
176}
177
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500178static int omap_usb2_remove(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530179{
180 struct omap_usb *phy = platform_get_drvdata(pdev);
181
182 clk_unprepare(phy->wkupclk);
183 usb_remove_phy(&phy->phy);
184
185 return 0;
186}
187
188#ifdef CONFIG_PM_RUNTIME
189
190static int omap_usb2_runtime_suspend(struct device *dev)
191{
192 struct platform_device *pdev = to_platform_device(dev);
193 struct omap_usb *phy = platform_get_drvdata(pdev);
194
195 clk_disable(phy->wkupclk);
196
197 return 0;
198}
199
200static int omap_usb2_runtime_resume(struct device *dev)
201{
202 u32 ret = 0;
203 struct platform_device *pdev = to_platform_device(dev);
204 struct omap_usb *phy = platform_get_drvdata(pdev);
205
206 ret = clk_enable(phy->wkupclk);
207 if (ret < 0)
208 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
209
210 return ret;
211}
212
213static const struct dev_pm_ops omap_usb2_pm_ops = {
214 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
215 NULL)
216};
217
218#define DEV_PM_OPS (&omap_usb2_pm_ops)
219#else
220#define DEV_PM_OPS NULL
221#endif
222
223#ifdef CONFIG_OF
224static const struct of_device_id omap_usb2_id_table[] = {
225 { .compatible = "ti,omap-usb2" },
226 {}
227};
228MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
229#endif
230
231static struct platform_driver omap_usb2_driver = {
232 .probe = omap_usb2_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500233 .remove = omap_usb2_remove,
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530234 .driver = {
235 .name = "omap-usb2",
236 .owner = THIS_MODULE,
237 .pm = DEV_PM_OPS,
238 .of_match_table = of_match_ptr(omap_usb2_id_table),
239 },
240};
241
242module_platform_driver(omap_usb2_driver);
243
244MODULE_ALIAS("platform: omap_usb2");
245MODULE_AUTHOR("Texas Instruments Inc.");
246MODULE_DESCRIPTION("OMAP USB2 phy driver");
247MODULE_LICENSE("GPL v2");