Skip to contentSkip to conversation

usb: usb251xb: Add upstream port lanes polarity inversion

PATCH In review Serge Semin 3 participants 8 messages Apr 24, 2019 – May 2, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover usb: usb251xb: Add upstream port lanes polarity inversion

A while ago I created an original patchset with functionality of any ports inversion setting implemented. But while I was busy with another work, Marco' version of the similar code has already been upstreamed. So I decided to at least refactor my patchset extracting alterations, which I think might be useful in the driver.

Even though the Marco' implementation was correct in general, it lacked a small specific detail. The polarity inversion property might be setup for downstream and upstream ports. Both of these cases are covered by current 'swap-dx-lanes' property implementation, but there is no any mentioning about number 0 being considered as Upstream port of the hub. This might be confusing seeing the rest of the port-related properties accept only the ports starting from 1 (which is starting number of downstream ports) and don't support number 0. So in order to unify the 'swap-dx-lanes' property with the rest port-related ones, I'd suggest to make it being responsible for downstream ports only, while upstream port lanes inversion would be enabled by setting a new 'swap-us-lanes' boolean property. Current property names mnemonic also fits quiet well for this update.

In addition the usb251xb_get_ofdata() method is getting to be too complicated with obvious identical peaces of code. In particular it concerns the ports-related properties like 'non-removable-ports', 'sp-disabled-ports' and 'bp-disabled-ports'. We can simplify the code responsible for the properties handling by moving a common part into a dedicated method. Needless to say that in case if the suggestion made above regarding the 'swap-dx-lanes' property alteration is accepted, the same could be done for parsing it' code as well.

So all of these updates are implemented in the patches of this small series.

Serge Semin (3):
usb: usb251xb: Add US lanes inversion dts-bindings
usb: usb251xb: Create a ports field collector method
usb: usb251xb: Add US port lanes inversion property

 .../devicetree/bindings/usb/usb251xb.txt      |  6 +-
 drivers/usb/misc/usb251xb.c                   | 73 +++++++------------
 2 files changed, 32 insertions(+), 47 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/3 usb: usb251xb: Add US lanes inversion dts-bindings

Since a separate US ports lanes polarity inversion property is going to be available the bindings doc-file should be updated with information about swap-us-lanes bool property, which will be responsible for it.

---
 Documentation/devicetree/bindings/usb/usb251xb.txt | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/usb/usb251xb.txt +4 -2
index 17915f64b8ee..bc7945e9dbfe 100644
@@ -64,8 +64,10 @@ Optional properties :
  - power-on-time-ms : Specifies the time it takes from the time the host
 	initiates the power-on sequence to a port until the port has adequate
 	power. The value is given in ms in a 0 - 510 range (default is 100ms).
- - swap-dx-lanes : Specifies the ports which will swap the differential-pair
-	(D+/D-), default is not-swapped.
+ - swap-dx-lanes : Specifies the downstream ports which will swap the
+	differential-pair (D+/D-), default is not-swapped.
+ - swap-us-lanes : Selects the upstream port differential-pair (D+/D-)
+	swapping (boolean, default is not-swapped)
 
 Examples:
 	usb2512b@2c {
Signature · 1 line
2.21.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] usb: usb251xb: Add US lanes inversion dts-bindings

Quoted 9 lines · Serge Semin

Since a separate US ports lanes polarity inversion property is going to be available the bindings doc-file should be updated with information about swap-us-lanes bool property, which will be responsible for it.

---
 Documentation/devicetree/bindings/usb/usb251xb.txt | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
maintainer Acked-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] usb: usb251xb: Add US lanes inversion dts-bindings

In reply to Serge Semin
Quoted 9 lines · Serge Semin

Since a separate US ports lanes polarity inversion property is going to be available the bindings doc-file should be updated with information about swap-us-lanes bool property, which will be responsible for it.

---
  Documentation/devicetree/bindings/usb/usb251xb.txt | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
#4 lore raw

PATCH 2/3 usb: usb251xb: Create a ports field collector method

In reply to Serge Semin

Seeing the ports field collection functionality is used four times per just one function, it's better to have a dedicated method performing the task. Note that this fix filters the port 0 out from the lanes swapping property the same way as it has been programmed for the rest multi-ports properties. But unlike the rest of ports config registers the BIT(0) of the Port Lanes Swap register refers to the Upstream Port lanes inversion. This fact hasn't been documented in the driver bindings nor there were any mentioning about port 0 being treated as upstream port. Lets then leave this fix as is for the properties unification and create an additional "swap-us-lanes" in the next patch.

---
 drivers/usb/misc/usb251xb.c | 71 ++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 45 deletions(-)
drivers/usb/misc/usb251xb.c +26 -45
index 04684849d683..4ef34df948ad 100644
@@ -331,18 +331,31 @@ static int usb251xb_connect(struct usb251xb *hub)
 }
 
 #ifdef CONFIG_OF
