Lines Matching full: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
115 * @gts: Gain time scale descriptor
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.
141 * @gts: Gain time scale descriptor
308 gains[i][j] = gts->hwgain_table[j].gain *
366 * @gts: Gain time scale descriptor
369 * originally given gain and time tables. When both time and gain tables are
418 * @gts: Gain time scale descriptor
484 * @gts: Gain time scale descriptor
499 * @gts: Gain time scale descriptor
503 * given gain and given time tables.
505 * When both time and gain tables are
539 * @gts: Gain time scale descriptor
558 * @gts: Gain time scale descriptor
562 * given gain and given time tables.
564 * When both time and gain tables are given this results:
601 if (g->sel < 0 || g->gain <= 0)
628 int gain, mul, res;
630 gain = gts->hwgain_table[g].gain;
633 if (check_mul_overflow(gain, mul, &res))
664 * devm_iio_init_iio_gts - Initialize the gain-time-scale helper
670 * @num_gain: number of gains in the gain table
680 * Initialize the gain-time-scale helper for use. Note, gains, times, selectors
682 * checking. The total gain (maximum gain * maximum time multiplier) must not
705 * @gts: Gain time scale descriptor
728 * @gts: Gain time scale descriptor
762 * @gts: Gain time scale descriptor
784 * iio_gts_find_sel_by_gain - find selector corresponding to a HW-gain
785 * @gts: Gain time scale descriptor
786 * @gain: HW-gain for which matching selector is searched for
788 * Return: a selector matching given HW-gain or -EINVAL if selector was
791 int iio_gts_find_sel_by_gain(struct iio_gts *gts, int gain)
796 if (gts->hwgain_table[i].gain == gain)
804 * iio_gts_find_gain_by_sel - find HW-gain corresponding to a selector
805 * @gts: Gain time scale descriptor
806 * @sel: selector for which matching HW-gain is searched for
808 * Return: a HW-gain matching given selector or -EINVAL if HW-gain was not
817 return gts->hwgain_table[i].gain;
824 * iio_gts_get_min_gain - find smallest valid HW-gain
825 * @gts: Gain time scale descriptor
827 * Return: The smallest HW-gain -EINVAL if no HW-gains were in the tables.
834 int gain = gts->hwgain_table[i].gain;
837 min = gain;
839 min = min(min, gain);
847 * iio_find_closest_gain_low - Find the closest lower matching gain
848 * @gts: Gain time scale descriptor
849 * @gain: HW-gain for which the closest match is searched
850 * @in_range: indicate if the @gain was actually in the range of
853 * Search for closest supported gain that is lower than or equal to the
854 * gain given as a parameter. This is usable for drivers which do not require
855 * user to request exact matching gain but rather for rounding to a supported
856 * gain value which is equal or lower (setting lower gain is typical for
859 * Return: The closest matching supported gain or -EINVAL if @gain
860 * was smaller than the smallest supported gain.
862 int iio_find_closest_gain_low(struct iio_gts *gts, int gain, bool *in_range)
870 if (gain == gts->hwgain_table[i].gain) {
872 return gain;
875 if (gain > gts->hwgain_table[i].gain) {
877 diff = gain - gts->hwgain_table[i].gain;
880 int tmp = gain - gts->hwgain_table[i].gain;
889 * We found valid HW-gain which is greater than
891 * will have found an in-range gain
896 /* The requested gain was smaller than anything we support */
903 return gts->hwgain_table[best].gain;
920 * iio_gts_find_gain_for_scale_using_time - Find gain by time and scale
921 * @gts: Gain time scale descriptor
922 * @time_sel: Integration time selector corresponding to the time gain is
926 * @gain: Pointer to value where gain is stored.
928 * In some cases the light sensors may want to find a gain setting which
930 * gain and time tables may use this helper to retrieve the gain.
932 * Return: 0 on success. -EINVAL if gain matching the parameters is not
937 int *gain)
952 ret = gain_get_scale_fraction(gts->max_scale, scale_linear, mul, gain);
956 if (!iio_gts_valid_gain(gts, *gain))
963 * iio_gts_find_gain_sel_for_scale_using_time - Fetch gain selector.
964 * @gts: Gain time scale descriptor
965 * @time_sel: Integration time selector corresponding to the time gain is
969 * @gain_sel: Pointer to value where gain selector is stored.
977 int gain, ret;
980 scale_nano, &gain);
984 ret = iio_gts_find_sel_by_gain(gts, gain);
995 * iio_gts_find_gain_time_sel_for_scale - Fetch gain and time selectors for scale
996 * @gts: Gain time scale descriptor
999 * @gain_sel: Pointer to value where gain selector is stored.
1003 * gain and time selectors for a given scale.
1030 * iio_gts_get_total_gain - Fetch total gain for given HW-gain and time
1031 * @gts: Gain time scale descriptor
1032 * @gain: HW-gain for which the total gain is searched for
1033 * @time: Integration time for which the total gain is searched for
1035 * Return: total gain on success and -EINVAL on error.
1037 int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
1041 if (!iio_gts_valid_gain(gts, gain))
1045 return gain;
1051 return gain * itime->mul;
1055 static int iio_gts_get_scale_linear(struct iio_gts *gts, int gain, int time,
1061 total_gain = iio_gts_get_total_gain(gts, gain, time);
1075 * iio_gts_get_scale - get scale based on integration time and HW-gain
1076 * @gts: Gain time scale descriptor
1077 * @gain: HW-gain for which the scale is computed
1082 * Compute scale matching the integration time and HW-gain given as parameter.
1086 int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int,
1092 ret = iio_gts_get_scale_linear(gts, gain, time, &lin_scale);
1102 * @gts: Gain time scale descriptor
1103 * @old_gain: Previously set gain
1106 * @new_gain: Pointer to value where new gain is to be written
1109 * time (for a light sensor) by also updating the (HW)gain. This helper computes
1110 * new gain value to maintain the scale with new integration time.
1112 * Return: 0 if an exactly matching supported new gain was found. When a
1114 * positive value. The negative value means that no gain could be computed.
1115 * Positive value will be the "best possible new gain there could be". There
1116 * can be two reasons why finding the "best possible" new gain is not deemed
1118 * gain required to maintain the scale would not be an integer. In this case,
1119 * the "best possible" new gain will be a floored optimal gain, which may or
1159 * @gts: Gain time scale descriptor
1160 * @old_gain: Previously set gain
1163 * @new_gain: Pointer to value where new gain is to be written
1166 * time (for a light sensor) by also updating the (HW)gain. This helper computes
1167 * new gain value to maintain the scale with new integration time.
1169 * Return: 0 if an exactly matching supported new gain was found. When a
1171 * positive value. The negative value means that no gain could be computed.
1172 * Positive value will be the "best possible new gain there could be". There
1173 * can be two reasons why finding the "best possible" new gain is not deemed
1175 * gain required to maintain the scale would not be an integer. In this case,
1176 * the "best possible" new gain will be a floored optimal gain, which may or
1211 * @gts: Gain time scale descriptor
1212 * @old_gain: Previously set gain
1215 * @new_gain: Pointer to value where new gain is to be written
1221 * minimum gain to avoid saturations if the optimal value is not in the
1224 * Return: 0 on success and a negative value if no gain was found.
1253 MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");