Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 03/11] hwmon: (ina2xx) Use bit operations
Date: Tue, 27 Aug 2024 08:34:47 -0700	[thread overview]
Message-ID: <20240827153455.1344529-4-linux@roeck-us.net> (raw)
In-Reply-To: <20240827153455.1344529-1-linux@roeck-us.net>

Use bit operations where possible to make the code more generic and to
align it with other drivers. Also use compile time conversion from bit
to mask to reduce runtime overhead.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/ina2xx.c | 84 +++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c
index 897657f8d685..1b4170d02c94 100644
--- a/drivers/hwmon/ina2xx.c
+++ b/drivers/hwmon/ina2xx.c
@@ -22,6 +22,8 @@
  * Thanks to Jan Volkering
  */
 
+#include <linux/bitfield.h>
+#include <linux/bits.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
@@ -65,25 +67,23 @@
 #define INA2XX_RSHUNT_DEFAULT		10000
 
 /* bit mask for reading the averaging setting in the configuration register */
-#define INA226_AVG_RD_MASK		0x0E00
+#define INA226_AVG_RD_MASK		GENMASK(11, 9)
 
-#define INA226_READ_AVG(reg)		(((reg) & INA226_AVG_RD_MASK) >> 9)
-#define INA226_SHIFT_AVG(val)		((val) << 9)
+#define INA226_READ_AVG(reg)		FIELD_GET(INA226_AVG_RD_MASK, reg)
 
-#define INA226_ALERT_POLARITY_MASK		0x0002
-#define INA226_SHIFT_ALERT_POLARITY(val)	((val) << 1)
-#define INA226_ALERT_POL_LOW			0
-#define INA226_ALERT_POL_HIGH			1
+#define INA226_ALERT_POLARITY_MASK	BIT(1)
+#define INA226_ALERT_POL_LOW		0
+#define INA226_ALERT_POL_HIGH		1
 
 /* bit number of alert functions in Mask/Enable Register */
-#define INA226_SHUNT_OVER_VOLTAGE_BIT	15
-#define INA226_SHUNT_UNDER_VOLTAGE_BIT	14
-#define INA226_BUS_OVER_VOLTAGE_BIT	13
-#define INA226_BUS_UNDER_VOLTAGE_BIT	12
-#define INA226_POWER_OVER_LIMIT_BIT	11
+#define INA226_SHUNT_OVER_VOLTAGE_MASK	BIT(15)
+#define INA226_SHUNT_UNDER_VOLTAGE_MASK	BIT(14)
+#define INA226_BUS_OVER_VOLTAGE_MASK	BIT(13)
+#define INA226_BUS_UNDER_VOLTAGE_MASK	BIT(12)
+#define INA226_POWER_OVER_LIMIT_MASK	BIT(11)
 
 /* bit mask for alert config bits of Mask/Enable Register */
-#define INA226_ALERT_CONFIG_MASK	0xFC00
+#define INA226_ALERT_CONFIG_MASK	GENMASK(15, 10)
 #define INA226_ALERT_FUNCTION_FLAG	BIT(4)
 
 /* common attrs, ina226 attrs and NULL */
@@ -177,7 +177,7 @@ static u16 ina226_interval_to_reg(int interval)
 	avg_bits = find_closest(avg, ina226_avg_tab,
 				ARRAY_SIZE(ina226_avg_tab));
 
-	return INA226_SHIFT_AVG(avg_bits);
+	return FIELD_PREP(INA226_AVG_RD_MASK, avg_bits);
 }
 
 static int ina2xx_set_alert_polarity(struct ina2xx_data *data,
@@ -185,7 +185,7 @@ static int ina2xx_set_alert_polarity(struct ina2xx_data *data,
 {
 	return regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
 				 INA226_ALERT_POLARITY_MASK,
-				 INA226_SHIFT_ALERT_POLARITY(val));
+				 FIELD_PREP(INA226_ALERT_POLARITY_MASK, val));
 }
 
 /*
@@ -322,20 +322,20 @@ static ssize_t ina2xx_value_show(struct device *dev,
 	return sysfs_emit(buf, "%d\n", ina2xx_get_value(data, attr->index, regval));
 }
 
-static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
+static int ina226_reg_to_alert(struct ina2xx_data *data, u32 mask, u16 regval)
 {
 	int reg;
 
-	switch (bit) {
-	case INA226_SHUNT_OVER_VOLTAGE_BIT:
-	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
+	switch (mask) {
+	case INA226_SHUNT_OVER_VOLTAGE_MASK:
+	case INA226_SHUNT_UNDER_VOLTAGE_MASK:
 		reg = INA2XX_SHUNT_VOLTAGE;
 		break;
-	case INA226_BUS_OVER_VOLTAGE_BIT:
-	case INA226_BUS_UNDER_VOLTAGE_BIT:
+	case INA226_BUS_OVER_VOLTAGE_MASK:
+	case INA226_BUS_UNDER_VOLTAGE_MASK:
 		reg = INA2XX_BUS_VOLTAGE;
 		break;
-	case INA226_POWER_OVER_LIMIT_BIT:
+	case INA226_POWER_OVER_LIMIT_MASK:
 		reg = INA2XX_POWER;
 		break;
 	default:
@@ -351,19 +351,19 @@ static int ina226_reg_to_alert(struct ina2xx_data *data, u8 bit, u16 regval)
  * Turns alert limit values into register values.
  * Opposite of the formula in ina2xx_get_value().
  */
