1/*
2 * 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:
23 *	This provides a "nop" transceiver for PHYs which are
24 *	autonomous such as isp1504, isp1707, etc.
25 */
26
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/dma-mapping.h>
30#include <linux/usb/gadget.h>
31#include <linux/usb/otg.h>
32#include <linux/usb/usb_phy_generic.h>
33#include <linux/slab.h>
34#include <linux/clk.h>
35#include <linux/regulator/consumer.h>
36#include <linux/of.h>
37#include <linux/of_gpio.h>
38#include <linux/gpio.h>
39#include <linux/delay.h>
40
41#include "phy-generic.h"
42
43#define VBUS_IRQ_FLAGS \
44	(IRQF_SHARED | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | \
45		IRQF_ONESHOT)
46
47struct platform_device *usb_phy_generic_register(void)
48{
49	return platform_device_register_simple("usb_phy_generic",
50			PLATFORM_DEVID_AUTO, NULL, 0);
51}
52EXPORT_SYMBOL_GPL(usb_phy_generic_register);
53
54void usb_phy_generic_unregister(struct platform_device *pdev)
55{
56	platform_device_unregister(pdev);
57}
58EXPORT_SYMBOL_GPL(usb_phy_generic_unregister);
59
60static int nop_set_suspend(struct usb_phy *x, int suspend)
61{
62	return 0;
63}
64
65static void nop_reset(struct usb_phy_generic *nop)
66{
67	if (!nop->gpiod_reset)
68		return;
69
70	gpiod_set_value(nop->gpiod_reset, 1);
71	usleep_range(10000, 20000);
72	gpiod_set_value(nop->gpiod_reset, 0);
73}
74
75/* interface to regulator framework */
76static void nop_set_vbus_draw(struct usb_phy_generic *nop, unsigned mA)
77{
78	struct regulator *vbus_draw = nop->vbus_draw;
79	int enabled;
80	int ret;
81
82	if (!vbus_draw)
83		return;
84
85	enabled = nop->vbus_draw_enabled;
86	if (mA) {
87		regulator_set_current_limit(vbus_draw, 0, 1000 * mA);
88		if (!enabled) {
89			ret = regulator_enable(vbus_draw);
90			if (ret < 0)
91				return;
92			nop->vbus_draw_enabled = 1;
93		}
94	} else {
95		if (enabled) {
96			ret = regulator_disable(vbus_draw);
97			if (ret < 0)
98				return;
99			nop->vbus_draw_enabled = 0;
100		}
101	}
102	nop->mA = mA;
103}
104
105
106static irqreturn_t nop_gpio_vbus_thread(int irq, void *data)
107{
108	struct usb_phy_generic *nop = data;
109	struct usb_otg *otg = nop->phy.otg;
110	int vbus, status;
111
112	vbus = gpiod_get_value(nop->gpiod_vbus);
113	if ((vbus ^ nop->vbus) == 0)
114		return IRQ_HANDLED;
115	nop->vbus = vbus;
116
117	if (vbus) {
118		status = USB_EVENT_VBUS;
119		otg->state = OTG_STATE_B_PERIPHERAL;
120		nop->phy.last_event = status;
121		usb_gadget_vbus_connect(otg->gadget);
122
123		/* drawing a "unit load" is *always* OK, except for OTG */
124		nop_set_vbus_draw(nop, 100);
125
126		atomic_notifier_call_chain(&nop->phy.notifier, status,
127					   otg->gadget);
128	} else {
129		nop_set_vbus_draw(nop, 0);
130
131		usb_gadget_vbus_disconnect(otg->gadget);
132		status = USB_EVENT_NONE;
133		otg->state = OTG_STATE_B_IDLE;
134		nop->phy.last_event = status;
135
136		atomic_notifier_call_chain(&nop->phy.notifier, status,
137					   otg->gadget);
138	}
139	return IRQ_HANDLED;
140}
141
142int usb_gen_phy_init(struct usb_phy *phy)
143{
144	struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
145
146	if (!IS_ERR(nop->vcc)) {
147		if (regulator_enable(nop->vcc))
148			dev_err(phy->dev, "Failed to enable power\n");
149	}
150
151	if (!IS_ERR(nop->clk))
152		clk_prepare_enable(nop->clk);
153
154	nop_reset(nop);
155
156	return 0;
157}
158EXPORT_SYMBOL_GPL(usb_gen_phy_init);
159
160void usb_gen_phy_shutdown(struct usb_phy *phy)
161{
162	struct usb_phy_generic *nop = dev_get_drvdata(phy->dev);
163
164	gpiod_set_value(nop->gpiod_reset, 1);
165
166	if (!IS_ERR(nop->clk))
167		clk_disable_unprepare(nop->clk);
168
169	if (!IS_ERR(nop->vcc)) {
170		if (regulator_disable(nop->vcc))
171			dev_err(phy->dev, "Failed to disable power\n");
172	}
173}
174EXPORT_SYMBOL_GPL(usb_gen_phy_shutdown);
175
176static int nop_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
177{
178	if (!otg)
179		return -ENODEV;
180
181	if (!gadget) {
182		otg->gadget = NULL;
183		return -ENODEV;
184	}
185
186	otg->gadget = gadget;
187	otg->state = OTG_STATE_B_IDLE;
188	return 0;
189}
190
191static int nop_set_host(struct usb_otg *otg, struct usb_bus *host)
192{
193	if (!otg)
194		return -ENODEV;
195
196	if (!host) {
197		otg->host = NULL;
198		return -ENODEV;
199	}
200
201	otg->host = host;
202	return 0;
203}
204
205int usb_phy_gen_create_phy(struct device *dev, struct usb_phy_generic *nop,
206		struct usb_phy_generic_platform_data *pdata)
207{
208	enum usb_phy_type type = USB_PHY_TYPE_USB2;
209	int err = 0;
210
211	u32 clk_rate = 0;
212	bool needs_vcc = false;
213
214	if (dev->of_node) {
215		struct device_node *node = dev->of_node;
216
217		if (of_property_read_u32(node, "clock-frequency", &clk_rate))
218			clk_rate = 0;
219
220		needs_vcc = of_property_read_bool(node, "vcc-supply");
221		nop->gpiod_reset = devm_gpiod_get_optional(dev, "reset");
222		err = PTR_ERR_OR_ZERO(nop->gpiod_reset);
223		if (!err) {
224			nop->gpiod_vbus = devm_gpiod_get_optional(dev,
225							 "vbus-detect");
226			err = PTR_ERR_OR_ZERO(nop->gpiod_vbus);
227		}
228	} else if (pdata) {
229		type = pdata->type;
230		clk_rate = pdata->clk_rate;
231		needs_vcc = pdata->needs_vcc;
232		if (gpio_is_valid(pdata->gpio_reset)) {
233			err = devm_gpio_request_one(dev, pdata->gpio_reset,
234						    GPIOF_ACTIVE_LOW,
235						    dev_name(dev));
236			if (!err)
237				nop->gpiod_reset =
238					gpio_to_desc(pdata->gpio_reset);
239		}
240		nop->gpiod_vbus = pdata->gpiod_vbus;
241	}
242
243	if (err == -EPROBE_DEFER)
244		return -EPROBE_DEFER;
245	if (err) {
246		dev_err(dev, "Error requesting RESET or VBUS GPIO\n");
247		return err;
248	}
249	if (nop->gpiod_reset)
250		gpiod_direction_output(nop->gpiod_reset, 1);
251
252	nop->phy.otg = devm_kzalloc(dev, sizeof(*nop->phy.otg),
253			GFP_KERNEL);
254	if (!nop->phy.otg)
255		return -ENOMEM;
256
257	nop->clk = devm_clk_get(dev, "main_clk");
258	if (IS_ERR(nop->clk)) {
259		dev_dbg(dev, "Can't get phy clock: %ld\n",
260					PTR_ERR(nop->clk));
261	}
262
263	if (!IS_ERR(nop->clk) && clk_rate) {
264		err = clk_set_rate(nop->clk, clk_rate);
265		if (err) {
266			dev_err(dev, "Error setting clock rate\n");
267			return err;
268		}
269	}
270
271	nop->vcc = devm_regulator_get(dev, "vcc");
272	if (IS_ERR(nop->vcc)) {
273		dev_dbg(dev, "Error getting vcc regulator: %ld\n",
274					PTR_ERR(nop->vcc));
275		if (needs_vcc)
276			return -EPROBE_DEFER;
277	}
278
279	nop->dev		= dev;
280	nop->phy.dev		= nop->dev;
281	nop->phy.label		= "nop-xceiv";
282	nop->phy.set_suspend	= nop_set_suspend;
283	nop->phy.type		= type;
284
285	nop->phy.otg->state		= OTG_STATE_UNDEFINED;
286	nop->phy.otg->usb_phy		= &nop->phy;
287	nop->phy.otg->set_host		= nop_set_host;
288	nop->phy.otg->set_peripheral	= nop_set_peripheral;
289
290	return 0;
291}
292EXPORT_SYMBOL_GPL(usb_phy_gen_create_phy);
293
294static int usb_phy_generic_probe(struct platform_device *pdev)
295{
296	struct device *dev = &pdev->dev;
297	struct usb_phy_generic	*nop;
298	int err;
299
300	nop = devm_kzalloc(dev, sizeof(*nop), GFP_KERNEL);
301	if (!nop)
302		return -ENOMEM;
303
304	err = usb_phy_gen_create_phy(dev, nop, dev_get_platdata(&pdev->dev));
305	if (err)
306		return err;
307	if (nop->gpiod_vbus) {
308		err = devm_request_threaded_irq(&pdev->dev,
309						gpiod_to_irq(nop->gpiod_vbus),
310						NULL, nop_gpio_vbus_thread,
311						VBUS_IRQ_FLAGS, "vbus_detect",
312						nop);
313		if (err) {
314			dev_err(&pdev->dev, "can't request irq %i, err: %d\n",
315				gpiod_to_irq(nop->gpiod_vbus), err);
316			return err;
317		}
318	}
319
320	nop->phy.init		= usb_gen_phy_init;
321	nop->phy.shutdown	= usb_gen_phy_shutdown;
322
323	err = usb_add_phy_dev(&nop->phy);
324	if (err) {
325		dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
326			err);
327		return err;
328	}
329
330	platform_set_drvdata(pdev, nop);
331
332	return 0;
333}
334
335static int usb_phy_generic_remove(struct platform_device *pdev)
336{
337	struct usb_phy_generic *nop = platform_get_drvdata(pdev);
338
339	usb_remove_phy(&nop->phy);
340
341	return 0;
342}
343
344static const struct of_device_id nop_xceiv_dt_ids[] = {
345	{ .compatible = "usb-nop-xceiv" },
346	{ }
347};
348
349MODULE_DEVICE_TABLE(of, nop_xceiv_dt_ids);
350
351static struct platform_driver usb_phy_generic_driver = {
352	.probe		= usb_phy_generic_probe,
353	.remove		= usb_phy_generic_remove,
354	.driver		= {
355		.name	= "usb_phy_generic",
356		.of_match_table = nop_xceiv_dt_ids,
357	},
358};
359
360static int __init usb_phy_generic_init(void)
361{
362	return platform_driver_register(&usb_phy_generic_driver);
363}
364subsys_initcall(usb_phy_generic_init);
365
366static void __exit usb_phy_generic_exit(void)
367{
368	platform_driver_unregister(&usb_phy_generic_driver);
369}
370module_exit(usb_phy_generic_exit);
371
372MODULE_ALIAS("platform:usb_phy_generic");
373MODULE_AUTHOR("Texas Instruments Inc");
374MODULE_DESCRIPTION("NOP USB Transceiver driver");
375MODULE_LICENSE("GPL");
376