Lines Matching refs:gain
2 /* gain-time-scale conversion helpers for IIO light sensors
22 * iio_gts_get_gain - Convert scale to total gain
24 * Internal helper for converting scale to total gain.
29 * @scale: Linearized scale to compute the gain for.
31 * Return: (floored) gain corresponding to the scale. -EINVAL if scale
45 * gain_get_scale_fraction - get the gain or time based on scale and known one
50 * @scale: Linearized scale to compute the gain/time for.
51 * @known: Either integration time or gain depending on which one is known
52 * @unknown: Pointer to variable where the computed gain/time is stored
54 * Internal helper for computing unknown fraction of total gain.
55 * Compute either gain or time based on scale and either the gain or time
71 /* We require total gain to be exact multiple of known * unknown */
114 * iio_gts_total_gain_to_scale - convert gain to scale
116 * @total_gain: the gain to be converted
120 * Convert the total gain value to scale. NOTE: This does not separate gain
121 * generated by HW-gain or integration time. It is up to caller to decide what
122 * part of the total gain is due to integration time and what due to HW-gain.
264 * originally given gain and time tables. When both time and gain tables are
302 per_time_gains[i][j] = gts->hwgain_table[j].gain *
422 * given gain and given time tables.
424 * When both time and gain tables are
481 * given gain and given time tables.
483 * When both time and gain tables are given this results:
520 if (g->sel < 0 || g->gain <= 0)
547 int gain, mul, res;
549 gain = gts->hwgain_table[g].gain;
552 if (check_mul_overflow(gain, mul, &res))
583 * devm_iio_init_iio_gts - Initialize the gain-time-scale helper
589 * @num_gain: number of gains in the gain table
599 * Initialize the gain-time-scale helper for use. Note, gains, times, selectors
601 * checking. The total gain (maximum gain * maximum time multiplier) must not
703 * iio_gts_find_sel_by_gain - find selector corresponding to a HW-gain
705 * @gain: HW-gain for which matching selector is searched for
707 * Return: a selector matching given HW-gain or -EINVAL if selector was
710 int iio_gts_find_sel_by_gain(struct iio_gts *gts, int gain)
715 if (gts->hwgain_table[i].gain == gain)
723 * iio_gts_find_gain_by_sel - find HW-gain corresponding to a selector
725 * @sel: selector for which matching HW-gain is searched for
727 * Return: a HW-gain matching given selector or -EINVAL if HW-gain was not
736 return gts->hwgain_table[i].gain;
743 * iio_gts_get_min_gain - find smallest valid HW-gain
746 * Return: The smallest HW-gain -EINVAL if no HW-gains were in the tables.
753 int gain = gts->hwgain_table[i].gain;
756 min = gain;
758 min = min(min, gain);
766 * iio_find_closest_gain_low - Find the closest lower matching gain
768 * @gain: HW-gain for which the closest match is searched
769 * @in_range: indicate if the @gain was actually in the range of
772 * Search for closest supported gain that is lower than or equal to the
773 * gain given as a parameter. This is usable for drivers which do not require
774 * user to request exact matching gain but rather for rounding to a supported
775 * gain value which is equal or lower (setting lower gain is typical for
778 * Return: The closest matching supported gain or -EINVAL if @gain
779 * was smaller than the smallest supported gain.
781 int iio_find_closest_gain_low(struct iio_gts *gts, int gain, bool *in_range)
789 if (gain == gts->hwgain_table[i].gain) {
791 return gain;
794 if (gain > gts->hwgain_table[i].gain) {
796 diff = gain - gts->hwgain_table[i].gain;
799 int tmp = gain - gts->hwgain_table[i].gain;
808 * We found valid HW-gain which is greater than
810 * will have found an in-range gain
815 /* The requested gain was smaller than anything we support */
822 return gts->hwgain_table[best].gain;
839 * iio_gts_find_gain_for_scale_using_time - Find gain by time and scale
841 * @time_sel: Integration time selector corresponding to the time gain is
845 * @gain: Pointer to value where gain is stored.
847 * In some cases the light sensors may want to find a gain setting which
849 * gain and time tables may use this helper to retrieve the gain.
851 * Return: 0 on success. -EINVAL if gain matching the parameters is not
856 int *gain)
871 ret = gain_get_scale_fraction(gts->max_scale, scale_linear, mul, gain);
875 if (!iio_gts_valid_gain(gts, *gain))
882 * iio_gts_find_gain_sel_for_scale_using_time - Fetch gain selector.
884 * @time_sel: Integration time selector corresponding to the time gain is
888 * @gain_sel: Pointer to value where gain selector is stored.
896 int gain, ret;
899 scale_nano, &gain);
903 ret = iio_gts_find_sel_by_gain(gts, gain);
913 static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
917 if (!iio_gts_valid_gain(gts, gain))
921 return gain;
927 return gain * itime->mul;
930 static int iio_gts_get_scale_linear(struct iio_gts *gts, int gain, int time,
936 total_gain = iio_gts_get_total_gain(gts, gain, time);
950 * iio_gts_get_scale - get scale based on integration time and HW-gain
952 * @gain: HW-gain for which the scale is computed
957 * Compute scale matching the integration time and HW-gain given as parameter.
961 int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int,
967 ret = iio_gts_get_scale_linear(gts, gain, time, &lin_scale);
978 * @old_gain: Previously set gain
981 * @new_gain: Pointer to value where new gain is to be written
984 * time (for a light sensor) by also updating the (HW)gain. This helper computes
985 * new gain value to maintain the scale with new integration time.
987 * Return: 0 if an exactly matching supported new gain was found. When a
989 * positive value. The negative value means that no gain could be computed.
990 * Positive value will be the "best possible new gain there could be". There
991 * can be two reasons why finding the "best possible" new gain is not deemed
993 * gain required to maintain the scale would not be an integer. In this case,
994 * the "best possible" new gain will be a floored optimal gain, which may or
1035 * @old_gain: Previously set gain
1038 * @new_gain: Pointer to value where new gain is to be written
1041 * time (for a light sensor) by also updating the (HW)gain. This helper computes
1042 * new gain value to maintain the scale with new integration time.
1044 * Return: 0 if an exactly matching supported new gain was found. When a
1046 * positive value. The negative value means that no gain could be computed.
1047 * Positive value will be the "best possible new gain there could be". There
1048 * can be two reasons why finding the "best possible" new gain is not deemed
1050 * gain required to maintain the scale would not be an integer. In this case,
1051 * the "best possible" new gain will be a floored optimal gain, which may or
1086 MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");