blob: 0c78f54d0650e9938d9c35d604d93fbdd2689dad [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>
Kishon Vijay Abraham I6284be22014-03-03 17:08:14 +053024#include <linux/phy/omap_usb.h>
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053025#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>
Roger Quadros478b6c742013-10-03 18:12:32 +030032#include <linux/of_platform.h>
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053033
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 */
44int 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}
56EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
57
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053058static 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
68static 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
78static 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
89static 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
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530101static 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
110static 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
119static struct phy_ops ops = {
120 .power_on = omap_usb_power_on,
121 .power_off = omap_usb_power_off,
122 .owner = THIS_MODULE,
123};
124
George Cherian09a01682014-03-06 18:11:52 +0530125#ifdef CONFIG_OF
126static const struct usb_phy_data omap_usb2_data = {
127 .label = "omap_usb2",
128 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
129};
130
131static const struct usb_phy_data am437x_usb2_data = {
132 .label = "am437x_usb2",
133 .flags = 0,
134};
135
136static const struct of_device_id omap_usb2_id_table[] = {
137 {
138 .compatible = "ti,omap-usb2",
139 .data = &omap_usb2_data,
140 },
141 {
142 .compatible = "ti,am437x-usb2",
143 .data = &am437x_usb2_data,
144 },
145 {},
146};
147MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
148#endif
149
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500150static int omap_usb2_probe(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530151{
Roger Quadros478b6c742013-10-03 18:12:32 +0300152 struct omap_usb *phy;
153 struct phy *generic_phy;
154 struct phy_provider *phy_provider;
155 struct usb_otg *otg;
156 struct device_node *node = pdev->dev.of_node;
157 struct device_node *control_node;
158 struct platform_device *control_pdev;
George Cherian09a01682014-03-06 18:11:52 +0530159 const struct of_device_id *of_id;
160 struct usb_phy_data *phy_data;
Roger Quadros478b6c742013-10-03 18:12:32 +0300161
George Cherian09a01682014-03-06 18:11:52 +0530162 of_id = of_match_device(of_match_ptr(omap_usb2_id_table), &pdev->dev);
163
164 if (!of_id)
Roger Quadros478b6c742013-10-03 18:12:32 +0300165 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530166
George Cherian09a01682014-03-06 18:11:52 +0530167 phy_data = (struct usb_phy_data *)of_id->data;
168
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530169 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
170 if (!phy) {
171 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
172 return -ENOMEM;
173 }
174
175 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
176 if (!otg) {
177 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
178 return -ENOMEM;
179 }
180
181 phy->dev = &pdev->dev;
182
183 phy->phy.dev = phy->dev;
George Cherian09a01682014-03-06 18:11:52 +0530184 phy->phy.label = phy_data->label;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530185 phy->phy.otg = otg;
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530186 phy->phy.type = USB_PHY_TYPE_USB2;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530187
Roger Quadros478b6c742013-10-03 18:12:32 +0300188 control_node = of_parse_phandle(node, "ctrl-module", 0);
189 if (!control_node) {
190 dev_err(&pdev->dev, "Failed to get control device phandle\n");
191 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530192 }
193
Roger Quadros478b6c742013-10-03 18:12:32 +0300194 control_pdev = of_find_device_by_node(control_node);
195 if (!control_pdev) {
196 dev_err(&pdev->dev, "Failed to get control device\n");
197 return -EINVAL;
198 }
199
200 phy->control_dev = &control_pdev->dev;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530201 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530202
203 otg->set_host = omap_usb_set_host;
204 otg->set_peripheral = omap_usb_set_peripheral;
George Cherian09a01682014-03-06 18:11:52 +0530205 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
206 otg->set_vbus = omap_usb_set_vbus;
207 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
208 otg->start_srp = omap_usb_start_srp;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530209 otg->phy = &phy->phy;
210
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530211 platform_set_drvdata(pdev, phy);
212 pm_runtime_enable(phy->dev);
213
214 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
215 if (IS_ERR(generic_phy))
216 return PTR_ERR(generic_phy);
217
218 phy_set_drvdata(generic_phy, phy);
219
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530220 phy_provider = devm_of_phy_provider_register(phy->dev,
221 of_phy_simple_xlate);
222 if (IS_ERR(phy_provider))
223 return PTR_ERR(phy_provider);
224
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530225 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
226 if (IS_ERR(phy->wkupclk)) {
227 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
228 return PTR_ERR(phy->wkupclk);
229 }
230 clk_prepare(phy->wkupclk);
231
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530232 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
233 if (IS_ERR(phy->optclk))
234 dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
235 else
236 clk_prepare(phy->optclk);
237
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530238 usb_add_phy_dev(&phy->phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530239
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530240 return 0;
241}
242
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500243static int omap_usb2_remove(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530244{
245 struct omap_usb *phy = platform_get_drvdata(pdev);
246
247 clk_unprepare(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530248 if (!IS_ERR(phy->optclk))
249 clk_unprepare(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530250 usb_remove_phy(&phy->phy);
251
252 return 0;
253}
254
255#ifdef CONFIG_PM_RUNTIME
256
257static int omap_usb2_runtime_suspend(struct device *dev)
258{
259 struct platform_device *pdev = to_platform_device(dev);
260 struct omap_usb *phy = platform_get_drvdata(pdev);
261
262 clk_disable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530263 if (!IS_ERR(phy->optclk))
264 clk_disable(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530265
266 return 0;
267}
268
269static int omap_usb2_runtime_resume(struct device *dev)
270{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530271 struct platform_device *pdev = to_platform_device(dev);
272 struct omap_usb *phy = platform_get_drvdata(pdev);
Dan Carpenter0f827682013-08-21 11:41:22 +0300273 int ret;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530274
275 ret = clk_enable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530276 if (ret < 0) {
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530277 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530278 goto err0;
279 }
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530280
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530281 if (!IS_ERR(phy->optclk)) {
282 ret = clk_enable(phy->optclk);
283 if (ret < 0) {
284 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
285 goto err1;
286 }
287 }
288
289 return 0;
290
291err1:
292 clk_disable(phy->wkupclk);
293
294err0:
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530295 return ret;
296}
297
298static const struct dev_pm_ops omap_usb2_pm_ops = {
299 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
300 NULL)
301};
302
303#define DEV_PM_OPS (&omap_usb2_pm_ops)
304#else
305#define DEV_PM_OPS NULL
306#endif
307
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530308static struct platform_driver omap_usb2_driver = {
309 .probe = omap_usb2_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500310 .remove = omap_usb2_remove,
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530311 .driver = {
312 .name = "omap-usb2",
313 .owner = THIS_MODULE,
314 .pm = DEV_PM_OPS,
315 .of_match_table = of_match_ptr(omap_usb2_id_table),
316 },
317};
318
319module_platform_driver(omap_usb2_driver);
320
321MODULE_ALIAS("platform: omap_usb2");
322MODULE_AUTHOR("Texas Instruments Inc.");
323MODULE_DESCRIPTION("OMAP USB2 phy driver");
324MODULE_LICENSE("GPL v2");