hwmon: (nct7802) Use multi-byte regmap operations

Use multi-byte regmap operations where possible to reduce code size
and the need for mutex protection.

No functional changes.

Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Guenter Roeck 2024-07-09 14:03:06 -07:00
parent 717c04cf43
commit 7b4882d9b8

View File

@ -229,41 +229,34 @@ abort:
static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan) static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
{ {
unsigned int f1, f2; unsigned int regs[2] = {reg_fan, REG_FANCOUNT_LOW};
u8 f[2];
int ret; int ret;
mutex_lock(&data->access_lock); ret = regmap_multi_reg_read(data->regmap, regs, f, 2);
ret = regmap_read(data->regmap, reg_fan, &f1); if (ret)
if (ret < 0) return ret;
goto abort; ret = (f[0] << 5) | (f[1] >> 3);
ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
if (ret < 0)
goto abort;
ret = (f1 << 5) | (f2 >> 3);
/* convert fan count to rpm */ /* convert fan count to rpm */
if (ret == 0x1fff) /* maximum value, assume fan is stopped */ if (ret == 0x1fff) /* maximum value, assume fan is stopped */
ret = 0; ret = 0;
else if (ret) else if (ret)
ret = DIV_ROUND_CLOSEST(1350000U, ret); ret = DIV_ROUND_CLOSEST(1350000U, ret);
abort:
mutex_unlock(&data->access_lock);
return ret; return ret;
} }
static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low, static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
u8 reg_fan_high) u8 reg_fan_high)
{ {
unsigned int f1, f2; unsigned int regs[2] = {reg_fan_low, reg_fan_high};
u8 f[2];
int ret; int ret;
mutex_lock(&data->access_lock); ret = regmap_multi_reg_read(data->regmap, regs, f, 2);
ret = regmap_read(data->regmap, reg_fan_low, &f1);
if (ret < 0) if (ret < 0)
goto abort; return ret;
ret = regmap_read(data->regmap, reg_fan_high, &f2);
if (ret < 0) ret = f[0] | ((f[1] & 0xf8) << 5);
goto abort;
ret = f1 | ((f2 & 0xf8) << 5);
/* convert fan count to rpm */ /* convert fan count to rpm */
if (ret == 0x1fff) /* maximum value, assume no limit */ if (ret == 0x1fff) /* maximum value, assume no limit */
ret = 0; ret = 0;
@ -271,8 +264,6 @@ static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
ret = DIV_ROUND_CLOSEST(1350000U, ret); ret = DIV_ROUND_CLOSEST(1350000U, ret);
else else
ret = 1350000U; ret = 1350000U;
abort:
mutex_unlock(&data->access_lock);
return ret; return ret;
} }
@ -302,33 +293,26 @@ static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index) static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
{ {
unsigned int v1, v2; u8 v[2];
int ret; int ret;
mutex_lock(&data->access_lock);
if (index == 0) { /* voltage */ if (index == 0) { /* voltage */
ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1); unsigned int regs[2] = {REG_VOLTAGE[nr], REG_VOLTAGE_LOW};
ret = regmap_multi_reg_read(data->regmap, regs, v, 2);
if (ret < 0) if (ret < 0)
goto abort; return ret;
ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2); ret = ((v[0] << 2) | (v[1] >> 6)) * nct7802_vmul[nr];
if (ret < 0)
goto abort;
ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
} else { /* limit */ } else { /* limit */
int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr]; int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
unsigned int regs[2] = {REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
REG_VOLTAGE_LIMIT_MSB[nr]};
ret = regmap_read(data->regmap, ret = regmap_multi_reg_read(data->regmap, regs, v, 2);
REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
if (ret < 0) if (ret < 0)
goto abort; return ret;
ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr], ret = (v[0] | ((v[1] << shift) & 0x300)) * nct7802_vmul[nr];
&v2);
if (ret < 0)
goto abort;
ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
} }
abort:
mutex_unlock(&data->access_lock);
return ret; return ret;
} }