blob: f1b719b45a535d3d61a791fbabe9bebff3bca2d5 [file] [log] [blame]
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +05301/*
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +05302 * NOP USB transceiver for all USB transceiver which are either built-in
3 * into USB IP or which are mostly autonomous.
4 *
5 * Copyright (C) 2009 Texas Instruments Inc
6 * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Current status:
David Brownellcc835e32009-03-31 12:28:31 -070023 * This provides a "nop" transceiver for PHYs which are
24 * autonomous such as isp1504, isp1707, etc.
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053025 */
26
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/dma-mapping.h>
30#include <linux/usb/otg.h>
Felipe Balbid7078df2014-04-16 15:28:32 -050031#include <linux/usb/usb_phy_generic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Roger Quadros2319fb82013-03-12 13:24:21 +020033#include <linux/clk.h>
Roger Quadros58f735f2013-03-12 13:24:22 +020034#include <linux/regulator/consumer.h>
Roger Quadros0eba3872013-03-12 13:24:25 +020035#include <linux/of.h>
Roger Quadrosbd27fa42013-09-24 11:53:48 +030036#include <linux/of_gpio.h>
37#include <linux/gpio.h>
38#include <linux/delay.h>
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053039
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +020040#include "phy-generic.h"
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053041
Felipe Balbi2f36ff62014-04-16 16:16:33 -050042struct platform_device *usb_phy_generic_register(void)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053043{
Felipe Balbi2f36ff62014-04-16 16:16:33 -050044 return platform_device_register_simple("usb_phy_generic",
45 PLATFORM_DEVID_AUTO, NULL, 0);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053046}
Felipe Balbi4525bee2014-04-16 15:20:44 -050047EXPORT_SYMBOL_GPL(usb_phy_generic_register);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053048
Felipe Balbi2f36ff62014-04-16 16:16:33 -050049void usb_phy_generic_unregister(struct platform_device *pdev)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053050{
Felipe Balbi2f36ff62014-04-16 16:16:33 -050051 platform_device_unregister(pdev);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053052}
Felipe Balbi4525bee2014-04-16 15:20:44 -050053EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053054
Heikki Krogerus86753812012-02-13 13:24:02 +020055static int nop_set_suspend(struct usb_phy *x, int suspend)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053056{
57 return 0;
58}
59
Felipe Balbi4525bee2014-04-16 15:20:44 -050060static void nop_reset_set(struct usb_phy_generic *nop, int asserted)
Roger Quadrosbd27fa42013-09-24 11:53:48 +030061{
62 int value;
63
64 if (!gpio_is_valid(nop->gpio_reset))
65 return;
66
67 value = asserted;
68 if (nop->reset_active_low)
69 value = !value;
70
71 gpio_set_value_cansleep(nop->gpio_reset, value);
72
73 if (!asserted)
74 usleep_range(10000, 20000);
75}
76
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +020077int usb_gen_phy_init(struct usb_phy *phy)
Roger Quadros2319fb82013-03-12 13:24:21 +020078{
Felipe Balbi4525bee2014-04-16 15:20:44 -050079 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
Roger Quadros2319fb82013-03-12 13:24:21 +020080
Roger Quadros58f735f2013-03-12 13:24:22 +020081 if (!IS_ERR(nop->vcc)) {
82 if (regulator_enable(nop->vcc))
83 dev_err(phy->dev, "Failed to enable power\n");
84 }
85
Roger Quadros2319fb82013-03-12 13:24:21 +020086 if (!IS_ERR(nop->clk))
Mark Brown4d175f32013-08-11 15:26:04 +010087 clk_prepare_enable(nop->clk);
Roger Quadros2319fb82013-03-12 13:24:21 +020088
Roger Quadrosbd27fa42013-09-24 11:53:48 +030089 /* De-assert RESET */
90 nop_reset_set(nop, 0);
Roger Quadrosad63ebfc2013-03-12 13:24:23 +020091
Roger Quadros2319fb82013-03-12 13:24:21 +020092 return 0;
93}
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +020094EXPORT_SYMBOL_GPL(usb_gen_phy_init);
Roger Quadros2319fb82013-03-12 13:24:21 +020095
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +020096void usb_gen_phy_shutdown(struct usb_phy *phy)
Roger Quadros2319fb82013-03-12 13:24:21 +020097{
Felipe Balbi4525bee2014-04-16 15:20:44 -050098 struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
Roger Quadros2319fb82013-03-12 13:24:21 +020099
Roger Quadrosbd27fa42013-09-24 11:53:48 +0300100 /* Assert RESET */
101 nop_reset_set(nop, 1);
Roger Quadrosad63ebfc2013-03-12 13:24:23 +0200102
Roger Quadros2319fb82013-03-12 13:24:21 +0200103 if (!IS_ERR(nop->clk))
Mark Brown4d175f32013-08-11 15:26:04 +0100104 clk_disable_unprepare(nop->clk);
Roger Quadros58f735f2013-03-12 13:24:22 +0200105
106 if (!IS_ERR(nop->vcc)) {
107 if (regulator_disable(nop->vcc))
108 dev_err(phy->dev, "Failed to disable power\n");
109 }
Roger Quadros2319fb82013-03-12 13:24:21 +0200110}
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200111EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
Roger Quadros2319fb82013-03-12 13:24:21 +0200112
Heikki Krogerus41adf102012-02-13 13:24:10 +0200113static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530114{
Heikki Krogerus41adf102012-02-13 13:24:10 +0200115 if (!otg)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530116 return -ENODEV;
117
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530118 if (!gadget) {
Heikki Krogerus41adf102012-02-13 13:24:10 +0200119 otg->gadget = NULL;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530120 return -ENODEV;
121 }
122
Heikki Krogerus41adf102012-02-13 13:24:10 +0200123 otg->gadget = gadget;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100124 otg->state = OTG_STATE_B_IDLE;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530125 return 0;
126}
127
Heikki Krogerus41adf102012-02-13 13:24:10 +0200128static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530129{
Heikki Krogerus41adf102012-02-13 13:24:10 +0200130 if (!otg)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530131 return -ENODEV;
132
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530133 if (!host) {
Heikki Krogerus41adf102012-02-13 13:24:10 +0200134 otg->host = NULL;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530135 return -ENODEV;
136 }
137
Heikki Krogerus41adf102012-02-13 13:24:10 +0200138 otg->host = host;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530139 return 0;
140}
141
Felipe Balbi4525bee2014-04-16 15:20:44 -0500142int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop,
143 struct usb_phy_generic_platform_data *pdata)
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200144{
Felipe Balbiaf9f51c2013-10-24 09:45:29 -0500145 enum usb_phy_type type = USB_PHY_TYPE_USB2;
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200146 int err;
147
Felipe Balbiaf9f51c2013-10-24 09:45:29 -0500148 u32 clk_rate = 0;
149 bool needs_vcc = false;
150
151 nop->reset_active_low = true; /* default behaviour */
152
153 if (dev->of_node) {
154 struct device_node *node = dev->of_node;
Heikki Krogerus37cfbc42013-11-15 10:35:12 +0200155 enum of_gpio_flags flags = 0;
Felipe Balbiaf9f51c2013-10-24 09:45:29 -0500156
157 if (of_property_read_u32(node, "clock-frequency", &clk_rate))
158 clk_rate = 0;
159
160 needs_vcc = of_property_read_bool(node, "vcc-supply");
161 nop->gpio_reset = of_get_named_gpio_flags(node, "reset-gpios",
162 0, &flags);
163 if (nop->gpio_reset == -EPROBE_DEFER)
164 return -EPROBE_DEFER;
165
166 nop->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
167
168 } else if (pdata) {
169 type = pdata->type;
170 clk_rate = pdata->clk_rate;
171 needs_vcc = pdata->needs_vcc;
172 nop->gpio_reset = pdata->gpio_reset;
Aaro Koskinendc52c572013-11-15 21:38:50 +0200173 } else {
174 nop->gpio_reset = -1;
Felipe Balbiaf9f51c2013-10-24 09:45:29 -0500175 }
176
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200177 nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
178 GFP_KERNEL);
179 if (!nop->phy.otg)
180 return -ENOMEM;
181
182 nop->clk = devm_clk_get(dev, "main_clk");
183 if (IS_ERR(nop->clk)) {
184 dev_dbg(dev, "Can't get phy clock: %ld\n",
185 PTR_ERR(nop->clk));
186 }
187
188 if (!IS_ERR(nop->clk) && clk_rate) {
189 err = clk_set_rate(nop->clk, clk_rate);
190 if (err) {
191 dev_err(dev, "Error setting clock rate\n");
192 return err;
193 }
194 }
195
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200196 nop->vcc = devm_regulator_get(dev, "vcc");
197 if (IS_ERR(nop->vcc)) {
198 dev_dbg(dev, "Error getting vcc regulator: %ld\n",
199 PTR_ERR(nop->vcc));
200 if (needs_vcc)
201 return -EPROBE_DEFER;
202 }
203
Roger Quadrosbd27fa42013-09-24 11:53:48 +0300204 if (gpio_is_valid(nop->gpio_reset)) {
205 unsigned long gpio_flags;
206
207 /* Assert RESET */
208 if (nop->reset_active_low)
209 gpio_flags = GPIOF_OUT_INIT_LOW;
210 else
211 gpio_flags = GPIOF_OUT_INIT_HIGH;
212
213 err = devm_gpio_request_one(dev, nop->gpio_reset,
214 gpio_flags, dev_name(dev));
215 if (err) {
216 dev_err(dev, "Error requesting RESET GPIO %d\n",
217 nop->gpio_reset);
218 return err;
219 }
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200220 }
221
222 nop->dev = dev;
223 nop->phy.dev = nop->dev;
224 nop->phy.label = "nop-xceiv";
225 nop->phy.set_suspend = nop_set_suspend;
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200226 nop->phy.type = type;
227
Antoine Tenarte47d9252014-10-30 18:41:13 +0100228 nop->phy.otg->state = OTG_STATE_UNDEFINED;
Antoine Tenart19c1eac2014-10-30 18:41:14 +0100229 nop->phy.otg->usb_phy = &nop->phy;
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200230 nop->phy.otg->set_host = nop_set_host;
231 nop->phy.otg->set_peripheral = nop_set_peripheral;
232
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200233 return 0;
234}
235EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
236
Felipe Balbi4525bee2014-04-16 15:20:44 -0500237static int usb_phy_generic_probe(struct platform_device *pdev)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530238{
Roger Quadros0eba3872013-03-12 13:24:25 +0200239 struct device *dev = &pdev->dev;
Felipe Balbi4525bee2014-04-16 15:20:44 -0500240 struct usb_phy_generic *nop;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530241 int err;
Felipe Balbic84d3642012-07-19 13:38:06 +0300242
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200243 nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
244 if (!nop)
245 return -ENOMEM;
Roger Quadros2319fb82013-03-12 13:24:21 +0200246
Felipe Balbiaf9f51c2013-10-24 09:45:29 -0500247 err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200248 if (err)
249 return err;
Roger Quadros2319fb82013-03-12 13:24:21 +0200250
Sebastian Andrzej Siewior53b6fc22013-07-30 17:20:06 +0200251 nop->phy.init = usb_gen_phy_init;
252 nop->phy.shutdown = usb_gen_phy_shutdown;
Heikki Krogerus41adf102012-02-13 13:24:10 +0200253
Roger Quadros90f42322013-03-12 13:24:24 +0200254 err = usb_add_phy_dev(&nop->phy);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530255 if (err) {
256 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
257 err);
Mark Brown4d175f32013-08-11 15:26:04 +0100258 return err;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530259 }
260
261 platform_set_drvdata(pdev, nop);
262
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530263 return 0;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530264}
265
Felipe Balbi4525bee2014-04-16 15:20:44 -0500266static int usb_phy_generic_remove(struct platform_device *pdev)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530267{
Felipe Balbi4525bee2014-04-16 15:20:44 -0500268 struct usb_phy_generic *nop = platform_get_drvdata(pdev);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530269
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530270 usb_remove_phy(&nop->phy);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530271
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530272 return 0;
273}
274
Roger Quadros0eba3872013-03-12 13:24:25 +0200275static const struct of_device_id nop_xceiv_dt_ids[] = {
276 { .compatible = "usb-nop-xceiv" },
277 { }
278};
279
280MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
281
Felipe Balbi4525bee2014-04-16 15:20:44 -0500282static struct platform_driver usb_phy_generic_driver = {
283 .probe = usb_phy_generic_probe,
284 .remove = usb_phy_generic_remove,
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530285 .driver = {
Felipe Balbi4525bee2014-04-16 15:20:44 -0500286 .name = "usb_phy_generic",
Sachin Kamat5462b0d2013-05-21 17:17:22 +0530287 .of_match_table = nop_xceiv_dt_ids,
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530288 },
289};
290
Felipe Balbi4525bee2014-04-16 15:20:44 -0500291static int __init usb_phy_generic_init(void)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530292{
Felipe Balbi4525bee2014-04-16 15:20:44 -0500293 return platform_driver_register(&usb_phy_generic_driver);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530294}
Felipe Balbi4525bee2014-04-16 15:20:44 -0500295subsys_initcall(usb_phy_generic_init);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530296
Felipe Balbi4525bee2014-04-16 15:20:44 -0500297static void __exit usb_phy_generic_exit(void)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530298{
Felipe Balbi4525bee2014-04-16 15:20:44 -0500299 platform_driver_unregister(&usb_phy_generic_driver);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530300}
Felipe Balbi4525bee2014-04-16 15:20:44 -0500301module_exit(usb_phy_generic_exit);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530302
Felipe Balbi4525bee2014-04-16 15:20:44 -0500303MODULE_ALIAS("platform:usb_phy_generic");
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530304MODULE_AUTHOR("Texas Instruments Inc");
305MODULE_DESCRIPTION("NOP USB Transceiver driver");
306MODULE_LICENSE("GPL");