Skip to content

Commit

Permalink
hwmon: Replace SENSORS_LIMIT with clamp_val
Browse files Browse the repository at this point in the history
SENSORS_LIMIT and the generic clamp_val have the same functionality,
and clamp_val is more efficient.

This patch reduces text size by 9052 bytes and bss size by 11624 bytes
for x86_64 builds.

Signed-off-by: Guenter Roeck <[email protected]>
Acked-by: George Joseph <[email protected]>
Acked-by: Jean Delvare <[email protected]>
  • Loading branch information
groeck committed Jan 26, 2013
1 parent 142c090 commit 2a844c1
Show file tree
Hide file tree
Showing 57 changed files with 297 additions and 319 deletions.
2 changes: 1 addition & 1 deletion drivers/hwmon/ad7414.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static ssize_t set_max_min(struct device *dev,
if (ret < 0)
return ret;

temp = SENSORS_LIMIT(temp, -40000, 85000);
temp = clamp_val(temp, -40000, 85000);
temp = (temp + (temp < 0 ? -500 : 500)) / 1000;

mutex_lock(&data->lock);
Expand Down
4 changes: 2 additions & 2 deletions drivers/hwmon/adm1021.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static ssize_t set_temp_max(struct device *dev,
temp /= 1000;

mutex_lock(&data->update_lock);
data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
data->temp_max[index] = clamp_val(temp, -128, 127);
if (!read_only)
i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
data->temp_max[index]);
Expand All @@ -218,7 +218,7 @@ static ssize_t set_temp_min(struct device *dev,
temp /= 1000;

mutex_lock(&data->update_lock);
data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
data->temp_min[index] = clamp_val(temp, -128, 127);
if (!read_only)
i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
data->temp_min[index]);
Expand Down
16 changes: 8 additions & 8 deletions drivers/hwmon/adm1026.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static int adm1026_scaling[] = { /* .001 Volts */
};
#define NEG12_OFFSET 16000
#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
#define INS_TO_REG(n, val) (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
#define INS_TO_REG(n, val) (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
0, 255))
#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))

Expand All @@ -207,22 +207,22 @@ static int adm1026_scaling[] = { /* .001 Volts */
* 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
*/
#define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
SENSORS_LIMIT(1350000 / ((val) * (div)), \
clamp_val(1350000 / ((val) * (div)), \
1, 254))
#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 0xff ? 0 : \
1350000 / ((val) * (div)))
#define DIV_FROM_REG(val) (1 << (val))
#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)

/* Temperature is reported in 1 degC increments */
#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) + ((val) < 0 ? -500 : 500)) \
#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
/ 1000, -127, 127))
#define TEMP_FROM_REG(val) ((val) * 1000)
#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val) + ((val) < 0 ? -500 : 500)) \
#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
/ 1000, -127, 127))
#define OFFSET_FROM_REG(val) ((val) * 1000)

#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
#define PWM_TO_REG(val) (clamp_val(val, 0, 255))
#define PWM_FROM_REG(val) (val)

