Skip to content

Commit e12ef7b

Browse files
Gene Chensre
authored andcommitted
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 <[email protected]> Reviewed-by: Matti Vaittinen <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent e73f0f0 commit e12ef7b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

include/linux/linear_range.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ int linear_range_get_selector_low(const struct linear_range *r,
4141
int linear_range_get_selector_high(const struct linear_range *r,
4242
unsigned int val, unsigned int *selector,
4343
bool *found);
44+
void linear_range_get_selector_within(const struct linear_range *r,
45+
unsigned int val, unsigned int *selector);
4446
int linear_range_get_selector_low_array(const struct linear_range *r,
4547
int ranges, unsigned int val,
4648
unsigned int *selector, bool *found);

lib/linear_ranges.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,36 @@ int linear_range_get_selector_high(const struct linear_range *r,
241241
}
242242
EXPORT_SYMBOL_GPL(linear_range_get_selector_high);
243243

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+
244275
MODULE_DESCRIPTION("linear-ranges helper");
245276
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)