+static void usb251xb_get_ports_field(struct usb251xb *hub,
+				    const char *prop_name, u8 port_cnt, u8 *fld)
+{
+	struct device *dev = hub->dev;
+	struct property *prop;
+	const __be32 *p;
+	u32 port;
+
+	of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) {
+		if ((port >= 1) && (port <= port_cnt))
+			*fld |= BIT(port);
+		else
+			dev_warn(dev, "port %u doesn't exist\n", port);
+	}
+}
+
 static int usb251xb_get_ofdata(struct usb251xb *hub,
 			       struct usb251xb_data *data)
 {
 	struct device *dev = hub->dev;
 	struct device_node *np = dev->of_node;
-	int len, err, i;
-	u32 port, property_u32 = 0;
-	const u32 *cproperty_u32;
+	int len, err;
+	u32 property_u32 = 0;
 	const char *cproperty_char;
 	char str[USB251XB_STRING_BUFSIZE / 2];
-	struct property *prop;
-	const __be32 *p;
 
 	if (!np) {
 		dev_err(dev, "failed to get ofdata\n");
@@ -444,46 +457,16 @@ static int usb251xb_get_ofdata(struct usb251xb *hub,
 		hub->conf_data3 |= BIT(0);
 
 	hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
-	cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
-	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-		for (i = 0; i < len / sizeof(u32); i++) {
-			u32 port = be32_to_cpu(cproperty_u32[i]);
-
-			if ((port >= 1) && (port <= data->port_cnt))
-				hub->non_rem_dev |= BIT(port);
-			else
-				dev_warn(dev, "NRD port %u doesn't exist\n",
-					port);
-		}
-	}
+	usb251xb_get_ports_field(hub, "non-removable-ports", data->port_cnt,
+				 &hub->non_rem_dev);
 
 	hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
-	cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
-	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-		for (i = 0; i < len / sizeof(u32); i++) {
-			u32 port = be32_to_cpu(cproperty_u32[i]);
-
-			if ((port >= 1) && (port <= data->port_cnt))
-				hub->port_disable_sp |= BIT(port);
-			else
-				dev_warn(dev, "PDS port %u doesn't exist\n",
-					port);
-		}
-	}
+	usb251xb_get_ports_field(hub, "sp-disabled-ports", data->port_cnt,
+				 &hub->port_disable_sp);
 
 	hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
-	cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
-	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
-		for (i = 0; i < len / sizeof(u32); i++) {
-			u32 port = be32_to_cpu(cproperty_u32[i]);
-
-			if ((port >= 1) && (port <= data->port_cnt))
-				hub->port_disable_bp |= BIT(port);
-			else
-				dev_warn(dev, "PDB port %u doesn't exist\n",
-					port);
-		}
-	}
+	usb251xb_get_ports_field(hub, "bp-disabled-ports", data->port_cnt,
+				 &hub->port_disable_bp);
 
 	hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
 	if (!of_property_read_u32(np, "sp-max-total-current-microamp",
@@ -546,10 +529,8 @@ static int usb251xb_get_ofdata(struct usb251xb *hub,
 	 * register controls the USB DP/DM signal swapping for each port.
 	 */
 	hub->port_swap = USB251XB_DEF_PORT_SWAP;
-	of_property_for_each_u32(np, "swap-dx-lanes", prop, p, port) {
-		if (port <= data->port_cnt)
-			hub->port_swap |= BIT(port);
-	}
+	usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt,
+				 &hub->port_swap);
 
 	/* The following parameters are currently not exposed to devicetree, but
 	 * may be as soon as needed.
maintainer Acked-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] usb: usb251xb: Create a ports field collector method

Quoted 11 lines · Serge Semin

Seeing the ports field collection functionality is used four times per just one function, it's better to have a dedicated method performing the task. Note that this fix filters the port 0 out from the lanes swapping property the same way as it has been programmed for the rest multi-ports properties. But unlike the rest of ports config registers the BIT(0) of the Port Lanes Swap register refers to the Upstream Port lanes inversion. This fact hasn't been documented in the driver bindings nor there were any mentioning about port 0 being treated as upstream port. Lets then leave this fix as is for the properties unification and create an additional "swap-us-lanes" in the next patch.

Quoted 5 lines
---
  drivers/usb/misc/usb251xb.c | 71 ++++++++++++++-----------------------
  1 file changed, 26 insertions(+), 45 deletions(-)
#6 lore raw

PATCH 3/3 usb: usb251xb: Add US port lanes inversion property

In reply to Serge Semin

The driver bindings already declare the "swap-dx-lanes" property to invert the downstream ports lanes polarity. The similar config can be defined for a single upstream port - "swap-us-lanes". It's going to be boolean since there is only one upstream port on the hub.

---
 drivers/usb/misc/usb251xb.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/misc/usb251xb.c +2 -0
index 4ef34df948ad..56f0a10633fc 100644
@@ -531,6 +531,8 @@ static int usb251xb_get_ofdata(struct usb251xb *hub,
 	hub->port_swap = USB251XB_DEF_PORT_SWAP;
 	usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt,
 				 &hub->port_swap);
+	if (of_get_property(np, "swap-us-lanes", NULL))
+		hub->port_swap |= BIT(0);
 
 	/* The following parameters are currently not exposed to devicetree, but
 	 * may be as soon as needed.
maintainer Acked-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] usb: usb251xb: Add US port lanes inversion property

Quoted 10 lines · Serge Semin

The driver bindings already declare the "swap-dx-lanes" property to invert the downstream ports lanes polarity. The similar config can be defined for a single upstream port - "swap-us-lanes". It's going to be boolean since there is only one upstream port on the hub.

---
  drivers/usb/misc/usb251xb.c | 2 ++
  1 file changed, 2 insertions(+)