aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/hwmon/adm1026.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/drivers/hwmon/adm1026.c b/drivers/hwmon/adm1026.c
index e67b9a50ac7c..b2a5d9e5c590 100644
--- a/drivers/hwmon/adm1026.c
+++ b/drivers/hwmon/adm1026.c
@@ -197,8 +197,9 @@ static int adm1026_scaling[] = { /* .001 Volts */
197 }; 197 };
198#define NEG12_OFFSET 16000 198#define NEG12_OFFSET 16000
199#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from)) 199#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
200#define INS_TO_REG(n, val) (clamp_val(SCALE(val, adm1026_scaling[n], 192),\ 200#define INS_TO_REG(n, val) \
201 0, 255)) 201 SCALE(clamp_val(val, 0, 255 * adm1026_scaling[n] / 192), \
202 adm1026_scaling[n], 192)
202#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n])) 203#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
203 204
204/* 205/*
@@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
215#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0) 216#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
216 217
217/* Temperature is reported in 1 degC increments */ 218/* Temperature is reported in 1 degC increments */
218#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \ 219#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
219 / 1000, -127, 127)) 220 1000)
220#define TEMP_FROM_REG(val) ((val) * 1000) 221#define TEMP_FROM_REG(val) ((val) * 1000)
221#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \ 222#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
222 / 1000, -127, 127)) 223 1000)
223#define OFFSET_FROM_REG(val) ((val) * 1000) 224#define OFFSET_FROM_REG(val) ((val) * 1000)
224 225
225#define PWM_TO_REG(val) (clamp_val(val, 0, 255)) 226#define PWM_TO_REG(val) (clamp_val(val, 0, 255))
@@ -233,7 +234,8 @@ static int adm1026_scaling[] = { /* .001 Volts */
233 * indicates that the DAC could be used to drive the fans, but in our 234 * indicates that the DAC could be used to drive the fans, but in our
234 * example board (Arima HDAMA) it isn't connected to the fans at all. 235 * example board (Arima HDAMA) it isn't connected to the fans at all.
235 */ 236 */
236#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255)) 237#define DAC_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 2500) * 255, \
238 2500)
237#define DAC_FROM_REG(val) (((val) * 2500) / 255) 239#define DAC_FROM_REG(val) (((val) * 2500) / 255)
238 240
239/* 241/*
@@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
593 return err; 595 return err;
594 596
595 mutex_lock(&data->update_lock); 597 mutex_lock(&data->update_lock);
596 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET); 598 data->in_min[16] = INS_TO_REG(16,
599 clamp_val(val, INT_MIN,
600 INT_MAX - NEG12_OFFSET) +
601 NEG12_OFFSET);
597 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]); 602 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
598 mutex_unlock(&data->update_lock); 603 mutex_unlock(&data->update_lock);
599 return count; 604 return count;
@@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
618 return err; 623 return err;
619 624
620 mutex_lock(&data->update_lock); 625 mutex_lock(&data->update_lock);
621 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET); 626 data->in_max[16] = INS_TO_REG(16,
627 clamp_val(val, INT_MIN,
628 INT_MAX - NEG12_OFFSET) +
629 NEG12_OFFSET);
622 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]); 630 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
623 mutex_unlock(&data->update_lock); 631 mutex_unlock(&data->update_lock);
624 return count; 632 return count;