usb: gadget: only GPL drivers in the gadget and phy framework
[deliverable/linux.git] / drivers / usb / phy / phy-generic.c
CommitLineData
f6d92a05
AKG
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:
cc835e32
DB
25 * This provides a "nop" transceiver for PHYs which are
26 * autonomous such as isp1504, isp1707, etc.
f6d92a05
AKG
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>
3fa4d734 33#include <linux/usb/usb_phy_gen_xceiv.h>
5a0e3ad6 34#include <linux/slab.h>
2319fb88 35#include <linux/clk.h>
58f735fe 36#include <linux/regulator/consumer.h>
0eba3879 37#include <linux/of.h>
bd27fa44
RQ
38#include <linux/of_gpio.h>
39#include <linux/gpio.h>
40#include <linux/delay.h>
f6d92a05 41
53b6fc28 42#include "phy-generic.h"
f6d92a05 43
cc835e32 44static struct platform_device *pd;
f6d92a05
AKG
45
46void usb_nop_xceiv_register(void)
47{
cc835e32
DB
48 if (pd)
49 return;
3fa4d734 50 pd = platform_device_register_simple("usb_phy_gen_xceiv", -1, NULL, 0);
e8d68f88 51 if (IS_ERR(pd)) {
3fa4d734 52 pr_err("Unable to register generic usb transceiver\n");
e8d68f88 53 pd = NULL;
f6d92a05
AKG
54 return;
55 }
56}
0700faaf 57EXPORT_SYMBOL_GPL(usb_nop_xceiv_register);
f6d92a05
AKG
58
59void usb_nop_xceiv_unregister(void)
60{
cc835e32 61 platform_device_unregister(pd);
dc7520c1 62 pd = NULL;
f6d92a05 63}
0700faaf 64EXPORT_SYMBOL_GPL(usb_nop_xceiv_unregister);
f6d92a05 65
86753811 66static int nop_set_suspend(struct usb_phy *x, int suspend)
f6d92a05
AKG
67{
68 return 0;
69}
70
bd27fa44
RQ
71static void nop_reset_set(struct usb_phy_gen_xceiv *nop, int asserted)
72{
73 int value;
74
75 if (!gpio_is_valid(nop->gpio_reset))
76 return;
77
78 value = asserted;
79 if (nop->reset_active_low)
80 value = !value;
81
82 gpio_set_value_cansleep(nop->gpio_reset, value);
83
84 if (!asserted)
85 usleep_range(10000, 20000);
86}
87
53b6fc28 88int usb_gen_phy_init(struct usb_phy *phy)
2319fb88 89{
3fa4d734 90 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
2319fb88 91
58f735fe
RQ
92 if (!IS_ERR(nop->vcc)) {
93 if (regulator_enable(nop->vcc))
94 dev_err(phy->dev, "Failed to enable power\n");
95 }
96
2319fb88 97 if (!IS_ERR(nop->clk))
4d175f34 98 clk_prepare_enable(nop->clk);
2319fb88 99
bd27fa44
RQ
100 /* De-assert RESET */
101 nop_reset_set(nop, 0);
ad63ebfc 102
2319fb88
RQ
103 return 0;
104}
53b6fc28 105EXPORT_SYMBOL_GPL(usb_gen_phy_init);
2319fb88 106
53b6fc28 107void usb_gen_phy_shutdown(struct usb_phy *phy)
2319fb88 108{
3fa4d734 109 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
2319fb88 110
bd27fa44
RQ
111 /* Assert RESET */
112 nop_reset_set(nop, 1);
ad63ebfc 113
2319fb88 114 if (!IS_ERR(nop->clk))
4d175f34 115 clk_disable_unprepare(nop->clk);
58f735fe
RQ
116
117 if (!IS_ERR(nop->vcc)) {
118 if (regulator_disable(nop->vcc))
119 dev_err(phy->dev, "Failed to disable power\n");
120 }
2319fb88 121}
53b6fc28 122EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
2319fb88 123
41adf109 124static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
f6d92a05 125{
41adf109 126 if (!otg)
f6d92a05
AKG
127 return -ENODEV;
128
f6d92a05 129 if (!gadget) {
41adf109 130 otg->gadget = NULL;
f6d92a05
AKG
131 return -ENODEV;
132 }
133
41adf109
HK
134 otg->gadget = gadget;
135 otg->phy->state = OTG_STATE_B_IDLE;
f6d92a05
AKG
136 return 0;
137}
138
41adf109 139static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
f6d92a05 140{
41adf109 141 if (!otg)
f6d92a05
AKG
142 return -ENODEV;
143
f6d92a05 144 if (!host) {
41adf109 145 otg->host = NULL;
f6d92a05
AKG
146 return -ENODEV;
147 }
148
41adf109 149 otg->host = host;
f6d92a05
AKG
150 return 0;
151}
152
53b6fc28 153int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_gen_xceiv *nop,
af9f51c5 154 struct usb_phy_gen_xceiv_platform_data *pdata)
f6d92a05 155{
af9f51c5 156 enum usb_phy_type type = USB_PHY_TYPE_USB2;
f6d92a05
AKG
157 int err;
158
af9f51c5
FB
159 u32 clk_rate = 0;
160 bool needs_vcc = false;
161
162 nop->reset_active_low = true; /* default behaviour */
163
164 if (dev->of_node) {
165 struct device_node *node = dev->of_node;
37cfbc42 166 enum of_gpio_flags flags = 0;
af9f51c5
FB
167
168 if (of_property_read_u32(node, "clock-frequency", &clk_rate))
169 clk_rate = 0;
170
171 needs_vcc = of_property_read_bool(node, "vcc-supply");
172 nop->gpio_reset = of_get_named_gpio_flags(node, "reset-gpios",
173 0, &flags);
174 if (nop->gpio_reset == -EPROBE_DEFER)
175 return -EPROBE_DEFER;
176
177 nop->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
178
179 } else if (pdata) {
180 type = pdata->type;
181 clk_rate = pdata->clk_rate;
182 needs_vcc = pdata->needs_vcc;
183 nop->gpio_reset = pdata->gpio_reset;
dc52c574
AK
184 } else {
185 nop->gpio_reset = -1;
af9f51c5
FB
186 }
187
53b6fc28
SAS
188 nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
189 GFP_KERNEL);
e4d7dc66 190 if (!nop->phy.otg)
41adf109 191 return -ENOMEM;
41adf109 192
53b6fc28 193 nop->clk = devm_clk_get(dev, "main_clk");
2319fb88 194 if (IS_ERR(nop->clk)) {
53b6fc28 195 dev_dbg(dev, "Can't get phy clock: %ld\n",
2319fb88
RQ
196 PTR_ERR(nop->clk));
197 }
198
0eba3879
RQ
199 if (!IS_ERR(nop->clk) && clk_rate) {
200 err = clk_set_rate(nop->clk, clk_rate);
2319fb88 201 if (err) {
53b6fc28 202 dev_err(dev, "Error setting clock rate\n");
2319fb88
RQ
203 return err;
204 }
205 }
206
53b6fc28 207 nop->vcc = devm_regulator_get(dev, "vcc");
58f735fe 208 if (IS_ERR(nop->vcc)) {
53b6fc28 209 dev_dbg(dev, "Error getting vcc regulator: %ld\n",
58f735fe 210 PTR_ERR(nop->vcc));
b54b5f56
RQ
211 if (needs_vcc)
212 return -EPROBE_DEFER;
58f735fe
RQ
213 }
214
bd27fa44
RQ
215 if (gpio_is_valid(nop->gpio_reset)) {
216 unsigned long gpio_flags;
217
218 /* Assert RESET */
219 if (nop->reset_active_low)
220 gpio_flags = GPIOF_OUT_INIT_LOW;
221 else
222 gpio_flags = GPIOF_OUT_INIT_HIGH;
223
224 err = devm_gpio_request_one(dev, nop->gpio_reset,
225 gpio_flags, dev_name(dev));
226 if (err) {
227 dev_err(dev, "Error requesting RESET GPIO %d\n",
228 nop->gpio_reset);
229 return err;
230 }
ad63ebfc
RQ
231 }
232
53b6fc28 233 nop->dev = dev;
41adf109
HK
234 nop->phy.dev = nop->dev;
235 nop->phy.label = "nop-xceiv";
236 nop->phy.set_suspend = nop_set_suspend;
237 nop->phy.state = OTG_STATE_UNDEFINED;
90f4232f 238 nop->phy.type = type;
41adf109
HK
239
240 nop->phy.otg->phy = &nop->phy;
241 nop->phy.otg->set_host = nop_set_host;
242 nop->phy.otg->set_peripheral = nop_set_peripheral;
243
53b6fc28
SAS
244 return 0;
245}
246EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
247
53b6fc28
SAS
248static int usb_phy_gen_xceiv_probe(struct platform_device *pdev)
249{
250 struct device *dev = &pdev->dev;
53b6fc28 251 struct usb_phy_gen_xceiv *nop;
53b6fc28 252 int err;
bd27fa44
RQ
253
254 nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
255 if (!nop)
256 return -ENOMEM;
257
af9f51c5 258 err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
53b6fc28
SAS
259 if (err)
260 return err;
261
262 nop->phy.init = usb_gen_phy_init;
263 nop->phy.shutdown = usb_gen_phy_shutdown;
264
90f4232f 265 err = usb_add_phy_dev(&nop->phy);
f6d92a05
AKG
266 if (err) {
267 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
268 err);
4d175f34 269 return err;
f6d92a05
AKG
270 }
271
272 platform_set_drvdata(pdev, nop);
273
274 return 0;
f6d92a05
AKG
275}
276
3fa4d734 277static int usb_phy_gen_xceiv_remove(struct platform_device *pdev)
f6d92a05 278{
3fa4d734 279 struct usb_phy_gen_xceiv *nop = platform_get_drvdata(pdev);
f6d92a05 280
662dca54 281 usb_remove_phy(&nop->phy);
f6d92a05 282
f6d92a05
AKG
283 return 0;
284}
285
0eba3879
RQ
286static const struct of_device_id nop_xceiv_dt_ids[] = {
287 { .compatible = "usb-nop-xceiv" },
288 { }
289};
290
291MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
292
3fa4d734
SAS
293static struct platform_driver usb_phy_gen_xceiv_driver = {
294 .probe = usb_phy_gen_xceiv_probe,
295 .remove = usb_phy_gen_xceiv_remove,
f6d92a05 296 .driver = {
3fa4d734 297 .name = "usb_phy_gen_xceiv",
f6d92a05 298 .owner = THIS_MODULE,
5462b0db 299 .of_match_table = nop_xceiv_dt_ids,
f6d92a05
AKG
300 },
301};
302
3fa4d734 303static int __init usb_phy_gen_xceiv_init(void)
f6d92a05 304{
3fa4d734 305 return platform_driver_register(&usb_phy_gen_xceiv_driver);
f6d92a05 306}
3fa4d734 307subsys_initcall(usb_phy_gen_xceiv_init);
f6d92a05 308
3fa4d734 309static void __exit usb_phy_gen_xceiv_exit(void)
f6d92a05 310{
3fa4d734 311 platform_driver_unregister(&usb_phy_gen_xceiv_driver);
f6d92a05 312}
3fa4d734 313module_exit(usb_phy_gen_xceiv_exit);
f6d92a05 314
3fa4d734 315MODULE_ALIAS("platform:usb_phy_gen_xceiv");
f6d92a05
AKG
316MODULE_AUTHOR("Texas Instruments Inc");
317MODULE_DESCRIPTION("NOP USB Transceiver driver");
318MODULE_LICENSE("GPL");
This page took 0.380548 seconds and 5 git commands to generate.