Skip to contentSkip to conversation

Generic PHY Framework

PATCH Superseded Kishon Vijay Abraham I 6 participants 24 messages Apr 3, 2013 – Apr 8, 2013

Subsystems (11)

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v5 cover Generic PHY Framework

Added a generic PHY framework that provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

This framework will be of use only to devices that uses external PHY (PHY functionality is not embedded within the controller).

The intention of creating this framework is to bring the phy drivers spread all over the Linux kernel to drivers/phy to increase code re-use and to increase code maintainability.

Comments to make PHY as bus wasn't done because PHY devices can be part of other bus and making a same device attached to multiple bus leads to bad design.

Making omap-usb2 and twl4030 to use this framework is provided as a sample.

This patch series is developed on linus tree HEAD. Once the patch series gets finalised I'll resend omap-usb2 and twl4030 part based on Felipe's tree.

Changes from v4:
* removed of_phy_get_with_args/devm_of_phy_get_with_args. Now the *phy providers* should use their custom implementation of of_xlate or use of_phy_xlate to get *phy instance* from *phy providers*.
* Added of_phy_xlate to be used by *phy providers* if it provides only one PHY.
* changed phy_core from having subsys_initcall to module_init.
* other minor fixes.

Changes from v3:
* Changed the return value of PHY APIs to ENOSYS
* Added APIs of_phy_get_with_args/devm_of_phy_get_with_args to support getting PHYs if the same IP implements multiple PHYs.
* modified phy_bind API so that the binding information can now be _updated_. In effect of this removed the binding information added in board files and added only in usb-musb.c. If a particular board uses a different phy binding, it can update it in board file after usb_musb_init().
* Added Documentation/devicetree/bindings/phy/phy-bindings.txt for dt binding information.

Changes from v2:
* removed phy_descriptor structure completely so changed the APIs which were taking phy_descriptor as parameters
* Added 2 more APIs *of_phy_get_byname* and *devm_of_phy_get_byname* to be used by PHY user drivers which has *phy* and *phy-names* binding in the dt data
* Fixed a few typos
* Removed phy_list and we now use class_dev_iter_init, class_dev_iter_next and class_dev_iter_exit for traversing through the phy list. (Note we still need phy_bind list and phy_bind_mutex).
* Changed the sysfs entry name from *bind* to *phy_bind*.

Changes from v1:
* Added Documentation for the PHY framework
* Added few more APIs mostly w.r.t devres
* Modified omap-usb2 and twl4030 to make use of the new framework

Did USB enumeration testing in panda and beagle.

Kishon Vijay Abraham I (6):
drivers: phy: add generic PHY framework
usb: phy: omap-usb2: use the new generic PHY framework
usb: otg: twl4030: use the new generic PHY framework
ARM: OMAP: USB: Add phy binding information
ARM: dts: omap: update usb_otg_hs data
usb: musb: omap2430: use the new generic PHY framework

 Documentation/ABI/testing/sysfs-class-phy          |   15 +
 .../devicetree/bindings/phy/phy-bindings.txt       |   67 +++
 Documentation/devicetree/bindings/usb/omap-usb.txt |    5 +
 Documentation/devicetree/bindings/usb/usb-phy.txt  |    6 +
 Documentation/phy.txt                              |  113 ++++
 MAINTAINERS                                        |    7 +
 arch/arm/boot/dts/omap3.dtsi                       |    2 +
 arch/arm/boot/dts/omap4.dtsi                       |    3 +
 arch/arm/boot/dts/twl4030.dtsi                     |    1 +
 arch/arm/mach-omap2/usb-musb.c                     |    7 +-
 drivers/Kconfig                                    |    2 +
 drivers/Makefile                                   |    2 +
 drivers/phy/Kconfig                                |   13 +
 drivers/phy/Makefile                               |    5 +
 drivers/phy/phy-core.c                             |  616 ++++++++++++++++++++
 drivers/usb/musb/musb_core.h                       |    2 +
 drivers/usb/musb/omap2430.c                        |   22 +-
 drivers/usb/otg/twl4030-usb.c                      |   37 ++
 drivers/usb/phy/omap-usb2.c                        |   48 ++
 include/linux/phy/phy.h                            |  228 ++++++++
 20 files changed, 1194 insertions(+), 7 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-phy
 create mode 100644 Documentation/devicetree/bindings/phy/phy-bindings.txt
 create mode 100644 Documentation/phy.txt
 create mode 100644 drivers/phy/Kconfig
 create mode 100644 drivers/phy/Makefile
 create mode 100644 drivers/phy/phy-core.c
 create mode 100644 include/linux/phy/phy.h
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v5 1/6 drivers: phy: add generic PHY framework

The PHY framework provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

PHY drivers should create the PHY by passing phy_descriptor that has describes the PHY (label, type etc..) and ops like init, exit, suspend, resume, power_on, power_off.

The documentation for the generic PHY framework is added in Documentation/phy.txt and the documentation for the sysfs entry is added in Documentation/ABI/testing/sysfs-class-phy and the documentation for dt binding is can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

---
 Documentation/ABI/testing/sysfs-class-phy          |   15 +
 .../devicetree/bindings/phy/phy-bindings.txt       |   67 +++
 Documentation/phy.txt                              |  113 ++++
 MAINTAINERS                                        |    7 +
 drivers/Kconfig                                    |    2 +
 drivers/Makefile                                   |    2 +
 drivers/phy/Kconfig                                |   13 +
 drivers/phy/Makefile                               |    5 +
 drivers/phy/phy-core.c                             |  616 ++++++++++++++++++++
 include/linux/phy/phy.h                            |  228 ++++++++
 10 files changed, 1068 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-phy
 create mode 100644 Documentation/devicetree/bindings/phy/phy-bindings.txt
 create mode 100644 Documentation/phy.txt
 create mode 100644 drivers/phy/Kconfig
 create mode 100644 drivers/phy/Makefile
 create mode 100644 drivers/phy/phy-core.c
 create mode 100644 include/linux/phy/phy.h
Documentation/ABI/testing/sysfs-class-phy new file +15 -0
new file mode 100644
index 0000000..b735467
@@ -0,0 +1,15 @@
+What:		/sys/class/phy/<phy>/label
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for getting the label of the phy.
+
+What:		/sys/class/phy/<phy>/phy_bind
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for reading the phy binding
+		information. It contains the device name of the controller,
+		the index and the device name of the PHY in that order.
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +67 -0

67 lines not on this page · raw message

Documentation/phy.txt new file +113 -0

113 lines not on this page · raw message

MAINTAINERS +7 -0
index 72b0843..f2674e7 100644
@@ -3474,6 +3474,13 @@ S:	Maintained
 F:	include/asm-generic
 F:	include/uapi/asm-generic
 
+GENERIC PHY FRAMEWORK
+M:	Kishon Vijay Abraham I <[email protected]>
+L:	linux-kernel at vger.kernel.org
+S:	Supported
+F:	drivers/phy/
+F:	include/linux/phy/
+
 GENERIC UIO DRIVER FOR PCI DEVICES
 M:	"Michael S. Tsirkin" <[email protected]>
 L:	kvm at vger.kernel.org
drivers/Kconfig +2 -0
index 202fa6d..ad2c374a 100644
@@ -162,4 +162,6 @@ source "drivers/irqchip/Kconfig"
 
 source "drivers/ipack/Kconfig"
 
+source "drivers/phy/Kconfig"
+
 endmenu
drivers/Makefile +2 -0
index dce39a9..9da8321 100644
@@ -45,6 +45,8 @@ obj-y				+= char/
 # gpu/ comes after char for AGP vs DRM startup
 obj-y				+= gpu/
 
+obj-y				+= phy/
+
 obj-$(CONFIG_CONNECTOR)		+= connector/
 
 # i810fb and intelfb depend on char/agp/
drivers/phy/Kconfig new file +13 -0
new file mode 100644
index 0000000..5f85909
@@ -0,0 +1,13 @@
+#
+# PHY
+#
+
+menuconfig GENERIC_PHY
+	tristate "PHY Subsystem"
+	help
+	  Generic PHY support.
+
+	  This framework is designed to provide a generic interface for PHY
+	  devices present in the kernel. This layer will have the generic
+	  API by which phy drivers can create PHY using the phy framework and
+	  phy users can obtain reference to the PHY.
drivers/phy/Makefile new file +5 -0
new file mode 100644
index 0000000..9e9560f
@@ -0,0 +1,5 @@
+#
+# Makefile for the phy drivers.
+#
+
+obj-$(CONFIG_GENERIC_PHY)	+= phy-core.o
drivers/phy/phy-core.c new file +616 -0

616 lines not on this page · raw message

include/linux/phy/phy.h new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Quoted 400 lines · Kishon Vijay Abraham I

The PHY framework provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

PHY drivers should create the PHY by passing phy_descriptor that has describes the PHY (label, type etc..) and ops like init, exit, suspend, resume, power_on, power_off.

The documentation for the generic PHY framework is added in Documentation/phy.txt and the documentation for the sysfs entry is added in Documentation/ABI/testing/sysfs-class-phy and the documentation for dt binding is can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

---
 Documentation/ABI/testing/sysfs-class-phy          |   15 +
 .../devicetree/bindings/phy/phy-bindings.txt       |   67 +++
 Documentation/phy.txt                              |  113 ++++
 MAINTAINERS                                        |    7 +
 drivers/Kconfig                                    |    2 +
 drivers/Makefile                                   |    2 +
 drivers/phy/Kconfig                                |   13 +
 drivers/phy/Makefile                               |    5 +
 drivers/phy/phy-core.c                             |  616 ++++++++++++++++++++
 include/linux/phy/phy.h                            |  228 ++++++++
 10 files changed, 1068 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-phy
 create mode 100644 Documentation/devicetree/bindings/phy/phy-bindings.txt
 create mode 100644 Documentation/phy.txt
 create mode 100644 drivers/phy/Kconfig
 create mode 100644 drivers/phy/Makefile
 create mode 100644 drivers/phy/phy-core.c
 create mode 100644 include/linux/phy/phy.h
Documentation/ABI/testing/sysfs-class-phy new file +15 -0
new file mode 100644
index 0000000..b735467
@@ -0,0 +1,15 @@
+What:		/sys/class/phy/<phy>/label
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for getting the label of the phy.
+
+What:		/sys/class/phy/<phy>/phy_bind
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for reading the phy binding
+		information. It contains the device name of the controller,
+		the index and the device name of the PHY in that order.
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +67 -0
new file mode 100644
index 0000000..e7b246a
@@ -0,0 +1,67 @@
+This document explains only the dt data binding. For general information about
+PHY subsystem refer Documentation/phy.txt
+
+PHY device node
+===============
+
+Required Properties:
+#phy-cells:	Number of cells in a PHY specifier;  The meaning of all those
+		cells is defined by the binding for the phy node. The PHY
+		provider can use the values in cells to find the appropriate
+		PHY.
+
+For example:
+
+phys: phy {
+    compatible = "xxx";
+    reg = <...>;
+    .
+    .
+    #phy-cells = <1>;
+    .
+    .
+};
+
+That node describes an IP block that implements 2 different PHYs. In order to
+differentiate between these 2 PHYs, an additonal specifier should be given
+while trying to get a reference to it.
+
+PHY user node
+=============
+
+Required Properties:
+phys : the phandle for the PHY device (used by the PHY subsystem)
+
+Optional properties:
+phy-names : the names of the PHY corresponding to the PHYs present in the
+	    *phys* phandle
+
+Example 1:
+usb1: usb_otg_ss at xxx {
+    compatible = "xxx";
+    reg = <xxx>;
+    .
+    .
+    phys = <&usb2_phy>, <&usb3_phy>;
+    phy-names = "usb2phy", "usb3phy";
+    .
+    .
+};
+
+This node represents a controller that uses two PHYs one for usb2 and one for
+usb3.
+
+Example 2:
+usb2: usb_otg_ss at xxx {
+    compatible = "xxx";
+    reg = <xxx>;
+    .
+    .
+    phys = <&phys 1>;
+    .
+    .
+};
+
+This node represents a controller that uses one of the PHYs which is defined
+previously. Note that the phy handle has an additional specifier "1" to
+differentiate between the two PHYs.
Documentation/phy.txt new file +113 -0
new file mode 100644
index 0000000..7785ec0
@@ -0,0 +1,113 @@
+			    PHY SUBSYSTEM
+		  Kishon Vijay Abraham I <[email protected]>
+
+This document explains the Generic PHY Framework along with the APIs provided,
+and how-to-use.
+
+1. Introduction
+
+*PHY* is the abbreviation for physical layer. It is used to connect a device
+to the physical medium e.g., the USB controller has a PHY to provide functions
+such as serialization, de-serialization, encoding, decoding and is responsible
+for obtaining the required data transmission rate. Note that some USB
+controller has PHY functionality embedded into it and others use an external
+PHY. Other peripherals that uses a PHY include Wireless LAN, Ethernet,
+SATA etc.
+
+The intention of creating this framework is to bring the phy drivers spread
+all over the Linux kernel to drivers/phy to increase code re-use and to
+increase code maintainability.
+
+This framework will be of use only to devices that uses external PHY (PHY
+functionality is not embedded within the controller).
+
+2. Creating the PHY
+
+The PHY driver should create the PHY in order for other peripheral controllers
+to make use of it. The PHY framework provides 2 APIs to create the PHY.
+
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+struct phy *devm_phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+
+The PHY drivers can use one of the above 2 APIs to create the PHY by passing
+the device pointer, label, device node, type, phy ops and a driver data.
+phy_ops is a set of function pointers for performing PHY operations such as
+init, exit, suspend, resume, power_on and power_off.
+
+3. Binding the PHY to the controller
+
+The framework provides an API for binding the controller to the PHY in the
+case of non dt boot.
+
+struct phy_bind *phy_bind(const char *dev_name, int index,
+				const char *phy_dev_name);
+
+The API fills the phy_bind structure with the dev_name (device name of the
+controller), index and phy_dev_name (device name of the PHY). This will
+be used when the controller requests this phy. This API should be used by
+platform specific initialization code (board file).
+
+In the case of dt boot, the binding information should be added in the dt
+data of the controller.
+
+4. Getting a reference to the PHY
+
+Before the controller can make use of the PHY, it has to get a reference to
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char *string);
+
+phy_get and devm_phy_get can be used to get the PHY in non-dt boot. This API
+uses the binding information added using the phy_bind API to find and return
+the appropriate PHY. The only difference between the two APIs is that
+devm_phy_get associates the device with the PHY using devres on successful PHY
+get. On driver detach, release function is invoked on the the devres data and
+devres data is freed.
+
+of_phy_get and devm_of_phy_get can be used to get the PHY in dt boot. These
+APIs take the phandle and index to get a reference to the PHY. The only
+difference between the two APIs is that devm_of_phy_get associates the device
+with the PHY using devres on successful phy get. On driver detach, release
+function is invoked on the devres data and it is freed.
+
+of_phy_get_byname and devm_of_phy_get_byname can also be used to get the PHY
+in dt boot. It is same as the above API except that the user has to pass the
+phy name as filled in "phy-names" phandle in dt data and the framework will
+find the index and get the PHY.
+
+5. Releasing a reference to the PHY
+
+When the controller no longer needs the PHY, it has to release the reference
+to the PHY it has obtained using the APIs mentioned in the above section. The
+PHY framework provides 2 APIS to release a reference to the PHY.
+
+void phy_put(struct phy *phy);
+void devm_phy_put(struct device *dev, struct phy *phy);
+
+Both these APIs are used to release a reference to the PHY and devm_phy_put
+destroys the devres associated with this PHY.
+
+6. Destroying the PHY
+
+When the driver that created the PHY is unloaded, it should destroy the PHY it
+created using one of the following 2 APIs.
+
+void phy_destroy(struct phy *phy);
+void devm_phy_destroy(struct device *dev, struct phy *phy);
+
+Both these APIs destroys the PHY and devm_phy_destroy destroys the devres
+associated with this PHY.
+
+7. DeviceTree Binding
+
+The documentation for PHY dt binding can be found @
+Documentation/devicetree/bindings/phy/phy-bindings.txt
MAINTAINERS +7 -0
index 72b0843..f2674e7 100644
@@ -3474,6 +3474,13 @@ S:	Maintained
 F:	include/asm-generic
 F:	include/uapi/asm-generic
 
