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

hwmon: (ina2xx) replace ina226_avg_bits() with find_closest()

Use find_closest() to locate the closest average in ina226_avg_tab.

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
d38df34e f2027543

+3 -14
+3 -14
drivers/hwmon/ina2xx.c
··· 36 36 #include <linux/jiffies.h> 37 37 #include <linux/of.h> 38 38 #include <linux/delay.h> 39 + #include <linux/util_macros.h> 39 40 40 41 #include <linux/platform_data/ina2xx.h> 41 42 ··· 142 141 */ 143 142 static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 }; 144 143 145 - static int ina226_avg_bits(int avg) 146 - { 147 - int i; 148 - 149 - /* Get the closest average from the tab. */ 150 - for (i = 0; i < ARRAY_SIZE(ina226_avg_tab) - 1; i++) { 151 - if (avg <= (ina226_avg_tab[i] + ina226_avg_tab[i + 1]) / 2) 152 - break; 153 - } 154 - 155 - return i; /* Return 0b0111 for values greater than 1024. */ 156 - } 157 - 158 144 static int ina226_reg_to_interval(u16 config) 159 145 { 160 146 int avg = ina226_avg_tab[INA226_READ_AVG(config)]; ··· 159 171 160 172 avg = DIV_ROUND_CLOSEST(interval * 1000, 161 173 INA226_TOTAL_CONV_TIME_DEFAULT); 162 - avg_bits = ina226_avg_bits(avg); 174 + avg_bits = find_closest(avg, ina226_avg_tab, 175 + ARRAY_SIZE(ina226_avg_tab)); 163 176 164 177 return (config & ~INA226_AVG_RD_MASK) | INA226_SHIFT_AVG(avg_bits); 165 178 }