Linux Documentation
 help / color / mirror / Atom feed
From: Kory Maincent <kory.maincent@bootlin.com>
To: Andrew Lunn <andrew@lunn.ch>,
	Oleksij Rempel <o.rempel@pengutronix.de>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	 Jonathan Corbet <corbet@lwn.net>,
	Donald Hunter <donald.hunter@gmail.com>,
	 Rob Herring <robh@kernel.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	 Simon Horman <horms@kernel.org>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	 Russell King <linux@armlinux.org.uk>,
	Liam Girdwood <lgirdwood@gmail.com>,
	 Mark Brown <broonie@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	 linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	 linux-doc@vger.kernel.org, Kyle Swenson <kyle.swenson@est.tech>,
	 Dent Project <dentproject@linuxfoundation.org>,
	kernel@pengutronix.de,
	 Maxime Chevallier <maxime.chevallier@bootlin.com>,
	 devicetree@vger.kernel.org,
	Kory Maincent <kory.maincent@bootlin.com>
Subject: [PATCH net-next v4 05/27] net: pse-pd: tps23881: Use helpers to calculate bit offset for a channel
Date: Fri, 03 Jan 2025 22:12:54 +0100	[thread overview]
Message-ID: <20250103-feature_poe_port_prio-v4-5-dc91a3c0c187@bootlin.com> (raw)
In-Reply-To: <20250103-feature_poe_port_prio-v4-0-dc91a3c0c187@bootlin.com>

From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>

This driver frequently follows a pattern where two registers are read or
written in a single operation, followed by calculating the bit offset for
a specific channel.

Introduce helpers to streamline this process and reduce code redundancy,
making the codebase cleaner and more maintainable.

Acked-by: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
---

Thanks to Oleksij for the design of the helpers functions.

Change in v3:
- Small fix: use chan >= 4 instead of chan > 4;

Change in v2:
- New patch
---
 drivers/net/pse-pd/tps23881.c | 107 +++++++++++++++++++++++++++---------------
 1 file changed, 69 insertions(+), 38 deletions(-)

diff --git a/drivers/net/pse-pd/tps23881.c b/drivers/net/pse-pd/tps23881.c
index a3507520ff87..4a75206b2de6 100644
--- a/drivers/net/pse-pd/tps23881.c
+++ b/drivers/net/pse-pd/tps23881.c
@@ -53,6 +53,55 @@ static struct tps23881_priv *to_tps23881_priv(struct pse_controller_dev *pcdev)
 	return container_of(pcdev, struct tps23881_priv, pcdev);
 }
 
