[PATCH net-next 2/8] net: pse-pd: Add helpers to convert between mW and uA

From: Kory Maincent (AddSecure)

Date: Wed Dec 02 2026 - 09:08:52 EST


The conversion between power in mW and current in uA for a given
voltage in uV is done several times in the PSE core and drivers.

Add pse_to_mW() and pse_to_uA() helpers to avoid duplicating the
conversion code.

Signed-off-by: Kory Maincent (AddSecure) <kory.maincent@xxxxxxxxxxx>
---
drivers/net/pse-pd/pse_core.c | 28 ++++++----------------------
include/linux/pse-pd/pse.h | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 0477cf6787bd3..8643ee53ef344 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -763,8 +763,7 @@ static int pse_pi_get_current_limit(struct regulator_dev *rdev)
{
struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
const struct pse_controller_ops *ops;
- int id, uV, mW, ret;
- s64 tmp_64;
+ int id, mW, ret;

ops = pcdev->ops;
id = rdev_get_id(rdev);
@@ -785,12 +784,8 @@ static int pse_pi_get_current_limit(struct regulator_dev *rdev)
}
if (ret < 0)
goto out;
- uV = ret;

- tmp_64 = mW;
- tmp_64 *= 1000000000ull;
- /* uA = mW * 1000000000 / uV */
- ret = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
+ ret = pse_to_uA(mW, ret);

out:
mutex_unlock(&pcdev->lock);
@@ -802,8 +797,7 @@ static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA,
{
struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
const struct pse_controller_ops *ops;
- int id, mW, ret;
- s64 tmp_64;
+ int id, ret;

ops = pcdev->ops;
if (!ops->pi_set_pw_limit || !ops->pi_get_voltage)
@@ -823,11 +817,7 @@ static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA,
if (ret < 0)
goto out;

- tmp_64 = ret;
- tmp_64 *= max_uA;
- /* mW = uA * uV / 1000000000 */
- mW = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
- ret = ops->pi_set_pw_limit(pcdev, id, mW);
+ ret = ops->pi_set_pw_limit(pcdev, id, pse_to_mW(max_uA, ret));
out:
mutex_unlock(&pcdev->lock);

@@ -1961,9 +1951,8 @@ int pse_ethtool_set_pw_limit(struct pse_control *psec,
struct netlink_ext_ack *extack,
const unsigned int pw_limit)
{
- int uV, uA, ret, previous_pw_allocated = 0;
+ int uA, ret, previous_pw_allocated = 0;
bool updated;
- s64 tmp_64;

if (pw_limit > MAX_PI_PW)
return -ERANGE;
@@ -1979,12 +1968,7 @@ int pse_ethtool_set_pw_limit(struct pse_control *psec,
"Error reading PSE voltage");
return ret;
}
- uV = ret;
-
- tmp_64 = pw_limit;
- tmp_64 *= 1000000000ull;
- /* uA = mW * 1000000000 / uV */
- uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
+ uA = pse_to_uA(pw_limit, ret);

/* Update power budget only in software power control case and
* if the PI is enabled and has a PD detected.
diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
index 17738ffcd224f..ea3743284f795 100644
--- a/include/linux/pse-pd/pse.h
+++ b/include/linux/pse-pd/pse.h
@@ -342,6 +342,40 @@ enum pse_budget_eval_strategies {
PSE_BUDGET_EVAL_STRAT_DYNAMIC = 1 << 2,
};

+/**
+ * pse_to_mW - Convert current and voltage to power in milliwatts
+ * @uA: current in microamps
+ * @uV: voltage in microvolts
+ *
+ * Return: the power in mW
+ */
+static inline int pse_to_mW(int uA, int uV)
+{
+ s64 tmp_64;
+
+ tmp_64 = uA;
+ tmp_64 *= uV;
+ /* mW = uA * uV / 1000000000 */
+ return DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
+}
+
+/**
+ * pse_to_uA - Convert power and voltage to current in microamps
+ * @mW: power in milliwatts
+ * @uV: voltage in microvolts
+ *
+ * Return: the current in uA
+ */
+static inline int pse_to_uA(int mW, int uV)
+{
+ s64 tmp_64;
+
+ tmp_64 = mW;
+ tmp_64 *= 1000000000ull;
+ /* uA = mW * 1000000000 / uV */
+ return DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
+}
+
#if IS_ENABLED(CONFIG_PSE_CONTROLLER)
int pse_controller_register(struct pse_controller_dev *pcdev);
void pse_controller_unregister(struct pse_controller_dev *pcdev);

--
2.43.0