2015-04-01 20:48:33

by Arun Ramamurthy

[permalink] [raw]
Subject: [PATCH v1 0/3] Add devm_of_phy_get_by_index and update platform drivers

This patch set adds a new API to get phy by index when multiple
phys are present. This patch is based on discussion with Arnd Bergmann
about dt bindings for multiple phys.

History:
v1:
- Removed null pointers on Dmitry's suggestion
- Improved documentation in commit messages
- Exported new phy api

Arun Ramamurthy (3):
phy: core: Add devm_of_phy_get_by_index to phy-core
usb: ehci-platform: Use devm_of_phy_get_by_index
usb: ohci-platform: Use devm_of_phy_get_by_index

drivers/phy/phy-core.c | 32 ++++++++++++++++++
drivers/usb/host/ehci-platform.c | 70 ++++++++++++++--------------------------
drivers/usb/host/ohci-platform.c | 70 ++++++++++++++--------------------------
include/linux/phy/phy.h | 2 ++
4 files changed, 84 insertions(+), 90 deletions(-)

--
2.3.4


2015-04-01 20:48:51

by Arun Ramamurthy

[permalink] [raw]
Subject: [PATCH v1 1/3] phy: core: Add devm_of_phy_get_by_index to phy-core

Some generic drivers, such as ehci, may use multiple phys and for such
drivers referencing phy(s) by name(s) does not make sense. Instead of
inventing new naming schemes and using custom code to iterate through them,
such drivers are better of using nameless phy bindings and using this newly
introduced API to iterate through them.

Signed-off-by: Arun Ramamurthy <[email protected]>
Reviewed-by: Ray Jui <[email protected]>
Reviewed-by: Scott Branden <[email protected]>
---
drivers/phy/phy-core.c | 32 ++++++++++++++++++++++++++++++++
include/linux/phy/phy.h | 2 ++
2 files changed, 34 insertions(+)

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index 3791838..964a84d 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -623,6 +623,38 @@ struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
EXPORT_SYMBOL_GPL(devm_of_phy_get);

