linear_range.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2020 ROHM Semiconductors */
  3. #ifndef LINEAR_RANGE_H
  4. #define LINEAR_RANGE_H
  5. #include <linux/types.h>
  6. /**
  7. * struct linear_range - table of selector - value pairs
  8. *
  9. * Define a lookup-table for range of values. Intended to help when looking
  10. * for a register value matching certaing physical measure (like voltage).
  11. * Usable when increment of one in register always results a constant increment
  12. * of the physical measure (like voltage).
  13. *
  14. * @min: Lowest value in range
  15. * @min_sel: Lowest selector for range
  16. * @max_sel: Highest selector for range
  17. * @step: Value step size
  18. */
  19. struct linear_range {
  20. unsigned int min;
  21. unsigned int min_sel;
  22. unsigned int max_sel;
  23. unsigned int step;
  24. };
  25. #define LINEAR_RANGE(_min, _min_sel, _max_sel, _step) \
  26. { \
  27. .min = _min, \
  28. .min_sel = _min_sel, \
  29. .max_sel = _max_sel, \
  30. .step = _step, \
  31. }
  32. #define LINEAR_RANGE_IDX(_idx, _min, _min_sel, _max_sel, _step) \
  33. [_idx] = LINEAR_RANGE(_min, _min_sel, _max_sel, _step)
  34. unsigned int linear_range_values_in_range(const struct linear_range *r);
  35. unsigned int linear_range_values_in_range_array(const struct linear_range *r,
  36. int ranges);
  37. unsigned int linear_range_get_max_value(const struct linear_range *r);
  38. int linear_range_get_value(const struct linear_range *r, unsigned int selector,
  39. unsigned int *val);
  40. int linear_range_get_value_array(const struct linear_range *r, int ranges,
  41. unsigned int selector, unsigned int *val);
  42. int linear_range_get_selector_low(const struct linear_range *r,
  43. unsigned int val, unsigned int *selector,
  44. bool *found);
  45. int linear_range_get_selector_high(const struct linear_range *r,
  46. unsigned int val, unsigned int *selector,
  47. bool *found);
  48. void linear_range_get_selector_within(const struct linear_range *r,
  49. unsigned int val, unsigned int *selector);
  50. int linear_range_get_selector_low_array(const struct linear_range *r,
  51. int ranges, unsigned int val,
  52. unsigned int *selector, bool *found);
  53. #endif