diff mbox series

[RFC,net-next,v1,2/2] net: dsa: microchip: ksz9477: Add drive strength configuration

Message ID 20230905124340.4116542-2-o.rempel@pengutronix.de (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [RFC,net-next,v1,1/2] dt-bindings: net: dsa: microchip: Update ksz device tree bindings for drive strength | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1330 this patch: 1330
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 1353 this patch: 1353
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1353 this patch: 1353
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 137 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Oleksij Rempel Sept. 5, 2023, 12:43 p.m. UTC
Add device tree based drive strength configuration support. It is needed to
pass EMI validation on our hardware.

Configuration values are based on the vendor's reference driver.

Tested on KSZ9563R.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/net/dsa/microchip/ksz9477.c | 120 ++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)

Comments

Andrew Lunn Sept. 5, 2023, 12:54 p.m. UTC | #1
> +static const struct ksz9477_drive_strength ksz9477_drive_strengths[] = {
> +	{ SW_DRIVE_STRENGTH_2MA,  2000 },
> +	{ SW_DRIVE_STRENGTH_4MA,  4000 },
> +	{ SW_DRIVE_STRENGTH_8MA,  8000 },
> +	{ SW_DRIVE_STRENGTH_12MA, 12000 },
> +	{ SW_DRIVE_STRENGTH_16MA, 16000 },
> +	{ SW_DRIVE_STRENGTH_20MA, 20000 },
> +	{ SW_DRIVE_STRENGTH_24MA, 24000 },
> +	{ SW_DRIVE_STRENGTH_28MA, 28000 },

These SW_DRIVE_STRENGTH_ values appear in both ksz8795_reg.h and
ksz9477_reg.h. Can the code be made common?

> +static u32 ksz9477_drive_strength_to_reg(u32 milliamp)
> +{
> +	size_t array_size = ARRAY_SIZE(ksz9477_drive_strengths);
> +	int i;
> +
> +	for (i = 0; i < array_size; i++) {
> +		if (ksz9477_drive_strengths[i].milliamp >= milliamp)
> +			return ksz9477_drive_strengths[i].reg_val;

Normally an exact match is used, not the nearest value.


    Andrew

---
pw-bot: cr
diff mbox series

Patch

diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c
index 83b7f2d5c1ea6..04582d3fcbe14 100644
--- a/drivers/net/dsa/microchip/ksz9477.c
+++ b/drivers/net/dsa/microchip/ksz9477.c
@@ -19,6 +19,30 @@ 
 #include "ksz_common.h"
 #include "ksz9477.h"
 
+/**
+ * struct ksz9477_drive_strength - drive strength mapping
+ * @reg_val:	register value
+ * @milliamp:	milliamp value
+ */
+struct ksz9477_drive_strength {
+	u32 reg_val;
+	u32 milliamp;
+};
+
+/* Drive strength mapping.
+ * This values are not documented and taken from vendor's reference driver.
+ */
+static const struct ksz9477_drive_strength ksz9477_drive_strengths[] = {
+	{ SW_DRIVE_STRENGTH_2MA,  2000 },
+	{ SW_DRIVE_STRENGTH_4MA,  4000 },
+	{ SW_DRIVE_STRENGTH_8MA,  8000 },
+	{ SW_DRIVE_STRENGTH_12MA, 12000 },
+	{ SW_DRIVE_STRENGTH_16MA, 16000 },
+	{ SW_DRIVE_STRENGTH_20MA, 20000 },
+	{ SW_DRIVE_STRENGTH_24MA, 24000 },
+	{ SW_DRIVE_STRENGTH_28MA, 28000 },
+};
+
 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
 {
 	regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
@@ -1097,11 +1121,107 @@  int ksz9477_enable_stp_addr(struct ksz_device *dev)
 	return 0;
 }
 
+/**
+ * ksz9477_drive_strength_to_reg() - convert milliamp value to register value
+ * @milliamp:	milliamp value
+ *
+ * Return: register value
+ */
+static u32 ksz9477_drive_strength_to_reg(u32 milliamp)
+{
+	size_t array_size = ARRAY_SIZE(ksz9477_drive_strengths);
+	int i;
+
+	for (i = 0; i < array_size; i++) {
+		if (ksz9477_drive_strengths[i].milliamp >= milliamp)
+			return ksz9477_drive_strengths[i].reg_val;
+	}
+
+	/* return the highest value if the requested value is too high */
+	return ksz9477_drive_strengths[array_size - 1].reg_val;
+}
+
+/**
+ * ksz9477_parse_drive_strength() - parse drive strength from device tree
+ * @dev:	ksz device
+ *
+ * Return: 0 on success, error code otherwise
+ */
+static int ksz9477_parse_drive_strength(struct ksz_device *dev)
+{
+	struct of_prop {
+		const char *name;
+		int offset;
+		int value;
+	} of_props[] = {
+		{
+			"microchip,hi-drive-strength-microamp",
+			SW_HI_SPEED_DRIVE_STRENGTH_S,
+			-1
+		},
+		{
+			"microchip,lo-drive-strength-microamp",
+			SW_LO_SPEED_DRIVE_STRENGTH_S,
+			-1
+		},
+	};
+	struct device_node *np = dev->dev->of_node;
+	bool found = false;
+	int ret;
+	u8 val;
+
+	if (!np)
+		return 0;
+
+	for (int i = 0; i < ARRAY_SIZE(of_props); i++) {
+		ret = of_property_read_u32(np, of_props[i].name,
+					   &of_props[i].value);
+		if (ret && ret != -EINVAL)
+			dev_warn(dev->dev, "Failed to read %s\n",
+				 of_props[i].name);
+		if (ret)
+			continue;
+
+		if (of_props[i].value > 28000 || of_props[i].value < 2000) {
+			dev_warn(dev->dev, "Drive strength value is out of range: %d. Supported values are (2000 - 28000)\n",
+				 of_props[i].value);
+			continue;
+		}
+
+		found = true;
+	}
+
+	if (!found)
+		return 0;
+
+	ret = ksz_read8(dev, REG_SW_IO_STRENGTH__1, &val);
+	if (ret)
+		return ret;
+
+	for (int i = 0; i < ARRAY_SIZE(of_props); i++) {
+		int strength_val;
+
+		if (of_props[i].value == -1)
+			continue;
+
+		strength_val = ksz9477_drive_strength_to_reg(of_props[i].value);
+
+		val &= ~(SW_DRIVE_STRENGTH_M << of_props[i].offset);
+		val |= strength_val << of_props[i].offset;
+	}
+
+	return ksz_write8(dev, REG_SW_IO_STRENGTH__1, val);
+}
+
 int ksz9477_setup(struct dsa_switch *ds)
 {
 	struct ksz_device *dev = ds->priv;
 	int ret = 0;
 
+	ret = ksz9477_parse_drive_strength(dev);
+	if (ret)
+		return ret;
+
 	ds->mtu_enforcement_ingress = true;
 
 	/* Required for port partitioning. */