/**
+ * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
+ * @dev: device that requests this phy
+ * @np: node containing the phy
+ * @index: index of the phy
+ *
+ * Gets the phy using _of_phy_get(), and associates a device with it using
+ * devres. On driver detach, release function is invoked on the devres data,
+ * then, devres data is freed.
+ *
+ */
+struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
+ int index)
+{
+ struct phy **ptr, *phy;
+
+ ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
+ if (!ptr)
+ return ERR_PTR(-ENOMEM);
+
+ phy = _of_phy_get(np, index);
+ if (!IS_ERR(phy)) {
+ *ptr = phy;
+ devres_add(dev, ptr);
+ } else {
+ devres_free(ptr);
+ }
+
+ return phy;
+}
+EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
+
+/**
* phy_create() - create a new phy
* @dev: device that is creating the new phy
* @node: device node of the phy
diff --git a/include/linux/phy/phy.h b/include/linux/phy/phy.h
index a0197fa..ae2ffaf 100644
--- a/include/linux/phy/phy.h
+++ b/include/linux/phy/phy.h
@@ -133,6 +133,8 @@ struct phy *devm_phy_get(struct device *dev, const char *string);
struct phy *devm_phy_optional_get(struct device *dev, const char *string);
struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
const char *con_id);
+struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
+ int index);
void phy_put(struct phy *phy);
void devm_phy_put(struct device *dev, struct phy *phy);
struct phy *of_phy_get(struct device_node *np, const char *con_id);
--
2.3.4

2015-04-01 20:48:55

by Arun Ramamurthy

[permalink] [raw]
Subject: [PATCH v1 2/3] usb: ehci-platform: Use devm_of_phy_get_by_index

Getting phys by index instead of phy names so that we do
not have to create a naming scheme when multiple phys
are present

Signed-off-by: Arun Ramamurthy <[email protected]>
Reviewed-by: Ray Jui <[email protected]>
Reviewed-by: Scott Branden <[email protected]>
---
drivers/usb/host/ehci-platform.c | 70 ++++++++++++++--------------------------
1 file changed, 25 insertions(+), 45 deletions(-)

diff --git a/drivers/usb/host/ehci-platform.c b/drivers/usb/host/ehci-platform.c
index d8a75a5..a7563b9 100644
--- a/drivers/usb/host/ehci-platform.c
+++ b/drivers/usb/host/ehci-platform.c
@@ -88,15 +88,13 @@ static int ehci_platform_power_on(struct platform_device *dev)
}

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- if (priv->phys[phy_num]) {
- ret = phy_init(priv->phys[phy_num]);
- if (ret)
- goto err_exit_phy;
- ret = phy_power_on(priv->phys[phy_num]);
- if (ret) {
- phy_exit(priv->phys[phy_num]);
- goto err_exit_phy;
- }
+ ret = phy_init(priv->phys[phy_num]);
+ if (ret)
+ goto err_exit_phy;
+ ret = phy_power_on(priv->phys[phy_num]);
+ if (ret) {
+ phy_exit(priv->phys[phy_num]);
+ goto err_exit_phy;
}
}

@@ -104,10 +102,8 @@ static int ehci_platform_power_on(struct platform_device *dev)

err_exit_phy:
while (--phy_num >= 0) {
- if (priv->phys[phy_num]) {
- phy_power_off(priv->phys[phy_num]);
- phy_exit(priv->phys[phy_num]);
- }
+ phy_power_off(priv->phys[phy_num]);
+ phy_exit(priv->phys[phy_num]);
}
err_disable_clks:
while (--clk >= 0)
@@ -123,10 +119,8 @@ static void ehci_platform_power_off(struct platform_device *dev)
int clk, phy_num;

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- if (priv->phys[phy_num]) {
- phy_power_off(priv->phys[phy_num]);
- phy_exit(priv->phys[phy_num]);
- }
+ phy_power_off(priv->phys[phy_num]);
+ phy_exit(priv->phys[phy_num]);
}

for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
@@ -154,7 +148,6 @@ static int ehci_platform_probe(struct platform_device *dev)
struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
struct ehci_platform_priv *priv;
struct ehci_hcd *ehci;
- const char *phy_name;
int err, irq, phy_num, clk = 0;

if (usb_disabled())
@@ -204,36 +197,23 @@ static int ehci_platform_probe(struct platform_device *dev)

priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
"phys", "#phy-cells");
- priv->num_phys = priv->num_phys > 0 ? priv->num_phys : 1;

- priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
- sizeof(struct phy *), GFP_KERNEL);
- if (!priv->phys)
- return -ENOMEM;
+ if (priv->num_phys > 0) {
+ priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
+ sizeof(struct phy *), GFP_KERNEL);
+ if (!priv->phys)
+ return -ENOMEM;
+ } else
+ priv->num_phys = 0;

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- err = of_property_read_string_index(
- dev->dev.of_node,
- "phy-names", phy_num,
- &phy_name);
-
- if (err < 0) {
- if (priv->num_phys > 1) {
- dev_err(&dev->dev, "phy-names not provided");
- goto err_put_hcd;
- } else
- phy_name = "usb";
- }
-
- priv->phys[phy_num] = devm_phy_get(&dev->dev,
- phy_name);
- if (IS_ERR(priv->phys[phy_num])) {
- err = PTR_ERR(priv->phys[phy_num]);
- if ((priv->num_phys > 1) ||
- (err == -EPROBE_DEFER))
- goto err_put_hcd;
- priv->phys[phy_num] = NULL;
- }
+ priv->phys[phy_num] = devm_of_phy_get_by_index(&dev->dev
+ , dev->dev.of_node
+ , phy_num);
+ if (IS_ERR(priv->phys[phy_num])) {
+ err = PTR_ERR(priv->phys[phy_num]);
+ goto err_put_hcd;
+ }
}

for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
--
2.3.4

2015-04-01 20:48:59

by Arun Ramamurthy

[permalink] [raw]
Subject: [PATCH v1 3/3] usb: ohci-platform: Use devm_of_phy_get_by_index

Getting phys by index instead of phy names so that we do
not have to create a naming scheme when multiple phys are present

Signed-off-by: Arun Ramamurthy <[email protected]>
Reviewed-by: Ray Jui <[email protected]>
Reviewed-by: Scott Branden <[email protected]>
---
drivers/usb/host/ohci-platform.c | 70 ++++++++++++++--------------------------
1 file changed, 25 insertions(+), 45 deletions(-)

diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
index 185ceee..0f8edf6 100644
--- a/drivers/usb/host/ohci-platform.c
+++ b/drivers/usb/host/ohci-platform.c
@@ -57,15 +57,13 @@ static int ohci_platform_power_on(struct platform_device *dev)
}

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- if (priv->phys[phy_num]) {
- ret = phy_init(priv->phys[phy_num]);
- if (ret)
- goto err_exit_phy;
- ret = phy_power_on(priv->phys[phy_num]);
- if (ret) {
- phy_exit(priv->phys[phy_num]);
- goto err_exit_phy;
- }
+ ret = phy_init(priv->phys[phy_num]);
+ if (ret)
+ goto err_exit_phy;
+ ret = phy_power_on(priv->phys[phy_num]);
+ if (ret) {
+ phy_exit(priv->phys[phy_num]);
+ goto err_exit_phy;
}
}

@@ -73,10 +71,8 @@ static int ohci_platform_power_on(struct platform_device *dev)

err_exit_phy:
while (--phy_num >= 0) {
- if (priv->phys[phy_num]) {
- phy_power_off(priv->phys[phy_num]);
- phy_exit(priv->phys[phy_num]);
- }
+ phy_power_off(priv->phys[phy_num]);
+ phy_exit(priv->phys[phy_num]);
}
err_disable_clks:
while (--clk >= 0)
@@ -92,10 +88,8 @@ static void ohci_platform_power_off(struct platform_device *dev)
int clk, phy_num;

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- if (priv->phys[phy_num]) {
- phy_power_off(priv->phys[phy_num]);
- phy_exit(priv->phys[phy_num]);
- }
+ phy_power_off(priv->phys[phy_num]);
+ phy_exit(priv->phys[phy_num]);
}

for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
@@ -123,7 +117,6 @@ static int ohci_platform_probe(struct platform_device *dev)
struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
struct ohci_platform_priv *priv;
struct ohci_hcd *ohci;
- const char *phy_name;
int err, irq, phy_num, clk = 0;

if (usb_disabled())
@@ -174,36 +167,23 @@ static int ohci_platform_probe(struct platform_device *dev)

priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
"phys", "#phy-cells");
- priv->num_phys = priv->num_phys > 0 ? priv->num_phys : 1;

- priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
- sizeof(struct phy *), GFP_KERNEL);
- if (!priv->phys)
- return -ENOMEM;
+ if (priv->num_phys > 0) {
+ priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
+ sizeof(struct phy *), GFP_KERNEL);
+ if (!priv->phys)
+ return -ENOMEM;
+ } else
+ priv->num_phys = 0;

for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
- err = of_property_read_string_index(
- dev->dev.of_node,
- "phy-names", phy_num,
- &phy_name);
-
- if (err < 0) {
- if (priv->num_phys > 1) {
- dev_err(&dev->dev, "phy-names not provided");
- goto err_put_hcd;
- } else
- phy_name = "usb";
- }
-
- priv->phys[phy_num] = devm_phy_get(&dev->dev,
- phy_name);
- if (IS_ERR(priv->phys[phy_num])) {
- err = PTR_ERR(priv->phys[phy_num]);
- if ((priv->num_phys > 1) ||
- (err == -EPROBE_DEFER))
- goto err_put_hcd;
- priv->phys[phy_num] = NULL;
- }
+ priv->phys[phy_num] = devm_of_phy_get_by_index(&dev->dev
+ , dev->dev.of_node
+ , phy_num);
+ if (IS_ERR(priv->phys[phy_num])) {
+ err = PTR_ERR(priv->phys[phy_num]);
+ goto err_put_hcd;
+ }
}

for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
--
2.3.4

2015-04-02 14:21:51

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH v1 2/3] usb: ehci-platform: Use devm_of_phy_get_by_index

On Wed, 1 Apr 2015, Arun Ramamurthy wrote:

> Getting phys by index instead of phy names so that we do
> not have to create a naming scheme when multiple phys
> are present
>
> Signed-off-by: Arun Ramamurthy <[email protected]>
> Reviewed-by: Ray Jui <[email protected]>
> Reviewed-by: Scott Branden <[email protected]>


> @@ -204,36 +197,23 @@ static int ehci_platform_probe(struct platform_device *dev)
>
> priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
> "phys", "#phy-cells");
> - priv->num_phys = priv->num_phys > 0 ? priv->num_phys : 1;
>
> - priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
> - sizeof(struct phy *), GFP_KERNEL);
> - if (!priv->phys)
> - return -ENOMEM;
> + if (priv->num_phys > 0) {
> + priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
> + sizeof(struct phy *), GFP_KERNEL);
> + if (!priv->phys)
> + return -ENOMEM;
> + } else
> + priv->num_phys = 0;

This last line isn't needed. A negative value for priv->num_phys will
work just as well as 0.

> + priv->phys[phy_num] = devm_of_phy_get_by_index(&dev->dev
> + , dev->dev.of_node
> + , phy_num);

What's with the funny placement of the commas? Are you trying to avoid
breaking the 80-column rule? It should look like this:

priv->phys[phy_num] = devm_of_phy_get_by_index(
&dev->dev, dev->dev.of_node, phy_num);

When you fix this, you can add:

Acked-by: Alan Stern <[email protected]>

The same comments apply to your 3/3 patch.

Alan Stern