2 * drivers/usb/otg/nop-usb-xceiv.c
4 * NOP USB transceiver for all USB transceiver which are either built-in
5 * into USB IP or which are mostly autonomous.
7 * Copyright (C) 2009 Texas Instruments Inc
8 * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * This provides a "nop" transceiver for PHYs which are
26 * autonomous such as isp1504, isp1707, etc.
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/usb/otg.h>
33 #include <linux/usb/nop-usb-xceiv.h>
34 #include <linux/slab.h>
35 #include <linux/clk.h>
36 #include <linux/regulator/consumer.h>
39 struct nop_usb_xceiv {
43 struct regulator *vcc;
44 struct regulator *reset;
47 static struct platform_device *pd;
49 void usb_nop_xceiv_register(void)
53 pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0);
55 printk(KERN_ERR "Unable to register usb nop transceiver\n");
59 EXPORT_SYMBOL(usb_nop_xceiv_register);
61 void usb_nop_xceiv_unregister(void)
63 platform_device_unregister(pd);
66 EXPORT_SYMBOL(usb_nop_xceiv_unregister);
68 static int nop_set_suspend(struct usb_phy *x, int suspend)
73 static int nop_init(struct usb_phy *phy)
75 struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
77 if (!IS_ERR(nop->vcc)) {
78 if (regulator_enable(nop->vcc))
79 dev_err(phy->dev, "Failed to enable power\n");
82 if (!IS_ERR(nop->clk))
85 if (!IS_ERR(nop->reset)) {
87 if (regulator_enable(nop->reset))
88 dev_err(phy->dev, "Failed to de-assert reset\n");
94 static void nop_shutdown(struct usb_phy *phy)
96 struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
98 if (!IS_ERR(nop->reset)) {
100 if (regulator_disable(nop->reset))
101 dev_err(phy->dev, "Failed to assert reset\n");
104 if (!IS_ERR(nop->clk))
105 clk_disable(nop->clk);
107 if (!IS_ERR(nop->vcc)) {
108 if (regulator_disable(nop->vcc))
109 dev_err(phy->dev, "Failed to disable power\n");
113 static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
123 otg->gadget = gadget;
124 otg->phy->state = OTG_STATE_B_IDLE;
128 static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
142 static int nop_usb_xceiv_probe(struct platform_device *pdev)
144 struct device *dev = &pdev->dev;
145 struct nop_usb_xceiv_platform_data *pdata = pdev->dev.platform_data;
146 struct nop_usb_xceiv *nop;
147 enum usb_phy_type type = USB_PHY_TYPE_USB2;
150 bool needs_vcc = false;
151 bool needs_reset = false;
153 nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL);
157 nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg),
163 struct device_node *node = dev->of_node;
165 if (of_property_read_u32(node, "clock-frequency", &clk_rate))
168 needs_vcc = of_property_read_bool(node, "vcc-supply");
169 needs_reset = of_property_read_bool(node, "reset-supply");
173 clk_rate = pdata->clk_rate;
174 needs_vcc = pdata->needs_vcc;
175 needs_reset = pdata->needs_reset;
178 nop->clk = devm_clk_get(&pdev->dev, "main_clk");
179 if (IS_ERR(nop->clk)) {
180 dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n",
184 if (!IS_ERR(nop->clk) && clk_rate) {
185 err = clk_set_rate(nop->clk, clk_rate);
187 dev_err(&pdev->dev, "Error setting clock rate\n");
192 if (!IS_ERR(nop->clk)) {
193 err = clk_prepare(nop->clk);
195 dev_err(&pdev->dev, "Error preparing clock\n");
200 nop->vcc = devm_regulator_get(&pdev->dev, "vcc");
201 if (IS_ERR(nop->vcc)) {
202 dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n",
205 return -EPROBE_DEFER;
208 nop->reset = devm_regulator_get(&pdev->dev, "reset");
209 if (IS_ERR(nop->reset)) {
210 dev_dbg(&pdev->dev, "Error getting reset regulator: %ld\n",
211 PTR_ERR(nop->reset));
213 return -EPROBE_DEFER;
216 nop->dev = &pdev->dev;
217 nop->phy.dev = nop->dev;
218 nop->phy.label = "nop-xceiv";
219 nop->phy.set_suspend = nop_set_suspend;
220 nop->phy.init = nop_init;
221 nop->phy.shutdown = nop_shutdown;
222 nop->phy.state = OTG_STATE_UNDEFINED;
223 nop->phy.type = type;
225 nop->phy.otg->phy = &nop->phy;
226 nop->phy.otg->set_host = nop_set_host;
227 nop->phy.otg->set_peripheral = nop_set_peripheral;
229 err = usb_add_phy_dev(&nop->phy);
231 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
236 platform_set_drvdata(pdev, nop);
238 ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
243 if (!IS_ERR(nop->clk))
244 clk_unprepare(nop->clk);
248 static int nop_usb_xceiv_remove(struct platform_device *pdev)
250 struct nop_usb_xceiv *nop = platform_get_drvdata(pdev);
252 if (!IS_ERR(nop->clk))
253 clk_unprepare(nop->clk);
255 usb_remove_phy(&nop->phy);
260 static const struct of_device_id nop_xceiv_dt_ids[] = {
261 { .compatible = "usb-nop-xceiv" },
265 MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
267 static struct platform_driver nop_usb_xceiv_driver = {
268 .probe = nop_usb_xceiv_probe,
269 .remove = nop_usb_xceiv_remove,
271 .name = "nop_usb_xceiv",
272 .owner = THIS_MODULE,
273 .of_match_table = nop_xceiv_dt_ids,
277 static int __init nop_usb_xceiv_init(void)
279 return platform_driver_register(&nop_usb_xceiv_driver);
281 subsys_initcall(nop_usb_xceiv_init);
283 static void __exit nop_usb_xceiv_exit(void)
285 platform_driver_unregister(&nop_usb_xceiv_driver);
287 module_exit(nop_usb_xceiv_exit);
289 MODULE_ALIAS("platform:nop_usb_xceiv");
290 MODULE_AUTHOR("Texas Instruments Inc");
291 MODULE_DESCRIPTION("NOP USB Transceiver driver");
292 MODULE_LICENSE("GPL");