Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / phy / phy-omap-usb2.c
blobbfc5c337f99a8178278d9aa8f0b9b7be74f41889
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/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>
32 #include <linux/of_platform.h>
34 /**
35 * omap_usb2_set_comparator - links the comparator present in the sytem with
36 * this phy
37 * @comparator - the companion phy(comparator) for this phy
39 * The phy companion driver should call this API passing the phy_companion
40 * filled with set_vbus and start_srp to be used by usb phy.
42 * For use by phy companion driver
44 int omap_usb2_set_comparator(struct phy_companion *comparator)
46 struct omap_usb *phy;
47 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
49 if (IS_ERR(x))
50 return -ENODEV;
52 phy = phy_to_omapusb(x);
53 phy->comparator = comparator;
54 return 0;
56 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
58 static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
60 struct omap_usb *phy = phy_to_omapusb(otg->phy);
62 if (!phy->comparator)
63 return -ENODEV;
65 return phy->comparator->set_vbus(phy->comparator, enabled);
68 static int omap_usb_start_srp(struct usb_otg *otg)
70 struct omap_usb *phy = phy_to_omapusb(otg->phy);
72 if (!phy->comparator)
73 return -ENODEV;
75 return phy->comparator->start_srp(phy->comparator);
78 static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
80 struct usb_phy *phy = otg->phy;
82 otg->host = host;
83 if (!host)
84 phy->state = OTG_STATE_UNDEFINED;
86 return 0;
89 static int omap_usb_set_peripheral(struct usb_otg *otg,
90 struct usb_gadget *gadget)
92 struct usb_phy *phy = otg->phy;
94 otg->gadget = gadget;
95 if (!gadget)
96 phy->state = OTG_STATE_UNDEFINED;
98 return 0;
101 static int omap_usb2_suspend(struct usb_phy *x, int suspend)
103 struct omap_usb *phy = phy_to_omapusb(x);
104 int ret;
106 if (suspend && !phy->is_suspended) {
107 omap_control_usb_phy_power(phy->control_dev, 0);
108 pm_runtime_put_sync(phy->dev);
109 phy->is_suspended = 1;
110 } else if (!suspend && phy->is_suspended) {
111 ret = pm_runtime_get_sync(phy->dev);
112 if (ret < 0) {
113 dev_err(phy->dev, "get_sync failed with err %d\n", ret);
114 return ret;
116 omap_control_usb_phy_power(phy->control_dev, 1);
117 phy->is_suspended = 0;
120 return 0;
123 static int omap_usb_power_off(struct phy *x)
125 struct omap_usb *phy = phy_get_drvdata(x);
127 omap_control_usb_phy_power(phy->control_dev, 0);
129 return 0;
132 static int omap_usb_power_on(struct phy *x)
134 struct omap_usb *phy = phy_get_drvdata(x);
136 omap_control_usb_phy_power(phy->control_dev, 1);
138 return 0;
141 static struct phy_ops ops = {
142 .power_on = omap_usb_power_on,
143 .power_off = omap_usb_power_off,
144 .owner = THIS_MODULE,
147 static int omap_usb2_probe(struct platform_device *pdev)
149 struct omap_usb *phy;
150 struct phy *generic_phy;
151 struct phy_provider *phy_provider;
152 struct usb_otg *otg;
153 struct device_node *node = pdev->dev.of_node;
154 struct device_node *control_node;
155 struct platform_device *control_pdev;
157 if (!node)
158 return -EINVAL;
160 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
161 if (!phy) {
162 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
163 return -ENOMEM;
166 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
167 if (!otg) {
168 dev_err(&pdev->dev, "unable to allocate memory for USB OTG\n");
169 return -ENOMEM;
172 phy->dev = &pdev->dev;
174 phy->phy.dev = phy->dev;
175 phy->phy.label = "omap-usb2";
176 phy->phy.set_suspend = omap_usb2_suspend;
177 phy->phy.otg = otg;
178 phy->phy.type = USB_PHY_TYPE_USB2;
180 phy_provider = devm_of_phy_provider_register(phy->dev,
181 of_phy_simple_xlate);
182 if (IS_ERR(phy_provider))
183 return PTR_ERR(phy_provider);
185 control_node = of_parse_phandle(node, "ctrl-module", 0);
186 if (!control_node) {
187 dev_err(&pdev->dev, "Failed to get control device phandle\n");
188 return -EINVAL;
191 control_pdev = of_find_device_by_node(control_node);
192 if (!control_pdev) {
193 dev_err(&pdev->dev, "Failed to get control device\n");
194 return -EINVAL;
197 phy->control_dev = &control_pdev->dev;
199 phy->is_suspended = 1;
200 omap_control_usb_phy_power(phy->control_dev, 0);
202 otg->set_host = omap_usb_set_host;
203 otg->set_peripheral = omap_usb_set_peripheral;
204 otg->set_vbus = omap_usb_set_vbus;
205 otg->start_srp = omap_usb_start_srp;
206 otg->phy = &phy->phy;
208 platform_set_drvdata(pdev, phy);
209 pm_runtime_enable(phy->dev);
211 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
212 if (IS_ERR(generic_phy))
213 return PTR_ERR(generic_phy);
215 phy_set_drvdata(generic_phy, phy);
217 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
218 if (IS_ERR(phy->wkupclk)) {
219 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
220 return PTR_ERR(phy->wkupclk);
222 clk_prepare(phy->wkupclk);
224 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
225 if (IS_ERR(phy->optclk))
226 dev_vdbg(&pdev->dev, "unable to get refclk960m\n");
227 else
228 clk_prepare(phy->optclk);
230 usb_add_phy_dev(&phy->phy);
232 return 0;
235 static int omap_usb2_remove(struct platform_device *pdev)
237 struct omap_usb *phy = platform_get_drvdata(pdev);
239 clk_unprepare(phy->wkupclk);
240 if (!IS_ERR(phy->optclk))
241 clk_unprepare(phy->optclk);
242 usb_remove_phy(&phy->phy);
244 return 0;
247 #ifdef CONFIG_PM_RUNTIME
249 static int omap_usb2_runtime_suspend(struct device *dev)
251 struct platform_device *pdev = to_platform_device(dev);
252 struct omap_usb *phy = platform_get_drvdata(pdev);
254 clk_disable(phy->wkupclk);
255 if (!IS_ERR(phy->optclk))
256 clk_disable(phy->optclk);
258 return 0;
261 static int omap_usb2_runtime_resume(struct device *dev)
263 struct platform_device *pdev = to_platform_device(dev);
264 struct omap_usb *phy = platform_get_drvdata(pdev);
265 int ret;
267 ret = clk_enable(phy->wkupclk);
268 if (ret < 0) {
269 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
270 goto err0;
273 if (!IS_ERR(phy->optclk)) {
274 ret = clk_enable(phy->optclk);
275 if (ret < 0) {
276 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
277 goto err1;
281 return 0;
283 err1:
284 clk_disable(phy->wkupclk);
286 err0:
287 return ret;
290 static const struct dev_pm_ops omap_usb2_pm_ops = {
291 SET_RUNTIME_PM_OPS(omap_usb2_runtime_suspend, omap_usb2_runtime_resume,
292 NULL)
295 #define DEV_PM_OPS (&omap_usb2_pm_ops)
296 #else
297 #define DEV_PM_OPS NULL
298 #endif
300 #ifdef CONFIG_OF
301 static const struct of_device_id omap_usb2_id_table[] = {
302 { .compatible = "ti,omap-usb2" },
305 MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
306 #endif
308 static struct platform_driver omap_usb2_driver = {
309 .probe = omap_usb2_probe,
310 .remove = omap_usb2_remove,
311 .driver = {
312 .name = "omap-usb2",
313 .owner = THIS_MODULE,
314 .pm = DEV_PM_OPS,
315 .of_match_table = of_match_ptr(omap_usb2_id_table),
319 module_platform_driver(omap_usb2_driver);
321 MODULE_ALIAS("platform: omap_usb2");
322 MODULE_AUTHOR("Texas Instruments Inc.");
323 MODULE_DESCRIPTION("OMAP USB2 phy driver");
324 MODULE_LICENSE("GPL v2");