blob: b220202a3f30d839990b56d60f539dc30a81b033 [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
Austin Beam7e4724022014-03-06 18:11:53 +053034#define USB2PHY_DISCON_BYP_LATCH (1 << 31)
35#define USB2PHY_ANA_CONFIG1 0x4c
36
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053037/**
38 * omap_usb2_set_comparator - links the comparator present in the sytem with
39 * this phy
40 * @comparator - the companion phy(comparator) for this phy
41 *
42 * The phy companion driver should call this API passing the phy_companion
43 * filled with set_vbus and start_srp to be used by usb phy.
44 *
45 * For use by phy companion driver
46 */
47int omap_usb2_set_comparator(struct phy_companion *comparator)
48{
49 struct omap_usb *phy;
50 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
51
52 if (IS_ERR(x))
53 return -ENODEV;
54
55 phy = phy_to_omapusb(x);
56 phy->comparator = comparator;
57 return 0;
58}
59EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
60
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053061static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
62{
63 struct omap_usb *phy = phy_to_omapusb(otg->phy);
64
65 if (!phy->comparator)
66 return -ENODEV;
67
68 return phy->comparator->set_vbus(phy->comparator, enabled);
69}
70
71static int omap_usb_start_srp(struct usb_otg *otg)
72{
73 struct omap_usb *phy = phy_to_omapusb(otg->phy);
74
75 if (!phy->comparator)
76 return -ENODEV;
77
78 return phy->comparator->start_srp(phy->comparator);
79}
80
81static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
82{
83 struct usb_phy *phy = otg->phy;
84
85 otg->host = host;
86 if (!host)
87 phy->state = OTG_STATE_UNDEFINED;
88
89 return 0;
90}
91
92static int omap_usb_set_peripheral(struct usb_otg *otg,
93 struct usb_gadget *gadget)
94{
95 struct usb_phy *phy = otg->phy;
96
97 otg->gadget = gadget;
98 if (!gadget)
99 phy->state = OTG_STATE_UNDEFINED;
100
101 return 0;
102}
103
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530104static int omap_usb_power_off(struct phy *x)
105{
106 struct omap_usb *phy = phy_get_drvdata(x);
107
108 omap_control_usb_phy_power(phy->control_dev, 0);
109
110 return 0;
111}
112
113static int omap_usb_power_on(struct phy *x)
114{
115 struct omap_usb *phy = phy_get_drvdata(x);
116
117 omap_control_usb_phy_power(phy->control_dev, 1);
118
119 return 0;
120}
121
Austin Beam7e4724022014-03-06 18:11:53 +0530122static int omap_usb_init(struct phy *x)
123{
124 struct omap_usb *phy = phy_get_drvdata(x);
125 u32 val;
126
127 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
128 /*
129 *
130 * Reduce the sensitivity of internal PHY by enabling the
131 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
132 * resolves issues with certain devices which can otherwise
133 * be prone to false disconnects.
134 *
135 */
136 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
137 val |= USB2PHY_DISCON_BYP_LATCH;
138 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
139 }
140
141 return 0;
142}
143
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530144static struct phy_ops ops = {
Austin Beam7e4724022014-03-06 18:11:53 +0530145 .init = omap_usb_init,
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530146 .power_on = omap_usb_power_on,
147 .power_off = omap_usb_power_off,
148 .owner = THIS_MODULE,
149};
150
George Cherian09a01682014-03-06 18:11:52 +0530151#ifdef CONFIG_OF
152static const struct usb_phy_data omap_usb2_data = {
153 .label = "omap_usb2",
154 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
155};
156
Austin Beam7e4724022014-03-06 18:11:53 +0530157static const struct usb_phy_data dra7x_usb2_data = {
158 .label = "dra7x_usb2",
159 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
160};
161
George Cherian09a01682014-03-06 18:11:52 +0530162static const struct usb_phy_data am437x_usb2_data = {
163 .label = "am437x_usb2",
164 .flags = 0,
165};
166
167static const struct of_device_id omap_usb2_id_table[] = {
168 {
169 .compatible = "ti,omap-usb2",
170 .data = &omap_usb2_data,
171 },
172 {
Austin Beam7e4724022014-03-06 18:11:53 +0530173 .compatible = "ti,dra7x-usb2",
174 .data = &dra7x_usb2_data,
175 },
176 {
George Cherian09a01682014-03-06 18:11:52 +0530177 .compatible = "ti,am437x-usb2",
178 .data = &am437x_usb2_data,
179 },
180 {},
181};
182MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
183#endif
184
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500185static int omap_usb2_probe(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530186{
Roger Quadros478b6c742013-10-03 18:12:32 +0300187 struct omap_usb *phy;
188 struct phy *generic_phy;
Austin Beam7e4724022014-03-06 18:11:53 +0530189 struct resource *res;
Roger Quadros478b6c742013-10-03 18:12:32 +0300190 struct phy_provider *phy_provider;
191 struct usb_otg *otg;
192 struct device_node *node = pdev->dev.of_node;
193 struct device_node *control_node;
194 struct platform_device *control_pdev;
George Cherian09a01682014-03-06 18:11:52 +0530195 const struct of_device_id *of_id;
196 struct usb_phy_data *phy_data;
Roger Quadros478b6c742013-10-03 18:12:32 +0300197
George Cherian09a01682014-03-06 18:11:52 +0530198 of_id = of_match_device(of_match_ptr(omap_usb2_id_table), &pdev->dev);
199
200 if (!of_id)
Roger Quadros478b6c742013-10-03 18:12:32 +0300201 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530202
George Cherian09a01682014-03-06 18:11:52 +0530203 phy_data = (struct usb_phy_data *)of_id->data;
204
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530205 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
206 if (!phy) {
207 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
208 return -ENOMEM;
209 }
210
211 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
212 if (!otg) {
213 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
214 return -ENOMEM;
215 }
216
217 phy->dev = &pdev->dev;
218
219 phy->phy.dev = phy->dev;
George Cherian09a01682014-03-06 18:11:52 +0530220 phy->phy.label = phy_data->label;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530221 phy->phy.otg = otg;
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530222 phy->phy.type = USB_PHY_TYPE_USB2;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530223
Austin Beam7e4724022014-03-06 18:11:53 +0530224 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
227 if (!phy->phy_base)
228 return -ENOMEM;
229 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
230 }
231
Roger Quadros478b6c742013-10-03 18:12:32 +0300232 control_node = of_parse_phandle(node, "ctrl-module", 0);
233 if (!control_node) {
234 dev_err(&pdev->dev, "Failed to get control device phandle\n");
235 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530236 }
237
Roger Quadros478b6c742013-10-03 18:12:32 +0300238 control_pdev = of_find_device_by_node(control_node);
239 if (!control_pdev) {
240 dev_err(&pdev->dev, "Failed to get control device\n");
241 return -EINVAL;
242 }
243
244 phy->control_dev = &control_pdev->dev;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530245 omap_control_usb_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530246
247 otg->set_host = omap_usb_set_host;
248 otg->set_peripheral = omap_usb_set_peripheral;
George Cherian09a01682014-03-06 18:11:52 +0530249 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
250 otg->set_vbus = omap_usb_set_vbus;
251 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
252 otg->start_srp = omap_usb_start_srp;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530253 otg->phy = &phy->phy;
254
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530255 platform_set_drvdata(pdev, phy);
256 pm_runtime_enable(phy->dev);
257
258 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
259 if (IS_ERR(generic_phy))
260 return PTR_ERR(generic_phy);
261
262 phy_set_drvdata(generic_phy, phy);
263
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530264 phy_provider = devm_of_phy_provider_register(phy->dev,
265 of_phy_simple_xlate);
266 if (IS_ERR(phy_provider))
267 return PTR_ERR(phy_provider);
268
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530269 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
270 if (IS_ERR(phy->wkupclk)) {
271 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
272 return PTR_ERR(phy->wkupclk);
273 }
274 clk_prepare(phy->wkupclk);
275
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530276 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
277 if (IS_ERR(phy->optclk))
278 dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
279 else
280 clk_prepare(phy->optclk);
281
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530282 usb_add_phy_dev(&phy->phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530283
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530284 return 0;
285}
286
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500287static int omap_usb2_remove(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530288{
289 struct omap_usb *phy = platform_get_drvdata(pdev);
290
291 clk_unprepare(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530292 if (!IS_ERR(phy->optclk))
293 clk_unprepare(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530294 usb_remove_phy(&phy->phy);
295
296 return 0;
297}
298
299#ifdef CONFIG_PM_RUNTIME
300
301static int omap_usb2_runtime_suspend(struct device *dev)
302{
303 struct platform_device *pdev = to_platform_device(dev);
304 struct omap_usb *phy = platform_get_drvdata(pdev);
305
306 clk_disable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530307 if (!IS_ERR(phy->optclk))
308 clk_disable(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530309
310 return 0;
311}
312
313static int omap_usb2_runtime_resume(struct device *dev)
314{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530315 struct platform_device *pdev = to_platform_device(dev);
316 struct omap_usb *phy = platform_get_drvdata(pdev);
Dan Carpenter0f827682013-08-21 11:41:22 +0300317 int ret;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530318
319 ret = clk_enable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530320 if (ret < 0) {
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530321 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530322 goto err0;
323 }
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530324
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530325 if (!IS_ERR(phy->optclk)) {
326 ret = clk_enable(phy->optclk);
327 if (ret < 0) {
328 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
329 goto err1;
330 }
331 }
332
333 return 0;
334
335err1:
336 clk_disable(phy->wkupclk);
337
338err0:
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530339 return ret;
340}
341
342static const struct dev_pm_ops omap_usb2_pm_ops = {
343 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
344 NULL)
345};
346
347#define DEV_PM_OPS (&omap_usb2_pm_ops)
348#else
349#define DEV_PM_OPS NULL
350#endif
351
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530352static struct platform_driver omap_usb2_driver = {
353 .probe = omap_usb2_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500354 .remove = omap_usb2_remove,
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530355 .driver = {
356 .name = "omap-usb2",
357 .owner = THIS_MODULE,
358 .pm = DEV_PM_OPS,
359 .of_match_table = of_match_ptr(omap_usb2_id_table),
360 },
361};
362
363module_platform_driver(omap_usb2_driver);
364
365MODULE_ALIAS("platform: omap_usb2");
366MODULE_AUTHOR("Texas Instruments Inc.");
367MODULE_DESCRIPTION("OMAP USB2 phy driver");
368MODULE_LICENSE("GPL v2");