summaryrefslogtreecommitdiffstats
path: root/lib/linear_ranges.c
diff options
context:
space:
mode:
authorGene Chen <gene_chen@richtek.com>2021-07-19 05:39:12 +0200
committerSebastian Reichel <sre@kernel.org>2021-08-13 18:37:38 +0200
commite12ef7bf34113f55c9bf444a680a15b6daf76f26 (patch)
tree59f202c7ba4e2cb43fbfa08663758908a4de23cf /lib/linear_ranges.c
parentLinux 5.14-rc1 (diff)
downloadlinux-e12ef7bf34113f55c9bf444a680a15b6daf76f26.tar.xz
linux-e12ef7bf34113f55c9bf444a680a15b6daf76f26.zip
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>
Diffstat (limited to 'lib/linear_ranges.c')
-rw-r--r--lib/linear_ranges.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/linear_ranges.c b/lib/linear_ranges.c
index ced5c15d3f04..a1a7dfa881de 100644
--- a/lib/linear_ranges.c
+++ b/lib/linear_ranges.c
@@ -241,5 +241,36 @@ int linear_range_get_selector_high(const struct linear_range *r,
}
EXPORT_SYMBOL_GPL(linear_range_get_selector_high);
+/**
+ * linear_range_get_selector_within - return linear range selector for value
+ * @r: pointer to linear range where selector is looked from
+ * @val: value for which the selector is searched
+ * @selector: address where found selector value is updated
+ *
+ * Return selector for which range value is closest match for given
+ * input value. Value is matching if it is equal or lower than given
+ * value. But return maximum selector if given value is higher than
+ * maximum value.
+ */
+void linear_range_get_selector_within(const struct linear_range *r,
+ unsigned int val, unsigned int *selector)
+{
+ if (r->min > val) {
+ *selector = r->min_sel;
+ return;
+ }
+
+ if (linear_range_get_max_value(r) < val) {
+ *selector = r->max_sel;
+ return;
+ }
+
+ if (r->step == 0)
+ *selector = r->min_sel;
+ else
+ *selector = (val - r->min) / r->step + r->min_sel;
+}
+EXPORT_SYMBOL_GPL(linear_range_get_selector_within);
+
MODULE_DESCRIPTION("linear-ranges helper");
MODULE_LICENSE("GPL");