phy-generic.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * drivers/usb/otg/nop-usb-xceiv.c
 *
 * NOP USB transceiver for all USB transceiver which are either built-in
 * into USB IP or which are mostly autonomous.
 *
 * Copyright (C) 2009 Texas Instruments Inc
 * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Current status:
25 26
 *	This provides a "nop" transceiver for PHYs which are
 *	autonomous such as isp1504, isp1707, etc.
27 28 29 30 31 32
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/usb/otg.h>
33
#include <linux/usb/usb_phy_gen_xceiv.h>
34
#include <linux/slab.h>
R
Roger Quadros 已提交
35
#include <linux/clk.h>
36
#include <linux/regulator/consumer.h>
37
#include <linux/of.h>
38

39
struct usb_phy_gen_xceiv {
40 41 42 43 44
	struct usb_phy phy;
	struct device *dev;
	struct clk *clk;
	struct regulator *vcc;
	struct regulator *reset;
45 46
};

47
static struct platform_device *pd;
48 49 50

void usb_nop_xceiv_register(void)
{
51 52
	if (pd)
		return;
53
	pd = platform_device_register_simple("usb_phy_gen_xceiv", -1, NULL, 0);
54
	if (!pd) {
55
		pr_err("Unable to register generic usb transceiver\n");
56 57 58
		return;
	}
}
59
EXPORT_SYMBOL(usb_nop_xceiv_register);
60 61 62

void usb_nop_xceiv_unregister(void)
{
63
	platform_device_unregister(pd);
64
	pd = NULL;
65
}
66
EXPORT_SYMBOL(usb_nop_xceiv_unregister);
67

68
static int nop_set_suspend(struct usb_phy *x, int suspend)
69 70 71 72
{
	return 0;
}

R
Roger Quadros 已提交
73 74
static int nop_init(struct usb_phy *phy)
{
75
	struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
R
Roger Quadros 已提交
76

77 78 79 80 81
	if (!IS_ERR(nop->vcc)) {
		if (regulator_enable(nop->vcc))
			dev_err(phy->dev, "Failed to enable power\n");
	}

R
Roger Quadros 已提交
82 83 84
	if (!IS_ERR(nop->clk))
		clk_enable(nop->clk);

85 86 87 88 89 90
	if (!IS_ERR(nop->reset)) {
		/* De-assert RESET */
		if (regulator_enable(nop->reset))
			dev_err(phy->dev, "Failed to de-assert reset\n");
	}

R
Roger Quadros 已提交
91 92 93 94 95
	return 0;
}

static void nop_shutdown(struct usb_phy *phy)
{
96
	struct usb_phy_gen_xceiv *nop = dev_get_drvdata(phy->dev);
R
Roger Quadros 已提交
97

98 99 100 101 102 103
	if (!IS_ERR(nop->reset)) {
		/* Assert RESET */
		if (regulator_disable(nop->reset))
			dev_err(phy->dev, "Failed to assert reset\n");
	}

R
Roger Quadros 已提交
104 105
	if (!IS_ERR(nop->clk))
		clk_disable(nop->clk);
106 107 108 109 110

	if (!IS_ERR(nop->vcc)) {
		if (regulator_disable(nop->vcc))
			dev_err(phy->dev, "Failed to disable power\n");
	}
R
Roger Quadros 已提交
111 112
}

113
static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
114
{
115
	if (!otg)
116 117 118
		return -ENODEV;

	if (!gadget) {
119
		otg->gadget = NULL;
120 121 122
		return -ENODEV;
	}

123 124
	otg->gadget = gadget;
	otg->phy->state = OTG_STATE_B_IDLE;
125 126 127
	return 0;
}

128
static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
129
{
130
	if (!otg)
131 132 133
		return -ENODEV;

	if (!host) {
134
		otg->host = NULL;
135 136 137
		return -ENODEV;
	}

138
	otg->host = host;
139 140 141
	return 0;
}

