]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
lib/linear_ranges: Add linear_range_get_selector_high_array
authorAmit Sunil Dhamne <amitsd@google.com>
Wed, 25 Mar 2026 22:22:25 +0000 (22:22 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 30 Mar 2026 14:58:33 +0000 (16:58 +0200)
Add a helper function to find the selector for a given value in a linear
range array. The selector should be such that the value it represents
should be higher or equal to the given value.

Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>
Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Acked-by: Mark Brown <broonie@kernel.org>
Link: https://patch.msgid.link/20260325-max77759-charger-v9-4-4486dd297adc@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/linear_range.h
lib/linear_ranges.c

index 2e4f4c3539c075a6caa69f6ae750a556f41fce5d..0f3037f1a94f58667ca7c5200dfda78afafe0cfd 100644 (file)
@@ -57,5 +57,8 @@ void linear_range_get_selector_within(const struct linear_range *r,
 int linear_range_get_selector_low_array(const struct linear_range *r,
                                        int ranges, unsigned int val,
                                        unsigned int *selector, bool *found);
+int linear_range_get_selector_high_array(const struct linear_range *r,
+                                        int ranges, unsigned int val,
+                                        unsigned int *selector, bool *found);
 
 #endif
index a1a7dfa881def894cc8cce8368cb6e9c21dcd6b1..c85583678f6b52a494f8c321ce2d307bdb6845ce 100644 (file)
@@ -241,6 +241,42 @@ int linear_range_get_selector_high(const struct linear_range *r,
 }
 EXPORT_SYMBOL_GPL(linear_range_get_selector_high);
 
+/**
+ * linear_range_get_selector_high_array - return linear range selector for value
+ * @r:         pointer to array of linear ranges where selector is looked from
+ * @ranges:    amount of ranges to scan from array
+ * @val:       value for which the selector is searched
+ * @selector:  address where found selector value is updated
+ * @found:     flag to indicate that given value was in the range
+ *
+ * Scan array of ranges for selector for which range value matches given
+ * input value. Value is matching if it is equal or higher than given value
+ * If given value is found to be in a range scanning is stopped and @found is
+ * set true. If a range with values greater than given value is found
+ * but the range min is being greater than given value, then the range's
+ * lowest selector is updated to @selector and scanning is stopped.
+ *
+ * Return: 0 on success, -EINVAL if range array is invalid or does not contain
+ * range with a value greater or equal to given value
+ */
+int linear_range_get_selector_high_array(const struct linear_range *r,
+                                        int ranges, unsigned int val,
+                                        unsigned int *selector, bool *found)
+{
+       int i;
+       int ret;
+
+       for (i = 0; i < ranges; i++) {
+               ret = linear_range_get_selector_high(&r[i], val, selector,
+                                                    found);
+               if (!ret)
+                       return 0;
+       }
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(linear_range_get_selector_high_array);
+
 /**
  * linear_range_get_selector_within - return linear range selector for value
  * @r:         pointer to linear range where selector is looked from