1/*
2 * phy.c -- USB phy handling
3 *
4 * Copyright (C) 2004-2013 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/export.h>
13#include <linux/err.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18
19#include <linux/usb/phy.h>
20
21static LIST_HEAD(phy_list);
22static LIST_HEAD(phy_bind_list);
23static DEFINE_SPINLOCK(phy_lock);
24
25static struct usb_phy *__usb_find_phy(struct list_head *list,
26	enum usb_phy_type type)
27{
28	struct usb_phy  *phy = NULL;
29
30	list_for_each_entry(phy, list, head) {
31		if (phy->type != type)
32			continue;
33
34		return phy;
35	}
36
37	return ERR_PTR(-ENODEV);
38}
39
40static struct usb_phy *__usb_find_phy_dev(struct device *dev,
41	struct list_head *list, u8 index)
42{
43	struct usb_phy_bind *phy_bind = NULL;
44
45	list_for_each_entry(phy_bind, list, list) {
46		if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
47				phy_bind->index == index) {
48			if (phy_bind->phy)
49				return phy_bind->phy;
50			else
51				return ERR_PTR(-EPROBE_DEFER);
52		}
53	}
54
55	return ERR_PTR(-ENODEV);
56}
57
58static struct usb_phy *__of_usb_find_phy(struct device_node *node)
59{
60	struct usb_phy  *phy;
61
62	if (!of_device_is_available(node))
63		return ERR_PTR(-ENODEV);
64
65	list_for_each_entry(phy, &phy_list, head) {
66		if (node != phy->dev->of_node)
67			continue;
68
69		return phy;
70	}
71
72	return ERR_PTR(-EPROBE_DEFER);
73}
74
75static void devm_usb_phy_release(struct device *dev, void *res)
76{
77	struct usb_phy *phy = *(struct usb_phy **)res;
78
79	usb_put_phy(phy);
80}
81
82static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
83{
84	struct usb_phy **phy = res;
85
86	return *phy == match_data;
87}
88
89/**
90 * devm_usb_get_phy - find the USB PHY
91 * @dev - device that requests this phy
92 * @type - the type of the phy the controller requires
93 *
94 * Gets the phy using usb_get_phy(), and associates a device with it using
95 * devres. On driver detach, release function is invoked on the devres data,
96 * then, devres data is freed.
97 *
98 * For use by USB host and peripheral drivers.
99 */
100struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
101{
102	struct usb_phy **ptr, *phy;
103
104	ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
105	if (!ptr)
106		return ERR_PTR(-ENOMEM);
107
108	phy = usb_get_phy(type);
109	if (!IS_ERR(phy)) {
110		*ptr = phy;
111		devres_add(dev, ptr);
112	} else
113		devres_free(ptr);
114
115	return phy;
116}
117EXPORT_SYMBOL_GPL(devm_usb_get_phy);
118
119/**
120 * usb_get_phy - find the USB PHY
121 * @type - the type of the phy the controller requires
122 *
123 * Returns the phy driver, after getting a refcount to it; or
124 * -ENODEV if there is no such phy.  The caller is responsible for
125 * calling usb_put_phy() to release that count.
126 *
127 * For use by USB host and peripheral drivers.
128 */
129struct usb_phy *usb_get_phy(enum usb_phy_type type)
130{
131	struct usb_phy	*phy = NULL;
132	unsigned long	flags;
133
134	spin_lock_irqsave(&phy_lock, flags);
135
136	phy = __usb_find_phy(&phy_list, type);
137	if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
138		pr_debug("PHY: unable to find transceiver of type %s\n",
139			usb_phy_type_string(type));
140		if (!IS_ERR(phy))
141			phy = ERR_PTR(-ENODEV);
142
143		goto err0;
144	}
145
146	get_device(phy->dev);
147
148err0:
149	spin_unlock_irqrestore(&phy_lock, flags);
150
151	return phy;
152}
153EXPORT_SYMBOL_GPL(usb_get_phy);
154
155/**
156 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
157 * @dev - device that requests this phy
158 * @phandle - name of the property holding the phy phandle value
159 * @index - the index of the phy
160 *
161 * Returns the phy driver associated with the given phandle value,
162 * after getting a refcount to it, -ENODEV if there is no such phy or
163 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
164 * not yet loaded. While at that, it also associates the device with
165 * the phy using devres. On driver detach, release function is invoked
166 * on the devres data, then, devres data is freed.
167 *
168 * For use by USB host and peripheral drivers.
169 */
170struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
171	const char *phandle, u8 index)
172{
173	struct usb_phy	*phy = ERR_PTR(-ENOMEM), **ptr;
174	unsigned long	flags;
175	struct device_node *node;
176
177	if (!dev->of_node) {
178		dev_dbg(dev, "device does not have a device node entry\n");
179		return ERR_PTR(-EINVAL);
180	}
181
182	node = of_parse_phandle(dev->of_node, phandle, index);
183	if (!node) {
184		dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
185			dev->of_node->full_name);
186		return ERR_PTR(-ENODEV);
187	}
188
189	ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
190	if (!ptr) {
191		dev_dbg(dev, "failed to allocate memory for devres\n");
192		goto err0;
193	}
194
195	spin_lock_irqsave(&phy_lock, flags);
196
197	phy = __of_usb_find_phy(node);
198	if (IS_ERR(phy)) {
199		devres_free(ptr);
200		goto err1;
201	}
202
203	if (!try_module_get(phy->dev->driver->owner)) {
204		phy = ERR_PTR(-ENODEV);
205		devres_free(ptr);
206		goto err1;
207	}
208
209	*ptr = phy;
210	devres_add(dev, ptr);
211
212	get_device(phy->dev);
213
214err1:
215	spin_unlock_irqrestore(&phy_lock, flags);
216
217err0:
218	of_node_put(node);
219
220	return phy;
221}
222EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
223
224/**
225 * usb_get_phy_dev - find the USB PHY
226 * @dev - device that requests this phy
227 * @index - the index of the phy
228 *
229 * Returns the phy driver, after getting a refcount to it; or
230 * -ENODEV if there is no such phy.  The caller is responsible for
231 * calling usb_put_phy() to release that count.
232 *
233 * For use by USB host and peripheral drivers.
234 */
235struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
236{
237	struct usb_phy	*phy = NULL;
238	unsigned long	flags;
239
240	spin_lock_irqsave(&phy_lock, flags);
241
242	phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
243	if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
244		dev_dbg(dev, "unable to find transceiver\n");
245		if (!IS_ERR(phy))
246			phy = ERR_PTR(-ENODEV);
247
248		goto err0;
249	}
250
251	get_device(phy->dev);
252
253err0:
254	spin_unlock_irqrestore(&phy_lock, flags);
255
256	return phy;
257}
258EXPORT_SYMBOL_GPL(usb_get_phy_dev);
259
260/**
261 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
262 * @dev - device that requests this phy
263 * @index - the index of the phy
264 *
265 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
266 * devres. On driver detach, release function is invoked on the devres data,
267 * then, devres data is freed.
268 *
269 * For use by USB host and peripheral drivers.
270 */
271struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
272{
273	struct usb_phy **ptr, *phy;
274
275	ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
276	if (!ptr)
277		return NULL;
278
279	phy = usb_get_phy_dev(dev, index);
280	if (!IS_ERR(phy)) {
281		*ptr = phy;
282		devres_add(dev, ptr);
283	} else
284		devres_free(ptr);
285
286	return phy;
287}
288EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
289
290/**
291 * devm_usb_put_phy - release the USB PHY
292 * @dev - device that wants to release this phy
293 * @phy - the phy returned by devm_usb_get_phy()
294 *
295 * destroys the devres associated with this phy and invokes usb_put_phy
296 * to release the phy.
297 *
298 * For use by USB host and peripheral drivers.
299 */
300void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
301{
302	int r;
303
304	r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
305	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
306}
307EXPORT_SYMBOL_GPL(devm_usb_put_phy);
308
309/**
310 * usb_put_phy - release the USB PHY
311 * @x: the phy returned by usb_get_phy()
312 *
313 * Releases a refcount the caller received from usb_get_phy().
314 *
315 * For use by USB host and peripheral drivers.
316 */
317void usb_put_phy(struct usb_phy *x)
318{
319	if (x) {
320		struct module *owner = x->dev->driver->owner;
321
322		put_device(x->dev);
323		module_put(owner);
324	}
325}
326EXPORT_SYMBOL_GPL(usb_put_phy);
327
328/**
329 * usb_add_phy - declare the USB PHY
330 * @x: the USB phy to be used; or NULL
331 * @type - the type of this PHY
332 *
333 * This call is exclusively for use by phy drivers, which
334 * coordinate the activities of drivers for host and peripheral
335 * controllers, and in some cases for VBUS current regulation.
336 */
337int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
338{
339	int		ret = 0;
340	unsigned long	flags;
341	struct usb_phy	*phy;
342
343	if (x->type != USB_PHY_TYPE_UNDEFINED) {
344		dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
345		return -EINVAL;
346	}
347
348	ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
349
350	spin_lock_irqsave(&phy_lock, flags);
351
352	list_for_each_entry(phy, &phy_list, head) {
353		if (phy->type == type) {
354			ret = -EBUSY;
355			dev_err(x->dev, "transceiver type %s already exists\n",
356						usb_phy_type_string(type));
357			goto out;
358		}
359	}
360
361	x->type = type;
362	list_add_tail(&x->head, &phy_list);
363
364out:
365	spin_unlock_irqrestore(&phy_lock, flags);
366	return ret;
367}
368EXPORT_SYMBOL_GPL(usb_add_phy);
369
370/**
371 * usb_add_phy_dev - declare the USB PHY
372 * @x: the USB phy to be used; or NULL
373 *
374 * This call is exclusively for use by phy drivers, which
375 * coordinate the activities of drivers for host and peripheral
376 * controllers, and in some cases for VBUS current regulation.
377 */
378int usb_add_phy_dev(struct usb_phy *x)
379{
380	struct usb_phy_bind *phy_bind;
381	unsigned long flags;
382
383	if (!x->dev) {
384		dev_err(x->dev, "no device provided for PHY\n");
385		return -EINVAL;
386	}
387
388	ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
389
390	spin_lock_irqsave(&phy_lock, flags);
391	list_for_each_entry(phy_bind, &phy_bind_list, list)
392		if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
393			phy_bind->phy = x;
394
395	list_add_tail(&x->head, &phy_list);
396
397	spin_unlock_irqrestore(&phy_lock, flags);
398	return 0;
399}
400EXPORT_SYMBOL_GPL(usb_add_phy_dev);
401
402/**
403 * usb_remove_phy - remove the OTG PHY
404 * @x: the USB OTG PHY to be removed;
405 *
406 * This reverts the effects of usb_add_phy
407 */
408void usb_remove_phy(struct usb_phy *x)
409{
410	unsigned long	flags;
411	struct usb_phy_bind *phy_bind;
412
413	spin_lock_irqsave(&phy_lock, flags);
414	if (x) {
415		list_for_each_entry(phy_bind, &phy_bind_list, list)
416			if (phy_bind->phy == x)
417				phy_bind->phy = NULL;
418		list_del(&x->head);
419	}
420	spin_unlock_irqrestore(&phy_lock, flags);
421}
422EXPORT_SYMBOL_GPL(usb_remove_phy);
423
424/**
425 * usb_bind_phy - bind the phy and the controller that uses the phy
426 * @dev_name: the device name of the device that will bind to the phy
427 * @index: index to specify the port number
428 * @phy_dev_name: the device name of the phy
429 *
430 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
431 * be used when the phy driver registers the phy and when the controller
432 * requests this phy.
433 *
434 * To be used by platform specific initialization code.
435 */
436int usb_bind_phy(const char *dev_name, u8 index,
437				const char *phy_dev_name)
438{
439	struct usb_phy_bind *phy_bind;
440	unsigned long flags;
441
442	phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
443	if (!phy_bind)
444		return -ENOMEM;
445
446	phy_bind->dev_name = dev_name;
447	phy_bind->phy_dev_name = phy_dev_name;
448	phy_bind->index = index;
449
450	spin_lock_irqsave(&phy_lock, flags);
451	list_add_tail(&phy_bind->list, &phy_bind_list);
452	spin_unlock_irqrestore(&phy_lock, flags);
453
454	return 0;
455}
456EXPORT_SYMBOL_GPL(usb_bind_phy);
457
458/**
459 * usb_phy_set_event - set event to phy event
460 * @x: the phy returned by usb_get_phy();
461 *
462 * This sets event to phy event
463 */
464void usb_phy_set_event(struct usb_phy *x, unsigned long event)
465{
466	x->last_event = event;
467}
468EXPORT_SYMBOL_GPL(usb_phy_set_event);
469