142
static int usb_phy_gen_xceiv_probe(struct platform_device *pdev)
143
{
144
	struct device *dev = &pdev->dev;
145
	struct usb_phy_gen_xceiv_platform_data *pdata =
J
Jingoo Han 已提交
146
			dev_get_platdata(&pdev->dev);
147
	struct usb_phy_gen_xceiv	*nop;
148
	enum usb_phy_type	type = USB_PHY_TYPE_USB2;
149
	int err;
150
	u32 clk_rate = 0;
151 152
	bool needs_vcc = false;
	bool needs_reset = false;
153

154
	nop = devm_kzalloc(&pdev->dev, sizeof(*nop), GFP_KERNEL);
155 156 157
	if (!nop)
		return -ENOMEM;

158 159 160
	nop->phy.otg = devm_kzalloc(&pdev->dev, sizeof(*nop->phy.otg),
							GFP_KERNEL);
	if (!nop->phy.otg)
161 162
		return -ENOMEM;

163 164 165 166 167 168
	if (dev->of_node) {
		struct device_node *node = dev->of_node;

		if (of_property_read_u32(node, "clock-frequency", &clk_rate))
			clk_rate = 0;

169 170 171
		needs_vcc = of_property_read_bool(node, "vcc-supply");
		needs_reset = of_property_read_bool(node, "reset-supply");

172
	} else if (pdata) {
173
		type = pdata->type;
174
		clk_rate = pdata->clk_rate;
175 176
		needs_vcc = pdata->needs_vcc;
		needs_reset = pdata->needs_reset;
177
	}
178

R
Roger Quadros 已提交
179 180 181 182 183 184
	nop->clk = devm_clk_get(&pdev->dev, "main_clk");
	if (IS_ERR(nop->clk)) {
		dev_dbg(&pdev->dev, "Can't get phy clock: %ld\n",
					PTR_ERR(nop->clk));
	}

185 186
	if (!IS_ERR(nop->clk) && clk_rate) {
		err = clk_set_rate(nop->clk, clk_rate);
R
Roger Quadros 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
		if (err) {
			dev_err(&pdev->dev, "Error setting clock rate\n");
			return err;
		}
	}

	if (!IS_ERR(nop->clk)) {
		err = clk_prepare(nop->clk);
		if (err) {
			dev_err(&pdev->dev, "Error preparing clock\n");
			return err;
		}
	}

201 202 203 204
	nop->vcc = devm_regulator_get(&pdev->dev, "vcc");
	if (IS_ERR(nop->vcc)) {
		dev_dbg(&pdev->dev, "Error getting vcc regulator: %ld\n",
					PTR_ERR(nop->vcc));
205 206
		if (needs_vcc)
			return -EPROBE_DEFER;
207 208
	}

209 210 211 212
	nop->reset = devm_regulator_get(&pdev->dev, "reset");
	if (IS_ERR(nop->reset)) {
		dev_dbg(&pdev->dev, "Error getting reset regulator: %ld\n",
					PTR_ERR(nop->reset));
213 214
		if (needs_reset)
			return -EPROBE_DEFER;
215 216
	}

217
	nop->dev		= &pdev->dev;
218 219 220
	nop->phy.dev		= nop->dev;
	nop->phy.label		= "nop-xceiv";
	nop->phy.set_suspend	= nop_set_suspend;
R
Roger Quadros 已提交
221 222
	nop->phy.init		= nop_init;
	nop->phy.shutdown	= nop_shutdown;
223
	nop->phy.state		= OTG_STATE_UNDEFINED;
224
	nop->phy.type		= type;
225 226 227 228 229

	nop->phy.otg->phy		= &nop->phy;
	nop->phy.otg->set_host		= nop_set_host;
	nop->phy.otg->set_peripheral	= nop_set_peripheral;

230
	err = usb_add_phy_dev(&nop->phy);
231 232 233
	if (err) {
		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
			err);
R
Roger Quadros 已提交
234
		goto err_add;
235 236 237 238
	}

	platform_set_drvdata(pdev, nop);

239
	ATOMIC_INIT_NOTIFIER_HEAD(&nop->phy.notifier);
240

241
	return 0;
R
Roger Quadros 已提交
242 243 244 245 246

err_add:
	if (!IS_ERR(nop->clk))
		clk_unprepare(nop->clk);
	return err;
247 248
}

249
static int usb_phy_gen_xceiv_remove(struct platform_device *pdev)
250
{
251
	struct usb_phy_gen_xceiv *nop = platform_get_drvdata(pdev);
252

R
Roger Quadros 已提交
253 254 255
	if (!IS_ERR(nop->clk))
		clk_unprepare(nop->clk);

256
	usb_remove_phy(&nop->phy);
257 258 259 260

	return 0;
}

261 262 263 264 265 266 267
static const struct of_device_id nop_xceiv_dt_ids[] = {
	{ .compatible = "usb-nop-xceiv" },
	{ }
};

MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);

268 269 270
static struct platform_driver usb_phy_gen_xceiv_driver = {
	.probe		= usb_phy_gen_xceiv_probe,
	.remove		= usb_phy_gen_xceiv_remove,
271
	.driver		= {
272
		.name	= "usb_phy_gen_xceiv",
273
		.owner	= THIS_MODULE,
274
		.of_match_table = nop_xceiv_dt_ids,
275 276 277
	},
};

278
static int __init usb_phy_gen_xceiv_init(void)
279
{
280
	return platform_driver_register(&usb_phy_gen_xceiv_driver);
281
}
282
subsys_initcall(usb_phy_gen_xceiv_init);
283

284
static void __exit usb_phy_gen_xceiv_exit(void)
285
{
286
	platform_driver_unregister(&usb_phy_gen_xceiv_driver);
287
}
288
module_exit(usb_phy_gen_xceiv_exit);
289

290
MODULE_ALIAS("platform:usb_phy_gen_xceiv");
291 292 293
MODULE_AUTHOR("Texas Instruments Inc");
MODULE_DESCRIPTION("NOP USB Transceiver driver");
MODULE_LICENSE("GPL");