Skip to content

Commit 7716d08

Browse files
javiercarrascocruzjic23
authored andcommitted
iio: gts-helper: add helpers to ease searches of gain_sel and new_gain
This helper functions reduce the burden in the drivers that want to fetch a gain and time selector for a given scale or a new optimal gain. The former is currently achieved by calling iio_gts_find_gain_sel_for_scale_using_time() for the current time selector, and then iterating over the rest of time selectors if the gain selector was not found. The latter requires a combination of multiple iio-gts helpers to find the new gain, look for an optimal gain if there was no exact match, and set a minimum gain if the optimal gain is not in the range of available gains. Provide simpler workflows by means of functions that address common patterns in the users of the iio-gts helpers. Acked-by: Matti Vaittinen <[email protected]> Signed-off-by: Javier Carrasco <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 627f3c4 commit 7716d08

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

drivers/iio/industrialio-gts-helper.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,41 @@ int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel
915915
}
916916
EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_sel_for_scale_using_time, "IIO_GTS_HELPER");
917917

918+
/**
919+
* iio_gts_find_gain_time_sel_for_scale - Fetch gain and time selectors for scale
920+
* @gts: Gain time scale descriptor
921+
* @scale_int: Integral part of the scale (typically val1)
922+
* @scale_nano: Fractional part of the scale (nano or ppb)
923+
* @gain_sel: Pointer to value where gain selector is stored.
924+
* @time_sel: Pointer to value where time selector is stored.
925+
*
926+
* Wrapper around iio_gts_find_gain_for_scale_using_time() to fetch the
927+
* gain and time selectors for a given scale.
928+
*
929+
* Return: 0 on success and -EINVAL on error.
930+
*/
931+
int iio_gts_find_gain_time_sel_for_scale(struct iio_gts *gts, int scale_int,
932+
int scale_nano, int *gain_sel,
933+
int *time_sel)
934+
{
935+
int i, ret;
936+
937+
for (i = 0; i < gts->num_itime; i++) {
938+
*time_sel = gts->itime_table[i].sel;
939+
ret = iio_gts_find_gain_sel_for_scale_using_time(gts, *time_sel,
940+
scale_int,
941+
scale_nano,
942+
gain_sel);
943+
if (ret)
944+
continue;
945+
946+
return 0;
947+
}
948+
949+
return -EINVAL;
950+
}
951+
EXPORT_SYMBOL_NS_GPL(iio_gts_find_gain_time_sel_for_scale, "IIO_GTS_HELPER");
952+
918953
static int iio_gts_get_total_gain(struct iio_gts *gts, int gain, int time)
919954
{
920955
const struct iio_itime_sel_mul *itime;
@@ -1086,6 +1121,48 @@ int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain,
10861121
}
10871122
EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_old_gain_time, "IIO_GTS_HELPER");
10881123

1124+
/**
1125+
* iio_gts_find_new_gain_by_gain_time_min - compensate for time change
1126+
* @gts: Gain time scale descriptor
1127+
* @old_gain: Previously set gain
1128+
* @old_time: Selector corresponding previously set time
1129+
* @new_time: Selector corresponding new time to be set
1130+
* @new_gain: Pointer to value where new gain is to be written
1131+
* @in_range: Indicate if the @new_gain was in the range of
1132+
* supported gains.
1133+
*
1134+
* Wrapper around iio_gts_find_new_gain_by_old_gain_time() that tries to
1135+
* set an optimal value if no exact match was found, defaulting to the
1136+
* minimum gain to avoid saturations if the optimal value is not in the
1137+
* range of supported gains.
1138+
*
1139+
* Return: 0 on success and a negative value if no gain was found.
1140+
*/
1141+
int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain,
1142+
int old_time, int new_time,
1143+
int *new_gain, bool *in_range)
1144+
{
1145+
int ret;
1146+
1147+
*in_range = true;
1148+
ret = iio_gts_find_new_gain_by_old_gain_time(gts, old_gain, old_time,
1149+
new_time, new_gain);
1150+
if (*new_gain < 0)
1151+
return -EINVAL;
1152+
1153+
if (ret) {
1154+
*new_gain = iio_find_closest_gain_low(gts, *new_gain, in_range);
1155+
if (*new_gain < 0) {
1156+
*new_gain = iio_gts_get_min_gain(gts);
1157+
if (*new_gain < 0)
1158+
return -EINVAL;
1159+
}
1160+
}
1161+
1162+
return 0;
1163+
}
1164+
EXPORT_SYMBOL_NS_GPL(iio_gts_find_new_gain_by_gain_time_min, "IIO_GTS_HELPER");
1165+
10891166
MODULE_LICENSE("GPL");
10901167
MODULE_AUTHOR("Matti Vaittinen <[email protected]>");
10911168
MODULE_DESCRIPTION("IIO light sensor gain-time-scale helpers");

include/linux/iio/iio-gts-helper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain,
188188
int iio_gts_find_gain_sel_for_scale_using_time(struct iio_gts *gts, int time_sel,
189189
int scale_int, int scale_nano,
190190
int *gain_sel);
191+
int iio_gts_find_gain_time_sel_for_scale(struct iio_gts *gts, int scale_int,
192+
int scale_nano, int *gain_sel,
193+
int *time_sel);
191194
int iio_gts_get_scale(struct iio_gts *gts, int gain, int time, int *scale_int,
192195
int *scale_nano);
193196
int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
@@ -196,6 +199,9 @@ int iio_gts_find_new_gain_sel_by_old_gain_time(struct iio_gts *gts,
196199
int iio_gts_find_new_gain_by_old_gain_time(struct iio_gts *gts, int old_gain,
197200
int old_time, int new_time,
198201
int *new_gain);
202+
int iio_gts_find_new_gain_by_gain_time_min(struct iio_gts *gts, int old_gain,
203+
int old_time, int new_time,
204+
int *new_gain, bool *in_range);
199205
int iio_gts_avail_times(struct iio_gts *gts, const int **vals, int *type,
200206
int *length);
201207
int iio_gts_all_avail_scales(struct iio_gts *gts, const int **vals, int *type,

0 commit comments

Comments
 (0)