#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
Expand All @@ -233,7 +233,7 @@ static int adm1026_scaling[] = { /* .001 Volts */
* indicates that the DAC could be used to drive the fans, but in our
* example board (Arima HDAMA) it isn't connected to the fans at all.
*/
#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val) * 255) + 500) / 2500), 0, 255))
#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
#define DAC_FROM_REG(val) (((val) * 2500) / 255)

/*
Expand Down Expand Up @@ -933,7 +933,7 @@ static void fixup_fan_min(struct device *dev, int fan, int old_div)
return;

new_min = data->fan_min[fan] * old_div / new_div;
new_min = SENSORS_LIMIT(new_min, 1, 254);
new_min = clamp_val(new_min, 1, 254);
data->fan_min[fan] = new_min;
adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
}
Expand Down Expand Up @@ -1527,7 +1527,7 @@ static ssize_t set_auto_pwm_min(struct device *dev,
return err;

mutex_lock(&data->update_lock);
data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
data->pwm1.auto_pwm_min = clamp_val(val, 0, 255);
if (data->pwm1.enable == 2) { /* apply immediately */
data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Expand Down
12 changes: 6 additions & 6 deletions drivers/hwmon/adm1031.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ adm1031_write_value(struct i2c_client *client, u8 reg, unsigned int value)
static int FAN_TO_REG(int reg, int div)
{
int tmp;
tmp = FAN_FROM_REG(SENSORS_LIMIT(reg, 0, 65535), div);
tmp = FAN_FROM_REG(clamp_val(reg, 0, 65535), div);
return tmp > 255 ? 255 : tmp;
}

#define FAN_DIV_FROM_REG(reg) (1<<(((reg)&0xc0)>>6))

#define PWM_TO_REG(val) (SENSORS_LIMIT((val), 0, 255) >> 4)
#define PWM_TO_REG(val) (clamp_val((val), 0, 255) >> 4)
#define PWM_FROM_REG(val) ((val) << 4)

#define FAN_CHAN_FROM_REG(reg) (((reg) >> 5) & 7)
Expand Down Expand Up @@ -675,7 +675,7 @@ static ssize_t set_temp_offset(struct device *dev,
if (ret)
return ret;

val = SENSORS_LIMIT(val, -15000, 15000);
val = clamp_val(val, -15000, 15000);
mutex_lock(&data->update_lock);
data->temp_offset[nr] = TEMP_OFFSET_TO_REG(val);
adm1031_write_value(client, ADM1031_REG_TEMP_OFFSET(nr),
Expand All @@ -696,7 +696,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
if (ret)
return ret;

val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
val = clamp_val(val, -55000, nr == 0 ? 127750 : 127875);
mutex_lock(&data->update_lock);
data->temp_min[nr] = TEMP_TO_REG(val);
adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
Expand All @@ -717,7 +717,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
if (ret)
return ret;

val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
val = clamp_val(val, -55000, nr == 0 ? 127750 : 127875);
mutex_lock(&data->update_lock);
data->temp_max[nr] = TEMP_TO_REG(val);
adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
Expand All @@ -738,7 +738,7 @@ static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
if (ret)
return ret;

val = SENSORS_LIMIT(val, -55000, nr == 0 ? 127750 : 127875);
val = clamp_val(val, -55000, nr == 0 ? 127750 : 127875);
mutex_lock(&data->update_lock);
data->temp_crit[nr] = TEMP_TO_REG(val);
adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
Expand Down
6 changes: 3 additions & 3 deletions drivers/hwmon/adm9240.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)

static inline u8 IN_TO_REG(unsigned long val, int n)
{
return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
}

/* temperature range: -40..125, 127 disables temperature alarm */
static inline s8 TEMP_TO_REG(long val)
{
return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
return clamp_val(SCALE(val, 1, 1000), -40, 127);
}

/* two fans, each with low fan speed limit */
Expand All @@ -122,7 +122,7 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
/* analog out 0..1250mV */
static inline u8 AOUT_TO_REG(unsigned long val)
{
return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
return clamp_val(SCALE(val, 255, 1250), 0, 255);
}

static inline unsigned int AOUT_FROM_REG(u8 reg)
Expand Down
6 changes: 3 additions & 3 deletions drivers/hwmon/ads7828.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ static int ads7828_probe(struct i2c_client *client,

/* Bound Vref with min/max values if it was provided */
if (data->vref_mv)
data->vref_mv = SENSORS_LIMIT(data->vref_mv,
ADS7828_EXT_VREF_MV_MIN,
ADS7828_EXT_VREF_MV_MAX);
data->vref_mv = clamp_val(data->vref_mv,
ADS7828_EXT_VREF_MV_MIN,
ADS7828_EXT_VREF_MV_MAX);
else
data->vref_mv = ADS7828_INT_VREF_MV;

Expand Down
10 changes: 5 additions & 5 deletions drivers/hwmon/adt7410.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ static struct adt7410_data *adt7410_update_device(struct device *dev)

static s16 ADT7410_TEMP_TO_REG(long temp)
{
return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, ADT7410_TEMP_MIN,
ADT7410_TEMP_MAX) * 128, 1000);
return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7410_TEMP_MIN,
ADT7410_TEMP_MAX) * 128, 1000);
}

