]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/phy/phy-generic.c
usb: phy-generic: fix nop xceiv probe
[mirror_ubuntu-artful-kernel.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);
cc835e32 51 if (!pd) {
3fa4d734 52 pr_err("Unable to register generic usb transceiver\n");
f6d92a05
AKG
53 return;
54 }
55}
cc835e32 56EXPORT_SYMBOL(usb_nop_xceiv_register);
f6d92a05
AKG
57
58void usb_nop_xceiv_unregister(void)
59{
cc835e32 60 platform_device_unregister(pd);
dc7520c1 61 pd = NULL;
f6d92a05 62}
cc835e32 63EXPORT_SYMBOL(usb_nop_xceiv_unregister);
f6d92a05 64
86753811 65static int nop_set_suspend(struct usb_phy *x, int suspend)
f6d92a05
AKG
66{
67 return 0;
68}
69
bd27fa44
RQ
70static void nop_reset_set(struct usb_phy_gen_xceiv *nop, int asserted)
71{
72 int value;
73
74 if (!gpio_is_valid(nop->gpio_reset))
75 return;
76
77 value = asserted;
78 if (nop->reset_active_low)
79 value = !value;
80
81 gpio_set_value_cansleep(nop->gpio_reset, value);
82
83 if (!asserted)
84 usleep_range(10000, 20000);
85}
86
53b6fc28 87int usb_gen_phy_init(struct usb_phy *phy)
2319fb88 88{
3fa4d734 89 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
2319fb88 90
58f735fe
RQ
91 if (!IS_ERR(nop->vcc)) {
92 if (regulator_enable(nop->vcc))
93 dev_err(phy->dev, "Failed to enable power\n");
94 }
95
2319fb88 96 if (!IS_ERR(nop->clk))
4d175f34 97 clk_prepare_enable(nop->clk);
2319fb88 98
bd27fa44
RQ
99 /* De-assert RESET */
100 nop_reset_set(nop, 0);
ad63ebfc 101
2319fb88
RQ
102 return 0;
103}
53b6fc28 104EXPORT_SYMBOL_GPL(usb_gen_phy_init);
2319fb88 105
53b6fc28 106void usb_gen_phy_shutdown(struct usb_phy *phy)
2319fb88 107{
3fa4d734 108 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
2319fb88 109
bd27fa44
RQ
110 /* Assert RESET */
111 nop_reset_set(nop, 1);
ad63ebfc 112
2319fb88 113 if (!IS_ERR(nop->clk))
4d175f34 114 clk_disable_unprepare(nop->clk);
58f735fe
RQ
115
116 if (!IS_ERR(nop->vcc)) {
117 if (regulator_disable(nop->vcc))
118 dev_err(phy->dev, "Failed to disable power\n");
119 }
2319fb88 120}
53b6fc28 121EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
2319fb88 122
41adf109 123static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
f6d92a05 124{
41adf109 125 if (!otg)
f6d92a05
AKG
126 return -ENODEV;
127
f6d92a05 128 if (!gadget) {
41adf109 129 otg->gadget = NULL;
f6d92a05
AKG
130 return -ENODEV;
131 }
132
41adf109
HK
133 otg->gadget = gadget;
134 otg->phy->state = OTG_STATE_B_IDLE;
f6d92a05
AKG
135 return 0;
136}
137
41adf109 138static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
f6d92a05 139{
41adf109 140 if (!otg)
f6d92a05
AKG
141 return -ENODEV;
142
f6d92a05 143 if (!host) {
41adf109 144 otg->host = NULL;
f6d92a05
AKG
145 return -ENODEV;
146 }
147
41adf109 148 otg->host = host;
f6d92a05
AKG
149 return 0;
150}
151
53b6fc28 152int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_gen_xceiv *nop,
af9f51c5 153 struct usb_phy_gen_xceiv_platform_data *pdata)
f6d92a05 154{
af9f51c5 155 enum usb_phy_type type = USB_PHY_TYPE_USB2;
f6d92a05
AKG
156 int err;
157
af9f51c5
FB
158 u32 clk_rate = 0;
159 bool needs_vcc = false;
160
161 nop->reset_active_low = true; /* default behaviour */
162
163 if (dev->of_node) {
164 struct device_node *node = dev->of_node;
165 enum of_gpio_flags flags;
166
167 if (of_property_read_u32(node, "clock-frequency", &clk_rate))
168 clk_rate = 0;
169
170 needs_vcc = of_property_read_bool(node, "vcc-supply");
171 nop->gpio_reset = of_get_named_gpio_flags(node, "reset-gpios",
172 0, &flags);
173 if (nop->gpio_reset == -EPROBE_DEFER)
174 return -EPROBE_DEFER;
175
176 nop->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
177
178 } else if (pdata) {
179 type = pdata->type;
180 clk_rate = pdata->clk_rate;
181 needs_vcc = pdata->needs_vcc;
182 nop->gpio_reset = pdata->gpio_reset;
dc52c574
AK
183 } else {
184 nop->gpio_reset = -1;
af9f51c5
FB
185 }
186
53b6fc28
SAS
187 nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
188 GFP_KERNEL);
e4d7dc66 189 if (!nop->phy.otg)
41adf109 190 return -ENOMEM;
41adf109 191
53b6fc28 192 nop->clk = devm_clk_get(dev, "main_clk");
2319fb88 193 if (IS_ERR(nop->clk)) {
53b6fc28 194 dev_dbg(dev, "Can't get phy clock: %ld\n",
2319fb88
RQ
195 PTR_ERR(nop->clk));
196 }
197
0eba3879
RQ
198 if (!IS_ERR(nop->clk) && clk_rate) {
199 err = clk_set_rate(nop->clk, clk_rate);
2319fb88 200 if (err) {
53b6fc28 201 dev_err(dev, "Error setting clock rate\n");
2319fb88
RQ
202 return err;
203 }
204 }
205
53b6fc28 206 nop->vcc = devm_regulator_get(dev, "vcc");
58f735fe 207 if (IS_ERR(nop->vcc)) {
53b6fc28 208 dev_dbg(dev, "Error getting vcc regulator: %ld\n",
58f735fe 209 PTR_ERR(nop->vcc));
b54b5f56
RQ
210 if (needs_vcc)
211 return -EPROBE_DEFER;
58f735fe
RQ
212 }
213
bd27fa44
RQ
214 if (gpio_is_valid(nop->gpio_reset)) {
215 unsigned long gpio_flags;
216
217 /* Assert RESET */
218 if (nop->reset_active_low)
219 gpio_flags = GPIOF_OUT_INIT_LOW;
220 else
221 gpio_flags = GPIOF_OUT_INIT_HIGH;
222
223 err = devm_gpio_request_one(dev, nop->gpio_reset,
224 gpio_flags, dev_name(dev));
225 if (err) {
226 dev_err(dev, "Error requesting RESET GPIO %d\n",
227 nop->gpio_reset);
228 return err;
229 }
ad63ebfc
RQ
230 }
231
53b6fc28 232 nop->dev = dev;
41adf109
HK
233 nop->phy.dev = nop->dev;
234 nop->phy.label = "nop-xceiv";
235 nop->phy.set_suspend = nop_set_suspend;
236 nop->phy.state = OTG_STATE_UNDEFINED;
90f4232f 237 nop->phy.type = type;
41adf109
HK
238
239 nop->phy.otg->phy = &nop->phy;
240 nop->phy.otg->set_host = nop_set_host;
241 nop->phy.otg->set_peripheral = nop_set_peripheral;
242
53b6fc28
SAS
243 ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
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");