blob: 6f4aef3db2481de0dc03df70521242a9be5d7fa5 [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 I14da6992014-03-06 16:38:37 +020030#include <linux/phy/omap_control_phy.h>
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +053031#include <linux/phy/phy.h>
Roger Quadros478b6c72013-10-03 18:12:32 +030032#include <linux/of_platform.h>
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053033
Austin Beam7e472402014-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{
Antoine Tenart19c1eac2014-10-30 18:41:14 +010063 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053064
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{
Antoine Tenart19c1eac2014-10-30 18:41:14 +010073 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053074
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{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053083 otg->host = host;
84 if (!host)
Antoine Tenarte47d9252014-10-30 18:41:13 +010085 otg->state = OTG_STATE_UNDEFINED;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053086
87 return 0;
88}
89
90static int omap_usb_set_peripheral(struct usb_otg *otg,
91 struct usb_gadget *gadget)
92{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053093 otg->gadget = gadget;
94 if (!gadget)
Antoine Tenarte47d9252014-10-30 18:41:13 +010095 otg->state = OTG_STATE_UNDEFINED;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +053096
97 return 0;
98}
99
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530100static int omap_usb_power_off(struct phy *x)
101{
102 struct omap_usb *phy = phy_get_drvdata(x);
103
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200104 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530105
106 return 0;
107}
108
109static int omap_usb_power_on(struct phy *x)
110{
111 struct omap_usb *phy = phy_get_drvdata(x);
112
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200113 omap_control_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530114
115 return 0;
116}
117
Austin Beam7e472402014-03-06 18:11:53 +0530118static int omap_usb_init(struct phy *x)
119{
120 struct omap_usb *phy = phy_get_drvdata(x);
121 u32 val;
122
123 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
124 /*
125 *
126 * Reduce the sensitivity of internal PHY by enabling the
127 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
128 * resolves issues with certain devices which can otherwise
129 * be prone to false disconnects.
130 *
131 */
132 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
133 val |= USB2PHY_DISCON_BYP_LATCH;
134 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
135 }
136
137 return 0;
138}
139
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530140static struct phy_ops ops = {
Austin Beam7e472402014-03-06 18:11:53 +0530141 .init = omap_usb_init,
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530142 .power_on = omap_usb_power_on,
143 .power_off = omap_usb_power_off,
144 .owner = THIS_MODULE,
145};
146
George Cherian09a01682014-03-06 18:11:52 +0530147#ifdef CONFIG_OF
148static const struct usb_phy_data omap_usb2_data = {
149 .label = "omap_usb2",
150 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
151};
152
George Cheriandb68e802014-03-06 18:11:54 +0530153static const struct usb_phy_data omap5_usb2_data = {
154 .label = "omap5_usb2",
155 .flags = 0,
156};
157
Austin Beam7e472402014-03-06 18:11:53 +0530158static const struct usb_phy_data dra7x_usb2_data = {
159 .label = "dra7x_usb2",
160 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
161};
162
George Cherian09a01682014-03-06 18:11:52 +0530163static const struct usb_phy_data am437x_usb2_data = {
164 .label = "am437x_usb2",
165 .flags = 0,
166};
167
168static const struct of_device_id omap_usb2_id_table[] = {
169 {
170 .compatible = "ti,omap-usb2",
171 .data = &omap_usb2_data,
172 },
173 {
George Cheriandb68e802014-03-06 18:11:54 +0530174 .compatible = "ti,omap5-usb2",
175 .data = &omap5_usb2_data,
176 },
177 {
Austin Beam7e472402014-03-06 18:11:53 +0530178 .compatible = "ti,dra7x-usb2",
179 .data = &dra7x_usb2_data,
180 },
181 {
George Cherian09a01682014-03-06 18:11:52 +0530182 .compatible = "ti,am437x-usb2",
183 .data = &am437x_usb2_data,
184 },
185 {},
186};
187MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
188#endif
189
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500190static int omap_usb2_probe(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530191{
Roger Quadros478b6c72013-10-03 18:12:32 +0300192 struct omap_usb *phy;
193 struct phy *generic_phy;
Austin Beam7e472402014-03-06 18:11:53 +0530194 struct resource *res;
Roger Quadros478b6c72013-10-03 18:12:32 +0300195 struct phy_provider *phy_provider;
196 struct usb_otg *otg;
197 struct device_node *node = pdev->dev.of_node;
198 struct device_node *control_node;
199 struct platform_device *control_pdev;
George Cherian09a01682014-03-06 18:11:52 +0530200 const struct of_device_id *of_id;
201 struct usb_phy_data *phy_data;
Roger Quadros478b6c72013-10-03 18:12:32 +0300202
George Cherian09a01682014-03-06 18:11:52 +0530203 of_id = of_match_device(of_match_ptr(omap_usb2_id_table), &pdev->dev);
204
205 if (!of_id)
Roger Quadros478b6c72013-10-03 18:12:32 +0300206 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530207
George Cherian09a01682014-03-06 18:11:52 +0530208 phy_data = (struct usb_phy_data *)of_id->data;
209
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530210 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
Peter Griffin0b682532014-08-15 13:40:10 +0100211 if (!phy)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530212 return -ENOMEM;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530213
214 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
Peter Griffin0b682532014-08-15 13:40:10 +0100215 if (!otg)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530216 return -ENOMEM;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530217
218 phy->dev = &pdev->dev;
219
220 phy->phy.dev = phy->dev;
George Cherian09a01682014-03-06 18:11:52 +0530221 phy->phy.label = phy_data->label;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530222 phy->phy.otg = otg;
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530223 phy->phy.type = USB_PHY_TYPE_USB2;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530224
Austin Beam7e472402014-03-06 18:11:53 +0530225 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
226 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
227 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
Himangi Saraogi3df9fcd2014-07-10 11:55:00 +0530228 if (IS_ERR(phy->phy_base))
229 return PTR_ERR(phy->phy_base);
Austin Beam7e472402014-03-06 18:11:53 +0530230 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
231 }
232
Roger Quadros478b6c72013-10-03 18:12:32 +0300233 control_node = of_parse_phandle(node, "ctrl-module", 0);
234 if (!control_node) {
235 dev_err(&pdev->dev, "Failed to get control device phandle\n");
236 return -EINVAL;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530237 }
238
Roger Quadros478b6c72013-10-03 18:12:32 +0300239 control_pdev = of_find_device_by_node(control_node);
240 if (!control_pdev) {
241 dev_err(&pdev->dev, "Failed to get control device\n");
242 return -EINVAL;
243 }
244
245 phy->control_dev = &control_pdev->dev;
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200246 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530247
248 otg->set_host = omap_usb_set_host;
249 otg->set_peripheral = omap_usb_set_peripheral;
George Cherian09a01682014-03-06 18:11:52 +0530250 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
251 otg->set_vbus = omap_usb_set_vbus;
252 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
253 otg->start_srp = omap_usb_start_srp;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100254 otg->usb_phy = &phy->phy;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530255
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530256 platform_set_drvdata(pdev, phy);
Oussama Ghorbelf20531a2014-11-04 11:47:06 +0530257 pm_runtime_enable(phy->dev);
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530258
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200259 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
Oussama Ghorbelf20531a2014-11-04 11:47:06 +0530260 if (IS_ERR(generic_phy)) {
261 pm_runtime_disable(phy->dev);
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530262 return PTR_ERR(generic_phy);
Oussama Ghorbelf20531a2014-11-04 11:47:06 +0530263 }
Kishon Vijay Abraham I5d93d1e2013-09-27 11:53:26 +0530264
265 phy_set_drvdata(generic_phy, phy);
266
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530267 phy_provider = devm_of_phy_provider_register(phy->dev,
268 of_phy_simple_xlate);
Roger Quadroseb82a3d2014-07-10 11:55:03 +0530269 if (IS_ERR(phy_provider)) {
270 pm_runtime_disable(phy->dev);
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530271 return PTR_ERR(phy_provider);
Roger Quadroseb82a3d2014-07-10 11:55:03 +0530272 }
Kishon Vijay Abraham I64fe1892014-02-17 14:29:25 +0530273
Roger Quadros9f1d8ed2014-05-05 12:54:40 +0300274 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530275 if (IS_ERR(phy->wkupclk)) {
Roger Quadros9f1d8ed2014-05-05 12:54:40 +0300276 dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n");
277 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
278 if (IS_ERR(phy->wkupclk)) {
279 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
280 return PTR_ERR(phy->wkupclk);
281 } else {
282 dev_warn(&pdev->dev,
283 "found usb_phy_cm_clk32k, please fix DTS\n");
284 }
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530285 }
286 clk_prepare(phy->wkupclk);
287
Roger Quadros9f1d8ed2014-05-05 12:54:40 +0300288 phy->optclk = devm_clk_get(phy->dev, "refclk");
289 if (IS_ERR(phy->optclk)) {
290 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
291 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
292 if (IS_ERR(phy->optclk)) {
293 dev_dbg(&pdev->dev,
294 "unable to get usb_otg_ss_refclk960m\n");
295 } else {
296 dev_warn(&pdev->dev,
297 "found usb_otg_ss_refclk960m, please fix DTS\n");
298 }
299 } else {
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530300 clk_prepare(phy->optclk);
Roger Quadros9f1d8ed2014-05-05 12:54:40 +0300301 }
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530302
Kishon Vijay Abraham Ic11747f2013-01-25 08:03:24 +0530303 usb_add_phy_dev(&phy->phy);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530304
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530305 return 0;
306}
307
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500308static int omap_usb2_remove(struct platform_device *pdev)
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530309{
310 struct omap_usb *phy = platform_get_drvdata(pdev);
311
312 clk_unprepare(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530313 if (!IS_ERR(phy->optclk))
314 clk_unprepare(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530315 usb_remove_phy(&phy->phy);
Roger Quadroseb82a3d2014-07-10 11:55:03 +0530316 pm_runtime_disable(phy->dev);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530317
318 return 0;
319}
320
Rafael J. Wysocki7ba37052014-12-13 00:42:12 +0100321#ifdef CONFIG_PM
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530322
323static int omap_usb2_runtime_suspend(struct device *dev)
324{
325 struct platform_device *pdev = to_platform_device(dev);
326 struct omap_usb *phy = platform_get_drvdata(pdev);
327
328 clk_disable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530329 if (!IS_ERR(phy->optclk))
330 clk_disable(phy->optclk);
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530331
332 return 0;
333}
334
335static int omap_usb2_runtime_resume(struct device *dev)
336{
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530337 struct platform_device *pdev = to_platform_device(dev);
338 struct omap_usb *phy = platform_get_drvdata(pdev);
Dan Carpenter0f827682013-08-21 11:41:22 +0300339 int ret;
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530340
341 ret = clk_enable(phy->wkupclk);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530342 if (ret < 0) {
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530343 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530344 goto err0;
345 }
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530346
Kishon Vijay Abraham I0c4c8bb2013-01-25 08:21:49 +0530347 if (!IS_ERR(phy->optclk)) {
348 ret = clk_enable(phy->optclk);
349 if (ret < 0) {
350 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
351 goto err1;
352 }
353 }
354
355 return 0;
356
357err1:
358 clk_disable(phy->wkupclk);
359
360err0:
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530361 return ret;
362}
363
364static const struct dev_pm_ops omap_usb2_pm_ops = {
365 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
366 NULL)
367};
368
369#define DEV_PM_OPS (&omap_usb2_pm_ops)
370#else
371#define DEV_PM_OPS NULL
372#endif
373
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530374static struct platform_driver omap_usb2_driver = {
375 .probe = omap_usb2_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500376 .remove = omap_usb2_remove,
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530377 .driver = {
378 .name = "omap-usb2",
Kishon Vijay Abraham I657b3062012-09-06 20:27:06 +0530379 .pm = DEV_PM_OPS,
380 .of_match_table = of_match_ptr(omap_usb2_id_table),
381 },
382};
383
384module_platform_driver(omap_usb2_driver);
385
386MODULE_ALIAS("platform: omap_usb2");
387MODULE_AUTHOR("Texas Instruments Inc.");
388MODULE_DESCRIPTION("OMAP USB2 phy driver");
389MODULE_LICENSE("GPL v2");