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>
32 * omap_usb2_set_comparator - links the comparator present in the sytem with
34 * @comparator - the companion phy(comparator) for this phy
36 * The phy companion driver should call this API passing the phy_companion
37 * filled with set_vbus and start_srp to be used by usb phy.
39 * For use by phy companion driver
41 int omap_usb2_set_comparator(struct phy_companion *comparator)
44 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
49 phy = phy_to_omapusb(x);
50 phy->comparator = comparator;
53 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
56 * omap_usb_phy_power - power on/off the phy using control module reg
57 * @phy: struct omap_usb *
58 * @on: 0 or 1, based on powering on or off the PHY
60 * XXX: Remove this function once control module driver gets merged
62 static void omap_usb_phy_power(struct omap_usb *phy, int on)
67 val = readl(phy->control_dev);
69 writel(~PHY_PD, phy->control_dev);
70 /* XXX: add proper documentation for this delay */
74 writel(PHY_PD, phy->control_dev);
78 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
80 struct omap_usb *phy = phy_to_omapusb(otg->phy);
85 return phy->comparator->set_vbus(phy->comparator, enabled);
88 static int omap_usb_start_srp(struct usb_otg *otg)
90 struct omap_usb *phy = phy_to_omapusb(otg->phy);
95 return phy->comparator->start_srp(phy->comparator);
98 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
100 struct usb_phy *phy = otg->phy;
104 phy->state = OTG_STATE_UNDEFINED;
109 static int omap_usb_set_peripheral(struct usb_otg *otg,
110 struct usb_gadget *gadget)
112 struct usb_phy *phy = otg->phy;
114 otg->gadget = gadget;
116 phy->state = OTG_STATE_UNDEFINED;
121 static int omap_usb2_suspend(struct usb_phy *x, int suspend)
124 struct omap_usb *phy = phy_to_omapusb(x);
126 if (suspend && !phy->is_suspended) {
127 omap_usb_phy_power(phy, 0);
128 pm_runtime_put_sync(phy->dev);
129 phy->is_suspended = 1;
130 } else if (!suspend && phy->is_suspended) {
131 ret = pm_runtime_get_sync(phy->dev);
133 dev_err(phy->dev, "get_sync failed with err %d\n",
137 omap_usb_phy_power(phy, 1);
138 phy->is_suspended = 0;
144 static int __devinit omap_usb2_probe(struct platform_device *pdev)
146 struct omap_usb *phy;
148 struct resource *res;
150 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
152 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
156 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
158 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
162 phy->dev = &pdev->dev;
164 phy->phy.dev = phy->dev;
165 phy->phy.label = "omap-usb2";
166 phy->phy.set_suspend = omap_usb2_suspend;
169 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
171 phy->control_dev = devm_request_and_ioremap(&pdev->dev, res);
172 if (phy->control_dev == NULL) {
173 dev_err(&pdev->dev, "Failed to obtain io memory\n");
177 phy->is_suspended = 1;
178 omap_usb_phy_power(phy, 0);
180 otg->set_host = omap_usb_set_host;
181 otg->set_peripheral = omap_usb_set_peripheral;
182 otg->set_vbus = omap_usb_set_vbus;
183 otg->start_srp = omap_usb_start_srp;
184 otg->phy = &phy->phy;
186 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
187 if (IS_ERR(phy->wkupclk)) {
188 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
189 return PTR_ERR(phy->wkupclk);
191 clk_prepare(phy->wkupclk);
193 usb_add_phy(&phy->phy, USB_PHY_TYPE_USB2);
195 platform_set_drvdata(pdev, phy);
197 pm_runtime_enable(phy->dev);
202 static int __devexit omap_usb2_remove(struct platform_device *pdev)
204 struct omap_usb *phy = platform_get_drvdata(pdev);
206 clk_unprepare(phy->wkupclk);
207 usb_remove_phy(&phy->phy);
212 #ifdef CONFIG_PM_RUNTIME
214 static int omap_usb2_runtime_suspend(struct device *dev)
216 struct platform_device *pdev = to_platform_device(dev);
217 struct omap_usb *phy = platform_get_drvdata(pdev);
219 clk_disable(phy->wkupclk);
224 static int omap_usb2_runtime_resume(struct device *dev)
227 struct platform_device *pdev = to_platform_device(dev);
228 struct omap_usb *phy = platform_get_drvdata(pdev);
230 ret = clk_enable(phy->wkupclk);
232 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
237 static const struct dev_pm_ops omap_usb2_pm_ops = {
238 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
242 #define DEV_PM_OPS (&omap_usb2_pm_ops)
244 #define DEV_PM_OPS NULL
248 static const struct of_device_id omap_usb2_id_table[] = {
249 { .compatible = "ti,omap-usb2" },
252 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
255 static struct platform_driver omap_usb2_driver = {
256 .probe = omap_usb2_probe,
257 .remove = __devexit_p(omap_usb2_remove),
260 .owner = THIS_MODULE,
262 .of_match_table = of_match_ptr(omap_usb2_id_table),
266 module_platform_driver(omap_usb2_driver);
268 MODULE_ALIAS("platform: omap_usb2");
269 MODULE_AUTHOR("Texas Instruments Inc.");
270 MODULE_DESCRIPTION("OMAP USB2 phy driver");
271 MODULE_LICENSE("GPL v2");