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

lib: add linear range get selector within

Add linear range get selector within for choose closest selector
between minimum and maximum selector.

Signed-off-by: Gene Chen <gene_chen@richtek.com>
Reviewed-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Gene Chen and committed by
Sebastian Reichel
e12ef7bf e73f0f0e

+33
+2
include/linux/linear_range.h
··· 41 41 int linear_range_get_selector_high(const struct linear_range *r, 42 42 unsigned int val, unsigned int *selector, 43 43 bool *found); 44 + void linear_range_get_selector_within(const struct linear_range *r, 45 + unsigned int val, unsigned int *selector); 44 46 int linear_range_get_selector_low_array(const struct linear_range *r, 45 47 int ranges, unsigned int val, 46 48 unsigned int *selector, bool *found);
+31
lib/linear_ranges.c
··· 241 241 } 242 242 EXPORT_SYMBOL_GPL(linear_range_get_selector_high); 243 243 244 + /** 245 + * linear_range_get_selector_within - return linear range selector for value 246 + * @r: pointer to linear range where selector is looked from 247 + * @val: value for which the selector is searched 248 + * @selector: address where found selector value is updated 249 + * 250 + * Return selector for which range value is closest match for given 251 + * input value. Value is matching if it is equal or lower than given 252 + * value. But return maximum selector if given value is higher than 253 + * maximum value. 254 + */ 255 + void linear_range_get_selector_within(const struct linear_range *r, 256 + unsigned int val, unsigned int *selector) 257 + { 258 + if (r->min > val) { 259 + *selector = r->min_sel; 260 + return; 261 + } 262 + 263 + if (linear_range_get_max_value(r) < val) { 264 + *selector = r->max_sel; 265 + return; 266 + } 267 + 268 + if (r->step == 0) 269 + *selector = r->min_sel; 270 + else 271 + *selector = (val - r->min) / r->step + r->min_sel; 272 + } 273 + EXPORT_SYMBOL_GPL(linear_range_get_selector_within); 274 + 244 275 MODULE_DESCRIPTION("linear-ranges helper"); 245 276 MODULE_LICENSE("GPL");