hwmon: (lm85) Select the closest PWM
PATCH 9/10 hwmon: (lm85) Select the closest PWM
The LM85 and compatible chips only support 8 arbitrary PWM frequencies. The algorithm to pick one of them based on the user input is not optimum. Improve it to always pick the closest supported frequency.
Signed-off-by Jean Delvare <[email protected]>
---
The new algorithm is exactly the same as the one used in RANGE_TO_REG().
drivers/hwmon/lm85.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/hwmon/lm85.c +12 -4
@@ -202,10 +202,18 @@ static int FREQ_TO_REG(int freq)if (freq >= lm85_freq_map[7 ])return 7 ; -for (i =0 ; i <7 ; ++i) -if (freq <= lm85_freq_map[i]) -break ; -return i; + +/* Find the closest match */ +for (i =6 ; i >=0 ; --i) { +if (freq >= lm85_freq_map[i]) { +if ((lm85_freq_map[i +1 ] - freq) < + (freq - lm85_freq_map[i])) +return i +1 ; +return i; + } + } + +return 0 ; }#define FREQ_FROM_REG (val) lm85_freq_map[(val) & 0x07]
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors