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