diff options
author | Guenter Roeck <linux@roeck-us.net> | 2024-07-23 13:25:31 -0700 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2024-08-30 08:34:23 -0700 |
commit | 232177a37b90d76d3616f28fb47636d81c02fe8b (patch) | |
tree | 0736f7134823e4410881029c617ae2fd79b0af86 /drivers/hwmon | |
parent | 61a4a8414e1c6331a5a2fcfd235e6252c985f9d7 (diff) |
hwmon: (ina2xx) Use bit operations
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.
Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r-- | drivers/hwmon/ina2xx.c | 84 |
1 files 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, ®val); 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); |