Skip to content

Commit 5d3c28b

Browse files
kishonFelipe Balbi
authored andcommitted
usb: otg: add device tree support to otg library
Added an API devm_usb_get_phy_by_phandle(), to get usb phy by passing a device node phandle value. This function will return a pointer to the phy on success, -EPROBE_DEFER if there is a device_node for the phandle, but the phy has not been added, or a ERR_PTR() otherwise. Cc: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com> Signed-off-by: Felipe Balbi <balbi@ti.com>
1 parent 0fa4fab commit 5d3c28b

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎drivers/usb/otg/otg.c‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include <linux/export.h>
1414
#include <linux/err.h>
1515
#include <linux/device.h>
16+
#include <linux/module.h>
1617
#include <linux/slab.h>
18+
#include <linux/of.h>
1719

1820
#include <linux/usb/otg.h>
1921

@@ -54,6 +56,20 @@ static struct usb_phy *__usb_find_phy_dev(struct device *dev,
5456
return ERR_PTR(-ENODEV);
5557
}
5658

59+
static struct usb_phy *__of_usb_find_phy(struct device_node *node)
60+
{
61+
struct usb_phy *phy;
62+
63+
list_for_each_entry(phy, &phy_list, head) {
64+
if (node != phy->dev->of_node)
65+
continue;
66+
67+
return phy;
68+
}
69+
70+
return ERR_PTR(-ENODEV);
71+
}
72+
5773
static void devm_usb_phy_release(struct device *dev, void *res)
5874
{
5975
struct usb_phy *phy = *(struct usb_phy **)res;
@@ -129,6 +145,70 @@ struct usb_phy *usb_get_phy(enum usb_phy_type type)
129145
}
130146
EXPORT_SYMBOL(usb_get_phy);
131147

148+
/**
149+
* devm_usb_get_phy_by_phandle - find the USB PHY by phandle
150+
* @dev - device that requests this phy
151+
* @phandle - name of the property holding the phy phandle value
152+
* @index - the index of the phy
153+
*
154+
* Returns the phy driver associated with the given phandle value,
155+
* after getting a refcount to it, -ENODEV if there is no such phy or
156+
* -EPROBE_DEFER if there is a phandle to the phy, but the device is
157+
* not yet loaded. While at that, it also associates the device with
158+
* the phy using devres. On driver detach, release function is invoked
159+
* on the devres data, then, devres data is freed.
160+
*
161+
* For use by USB host and peripheral drivers.
162+
*/
163+
struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
164+
const char *phandle, u8 index)
165+
{
166+
struct usb_phy *phy = ERR_PTR(-ENOMEM), **ptr;
167+
unsigned long flags;
168+
struct device_node *node;
169+
170+
if (!dev->of_node) {
171+
dev_dbg(dev, "device does not have a device node entry\n");
172+
return ERR_PTR(-EINVAL);
173+
}
174+
175+
node = of_parse_phandle(dev->of_node, phandle, index);
176+
if (!node) {
177+
dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
178+
dev->of_node->full_name);
179+
return ERR_PTR(-ENODEV);
180+
}
181+
182+
ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
183+
if (!ptr) {
184+
dev_dbg(dev, "failed to allocate memory for devres\n");
185+
goto err0;
186+
}
187+
188+
spin_lock_irqsave(&phy_lock, flags);
189+
190+
phy = __of_usb_find_phy(node);
191+
if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
192+
phy = ERR_PTR(-EPROBE_DEFER);
193+
devres_free(ptr);
194+
goto err1;
195+
}
196+
197+
*ptr = phy;
198+
devres_add(dev, ptr);
199+
200+
get_device(phy->dev);
201+
202+
err1:
203+
spin_unlock_irqrestore(&phy_lock, flags);
204+
205+
err0:
206+
of_node_put(node);
207+
208+
return phy;
209+
}
210+
EXPORT_SYMBOL(devm_usb_get_phy_by_phandle);
211+
132212
/**
133213
* usb_get_phy_dev - find the USB PHY
134214
* @dev - device that requests this phy

‎include/linux/usb/phy.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ extern struct usb_phy *devm_usb_get_phy(struct device *dev,
167167
enum usb_phy_type type);
168168
extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
169169
extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
170+
extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
171+
const char *phandle, u8 index);
170172
extern void usb_put_phy(struct usb_phy *);
171173
extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
172174
extern int usb_bind_phy(const char *dev_name, u8 index,
@@ -193,6 +195,12 @@ static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
193195
return NULL;
194196
}
195197

198+
static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
199+
const char *phandle, u8 index)
200+
{
201+
return NULL;
202+
}
203+
196204
static inline void usb_put_phy(struct usb_phy *x)
197205
{
198206
}

0 commit comments

Comments
 (0)