+/*
+ * Helper to extract a value from a u16 register value, which is made of two
+ * u8 registers. The function calculates the bit offset based on the channel
+ * and extracts the relevant bits using a provided field mask.
+ *
+ * @param reg_val: The u16 register value (composed of two u8 registers).
+ * @param chan: The channel number (0-7).
+ * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
+ * @param field_mask: The mask to apply to extract the required bits.
+ * @return: The extracted value for the specific channel.
+ */
+static u16 tps23881_calc_val(u16 reg_val, u8 chan, u8 field_offset,
+			     u16 field_mask)
+{
+	if (chan >= 4)
+		reg_val >>= 8;
+
+	return (reg_val >> field_offset) & field_mask;
+}
+
+/*
+ * Helper to combine individual channel values into a u16 register value.
+ * The function sets the value for a specific channel in the appropriate
+ * position.
+ *
+ * @param reg_val: The current u16 register value.
+ * @param chan: The channel number (0-7).
+ * @param field_offset: The base bit offset to apply (e.g., 0 or 4).
+ * @param field_mask: The mask to apply for the field (e.g., 0x0F).
+ * @param field_val: The value to set for the specific channel (masked by
+ *                   field_mask).
+ * @return: The updated u16 register value with the channel value set.
+ */
+static u16 tps23881_set_val(u16 reg_val, u8 chan, u8 field_offset,
+			    u16 field_mask, u16 field_val)
+{
+	field_val &= field_mask;
+
+	if (chan < 4) {
+		reg_val &= ~(field_mask << field_offset);
+		reg_val |= (field_val << field_offset);
+	} else {
+		reg_val &= ~(field_mask << (field_offset + 8));
+		reg_val |= (field_val << (field_offset + 8));
+	}
+
+	return reg_val;
+}
+
 static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
 {
 	struct tps23881_priv *priv = to_tps23881_priv(pcdev);
@@ -64,17 +113,12 @@ static int tps23881_pi_enable(struct pse_controller_dev *pcdev, int id)
 		return -ERANGE;
 
 	chan = priv->port[id].chan[0];
-	if (chan < 4)
-		val = BIT(chan);
-	else
-		val = BIT(chan + 4);
+	val = tps23881_set_val(0, chan, 0, BIT(chan % 4), BIT(chan % 4));
 
 	if (priv->port[id].is_4p) {
 		chan = priv->port[id].chan[1];
-		if (chan < 4)
-			val |= BIT(chan);
-		else
-			val |= BIT(chan + 4);
+		val = tps23881_set_val(val, chan, 0, BIT(chan % 4),
+				       BIT(chan % 4));
 	}
 
 	return i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
@@ -91,17 +135,12 @@ static int tps23881_pi_disable(struct pse_controller_dev *pcdev, int id)
 		return -ERANGE;
 
 	chan = priv->port[id].chan[0];
-	if (chan < 4)
-		val = BIT(chan + 4);
-	else
-		val = BIT(chan + 8);
+	val = tps23881_set_val(0, chan, 4, BIT(chan % 4), BIT(chan % 4));
 
 	if (priv->port[id].is_4p) {
 		chan = priv->port[id].chan[1];
-		if (chan < 4)
-			val |= BIT(chan + 4);
-		else
-			val |= BIT(chan + 8);
+		val = tps23881_set_val(val, chan, 4, BIT(chan % 4),
+				       BIT(chan % 4));
 	}
 
 	return i2c_smbus_write_word_data(client, TPS23881_REG_PW_EN, val);
@@ -113,6 +152,7 @@ static int tps23881_pi_is_enabled(struct pse_controller_dev *pcdev, int id)
 	struct i2c_client *client = priv->client;
 	bool enabled;
 	u8 chan;
+	u16 val;
 	int ret;
 
 	ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
@@ -120,17 +160,13 @@ static int tps23881_pi_is_enabled(struct pse_controller_dev *pcdev, int id)
 		return ret;
 
 	chan = priv->port[id].chan[0];
-	if (chan < 4)
-		enabled = ret & BIT(chan);
-	else
-		enabled = ret & BIT(chan + 4);
+	val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
+	enabled = !!(val);
 
 	if (priv->port[id].is_4p) {
 		chan = priv->port[id].chan[1];
-		if (chan < 4)
-			enabled &= !!(ret & BIT(chan));
-		else
-			enabled &= !!(ret & BIT(chan + 4));
+		val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
+		enabled &= !!(val);
 	}
 
 	/* Return enabled status only if both channel are on this state */
@@ -146,6 +182,7 @@ static int tps23881_ethtool_get_status(struct pse_controller_dev *pcdev,
 	struct i2c_client *client = priv->client;
 	bool enabled, delivering;
 	u8 chan;
+	u16 val;
 	int ret;
 
 	ret = i2c_smbus_read_word_data(client, TPS23881_REG_PW_STATUS);
@@ -153,23 +190,17 @@ static int tps23881_ethtool_get_status(struct pse_controller_dev *pcdev,
 		return ret;
 
 	chan = priv->port[id].chan[0];
-	if (chan < 4) {
-		enabled = ret & BIT(chan);
-		delivering = ret & BIT(chan + 4);
-	} else {
-		enabled = ret & BIT(chan + 4);
-		delivering = ret & BIT(chan + 8);
-	}
+	val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
+	enabled = !!(val);
+	val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
+	delivering = !!(val);
 
 	if (priv->port[id].is_4p) {
 		chan = priv->port[id].chan[1];
-		if (chan < 4) {
-			enabled &= !!(ret & BIT(chan));
-			delivering &= !!(ret & BIT(chan + 4));
-		} else {
-			enabled &= !!(ret & BIT(chan + 4));
-			delivering &= !!(ret & BIT(chan + 8));
-		}
+		val = tps23881_calc_val(ret, chan, 0, BIT(chan % 4));
+		enabled &= !!(val);
+		val = tps23881_calc_val(ret, chan, 4, BIT(chan % 4));
+		delivering &= !!(val);
 	}
 
 	/* Return delivering status only if both channel are on this state */

-- 
2.34.1


  parent reply	other threads:[~2025-01-03 21:13 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-03 21:12 [PATCH net-next v4 00/27] Add support for PSE budget evaluation strategy Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 01/27] net: pse-pd: Remove unused pse_ethtool_get_pw_limit function declaration Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 02/27] net: pse-pd: Avoid setting max_uA in regulator constraints Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 03/27] net: pse-pd: Add power limit check Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 04/27] net: pse-pd: tps23881: Simplify function returns by removing redundant checks Kory Maincent
