Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (lm85) use find_closest() in x_TO_REG() functions

Replace RANGE_TO_REG() and FREQ_TO_REG() implementations with
calls to find_closest().

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Bartosz Golaszewski and committed by
Linus Torvalds
0f3721c5 d38df34e

+8 -18
+8 -18
drivers/hwmon/lm85.c
··· 34 34 #include <linux/hwmon-sysfs.h> 35 35 #include <linux/err.h> 36 36 #include <linux/mutex.h> 37 + #include <linux/util_macros.h> 37 38 38 39 /* Addresses to scan */ 39 40 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END }; ··· 191 190 192 191 static int RANGE_TO_REG(long range) 193 192 { 194 - int i; 195 - 196 - /* Find the closest match */ 197 - for (i = 0; i < 15; ++i) { 198 - if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2) 199 - break; 200 - } 201 - 202 - return i; 193 + return find_closest(range, lm85_range_map, ARRAY_SIZE(lm85_range_map)); 203 194 } 204 195 #define RANGE_FROM_REG(val) lm85_range_map[(val) & 0x0f] 205 196 ··· 202 209 static const int adm1027_freq_map[8] = { /* 1 Hz */ 203 210 11, 15, 22, 29, 35, 44, 59, 88 204 211 }; 212 + #define FREQ_MAP_LEN 8 205 213 206 - static int FREQ_TO_REG(const int *map, unsigned long freq) 214 + static int FREQ_TO_REG(const int *map, 215 + unsigned int map_size, unsigned long freq) 207 216 { 208 - int i; 209 - 210 - /* Find the closest match */ 211 - for (i = 0; i < 7; ++i) 212 - if (freq <= (map[i] + map[i + 1]) / 2) 213 - break; 214 - return i; 217 + return find_closest(freq, map, map_size); 215 218 } 216 219 217 220 static int FREQ_FROM_REG(const int *map, u8 reg) ··· 817 828 data->cfg5 &= ~ADT7468_HFPWM; 818 829 lm85_write_value(client, ADT7468_REG_CFG5, data->cfg5); 819 830 } else { /* Low freq. mode */ 820 - data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, val); 831 + data->pwm_freq[nr] = FREQ_TO_REG(data->freq_map, 832 + FREQ_MAP_LEN, val); 821 833 lm85_write_value(client, LM85_REG_AFAN_RANGE(nr), 822 834 (data->zone[nr].range << 4) 823 835 | data->pwm_freq[nr]);