static int ADT7410_REG_TO_TEMP(struct adt7410_data *data, s16 reg)
Expand Down Expand Up @@ -269,9 +269,9 @@ static ssize_t adt7410_set_t_hyst(struct device *dev,
return ret;
/* convert absolute hysteresis value to a 4 bit delta value */
limit = ADT7410_REG_TO_TEMP(data, data->temp[1]);
hyst = SENSORS_LIMIT(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
data->hyst = SENSORS_LIMIT(DIV_ROUND_CLOSEST(limit - hyst, 1000),
0, ADT7410_T_HYST_MASK);
hyst = clamp_val(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000), 0,
ADT7410_T_HYST_MASK);
ret = i2c_smbus_write_byte_data(client, ADT7410_T_HYST, data->hyst);
if (ret)
return ret;
Expand Down
20 changes: 10 additions & 10 deletions drivers/hwmon/adt7462.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ static ssize_t set_temp_min(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->temp_min[attr->index] = temp;
Expand Down Expand Up @@ -874,7 +874,7 @@ static ssize_t set_temp_max(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->temp_max[attr->index] = temp;
Expand Down Expand Up @@ -939,7 +939,7 @@ static ssize_t set_volt_max(struct device *dev,

temp *= 1000; /* convert mV to uV */
temp = DIV_ROUND_CLOSEST(temp, x);
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->volt_max[attr->index] = temp;
Expand Down Expand Up @@ -981,7 +981,7 @@ static ssize_t set_volt_min(struct device *dev,

temp *= 1000; /* convert mV to uV */
temp = DIV_ROUND_CLOSEST(temp, x);
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->volt_min[attr->index] = temp;
Expand Down Expand Up @@ -1071,7 +1071,7 @@ static ssize_t set_fan_min(struct device *dev,

temp = FAN_RPM_TO_PERIOD(temp);
temp >>= 8;
temp = SENSORS_LIMIT(temp, 1, 255);
temp = clamp_val(temp, 1, 255);

mutex_lock(&data->lock);
data->fan_min[attr->index] = temp;
Expand Down Expand Up @@ -1149,7 +1149,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm[attr->index] = temp;
Expand Down Expand Up @@ -1179,7 +1179,7 @@ static ssize_t set_pwm_max(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_max = temp;
Expand Down Expand Up @@ -1211,7 +1211,7 @@ static ssize_t set_pwm_min(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_min[attr->index] = temp;
Expand Down Expand Up @@ -1246,7 +1246,7 @@ static ssize_t set_pwm_hyst(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000);
temp = SENSORS_LIMIT(temp, 0, 15);
temp = clamp_val(temp, 0, 15);

/* package things up */
temp &= ADT7462_PWM_HYST_MASK;
Expand Down Expand Up @@ -1333,7 +1333,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_tmin[attr->index] = temp;
Expand Down
20 changes: 10 additions & 10 deletions drivers/hwmon/adt7470.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ static ssize_t set_auto_update_interval(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 60000);
temp = clamp_val(temp, 0, 60000);

mutex_lock(&data->lock);
data->auto_update_interval = temp;
Expand Down Expand Up @@ -481,7 +481,7 @@ static ssize_t set_num_temp_sensors(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, -1, 10);
temp = clamp_val(temp, -1, 10);

mutex_lock(&data->lock);
data->num_temp_sensors = temp;
Expand Down Expand Up @@ -515,7 +515,7 @@ static ssize_t set_temp_min(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000);
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->temp_min[attr->index] = temp;
Expand Down Expand Up @@ -549,7 +549,7 @@ static ssize_t set_temp_max(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000);
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->temp_max[attr->index] = temp;
Expand Down Expand Up @@ -604,7 +604,7 @@ static ssize_t set_fan_max(struct device *dev,
return -EINVAL;

temp = FAN_RPM_TO_PERIOD(temp);
temp = SENSORS_LIMIT(temp, 1, 65534);
temp = clamp_val(temp, 1, 65534);

mutex_lock(&data->lock);
data->fan_max[attr->index] = temp;
Expand Down Expand Up @@ -641,7 +641,7 @@ static ssize_t set_fan_min(struct device *dev,
return -EINVAL;

temp = FAN_RPM_TO_PERIOD(temp);
temp = SENSORS_LIMIT(temp, 1, 65534);
temp = clamp_val(temp, 1, 65534);

mutex_lock(&data->lock);
data->fan_min[attr->index] = temp;
Expand Down Expand Up @@ -717,7 +717,7 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm[attr->index] = temp;
Expand Down Expand Up @@ -749,7 +749,7 @@ static ssize_t set_pwm_max(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_max[attr->index] = temp;
Expand Down Expand Up @@ -782,7 +782,7 @@ static ssize_t set_pwm_min(struct device *dev,
if (kstrtol(buf, 10, &temp))
return -EINVAL;

temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_min[attr->index] = temp;
Expand Down Expand Up @@ -826,7 +826,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
return -EINVAL;

temp = DIV_ROUND_CLOSEST(temp, 1000);
temp = SENSORS_LIMIT(temp, 0, 255);
temp = clamp_val(temp, 0, 255);

mutex_lock(&data->lock);
data->pwm_tmin[attr->index] = temp;
Expand Down
Loading

0 comments on commit 2a844c1

Please sign in to comment.