blob: 406f8e43f852a4448a3fbd7e429bda32802c05e1 [file] [log] [blame]
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +05301/*
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:
David Brownellcc835e32009-03-31 12:28:31 -070025 * This provides a "nop" transceiver for PHYs which are
26 * autonomous such as isp1504, isp1707, etc.
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053027 */
28
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/dma-mapping.h>
32#include <linux/usb/otg.h>
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020033#include <linux/usb/usb_phy_gen_xceiv.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Roger Quadros2319fb82013-03-12 13:24:21 +020035#include <linux/clk.h>
Roger Quadros58f735f2013-03-12 13:24:22 +020036#include <linux/regulator/consumer.h>
Roger Quadros0eba3872013-03-12 13:24:25 +020037#include <linux/of.h>
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053038
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020039struct usb_phy_gen_xceiv {
Roger Quadros0eba3872013-03-12 13:24:25 +020040 struct usb_phy phy;
41 struct device *dev;
42 struct clk *clk;
43 struct regulator *vcc;
44 struct regulator *reset;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053045};
46
David Brownellcc835e32009-03-31 12:28:31 -070047static struct platform_device *pd;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053048
49void usb_nop_xceiv_register(void)
50{
David Brownellcc835e32009-03-31 12:28:31 -070051 if (pd)
52 return;
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020053 pd = platform_device_register_simple("usb_phy_gen_xceiv", -1, NULL, 0);
David Brownellcc835e32009-03-31 12:28:31 -070054 if (!pd) {
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020055 pr_err("Unable to register generic usb transceiver\n");
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053056 return;
57 }
58}
David Brownellcc835e32009-03-31 12:28:31 -070059EXPORT_SYMBOL(usb_nop_xceiv_register);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053060
61void usb_nop_xceiv_unregister(void)
62{
David Brownellcc835e32009-03-31 12:28:31 -070063 platform_device_unregister(pd);
Ajay Kumar Guptadc7520c2009-07-03 13:18:45 +053064 pd = NULL;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053065}
David Brownellcc835e32009-03-31 12:28:31 -070066EXPORT_SYMBOL(usb_nop_xceiv_unregister);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053067
Heikki Krogerus86753812012-02-13 13:24:02 +020068static int nop_set_suspend(struct usb_phy *x, int suspend)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +053069{
70 return 0;
71}
72
Roger Quadros2319fb82013-03-12 13:24:21 +020073static int nop_init(struct usb_phy *phy)
74{
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020075 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
Roger Quadros2319fb82013-03-12 13:24:21 +020076
Roger Quadros58f735f2013-03-12 13:24:22 +020077 if (!IS_ERR(nop->vcc)) {
78 if (regulator_enable(nop->vcc))
79 dev_err(phy->dev, "Failed to enable power\n");
80 }
81
Roger Quadros2319fb82013-03-12 13:24:21 +020082 if (!IS_ERR(nop->clk))
83 clk_enable(nop->clk);
84
Roger Quadrosad63ebfc2013-03-12 13:24:23 +020085 if (!IS_ERR(nop->reset)) {
86 /* De-assert RESET */
87 if (regulator_enable(nop->reset))
88 dev_err(phy->dev, "Failed to de-assert reset\n");
89 }
90
Roger Quadros2319fb82013-03-12 13:24:21 +020091 return 0;
92}
93
94static void nop_shutdown(struct usb_phy *phy)
95{
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +020096 struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
Roger Quadros2319fb82013-03-12 13:24:21 +020097
Roger Quadrosad63ebfc2013-03-12 13:24:23 +020098 if (!IS_ERR(nop->reset)) {
99 /* Assert RESET */
100 if (regulator_disable(nop->reset))
101 dev_err(phy->dev, "Failed to assert reset\n");
102 }
103
Roger Quadros2319fb82013-03-12 13:24:21 +0200104 if (!IS_ERR(nop->clk))
105 clk_disable(nop->clk);
Roger Quadros58f735f2013-03-12 13:24:22 +0200106
107 if (!IS_ERR(nop->vcc)) {
108 if (regulator_disable(nop->vcc))
109 dev_err(phy->dev, "Failed to disable power\n");
110 }
Roger Quadros2319fb82013-03-12 13:24:21 +0200111}
112
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;
124 otg->phy->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
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200142static int usb_phy_gen_xceiv_probe(struct platform_device *pdev)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530143{
Roger Quadros0eba3872013-03-12 13:24:25 +0200144 struct device *dev = &pdev->dev;
Felipe Balbi9e5f9c82013-08-09 17:31:23 +0300145 struct usb_phy_gen_xceiv_platform_data *pdata =
Jingoo Han19f9e182013-07-30 17:02:13 +0900146 dev_get_platdata(&pdev->dev);
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200147 struct usb_phy_gen_xceiv *nop;
Felipe Balbic84d3642012-07-19 13:38:06 +0300148 enum usb_phy_type type = USB_PHY_TYPE_USB2;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530149 int err;
Roger Quadros0eba3872013-03-12 13:24:25 +0200150 u32 clk_rate = 0;
Roger Quadrosb54b5f52013-03-12 13:24:26 +0200151 bool needs_vcc = false;
152 bool needs_reset = false;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530153
Roger Quadrose4d7dc62013-03-12 13:24:20 +0200154 nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530155 if (!nop)
156 return -ENOMEM;
157
Roger Quadrose4d7dc62013-03-12 13:24:20 +0200158 nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg),
159 GFP_KERNEL);
160 if (!nop->phy.otg)
Heikki Krogerus41adf102012-02-13 13:24:10 +0200161 return -ENOMEM;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530162
Roger Quadros0eba3872013-03-12 13:24:25 +0200163 if (dev->of_node) {
164 struct device_node *node = dev->of_node;
165
166 if (of_property_read_u32(node, "clock-frequency", &clk_rate))
167 clk_rate = 0;
168
Roger Quadrosb54b5f52013-03-12 13:24:26 +0200169 needs_vcc = of_property_read_bool(node, "vcc-supply");
170 needs_reset = of_property_read_bool(node, "reset-supply");
171
Roger Quadros0eba3872013-03-12 13:24:25 +0200172 } else if (pdata) {
Felipe Balbic84d3642012-07-19 13:38:06 +0300173 type = pdata->type;
Roger Quadros0eba3872013-03-12 13:24:25 +0200174 clk_rate = pdata->clk_rate;
Roger Quadrosb54b5f52013-03-12 13:24:26 +0200175 needs_vcc = pdata->needs_vcc;
176 needs_reset = pdata->needs_reset;
Roger Quadros0eba3872013-03-12 13:24:25 +0200177 }
Felipe Balbic84d3642012-07-19 13:38:06 +0300178
Roger Quadros2319fb82013-03-12 13:24:21 +0200179 nop->clk = devm_clk_get(&pdev->dev, "main_clk");
180 if (IS_ERR(nop->clk)) {
181 dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n",
182 PTR_ERR(nop->clk));
183 }
184
Roger Quadros0eba3872013-03-12 13:24:25 +0200185 if (!IS_ERR(nop->clk) && clk_rate) {
186 err = clk_set_rate(nop->clk, clk_rate);
Roger Quadros2319fb82013-03-12 13:24:21 +0200187 if (err) {
188 dev_err(&pdev->dev, "Error setting clock rate\n");
189 return err;
190 }
191 }
192
193 if (!IS_ERR(nop->clk)) {
194 err = clk_prepare(nop->clk);
195 if (err) {
196 dev_err(&pdev->dev, "Error preparing clock\n");
197 return err;
198 }
199 }
200
Roger Quadros58f735f2013-03-12 13:24:22 +0200201 nop->vcc = devm_regulator_get(&pdev->dev, "vcc");
202 if (IS_ERR(nop->vcc)) {
203 dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n",
204 PTR_ERR(nop->vcc));
Roger Quadrosb54b5f52013-03-12 13:24:26 +0200205 if (needs_vcc)
206 return -EPROBE_DEFER;
Roger Quadros58f735f2013-03-12 13:24:22 +0200207 }
208
Roger Quadrosad63ebfc2013-03-12 13:24:23 +0200209 nop->reset = devm_regulator_get(&pdev->dev, "reset");
210 if (IS_ERR(nop->reset)) {
211 dev_dbg(&pdev->dev, "Error getting reset regulator: %ld\n",
212 PTR_ERR(nop->reset));
Roger Quadrosb54b5f52013-03-12 13:24:26 +0200213 if (needs_reset)
214 return -EPROBE_DEFER;
Roger Quadrosad63ebfc2013-03-12 13:24:23 +0200215 }
216
Heikki Krogerus41adf102012-02-13 13:24:10 +0200217 nop->dev = &pdev->dev;
218 nop->phy.dev = nop->dev;
219 nop->phy.label = "nop-xceiv";
220 nop->phy.set_suspend = nop_set_suspend;
Roger Quadros2319fb82013-03-12 13:24:21 +0200221 nop->phy.init = nop_init;
222 nop->phy.shutdown = nop_shutdown;
Heikki Krogerus41adf102012-02-13 13:24:10 +0200223 nop->phy.state = OTG_STATE_UNDEFINED;
Roger Quadros90f42322013-03-12 13:24:24 +0200224 nop->phy.type = type;
Heikki Krogerus41adf102012-02-13 13:24:10 +0200225
226 nop->phy.otg->phy = &nop->phy;
227 nop->phy.otg->set_host = nop_set_host;
228 nop->phy.otg->set_peripheral = nop_set_peripheral;
229
Roger Quadros90f42322013-03-12 13:24:24 +0200230 err = usb_add_phy_dev(&nop->phy);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530231 if (err) {
232 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
233 err);
Roger Quadros2319fb82013-03-12 13:24:21 +0200234 goto err_add;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530235 }
236
237 platform_set_drvdata(pdev, nop);
238
Heikki Krogerus41adf102012-02-13 13:24:10 +0200239 ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
Ming Lei20831ad2011-01-04 20:21:32 +0800240
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530241 return 0;
Roger Quadros2319fb82013-03-12 13:24:21 +0200242
243err_add:
244 if (!IS_ERR(nop->clk))
245 clk_unprepare(nop->clk);
246 return err;
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530247}
248
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200249static int usb_phy_gen_xceiv_remove(struct platform_device *pdev)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530250{
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200251 struct usb_phy_gen_xceiv *nop = platform_get_drvdata(pdev);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530252
Roger Quadros2319fb82013-03-12 13:24:21 +0200253 if (!IS_ERR(nop->clk))
254 clk_unprepare(nop->clk);
255
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530256 usb_remove_phy(&nop->phy);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530257
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530258 return 0;
259}
260
Roger Quadros0eba3872013-03-12 13:24:25 +0200261static const struct of_device_id nop_xceiv_dt_ids[] = {
262 { .compatible = "usb-nop-xceiv" },
263 { }
264};
265
266MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
267
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200268static struct platform_driver usb_phy_gen_xceiv_driver = {
269 .probe = usb_phy_gen_xceiv_probe,
270 .remove = usb_phy_gen_xceiv_remove,
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530271 .driver = {
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200272 .name = "usb_phy_gen_xceiv",
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530273 .owner = THIS_MODULE,
Sachin Kamat5462b0d2013-05-21 17:17:22 +0530274 .of_match_table = nop_xceiv_dt_ids,
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530275 },
276};
277
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200278static int __init usb_phy_gen_xceiv_init(void)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530279{
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200280 return platform_driver_register(&usb_phy_gen_xceiv_driver);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530281}
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200282subsys_initcall(usb_phy_gen_xceiv_init);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530283
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200284static void __exit usb_phy_gen_xceiv_exit(void)
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530285{
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200286 platform_driver_unregister(&usb_phy_gen_xceiv_driver);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530287}
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200288module_exit(usb_phy_gen_xceiv_exit);
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530289
Sebastian Andrzej Siewior3fa4d732013-07-26 12:16:42 +0200290MODULE_ALIAS("platform:usb_phy_gen_xceiv");
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530291MODULE_AUTHOR("Texas Instruments Inc");
292MODULE_DESCRIPTION("NOP USB Transceiver driver");
293MODULE_LICENSE("GPL");