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>
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>
30 #include <linux/usb/omap_control_usb.h>
31 #include <linux/phy/phy.h>
34 * omap_usb2_set_comparator - links the comparator present in the sytem with
36 * @comparator - the companion phy(comparator) for this phy
38 * The phy companion driver should call this API passing the phy_companion
39 * filled with set_vbus and start_srp to be used by usb phy.
41 * For use by phy companion driver
43 int omap_usb2_set_comparator(struct phy_companion *comparator)
46 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
51 phy = phy_to_omapusb(x);
52 phy->comparator = comparator;
55 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
57 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
59 struct omap_usb *phy = phy_to_omapusb(otg->phy);
64 return phy->comparator->set_vbus(phy->comparator, enabled);
67 static int omap_usb_start_srp(struct usb_otg *otg)
69 struct omap_usb *phy = phy_to_omapusb(otg->phy);
74 return phy->comparator->start_srp(phy->comparator);
77 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
79 struct usb_phy *phy = otg->phy;
83 phy->state = OTG_STATE_UNDEFINED;
88 static int omap_usb_set_peripheral(struct usb_otg *otg,
89 struct usb_gadget *gadget)
91 struct usb_phy *phy = otg->phy;
95 phy->state = OTG_STATE_UNDEFINED;
100 static int omap_usb2_suspend(struct usb_phy *x, int suspend)
102 struct omap_usb *phy = phy_to_omapusb(x);
105 if (suspend && !phy->is_suspended) {
106 omap_control_usb_phy_power(phy->control_dev, 0);
107 pm_runtime_put_sync(phy->dev);
108 phy->is_suspended = 1;
109 } else if (!suspend && phy->is_suspended) {
110 ret = pm_runtime_get_sync(phy->dev);
112 dev_err(phy->dev, "get_sync failed with err %d\n", ret);
115 omap_control_usb_phy_power(phy->control_dev, 1);
116 phy->is_suspended = 0;
122 static int omap_usb_power_off(struct phy *x)
124 struct omap_usb *phy = phy_get_drvdata(x);
126 omap_control_usb_phy_power(phy->control_dev, 0);
131 static int omap_usb_power_on(struct phy *x)
133 struct omap_usb *phy = phy_get_drvdata(x);
135 omap_control_usb_phy_power(phy->control_dev, 1);
140 static struct phy_ops ops = {
141 .power_on = omap_usb_power_on,
142 .power_off = omap_usb_power_off,
143 .owner = THIS_MODULE,
146 static int omap_usb2_probe(struct platform_device *pdev)
148 struct omap_usb *phy;
149 struct phy *generic_phy;
151 struct phy_provider *phy_provider;
153 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
155 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
159 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
161 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
165 phy->dev = &pdev->dev;
167 phy->phy.dev = phy->dev;
168 phy->phy.label = "omap-usb2";
169 phy->phy.set_suspend = omap_usb2_suspend;
171 phy->phy.type = USB_PHY_TYPE_USB2;
173 phy_provider = devm_of_phy_provider_register(phy->dev,
174 of_phy_simple_xlate);
175 if (IS_ERR(phy_provider))
176 return PTR_ERR(phy_provider);
178 phy->control_dev = omap_get_control_dev();
179 if (IS_ERR(phy->control_dev)) {
180 dev_dbg(&pdev->dev, "Failed to get control device\n");
184 phy->is_suspended = 1;
185 omap_control_usb_phy_power(phy->control_dev, 0);
187 otg->set_host = omap_usb_set_host;
188 otg->set_peripheral = omap_usb_set_peripheral;
189 otg->set_vbus = omap_usb_set_vbus;
190 otg->start_srp = omap_usb_start_srp;
191 otg->phy = &phy->phy;
193 platform_set_drvdata(pdev, phy);
194 pm_runtime_enable(phy->dev);
196 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
197 if (IS_ERR(generic_phy))
198 return PTR_ERR(generic_phy);
200 phy_set_drvdata(generic_phy, phy);
202 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
203 if (IS_ERR(phy->wkupclk)) {
204 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
205 return PTR_ERR(phy->wkupclk);
207 clk_prepare(phy->wkupclk);
209 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
210 if (IS_ERR(phy->optclk))
211 dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
213 clk_prepare(phy->optclk);
215 usb_add_phy_dev(&phy->phy);
220 static int omap_usb2_remove(struct platform_device *pdev)
222 struct omap_usb *phy = platform_get_drvdata(pdev);
224 clk_unprepare(phy->wkupclk);
225 if (!IS_ERR(phy->optclk))
226 clk_unprepare(phy->optclk);
227 usb_remove_phy(&phy->phy);
232 #ifdef CONFIG_PM_RUNTIME
234 static int omap_usb2_runtime_suspend(struct device *dev)
236 struct platform_device *pdev = to_platform_device(dev);
237 struct omap_usb *phy = platform_get_drvdata(pdev);
239 clk_disable(phy->wkupclk);
240 if (!IS_ERR(phy->optclk))
241 clk_disable(phy->optclk);
246 static int omap_usb2_runtime_resume(struct device *dev)
248 struct platform_device *pdev = to_platform_device(dev);
249 struct omap_usb *phy = platform_get_drvdata(pdev);
252 ret = clk_enable(phy->wkupclk);
254 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
258 if (!IS_ERR(phy->optclk)) {
259 ret = clk_enable(phy->optclk);
261 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
269 clk_disable(phy->wkupclk);
275 static const struct dev_pm_ops omap_usb2_pm_ops = {
276 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
280 #define DEV_PM_OPS (&omap_usb2_pm_ops)
282 #define DEV_PM_OPS NULL
286 static const struct of_device_id omap_usb2_id_table[] = {
287 { .compatible = "ti,omap-usb2" },
290 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
293 static struct platform_driver omap_usb2_driver = {
294 .probe = omap_usb2_probe,
295 .remove = omap_usb2_remove,
298 .owner = THIS_MODULE,
300 .of_match_table = of_match_ptr(omap_usb2_id_table),
304 module_platform_driver(omap_usb2_driver);
306 MODULE_ALIAS("platform: omap_usb2");
307 MODULE_AUTHOR("Texas Instruments Inc.");
308 MODULE_DESCRIPTION("OMAP USB2 phy driver");
309 MODULE_LICENSE("GPL v2");