+GENERIC PHY FRAMEWORK
+M:	Kishon Vijay Abraham I <[email protected]>
+L:	linux-kernel at vger.kernel.org
+S:	Supported
+F:	drivers/phy/
+F:	include/linux/phy/
+
 GENERIC UIO DRIVER FOR PCI DEVICES
 M:	"Michael S. Tsirkin" <[email protected]>
 L:	kvm at vger.kernel.org
drivers/Kconfig +2 -0
index 202fa6d..ad2c374a 100644
@@ -162,4 +162,6 @@ source "drivers/irqchip/Kconfig"
 
 source "drivers/ipack/Kconfig"
 
+source "drivers/phy/Kconfig"
+
 endmenu
drivers/Makefile +2 -0
index dce39a9..9da8321 100644
@@ -45,6 +45,8 @@ obj-y				+= char/
 # gpu/ comes after char for AGP vs DRM startup
 obj-y				+= gpu/
 
+obj-y				+= phy/
+
 obj-$(CONFIG_CONNECTOR)		+= connector/
 
 # i810fb and intelfb depend on char/agp/
drivers/phy/Kconfig new file +13 -0
new file mode 100644
index 0000000..5f85909
@@ -0,0 +1,13 @@
+#
+# PHY
+#
+
+menuconfig GENERIC_PHY
+	tristate "PHY Subsystem"
+	help
+	  Generic PHY support.
+
+	  This framework is designed to provide a generic interface for PHY
+	  devices present in the kernel. This layer will have the generic
+	  API by which phy drivers can create PHY using the phy framework and
+	  phy users can obtain reference to the PHY.
drivers/phy/Makefile new file +5 -0
new file mode 100644
index 0000000..9e9560f
@@ -0,0 +1,5 @@
+#
+# Makefile for the phy drivers.
+#
+
+obj-$(CONFIG_GENERIC_PHY)	+= phy-core.o
drivers/phy/phy-core.c new file +70 -0
new file mode 100644
index 0000000..1d753f2
@@ -0,0 +1,616 @@
+/*
+ * phy-core.c  --  Generic Phy framework.
+ *
+ * Copyright (C) 2013 Texas Instruments
+ *
+ * Author: Kishon Vijay Abraham I <[email protected]>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/kernel.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/phy/phy.h>
+
+static struct class *phy_class;
+static DEFINE_MUTEX(phy_bind_mutex);
+static LIST_HEAD(phy_bind_list);
+static int phy_core_init(void);
+
+static void devm_phy_release(struct device *dev, void *res)
+{
+	struct phy *phy = *(struct phy **)res;
+
+	phy_put(phy);
+}
+
+static void devm_phy_consume(struct device *dev, void *res)
+{
+	struct phy *phy = *(struct phy **)res;
+
+	phy_destroy(phy);
+}
+
+static int devm_phy_match(struct device *dev, void *res, void *match_data)
+{
+	return res == match_data;
+}
+
+static struct phy *phy_lookup(struct device *dev, int index)
+{
+	struct phy_bind *phy_bind = NULL;
+
+	list_for_each_entry(phy_bind, &phy_bind_list, list) {
+		if (!(strcmp(phy_bind->dev_name, dev_name(dev)) &&
+				phy_bind->index == index)) {
+			if (phy_bind->phy)
+				return phy_bind->phy;
+			else
+				return ERR_PTR(-EPROBE_DEFER);
+		}
+	}
+
+	return ERR_PTR(-ENODEV);
+}

drivers/phy/phy-core.c line 80

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+	struct phy *phy;
+	struct device *dev;
+	struct class_dev_iter iter;
+
+	class_dev_iter_init(&iter, phy_class, NULL, NULL);
+	while ((dev = class_dev_iter_next(&iter))) {
+		phy = container_of(dev, struct phy, dev);

it would look a bit better if you provided a to_phy() macro. Specially since this container_of() repeats multiple times in this file.

drivers/phy/phy-core.c line 99

Quoted 8 lines
drivers/phy/phy-core.c new file +8 -0
+/**
+ * phy_put() - release the PHY
+ * @phy: the phy returned by phy_get()
+ *
+ * Releases a refcount the caller received from phy_get().
+ */
+void phy_put(struct phy *phy)
+{

I would rather:

if (WARN(IS_ERR(phy), "invalid parameter\n"))

	return;

module_put(phy->ops->owner);
put_device(&phy->dev);

that way we can catch users passing bogus pointers here. When PHY layer is disabled, you want to make this is no-op with a static inline in a header anyway.

drivers/phy/phy-core.c line 138

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+struct phy *of_phy_xlate(struct phy *phy, struct of_phandle_args *args)
+{
+	return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_xlate);

so you get a PHY and just return it ? What gives ?? (maybe I skipped some of the discussion...)

Quoted 18 lines
drivers/phy/phy-core.c new file +18 -0
+struct phy *of_phy_get(struct device *dev, int index)
+{
+	int ret;
+	struct phy *phy = NULL;
+	struct phy_bind *phy_map = NULL;
+	struct of_phandle_args args;
+	struct device_node *node;
+
+	if (!dev->of_node) {
+		dev_dbg(dev, "device does not have a device node entry\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+		index, &args);
+	if (ret) {
+		dev_dbg(dev, "failed to get phy in %s node\n",
+			dev->of_node->full_name);

drivers/phy/phy-core.c line 177

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+		return ERR_PTR(-ENODEV);
+	}
+
+	phy = of_phy_lookup(args.np);
+	if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+		phy = ERR_PTR(-EPROBE_DEFER);
+		goto err0;
+	}
+
+	phy = phy->ops->of_xlate(phy, &args);

alright, so of_xlate() is optional, am I right ? How about not implementing the above and have a check for of_xlate() being a valid pointer here ?

drivers/phy/phy-core.c line 353

Quoted 11 lines
drivers/phy/phy-core.c new file +11 -0
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv)
+{
+	int ret;
+	struct phy *phy;
+	struct phy_bind *phy_bind;
+	const char *devname = NULL;
+
+	if (!dev) {
+		dev_err(dev, "no device provided for PHY\n");

I'd call this a WARN() or am I too pedantic? :-p

drivers/phy/phy-core.c line 359

Quoted 2 lines
drivers/phy/phy-core.c new file +2 -0
+	if (!ops || !ops->of_xlate || !priv) {
+		dev_err(dev, "no PHY ops/PHY data provided\n");

likewise here.

drivers/phy/phy-core.c line 365

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

drivers/phy/phy-core.c line 569

Quoted 3 lines
drivers/phy/phy-core.c new file +3 -0
+static struct device_attribute phy_dev_attrs[] = {
+	__ATTR(label, 0444, phy_name_show, NULL),
+	__ATTR(phy_bind, 0444, phy_bind_show, NULL),

you could expose a human-readable 'type' string. BTW, how are you using type ? USB2/USB3/etc ? Have you considered our OMAP5 SerDes pair which are currently for USB3 and SATA (and could just as easily be used for PCIe)

drivers/phy/phy-core.c line 585

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+static void phy_release(struct device *dev)
+{
+	struct phy *phy;
+
+	phy = container_of(dev, struct phy, dev);
+	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));

how about dev_vdbg() ? I doubt anyone will be waiting for this message... Just a thought

drivers/phy/phy-core.c line 592

Quoted 4 lines
drivers/phy/phy-core.c new file +4 -0
+static int phy_core_init(void)
+{
+	if (phy_class)
+		return 0;

Weird.. if you initialize the class here, why do you need to initialize it during phy_create() ?

What's going on ? Also, module_init() will only be called once, why this if (phy_class) check ?

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130403/f22c3489/attachment.sig>
#3 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 406 lines · Felipe Balbi
Quoted 405 lines · Kishon Vijay Abraham I

The PHY framework provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

PHY drivers should create the PHY by passing phy_descriptor that has describes the PHY (label, type etc..) and ops like init, exit, suspend, resume, power_on, power_off.

The documentation for the generic PHY framework is added in Documentation/phy.txt and the documentation for the sysfs entry is added in Documentation/ABI/testing/sysfs-class-phy and the documentation for dt binding is can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

---
  Documentation/ABI/testing/sysfs-class-phy          |   15 +
  .../devicetree/bindings/phy/phy-bindings.txt       |   67 +++
  Documentation/phy.txt                              |  113 ++++
  MAINTAINERS                                        |    7 +
  drivers/Kconfig                                    |    2 +
  drivers/Makefile                                   |    2 +
  drivers/phy/Kconfig                                |   13 +
  drivers/phy/Makefile                               |    5 +
  drivers/phy/phy-core.c                             |  616 ++++++++++++++++++++
  include/linux/phy/phy.h                            |  228 ++++++++
  10 files changed, 1068 insertions(+)

create mode 100644 Documentation/ABI/testing/sysfs-class-phy create mode 100644 Documentation/devicetree/bindings/phy/phy-bindings.txt create mode 100644 Documentation/phy.txt
create mode 100644 drivers/phy/Kconfig
create mode 100644 drivers/phy/Makefile
create mode 100644 drivers/phy/phy-core.c
create mode 100644 include/linux/phy/phy.h

Documentation/ABI/testing/sysfs-class-phy new file +15 -0
new file mode 100644
index 0000000..b735467
@@ -0,0 +1,15 @@
+What:		/sys/class/phy/<phy>/label
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for getting the label of the phy.
+
+What:		/sys/class/phy/<phy>/phy_bind
+Date:		Apr 2013
+KernelVersion:	3.10
+Contact:	Kishon Vijay Abraham I <[email protected]>
+Description:
+		This is a read-only file for reading the phy binding
+		information. It contains the device name of the controller,
+		the index and the device name of the PHY in that order.
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +67 -0
new file mode 100644
index 0000000..e7b246a
@@ -0,0 +1,67 @@
+This document explains only the dt data binding. For general information about
+PHY subsystem refer Documentation/phy.txt
+
+PHY device node
+===============
+
+Required Properties:
+#phy-cells:	Number of cells in a PHY specifier;  The meaning of all those
+		cells is defined by the binding for the phy node. The PHY
+		provider can use the values in cells to find the appropriate
+		PHY.
+
+For example:
+
+phys: phy {
+    compatible = "xxx";
+    reg = <...>;
+    .
+    .
+    #phy-cells = <1>;
+    .
+    .
+};
+
+That node describes an IP block that implements 2 different PHYs. In order to
+differentiate between these 2 PHYs, an additonal specifier should be given
+while trying to get a reference to it.
+
+PHY user node
+=============
+
+Required Properties:
+phys : the phandle for the PHY device (used by the PHY subsystem)
+
+Optional properties:
+phy-names : the names of the PHY corresponding to the PHYs present in the
+	    *phys* phandle
+
+Example 1:
+usb1: usb_otg_ss at xxx {
+    compatible = "xxx";
+    reg = <xxx>;
+    .
+    .
+    phys = <&usb2_phy>, <&usb3_phy>;
+    phy-names = "usb2phy", "usb3phy";
+    .
+    .
+};
+
+This node represents a controller that uses two PHYs one for usb2 and one for
+usb3.
+
+Example 2:
+usb2: usb_otg_ss at xxx {
+    compatible = "xxx";
+    reg = <xxx>;
+    .
+    .
+    phys = <&phys 1>;
+    .
+    .
+};
+
+This node represents a controller that uses one of the PHYs which is defined
+previously. Note that the phy handle has an additional specifier "1" to
+differentiate between the two PHYs.
Documentation/phy.txt new file +113 -0
new file mode 100644
index 0000000..7785ec0
@@ -0,0 +1,113 @@
+			    PHY SUBSYSTEM
+		  Kishon Vijay Abraham I <[email protected]>
+
+This document explains the Generic PHY Framework along with the APIs provided,
+and how-to-use.
+
+1. Introduction
+
+*PHY* is the abbreviation for physical layer. It is used to connect a device
+to the physical medium e.g., the USB controller has a PHY to provide functions
+such as serialization, de-serialization, encoding, decoding and is responsible
+for obtaining the required data transmission rate. Note that some USB
+controller has PHY functionality embedded into it and others use an external
+PHY. Other peripherals that uses a PHY include Wireless LAN, Ethernet,
+SATA etc.
+
+The intention of creating this framework is to bring the phy drivers spread
+all over the Linux kernel to drivers/phy to increase code re-use and to
+increase code maintainability.
+
+This framework will be of use only to devices that uses external PHY (PHY
+functionality is not embedded within the controller).
+
+2. Creating the PHY
+
+The PHY driver should create the PHY in order for other peripheral controllers
+to make use of it. The PHY framework provides 2 APIs to create the PHY.
+
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+struct phy *devm_phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+
+The PHY drivers can use one of the above 2 APIs to create the PHY by passing
+the device pointer, label, device node, type, phy ops and a driver data.
+phy_ops is a set of function pointers for performing PHY operations such as
+init, exit, suspend, resume, power_on and power_off.
+
+3. Binding the PHY to the controller
+
+The framework provides an API for binding the controller to the PHY in the
+case of non dt boot.
+
+struct phy_bind *phy_bind(const char *dev_name, int index,
+				const char *phy_dev_name);
+
+The API fills the phy_bind structure with the dev_name (device name of the
+controller), index and phy_dev_name (device name of the PHY). This will
+be used when the controller requests this phy. This API should be used by
+platform specific initialization code (board file).
+
+In the case of dt boot, the binding information should be added in the dt
+data of the controller.
+
+4. Getting a reference to the PHY
+
+Before the controller can make use of the PHY, it has to get a reference to
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char *string);
+
+phy_get and devm_phy_get can be used to get the PHY in non-dt boot. This API
+uses the binding information added using the phy_bind API to find and return
+the appropriate PHY. The only difference between the two APIs is that
+devm_phy_get associates the device with the PHY using devres on successful PHY
+get. On driver detach, release function is invoked on the the devres data and
+devres data is freed.
+
+of_phy_get and devm_of_phy_get can be used to get the PHY in dt boot. These
+APIs take the phandle and index to get a reference to the PHY. The only
+difference between the two APIs is that devm_of_phy_get associates the device
+with the PHY using devres on successful phy get. On driver detach, release
+function is invoked on the devres data and it is freed.
+
+of_phy_get_byname and devm_of_phy_get_byname can also be used to get the PHY
+in dt boot. It is same as the above API except that the user has to pass the
+phy name as filled in "phy-names" phandle in dt data and the framework will
+find the index and get the PHY.
+
+5. Releasing a reference to the PHY
+
+When the controller no longer needs the PHY, it has to release the reference
+to the PHY it has obtained using the APIs mentioned in the above section. The
+PHY framework provides 2 APIS to release a reference to the PHY.
+
+void phy_put(struct phy *phy);
+void devm_phy_put(struct device *dev, struct phy *phy);
+
+Both these APIs are used to release a reference to the PHY and devm_phy_put
+destroys the devres associated with this PHY.
+
+6. Destroying the PHY
+
+When the driver that created the PHY is unloaded, it should destroy the PHY it
+created using one of the following 2 APIs.
+
+void phy_destroy(struct phy *phy);
+void devm_phy_destroy(struct device *dev, struct phy *phy);
+
+Both these APIs destroys the PHY and devm_phy_destroy destroys the devres
+associated with this PHY.
+
+7. DeviceTree Binding
+
+The documentation for PHY dt binding can be found @
+Documentation/devicetree/bindings/phy/phy-bindings.txt
MAINTAINERS +7 -0
index 72b0843..f2674e7 100644
@@ -3474,6 +3474,13 @@ S:	Maintained
  F:	include/asm-generic
  F:	include/uapi/asm-generic
 
+GENERIC PHY FRAMEWORK
+M:	Kishon Vijay Abraham I <[email protected]>
+L:	linux-kernel at vger.kernel.org
+S:	Supported
+F:	drivers/phy/
+F:	include/linux/phy/
+
  GENERIC UIO DRIVER FOR PCI DEVICES
  M:	"Michael S. Tsirkin" <[email protected]>
  L:	kvm at vger.kernel.org
drivers/Kconfig +2 -0
index 202fa6d..ad2c374a 100644
@@ -162,4 +162,6 @@ source "drivers/irqchip/Kconfig"
 
  source "drivers/ipack/Kconfig"
 
+source "drivers/phy/Kconfig"
+
  endmenu
drivers/Makefile +2 -0
index dce39a9..9da8321 100644
@@ -45,6 +45,8 @@ obj-y				+= char/
  # gpu/ comes after char for AGP vs DRM startup
  obj-y				+= gpu/
 
+obj-y				+= phy/
+
  obj-$(CONFIG_CONNECTOR)		+= connector/
 
  # i810fb and intelfb depend on char/agp/
drivers/phy/Kconfig new file +13 -0
new file mode 100644
index 0000000..5f85909
@@ -0,0 +1,13 @@
+#
+# PHY
+#
+
+menuconfig GENERIC_PHY
+	tristate "PHY Subsystem"
+	help
+	  Generic PHY support.
+
+	  This framework is designed to provide a generic interface for PHY
+	  devices present in the kernel. This layer will have the generic
+	  API by which phy drivers can create PHY using the phy framework and
+	  phy users can obtain reference to the PHY.
drivers/phy/Makefile new file +5 -0
new file mode 100644
index 0000000..9e9560f
@@ -0,0 +1,5 @@
+#
+# Makefile for the phy drivers.
+#
+
+obj-$(CONFIG_GENERIC_PHY)	+= phy-core.o
drivers/phy/phy-core.c new file +75 -0
new file mode 100644
index 0000000..1d753f2
@@ -0,0 +1,616 @@
+/*
+ * phy-core.c  --  Generic Phy framework.
+ *
+ * Copyright (C) 2013 Texas Instruments
+ *
+ * Author: Kishon Vijay Abraham I <[email protected]>
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/kernel.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/err.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/phy/phy.h>
+
+static struct class *phy_class;
+static DEFINE_MUTEX(phy_bind_mutex);
+static LIST_HEAD(phy_bind_list);
+static int phy_core_init(void);
+
+static void devm_phy_release(struct device *dev, void *res)
+{
+	struct phy *phy = *(struct phy **)res;
+
+	phy_put(phy);
+}
+
+static void devm_phy_consume(struct device *dev, void *res)
+{
+	struct phy *phy = *(struct phy **)res;
+
+	phy_destroy(phy);
+}
+
+static int devm_phy_match(struct device *dev, void *res, void *match_data)
+{
+	return res == match_data;
+}
+
+static struct phy *phy_lookup(struct device *dev, int index)
+{
+	struct phy_bind *phy_bind = NULL;
+
+	list_for_each_entry(phy_bind, &phy_bind_list, list) {
+		if (!(strcmp(phy_bind->dev_name, dev_name(dev)) &&
+				phy_bind->index == index)) {
+			if (phy_bind->phy)
+				return phy_bind->phy;
+			else
+				return ERR_PTR(-EPROBE_DEFER);
+		}
+	}
+
+	return ERR_PTR(-ENODEV);
+}
+
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+	struct phy *phy;
+	struct device *dev;
Quoted 8 lines

drivers/phy/phy-core.c line 80

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+	struct class_dev_iter iter;
+
+	class_dev_iter_init(&iter, phy_class, NULL, NULL);
+	while ((dev = class_dev_iter_next(&iter))) {
+		phy = container_of(dev, struct phy, dev);

it would look a bit better if you provided a to_phy() macro. Specially since this container_of() repeats multiple times in this file.

hmm.. ok.

Quoted 13 lines

drivers/phy/phy-core.c line 99

Quoted 9 lines
drivers/phy/phy-core.c new file +8 -0
+/**
+ * phy_put() - release the PHY
+ * @phy: the phy returned by phy_get()
+ *
+ * Releases a refcount the caller received from phy_get().
+ */
+void phy_put(struct phy *phy)
+{

I would rather:

if (WARN(IS_ERR(phy), "invalid parameter\n"))

Quoted 8 lines
	return;

module_put(phy->ops->owner);
put_device(&phy->dev);

that way we can catch users passing bogus pointers here. When PHY layer is disabled, you want to make this is no-op with a static inline in a header anyway.

yeah. Have that no-op in header file.

Quoted 9 lines

drivers/phy/phy-core.c line 138

Quoted 6 lines
drivers/phy/phy-core.c new file +5 -0
+struct phy *of_phy_xlate(struct phy *phy, struct of_phandle_args *args)
+{
+	return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_xlate);

so you get a PHY and just return it ? What gives ?? (maybe I skipped some of the discussion...)

hmm.. this is for the common case where the PHY provider implements only one PHY. And both phy provider and phy_instance is represented by struct phy *.

For the case where PHY provider implements multiple PHYs (here it will have a single dt node), the PHY provider will implement it's own version of of_xlate that takes *of_phandle_args* as argument and finds the appropriate PHY.

Quoted 23 lines
Quoted 23 lines
drivers/phy/phy-core.c new file +22 -0
+struct phy *of_phy_get(struct device *dev, int index)
+{
+	int ret;
+	struct phy *phy = NULL;
+	struct phy_bind *phy_map = NULL;
+	struct of_phandle_args args;
+	struct device_node *node;
+
+	if (!dev->of_node) {
+		dev_dbg(dev, "device does not have a device node entry\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+		index, &args);
+	if (ret) {
+		dev_dbg(dev, "failed to get phy in %s node\n",
+			dev->of_node->full_name);
+		return ERR_PTR(-ENODEV);
+	}
+
+	phy = of_phy_lookup(args.np);
Quoted 8 lines

drivers/phy/phy-core.c line 177

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+	if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+		phy = ERR_PTR(-EPROBE_DEFER);
+		goto err0;
+	}
+
+	phy = phy->ops->of_xlate(phy, &args);

alright, so of_xlate() is optional, am I right ? How about not

Not really. of_xlate is mandatory (it's even checked in phy_create). Either the PHY provider can implement it's own version or use the implementation above (by filling the function pointer).

Quoted 2 lines

implementing the above and have a check for of_xlate() being a valid pointer here ?

Having the way it is actually mandates the PHY providers to always provide of_xlate which IMO is better since some PHY providers wont accidentally be using the default implementation.

Quoted 15 lines

drivers/phy/phy-core.c line 353

Quoted 12 lines
drivers/phy/phy-core.c new file +11 -0
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv)
+{
+	int ret;
+	struct phy *phy;
+	struct phy_bind *phy_bind;
+	const char *devname = NULL;
+
+	if (!dev) {
+		dev_err(dev, "no device provided for PHY\n");

I'd call this a WARN() or am I too pedantic? :-p

Quoted 4 lines

drivers/phy/phy-core.c line 359

Quoted 2 lines
drivers/phy/phy-core.c new file +2 -0
+	if (!ops || !ops->of_xlate || !priv) {
+		dev_err(dev, "no PHY ops/PHY data provided\n");

likewise here.

hmm.. ok.

Quoted 10 lines

drivers/phy/phy-core.c line 365

Quoted 7 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

This is for the case where the PHY driver gets loaded before the PHY framework. I could have returned EPROBE_DEFER here instead I thought will have it this way.

Quoted 7 lines

drivers/phy/phy-core.c line 569

Quoted 4 lines
drivers/phy/phy-core.c new file +3 -0
+static struct device_attribute phy_dev_attrs[] = {
+	__ATTR(label, 0444, phy_name_show, NULL),
+	__ATTR(phy_bind, 0444, phy_bind_show, NULL),

you could expose a human-readable 'type' string. BTW, how are you using type ? USB2/USB3/etc ? Have you considered our OMAP5 SerDes pair which

Actually not using *type* anywhere. Just used as an additional info about the PHY. It's actually not even enum. Maybe I can remove it completely.

Quoted 2 lines

are currently for USB3 and SATA (and could just as easily be used for PCIe)

Yeah. Me and Balaji were planning to work on it for having a single driver to be used for all the above.

Quoted 19 lines

drivers/phy/phy-core.c line 585

Quoted 7 lines
drivers/phy/phy-core.c new file +6 -0
+static void phy_release(struct device *dev)
+{
+	struct phy *phy;
+
+	phy = container_of(dev, struct phy, dev);
+	dev_dbg(dev, "releasing '%s'\n", dev_name(dev));

how about dev_vdbg() ? I doubt anyone will be waiting for this message... Just a thought

drivers/phy/phy-core.c line 592

Quoted 4 lines
drivers/phy/phy-core.c new file +4 -0
+static int phy_core_init(void)
+{
+	if (phy_class)
+		return 0;

Weird.. if you initialize the class here, why do you need to initialize it during phy_create() ?

Quoted 2 lines

What's going on ? Also, module_init() will only be called once, why this if (phy_class) check ?

er.. for the case where phy driver is loaded before this PHY framework, phy_create would have already called phy_core_init to create the phy_class. So module_init() is not needed at all since we have already created the phy_class. I think this looks a bit hacky. Either we can have EPROBE_DEFER in phy_create or have this module as subsys_initcall() like I had it before. I would actually prefer it to be subsys_initcall().

Thanks
Kishon

#4 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

hi,

Quoted 9 lines · Kishon Vijay Abraham I
Quoted 8 lines

drivers/phy/phy-core.c line 138

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+struct phy *of_phy_xlate(struct phy *phy, struct of_phandle_args *args)
+{
+	return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_xlate);

so you get a PHY and just return it ? What gives ?? (maybe I skipped some of the discussion...)

Quoted 8 lines

hmm.. this is for the common case where the PHY provider implements only one PHY. And both phy provider and phy_instance is represented by struct phy *.

For the case where PHY provider implements multiple PHYs (here it will have a single dt node), the PHY provider will implement it's own version of of_xlate that takes *of_phandle_args* as argument and finds the appropriate PHY.

got it.

Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
drivers/phy/phy-core.c new file +22 -0
+struct phy *of_phy_get(struct device *dev, int index)
+{
+	int ret;
+	struct phy *phy = NULL;
+	struct phy_bind *phy_map = NULL;
+	struct of_phandle_args args;
+	struct device_node *node;
+
+	if (!dev->of_node) {
+		dev_dbg(dev, "device does not have a device node entry\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+		index, &args);
+	if (ret) {
+		dev_dbg(dev, "failed to get phy in %s node\n",
+			dev->of_node->full_name);
+		return ERR_PTR(-ENODEV);
+	}
+
+	phy = of_phy_lookup(args.np);
Quoted 8 lines

drivers/phy/phy-core.c line 177

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+	if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+		phy = ERR_PTR(-EPROBE_DEFER);
+		goto err0;
+	}
+
+	phy = phy->ops->of_xlate(phy, &args);

alright, so of_xlate() is optional, am I right ? How about not

Quoted 3 lines

Not really. of_xlate is mandatory (it's even checked in phy_create). Either the PHY provider can implement it's own version or use the implementation above (by filling the function pointer).

alright.

Quoted 6 lines
Quoted 2 lines

implementing the above and have a check for of_xlate() being a valid pointer here ?

Having the way it is actually mandates the PHY providers to always provide of_xlate which IMO is better since some PHY providers wont accidentally be using the default implementation.

ok cool, thanks for clarifying.

Quoted 10 lines
Quoted 9 lines

drivers/phy/phy-core.c line 365

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

Quoted 3 lines

This is for the case where the PHY driver gets loaded before the PHY framework. I could have returned EPROBE_DEFER here instead I thought will have it this way.

looks a bit weird IMO. Is it really possible for PHY to load before ? Since PHY driver uses symbols from phy-core, modprobe will make sure to load drivers based on symbol dependency, right ?

Quoted 10 lines
Quoted 6 lines

drivers/phy/phy-core.c line 569

Quoted 3 lines
drivers/phy/phy-core.c new file +3 -0
+static struct device_attribute phy_dev_attrs[] = {
+	__ATTR(label, 0444, phy_name_show, NULL),
+	__ATTR(phy_bind, 0444, phy_bind_show, NULL),

you could expose a human-readable 'type' string. BTW, how are you using type ? USB2/USB3/etc ? Have you considered our OMAP5 SerDes pair which

Actually not using *type* anywhere. Just used as an additional info about the PHY. It's actually not even enum. Maybe I can remove it completely.

makes sense.

Quoted 5 lines
Quoted 2 lines

are currently for USB3 and SATA (and could just as easily be used for PCIe)

Yeah. Me and Balaji were planning to work on it for having a single driver to be used for all the above.

cool :-)

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130403/57dd0ca2/attachment.sig>
#5 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 79 lines · Felipe Balbi

hi,

Quoted 9 lines · Kishon Vijay Abraham I
Quoted 8 lines

drivers/phy/phy-core.c line 138

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+struct phy *of_phy_xlate(struct phy *phy, struct of_phandle_args *args)
+{
+	return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_xlate);

so you get a PHY and just return it ? What gives ?? (maybe I skipped some of the discussion...)

Quoted 8 lines

hmm.. this is for the common case where the PHY provider implements only one PHY. And both phy provider and phy_instance is represented by struct phy *.

For the case where PHY provider implements multiple PHYs (here it will have a single dt node), the PHY provider will implement it's own version of of_xlate that takes *of_phandle_args* as argument and finds the appropriate PHY.

got it.

Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
drivers/phy/phy-core.c new file +22 -0
+struct phy *of_phy_get(struct device *dev, int index)
+{
+	int ret;
+	struct phy *phy = NULL;
+	struct phy_bind *phy_map = NULL;
+	struct of_phandle_args args;
+	struct device_node *node;
+
+	if (!dev->of_node) {
+		dev_dbg(dev, "device does not have a device node entry\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+		index, &args);
+	if (ret) {
+		dev_dbg(dev, "failed to get phy in %s node\n",
+			dev->of_node->full_name);
+		return ERR_PTR(-ENODEV);
+	}
+
+	phy = of_phy_lookup(args.np);
Quoted 8 lines

drivers/phy/phy-core.c line 177

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+	if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+		phy = ERR_PTR(-EPROBE_DEFER);
+		goto err0;
+	}
+
+	phy = phy->ops->of_xlate(phy, &args);

alright, so of_xlate() is optional, am I right ? How about not

Quoted 3 lines

Not really. of_xlate is mandatory (it's even checked in phy_create). Either the PHY provider can implement it's own version or use the implementation above (by filling the function pointer).

alright.

Quoted 6 lines
Quoted 2 lines

implementing the above and have a check for of_xlate() being a valid pointer here ?

Having the way it is actually mandates the PHY providers to always provide of_xlate which IMO is better since some PHY providers wont accidentally be using the default implementation.

ok cool, thanks for clarifying.

Quoted 10 lines
Quoted 9 lines

drivers/phy/phy-core.c line 365

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

Quoted 5 lines
Quoted 3 lines

This is for the case where the PHY driver gets loaded before the PHY framework. I could have returned EPROBE_DEFER here instead I thought will have it this way.

looks a bit weird IMO. Is it really possible for PHY to load before ?

yeah. it actually happened when I tried with beagle and had all the modules as built-in. Because twl4030 has subsys_initcall(), it loads before PHY framework.

Thanks
Kishon

#6 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 14 lines · Kishon Vijay Abraham I
Quoted 14 lines
Quoted 13 lines
Quoted 9 lines

drivers/phy/phy-core.c line 365

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

This is for the case where the PHY driver gets loaded before the PHY framework. I could have returned EPROBE_DEFER here instead I thought will have it this way.

Quoted 5 lines
Quoted 1 line

looks a bit weird IMO. Is it really possible for PHY to load before ?

yeah. it actually happened when I tried with beagle and had all the modules as built-in. Because twl4030 has subsys_initcall(), it loads before PHY framework.

that's a bug in twl4030.

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130403/494c6f11/attachment.sig>
#7 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 17 lines · Felipe Balbi

Hi,

Quoted 14 lines · Kishon Vijay Abraham I
Quoted 14 lines
Quoted 13 lines
Quoted 9 lines

drivers/phy/phy-core.c line 365

Quoted 6 lines
drivers/phy/phy-core.c new file +6 -0
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!phy_class)
+		phy_core_init();

why don't you setup the class on module_init ? Then this would be a terrible error condition here :-)

This is for the case where the PHY driver gets loaded before the PHY framework. I could have returned EPROBE_DEFER here instead I thought will have it this way.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

looks a bit weird IMO. Is it really possible for PHY to load before ?

yeah. it actually happened when I tried with beagle and had all the modules as built-in. Because twl4030 has subsys_initcall(), it loads before PHY framework.

that's a bug in twl4030.

right. Will fix it.

Thanks
Kishon

#8 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

In reply to Kishon Vijay Abraham I

Documentation/devicetree/bindings/phy/phy-bindings.txt line 1

Quoted 4 lines · Kishon Vijay Abraham I
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +1 -0
@@ -0,0 +1,67 @@
+This document explains only the dt data binding. For general information about

s/dt data/device tree ?

Documentation/devicetree/bindings/phy/phy-bindings.txt line 2

Quoted 1 line
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +1 -0
+PHY subsystem refer Documentation/phy.txt

s/refer/refer to ?

Quoted 39 lines
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +39 -0
+
+PHY device node
+===============
+
+Required Properties:
+#phy-cells:	Number of cells in a PHY specifier;  The meaning of all those
+		cells is defined by the binding for the phy node. The PHY
+		provider can use the values in cells to find the appropriate
+		PHY.
+
+For example:
+
+phys: phy {
+    compatible = "xxx";
+    reg =<...>;
+    .
+    .
+    #phy-cells =<1>;
+    .
+    .
+};
+
+That node describes an IP block that implements 2 different PHYs. In order to
+differentiate between these 2 PHYs, an additonal specifier should be given
+while trying to get a reference to it.
+
+PHY user node
+=============
+
+Required Properties:
+phys : the phandle for the PHY device (used by the PHY subsystem)
+
+Optional properties:
+phy-names : the names of the PHY corresponding to the PHYs present in the
+	    *phys* phandle
+
+Example 1:
+usb1: usb_otg_ss at xxx {
+    compatible = "xxx";

Documentation/devicetree/bindings/phy/phy-bindings.txt line 51

Quoted 10 lines
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +10 -0
+    reg =<xxx>;
+    .
+    .
+    phys =<&usb2_phy>,<&usb3_phy>;
+    phy-names = "usb2phy", "usb3phy";
+    .
+    .
+};
+
+This node represents a controller that uses two PHYs one for usb2 and one for

s/PHYs/PHYs, ?

Quoted 5 lines
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +5 -0
+usb3.
+
+Example 2:
+usb2: usb_otg_ss at xxx {
+    compatible = "xxx";

Documentation/devicetree/bindings/phy/phy-bindings.txt line 66

Quoted 10 lines
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +10 -0
+    reg =<xxx>;
+    .
+    .
+    phys =<&phys 1>;
+    .
+    .
+};
+
+This node represents a controller that uses one of the PHYs which is defined
+previously. Note that the phy handle has an additional specifier "1" to

I find it a bit difficult to parse. Perhaps

"This node represents a controller that uses one of the PHYs of the PHY provider
device defined previously. ..."

or something similar ?

Documentation/devicetree/bindings/phy/phy-bindings.txt line 67

Quoted 1 line
Documentation/devicetree/bindings/phy/phy-bindings.txt new file +1 -0
+differentiate between the two PHYs.

s/the two/the two available ?

Quoted 6 lines
Documentation/phy.txt new file +0 -0
new file mode 100644
index 0000000..7785ec0

Documentation/phy.txt line 10

Quoted 10 lines
Documentation/phy.txt new file +10 -0
@@ -0,0 +1,113 @@
+			    PHY SUBSYSTEM
+		  Kishon Vijay Abraham I<[email protected]>
+
+This document explains the Generic PHY Framework along with the APIs provided,
+and how-to-use.
+
+1. Introduction
+
+*PHY* is the abbreviation for physical layer. It is used to connect a device
+to the physical medium e.g., the USB controller has a PHY to provide functions

Shouldn't it be "...medium, e.g. the USB..." ?

Documentation/phy.txt line 13

Quoted 3 lines
Documentation/phy.txt new file +3 -0
+such as serialization, de-serialization, encoding, decoding and is responsible
+for obtaining the required data transmission rate. Note that some USB
+controller has PHY functionality embedded into it and others use an external

"controllers have PHY functionality embedded into them" ?

Documentation/phy.txt line 14

Quoted 1 line
Documentation/phy.txt new file +1 -0
+PHY. Other peripherals that uses a PHY include Wireless LAN, Ethernet,

s/uses/use ?

Documentation/phy.txt line 21

Quoted 7 lines
Documentation/phy.txt new file +7 -0
+SATA etc.
+
+The intention of creating this framework is to bring the phy drivers spread
+all over the Linux kernel to drivers/phy to increase code re-use and to
+increase code maintainability.
+
+This framework will be of use only to devices that uses external PHY (PHY

s/uses/use ?

Quoted 24 lines
Documentation/phy.txt new file +24 -0
+functionality is not embedded within the controller).
+
+2. Creating the PHY
+
+The PHY driver should create the PHY in order for other peripheral controllers
+to make use of it. The PHY framework provides 2 APIs to create the PHY.
+
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+struct phy *devm_phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);
+
+The PHY drivers can use one of the above 2 APIs to create the PHY by passing
+the device pointer, label, device node, type, phy ops and a driver data.
+phy_ops is a set of function pointers for performing PHY operations such as
+init, exit, suspend, resume, power_on and power_off.
+
+3. Binding the PHY to the controller
+
+The framework provides an API for binding the controller to the PHY in the
+case of non dt boot.
+

Documentation/phy.txt line 55

Quoted 10 lines
Documentation/phy.txt new file +10 -0
+struct phy_bind *phy_bind(const char *dev_name, int index,
+				const char *phy_dev_name);
+
+The API fills the phy_bind structure with the dev_name (device name of the
+controller), index and phy_dev_name (device name of the PHY). This will
+be used when the controller requests this phy. This API should be used by
+platform specific initialization code (board file).
+
+In the case of dt boot, the binding information should be added in the dt
+data of the controller.

s/in the dt data of/in the device tree node of ?

Documentation/phy.txt line 65

Quoted 9 lines
Documentation/phy.txt new file +9 -0
+4. Getting a reference to the PHY
+
+Before the controller can make use of the PHY, it has to get a reference to
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);

Why do we need separate functions for dt and non-dt ? Couldn't it be handled internally by the framework ? So the PHY users can use same calls for dt and non-dt, like in case of e.g. the regulators API ?

Also signatures of some functions are different now:

extern struct phy *phy_get(struct device *dev, int index); extern struct phy *devm_phy_get(struct device *dev, int index); extern struct phy *of_phy_get(struct device *dev, int index); extern struct phy *devm_of_phy_get(struct device *dev, int index);

BTW, I think "extern" could be dropped, does it have any significance in function declarations in header files ?

Documentation/phy.txt line 73

Quoted 8 lines
Documentation/phy.txt new file +8 -0
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char *string);
+
+phy_get and devm_phy_get can be used to get the PHY in non-dt boot. This API
+uses the binding information added using the phy_bind API to find and return
+the appropriate PHY. The only difference between the two APIs is that
+devm_phy_get associates the device with the PHY using devres on successful PHY
+get. On driver detach, release function is invoked on the the devres data and

s/the the/the

Documentation/phy.txt line 84

Quoted 11 lines
Documentation/phy.txt new file +11 -0
+devres data is freed.
+
+of_phy_get and devm_of_phy_get can be used to get the PHY in dt boot. These
+APIs take the phandle and index to get a reference to the PHY. The only
+difference between the two APIs is that devm_of_phy_get associates the device
+with the PHY using devres on successful phy get. On driver detach, release
+function is invoked on the devres data and it is freed.
+
+of_phy_get_byname and devm_of_phy_get_byname can also be used to get the PHY
+in dt boot. It is same as the above API except that the user has to pass the
+phy name as filled in "phy-names" phandle in dt data and the framework will

s/phandle/property ?

Documentation/phy.txt line 91

Quoted 7 lines
Documentation/phy.txt new file +7 -0
+find the index and get the PHY.
+
+5. Releasing a reference to the PHY
+
+When the controller no longer needs the PHY, it has to release the reference
+to the PHY it has obtained using the APIs mentioned in the above section. The
+PHY framework provides 2 APIS to release a reference to the PHY.

s/APIS/APIs ?

Quoted 6 lines
Documentation/phy.txt new file +6 -0
+
+void phy_put(struct phy *phy);
+void devm_phy_put(struct device *dev, struct phy *phy);
+
+Both these APIs are used to release a reference to the PHY and devm_phy_put
+destroys the devres associated with this PHY.

Documentation/phy.txt line 107

Quoted 10 lines
Documentation/phy.txt new file +10 -0
+
+6. Destroying the PHY
+
+When the driver that created the PHY is unloaded, it should destroy the PHY it
+created using one of the following 2 APIs.
+
+void phy_destroy(struct phy *phy);
+void devm_phy_destroy(struct device *dev, struct phy *phy);
+
+Both these APIs destroys the PHY and devm_phy_destroy destroys the devres

s/APIs destroys/APIs destroy ?

Quoted 10 lines
Documentation/phy.txt new file +6 -0
+associated with this PHY.
+
+7. DeviceTree Binding
+
+The documentation for PHY dt binding can be found @
+Documentation/devicetree/bindings/phy/phy-bindings.txt

drivers/phy/phy-core.c line 78

Quoted 8 lines
drivers/phy/phy-core.c new file +7 -0
@@ -0,0 +1,616 @@
 
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+	struct phy *phy;
+	struct device *dev;
+	struct class_dev_iter iter;
+
+	class_dev_iter_init(&iter, phy_class, NULL, NULL);

There is currently nothing preventing a call to this function before phy_class is initialized. It happened during my tests, and the nice stack dump showed that it was the PHY user attempting to get the PHY before the framework got initialized. So either phy_core_init should be a subsys_initcall or we need a better protection against phy_class being NULL or ERR_PTR in more places.

Quoted 48 lines
drivers/phy/phy-core.c new file +47 -0
+	while ((dev = class_dev_iter_next(&iter))) {
+		phy = container_of(dev, struct phy, dev);
+		if (node != phy->of_node)
+			continue;
+
+		class_dev_iter_exit(&iter);
+		return phy;
+	}
+
+	class_dev_iter_exit(&iter);
+	return ERR_PTR(-EPROBE_DEFER);
+}
 
+/**
+ * of_phy_get() - lookup and obtain a reference to a phy by phandle
+ * @dev: device that requests this phy
+ * @index: the index of the phy
+ *
+ * Returns the phy associated with the given phandle value,
+ * after getting a refcount to it or -ENODEV if there is no such phy or
+ * -EPROBE_DEFER if there is a phandle to the phy, but the device is
+ * not yet loaded.
+ */
+struct phy *of_phy_get(struct device *dev, int index)
+{
+	int ret;
+	struct phy *phy = NULL;
+	struct phy_bind *phy_map = NULL;
+	struct of_phandle_args args;
+	struct device_node *node;
+
+	if (!dev->of_node) {
+		dev_dbg(dev, "device does not have a device node entry\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+		index,&args);
+	if (ret) {
+		dev_dbg(dev, "failed to get phy in %s node\n",
+			dev->of_node->full_name);
+		return ERR_PTR(-ENODEV);
+	}
+
+	phy = of_phy_lookup(args.np);
+	if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+		phy = ERR_PTR(-EPROBE_DEFER);
+		goto err0;

drivers/phy/phy-core.c line 184

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+	}
+
+	phy = phy->ops->of_xlate(phy,&args);
+	if (IS_ERR(phy))
+		goto err1;
+
+	phy_map = phy_bind(dev_name(dev), index, dev_name(&phy->dev));
+	if (!IS_ERR(phy_map)) {
+		phy_map->phy = phy;
+		phy_map->auto_bind = true;

Shouldn't it be done with the phy_bind_mutex held ? I guess an unlocked version of the phy_bind functions is needed, so it can be used internally.

Quoted 46 lines
drivers/phy/phy-core.c new file +45 -0
+	}
+
+	get_device(&phy->dev);
+
+err1:
+	module_put(phy->ops->owner);
+
+err0:
+	of_node_put(node);
+
+	return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_get);
 
+/**
+ * phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Called to create a phy using phy framework.
+ */
+struct phy *phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv)
+{
+	int ret;
+	struct phy *phy;
+	struct phy_bind *phy_bind;
+	const char *devname = NULL;
+
+	if (!dev) {
+		dev_err(dev, "no device provided for PHY\n");
+		ret = -EINVAL;
+		goto err0;
+	}
+
+	if (!ops || !ops->of_xlate || !priv) {
+		dev_err(dev, "no PHY ops/PHY data provided\n");
+		ret = -EINVAL;
+		goto err0;
+	}
+

drivers/phy/phy-core.c line 373

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+	if (!phy_class)
+		phy_core_init();
+
+	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
+	if (!phy) {
+		ret = -ENOMEM;
+		goto err0;
+	}
+
+	devname = dev_name(dev);

OK, a sort of serious issue here is that you can't call phy_create() multiple times for same PHY provider device.

device_add() further will fail as sysfs won't let you create multiple objects with same name. So I would assume we need to add a new argument to phy_create() or rename @type to e.g. @phy_id, which could be concatenated with dev_name(dev) to create a unique name of the phy device.

And how is the PHY provider supposed to identify a PHY in its common PHY ops, now when the struct phy port field is removed ? I have temporarily (ab)used the type field in order to select proper registers within the phy ops.

drivers/phy/phy-core.c line 378

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+	device_initialize(&phy->dev);
+
+	phy->dev.class = phy_class;
+	phy->dev.parent = dev;
+	phy->label = label;

What about duplicating the string here ? That would make the API a bit more convenient and the callers wouldn't be required to keep all the labels around.

Quoted 28 lines
drivers/phy/phy-core.c new file +28 -0
+	phy->of_node = of_node;
+	phy->type = type;
+	phy->ops = ops;
+
+	dev_set_drvdata(&phy->dev, priv);
+
+	ret = dev_set_name(&phy->dev, "%s", devname);
+	if (ret)
+		goto err1;
+
+	mutex_lock(&phy_bind_mutex);
+	list_for_each_entry(phy_bind,&phy_bind_list, list)
+		if (!(strcmp(phy_bind->phy_dev_name, devname)))
+			phy_bind->phy = phy;
+	mutex_unlock(&phy_bind_mutex);
+
+	ret = device_add(&phy->dev);
+	if (ret)
+		goto err2;
+
+	return phy;
+
+err2:
+	phy_bind->phy = NULL;
+
+err1:
+	put_device(&phy->dev);
+	kfree(phy);

drivers/phy/phy-core.c line 416

Quoted 10 lines
drivers/phy/phy-core.c new file +9 -0
+
+err0:
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(phy_create);
 
+/**
+ * devm_phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @dev: device that is creating the new phy

Duplicated line.

Quoted 57 lines

+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Creates a new PHY device adding it to the PHY class.
+ * While at that, it also associates the device with the phy using devres.
+ * On driver detach, release function is invoked on the devres data,
+ * then, devres data is freed.
+ */

drivers/phy/phy-core.c new file +44 -0
+/**
+ * phy_bind() - bind the phy and the controller that uses the phy
+ * @dev_name: the device name of the device that will bind to the phy
+ * @index: index to specify the port number
+ * @phy_dev_name: the device name of the phy
+ *
+ * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
+ * be used when the phy driver registers the phy and when the controller
+ * requests this phy.
+ *
+ * To be used by platform specific initialization code.
+ */
+struct phy_bind *phy_bind(const char *dev_name, int index,
+				const char *phy_dev_name)
+{
+	struct phy_bind *phy_bind;
+
+	mutex_lock(&phy_bind_mutex);
+	list_for_each_entry(phy_bind,&phy_bind_list, list) {
+		if (!strcmp(phy_bind->dev_name, dev_name)&&  phy_bind->index ==
+			index) {
+			phy_bind->phy_dev_name = phy_dev_name;
+			goto ret0;
+		}
+	}
+
+	phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
+	if (!phy_bind) {
+		phy_bind = ERR_PTR(-ENOMEM);
+		goto ret0;
+	}
+
+	phy_bind->dev_name = dev_name;
+	phy_bind->phy_dev_name = phy_dev_name;
+	phy_bind->index = index;
+	phy_bind->auto_bind = false;
+
+	list_add_tail(&phy_bind->list,&phy_bind_list);
+
+ret0:
+	mutex_unlock(&phy_bind_mutex);
+	return phy_bind;
+}
+EXPORT_SYMBOL_GPL(phy_bind);
 

drivers/phy/phy-core.c line 557

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+static ssize_t phy_bind_show(struct device *dev,
+			     struct device_attribute *attr, char *buf)
+{
+	struct phy_bind *phy_bind;
+	struct phy *phy;
+	char *p = buf;
+	int len;
+
+	phy = container_of(dev, struct phy, dev);
+

Shouldn't this iteration also be protected with the phy_bind_mutex ?

Quoted 17 lines
drivers/phy/phy-core.c new file +16 -0
+	list_for_each_entry(phy_bind,&phy_bind_list, list)
+		if (phy_bind->phy == phy)
+			p += sprintf(p, "%s %d %s\n", phy_bind->dev_name,
+				phy_bind->index, phy_bind->phy_dev_name);
+	len = (p - buf);
+
+	return len;
+}
 
+static int phy_core_init(void)
+{
+	if (phy_class)
+		return 0;
+
+	phy_class = class_create(THIS_MODULE, "phy");
+	if (IS_ERR(phy_class)) {
+		pr_err("failed to create phy class -->  %ld\n",

drivers/phy/phy-core.c line 606

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+			PTR_ERR(phy_class));
+		return PTR_ERR(phy_class);
+	}
+
+	phy_class->dev_release = phy_release;
+	phy_class->dev_attrs = phy_dev_attrs;
+
+	return 0;
+}
+module_init(phy_core_init);

Having this framework initialized before the PHY provider and consumer drivers could save some unnecessary probe deferrals. I was wondering, what is really an advantage of having it as module_init(), rather than subsys_initcall() ?

Quoted 23 lines
drivers/phy/phy-core.c new file +9 -0
+static void __exit phy_core_exit(void)
+{
+	class_destroy(phy_class);
+}
+module_exit(phy_core_exit);
+
+MODULE_DESCRIPTION("Generic PHY Framework");
+MODULE_AUTHOR("Kishon Vijay Abraham I<[email protected]>");
+MODULE_LICENSE("GPL v2");
include/linux/phy/phy.h new file +7 -0
new file mode 100644
index 0000000..97a48bd
@@ -0,0 +1,228 @@
+/*
+ * phy.h -- generic phy header file
+ *
 
+/**
+ * struct phy - represent the phy device
+ * @dev: phy device
+ * @label: label given to phy

include/linux/phy/phy.h line 63

Quoted 10 lines
include/linux/phy/phy.h new file +10 -0
+ * @type: specifies the phy type
+ * @of_node: device node of the phy
+ * @ops: function pointers for performing phy operations
+ */
+struct phy {
+	struct device		dev;
+	const char		*label;
+	int			type;
+	struct device_node	*of_node;
+	struct phy_ops		*ops;

Something in this structure that would identify a PHY when a PHY provider supports multiple PHYs is probably needed.

Quoted 11 lines
include/linux/phy/phy.h new file +11 -0
+};
+
+/**
+ * struct phy_bind - represent the binding for the phy
+ * @dev_name: the device name of the device that will bind to the phy
+ * @phy_dev_name: the device name of the phy
+ * @index: used if a single controller uses multiple phys
+ * @auto_bind: tells if the binding is done explicitly from board file or not
+ * @phy: reference to the phy
+ * @list: to maintain a linked list of the binding information
+ */

include/linux/phy/phy.h

Quoted 10 lines
include/linux/phy/phy.h new file +10 -0
+struct phy_bind {
+	const char	*dev_name;
+	const char	*phy_dev_name;
+	int		index;
+	int		auto_bind:1;
+	struct phy	*phy;
+	struct list_head list;
+};
+
+static inline int phy_poweron(struct phy *phy)

phy_power_on ? Just a humble suggestion...

include/linux/phy/phy.h line 221

Quoted 8 lines
include/linux/phy/phy.h new file +8 -0
+{
+	if (phy->ops->power_on)
+		return phy->ops->power_on(phy);
+
+	return -EINVAL;
+}
+
+static inline int phy_poweroff(struct phy *phy)

and phy_power_off ? :-)

include/linux/phy/phy.h line 228

Quoted 7 lines
include/linux/phy/phy.h new file +7 -0
+{
+	if (phy->ops->power_off)
+		return phy->ops->power_off(phy);
+
+	return -EINVAL;
+}
+#endif /* __DRIVERS_PHY_H */

Thanks,
Sylwester

#9 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 1 line · Sylwester Nawrocki

On 04/03/2013 02:53 PM, Kishon Vijay Abraham I wrote:

.
.
<snip>
.
.

Quoted 99 lines
Quoted 16 lines
Documentation/phy.txt new file +4 -0
new file mode 100644
index 0000000..7785ec0
@@ -0,0 +1,113 @@
+                PHY SUBSYSTEM
+          Kishon Vijay Abraham I<[email protected]>
+
+This document explains the Generic PHY Framework along with the APIs

provided,

Documentation/phy.txt new file +5 -0
+and how-to-use.
+
+1. Introduction
+
+*PHY* is the abbreviation for physical layer. It is used to connect a
Quoted 3 lines

device
+to the physical medium e.g., the USB controller has a PHY to provide functions

Shouldn't it be "...medium, e.g. the USB..." ?

Quoted 5 lines

+such as serialization, de-serialization, encoding, decoding and is responsible

Documentation/phy.txt new file +2 -0
+for obtaining the required data transmission rate. Note that some USB
+controller has PHY functionality embedded into it and others use an

external

"controllers have PHY functionality embedded into them" ?

Quoted 1 line

+PHY. Other peripherals that uses a PHY include Wireless LAN, Ethernet,

s/uses/use ?

Quoted 9 lines
Documentation/phy.txt new file +3 -0
+SATA etc.
+
+The intention of creating this framework is to bring the phy drivers

spread

Documentation/phy.txt new file +4 -0
+all over the Linux kernel to drivers/phy to increase code re-use and to
+increase code maintainability.
+
+This framework will be of use only to devices that uses external PHY

(PHY

s/uses/use ?

Quoted 39 lines
Documentation/phy.txt new file +5 -0
+functionality is not embedded within the controller).
+
+2. Creating the PHY
+
+The PHY driver should create the PHY in order for other peripheral

controllers

Documentation/phy.txt new file +10 -0
+to make use of it. The PHY framework provides 2 APIs to create the PHY.
+
+struct phy *phy_create(struct device *dev, const char *label,
+    struct device_node *of_node, int type, struct phy_ops *ops,
+    void *priv);
+struct phy *devm_phy_create(struct device *dev, const char *label,
+    struct device_node *of_node, int type, struct phy_ops *ops,
+    void *priv);
+
+The PHY drivers can use one of the above 2 APIs to create the PHY by

passing

Documentation/phy.txt new file +2 -0
+the device pointer, label, device node, type, phy ops and a driver data.
+phy_ops is a set of function pointers for performing PHY operations

such as

Documentation/phy.txt new file +5 -0
+init, exit, suspend, resume, power_on and power_off.
+
+3. Binding the PHY to the controller
+
+The framework provides an API for binding the controller to the PHY

in the

Documentation/phy.txt new file +6 -0
+case of non dt boot.
+
+struct phy_bind *phy_bind(const char *dev_name, int index,
+                const char *phy_dev_name);
+
+The API fills the phy_bind structure with the dev_name (device name

of the

Documentation/phy.txt new file +2 -0
+controller), index and phy_dev_name (device name of the PHY). This will
+be used when the controller requests this phy. This API should be

used by

Documentation/phy.txt new file +3 -0
+platform specific initialization code (board file).
+
+In the case of dt boot, the binding information should be added in
Quoted 2 lines

the dt
+data of the controller.

s/in the dt data of/in the device tree node of ?

Quoted 9 lines

+4. Getting a reference to the PHY

Documentation/phy.txt new file +2 -0
+
+Before the controller can make use of the PHY, it has to get a

reference to

Documentation/phy.txt new file +5 -0
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int
Quoted 8 lines
Quoted 3 lines

index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);

Why do we need separate functions for dt and non-dt ? Couldn't it be handled
internally by the framework ? So the PHY users can use same calls for dt and non-dt, like in case of e.g. the regulators API ?

yeah. Indeed it makes sense to do it that way.

Quoted 7 lines

Also signatures of some functions are different now:

extern struct phy *phy_get(struct device *dev, int index); extern struct phy *devm_phy_get(struct device *dev, int index); extern struct phy *of_phy_get(struct device *dev, int index); extern struct phy *devm_of_phy_get(struct device *dev, int index);

My bad :-(

Quoted 3 lines

BTW, I think "extern" could be dropped, does it have any significance in function declarations in header files ?

it shouldn't have any effect I guess. It's harmless nevertheless.

Quoted 82 lines
Quoted 11 lines
Documentation/phy.txt new file +2 -0
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char

*string);

Documentation/phy.txt new file +2 -0
+
+phy_get and devm_phy_get can be used to get the PHY in non-dt boot.

This API
+uses the binding information added using the phy_bind API to find and return

Documentation/phy.txt new file +2 -0
+the appropriate PHY. The only difference between the two APIs is that
+devm_phy_get associates the device with the PHY using devres on
Quoted 3 lines

successful PHY
+get. On driver detach, release function is invoked on the the devres data and

s/the the/the

Quoted 12 lines
Documentation/phy.txt new file +3 -0
+devres data is freed.
+
+of_phy_get and devm_of_phy_get can be used to get the PHY in dt boot.

These

Documentation/phy.txt new file +2 -0
+APIs take the phandle and index to get a reference to the PHY. The only
+difference between the two APIs is that devm_of_phy_get associates

the device
+with the PHY using devres on successful phy get. On driver detach, release

Documentation/phy.txt new file +3 -0
+function is invoked on the devres data and it is freed.
+
+of_phy_get_byname and devm_of_phy_get_byname can also be used to get
Quoted 5 lines

the PHY
+in dt boot. It is same as the above API except that the user has to pass the
+phy name as filled in "phy-names" phandle in dt data and the framework will

s/phandle/property ?

Quoted 9 lines
Documentation/phy.txt new file +5 -0
+find the index and get the PHY.
+
+5. Releasing a reference to the PHY
+
+When the controller no longer needs the PHY, it has to release the

reference
+to the PHY it has obtained using the APIs mentioned in the above section. The
+PHY framework provides 2 APIS to release a reference to the PHY.

s/APIS/APIs ?

Quoted 18 lines
Documentation/phy.txt new file +5 -0
+
+void phy_put(struct phy *phy);
+void devm_phy_put(struct device *dev, struct phy *phy);
+
+Both these APIs are used to release a reference to the PHY and

devm_phy_put

Documentation/phy.txt new file +5 -0
+destroys the devres associated with this PHY.
+
+6. Destroying the PHY
+
+When the driver that created the PHY is unloaded, it should destroy

the PHY it

Documentation/phy.txt new file +6 -0
+created using one of the following 2 APIs.
+
+void phy_destroy(struct phy *phy);
+void devm_phy_destroy(struct device *dev, struct phy *phy);
+
+Both these APIs destroys the PHY and devm_phy_destroy destroys the
Quoted 1 line

devres

s/APIs destroys/APIs destroy ?

Quoted 10 lines
Documentation/phy.txt new file +6 -0
+associated with this PHY.
+
+7. DeviceTree Binding
+
+The documentation for PHY dt binding can be found @
+Documentation/devicetree/bindings/phy/phy-bindings.txt
drivers/phy/phy-core.c new file +0 -0
@@ -0,0 +1,616 @@
Quoted 14 lines

drivers/phy/phy-core.c line 78

Quoted 7 lines
drivers/phy/phy-core.c new file +7 -0
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+    struct phy *phy;
+    struct device *dev;
+    struct class_dev_iter iter;
+
+    class_dev_iter_init(&iter, phy_class, NULL, NULL);

There is currently nothing preventing a call to this function before phy_class is initialized. It happened during my tests, and the nice stack dump showed that it was the PHY user attempting to get the PHY before the framework got initialized. So either phy_core_init should be a subsys_initcall or we need a better protection against phy_class being NULL or ERR_PTR in more places.

Whats the initcall used in your PHY user? Looks like more people prefer having module_init and any issue because of it should be fixed in PHY providers and PHY users.

Quoted 54 lines
Quoted 54 lines
drivers/phy/phy-core.c new file +52 -0
+    while ((dev = class_dev_iter_next(&iter))) {
+        phy = container_of(dev, struct phy, dev);
+        if (node != phy->of_node)
+            continue;
+
+        class_dev_iter_exit(&iter);
+        return phy;
+    }
+
+    class_dev_iter_exit(&iter);
+    return ERR_PTR(-EPROBE_DEFER);
+}
 
+/**
+ * of_phy_get() - lookup and obtain a reference to a phy by phandle
+ * @dev: device that requests this phy
+ * @index: the index of the phy
+ *
+ * Returns the phy associated with the given phandle value,
+ * after getting a refcount to it or -ENODEV if there is no such phy or
+ * -EPROBE_DEFER if there is a phandle to the phy, but the device is
+ * not yet loaded.
+ */
+struct phy *of_phy_get(struct device *dev, int index)
+{
+    int ret;
+    struct phy *phy = NULL;
+    struct phy_bind *phy_map = NULL;
+    struct of_phandle_args args;
+    struct device_node *node;
+
+    if (!dev->of_node) {
+        dev_dbg(dev, "device does not have a device node entry\n");
+        return ERR_PTR(-EINVAL);
+    }
+
+    ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+        index,&args);
+    if (ret) {
+        dev_dbg(dev, "failed to get phy in %s node\n",
+            dev->of_node->full_name);
+        return ERR_PTR(-ENODEV);
+    }
+
+    phy = of_phy_lookup(args.np);
+    if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+        phy = ERR_PTR(-EPROBE_DEFER);
+        goto err0;
+    }
+
+    phy = phy->ops->of_xlate(phy,&args);
+    if (IS_ERR(phy))
+        goto err1;
Quoted 8 lines

drivers/phy/phy-core.c line 184

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+
+    phy_map = phy_bind(dev_name(dev), index, dev_name(&phy->dev));
+    if (!IS_ERR(phy_map)) {
+        phy_map->phy = phy;
+        phy_map->auto_bind = true;

Shouldn't it be done with the phy_bind_mutex held ? I guess an unlocked version of the phy_bind functions is needed, so it can be used internally.

The locking is done inside phy_bind function. I'm not sure but I vaguely remember getting a dump stack (incorrect mutex ordering or something) when trying to have phy_bind_mutex here. I can check it again.

Quoted 52 lines
Quoted 52 lines
drivers/phy/phy-core.c new file +50 -0
+    }
+
+    get_device(&phy->dev);
+
+err1:
+    module_put(phy->ops->owner);
+
+err0:
+    of_node_put(node);
+
+    return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_get);
 
+/**
+ * phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Called to create a phy using phy framework.
+ */
+struct phy *phy_create(struct device *dev, const char *label,
+    struct device_node *of_node, int type, struct phy_ops *ops,
+    void *priv)
+{
+    int ret;
+    struct phy *phy;
+    struct phy_bind *phy_bind;
+    const char *devname = NULL;
+
+    if (!dev) {
+        dev_err(dev, "no device provided for PHY\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+
+    if (!ops || !ops->of_xlate || !priv) {
+        dev_err(dev, "no PHY ops/PHY data provided\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+
+    if (!phy_class)
+        phy_core_init();
+
+    phy = kzalloc(sizeof(*phy), GFP_KERNEL);
+    if (!phy) {
Quoted 8 lines

drivers/phy/phy-core.c line 373

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+        ret = -ENOMEM;
+        goto err0;
+    }
+
+    devname = dev_name(dev);

OK, a sort of serious issue here is that you can't call phy_create() multiple times for same PHY provider device.

Ah.. right.

Quoted 6 lines

device_add() further will fail as sysfs won't let you create multiple objects with same name. So I would assume we need to add a new argument to phy_create() or rename @type to e.g. @phy_id, which could be concatenated with dev_name(dev) to create a unique name of the phy device.

hmm.. ok

Quoted 5 lines

And how is the PHY provider supposed to identify a PHY in its common PHY ops, now when the struct phy port field is removed ? I have temporarily (ab)used the type field in order to select proper registers within the phy ops.

Can't it be handled in the PHY provider driver without using struct phy *? Moreover the PHY ops passes on the *phy to phy provider right? Not sure I understand you here.

Quoted 10 lines

drivers/phy/phy-core.c line 378

Quoted 6 lines
drivers/phy/phy-core.c new file +5 -0
+    device_initialize(&phy->dev);
+
+    phy->dev.class = phy_class;
+    phy->dev.parent = dev;
+    phy->label = label;

What about duplicating the string here ? That would make the API a bit more convenient and the callers wouldn't be required to keep all the labels around.

you mean use dev_name? The device names are sometime more cryptic so preferred to have it like this.

Quoted 101 lines
Quoted 29 lines
drivers/phy/phy-core.c new file +28 -0
+    phy->of_node = of_node;
+    phy->type = type;
+    phy->ops = ops;
+
+    dev_set_drvdata(&phy->dev, priv);
+
+    ret = dev_set_name(&phy->dev, "%s", devname);
+    if (ret)
+        goto err1;
+
+    mutex_lock(&phy_bind_mutex);
+    list_for_each_entry(phy_bind,&phy_bind_list, list)
+        if (!(strcmp(phy_bind->phy_dev_name, devname)))
+            phy_bind->phy = phy;
+    mutex_unlock(&phy_bind_mutex);
+
+    ret = device_add(&phy->dev);
+    if (ret)
+        goto err2;
+
+    return phy;
+
+err2:
+    phy_bind->phy = NULL;
+
+err1:
+    put_device(&phy->dev);
+    kfree(phy);

drivers/phy/phy-core.c line 416

Quoted 10 lines
drivers/phy/phy-core.c new file +9 -0
+
+err0:
+    return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_GPL(phy_create);
 
+/**
+ * devm_phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @dev: device that is creating the new phy

Duplicated line.

Quoted 58 lines

+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Creates a new PHY device adding it to the PHY class.
+ * While at that, it also associates the device with the phy using devres.
+ * On driver detach, release function is invoked on the devres data,
+ * then, devres data is freed.
+ */

+/**
+ * phy_bind() - bind the phy and the controller that uses the phy
+ * @dev_name: the device name of the device that will bind to the phy
+ * @index: index to specify the port number
+ * @phy_dev_name: the device name of the phy
+ *
+ * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
+ * be used when the phy driver registers the phy and when the controller
+ * requests this phy.
+ *
+ * To be used by platform specific initialization code.
+ */

drivers/phy/phy-core.c new file +32 -0
+struct phy_bind *phy_bind(const char *dev_name, int index,
+                const char *phy_dev_name)
+{
+    struct phy_bind *phy_bind;
+
+    mutex_lock(&phy_bind_mutex);
+    list_for_each_entry(phy_bind,&phy_bind_list, list) {
+        if (!strcmp(phy_bind->dev_name, dev_name)&&  phy_bind->index ==
+            index) {
+            phy_bind->phy_dev_name = phy_dev_name;
+            goto ret0;
+        }
+    }
+
+    phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
+    if (!phy_bind) {
+        phy_bind = ERR_PTR(-ENOMEM);
+        goto ret0;
+    }
+
+    phy_bind->dev_name = dev_name;
+    phy_bind->phy_dev_name = phy_dev_name;
+    phy_bind->index = index;
+    phy_bind->auto_bind = false;
+
+    list_add_tail(&phy_bind->list,&phy_bind_list);
+
+ret0:
+    mutex_unlock(&phy_bind_mutex);
+    return phy_bind;
+}
+EXPORT_SYMBOL_GPL(phy_bind);
Quoted 12 lines

drivers/phy/phy-core.c line 557

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+static ssize_t phy_bind_show(struct device *dev,
+                 struct device_attribute *attr, char *buf)
+{
+    struct phy_bind *phy_bind;
+    struct phy *phy;
+    char *p = buf;
+    int len;
+
+    phy = container_of(dev, struct phy, dev);
+

Shouldn't this iteration also be protected with the phy_bind_mutex ?

Indeed.

Quoted 25 lines
Quoted 25 lines
drivers/phy/phy-core.c new file +23 -0
+    list_for_each_entry(phy_bind,&phy_bind_list, list)
+        if (phy_bind->phy == phy)
+            p += sprintf(p, "%s %d %s\n", phy_bind->dev_name,
+                phy_bind->index, phy_bind->phy_dev_name);
+    len = (p - buf);
+
+    return len;
+}
 
+static int phy_core_init(void)
+{
+    if (phy_class)
+        return 0;
+
+    phy_class = class_create(THIS_MODULE, "phy");
+    if (IS_ERR(phy_class)) {
+        pr_err("failed to create phy class -->  %ld\n",
+            PTR_ERR(phy_class));
+        return PTR_ERR(phy_class);
+    }
+
+    phy_class->dev_release = phy_release;
+    phy_class->dev_attrs = phy_dev_attrs;
+
Quoted 8 lines

drivers/phy/phy-core.c line 606

Quoted 3 lines
drivers/phy/phy-core.c new file +3 -0
+    return 0;
+}
+module_init(phy_core_init);

Having this framework initialized before the PHY provider and consumer drivers could save some unnecessary probe deferrals. I was wondering, what is really an advantage of having it as module_init(), rather than subsys_initcall() ?

I'm not sure of the exact reason but after the advent of EPROBE_DEFER, everyone recommends to use module_init only.

Thanks for the detailed review.

Regards
Kishon

#10 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 30 lines · Kishon Vijay Abraham I
Quoted 11 lines · Sylwester Nawrocki
Quoted 9 lines · Kishon Vijay Abraham I

+4. Getting a reference to the PHY

(quoted diff) +2 -0
+
+Before the controller can make use of the PHY, it has to get a

reference to

Documentation/phy.txt new file +5 -0
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int
Quoted 8 lines
Quoted 3 lines

index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);

Why do we need separate functions for dt and non-dt ? Couldn't it be handled
internally by the framework ? So the PHY users can use same calls for dt and non-dt, like in case of e.g. the regulators API ?

yeah. Indeed it makes sense to do it that way.

Quoted 6 lines

Also signatures of some functions are different now:

extern struct phy *phy_get(struct device *dev, int index); extern struct phy *devm_phy_get(struct device *dev, int index); extern struct phy *of_phy_get(struct device *dev, int index); extern struct phy *devm_of_phy_get(struct device *dev, int index);

Quoted 6 lines

My bad :-(

Quoted 2 lines

BTW, I think "extern" could be dropped, does it have any significance in function declarations in header files ?

it shouldn't have any effect I guess. It's harmless nevertheless.

Yup.

Quoted 23 lines
Quoted 8 lines
Quoted 7 lines
(quoted diff) +2 -0
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char

*string);

drivers/phy/phy-core.c new file +0 -0
@@ -0,0 +1,616 @@
Quoted 14 lines

drivers/phy/phy-core.c line 78

Quoted 7 lines
drivers/phy/phy-core.c new file +7 -0
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+    struct phy *phy;
+    struct device *dev;
+    struct class_dev_iter iter;
+
+    class_dev_iter_init(&iter, phy_class, NULL, NULL);

There is currently nothing preventing a call to this function before phy_class is initialized. It happened during my tests, and the nice stack dump showed that it was the PHY user attempting to get the PHY before the framework got initialized. So either phy_core_init should be a subsys_initcall or we need a better protection against phy_class being NULL or ERR_PTR in more places.

Quoted 1 line

Whats the initcall used in your PHY user? Looks like more people prefer having

It happened in a regular platform driver probe() callback.

Quoted 2 lines

module_init and any issue because of it should be fixed in PHY providers and PHY users.

OK. In fact this uncovered some issues in the MIPI DSI interface driver (some unexpected interrupts). But this should just be fixed in those drivers anyway. Now the DSI interface driver needs to wait for the PHY to be registered and ready, so the initialization will likely be changed regardless the framework initializes in module_init or earlier.

Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
drivers/phy/phy-core.c new file +52 -0
+    while ((dev = class_dev_iter_next(&iter))) {
+        phy = container_of(dev, struct phy, dev);
+        if (node != phy->of_node)
+            continue;
+
+        class_dev_iter_exit(&iter);
+        return phy;
+    }
+
+    class_dev_iter_exit(&iter);
+    return ERR_PTR(-EPROBE_DEFER);
+}
 
+/**
+ * of_phy_get() - lookup and obtain a reference to a phy by phandle
+ * @dev: device that requests this phy
+ * @index: the index of the phy
+ *
+ * Returns the phy associated with the given phandle value,
+ * after getting a refcount to it or -ENODEV if there is no such phy or
+ * -EPROBE_DEFER if there is a phandle to the phy, but the device is
+ * not yet loaded.
+ */
+struct phy *of_phy_get(struct device *dev, int index)
+{
+    int ret;
+    struct phy *phy = NULL;
+    struct phy_bind *phy_map = NULL;
+    struct of_phandle_args args;
+    struct device_node *node;
+
+    if (!dev->of_node) {
+        dev_dbg(dev, "device does not have a device node entry\n");
+        return ERR_PTR(-EINVAL);
+    }
+
+    ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+        index,&args);
+    if (ret) {
+        dev_dbg(dev, "failed to get phy in %s node\n",
+            dev->of_node->full_name);
+        return ERR_PTR(-ENODEV);
+    }
+
+    phy = of_phy_lookup(args.np);
+    if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+        phy = ERR_PTR(-EPROBE_DEFER);
+        goto err0;
+    }
+
+    phy = phy->ops->of_xlate(phy,&args);
+    if (IS_ERR(phy))
+        goto err1;
Quoted 8 lines

drivers/phy/phy-core.c line 184

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+
+    phy_map = phy_bind(dev_name(dev), index, dev_name(&phy->dev));
+    if (!IS_ERR(phy_map)) {
+        phy_map->phy = phy;
+        phy_map->auto_bind = true;

Shouldn't it be done with the phy_bind_mutex held ? I guess an unlocked version of the phy_bind functions is needed, so it can be used internally.

Quoted 3 lines

The locking is done inside phy_bind function. I'm not sure but I vaguely remember getting a dump stack (incorrect mutex ordering or something) when trying to have phy_bind_mutex here. I can check it again.

Yes, IIUC the locking needs to be reworked a bit so "phy_map" is not modified without the mutex held.

Quoted 60 lines
Quoted 51 lines
Quoted 51 lines
drivers/phy/phy-core.c new file +50 -0
+    }
+
+    get_device(&phy->dev);
+
+err1:
+    module_put(phy->ops->owner);
+
+err0:
+    of_node_put(node);
+
+    return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_get);
 
+/**
+ * phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Called to create a phy using phy framework.
+ */
+struct phy *phy_create(struct device *dev, const char *label,
+    struct device_node *of_node, int type, struct phy_ops *ops,
+    void *priv)
+{
+    int ret;
+    struct phy *phy;
+    struct phy_bind *phy_bind;
+    const char *devname = NULL;
+
+    if (!dev) {
+        dev_err(dev, "no device provided for PHY\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+
+    if (!ops || !ops->of_xlate || !priv) {
+        dev_err(dev, "no PHY ops/PHY data provided\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+
+    if (!phy_class)
+        phy_core_init();
+
+    phy = kzalloc(sizeof(*phy), GFP_KERNEL);
+    if (!phy) {
Quoted 8 lines

drivers/phy/phy-core.c line 373

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+        ret = -ENOMEM;
+        goto err0;
+    }
+
+    devname = dev_name(dev);

OK, a sort of serious issue here is that you can't call phy_create() multiple times for same PHY provider device.

Quoted 1 line

Ah.. right.

The other question I had was why we needed struct device object for each PHY. And if it wouldn't be enough to have only struct device * in struct phy, and the real device would be the PHY provider only. I might be missing some reasons behind this decision though, as I have not been following your work since the beginning.

Quoted 6 lines
Quoted 5 lines

device_add() further will fail as sysfs won't let you create multiple objects with same name. So I would assume we need to add a new argument to phy_create() or rename @type to e.g. @phy_id, which could be concatenated with dev_name(dev) to create a unique name of the phy device.

Quoted 8 lines

hmm.. ok

Quoted 5 lines

And how is the PHY provider supposed to identify a PHY in its common PHY ops, now when the struct phy port field is removed ? I have temporarily (ab)used the type field in order to select proper registers within the phy ops.

Can't it be handled in the PHY provider driver without using struct phy *?

You need to select registers/bit fields corresponding to a specific PHY, since the phy ops are common. So we need to know exactly with which phy we deal at the moment and I can't see any other option to figure out that than by dereferencing struct phy *... ;)

Quoted 1 line

Moreover the PHY ops passes on the *phy to phy provider right? Not sure I

Yes, all you get in the ops is phy *. I wanted to avoid global variables in the provider driver and be able to get to any provider private data from phy * only.

If I use something like below for the PHY provider private data then from struct phy * only I would need to get an index. This could be looked up by iterating over phys[] array, but that seems an unnecessary complication. Maybe there are better/easier ways to resolve it, without adding a new field to struct phy.

struct phy_povider_priv {
	struct phy *phys[4];
	...
};

static in phy_power_on(struct phy *phy)

{
	struct phy_provider_priv *priv = dev_get_drvdata(&phy->dev);
	int phy_id = ...
	....
}
...
struct phy_ops ops = {
	.power_on = phy_power_on,
	...
};
Quoted 8 lines

understand you here.

Quoted 6 lines
Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+    device_initialize(&phy->dev);
+
+    phy->dev.class = phy_class;
+    phy->dev.parent = dev;
+    phy->label = label;
Quoted 6 lines
Quoted 3 lines

What about duplicating the string here ? That would make the API a bit more convenient and the callers wouldn't be required to keep all the labels around.

you mean use dev_name? The device names are sometime more cryptic so preferred to have it like this.

No, I meant something like:

phy->label = kstrdup(label, GFP_KERNEL);

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
drivers/phy/phy-core.c new file +15 -0
+static int phy_core_init(void)
+{
+    if (phy_class)
+        return 0;
+
+    phy_class = class_create(THIS_MODULE, "phy");
+    if (IS_ERR(phy_class)) {
+        pr_err("failed to create phy class -->  %ld\n",
+            PTR_ERR(phy_class));
+        return PTR_ERR(phy_class);
+    }
+
+    phy_class->dev_release = phy_release;
+    phy_class->dev_attrs = phy_dev_attrs;
+
Quoted 8 lines

drivers/phy/phy-core.c line 606

Quoted 3 lines
drivers/phy/phy-core.c new file +3 -0
+    return 0;
+}
+module_init(phy_core_init);

Having this framework initialized before the PHY provider and consumer drivers could save some unnecessary probe deferrals. I was wondering, what is really an advantage of having it as module_init(), rather than subsys_initcall() ?

Quoted 2 lines

I'm not sure of the exact reason but after the advent of EPROBE_DEFER, everyone recommends to use module_init only.

Ah...OK. We just need to ensure the resources are properly checked before they are used then.

Quoted 1 line

Thanks for the detailed review.

Regards,
Sylwester

#11 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 207 lines · Sylwester Nawrocki

Hi,

Quoted 30 lines · Kishon Vijay Abraham I
Quoted 11 lines · Sylwester Nawrocki
Quoted 9 lines · Kishon Vijay Abraham I

+4. Getting a reference to the PHY

(quoted diff) +2 -0
+
+Before the controller can make use of the PHY, it has to get a

reference to

Documentation/phy.txt new file +5 -0
+it. This framework provides 6 APIs to get a reference to the PHY.
+
+struct phy *phy_get(struct device *dev, int index);
+struct phy *devm_phy_get(struct device *dev, int index);
+struct phy *of_phy_get(struct device *dev, const char *phandle, int
Quoted 8 lines
Quoted 3 lines

index);
+struct phy *devm_of_phy_get(struct device *dev, const char *phandle, int index);

Why do we need separate functions for dt and non-dt ? Couldn't it be handled
internally by the framework ? So the PHY users can use same calls for dt and non-dt, like in case of e.g. the regulators API ?

yeah. Indeed it makes sense to do it that way.

Quoted 6 lines

Also signatures of some functions are different now:

extern struct phy *phy_get(struct device *dev, int index); extern struct phy *devm_phy_get(struct device *dev, int index); extern struct phy *of_phy_get(struct device *dev, int index); extern struct phy *devm_of_phy_get(struct device *dev, int index);

Quoted 6 lines

My bad :-(

Quoted 2 lines

BTW, I think "extern" could be dropped, does it have any significance in function declarations in header files ?

it shouldn't have any effect I guess. It's harmless nevertheless.

Yup.

Quoted 23 lines
Quoted 8 lines
Quoted 7 lines
(quoted diff) +2 -0
+struct phy *of_phy_get_byname(struct device *dev, const char *string);
+struct phy *devm_of_phy_get_byname(struct device *dev, const char

*string);

drivers/phy/phy-core.c new file +0 -0
@@ -0,0 +1,616 @@
Quoted 14 lines

drivers/phy/phy-core.c line 78

Quoted 7 lines
drivers/phy/phy-core.c new file +7 -0
+static struct phy *of_phy_lookup(struct device_node *node)
+{
+    struct phy *phy;
+    struct device *dev;
+    struct class_dev_iter iter;
+
+    class_dev_iter_init(&iter, phy_class, NULL, NULL);

There is currently nothing preventing a call to this function before phy_class is initialized. It happened during my tests, and the nice stack dump showed that it was the PHY user attempting to get the PHY before the framework got initialized. So either phy_core_init should be a subsys_initcall or we need a better protection against phy_class being NULL or ERR_PTR in more places.

Quoted 1 line

Whats the initcall used in your PHY user? Looks like more people prefer having

It happened in a regular platform driver probe() callback.

Quoted 2 lines

module_init and any issue because of it should be fixed in PHY providers and PHY users.

OK. In fact this uncovered some issues in the MIPI DSI interface driver (some unexpected interrupts). But this should just be fixed in those drivers anyway. Now the DSI interface driver needs to wait for the PHY to be registered and ready, so the initialization will likely be changed regardless the framework initializes in module_init or earlier.

Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
drivers/phy/phy-core.c new file +52 -0
+    while ((dev = class_dev_iter_next(&iter))) {
+        phy = container_of(dev, struct phy, dev);
+        if (node != phy->of_node)
+            continue;
+
+        class_dev_iter_exit(&iter);
+        return phy;
+    }
+
+    class_dev_iter_exit(&iter);
+    return ERR_PTR(-EPROBE_DEFER);
+}
 
+/**
+ * of_phy_get() - lookup and obtain a reference to a phy by phandle
+ * @dev: device that requests this phy
+ * @index: the index of the phy
+ *
+ * Returns the phy associated with the given phandle value,
+ * after getting a refcount to it or -ENODEV if there is no such phy or
+ * -EPROBE_DEFER if there is a phandle to the phy, but the device is
+ * not yet loaded.
+ */
+struct phy *of_phy_get(struct device *dev, int index)
+{
+    int ret;
+    struct phy *phy = NULL;
+    struct phy_bind *phy_map = NULL;
+    struct of_phandle_args args;
+    struct device_node *node;
+
+    if (!dev->of_node) {
+        dev_dbg(dev, "device does not have a device node entry\n");
+        return ERR_PTR(-EINVAL);
+    }
+
+    ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
+        index,&args);
+    if (ret) {
+        dev_dbg(dev, "failed to get phy in %s node\n",
+            dev->of_node->full_name);
+        return ERR_PTR(-ENODEV);
+    }
+
+    phy = of_phy_lookup(args.np);
+    if (IS_ERR(phy) || !try_module_get(phy->ops->owner)) {
+        phy = ERR_PTR(-EPROBE_DEFER);
+        goto err0;
+    }
+
+    phy = phy->ops->of_xlate(phy,&args);
+    if (IS_ERR(phy))
+        goto err1;
Quoted 8 lines

drivers/phy/phy-core.c line 184

Quoted 5 lines
drivers/phy/phy-core.c new file +5 -0
+
+    phy_map = phy_bind(dev_name(dev), index, dev_name(&phy->dev));
+    if (!IS_ERR(phy_map)) {
+        phy_map->phy = phy;
+        phy_map->auto_bind = true;

Shouldn't it be done with the phy_bind_mutex held ? I guess an unlocked version of the phy_bind functions is needed, so it can be used internally.

Quoted 3 lines

The locking is done inside phy_bind function. I'm not sure but I vaguely remember getting a dump stack (incorrect mutex ordering or something) when trying to have phy_bind_mutex here. I can check it again.

Yes, IIUC the locking needs to be reworked a bit so "phy_map" is not modified without the mutex held.

Quoted 60 lines
Quoted 59 lines
Quoted 46 lines
drivers/phy/phy-core.c new file +45 -0
+    }
+
+    get_device(&phy->dev);
+
+err1:
+    module_put(phy->ops->owner);
+
+err0:
+    of_node_put(node);
+
+    return phy;
+}
+EXPORT_SYMBOL_GPL(of_phy_get);
 
+/**
+ * phy_create() - create a new phy
+ * @dev: device that is creating the new phy
+ * @label: label given to phy
+ * @of_node: device node of the phy
+ * @type: specifies the phy type
+ * @ops: function pointers for performing phy operations
+ * @priv: private data from phy driver
+ *
+ * Called to create a phy using phy framework.
+ */
+struct phy *phy_create(struct device *dev, const char *label,
+    struct device_node *of_node, int type, struct phy_ops *ops,
+    void *priv)
+{
+    int ret;
+    struct phy *phy;
+    struct phy_bind *phy_bind;
+    const char *devname = NULL;
+
+    if (!dev) {
+        dev_err(dev, "no device provided for PHY\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+
+    if (!ops || !ops->of_xlate || !priv) {
+        dev_err(dev, "no PHY ops/PHY data provided\n");
+        ret = -EINVAL;
+        goto err0;
+    }
+

drivers/phy/phy-core.c line 373

Quoted 10 lines
drivers/phy/phy-core.c new file +10 -0
+    if (!phy_class)
+        phy_core_init();
+
+    phy = kzalloc(sizeof(*phy), GFP_KERNEL);
+    if (!phy) {
+        ret = -ENOMEM;
+        goto err0;
+    }
+
+    devname = dev_name(dev);

OK, a sort of serious issue here is that you can't call phy_create() multiple times for same PHY provider device.

Quoted 5 lines
Quoted 1 line

Ah.. right.

The other question I had was why we needed struct device object for each PHY. And if it wouldn't be enough to have only struct device * in struct phy, and the real device would be the PHY provider only. I might

IMO the PHY framework shouldn't be modifying the properties of device of some other driver. Consider the case where the PHY provider implements other functions also (in addition to acting as a PHY) in which case the PHY provider entirely doesn't come under *PHY class* but only a part of it. There might be other reasons as well.

Thanks
Kishon

#12 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

In reply to Kishon Vijay Abraham I
Quoted 19 lines · Kishon Vijay Abraham I

The PHY framework provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

PHY drivers should create the PHY by passing phy_descriptor that has describes the PHY (label, type etc..) and ops like init, exit, suspend, resume, power_on, power_off.

The documentation for the generic PHY framework is added in Documentation/phy.txt and the documentation for the sysfs entry is added in Documentation/ABI/testing/sysfs-class-phy and the documentation for dt binding is can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

include/linux/phy/phy.h line 100

Quoted 4 lines
include/linux/phy/phy.h +3 -0
 
+extern struct phy *devm_phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);

Can't the function get of_node from dev->of_node?

I wonder if we shouldn't split up the registration a bit though:

A function which registers a PHY object itself. That's the function above.

A function which registers a DT-based PHY provider.

Then, the of_xlate op would be part of the PHY provider, not part of some random PHY that happens to exist on that node. So:

struct phy {
	struct device *dev;
	struct module *owner;
	int	(*init)(struct phy *phy);
	int	(*exit)(struct phy *phy);
	int	(*suspend)(struct phy *phy);
	int	(*resume)(struct phy *phy);
	int	(*power_on)(struct phy *phy);
	int	(*power_off)(struct phy *phy);
};

int phy_register(struct phy *phy);

All PHY providers would use that API, whether running in a DT-base system or not.

struct of_phy_provider {
	struct device *dev;
	struct phy * (*of_xlate)(struct of_phy_provider *provider,
			struct of_phandle_args *args);
};

int phy_register_of_provider(struct of_phy_provider *provider);

Only DT-based PHY providers would use that API.

... or something like that?

phy_get() would do something like:

	if dev->of_node:
		# look up using registerd of_phy_providers
		phy = phy_get_of(...)
		if phy: return phy
	# now look up using whatever other mapping table exists
	phy = ...
	return phy
#13 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

Hi,

Quoted 7 lines · Stephen Warren
struct phy {
	struct device *dev;
	struct module *owner;
	int	(*init)(struct phy *phy);
	int	(*exit)(struct phy *phy);
	int	(*suspend)(struct phy *phy);
	int	(*resume)(struct phy *phy);

I wonder whether it makes sense to provide ->suspend/->resume callbacks here. We already have dev_pm_ops providing those methods and we can just wrap pm_runtime_* calls to be called by consumers.

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130404/c71b8872/attachment.sig>
#14 lore raw

Re: [PATCH v5 1/6] drivers: phy: add generic PHY framework

In reply to Stephen Warren

Hi,

Quoted 27 lines · Stephen Warren
Quoted 19 lines · Kishon Vijay Abraham I

The PHY framework provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

PHY drivers should create the PHY by passing phy_descriptor that has describes the PHY (label, type etc..) and ops like init, exit, suspend, resume, power_on, power_off.

The documentation for the generic PHY framework is added in Documentation/phy.txt and the documentation for the sysfs entry is added in Documentation/ABI/testing/sysfs-class-phy and the documentation for dt binding is can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

include/linux/phy/phy.h line 100

Quoted 4 lines
include/linux/phy/phy.h +3 -0
 
+extern struct phy *devm_phy_create(struct device *dev, const char *label,
+	struct device_node *of_node, int type, struct phy_ops *ops,
+	void *priv);

Can't the function get of_node from dev->of_node?

Quoted 8 lines

I wonder if we shouldn't split up the registration a bit though:

A function which registers a PHY object itself. That's the function above.

A function which registers a DT-based PHY provider.

Then, the of_xlate op would be part of the PHY provider, not part of some random PHY that happens to exist on that node. So:

Yeah. Makes sense to me. By this we can avoid struct phy * to represent two different things (phy provider and phy instance).

Thanks
Kishon

#15 lore raw

PATCH v5 2/6 usb: phy: omap-usb2: use the new generic PHY framework

In reply to Kishon Vijay Abraham I

Used the generic PHY framework API to create the PHY. omap_usb2_suspend is split into omap_usb_suspend and omap_usb_resume in order to align with the new framework.

However using the old USB PHY library cannot be completely removed because OTG is intertwined with PHY and moving to the new framework will break OTG. Once we have a separate OTG state machine, we can get rid of the USB PHY library.

---
 drivers/usb/phy/omap-usb2.c |   48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
drivers/usb/phy/omap-usb2.c +48 -0
index 844ab68..4e48db4 100644
@@ -28,6 +28,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/delay.h>
 #include <linux/usb/omap_control_usb.h>
+#include <linux/phy/phy.h>
 
 /**
  * omap_usb2_set_comparator - links the comparator present in the sytem with
@@ -119,9 +120,49 @@ static int omap_usb2_suspend(struct usb_phy *x, int suspend)
 	return 0;
 }
 
+static int omap_usb_suspend(struct phy *x)
+{
+	struct omap_usb *phy = dev_get_drvdata(&x->dev);
+
+	if (!phy->is_suspended) {
+		omap_control_usb_phy_power(phy->control_dev, 0);
+		pm_runtime_put_sync(phy->dev);
+		phy->is_suspended = 1;
+	}
+
+	return 0;
+}
+
+static int omap_usb_resume(struct phy *x)
+{
+	u32 ret;
+	struct omap_usb *phy = dev_get_drvdata(&x->dev);
+
+	if (phy->is_suspended) {
+		ret = pm_runtime_get_sync(phy->dev);
+		if (ret < 0) {
+			dev_err(phy->dev, "get_sync failed with err %d\n",
+									ret);
+			return ret;
+		}
+		omap_control_usb_phy_power(phy->control_dev, 1);
+		phy->is_suspended = 0;
+	}
+
+	return 0;
+}
+
+static struct phy_ops ops = {
+	.suspend	= omap_usb_suspend,
+	.resume		= omap_usb_resume,
+	.of_xlate	= of_phy_xlate,
+	.owner		= THIS_MODULE,
+};
+
 static int omap_usb2_probe(struct platform_device *pdev)
 {
 	struct omap_usb			*phy;
+	struct phy			*generic_phy;
 	struct usb_otg			*otg;
 
 	phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
@@ -144,6 +185,13 @@ static int omap_usb2_probe(struct platform_device *pdev)
 	phy->phy.otg		= otg;
 	phy->phy.type		= USB_PHY_TYPE_USB2;
 
+	generic_phy = devm_phy_create(phy->dev, "omap-usb2", pdev->dev.of_node,
+		USB_PHY_TYPE_USB2, &ops, phy);
+	if (IS_ERR(generic_phy)) {
+		dev_dbg(&pdev->dev, "Failed to create PHY\n");
+		return PTR_ERR(generic_phy);
+	}
+
 	phy->control_dev = omap_get_control_dev();
 	if (IS_ERR(phy->control_dev)) {
 		dev_dbg(&pdev->dev, "Failed to get control device\n");
Signature · 1 line
1.7.10.4
#16 lore raw

Re: [PATCH v5 2/6] usb: phy: omap-usb2: use the new generic PHY framework

Quoted 53 lines · Kishon Vijay Abraham I

Used the generic PHY framework API to create the PHY. omap_usb2_suspend is split into omap_usb_suspend and omap_usb_resume in order to align with the new framework.

However using the old USB PHY library cannot be completely removed because OTG is intertwined with PHY and moving to the new framework will break OTG. Once we have a separate OTG state machine, we can get rid of the USB PHY library.

---
 drivers/usb/phy/omap-usb2.c |   48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
drivers/usb/phy/omap-usb2.c +24 -0
index 844ab68..4e48db4 100644
@@ -28,6 +28,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/delay.h>
 #include <linux/usb/omap_control_usb.h>
+#include <linux/phy/phy.h>
 
 /**
  * omap_usb2_set_comparator - links the comparator present in the sytem with
@@ -119,9 +120,49 @@ static int omap_usb2_suspend(struct usb_phy *x, int suspend)
 	return 0;
 }
 
+static int omap_usb_suspend(struct phy *x)
+{
+	struct omap_usb *phy = dev_get_drvdata(&x->dev);
+
+	if (!phy->is_suspended) {
+		omap_control_usb_phy_power(phy->control_dev, 0);
+		pm_runtime_put_sync(phy->dev);
+		phy->is_suspended = 1;
+	}
+
+	return 0;
+}
+
+static int omap_usb_resume(struct phy *x)
+{
+	u32 ret;
+	struct omap_usb *phy = dev_get_drvdata(&x->dev);
+
+	if (phy->is_suspended) {
+		ret = pm_runtime_get_sync(phy->dev);
+		if (ret < 0) {
+			dev_err(phy->dev, "get_sync failed with err %d\n",
+									ret);

drivers/usb/phy/omap-usb2.c static int omap_usb2_suspend(struct usb_phy *x, int suspend) · line 155

Quoted 10 lines
drivers/usb/phy/omap-usb2.c +10 -0
+			return ret;
+		}
+		omap_control_usb_phy_power(phy->control_dev, 1);
+		phy->is_suspended = 0;
+	}
+
+	return 0;
+}
+
+static struct phy_ops ops = {

const ? Maybe provide a:

#define DEFINE_PHY_OPS(name) \
const struct phy_ops #name_phy_ops = {

macro ? This will force people to add the const keyword :-)

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130403/c783de21/attachment.sig>
#17 lore raw

Re: [PATCH v5 2/6] usb: phy: omap-usb2: use the new generic PHY framework

Quoted 6 lines · Felipe Balbi

const ? Maybe provide a:

#define DEFINE_PHY_OPS(name) \
const struct phy_ops #name_phy_ops = {

macro ? This will force people to add the const keyword :-)

Forcing people to use const structures is good, but I think it would be better without the macro, just by marking the argument in devm_phy_create() as const.

Arnd

#18 lore raw

Re: [PATCH v5 2/6] usb: phy: omap-usb2: use the new generic PHY framework

Hi,

Quoted 11 lines · Arnd Bergmann
Quoted 6 lines · Felipe Balbi

const ? Maybe provide a:

#define DEFINE_PHY_OPS(name) \
const struct phy_ops #name_phy_ops = {

macro ? This will force people to add the const keyword :-)

Forcing people to use const structures is good, but I think it would be better without the macro, just by marking the argument in devm_phy_create() as const.

that won't force the definition of the struct to be const, however. But I get your point.

Signature · 8 lines
balbi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20130403/661c611c/attachment-0001.sig>
#19 lore raw

PATCH v5 3/6 usb: otg: twl4030: use the new generic PHY framework

In reply to Kishon Vijay Abraham I

Used the generic PHY framework API to create the PHY. twl4030_usb_suspend and twl4030_usb_resume is added to phy_ops in order to align with the new framework.

However using the old USB PHY library cannot be completely removed because OTG is intertwined with PHY and moving to the new framework completely will break OTG. Once we have a separate OTG state machine, we can get rid of the USB PHY library.

---
 drivers/usb/otg/twl4030-usb.c |   37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/usb/otg/twl4030-usb.c +37 -0
index a994715..6dab049 100644
@@ -33,6 +33,7 @@
 #include <linux/io.h>
 #include <linux/delay.h>
 #include <linux/usb/otg.h>
+#include <linux/phy/phy.h>
 #include <linux/usb/musb-omap.h>
 #include <linux/usb/ulpi.h>
 #include <linux/i2c/twl.h>
@@ -575,10 +576,39 @@ static int twl4030_set_host(struct usb_otg *otg, struct usb_bus *host)
 	return 0;
 }
 
+static int twl4030_usb_suspend(struct phy *phy)
+{
+	struct twl4030_usb *twl = dev_get_drvdata(&phy->dev);
+
+	twl4030_phy_suspend(twl, 1);
+
+	return 0;
+}
+
+static int twl4030_usb_resume(struct phy *phy)
+{
+	struct twl4030_usb *twl = dev_get_drvdata(&phy->dev);
+
+	if (!twl->asleep)
+		return -EBUSY;
+	__twl4030_phy_resume(twl);
+	twl->asleep = 0;
+
+	return 0;
+}
+
+static struct phy_ops ops = {
+	.suspend	= twl4030_usb_suspend,
+	.resume		= twl4030_usb_resume,
+	.of_xlate	= of_phy_xlate,
+	.owner		= THIS_MODULE,
+};
+
 static int twl4030_usb_probe(struct platform_device *pdev)
 {
 	struct twl4030_usb_data *pdata = pdev->dev.platform_data;
 	struct twl4030_usb	*twl;
+	struct phy		*phy;
 	int			status, err;
 	struct usb_otg		*otg;
 	struct device_node	*np = pdev->dev.of_node;
@@ -617,6 +647,13 @@ static int twl4030_usb_probe(struct platform_device *pdev)
 	otg->set_host		= twl4030_set_host;
 	otg->set_peripheral	= twl4030_set_peripheral;
 
+	phy = devm_phy_create(twl->dev, "twl4030", pdev->dev.of_node,
+		USB_PHY_TYPE_USB2, &ops, twl);
+	if (IS_ERR(phy)) {
+		dev_dbg(&pdev->dev, "Failed to create PHY\n");
+		return PTR_ERR(phy);
+	}
+
 	/* init spinlock for workqueue */
 	spin_lock_init(&twl->lock);
 
Signature · 1 line
1.7.10.4
#20 lore raw

PATCH v5 4/6 ARM: OMAP: USB: Add phy binding information

In reply to Kishon Vijay Abraham I

In order for controllers to get PHY in case of non dt boot, the phy binding information should be added in the platform specific initialization code using phy_bind.

---
 arch/arm/mach-omap2/usb-musb.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm/mach-omap2/usb-musb.c +6 -1
index 3242a55..f01bc42 100644
@@ -24,6 +24,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/io.h>
 #include <linux/usb/musb.h>
+#include <linux/phy/phy.h>
 
 #include "omap_device.h"
 #include "soc.h"
@@ -85,8 +86,12 @@ void __init usb_musb_init(struct omap_musb_board_data *musb_board_data)
 	musb_plat.mode = board_data->mode;
 	musb_plat.extvbus = board_data->extvbus;
 
-	if (cpu_is_omap44xx())
+	if (cpu_is_omap44xx()) {
 		musb_plat.has_mailbox = true;
+		phy_bind("musb-hdrc.0.auto", 0, "omap-usb2.1.auto");
+	} else if (cpu_is_omap34xx()) {
+		phy_bind("musb-hdrc.0.auto", 0, "twl4030_usb");
+	}
 
 	if (soc_is_am35xx()) {
 		oh_name = "am35x_otg_hs";
Signature · 1 line
1.7.10.4
#21 lore raw

PATCH v5 5/6 ARM: dts: omap: update usb_otg_hs data

In reply to Kishon Vijay Abraham I

Updated the usb_otg_hs dt data to include the *phy* and *phy-names* binding in order for the driver to use the new generic PHY framework. Also updated the Documentation to include the binding information. The PHY binding information can be found at Documentation/devicetree/bindings/phy/phy-bindings.txt

---
 Documentation/devicetree/bindings/usb/omap-usb.txt |    5 +++++
 Documentation/devicetree/bindings/usb/usb-phy.txt  |    6 ++++++
 arch/arm/boot/dts/omap3.dtsi                       |    2 ++
 arch/arm/boot/dts/omap4.dtsi                       |    3 +++
 arch/arm/boot/dts/twl4030.dtsi                     |    1 +
 5 files changed, 17 insertions(+)
Documentation/devicetree/bindings/usb/omap-usb.txt +5 -0
index abce256..9324e79 100644
@@ -19,6 +19,9 @@ OMAP MUSB GLUE
  - power : Should be "50". This signifies the controller can supply upto
    100mA when operating in host mode.
  - usb-phy : the phandle for the PHY device
+ - phys : the phandle for the PHY device (used by generic PHY framework)
+ - phy-names : the names of the PHY corresponding to the PHYs present in the
+   *phy* phandle.
 
 Optional properties:
  - ctrl-module : phandle of the control module this glue uses to write to
@@ -33,6 +36,8 @@ usb_otg_hs: usb_otg_hs at 4a0ab000 {
 	num_eps = <16>;
 	ram_bits = <12>;
 	ctrl-module = <&omap_control_usb>;
+	phys = <&usb2_phy>;
+	phy-names = "usb2-phy";
 };
 
 Board specific device node entry
Documentation/devicetree/bindings/usb/usb-phy.txt +6 -0
index 61496f5..c0245c8 100644
@@ -5,6 +5,8 @@ OMAP USB2 PHY
 Required properties:
  - compatible: Should be "ti,omap-usb2"
  - reg : Address and length of the register set for the device.
+ - #phy-cells: determine the number of cells that should be given in the
+   phandle while referencing this phy.
 
 Optional properties:
  - ctrl-module : phandle of the control module used by PHY driver to power on
@@ -16,6 +18,7 @@ usb2phy at 4a0ad080 {
 	compatible = "ti,omap-usb2";
 	reg = <0x4a0ad080 0x58>;
 	ctrl-module = <&omap_control_usb>;
+	#phy-cells = <0>;
 };
 
 OMAP USB3 PHY
@@ -25,6 +28,8 @@ Required properties:
  - reg : Address and length of the register set for the device.
  - reg-names: The names of the register addresses corresponding to the registers
    filled in "reg".
+ - #phy-cells: determine the number of cells that should be given in the
+   phandle while referencing this phy.
 
 Optional properties:
  - ctrl-module : phandle of the control module used by PHY driver to power on
@@ -39,4 +44,5 @@ usb3phy at 4a084400 {
 	      <0x4a084c00 0x40>;
 	reg-names = "phy_rx", "phy_tx", "pll_ctrl";
 	ctrl-module = <&omap_control_usb>;
+	#phy-cells = <0>;
 };
arch/arm/boot/dts/omap3.dtsi +2 -0
index 1e21565..dd7d2ff 100644
@@ -405,6 +405,8 @@
 			interrupt-names = "mc", "dma";
 			ti,hwmods = "usb_otg_hs";
 			usb-phy = <&usb2_phy>;
+			phys = <&usb2_phy>;
+			phy-names = "usb2-phy";
 			multipoint = <1>;
 			num-eps = <16>;
 			ram-bits = <12>;
arch/arm/boot/dts/omap4.dtsi +3 -0
index 06d044e..64cd000 100644
@@ -442,6 +442,7 @@
 				compatible = "ti,omap-usb2";
 				reg = <0x4a0ad080 0x58>;
 				ctrl-module = <&omap_control_usb>;
+				#phy-cells = <0>;
 			};
 		};
 
@@ -550,6 +551,8 @@
 			interrupt-names = "mc", "dma";
 			ti,hwmods = "usb_otg_hs";
 			usb-phy = <&usb2_phy>;
+			phys = <&usb2_phy>;
+			phy-names = "usb2-phy";
 			multipoint = <1>;
 			num-eps = <16>;
 			ram-bits = <12>;
arch/arm/boot/dts/twl4030.dtsi +1 -0
index 398d2c3..6f71de7 100644
@@ -74,5 +74,6 @@
 		usb1v8-supply = <&vusb1v8>;
 		usb3v1-supply = <&vusb3v1>;
 		usb_mode = <1>;
+		#phy-cells = <0>;
 	};
 };
Signature · 1 line
1.7.10.4
#22 lore raw

PATCH v5 6/6 usb: musb: omap2430: use the new generic PHY framework

In reply to Kishon Vijay Abraham I

Use the generic PHY framework API to get the PHY. The usb_phy_set_suspend and usb_phy_set_resume is replaced with phy_suspend and phy_resume to align with the new PHY framework.

musb->xceiv can't be removed as of now because musb core uses xceiv.state and xceiv.otg. Once there is a separate state machine to handle otg, these can be moved out of xceiv and then we can start using the generic PHY framework.

---
 drivers/usb/musb/musb_core.h |    2 ++
 drivers/usb/musb/omap2430.c  |   22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/usb/musb/musb_core.h +2 -0
index 7fb4819..78251fd 100644
@@ -46,6 +46,7 @@
 #include <linux/usb.h>
 #include <linux/usb/otg.h>
 #include <linux/usb/musb.h>
+#include <linux/phy/phy.h>
 
 struct musb;
 struct musb_hw_ep;
@@ -357,6 +358,7 @@ struct musb {
 	u16			int_tx;
 
 	struct usb_phy		*xceiv;
+	struct phy		*phy;
 
 	int nIrq;
 	unsigned		irq_wake:1;
drivers/usb/musb/omap2430.c +16 -6
index 1a42a45..55f071d 100644
@@ -349,14 +349,24 @@ static int omap2430_musb_init(struct musb *musb)
 	 * up through ULPI.  TWL4030-family PMICs include one,
 	 * which needs a driver, drivers aren't always needed.
 	 */
-	if (dev->parent->of_node)
+	if (dev->parent->of_node) {
+		musb->phy = devm_of_phy_get_byname(dev->parent, "usb2-phy");
+
+		/* We can't totally remove musb->xceiv as of now because
+		 * musb core uses xceiv.state and xceiv.otg. Once we have
+		 * a separate state machine to handle otg, these can be moved
+		 * out of xceiv and then we can start using the generic PHY
+		 * framework
+		 */
 		musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
 		    "usb-phy", 0);
-	else
+	} else {
 		musb->xceiv = devm_usb_get_phy_dev(dev, 0);
+		musb->phy = devm_phy_get(dev, 0);
+	}
 
-	if (IS_ERR_OR_NULL(musb->xceiv)) {
-		pr_err("HS USB OTG: no transceiver configured\n");
+	if (IS_ERR_OR_NULL(musb->xceiv) || IS_ERR(musb->phy)) {
+		dev_err(dev, "no transceiver configured\n");
 		return -EPROBE_DEFER;
 	}
 
@@ -612,7 +622,7 @@ static int omap2430_runtime_suspend(struct device *dev)
 				OTG_INTERFSEL);
 
 		omap2430_low_level_exit(musb);
-		usb_phy_set_suspend(musb->xceiv, 1);
+		phy_suspend(musb->phy);
 	}
 
 	return 0;
@@ -628,7 +638,7 @@ static int omap2430_runtime_resume(struct device *dev)
 		musb_writel(musb->mregs, OTG_INTERFSEL,
 				musb->context.otg_interfsel);
 
-		usb_phy_set_suspend(musb->xceiv, 0);
+		phy_resume(musb->phy);
 	}
 
 	return 0;
Signature · 1 line
1.7.10.4
#23 lore raw
In reply to Kishon Vijay Abraham I
Quoted 7 lines · Kishon Vijay Abraham I

Added a generic PHY framework that provides a set of APIs for the PHY drivers to create/destroy a PHY and APIs for the PHY users to obtain a reference to the PHY with or without using phandle. To obtain a reference to the PHY without using phandle, the platform specfic intialization code (say from board file) should have already called phy_bind with the binding information. The binding information consists of phy's device name, phy user device name and an index. The index is used when the same phy user binds to mulitple phys.

...

Quoted 6 lines

Changes from v4:
* removed of_phy_get_with_args/devm_of_phy_get_with_args. Now the *phy providers* should use their custom implementation of of_xlate or use of_phy_xlate to get *phy instance* from *phy providers*.
* Added of_phy_xlate to be used by *phy providers* if it provides only one PHY.
* changed phy_core from having subsys_initcall to module_init.

s/of_phy_xlate/of_phy_simple_xlate/ ? That would hightlight the fact it's just /an/ implementation for the simple case, not /the/ implementation for all cases. It'd be consistent with e.g. drivers/gpio, which has of_gpio_simple_xlate().