Linux 4.2.1
[linux/fpc-iii.git] / drivers / phy / phy-omap-usb2.c
blobc1a468686bdc72433b7596512cb70852f3ef2420
1 /*
2 * omap-usb2.c - USB PHY, talking to musb controller in OMAP.
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.
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
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.
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/phy/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>
30 #include <linux/phy/omap_control_phy.h>
31 #include <linux/phy/phy.h>
32 #include <linux/of_platform.h>
34 #define USB2PHY_DISCON_BYP_LATCH (1 << 31)
35 #define USB2PHY_ANA_CONFIG1 0x4c
37 /**
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
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.
45 * For use by phy companion driver
47 int omap_usb2_set_comparator(struct phy_companion *comparator)
49 struct omap_usb *phy;
50 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
52 if (IS_ERR(x))
53 return -ENODEV;
55 phy = phy_to_omapusb(x);
56 phy->comparator = comparator;
57 return 0;
59 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
61 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
63 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
65 if (!phy->comparator)
66 return -ENODEV;
68 return phy->comparator->set_vbus(phy->comparator, enabled);
71 static int omap_usb_start_srp(struct usb_otg *otg)
73 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
75 if (!phy->comparator)
76 return -ENODEV;
78 return phy->comparator->start_srp(phy->comparator);
81 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
83 otg->host = host;
84 if (!host)
85 otg->state = OTG_STATE_UNDEFINED;
87 return 0;
90 static int omap_usb_set_peripheral(struct usb_otg *otg,
91 struct usb_gadget *gadget)
93 otg->gadget = gadget;
94 if (!gadget)
95 otg->state = OTG_STATE_UNDEFINED;
97 return 0;
100 static int omap_usb_power_off(struct phy *x)
102 struct omap_usb *phy = phy_get_drvdata(x);
104 omap_control_phy_power(phy->control_dev, 0);
106 return 0;
109 static int omap_usb_power_on(struct phy *x)
111 struct omap_usb *phy = phy_get_drvdata(x);
113 omap_control_phy_power(phy->control_dev, 1);
115 return 0;
118 static int omap_usb_init(struct phy *x)
120 struct omap_usb *phy = phy_get_drvdata(x);
121 u32 val;
123 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
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.
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);
137 return 0;
140 static struct phy_ops ops = {
141 .init = omap_usb_init,
142 .power_on = omap_usb_power_on,
143 .power_off = omap_usb_power_off,
144 .owner = THIS_MODULE,
147 static const struct usb_phy_data omap_usb2_data = {
148 .label = "omap_usb2",
149 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
152 static const struct usb_phy_data omap5_usb2_data = {
153 .label = "omap5_usb2",
154 .flags = 0,
157 static const struct usb_phy_data dra7x_usb2_data = {
158 .label = "dra7x_usb2",
159 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
162 static const struct usb_phy_data am437x_usb2_data = {
163 .label = "am437x_usb2",
164 .flags = 0,
167 static const struct of_device_id omap_usb2_id_table[] = {
169 .compatible = "ti,omap-usb2",
170 .data = &omap_usb2_data,
173 .compatible = "ti,omap5-usb2",
174 .data = &omap5_usb2_data,
177 .compatible = "ti,dra7x-usb2",
178 .data = &dra7x_usb2_data,
181 .compatible = "ti,am437x-usb2",
182 .data = &am437x_usb2_data,
186 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
188 static int omap_usb2_probe(struct platform_device *pdev)
190 struct omap_usb *phy;
191 struct phy *generic_phy;
192 struct resource *res;
193 struct phy_provider *phy_provider;
194 struct usb_otg *otg;
195 struct device_node *node = pdev->dev.of_node;
196 struct device_node *control_node;
197 struct platform_device *control_pdev;
198 const struct of_device_id *of_id;
199 struct usb_phy_data *phy_data;
201 of_id = of_match_device(omap_usb2_id_table, &pdev->dev);
203 if (!of_id)
204 return -EINVAL;
206 phy_data = (struct usb_phy_data *)of_id->data;
208 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
209 if (!phy)
210 return -ENOMEM;
212 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
213 if (!otg)
214 return -ENOMEM;
216 phy->dev = &pdev->dev;
218 phy->phy.dev = phy->dev;
219 phy->phy.label = phy_data->label;
220 phy->phy.otg = otg;
221 phy->phy.type = USB_PHY_TYPE_USB2;
223 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
224 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
225 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
226 if (IS_ERR(phy->phy_base))
227 return PTR_ERR(phy->phy_base);
228 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
231 control_node = of_parse_phandle(node, "ctrl-module", 0);
232 if (!control_node) {
233 dev_err(&pdev->dev, "Failed to get control device phandle\n");
234 return -EINVAL;
237 control_pdev = of_find_device_by_node(control_node);
238 if (!control_pdev) {
239 dev_err(&pdev->dev, "Failed to get control device\n");
240 return -EINVAL;
243 phy->control_dev = &control_pdev->dev;
244 omap_control_phy_power(phy->control_dev, 0);
246 otg->set_host = omap_usb_set_host;
247 otg->set_peripheral = omap_usb_set_peripheral;
248 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
249 otg->set_vbus = omap_usb_set_vbus;
250 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
251 otg->start_srp = omap_usb_start_srp;
252 otg->usb_phy = &phy->phy;
254 platform_set_drvdata(pdev, phy);
255 pm_runtime_enable(phy->dev);
257 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
258 if (IS_ERR(generic_phy)) {
259 pm_runtime_disable(phy->dev);
260 return PTR_ERR(generic_phy);
263 phy_set_drvdata(generic_phy, phy);
265 phy_provider = devm_of_phy_provider_register(phy->dev,
266 of_phy_simple_xlate);
267 if (IS_ERR(phy_provider)) {
268 pm_runtime_disable(phy->dev);
269 return PTR_ERR(phy_provider);
272 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
273 if (IS_ERR(phy->wkupclk)) {
274 dev_warn(&pdev->dev, "unable to get wkupclk, trying old name\n");
275 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
276 if (IS_ERR(phy->wkupclk)) {
277 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
278 pm_runtime_disable(phy->dev);
279 return PTR_ERR(phy->wkupclk);
280 } else {
281 dev_warn(&pdev->dev,
282 "found usb_phy_cm_clk32k, please fix DTS\n");
285 clk_prepare(phy->wkupclk);
287 phy->optclk = devm_clk_get(phy->dev, "refclk");
288 if (IS_ERR(phy->optclk)) {
289 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
290 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
291 if (IS_ERR(phy->optclk)) {
292 dev_dbg(&pdev->dev,
293 "unable to get usb_otg_ss_refclk960m\n");
294 } else {
295 dev_warn(&pdev->dev,
296 "found usb_otg_ss_refclk960m, please fix DTS\n");
300 if (!IS_ERR(phy->optclk))
301 clk_prepare(phy->optclk);
303 usb_add_phy_dev(&phy->phy);
305 return 0;
308 static int omap_usb2_remove(struct platform_device *pdev)
310 struct omap_usb *phy = platform_get_drvdata(pdev);
312 clk_unprepare(phy->wkupclk);
313 if (!IS_ERR(phy->optclk))
314 clk_unprepare(phy->optclk);
315 usb_remove_phy(&phy->phy);
316 pm_runtime_disable(phy->dev);
318 return 0;
321 #ifdef CONFIG_PM
323 static int omap_usb2_runtime_suspend(struct device *dev)
325 struct platform_device *pdev = to_platform_device(dev);
326 struct omap_usb *phy = platform_get_drvdata(pdev);
328 clk_disable(phy->wkupclk);
329 if (!IS_ERR(phy->optclk))
330 clk_disable(phy->optclk);
332 return 0;
335 static int omap_usb2_runtime_resume(struct device *dev)
337 struct platform_device *pdev = to_platform_device(dev);
338 struct omap_usb *phy = platform_get_drvdata(pdev);
339 int ret;
341 ret = clk_enable(phy->wkupclk);
342 if (ret < 0) {
343 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
344 goto err0;
347 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;
355 return 0;
357 err1:
358 clk_disable(phy->wkupclk);
360 err0:
361 return ret;
364 static const struct dev_pm_ops omap_usb2_pm_ops = {
365 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
366 NULL)
369 #define DEV_PM_OPS (&omap_usb2_pm_ops)
370 #else
371 #define DEV_PM_OPS NULL
372 #endif
374 static struct platform_driver omap_usb2_driver = {
375 .probe = omap_usb2_probe,
376 .remove = omap_usb2_remove,
377 .driver = {
378 .name = "omap-usb2",
379 .pm = DEV_PM_OPS,
380 .of_match_table = omap_usb2_id_table,
384 module_platform_driver(omap_usb2_driver);
386 MODULE_ALIAS("platform:omap_usb2");
387 MODULE_AUTHOR("Texas Instruments Inc.");
388 MODULE_DESCRIPTION("OMAP USB2 phy driver");
389 MODULE_LICENSE("GPL v2");