-static s16 ina226_alert_to_reg(struct ina2xx_data *data, u8 bit, int val)
+static s16 ina226_alert_to_reg(struct ina2xx_data *data, u32 mask, int val)
 {
-	switch (bit) {
-	case INA226_SHUNT_OVER_VOLTAGE_BIT:
-	case INA226_SHUNT_UNDER_VOLTAGE_BIT:
+	switch (mask) {
+	case INA226_SHUNT_OVER_VOLTAGE_MASK:
+	case INA226_SHUNT_UNDER_VOLTAGE_MASK:
 		val *= data->config->shunt_div;
 		return clamp_val(val, SHRT_MIN, SHRT_MAX);
-	case INA226_BUS_OVER_VOLTAGE_BIT:
-	case INA226_BUS_UNDER_VOLTAGE_BIT:
+	case INA226_BUS_OVER_VOLTAGE_MASK:
+	case INA226_BUS_UNDER_VOLTAGE_MASK:
 		val = (val * 1000) << data->config->bus_voltage_shift;
 		val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
 		return clamp_val(val, 0, SHRT_MAX);
-	case INA226_POWER_OVER_LIMIT_BIT:
+	case INA226_POWER_OVER_LIMIT_MASK:
 		val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
 		return clamp_val(val, 0, USHRT_MAX);
 	default:
@@ -387,7 +387,7 @@ static ssize_t ina226_alert_show(struct device *dev,
 	if (ret)
 		goto abort;
 
-	if (regval & BIT(attr->index)) {
+	if (regval & attr->index) {
 		ret = regmap_read(data->regmap, INA226_ALERT_LIMIT, &regval);
 		if (ret)
 			goto abort;
@@ -432,7 +432,7 @@ static ssize_t ina226_alert_store(struct device *dev,
 	if (val != 0) {
 		ret = regmap_update_bits(data->regmap, INA226_MASK_ENABLE,
 					 INA226_ALERT_CONFIG_MASK,
-					 BIT(attr->index));
+					 attr->index);
 		if (ret < 0)
 			goto abort;
 	}
@@ -456,7 +456,7 @@ static ssize_t ina226_alarm_show(struct device *dev,
 	if (ret)
 		return ret;
 
-	alarm = (regval & BIT(attr->index)) &&
+	alarm = (regval & attr->index) &&
 		(regval & INA226_ALERT_FUNCTION_FLAG);
 	return sysfs_emit(buf, "%d\n", alarm);
 }
@@ -552,25 +552,25 @@ static ssize_t ina226_interval_show(struct device *dev,
 static SENSOR_DEVICE_ATTR_RO(in0_input, ina2xx_value, INA2XX_SHUNT_VOLTAGE);
 /* shunt voltage over/under voltage alert setting and alarm */
 static SENSOR_DEVICE_ATTR_RW(in0_crit, ina226_alert,
-			     INA226_SHUNT_OVER_VOLTAGE_BIT);
+			     INA226_SHUNT_OVER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RW(in0_lcrit, ina226_alert,
-			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
+			     INA226_SHUNT_UNDER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RO(in0_crit_alarm, ina226_alarm,
-			     INA226_SHUNT_OVER_VOLTAGE_BIT);
+			     INA226_SHUNT_OVER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RO(in0_lcrit_alarm, ina226_alarm,
-			     INA226_SHUNT_UNDER_VOLTAGE_BIT);
+			     INA226_SHUNT_UNDER_VOLTAGE_MASK);
 
 /* bus voltage */
 static SENSOR_DEVICE_ATTR_RO(in1_input, ina2xx_value, INA2XX_BUS_VOLTAGE);
 /* bus voltage over/under voltage alert setting and alarm */
 static SENSOR_DEVICE_ATTR_RW(in1_crit, ina226_alert,
-			     INA226_BUS_OVER_VOLTAGE_BIT);
+			     INA226_BUS_OVER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RW(in1_lcrit, ina226_alert,
-			     INA226_BUS_UNDER_VOLTAGE_BIT);
+			     INA226_BUS_UNDER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RO(in1_crit_alarm, ina226_alarm,
-			     INA226_BUS_OVER_VOLTAGE_BIT);
+			     INA226_BUS_OVER_VOLTAGE_MASK);
 static SENSOR_DEVICE_ATTR_RO(in1_lcrit_alarm, ina226_alarm,
-			     INA226_BUS_UNDER_VOLTAGE_BIT);
+			     INA226_BUS_UNDER_VOLTAGE_MASK);
 
 /* calculated current */
 static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
@@ -579,9 +579,9 @@ static SENSOR_DEVICE_ATTR_RO(curr1_input, ina2xx_value, INA2XX_CURRENT);
 static SENSOR_DEVICE_ATTR_RO(power1_input, ina2xx_value, INA2XX_POWER);
 /* over-limit power alert setting and alarm */
 static SENSOR_DEVICE_ATTR_RW(power1_crit, ina226_alert,
-			     INA226_POWER_OVER_LIMIT_BIT);
+			     INA226_POWER_OVER_LIMIT_MASK);
 static SENSOR_DEVICE_ATTR_RO(power1_crit_alarm, ina226_alarm,
-			     INA226_POWER_OVER_LIMIT_BIT);
+			     INA226_POWER_OVER_LIMIT_MASK);
 
 /* shunt resistance */
 static SENSOR_DEVICE_ATTR_RW(shunt_resistor, ina2xx_shunt, INA2XX_CALIBRATION);
-- 
2.45.2


  parent reply	other threads:[~2024-08-27 15:35 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-27 15:34 [PATCH 00/11] hwmon: (ina2xx) Cleanup and convert to use with_info API Guenter Roeck
2024-08-27 15:34 ` [PATCH 01/11] hwmon: (ina2xx) Reorder include files to alphabetic order Guenter Roeck
2024-08-29 14:53   ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 02/11] hwmon: (ina2xx) Replace platform data with device properties Guenter Roeck
2024-08-29 14:53   ` Tzung-Bi Shih
2024-08-27 15:34 ` Guenter Roeck [this message]
2024-08-29 14:53   ` [PATCH 03/11] hwmon: (ina2xx) Use bit operations Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 04/11] hwmon: (ina2xx) Mark regmap_config as const Guenter Roeck
2024-08-29 14:54   ` Tzung-Bi Shih
2024-08-29 16:44     ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 05/11] hwmon: (ina2xx) Use local regmap pointer if used more than once Guenter Roeck
2024-08-29 14:54   ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 06/11] hwmon: (ina2xx) Re-initialize chip using regmap functions Guenter Roeck
2024-08-29 14:54   ` Tzung-Bi Shih
2024-08-29 16:45     ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 07/11] hwmon: (ina2xx) Set alert latch when enabling alerts Guenter Roeck
2024-08-29 14:54   ` Tzung-Bi Shih
2024-08-29 17:28     ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 08/11] hwmon: (ina2xx) Fix various overflow issues Guenter Roeck
2024-08-29 14:55   ` Tzung-Bi Shih
2024-08-29 16:56     ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 09/11] hwmon: (ina2xx) Consolidate chip initialization code Guenter Roeck
2024-08-29 14:55   ` Tzung-Bi Shih
2024-08-29 16:57     ` Guenter Roeck
2024-08-27 15:34 ` [PATCH 10/11] hwmon: (ina2xx) Move ina2xx_get_value() Guenter Roeck
2024-08-29 14:55   ` Tzung-Bi Shih
2024-08-27 15:34 ` [PATCH 11/11] hwmon: (ina2xx) Convert to use with_info hwmon API Guenter Roeck
2024-08-29 14:55   ` Tzung-Bi Shih
2024-08-29 16:53     ` Guenter Roeck

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=20240827153455.1344529-4-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=linux-hwmon@vger.kernel.org \
    /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