2025-01-03 21:12 ` Kory Maincent [this message]
2025-01-03 21:12 ` [PATCH net-next v4 06/27] net: pse-pd: tps23881: Add missing configuration register after disable Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 07/27] net: pse-pd: Use power limit at driver side instead of current limit Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 08/27] net: pse-pd: Split ethtool_get_status into multiple callbacks Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 09/27] net: pse-pd: Remove is_enabled callback from drivers Kory Maincent
2025-01-03 21:12 ` [PATCH net-next v4 10/27] net: pse-pd: tps23881: Add support for power limit and measurement features Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 11/27] net: pse-pd: Add support for PSE device index Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 12/27] net: ethtool: Add support for new PSE device index description Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 13/27] net: ethtool: Add support for ethnl_info_init_ntf helper function Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 14/27] net: pse-pd: Add support for reporting events Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 15/27] net: pse-pd: tps23881: Add support for PSE events and interrupts Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 16/27] regulator: core: Resolve supply using of_node from regulator_config Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 17/27] regulator: Add support for power budget description Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 18/27] regulator: dt-bindings: Add regulator-power-budget property Kory Maincent
2025-01-04  9:42   ` Krzysztof Kozlowski
2025-01-04 15:50     ` Kory Maincent
2025-01-05  9:06       ` Krzysztof Kozlowski
2025-01-04  9:43   ` Krzysztof Kozlowski
2025-01-04 15:37     ` Kory Maincent
2025-01-05  9:04       ` Krzysztof Kozlowski
2025-01-03 21:13 ` [PATCH net-next v4 19/27] net: pse-pd: Fix missing PI of_node description Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 20/27] net: pse-pd: Add support for PSE power domains Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 21/27] net: ethtool: Add support for new power domains index description Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 22/27] net: pse-pd: Add support for getting budget evaluation strategies Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 23/27] net: ethtool: Add PSE new budget evaluation strategy support feature Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 24/27] net: pse-pd: pd692x0: Add support for PSE PI priority feature Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 25/27] dt-bindings: net: pse-pd: microchip,pd692x0: Add manager regulator supply Kory Maincent
2025-01-04  9:52   ` Krzysztof Kozlowski
2025-02-05 14:05     ` Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 26/27] net: pse-pd: tps23881: Add support for static port priority feature Kory Maincent
2025-01-03 21:13 ` [PATCH net-next v4 27/27] dt-bindings: net: pse-pd: ti,tps23881: Add interrupt description Kory Maincent
2025-01-04  9:44   ` Krzysztof Kozlowski
2025-02-05 14:18     ` Kory Maincent
2025-02-05 16:35       ` Krzysztof Kozlowski
2025-01-04 11:37 ` [PATCH net-next v4 00/27] Add support for PSE budget evaluation strategy Oleksij Rempel
2025-01-04 15:16   ` Kory Maincent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250103-feature_poe_port_prio-v4-5-dc91a3c0c187@bootlin.com \
    --to=kory.maincent@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrew@lunn.ch \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dentproject@linuxfoundation.org \
    --cc=devicetree@vger.kernel.org \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@google.com \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=kernel@pengutronix.de \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kyle.swenson@est.tech \
    --cc=lgirdwood@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox