fbdcfef7169b498b9832b0b7874eb481f5b2d837
[profile/ivi/kernel-x86-ivi.git] / drivers / usb / otg / nop-usb-xceiv.c
1 /*
2  * drivers/usb/otg/nop-usb-xceiv.c
3  *
4  * NOP USB transceiver for all USB transceiver which are either built-in
5  * into USB IP or which are mostly autonomous.
6  *
7  * Copyright (C) 2009 Texas Instruments Inc
8  * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
9  *
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.
14  *
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.
19  *
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.
23  *
24  * Current status:
25  *      This provides a "nop" transceiver for PHYs which are
26  *      autonomous such as isp1504, isp1707, etc.
27  */
28
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>
37
38 struct nop_usb_xceiv {
39         struct usb_phy          phy;
40         struct device           *dev;
41         struct clk              *clk;
42         struct regulator        *vcc;
43 };
44
45 static struct platform_device *pd;
46
47 void usb_nop_xceiv_register(void)
48 {
49         if (pd)
50                 return;
51         pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0);
52         if (!pd) {
53                 printk(KERN_ERR "Unable to register usb nop transceiver\n");
54                 return;
55         }
56 }
57 EXPORT_SYMBOL(usb_nop_xceiv_register);
58
59 void usb_nop_xceiv_unregister(void)
60 {
61         platform_device_unregister(pd);
62         pd = NULL;
63 }
64 EXPORT_SYMBOL(usb_nop_xceiv_unregister);
65
66 static int nop_set_suspend(struct usb_phy *x, int suspend)
67 {
68         return 0;
69 }
70
71 static int nop_init(struct usb_phy *phy)
72 {
73         struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
74
75         if (!IS_ERR(nop->vcc)) {
76                 if (regulator_enable(nop->vcc))
77                         dev_err(phy->dev, "Failed to enable power\n");
78         }
79
80         if (!IS_ERR(nop->clk))
81                 clk_enable(nop->clk);
82
83         return 0;
84 }
85
86 static void nop_shutdown(struct usb_phy *phy)
87 {
88         struct nop_usb_xceiv *nop = dev_get_drvdata(phy->dev);
89
90         if (!IS_ERR(nop->clk))
91                 clk_disable(nop->clk);
92
93         if (!IS_ERR(nop->vcc)) {
94                 if (regulator_disable(nop->vcc))
95                         dev_err(phy->dev, "Failed to disable power\n");
96         }
97 }
98
99 static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
100 {
101         if (!otg)
102                 return -ENODEV;
103
104         if (!gadget) {
105                 otg->gadget = NULL;
106                 return -ENODEV;
107         }
108
109         otg->gadget = gadget;
110         otg->phy->state = OTG_STATE_B_IDLE;
111         return 0;
112 }
113
114 static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
115 {
116         if (!otg)
117                 return -ENODEV;
118
119         if (!host) {
120                 otg->host = NULL;
121                 return -ENODEV;
122         }
123
124         otg->host = host;
125         return 0;
126 }
127
128 static int nop_usb_xceiv_probe(struct platform_device *pdev)
129 {
130         struct nop_usb_xceiv_platform_data *pdata = pdev->dev.platform_data;
131         struct nop_usb_xceiv    *nop;
132         enum usb_phy_type       type = USB_PHY_TYPE_USB2;
133         int err;
134
135         nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL);
136         if (!nop)
137                 return -ENOMEM;
138
139         nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg),
140                                                         GFP_KERNEL);
141         if (!nop->phy.otg)
142                 return -ENOMEM;
143
144         if (pdata)
145                 type = pdata->type;
146
147         nop->clk = devm_clk_get(&pdev->dev, "main_clk");
148         if (IS_ERR(nop->clk)) {
149                 dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n",
150                                         PTR_ERR(nop->clk));
151         }
152
153         if (!IS_ERR(nop->clk) && pdata && pdata->clk_rate) {
154                 err = clk_set_rate(nop->clk, pdata->clk_rate);
155                 if (err) {
156                         dev_err(&pdev->dev, "Error setting clock rate\n");
157                         return err;
158                 }
159         }
160
161         if (!IS_ERR(nop->clk)) {
162                 err = clk_prepare(nop->clk);
163                 if (err) {
164                         dev_err(&pdev->dev, "Error preparing clock\n");
165                         return err;
166                 }
167         }
168
169         nop->vcc = devm_regulator_get(&pdev->dev, "vcc");
170         if (IS_ERR(nop->vcc)) {
171                 dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n",
172                                         PTR_ERR(nop->vcc));
173         }
174
175         nop->dev                = &pdev->dev;
176         nop->phy.dev            = nop->dev;
177         nop->phy.label          = "nop-xceiv";
178         nop->phy.set_suspend    = nop_set_suspend;
179         nop->phy.init           = nop_init;
180         nop->phy.shutdown       = nop_shutdown;
181         nop->phy.state          = OTG_STATE_UNDEFINED;
182
183         nop->phy.otg->phy               = &nop->phy;
184         nop->phy.otg->set_host          = nop_set_host;
185         nop->phy.otg->set_peripheral    = nop_set_peripheral;
186
187         err = usb_add_phy(&nop->phy, type);
188         if (err) {
189                 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
190                         err);
191                 goto err_add;
192         }
193
194         platform_set_drvdata(pdev, nop);
195
196         ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
197
198         return 0;
199
200 err_add:
201         if (!IS_ERR(nop->clk))
202                 clk_unprepare(nop->clk);
203         return err;
204 }
205
206 static int nop_usb_xceiv_remove(struct platform_device *pdev)
207 {
208         struct nop_usb_xceiv *nop = platform_get_drvdata(pdev);
209
210         if (!IS_ERR(nop->clk))
211                 clk_unprepare(nop->clk);
212
213         usb_remove_phy(&nop->phy);
214
215         platform_set_drvdata(pdev, NULL);
216
217         return 0;
218 }
219
220 static struct platform_driver nop_usb_xceiv_driver = {
221         .probe          = nop_usb_xceiv_probe,
222         .remove         = nop_usb_xceiv_remove,
223         .driver         = {
224                 .name   = "nop_usb_xceiv",
225                 .owner  = THIS_MODULE,
226         },
227 };
228
229 static int __init nop_usb_xceiv_init(void)
230 {
231         return platform_driver_register(&nop_usb_xceiv_driver);
232 }
233 subsys_initcall(nop_usb_xceiv_init);
234
235 static void __exit nop_usb_xceiv_exit(void)
236 {
237         platform_driver_unregister(&nop_usb_xceiv_driver);
238 }
239 module_exit(nop_usb_xceiv_exit);
240
241 MODULE_ALIAS("platform:nop_usb_xceiv");
242 MODULE_AUTHOR("Texas Instruments Inc");
243 MODULE_DESCRIPTION("NOP USB Transceiver driver");
244 MODULE_LICENSE("GPL");